Skip to content

Commit 6f7e266

Browse files
committed
docs(sidebar): update sidebar items and add new documentation pages
- Add new sidebar items for message rendering and middleware - Create new documentation pages for message read and message render - Update existing documentation to include new features and clarifications
1 parent b287f78 commit 6f7e266

File tree

4 files changed

+162
-5
lines changed

4 files changed

+162
-5
lines changed

docs/.vitepress/config.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,10 @@ function sidebarDevelopment(): DefaultTheme.SidebarItem[] {
499499
text: '消息读取',
500500
link: '/development/connect-to-core-services/message-read',
501501
},
502+
{
503+
text: '消息渲染',
504+
link: '/development/connect-to-core-services/message-render',
505+
},
502506
],
503507
},
504508
{
@@ -509,15 +513,15 @@ function sidebarDevelopment(): DefaultTheme.SidebarItem[] {
509513
link: '/development/api-reference/chatluna-service',
510514
},
511515
{
512-
text: 'ChatLuna 插件(ChatLuna Plugin)',
516+
text: 'ChatLuna 插件 (ChatLuna Plugin)',
513517
link: '/development/api-reference/chatluna-plugin',
514518
},
515519
{
516-
text: 'ChatLuna 事件(ChatLuna Events)',
520+
text: 'ChatLuna 事件 (ChatLuna Events)',
517521
link: '/development/api-reference/chatluna-events',
518522
},
519523
{
520-
text: 'ChatLuna 大语言模型核心 (LLM Core)',
524+
text: 'ChatLuna 大语言模型核心',
521525
collapsed: true,
522526
items: [
523527
{
@@ -539,7 +543,25 @@ function sidebarDevelopment(): DefaultTheme.SidebarItem[] {
539543
],
540544
},
541545
{
542-
text: 'ChatLuna 辅助工具 (ChatLuna Utils)',
546+
text: 'ChatLuna 中间件相关',
547+
collapsed: true,
548+
items: [
549+
{
550+
text: '消息 (ChatLuna Message)',
551+
link: '/development/api-reference/middleware/message',
552+
},
553+
{
554+
text: '消息渲染器 (Message Renderer)',
555+
link: '/development/api-reference/middleware/message-renderer',
556+
},
557+
{
558+
text: '消息转换器 (Message Transformer)',
559+
link: '/development/api-reference/middleware/message-transformer',
560+
},
561+
],
562+
},
563+
{
564+
text: 'ChatLuna 辅助工具',
543565
collapsed: true,
544566
items: [
545567
{
@@ -554,6 +576,10 @@ function sidebarDevelopment(): DefaultTheme.SidebarItem[] {
554576
text: 'SSE 工具 (sse)',
555577
link: '/development/api-reference/chatluna-utils/sse',
556578
},
579+
{
580+
text: '错误 (Error)',
581+
link: '/development/api-reference/chatluna-utils/error',
582+
},
557583
],
558584
},
559585
],
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 消息读取
2+
3+
ChatLuna 是在 Koishi 下的插件。Koishi 作为一个聊天机器人框架,自身也有一套消息元素的机制。
4+
5+
为了让 LLM 能够正确理解各种消息元素(如@提及、表情、图片等),ChatLuna 提供了一个消息转换机制,将这些元素转换为文本形式。
6+
7+
## 用法
8+
9+
`messageTransformer` 中添加消息转换器即可:
10+
11+
```typescript
12+
const dispose = ctx.chatluna.messageTransformer.intercept(
13+
'at',
14+
async (session, element, message) => {
15+
const name = element.attrs['name']
16+
const id = element.attrs['id']
17+
18+
if (name && id !== session.bot.selfId) {
19+
message.content += `[at:${name}:${id}]`
20+
}
21+
}
22+
)
23+
24+
ctx.effect(() => dispose)
25+
```
26+
27+
在大部分情况下,将消息元素转换为文本,添加到 `message.content` 中即可。
28+
29+
## API 参考
30+
31+
`intercept` 方法接收两个参数:
32+
33+
- `type: string` - 消息元素的类型,支持的类型请参考 [Koishi 文档](https://koishi.chat/zh-CN/guide/basic/element.html#%E6%B3%A8%E5%86%8C%E5%85%A8%E5%B1%80%E7%BB%84%E4%BB%B6)
34+
35+
- `handler: (session, element, message) => Promise<void>` - 处理函数,包含:
36+
- `session`: 当前的 [会话对象](https://koishi.chat/zh-CN/api/core/session.html)
37+
- `element`: 当前的 [消息元素对象](https://koishi.chat/zh-CN/api/message/elements.html)
38+
- `message`: 当前的 [ChatLuna 消息对象](../api-reference/middleware/message)
39+
40+
返回值 `() => void` 用于清理资源。建议使用 `ctx.effect(() => dispose)` 确保资源被正确释放。
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 消息渲染
2+
3+
LLM 的默认输出都是文本。但在 Koishi 中,可以发送多种类型的消息元素(图片,语音等)。
4+
5+
为了让用户能够自由的选择消息元素的渲染方式,ChatLuna 提供了一个消息渲染 API,将这些文本渲染为 Koishi 的消息元素。
6+
7+
## 注册插件
8+
9+
所有需要接入功能到 ChatLuna 的插件,都得新建 `ChatLunaPlugin` 实例,并注册到 `ChatLuna` 服务中。
10+
11+
```typescript
12+
import { ChatLunaPlugin } from 'koishi-plugin-chatluna/services/chat'
13+
import { Context, Schema } from 'koishi'
14+
15+
export function apply(ctx: Context, config: Config) {
16+
const plugin = new ChatLunaPlugin(ctx, config, 'your-plugin-name', false)
17+
18+
ctx.on('ready', async () => {
19+
// 在 ready 事件中注册到 ChatLuna 服务
20+
plugin.registerToService()
21+
22+
// 继续...
23+
})
24+
}
25+
26+
```
27+
28+
> [!NOTE]
29+
> 如果你的插件不需要注册模型适配器,`ChatLunaPlugin` 的构造函数需要传入 `false` 作为第四个参数。
30+
> 该参数默认为 `true`,表示插件需要注册模型适配器。
31+
32+
## 继承 Renderer 类
33+
34+
继承 `Renderer` 类,实现你自己的消息渲染逻辑。
35+
36+
```typescript
37+
import {
38+
Message,
39+
Renderer,
40+
RenderMessage,
41+
RenderOptions
42+
} from 'koishi-plugin-chatluna'
43+
import { h, Schema } from 'koishi'
44+
45+
export class RawRenderer extends Renderer {
46+
async render(
47+
message: Message,
48+
options: RenderOptions
49+
): Promise<RenderMessage> {
50+
return {
51+
element: h.text(message.content)
52+
}
53+
}
54+
55+
schema = Schema.const('raw').i18n({
56+
'zh-CN': '原始输出',
57+
'en-US': 'Raw text'
58+
})
59+
}
60+
61+
```
62+
63+
`render` 方法接收两个参数:
64+
65+
- `message`: 当前的 [ChatLuna 消息对象](../api-reference/middleware/message)
66+
- `options`: 当前的 [渲染选项](../api-reference/middleware/message-renderer')
67+
68+
返回值 `RenderMessage` 包含一个 `element` 属性,用于指定渲染后的消息元素。
69+
70+
对于 `Renderer` 类,`schema` 属性是必须的,用于指定渲染器的名称。
71+
72+
## 注册 Renderer
73+
74+
`ready` 事件中,调用 `renderer` 服务的 `addRenderer` 方法注册消息渲染器。
75+
76+
```typescript
77+
ctx.on('ready', async () => {
78+
// 在 ready 事件中注册到 ChatLuna 服务
79+
plugin.registerToService()
80+
81+
ctx.effect(() =>
82+
ctx.chatluna.renderer.addRenderer('raw', (_) => {
83+
return new RawRenderer(ctx, config)
84+
})
85+
)
86+
})
87+
```

docs/development/connect-to-core-services/vector-database.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ChatLunaPlugin } from 'koishi-plugin-chatluna/services/chat'
1111
import { Context, Schema } from 'koishi'
1212

1313
export function apply(ctx: Context, config: Config) {
14-
const plugin = new ChatLunaPlugin(ctx, config, 'your-plugin-name')
14+
const plugin = new ChatLunaPlugin(ctx, config, 'your-plugin-name', false)
1515

1616
ctx.on('ready', async () => {
1717
// 在 ready 事件中注册到 ChatLuna 服务
@@ -22,6 +22,10 @@ export function apply(ctx: Context, config: Config) {
2222
}
2323
```
2424

25+
> [!NOTE]
26+
> 如果你的插件不需要注册模型适配器,`ChatLunaPlugin` 的构造函数需要传入 `false` 作为第四个参数。
27+
> 该参数默认为 `true`,表示插件需要注册模型适配器。
28+
2529
## 配置 Schema
2630

2731
如果你的向量数据库需要连接 URL 等参数,则需要自行声明 Schema。

0 commit comments

Comments
 (0)