@ai-setting/roy-agent-core 1.5.17-beta.1 → 1.5.22
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/dist/env/index.js +20 -8
- package/dist/env/log-trace/index.js +1 -1
- package/dist/env/prompt/index.js +1 -1
- package/dist/env/workflow/engine/index.js +1 -1
- package/dist/env/workflow/index.js +2 -2
- package/dist/index.js +6 -7
- package/dist/shared/@ai-setting/{roy-agent-core-xq8hhqb8.js → roy-agent-core-4wjywp3c.js} +4 -2
- package/dist/shared/@ai-setting/roy-agent-core-8jxva565.js +19 -0
- package/dist/shared/@ai-setting/roy-agent-core-avq1x4t7.js +84 -0
- package/dist/shared/@ai-setting/{roy-agent-core-gq20wsgv.js → roy-agent-core-ffb9fq4v.js} +23 -2
- package/dist/shared/@ai-setting/{roy-agent-core-93zfb3r1.js → roy-agent-core-mrcxzpbg.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-rhmtwnw1.js → roy-agent-core-pw7cv1px.js} +1 -1
- package/dist/shared/@ai-setting/{roy-agent-core-gbqcyegm.js → roy-agent-core-rccptwv0.js} +512 -673
- package/dist/shared/@ai-setting/{roy-agent-core-wrcy0h6z.js → roy-agent-core-ty94k28r.js} +1 -1
- package/package.json +8 -29
- package/dist/config/index.d.ts +0 -1250
- package/dist/env/agent/index.d.ts +0 -2279
- package/dist/env/commands/index.d.ts +0 -1131
- package/dist/env/debug/formatters/index.d.ts +0 -236
- package/dist/env/debug/index.d.ts +0 -1652
- package/dist/env/hook/index.d.ts +0 -279
- package/dist/env/index.d.ts +0 -3481
- package/dist/env/llm/index.d.ts +0 -1760
- package/dist/env/log-trace/index.d.ts +0 -1574
- package/dist/env/mcp/index.d.ts +0 -1331
- package/dist/env/mcp/tool/index.d.ts +0 -183
- package/dist/env/memory/built-in/index.d.ts +0 -232
- package/dist/env/memory/index.d.ts +0 -1799
- package/dist/env/memory/plugin/index.d.ts +0 -747
- package/dist/env/prompt/index.d.ts +0 -1164
- package/dist/env/session/index.d.ts +0 -1908
- package/dist/env/session/storage/index.d.ts +0 -564
- package/dist/env/skill/index.d.ts +0 -1266
- package/dist/env/skill/tool/index.d.ts +0 -193
- package/dist/env/task/delegate/index.d.ts +0 -1612
- package/dist/env/task/events/index.d.ts +0 -171
- package/dist/env/task/hooks/index.d.ts +0 -624
- package/dist/env/task/index.d.ts +0 -1553
- package/dist/env/task/plugins/index.d.ts +0 -466
- package/dist/env/task/storage/index.d.ts +0 -241
- package/dist/env/task/tools/index.d.ts +0 -1485
- package/dist/env/task/tools/operation/index.d.ts +0 -1484
- package/dist/env/tool/built-in/index.d.ts +0 -218
- package/dist/env/tool/index.d.ts +0 -1396
- package/dist/env/workflow/decorators/index.d.ts +0 -2161
- package/dist/env/workflow/engine/index.d.ts +0 -3453
- package/dist/env/workflow/index.d.ts +0 -3546
- package/dist/env/workflow/nodes/index.d.ts +0 -2092
- package/dist/env/workflow/service/index.d.ts +0 -227
- package/dist/env/workflow/storage/index.d.ts +0 -165
- package/dist/env/workflow/tools/index.d.ts +0 -416
- package/dist/env/workflow/types/index.d.ts +0 -2255
- package/dist/env/workflow/utils/index.d.ts +0 -2031
- package/dist/index.d.ts +0 -7858
|
@@ -1,3546 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Hook 系统统一类型定义
|
|
3
|
-
*
|
|
4
|
-
* 提供所有 Component 使用的统一 Hook 接口
|
|
5
|
-
*/
|
|
6
|
-
/**
|
|
7
|
-
* Hook 执行阶段
|
|
8
|
-
*/
|
|
9
|
-
type HookPhase = "before" | "after" | "error";
|
|
10
|
-
/**
|
|
11
|
-
* Hook 元信息
|
|
12
|
-
*/
|
|
13
|
-
interface HookMeta {
|
|
14
|
-
/** Hook 唯一名称 */
|
|
15
|
-
name: string;
|
|
16
|
-
/** 执行优先级,默认 0,数值越小越先执行 */
|
|
17
|
-
priority?: number;
|
|
18
|
-
/** 描述 */
|
|
19
|
-
description?: string;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* 统一 Hook 上下文
|
|
23
|
-
*
|
|
24
|
-
* 泛型参数 T 表示该 Component 的上下文数据类型
|
|
25
|
-
*/
|
|
26
|
-
interface HookContext<T = unknown> {
|
|
27
|
-
/** 当前组件信息 */
|
|
28
|
-
component: {
|
|
29
|
-
/** 组件名称 */
|
|
30
|
-
name: string;
|
|
31
|
-
/** 组件版本 */
|
|
32
|
-
version: string;
|
|
33
|
-
};
|
|
34
|
-
/** 上下文数据(Component 特定) */
|
|
35
|
-
data: T;
|
|
36
|
-
/** 元数据(可用于传递额外信息) */
|
|
37
|
-
metadata: Record<string, unknown>;
|
|
38
|
-
/** 执行阶段 */
|
|
39
|
-
phase: HookPhase;
|
|
40
|
-
/** 关联的 hook 点名称 */
|
|
41
|
-
hookPoint: string;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* 统一 Hook 接口
|
|
45
|
-
*
|
|
46
|
-
* 所有 Component 的 Hook 都应实现此接口
|
|
47
|
-
*/
|
|
48
|
-
interface Hook<T = unknown> extends HookMeta {
|
|
49
|
-
/** 执行 Hook
|
|
50
|
-
* @param ctx 上下文
|
|
51
|
-
*/
|
|
52
|
-
execute(ctx: HookContext<T>): void | Promise<void>;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Hook 函数类型(便捷别名)
|
|
56
|
-
*/
|
|
57
|
-
type HookFn<T = unknown> = (ctx: HookContext<T>) => void | Promise<void>;
|
|
58
|
-
/**
|
|
59
|
-
* Hook 干预动作类型
|
|
60
|
-
*/
|
|
61
|
-
type HookActionType = "stop" | "retry" | "compress" | "extract_memory" | "inject_message" | "skip_tool" | "custom";
|
|
62
|
-
/**
|
|
63
|
-
* Hook 干预动作
|
|
64
|
-
*/
|
|
65
|
-
interface HookAction {
|
|
66
|
-
/** 动作类型 */
|
|
67
|
-
type: HookActionType;
|
|
68
|
-
/** 动作参数 */
|
|
69
|
-
params?: Record<string, unknown>;
|
|
70
|
-
/** 动作描述 */
|
|
71
|
-
description?: string;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Hook 执行结果
|
|
75
|
-
*/
|
|
76
|
-
interface HookResult {
|
|
77
|
-
/** 是否被停止 */
|
|
78
|
-
stopped?: boolean;
|
|
79
|
-
/** 干预动作(如果有) */
|
|
80
|
-
action?: HookAction;
|
|
81
|
-
/** 执行结果数据 */
|
|
82
|
-
results?: unknown[];
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* HookManager 配置
|
|
86
|
-
*/
|
|
87
|
-
interface HookManagerOptions {
|
|
88
|
-
/** 组件名称(用于上下文) */
|
|
89
|
-
componentName?: string;
|
|
90
|
-
/** 组件版本(用于上下文) */
|
|
91
|
-
componentVersion?: string;
|
|
92
|
-
/** 默认优先级 */
|
|
93
|
-
defaultPriority?: number;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* HookManager
|
|
97
|
-
*
|
|
98
|
-
* 统一管理 Component 的 Hook
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```typescript
|
|
102
|
-
* const manager = new HookManager<MyContext>();
|
|
103
|
-
*
|
|
104
|
-
* // 注册 Hook
|
|
105
|
-
* manager.register("before-action", {
|
|
106
|
-
* name: "my-hook",
|
|
107
|
-
* priority: 10,
|
|
108
|
-
* execute: async (ctx) => { /* ... *\/ }
|
|
109
|
-
* });
|
|
110
|
-
*
|
|
111
|
-
* // 执行 Hooks
|
|
112
|
-
* await manager.execute("before-action", data);
|
|
113
|
-
* ```
|
|
114
|
-
*/
|
|
115
|
-
declare class HookManager<T = unknown> {
|
|
116
|
-
private _hooks;
|
|
117
|
-
private componentName;
|
|
118
|
-
private componentVersion;
|
|
119
|
-
private defaultPriority;
|
|
120
|
-
constructor(options?: HookManagerOptions);
|
|
121
|
-
/**
|
|
122
|
-
* 注册 Hook 到指定 hook 点
|
|
123
|
-
*/
|
|
124
|
-
register(hookPoint: string, hook: Hook<T>): void;
|
|
125
|
-
/**
|
|
126
|
-
* 注册多个 Hook
|
|
127
|
-
*/
|
|
128
|
-
registerMany(hookPoint: string, hooks: Hook<T>[]): void;
|
|
129
|
-
/**
|
|
130
|
-
* 按名称取消注册 Hook
|
|
131
|
-
* @returns 是否成功取消注册
|
|
132
|
-
*/
|
|
133
|
-
unregister(hookPoint: string, name: string): boolean;
|
|
134
|
-
/**
|
|
135
|
-
* 取消注册所有指定 hook 点的 Hook
|
|
136
|
-
*/
|
|
137
|
-
unregisterAll(hookPoint: string): void;
|
|
138
|
-
/**
|
|
139
|
-
* 执行指定 hook 点的所有 Hook
|
|
140
|
-
*/
|
|
141
|
-
execute(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
|
|
142
|
-
/**
|
|
143
|
-
* 执行指定 hook 点的所有 Hook 并收集返回值
|
|
144
|
-
*/
|
|
145
|
-
executeAndCollect<R>(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<R[]>;
|
|
146
|
-
/**
|
|
147
|
-
* 获取指定 hook 点的 Hook 数量
|
|
148
|
-
*/
|
|
149
|
-
count(hookPoint: string): number;
|
|
150
|
-
/**
|
|
151
|
-
* 清空所有 Hook
|
|
152
|
-
*/
|
|
153
|
-
clear(): void;
|
|
154
|
-
/**
|
|
155
|
-
* 获取所有已注册的 hook 点
|
|
156
|
-
*/
|
|
157
|
-
getHookPoints(): string[];
|
|
158
|
-
/**
|
|
159
|
-
* 检查 hook 点是否有任何 Hook
|
|
160
|
-
*/
|
|
161
|
-
hasHooks(hookPoint: string): boolean;
|
|
162
|
-
/**
|
|
163
|
-
* 设置组件信息
|
|
164
|
-
*/
|
|
165
|
-
setComponentInfo(name: string, version: string): void;
|
|
166
|
-
/**
|
|
167
|
-
* 获取所有注册的 hooks(只读副本)
|
|
168
|
-
*/
|
|
169
|
-
get hooks(): ReadonlyMap<string, Hook<T>[]>;
|
|
170
|
-
/**
|
|
171
|
-
* 执行 Hook 并支持干预机制
|
|
172
|
-
*
|
|
173
|
-
* 如果任何一个 Hook 返回 { stop: true },执行会停止
|
|
174
|
-
* Hook 可以通过返回 HookResult 来执行干预动作
|
|
175
|
-
*
|
|
176
|
-
* @param hookPoint Hook 点
|
|
177
|
-
* @param data 数据
|
|
178
|
-
* @param metadata 元数据
|
|
179
|
-
* @returns 执行结果
|
|
180
|
-
*/
|
|
181
|
-
executeWithIntervention(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<HookResult>;
|
|
182
|
-
/**
|
|
183
|
-
* 获取或创建 hook 列表
|
|
184
|
-
*/
|
|
185
|
-
private getOrCreateHooks;
|
|
186
|
-
/**
|
|
187
|
-
* 按优先级排序 Hook
|
|
188
|
-
*
|
|
189
|
-
* 优先级相同时保持原顺序(稳定排序)
|
|
190
|
-
*/
|
|
191
|
-
private sortHooks;
|
|
192
|
-
/**
|
|
193
|
-
* 创建 Hook 上下文
|
|
194
|
-
*/
|
|
195
|
-
private createContext;
|
|
196
|
-
/**
|
|
197
|
-
* 安全执行 Hook(错误隔离)
|
|
198
|
-
*/
|
|
199
|
-
private safeExecute;
|
|
200
|
-
/**
|
|
201
|
-
* 安全执行 Hook 并返回结果
|
|
202
|
-
*/
|
|
203
|
-
private safeExecuteAndReturn;
|
|
204
|
-
}
|
|
205
|
-
import { z as z5 } from "zod";
|
|
206
|
-
import { z as z4 } from "zod";
|
|
207
|
-
import { z } from "zod";
|
|
208
|
-
declare const BaseEventSchema: unknown;
|
|
209
|
-
type BaseEvent = z.infer<typeof BaseEventSchema>;
|
|
210
|
-
declare const WorkflowStartedEventSchema: unknown;
|
|
211
|
-
declare const WorkflowPausedEventSchema: unknown;
|
|
212
|
-
declare const WorkflowResumedEventSchema: unknown;
|
|
213
|
-
declare const WorkflowStoppedEventSchema: unknown;
|
|
214
|
-
declare const WorkflowCompletedEventSchema: unknown;
|
|
215
|
-
declare const WorkflowFailedEventSchema: unknown;
|
|
216
|
-
declare const WorkflowOutputEventSchema: unknown;
|
|
217
|
-
declare const NodeScheduledEventSchema: unknown;
|
|
218
|
-
declare const NodeStartedEventSchema: unknown;
|
|
219
|
-
declare const NodeProgressEventSchema: unknown;
|
|
220
|
-
declare const NodeCompletedEventSchema: unknown;
|
|
221
|
-
declare const NodeFailedEventSchema: unknown;
|
|
222
|
-
declare const NodeSkippedEventSchema: unknown;
|
|
223
|
-
declare const NodeDataEventSchema: unknown;
|
|
224
|
-
declare const NodeAddedEventSchema: unknown;
|
|
225
|
-
declare const NodeRemovedEventSchema: unknown;
|
|
226
|
-
declare const ControlPauseEventSchema: unknown;
|
|
227
|
-
declare const ControlResumeEventSchema: unknown;
|
|
228
|
-
declare const ControlStopEventSchema: unknown;
|
|
229
|
-
declare const NodeInterruptEventSchema: unknown;
|
|
230
|
-
declare const WorkflowAskUserEventSchema: unknown;
|
|
231
|
-
declare const WorkflowEventSchema: unknown;
|
|
232
|
-
type WorkflowEvent = z.infer<typeof WorkflowEventSchema>;
|
|
233
|
-
declare function createWorkflowEvent<T extends WorkflowEvent["type"]>(type: T, runId: string, data: Omit<Extract<WorkflowEvent, {
|
|
234
|
-
type: T;
|
|
235
|
-
}>, "type" | "run_id" | "timestamp">): any;
|
|
236
|
-
type EventHandler = (event: WorkflowEvent) => void | Promise<void>;
|
|
237
|
-
/**
|
|
238
|
-
* EventBus for workflow events.
|
|
239
|
-
* Custom event bus implementation with support for wildcard subscriptions.
|
|
240
|
-
*/
|
|
241
|
-
declare class EventBus {
|
|
242
|
-
private handlers;
|
|
243
|
-
private wildcardHandlers;
|
|
244
|
-
constructor();
|
|
245
|
-
/**
|
|
246
|
-
* Subscribe to a specific event type.
|
|
247
|
-
* @returns Unsubscribe function
|
|
248
|
-
*/
|
|
249
|
-
on(eventType: string, handler: EventHandler): () => void;
|
|
250
|
-
/**
|
|
251
|
-
* Subscribe to an event type only once (auto-unsubscribe after first call).
|
|
252
|
-
* @returns Unsubscribe function
|
|
253
|
-
*/
|
|
254
|
-
once(eventType: string, handler: EventHandler): () => void;
|
|
255
|
-
/**
|
|
256
|
-
* Unsubscribe from an event type.
|
|
257
|
-
*/
|
|
258
|
-
off(eventType: string, handler: EventHandler): void;
|
|
259
|
-
/**
|
|
260
|
-
* Subscribe to all events (wildcard subscription).
|
|
261
|
-
* @returns Unsubscribe function
|
|
262
|
-
*/
|
|
263
|
-
onAny(handler: EventHandler): () => void;
|
|
264
|
-
/**
|
|
265
|
-
* Publish an event to all subscribers.
|
|
266
|
-
* Handler errors are caught and do not crash the bus.
|
|
267
|
-
*
|
|
268
|
-
* @param event - The event to publish
|
|
269
|
-
* @param waitForHandlers - If true, waits for all async handlers to complete (default: false)
|
|
270
|
-
*/
|
|
271
|
-
publish(event: WorkflowEvent, waitForHandlers?: boolean): Promise<void>;
|
|
272
|
-
/**
|
|
273
|
-
* Execute a handler safely, catching any errors.
|
|
274
|
-
*/
|
|
275
|
-
private executeHandler;
|
|
276
|
-
/**
|
|
277
|
-
* Clear all subscriptions.
|
|
278
|
-
*/
|
|
279
|
-
clear(): void;
|
|
280
|
-
/**
|
|
281
|
-
* Get statistics about the event bus.
|
|
282
|
-
*/
|
|
283
|
-
getStats(): {
|
|
284
|
-
eventTypes: number;
|
|
285
|
-
wildcardHandlers: number;
|
|
286
|
-
totalHandlers: number;
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
import { ZodType as ZodType3, ZodTypeDef as ZodTypeDef2 } from "zod";
|
|
290
|
-
/**
|
|
291
|
-
* @fileoverview Environment types and interfaces
|
|
292
|
-
*
|
|
293
|
-
* 定义 Environment 的核心类型,包括:
|
|
294
|
-
* - Environment 配置
|
|
295
|
-
* - EnvEvent 事件类型
|
|
296
|
-
*/
|
|
297
|
-
/**
|
|
298
|
-
* Environment Config
|
|
299
|
-
*/
|
|
300
|
-
interface EnvironmentConfig {
|
|
301
|
-
/** 环境名称 */
|
|
302
|
-
name: string;
|
|
303
|
-
/** 环境版本 */
|
|
304
|
-
version: string;
|
|
305
|
-
/** 是否启用 */
|
|
306
|
-
enabled: boolean;
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* EnvEvent 元信息
|
|
310
|
-
*/
|
|
311
|
-
interface EnvEventMetadata {
|
|
312
|
-
/** 触发事件的 session ID */
|
|
313
|
-
trigger_session_id?: string;
|
|
314
|
-
/** 触发事件的 agent ID */
|
|
315
|
-
trigger_agent_id?: string;
|
|
316
|
-
/** Agent 名称 */
|
|
317
|
-
trigger_agent_name?: string;
|
|
318
|
-
/** 环境名称 */
|
|
319
|
-
env_name?: string;
|
|
320
|
-
/** 事件来源 */
|
|
321
|
-
source?: string;
|
|
322
|
-
/** 其他元数据 */
|
|
323
|
-
[key: string]: unknown;
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* EnvEvent
|
|
327
|
-
*
|
|
328
|
-
* 统一的事件机制,用于:
|
|
329
|
-
* - Stream 事件(stream.start, stream.text, stream.completed 等)
|
|
330
|
-
* - 工具调用事件(tool.call, tool.result 等)
|
|
331
|
-
* - 会话事件(session.created, session.updated 等)
|
|
332
|
-
* - Agent 事件(agent.thinking, agent.acting 等)
|
|
333
|
-
*/
|
|
334
|
-
interface EnvEvent<T = unknown> {
|
|
335
|
-
/** 事件 ID */
|
|
336
|
-
id: string;
|
|
337
|
-
/** 事件类型 */
|
|
338
|
-
type: string;
|
|
339
|
-
/** 时间戳 */
|
|
340
|
-
timestamp: number;
|
|
341
|
-
/** 元信息 */
|
|
342
|
-
metadata: EnvEventMetadata;
|
|
343
|
-
/** 事件负载 */
|
|
344
|
-
payload: T;
|
|
345
|
-
}
|
|
346
|
-
/**
|
|
347
|
-
* EnvEvent Handler
|
|
348
|
-
*/
|
|
349
|
-
type EnvEventHandler = (event: EnvEvent) => void | Promise<void>;
|
|
350
|
-
/**
|
|
351
|
-
* EnvEvent 创建参数(部分属性,可选)
|
|
352
|
-
*
|
|
353
|
-
* 用于 pushEnvEvent 方法,允许传入部分属性,自动补全必填字段
|
|
354
|
-
*/
|
|
355
|
-
interface EnvEventInput {
|
|
356
|
-
/** 事件类型(必填) */
|
|
357
|
-
type: string;
|
|
358
|
-
/** 事件 ID(可选,自动生成) */
|
|
359
|
-
id?: string;
|
|
360
|
-
/** 时间戳(可选,自动生成) */
|
|
361
|
-
timestamp?: number;
|
|
362
|
-
/** 元信息(可选,自动创建) */
|
|
363
|
-
metadata?: EnvEventMetadata;
|
|
364
|
-
/** 事件负载(可选) */
|
|
365
|
-
payload?: unknown;
|
|
366
|
-
}
|
|
367
|
-
/**
|
|
368
|
-
* Action 类型
|
|
369
|
-
*/
|
|
370
|
-
interface Action {
|
|
371
|
-
/** Action 类型 */
|
|
372
|
-
type: string;
|
|
373
|
-
/** Action 参数 */
|
|
374
|
-
params?: Record<string, unknown>;
|
|
375
|
-
}
|
|
376
|
-
/**
|
|
377
|
-
* Tool Result
|
|
378
|
-
*/
|
|
379
|
-
interface ToolResult {
|
|
380
|
-
/** 是否成功 */
|
|
381
|
-
success: boolean;
|
|
382
|
-
/** 输出内容 */
|
|
383
|
-
output: string | Record<string, unknown>;
|
|
384
|
-
/** 错误信息 */
|
|
385
|
-
error?: string;
|
|
386
|
-
}
|
|
387
|
-
/**
|
|
388
|
-
* Context
|
|
389
|
-
*/
|
|
390
|
-
interface Context {
|
|
391
|
-
/** 会话 ID */
|
|
392
|
-
sessionId?: string;
|
|
393
|
-
/** 用户 ID */
|
|
394
|
-
userId?: string;
|
|
395
|
-
/** 工作目录 */
|
|
396
|
-
workdir?: string;
|
|
397
|
-
/** 中断信号 */
|
|
398
|
-
abort?: AbortSignal;
|
|
399
|
-
/** 元数据 */
|
|
400
|
-
metadata?: Record<string, unknown>;
|
|
401
|
-
}
|
|
402
|
-
import { z as z2, ZodType, ZodError } from "zod";
|
|
403
|
-
/**
|
|
404
|
-
* 沙箱配置
|
|
405
|
-
*/
|
|
406
|
-
interface SandboxConfig {
|
|
407
|
-
/** 是否启用沙箱 */
|
|
408
|
-
enabled: boolean;
|
|
409
|
-
/** 沙箱类型 */
|
|
410
|
-
type: "native" | "docker";
|
|
411
|
-
/** 动作过滤 */
|
|
412
|
-
actionFilter?: {
|
|
413
|
-
/** 包含的动作 */
|
|
414
|
-
include?: string[];
|
|
415
|
-
/** 排除的动作 */
|
|
416
|
-
exclude?: string[];
|
|
417
|
-
};
|
|
418
|
-
/** 文件系统限制 */
|
|
419
|
-
filesystem?: {
|
|
420
|
-
/** 允许读取 */
|
|
421
|
-
allowRead?: string[];
|
|
422
|
-
/** 禁止读取 */
|
|
423
|
-
denyRead?: string[];
|
|
424
|
-
/** 允许写入 */
|
|
425
|
-
allowWrite?: string[];
|
|
426
|
-
/** 禁止写入 */
|
|
427
|
-
denyWrite?: string[];
|
|
428
|
-
};
|
|
429
|
-
/** 网络限制 */
|
|
430
|
-
network?: {
|
|
431
|
-
/** 允许的域名 */
|
|
432
|
-
allowedDomains?: string[];
|
|
433
|
-
/** 禁止的域名 */
|
|
434
|
-
deniedDomains?: string[];
|
|
435
|
-
};
|
|
436
|
-
/** Docker 配置 */
|
|
437
|
-
docker?: {
|
|
438
|
-
/** 镜像 */
|
|
439
|
-
image?: string;
|
|
440
|
-
/** 网络模式 */
|
|
441
|
-
networkMode?: "bridge" | "host" | "none";
|
|
442
|
-
/** 卷挂载 */
|
|
443
|
-
volumes?: Record<string, string>;
|
|
444
|
-
};
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* 工具执行上下文
|
|
448
|
-
*/
|
|
449
|
-
interface ToolContext {
|
|
450
|
-
/** 工作目录 */
|
|
451
|
-
workdir?: string;
|
|
452
|
-
/** 用户 ID */
|
|
453
|
-
user_id?: string;
|
|
454
|
-
/** 会话 ID */
|
|
455
|
-
session_id?: string;
|
|
456
|
-
/** 消息 ID */
|
|
457
|
-
message_id?: string;
|
|
458
|
-
/** 中断信号 */
|
|
459
|
-
abort?: AbortSignal;
|
|
460
|
-
/** 额外元数据 */
|
|
461
|
-
metadata?: Record<string, unknown>;
|
|
462
|
-
/** 沙箱配置 */
|
|
463
|
-
sandbox?: SandboxConfig;
|
|
464
|
-
/** 沙箱 Provider */
|
|
465
|
-
sandboxProvider?: unknown | null;
|
|
466
|
-
}
|
|
467
|
-
/**
|
|
468
|
-
* 工具结果元数据
|
|
469
|
-
*/
|
|
470
|
-
interface ToolResultMetadata {
|
|
471
|
-
/** 执行时间(毫秒) */
|
|
472
|
-
execution_time_ms: number;
|
|
473
|
-
/** 输出大小(字节) */
|
|
474
|
-
output_size?: number;
|
|
475
|
-
/** 标准输出 */
|
|
476
|
-
stdout?: string;
|
|
477
|
-
/** 标准错误 */
|
|
478
|
-
stderr?: string;
|
|
479
|
-
/** 退出码 */
|
|
480
|
-
exit_code?: number;
|
|
481
|
-
/** Token 使用量 */
|
|
482
|
-
usage?: {
|
|
483
|
-
inputTokens: number;
|
|
484
|
-
outputTokens: number;
|
|
485
|
-
totalTokens?: number;
|
|
486
|
-
};
|
|
487
|
-
/** 额外元数据 */
|
|
488
|
-
[key: string]: unknown;
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* 工具执行结果
|
|
492
|
-
*/
|
|
493
|
-
interface ToolResult2 {
|
|
494
|
-
/** 是否成功 */
|
|
495
|
-
success: boolean;
|
|
496
|
-
/** 输出内容 */
|
|
497
|
-
output: string | Record<string, unknown>;
|
|
498
|
-
/** 错误信息 */
|
|
499
|
-
error?: string;
|
|
500
|
-
/** 结果元数据 */
|
|
501
|
-
metadata?: ToolResultMetadata;
|
|
502
|
-
}
|
|
503
|
-
/**
|
|
504
|
-
* 工具元数据
|
|
505
|
-
*/
|
|
506
|
-
interface ToolMetadata {
|
|
507
|
-
/** 分类 */
|
|
508
|
-
category?: string;
|
|
509
|
-
/** 标签 */
|
|
510
|
-
tags?: string[];
|
|
511
|
-
/** 版本 */
|
|
512
|
-
version?: string;
|
|
513
|
-
/** 作者 */
|
|
514
|
-
author?: string;
|
|
515
|
-
/** 是否实验性 */
|
|
516
|
-
experimental?: boolean;
|
|
517
|
-
/** MCP 工具信息(MCP adapter 使用) */
|
|
518
|
-
mcpTool?: {
|
|
519
|
-
originalName: string;
|
|
520
|
-
originalDescription: string;
|
|
521
|
-
inputSchema?: unknown;
|
|
522
|
-
};
|
|
523
|
-
}
|
|
524
|
-
/**
|
|
525
|
-
* 工具沙箱配置
|
|
526
|
-
*/
|
|
527
|
-
interface ToolSandboxConfig {
|
|
528
|
-
/** 是否启用沙箱 */
|
|
529
|
-
enabled: boolean;
|
|
530
|
-
/** 沙箱配置 */
|
|
531
|
-
config?: Partial<SandboxConfig>;
|
|
532
|
-
}
|
|
533
|
-
/**
|
|
534
|
-
* 工具权限配置
|
|
535
|
-
*/
|
|
536
|
-
interface ToolPermissionConfig {
|
|
537
|
-
/** 是否需要用户确认 */
|
|
538
|
-
requireConfirmation?: boolean;
|
|
539
|
-
/** 权限级别 */
|
|
540
|
-
level?: "safe" | "dangerous" | "critical";
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* 工具信息
|
|
544
|
-
*/
|
|
545
|
-
interface ToolInfo<Parameters extends ZodType = ZodType> {
|
|
546
|
-
/** 工具名称 */
|
|
547
|
-
name: string;
|
|
548
|
-
/** 工具描述 */
|
|
549
|
-
description: string;
|
|
550
|
-
/** 参数 Schema */
|
|
551
|
-
parameters: Parameters;
|
|
552
|
-
/** 初始化函数(可选) */
|
|
553
|
-
init?: (ctx?: ToolContext) => Promise<void>;
|
|
554
|
-
/** 执行函数 */
|
|
555
|
-
execute: (args: z2.infer<Parameters>, ctx: ToolContext) => Promise<ToolResult2>;
|
|
556
|
-
/** 参数验证错误格式化(可选) */
|
|
557
|
-
formatValidationError?: (error: ZodError) => string;
|
|
558
|
-
/** 沙箱配置 */
|
|
559
|
-
sandbox?: ToolSandboxConfig;
|
|
560
|
-
/** 权限配置 */
|
|
561
|
-
permission?: ToolPermissionConfig;
|
|
562
|
-
/** 工具元数据 */
|
|
563
|
-
metadata?: ToolMetadata;
|
|
564
|
-
}
|
|
565
|
-
/**
|
|
566
|
-
* 工具类型别名
|
|
567
|
-
*/
|
|
568
|
-
type Tool = ToolInfo;
|
|
569
|
-
/**
|
|
570
|
-
* 工具注册来源
|
|
571
|
-
*/
|
|
572
|
-
type ToolSource = "built-in" | "plugin" | "dynamic";
|
|
573
|
-
/**
|
|
574
|
-
* 工具注册信息
|
|
575
|
-
*/
|
|
576
|
-
interface ToolRegistration {
|
|
577
|
-
/** 工具实例 */
|
|
578
|
-
tool: Tool;
|
|
579
|
-
/** 注册时间 */
|
|
580
|
-
registeredAt: number;
|
|
581
|
-
/** 注册来源 */
|
|
582
|
-
source: ToolSource;
|
|
583
|
-
/** 是否启用 */
|
|
584
|
-
enabled: boolean;
|
|
585
|
-
}
|
|
586
|
-
/**
|
|
587
|
-
* 工具执行请求
|
|
588
|
-
*/
|
|
589
|
-
interface ToolExecuteRequest {
|
|
590
|
-
/** 工具名称 */
|
|
591
|
-
name: string;
|
|
592
|
-
/** 参数 */
|
|
593
|
-
args: Record<string, unknown>;
|
|
594
|
-
/** 上下文 */
|
|
595
|
-
context: ToolContext;
|
|
596
|
-
/** 是否跳过 Hook */
|
|
597
|
-
skipHooks?: boolean;
|
|
598
|
-
}
|
|
599
|
-
/**
|
|
600
|
-
* 工具列表过滤器
|
|
601
|
-
*/
|
|
602
|
-
interface ToolListFilter {
|
|
603
|
-
/** 按名称过滤(支持 glob) */
|
|
604
|
-
name?: string | string[];
|
|
605
|
-
/** 按分类过滤 */
|
|
606
|
-
category?: string;
|
|
607
|
-
/** 按标签过滤 */
|
|
608
|
-
tags?: string[];
|
|
609
|
-
/** 按来源过滤 */
|
|
610
|
-
source?: ToolSource;
|
|
611
|
-
/** 只返回启用的工具 */
|
|
612
|
-
enabledOnly?: boolean;
|
|
613
|
-
/** 排除的工具 */
|
|
614
|
-
exclude?: string[];
|
|
615
|
-
}
|
|
616
|
-
/**
|
|
617
|
-
* LLM Message
|
|
618
|
-
*/
|
|
619
|
-
interface LLMMessage {
|
|
620
|
-
/** 消息角色 */
|
|
621
|
-
role: "system" | "user" | "assistant" | "tool";
|
|
622
|
-
/** 消息内容 */
|
|
623
|
-
content: string;
|
|
624
|
-
/** 工具调用(仅 assistant 角色) */
|
|
625
|
-
toolCalls?: ToolCall[];
|
|
626
|
-
/** 工具调用 ID(仅 tool 角色) */
|
|
627
|
-
toolCallId?: string;
|
|
628
|
-
/** 名称(用于 function 调用) */
|
|
629
|
-
name?: string;
|
|
630
|
-
}
|
|
631
|
-
/**
|
|
632
|
-
* Tool Call (OpenAI format)
|
|
633
|
-
*/
|
|
634
|
-
interface ToolCall {
|
|
635
|
-
/** 工具调用 ID */
|
|
636
|
-
id: string;
|
|
637
|
-
/** 工具名称 */
|
|
638
|
-
name: string;
|
|
639
|
-
/** 工具参数(JSON 字符串) */
|
|
640
|
-
arguments: string;
|
|
641
|
-
/** OpenAI 格式的 function 对象 */
|
|
642
|
-
function?: {
|
|
643
|
-
/** 函数名称 */
|
|
644
|
-
name: string;
|
|
645
|
-
/** 函数参数(JSON 字符串) */
|
|
646
|
-
arguments: string;
|
|
647
|
-
};
|
|
648
|
-
}
|
|
649
|
-
/**
|
|
650
|
-
* Usage Information
|
|
651
|
-
*/
|
|
652
|
-
interface UsageInfo {
|
|
653
|
-
/** Prompt Token 数 */
|
|
654
|
-
promptTokens: number;
|
|
655
|
-
/** Completion Token 数 */
|
|
656
|
-
completionTokens: number;
|
|
657
|
-
/** 总 Token 数 */
|
|
658
|
-
totalTokens: number;
|
|
659
|
-
}
|
|
660
|
-
/**
|
|
661
|
-
* LLM Output
|
|
662
|
-
*/
|
|
663
|
-
interface LLMOutput {
|
|
664
|
-
/** 生成的内容 */
|
|
665
|
-
content: string;
|
|
666
|
-
/** 推理/思考内容(支持 reasoning 模型的输出) */
|
|
667
|
-
reasoning?: string;
|
|
668
|
-
/** 完成原因 */
|
|
669
|
-
finishReason: "stop" | "length" | "content-filter" | "tool-calls" | "function-call";
|
|
670
|
-
/** 工具调用 */
|
|
671
|
-
toolCalls?: ToolCall[];
|
|
672
|
-
/** Usage 信息 */
|
|
673
|
-
usage?: UsageInfo;
|
|
674
|
-
/** 生成模型 */
|
|
675
|
-
model?: string;
|
|
676
|
-
}
|
|
677
|
-
/**
|
|
678
|
-
* 模型限制配置
|
|
679
|
-
*/
|
|
680
|
-
interface ModelLimits {
|
|
681
|
-
/** 上下文窗口大小(Token 数) */
|
|
682
|
-
contextWindow: number;
|
|
683
|
-
/** 压缩阈值(0-1),超过此比例触发上下文压缩 */
|
|
684
|
-
compactionThreshold?: number;
|
|
685
|
-
/** 最大输出 Token 数 */
|
|
686
|
-
maxOutputTokens?: number;
|
|
687
|
-
}
|
|
688
|
-
/**
|
|
689
|
-
* LLM Invoke 请求
|
|
690
|
-
*/
|
|
691
|
-
interface LLMInvokeRequest {
|
|
692
|
-
/** 消息列表 */
|
|
693
|
-
messages: LLMMessage[];
|
|
694
|
-
/** Provider ID(可选,默认使用配置的默认 Provider) */
|
|
695
|
-
providerId?: string;
|
|
696
|
-
/** 模型(可选,默认使用配置的默认模型) */
|
|
697
|
-
model?: string;
|
|
698
|
-
/** 工具列表 */
|
|
699
|
-
tools?: ToolInfo2[];
|
|
700
|
-
/** 温度参数 */
|
|
701
|
-
temperature?: number;
|
|
702
|
-
/** 最大 Token 数 */
|
|
703
|
-
maxTokens?: number;
|
|
704
|
-
/** Top P */
|
|
705
|
-
topP?: number;
|
|
706
|
-
/** 工具选择策略 */
|
|
707
|
-
toolChoice?: "auto" | "none" | {
|
|
708
|
-
type: "function";
|
|
709
|
-
function: {
|
|
710
|
-
name: string;
|
|
711
|
-
};
|
|
712
|
-
};
|
|
713
|
-
/** 停止序列 */
|
|
714
|
-
stopSequences?: string[];
|
|
715
|
-
/** Abort Signal */
|
|
716
|
-
abortSignal?: AbortSignal;
|
|
717
|
-
/** 调用上下文(用于事件元数据) */
|
|
718
|
-
context?: LLMInvokeContext;
|
|
719
|
-
/** 跳过上下文阈值检查(用于 SummaryAgent 等内部调用,避免循环触发压缩) */
|
|
720
|
-
skipThresholdCheck?: boolean;
|
|
721
|
-
}
|
|
722
|
-
/**
|
|
723
|
-
* LLM Invoke 结果
|
|
724
|
-
*/
|
|
725
|
-
interface LLMInvokeResult {
|
|
726
|
-
/** LLM 输出 */
|
|
727
|
-
output: LLMOutput;
|
|
728
|
-
/** 使用的 Provider ID */
|
|
729
|
-
providerId: string;
|
|
730
|
-
/** 使用的模型 */
|
|
731
|
-
model: string;
|
|
732
|
-
/** 调用延迟(毫秒) */
|
|
733
|
-
latencyMs: number;
|
|
734
|
-
}
|
|
735
|
-
/**
|
|
736
|
-
* Tool Info(用于 LLM 工具调用)
|
|
737
|
-
*/
|
|
738
|
-
interface ToolInfo2 {
|
|
739
|
-
/** 工具名称 */
|
|
740
|
-
name: string;
|
|
741
|
-
/** 工具描述 */
|
|
742
|
-
description?: string;
|
|
743
|
-
/** 工具参数 Schema */
|
|
744
|
-
parameters: Record<string, unknown>;
|
|
745
|
-
}
|
|
746
|
-
/**
|
|
747
|
-
* LLM 调用上下文(用于事件元数据)
|
|
748
|
-
*/
|
|
749
|
-
interface LLMInvokeContext {
|
|
750
|
-
/** Session ID */
|
|
751
|
-
sessionId?: string;
|
|
752
|
-
/** Message ID */
|
|
753
|
-
messageId?: string;
|
|
754
|
-
}
|
|
755
|
-
type AbortSignalType = AbortSignal;
|
|
756
|
-
/**
|
|
757
|
-
* Agent 执行上下文
|
|
758
|
-
*/
|
|
759
|
-
interface AgentContext {
|
|
760
|
-
/** 会话 ID */
|
|
761
|
-
sessionId?: string;
|
|
762
|
-
/** 消息 ID */
|
|
763
|
-
messageId?: string;
|
|
764
|
-
/** 中断信号 */
|
|
765
|
-
abort?: AbortSignalType;
|
|
766
|
-
/** 额外的上下文信息(会添加到 system prompt) */
|
|
767
|
-
additionInfo?: string;
|
|
768
|
-
/** 元数据 */
|
|
769
|
-
metadata?: Record<string, unknown>;
|
|
770
|
-
/** Agent 类型(用于选择 Behavior Spec) */
|
|
771
|
-
agentType?: string;
|
|
772
|
-
/** 使用的模型 */
|
|
773
|
-
model?: string;
|
|
774
|
-
/** 允许的工具列表(上下文级别,会覆盖 agent 配置) */
|
|
775
|
-
allowedTools?: string[];
|
|
776
|
-
/** 拒绝的工具列表(上下文级别,会覆盖 agent 配置) */
|
|
777
|
-
deniedTools?: string[];
|
|
778
|
-
/** 是否过滤 history 中的 tool 消息(上下文级别,会覆盖 agent 配置) */
|
|
779
|
-
filterHistory?: boolean;
|
|
780
|
-
}
|
|
781
|
-
import { ZodType as ZodType2, ZodTypeDef } from "zod";
|
|
782
|
-
/**
|
|
783
|
-
* ConfigSource 类型
|
|
784
|
-
*/
|
|
785
|
-
type ConfigSourceType = "memory" | "file" | "env" | "remote";
|
|
786
|
-
/**
|
|
787
|
-
* SourceRegistration - Source 注册配置
|
|
788
|
-
*/
|
|
789
|
-
interface SourceRegistration {
|
|
790
|
-
/** Source 类型 */
|
|
791
|
-
type: ConfigSourceType;
|
|
792
|
-
/** 相对路径(file 类型使用,基于 XDG_DATA_HOME) */
|
|
793
|
-
relativePath?: string;
|
|
794
|
-
/** 环境变量前缀(env 类型使用) */
|
|
795
|
-
envPrefix?: string;
|
|
796
|
-
/** 优先级,默认值:memory=0, file=10, env=20 */
|
|
797
|
-
priority?: number;
|
|
798
|
-
/** 是否可选(file 类型使用,文件不存在时不报错) */
|
|
799
|
-
optional?: boolean;
|
|
800
|
-
/** 是否启用文件监听(file 类型使用,默认 true) */
|
|
801
|
-
watch?: boolean;
|
|
802
|
-
}
|
|
803
|
-
/**
|
|
804
|
-
* KeyRegistration - 配置 Key 注册
|
|
805
|
-
*/
|
|
806
|
-
interface KeyRegistration {
|
|
807
|
-
/** 配置 Key(支持点号嵌套,如 "llm.provider") */
|
|
808
|
-
key: string;
|
|
809
|
-
/** 优先使用的 Source 列表(按优先级从低到高) */
|
|
810
|
-
sources: ConfigSourceType[];
|
|
811
|
-
/** 自定义优先级(覆盖默认优先级) */
|
|
812
|
-
priority?: number;
|
|
813
|
-
/** 默认值 */
|
|
814
|
-
default?: unknown;
|
|
815
|
-
}
|
|
816
|
-
/**
|
|
817
|
-
* ComponentRegistration - Component 注册配置
|
|
818
|
-
*/
|
|
819
|
-
interface ComponentRegistration {
|
|
820
|
-
/** Component 名称 */
|
|
821
|
-
name: string;
|
|
822
|
-
/** 声明需要的 Source */
|
|
823
|
-
sources: SourceRegistration[];
|
|
824
|
-
/** 声明需要的配置 Key */
|
|
825
|
-
keys: KeyRegistration[];
|
|
826
|
-
/** 配置 Schema(用于验证,可选) */
|
|
827
|
-
schema?: ZodType2<unknown, ZodTypeDef, unknown>;
|
|
828
|
-
/** 默认值 */
|
|
829
|
-
defaults?: Record<string, unknown>;
|
|
830
|
-
}
|
|
831
|
-
/**
|
|
832
|
-
* ConfigSource 接口
|
|
833
|
-
*/
|
|
834
|
-
interface ConfigSource {
|
|
835
|
-
readonly name: ConfigSourceType;
|
|
836
|
-
readonly priority: number;
|
|
837
|
-
read(key: string): unknown | undefined;
|
|
838
|
-
write?(key: string, value: unknown): boolean;
|
|
839
|
-
delete?(key: string): boolean;
|
|
840
|
-
list(): Array<{
|
|
841
|
-
key: string;
|
|
842
|
-
value: unknown;
|
|
843
|
-
}>;
|
|
844
|
-
watch?(callback: (event: ConfigChangeEvent) => void): () => void;
|
|
845
|
-
}
|
|
846
|
-
/**
|
|
847
|
-
* Config 变更事件
|
|
848
|
-
*/
|
|
849
|
-
interface ConfigChangeEvent {
|
|
850
|
-
type: "change" | "add" | "delete";
|
|
851
|
-
key: string;
|
|
852
|
-
oldValue: unknown;
|
|
853
|
-
newValue: unknown;
|
|
854
|
-
source: ConfigSourceType;
|
|
855
|
-
timestamp: number;
|
|
856
|
-
}
|
|
857
|
-
/**
|
|
858
|
-
* ConfigComponent 配置
|
|
859
|
-
*/
|
|
860
|
-
interface ConfigComponentOptions {
|
|
861
|
-
/** 持久化文件路径 */
|
|
862
|
-
persistFile?: string;
|
|
863
|
-
/** 防抖延迟(ms) */
|
|
864
|
-
debounceMs?: number;
|
|
865
|
-
/** 最大批量大小 */
|
|
866
|
-
maxBatchSize?: number;
|
|
867
|
-
}
|
|
868
|
-
/**
|
|
869
|
-
* ConfigComponent
|
|
870
|
-
*/
|
|
871
|
-
declare class ConfigComponent extends BaseComponent {
|
|
872
|
-
readonly name = "config";
|
|
873
|
-
readonly version = "1.0.0";
|
|
874
|
-
private memorySource;
|
|
875
|
-
private sources;
|
|
876
|
-
private componentSchemas;
|
|
877
|
-
private componentPaths;
|
|
878
|
-
private watchers;
|
|
879
|
-
/** Source watch unsubscribe 函数 */
|
|
880
|
-
private sourceUnwatchFns;
|
|
881
|
-
private persistQueue;
|
|
882
|
-
private persistFile?;
|
|
883
|
-
/** Key 注册表:key -> KeyRegistration */
|
|
884
|
-
private keyRegistry;
|
|
885
|
-
/** Component 注册表:name -> ComponentRegistration */
|
|
886
|
-
private componentRegistrations;
|
|
887
|
-
/** 协议解析器 */
|
|
888
|
-
private protocolResolver?;
|
|
889
|
-
/** XDG_DATA_HOME 缓存(用于测试注入) */
|
|
890
|
-
private __xdgDataHome?;
|
|
891
|
-
/** Source 注册配置(延迟创建) */
|
|
892
|
-
private pendingSources;
|
|
893
|
-
/** Source 唯一标识缓存:type:relativePath -> true */
|
|
894
|
-
private sourceKeys;
|
|
895
|
-
/**
|
|
896
|
-
* 获取 XDG_DATA_HOME(动态获取,支持运行时覆盖)
|
|
897
|
-
*/
|
|
898
|
-
getXdgDataHome(): string;
|
|
899
|
-
/**
|
|
900
|
-
* 设置 XDG_DATA_HOME(用于测试)
|
|
901
|
-
*/
|
|
902
|
-
setXdgDataHome(path: string): void;
|
|
903
|
-
constructor(options?: ConfigComponentOptions);
|
|
904
|
-
/**
|
|
905
|
-
* 注册 Component Schema
|
|
906
|
-
*/
|
|
907
|
-
register(schema: ComponentSchema): void;
|
|
908
|
-
/**
|
|
909
|
-
* 注销 Component
|
|
910
|
-
*/
|
|
911
|
-
unregister(name: string): boolean;
|
|
912
|
-
/**
|
|
913
|
-
* 检查是否已注册
|
|
914
|
-
*/
|
|
915
|
-
isRegistered(name: string): boolean;
|
|
916
|
-
/**
|
|
917
|
-
* 列出所有注册的 Component
|
|
918
|
-
*/
|
|
919
|
-
listComponents(): string[];
|
|
920
|
-
/**
|
|
921
|
-
* 获取配置值(支持 xxx.yyy.zzz 嵌套访问)
|
|
922
|
-
* 支持两种模式:
|
|
923
|
-
* 1. 完整 key:直接查找,如 "component.key" -> 读取 "component.key"
|
|
924
|
-
* 2. 嵌套对象:查找 component 对象后从中获取属性,如 "component.nested.deep"
|
|
925
|
-
* -> 先找 "component.nested",再获取其 "deep" 属性
|
|
926
|
-
*/
|
|
927
|
-
get(key: string): unknown;
|
|
928
|
-
/**
|
|
929
|
-
* 批量获取
|
|
930
|
-
*/
|
|
931
|
-
getMany(keys: string[]): Record<string, unknown>;
|
|
932
|
-
/**
|
|
933
|
-
* 设置配置值(内存级别 + 异步持久化)
|
|
934
|
-
*/
|
|
935
|
-
set(key: string, value: unknown): Promise<void>;
|
|
936
|
-
/**
|
|
937
|
-
* 批量设置
|
|
938
|
-
*/
|
|
939
|
-
setMany(config: Record<string, unknown>): Promise<void>;
|
|
940
|
-
/**
|
|
941
|
-
* 获取 Component 的路径
|
|
942
|
-
*/
|
|
943
|
-
getPath(component: string, subPath?: string): string;
|
|
944
|
-
/**
|
|
945
|
-
* 手动持久化
|
|
946
|
-
*/
|
|
947
|
-
save(key?: string): Promise<void>;
|
|
948
|
-
/**
|
|
949
|
-
* 重置到默认值
|
|
950
|
-
*/
|
|
951
|
-
reset(key: string): Promise<void>;
|
|
952
|
-
/**
|
|
953
|
-
* 监听配置变更
|
|
954
|
-
*/
|
|
955
|
-
watch(pattern: string, callback: ComponentWatchCallback): () => void;
|
|
956
|
-
/**
|
|
957
|
-
* 添加配置源
|
|
958
|
-
*/
|
|
959
|
-
addSource(source: ConfigSource): void;
|
|
960
|
-
/**
|
|
961
|
-
* 获取 Source 唯一标识
|
|
962
|
-
*/
|
|
963
|
-
private getSourceKey;
|
|
964
|
-
/**
|
|
965
|
-
* 移除配置源
|
|
966
|
-
*/
|
|
967
|
-
removeSource(name: ConfigSourceType): boolean;
|
|
968
|
-
/**
|
|
969
|
-
* 关闭所有配置源的 watcher(用于优雅退出)
|
|
970
|
-
*/
|
|
971
|
-
unwatchAll(): void;
|
|
972
|
-
/**
|
|
973
|
-
* 获取配置源列表
|
|
974
|
-
*/
|
|
975
|
-
getSources(): ConfigSource[];
|
|
976
|
-
private getNestedValue;
|
|
977
|
-
private notifyWatchers;
|
|
978
|
-
private persistToFile;
|
|
979
|
-
/**
|
|
980
|
-
* 注册配置 Source
|
|
981
|
-
*
|
|
982
|
-
* Source 会在首次 load() 时被创建
|
|
983
|
-
*
|
|
984
|
-
* @example
|
|
985
|
-
* config.registerSource({ type: "file", relativePath: "llm-config.jsonc" });
|
|
986
|
-
* config.registerSource({ type: "env", envPrefix: "LLM" });
|
|
987
|
-
*/
|
|
988
|
-
registerSource(registration: SourceRegistration): void;
|
|
989
|
-
/**
|
|
990
|
-
* 创建 Source 实例
|
|
991
|
-
*/
|
|
992
|
-
private createSource;
|
|
993
|
-
/**
|
|
994
|
-
* 确保所有待创建的 Source 已被创建
|
|
995
|
-
*/
|
|
996
|
-
private ensureSourcesCreated;
|
|
997
|
-
/**
|
|
998
|
-
* 注册配置 Key
|
|
999
|
-
*
|
|
1000
|
-
* @example
|
|
1001
|
-
* config.registerKeys([
|
|
1002
|
-
* { key: "llm.provider", sources: ["file", "memory"], default: "openai" },
|
|
1003
|
-
* { key: "llm.model", sources: ["file"] },
|
|
1004
|
-
* ]);
|
|
1005
|
-
*/
|
|
1006
|
-
registerKeys(keys: KeyRegistration[]): void;
|
|
1007
|
-
/**
|
|
1008
|
-
* 注册 Component(一次性注册 Source 和 Keys)
|
|
1009
|
-
*
|
|
1010
|
-
* @example
|
|
1011
|
-
* config.registerComponent({
|
|
1012
|
-
* name: "llm",
|
|
1013
|
-
* sources: [
|
|
1014
|
-
* { type: "file", relativePath: "llm-config.jsonc" },
|
|
1015
|
-
* { type: "env", envPrefix: "LLM" },
|
|
1016
|
-
* ],
|
|
1017
|
-
* keys: [
|
|
1018
|
-
* { key: "llm.provider", sources: ["file", "env", "memory"] },
|
|
1019
|
-
* ],
|
|
1020
|
-
* });
|
|
1021
|
-
*/
|
|
1022
|
-
registerComponent(registration: ComponentRegistration): void;
|
|
1023
|
-
/**
|
|
1024
|
-
* 按需加载配置
|
|
1025
|
-
*
|
|
1026
|
-
* 从注册的 Source 加载已注册 Keys 的配置值到内存中
|
|
1027
|
-
*
|
|
1028
|
-
* @param componentName - 可选,指定要加载的 Component 名称
|
|
1029
|
-
*
|
|
1030
|
-
* @example
|
|
1031
|
-
* // 加载所有已注册的 Keys
|
|
1032
|
-
* await config.load();
|
|
1033
|
-
*
|
|
1034
|
-
* // 只加载指定 Component 的 Keys
|
|
1035
|
-
* await config.load("llm");
|
|
1036
|
-
*/
|
|
1037
|
-
load(componentName?: string): Promise<void>;
|
|
1038
|
-
/**
|
|
1039
|
-
* 获取已注册的 Source 列表
|
|
1040
|
-
*/
|
|
1041
|
-
getRegisteredSources(): ConfigSource[];
|
|
1042
|
-
}
|
|
1043
|
-
/**
|
|
1044
|
-
* 服务配置接口
|
|
1045
|
-
*/
|
|
1046
|
-
interface ServiceConfig {
|
|
1047
|
-
/** 环境配置 */
|
|
1048
|
-
environment?: {
|
|
1049
|
-
name?: string;
|
|
1050
|
-
version?: string;
|
|
1051
|
-
};
|
|
1052
|
-
/** 组件配置映射 */
|
|
1053
|
-
components?: Record<string, ComponentConfigEntry>;
|
|
1054
|
-
}
|
|
1055
|
-
/**
|
|
1056
|
-
* 组件配置项
|
|
1057
|
-
*/
|
|
1058
|
-
interface ComponentConfigEntry {
|
|
1059
|
-
/** 配置文件路径(可选) */
|
|
1060
|
-
configPath?: string;
|
|
1061
|
-
/** 环境变量前缀(可选) */
|
|
1062
|
-
envPrefix?: string;
|
|
1063
|
-
/** 直接配置对象(可选) */
|
|
1064
|
-
config?: Record<string, unknown>;
|
|
1065
|
-
/** 是否启用(可选,默认 true) */
|
|
1066
|
-
enabled?: boolean;
|
|
1067
|
-
}
|
|
1068
|
-
/**
|
|
1069
|
-
* 组件初始化选项(由 generateComponentOptions 生成)
|
|
1070
|
-
*/
|
|
1071
|
-
interface GeneratedComponentOptions {
|
|
1072
|
-
/** ConfigComponent 实例 */
|
|
1073
|
-
configComponent: ConfigComponent;
|
|
1074
|
-
/** 配置文件路径 */
|
|
1075
|
-
configPath?: string;
|
|
1076
|
-
/** 环境变量前缀 */
|
|
1077
|
-
envPrefix?: string;
|
|
1078
|
-
/** 直接配置对象 */
|
|
1079
|
-
config?: Record<string, unknown>;
|
|
1080
|
-
/** 允许的其他属性 */
|
|
1081
|
-
[key: string]: unknown;
|
|
1082
|
-
}
|
|
1083
|
-
/**
|
|
1084
|
-
* Environment 接口
|
|
1085
|
-
*
|
|
1086
|
-
* 定义 Agent 运行时的核心能力接口。
|
|
1087
|
-
*/
|
|
1088
|
-
interface Environment {
|
|
1089
|
-
/** 环境名称 */
|
|
1090
|
-
readonly name: string;
|
|
1091
|
-
/** 环境版本 */
|
|
1092
|
-
readonly version: string;
|
|
1093
|
-
/** 获取环境配置 */
|
|
1094
|
-
getConfig(): EnvironmentConfig;
|
|
1095
|
-
/**
|
|
1096
|
-
* 注册组件
|
|
1097
|
-
*/
|
|
1098
|
-
registerComponent(component: Component): void;
|
|
1099
|
-
/**
|
|
1100
|
-
* 注销组件
|
|
1101
|
-
*/
|
|
1102
|
-
unregisterComponent(name: string): void;
|
|
1103
|
-
/**
|
|
1104
|
-
* 获取组件
|
|
1105
|
-
*/
|
|
1106
|
-
getComponent<T extends Component>(name: string): T | undefined;
|
|
1107
|
-
/**
|
|
1108
|
-
* 列出所有组件
|
|
1109
|
-
*/
|
|
1110
|
-
listComponents(): Component[];
|
|
1111
|
-
/**
|
|
1112
|
-
* 处理自然语言查询(核心入口)
|
|
1113
|
-
*
|
|
1114
|
-
* @param query 用户查询文本
|
|
1115
|
-
* @param context 执行上下文(可选)
|
|
1116
|
-
* @returns 完整的文本响应
|
|
1117
|
-
*/
|
|
1118
|
-
handle_query(query: string, context?: AgentContext): Promise<string>;
|
|
1119
|
-
/**
|
|
1120
|
-
* 处理动作
|
|
1121
|
-
*/
|
|
1122
|
-
handle_action(action: Action, context: Context): Promise<ToolResult>;
|
|
1123
|
-
/**
|
|
1124
|
-
* 订阅 EnvEvent(返回取消订阅函数)
|
|
1125
|
-
*/
|
|
1126
|
-
subscribe(handler: EnvEventHandler): () => void;
|
|
1127
|
-
/**
|
|
1128
|
-
* 订阅指定类型事件
|
|
1129
|
-
*/
|
|
1130
|
-
subscribeTo(eventType: string | string[], handler: EnvEventHandler): () => void;
|
|
1131
|
-
/**
|
|
1132
|
-
* 订阅所有事件(通配符)
|
|
1133
|
-
*/
|
|
1134
|
-
subscribeAll(handler: EnvEventHandler): () => void;
|
|
1135
|
-
/**
|
|
1136
|
-
* 推送事件
|
|
1137
|
-
*
|
|
1138
|
-
* @param event EnvEvent 或 EnvEventInput(部分属性)
|
|
1139
|
-
*/
|
|
1140
|
-
pushEnvEvent(event: EnvEvent | EnvEventInput): void;
|
|
1141
|
-
/**
|
|
1142
|
-
* 从配置文件加载服务配置
|
|
1143
|
-
*
|
|
1144
|
-
* @param configPath 配置文件路径(基于 XDG_DATA_HOME)
|
|
1145
|
-
* @returns 解析后的服务配置对象
|
|
1146
|
-
*/
|
|
1147
|
-
loadServiceConfig(configPath: string): Promise<ServiceConfig>;
|
|
1148
|
-
/**
|
|
1149
|
-
* 生成组件初始化选项
|
|
1150
|
-
*
|
|
1151
|
-
* 根据组件配置项生成可用于初始化的选项对象。
|
|
1152
|
-
*
|
|
1153
|
-
* @param componentName 组件名称
|
|
1154
|
-
* @param configEntry 组件配置项
|
|
1155
|
-
* @returns 生成的组件初始化选项
|
|
1156
|
-
*/
|
|
1157
|
-
generateComponentOptions(componentName: string, configEntry: ComponentConfigEntry): GeneratedComponentOptions;
|
|
1158
|
-
/**
|
|
1159
|
-
* 注册组件并用配置初始化
|
|
1160
|
-
*
|
|
1161
|
-
* 便捷方法:注册组件并通过 ConfigComponent 初始化。
|
|
1162
|
-
*
|
|
1163
|
-
* @param component 要注册的组件实例
|
|
1164
|
-
* @param configEntry 组件配置项
|
|
1165
|
-
*/
|
|
1166
|
-
registerComponentWithConfig(component: Component, configEntry: ComponentConfigEntry): Promise<void>;
|
|
1167
|
-
/**
|
|
1168
|
-
* 从配置文件初始化整个环境
|
|
1169
|
-
*
|
|
1170
|
-
* 加载服务配置,注册并初始化所有配置的组件。
|
|
1171
|
-
*
|
|
1172
|
-
* @param configPath 配置文件路径(基于 XDG_DATA_HOME)
|
|
1173
|
-
*/
|
|
1174
|
-
initFromConfig(configPath: string): Promise<void>;
|
|
1175
|
-
}
|
|
1176
|
-
/**
|
|
1177
|
-
* Component 状态
|
|
1178
|
-
*/
|
|
1179
|
-
type ComponentStatus = "created" | "initializing" | "running" | "stopping" | "stopped" | "error";
|
|
1180
|
-
/**
|
|
1181
|
-
* Component 配置
|
|
1182
|
-
*/
|
|
1183
|
-
interface ComponentConfig {
|
|
1184
|
-
name: string;
|
|
1185
|
-
version: string;
|
|
1186
|
-
enabled: boolean;
|
|
1187
|
-
options?: Record<string, unknown>;
|
|
1188
|
-
/**
|
|
1189
|
-
* Environment 实例(可选)
|
|
1190
|
-
*
|
|
1191
|
-
* 推荐在 init 时接收 env 实例,以便:
|
|
1192
|
-
* - 通过 env.getComponent() 获取其他组件
|
|
1193
|
-
* - 通过 env.pushEnvEvent() 发布事件
|
|
1194
|
-
* - 通过 env.handle_query() 处理查询
|
|
1195
|
-
*
|
|
1196
|
-
* 如果未提供,部分 Component 仍可正常工作(如测试场景)
|
|
1197
|
-
*
|
|
1198
|
-
* @example
|
|
1199
|
-
* ```typescript
|
|
1200
|
-
* async onInit(): Promise<void> {
|
|
1201
|
-
* if (this.env) {
|
|
1202
|
-
* const configComp = this.env.getComponent("config");
|
|
1203
|
-
* }
|
|
1204
|
-
* }
|
|
1205
|
-
* ```
|
|
1206
|
-
*/
|
|
1207
|
-
env?: Environment;
|
|
1208
|
-
}
|
|
1209
|
-
/**
|
|
1210
|
-
* Component 路径声明
|
|
1211
|
-
*/
|
|
1212
|
-
interface ComponentPaths {
|
|
1213
|
-
base: "config" | "state" | "data" | "cache";
|
|
1214
|
-
subPath: string;
|
|
1215
|
-
isDirectory: boolean;
|
|
1216
|
-
}
|
|
1217
|
-
/**
|
|
1218
|
-
* Component Schema(注册配置)
|
|
1219
|
-
*/
|
|
1220
|
-
interface ComponentSchema {
|
|
1221
|
-
/** Component 名称 */
|
|
1222
|
-
name: string;
|
|
1223
|
-
/** 配置 Schema(Zod) */
|
|
1224
|
-
schema: ZodType3<unknown, ZodTypeDef2, unknown>;
|
|
1225
|
-
/** 默认值(从 Schema 提取) */
|
|
1226
|
-
defaults: Record<string, unknown>;
|
|
1227
|
-
/** 路径声明 */
|
|
1228
|
-
paths?: Record<string, ComponentPaths>;
|
|
1229
|
-
/** 元信息 */
|
|
1230
|
-
metadata?: {
|
|
1231
|
-
description?: string;
|
|
1232
|
-
version?: string;
|
|
1233
|
-
author?: string;
|
|
1234
|
-
};
|
|
1235
|
-
}
|
|
1236
|
-
/**
|
|
1237
|
-
* Component 配置变更事件
|
|
1238
|
-
*/
|
|
1239
|
-
interface ComponentConfigChangeEvent {
|
|
1240
|
-
/** Component 名称 */
|
|
1241
|
-
component: string;
|
|
1242
|
-
/** 变更的配置键 */
|
|
1243
|
-
key: string;
|
|
1244
|
-
/** 旧值 */
|
|
1245
|
-
oldValue: unknown;
|
|
1246
|
-
/** 新值 */
|
|
1247
|
-
newValue: unknown;
|
|
1248
|
-
/** 是否持久化 */
|
|
1249
|
-
persisted: boolean;
|
|
1250
|
-
/** 时间戳 */
|
|
1251
|
-
timestamp: number;
|
|
1252
|
-
}
|
|
1253
|
-
/**
|
|
1254
|
-
* Component 配置监听回调
|
|
1255
|
-
*/
|
|
1256
|
-
type ComponentWatchCallback = (event: ComponentConfigChangeEvent) => void | Promise<void>;
|
|
1257
|
-
/**
|
|
1258
|
-
* Component 基类接口
|
|
1259
|
-
*/
|
|
1260
|
-
interface Component {
|
|
1261
|
-
/** Component 名称 */
|
|
1262
|
-
readonly name: string;
|
|
1263
|
-
/** Component 版本 */
|
|
1264
|
-
readonly version: string;
|
|
1265
|
-
/** 获取状态 */
|
|
1266
|
-
getStatus(): ComponentStatus;
|
|
1267
|
-
/** 获取配置 */
|
|
1268
|
-
getConfig(): ComponentConfig;
|
|
1269
|
-
/** 初始化 */
|
|
1270
|
-
init(config: ComponentConfig): Promise<void>;
|
|
1271
|
-
/** 启动 */
|
|
1272
|
-
start(): Promise<void>;
|
|
1273
|
-
/** 停止 */
|
|
1274
|
-
stop(): Promise<void>;
|
|
1275
|
-
}
|
|
1276
|
-
/**
|
|
1277
|
-
* Component 基类实现
|
|
1278
|
-
*/
|
|
1279
|
-
declare abstract class BaseComponent implements Component {
|
|
1280
|
-
abstract readonly name: string;
|
|
1281
|
-
abstract readonly version: string;
|
|
1282
|
-
protected _status: ComponentStatus;
|
|
1283
|
-
protected _enabled: boolean;
|
|
1284
|
-
protected env: Environment;
|
|
1285
|
-
/** Hook 管理器 - 子类初始化时由构造函数设置 */
|
|
1286
|
-
readonly hookManager: HookManager;
|
|
1287
|
-
constructor();
|
|
1288
|
-
getStatus(): ComponentStatus;
|
|
1289
|
-
getConfig(): ComponentConfig;
|
|
1290
|
-
/**
|
|
1291
|
-
* 检查 Environment 是否已初始化
|
|
1292
|
-
*/
|
|
1293
|
-
protected isEnvInitialized(): boolean;
|
|
1294
|
-
protected setStatus(status: ComponentStatus): void;
|
|
1295
|
-
/**
|
|
1296
|
-
* 获取 Environment 实例
|
|
1297
|
-
*/
|
|
1298
|
-
protected getEnv(): Environment;
|
|
1299
|
-
init(config?: ComponentConfig): Promise<void>;
|
|
1300
|
-
start(): Promise<void>;
|
|
1301
|
-
stop(): Promise<void>;
|
|
1302
|
-
/**
|
|
1303
|
-
* 子类可覆盖的钩子方法
|
|
1304
|
-
*/
|
|
1305
|
-
protected onInit(): Promise<void>;
|
|
1306
|
-
protected onStart(): Promise<void>;
|
|
1307
|
-
protected onStop(): Promise<void>;
|
|
1308
|
-
/**
|
|
1309
|
-
* 注册 Hook
|
|
1310
|
-
*
|
|
1311
|
-
* @param hookPoint Hook 点名称
|
|
1312
|
-
* @param hook Hook 实例
|
|
1313
|
-
*/
|
|
1314
|
-
protected registerHook<T = unknown>(hookPoint: string, hook: Hook<T>): void;
|
|
1315
|
-
/**
|
|
1316
|
-
* 便捷方法:快速注册 Hook
|
|
1317
|
-
*/
|
|
1318
|
-
protected addHook<T = unknown>(hookPoint: string, name: string, fn: HookFn<T>, priority?: number): void;
|
|
1319
|
-
/**
|
|
1320
|
-
* 便捷方法:按名称取消注册 Hook
|
|
1321
|
-
*/
|
|
1322
|
-
protected removeHook(hookPoint: string, name: string): boolean;
|
|
1323
|
-
/**
|
|
1324
|
-
* 执行 Hook
|
|
1325
|
-
*/
|
|
1326
|
-
protected executeHooks<T = unknown>(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
|
|
1327
|
-
/**
|
|
1328
|
-
* 获取 ConfigComponent 实例
|
|
1329
|
-
*
|
|
1330
|
-
* 子类可使用此方法获取配置组件,支持热更新
|
|
1331
|
-
*/
|
|
1332
|
-
protected getConfigComponent(): {
|
|
1333
|
-
get(key: string): unknown;
|
|
1334
|
-
} | undefined;
|
|
1335
|
-
/**
|
|
1336
|
-
* 动态获取运行时配置值
|
|
1337
|
-
*
|
|
1338
|
-
* 优先从 ConfigComponent 获取(支持热更新),否则返回默认值
|
|
1339
|
-
* 适用于运行时可能变化的配置(如 API Keys、模型参数)
|
|
1340
|
-
*
|
|
1341
|
-
* @param key 配置键(支持点号分隔的路径,如 "llm.temperature")
|
|
1342
|
-
* @param defaultValue 默认值
|
|
1343
|
-
* @returns 配置值或默认值
|
|
1344
|
-
*
|
|
1345
|
-
* @example
|
|
1346
|
-
* ```typescript
|
|
1347
|
-
* // 获取 LLM 温度配置(可能支持热更新)
|
|
1348
|
-
* const temperature = this.getRuntimeConfig("llm.temperature", 0.7);
|
|
1349
|
-
*
|
|
1350
|
-
* // 获取 API Key
|
|
1351
|
-
* const apiKey = this.getRuntimeConfig<string>("llm.providers.openai.apiKey");
|
|
1352
|
-
* ```
|
|
1353
|
-
*/
|
|
1354
|
-
protected getRuntimeConfig<T>(key: string, defaultValue: T): T;
|
|
1355
|
-
}
|
|
1356
|
-
/**
|
|
1357
|
-
* Checkpoint type
|
|
1358
|
-
*/
|
|
1359
|
-
type CheckpointType = "compact" | "manual";
|
|
1360
|
-
/**
|
|
1361
|
-
* Checkpoint metadata (stored in Session.metadata)
|
|
1362
|
-
*/
|
|
1363
|
-
interface CheckpointMeta {
|
|
1364
|
-
id: string;
|
|
1365
|
-
messageIndex: number;
|
|
1366
|
-
title: string;
|
|
1367
|
-
createdAt: number;
|
|
1368
|
-
type: CheckpointType;
|
|
1369
|
-
}
|
|
1370
|
-
/**
|
|
1371
|
-
* User Intent type - represents a user's intent extracted from conversation
|
|
1372
|
-
*/
|
|
1373
|
-
type UserIntent = string;
|
|
1374
|
-
/**
|
|
1375
|
-
* Recent message for checkpoint
|
|
1376
|
-
*
|
|
1377
|
-
* Stores a simplified version of a message for context preservation
|
|
1378
|
-
*/
|
|
1379
|
-
interface RecentMessage {
|
|
1380
|
-
role: string;
|
|
1381
|
-
content: string;
|
|
1382
|
-
}
|
|
1383
|
-
/**
|
|
1384
|
-
* Complete checkpoint information (includes generated content)
|
|
1385
|
-
*/
|
|
1386
|
-
interface SessionCheckpoint extends CheckpointMeta {
|
|
1387
|
-
summary: string;
|
|
1388
|
-
processKeyPoints: string[];
|
|
1389
|
-
currentState: string;
|
|
1390
|
-
nextSteps: string[];
|
|
1391
|
-
messageCountBefore: number;
|
|
1392
|
-
metadata?: Record<string, unknown>;
|
|
1393
|
-
/** User intents extracted from the conversation */
|
|
1394
|
-
userIntents: UserIntent[];
|
|
1395
|
-
/**
|
|
1396
|
-
* Recent messages preserved from the compacted conversation
|
|
1397
|
-
*
|
|
1398
|
-
* Contains the last N turns of user query + assistant text response
|
|
1399
|
-
* (excluding tool calls and tool results).
|
|
1400
|
-
* This allows the checkpoint to serve as a fresh history context.
|
|
1401
|
-
*
|
|
1402
|
-
* Format: [user, assistant, user, assistant, ...] from most recent
|
|
1403
|
-
*/
|
|
1404
|
-
recentMessages?: RecentMessage[];
|
|
1405
|
-
}
|
|
1406
|
-
/**
|
|
1407
|
-
* Checkpoints metadata in session
|
|
1408
|
-
*/
|
|
1409
|
-
interface SessionCheckpointsMeta {
|
|
1410
|
-
latestCheckpointId?: string;
|
|
1411
|
-
checkpoints: CheckpointMeta[];
|
|
1412
|
-
}
|
|
1413
|
-
/**
|
|
1414
|
-
* Session entity
|
|
1415
|
-
*/
|
|
1416
|
-
interface Session {
|
|
1417
|
-
id: string;
|
|
1418
|
-
title: string;
|
|
1419
|
-
directory: string;
|
|
1420
|
-
parentID?: string;
|
|
1421
|
-
createdAt: number;
|
|
1422
|
-
updatedAt: number;
|
|
1423
|
-
messageCount: number;
|
|
1424
|
-
metadata?: SessionMetadata;
|
|
1425
|
-
}
|
|
1426
|
-
/**
|
|
1427
|
-
* Session metadata
|
|
1428
|
-
*/
|
|
1429
|
-
interface SessionMetadata extends Record<string, unknown> {
|
|
1430
|
-
/** Total number of messages in the session */
|
|
1431
|
-
messageCount?: number;
|
|
1432
|
-
checkpoints?: SessionCheckpointsMeta;
|
|
1433
|
-
checkpointDetails?: Record<string, SessionCheckpoint>;
|
|
1434
|
-
}
|
|
1435
|
-
/**
|
|
1436
|
-
* Session message
|
|
1437
|
-
*/
|
|
1438
|
-
interface SessionMessage {
|
|
1439
|
-
id: string;
|
|
1440
|
-
sessionID: string;
|
|
1441
|
-
role: Role;
|
|
1442
|
-
content: string;
|
|
1443
|
-
timestamp: number;
|
|
1444
|
-
parts?: MessagePart[];
|
|
1445
|
-
/** Whether the message has been archived (compacted) */
|
|
1446
|
-
isArchived?: boolean;
|
|
1447
|
-
/** Timestamp when the message was archived */
|
|
1448
|
-
archivedAt?: number;
|
|
1449
|
-
/** Checkpoint ID this message belongs to */
|
|
1450
|
-
checkpointId?: string;
|
|
1451
|
-
/** Message metadata (includes checkpoint info if this is a checkpoint message) */
|
|
1452
|
-
metadata?: MessageMetadata;
|
|
1453
|
-
}
|
|
1454
|
-
/**
|
|
1455
|
-
* Message metadata
|
|
1456
|
-
*/
|
|
1457
|
-
interface MessageMetadata extends Record<string, unknown> {
|
|
1458
|
-
/** 消息类型标识,用于过滤和分类 */
|
|
1459
|
-
type?: "tool_call" | "tool_result" | "checkpoint" | "user_intent" | "workflow.node.start" | "workflow.node.interrupt" | "workflow.node.end" | "workflow.node.resume";
|
|
1460
|
-
isCheckpoint?: boolean;
|
|
1461
|
-
checkpointId?: string;
|
|
1462
|
-
checkpointMeta?: SessionCheckpoint;
|
|
1463
|
-
/** Workflow node ID (for workflow.node.* messages) */
|
|
1464
|
-
workflowNodeId?: string;
|
|
1465
|
-
/** Workflow node type (for workflow.node.* messages) */
|
|
1466
|
-
workflowNodeType?: string;
|
|
1467
|
-
/** Agent session ID (for agent nodes in workflow) */
|
|
1468
|
-
agentSessionId?: string;
|
|
1469
|
-
/** User response (for workflow.node.resume messages) */
|
|
1470
|
-
response?: string;
|
|
1471
|
-
/** Success flag (for workflow.node.end messages) */
|
|
1472
|
-
success?: boolean;
|
|
1473
|
-
}
|
|
1474
|
-
/**
|
|
1475
|
-
* Message role
|
|
1476
|
-
*/
|
|
1477
|
-
type Role = "user" | "assistant" | "system" | "tool" | "workflow.node.start" | "workflow.node.interrupt" | "workflow.node.end" | "workflow.node.resume";
|
|
1478
|
-
/**
|
|
1479
|
-
* Message part
|
|
1480
|
-
*/
|
|
1481
|
-
type MessagePart = SessionPart;
|
|
1482
|
-
/**
|
|
1483
|
-
* Text part - plain text content
|
|
1484
|
-
*/
|
|
1485
|
-
interface TextPart {
|
|
1486
|
-
type: "text";
|
|
1487
|
-
content: string;
|
|
1488
|
-
/** If true, this part was generated by the system (e.g., auto-summary) */
|
|
1489
|
-
synthetic?: boolean;
|
|
1490
|
-
/** If true, this part should be ignored (e.g., replaced by reasoning) */
|
|
1491
|
-
ignored?: boolean;
|
|
1492
|
-
}
|
|
1493
|
-
/**
|
|
1494
|
-
* Tool call part - represents a tool invocation
|
|
1495
|
-
*/
|
|
1496
|
-
interface ToolCallPart {
|
|
1497
|
-
type: "tool-call";
|
|
1498
|
-
/** AI SDK standard field name */
|
|
1499
|
-
toolCallId: string;
|
|
1500
|
-
toolName: string;
|
|
1501
|
-
/** Tool arguments - can be string (JSON) or object */
|
|
1502
|
-
arguments: string | Record<string, unknown>;
|
|
1503
|
-
state: "pending" | "running";
|
|
1504
|
-
}
|
|
1505
|
-
/**
|
|
1506
|
-
* Tool result part - represents the result of a tool execution
|
|
1507
|
-
*/
|
|
1508
|
-
interface ToolResultPart {
|
|
1509
|
-
type: "tool-result";
|
|
1510
|
-
toolCallId: string;
|
|
1511
|
-
toolName: string;
|
|
1512
|
-
output: string;
|
|
1513
|
-
error?: string;
|
|
1514
|
-
state: "completed" | "error";
|
|
1515
|
-
}
|
|
1516
|
-
/**
|
|
1517
|
-
* Reasoning part - AI thinking/thought content
|
|
1518
|
-
*/
|
|
1519
|
-
interface ReasoningPart {
|
|
1520
|
-
type: "reasoning";
|
|
1521
|
-
/** Reasoning content */
|
|
1522
|
-
content: string;
|
|
1523
|
-
/** Reasoning type (for different models) */
|
|
1524
|
-
reasoningType?: "core" | "effort" | "summary";
|
|
1525
|
-
/** State */
|
|
1526
|
-
state?: "thinking" | "completed";
|
|
1527
|
-
/** Whether to collapse by default (like AI SDK default) */
|
|
1528
|
-
isCollapsed?: boolean;
|
|
1529
|
-
/** Metadata */
|
|
1530
|
-
metadata?: {
|
|
1531
|
-
tokenCount?: number;
|
|
1532
|
-
time?: {
|
|
1533
|
-
start: number;
|
|
1534
|
-
end?: number;
|
|
1535
|
-
};
|
|
1536
|
-
};
|
|
1537
|
-
}
|
|
1538
|
-
/**
|
|
1539
|
-
* File part - file attachment
|
|
1540
|
-
*/
|
|
1541
|
-
interface FilePart {
|
|
1542
|
-
type: "file";
|
|
1543
|
-
mime: string;
|
|
1544
|
-
url: string;
|
|
1545
|
-
filename?: string;
|
|
1546
|
-
}
|
|
1547
|
-
/**
|
|
1548
|
-
* Checkpoint part - compaction checkpoint marker
|
|
1549
|
-
*
|
|
1550
|
-
* Represents a checkpoint message in the conversation history.
|
|
1551
|
-
* This is stored as a user role message with markdown content summarizing
|
|
1552
|
-
* the compacted conversation segment.
|
|
1553
|
-
*/
|
|
1554
|
-
interface CheckpointPart {
|
|
1555
|
-
type: "checkpoint";
|
|
1556
|
-
/** Checkpoint ID reference */
|
|
1557
|
-
checkpointId: string;
|
|
1558
|
-
/** Markdown content summarizing the checkpoint */
|
|
1559
|
-
content: string;
|
|
1560
|
-
/** Title of the checkpoint */
|
|
1561
|
-
title?: string;
|
|
1562
|
-
/** Process key points */
|
|
1563
|
-
processKeyPoints?: string[];
|
|
1564
|
-
/** Current state description */
|
|
1565
|
-
currentState?: string;
|
|
1566
|
-
/** Next steps */
|
|
1567
|
-
nextSteps?: string[];
|
|
1568
|
-
/** Number of messages that were compacted */
|
|
1569
|
-
messageCountBefore?: number;
|
|
1570
|
-
/** Creation timestamp */
|
|
1571
|
-
createdAt?: number;
|
|
1572
|
-
}
|
|
1573
|
-
/**
|
|
1574
|
-
* Union type for all Session Part types
|
|
1575
|
-
*/
|
|
1576
|
-
type SessionPart = TextPart | ToolCallPart | ToolResultPart | ReasoningPart | FilePart | CheckpointPart | WorkflowNodeStartPart | WorkflowNodeInterruptPart | WorkflowNodeEndPart | WorkflowNodeResumePart;
|
|
1577
|
-
/**
|
|
1578
|
-
* workflow.node.start 消息 part (对应 tool-call)
|
|
1579
|
-
*/
|
|
1580
|
-
interface WorkflowNodeStartPart {
|
|
1581
|
-
type: "workflow-node-start";
|
|
1582
|
-
nodeId: string;
|
|
1583
|
-
nodeType: string;
|
|
1584
|
-
input?: unknown;
|
|
1585
|
-
startTime: number;
|
|
1586
|
-
/** Agent session ID (only for agent nodes) */
|
|
1587
|
-
agentSessionId?: string;
|
|
1588
|
-
}
|
|
1589
|
-
/**
|
|
1590
|
-
* workflow.node.interrupt 消息 part
|
|
1591
|
-
*/
|
|
1592
|
-
interface WorkflowNodeInterruptPart {
|
|
1593
|
-
type: "workflow-node-interrupt";
|
|
1594
|
-
nodeId: string;
|
|
1595
|
-
nodeType: string;
|
|
1596
|
-
query: string;
|
|
1597
|
-
options?: string[];
|
|
1598
|
-
timestamp: number;
|
|
1599
|
-
/** Agent session ID (only for agent nodes) */
|
|
1600
|
-
agentSessionId?: string;
|
|
1601
|
-
}
|
|
1602
|
-
/**
|
|
1603
|
-
* workflow.node.end 消息 part (对应 tool-result)
|
|
1604
|
-
*/
|
|
1605
|
-
interface WorkflowNodeEndPart {
|
|
1606
|
-
type: "workflow-node-end";
|
|
1607
|
-
nodeId: string;
|
|
1608
|
-
nodeType: string;
|
|
1609
|
-
output?: unknown;
|
|
1610
|
-
error?: string;
|
|
1611
|
-
durationMs: number;
|
|
1612
|
-
/** Agent session ID (only for agent nodes) */
|
|
1613
|
-
agentSessionId?: string;
|
|
1614
|
-
}
|
|
1615
|
-
/**
|
|
1616
|
-
* workflow.node.resume 消息 part
|
|
1617
|
-
*/
|
|
1618
|
-
interface WorkflowNodeResumePart {
|
|
1619
|
-
type: "workflow-node-resume";
|
|
1620
|
-
nodeId: string;
|
|
1621
|
-
response: string;
|
|
1622
|
-
timestamp: number;
|
|
1623
|
-
/** Agent session ID (only for agent nodes) */
|
|
1624
|
-
agentSessionId?: string;
|
|
1625
|
-
}
|
|
1626
|
-
/**
|
|
1627
|
-
* Options for creating a session
|
|
1628
|
-
*/
|
|
1629
|
-
interface CreateSessionOptions {
|
|
1630
|
-
/** Custom session ID (optional, will be generated if not provided) */
|
|
1631
|
-
id?: string;
|
|
1632
|
-
title?: string;
|
|
1633
|
-
directory?: string;
|
|
1634
|
-
parentID?: string;
|
|
1635
|
-
metadata?: Record<string, unknown>;
|
|
1636
|
-
hooks?: SessionHooksConfig;
|
|
1637
|
-
}
|
|
1638
|
-
/**
|
|
1639
|
-
* Options for updating a session
|
|
1640
|
-
*/
|
|
1641
|
-
interface UpdateSessionOptions {
|
|
1642
|
-
title?: string;
|
|
1643
|
-
metadata?: Record<string, unknown>;
|
|
1644
|
-
}
|
|
1645
|
-
/**
|
|
1646
|
-
* Options for listing sessions
|
|
1647
|
-
*/
|
|
1648
|
-
interface ListSessionsOptions {
|
|
1649
|
-
filter?: {
|
|
1650
|
-
metadata?: Record<string, unknown>;
|
|
1651
|
-
startTime?: number;
|
|
1652
|
-
endTime?: number;
|
|
1653
|
-
};
|
|
1654
|
-
sort?: {
|
|
1655
|
-
field: "updatedAt" | "createdAt" | "title";
|
|
1656
|
-
order: "asc" | "desc";
|
|
1657
|
-
};
|
|
1658
|
-
offset?: number;
|
|
1659
|
-
limit?: number;
|
|
1660
|
-
}
|
|
1661
|
-
/**
|
|
1662
|
-
* Options for adding a message
|
|
1663
|
-
*/
|
|
1664
|
-
interface AddMessageOptions {
|
|
1665
|
-
role: Role;
|
|
1666
|
-
content: string;
|
|
1667
|
-
parts?: MessagePart[];
|
|
1668
|
-
metadata?: MessageMetadata;
|
|
1669
|
-
}
|
|
1670
|
-
/**
|
|
1671
|
-
* Options for compact operation
|
|
1672
|
-
*/
|
|
1673
|
-
interface CompactOptions {
|
|
1674
|
-
/** User-provided summary hint for checkpoint generation */
|
|
1675
|
-
summary?: string;
|
|
1676
|
-
/** User-provided process key points hint */
|
|
1677
|
-
processKeyPoints?: string[];
|
|
1678
|
-
/** User-provided current state hint */
|
|
1679
|
-
currentState?: string;
|
|
1680
|
-
/** User-provided next steps hint */
|
|
1681
|
-
nextSteps?: string[];
|
|
1682
|
-
/** Whether this is auto-triggered compact */
|
|
1683
|
-
auto?: boolean;
|
|
1684
|
-
/**
|
|
1685
|
-
* Scenario hint for guiding checkpoint generation
|
|
1686
|
-
*
|
|
1687
|
-
* A natural language description that provides context about the current scenario.
|
|
1688
|
-
* This helps the SummaryAgent generate more relevant checkpoints based on the specific
|
|
1689
|
-
* situation. Examples:
|
|
1690
|
-
* - "Task execution in progress: implementing feature X, current working directory is /path/to/project"
|
|
1691
|
-
* - "Bug investigation: analyzing crash dump, current hypothesis is memory corruption"
|
|
1692
|
-
* - "Code review session: reviewing PR #123, focus on security implications"
|
|
1693
|
-
*
|
|
1694
|
-
* When provided, this will be prepended to the system prompt to guide the checkpoint
|
|
1695
|
-
* generation with scenario-specific emphasis.
|
|
1696
|
-
*/
|
|
1697
|
-
scenarioHint?: string;
|
|
1698
|
-
}
|
|
1699
|
-
/**
|
|
1700
|
-
* Result of compact operation
|
|
1701
|
-
*/
|
|
1702
|
-
interface CompactResult {
|
|
1703
|
-
checkpoint: SessionCheckpoint;
|
|
1704
|
-
deletedMessageCount: number;
|
|
1705
|
-
remainingMessageCount: number;
|
|
1706
|
-
checkpointCount: number;
|
|
1707
|
-
}
|
|
1708
|
-
/**
|
|
1709
|
-
* Preview of compact operation
|
|
1710
|
-
*/
|
|
1711
|
-
interface CompactPreview {
|
|
1712
|
-
messageCountToCompact: number;
|
|
1713
|
-
estimatedCheckpointTokens: number;
|
|
1714
|
-
wouldTrigger: boolean;
|
|
1715
|
-
}
|
|
1716
|
-
/**
|
|
1717
|
-
* Options for getting session context
|
|
1718
|
-
*/
|
|
1719
|
-
interface GetContextOptions {
|
|
1720
|
-
/** Start from specified checkpoint */
|
|
1721
|
-
fromCheckpoint?: string;
|
|
1722
|
-
/** Start from latest checkpoint (default: true) */
|
|
1723
|
-
fromLatestCheckpoint?: boolean;
|
|
1724
|
-
/** Read full history (ignore checkpoints) */
|
|
1725
|
-
fullHistory?: boolean;
|
|
1726
|
-
/** Message limit */
|
|
1727
|
-
messageLimit?: number;
|
|
1728
|
-
/** Minimum user messages to include (ensures starting from user message boundary) */
|
|
1729
|
-
minUserMessages?: number;
|
|
1730
|
-
/** Include checkpoint details */
|
|
1731
|
-
includeCheckpoints?: boolean;
|
|
1732
|
-
}
|
|
1733
|
-
/**
|
|
1734
|
-
* Result of getting session context
|
|
1735
|
-
*/
|
|
1736
|
-
interface GetContextResult {
|
|
1737
|
-
session: Session;
|
|
1738
|
-
checkpoints: SessionCheckpoint[];
|
|
1739
|
-
startCheckpoint?: SessionCheckpoint;
|
|
1740
|
-
messages: SessionMessage[];
|
|
1741
|
-
totalMessageCount: number;
|
|
1742
|
-
activeMessageCount: number;
|
|
1743
|
-
archivedMessageCount: number;
|
|
1744
|
-
estimatedTokens: number;
|
|
1745
|
-
}
|
|
1746
|
-
/**
|
|
1747
|
-
* Session hook callback
|
|
1748
|
-
*/
|
|
1749
|
-
type SessionHookCallback = (context?: any) => Promise<void>;
|
|
1750
|
-
/**
|
|
1751
|
-
* Session hook definition
|
|
1752
|
-
*/
|
|
1753
|
-
interface SessionHook {
|
|
1754
|
-
name: string;
|
|
1755
|
-
before?: SessionHookCallback;
|
|
1756
|
-
after?: SessionHookCallback;
|
|
1757
|
-
}
|
|
1758
|
-
/**
|
|
1759
|
-
* Session hooks configuration
|
|
1760
|
-
*/
|
|
1761
|
-
interface SessionHooksConfig {
|
|
1762
|
-
[key: string]: SessionHook[];
|
|
1763
|
-
}
|
|
1764
|
-
/**
|
|
1765
|
-
* Options for searching messages
|
|
1766
|
-
*/
|
|
1767
|
-
interface SearchMessagesOptions {
|
|
1768
|
-
/** Search query (supports AND/OR/NOT syntax) */
|
|
1769
|
-
query: string;
|
|
1770
|
-
/** Limit search to specific session ID */
|
|
1771
|
-
sessionId?: string;
|
|
1772
|
-
/** Maximum number of results per session */
|
|
1773
|
-
limit?: number;
|
|
1774
|
-
/** Maximum total results */
|
|
1775
|
-
maxResults?: number;
|
|
1776
|
-
/** Only search messages before this timestamp */
|
|
1777
|
-
beforeTime?: number;
|
|
1778
|
-
/** Only search messages after this timestamp */
|
|
1779
|
-
afterTime?: number;
|
|
1780
|
-
/** Include archived messages */
|
|
1781
|
-
includeArchived?: boolean;
|
|
1782
|
-
/** Include context (messages before/after matches) */
|
|
1783
|
-
includeContext?: boolean;
|
|
1784
|
-
/** Number of context lines before/after */
|
|
1785
|
-
contextLines?: number;
|
|
1786
|
-
}
|
|
1787
|
-
/**
|
|
1788
|
-
* A single message match
|
|
1789
|
-
*/
|
|
1790
|
-
interface MessageMatch {
|
|
1791
|
-
/** Message ID */
|
|
1792
|
-
messageId: string;
|
|
1793
|
-
/** Session ID */
|
|
1794
|
-
sessionId: string;
|
|
1795
|
-
/** Message role */
|
|
1796
|
-
role: Role;
|
|
1797
|
-
/** Message content */
|
|
1798
|
-
content: string;
|
|
1799
|
-
/** Message timestamp */
|
|
1800
|
-
timestamp: number;
|
|
1801
|
-
/** Context before the match (if enabled) */
|
|
1802
|
-
contextBefore?: string;
|
|
1803
|
-
/** Context after the match (if enabled) */
|
|
1804
|
-
contextAfter?: string;
|
|
1805
|
-
/** Match score (relevance) */
|
|
1806
|
-
score?: number;
|
|
1807
|
-
}
|
|
1808
|
-
/**
|
|
1809
|
-
* Search result aggregated by session
|
|
1810
|
-
*/
|
|
1811
|
-
interface SearchResult {
|
|
1812
|
-
/** Session ID */
|
|
1813
|
-
sessionId: string;
|
|
1814
|
-
/** Session title */
|
|
1815
|
-
sessionTitle: string;
|
|
1816
|
-
/** Session directory */
|
|
1817
|
-
sessionDirectory: string;
|
|
1818
|
-
/** Session update timestamp */
|
|
1819
|
-
updatedAt: number;
|
|
1820
|
-
/** Total message count in session */
|
|
1821
|
-
messageCount: number;
|
|
1822
|
-
/** Matching messages in this session */
|
|
1823
|
-
matches: MessageMatch[];
|
|
1824
|
-
}
|
|
1825
|
-
/**
|
|
1826
|
-
* SessionComponent
|
|
1827
|
-
*
|
|
1828
|
-
* Manages session lifecycle including:
|
|
1829
|
-
* - Session CRUD operations
|
|
1830
|
-
* - Message management
|
|
1831
|
-
* - Active session tracking
|
|
1832
|
-
*/
|
|
1833
|
-
declare class SessionComponent extends BaseComponent {
|
|
1834
|
-
readonly name = "session";
|
|
1835
|
-
readonly version = "1.1.0";
|
|
1836
|
-
private config?;
|
|
1837
|
-
private store?;
|
|
1838
|
-
private activeSessionId?;
|
|
1839
|
-
private hooksConfig;
|
|
1840
|
-
/** ConfigComponent 用于配置管理 */
|
|
1841
|
-
private configComponent?;
|
|
1842
|
-
/** 配置变更 watcher 清理函数 */
|
|
1843
|
-
private configWatcher?;
|
|
1844
|
-
/** Summary Agent for checkpoint generation [new] */
|
|
1845
|
-
private summaryAgent?;
|
|
1846
|
-
/** PromptComponent reference for SummaryAgent */
|
|
1847
|
-
private promptComponent?;
|
|
1848
|
-
/** LLMComponent reference for SummaryAgent */
|
|
1849
|
-
private llmComponent?;
|
|
1850
|
-
constructor();
|
|
1851
|
-
/**
|
|
1852
|
-
* Initialize the component
|
|
1853
|
-
*
|
|
1854
|
-
* 配置加载优先级(从高到低):
|
|
1855
|
-
* 1. Object - 直接传入的 config 对象
|
|
1856
|
-
* 2. Env - 环境变量(通过 envPrefix 配置前缀)
|
|
1857
|
-
* 3. File - 配置文件(通过 configPath 指定)
|
|
1858
|
-
*/
|
|
1859
|
-
init(config?: ComponentConfig): Promise<void>;
|
|
1860
|
-
/**
|
|
1861
|
-
* 获取默认 session 数据库路径
|
|
1862
|
-
*/
|
|
1863
|
-
private getDefaultSessionDbPath;
|
|
1864
|
-
/**
|
|
1865
|
-
* 注册配置到 ConfigComponent
|
|
1866
|
-
*
|
|
1867
|
-
* 配置加载顺序(优先级从低到高):
|
|
1868
|
-
* 1. Defaults - 默认值(最低)
|
|
1869
|
-
* 2. FileSource - 从配置文件加载
|
|
1870
|
-
* 3. EnvSource - 从环境变量加载
|
|
1871
|
-
* 4. config 对象 - 直接传入的配置(最高)
|
|
1872
|
-
*/
|
|
1873
|
-
private registerConfig;
|
|
1874
|
-
/**
|
|
1875
|
-
* 构建最终配置对象
|
|
1876
|
-
*/
|
|
1877
|
-
private buildConfig;
|
|
1878
|
-
/**
|
|
1879
|
-
* 将配置对象展平为点号路径
|
|
1880
|
-
*/
|
|
1881
|
-
private flattenConfig;
|
|
1882
|
-
/**
|
|
1883
|
-
* 注册配置热更新监听
|
|
1884
|
-
*/
|
|
1885
|
-
private registerConfigWatcher;
|
|
1886
|
-
/**
|
|
1887
|
-
* 处理配置变更
|
|
1888
|
-
*/
|
|
1889
|
-
protected onConfigChange(event: {
|
|
1890
|
-
key: string;
|
|
1891
|
-
oldValue?: unknown;
|
|
1892
|
-
newValue?: unknown;
|
|
1893
|
-
}): void;
|
|
1894
|
-
/**
|
|
1895
|
-
* Start the component
|
|
1896
|
-
*/
|
|
1897
|
-
onStart(): Promise<void>;
|
|
1898
|
-
/**
|
|
1899
|
-
* Stop the component
|
|
1900
|
-
*/
|
|
1901
|
-
onStop(): Promise<void>;
|
|
1902
|
-
/**
|
|
1903
|
-
* Create a new session
|
|
1904
|
-
*/
|
|
1905
|
-
create(options?: CreateSessionOptions): Promise<Session>;
|
|
1906
|
-
/**
|
|
1907
|
-
* Get a session by ID
|
|
1908
|
-
*/
|
|
1909
|
-
get(id: string): Promise<Session | undefined>;
|
|
1910
|
-
/**
|
|
1911
|
-
* Get a session with full context (for Memory Agent)
|
|
1912
|
-
*/
|
|
1913
|
-
getSession(id: string): Promise<{
|
|
1914
|
-
sessionId: string;
|
|
1915
|
-
title: string;
|
|
1916
|
-
messages: any[];
|
|
1917
|
-
} | null>;
|
|
1918
|
-
/**
|
|
1919
|
-
* Search sessions by query (for Memory Agent)
|
|
1920
|
-
*
|
|
1921
|
-
* Simple implementation: list all sessions and return them for the agent to filter
|
|
1922
|
-
*/
|
|
1923
|
-
searchSessions(query: string, options?: {
|
|
1924
|
-
session_id?: string;
|
|
1925
|
-
limit?: number;
|
|
1926
|
-
}): Promise<Array<{
|
|
1927
|
-
sessionId: string;
|
|
1928
|
-
title: string;
|
|
1929
|
-
preview: string;
|
|
1930
|
-
timestamp: number;
|
|
1931
|
-
}>>;
|
|
1932
|
-
/**
|
|
1933
|
-
* List all sessions
|
|
1934
|
-
*/
|
|
1935
|
-
list(options?: ListSessionsOptions): Promise<Session[]>;
|
|
1936
|
-
/**
|
|
1937
|
-
* Get total session count
|
|
1938
|
-
*/
|
|
1939
|
-
getCount(): Promise<number>;
|
|
1940
|
-
/**
|
|
1941
|
-
* Update a session
|
|
1942
|
-
*/
|
|
1943
|
-
update(id: string, updates: UpdateSessionOptions): Promise<boolean>;
|
|
1944
|
-
/**
|
|
1945
|
-
* Delete a session
|
|
1946
|
-
*/
|
|
1947
|
-
delete(id: string): Promise<boolean>;
|
|
1948
|
-
/**
|
|
1949
|
-
* Get the current active session
|
|
1950
|
-
*/
|
|
1951
|
-
getActiveSession(): Session | undefined;
|
|
1952
|
-
/**
|
|
1953
|
-
* Get the active session ID
|
|
1954
|
-
*/
|
|
1955
|
-
getActiveSessionId(): string | undefined;
|
|
1956
|
-
/**
|
|
1957
|
-
* Set the active session
|
|
1958
|
-
*/
|
|
1959
|
-
setActiveSession(id: string): Promise<boolean>;
|
|
1960
|
-
/**
|
|
1961
|
-
* Add a message to a session
|
|
1962
|
-
*/
|
|
1963
|
-
addMessage(sessionId: string, message: AddMessageOptions): Promise<string | undefined>;
|
|
1964
|
-
/**
|
|
1965
|
-
* Get messages from a session
|
|
1966
|
-
*/
|
|
1967
|
-
getMessages(sessionId: string, options?: {
|
|
1968
|
-
offset?: number;
|
|
1969
|
-
limit?: number;
|
|
1970
|
-
}): Promise<SessionMessage[]>;
|
|
1971
|
-
/**
|
|
1972
|
-
* Get message count for a session
|
|
1973
|
-
*/
|
|
1974
|
-
getMessageCount(sessionId: string, includeArchived?: boolean): Promise<number>;
|
|
1975
|
-
/**
|
|
1976
|
-
* Get indexes of all messages with the specified role
|
|
1977
|
-
* @param sessionId - Session ID
|
|
1978
|
-
* @param role - Role to filter by (e.g., "user", "assistant")
|
|
1979
|
-
* @returns Array of message indexes (0-indexed, in chronological order)
|
|
1980
|
-
*/
|
|
1981
|
-
getMessageIndexes(sessionId: string, role: string): Promise<number[]>;
|
|
1982
|
-
private executeBeforeHooks;
|
|
1983
|
-
private executeAfterHooks;
|
|
1984
|
-
private generateTitle;
|
|
1985
|
-
/**
|
|
1986
|
-
* Set components for SummaryAgent
|
|
1987
|
-
*
|
|
1988
|
-
* Call this after initialization if you need compact functionality
|
|
1989
|
-
*/
|
|
1990
|
-
setSummaryComponents(promptComponent: any, llmComponent: any): void;
|
|
1991
|
-
/**
|
|
1992
|
-
* 初始化 SummaryAgent(如未初始化)
|
|
1993
|
-
*/
|
|
1994
|
-
private ensureSummaryAgent;
|
|
1995
|
-
/**
|
|
1996
|
-
* 生成场景化压缩提示 (Compact Hint)
|
|
1997
|
-
*
|
|
1998
|
-
* 两阶段自动压缩的第一阶段:
|
|
1999
|
-
* 根据会话历史生成场景化的压缩提示
|
|
2000
|
-
*
|
|
2001
|
-
* @param sessionId 会话 ID
|
|
2002
|
-
* @returns 生成的场景化提示
|
|
2003
|
-
*/
|
|
2004
|
-
generateCompactHint(sessionId: string): Promise<string>;
|
|
2005
|
-
/**
|
|
2006
|
-
* Compact a session
|
|
2007
|
-
*
|
|
2008
|
-
* 1. Get messages from latest checkpoint to current
|
|
2009
|
-
* 2. Call Summary Agent to generate checkpoint
|
|
2010
|
-
* 3. Save checkpoint and archive messages
|
|
2011
|
-
*
|
|
2012
|
-
* 支持 scenarioHint 参数,用于指导不同场景下的压缩重点提取
|
|
2013
|
-
*/
|
|
2014
|
-
compact(sessionId: string, options?: CompactOptions): Promise<CompactResult>;
|
|
2015
|
-
/**
|
|
2016
|
-
* Extract recent messages from the conversation for checkpoint context
|
|
2017
|
-
*
|
|
2018
|
-
* Extracts the last N turns of user query + assistant text response,
|
|
2019
|
-
* excluding tool calls and tool results.
|
|
2020
|
-
*
|
|
2021
|
-
* @param messages - All messages in the conversation
|
|
2022
|
-
* @param turnCount - Number of turns to extract (default: 2)
|
|
2023
|
-
* @returns Array of recent messages formatted as { role, content }
|
|
2024
|
-
*/
|
|
2025
|
-
private extractRecentMessages;
|
|
2026
|
-
/**
|
|
2027
|
-
* Preview compact operation
|
|
2028
|
-
*/
|
|
2029
|
-
previewCompact(sessionId: string): Promise<CompactPreview>;
|
|
2030
|
-
/**
|
|
2031
|
-
* Get all checkpoints for a session
|
|
2032
|
-
*/
|
|
2033
|
-
getCheckpoints(sessionId: string): Promise<SessionCheckpoint[]>;
|
|
2034
|
-
/**
|
|
2035
|
-
* Get a specific checkpoint
|
|
2036
|
-
*/
|
|
2037
|
-
getCheckpoint(sessionId: string, checkpointId: string): Promise<SessionCheckpoint | undefined>;
|
|
2038
|
-
/**
|
|
2039
|
-
* Delete a checkpoint and restore archived messages
|
|
2040
|
-
*/
|
|
2041
|
-
deleteCheckpoint(sessionId: string, checkpointId: string): Promise<boolean>;
|
|
2042
|
-
/**
|
|
2043
|
-
* Get session context
|
|
2044
|
-
*
|
|
2045
|
-
* Returns messages starting from the latest checkpoint by default
|
|
2046
|
-
*/
|
|
2047
|
-
getContext(sessionId: string, options?: GetContextOptions): Promise<GetContextResult>;
|
|
2048
|
-
/**
|
|
2049
|
-
* Check if session needs compaction
|
|
2050
|
-
*/
|
|
2051
|
-
shouldCompact(sessionId: string): Promise<boolean>;
|
|
2052
|
-
/**
|
|
2053
|
-
* Search messages across sessions
|
|
2054
|
-
*
|
|
2055
|
-
* Searches all messages matching the query, aggregated by session.
|
|
2056
|
-
* Supports SQLite FTS5 for fast full-text search.
|
|
2057
|
-
*/
|
|
2058
|
-
searchMessages(options: SearchMessagesOptions): Promise<SearchResult[]>;
|
|
2059
|
-
/**
|
|
2060
|
-
* Get latest checkpoint from session
|
|
2061
|
-
*/
|
|
2062
|
-
private getLatestCheckpoint;
|
|
2063
|
-
/**
|
|
2064
|
-
* Create checkpoint message (user role)
|
|
2065
|
-
*
|
|
2066
|
-
* Generates a markdown-formatted checkpoint summary as a user message,
|
|
2067
|
-
* allowing it to appear in the message list and provide context for
|
|
2068
|
-
* subsequent LLM calls.
|
|
2069
|
-
*/
|
|
2070
|
-
private createCheckpointMessage;
|
|
2071
|
-
}
|
|
2072
|
-
declare const RunStatusSchema: unknown;
|
|
2073
|
-
type RunStatus = z4.infer<typeof RunStatusSchema>;
|
|
2074
|
-
declare const NodeStatusSchema: unknown;
|
|
2075
|
-
type NodeStatus = z4.infer<typeof NodeStatusSchema>;
|
|
2076
|
-
interface WorkflowRun {
|
|
2077
|
-
id: string;
|
|
2078
|
-
workflowId: string;
|
|
2079
|
-
workflowName: string;
|
|
2080
|
-
status: RunStatus;
|
|
2081
|
-
input?: Record<string, any>;
|
|
2082
|
-
output?: Record<string, any>;
|
|
2083
|
-
error?: string;
|
|
2084
|
-
startedAt?: Date;
|
|
2085
|
-
pausedAt?: Date;
|
|
2086
|
-
resumedAt?: Date;
|
|
2087
|
-
stoppedAt?: Date;
|
|
2088
|
-
completedAt?: Date;
|
|
2089
|
-
durationMs?: number;
|
|
2090
|
-
debugMode: boolean;
|
|
2091
|
-
parentRunId?: string;
|
|
2092
|
-
}
|
|
2093
|
-
interface NodeRun {
|
|
2094
|
-
id: string;
|
|
2095
|
-
runId: string;
|
|
2096
|
-
nodeId: string;
|
|
2097
|
-
nodeType: string;
|
|
2098
|
-
status: NodeStatus;
|
|
2099
|
-
input?: any;
|
|
2100
|
-
output?: any;
|
|
2101
|
-
error?: string;
|
|
2102
|
-
startedAt?: Date;
|
|
2103
|
-
completedAt?: Date;
|
|
2104
|
-
durationMs?: number;
|
|
2105
|
-
retryCount: number;
|
|
2106
|
-
}
|
|
2107
|
-
interface RunOptions {
|
|
2108
|
-
input?: Record<string, any>;
|
|
2109
|
-
debug?: boolean;
|
|
2110
|
-
parallelLimit?: number | null;
|
|
2111
|
-
timeout?: number | null;
|
|
2112
|
-
/** @deprecated No-op: workflow always waits for completion */
|
|
2113
|
-
sync?: boolean;
|
|
2114
|
-
}
|
|
2115
|
-
interface RunResult {
|
|
2116
|
-
runId: string;
|
|
2117
|
-
/** Session ID (workflow_{runId} format) - useful for CLI resume */
|
|
2118
|
-
sessionId?: string;
|
|
2119
|
-
status: RunStatus;
|
|
2120
|
-
output?: Record<string, any>;
|
|
2121
|
-
error?: string;
|
|
2122
|
-
durationMs?: number;
|
|
2123
|
-
/** Pending node ID (when status is 'paused') */
|
|
2124
|
-
pendingNodeId?: string;
|
|
2125
|
-
/** Query from ask_user (when status is 'paused') */
|
|
2126
|
-
query?: string;
|
|
2127
|
-
/** Agent sub-session ID (for resuming agent nodes) */
|
|
2128
|
-
agentSessionId?: string;
|
|
2129
|
-
}
|
|
2130
|
-
/**
|
|
2131
|
-
* 节点执行上下文
|
|
2132
|
-
*
|
|
2133
|
-
* 扩展后包含:
|
|
2134
|
-
* - sessionId: Session ID,用于消息持久化
|
|
2135
|
-
* - askUser(): 方法,让节点可以请求用户输入
|
|
2136
|
-
*/
|
|
2137
|
-
interface NodeExecutionContext {
|
|
2138
|
-
runId: string;
|
|
2139
|
-
sessionId: string;
|
|
2140
|
-
workflowName: string;
|
|
2141
|
-
nodeId: string;
|
|
2142
|
-
input: unknown;
|
|
2143
|
-
previousOutputs: Map<string, unknown>;
|
|
2144
|
-
nodeOutputs: Map<string, unknown>;
|
|
2145
|
-
config: Record<string, unknown>;
|
|
2146
|
-
debug: boolean;
|
|
2147
|
-
eventBus: EventBus;
|
|
2148
|
-
workflowHistory?: Array<{
|
|
2149
|
-
role: "user" | "assistant" | "tool";
|
|
2150
|
-
content: string;
|
|
2151
|
-
toolCallId?: string;
|
|
2152
|
-
toolName?: string;
|
|
2153
|
-
nodeId?: string;
|
|
2154
|
-
}>;
|
|
2155
|
-
sessionComponent?: SessionComponent;
|
|
2156
|
-
agentSessionId?: string;
|
|
2157
|
-
workflowInput?: Record<string, unknown>;
|
|
2158
|
-
/**
|
|
2159
|
-
* 请求用户输入
|
|
2160
|
-
*
|
|
2161
|
-
* 调用此方法会抛出 AskUserError,引擎捕获后会暂停 workflow
|
|
2162
|
-
*
|
|
2163
|
-
* @param query - 向用户询问的问题
|
|
2164
|
-
* @throws AskUserError - 总是抛出此异常
|
|
2165
|
-
*/
|
|
2166
|
-
askUser(query: string): never;
|
|
2167
|
-
}
|
|
2168
|
-
interface NodeExecutionResult {
|
|
2169
|
-
success?: boolean;
|
|
2170
|
-
output?: any;
|
|
2171
|
-
error?: string | Error;
|
|
2172
|
-
duration?: number;
|
|
2173
|
-
durationMs?: number;
|
|
2174
|
-
metadata?: Record<string, unknown>;
|
|
2175
|
-
}
|
|
2176
|
-
/**
|
|
2177
|
-
* 创建 NodeExecutionContext
|
|
2178
|
-
*/
|
|
2179
|
-
declare function createNodeExecutionContext(params: {
|
|
2180
|
-
runId: string;
|
|
2181
|
-
workflowName: string;
|
|
2182
|
-
nodeId: string;
|
|
2183
|
-
input: unknown;
|
|
2184
|
-
previousOutputs: Map<string, unknown>;
|
|
2185
|
-
nodeOutputs: Map<string, unknown>;
|
|
2186
|
-
config: Record<string, unknown>;
|
|
2187
|
-
debug: boolean;
|
|
2188
|
-
eventBus: EventBus;
|
|
2189
|
-
sessionId: string;
|
|
2190
|
-
workflowHistory?: NodeExecutionContext["workflowHistory"];
|
|
2191
|
-
}): NodeExecutionContext;
|
|
2192
|
-
declare const DependsOnSchema: unknown;
|
|
2193
|
-
type DependsOn = z5.infer<typeof DependsOnSchema>;
|
|
2194
|
-
declare const RetryConfigSchema: unknown;
|
|
2195
|
-
type RetryConfig = z5.infer<typeof RetryConfigSchema>;
|
|
2196
|
-
declare const NodeDefinitionSchema: unknown;
|
|
2197
|
-
type NodeDefinition = z5.infer<typeof NodeDefinitionSchema>;
|
|
2198
|
-
/**
|
|
2199
|
-
* Node interface - base interface for all workflow nodes
|
|
2200
|
-
*/
|
|
2201
|
-
interface Node {
|
|
2202
|
-
/** Node type identifier */
|
|
2203
|
-
readonly type: string;
|
|
2204
|
-
/** Node unique identifier */
|
|
2205
|
-
readonly id: string;
|
|
2206
|
-
/** Execute the node with the given context */
|
|
2207
|
-
execute(context: NodeExecutionContext): Promise<NodeExecutionResult>;
|
|
2208
|
-
}
|
|
2209
|
-
declare const WorkflowConfigSchema: unknown;
|
|
2210
|
-
type WorkflowConfig = z5.infer<typeof WorkflowConfigSchema>;
|
|
2211
|
-
declare const OutputDefinitionSchema: unknown;
|
|
2212
|
-
type OutputDefinition = z5.infer<typeof OutputDefinitionSchema>;
|
|
2213
|
-
/**
|
|
2214
|
-
* Single input parameter definition
|
|
2215
|
-
*/
|
|
2216
|
-
declare const InputParameterSchema: unknown;
|
|
2217
|
-
type InputParameter = z5.infer<typeof InputParameterSchema>;
|
|
2218
|
-
/**
|
|
2219
|
-
* Input parameters schema for workflow definition
|
|
2220
|
-
* Maps parameter names to their definitions
|
|
2221
|
-
*/
|
|
2222
|
-
declare const WorkflowInputsSchema: unknown;
|
|
2223
|
-
type WorkflowInputs = z5.infer<typeof WorkflowInputsSchema>;
|
|
2224
|
-
declare const WorkflowMetadataSchema: unknown;
|
|
2225
|
-
type WorkflowMetadata = z5.infer<typeof WorkflowMetadataSchema>;
|
|
2226
|
-
declare const WorkflowDefinitionSchema: unknown;
|
|
2227
|
-
type WorkflowDefinition = z5.infer<typeof WorkflowDefinitionSchema>;
|
|
2228
|
-
interface WorkflowDefinitionExtended extends WorkflowDefinition {
|
|
2229
|
-
computedEntry: string[];
|
|
2230
|
-
nodeMap: Map<string, NodeDefinition>;
|
|
2231
|
-
topologicalOrder: string[];
|
|
2232
|
-
}
|
|
2233
|
-
interface Workflow {
|
|
2234
|
-
id: string;
|
|
2235
|
-
name: string;
|
|
2236
|
-
version: string;
|
|
2237
|
-
description?: string;
|
|
2238
|
-
definition: WorkflowDefinition;
|
|
2239
|
-
config: WorkflowConfig;
|
|
2240
|
-
metadata: WorkflowMetadata;
|
|
2241
|
-
tags: string[];
|
|
2242
|
-
createdAt: Date;
|
|
2243
|
-
updatedAt: Date;
|
|
2244
|
-
}
|
|
2245
|
-
interface ParsedWorkflow {
|
|
2246
|
-
format: "yaml" | "json";
|
|
2247
|
-
definition: WorkflowDefinition;
|
|
2248
|
-
}
|
|
2249
|
-
/**
|
|
2250
|
-
* 解析 Workflow 文件
|
|
2251
|
-
* @param content 文件内容
|
|
2252
|
-
* @param filename 文件名,用于自动检测格式
|
|
2253
|
-
*/
|
|
2254
|
-
declare function parseWorkflowFile(content: string, filename: string): Promise<ParsedWorkflow>;
|
|
2255
|
-
/**
|
|
2256
|
-
* Synchronous version of parseWorkflowFile
|
|
2257
|
-
* Uses JSON parsing only for auto-detection
|
|
2258
|
-
*/
|
|
2259
|
-
declare function parseWorkflowFileSync(content: string, filename: string): ParsedWorkflow;
|
|
2260
|
-
import { z as z6 } from "zod";
|
|
2261
|
-
/**
|
|
2262
|
-
* Node 中断事件 - 节点等待用户输入
|
|
2263
|
-
*/
|
|
2264
|
-
interface NodeInterruptEvent extends BaseEvent {
|
|
2265
|
-
type: "node.interrupt";
|
|
2266
|
-
node_id: string;
|
|
2267
|
-
node_type: string;
|
|
2268
|
-
query: string;
|
|
2269
|
-
/** Agent sub-session ID (for agent nodes that need resume) */
|
|
2270
|
-
agent_session_id?: string;
|
|
2271
|
-
}
|
|
2272
|
-
/**
|
|
2273
|
-
* Workflow Ask User 事件 - Workflow 等待用户输入
|
|
2274
|
-
*/
|
|
2275
|
-
interface WorkflowAskUserEvent extends BaseEvent {
|
|
2276
|
-
type: "workflow.ask-user";
|
|
2277
|
-
session_id: string;
|
|
2278
|
-
node_id: string;
|
|
2279
|
-
node_type: string;
|
|
2280
|
-
query: string;
|
|
2281
|
-
}
|
|
2282
|
-
/**
|
|
2283
|
-
* 创建 NodeInterruptEvent
|
|
2284
|
-
*/
|
|
2285
|
-
declare function createNodeInterruptEvent(runId: string, nodeId: string, nodeType: string, query: string, agentSessionId?: string): NodeInterruptEvent;
|
|
2286
|
-
/**
|
|
2287
|
-
* 创建 WorkflowAskUserEvent
|
|
2288
|
-
*/
|
|
2289
|
-
declare function createWorkflowAskUserEvent(runId: string, sessionId: string, nodeId: string, nodeType: string, query: string): WorkflowAskUserEvent;
|
|
2290
|
-
/**
|
|
2291
|
-
* AskUserError - 当节点调用 askUser() 时抛出
|
|
2292
|
-
*/
|
|
2293
|
-
declare class AskUserError extends Error {
|
|
2294
|
-
readonly runId: string;
|
|
2295
|
-
readonly sessionId: string;
|
|
2296
|
-
readonly nodeId: string;
|
|
2297
|
-
readonly nodeType: string;
|
|
2298
|
-
readonly query: string;
|
|
2299
|
-
readonly agentSessionId?: string | undefined;
|
|
2300
|
-
readonly timestamp: number;
|
|
2301
|
-
readonly type = "ask-user";
|
|
2302
|
-
readonly name = "AskUserError";
|
|
2303
|
-
constructor(runId: string, sessionId: string, nodeId: string, nodeType: string, query: string, agentSessionId?: string | undefined, timestamp?: number);
|
|
2304
|
-
/**
|
|
2305
|
-
* 转换为 WorkflowAskUserEvent
|
|
2306
|
-
*/
|
|
2307
|
-
toEvent(): WorkflowAskUserEvent;
|
|
2308
|
-
}
|
|
2309
|
-
/**
|
|
2310
|
-
* Workflow 节点执行消息角色类型
|
|
2311
|
-
*
|
|
2312
|
-
* 设计原则:
|
|
2313
|
-
* - workflow.node.start: 对应 tool-call,节点开始执行
|
|
2314
|
-
* - workflow.node.interrupt: 节点中断等待用户输入
|
|
2315
|
-
* - workflow.node.end: 对应 tool-result,节点执行结束
|
|
2316
|
-
* - workflow.node.resume: 用户响应后恢复节点执行
|
|
2317
|
-
*/
|
|
2318
|
-
declare const WorkflowMessageRoleSchema: unknown;
|
|
2319
|
-
type WorkflowMessageRole = z6.infer<typeof WorkflowMessageRoleSchema>;
|
|
2320
|
-
/**
|
|
2321
|
-
* workflow.node.start 消息 part
|
|
2322
|
-
* 对应 tool-call,节点开始执行
|
|
2323
|
-
*/
|
|
2324
|
-
interface WorkflowNodeStartPart2 {
|
|
2325
|
-
type: "workflow-node-start";
|
|
2326
|
-
nodeId: string;
|
|
2327
|
-
nodeType: string;
|
|
2328
|
-
input?: unknown;
|
|
2329
|
-
startTime: number;
|
|
2330
|
-
/** Agent session ID (only for agent nodes) */
|
|
2331
|
-
agentSessionId?: string;
|
|
2332
|
-
}
|
|
2333
|
-
/**
|
|
2334
|
-
* workflow.node.interrupt 消息 part
|
|
2335
|
-
* 节点中断等待用户输入
|
|
2336
|
-
*/
|
|
2337
|
-
interface WorkflowNodeInterruptPart2 {
|
|
2338
|
-
type: "workflow-node-interrupt";
|
|
2339
|
-
nodeId: string;
|
|
2340
|
-
nodeType: string;
|
|
2341
|
-
query: string;
|
|
2342
|
-
timestamp: number;
|
|
2343
|
-
/** Agent session ID (only for agent nodes) - 用于恢复 agent 子会话 */
|
|
2344
|
-
agentSessionId?: string;
|
|
2345
|
-
}
|
|
2346
|
-
/**
|
|
2347
|
-
* workflow.node.end 消息 part
|
|
2348
|
-
* 对应 tool-result,节点执行结束
|
|
2349
|
-
*/
|
|
2350
|
-
interface WorkflowNodeEndPart2 {
|
|
2351
|
-
type: "workflow-node-end";
|
|
2352
|
-
nodeId: string;
|
|
2353
|
-
nodeType: string;
|
|
2354
|
-
output?: unknown;
|
|
2355
|
-
error?: string;
|
|
2356
|
-
durationMs: number;
|
|
2357
|
-
/** Agent session ID (only for agent nodes) */
|
|
2358
|
-
agentSessionId?: string;
|
|
2359
|
-
}
|
|
2360
|
-
/**
|
|
2361
|
-
* workflow.node.resume 消息 part
|
|
2362
|
-
* 用户响应后恢复节点执行
|
|
2363
|
-
*/
|
|
2364
|
-
interface WorkflowNodeResumePart2 {
|
|
2365
|
-
type: "workflow-node-resume";
|
|
2366
|
-
nodeId: string;
|
|
2367
|
-
response: string;
|
|
2368
|
-
timestamp: number;
|
|
2369
|
-
/** Agent session ID (only for agent nodes) */
|
|
2370
|
-
agentSessionId?: string;
|
|
2371
|
-
}
|
|
2372
|
-
/**
|
|
2373
|
-
* workflow 消息 part 联合类型
|
|
2374
|
-
*/
|
|
2375
|
-
type WorkflowMessagePart = WorkflowNodeStartPart2 | WorkflowNodeInterruptPart2 | WorkflowNodeEndPart2 | WorkflowNodeResumePart2;
|
|
2376
|
-
/**
|
|
2377
|
-
* Workflow Session 元数据(简化版)
|
|
2378
|
-
*
|
|
2379
|
-
* 简化原则:只保留必要字段,其他信息从 messages 推断
|
|
2380
|
-
*
|
|
2381
|
-
* 设计原则:
|
|
2382
|
-
* - sessionId 直接作为 runId 使用
|
|
2383
|
-
* - Session.metadata.status 跟踪运行状态
|
|
2384
|
-
* - Session.messages 按时间顺序记录节点执行状态
|
|
2385
|
-
* - Next node 从 messages 推断(最后一条消息决定恢复点)
|
|
2386
|
-
*/
|
|
2387
|
-
interface WorkflowSessionMetadata2 {
|
|
2388
|
-
type: "workflow";
|
|
2389
|
-
/** Used to load workflow definition */
|
|
2390
|
-
workflowId: string;
|
|
2391
|
-
/** Display name */
|
|
2392
|
-
workflowName: string;
|
|
2393
|
-
/** Workflow 版本 (可选) */
|
|
2394
|
-
workflowVersion?: string;
|
|
2395
|
-
/** Execution status */
|
|
2396
|
-
status: "running" | "paused" | "completed" | "failed" | "stopped";
|
|
2397
|
-
/** Allow additional properties for metadata extension */
|
|
2398
|
-
[key: string]: unknown;
|
|
2399
|
-
}
|
|
2400
|
-
/**
|
|
2401
|
-
* Agent Session 引用(存储在 workflow session metadata 中)
|
|
2402
|
-
*/
|
|
2403
|
-
interface AgentSessionRef2 {
|
|
2404
|
-
nodeId: string;
|
|
2405
|
-
sessionId: string;
|
|
2406
|
-
status: "active" | "paused" | "completed";
|
|
2407
|
-
createdAt?: number;
|
|
2408
|
-
}
|
|
2409
|
-
/**
|
|
2410
|
-
* workflow.node.start 消息元数据
|
|
2411
|
-
* 对应 tool-call
|
|
2412
|
-
*/
|
|
2413
|
-
interface WorkflowNodeStartMetadata {
|
|
2414
|
-
type: "workflow.node.start";
|
|
2415
|
-
workflowNodeId: string;
|
|
2416
|
-
workflowNodeType: string;
|
|
2417
|
-
/** Agent session ID (only for agent nodes) */
|
|
2418
|
-
agentSessionId?: string;
|
|
2419
|
-
}
|
|
2420
|
-
/**
|
|
2421
|
-
* workflow.node.interrupt 消息元数据
|
|
2422
|
-
*/
|
|
2423
|
-
interface WorkflowNodeInterruptMetadata {
|
|
2424
|
-
type: "workflow.node.interrupt";
|
|
2425
|
-
workflowNodeId: string;
|
|
2426
|
-
workflowNodeType: string;
|
|
2427
|
-
query: string;
|
|
2428
|
-
/** Agent session ID (only for agent nodes) */
|
|
2429
|
-
agentSessionId?: string;
|
|
2430
|
-
}
|
|
2431
|
-
/**
|
|
2432
|
-
* workflow.node.end 消息元数据
|
|
2433
|
-
* 对应 tool-result
|
|
2434
|
-
*/
|
|
2435
|
-
interface WorkflowNodeEndMetadata {
|
|
2436
|
-
type: "workflow.node.end";
|
|
2437
|
-
workflowNodeId: string;
|
|
2438
|
-
workflowNodeType: string;
|
|
2439
|
-
success: boolean;
|
|
2440
|
-
}
|
|
2441
|
-
/**
|
|
2442
|
-
* workflow.node.resume 消息元数据
|
|
2443
|
-
*/
|
|
2444
|
-
interface WorkflowNodeResumeMetadata {
|
|
2445
|
-
type: "workflow.node.resume";
|
|
2446
|
-
workflowNodeId: string;
|
|
2447
|
-
response: string;
|
|
2448
|
-
/** Agent session ID (only for agent nodes) */
|
|
2449
|
-
agentSessionId?: string;
|
|
2450
|
-
}
|
|
2451
|
-
/**
|
|
2452
|
-
* workflow 消息元数据联合类型
|
|
2453
|
-
*/
|
|
2454
|
-
type WorkflowMessageMetadata = WorkflowNodeStartMetadata | WorkflowNodeInterruptMetadata | WorkflowNodeEndMetadata | WorkflowNodeResumeMetadata;
|
|
2455
|
-
/**
|
|
2456
|
-
* Agent Session 信息(存储在 workflow message metadata 中)
|
|
2457
|
-
*/
|
|
2458
|
-
interface AgentSessionInfo {
|
|
2459
|
-
/** Agent session ID */
|
|
2460
|
-
sessionId: string;
|
|
2461
|
-
/** Node ID */
|
|
2462
|
-
nodeId: string;
|
|
2463
|
-
/** 创建时间 */
|
|
2464
|
-
createdAt: number;
|
|
2465
|
-
}
|
|
2466
|
-
/**
|
|
2467
|
-
* 检查 SessionMetadata 是否为 Workflow 类型
|
|
2468
|
-
*
|
|
2469
|
-
* 简化验证:只检查必要字段存在性
|
|
2470
|
-
* - type === 'workflow'
|
|
2471
|
-
* - workflowId 存在
|
|
2472
|
-
* - workflowName 存在
|
|
2473
|
-
* - status 存在且为有效值
|
|
2474
|
-
*/
|
|
2475
|
-
declare function isWorkflowSessionMetadata(metadata: unknown): metadata is WorkflowSessionMetadata2;
|
|
2476
|
-
/**
|
|
2477
|
-
* 获取 workflow session 的状态
|
|
2478
|
-
*/
|
|
2479
|
-
declare function getWorkflowSessionStatus(metadata: WorkflowSessionMetadata2): "running" | "paused" | "completed" | "failed" | "stopped";
|
|
2480
|
-
/**
|
|
2481
|
-
* askUser 选项
|
|
2482
|
-
*/
|
|
2483
|
-
interface AskUserOptions {
|
|
2484
|
-
options?: string[];
|
|
2485
|
-
required_confirm?: boolean;
|
|
2486
|
-
}
|
|
2487
|
-
/**
|
|
2488
|
-
* 从 Session 恢复的运行时状态
|
|
2489
|
-
*/
|
|
2490
|
-
interface RuntimeState {
|
|
2491
|
-
sessionId: string;
|
|
2492
|
-
nodeOutputs: Map<string, unknown>;
|
|
2493
|
-
pendingNodeId: string | null;
|
|
2494
|
-
waitingForUser: boolean;
|
|
2495
|
-
agentSessionId?: string;
|
|
2496
|
-
}
|
|
2497
|
-
/**
|
|
2498
|
-
* Template resolution options
|
|
2499
|
-
*/
|
|
2500
|
-
interface TemplateResolverOptions {
|
|
2501
|
-
/** Input passed to the workflow/node */
|
|
2502
|
-
input?: Record<string, unknown>;
|
|
2503
|
-
/** Map of previous node outputs */
|
|
2504
|
-
previousOutputs?: Map<string, unknown>;
|
|
2505
|
-
}
|
|
2506
|
-
/**
|
|
2507
|
-
* TemplateResolver
|
|
2508
|
-
*
|
|
2509
|
-
* Resolves template strings that reference workflow inputs and node outputs.
|
|
2510
|
-
*/
|
|
2511
|
-
declare class TemplateResolver {
|
|
2512
|
-
private input;
|
|
2513
|
-
private previousOutputs;
|
|
2514
|
-
constructor(options?: TemplateResolverOptions);
|
|
2515
|
-
/**
|
|
2516
|
-
* Create a TemplateResolver from NodeExecutionContext
|
|
2517
|
-
*
|
|
2518
|
-
* @param context - Node execution context
|
|
2519
|
-
* @returns TemplateResolver instance
|
|
2520
|
-
*/
|
|
2521
|
-
static fromContext(context: NodeExecutionContext): TemplateResolver;
|
|
2522
|
-
/**
|
|
2523
|
-
* Resolve template references in an input value.
|
|
2524
|
-
*
|
|
2525
|
-
* Handles:
|
|
2526
|
-
* - Primitive values (returned as-is)
|
|
2527
|
-
* - Objects (recursively resolved)
|
|
2528
|
-
* - Arrays (recursively resolved)
|
|
2529
|
-
* - Strings (template resolution)
|
|
2530
|
-
*
|
|
2531
|
-
* Key behavior for template strings:
|
|
2532
|
-
* - If the string is a pure template reference (e.g., "{{nodes.fetch}}"),
|
|
2533
|
-
* the resolved value is returned as-is (preserving objects/arrays)
|
|
2534
|
-
* - If the string contains text + templates (e.g., "Hello {{name}}"),
|
|
2535
|
-
* returns a string with interpolated values
|
|
2536
|
-
*
|
|
2537
|
-
* @param input - Input value that may contain template strings
|
|
2538
|
-
* @returns Input with template references resolved
|
|
2539
|
-
*/
|
|
2540
|
-
resolveValue(input: unknown): unknown;
|
|
2541
|
-
/**
|
|
2542
|
-
* Check if a string is a pure template reference (starts with {{ and ends with }})
|
|
2543
|
-
* @param str - String to check
|
|
2544
|
-
* @returns The template content if pure, null otherwise
|
|
2545
|
-
*/
|
|
2546
|
-
private extractPureTemplate;
|
|
2547
|
-
/**
|
|
2548
|
-
* Resolve a pure template reference and return the value as-is.
|
|
2549
|
-
* @param templateContent - Content between {{ and }}
|
|
2550
|
-
* @returns The resolved value (may be object, array, or primitive)
|
|
2551
|
-
*/
|
|
2552
|
-
private resolvePureTemplate;
|
|
2553
|
-
/**
|
|
2554
|
-
* Resolve template content (the part inside {{ }}) and return the value.
|
|
2555
|
-
* @param templateContent - The path to resolve (e.g., "input.key", "nodes.fetch.data")
|
|
2556
|
-
* @returns The resolved value
|
|
2557
|
-
*/
|
|
2558
|
-
private resolveTemplateContent;
|
|
2559
|
-
/**
|
|
2560
|
-
* Resolve a single template string.
|
|
2561
|
-
*
|
|
2562
|
-
* Supports patterns:
|
|
2563
|
-
* - {{input.key}} - Input variable
|
|
2564
|
-
* - {{nodeId}} - Node's entire output
|
|
2565
|
-
* - {{nodeId.output}} - Node's output property
|
|
2566
|
-
* - {{nodeId.output.path}} - Node's nested output property
|
|
2567
|
-
* - {{nodes.nodeId}} - Node's entire output (with prefix)
|
|
2568
|
-
* - {{nodes.nodeId.output.path}} - Node's nested property (with prefix)
|
|
2569
|
-
*
|
|
2570
|
-
* @param template - Template string to resolve
|
|
2571
|
-
* @returns Resolved string
|
|
2572
|
-
*/
|
|
2573
|
-
resolveString(template: string): string;
|
|
2574
|
-
/**
|
|
2575
|
-
* Resolve a node output path.
|
|
2576
|
-
*
|
|
2577
|
-
* @param path - Path like "nodeId" or "nodeId.output" or "nodeId.output.path"
|
|
2578
|
-
* @returns The resolved value or undefined
|
|
2579
|
-
*/
|
|
2580
|
-
private resolveNodePath;
|
|
2581
|
-
/**
|
|
2582
|
-
* Get a nested value from an object using dot notation.
|
|
2583
|
-
*
|
|
2584
|
-
* @param obj - Object to traverse
|
|
2585
|
-
* @param path - Dot-separated path
|
|
2586
|
-
* @returns The nested value or undefined
|
|
2587
|
-
*/
|
|
2588
|
-
private getNestedValue;
|
|
2589
|
-
/**
|
|
2590
|
-
* Convert a value to string, or return the original template if value is undefined.
|
|
2591
|
-
*/
|
|
2592
|
-
private stringify;
|
|
2593
|
-
/**
|
|
2594
|
-
* Check if a string contains template references.
|
|
2595
|
-
*
|
|
2596
|
-
* @param str - String to check
|
|
2597
|
-
* @returns True if the string contains template references
|
|
2598
|
-
*/
|
|
2599
|
-
containsTemplates(str: string): boolean;
|
|
2600
|
-
/**
|
|
2601
|
-
* Extract all template references from a string.
|
|
2602
|
-
*
|
|
2603
|
-
* @param str - String to extract from
|
|
2604
|
-
* @returns Array of template references
|
|
2605
|
-
*/
|
|
2606
|
-
extractTemplates(str: string): string[];
|
|
2607
|
-
}
|
|
2608
|
-
interface WorkflowRepository {
|
|
2609
|
-
create(workflow: Omit<Workflow, "id" | "createdAt" | "updatedAt">): Workflow;
|
|
2610
|
-
getById(id: string): Workflow | null;
|
|
2611
|
-
getByName(name: string): Workflow | null;
|
|
2612
|
-
list(options?: ListOptions): Workflow[];
|
|
2613
|
-
update(id: string, updates: Partial<Workflow>): Workflow | null;
|
|
2614
|
-
delete(id: string): boolean;
|
|
2615
|
-
}
|
|
2616
|
-
interface CreateOptions {
|
|
2617
|
-
tags?: string[];
|
|
2618
|
-
metadata?: WorkflowMetadata;
|
|
2619
|
-
taskId?: number;
|
|
2620
|
-
force?: boolean;
|
|
2621
|
-
}
|
|
2622
|
-
interface ListOptions {
|
|
2623
|
-
tag?: string;
|
|
2624
|
-
taskId?: number;
|
|
2625
|
-
search?: string;
|
|
2626
|
-
limit?: number;
|
|
2627
|
-
offset?: number;
|
|
2628
|
-
}
|
|
2629
|
-
interface WorkflowUpdates {
|
|
2630
|
-
definition?: WorkflowDefinition;
|
|
2631
|
-
config?: WorkflowConfig;
|
|
2632
|
-
tags?: string[];
|
|
2633
|
-
metadata?: WorkflowMetadata;
|
|
2634
|
-
}
|
|
2635
|
-
interface WorkflowEngine {
|
|
2636
|
-
createSession(workflowId: string, options?: {
|
|
2637
|
-
input?: Record<string, any>;
|
|
2638
|
-
workflowName?: string;
|
|
2639
|
-
workflowVersion?: string;
|
|
2640
|
-
}): Promise<string>;
|
|
2641
|
-
run(sessionId: string, options?: RunOptions): Promise<RunResult>;
|
|
2642
|
-
runWorkflow(workflow: Workflow | WorkflowDefinition, options?: RunOptions): Promise<RunResult>;
|
|
2643
|
-
stop(sessionId: string, reason?: string): Promise<void>;
|
|
2644
|
-
addNode(sessionId: string, nodeDefinition: NodeDefinition): Promise<void>;
|
|
2645
|
-
removeNode(sessionId: string, nodeId: string): Promise<void>;
|
|
2646
|
-
}
|
|
2647
|
-
type WorkflowEngineFactory = (workflow: Workflow, options?: RunOptions) => Pick<WorkflowEngine, "run" | "stop" | "runWorkflow">;
|
|
2648
|
-
interface SessionInfo {
|
|
2649
|
-
id: string;
|
|
2650
|
-
title: string;
|
|
2651
|
-
metadata: WorkflowSessionMetadata3;
|
|
2652
|
-
createdAt: Date;
|
|
2653
|
-
updatedAt: Date;
|
|
2654
|
-
}
|
|
2655
|
-
interface WorkflowSessionMetadata3 {
|
|
2656
|
-
type: "workflow";
|
|
2657
|
-
runId?: string;
|
|
2658
|
-
workflowId?: string;
|
|
2659
|
-
workflowName: string;
|
|
2660
|
-
workflowVersion?: string;
|
|
2661
|
-
status: "running" | "paused" | "completed" | "failed";
|
|
2662
|
-
input?: any;
|
|
2663
|
-
rootNodeId?: string;
|
|
2664
|
-
agentSessions?: AgentSessionRef3[];
|
|
2665
|
-
}
|
|
2666
|
-
interface AgentSessionRef3 {
|
|
2667
|
-
nodeId: string;
|
|
2668
|
-
sessionId: string;
|
|
2669
|
-
status: "active" | "paused" | "completed";
|
|
2670
|
-
}
|
|
2671
|
-
declare class WorkflowService {
|
|
2672
|
-
private workflowRepository;
|
|
2673
|
-
private engineFactory;
|
|
2674
|
-
private sessionComponent?;
|
|
2675
|
-
constructor(workflowRepository: WorkflowRepository, engineFactory: WorkflowEngineFactory, sessionComponent?: {
|
|
2676
|
-
create(options: {
|
|
2677
|
-
id?: string;
|
|
2678
|
-
title: string;
|
|
2679
|
-
metadata: any;
|
|
2680
|
-
}): Promise<{
|
|
2681
|
-
id: string;
|
|
2682
|
-
}>;
|
|
2683
|
-
get(id: string): Promise<SessionInfo | null | undefined>;
|
|
2684
|
-
list?(options?: {
|
|
2685
|
-
limit?: number;
|
|
2686
|
-
offset?: number;
|
|
2687
|
-
}): Promise<SessionInfo[]>;
|
|
2688
|
-
update(id: string, updates: {
|
|
2689
|
-
metadata?: any;
|
|
2690
|
-
}): Promise<boolean>;
|
|
2691
|
-
addMessage(sessionId: string, message: any): Promise<string>;
|
|
2692
|
-
getMessages(sessionId: string): Promise<any[]>;
|
|
2693
|
-
} | undefined);
|
|
2694
|
-
/**
|
|
2695
|
-
* Create a new workflow from definition
|
|
2696
|
-
*/
|
|
2697
|
-
createWorkflow(definition: WorkflowDefinition, options?: CreateOptions): Promise<Workflow>;
|
|
2698
|
-
/**
|
|
2699
|
-
* Get workflow by id or name
|
|
2700
|
-
*/
|
|
2701
|
-
getWorkflow(idOrName: string): Workflow | null;
|
|
2702
|
-
/**
|
|
2703
|
-
* Get workflow by id
|
|
2704
|
-
*/
|
|
2705
|
-
getWorkflowById(id: string): Workflow | null;
|
|
2706
|
-
/**
|
|
2707
|
-
* Get workflow by name
|
|
2708
|
-
*/
|
|
2709
|
-
getWorkflowByName(name: string): Workflow | null;
|
|
2710
|
-
/**
|
|
2711
|
-
* List workflows with optional filters
|
|
2712
|
-
*/
|
|
2713
|
-
listWorkflows(options?: ListOptions): Workflow[];
|
|
2714
|
-
/**
|
|
2715
|
-
* Update an existing workflow
|
|
2716
|
-
*/
|
|
2717
|
-
updateWorkflow(idOrName: string, updates: WorkflowUpdates, options?: {
|
|
2718
|
-
tags?: string[];
|
|
2719
|
-
}): Promise<Workflow>;
|
|
2720
|
-
/**
|
|
2721
|
-
* Delete a workflow by name or id
|
|
2722
|
-
*/
|
|
2723
|
-
deleteWorkflow(idOrName: string): Promise<boolean>;
|
|
2724
|
-
/**
|
|
2725
|
-
* Run a workflow by definition or name
|
|
2726
|
-
*
|
|
2727
|
-
* This creates a Session that represents the workflow run.
|
|
2728
|
-
* The Session ID is used as the run identifier for pause/resume.
|
|
2729
|
-
*/
|
|
2730
|
-
runWorkflow(idOrNameOrDefinition: string | WorkflowDefinition, input?: Record<string, any>, options?: RunOptions): Promise<RunResult>;
|
|
2731
|
-
/**
|
|
2732
|
-
* Run workflow by name (convenience method)
|
|
2733
|
-
*/
|
|
2734
|
-
runWorkflowByName(name: string, input?: Record<string, any>, options?: RunOptions): Promise<RunResult>;
|
|
2735
|
-
/**
|
|
2736
|
-
* Stop a running workflow session
|
|
2737
|
-
*/
|
|
2738
|
-
stopRun(sessionId: string, reason?: string): Promise<void>;
|
|
2739
|
-
/**
|
|
2740
|
-
* Get workflow session by ID
|
|
2741
|
-
*/
|
|
2742
|
-
getSession(sessionId: string): Promise<SessionInfo | null>;
|
|
2743
|
-
/**
|
|
2744
|
-
* List workflow sessions
|
|
2745
|
-
*/
|
|
2746
|
-
listSessions(options?: {
|
|
2747
|
-
limit?: number;
|
|
2748
|
-
offset?: number;
|
|
2749
|
-
}): Promise<SessionInfo[]>;
|
|
2750
|
-
/**
|
|
2751
|
-
* Get messages from a workflow session
|
|
2752
|
-
*/
|
|
2753
|
-
getSessionMessages(sessionId: string): Promise<any[]>;
|
|
2754
|
-
}
|
|
2755
|
-
/**
|
|
2756
|
-
* @fileoverview 统一错误类型体系
|
|
2757
|
-
*
|
|
2758
|
-
* 提供 AgentError, ToolError, LLMError, ComponentError, ContextError 等错误类型
|
|
2759
|
-
* 每个错误包含 code(错误码)和 details(额外信息)
|
|
2760
|
-
*/
|
|
2761
|
-
/**
|
|
2762
|
-
* 错误码枚举
|
|
2763
|
-
*/
|
|
2764
|
-
declare const ErrorCodes: {
|
|
2765
|
-
readonly AGENT_NOT_FOUND: "AGENT_001";
|
|
2766
|
-
readonly AGENT_RUN_FAILED: "AGENT_002";
|
|
2767
|
-
readonly AGENT_TIMEOUT: "AGENT_003";
|
|
2768
|
-
readonly AGENT_CONFIG_INVALID: "AGENT_004";
|
|
2769
|
-
readonly TOOL_NOT_FOUND: "TOOL_001";
|
|
2770
|
-
readonly TOOL_DISABLED: "TOOL_002";
|
|
2771
|
-
readonly TOOL_EXECUTION_FAILED: "TOOL_003";
|
|
2772
|
-
readonly TOOL_VALIDATION_FAILED: "TOOL_004";
|
|
2773
|
-
readonly LLM_PROVIDER_NOT_FOUND: "LLM_001";
|
|
2774
|
-
readonly LLM_INVOCATION_FAILED: "LLM_002";
|
|
2775
|
-
readonly LLM_TIMEOUT: "LLM_003";
|
|
2776
|
-
readonly LLM_CONFIG_INVALID: "LLM_004";
|
|
2777
|
-
readonly COMPONENT_NOT_FOUND: "COMP_001";
|
|
2778
|
-
readonly COMPONENT_INIT_FAILED: "COMP_002";
|
|
2779
|
-
readonly COMPONENT_NOT_RUNNING: "COMP_003";
|
|
2780
|
-
readonly CONFIG_NOT_FOUND: "CONFIG_001";
|
|
2781
|
-
readonly CONFIG_INVALID: "CONFIG_002";
|
|
2782
|
-
readonly CONFIG_SOURCE_ERROR: "CONFIG_003";
|
|
2783
|
-
readonly CONTEXT_THRESHOLD_EXCEEDED: "CTX_001";
|
|
2784
|
-
readonly CONTEXT_COMPACTION_FAILED: "CTX_002";
|
|
2785
|
-
readonly MCP_INIT_FAILED: "MCP_001";
|
|
2786
|
-
readonly MCP_SERVER_ERROR: "MCP_002";
|
|
2787
|
-
readonly MCP_TOOL_ERROR: "MCP_003";
|
|
2788
|
-
};
|
|
2789
|
-
type ErrorCode = typeof ErrorCodes[keyof typeof ErrorCodes];
|
|
2790
|
-
/**
|
|
2791
|
-
* RoyError 基类
|
|
2792
|
-
*/
|
|
2793
|
-
declare class RoyError extends Error {
|
|
2794
|
-
readonly code: ErrorCode;
|
|
2795
|
-
readonly details?: unknown;
|
|
2796
|
-
constructor(message: string, code: ErrorCode, details?: unknown);
|
|
2797
|
-
}
|
|
2798
|
-
/**
|
|
2799
|
-
* LLM Output 结构(用于 ContextError 携带 assistant message)
|
|
2800
|
-
*
|
|
2801
|
-
* 这是简化版本,仅包含保存到 session 所需的最少字段。
|
|
2802
|
-
* 与 llm/types.ts 中的 LLMOutput 不同,那个版本包含完整的 LLM 调用结果。
|
|
2803
|
-
*/
|
|
2804
|
-
interface LLMOutput2 {
|
|
2805
|
-
content?: string;
|
|
2806
|
-
reasoning?: string;
|
|
2807
|
-
toolCalls?: Array<{
|
|
2808
|
-
id: string;
|
|
2809
|
-
function?: {
|
|
2810
|
-
name: string;
|
|
2811
|
-
arguments: string;
|
|
2812
|
-
};
|
|
2813
|
-
name?: string;
|
|
2814
|
-
arguments?: unknown;
|
|
2815
|
-
}>;
|
|
2816
|
-
usage?: {
|
|
2817
|
-
promptTokens: number;
|
|
2818
|
-
completionTokens: number;
|
|
2819
|
-
totalTokens: number;
|
|
2820
|
-
};
|
|
2821
|
-
}
|
|
2822
|
-
/**
|
|
2823
|
-
* Context 相关错误
|
|
2824
|
-
*/
|
|
2825
|
-
declare class ContextError extends RoyError {
|
|
2826
|
-
readonly sessionId?: string | undefined;
|
|
2827
|
-
readonly usage?: {
|
|
2828
|
-
promptTokens: number;
|
|
2829
|
-
completionTokens: number;
|
|
2830
|
-
totalTokens: number;
|
|
2831
|
-
} | undefined;
|
|
2832
|
-
readonly contextWindow?: number | undefined;
|
|
2833
|
-
readonly llmOutput?: LLMOutput2;
|
|
2834
|
-
constructor(message: string, code?: ErrorCode, sessionId?: string | undefined, usage?: {
|
|
2835
|
-
promptTokens: number;
|
|
2836
|
-
completionTokens: number;
|
|
2837
|
-
totalTokens: number;
|
|
2838
|
-
} | undefined, contextWindow?: number | undefined, llmOutput?: LLMOutput2, details?: unknown);
|
|
2839
|
-
}
|
|
2840
|
-
/**
|
|
2841
|
-
* LLMComponent
|
|
2842
|
-
* 管理 LLM 调用,使用 AI SDK 进行实际调用
|
|
2843
|
-
*
|
|
2844
|
-
* 配置通过 ConfigComponent 实时获取,支持配置热更新
|
|
2845
|
-
*
|
|
2846
|
-
* @example
|
|
2847
|
-
* // 通过 ConfigComponent 加载配置
|
|
2848
|
-
* const configComponent = new ConfigComponent();
|
|
2849
|
-
* configComponent.setXdgDataHome(__dirname);
|
|
2850
|
-
*
|
|
2851
|
-
* const llmComponent = new LLMComponent();
|
|
2852
|
-
* await llmComponent.init({
|
|
2853
|
-
* env, // Environment 实例(必填)
|
|
2854
|
-
* options: {
|
|
2855
|
-
* configComponent, // ConfigComponent 实例
|
|
2856
|
-
* configPath: "config.jsonc" // 配置文件路径
|
|
2857
|
-
* }
|
|
2858
|
-
* });
|
|
2859
|
-
*
|
|
2860
|
-
* // 配置更新后,下次调用会自动获取最新配置
|
|
2861
|
-
* configComponent.set("llm.temperature", 0.5);
|
|
2862
|
-
*/
|
|
2863
|
-
declare class LLMComponent extends BaseComponent {
|
|
2864
|
-
readonly name = "llm";
|
|
2865
|
-
readonly version = "2.0.0";
|
|
2866
|
-
private configComponent?;
|
|
2867
|
-
/** 配置变更 watcher 清理函数 */
|
|
2868
|
-
private configWatcher?;
|
|
2869
|
-
constructor();
|
|
2870
|
-
/**
|
|
2871
|
-
* 初始化组件
|
|
2872
|
-
*
|
|
2873
|
-
* 配置加载优先级(从高到低):
|
|
2874
|
-
* 1. Object - 直接传入的 config 对象
|
|
2875
|
-
* 2. Env - 环境变量(通过 envPrefix 配置前缀)
|
|
2876
|
-
* 3. File - 配置文件(通过 configPath 指定)
|
|
2877
|
-
*/
|
|
2878
|
-
init(config: ComponentConfig): Promise<void>;
|
|
2879
|
-
/**
|
|
2880
|
-
* 注册配置到 ConfigComponent
|
|
2881
|
-
*
|
|
2882
|
-
* 配置加载顺序(优先级从低到高):
|
|
2883
|
-
* 1. FileSource - 从配置文件加载(最低)
|
|
2884
|
-
* 2. EnvSource - 从环境变量加载(中等)
|
|
2885
|
-
* 3. MemorySource - 直接配置对象(最高)
|
|
2886
|
-
*/
|
|
2887
|
-
private registerConfig;
|
|
2888
|
-
/**
|
|
2889
|
-
* 将配置对象展平为点号路径
|
|
2890
|
-
*
|
|
2891
|
-
* @example
|
|
2892
|
-
* flattenConfig({ providers: { openai: { apiKey: "xxx" } } })
|
|
2893
|
-
* -> { "llm.providers.openai.apiKey": "xxx" }
|
|
2894
|
-
*/
|
|
2895
|
-
private flattenConfig;
|
|
2896
|
-
/**
|
|
2897
|
-
* 注册配置热更新监听
|
|
2898
|
-
*/
|
|
2899
|
-
private registerConfigWatcher;
|
|
2900
|
-
/**
|
|
2901
|
-
* 处理配置变更(支持热更新)
|
|
2902
|
-
*/
|
|
2903
|
-
protected onConfigChange(event: {
|
|
2904
|
-
key: string;
|
|
2905
|
-
oldValue?: unknown;
|
|
2906
|
-
newValue?: unknown;
|
|
2907
|
-
}): void;
|
|
2908
|
-
/**
|
|
2909
|
-
* 启动组件
|
|
2910
|
-
*/
|
|
2911
|
-
onStart(): Promise<void>;
|
|
2912
|
-
/**
|
|
2913
|
-
* 停止组件
|
|
2914
|
-
*/
|
|
2915
|
-
onStop(): Promise<void>;
|
|
2916
|
-
/**
|
|
2917
|
-
* 从配置源获取配置值
|
|
2918
|
-
* 优先从 ConfigComponent 获取(支持热更新),否则返回默认值
|
|
2919
|
-
*/
|
|
2920
|
-
private getLLMConfig;
|
|
2921
|
-
/**
|
|
2922
|
-
* 解析请求,构建调用上下文(invoke 和 invokeStream 共享)
|
|
2923
|
-
*/
|
|
2924
|
-
private resolveRequest;
|
|
2925
|
-
/**
|
|
2926
|
-
* 调用 LLM
|
|
2927
|
-
*
|
|
2928
|
-
* 流式事件通过 env.pushEnvEvent() 发布
|
|
2929
|
-
*/
|
|
2930
|
-
invoke(request: LLMInvokeRequest): Promise<LLMInvokeResult>;
|
|
2931
|
-
/**
|
|
2932
|
-
* 获取 Provider API Key
|
|
2933
|
-
*/
|
|
2934
|
-
private getApiKey;
|
|
2935
|
-
/**
|
|
2936
|
-
* 获取 Provider
|
|
2937
|
-
*/
|
|
2938
|
-
getProvider(id: string);
|
|
2939
|
-
/**
|
|
2940
|
-
* 列出所有 Provider
|
|
2941
|
-
*/
|
|
2942
|
-
listProviders();
|
|
2943
|
-
/**
|
|
2944
|
-
* 获取模型的限制配置
|
|
2945
|
-
*
|
|
2946
|
-
* @param providerId - Provider ID
|
|
2947
|
-
* @param modelId - 模型 ID(可选,默认使用 provider 的 defaultModel)
|
|
2948
|
-
* @returns 模型限制配置,如果没有配置则返回 undefined
|
|
2949
|
-
*/
|
|
2950
|
-
getModelLimits(providerId: string, modelId?: string): ModelLimits | undefined;
|
|
2951
|
-
/**
|
|
2952
|
-
* 获取上下文窗口阈值配置
|
|
2953
|
-
*
|
|
2954
|
-
* @param providerId - Provider ID
|
|
2955
|
-
* @param modelId - 模型 ID
|
|
2956
|
-
* @returns 包含 contextWindow 和 thresholdRatio 的对象
|
|
2957
|
-
*/
|
|
2958
|
-
getContextThresholdConfig(providerId: string, modelId?: string): {
|
|
2959
|
-
contextWindow: number;
|
|
2960
|
-
thresholdRatio: number;
|
|
2961
|
-
};
|
|
2962
|
-
/**
|
|
2963
|
-
* 检测上下文阈值是否超过
|
|
2964
|
-
*
|
|
2965
|
-
* @param usage - LLM 使用量信息
|
|
2966
|
-
* @param providerId - Provider ID
|
|
2967
|
-
* @param modelId - 模型 ID
|
|
2968
|
-
* @param sessionId - Session ID(用于错误信息)
|
|
2969
|
-
* @returns 如果超过阈值,返回 ContextError;否则返回 undefined
|
|
2970
|
-
*/
|
|
2971
|
-
checkContextThreshold(usage: {
|
|
2972
|
-
totalTokens: number;
|
|
2973
|
-
promptTokens?: number;
|
|
2974
|
-
completionTokens?: number;
|
|
2975
|
-
} | undefined, providerId: string, modelId: string | undefined, sessionId?: string): ContextError | undefined;
|
|
2976
|
-
}
|
|
2977
|
-
/**
|
|
2978
|
-
* @Workflow 装饰器选项
|
|
2979
|
-
*/
|
|
2980
|
-
interface WorkflowOptions {
|
|
2981
|
-
/** Workflow 名称 */
|
|
2982
|
-
name: string;
|
|
2983
|
-
/** 版本号,默认 "1.0" */
|
|
2984
|
-
version?: string;
|
|
2985
|
-
/** 描述 */
|
|
2986
|
-
description?: string;
|
|
2987
|
-
/** 入口节点 ID */
|
|
2988
|
-
entry?: string | string[];
|
|
2989
|
-
/** 全局配置 */
|
|
2990
|
-
config?: {
|
|
2991
|
-
parallel_limit?: number | null;
|
|
2992
|
-
timeout?: number | null;
|
|
2993
|
-
retry?: {
|
|
2994
|
-
max_attempts: number;
|
|
2995
|
-
backoff: "fixed" | "exponential";
|
|
2996
|
-
initial_delay: number;
|
|
2997
|
-
};
|
|
2998
|
-
debug?: boolean;
|
|
2999
|
-
};
|
|
3000
|
-
/** 标签 */
|
|
3001
|
-
tags?: string[];
|
|
3002
|
-
/** 作者 */
|
|
3003
|
-
author?: string;
|
|
3004
|
-
}
|
|
3005
|
-
/**
|
|
3006
|
-
* @NodeAs 装饰器选项
|
|
3007
|
-
*/
|
|
3008
|
-
interface NodeAsOptions {
|
|
3009
|
-
/** 节点 ID,默认使用方法名 */
|
|
3010
|
-
nodeId?: string;
|
|
3011
|
-
/** 节点类型 */
|
|
3012
|
-
nodeType?: "tool" | "skill" | "agent" | "workflow";
|
|
3013
|
-
/** 显示名称 */
|
|
3014
|
-
name?: string;
|
|
3015
|
-
/** 依赖的节点 ID 列表 */
|
|
3016
|
-
dependsOn?: string | string[];
|
|
3017
|
-
/** 执行条件 */
|
|
3018
|
-
condition?: string;
|
|
3019
|
-
/** 重试配置 */
|
|
3020
|
-
retry?: {
|
|
3021
|
-
max_attempts: number;
|
|
3022
|
-
backoff: "fixed" | "exponential";
|
|
3023
|
-
initial_delay: number;
|
|
3024
|
-
};
|
|
3025
|
-
/** 超时时间(毫秒) */
|
|
3026
|
-
timeout?: number;
|
|
3027
|
-
/** 节点配置 */
|
|
3028
|
-
config?: Record<string, unknown>;
|
|
3029
|
-
}
|
|
3030
|
-
/**
|
|
3031
|
-
* @Edge 装饰器选项
|
|
3032
|
-
*/
|
|
3033
|
-
interface EdgeOptions {
|
|
3034
|
-
from: string;
|
|
3035
|
-
to: string;
|
|
3036
|
-
/** 条件表达式 */
|
|
3037
|
-
condition?: string;
|
|
3038
|
-
}
|
|
3039
|
-
/**
|
|
3040
|
-
* Workflow 元数据(存储在类的 Reflect Metadata 中)
|
|
3041
|
-
*/
|
|
3042
|
-
interface WorkflowMetadata2 {
|
|
3043
|
-
name: string;
|
|
3044
|
-
version: string;
|
|
3045
|
-
description?: string;
|
|
3046
|
-
entry?: string | string[];
|
|
3047
|
-
config?: {
|
|
3048
|
-
parallel_limit?: number | null;
|
|
3049
|
-
timeout?: number | null;
|
|
3050
|
-
retry?: {
|
|
3051
|
-
max_attempts: number;
|
|
3052
|
-
backoff: "fixed" | "exponential";
|
|
3053
|
-
initial_delay: number;
|
|
3054
|
-
};
|
|
3055
|
-
debug?: boolean;
|
|
3056
|
-
};
|
|
3057
|
-
tags?: string[];
|
|
3058
|
-
author?: string;
|
|
3059
|
-
/** 类实例引用 */
|
|
3060
|
-
instance?: any;
|
|
3061
|
-
}
|
|
3062
|
-
/**
|
|
3063
|
-
* Node 元数据(存储在类的 Reflect Metadata 中)
|
|
3064
|
-
*/
|
|
3065
|
-
interface NodeMetadata {
|
|
3066
|
-
nodeId: string;
|
|
3067
|
-
nodeType: string;
|
|
3068
|
-
name?: string;
|
|
3069
|
-
dependsOn: string[];
|
|
3070
|
-
condition?: string;
|
|
3071
|
-
retry?: {
|
|
3072
|
-
max_attempts: number;
|
|
3073
|
-
backoff: "fixed" | "exponential";
|
|
3074
|
-
initial_delay: number;
|
|
3075
|
-
};
|
|
3076
|
-
timeout?: number;
|
|
3077
|
-
config: Record<string, unknown>;
|
|
3078
|
-
methodName: string;
|
|
3079
|
-
method: Function;
|
|
3080
|
-
}
|
|
3081
|
-
/**
|
|
3082
|
-
* Edge 元数据(存储在类的 Reflect Metadata 中)
|
|
3083
|
-
*/
|
|
3084
|
-
interface EdgeMetadata {
|
|
3085
|
-
from: string;
|
|
3086
|
-
to: string;
|
|
3087
|
-
condition?: string;
|
|
3088
|
-
}
|
|
3089
|
-
/**
|
|
3090
|
-
* Workflow 验证结果
|
|
3091
|
-
*/
|
|
3092
|
-
interface ValidationResult2 {
|
|
3093
|
-
valid: boolean;
|
|
3094
|
-
errors: string[];
|
|
3095
|
-
warnings: string[];
|
|
3096
|
-
}
|
|
3097
|
-
/**
|
|
3098
|
-
* 转换为 workflow 包中的 NodeDefinition
|
|
3099
|
-
* 这是最终输出给 WorkflowEngine 使用的格式
|
|
3100
|
-
*/
|
|
3101
|
-
interface ConvertedNodeDefinition {
|
|
3102
|
-
id: string;
|
|
3103
|
-
type: string;
|
|
3104
|
-
name?: string;
|
|
3105
|
-
config: Record<string, unknown>;
|
|
3106
|
-
depends_on: string[];
|
|
3107
|
-
condition?: string;
|
|
3108
|
-
retry?: {
|
|
3109
|
-
max_attempts: number;
|
|
3110
|
-
backoff: "fixed" | "exponential";
|
|
3111
|
-
initial_delay: number;
|
|
3112
|
-
};
|
|
3113
|
-
timeout?: number;
|
|
3114
|
-
}
|
|
3115
|
-
/**
|
|
3116
|
-
* 转换为标准的 WorkflowDefinition
|
|
3117
|
-
* 这是最终输出给 WorkflowEngine 使用的格式
|
|
3118
|
-
*/
|
|
3119
|
-
interface ConvertedWorkflowDefinition {
|
|
3120
|
-
name: string;
|
|
3121
|
-
version: string;
|
|
3122
|
-
description?: string;
|
|
3123
|
-
config: {
|
|
3124
|
-
parallel_limit?: number | null;
|
|
3125
|
-
timeout?: number | null;
|
|
3126
|
-
retry?: {
|
|
3127
|
-
max_attempts: number;
|
|
3128
|
-
backoff: "fixed" | "exponential";
|
|
3129
|
-
initial_delay: number;
|
|
3130
|
-
};
|
|
3131
|
-
debug?: boolean;
|
|
3132
|
-
};
|
|
3133
|
-
nodes: ConvertedNodeDefinition[];
|
|
3134
|
-
entry: string | string[];
|
|
3135
|
-
outputs: Array<{
|
|
3136
|
-
name: string;
|
|
3137
|
-
source: string;
|
|
3138
|
-
path: string;
|
|
3139
|
-
}>;
|
|
3140
|
-
metadata: {
|
|
3141
|
-
tags?: string[];
|
|
3142
|
-
author?: string;
|
|
3143
|
-
};
|
|
3144
|
-
}
|
|
3145
|
-
/**
|
|
3146
|
-
* 声明一个类为 Workflow 类
|
|
3147
|
-
*
|
|
3148
|
-
* @param options - Workflow 配置选项
|
|
3149
|
-
*
|
|
3150
|
-
* @example
|
|
3151
|
-
* ```typescript
|
|
3152
|
-
* @Workflow({
|
|
3153
|
-
* name: 'my-workflow',
|
|
3154
|
-
* version: '1.0',
|
|
3155
|
-
* entry: 'start'
|
|
3156
|
-
* })
|
|
3157
|
-
* class MyWorkflow {
|
|
3158
|
-
* @NodeAs({ nodeId: 'start' })
|
|
3159
|
-
* async start() { }
|
|
3160
|
-
* }
|
|
3161
|
-
* ```
|
|
3162
|
-
*/
|
|
3163
|
-
declare function Workflow2(options: WorkflowOptions): ClassDecorator;
|
|
3164
|
-
/**
|
|
3165
|
-
* 将方法声明为 Workflow 的 Node
|
|
3166
|
-
*
|
|
3167
|
-
* @param options - Node 配置选项
|
|
3168
|
-
*
|
|
3169
|
-
* @example
|
|
3170
|
-
* ```typescript
|
|
3171
|
-
* @Workflow({ name: 'my-workflow', entry: 'fetch' })
|
|
3172
|
-
* class MyWorkflow {
|
|
3173
|
-
* @NodeAs({
|
|
3174
|
-
* nodeId: 'fetch',
|
|
3175
|
-
* nodeType: 'tool',
|
|
3176
|
-
* config: { tool: 'web_search' }
|
|
3177
|
-
* })
|
|
3178
|
-
* async fetch(input: { query: string }) {
|
|
3179
|
-
* return { data: [] };
|
|
3180
|
-
* }
|
|
3181
|
-
*
|
|
3182
|
-
* @NodeAs({
|
|
3183
|
-
* nodeId: 'process',
|
|
3184
|
-
* dependsOn: 'fetch'
|
|
3185
|
-
* })
|
|
3186
|
-
* async process(input: { data: any }) {
|
|
3187
|
-
* return { result: {} };
|
|
3188
|
-
* }
|
|
3189
|
-
* }
|
|
3190
|
-
* ```
|
|
3191
|
-
*/
|
|
3192
|
-
declare function NodeAs(options?: NodeAsOptions): MethodDecorator;
|
|
3193
|
-
/**
|
|
3194
|
-
* 声明一条边 (from -> to)
|
|
3195
|
-
*
|
|
3196
|
-
* 这是一个方法装饰器,用于在方法上声明边关系
|
|
3197
|
-
*
|
|
3198
|
-
* @param from - 起始节点 ID
|
|
3199
|
-
* @param to - 目标节点 ID
|
|
3200
|
-
*
|
|
3201
|
-
* @example
|
|
3202
|
-
* ```typescript
|
|
3203
|
-
* @Edge('nodeA', 'nodeB')
|
|
3204
|
-
* static __edge_nodeA_to_nodeB__ = null;
|
|
3205
|
-
* ```
|
|
3206
|
-
*/
|
|
3207
|
-
declare function Edge(from: string, to: string): MethodDecorator;
|
|
3208
|
-
/**
|
|
3209
|
-
* 类级别装饰器,声明多条边
|
|
3210
|
-
*
|
|
3211
|
-
* @param edges - 边数组
|
|
3212
|
-
*
|
|
3213
|
-
* @example
|
|
3214
|
-
* ```typescript
|
|
3215
|
-
* @WorkflowEdges([
|
|
3216
|
-
* { from: 'A', to: 'B' },
|
|
3217
|
-
* { from: 'B', to: 'C' }
|
|
3218
|
-
* ])
|
|
3219
|
-
* @Workflow({ name: 'test' })
|
|
3220
|
-
* class TestWorkflow { }
|
|
3221
|
-
* ```
|
|
3222
|
-
*/
|
|
3223
|
-
declare function WorkflowEdges(edges: EdgeOptions[]): ClassDecorator;
|
|
3224
|
-
/**
|
|
3225
|
-
* DecoratorNode - 执行装饰器类方法的节点
|
|
3226
|
-
*
|
|
3227
|
-
* 用于执行使用 @Workflow/@NodeAs 装饰器定义的类方法
|
|
3228
|
-
*
|
|
3229
|
-
* @example
|
|
3230
|
-
* ```typescript
|
|
3231
|
-
* const definition: DecoratorNodeConfig = {
|
|
3232
|
-
* id: 'myNode',
|
|
3233
|
-
* type: 'decorator',
|
|
3234
|
-
* config: {
|
|
3235
|
-
* _methodName: 'myMethod',
|
|
3236
|
-
* _instance: myWorkflowInstance,
|
|
3237
|
-
* }
|
|
3238
|
-
* };
|
|
3239
|
-
*
|
|
3240
|
-
* const node = new DecoratorNode(definition);
|
|
3241
|
-
* const result = await node.execute(context);
|
|
3242
|
-
* ```
|
|
3243
|
-
*/
|
|
3244
|
-
declare class DecoratorNode implements Node {
|
|
3245
|
-
readonly type = "decorator";
|
|
3246
|
-
readonly id: string;
|
|
3247
|
-
private readonly methodName;
|
|
3248
|
-
private readonly instance;
|
|
3249
|
-
private readonly dependsOn;
|
|
3250
|
-
private readonly timeout?;
|
|
3251
|
-
private readonly retryConfig?;
|
|
3252
|
-
constructor(definition: NodeDefinition);
|
|
3253
|
-
/**
|
|
3254
|
-
* 执行装饰器方法
|
|
3255
|
-
*/
|
|
3256
|
-
execute(context: NodeExecutionContext): Promise<NodeExecutionResult>;
|
|
3257
|
-
/**
|
|
3258
|
-
* 准备输入参数
|
|
3259
|
-
*
|
|
3260
|
-
* 根据依赖的节点 ID,从 nodeOutputs 中提取对应的输出
|
|
3261
|
-
* 返回格式: { [depNodeId]: depOutput }
|
|
3262
|
-
*/
|
|
3263
|
-
private prepareInput;
|
|
3264
|
-
/**
|
|
3265
|
-
* 带重试的执行
|
|
3266
|
-
*/
|
|
3267
|
-
private executeWithRetry;
|
|
3268
|
-
/**
|
|
3269
|
-
* 带超时的执行
|
|
3270
|
-
*/
|
|
3271
|
-
private executeWithTimeout;
|
|
3272
|
-
/**
|
|
3273
|
-
* 计算退避延迟
|
|
3274
|
-
*/
|
|
3275
|
-
private calculateBackoff;
|
|
3276
|
-
/**
|
|
3277
|
-
* 睡眠
|
|
3278
|
-
*/
|
|
3279
|
-
private sleep;
|
|
3280
|
-
}
|
|
3281
|
-
/**
|
|
3282
|
-
* Workflow 转换器
|
|
3283
|
-
*
|
|
3284
|
-
* 负责将使用装饰器定义的类转换为标准的 WorkflowDefinition
|
|
3285
|
-
*/
|
|
3286
|
-
declare class WorkflowConverter {
|
|
3287
|
-
/**
|
|
3288
|
-
* 从装饰器类提取 Workflow 元数据
|
|
3289
|
-
*/
|
|
3290
|
-
static extractWorkflowMetadata(targetClass: Function): WorkflowMetadata2 | undefined;
|
|
3291
|
-
/**
|
|
3292
|
-
* 从装饰器类提取所有 Node 元数据
|
|
3293
|
-
*/
|
|
3294
|
-
static extractNodeMetadatas(targetClass: Function): NodeMetadata[];
|
|
3295
|
-
/**
|
|
3296
|
-
* 从装饰器类提取所有 Edge 元数据
|
|
3297
|
-
*/
|
|
3298
|
-
static extractEdgeMetadatas(targetClass: Function): EdgeMetadata[];
|
|
3299
|
-
/**
|
|
3300
|
-
* 将装饰器类转换为 WorkflowDefinition
|
|
3301
|
-
*/
|
|
3302
|
-
static fromClass(targetClass: new (...args: any[]) => any, instance: any): ConvertedWorkflowDefinition;
|
|
3303
|
-
/**
|
|
3304
|
-
* 构建节点映射,并合并边依赖
|
|
3305
|
-
*/
|
|
3306
|
-
private static buildNodeMap;
|
|
3307
|
-
/**
|
|
3308
|
-
* 验证装饰器类的有效性
|
|
3309
|
-
*/
|
|
3310
|
-
static validateWorkflowClass(targetClass: Function): ValidationResult2;
|
|
3311
|
-
/**
|
|
3312
|
-
* 检测是否存在循环依赖
|
|
3313
|
-
*
|
|
3314
|
-
* dependsOn 语义:A 依赖 B 表示 A.dependsOn = [B]
|
|
3315
|
-
* 这意味着 B 执行完成后才能执行 A,所以边是 B → A
|
|
3316
|
-
*
|
|
3317
|
-
* 检测方法:构建反向邻接表(谁依赖我),然后 DFS 检测环
|
|
3318
|
-
*/
|
|
3319
|
-
private static hasCycle;
|
|
3320
|
-
/**
|
|
3321
|
-
* 执行装饰器方法(用于 WorkflowEngine 调用)
|
|
3322
|
-
*/
|
|
3323
|
-
static executeNode(nodeDef: ConvertedNodeDefinition, context: {
|
|
3324
|
-
input: any;
|
|
3325
|
-
previousOutputs: Map<string, any>;
|
|
3326
|
-
}): Promise<{
|
|
3327
|
-
output: any;
|
|
3328
|
-
error?: Error;
|
|
3329
|
-
}>;
|
|
3330
|
-
}
|
|
3331
|
-
/**
|
|
3332
|
-
* 从装饰器类创建 WorkflowDefinition
|
|
3333
|
-
*
|
|
3334
|
-
* 这是最常用的便捷函数,自动处理实例创建和转换
|
|
3335
|
-
*
|
|
3336
|
-
* @param targetClass - 使用 @Workflow 装饰的类
|
|
3337
|
-
* @param options - 可选配置
|
|
3338
|
-
* @returns WorkflowDefinition
|
|
3339
|
-
*
|
|
3340
|
-
* @example
|
|
3341
|
-
* ```typescript
|
|
3342
|
-
* @Workflow({ name: 'my-workflow', entry: 'start' })
|
|
3343
|
-
* class MyWorkflow {
|
|
3344
|
-
* @NodeAs({ nodeId: 'start' })
|
|
3345
|
-
* async start() { return { done: true }; }
|
|
3346
|
-
* }
|
|
3347
|
-
*
|
|
3348
|
-
* // 方式一:自动创建实例
|
|
3349
|
-
* const workflow = createWorkflowFromClass(MyWorkflow);
|
|
3350
|
-
*
|
|
3351
|
-
* // 方式二:传入自定义实例
|
|
3352
|
-
* const myInstance = new MyWorkflow(customDep);
|
|
3353
|
-
* const workflow = createWorkflowFromClass(MyWorkflow, { instance: myInstance });
|
|
3354
|
-
* ```
|
|
3355
|
-
*/
|
|
3356
|
-
declare function createWorkflowFromClass(targetClass: new (...args: any[]) => any, options?: {
|
|
3357
|
-
/** 自定义实例,不传则自动创建 */
|
|
3358
|
-
instance?: any;
|
|
3359
|
-
/** 构造函数参数 */
|
|
3360
|
-
constructorArgs?: any[];
|
|
3361
|
-
}): ConvertedWorkflowDefinition;
|
|
3362
|
-
/**
|
|
3363
|
-
* ToolComponent
|
|
3364
|
-
* 工具管理核心组件
|
|
3365
|
-
*/
|
|
3366
|
-
declare class ToolComponent extends BaseComponent {
|
|
3367
|
-
readonly name = "tool";
|
|
3368
|
-
readonly version = "1.0.0";
|
|
3369
|
-
private registry;
|
|
3370
|
-
private validator;
|
|
3371
|
-
private config?;
|
|
3372
|
-
/** ConfigComponent 用于配置管理 */
|
|
3373
|
-
private configComponent?;
|
|
3374
|
-
/** 配置变更 watcher 清理函数 */
|
|
3375
|
-
private configWatcher?;
|
|
3376
|
-
constructor();
|
|
3377
|
-
/**
|
|
3378
|
-
* 初始化组件
|
|
3379
|
-
*
|
|
3380
|
-
* 配置加载优先级(从高到低):
|
|
3381
|
-
* 1. Object - 直接传入的 config 对象
|
|
3382
|
-
* 2. Env - 环境变量(通过 envPrefix 配置前缀)
|
|
3383
|
-
* 3. File - 配置文件(通过 configPath 指定)
|
|
3384
|
-
*/
|
|
3385
|
-
init(config?: ComponentConfig): Promise<void>;
|
|
3386
|
-
/**
|
|
3387
|
-
* 注册配置到 ConfigComponent
|
|
3388
|
-
*
|
|
3389
|
-
* 配置加载顺序(优先级从低到高):
|
|
3390
|
-
* 1. FileSource - 从配置文件加载(最低)
|
|
3391
|
-
* 2. EnvSource - 从环境变量加载(中等)
|
|
3392
|
-
* 3. MemorySource - 直接配置对象(最高)
|
|
3393
|
-
*
|
|
3394
|
-
* 实现步骤:
|
|
3395
|
-
* 1. 使用 registerComponent(TOOL_CONFIG_REGISTRATION) 注册配置
|
|
3396
|
-
* 2. 使用 load("tool") 加载已注册的配置
|
|
3397
|
-
* 3. 设置 TOOL_DEFAULTS 默认值
|
|
3398
|
-
* 4. 处理环境变量后备方案
|
|
3399
|
-
* 5. 处理 config 对象(最高优先级)
|
|
3400
|
-
*/
|
|
3401
|
-
private registerConfig;
|
|
3402
|
-
/**
|
|
3403
|
-
* 将配置对象展平为点号路径
|
|
3404
|
-
*/
|
|
3405
|
-
private flattenConfig;
|
|
3406
|
-
/**
|
|
3407
|
-
* 注册配置热更新监听
|
|
3408
|
-
*/
|
|
3409
|
-
private registerConfigWatcher;
|
|
3410
|
-
/**
|
|
3411
|
-
* 处理配置变更
|
|
3412
|
-
*/
|
|
3413
|
-
protected onConfigChange(event: {
|
|
3414
|
-
key: string;
|
|
3415
|
-
oldValue?: unknown;
|
|
3416
|
-
newValue?: unknown;
|
|
3417
|
-
}): void;
|
|
3418
|
-
/**
|
|
3419
|
-
* 停止组件
|
|
3420
|
-
*/
|
|
3421
|
-
onStop(): Promise<void>;
|
|
3422
|
-
/**
|
|
3423
|
-
* 注册工具
|
|
3424
|
-
*/
|
|
3425
|
-
register(tool: Tool, source?: ToolRegistration["source"]): void;
|
|
3426
|
-
/**
|
|
3427
|
-
* 批量注册工具
|
|
3428
|
-
*/
|
|
3429
|
-
registerMany(tools: Tool[], source?: ToolRegistration["source"]): void;
|
|
3430
|
-
/**
|
|
3431
|
-
* 注销工具
|
|
3432
|
-
*/
|
|
3433
|
-
unregister(name: string): boolean;
|
|
3434
|
-
/**
|
|
3435
|
-
* 获取工具
|
|
3436
|
-
*/
|
|
3437
|
-
getTool(name: string): Tool | undefined;
|
|
3438
|
-
/**
|
|
3439
|
-
* 列出工具
|
|
3440
|
-
*/
|
|
3441
|
-
listTools(filter?: ToolListFilter): Tool[];
|
|
3442
|
-
/**
|
|
3443
|
-
* 获取工具数量
|
|
3444
|
-
*/
|
|
3445
|
-
getToolCount(): number;
|
|
3446
|
-
/**
|
|
3447
|
-
* 检查工具是否存在
|
|
3448
|
-
*/
|
|
3449
|
-
hasTool(name: string): boolean;
|
|
3450
|
-
/**
|
|
3451
|
-
* 按分类获取工具
|
|
3452
|
-
*/
|
|
3453
|
-
getToolsByCategory(category: string): Tool[];
|
|
3454
|
-
/**
|
|
3455
|
-
* 执行工具
|
|
3456
|
-
*/
|
|
3457
|
-
execute(request: ToolExecuteRequest): Promise<ToolResult2>;
|
|
3458
|
-
/**
|
|
3459
|
-
* 加载内置工具
|
|
3460
|
-
*/
|
|
3461
|
-
loadBuiltInTools(): Promise<void>;
|
|
3462
|
-
/**
|
|
3463
|
-
* 获取 Tool Hook 点列表
|
|
3464
|
-
*/
|
|
3465
|
-
getHookPoints(): string[];
|
|
3466
|
-
}
|
|
3467
|
-
declare class WorkflowComponent extends BaseComponent {
|
|
3468
|
-
readonly name = "workflow";
|
|
3469
|
-
readonly version = "1.0.0";
|
|
3470
|
-
workflowService: WorkflowService | null;
|
|
3471
|
-
sessionComponent: any;
|
|
3472
|
-
private nodeRegistry;
|
|
3473
|
-
private decoratorsRegistered;
|
|
3474
|
-
/**
|
|
3475
|
-
* 获取 WorkflowService 实例
|
|
3476
|
-
*/
|
|
3477
|
-
getService(): WorkflowService | null;
|
|
3478
|
-
private toolComponent?;
|
|
3479
|
-
private llmComponent?;
|
|
3480
|
-
private logTraceComponent?;
|
|
3481
|
-
private _workflowEnv?;
|
|
3482
|
-
private skillRegistry?;
|
|
3483
|
-
private agentRunner?;
|
|
3484
|
-
/**
|
|
3485
|
-
* 初始化 WorkflowComponent
|
|
3486
|
-
*/
|
|
3487
|
-
init(options: any): Promise<void>;
|
|
3488
|
-
/**
|
|
3489
|
-
* 注册 decorator 节点类型
|
|
3490
|
-
*/
|
|
3491
|
-
private registerDecorators;
|
|
3492
|
-
/**
|
|
3493
|
-
* 创建 WorkflowService(需要外部传入依赖)
|
|
3494
|
-
*
|
|
3495
|
-
* Note: After session-based refactoring, runRepository is no longer needed.
|
|
3496
|
-
* Workflow runs are managed via SessionComponent.
|
|
3497
|
-
*/
|
|
3498
|
-
createService(options: {
|
|
3499
|
-
workflowRepository: any;
|
|
3500
|
-
toolComponent?: ToolComponent;
|
|
3501
|
-
llmComponent?: LLMComponent;
|
|
3502
|
-
env?: Environment;
|
|
3503
|
-
skillRegistry?: {
|
|
3504
|
-
getSkill(name: string): any;
|
|
3505
|
-
hasSkill(name: string): boolean;
|
|
3506
|
-
};
|
|
3507
|
-
sessionComponent?: any;
|
|
3508
|
-
}): WorkflowService;
|
|
3509
|
-
/**
|
|
3510
|
-
* 运行工作流定义
|
|
3511
|
-
*/
|
|
3512
|
-
runWorkflow(definition: WorkflowDefinition, input?: Record<string, any>, options?: RunOptions): Promise<RunResult>;
|
|
3513
|
-
/**
|
|
3514
|
-
* 停止组件并清理资源
|
|
3515
|
-
*
|
|
3516
|
-
* 遵循 Component 接口契约,通过 onStop 钩子实现
|
|
3517
|
-
*/
|
|
3518
|
-
protected onStop(): Promise<void>;
|
|
3519
|
-
/**
|
|
3520
|
-
* 启动组件 - 初始化持久化的 SQLite WorkflowService
|
|
3521
|
-
*/
|
|
3522
|
-
start(): Promise<void>;
|
|
3523
|
-
/**
|
|
3524
|
-
* 注册 ask_user 工具到 ToolComponent
|
|
3525
|
-
*
|
|
3526
|
-
* 这样 Workflow 中的 AgentNode 执行时,Agent 能够找到并调用 ask_user 工具。
|
|
3527
|
-
* ask_user 工具会抛出 AskUserError,触发 workflow pause。
|
|
3528
|
-
*/
|
|
3529
|
-
private registerAskUserTool;
|
|
3530
|
-
/**
|
|
3531
|
-
* 初始化 SQLite 持久化的 WorkflowService
|
|
3532
|
-
*
|
|
3533
|
-
* Note: After session-based refactoring, runRepository is no longer needed.
|
|
3534
|
-
* Workflow runs are managed via SessionComponent.
|
|
3535
|
-
*/
|
|
3536
|
-
private initSqliteService;
|
|
3537
|
-
/**
|
|
3538
|
-
* 创建内存中的 WorkflowService(用于快速运行或降级)
|
|
3539
|
-
*/
|
|
3540
|
-
private createInMemoryService;
|
|
3541
|
-
/**
|
|
3542
|
-
* 停止组件
|
|
3543
|
-
*/
|
|
3544
|
-
stop(): Promise<void>;
|
|
3545
|
-
}
|
|
3546
|
-
export { parseWorkflowFileSync, parseWorkflowFile, isWorkflowSessionMetadata, getWorkflowSessionStatus, createWorkflowFromClass, createWorkflowEvent, createWorkflowAskUserEvent, createNodeInterruptEvent, createNodeExecutionContext, WorkflowStoppedEventSchema, WorkflowStartedEventSchema, WorkflowSessionMetadata2 as WorkflowSessionMetadata, WorkflowRun, WorkflowResumedEventSchema, WorkflowPausedEventSchema, WorkflowOutputEventSchema, WorkflowNodeStartPart2 as WorkflowNodeStartPart, WorkflowNodeStartMetadata, WorkflowNodeResumePart2 as WorkflowNodeResumePart, WorkflowNodeResumeMetadata, WorkflowNodeInterruptPart2 as WorkflowNodeInterruptPart, WorkflowNodeInterruptMetadata, WorkflowNodeEndPart2 as WorkflowNodeEndPart, WorkflowNodeEndMetadata, WorkflowMetadataSchema, WorkflowMetadata, WorkflowMessageRoleSchema, WorkflowMessageRole, WorkflowMessagePart, WorkflowMessageMetadata, WorkflowInputsSchema, WorkflowInputs, WorkflowFailedEventSchema, WorkflowEventSchema, WorkflowEvent, WorkflowEdges, WorkflowDefinitionSchema, WorkflowDefinitionExtended, WorkflowDefinition, WorkflowConverter, WorkflowConfigSchema, WorkflowConfig, WorkflowComponent, WorkflowCompletedEventSchema, WorkflowAskUserEventSchema, WorkflowAskUserEvent, Workflow2 as Workflow, TemplateResolver, RuntimeState, RunStatusSchema, RunStatus, RunResult, RunOptions, RetryConfigSchema, RetryConfig, ParsedWorkflow, OutputDefinitionSchema, OutputDefinition, NodeStatusSchema, NodeStatus, NodeStartedEventSchema, NodeSkippedEventSchema, NodeScheduledEventSchema, NodeRun, NodeRemovedEventSchema, NodeProgressEventSchema, NodeInterruptEventSchema, NodeInterruptEvent, NodeFailedEventSchema, NodeExecutionResult, NodeExecutionContext, NodeDefinitionSchema, NodeDefinition, NodeDataEventSchema, NodeCompletedEventSchema, NodeAs, NodeAddedEventSchema, Node, InputParameterSchema, InputParameter, Edge, DependsOnSchema, DependsOn, DecoratorNode, ControlStopEventSchema, ControlResumeEventSchema, ControlPauseEventSchema, BaseEventSchema, BaseEvent, AskUserOptions, AskUserError, AgentSessionRef2 as AgentSessionRef, AgentSessionInfo };
|