@ai-setting/roy-agent-core 1.5.16-test → 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.
Files changed (54) hide show
  1. package/dist/env/index.js +20 -8
  2. package/dist/env/log-trace/index.js +1 -1
  3. package/dist/env/prompt/index.js +1 -1
  4. package/dist/env/workflow/engine/index.js +1 -1
  5. package/dist/env/workflow/index.js +2 -2
  6. package/dist/index.js +6 -7
  7. package/dist/shared/@ai-setting/{roy-agent-core-xq8hhqb8.js → roy-agent-core-4wjywp3c.js} +4 -2
  8. package/dist/shared/@ai-setting/roy-agent-core-8jxva565.js +19 -0
  9. package/dist/shared/@ai-setting/roy-agent-core-avq1x4t7.js +84 -0
  10. package/dist/shared/@ai-setting/{roy-agent-core-gq20wsgv.js → roy-agent-core-ffb9fq4v.js} +23 -2
  11. package/dist/shared/@ai-setting/{roy-agent-core-93zfb3r1.js → roy-agent-core-mrcxzpbg.js} +1 -1
  12. package/dist/shared/@ai-setting/{roy-agent-core-rhmtwnw1.js → roy-agent-core-pw7cv1px.js} +1 -1
  13. package/dist/shared/@ai-setting/{roy-agent-core-gbqcyegm.js → roy-agent-core-rccptwv0.js} +512 -673
  14. package/dist/shared/@ai-setting/{roy-agent-core-wrcy0h6z.js → roy-agent-core-ty94k28r.js} +1 -1
  15. package/package.json +1 -1
  16. package/dist/config/index.d.ts +0 -1250
  17. package/dist/env/agent/index.d.ts +0 -2279
  18. package/dist/env/commands/index.d.ts +0 -1131
  19. package/dist/env/debug/formatters/index.d.ts +0 -236
  20. package/dist/env/debug/index.d.ts +0 -1652
  21. package/dist/env/hook/index.d.ts +0 -279
  22. package/dist/env/index.d.ts +0 -3481
  23. package/dist/env/llm/index.d.ts +0 -1760
  24. package/dist/env/log-trace/index.d.ts +0 -1574
  25. package/dist/env/mcp/index.d.ts +0 -1331
  26. package/dist/env/mcp/tool/index.d.ts +0 -183
  27. package/dist/env/memory/built-in/index.d.ts +0 -232
  28. package/dist/env/memory/index.d.ts +0 -1799
  29. package/dist/env/memory/plugin/index.d.ts +0 -747
  30. package/dist/env/prompt/index.d.ts +0 -1164
  31. package/dist/env/session/index.d.ts +0 -1908
  32. package/dist/env/session/storage/index.d.ts +0 -564
  33. package/dist/env/skill/index.d.ts +0 -1266
  34. package/dist/env/skill/tool/index.d.ts +0 -193
  35. package/dist/env/task/delegate/index.d.ts +0 -1612
  36. package/dist/env/task/events/index.d.ts +0 -171
  37. package/dist/env/task/hooks/index.d.ts +0 -624
  38. package/dist/env/task/index.d.ts +0 -1553
  39. package/dist/env/task/plugins/index.d.ts +0 -466
  40. package/dist/env/task/storage/index.d.ts +0 -241
  41. package/dist/env/task/tools/index.d.ts +0 -1485
  42. package/dist/env/task/tools/operation/index.d.ts +0 -1484
  43. package/dist/env/tool/built-in/index.d.ts +0 -218
  44. package/dist/env/tool/index.d.ts +0 -1396
  45. package/dist/env/workflow/decorators/index.d.ts +0 -2161
  46. package/dist/env/workflow/engine/index.d.ts +0 -3453
  47. package/dist/env/workflow/index.d.ts +0 -3546
  48. package/dist/env/workflow/nodes/index.d.ts +0 -2092
  49. package/dist/env/workflow/service/index.d.ts +0 -227
  50. package/dist/env/workflow/storage/index.d.ts +0 -165
  51. package/dist/env/workflow/tools/index.d.ts +0 -416
  52. package/dist/env/workflow/types/index.d.ts +0 -2255
  53. package/dist/env/workflow/utils/index.d.ts +0 -2031
  54. package/dist/index.d.ts +0 -7858
@@ -1,3481 +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
- * 创建 Hook 的工厂函数
86
- *
87
- * @param meta Hook 元信息
88
- * @param fn 执行函数
89
- * @returns Hook 实例
90
- *
91
- * @example
92
- * ```typescript
93
- * const hook = createHook(
94
- * { name: "my-hook", priority: 10 },
95
- * async (ctx) => { console.log(ctx.data); }
96
- * );
97
- * ```
98
- */
99
- declare function createHook<T = unknown>(meta: HookMeta, fn: HookFn<T>): Hook<T>;
100
- /**
101
- * 创建带优先级的 Hook 的便捷函数
102
- */
103
- declare function createPriorityHook<T = unknown>(name: string, fn: HookFn<T>, priority: number): Hook<T>;
104
- /**
105
- * HookManager 配置
106
- */
107
- interface HookManagerOptions {
108
- /** 组件名称(用于上下文) */
109
- componentName?: string;
110
- /** 组件版本(用于上下文) */
111
- componentVersion?: string;
112
- /** 默认优先级 */
113
- defaultPriority?: number;
114
- }
115
- /**
116
- * HookManager
117
- *
118
- * 统一管理 Component 的 Hook
119
- *
120
- * @example
121
- * ```typescript
122
- * const manager = new HookManager<MyContext>();
123
- *
124
- * // 注册 Hook
125
- * manager.register("before-action", {
126
- * name: "my-hook",
127
- * priority: 10,
128
- * execute: async (ctx) => { /* ... *\/ }
129
- * });
130
- *
131
- * // 执行 Hooks
132
- * await manager.execute("before-action", data);
133
- * ```
134
- */
135
- declare class HookManager<T = unknown> {
136
- private _hooks;
137
- private componentName;
138
- private componentVersion;
139
- private defaultPriority;
140
- constructor(options?: HookManagerOptions);
141
- /**
142
- * 注册 Hook 到指定 hook 点
143
- */
144
- register(hookPoint: string, hook: Hook<T>): void;
145
- /**
146
- * 注册多个 Hook
147
- */
148
- registerMany(hookPoint: string, hooks: Hook<T>[]): void;
149
- /**
150
- * 按名称取消注册 Hook
151
- * @returns 是否成功取消注册
152
- */
153
- unregister(hookPoint: string, name: string): boolean;
154
- /**
155
- * 取消注册所有指定 hook 点的 Hook
156
- */
157
- unregisterAll(hookPoint: string): void;
158
- /**
159
- * 执行指定 hook 点的所有 Hook
160
- */
161
- execute(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
162
- /**
163
- * 执行指定 hook 点的所有 Hook 并收集返回值
164
- */
165
- executeAndCollect<R>(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<R[]>;
166
- /**
167
- * 获取指定 hook 点的 Hook 数量
168
- */
169
- count(hookPoint: string): number;
170
- /**
171
- * 清空所有 Hook
172
- */
173
- clear(): void;
174
- /**
175
- * 获取所有已注册的 hook 点
176
- */
177
- getHookPoints(): string[];
178
- /**
179
- * 检查 hook 点是否有任何 Hook
180
- */
181
- hasHooks(hookPoint: string): boolean;
182
- /**
183
- * 设置组件信息
184
- */
185
- setComponentInfo(name: string, version: string): void;
186
- /**
187
- * 获取所有注册的 hooks(只读副本)
188
- */
189
- get hooks(): ReadonlyMap<string, Hook<T>[]>;
190
- /**
191
- * 执行 Hook 并支持干预机制
192
- *
193
- * 如果任何一个 Hook 返回 { stop: true },执行会停止
194
- * Hook 可以通过返回 HookResult 来执行干预动作
195
- *
196
- * @param hookPoint Hook 点
197
- * @param data 数据
198
- * @param metadata 元数据
199
- * @returns 执行结果
200
- */
201
- executeWithIntervention(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<HookResult>;
202
- /**
203
- * 获取或创建 hook 列表
204
- */
205
- private getOrCreateHooks;
206
- /**
207
- * 按优先级排序 Hook
208
- *
209
- * 优先级相同时保持原顺序(稳定排序)
210
- */
211
- private sortHooks;
212
- /**
213
- * 创建 Hook 上下文
214
- */
215
- private createContext;
216
- /**
217
- * 安全执行 Hook(错误隔离)
218
- */
219
- private safeExecute;
220
- /**
221
- * 安全执行 Hook 并返回结果
222
- */
223
- private safeExecuteAndReturn;
224
- }
225
- import { ZodType as ZodType4, ZodTypeDef as ZodTypeDef2 } from "zod";
226
- /**
227
- * @fileoverview Environment types and interfaces
228
- *
229
- * 定义 Environment 的核心类型,包括:
230
- * - Environment 配置
231
- * - EnvEvent 事件类型
232
- */
233
- /**
234
- * Environment Config
235
- */
236
- interface EnvironmentConfig {
237
- /** 环境名称 */
238
- name: string;
239
- /** 环境版本 */
240
- version: string;
241
- /** 是否启用 */
242
- enabled: boolean;
243
- }
244
- /**
245
- * BaseEnvironment 配置
246
- */
247
- interface BaseEnvironmentConfig {
248
- /** 环境名称 */
249
- name?: string;
250
- /** 环境版本 */
251
- version?: string;
252
- /** 是否启用 */
253
- enabled?: boolean;
254
- }
255
- /**
256
- * EnvEvent 元信息
257
- */
258
- interface EnvEventMetadata {
259
- /** 触发事件的 session ID */
260
- trigger_session_id?: string;
261
- /** 触发事件的 agent ID */
262
- trigger_agent_id?: string;
263
- /** Agent 名称 */
264
- trigger_agent_name?: string;
265
- /** 环境名称 */
266
- env_name?: string;
267
- /** 事件来源 */
268
- source?: string;
269
- /** 其他元数据 */
270
- [key: string]: unknown;
271
- }
272
- /**
273
- * EnvEvent
274
- *
275
- * 统一的事件机制,用于:
276
- * - Stream 事件(stream.start, stream.text, stream.completed 等)
277
- * - 工具调用事件(tool.call, tool.result 等)
278
- * - 会话事件(session.created, session.updated 等)
279
- * - Agent 事件(agent.thinking, agent.acting 等)
280
- */
281
- interface EnvEvent<T = unknown> {
282
- /** 事件 ID */
283
- id: string;
284
- /** 事件类型 */
285
- type: string;
286
- /** 时间戳 */
287
- timestamp: number;
288
- /** 元信息 */
289
- metadata: EnvEventMetadata;
290
- /** 事件负载 */
291
- payload: T;
292
- }
293
- /**
294
- * EnvEvent Handler
295
- */
296
- type EnvEventHandler = (event: EnvEvent) => void | Promise<void>;
297
- /**
298
- * EnvEvent 创建参数(部分属性,可选)
299
- *
300
- * 用于 pushEnvEvent 方法,允许传入部分属性,自动补全必填字段
301
- */
302
- interface EnvEventInput {
303
- /** 事件类型(必填) */
304
- type: string;
305
- /** 事件 ID(可选,自动生成) */
306
- id?: string;
307
- /** 时间戳(可选,自动生成) */
308
- timestamp?: number;
309
- /** 元信息(可选,自动创建) */
310
- metadata?: EnvEventMetadata;
311
- /** 事件负载(可选) */
312
- payload?: unknown;
313
- }
314
- /**
315
- * Action 类型
316
- */
317
- interface Action {
318
- /** Action 类型 */
319
- type: string;
320
- /** Action 参数 */
321
- params?: Record<string, unknown>;
322
- }
323
- /**
324
- * Tool Result
325
- */
326
- interface ToolResult {
327
- /** 是否成功 */
328
- success: boolean;
329
- /** 输出内容 */
330
- output: string | Record<string, unknown>;
331
- /** 错误信息 */
332
- error?: string;
333
- }
334
- /**
335
- * Context
336
- */
337
- interface Context {
338
- /** 会话 ID */
339
- sessionId?: string;
340
- /** 用户 ID */
341
- userId?: string;
342
- /** 工作目录 */
343
- workdir?: string;
344
- /** 中断信号 */
345
- abort?: AbortSignal;
346
- /** 元数据 */
347
- metadata?: Record<string, unknown>;
348
- }
349
- import { ModelMessage } from "ai";
350
- import { z, ZodType as ZodType2, ZodError } from "zod";
351
- /**
352
- * 沙箱配置
353
- */
354
- interface SandboxConfig {
355
- /** 是否启用沙箱 */
356
- enabled: boolean;
357
- /** 沙箱类型 */
358
- type: "native" | "docker";
359
- /** 动作过滤 */
360
- actionFilter?: {
361
- /** 包含的动作 */
362
- include?: string[];
363
- /** 排除的动作 */
364
- exclude?: string[];
365
- };
366
- /** 文件系统限制 */
367
- filesystem?: {
368
- /** 允许读取 */
369
- allowRead?: string[];
370
- /** 禁止读取 */
371
- denyRead?: string[];
372
- /** 允许写入 */
373
- allowWrite?: string[];
374
- /** 禁止写入 */
375
- denyWrite?: string[];
376
- };
377
- /** 网络限制 */
378
- network?: {
379
- /** 允许的域名 */
380
- allowedDomains?: string[];
381
- /** 禁止的域名 */
382
- deniedDomains?: string[];
383
- };
384
- /** Docker 配置 */
385
- docker?: {
386
- /** 镜像 */
387
- image?: string;
388
- /** 网络模式 */
389
- networkMode?: "bridge" | "host" | "none";
390
- /** 卷挂载 */
391
- volumes?: Record<string, string>;
392
- };
393
- }
394
- /**
395
- * 工具执行上下文
396
- */
397
- interface ToolContext {
398
- /** 工作目录 */
399
- workdir?: string;
400
- /** 用户 ID */
401
- user_id?: string;
402
- /** 会话 ID */
403
- session_id?: string;
404
- /** 消息 ID */
405
- message_id?: string;
406
- /** 中断信号 */
407
- abort?: AbortSignal;
408
- /** 额外元数据 */
409
- metadata?: Record<string, unknown>;
410
- /** 沙箱配置 */
411
- sandbox?: SandboxConfig;
412
- /** 沙箱 Provider */
413
- sandboxProvider?: unknown | null;
414
- }
415
- /**
416
- * 工具结果元数据
417
- */
418
- interface ToolResultMetadata {
419
- /** 执行时间(毫秒) */
420
- execution_time_ms: number;
421
- /** 输出大小(字节) */
422
- output_size?: number;
423
- /** 标准输出 */
424
- stdout?: string;
425
- /** 标准错误 */
426
- stderr?: string;
427
- /** 退出码 */
428
- exit_code?: number;
429
- /** Token 使用量 */
430
- usage?: {
431
- inputTokens: number;
432
- outputTokens: number;
433
- totalTokens?: number;
434
- };
435
- /** 额外元数据 */
436
- [key: string]: unknown;
437
- }
438
- /**
439
- * 工具执行结果
440
- */
441
- interface ToolResult2 {
442
- /** 是否成功 */
443
- success: boolean;
444
- /** 输出内容 */
445
- output: string | Record<string, unknown>;
446
- /** 错误信息 */
447
- error?: string;
448
- /** 结果元数据 */
449
- metadata?: ToolResultMetadata;
450
- }
451
- /**
452
- * 工具元数据
453
- */
454
- interface ToolMetadata {
455
- /** 分类 */
456
- category?: string;
457
- /** 标签 */
458
- tags?: string[];
459
- /** 版本 */
460
- version?: string;
461
- /** 作者 */
462
- author?: string;
463
- /** 是否实验性 */
464
- experimental?: boolean;
465
- /** MCP 工具信息(MCP adapter 使用) */
466
- mcpTool?: {
467
- originalName: string;
468
- originalDescription: string;
469
- inputSchema?: unknown;
470
- };
471
- }
472
- /**
473
- * 工具沙箱配置
474
- */
475
- interface ToolSandboxConfig {
476
- /** 是否启用沙箱 */
477
- enabled: boolean;
478
- /** 沙箱配置 */
479
- config?: Partial<SandboxConfig>;
480
- }
481
- /**
482
- * 工具权限配置
483
- */
484
- interface ToolPermissionConfig {
485
- /** 是否需要用户确认 */
486
- requireConfirmation?: boolean;
487
- /** 权限级别 */
488
- level?: "safe" | "dangerous" | "critical";
489
- }
490
- /**
491
- * 工具信息
492
- */
493
- interface ToolInfo<Parameters extends ZodType2 = ZodType2> {
494
- /** 工具名称 */
495
- name: string;
496
- /** 工具描述 */
497
- description: string;
498
- /** 参数 Schema */
499
- parameters: Parameters;
500
- /** 初始化函数(可选) */
501
- init?: (ctx?: ToolContext) => Promise<void>;
502
- /** 执行函数 */
503
- execute: (args: z.infer<Parameters>, ctx: ToolContext) => Promise<ToolResult2>;
504
- /** 参数验证错误格式化(可选) */
505
- formatValidationError?: (error: ZodError) => string;
506
- /** 沙箱配置 */
507
- sandbox?: ToolSandboxConfig;
508
- /** 权限配置 */
509
- permission?: ToolPermissionConfig;
510
- /** 工具元数据 */
511
- metadata?: ToolMetadata;
512
- }
513
- /**
514
- * 工具类型别名
515
- */
516
- type Tool = ToolInfo;
517
- /**
518
- * 工具注册来源
519
- */
520
- type ToolSource = "built-in" | "plugin" | "dynamic";
521
- /**
522
- * 工具注册信息
523
- */
524
- interface ToolRegistration {
525
- /** 工具实例 */
526
- tool: Tool;
527
- /** 注册时间 */
528
- registeredAt: number;
529
- /** 注册来源 */
530
- source: ToolSource;
531
- /** 是否启用 */
532
- enabled: boolean;
533
- }
534
- /**
535
- * 工具执行请求
536
- */
537
- interface ToolExecuteRequest {
538
- /** 工具名称 */
539
- name: string;
540
- /** 参数 */
541
- args: Record<string, unknown>;
542
- /** 上下文 */
543
- context: ToolContext;
544
- /** 是否跳过 Hook */
545
- skipHooks?: boolean;
546
- }
547
- /**
548
- * 工具列表过滤器
549
- */
550
- interface ToolListFilter {
551
- /** 按名称过滤(支持 glob) */
552
- name?: string | string[];
553
- /** 按分类过滤 */
554
- category?: string;
555
- /** 按标签过滤 */
556
- tags?: string[];
557
- /** 按来源过滤 */
558
- source?: ToolSource;
559
- /** 只返回启用的工具 */
560
- enabledOnly?: boolean;
561
- /** 排除的工具 */
562
- exclude?: string[];
563
- }
564
- /**
565
- * LLM Message
566
- */
567
- interface LLMMessage {
568
- /** 消息角色 */
569
- role: "system" | "user" | "assistant" | "tool";
570
- /** 消息内容 */
571
- content: string;
572
- /** 工具调用(仅 assistant 角色) */
573
- toolCalls?: ToolCall[];
574
- /** 工具调用 ID(仅 tool 角色) */
575
- toolCallId?: string;
576
- /** 名称(用于 function 调用) */
577
- name?: string;
578
- }
579
- /**
580
- * Tool Call (OpenAI format)
581
- */
582
- interface ToolCall {
583
- /** 工具调用 ID */
584
- id: string;
585
- /** 工具名称 */
586
- name: string;
587
- /** 工具参数(JSON 字符串) */
588
- arguments: string;
589
- /** OpenAI 格式的 function 对象 */
590
- function?: {
591
- /** 函数名称 */
592
- name: string;
593
- /** 函数参数(JSON 字符串) */
594
- arguments: string;
595
- };
596
- }
597
- /**
598
- * Usage Information
599
- */
600
- interface UsageInfo {
601
- /** Prompt Token 数 */
602
- promptTokens: number;
603
- /** Completion Token 数 */
604
- completionTokens: number;
605
- /** 总 Token 数 */
606
- totalTokens: number;
607
- }
608
- /**
609
- * LLM Output
610
- */
611
- interface LLMOutput {
612
- /** 生成的内容 */
613
- content: string;
614
- /** 推理/思考内容(支持 reasoning 模型的输出) */
615
- reasoning?: string;
616
- /** 完成原因 */
617
- finishReason: "stop" | "length" | "content-filter" | "tool-calls" | "function-call";
618
- /** 工具调用 */
619
- toolCalls?: ToolCall[];
620
- /** Usage 信息 */
621
- usage?: UsageInfo;
622
- /** 生成模型 */
623
- model?: string;
624
- }
625
- /**
626
- * 模型限制配置
627
- */
628
- interface ModelLimits {
629
- /** 上下文窗口大小(Token 数) */
630
- contextWindow: number;
631
- /** 压缩阈值(0-1),超过此比例触发上下文压缩 */
632
- compactionThreshold?: number;
633
- /** 最大输出 Token 数 */
634
- maxOutputTokens?: number;
635
- }
636
- /**
637
- * LLM Invoke 请求
638
- */
639
- interface LLMInvokeRequest {
640
- /** 消息列表 */
641
- messages: LLMMessage[];
642
- /** Provider ID(可选,默认使用配置的默认 Provider) */
643
- providerId?: string;
644
- /** 模型(可选,默认使用配置的默认模型) */
645
- model?: string;
646
- /** 工具列表 */
647
- tools?: ToolInfo2[];
648
- /** 温度参数 */
649
- temperature?: number;
650
- /** 最大 Token 数 */
651
- maxTokens?: number;
652
- /** Top P */
653
- topP?: number;
654
- /** 工具选择策略 */
655
- toolChoice?: "auto" | "none" | {
656
- type: "function";
657
- function: {
658
- name: string;
659
- };
660
- };
661
- /** 停止序列 */
662
- stopSequences?: string[];
663
- /** Abort Signal */
664
- abortSignal?: AbortSignal;
665
- /** 调用上下文(用于事件元数据) */
666
- context?: LLMInvokeContext;
667
- /** 跳过上下文阈值检查(用于 SummaryAgent 等内部调用,避免循环触发压缩) */
668
- skipThresholdCheck?: boolean;
669
- }
670
- /**
671
- * LLM Invoke 结果
672
- */
673
- interface LLMInvokeResult {
674
- /** LLM 输出 */
675
- output: LLMOutput;
676
- /** 使用的 Provider ID */
677
- providerId: string;
678
- /** 使用的模型 */
679
- model: string;
680
- /** 调用延迟(毫秒) */
681
- latencyMs: number;
682
- }
683
- /**
684
- * Tool Info(用于 LLM 工具调用)
685
- */
686
- interface ToolInfo2 {
687
- /** 工具名称 */
688
- name: string;
689
- /** 工具描述 */
690
- description?: string;
691
- /** 工具参数 Schema */
692
- parameters: Record<string, unknown>;
693
- }
694
- /**
695
- * LLM 调用上下文(用于事件元数据)
696
- */
697
- interface LLMInvokeContext {
698
- /** Session ID */
699
- sessionId?: string;
700
- /** Message ID */
701
- messageId?: string;
702
- }
703
- type AbortSignalType = AbortSignal;
704
- interface ProjectSummary {
705
- projectPath: string;
706
- projectName: string;
707
- lastUpdated: number;
708
- summary: string;
709
- shortSummary: string;
710
- draftCount: number;
711
- memoryMdPath: string;
712
- draftsDir: string;
713
- detailsDir: string;
714
- }
715
- /**
716
- * Agent 执行上下文
717
- */
718
- interface AgentContext {
719
- /** 会话 ID */
720
- sessionId?: string;
721
- /** 消息 ID */
722
- messageId?: string;
723
- /** 中断信号 */
724
- abort?: AbortSignalType;
725
- /** 额外的上下文信息(会添加到 system prompt) */
726
- additionInfo?: string;
727
- /** 元数据 */
728
- metadata?: Record<string, unknown>;
729
- /** Agent 类型(用于选择 Behavior Spec) */
730
- agentType?: string;
731
- /** 使用的模型 */
732
- model?: string;
733
- /** 允许的工具列表(上下文级别,会覆盖 agent 配置) */
734
- allowedTools?: string[];
735
- /** 拒绝的工具列表(上下文级别,会覆盖 agent 配置) */
736
- deniedTools?: string[];
737
- /** 是否过滤 history 中的 tool 消息(上下文级别,会覆盖 agent 配置) */
738
- filterHistory?: boolean;
739
- }
740
- /**
741
- * 工具调用(内部使用)
742
- */
743
- interface ToolCall2 {
744
- /** 调用 ID */
745
- id: string;
746
- /** 工具名称 */
747
- name: string;
748
- /** 调用参数 */
749
- arguments: Record<string, unknown>;
750
- }
751
- /**
752
- * 工具调用结果
753
- */
754
- interface ToolCallResult {
755
- /** 调用 ID */
756
- id: string;
757
- /** 工具名称 */
758
- name: string;
759
- /** 调用结果 */
760
- result: ToolResult2;
761
- /** 是否成功 */
762
- success: boolean;
763
- }
764
- /**
765
- * Hook 点枚举(带 component 前缀)
766
- */
767
- type HookPoint = "agent:before.start" | "agent:before.llm" | "agent:after.llm" | "agent:before.tool" | "agent:after.tool" | "agent:on.iteration" | "agent:on.threshold" | "agent:after.complete" | "agent:on.error" | "agent:after.react" | "on.error";
768
- /**
769
- * Hook 上下文(Agent 特有版本)
770
- *
771
- * 注意:这是 AgentComponent 使用的 HookContext 扩展版本
772
- * 包含 iteration, maxIterations, messages 等 Agent 特有字段
773
- */
774
- interface HookContext2 {
775
- /** 当前 LLM 输出(after.llm hook 可用) */
776
- llmOutput?: AgentLLMOutput;
777
- /** 当前工具调用(before.tool/after.tool hook 可用) */
778
- currentToolCall?: ToolCall2;
779
- /** 工具执行结果(after.tool hook 可用) */
780
- toolResult?: ToolCallResult;
781
- /** 阈值信息(on.threshold hook 可用) */
782
- threshold?: {
783
- type: "tokens" | "turns" | "iterations" | "custom";
784
- value: number;
785
- limit?: number;
786
- };
787
- /** 当前错误(on.error hook 可用) */
788
- error?: Error;
789
- /** 当前 Agent 实例 */
790
- agent: AgentInstance;
791
- /** 当前迭代次数(可修改) */
792
- iteration: number;
793
- /** 最大迭代次数限制(before.llm hook 可用) */
794
- maxIterations: number;
795
- /** 消息列表(可直接修改) */
796
- messages: ModelMessage[];
797
- /** 可用工具列表(可直接修改) */
798
- tools: Tool[];
799
- /** 当前 system prompt(可直接修改) */
800
- systemPrompt: string;
801
- /** 额外的上下文信息(可直接修改) */
802
- additionInfo?: string;
803
- /** 执行上下文(可直接修改属性) */
804
- context: AgentContext;
805
- /** 是否已停止 */
806
- _stopped?: boolean;
807
- /** 停止原因 */
808
- _stopReason?: string;
809
- /** 待执行的干预动作 */
810
- _pendingAction?: {
811
- type: HookActionType2;
812
- payload?: unknown;
813
- };
814
- /** 错误列表 */
815
- _errors?: Array<{
816
- plugin: string;
817
- error: string;
818
- }>;
819
- /** Project Memory Agent 生成的摘要列表(由 Project Agent 写入,Global Agent 读取) */
820
- projectSummaries?: ProjectSummary[];
821
- }
822
- /**
823
- * Hook 干预动作
824
- */
825
- type HookActionType2 = "stop" | "retry" | "compress" | "extract_memory" | "inject_message" | "skip_tool";
826
- /**
827
- * Hook 返回结果
828
- */
829
- interface HookResult2 {
830
- /** 是否继续执行(false = 停止 Agent) */
831
- continue: boolean;
832
- /** 干预动作(可选) */
833
- action?: {
834
- type: HookActionType2;
835
- payload?: unknown;
836
- };
837
- }
838
- /**
839
- * Plugin 定义
840
- */
841
- interface Plugin {
842
- /** Plugin 唯一名称 */
843
- name: string;
844
- /** Plugin 版本 */
845
- version: string;
846
- /** Plugin 描述 */
847
- description?: string;
848
- /** 订阅的 Hook 点及优先级 */
849
- hooks: Array<{
850
- point: HookPoint;
851
- /** 优先级,默认 0,数字越大越先执行 */
852
- priority?: number;
853
- }>;
854
- /** 执行函数 */
855
- execute: (ctx: HookContext2) => HookResult2 | Promise<HookResult2>;
856
- }
857
- /**
858
- * Agent 类型
859
- */
860
- type AgentType = "primary" | "sub" | "summary";
861
- /**
862
- * Agent 状态
863
- */
864
- type AgentStatus = "idle" | "running" | "paused" | "stopped" | "error";
865
- /**
866
- * Agent 实例配置
867
- */
868
- interface AgentInstanceConfig {
869
- /** Agent 类型 */
870
- type: AgentType;
871
- /** Agent 名称 */
872
- name?: string;
873
- /** System Prompt */
874
- systemPrompt?: string;
875
- /** Behavior Spec ID */
876
- behaviorSpecId?: string;
877
- /** 使用的模型 */
878
- model?: string;
879
- /** 最大迭代次数 */
880
- maxIterations?: number;
881
- /** 最大错误重试次数 */
882
- maxErrorRetries?: number;
883
- /** 死循环检测阈值 */
884
- doomLoopThreshold?: number;
885
- /** 允许的工具列表 */
886
- allowedTools?: string[];
887
- /** 拒绝的工具列表 */
888
- deniedTools?: string[];
889
- /** 工具超时(毫秒) */
890
- toolTimeout?: number;
891
- /** 工具重试次数 */
892
- toolRetries?: number;
893
- /** 是否过滤 history 中的 tool 消息(默认过滤,避免上下文过长) */
894
- filterHistory?: boolean;
895
- }
896
- /**
897
- * Agent 实例
898
- */
899
- interface AgentInstance {
900
- /** Agent 名称 */
901
- name: string;
902
- /** Agent 配置 */
903
- config: Required<AgentInstanceConfig>;
904
- /** 当前状态 */
905
- status: AgentStatus;
906
- /** 注册的 Plugins */
907
- plugins: Map<string, Plugin>;
908
- }
909
- /**
910
- * Agent 执行结果
911
- */
912
- interface AgentRunResult {
913
- /** 最终文本响应 */
914
- finalText?: string;
915
- /** 是否被 hook 停止 */
916
- stopped?: boolean;
917
- /** 停止原因 */
918
- stopReason?: string;
919
- /** 执行的迭代次数 */
920
- iterations: number;
921
- /** 工具调用列表 */
922
- toolCalls: ToolCall2[];
923
- /** 错误信息(如有) */
924
- error?: string;
925
- }
926
- /**
927
- * Agent 内部使用的 LLM 输出
928
- * 独立定义以避免与 LLMOutput 的类型冲突
929
- */
930
- interface AgentLLMOutput {
931
- /** 生成的内容 */
932
- content: string;
933
- /** 完成原因 */
934
- finishReason: "stop" | "length" | "content-filter" | "tool-calls" | "function-call";
935
- /** 工具调用(兼容格式) */
936
- toolCalls?: Array<{
937
- id: string;
938
- function?: {
939
- name: string;
940
- arguments: string;
941
- };
942
- name?: string;
943
- arguments?: string;
944
- }>;
945
- /** Usage 信息 */
946
- usage?: {
947
- promptTokens: number;
948
- completionTokens: number;
949
- totalTokens: number;
950
- };
951
- /** 生成模型 */
952
- model?: string;
953
- /** 推理内容(思考过程) */
954
- reasoning?: string;
955
- }
956
- import { ZodType as ZodType3, ZodTypeDef } from "zod";
957
- /**
958
- * ConfigSource 类型
959
- */
960
- type ConfigSourceType = "memory" | "file" | "env" | "remote";
961
- /**
962
- * SourceRegistration - Source 注册配置
963
- */
964
- interface SourceRegistration {
965
- /** Source 类型 */
966
- type: ConfigSourceType;
967
- /** 相对路径(file 类型使用,基于 XDG_DATA_HOME) */
968
- relativePath?: string;
969
- /** 环境变量前缀(env 类型使用) */
970
- envPrefix?: string;
971
- /** 优先级,默认值:memory=0, file=10, env=20 */
972
- priority?: number;
973
- /** 是否可选(file 类型使用,文件不存在时不报错) */
974
- optional?: boolean;
975
- /** 是否启用文件监听(file 类型使用,默认 true) */
976
- watch?: boolean;
977
- }
978
- /**
979
- * KeyRegistration - 配置 Key 注册
980
- */
981
- interface KeyRegistration {
982
- /** 配置 Key(支持点号嵌套,如 "llm.provider") */
983
- key: string;
984
- /** 优先使用的 Source 列表(按优先级从低到高) */
985
- sources: ConfigSourceType[];
986
- /** 自定义优先级(覆盖默认优先级) */
987
- priority?: number;
988
- /** 默认值 */
989
- default?: unknown;
990
- }
991
- /**
992
- * ComponentRegistration - Component 注册配置
993
- */
994
- interface ComponentRegistration {
995
- /** Component 名称 */
996
- name: string;
997
- /** 声明需要的 Source */
998
- sources: SourceRegistration[];
999
- /** 声明需要的配置 Key */
1000
- keys: KeyRegistration[];
1001
- /** 配置 Schema(用于验证,可选) */
1002
- schema?: ZodType3<unknown, ZodTypeDef, unknown>;
1003
- /** 默认值 */
1004
- defaults?: Record<string, unknown>;
1005
- }
1006
- /**
1007
- * ConfigSource 接口
1008
- */
1009
- interface ConfigSource {
1010
- readonly name: ConfigSourceType;
1011
- readonly priority: number;
1012
- read(key: string): unknown | undefined;
1013
- write?(key: string, value: unknown): boolean;
1014
- delete?(key: string): boolean;
1015
- list(): Array<{
1016
- key: string;
1017
- value: unknown;
1018
- }>;
1019
- watch?(callback: (event: ConfigChangeEvent) => void): () => void;
1020
- }
1021
- /**
1022
- * Config 变更事件
1023
- */
1024
- interface ConfigChangeEvent {
1025
- type: "change" | "add" | "delete";
1026
- key: string;
1027
- oldValue: unknown;
1028
- newValue: unknown;
1029
- source: ConfigSourceType;
1030
- timestamp: number;
1031
- }
1032
- /**
1033
- * ConfigComponent 配置
1034
- */
1035
- interface ConfigComponentOptions {
1036
- /** 持久化文件路径 */
1037
- persistFile?: string;
1038
- /** 防抖延迟(ms) */
1039
- debounceMs?: number;
1040
- /** 最大批量大小 */
1041
- maxBatchSize?: number;
1042
- }
1043
- /**
1044
- * ConfigComponent
1045
- */
1046
- declare class ConfigComponent extends BaseComponent {
1047
- readonly name = "config";
1048
- readonly version = "1.0.0";
1049
- private memorySource;
1050
- private sources;
1051
- private componentSchemas;
1052
- private componentPaths;
1053
- private watchers;
1054
- /** Source watch unsubscribe 函数 */
1055
- private sourceUnwatchFns;
1056
- private persistQueue;
1057
- private persistFile?;
1058
- /** Key 注册表:key -> KeyRegistration */
1059
- private keyRegistry;
1060
- /** Component 注册表:name -> ComponentRegistration */
1061
- private componentRegistrations;
1062
- /** 协议解析器 */
1063
- private protocolResolver?;
1064
- /** XDG_DATA_HOME 缓存(用于测试注入) */
1065
- private __xdgDataHome?;
1066
- /** Source 注册配置(延迟创建) */
1067
- private pendingSources;
1068
- /** Source 唯一标识缓存:type:relativePath -> true */
1069
- private sourceKeys;
1070
- /**
1071
- * 获取 XDG_DATA_HOME(动态获取,支持运行时覆盖)
1072
- */
1073
- getXdgDataHome(): string;
1074
- /**
1075
- * 设置 XDG_DATA_HOME(用于测试)
1076
- */
1077
- setXdgDataHome(path: string): void;
1078
- constructor(options?: ConfigComponentOptions);
1079
- /**
1080
- * 注册 Component Schema
1081
- */
1082
- register(schema: ComponentSchema): void;
1083
- /**
1084
- * 注销 Component
1085
- */
1086
- unregister(name: string): boolean;
1087
- /**
1088
- * 检查是否已注册
1089
- */
1090
- isRegistered(name: string): boolean;
1091
- /**
1092
- * 列出所有注册的 Component
1093
- */
1094
- listComponents(): string[];
1095
- /**
1096
- * 获取配置值(支持 xxx.yyy.zzz 嵌套访问)
1097
- * 支持两种模式:
1098
- * 1. 完整 key:直接查找,如 "component.key" -> 读取 "component.key"
1099
- * 2. 嵌套对象:查找 component 对象后从中获取属性,如 "component.nested.deep"
1100
- * -> 先找 "component.nested",再获取其 "deep" 属性
1101
- */
1102
- get(key: string): unknown;
1103
- /**
1104
- * 批量获取
1105
- */
1106
- getMany(keys: string[]): Record<string, unknown>;
1107
- /**
1108
- * 设置配置值(内存级别 + 异步持久化)
1109
- */
1110
- set(key: string, value: unknown): Promise<void>;
1111
- /**
1112
- * 批量设置
1113
- */
1114
- setMany(config: Record<string, unknown>): Promise<void>;
1115
- /**
1116
- * 获取 Component 的路径
1117
- */
1118
- getPath(component: string, subPath?: string): string;
1119
- /**
1120
- * 手动持久化
1121
- */
1122
- save(key?: string): Promise<void>;
1123
- /**
1124
- * 重置到默认值
1125
- */
1126
- reset(key: string): Promise<void>;
1127
- /**
1128
- * 监听配置变更
1129
- */
1130
- watch(pattern: string, callback: ComponentWatchCallback): () => void;
1131
- /**
1132
- * 添加配置源
1133
- */
1134
- addSource(source: ConfigSource): void;
1135
- /**
1136
- * 获取 Source 唯一标识
1137
- */
1138
- private getSourceKey;
1139
- /**
1140
- * 移除配置源
1141
- */
1142
- removeSource(name: ConfigSourceType): boolean;
1143
- /**
1144
- * 关闭所有配置源的 watcher(用于优雅退出)
1145
- */
1146
- unwatchAll(): void;
1147
- /**
1148
- * 获取配置源列表
1149
- */
1150
- getSources(): ConfigSource[];
1151
- private getNestedValue;
1152
- private notifyWatchers;
1153
- private persistToFile;
1154
- /**
1155
- * 注册配置 Source
1156
- *
1157
- * Source 会在首次 load() 时被创建
1158
- *
1159
- * @example
1160
- * config.registerSource({ type: "file", relativePath: "llm-config.jsonc" });
1161
- * config.registerSource({ type: "env", envPrefix: "LLM" });
1162
- */
1163
- registerSource(registration: SourceRegistration): void;
1164
- /**
1165
- * 创建 Source 实例
1166
- */
1167
- private createSource;
1168
- /**
1169
- * 确保所有待创建的 Source 已被创建
1170
- */
1171
- private ensureSourcesCreated;
1172
- /**
1173
- * 注册配置 Key
1174
- *
1175
- * @example
1176
- * config.registerKeys([
1177
- * { key: "llm.provider", sources: ["file", "memory"], default: "openai" },
1178
- * { key: "llm.model", sources: ["file"] },
1179
- * ]);
1180
- */
1181
- registerKeys(keys: KeyRegistration[]): void;
1182
- /**
1183
- * 注册 Component(一次性注册 Source 和 Keys)
1184
- *
1185
- * @example
1186
- * config.registerComponent({
1187
- * name: "llm",
1188
- * sources: [
1189
- * { type: "file", relativePath: "llm-config.jsonc" },
1190
- * { type: "env", envPrefix: "LLM" },
1191
- * ],
1192
- * keys: [
1193
- * { key: "llm.provider", sources: ["file", "env", "memory"] },
1194
- * ],
1195
- * });
1196
- */
1197
- registerComponent(registration: ComponentRegistration): void;
1198
- /**
1199
- * 按需加载配置
1200
- *
1201
- * 从注册的 Source 加载已注册 Keys 的配置值到内存中
1202
- *
1203
- * @param componentName - 可选,指定要加载的 Component 名称
1204
- *
1205
- * @example
1206
- * // 加载所有已注册的 Keys
1207
- * await config.load();
1208
- *
1209
- * // 只加载指定 Component 的 Keys
1210
- * await config.load("llm");
1211
- */
1212
- load(componentName?: string): Promise<void>;
1213
- /**
1214
- * 获取已注册的 Source 列表
1215
- */
1216
- getRegisteredSources(): ConfigSource[];
1217
- }
1218
- /**
1219
- * 服务配置接口
1220
- */
1221
- interface ServiceConfig {
1222
- /** 环境配置 */
1223
- environment?: {
1224
- name?: string;
1225
- version?: string;
1226
- };
1227
- /** 组件配置映射 */
1228
- components?: Record<string, ComponentConfigEntry>;
1229
- }
1230
- /**
1231
- * 组件配置项
1232
- */
1233
- interface ComponentConfigEntry {
1234
- /** 配置文件路径(可选) */
1235
- configPath?: string;
1236
- /** 环境变量前缀(可选) */
1237
- envPrefix?: string;
1238
- /** 直接配置对象(可选) */
1239
- config?: Record<string, unknown>;
1240
- /** 是否启用(可选,默认 true) */
1241
- enabled?: boolean;
1242
- }
1243
- /**
1244
- * 组件初始化选项(由 generateComponentOptions 生成)
1245
- */
1246
- interface GeneratedComponentOptions {
1247
- /** ConfigComponent 实例 */
1248
- configComponent: ConfigComponent;
1249
- /** 配置文件路径 */
1250
- configPath?: string;
1251
- /** 环境变量前缀 */
1252
- envPrefix?: string;
1253
- /** 直接配置对象 */
1254
- config?: Record<string, unknown>;
1255
- /** 允许的其他属性 */
1256
- [key: string]: unknown;
1257
- }
1258
- /**
1259
- * Environment 接口
1260
- *
1261
- * 定义 Agent 运行时的核心能力接口。
1262
- */
1263
- interface Environment {
1264
- /** 环境名称 */
1265
- readonly name: string;
1266
- /** 环境版本 */
1267
- readonly version: string;
1268
- /** 获取环境配置 */
1269
- getConfig(): EnvironmentConfig;
1270
- /**
1271
- * 注册组件
1272
- */
1273
- registerComponent(component: Component): void;
1274
- /**
1275
- * 注销组件
1276
- */
1277
- unregisterComponent(name: string): void;
1278
- /**
1279
- * 获取组件
1280
- */
1281
- getComponent<T extends Component>(name: string): T | undefined;
1282
- /**
1283
- * 列出所有组件
1284
- */
1285
- listComponents(): Component[];
1286
- /**
1287
- * 处理自然语言查询(核心入口)
1288
- *
1289
- * @param query 用户查询文本
1290
- * @param context 执行上下文(可选)
1291
- * @returns 完整的文本响应
1292
- */
1293
- handle_query(query: string, context?: AgentContext): Promise<string>;
1294
- /**
1295
- * 处理动作
1296
- */
1297
- handle_action(action: Action, context: Context): Promise<ToolResult>;
1298
- /**
1299
- * 订阅 EnvEvent(返回取消订阅函数)
1300
- */
1301
- subscribe(handler: EnvEventHandler): () => void;
1302
- /**
1303
- * 订阅指定类型事件
1304
- */
1305
- subscribeTo(eventType: string | string[], handler: EnvEventHandler): () => void;
1306
- /**
1307
- * 订阅所有事件(通配符)
1308
- */
1309
- subscribeAll(handler: EnvEventHandler): () => void;
1310
- /**
1311
- * 推送事件
1312
- *
1313
- * @param event EnvEvent 或 EnvEventInput(部分属性)
1314
- */
1315
- pushEnvEvent(event: EnvEvent | EnvEventInput): void;
1316
- /**
1317
- * 从配置文件加载服务配置
1318
- *
1319
- * @param configPath 配置文件路径(基于 XDG_DATA_HOME)
1320
- * @returns 解析后的服务配置对象
1321
- */
1322
- loadServiceConfig(configPath: string): Promise<ServiceConfig>;
1323
- /**
1324
- * 生成组件初始化选项
1325
- *
1326
- * 根据组件配置项生成可用于初始化的选项对象。
1327
- *
1328
- * @param componentName 组件名称
1329
- * @param configEntry 组件配置项
1330
- * @returns 生成的组件初始化选项
1331
- */
1332
- generateComponentOptions(componentName: string, configEntry: ComponentConfigEntry): GeneratedComponentOptions;
1333
- /**
1334
- * 注册组件并用配置初始化
1335
- *
1336
- * 便捷方法:注册组件并通过 ConfigComponent 初始化。
1337
- *
1338
- * @param component 要注册的组件实例
1339
- * @param configEntry 组件配置项
1340
- */
1341
- registerComponentWithConfig(component: Component, configEntry: ComponentConfigEntry): Promise<void>;
1342
- /**
1343
- * 从配置文件初始化整个环境
1344
- *
1345
- * 加载服务配置,注册并初始化所有配置的组件。
1346
- *
1347
- * @param configPath 配置文件路径(基于 XDG_DATA_HOME)
1348
- */
1349
- initFromConfig(configPath: string): Promise<void>;
1350
- }
1351
- /**
1352
- * Component 状态
1353
- */
1354
- type ComponentStatus = "created" | "initializing" | "running" | "stopping" | "stopped" | "error";
1355
- /**
1356
- * Component 配置
1357
- */
1358
- interface ComponentConfig {
1359
- name: string;
1360
- version: string;
1361
- enabled: boolean;
1362
- options?: Record<string, unknown>;
1363
- /**
1364
- * Environment 实例(可选)
1365
- *
1366
- * 推荐在 init 时接收 env 实例,以便:
1367
- * - 通过 env.getComponent() 获取其他组件
1368
- * - 通过 env.pushEnvEvent() 发布事件
1369
- * - 通过 env.handle_query() 处理查询
1370
- *
1371
- * 如果未提供,部分 Component 仍可正常工作(如测试场景)
1372
- *
1373
- * @example
1374
- * ```typescript
1375
- * async onInit(): Promise<void> {
1376
- * if (this.env) {
1377
- * const configComp = this.env.getComponent("config");
1378
- * }
1379
- * }
1380
- * ```
1381
- */
1382
- env?: Environment;
1383
- }
1384
- /**
1385
- * Component 路径声明
1386
- */
1387
- interface ComponentPaths {
1388
- base: "config" | "state" | "data" | "cache";
1389
- subPath: string;
1390
- isDirectory: boolean;
1391
- }
1392
- /**
1393
- * Component Schema(注册配置)
1394
- */
1395
- interface ComponentSchema {
1396
- /** Component 名称 */
1397
- name: string;
1398
- /** 配置 Schema(Zod) */
1399
- schema: ZodType4<unknown, ZodTypeDef2, unknown>;
1400
- /** 默认值(从 Schema 提取) */
1401
- defaults: Record<string, unknown>;
1402
- /** 路径声明 */
1403
- paths?: Record<string, ComponentPaths>;
1404
- /** 元信息 */
1405
- metadata?: {
1406
- description?: string;
1407
- version?: string;
1408
- author?: string;
1409
- };
1410
- }
1411
- /**
1412
- * Component 配置变更事件
1413
- */
1414
- interface ComponentConfigChangeEvent {
1415
- /** Component 名称 */
1416
- component: string;
1417
- /** 变更的配置键 */
1418
- key: string;
1419
- /** 旧值 */
1420
- oldValue: unknown;
1421
- /** 新值 */
1422
- newValue: unknown;
1423
- /** 是否持久化 */
1424
- persisted: boolean;
1425
- /** 时间戳 */
1426
- timestamp: number;
1427
- }
1428
- /**
1429
- * Component 配置监听回调
1430
- */
1431
- type ComponentWatchCallback = (event: ComponentConfigChangeEvent) => void | Promise<void>;
1432
- /**
1433
- * Component 基类接口
1434
- */
1435
- interface Component {
1436
- /** Component 名称 */
1437
- readonly name: string;
1438
- /** Component 版本 */
1439
- readonly version: string;
1440
- /** 获取状态 */
1441
- getStatus(): ComponentStatus;
1442
- /** 获取配置 */
1443
- getConfig(): ComponentConfig;
1444
- /** 初始化 */
1445
- init(config: ComponentConfig): Promise<void>;
1446
- /** 启动 */
1447
- start(): Promise<void>;
1448
- /** 停止 */
1449
- stop(): Promise<void>;
1450
- }
1451
- /**
1452
- * Component 基类实现
1453
- */
1454
- declare abstract class BaseComponent implements Component {
1455
- abstract readonly name: string;
1456
- abstract readonly version: string;
1457
- protected _status: ComponentStatus;
1458
- protected _enabled: boolean;
1459
- protected env: Environment;
1460
- /** Hook 管理器 - 子类初始化时由构造函数设置 */
1461
- readonly hookManager: HookManager;
1462
- constructor();
1463
- getStatus(): ComponentStatus;
1464
- getConfig(): ComponentConfig;
1465
- /**
1466
- * 检查 Environment 是否已初始化
1467
- */
1468
- protected isEnvInitialized(): boolean;
1469
- protected setStatus(status: ComponentStatus): void;
1470
- /**
1471
- * 获取 Environment 实例
1472
- */
1473
- protected getEnv(): Environment;
1474
- init(config?: ComponentConfig): Promise<void>;
1475
- start(): Promise<void>;
1476
- stop(): Promise<void>;
1477
- /**
1478
- * 子类可覆盖的钩子方法
1479
- */
1480
- protected onInit(): Promise<void>;
1481
- protected onStart(): Promise<void>;
1482
- protected onStop(): Promise<void>;
1483
- /**
1484
- * 注册 Hook
1485
- *
1486
- * @param hookPoint Hook 点名称
1487
- * @param hook Hook 实例
1488
- */
1489
- protected registerHook<T = unknown>(hookPoint: string, hook: Hook<T>): void;
1490
- /**
1491
- * 便捷方法:快速注册 Hook
1492
- */
1493
- protected addHook<T = unknown>(hookPoint: string, name: string, fn: HookFn<T>, priority?: number): void;
1494
- /**
1495
- * 便捷方法:按名称取消注册 Hook
1496
- */
1497
- protected removeHook(hookPoint: string, name: string): boolean;
1498
- /**
1499
- * 执行 Hook
1500
- */
1501
- protected executeHooks<T = unknown>(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
1502
- /**
1503
- * 获取 ConfigComponent 实例
1504
- *
1505
- * 子类可使用此方法获取配置组件,支持热更新
1506
- */
1507
- protected getConfigComponent(): {
1508
- get(key: string): unknown;
1509
- } | undefined;
1510
- /**
1511
- * 动态获取运行时配置值
1512
- *
1513
- * 优先从 ConfigComponent 获取(支持热更新),否则返回默认值
1514
- * 适用于运行时可能变化的配置(如 API Keys、模型参数)
1515
- *
1516
- * @param key 配置键(支持点号分隔的路径,如 "llm.temperature")
1517
- * @param defaultValue 默认值
1518
- * @returns 配置值或默认值
1519
- *
1520
- * @example
1521
- * ```typescript
1522
- * // 获取 LLM 温度配置(可能支持热更新)
1523
- * const temperature = this.getRuntimeConfig("llm.temperature", 0.7);
1524
- *
1525
- * // 获取 API Key
1526
- * const apiKey = this.getRuntimeConfig<string>("llm.providers.openai.apiKey");
1527
- * ```
1528
- */
1529
- protected getRuntimeConfig<T>(key: string, defaultValue: T): T;
1530
- }
1531
- import { ModelMessage as ModelMessage2 } from "ai";
1532
- import { z as z2 } from "zod";
1533
- /**
1534
- * AgentComponent 配置 Schema
1535
- */
1536
- declare const AgentComponentConfigSchema: unknown;
1537
- type AgentComponentConfig = z2.infer<typeof AgentComponentConfigSchema>;
1538
- /**
1539
- * AgentComponent
1540
- *
1541
- * 提供 Agent 执行能力和 Hook 扩展机制
1542
- */
1543
- declare class AgentComponent extends BaseComponent {
1544
- readonly name = "agent";
1545
- readonly version = "1.0.0";
1546
- private agents;
1547
- private config;
1548
- private llmComponent;
1549
- private toolComponent;
1550
- private aborted;
1551
- private abortControllers;
1552
- private defaultTools;
1553
- private doomLoopCaches;
1554
- /** ConfigComponent 用于配置管理 */
1555
- private configComponent?;
1556
- /** 配置变更 watcher 清理函数 */
1557
- private configWatcher?;
1558
- /** Session 消息转换器 */
1559
- private messageConverter;
1560
- /** 构造函数传入的配置(最高优先级) */
1561
- private _constructorConfig?;
1562
- /** Run ID 计数器(用于生成唯一的 runId) */
1563
- private runCounter;
1564
- constructor(options?: {
1565
- config?: Partial<AgentComponentConfig>;
1566
- });
1567
- /**
1568
- * 初始化组件
1569
- *
1570
- * 配置加载优先级(从高到低):
1571
- * 1. Object - 直接传入的 config 对象
1572
- * 2. Env - 环境变量(通过 envPrefix 配置前缀)
1573
- * 3. File - 配置文件(通过 configPath 指定)
1574
- */
1575
- init(config: ComponentConfig): Promise<void>;
1576
- /**
1577
- * 注册配置到 ConfigComponent
1578
- *
1579
- * 配置加载顺序(优先级从低到高):
1580
- * 1. Defaults - 默认值(最低)
1581
- * 2. FileSource - 从配置文件加载
1582
- * 3. EnvSource - 从环境变量加载
1583
- * 4. config 对象 - 直接传入的配置(最高)
1584
- */
1585
- private registerConfig;
1586
- /**
1587
- * 将配置对象展平为点号路径
1588
- */
1589
- private flattenConfig;
1590
- /**
1591
- * 注册配置热更新监听
1592
- */
1593
- private registerConfigWatcher;
1594
- /**
1595
- * 处理配置变更
1596
- */
1597
- protected onConfigChange(event: {
1598
- key: string;
1599
- oldValue?: unknown;
1600
- newValue?: unknown;
1601
- }): void;
1602
- /**
1603
- * 组件停止时的清理逻辑
1604
- *
1605
- * 清理内容:
1606
- * 1. 终止所有正在运行的 Agent(调用 abort() 以中断 LLM 调用)
1607
- * 2. 清空 AbortController 注册表
1608
- * 3. 清空 Doom Loop 缓存
1609
- * 4. 取消配置变更监听
1610
- * 5. 清空 Agent 注册表
1611
- */
1612
- protected onStop(): Promise<void>;
1613
- /**
1614
- * 刷新依赖组件引用
1615
- *
1616
- * 当组件被添加到 Environment 后调用,以确保获取到最新的组件引用
1617
- */
1618
- refreshDependencies(): void;
1619
- /**
1620
- * 获取 Agent 实例
1621
- */
1622
- getAgent(agentName: string): AgentInstance | undefined;
1623
- /**
1624
- * 获取所有 Agent 实例
1625
- */
1626
- listAgents(): AgentInstance[];
1627
- /**
1628
- * Add placeholder tool results for remaining unprocessed tool calls
1629
- *
1630
- * This ensures that the message history maintains a 1:1 correspondence
1631
- * between tool-calls and tool-results, which is required by AI SDK v6.
1632
- *
1633
- * @param ctx - Hook context containing messages
1634
- * @param allToolCalls - All tool calls from the LLM response
1635
- * @param processedCount - Number of tool calls that have been processed
1636
- * @param reason - Reason for the abort/interruption
1637
- */
1638
- private addRemainingToolResults;
1639
- /**
1640
- * 注册 Agent
1641
- *
1642
- * 配置优先级(从高到低):
1643
- * 1. registerAgent 传入的 config(来自 PromptComponent)
1644
- * 2. defaultAgent 配置(来自 ConfigComponent,作为 fallback)
1645
- */
1646
- registerAgent(name: string, config: Partial<AgentInstanceConfig>): AgentInstance;
1647
- /**
1648
- * 注销 Agent
1649
- */
1650
- unregisterAgent(name: string): boolean;
1651
- /**
1652
- * 注册 Plugin 到指定 Agent
1653
- */
1654
- registerPlugin(agentName: string, plugin: Plugin): void;
1655
- /**
1656
- * 注销 Plugin
1657
- */
1658
- unregisterPlugin(agentName: string, pluginName: string): boolean;
1659
- /**
1660
- * 设置默认工具列表
1661
- */
1662
- setDefaultTools(tools: Tool[]): void;
1663
- /**
1664
- * 获取可用工具
1665
- */
1666
- getAvailableTools(agent: AgentInstance, context?: AgentContext): Tool[];
1667
- /**
1668
- * 执行 Agent
1669
- *
1670
- * @param agentName Agent 名称
1671
- * @param query 用户查询
1672
- * @param context 执行上下文
1673
- * @returns Agent 执行结果
1674
- */
1675
- run(agentName: string, query: string, context?: AgentContext): Promise<AgentRunResult>;
1676
- private pushMessage;
1677
- /**
1678
- * 执行 Agent(核心方法,内部实现)
1679
- */
1680
- private _run;
1681
- /**
1682
- * 中止 Agent
1683
- *
1684
- * 调用此方法会:
1685
- * 1. 设置 aborted 标志(用于 ReAct 循环检查)
1686
- * 2. 触发 AbortController.abort()(用于中断正在进行的 LLM 调用)
1687
- *
1688
- * 注意:不直接修改 agent.status,由 _run() 的 finally 统一处理
1689
- */
1690
- abort(agentName: string): void;
1691
- /**
1692
- * 构建消息列表
1693
- *
1694
- * 注意:ctx.messages 已经包含了历史消息(包括 system prompt 和 user query)
1695
- * 这里只需要构建当前请求的消息,不重复添加
1696
- */
1697
- private buildMessages;
1698
- /**
1699
- * 调用 LLM
1700
- *
1701
- * 流式事件由 LLMComponent 通过 env.pushEnvEvent() 发布
1702
- */
1703
- private invokeLLM;
1704
- /**
1705
- * 执行工具
1706
- */
1707
- private executeTool;
1708
- /**
1709
- * 执行 Plugin Hooks (Agent 特有的 Hook 执行机制)
1710
- */
1711
- private executePluginHooks;
1712
- /**
1713
- * 处理 Hook 动作
1714
- */
1715
- private handleHookAction;
1716
- /**
1717
- * 通知消息添加(扩展点)
1718
- */
1719
- protected notifyMessageAdded(message: ModelMessage2): void;
1720
- /**
1721
- * 结束结果
1722
- */
1723
- private finalizeResult;
1724
- /**
1725
- * Record messages to session
1726
- *
1727
- * 存储策略:
1728
- * 1. 存储 allMessages 中的所有 user、assistant、tool 消息
1729
- * 2. 跳过 system 消息
1730
- * 3. 跳过空内容的 assistant/tool 消息
1731
- */
1732
- private recordSessionMessages;
1733
- /**
1734
- * 检查消息内容是否为空
1735
- *
1736
- * 空内容定义:
1737
- * - 空字符串 ""
1738
- * - 空数组 []
1739
- * - null
1740
- * - undefined
1741
- *
1742
- * @param content - 消息内容
1743
- * @returns 是否为空
1744
- */
1745
- private isEmptyMessage;
1746
- /**
1747
- * 通过 Environment 发送事件
1748
- */
1749
- private pushEnvEvent;
1750
- /**
1751
- * 获取会话对话历史
1752
- *
1753
- * 核心逻辑:
1754
- * 1. 使用 sessionComponent.getContext() 获取消息(自动从 checkpoint 开始)
1755
- * 2. 根据 messageLimit 限制消息数量(向后兼容 minUserMessages)
1756
- * 3. 过滤掉 tool_call/tool_result 消息(如果 filterHistory=true)
1757
- *
1758
- * 关键改进:利用 checkpoint 概念,避免重复获取已压缩的历史消息,
1759
- * 从而减少重复触发压缩的问题。
1760
- *
1761
- * @param sessionComponent - SessionComponent 实例
1762
- * @param sessionId - 会话 ID
1763
- * @param options - 配置选项
1764
- * @param options.minUserMessages - 最少包含的 user 消息数量(默认 100)
1765
- * @param options.filterHistory - 是否过滤 tool 消息(默认 true)
1766
- * @returns 过滤后的会话消息列表
1767
- */
1768
- private getConversationHistory;
1769
- }
1770
- /**
1771
- * @fileoverview 统一错误类型体系
1772
- *
1773
- * 提供 AgentError, ToolError, LLMError, ComponentError, ContextError 等错误类型
1774
- * 每个错误包含 code(错误码)和 details(额外信息)
1775
- */
1776
- /**
1777
- * 错误码枚举
1778
- */
1779
- declare const ErrorCodes: {
1780
- readonly AGENT_NOT_FOUND: "AGENT_001";
1781
- readonly AGENT_RUN_FAILED: "AGENT_002";
1782
- readonly AGENT_TIMEOUT: "AGENT_003";
1783
- readonly AGENT_CONFIG_INVALID: "AGENT_004";
1784
- readonly TOOL_NOT_FOUND: "TOOL_001";
1785
- readonly TOOL_DISABLED: "TOOL_002";
1786
- readonly TOOL_EXECUTION_FAILED: "TOOL_003";
1787
- readonly TOOL_VALIDATION_FAILED: "TOOL_004";
1788
- readonly LLM_PROVIDER_NOT_FOUND: "LLM_001";
1789
- readonly LLM_INVOCATION_FAILED: "LLM_002";
1790
- readonly LLM_TIMEOUT: "LLM_003";
1791
- readonly LLM_CONFIG_INVALID: "LLM_004";
1792
- readonly COMPONENT_NOT_FOUND: "COMP_001";
1793
- readonly COMPONENT_INIT_FAILED: "COMP_002";
1794
- readonly COMPONENT_NOT_RUNNING: "COMP_003";
1795
- readonly CONFIG_NOT_FOUND: "CONFIG_001";
1796
- readonly CONFIG_INVALID: "CONFIG_002";
1797
- readonly CONFIG_SOURCE_ERROR: "CONFIG_003";
1798
- readonly CONTEXT_THRESHOLD_EXCEEDED: "CTX_001";
1799
- readonly CONTEXT_COMPACTION_FAILED: "CTX_002";
1800
- readonly MCP_INIT_FAILED: "MCP_001";
1801
- readonly MCP_SERVER_ERROR: "MCP_002";
1802
- readonly MCP_TOOL_ERROR: "MCP_003";
1803
- };
1804
- type ErrorCode = typeof ErrorCodes[keyof typeof ErrorCodes];
1805
- /**
1806
- * RoyError 基类
1807
- */
1808
- declare class RoyError extends Error {
1809
- readonly code: ErrorCode;
1810
- readonly details?: unknown;
1811
- constructor(message: string, code: ErrorCode, details?: unknown);
1812
- }
1813
- /**
1814
- * LLM Output 结构(用于 ContextError 携带 assistant message)
1815
- *
1816
- * 这是简化版本,仅包含保存到 session 所需的最少字段。
1817
- * 与 llm/types.ts 中的 LLMOutput 不同,那个版本包含完整的 LLM 调用结果。
1818
- */
1819
- interface LLMOutput2 {
1820
- content?: string;
1821
- reasoning?: string;
1822
- toolCalls?: Array<{
1823
- id: string;
1824
- function?: {
1825
- name: string;
1826
- arguments: string;
1827
- };
1828
- name?: string;
1829
- arguments?: unknown;
1830
- }>;
1831
- usage?: {
1832
- promptTokens: number;
1833
- completionTokens: number;
1834
- totalTokens: number;
1835
- };
1836
- }
1837
- /**
1838
- * Context 相关错误
1839
- */
1840
- declare class ContextError extends RoyError {
1841
- readonly sessionId?: string | undefined;
1842
- readonly usage?: {
1843
- promptTokens: number;
1844
- completionTokens: number;
1845
- totalTokens: number;
1846
- } | undefined;
1847
- readonly contextWindow?: number | undefined;
1848
- readonly llmOutput?: LLMOutput2;
1849
- constructor(message: string, code?: ErrorCode, sessionId?: string | undefined, usage?: {
1850
- promptTokens: number;
1851
- completionTokens: number;
1852
- totalTokens: number;
1853
- } | undefined, contextWindow?: number | undefined, llmOutput?: LLMOutput2, details?: unknown);
1854
- }
1855
- /**
1856
- * LLMComponent
1857
- * 管理 LLM 调用,使用 AI SDK 进行实际调用
1858
- *
1859
- * 配置通过 ConfigComponent 实时获取,支持配置热更新
1860
- *
1861
- * @example
1862
- * // 通过 ConfigComponent 加载配置
1863
- * const configComponent = new ConfigComponent();
1864
- * configComponent.setXdgDataHome(__dirname);
1865
- *
1866
- * const llmComponent = new LLMComponent();
1867
- * await llmComponent.init({
1868
- * env, // Environment 实例(必填)
1869
- * options: {
1870
- * configComponent, // ConfigComponent 实例
1871
- * configPath: "config.jsonc" // 配置文件路径
1872
- * }
1873
- * });
1874
- *
1875
- * // 配置更新后,下次调用会自动获取最新配置
1876
- * configComponent.set("llm.temperature", 0.5);
1877
- */
1878
- declare class LLMComponent extends BaseComponent {
1879
- readonly name = "llm";
1880
- readonly version = "2.0.0";
1881
- private configComponent?;
1882
- /** 配置变更 watcher 清理函数 */
1883
- private configWatcher?;
1884
- constructor();
1885
- /**
1886
- * 初始化组件
1887
- *
1888
- * 配置加载优先级(从高到低):
1889
- * 1. Object - 直接传入的 config 对象
1890
- * 2. Env - 环境变量(通过 envPrefix 配置前缀)
1891
- * 3. File - 配置文件(通过 configPath 指定)
1892
- */
1893
- init(config: ComponentConfig): Promise<void>;
1894
- /**
1895
- * 注册配置到 ConfigComponent
1896
- *
1897
- * 配置加载顺序(优先级从低到高):
1898
- * 1. FileSource - 从配置文件加载(最低)
1899
- * 2. EnvSource - 从环境变量加载(中等)
1900
- * 3. MemorySource - 直接配置对象(最高)
1901
- */
1902
- private registerConfig;
1903
- /**
1904
- * 将配置对象展平为点号路径
1905
- *
1906
- * @example
1907
- * flattenConfig({ providers: { openai: { apiKey: "xxx" } } })
1908
- * -> { "llm.providers.openai.apiKey": "xxx" }
1909
- */
1910
- private flattenConfig;
1911
- /**
1912
- * 注册配置热更新监听
1913
- */
1914
- private registerConfigWatcher;
1915
- /**
1916
- * 处理配置变更(支持热更新)
1917
- */
1918
- protected onConfigChange(event: {
1919
- key: string;
1920
- oldValue?: unknown;
1921
- newValue?: unknown;
1922
- }): void;
1923
- /**
1924
- * 启动组件
1925
- */
1926
- onStart(): Promise<void>;
1927
- /**
1928
- * 停止组件
1929
- */
1930
- onStop(): Promise<void>;
1931
- /**
1932
- * 从配置源获取配置值
1933
- * 优先从 ConfigComponent 获取(支持热更新),否则返回默认值
1934
- */
1935
- private getLLMConfig;
1936
- /**
1937
- * 解析请求,构建调用上下文(invoke 和 invokeStream 共享)
1938
- */
1939
- private resolveRequest;
1940
- /**
1941
- * 调用 LLM
1942
- *
1943
- * 流式事件通过 env.pushEnvEvent() 发布
1944
- */
1945
- invoke(request: LLMInvokeRequest): Promise<LLMInvokeResult>;
1946
- /**
1947
- * 获取 Provider API Key
1948
- */
1949
- private getApiKey;
1950
- /**
1951
- * 获取 Provider
1952
- */
1953
- getProvider(id: string);
1954
- /**
1955
- * 列出所有 Provider
1956
- */
1957
- listProviders();
1958
- /**
1959
- * 获取模型的限制配置
1960
- *
1961
- * @param providerId - Provider ID
1962
- * @param modelId - 模型 ID(可选,默认使用 provider 的 defaultModel)
1963
- * @returns 模型限制配置,如果没有配置则返回 undefined
1964
- */
1965
- getModelLimits(providerId: string, modelId?: string): ModelLimits | undefined;
1966
- /**
1967
- * 获取上下文窗口阈值配置
1968
- *
1969
- * @param providerId - Provider ID
1970
- * @param modelId - 模型 ID
1971
- * @returns 包含 contextWindow 和 thresholdRatio 的对象
1972
- */
1973
- getContextThresholdConfig(providerId: string, modelId?: string): {
1974
- contextWindow: number;
1975
- thresholdRatio: number;
1976
- };
1977
- /**
1978
- * 检测上下文阈值是否超过
1979
- *
1980
- * @param usage - LLM 使用量信息
1981
- * @param providerId - Provider ID
1982
- * @param modelId - 模型 ID
1983
- * @param sessionId - Session ID(用于错误信息)
1984
- * @returns 如果超过阈值,返回 ContextError;否则返回 undefined
1985
- */
1986
- checkContextThreshold(usage: {
1987
- totalTokens: number;
1988
- promptTokens?: number;
1989
- completionTokens?: number;
1990
- } | undefined, providerId: string, modelId: string | undefined, sessionId?: string): ContextError | undefined;
1991
- }
1992
- import { z as z7 } from "zod";
1993
- import { z as z6 } from "zod";
1994
- declare const RunStatusSchema: unknown;
1995
- type RunStatus = z6.infer<typeof RunStatusSchema>;
1996
- interface RunOptions {
1997
- input?: Record<string, any>;
1998
- debug?: boolean;
1999
- parallelLimit?: number | null;
2000
- timeout?: number | null;
2001
- /** @deprecated No-op: workflow always waits for completion */
2002
- sync?: boolean;
2003
- }
2004
- interface RunResult {
2005
- runId: string;
2006
- /** Session ID (workflow_{runId} format) - useful for CLI resume */
2007
- sessionId?: string;
2008
- status: RunStatus;
2009
- output?: Record<string, any>;
2010
- error?: string;
2011
- durationMs?: number;
2012
- /** Pending node ID (when status is 'paused') */
2013
- pendingNodeId?: string;
2014
- /** Query from ask_user (when status is 'paused') */
2015
- query?: string;
2016
- /** Agent sub-session ID (for resuming agent nodes) */
2017
- agentSessionId?: string;
2018
- }
2019
- declare const NodeDefinitionSchema: unknown;
2020
- type NodeDefinition = z7.infer<typeof NodeDefinitionSchema>;
2021
- declare const WorkflowConfigSchema: unknown;
2022
- type WorkflowConfig = z7.infer<typeof WorkflowConfigSchema>;
2023
- declare const WorkflowMetadataSchema: unknown;
2024
- type WorkflowMetadata = z7.infer<typeof WorkflowMetadataSchema>;
2025
- declare const WorkflowDefinitionSchema: unknown;
2026
- type WorkflowDefinition = z7.infer<typeof WorkflowDefinitionSchema>;
2027
- interface Workflow {
2028
- id: string;
2029
- name: string;
2030
- version: string;
2031
- description?: string;
2032
- definition: WorkflowDefinition;
2033
- config: WorkflowConfig;
2034
- metadata: WorkflowMetadata;
2035
- tags: string[];
2036
- createdAt: Date;
2037
- updatedAt: Date;
2038
- }
2039
- /**
2040
- * TaskPlugin 配置
2041
- */
2042
- interface TaskPluginConfig {
2043
- /** 是否启用 */
2044
- enabled?: boolean;
2045
- /** 优先级 */
2046
- priority?: number;
2047
- }
2048
- /**
2049
- * TaskPlugin Hook 定义
2050
- */
2051
- interface TaskPluginHook {
2052
- /** Hook 点 */
2053
- point: string;
2054
- /** 优先级(数字越大越先执行)*/
2055
- priority?: number;
2056
- }
2057
- /**
2058
- * TaskPlugin 基类
2059
- */
2060
- declare abstract class TaskPlugin {
2061
- abstract readonly name: string;
2062
- abstract readonly hooks: TaskPluginHook[];
2063
- protected config: TaskPluginConfig;
2064
- constructor(config?: TaskPluginConfig);
2065
- /**
2066
- * 设置依赖组件(可选实现)
2067
- */
2068
- setComponents?(llmComponent: unknown, taskComponent: unknown): void;
2069
- /**
2070
- * 获取 Hook 点列表
2071
- */
2072
- getHookPoints(): string[];
2073
- /**
2074
- * 获取指定 Hook 点的优先级
2075
- */
2076
- getPriorityForHook(hookPoint: string): number;
2077
- /**
2078
- * 检查是否启用
2079
- */
2080
- isEnabled(): boolean;
2081
- /**
2082
- * 设置启用状态
2083
- */
2084
- setEnabled(enabled: boolean): void;
2085
- }
2086
- import { z as z9 } from "zod";
2087
- /**
2088
- * Task status enum
2089
- */
2090
- declare const TaskStatusEnum: unknown;
2091
- type TaskStatus = z9.infer<typeof TaskStatusEnum>;
2092
- /**
2093
- * Task priority enum
2094
- */
2095
- declare const TaskPriorityEnum: unknown;
2096
- type TaskPriority = z9.infer<typeof TaskPriorityEnum>;
2097
- /**
2098
- * Action type for task operations
2099
- */
2100
- declare const ActionTypeEnum: unknown;
2101
- type ActionType = z9.infer<typeof ActionTypeEnum>;
2102
- /**
2103
- * Task entity
2104
- */
2105
- interface Task {
2106
- id: number;
2107
- title: string;
2108
- description: string;
2109
- status: TaskStatus;
2110
- priority: TaskPriority;
2111
- progress: number;
2112
- current_status: string;
2113
- goals_and_expected_deliverables: string;
2114
- parent_task_id?: number;
2115
- createdAt: string;
2116
- updatedAt: string;
2117
- due_date?: string;
2118
- tags?: string[];
2119
- /** Project root path for task positioning */
2120
- project_path: string;
2121
- /** JSON string with time-space positioning info (worktree, branch, etc.) */
2122
- context: string;
2123
- }
2124
- /**
2125
- * TaskOperation entity
2126
- *
2127
- * 记录任务的操作历史,每个操作关联到一个 sessionId
2128
- */
2129
- interface TaskOperation {
2130
- id: number;
2131
- taskId: number;
2132
- sessionId: string;
2133
- actionType: ActionType;
2134
- actionTitle: string;
2135
- actionDescription: string;
2136
- timestamp: string;
2137
- mdPath?: string;
2138
- }
2139
- /**
2140
- * TaskTag entity
2141
- *
2142
- * Tag 池中的单个 Tag
2143
- */
2144
- interface TaskTag {
2145
- id: number;
2146
- name: string;
2147
- createdAt: string;
2148
- taskCount: number;
2149
- }
2150
- declare const TaskConfigSchema: unknown;
2151
- type TaskConfig = z9.infer<typeof TaskConfigSchema>;
2152
- interface CreateTaskOptions {
2153
- title: string;
2154
- description?: string;
2155
- priority?: TaskPriority;
2156
- parent_task_id?: number;
2157
- goals_and_expected_deliverables?: string;
2158
- due_date?: string;
2159
- tags?: string[];
2160
- sessionId: string;
2161
- /** Project root path for task positioning. Use 'unknown' if unknown. */
2162
- project_path: string;
2163
- /**
2164
- * JSON string with time-space positioning info.
2165
- * - For coding tasks: include worktree_path, branch, type
2166
- * - For other tasks: use '{}' or '{"type":"unknown"}'
2167
- * Must be a valid JSON string.
2168
- */
2169
- context: string;
2170
- }
2171
- interface UpdateTaskOptions {
2172
- title?: string;
2173
- description?: string;
2174
- status?: TaskStatus;
2175
- priority?: TaskPriority;
2176
- progress?: number;
2177
- current_status?: string;
2178
- goals_and_expected_deliverables?: string;
2179
- due_date?: string;
2180
- tags?: string[];
2181
- /** 项目路径,标识任务所属的项目 */
2182
- project_path?: string;
2183
- /** 任务上下文信息,用于存储额外上下文 */
2184
- context?: string;
2185
- }
2186
- interface ListTasksOptions {
2187
- status?: TaskStatus;
2188
- priority?: TaskPriority;
2189
- parent_task_id?: number;
2190
- limit?: number;
2191
- offset?: number;
2192
- }
2193
- interface CreateOperationOptions {
2194
- taskId: number;
2195
- sessionId: string;
2196
- actionType: ActionType;
2197
- actionTitle: string;
2198
- actionDescription?: string;
2199
- mdPath?: string;
2200
- }
2201
- interface ListOperationsOptions {
2202
- taskId: number;
2203
- actionType?: ActionType;
2204
- limit?: number;
2205
- offset?: number;
2206
- }
2207
- interface ListTagsOptions {
2208
- limit?: number;
2209
- offset?: number;
2210
- orderBy?: "count" | "name" | "created_at";
2211
- }
2212
- interface SearchTagsOptions {
2213
- query: string;
2214
- limit?: number;
2215
- }
2216
- interface TagService {
2217
- /**
2218
- * 创建 Tag(如果已存在则返回现有 Tag)
2219
- */
2220
- createTag(name: string): Promise<TaskTag>;
2221
- /**
2222
- * 获取 Tag by ID
2223
- */
2224
- getTag(id: number): Promise<TaskTag | undefined>;
2225
- /**
2226
- * 获取 Tag by Name
2227
- */
2228
- getTagByName(name: string): Promise<TaskTag | undefined>;
2229
- /**
2230
- * 列出所有 Tag
2231
- */
2232
- listTags(options?: ListTagsOptions): Promise<TaskTag[]>;
2233
- /**
2234
- * 搜索 Tag(模糊匹配)
2235
- */
2236
- searchTags(options: SearchTagsOptions): Promise<TaskTag[]>;
2237
- /**
2238
- * 删除 Tag(同时删除所有关联)
2239
- */
2240
- deleteTag(id: number): Promise<boolean>;
2241
- /**
2242
- * 给 Task 添加 Tags
2243
- */
2244
- addTagsToTask(taskId: number, tagIds: number[]): Promise<void>;
2245
- /**
2246
- * 从 Task 移除 Tags
2247
- */
2248
- removeTagsFromTask(taskId: number, tagIds: number[]): Promise<void>;
2249
- /**
2250
- * 获取 Task 的所有 Tags
2251
- */
2252
- getTagsForTask(taskId: number): Promise<TaskTag[]>;
2253
- /**
2254
- * 根据 Tag 名称获取关联的 Task IDs
2255
- */
2256
- getTaskIdsByTags(tagNames: string[]): Promise<number[]>;
2257
- /**
2258
- * 根据关键词列表搜索 Task(检索 tags、description、goals、operations)
2259
- * 这是新的核心检索方法
2260
- */
2261
- searchTasksByKeywords(keywords: string[], options?: {
2262
- limit?: number;
2263
- excludeTaskId?: number;
2264
- }): Promise<Task[]>;
2265
- /**
2266
- * 根据关键词查找类似 Task(排除自身,按相关性排序)
2267
- * 这是新的核心检索方法
2268
- */
2269
- findSimilarTasksByKeywords(keywords: string[], options?: {
2270
- limit?: number;
2271
- excludeTaskId?: number;
2272
- }): Promise<Task[]>;
2273
- /**
2274
- * 同步 Tags 到 Task(根据 tag 名称列表)
2275
- * - 查找或创建 Tag
2276
- * - 更新关联关系
2277
- * - 更新 task.tags JSON 字段
2278
- */
2279
- syncTagsToTask(taskId: number, tagNames: string[]): Promise<void>;
2280
- /**
2281
- * 查找类似 Task(基于 Tag 匹配度)- 原始实现,保留兼容
2282
- * @deprecated 使用 findSimilarTasksByKeywords 代替
2283
- */
2284
- findSimilarTasks(taskId: number, limit?: number): Promise<Task[]>;
2285
- /**
2286
- * 根据关键词搜索 Task(标题 + 描述)- 原始实现,保留兼容
2287
- * @deprecated 使用 searchTasksByKeywords 代替
2288
- */
2289
- searchTasks(query: string, limit?: number): Promise<Task[]>;
2290
- }
2291
- /**
2292
- * TaskComponent 配置选项(通过 options 传递)
2293
- *
2294
- * 配置加载顺序(优先级从低到高):
2295
- * 1. File - 配置文件(通过 configPath 指定)
2296
- * 2. Env - 环境变量(通过 envPrefix 配置前缀)
2297
- * 3. Object - 直接传入的 config 对象(最高优先级)
2298
- */
2299
- interface TaskComponentOptions {
2300
- /** ConfigComponent 实例(必填) */
2301
- configComponent: ConfigComponent;
2302
- /** 配置文件相对路径(可选,基于 XDG_DATA_HOME) */
2303
- configPath?: string;
2304
- /** 环境变量前缀(可选,默认 "TASK") */
2305
- envPrefix?: string;
2306
- /** 直接传入的配置对象(可选,优先级最高) */
2307
- config?: Partial<TaskConfig>;
2308
- }
2309
- declare class TaskComponent extends BaseComponent {
2310
- name: string;
2311
- version: string;
2312
- private store;
2313
- private config?;
2314
- private toolComponent?;
2315
- private backgroundTaskManager?;
2316
- private configComponent?;
2317
- private tagService;
2318
- private plugins;
2319
- /**
2320
- * 发布 EnvEvent
2321
- */
2322
- private publishEvent;
2323
- constructor();
2324
- /**
2325
- * Initialize the component
2326
- *
2327
- * 配置加载优先级(从高到低):
2328
- * 1. Object - 直接传入的 config 对象
2329
- * 2. Env - 环境变量(通过 envPrefix 配置前缀)
2330
- * 3. File - 配置文件(通过 configPath 指定)
2331
- */
2332
- init(config?: ComponentConfig): Promise<void>;
2333
- /**
2334
- * 注册配置到 ConfigComponent
2335
- *
2336
- * 配置加载顺序(优先级从低到高):
2337
- * 1. Defaults - 默认值(最低)
2338
- * 2. FileSource - 从配置文件加载
2339
- * 3. EnvSource - 从环境变量加载
2340
- * 4. config 对象 - 直接传入的配置(最高)
2341
- */
2342
- private registerConfig;
2343
- /**
2344
- * 根据配置初始化 store
2345
- */
2346
- private initStore;
2347
- /**
2348
- * 将配置对象展平为点号路径
2349
- */
2350
- private flattenConfig;
2351
- /**
2352
- * 注册配置热更新监听
2353
- */
2354
- private registerConfigWatcher;
2355
- /**
2356
- * 处理配置变更
2357
- */
2358
- protected onConfigChange(event: {
2359
- key: string;
2360
- oldValue?: unknown;
2361
- newValue?: unknown;
2362
- }): void;
2363
- /** 配置变更 watcher 清理函数 */
2364
- private configWatcher?;
2365
- /**
2366
- * Start the component - register tools
2367
- */
2368
- start(): Promise<void>;
2369
- /**
2370
- * Stop the component
2371
- */
2372
- stop(): Promise<void>;
2373
- /**
2374
- * Register task tools to ToolComponent
2375
- */
2376
- private registerTools;
2377
- /**
2378
- * 执行 Hook
2379
- */
2380
- private executeHook;
2381
- /**
2382
- * Create a new task
2383
- */
2384
- createTask(options: CreateTaskOptions): Promise<Task>;
2385
- /**
2386
- * Get task by ID
2387
- */
2388
- getTask(id: number): Promise<Task | undefined>;
2389
- /**
2390
- * Get task with operations
2391
- */
2392
- getTaskWithOperations(id: number): Promise<{
2393
- task: Task;
2394
- operations: TaskOperation[];
2395
- } | undefined>;
2396
- /**
2397
- * Update task
2398
- */
2399
- updateTask(id: number, options: UpdateTaskOptions): Promise<Task | undefined>;
2400
- /**
2401
- * Delete task
2402
- */
2403
- deleteTask(id: number): Promise<boolean>;
2404
- /**
2405
- * List tasks
2406
- */
2407
- listTasks(options?: ListTasksOptions): Promise<Task[]>;
2408
- /**
2409
- * Complete task
2410
- */
2411
- completeTask(id: number, sessionId: string): Promise<Task | undefined>;
2412
- /**
2413
- * Create operation record
2414
- */
2415
- createOperation(options: CreateOperationOptions): Promise<TaskOperation>;
2416
- /**
2417
- * Get operation by ID
2418
- */
2419
- getOperation(id: number): Promise<TaskOperation | undefined>;
2420
- /**
2421
- * Update operation
2422
- */
2423
- updateOperation(id: number, updates: {
2424
- actionTitle?: string;
2425
- actionDescription?: string;
2426
- }): Promise<TaskOperation | undefined>;
2427
- /**
2428
- * Delete operation
2429
- */
2430
- deleteOperation(id: number): Promise<boolean>;
2431
- /**
2432
- * List operations for a task
2433
- */
2434
- listOperations(options: ListOperationsOptions): Promise<TaskOperation[]>;
2435
- /**
2436
- * 获取 TagService
2437
- */
2438
- getTagService(): TagService;
2439
- /**
2440
- * 注册 Plugin
2441
- */
2442
- registerPlugin(plugin: TaskPlugin): void;
2443
- /**
2444
- * 注销 Plugin
2445
- */
2446
- unregisterPlugin(name: string): boolean;
2447
- /**
2448
- * 获取所有注册的 Plugin
2449
- */
2450
- listPlugins(): TaskPlugin[];
2451
- }
2452
- import { z as z10 } from "zod";
2453
- /**
2454
- * MCP Server 配置(联合类型)
2455
- */
2456
- declare const McpServerConfigSchema: unknown;
2457
- type McpServerConfig = z10.infer<typeof McpServerConfigSchema>;
2458
- /**
2459
- * 扫描发现的 MCP 服务器信息
2460
- */
2461
- interface DiscoveredMcpServer {
2462
- /** 服务器名称(目录名) */
2463
- name: string;
2464
- /** 入口脚本路径 */
2465
- entryPath?: string;
2466
- /** 配置文件路径 */
2467
- configPath?: string;
2468
- /** package.json 路径 */
2469
- packagePath?: string;
2470
- /** 目录中的配置 */
2471
- directoryConfig?: Record<string, unknown>;
2472
- /** 解析后的默认命令(兼容不同路径) */
2473
- defaultCommand?: string[];
2474
- }
2475
- /**
2476
- * MCP 服务器状态
2477
- */
2478
- interface McpServerStatus {
2479
- /** 服务器名称 */
2480
- name: string;
2481
- /** 连接状态 */
2482
- status: "connecting" | "connected" | "disconnected" | "error";
2483
- /** 错误信息 */
2484
- error?: string;
2485
- /** 工具数量 */
2486
- toolsCount?: number;
2487
- }
2488
- import { z as z11 } from "zod";
2489
- /**
2490
- * MCP 路径配置
2491
- */
2492
- interface McpPath {
2493
- /** 来源类型 */
2494
- type: "user" | "project" | "custom";
2495
- /** 路径 */
2496
- path: string;
2497
- }
2498
- /**
2499
- * McpComponent 配置
2500
- */
2501
- interface McpComponentConfig {
2502
- /** 扫描路径列表 */
2503
- mcpPaths: McpPath[];
2504
- /** 显式配置的服务器 */
2505
- servers?: Record<string, z11.infer<typeof McpServerConfigSchema>>;
2506
- /** 默认超时(毫秒) */
2507
- timeout?: number;
2508
- /** 是否启用 */
2509
- enabled?: boolean;
2510
- }
2511
- /**
2512
- * McpComponentOptions(通过 options 传递)
2513
- */
2514
- interface McpComponentOptions {
2515
- /** ConfigComponent 实例(必填) */
2516
- configComponent: ConfigComponent;
2517
- /** 配置文件相对路径 */
2518
- configPath?: string;
2519
- /** 环境变量前缀 */
2520
- envPrefix?: string;
2521
- /** 直接传入的配置对象(最高优先级) */
2522
- config?: Partial<McpComponentConfig>;
2523
- }
2524
- /**
2525
- * McpComponent
2526
- *
2527
- * MCP 客户端管理组件
2528
- *
2529
- * 功能:
2530
- * 1. 配置管理(支持 file/env/object 三层配置)
2531
- * 2. 扫描 MCP 服务器
2532
- * 3. 连接/断开 MCP 服务器
2533
- * 4. 注册 MCP tools 到 ToolComponent
2534
- */
2535
- declare class McpComponent extends BaseComponent {
2536
- readonly name = "mcp";
2537
- readonly version = "1.0.0";
2538
- private config;
2539
- private toolComponent?;
2540
- private configComponent?;
2541
- private scanner?;
2542
- private loader?;
2543
- private serverStatus;
2544
- private registeredTools;
2545
- constructor();
2546
- init(config?: ComponentConfig): Promise<void>;
2547
- start(): Promise<void>;
2548
- stop(): Promise<void>;
2549
- private registerConfig;
2550
- private flattenConfig;
2551
- private loadServers;
2552
- private registerServerTools;
2553
- /**
2554
- * 获取服务器状态
2555
- */
2556
- getServerStatus(name: string): McpServerStatus | undefined;
2557
- /**
2558
- * 列出所有服务器
2559
- */
2560
- listServers(): McpServerStatus[];
2561
- /**
2562
- * 列出所有 MCP 工具
2563
- */
2564
- listTools(): ToolInfo[];
2565
- /**
2566
- * 重新加载所有服务器
2567
- */
2568
- reload(): Promise<void>;
2569
- }
2570
- interface WorkflowRepository {
2571
- create(workflow: Omit<Workflow, "id" | "createdAt" | "updatedAt">): Workflow;
2572
- getById(id: string): Workflow | null;
2573
- getByName(name: string): Workflow | null;
2574
- list(options?: ListOptions): Workflow[];
2575
- update(id: string, updates: Partial<Workflow>): Workflow | null;
2576
- delete(id: string): boolean;
2577
- }
2578
- interface CreateOptions {
2579
- tags?: string[];
2580
- metadata?: WorkflowMetadata;
2581
- taskId?: number;
2582
- force?: boolean;
2583
- }
2584
- interface ListOptions {
2585
- tag?: string;
2586
- taskId?: number;
2587
- search?: string;
2588
- limit?: number;
2589
- offset?: number;
2590
- }
2591
- interface WorkflowUpdates {
2592
- definition?: WorkflowDefinition;
2593
- config?: WorkflowConfig;
2594
- tags?: string[];
2595
- metadata?: WorkflowMetadata;
2596
- }
2597
- interface WorkflowEngine {
2598
- createSession(workflowId: string, options?: {
2599
- input?: Record<string, any>;
2600
- workflowName?: string;
2601
- workflowVersion?: string;
2602
- }): Promise<string>;
2603
- run(sessionId: string, options?: RunOptions): Promise<RunResult>;
2604
- runWorkflow(workflow: Workflow | WorkflowDefinition, options?: RunOptions): Promise<RunResult>;
2605
- stop(sessionId: string, reason?: string): Promise<void>;
2606
- addNode(sessionId: string, nodeDefinition: NodeDefinition): Promise<void>;
2607
- removeNode(sessionId: string, nodeId: string): Promise<void>;
2608
- }
2609
- type WorkflowEngineFactory = (workflow: Workflow, options?: RunOptions) => Pick<WorkflowEngine, "run" | "stop" | "runWorkflow">;
2610
- interface SessionInfo {
2611
- id: string;
2612
- title: string;
2613
- metadata: WorkflowSessionMetadata3;
2614
- createdAt: Date;
2615
- updatedAt: Date;
2616
- }
2617
- interface WorkflowSessionMetadata3 {
2618
- type: "workflow";
2619
- runId?: string;
2620
- workflowId?: string;
2621
- workflowName: string;
2622
- workflowVersion?: string;
2623
- status: "running" | "paused" | "completed" | "failed";
2624
- input?: any;
2625
- rootNodeId?: string;
2626
- agentSessions?: AgentSessionRef3[];
2627
- }
2628
- interface AgentSessionRef3 {
2629
- nodeId: string;
2630
- sessionId: string;
2631
- status: "active" | "paused" | "completed";
2632
- }
2633
- declare class WorkflowService {
2634
- private workflowRepository;
2635
- private engineFactory;
2636
- private sessionComponent?;
2637
- constructor(workflowRepository: WorkflowRepository, engineFactory: WorkflowEngineFactory, sessionComponent?: {
2638
- create(options: {
2639
- id?: string;
2640
- title: string;
2641
- metadata: any;
2642
- }): Promise<{
2643
- id: string;
2644
- }>;
2645
- get(id: string): Promise<SessionInfo | null | undefined>;
2646
- list?(options?: {
2647
- limit?: number;
2648
- offset?: number;
2649
- }): Promise<SessionInfo[]>;
2650
- update(id: string, updates: {
2651
- metadata?: any;
2652
- }): Promise<boolean>;
2653
- addMessage(sessionId: string, message: any): Promise<string>;
2654
- getMessages(sessionId: string): Promise<any[]>;
2655
- } | undefined);
2656
- /**
2657
- * Create a new workflow from definition
2658
- */
2659
- createWorkflow(definition: WorkflowDefinition, options?: CreateOptions): Promise<Workflow>;
2660
- /**
2661
- * Get workflow by id or name
2662
- */
2663
- getWorkflow(idOrName: string): Workflow | null;
2664
- /**
2665
- * Get workflow by id
2666
- */
2667
- getWorkflowById(id: string): Workflow | null;
2668
- /**
2669
- * Get workflow by name
2670
- */
2671
- getWorkflowByName(name: string): Workflow | null;
2672
- /**
2673
- * List workflows with optional filters
2674
- */
2675
- listWorkflows(options?: ListOptions): Workflow[];
2676
- /**
2677
- * Update an existing workflow
2678
- */
2679
- updateWorkflow(idOrName: string, updates: WorkflowUpdates, options?: {
2680
- tags?: string[];
2681
- }): Promise<Workflow>;
2682
- /**
2683
- * Delete a workflow by name or id
2684
- */
2685
- deleteWorkflow(idOrName: string): Promise<boolean>;
2686
- /**
2687
- * Run a workflow by definition or name
2688
- *
2689
- * This creates a Session that represents the workflow run.
2690
- * The Session ID is used as the run identifier for pause/resume.
2691
- */
2692
- runWorkflow(idOrNameOrDefinition: string | WorkflowDefinition, input?: Record<string, any>, options?: RunOptions): Promise<RunResult>;
2693
- /**
2694
- * Run workflow by name (convenience method)
2695
- */
2696
- runWorkflowByName(name: string, input?: Record<string, any>, options?: RunOptions): Promise<RunResult>;
2697
- /**
2698
- * Stop a running workflow session
2699
- */
2700
- stopRun(sessionId: string, reason?: string): Promise<void>;
2701
- /**
2702
- * Get workflow session by ID
2703
- */
2704
- getSession(sessionId: string): Promise<SessionInfo | null>;
2705
- /**
2706
- * List workflow sessions
2707
- */
2708
- listSessions(options?: {
2709
- limit?: number;
2710
- offset?: number;
2711
- }): Promise<SessionInfo[]>;
2712
- /**
2713
- * Get messages from a workflow session
2714
- */
2715
- getSessionMessages(sessionId: string): Promise<any[]>;
2716
- }
2717
- /**
2718
- * BaseEnvironment 实现类(非抽象)
2719
- *
2720
- * 实现 Environment 接口,提供简洁的环境基础功能。
2721
- * 子类可继承此类并覆盖特定方法来自定义行为。
2722
- */
2723
- declare class BaseEnvironment extends BaseComponent implements Environment {
2724
- readonly name: string;
2725
- readonly version: string;
2726
- protected components;
2727
- /**
2728
- * 构造函数
2729
- */
2730
- constructor(config?: BaseEnvironmentConfig);
2731
- /**
2732
- * 注册默认组件(子类可覆盖)
2733
- */
2734
- protected registerDefaultComponents(): void;
2735
- /**
2736
- * 获取环境配置
2737
- */
2738
- getConfig(): EnvironmentConfig & {
2739
- env: Environment;
2740
- };
2741
- /**
2742
- * 注册组件
2743
- */
2744
- registerComponent(component: Component): void;
2745
- /**
2746
- * 注销组件
2747
- */
2748
- unregisterComponent(name: string): void;
2749
- /**
2750
- * 获取组件
2751
- */
2752
- getComponent<T extends Component>(name: string): T | undefined;
2753
- /**
2754
- * 列出所有组件
2755
- */
2756
- listComponents(): Component[];
2757
- /**
2758
- * 处理自然语言查询(核心入口)
2759
- *
2760
- * 委托给 AgentComponent.run() 处理
2761
- */
2762
- handle_query(query: string, context?: AgentContext): Promise<string>;
2763
- /**
2764
- * 处理动作
2765
- *
2766
- * 默认实现抛出错误,子类必须实现。
2767
- */
2768
- handle_action(action: Action, context: Context): Promise<ToolResult>;
2769
- /**
2770
- * 事件订阅者映射
2771
- */
2772
- protected eventHandlers;
2773
- /**
2774
- * 通配符订阅者(接收所有事件)
2775
- */
2776
- protected wildcardHandlers;
2777
- /**
2778
- * 订阅 EnvEvent(返回取消订阅函数)
2779
- */
2780
- subscribe(handler: EnvEventHandler): () => void;
2781
- /**
2782
- * 订阅指定类型事件
2783
- */
2784
- subscribeTo(eventType: string | string[], handler: EnvEventHandler): () => void;
2785
- /**
2786
- * 订阅所有事件(通配符)
2787
- *
2788
- * 与 subscribe 相同,为保持 API 一致性
2789
- */
2790
- subscribeAll(handler: EnvEventHandler): () => void;
2791
- /**
2792
- * 推送事件
2793
- */
2794
- pushEnvEvent(event: EnvEvent | EnvEventInput): void;
2795
- /**
2796
- * 创建并推送事件(便捷方法)
2797
- */
2798
- protected emit<T = unknown>(type: string, payload: T, metadata?: Partial<EnvEvent["metadata"]>): void;
2799
- /**
2800
- * 初始化所有组件(分阶段初始化)
2801
- *
2802
- * 1. 第一阶段:初始化 ConfigComponent(如果存在)
2803
- * 2. 第二阶段:初始化其他组件
2804
- */
2805
- protected initializeComponents(): Promise<void>;
2806
- /**
2807
- * 启动所有组件
2808
- *
2809
- * 注意:由于 Environment.init() 会将组件状态设为 "running",我们需要确保
2810
- * 即使组件已经是 "running" 状态,它们的 start() 方法仍然被调用。
2811
- * 这是因为 start() 负责注册工具等运行时资源。
2812
- */
2813
- protected startComponents(): Promise<void>;
2814
- /**
2815
- * 停止所有组件
2816
- */
2817
- protected stopComponents(): Promise<void>;
2818
- /**
2819
- * 初始化(可覆盖)
2820
- */
2821
- protected onInit(): Promise<void>;
2822
- /**
2823
- * 启动(可覆盖)
2824
- */
2825
- protected onStart(): Promise<void>;
2826
- /**
2827
- * 停止(可覆盖)
2828
- */
2829
- protected onStop(): Promise<void>;
2830
- /**
2831
- * BaseComponent 钩子
2832
- *
2833
- * Environment 特殊处理:它本身就是 env,所以不需要外部传入。
2834
- * 此方法覆盖 BaseComponent.init(),直接设置自引用。
2835
- */
2836
- init(): Promise<void>;
2837
- start(): Promise<void>;
2838
- stop(): Promise<void>;
2839
- /**
2840
- * 从配置文件加载服务配置
2841
- */
2842
- loadServiceConfig(configPath: string): Promise<ServiceConfig>;
2843
- /**
2844
- * 生成组件初始化选项
2845
- */
2846
- generateComponentOptions(componentName: string, configEntry: ComponentConfigEntry): GeneratedComponentOptions;
2847
- /**
2848
- * 注册组件并用配置初始化
2849
- */
2850
- registerComponentWithConfig(component: Component, configEntry: ComponentConfigEntry): Promise<void>;
2851
- /**
2852
- * 从配置文件初始化整个环境
2853
- */
2854
- initFromConfig(configPath: string): Promise<void>;
2855
- }
2856
- /**
2857
- * XDG 基础目录类型
2858
- */
2859
- type XDGDirType = "config" | "state" | "data" | "cache";
2860
- /**
2861
- * XDG 路径接口
2862
- */
2863
- interface XDGPaths {
2864
- home: string;
2865
- config: string;
2866
- state: string;
2867
- data: string;
2868
- cache: string;
2869
- }
2870
- /**
2871
- * 全局 XDG 路径实例
2872
- */
2873
- declare const XDG_PATHS: unknown;
2874
- /**
2875
- * 获取 XDG 路径
2876
- */
2877
- declare function getXDGPaths(): XDGPaths;
2878
- /**
2879
- * 获取特定类型的 XDG 路径
2880
- */
2881
- declare function getXDGPath(type: XDGDirType): string;
2882
- /**
2883
- * EventSource 类型定义
2884
- *
2885
- * 定义事件源组件的类型、接口和状态
2886
- */
2887
- /**
2888
- * EventSource 状态
2889
- */
2890
- type EventSourceStatus = "created" | "starting" | "running" | "stopping" | "stopped" | "error";
2891
- /**
2892
- * EventSource 类型
2893
- */
2894
- type EventSourceType = "lark-cli" | "websocket" | "timer" | "http-webhook" | "file-watcher";
2895
- /**
2896
- * EventSource 配置
2897
- */
2898
- interface EventSourceConfig {
2899
- /** 事件源 ID */
2900
- id: string;
2901
- /** 事件源名称 */
2902
- name: string;
2903
- /** 事件源类型 */
2904
- type: EventSourceType;
2905
- /** 是否启用 */
2906
- enabled: boolean;
2907
- /** 事件类型过滤(支持通配符) */
2908
- eventTypes?: string[];
2909
- /** 轮询间隔(毫秒,定时器用) */
2910
- interval?: number;
2911
- /** cron 表达式(定时器用) */
2912
- cron?: string;
2913
- /** 命令参数(lark-cli 用) */
2914
- command?: string;
2915
- /** WebSocket URL */
2916
- url?: string;
2917
- /** WebSocket headers */
2918
- headers?: Record<string, string>;
2919
- /** 自定义选项 */
2920
- options?: Record<string, unknown>;
2921
- }
2922
- /**
2923
- * 事件元信息
2924
- *
2925
- * 包含帮助 Agent 决策和回复的关键信息
2926
- */
2927
- interface EventMetadata {
2928
- /** 事件类型,如 im.message.receive_v1 */
2929
- eventType?: string;
2930
- /** 应用 ID */
2931
- appId?: string;
2932
- /** 会话 ID */
2933
- chatId?: string;
2934
- /** 会话类型,如 p2p, group */
2935
- chatType?: string;
2936
- /** 消息 ID */
2937
- messageId?: string;
2938
- /** 消息类型,如 text, image */
2939
- messageType?: string;
2940
- /** 发送者 ID */
2941
- senderId?: string;
2942
- /** 租户 key */
2943
- tenantKey?: string;
2944
- /** 其他原始字段 */
2945
- [key: string]: unknown;
2946
- }
2947
- /**
2948
- * 推荐动作
2949
- *
2950
- * 告诉 Agent 应该如何处理这个事件
2951
- */
2952
- interface RecommendedAction {
2953
- /** 动作描述 */
2954
- action: string;
2955
- /** 回复目标(如果有) */
2956
- replyTo?: {
2957
- appId?: string;
2958
- chatId?: string;
2959
- messageId?: string;
2960
- };
2961
- /** 原始回复指令(用于调用事件源 API) */
2962
- rawCommand?: string;
2963
- }
2964
- /**
2965
- * 反馈通道
2966
- *
2967
- * 告诉 Agent 处理完事件后,如何将结果反馈到事件源
2968
- */
2969
- interface ReplyChannel {
2970
- /** 渠道类型,如 lark-cli, websocket */
2971
- type: string;
2972
- /** 发送目标的 app ID */
2973
- appId?: string;
2974
- /** 发送目标的会话 ID */
2975
- chatId?: string;
2976
- /** 原始消息 ID(用于回复) */
2977
- messageId?: string;
2978
- /** 原始回复指令 */
2979
- rawCommand?: string;
2980
- /** 其他自定义参数 */
2981
- params?: Record<string, unknown>;
2982
- }
2983
- /**
2984
- * EventSource 事件负载
2985
- */
2986
- interface EventSourceEventPayload {
2987
- /** 事件源 ID */
2988
- sourceId: string;
2989
- /** 事件源类型 */
2990
- sourceType: EventSourceType;
2991
- /** 原始事件数据 */
2992
- rawEvent: unknown;
2993
- /** 处理后的消息内容 */
2994
- message: string;
2995
- /** 事件元信息(帮助 Agent 决策) */
2996
- metadata: EventMetadata;
2997
- /** 反馈通道:处理完成后如何将结果返回到事件源 */
2998
- replyChannel?: ReplyChannel;
2999
- /** 推荐动作 */
3000
- recommendedAction?: RecommendedAction;
3001
- /** 时间戳 */
3002
- timestamp: number;
3003
- }
3004
- /**
3005
- * EventSource 事件
3006
- */
3007
- interface EventSourceEvent {
3008
- /** 事件源 ID */
3009
- sourceId: string;
3010
- /** 事件类型 */
3011
- type: string;
3012
- /** 时间戳 */
3013
- timestamp: number;
3014
- /** 事件负载 */
3015
- payload: EventSourceEventPayload;
3016
- }
3017
- /**
3018
- * EventSource 事件处理器
3019
- */
3020
- type EventSourceEventHandler = (event: EventSourceEvent) => string | undefined | void | Promise<void | string>;
3021
- /**
3022
- * EventSource 状态变更事件
3023
- */
3024
- interface EventSourceStatusChangeEvent {
3025
- /** 事件源 ID */
3026
- sourceId: string;
3027
- /** 事件源类型 */
3028
- sourceType: EventSourceType;
3029
- /** 旧状态 */
3030
- oldStatus: EventSourceStatus;
3031
- /** 新状态 */
3032
- newStatus: EventSourceStatus;
3033
- /** 错误信息(如果有) */
3034
- error?: string;
3035
- }
3036
- /**
3037
- * EventSource 持久化配置
3038
- */
3039
- interface EventSourcePersistenceConfig {
3040
- /** 版本 */
3041
- version: string;
3042
- /** 事件源列表 */
3043
- sources: EventSourceConfig[];
3044
- }
3045
- /**
3046
- * EventSourceComponent 接口
3047
- *
3048
- * 定义事件源组件的公共方法
3049
- */
3050
- interface EventSourceComponentInterface {
3051
- /** 组件名称 */
3052
- readonly name: string;
3053
- /** 组件版本 */
3054
- readonly version: string;
3055
- init(config?: unknown): Promise<void>;
3056
- start(): Promise<void>;
3057
- stop(): Promise<void>;
3058
- getStatus(): unknown;
3059
- register(config: EventSourceConfig): void;
3060
- unregister(id: string): boolean;
3061
- get(id: string): EventSourceConfig | undefined;
3062
- list(): EventSourceConfig[];
3063
- getStatus(id: string): EventSourceStatus | undefined;
3064
- startSource(id: string): Promise<void>;
3065
- stopSource(id: string): Promise<void>;
3066
- onEvent(id: string, handler: EventSourceEventHandler): void;
3067
- offEvent(id: string): void;
3068
- }
3069
- /**
3070
- * EventSourceAgentHandler 配置
3071
- */
3072
- interface EventSourceAgentHandlerConfig {
3073
- /** 是否启用 */
3074
- enabled?: boolean;
3075
- /** 前缀(用于区分事件消息) */
3076
- prefix?: string;
3077
- /** 是否包含原始事件 */
3078
- includeRawEvent?: boolean;
3079
- /** 是否包含时间戳 */
3080
- includeTimestamp?: boolean;
3081
- }
3082
- /**
3083
- * 检查是否为有效的事件源类型
3084
- */
3085
- declare function isValidEventSourceType(type: string): type is EventSourceType;
3086
- /**
3087
- * 获取事件源类型的默认配置
3088
- */
3089
- declare function getDefaultConfigForType(type: EventSourceType): Partial<EventSourceConfig>;
3090
- /**
3091
- * 验证事件源配置
3092
- */
3093
- declare function validateEventSourceConfig(config: EventSourceConfig): string[];
3094
- declare class EventSourceComponent extends BaseComponent {
3095
- readonly name = "event-source";
3096
- readonly version = "1.0.0";
3097
- private sources;
3098
- private statuses;
3099
- private processes;
3100
- private handlers;
3101
- private timers;
3102
- private buffers;
3103
- private configPath;
3104
- private persistenceEnabled;
3105
- /** ConfigComponent 用于配置管理 */
3106
- private configComponent?;
3107
- /** 配置热更新 watcher 清理函数 */
3108
- private configWatcher?;
3109
- /**
3110
- * 初始化
3111
- */
3112
- protected onInit(): Promise<void>;
3113
- /**
3114
- * 公开的初始化方法,用于子类覆盖
3115
- *
3116
- * 这个方法会被 BaseComponent.init() 调用,用于执行子类特有的初始化逻辑。
3117
- * 它接收 ComponentConfig,并通过 ComponentConfig.options 获取 EventSourceComponentOptions。
3118
- */
3119
- init(options?: ComponentConfig): Promise<void>;
3120
- protected onStart(): Promise<void>;
3121
- protected onStop(): Promise<void>;
3122
- /**
3123
- * 注册配置到 ConfigComponent
3124
- *
3125
- * 配置加载顺序(优先级从低到高):
3126
- * 1. Defaults - 默认值(最低)
3127
- * 2. FileSource - 从配置文件加载
3128
- * 3. EnvSource - 从环境变量加载
3129
- * 4. config 对象 - 直接传入的配置(最高)
3130
- */
3131
- private registerConfig;
3132
- /**
3133
- * 构建最终配置对象
3134
- */
3135
- private buildConfig;
3136
- /**
3137
- * 将配置对象展平为点号路径
3138
- */
3139
- private flattenConfig;
3140
- /**
3141
- * 注册配置热更新监听
3142
- */
3143
- private registerConfigWatcher;
3144
- /**
3145
- * 处理配置变更
3146
- */
3147
- protected onConfigChange(event: {
3148
- key: string;
3149
- oldValue?: unknown;
3150
- newValue?: unknown;
3151
- }): void;
3152
- /**
3153
- * 注册事件源
3154
- */
3155
- register(config: EventSourceConfig): void;
3156
- /**
3157
- * 注销事件源
3158
- */
3159
- unregister(id: string): boolean;
3160
- /**
3161
- * 获取事件源配置
3162
- */
3163
- get(id: string): EventSourceConfig | undefined;
3164
- /**
3165
- * 列出所有事件源
3166
- */
3167
- list(): EventSourceConfig[];
3168
- /**
3169
- * 获取事件源状态
3170
- */
3171
- getEventSourceStatus(id: string): EventSourceStatus | undefined;
3172
- /**
3173
- * 获取指定事件源的状态
3174
- */
3175
- getEventSourceStatusById(id: string): EventSourceStatus | undefined;
3176
- /**
3177
- * 设置事件处理器
3178
- */
3179
- onEvent(id: string, handler: EventSourceEventHandler): void;
3180
- /**
3181
- * 移除事件处理器
3182
- */
3183
- offEvent(id: string): void;
3184
- /**
3185
- * 启动事件源
3186
- *
3187
- * 注意:方法名为 startSource 而非 start,以避免覆盖 Component 接口的 start() 方法
3188
- */
3189
- startSource(id: string): Promise<void>;
3190
- /**
3191
- * 停止事件源
3192
- *
3193
- * 注意:方法名为 stopSource 而非 stop,以避免覆盖 Component 接口的 stop() 方法
3194
- */
3195
- stopSource(id: string): Promise<void>;
3196
- /**
3197
- * 根据命令模式查找所有相关进程 PID
3198
- *
3199
- * @param pattern 命令模式,用于匹配进程
3200
- * @returns 匹配的 PID 列表
3201
- */
3202
- private findRelatedProcesses;
3203
- /**
3204
- * 查找并记录所有相关进程 PID
3205
- *
3206
- * @param id 事件源 ID
3207
- */
3208
- private trackRelatedProcesses;
3209
- /**
3210
- * 启动 lark-cli 事件源
3211
- * 返回 Promise,只有在进程真正成功启动后才 resolve
3212
- */
3213
- private startLarkCli;
3214
- /**
3215
- * 启动定时器事件源
3216
- */
3217
- private startTimer;
3218
- /**
3219
- * 启动 WebSocket 事件源
3220
- */
3221
- private startWebSocket;
3222
- /**
3223
- * 处理数据流
3224
- */
3225
- private processStream;
3226
- /**
3227
- * 处理事件
3228
- */
3229
- handleEvent(sourceId: string, rawData: string): void;
3230
- /**
3231
- * 事件类型匹配(支持通配符)
3232
- */
3233
- private matchEventType;
3234
- /**
3235
- * 提取事件元信息
3236
- */
3237
- private extractMetadata;
3238
- /**
3239
- * 格式化事件消息
3240
- */
3241
- private formatEventMessage;
3242
- /**
3243
- * 获取配置文件路径
3244
- */
3245
- private getConfigFilePath;
3246
- /**
3247
- * 持久化配置
3248
- */
3249
- saveConfig(): Promise<void>;
3250
- /**
3251
- * 加载配置
3252
- */
3253
- loadConfig(): Promise<void>;
3254
- }
3255
- /**
3256
- * EventSourceAgentHandler 配置
3257
- */
3258
- interface EventSourceAgentHandlerConfig2 {
3259
- /** 是否启用 */
3260
- enabled?: boolean;
3261
- /** 前缀(用于区分事件消息) */
3262
- prefix?: string;
3263
- /** 是否包含原始事件 */
3264
- includeRawEvent?: boolean;
3265
- /** 是否包含时间戳 */
3266
- includeTimestamp?: boolean;
3267
- }
3268
- /**
3269
- * EventSourceAgentHandler
3270
- *
3271
- * 将事件源事件转换为 Agent 可读的文本消息
3272
- */
3273
- declare class EventSourceAgentHandler {
3274
- private config;
3275
- constructor(config?: EventSourceAgentHandlerConfig2);
3276
- /**
3277
- * 创建事件处理器
3278
- */
3279
- createHandler(): EventSourceEventHandler;
3280
- /**
3281
- * 格式化事件为 Agent 消息
3282
- */
3283
- formatEventForAgent(event: EventSourceEvent): string;
3284
- /**
3285
- * 更新配置
3286
- */
3287
- updateConfig(config: Partial<EventSourceAgentHandlerConfig2>): void;
3288
- /**
3289
- * 获取配置
3290
- */
3291
- getConfig(): Readonly<Required<EventSourceAgentHandlerConfig2>>;
3292
- }
3293
- /**
3294
- * ToolComponent
3295
- * 工具管理核心组件
3296
- */
3297
- declare class ToolComponent extends BaseComponent {
3298
- readonly name = "tool";
3299
- readonly version = "1.0.0";
3300
- private registry;
3301
- private validator;
3302
- private config?;
3303
- /** ConfigComponent 用于配置管理 */
3304
- private configComponent?;
3305
- /** 配置变更 watcher 清理函数 */
3306
- private configWatcher?;
3307
- constructor();
3308
- /**
3309
- * 初始化组件
3310
- *
3311
- * 配置加载优先级(从高到低):
3312
- * 1. Object - 直接传入的 config 对象
3313
- * 2. Env - 环境变量(通过 envPrefix 配置前缀)
3314
- * 3. File - 配置文件(通过 configPath 指定)
3315
- */
3316
- init(config?: ComponentConfig): Promise<void>;
3317
- /**
3318
- * 注册配置到 ConfigComponent
3319
- *
3320
- * 配置加载顺序(优先级从低到高):
3321
- * 1. FileSource - 从配置文件加载(最低)
3322
- * 2. EnvSource - 从环境变量加载(中等)
3323
- * 3. MemorySource - 直接配置对象(最高)
3324
- *
3325
- * 实现步骤:
3326
- * 1. 使用 registerComponent(TOOL_CONFIG_REGISTRATION) 注册配置
3327
- * 2. 使用 load("tool") 加载已注册的配置
3328
- * 3. 设置 TOOL_DEFAULTS 默认值
3329
- * 4. 处理环境变量后备方案
3330
- * 5. 处理 config 对象(最高优先级)
3331
- */
3332
- private registerConfig;
3333
- /**
3334
- * 将配置对象展平为点号路径
3335
- */
3336
- private flattenConfig;
3337
- /**
3338
- * 注册配置热更新监听
3339
- */
3340
- private registerConfigWatcher;
3341
- /**
3342
- * 处理配置变更
3343
- */
3344
- protected onConfigChange(event: {
3345
- key: string;
3346
- oldValue?: unknown;
3347
- newValue?: unknown;
3348
- }): void;
3349
- /**
3350
- * 停止组件
3351
- */
3352
- onStop(): Promise<void>;
3353
- /**
3354
- * 注册工具
3355
- */
3356
- register(tool: Tool, source?: ToolRegistration["source"]): void;
3357
- /**
3358
- * 批量注册工具
3359
- */
3360
- registerMany(tools: Tool[], source?: ToolRegistration["source"]): void;
3361
- /**
3362
- * 注销工具
3363
- */
3364
- unregister(name: string): boolean;
3365
- /**
3366
- * 获取工具
3367
- */
3368
- getTool(name: string): Tool | undefined;
3369
- /**
3370
- * 列出工具
3371
- */
3372
- listTools(filter?: ToolListFilter): Tool[];
3373
- /**
3374
- * 获取工具数量
3375
- */
3376
- getToolCount(): number;
3377
- /**
3378
- * 检查工具是否存在
3379
- */
3380
- hasTool(name: string): boolean;
3381
- /**
3382
- * 按分类获取工具
3383
- */
3384
- getToolsByCategory(category: string): Tool[];
3385
- /**
3386
- * 执行工具
3387
- */
3388
- execute(request: ToolExecuteRequest): Promise<ToolResult2>;
3389
- /**
3390
- * 加载内置工具
3391
- */
3392
- loadBuiltInTools(): Promise<void>;
3393
- /**
3394
- * 获取 Tool Hook 点列表
3395
- */
3396
- getHookPoints(): string[];
3397
- }
3398
- interface WorkflowComponentConfig extends ComponentConfig {
3399
- /** 自动注册 decorator 节点类型 */
3400
- autoRegisterDecorators?: boolean;
3401
- }
3402
- declare class WorkflowComponent extends BaseComponent {
3403
- readonly name = "workflow";
3404
- readonly version = "1.0.0";
3405
- workflowService: WorkflowService | null;
3406
- sessionComponent: any;
3407
- private nodeRegistry;
3408
- private decoratorsRegistered;
3409
- /**
3410
- * 获取 WorkflowService 实例
3411
- */
3412
- getService(): WorkflowService | null;
3413
- private toolComponent?;
3414
- private llmComponent?;
3415
- private logTraceComponent?;
3416
- private _workflowEnv?;
3417
- private skillRegistry?;
3418
- private agentRunner?;
3419
- /**
3420
- * 初始化 WorkflowComponent
3421
- */
3422
- init(options: any): Promise<void>;
3423
- /**
3424
- * 注册 decorator 节点类型
3425
- */
3426
- private registerDecorators;
3427
- /**
3428
- * 创建 WorkflowService(需要外部传入依赖)
3429
- *
3430
- * Note: After session-based refactoring, runRepository is no longer needed.
3431
- * Workflow runs are managed via SessionComponent.
3432
- */
3433
- createService(options: {
3434
- workflowRepository: any;
3435
- toolComponent?: ToolComponent;
3436
- llmComponent?: LLMComponent;
3437
- env?: Environment;
3438
- skillRegistry?: {
3439
- getSkill(name: string): any;
3440
- hasSkill(name: string): boolean;
3441
- };
3442
- sessionComponent?: any;
3443
- }): WorkflowService;
3444
- /**
3445
- * 运行工作流定义
3446
- */
3447
- runWorkflow(definition: WorkflowDefinition, input?: Record<string, any>, options?: RunOptions): Promise<RunResult>;
3448
- /**
3449
- * 停止组件并清理资源
3450
- *
3451
- * 遵循 Component 接口契约,通过 onStop 钩子实现
3452
- */
3453
- protected onStop(): Promise<void>;
3454
- /**
3455
- * 启动组件 - 初始化持久化的 SQLite WorkflowService
3456
- */
3457
- start(): Promise<void>;
3458
- /**
3459
- * 注册 ask_user 工具到 ToolComponent
3460
- *
3461
- * 这样 Workflow 中的 AgentNode 执行时,Agent 能够找到并调用 ask_user 工具。
3462
- * ask_user 工具会抛出 AskUserError,触发 workflow pause。
3463
- */
3464
- private registerAskUserTool;
3465
- /**
3466
- * 初始化 SQLite 持久化的 WorkflowService
3467
- *
3468
- * Note: After session-based refactoring, runRepository is no longer needed.
3469
- * Workflow runs are managed via SessionComponent.
3470
- */
3471
- private initSqliteService;
3472
- /**
3473
- * 创建内存中的 WorkflowService(用于快速运行或降级)
3474
- */
3475
- private createInMemoryService;
3476
- /**
3477
- * 停止组件
3478
- */
3479
- stop(): Promise<void>;
3480
- }
3481
- export { validateEventSourceConfig, isValidEventSourceType, getXDGPaths, getXDGPath, getDefaultConfigForType, createPriorityHook, createHook, XDG_PATHS, XDGPaths, XDGDirType, WorkflowComponentConfig, WorkflowComponent, UpdateTaskOptions, ToolResult, ToolCallResult, ToolCall2 as ToolCall, Tool, TaskStatus, TaskPriority, TaskOperation, TaskConfig, TaskComponentOptions, TaskComponent, Task, Plugin, McpServerStatus, McpServerConfig, McpPath, McpComponentOptions, McpComponentConfig, McpComponent, ListTasksOptions, ListOperationsOptions, LLMOutput, HookResult2 as HookResult, HookPoint, HookPhase, HookMeta, HookManagerOptions, HookManager, HookFn, HookContext, HookActionType2 as HookActionType, Hook, EventSourceType, EventSourceStatusChangeEvent, EventSourceStatus, EventSourcePersistenceConfig, EventSourceEventPayload, EventSourceEventHandler, EventSourceEvent, EventSourceConfig, EventSourceComponentInterface, EventSourceComponent, EventSourceAgentHandlerConfig, EventSourceAgentHandler, EnvironmentConfig, Environment, EnvEventMetadata, EnvEventHandler, EnvEvent, DiscoveredMcpServer, CreateTaskOptions, CreateOperationOptions, Context, ComponentWatchCallback, ComponentStatus, ComponentSchema, ComponentPaths, ComponentConfigChangeEvent, ComponentConfig, Component, BaseEnvironmentConfig, BaseEnvironment, BaseComponent, ToolResult2 as AgentToolResult, AgentRunResult, AgentInstanceConfig, AgentInstance, HookContext2 as AgentHookContext, AgentContext, AgentComponentConfigSchema, AgentComponentConfig, AgentComponent, ActionType, Action };