agent-scene-toolkit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +135 -0
- package/dist/index.cjs +570 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +463 -0
- package/dist/index.d.ts +463 -0
- package/dist/index.js +558 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import { StructuredToolInterface } from '@langchain/core/tools';
|
|
2
|
+
import * as _langchain_langgraph from '@langchain/langgraph';
|
|
3
|
+
import { BaseCheckpointSaver } from '@langchain/langgraph';
|
|
4
|
+
import { BaseCallbackHandler } from '@langchain/core/callbacks/base';
|
|
5
|
+
import { RequestHandler } from 'express';
|
|
6
|
+
import * as _langchain_core_utils_stream from '@langchain/core/utils/stream';
|
|
7
|
+
import * as langchain from 'langchain';
|
|
8
|
+
import * as _langchain_core_messages from '@langchain/core/messages';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 静态能力包 — 按领域分组的工具集 + 使用策略提示词。
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const canvasToolKit = defineToolKit({
|
|
16
|
+
* name: 'canvas',
|
|
17
|
+
* tools: [bindElementTool, bindTrackTool],
|
|
18
|
+
* prompt: '画面调整时优先使用 canvas 工具...',
|
|
19
|
+
* })
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
interface ToolKit {
|
|
23
|
+
/** 唯一标识(如 `'canvas'`、`'ai'`) */
|
|
24
|
+
readonly name: string;
|
|
25
|
+
/** LangChain Tool 数组 */
|
|
26
|
+
readonly tools: StructuredToolInterface[];
|
|
27
|
+
/** 使用策略提示词,告诉 LLM 何时/如何使用这组工具 */
|
|
28
|
+
readonly prompt: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 角色身份 — 极简声明:名字 + 系统提示词 + 模型。
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const director = defineProfile({
|
|
36
|
+
* name: '导演',
|
|
37
|
+
* systemPrompt: '你是一位视频导演...',
|
|
38
|
+
* model: 'gpt-4o',
|
|
39
|
+
* })
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
interface AgentProfile {
|
|
43
|
+
/** 角色名称,多 Agent 时作为唯一标识 */
|
|
44
|
+
readonly name: string;
|
|
45
|
+
/** 角色系统提示词 */
|
|
46
|
+
readonly systemPrompt: string;
|
|
47
|
+
/** 模型标识(如 `'gpt-4o'`、`'gpt-4o-mini'`) */
|
|
48
|
+
readonly model: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 运行时场景 — 声明当前需要的工具集 + 动态上下文提示词 + 生命周期回调。
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const timelineScene = defineScene({
|
|
56
|
+
* name: 'timeline-editing',
|
|
57
|
+
* toolkits: ['canvas', 'ai'],
|
|
58
|
+
* prompt: (ctx) => `视频时长: ${ctx.duration}秒`,
|
|
59
|
+
* onToolEnd: (toolName, result) => {
|
|
60
|
+
* if (toolName === 'bindTrack') refreshTimeline()
|
|
61
|
+
* },
|
|
62
|
+
* })
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
interface Scene {
|
|
66
|
+
/** 场景名称 */
|
|
67
|
+
readonly name: string;
|
|
68
|
+
/** 当前场景需要的 ToolKit 名称列表,从全局能力池中过滤 */
|
|
69
|
+
readonly toolkits: string[];
|
|
70
|
+
/** 动态提示词模板,接收运行时上下文数据(来自 `chat()` 的 `sceneContext`) */
|
|
71
|
+
readonly prompt: (ctx: Record<string, any>) => string;
|
|
72
|
+
/** 工具调用完成后的生命周期回调 */
|
|
73
|
+
readonly onToolEnd?: (toolName: string, result: any) => void;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* `createAgent()` 配置项。
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* const agent = createAgent({
|
|
81
|
+
* toolkits: [canvasToolKit, aiToolKit],
|
|
82
|
+
* agents: [director, screenwriter],
|
|
83
|
+
* supervisor: '导演',
|
|
84
|
+
* scene: timelineScene,
|
|
85
|
+
* checkpointer: new MemorySaver(),
|
|
86
|
+
* maxMessages: 50,
|
|
87
|
+
* llm: {
|
|
88
|
+
* baseURL: 'https://api.bltcy.ai',
|
|
89
|
+
* apiKey: 'sk-xxx',
|
|
90
|
+
* },
|
|
91
|
+
* })
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
interface AgentOptions {
|
|
95
|
+
/** 全局能力池,所有可用的 ToolKit */
|
|
96
|
+
toolkits: ToolKit[];
|
|
97
|
+
/** Agent 列表 */
|
|
98
|
+
agents: AgentProfile[];
|
|
99
|
+
/** Supervisor 的 Agent name,有值则启用多 Agent 模式 */
|
|
100
|
+
supervisor?: string;
|
|
101
|
+
/** 运行时场景,决定工具集过滤和动态 Prompt */
|
|
102
|
+
scene?: Scene;
|
|
103
|
+
/** LangGraph Checkpointer 实例(默认 MemorySaver) */
|
|
104
|
+
checkpointer?: BaseCheckpointSaver;
|
|
105
|
+
/** 滑动窗口大小(默认 50) */
|
|
106
|
+
maxMessages?: number;
|
|
107
|
+
/** LangChain Callbacks(如 LangFuse) */
|
|
108
|
+
callbacks?: BaseCallbackHandler[];
|
|
109
|
+
/** OpenAI 兼容网关配置(如中转商) */
|
|
110
|
+
llm?: {
|
|
111
|
+
/** 兼容 OpenAI 的 base URL,例如 https://api.bltcy.ai */
|
|
112
|
+
baseURL?: string;
|
|
113
|
+
/** API Key,优先级高于环境变量 OPENAI_API_KEY */
|
|
114
|
+
apiKey?: string;
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* `agent.chat()` 调用参数。
|
|
119
|
+
*/
|
|
120
|
+
interface ChatOptions {
|
|
121
|
+
/** 用户消息 */
|
|
122
|
+
message: string;
|
|
123
|
+
/** 对话线程 ID,用于记忆隔离 */
|
|
124
|
+
threadId: string;
|
|
125
|
+
/** 传给 `scene.prompt(ctx)` 的动态运行时数据 */
|
|
126
|
+
sceneContext?: Record<string, any>;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 标准化 SSE 事件联合类型。
|
|
130
|
+
*
|
|
131
|
+
* | 类型 | 触发时机 |
|
|
132
|
+
* |------|---------|
|
|
133
|
+
* | `text` | LLM 输出文本 token |
|
|
134
|
+
* | `tool_start` | 工具调用开始 |
|
|
135
|
+
* | `tool_end` | 工具调用结束 |
|
|
136
|
+
* | `handoff` | Agent 切换(多 Agent) |
|
|
137
|
+
* | `agent` | 当前回答的 Agent 身份 |
|
|
138
|
+
* | `error` | 执行出错 |
|
|
139
|
+
* | `done` | 流结束 |
|
|
140
|
+
*/
|
|
141
|
+
type SSEEvent = {
|
|
142
|
+
type: 'text';
|
|
143
|
+
content: string;
|
|
144
|
+
} | {
|
|
145
|
+
type: 'tool_start';
|
|
146
|
+
toolName: string;
|
|
147
|
+
input: Record<string, any>;
|
|
148
|
+
} | {
|
|
149
|
+
type: 'tool_end';
|
|
150
|
+
toolName: string;
|
|
151
|
+
output: any;
|
|
152
|
+
} | {
|
|
153
|
+
type: 'handoff';
|
|
154
|
+
from: string;
|
|
155
|
+
to: string;
|
|
156
|
+
} | {
|
|
157
|
+
type: 'agent';
|
|
158
|
+
name: string;
|
|
159
|
+
} | {
|
|
160
|
+
type: 'error';
|
|
161
|
+
message: string;
|
|
162
|
+
} | {
|
|
163
|
+
type: 'done';
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 定义一个 Agent 角色身份。
|
|
168
|
+
*
|
|
169
|
+
* 校验必填字段后返回不可变对象。
|
|
170
|
+
*
|
|
171
|
+
* @param input - 角色配置
|
|
172
|
+
* @returns 冻结的 AgentProfile 对象
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* const director = defineProfile({
|
|
177
|
+
* name: '导演',
|
|
178
|
+
* systemPrompt: '你是一位视频导演...',
|
|
179
|
+
* model: 'gpt-4o',
|
|
180
|
+
* })
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
declare function defineProfile(input: AgentProfile): Readonly<AgentProfile>;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 定义一个静态能力包。
|
|
187
|
+
*
|
|
188
|
+
* 校验必填字段后返回不可变对象。
|
|
189
|
+
*
|
|
190
|
+
* @param input - 能力包配置
|
|
191
|
+
* @returns 冻结的 ToolKit 对象
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* const canvasToolKit = defineToolKit({
|
|
196
|
+
* name: 'canvas',
|
|
197
|
+
* tools: [bindElementTool, bindTrackTool],
|
|
198
|
+
* prompt: '画面调整时优先使用 canvas 工具...',
|
|
199
|
+
* })
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
declare function defineToolKit(input: ToolKit): Readonly<ToolKit>;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 定义一个运行时场景。
|
|
206
|
+
*
|
|
207
|
+
* 校验必填字段后返回不可变对象。
|
|
208
|
+
*
|
|
209
|
+
* @param input - 场景配置
|
|
210
|
+
* @returns 冻结的 Scene 对象
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```ts
|
|
214
|
+
* const timelineScene = defineScene({
|
|
215
|
+
* name: 'timeline-editing',
|
|
216
|
+
* toolkits: ['canvas', 'ai'],
|
|
217
|
+
* prompt: (ctx) => `视频时长: ${ctx.duration}秒`,
|
|
218
|
+
* onToolEnd: (toolName, result) => {
|
|
219
|
+
* if (toolName === 'bindTrack') refreshTimeline()
|
|
220
|
+
* },
|
|
221
|
+
* })
|
|
222
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
declare function defineScene(input: Scene): Readonly<Scene>;
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Agent 实例的内部已解析配置类型。
|
|
228
|
+
*
|
|
229
|
+
* 将可选字段填充为默认值后的完整配置。
|
|
230
|
+
*/
|
|
231
|
+
interface ResolvedOptions extends AgentOptions {
|
|
232
|
+
maxMessages: number;
|
|
233
|
+
callbacks: BaseCallbackHandler[];
|
|
234
|
+
checkpointer: BaseCheckpointSaver;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Agent 核心类。
|
|
238
|
+
*
|
|
239
|
+
* 串联完整流程:参数校验 → ToolKit 过滤 → Prompt 拼接 → 图构建 → 流式输出。
|
|
240
|
+
*
|
|
241
|
+
* 不直接 new,通过 `createAgent()` 工厂函数创建。
|
|
242
|
+
*/
|
|
243
|
+
declare class Agent {
|
|
244
|
+
/** @internal */
|
|
245
|
+
readonly options: ResolvedOptions;
|
|
246
|
+
constructor(options: AgentOptions);
|
|
247
|
+
/**
|
|
248
|
+
* 发起对话,返回标准化 SSE 事件的异步生成器。
|
|
249
|
+
*
|
|
250
|
+
* 完整流程:
|
|
251
|
+
* 1. ToolKit 过滤(Scene.toolkits 决定)
|
|
252
|
+
* 2. Prompt 4 层拼接(Base → Profile → ToolKit → Scene)
|
|
253
|
+
* 3. 构建 LangGraph 图 + stream
|
|
254
|
+
* 4. 转换流事件 → 标准化 SSEEvent
|
|
255
|
+
*
|
|
256
|
+
* 任何异常均以 `error` + `done` 事件正常结束流,不崩溃。
|
|
257
|
+
*
|
|
258
|
+
* @param chatOptions - 对话参数
|
|
259
|
+
* @yields 标准化 SSE 事件序列
|
|
260
|
+
*/
|
|
261
|
+
chat(chatOptions: ChatOptions): AsyncGenerator<SSEEvent>;
|
|
262
|
+
/**
|
|
263
|
+
* 返回 Express RequestHandler,直接用于路由挂载。
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* app.post('/chat', agent.handleRequest())
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
handleRequest(): RequestHandler;
|
|
271
|
+
/**
|
|
272
|
+
* 参数校验 — 在构造时执行,快速失败。
|
|
273
|
+
*/
|
|
274
|
+
private validate;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* 创建 Agent 实例。
|
|
278
|
+
*
|
|
279
|
+
* @param options - Agent 配置项
|
|
280
|
+
* @returns Agent 实例
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* const agent = createAgent({
|
|
285
|
+
* toolkits: [canvasToolKit],
|
|
286
|
+
* agents: [director],
|
|
287
|
+
* scene: timelineScene,
|
|
288
|
+
* })
|
|
289
|
+
*
|
|
290
|
+
* for await (const event of agent.chat({ message: '你好', threadId: 'thread-001' })) {
|
|
291
|
+
* console.log(event)
|
|
292
|
+
* }
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
declare function createAgent(options: AgentOptions): Agent;
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* 为 Agent 创建 Express SSE 处理器。
|
|
299
|
+
*
|
|
300
|
+
* 请求体格式:
|
|
301
|
+
* {
|
|
302
|
+
* "message": "...",
|
|
303
|
+
* "threadId": "...",
|
|
304
|
+
* "sceneContext": { ... }
|
|
305
|
+
* }
|
|
306
|
+
*
|
|
307
|
+
* ## 错误处理
|
|
308
|
+
*
|
|
309
|
+
* - **请求体解析异常**:parseChatOptions 失败时返回 `error` 事件
|
|
310
|
+
* - **agent.chat() 异常**:流迭代过程中的异常会被捕获并转换为 `error` 事件
|
|
311
|
+
* - **响应写入异常**:res.write() 失败时(客户端断开连接)静默捕获,避免服务器崩溃
|
|
312
|
+
* - **所有异常路径**:确保最终都会发送 `done` 事件并关闭响应
|
|
313
|
+
*/
|
|
314
|
+
declare function createExpressHandler(agent: Agent): RequestHandler;
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* 构建 4 层 Prompt 拼接链。
|
|
318
|
+
*
|
|
319
|
+
* ```
|
|
320
|
+
* ① Base — 库内置固定指令(通用行为约束、防御性指令)
|
|
321
|
+
* ② Profile — agent.systemPrompt(角色身份)
|
|
322
|
+
* ③ ToolKit — 当前场景激活的 ToolKit.prompt(0~N 个)
|
|
323
|
+
* ④ Scene — scene.prompt(sceneContext)(仅绑定 Scene 时)
|
|
324
|
+
* ```
|
|
325
|
+
*
|
|
326
|
+
* 各层以 `\n\n` 拼接,合并为单条 SystemMessage 字符串。
|
|
327
|
+
*
|
|
328
|
+
* @param params - 拼接所需的各层数据
|
|
329
|
+
* @returns 完整的 system prompt 字符串
|
|
330
|
+
*/
|
|
331
|
+
declare function buildPromptChain(params: {
|
|
332
|
+
/** 当前 Agent 角色 */
|
|
333
|
+
profile: AgentProfile;
|
|
334
|
+
/** 当前场景激活的 ToolKit prompt 列表 */
|
|
335
|
+
toolkitPrompts: string[];
|
|
336
|
+
/** 运行时场景(可选) */
|
|
337
|
+
scene?: Scene;
|
|
338
|
+
/** 传给 scene.prompt(ctx) 的动态数据(可选) */
|
|
339
|
+
sceneContext?: Record<string, any>;
|
|
340
|
+
}): string;
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* 构建单 Agent 图并返回双模式流。
|
|
344
|
+
*
|
|
345
|
+
* 使用 `createAgent` 创建 ReAct Agent,
|
|
346
|
+
* 通过 `streamMode: ['messages', 'updates']` 同时获取:
|
|
347
|
+
* - 逐 token 的文本流(messages 模式)
|
|
348
|
+
* - 节点级别的完整更新(updates 模式,用于工具调用结果)
|
|
349
|
+
*
|
|
350
|
+
* Callbacks 在 LLM 层和 graph.stream() 层双重透传,
|
|
351
|
+
* 确保 LangFuse 等观测工具能追踪完整 Agent 执行链路。
|
|
352
|
+
*
|
|
353
|
+
* ## 错误处理策略
|
|
354
|
+
*
|
|
355
|
+
* - **LLM 初始化异常**:ChatOpenAI 构造函数会在无效配置时抛出异常(如无效 API Key),
|
|
356
|
+
* 由调用方(agent.ts)的顶层 try-catch 捕获并转换为 `error` 事件
|
|
357
|
+
* - **Checkpointer 异常**:LangGraph 内部处理 checkpointer 加载/保存失败,
|
|
358
|
+
* 失败时会降级为无记忆模式继续执行,不会中断流
|
|
359
|
+
* - **工具执行异常**:LangGraph 内置异常处理,工具失败时会将错误信息作为 ToolMessage 返回给 LLM,
|
|
360
|
+
* 由 LLM 决定如何处理(重试、跳过或报告用户)
|
|
361
|
+
* - **流式输出异常**:stream() 过程中的网络异常或 LLM API 异常会抛出,
|
|
362
|
+
* 由调用方的 try-catch 捕获
|
|
363
|
+
*
|
|
364
|
+
* @param params - 图构建参数
|
|
365
|
+
* @returns 双模式流的异步可迭代对象
|
|
366
|
+
* @throws 当 LLM 初始化失败或 stream() 执行失败时抛出异常
|
|
367
|
+
*/
|
|
368
|
+
declare function buildSingleGraph(params: {
|
|
369
|
+
/** 完整的 system prompt(4 层拼接后) */
|
|
370
|
+
systemPrompt: string;
|
|
371
|
+
/** 当前场景激活的工具列表 */
|
|
372
|
+
tools: StructuredToolInterface[];
|
|
373
|
+
/** Agent 模型标识 */
|
|
374
|
+
model: string;
|
|
375
|
+
/** 用户消息 */
|
|
376
|
+
message: string;
|
|
377
|
+
/** 对话线程 ID */
|
|
378
|
+
threadId: string;
|
|
379
|
+
/** LangGraph Checkpointer */
|
|
380
|
+
checkpointer: BaseCheckpointSaver;
|
|
381
|
+
/** 滑动窗口大小 */
|
|
382
|
+
maxMessages: number;
|
|
383
|
+
/** LangChain Callbacks */
|
|
384
|
+
callbacks: BaseCallbackHandler[];
|
|
385
|
+
/** 底层 LLM 网关配置(OpenAI 兼容) */
|
|
386
|
+
llm?: AgentOptions['llm'];
|
|
387
|
+
}): Promise<_langchain_core_utils_stream.IterableReadableStream<["updates", Record<string, any>] | ["messages", [langchain.BaseMessage<_langchain_core_messages.MessageStructure<_langchain_core_messages.MessageToolSet>, _langchain_core_messages.MessageType>, Record<string, any>]]>>;
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* 构建多 Agent Supervisor 图并返回双模式流。
|
|
391
|
+
*
|
|
392
|
+
* 使用 `@langchain/langgraph-supervisor` 的 `createSupervisor` 构建:
|
|
393
|
+
* - Supervisor 负责任务分析与分派
|
|
394
|
+
* - Workers 为各 AgentProfile 对应的 ReAct Agent
|
|
395
|
+
*
|
|
396
|
+
* 工具在 Supervisor 级别不注入(Supervisor 只负责路由),
|
|
397
|
+
* 所有工具注入到 Worker Agent 中,由 Scene 过滤后的工具集共享。
|
|
398
|
+
*
|
|
399
|
+
* ## 错误处理策略
|
|
400
|
+
*
|
|
401
|
+
* - **LLM 初始化异常**:任一 LLM(Supervisor / Worker)初始化失败时抛出异常,
|
|
402
|
+
* 由调用方(agent.ts)的顶层 try-catch 捕获
|
|
403
|
+
* - **Worker 创建异常**:单个 Worker 创建失败时记录错误并跳过该 Worker,
|
|
404
|
+
* 至少保证 1 个 Worker 可用才继续执行
|
|
405
|
+
* - **Checkpointer 异常**:同单 Agent 模式,LangGraph 内部降级处理
|
|
406
|
+
* - **流式输出异常**:stream() 过程中的异常会抛出,由调用方捕获
|
|
407
|
+
*
|
|
408
|
+
* @param params - 图构建参数
|
|
409
|
+
* @returns 双模式流的异步可迭代对象
|
|
410
|
+
* @throws 当 LLM 初始化失败、Worker 全部创建失败或 stream() 执行失败时抛出异常
|
|
411
|
+
*/
|
|
412
|
+
declare function buildSupervisorGraph(params: {
|
|
413
|
+
/** Supervisor 的 Prompt(4 层拼接后,用于 Supervisor Agent) */
|
|
414
|
+
supervisorPrompt: string;
|
|
415
|
+
/** 所有 AgentProfile 列表 */
|
|
416
|
+
agents: AgentProfile[];
|
|
417
|
+
/** Supervisor 的 agent name */
|
|
418
|
+
supervisorName: string;
|
|
419
|
+
/** 当前场景激活的工具列表(所有 Worker 共享) */
|
|
420
|
+
tools: StructuredToolInterface[];
|
|
421
|
+
/** 各 Worker 的 Prompt 映射(profile.name → 4 层拼接后的 prompt) */
|
|
422
|
+
workerPrompts: Map<string, string>;
|
|
423
|
+
/** 用户消息 */
|
|
424
|
+
message: string;
|
|
425
|
+
/** 对话线程 ID */
|
|
426
|
+
threadId: string;
|
|
427
|
+
/** LangGraph Checkpointer */
|
|
428
|
+
checkpointer: BaseCheckpointSaver;
|
|
429
|
+
/** 滑动窗口大小 */
|
|
430
|
+
maxMessages: number;
|
|
431
|
+
/** LangChain Callbacks */
|
|
432
|
+
callbacks: BaseCallbackHandler[];
|
|
433
|
+
/** 底层 LLM 网关配置(OpenAI 兼容) */
|
|
434
|
+
llm?: AgentOptions['llm'];
|
|
435
|
+
}): Promise<_langchain_core_utils_stream.IterableReadableStream<["messages", [langchain.BaseMessage<_langchain_core_messages.MessageStructure<_langchain_core_messages.MessageToolSet>, _langchain_core_messages.MessageType>, Record<string, any>]] | ["updates", Record<string, _langchain_langgraph.UpdateType<_langchain_langgraph.StateDefinition> | _langchain_langgraph.UpdateType<any>>]>>;
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* 将 LangGraph `stream()` 的双模式流(messages + updates)
|
|
439
|
+
* 转换为标准化 SSEEvent 序列。
|
|
440
|
+
*
|
|
441
|
+
* 事件映射逻辑:
|
|
442
|
+
* - `messages` 模式的 AIMessageChunk(含 content)→ `text` 事件
|
|
443
|
+
* - `messages` 模式的 AIMessageChunk(含 tool_call_chunks)→ `tool_start` 事件
|
|
444
|
+
* - `updates` 模式的 tools 节点输出(ToolMessage)→ `tool_end` 事件
|
|
445
|
+
* - `messages` 模式的 metadata.langgraph_node 变化 → `agent` + `handoff` 事件(多 Agent)
|
|
446
|
+
*
|
|
447
|
+
* ## 错误处理
|
|
448
|
+
*
|
|
449
|
+
* - **流迭代异常**:stream 本身抛出异常时(网络中断、LLM API 异常),
|
|
450
|
+
* 由调用方(agent.ts)的 try-catch 捕获并转换为 `error` 事件
|
|
451
|
+
* - **chunk 解析异常**:单个 chunk 解析失败时记录错误并跳过该 chunk,不中断流
|
|
452
|
+
* - **生命周期回调异常**:onToolEnd 抛出异常时静默捕获,不影响流
|
|
453
|
+
*
|
|
454
|
+
* @param stream - LangGraph stream() 返回的异步可迭代对象
|
|
455
|
+
* @param onToolEnd - Scene.onToolEnd 生命周期回调(可选)
|
|
456
|
+
*/
|
|
457
|
+
declare function transformStream(stream: AsyncIterable<any>, onToolEnd?: (toolName: string, result: any) => void): AsyncGenerator<SSEEvent>;
|
|
458
|
+
/**
|
|
459
|
+
* 将 SSEEvent 格式化为 SSE 协议字符串 `data: JSON\n\n`。
|
|
460
|
+
*/
|
|
461
|
+
declare function formatSSE(event: SSEEvent): string;
|
|
462
|
+
|
|
463
|
+
export { Agent, type AgentOptions, type AgentProfile, type ChatOptions, type SSEEvent, type Scene, type ToolKit, buildPromptChain, buildSingleGraph, buildSupervisorGraph, createAgent, createExpressHandler, defineProfile, defineScene, defineToolKit, formatSSE, transformStream };
|