Skip to content

Commit 118d3db

Browse files
committed
docs(api): add prompt renderer and update related docs
- Add new documentation for Prompt Renderer service - Update ChatLunaService docs with new methods and return types - Update PlatformService docs with reactive patterns and new APIs - Update PresetTemplate docs with full interface and service details - Update Requester docs with simplified parameter descriptions - Update MessageTransformer docs with priority support and new options - Update Message docs with content type changes - Add new chat events documentation - Update sidebar navigation with new preset and prompt renderer links
1 parent 6916bd7 commit 118d3db

19 files changed

Lines changed: 2472 additions & 1267 deletions

docs/.vitepress/config.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,10 @@ function sidebarDevelopment(): DefaultTheme.SidebarItem[] {
526526
text: "Agent",
527527
link: "/development/call-core-services/agent",
528528
},
529+
{
530+
text: "预设",
531+
link: "/development/call-core-services/preset",
532+
},
529533
],
530534
},
531535
{
@@ -602,6 +606,10 @@ function sidebarDevelopment(): DefaultTheme.SidebarItem[] {
602606
text: "消息渲染器 (Message Renderer)",
603607
link: "/development/api-reference/middleware/message-renderer",
604608
},
609+
{
610+
text: "Prompt 渲染服务 (Prompt Renderer)",
611+
link: "/development/api-reference/middleware/prompt-renderer",
612+
},
605613
{
606614
text: "消息转换器 (Message Transformer)",
607615
link: "/development/api-reference/middleware/message-transformer",

docs/development/api-reference/chatluna-events.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,53 @@ ChatLuna 的通用会话事件。
1919

2020
当返回值为 `true`,ChatLuna 将不会处理该会话。
2121

22+
## 聊天事件
23+
24+
ChatLuna 的聊天流程事件。
25+
26+
### 事件:chatluna/before-chat
27+
28+
- **conversationId**: `string` 会话 ID
29+
- **message**: `HumanMessage` 用户发送的消息
30+
- **promptVariables**: `ChainValues` 当前可用的提示词变量
31+
- **chatInterface**: `ChatInterface` 当前聊天接口实例
32+
- **session**: `Session` 会话
33+
- **触发方式**: parallel
34+
35+
在开始调用模型之前触发,可用于检查输入、动态修改提示词变量、注入提示词或提前拦截聊天流程。
36+
37+
### 事件:chatluna/after-chat
38+
39+
- **conversationId**: `string` 会话 ID
40+
- **sourceMessage**: `HumanMessage` 原始用户消息
41+
- **responseMessage**: `AIMessage` 模型返回的消息
42+
- **promptVariables**: `ChainValues` 调用时携带的提示词变量,包含 `chatCount` 等信息
43+
- **chatInterface**: `ChatInterface` 当前聊天接口实例
44+
- **session**: `Session` 会话
45+
- **触发方式**: parallel
46+
47+
在模型生成回复后触发,可用于对回复进行记录、二次处理或推送至其他系统。
48+
49+
### 事件:chatluna/clear-chat-history
50+
51+
- **conversationId**: `string` 会话 ID
52+
- **chatInterface**: `ChatInterface` 当前聊天接口实例
53+
- **触发方式**: parallel
54+
55+
当会话历史被清空时触发,方便同步清理缓存、外部存储或关联上下文。
56+
57+
### 事件:chatluna/after-chat-error
58+
59+
- **error**: `Error` 对话过程中捕获的异常对象
60+
- **conversationId**: `string` 会话 ID
61+
- **sourceMessage**: `HumanMessage` 原始用户消息
62+
- **promptVariables**: `ChainValues` 调用时的提示词变量
63+
- **chatInterface**: `ChatInterface` 当前聊天接口实例
64+
- **chain**: `ChatLunaLLMChainWrapper` 触发错误的链路实例(可选)
65+
- **触发方式**: parallel
66+
67+
当聊天流程出现异常时触发,可用于日志上报、兜底回复或通知维护者。
68+
2269
## 生命周期事件
2370

2471
ChatLuna 的生命周期事件,监听某些模型或者工具的变化。

docs/development/api-reference/chatluna-plugin.md

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,48 +21,54 @@ export class ChatLunaPlugin<
2121
- **platformName**: `PlatformClientNames` 平台名称
2222
- **createConfigPool**: `boolean` 是否创建配置池(默认为 `true`)
2323

24+
构造函数会在上下文 `ready` 时自动安装插件,并在上下文销毁时自动卸载,无需额外调用注册方法。
25+
2426
### parseConfig()
2527

2628
- **f**: `(config: T) => R[]` 配置解析函数
27-
- 返回值: `Promise<void>`
29+
- 返回值: `void`
2830

2931
解析插件配置并添加到配置池中。
3032

3133
```typescript
32-
await plugin.parseConfig(config => [{
34+
plugin.parseConfig(config => [{
3335
apiKey: config.apiKey,
3436
baseURL: config.baseURL
3537
}])
3638
```
3739

38-
### initClients()
40+
### initClient()
3941

4042
- 返回值: `Promise<void>`
4143

42-
初始化平台客户端。该方法会:
44+
初始化当前平台的客户端。如果创建过程中发生错误会回滚安装流程并抛出异常。
4345

44-
1. 注册配置池
45-
2. 创建所有客户端
46-
3. 更新支持的模型列表
46+
### dispose()
4747

48-
### registerToService()
48+
销毁插件,清理所有注册的资源。
4949

50-
注册插件到 ChatLuna 服务中。通常在插件初始化时调用。
50+
### supportedModels
5151

52-
### dispose()
52+
- 返回值: `readonly string[]`
5353

54-
销毁插件,清理所有注册的资源。
54+
获取当前插件可用的模型名称列表,形式为 `platform/model`
55+
56+
### platformConfigPool
57+
58+
- 返回值: `ClientConfigPool<R>`
59+
60+
访问插件的配置池,可用于自定义负载均衡策略或读取已解析的配置。
5561

5662
### registerClient()
5763

58-
- **func**: `(ctx: Context, config: R) => BasePlatformClient` 客户端创建函数
64+
- **func**: `() => BasePlatformClient` 客户端创建函数
5965
- **platformName**: `string` 平台名称(可选,默认为插件的 platformName)
6066
- 返回值: `void`
6167

6268
注册一个平台客户端。
6369

6470
```typescript
65-
plugin.registerClient((ctx, config) => new MyPlatformClient(ctx, config))
71+
plugin.registerClient(() => new MyPlatformClient(ctx, plugin.platformConfigPool))
6672
```
6773

6874
### registerVectorStore()
@@ -85,11 +91,19 @@ plugin.registerClient((ctx, config) => new MyPlatformClient(ctx, config))
8591

8692
- **name**: `string` 对话链名称
8793
- **description**: `Dict<string>` 多语言描述
88-
- **func**: `(params: CreateChatLunaLLMChainParams) => Promise<ChatLunaLLMChainWrapper>` 对话链创建函数
94+
- **func**: `(params: CreateChatLunaLLMChainParams) => ChatLunaLLMChainWrapper` 对话链创建函数
8995
- 返回值: `void`
9096

9197
注册一个对话链提供者。
9298

99+
### registerRenderer()
100+
101+
- **name**: `string` 渲染器名称
102+
- **renderer**: `(ctx: Context, config: Config) => Renderer` 渲染器工厂函数
103+
- 返回值: `void`
104+
105+
注册一个响应渲染器。
106+
93107
### fetch()
94108

95109
- **info**: `RequestInfo` 请求信息
@@ -112,4 +126,4 @@ plugin.registerClient((ctx, config) => new MyPlatformClient(ctx, config))
112126

113127
根据插件配置的代理模式创建 WebSocket 连接。
114128

115-
支持与 fetch 相同的代理模式。
129+
支持与 fetch 相同的代理模式。

docs/development/api-reference/chatluna-service.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ ChatLuna 服务是整个系统的核心,用于管理插件、对话、模型
44

55
## 类:ChatLunaService
66

7-
### chatluna.registerPlugin()
7+
### chatluna.installPlugin()
88

99
- **plugin**: `ChatLunaPlugin` 要注册的插件
1010
- 返回值: `Promise<void>`
1111

12-
注册一个 ChatLuna 插件。如果插件已注册则会抛出错误
12+
安装一个 ChatLuna 插件。如果同名插件已存在会抛出错误。插件通常会在构造函数中自动调用该方法
1313

1414
### chatluna.awaitLoadPlatform()
1515

@@ -19,18 +19,17 @@ ChatLuna 服务是整个系统的核心,用于管理插件、对话、模型
1919

2020
等待平台加载完成。如果超时会抛出错误。
2121

22-
### chatluna.unregisterPlugin()
22+
### chatluna.uninstallPlugin()
2323

2424
- **plugin**: `ChatLunaPlugin | string` 插件实例或平台名称
25-
- **withError**: `boolean` 是否在插件不存在时抛出错误,默认为 true
2625
- 返回值: `void`
2726

28-
注销一个 ChatLuna 插件。
27+
卸载一个 ChatLuna 插件。如果插件未安装将被忽略
2928

3029
### chatluna.getPlugin()
3130

3231
- **platformName**: `string` 平台名称
33-
- 返回值: `ChatLunaPlugin`
32+
- 返回值: `ChatLunaPlugin | undefined`
3433

3534
获取指定平台名称的插件。
3635

@@ -44,17 +43,17 @@ ChatLuna 服务是整个系统的核心,用于管理插件、对话、模型
4443
- **variables**: `Record<string, any>` 变量对象,默认为空对象
4544
- **postHandler**: `PostHandler` 后处理函数(可选)
4645
- **requestId**: `string` 请求 ID,默认自动生成
47-
- 返回值: `Promise<void>`
46+
- 返回值: `Promise<Message>`
4847

49-
发起一个对话请求。
48+
发起一个对话请求,返回最终回复消息。部分模型会附带 `additionalReplyMessages` 用于展示思考过程等扩展内容
5049

5150
### chatluna.stopChat()
5251

5352
- **room**: `ConversationRoom` 对话房间
5453
- **requestId**: `string` 请求 ID
55-
- 返回值: `Promise<void>`
54+
- 返回值: `Promise<boolean | undefined>`
5655

57-
停止指定请求 ID 的对话。
56+
尝试停止指定请求 ID 的对话。当没有找到对应请求时返回 `false`,如果会话尚未创建则返回 `undefined`
5857

5958
### chatluna.clearChatHistory()
6059

@@ -66,25 +65,25 @@ ChatLuna 服务是整个系统的核心,用于管理插件、对话、模型
6665
### chatluna.clearCache()
6766

6867
- **room**: `ConversationRoom` 对话房间
69-
- 返回值: `Promise<void>`
68+
- 返回值: `Promise<boolean>`
7069

71-
清除指定房间的缓存
70+
清除指定房间的缓存并同步触发 `chatluna/clear-chat-history` 事件。返回值表示是否成功移除缓存的聊天接口
7271

7372
### chatluna.createChatModel()
7473

7574
- **platformName**: `string` 平台名称
7675
- **model**: `string` 模型名称
77-
- 返回值: `Promise<ChatLunaChatModel>`
76+
- 返回值: `Promise<ComputedRef<ChatLunaChatModel | undefined>>`
7877

79-
创建一个聊天模型实例
78+
创建一个聊天模型的计算引用。也支持直接传入 `platform/model` 组合字符串
8079

8180
### chatluna.createEmbeddings()
8281

8382
- **platformName**: `string` 平台名称
8483
- **modelName**: `string` 模型名称
85-
- 返回值: `Promise<ChatHubBaseEmbeddings>`
84+
- 返回值: `Promise<ComputedRef<Embeddings | undefined>>`
8685

87-
创建一个嵌入模型实例
86+
创建一个嵌入模型的计算引用。若模型不可用会回退为空嵌入实现
8887

8988
### 属性
9089

@@ -129,3 +128,10 @@ ChatLuna 服务是整个系统的核心,用于管理插件、对话、模型
129128
- **只读**
130129

131130
默认渲染器实例。
131+
132+
#### chatluna.promptRenderer
133+
134+
- **类型**: `ChatLunaPromptRenderService`
135+
- **只读**
136+
137+
提示词渲染服务实例。

0 commit comments

Comments
 (0)