@ai-setting/roy-agent-core 1.5.15-test → 1.5.17-beta.1

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 (141) hide show
  1. package/dist/config/index.d.ts +1250 -0
  2. package/dist/config/index.js +32 -0
  3. package/dist/env/agent/index.d.ts +2279 -0
  4. package/dist/env/agent/index.js +24 -0
  5. package/dist/env/commands/index.d.ts +1131 -0
  6. package/dist/env/commands/index.js +14 -0
  7. package/dist/env/debug/formatters/index.d.ts +236 -0
  8. package/dist/env/debug/formatters/index.js +11 -0
  9. package/dist/env/debug/index.d.ts +1652 -0
  10. package/dist/env/debug/index.js +26 -0
  11. package/dist/env/hook/index.d.ts +279 -0
  12. package/dist/env/hook/index.js +29 -0
  13. package/dist/env/index.d.ts +3481 -0
  14. package/dist/env/index.js +82 -0
  15. package/dist/env/llm/index.d.ts +1760 -0
  16. package/dist/env/llm/index.js +40 -0
  17. package/dist/env/log-trace/index.d.ts +1574 -0
  18. package/dist/env/log-trace/index.js +83 -0
  19. package/dist/env/mcp/index.d.ts +1331 -0
  20. package/dist/env/mcp/index.js +39 -0
  21. package/dist/env/mcp/tool/index.d.ts +183 -0
  22. package/dist/env/mcp/tool/index.js +14 -0
  23. package/dist/env/memory/built-in/index.d.ts +232 -0
  24. package/dist/env/memory/built-in/index.js +11 -0
  25. package/dist/env/memory/index.d.ts +1799 -0
  26. package/dist/env/memory/index.js +56 -0
  27. package/dist/env/memory/plugin/index.d.ts +747 -0
  28. package/dist/env/memory/plugin/index.js +36 -0
  29. package/dist/env/prompt/index.d.ts +1164 -0
  30. package/dist/env/prompt/index.js +20 -0
  31. package/dist/env/session/index.d.ts +1908 -0
  32. package/dist/env/session/index.js +25 -0
  33. package/dist/env/session/storage/index.d.ts +564 -0
  34. package/dist/env/session/storage/index.js +18 -0
  35. package/dist/env/skill/index.d.ts +1266 -0
  36. package/dist/env/skill/index.js +34 -0
  37. package/dist/env/skill/tool/index.d.ts +193 -0
  38. package/dist/env/skill/tool/index.js +9 -0
  39. package/dist/env/task/delegate/index.d.ts +1612 -0
  40. package/dist/env/task/delegate/index.js +18 -0
  41. package/dist/env/task/events/index.d.ts +171 -0
  42. package/dist/env/task/events/index.js +7 -0
  43. package/dist/env/task/hooks/index.d.ts +624 -0
  44. package/dist/env/task/hooks/index.js +7 -0
  45. package/dist/env/task/index.d.ts +1553 -0
  46. package/dist/env/task/index.js +34 -0
  47. package/dist/env/task/plugins/index.d.ts +466 -0
  48. package/dist/env/task/plugins/index.js +23 -0
  49. package/dist/env/task/storage/index.d.ts +241 -0
  50. package/dist/env/task/storage/index.js +14 -0
  51. package/dist/env/task/tools/index.d.ts +1485 -0
  52. package/dist/env/task/tools/index.js +17 -0
  53. package/dist/env/task/tools/operation/index.d.ts +1484 -0
  54. package/dist/env/task/tools/operation/index.js +15 -0
  55. package/dist/env/tool/built-in/index.d.ts +218 -0
  56. package/dist/env/tool/built-in/index.js +25 -0
  57. package/dist/env/tool/index.d.ts +1396 -0
  58. package/dist/env/tool/index.js +39 -0
  59. package/dist/env/workflow/decorators/index.d.ts +2161 -0
  60. package/dist/env/workflow/decorators/index.js +27 -0
  61. package/dist/env/workflow/engine/index.d.ts +3453 -0
  62. package/dist/env/workflow/engine/index.js +28 -0
  63. package/dist/env/workflow/index.d.ts +3546 -0
  64. package/dist/env/workflow/index.js +136 -0
  65. package/dist/env/workflow/nodes/index.d.ts +2092 -0
  66. package/dist/env/workflow/nodes/index.js +19 -0
  67. package/dist/env/workflow/service/index.d.ts +227 -0
  68. package/dist/env/workflow/service/index.js +13 -0
  69. package/dist/env/workflow/storage/index.d.ts +165 -0
  70. package/dist/env/workflow/storage/index.js +27 -0
  71. package/dist/env/workflow/tools/index.d.ts +416 -0
  72. package/dist/env/workflow/tools/index.js +159 -0
  73. package/dist/env/workflow/types/index.d.ts +2255 -0
  74. package/dist/env/workflow/types/index.js +98 -0
  75. package/dist/env/workflow/utils/index.d.ts +2031 -0
  76. package/dist/env/workflow/utils/index.js +637 -0
  77. package/dist/index.d.ts +7858 -0
  78. package/dist/index.js +399 -0
  79. package/dist/shared/@ai-setting/roy-agent-core-0rtxwr28.js +258 -0
  80. package/dist/shared/@ai-setting/roy-agent-core-0vbdz0x7.js +36 -0
  81. package/dist/shared/@ai-setting/roy-agent-core-1akcqxj9.js +349 -0
  82. package/dist/shared/@ai-setting/roy-agent-core-1ce3fqrk.js +117 -0
  83. package/dist/shared/@ai-setting/roy-agent-core-2dhd60aw.js +11 -0
  84. package/dist/shared/@ai-setting/roy-agent-core-3jywqmdd.js +393 -0
  85. package/dist/shared/@ai-setting/roy-agent-core-3rr5k71j.js +200 -0
  86. package/dist/shared/@ai-setting/roy-agent-core-44hnfb02.js +299 -0
  87. package/dist/shared/@ai-setting/roy-agent-core-4t40mkpv.js +206 -0
  88. package/dist/shared/@ai-setting/roy-agent-core-4txzpsbt.js +393 -0
  89. package/dist/shared/@ai-setting/roy-agent-core-5x94xmt6.js +350 -0
  90. package/dist/shared/@ai-setting/roy-agent-core-69jskqjg.js +180 -0
  91. package/dist/shared/@ai-setting/roy-agent-core-6kvtahqv.js +408 -0
  92. package/dist/shared/@ai-setting/roy-agent-core-7fgf85wc.js +284 -0
  93. package/dist/shared/@ai-setting/roy-agent-core-81w1963m.js +762 -0
  94. package/dist/shared/@ai-setting/roy-agent-core-8gxth0eh.js +10 -0
  95. package/dist/shared/@ai-setting/roy-agent-core-92z6t4he.js +14 -0
  96. package/dist/shared/@ai-setting/roy-agent-core-93zfb3r1.js +922 -0
  97. package/dist/shared/@ai-setting/roy-agent-core-9yxb3ty9.js +15 -0
  98. package/dist/shared/@ai-setting/roy-agent-core-b0x5dda6.js +1130 -0
  99. package/dist/shared/@ai-setting/roy-agent-core-bcbqy27c.js +14 -0
  100. package/dist/shared/@ai-setting/roy-agent-core-bvr1761x.js +653 -0
  101. package/dist/shared/@ai-setting/roy-agent-core-ctdhjv68.js +93 -0
  102. package/dist/shared/@ai-setting/roy-agent-core-d7cyjkf7.js +872 -0
  103. package/dist/shared/@ai-setting/roy-agent-core-dh9d7a3m.js +11 -0
  104. package/dist/shared/@ai-setting/roy-agent-core-e25xkv53.js +64 -0
  105. package/dist/shared/@ai-setting/roy-agent-core-eajcvp4e.js +378 -0
  106. package/dist/shared/@ai-setting/roy-agent-core-f7q2x5z6.js +492 -0
  107. package/dist/shared/@ai-setting/roy-agent-core-fs0mn2jk.js +52 -0
  108. package/dist/shared/@ai-setting/roy-agent-core-g1s2h0e5.js +171 -0
  109. package/dist/shared/@ai-setting/roy-agent-core-g99pxzn5.js +862 -0
  110. package/dist/shared/@ai-setting/roy-agent-core-gbqcyegm.js +1387 -0
  111. package/dist/shared/@ai-setting/roy-agent-core-gjq1yk68.js +208 -0
  112. package/dist/shared/@ai-setting/roy-agent-core-gq20wsgv.js +139 -0
  113. package/dist/shared/@ai-setting/roy-agent-core-gwc4h96n.js +534 -0
  114. package/dist/shared/@ai-setting/roy-agent-core-jfh9q2qh.js +204 -0
  115. package/dist/shared/@ai-setting/roy-agent-core-jvatggbb.js +603 -0
  116. package/dist/shared/@ai-setting/roy-agent-core-kkbwepqb.js +97 -0
  117. package/dist/shared/@ai-setting/roy-agent-core-pjr12nnd.js +587 -0
  118. package/dist/shared/@ai-setting/roy-agent-core-psv4v63c.js +176 -0
  119. package/dist/shared/@ai-setting/roy-agent-core-psvxt4c9.js +60 -0
  120. package/dist/shared/@ai-setting/roy-agent-core-qqceba6k.js +442 -0
  121. package/dist/shared/@ai-setting/roy-agent-core-qxhq8ven.js +57 -0
  122. package/dist/shared/@ai-setting/roy-agent-core-qxnbvgwe.js +66 -0
  123. package/dist/shared/@ai-setting/roy-agent-core-r9ezzemr.js +10 -0
  124. package/dist/shared/@ai-setting/roy-agent-core-rhmtwnw1.js +267 -0
  125. package/dist/shared/@ai-setting/roy-agent-core-rvv6ydff.js +584 -0
  126. package/dist/shared/@ai-setting/roy-agent-core-rvxg1wps.js +102 -0
  127. package/dist/shared/@ai-setting/roy-agent-core-satmq6sh.js +549 -0
  128. package/dist/shared/@ai-setting/roy-agent-core-sx7wsvnn.js +15 -0
  129. package/dist/shared/@ai-setting/roy-agent-core-t94ktchq.js +213 -0
  130. package/dist/shared/@ai-setting/roy-agent-core-vf215qfv.js +812 -0
  131. package/dist/shared/@ai-setting/roy-agent-core-vkz81f7v.js +1316 -0
  132. package/dist/shared/@ai-setting/roy-agent-core-vn2bc59q.js +1205 -0
  133. package/dist/shared/@ai-setting/roy-agent-core-wa1kzqky.js +328 -0
  134. package/dist/shared/@ai-setting/roy-agent-core-wft9ra24.js +20 -0
  135. package/dist/shared/@ai-setting/roy-agent-core-wrcy0h6z.js +2098 -0
  136. package/dist/shared/@ai-setting/roy-agent-core-xq8hhqb8.js +419 -0
  137. package/dist/shared/@ai-setting/roy-agent-core-xs5rsgat.js +368 -0
  138. package/dist/shared/@ai-setting/roy-agent-core-zbkpc41z.js +377 -0
  139. package/dist/shared/@ai-setting/roy-agent-core-zgypchmt.js +172 -0
  140. package/dist/shared/@ai-setting/roy-agent-core-zpn0bqa8.js +103 -0
  141. package/package.json +29 -8
@@ -0,0 +1,2279 @@
1
+ /**
2
+ * @fileoverview Hook 系统统一类型定义
3
+ *
4
+ * 提供所有 Component 使用的统一 Hook 接口
5
+ */
6
+ /**
7
+ * Hook 执行阶段
8
+ */
9
+ type HookPhase = "before" | "after" | "error";
10
+ /**
11
+ * Hook 元信息
12
+ */
13
+ interface HookMeta {
14
+ /** Hook 唯一名称 */
15
+ name: string;
16
+ /** 执行优先级,默认 0,数值越小越先执行 */
17
+ priority?: number;
18
+ /** 描述 */
19
+ description?: string;
20
+ }
21
+ /**
22
+ * 统一 Hook 上下文
23
+ *
24
+ * 泛型参数 T 表示该 Component 的上下文数据类型
25
+ */
26
+ interface HookContext<T = unknown> {
27
+ /** 当前组件信息 */
28
+ component: {
29
+ /** 组件名称 */
30
+ name: string;
31
+ /** 组件版本 */
32
+ version: string;
33
+ };
34
+ /** 上下文数据(Component 特定) */
35
+ data: T;
36
+ /** 元数据(可用于传递额外信息) */
37
+ metadata: Record<string, unknown>;
38
+ /** 执行阶段 */
39
+ phase: HookPhase;
40
+ /** 关联的 hook 点名称 */
41
+ hookPoint: string;
42
+ }
43
+ /**
44
+ * 统一 Hook 接口
45
+ *
46
+ * 所有 Component 的 Hook 都应实现此接口
47
+ */
48
+ interface Hook<T = unknown> extends HookMeta {
49
+ /** 执行 Hook
50
+ * @param ctx 上下文
51
+ */
52
+ execute(ctx: HookContext<T>): void | Promise<void>;
53
+ }
54
+ /**
55
+ * Hook 函数类型(便捷别名)
56
+ */
57
+ type HookFn<T = unknown> = (ctx: HookContext<T>) => void | Promise<void>;
58
+ /**
59
+ * Hook 干预动作类型
60
+ */
61
+ type HookActionType = "stop" | "retry" | "compress" | "extract_memory" | "inject_message" | "skip_tool" | "custom";
62
+ /**
63
+ * Hook 干预动作
64
+ */
65
+ interface HookAction {
66
+ /** 动作类型 */
67
+ type: HookActionType;
68
+ /** 动作参数 */
69
+ params?: Record<string, unknown>;
70
+ /** 动作描述 */
71
+ description?: string;
72
+ }
73
+ /**
74
+ * Hook 执行结果
75
+ */
76
+ interface HookResult {
77
+ /** 是否被停止 */
78
+ stopped?: boolean;
79
+ /** 干预动作(如果有) */
80
+ action?: HookAction;
81
+ /** 执行结果数据 */
82
+ results?: unknown[];
83
+ }
84
+ /**
85
+ * HookManager 配置
86
+ */
87
+ interface HookManagerOptions {
88
+ /** 组件名称(用于上下文) */
89
+ componentName?: string;
90
+ /** 组件版本(用于上下文) */
91
+ componentVersion?: string;
92
+ /** 默认优先级 */
93
+ defaultPriority?: number;
94
+ }
95
+ /**
96
+ * HookManager
97
+ *
98
+ * 统一管理 Component 的 Hook
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * const manager = new HookManager<MyContext>();
103
+ *
104
+ * // 注册 Hook
105
+ * manager.register("before-action", {
106
+ * name: "my-hook",
107
+ * priority: 10,
108
+ * execute: async (ctx) => { /* ... *\/ }
109
+ * });
110
+ *
111
+ * // 执行 Hooks
112
+ * await manager.execute("before-action", data);
113
+ * ```
114
+ */
115
+ declare class HookManager<T = unknown> {
116
+ private _hooks;
117
+ private componentName;
118
+ private componentVersion;
119
+ private defaultPriority;
120
+ constructor(options?: HookManagerOptions);
121
+ /**
122
+ * 注册 Hook 到指定 hook 点
123
+ */
124
+ register(hookPoint: string, hook: Hook<T>): void;
125
+ /**
126
+ * 注册多个 Hook
127
+ */
128
+ registerMany(hookPoint: string, hooks: Hook<T>[]): void;
129
+ /**
130
+ * 按名称取消注册 Hook
131
+ * @returns 是否成功取消注册
132
+ */
133
+ unregister(hookPoint: string, name: string): boolean;
134
+ /**
135
+ * 取消注册所有指定 hook 点的 Hook
136
+ */
137
+ unregisterAll(hookPoint: string): void;
138
+ /**
139
+ * 执行指定 hook 点的所有 Hook
140
+ */
141
+ execute(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
142
+ /**
143
+ * 执行指定 hook 点的所有 Hook 并收集返回值
144
+ */
145
+ executeAndCollect<R>(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<R[]>;
146
+ /**
147
+ * 获取指定 hook 点的 Hook 数量
148
+ */
149
+ count(hookPoint: string): number;
150
+ /**
151
+ * 清空所有 Hook
152
+ */
153
+ clear(): void;
154
+ /**
155
+ * 获取所有已注册的 hook 点
156
+ */
157
+ getHookPoints(): string[];
158
+ /**
159
+ * 检查 hook 点是否有任何 Hook
160
+ */
161
+ hasHooks(hookPoint: string): boolean;
162
+ /**
163
+ * 设置组件信息
164
+ */
165
+ setComponentInfo(name: string, version: string): void;
166
+ /**
167
+ * 获取所有注册的 hooks(只读副本)
168
+ */
169
+ get hooks(): ReadonlyMap<string, Hook<T>[]>;
170
+ /**
171
+ * 执行 Hook 并支持干预机制
172
+ *
173
+ * 如果任何一个 Hook 返回 { stop: true },执行会停止
174
+ * Hook 可以通过返回 HookResult 来执行干预动作
175
+ *
176
+ * @param hookPoint Hook 点
177
+ * @param data 数据
178
+ * @param metadata 元数据
179
+ * @returns 执行结果
180
+ */
181
+ executeWithIntervention(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<HookResult>;
182
+ /**
183
+ * 获取或创建 hook 列表
184
+ */
185
+ private getOrCreateHooks;
186
+ /**
187
+ * 按优先级排序 Hook
188
+ *
189
+ * 优先级相同时保持原顺序(稳定排序)
190
+ */
191
+ private sortHooks;
192
+ /**
193
+ * 创建 Hook 上下文
194
+ */
195
+ private createContext;
196
+ /**
197
+ * 安全执行 Hook(错误隔离)
198
+ */
199
+ private safeExecute;
200
+ /**
201
+ * 安全执行 Hook 并返回结果
202
+ */
203
+ private safeExecuteAndReturn;
204
+ }
205
+ import { ZodType as ZodType3, ZodTypeDef as ZodTypeDef2 } from "zod";
206
+ /**
207
+ * @fileoverview Environment types and interfaces
208
+ *
209
+ * 定义 Environment 的核心类型,包括:
210
+ * - Environment 配置
211
+ * - EnvEvent 事件类型
212
+ */
213
+ /**
214
+ * Environment Config
215
+ */
216
+ interface EnvironmentConfig {
217
+ /** 环境名称 */
218
+ name: string;
219
+ /** 环境版本 */
220
+ version: string;
221
+ /** 是否启用 */
222
+ enabled: boolean;
223
+ }
224
+ /**
225
+ * EnvEvent 元信息
226
+ */
227
+ interface EnvEventMetadata {
228
+ /** 触发事件的 session ID */
229
+ trigger_session_id?: string;
230
+ /** 触发事件的 agent ID */
231
+ trigger_agent_id?: string;
232
+ /** Agent 名称 */
233
+ trigger_agent_name?: string;
234
+ /** 环境名称 */
235
+ env_name?: string;
236
+ /** 事件来源 */
237
+ source?: string;
238
+ /** 其他元数据 */
239
+ [key: string]: unknown;
240
+ }
241
+ /**
242
+ * EnvEvent
243
+ *
244
+ * 统一的事件机制,用于:
245
+ * - Stream 事件(stream.start, stream.text, stream.completed 等)
246
+ * - 工具调用事件(tool.call, tool.result 等)
247
+ * - 会话事件(session.created, session.updated 等)
248
+ * - Agent 事件(agent.thinking, agent.acting 等)
249
+ */
250
+ interface EnvEvent<T = unknown> {
251
+ /** 事件 ID */
252
+ id: string;
253
+ /** 事件类型 */
254
+ type: string;
255
+ /** 时间戳 */
256
+ timestamp: number;
257
+ /** 元信息 */
258
+ metadata: EnvEventMetadata;
259
+ /** 事件负载 */
260
+ payload: T;
261
+ }
262
+ /**
263
+ * EnvEvent Handler
264
+ */
265
+ type EnvEventHandler = (event: EnvEvent) => void | Promise<void>;
266
+ /**
267
+ * EnvEvent 创建参数(部分属性,可选)
268
+ *
269
+ * 用于 pushEnvEvent 方法,允许传入部分属性,自动补全必填字段
270
+ */
271
+ interface EnvEventInput {
272
+ /** 事件类型(必填) */
273
+ type: string;
274
+ /** 事件 ID(可选,自动生成) */
275
+ id?: string;
276
+ /** 时间戳(可选,自动生成) */
277
+ timestamp?: number;
278
+ /** 元信息(可选,自动创建) */
279
+ metadata?: EnvEventMetadata;
280
+ /** 事件负载(可选) */
281
+ payload?: unknown;
282
+ }
283
+ /**
284
+ * Action 类型
285
+ */
286
+ interface Action {
287
+ /** Action 类型 */
288
+ type: string;
289
+ /** Action 参数 */
290
+ params?: Record<string, unknown>;
291
+ }
292
+ /**
293
+ * Tool Result
294
+ */
295
+ interface ToolResult {
296
+ /** 是否成功 */
297
+ success: boolean;
298
+ /** 输出内容 */
299
+ output: string | Record<string, unknown>;
300
+ /** 错误信息 */
301
+ error?: string;
302
+ }
303
+ /**
304
+ * Context
305
+ */
306
+ interface Context {
307
+ /** 会话 ID */
308
+ sessionId?: string;
309
+ /** 用户 ID */
310
+ userId?: string;
311
+ /** 工作目录 */
312
+ workdir?: string;
313
+ /** 中断信号 */
314
+ abort?: AbortSignal;
315
+ /** 元数据 */
316
+ metadata?: Record<string, unknown>;
317
+ }
318
+ import { ModelMessage } from "ai";
319
+ import { z, ZodType, ZodError } from "zod";
320
+ /**
321
+ * 沙箱配置
322
+ */
323
+ interface SandboxConfig {
324
+ /** 是否启用沙箱 */
325
+ enabled: boolean;
326
+ /** 沙箱类型 */
327
+ type: "native" | "docker";
328
+ /** 动作过滤 */
329
+ actionFilter?: {
330
+ /** 包含的动作 */
331
+ include?: string[];
332
+ /** 排除的动作 */
333
+ exclude?: string[];
334
+ };
335
+ /** 文件系统限制 */
336
+ filesystem?: {
337
+ /** 允许读取 */
338
+ allowRead?: string[];
339
+ /** 禁止读取 */
340
+ denyRead?: string[];
341
+ /** 允许写入 */
342
+ allowWrite?: string[];
343
+ /** 禁止写入 */
344
+ denyWrite?: string[];
345
+ };
346
+ /** 网络限制 */
347
+ network?: {
348
+ /** 允许的域名 */
349
+ allowedDomains?: string[];
350
+ /** 禁止的域名 */
351
+ deniedDomains?: string[];
352
+ };
353
+ /** Docker 配置 */
354
+ docker?: {
355
+ /** 镜像 */
356
+ image?: string;
357
+ /** 网络模式 */
358
+ networkMode?: "bridge" | "host" | "none";
359
+ /** 卷挂载 */
360
+ volumes?: Record<string, string>;
361
+ };
362
+ }
363
+ /**
364
+ * 工具执行上下文
365
+ */
366
+ interface ToolContext {
367
+ /** 工作目录 */
368
+ workdir?: string;
369
+ /** 用户 ID */
370
+ user_id?: string;
371
+ /** 会话 ID */
372
+ session_id?: string;
373
+ /** 消息 ID */
374
+ message_id?: string;
375
+ /** 中断信号 */
376
+ abort?: AbortSignal;
377
+ /** 额外元数据 */
378
+ metadata?: Record<string, unknown>;
379
+ /** 沙箱配置 */
380
+ sandbox?: SandboxConfig;
381
+ /** 沙箱 Provider */
382
+ sandboxProvider?: unknown | null;
383
+ }
384
+ /**
385
+ * 工具结果元数据
386
+ */
387
+ interface ToolResultMetadata {
388
+ /** 执行时间(毫秒) */
389
+ execution_time_ms: number;
390
+ /** 输出大小(字节) */
391
+ output_size?: number;
392
+ /** 标准输出 */
393
+ stdout?: string;
394
+ /** 标准错误 */
395
+ stderr?: string;
396
+ /** 退出码 */
397
+ exit_code?: number;
398
+ /** Token 使用量 */
399
+ usage?: {
400
+ inputTokens: number;
401
+ outputTokens: number;
402
+ totalTokens?: number;
403
+ };
404
+ /** 额外元数据 */
405
+ [key: string]: unknown;
406
+ }
407
+ /**
408
+ * 工具执行结果
409
+ */
410
+ interface ToolResult2 {
411
+ /** 是否成功 */
412
+ success: boolean;
413
+ /** 输出内容 */
414
+ output: string | Record<string, unknown>;
415
+ /** 错误信息 */
416
+ error?: string;
417
+ /** 结果元数据 */
418
+ metadata?: ToolResultMetadata;
419
+ }
420
+ /**
421
+ * 工具元数据
422
+ */
423
+ interface ToolMetadata {
424
+ /** 分类 */
425
+ category?: string;
426
+ /** 标签 */
427
+ tags?: string[];
428
+ /** 版本 */
429
+ version?: string;
430
+ /** 作者 */
431
+ author?: string;
432
+ /** 是否实验性 */
433
+ experimental?: boolean;
434
+ /** MCP 工具信息(MCP adapter 使用) */
435
+ mcpTool?: {
436
+ originalName: string;
437
+ originalDescription: string;
438
+ inputSchema?: unknown;
439
+ };
440
+ }
441
+ /**
442
+ * 工具沙箱配置
443
+ */
444
+ interface ToolSandboxConfig {
445
+ /** 是否启用沙箱 */
446
+ enabled: boolean;
447
+ /** 沙箱配置 */
448
+ config?: Partial<SandboxConfig>;
449
+ }
450
+ /**
451
+ * 工具权限配置
452
+ */
453
+ interface ToolPermissionConfig {
454
+ /** 是否需要用户确认 */
455
+ requireConfirmation?: boolean;
456
+ /** 权限级别 */
457
+ level?: "safe" | "dangerous" | "critical";
458
+ }
459
+ /**
460
+ * 工具信息
461
+ */
462
+ interface ToolInfo<Parameters extends ZodType = ZodType> {
463
+ /** 工具名称 */
464
+ name: string;
465
+ /** 工具描述 */
466
+ description: string;
467
+ /** 参数 Schema */
468
+ parameters: Parameters;
469
+ /** 初始化函数(可选) */
470
+ init?: (ctx?: ToolContext) => Promise<void>;
471
+ /** 执行函数 */
472
+ execute: (args: z.infer<Parameters>, ctx: ToolContext) => Promise<ToolResult2>;
473
+ /** 参数验证错误格式化(可选) */
474
+ formatValidationError?: (error: ZodError) => string;
475
+ /** 沙箱配置 */
476
+ sandbox?: ToolSandboxConfig;
477
+ /** 权限配置 */
478
+ permission?: ToolPermissionConfig;
479
+ /** 工具元数据 */
480
+ metadata?: ToolMetadata;
481
+ }
482
+ /**
483
+ * 工具类型别名
484
+ */
485
+ type Tool = ToolInfo;
486
+ /**
487
+ * LLM Message
488
+ */
489
+ interface LLMMessage {
490
+ /** 消息角色 */
491
+ role: "system" | "user" | "assistant" | "tool";
492
+ /** 消息内容 */
493
+ content: string;
494
+ /** 工具调用(仅 assistant 角色) */
495
+ toolCalls?: ToolCall[];
496
+ /** 工具调用 ID(仅 tool 角色) */
497
+ toolCallId?: string;
498
+ /** 名称(用于 function 调用) */
499
+ name?: string;
500
+ }
501
+ /**
502
+ * Tool Call (OpenAI format)
503
+ */
504
+ interface ToolCall {
505
+ /** 工具调用 ID */
506
+ id: string;
507
+ /** 工具名称 */
508
+ name: string;
509
+ /** 工具参数(JSON 字符串) */
510
+ arguments: string;
511
+ /** OpenAI 格式的 function 对象 */
512
+ function?: {
513
+ /** 函数名称 */
514
+ name: string;
515
+ /** 函数参数(JSON 字符串) */
516
+ arguments: string;
517
+ };
518
+ }
519
+ /**
520
+ * Usage Information
521
+ */
522
+ interface UsageInfo {
523
+ /** Prompt Token 数 */
524
+ promptTokens: number;
525
+ /** Completion Token 数 */
526
+ completionTokens: number;
527
+ /** 总 Token 数 */
528
+ totalTokens: number;
529
+ }
530
+ /**
531
+ * LLM Output
532
+ */
533
+ interface LLMOutput {
534
+ /** 生成的内容 */
535
+ content: string;
536
+ /** 推理/思考内容(支持 reasoning 模型的输出) */
537
+ reasoning?: string;
538
+ /** 完成原因 */
539
+ finishReason: "stop" | "length" | "content-filter" | "tool-calls" | "function-call";
540
+ /** 工具调用 */
541
+ toolCalls?: ToolCall[];
542
+ /** Usage 信息 */
543
+ usage?: UsageInfo;
544
+ /** 生成模型 */
545
+ model?: string;
546
+ }
547
+ /**
548
+ * 模型限制配置
549
+ */
550
+ interface ModelLimits {
551
+ /** 上下文窗口大小(Token 数) */
552
+ contextWindow: number;
553
+ /** 压缩阈值(0-1),超过此比例触发上下文压缩 */
554
+ compactionThreshold?: number;
555
+ /** 最大输出 Token 数 */
556
+ maxOutputTokens?: number;
557
+ }
558
+ /**
559
+ * LLM Invoke 请求
560
+ */
561
+ interface LLMInvokeRequest {
562
+ /** 消息列表 */
563
+ messages: LLMMessage[];
564
+ /** Provider ID(可选,默认使用配置的默认 Provider) */
565
+ providerId?: string;
566
+ /** 模型(可选,默认使用配置的默认模型) */
567
+ model?: string;
568
+ /** 工具列表 */
569
+ tools?: ToolInfo2[];
570
+ /** 温度参数 */
571
+ temperature?: number;
572
+ /** 最大 Token 数 */
573
+ maxTokens?: number;
574
+ /** Top P */
575
+ topP?: number;
576
+ /** 工具选择策略 */
577
+ toolChoice?: "auto" | "none" | {
578
+ type: "function";
579
+ function: {
580
+ name: string;
581
+ };
582
+ };
583
+ /** 停止序列 */
584
+ stopSequences?: string[];
585
+ /** Abort Signal */
586
+ abortSignal?: AbortSignal;
587
+ /** 调用上下文(用于事件元数据) */
588
+ context?: LLMInvokeContext;
589
+ /** 跳过上下文阈值检查(用于 SummaryAgent 等内部调用,避免循环触发压缩) */
590
+ skipThresholdCheck?: boolean;
591
+ }
592
+ /**
593
+ * LLM Invoke 结果
594
+ */
595
+ interface LLMInvokeResult {
596
+ /** LLM 输出 */
597
+ output: LLMOutput;
598
+ /** 使用的 Provider ID */
599
+ providerId: string;
600
+ /** 使用的模型 */
601
+ model: string;
602
+ /** 调用延迟(毫秒) */
603
+ latencyMs: number;
604
+ }
605
+ /**
606
+ * Tool Info(用于 LLM 工具调用)
607
+ */
608
+ interface ToolInfo2 {
609
+ /** 工具名称 */
610
+ name: string;
611
+ /** 工具描述 */
612
+ description?: string;
613
+ /** 工具参数 Schema */
614
+ parameters: Record<string, unknown>;
615
+ }
616
+ /**
617
+ * LLM 调用上下文(用于事件元数据)
618
+ */
619
+ interface LLMInvokeContext {
620
+ /** Session ID */
621
+ sessionId?: string;
622
+ /** Message ID */
623
+ messageId?: string;
624
+ }
625
+ type AbortSignalType = AbortSignal;
626
+ interface ProjectSummary {
627
+ projectPath: string;
628
+ projectName: string;
629
+ lastUpdated: number;
630
+ summary: string;
631
+ shortSummary: string;
632
+ draftCount: number;
633
+ memoryMdPath: string;
634
+ draftsDir: string;
635
+ detailsDir: string;
636
+ }
637
+ /**
638
+ * Agent 执行上下文
639
+ */
640
+ interface AgentContext {
641
+ /** 会话 ID */
642
+ sessionId?: string;
643
+ /** 消息 ID */
644
+ messageId?: string;
645
+ /** 中断信号 */
646
+ abort?: AbortSignalType;
647
+ /** 额外的上下文信息(会添加到 system prompt) */
648
+ additionInfo?: string;
649
+ /** 元数据 */
650
+ metadata?: Record<string, unknown>;
651
+ /** Agent 类型(用于选择 Behavior Spec) */
652
+ agentType?: string;
653
+ /** 使用的模型 */
654
+ model?: string;
655
+ /** 允许的工具列表(上下文级别,会覆盖 agent 配置) */
656
+ allowedTools?: string[];
657
+ /** 拒绝的工具列表(上下文级别,会覆盖 agent 配置) */
658
+ deniedTools?: string[];
659
+ /** 是否过滤 history 中的 tool 消息(上下文级别,会覆盖 agent 配置) */
660
+ filterHistory?: boolean;
661
+ }
662
+ /**
663
+ * 工具调用(内部使用)
664
+ */
665
+ interface ToolCall2 {
666
+ /** 调用 ID */
667
+ id: string;
668
+ /** 工具名称 */
669
+ name: string;
670
+ /** 调用参数 */
671
+ arguments: Record<string, unknown>;
672
+ }
673
+ /**
674
+ * 工具调用结果
675
+ */
676
+ interface ToolCallResult {
677
+ /** 调用 ID */
678
+ id: string;
679
+ /** 工具名称 */
680
+ name: string;
681
+ /** 调用结果 */
682
+ result: ToolResult2;
683
+ /** 是否成功 */
684
+ success: boolean;
685
+ }
686
+ /**
687
+ * Hook 点枚举(带 component 前缀)
688
+ */
689
+ 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";
690
+ /**
691
+ * Context data for agent:after.react hook
692
+ */
693
+ interface AgentReactContext {
694
+ /** Current messages for memory extraction */
695
+ messages: ModelMessage[];
696
+ /** Current session ID */
697
+ sessionId?: string;
698
+ /** Execution summary */
699
+ summary?: string;
700
+ }
701
+ /**
702
+ * Hook 上下文(Agent 特有版本)
703
+ *
704
+ * 注意:这是 AgentComponent 使用的 HookContext 扩展版本
705
+ * 包含 iteration, maxIterations, messages 等 Agent 特有字段
706
+ */
707
+ interface HookContext2 {
708
+ /** 当前 LLM 输出(after.llm hook 可用) */
709
+ llmOutput?: AgentLLMOutput;
710
+ /** 当前工具调用(before.tool/after.tool hook 可用) */
711
+ currentToolCall?: ToolCall2;
712
+ /** 工具执行结果(after.tool hook 可用) */
713
+ toolResult?: ToolCallResult;
714
+ /** 阈值信息(on.threshold hook 可用) */
715
+ threshold?: {
716
+ type: "tokens" | "turns" | "iterations" | "custom";
717
+ value: number;
718
+ limit?: number;
719
+ };
720
+ /** 当前错误(on.error hook 可用) */
721
+ error?: Error;
722
+ /** 当前 Agent 实例 */
723
+ agent: AgentInstance;
724
+ /** 当前迭代次数(可修改) */
725
+ iteration: number;
726
+ /** 最大迭代次数限制(before.llm hook 可用) */
727
+ maxIterations: number;
728
+ /** 消息列表(可直接修改) */
729
+ messages: ModelMessage[];
730
+ /** 可用工具列表(可直接修改) */
731
+ tools: Tool[];
732
+ /** 当前 system prompt(可直接修改) */
733
+ systemPrompt: string;
734
+ /** 额外的上下文信息(可直接修改) */
735
+ additionInfo?: string;
736
+ /** 执行上下文(可直接修改属性) */
737
+ context: AgentContext;
738
+ /** 是否已停止 */
739
+ _stopped?: boolean;
740
+ /** 停止原因 */
741
+ _stopReason?: string;
742
+ /** 待执行的干预动作 */
743
+ _pendingAction?: {
744
+ type: HookActionType2;
745
+ payload?: unknown;
746
+ };
747
+ /** 错误列表 */
748
+ _errors?: Array<{
749
+ plugin: string;
750
+ error: string;
751
+ }>;
752
+ /** Project Memory Agent 生成的摘要列表(由 Project Agent 写入,Global Agent 读取) */
753
+ projectSummaries?: ProjectSummary[];
754
+ }
755
+ /**
756
+ * Hook 干预动作
757
+ */
758
+ type HookActionType2 = "stop" | "retry" | "compress" | "extract_memory" | "inject_message" | "skip_tool";
759
+ /**
760
+ * Hook 返回结果
761
+ */
762
+ interface HookResult2 {
763
+ /** 是否继续执行(false = 停止 Agent) */
764
+ continue: boolean;
765
+ /** 干预动作(可选) */
766
+ action?: {
767
+ type: HookActionType2;
768
+ payload?: unknown;
769
+ };
770
+ }
771
+ /**
772
+ * Plugin 定义
773
+ */
774
+ interface Plugin {
775
+ /** Plugin 唯一名称 */
776
+ name: string;
777
+ /** Plugin 版本 */
778
+ version: string;
779
+ /** Plugin 描述 */
780
+ description?: string;
781
+ /** 订阅的 Hook 点及优先级 */
782
+ hooks: Array<{
783
+ point: HookPoint;
784
+ /** 优先级,默认 0,数字越大越先执行 */
785
+ priority?: number;
786
+ }>;
787
+ /** 执行函数 */
788
+ execute: (ctx: HookContext2) => HookResult2 | Promise<HookResult2>;
789
+ }
790
+ /**
791
+ * Agent 类型
792
+ */
793
+ type AgentType = "primary" | "sub" | "summary";
794
+ /**
795
+ * Agent 状态
796
+ */
797
+ type AgentStatus = "idle" | "running" | "paused" | "stopped" | "error";
798
+ /**
799
+ * Agent 实例配置
800
+ */
801
+ interface AgentInstanceConfig {
802
+ /** Agent 类型 */
803
+ type: AgentType;
804
+ /** Agent 名称 */
805
+ name?: string;
806
+ /** System Prompt */
807
+ systemPrompt?: string;
808
+ /** Behavior Spec ID */
809
+ behaviorSpecId?: string;
810
+ /** 使用的模型 */
811
+ model?: string;
812
+ /** 最大迭代次数 */
813
+ maxIterations?: number;
814
+ /** 最大错误重试次数 */
815
+ maxErrorRetries?: number;
816
+ /** 死循环检测阈值 */
817
+ doomLoopThreshold?: number;
818
+ /** 允许的工具列表 */
819
+ allowedTools?: string[];
820
+ /** 拒绝的工具列表 */
821
+ deniedTools?: string[];
822
+ /** 工具超时(毫秒) */
823
+ toolTimeout?: number;
824
+ /** 工具重试次数 */
825
+ toolRetries?: number;
826
+ /** 是否过滤 history 中的 tool 消息(默认过滤,避免上下文过长) */
827
+ filterHistory?: boolean;
828
+ }
829
+ /**
830
+ * Agent 实例
831
+ */
832
+ interface AgentInstance {
833
+ /** Agent 名称 */
834
+ name: string;
835
+ /** Agent 配置 */
836
+ config: Required<AgentInstanceConfig>;
837
+ /** 当前状态 */
838
+ status: AgentStatus;
839
+ /** 注册的 Plugins */
840
+ plugins: Map<string, Plugin>;
841
+ }
842
+ /**
843
+ * Agent 执行结果
844
+ */
845
+ interface AgentRunResult {
846
+ /** 最终文本响应 */
847
+ finalText?: string;
848
+ /** 是否被 hook 停止 */
849
+ stopped?: boolean;
850
+ /** 停止原因 */
851
+ stopReason?: string;
852
+ /** 执行的迭代次数 */
853
+ iterations: number;
854
+ /** 工具调用列表 */
855
+ toolCalls: ToolCall2[];
856
+ /** 错误信息(如有) */
857
+ error?: string;
858
+ }
859
+ /**
860
+ * LLM 调用结果
861
+ *
862
+ * 复用 LLMComponent 的 LLMOutput,并扩展 Agent 特有字段
863
+ */
864
+ interface LLMCallResult {
865
+ /** 成功 */
866
+ success: boolean;
867
+ /** 输出 */
868
+ output?: LLMOutput;
869
+ /** 错误信息 */
870
+ error?: string;
871
+ /** Usage 信息 */
872
+ usage?: {
873
+ promptTokens: number;
874
+ completionTokens: number;
875
+ totalTokens: number;
876
+ };
877
+ }
878
+ /**
879
+ * Agent 内部使用的 LLM 输出
880
+ * 独立定义以避免与 LLMOutput 的类型冲突
881
+ */
882
+ interface AgentLLMOutput {
883
+ /** 生成的内容 */
884
+ content: string;
885
+ /** 完成原因 */
886
+ finishReason: "stop" | "length" | "content-filter" | "tool-calls" | "function-call";
887
+ /** 工具调用(兼容格式) */
888
+ toolCalls?: Array<{
889
+ id: string;
890
+ function?: {
891
+ name: string;
892
+ arguments: string;
893
+ };
894
+ name?: string;
895
+ arguments?: string;
896
+ }>;
897
+ /** Usage 信息 */
898
+ usage?: {
899
+ promptTokens: number;
900
+ completionTokens: number;
901
+ totalTokens: number;
902
+ };
903
+ /** 生成模型 */
904
+ model?: string;
905
+ /** 推理内容(思考过程) */
906
+ reasoning?: string;
907
+ }
908
+ /**
909
+ * Summary Agent 配置
910
+ *
911
+ * 专门用于生成 session checkpoint 的 Agent
912
+ */
913
+ interface SummaryAgentConfig extends AgentInstanceConfig {
914
+ /** Agent 类型 */
915
+ type: "summary";
916
+ /** 使用的 prompt */
917
+ promptName: "session/compact";
918
+ /** 默认模型 */
919
+ model?: string;
920
+ /** 最大迭代次数(summary 任务只需 1 次) */
921
+ maxIterations: 1;
922
+ }
923
+ /**
924
+ * Summary Agent 运行选项
925
+ */
926
+ interface SummaryAgentRunOptions {
927
+ /** 消息列表 */
928
+ messages: Array<{
929
+ role: string;
930
+ content: string;
931
+ }>;
932
+ /** 用户提供的上下文/提示 */
933
+ userContext?: string;
934
+ /** 期望的输出格式 */
935
+ outputFormat: "json";
936
+ /**
937
+ * 场景化提示 (Scenario Hint)
938
+ *
939
+ * 用于指导 checkpoint 生成的重点方向。
940
+ * 当提供此参数时,会在系统提示中添加场景上下文,
941
+ * 帮助 LLM 生成更符合当前场景的 checkpoint。
942
+ */
943
+ scenarioHint?: string;
944
+ }
945
+ /**
946
+ * Summary Agent 运行结果
947
+ */
948
+ interface SummaryAgentResult {
949
+ /** Checkpoint 标题 */
950
+ title: string;
951
+ /** 过程要点 */
952
+ processKeyPoints: string[];
953
+ /** 当前状态 */
954
+ currentState: string;
955
+ /** 后续待跟进 */
956
+ nextSteps: string[];
957
+ /** 用户意图列表 - 从对话中提取的用户相关意图 */
958
+ userIntents: string[];
959
+ /** 原始 LLM 响应 */
960
+ rawResponse?: string;
961
+ }
962
+ /**
963
+ * Compact Hint 生成选项
964
+ *
965
+ * 用于两阶段自动压缩流程中的第一阶段:
966
+ * 根据会话历史生成场景化的压缩提示
967
+ */
968
+ interface CompactHintGenerationOptions {
969
+ /** 消息列表 */
970
+ messages: Array<{
971
+ role: string;
972
+ content: string;
973
+ }>;
974
+ /**
975
+ * 会话上下文信息(可选)
976
+ *
977
+ * 包含会话相关的额外信息,如:
978
+ * - 当前活跃任务信息
979
+ * - 工作目录
980
+ * - 最近的操作记录等
981
+ */
982
+ sessionContext?: {
983
+ activeTaskId?: string;
984
+ activeTaskTitle?: string;
985
+ workingDirectory?: string;
986
+ recentActions?: string[];
987
+ };
988
+ }
989
+ /**
990
+ * Compact Hint 生成结果
991
+ */
992
+ interface CompactHintGenerationResult {
993
+ /**
994
+ * 场景化压缩指导提示(自然语言)
995
+ *
996
+ * 这是一段"压缩指导 Prompt",用于告诉 Compact 阶段:
997
+ * "在当前这个场景/目标/任务下,生成 checkpoint 时应该重点关注什么"
998
+ *
999
+ * 示例:
1000
+ * "Feature development. User is implementing JWT authentication for auth-service.
1001
+ * Focus on: auth flow decisions, token refresh progress, middleware implementation."
1002
+ */
1003
+ hint: string;
1004
+ /** 原始 LLM 响应 */
1005
+ rawResponse?: string;
1006
+ }
1007
+ import { ZodType as ZodType2, ZodTypeDef } from "zod";
1008
+ /**
1009
+ * ConfigSource 类型
1010
+ */
1011
+ type ConfigSourceType = "memory" | "file" | "env" | "remote";
1012
+ /**
1013
+ * SourceRegistration - Source 注册配置
1014
+ */
1015
+ interface SourceRegistration {
1016
+ /** Source 类型 */
1017
+ type: ConfigSourceType;
1018
+ /** 相对路径(file 类型使用,基于 XDG_DATA_HOME) */
1019
+ relativePath?: string;
1020
+ /** 环境变量前缀(env 类型使用) */
1021
+ envPrefix?: string;
1022
+ /** 优先级,默认值:memory=0, file=10, env=20 */
1023
+ priority?: number;
1024
+ /** 是否可选(file 类型使用,文件不存在时不报错) */
1025
+ optional?: boolean;
1026
+ /** 是否启用文件监听(file 类型使用,默认 true) */
1027
+ watch?: boolean;
1028
+ }
1029
+ /**
1030
+ * KeyRegistration - 配置 Key 注册
1031
+ */
1032
+ interface KeyRegistration {
1033
+ /** 配置 Key(支持点号嵌套,如 "llm.provider") */
1034
+ key: string;
1035
+ /** 优先使用的 Source 列表(按优先级从低到高) */
1036
+ sources: ConfigSourceType[];
1037
+ /** 自定义优先级(覆盖默认优先级) */
1038
+ priority?: number;
1039
+ /** 默认值 */
1040
+ default?: unknown;
1041
+ }
1042
+ /**
1043
+ * ComponentRegistration - Component 注册配置
1044
+ */
1045
+ interface ComponentRegistration {
1046
+ /** Component 名称 */
1047
+ name: string;
1048
+ /** 声明需要的 Source */
1049
+ sources: SourceRegistration[];
1050
+ /** 声明需要的配置 Key */
1051
+ keys: KeyRegistration[];
1052
+ /** 配置 Schema(用于验证,可选) */
1053
+ schema?: ZodType2<unknown, ZodTypeDef, unknown>;
1054
+ /** 默认值 */
1055
+ defaults?: Record<string, unknown>;
1056
+ }
1057
+ /**
1058
+ * ConfigSource 接口
1059
+ */
1060
+ interface ConfigSource {
1061
+ readonly name: ConfigSourceType;
1062
+ readonly priority: number;
1063
+ read(key: string): unknown | undefined;
1064
+ write?(key: string, value: unknown): boolean;
1065
+ delete?(key: string): boolean;
1066
+ list(): Array<{
1067
+ key: string;
1068
+ value: unknown;
1069
+ }>;
1070
+ watch?(callback: (event: ConfigChangeEvent) => void): () => void;
1071
+ }
1072
+ /**
1073
+ * Config 变更事件
1074
+ */
1075
+ interface ConfigChangeEvent {
1076
+ type: "change" | "add" | "delete";
1077
+ key: string;
1078
+ oldValue: unknown;
1079
+ newValue: unknown;
1080
+ source: ConfigSourceType;
1081
+ timestamp: number;
1082
+ }
1083
+ /**
1084
+ * ConfigComponent 配置
1085
+ */
1086
+ interface ConfigComponentOptions {
1087
+ /** 持久化文件路径 */
1088
+ persistFile?: string;
1089
+ /** 防抖延迟(ms) */
1090
+ debounceMs?: number;
1091
+ /** 最大批量大小 */
1092
+ maxBatchSize?: number;
1093
+ }
1094
+ /**
1095
+ * ConfigComponent
1096
+ */
1097
+ declare class ConfigComponent extends BaseComponent {
1098
+ readonly name = "config";
1099
+ readonly version = "1.0.0";
1100
+ private memorySource;
1101
+ private sources;
1102
+ private componentSchemas;
1103
+ private componentPaths;
1104
+ private watchers;
1105
+ /** Source watch unsubscribe 函数 */
1106
+ private sourceUnwatchFns;
1107
+ private persistQueue;
1108
+ private persistFile?;
1109
+ /** Key 注册表:key -> KeyRegistration */
1110
+ private keyRegistry;
1111
+ /** Component 注册表:name -> ComponentRegistration */
1112
+ private componentRegistrations;
1113
+ /** 协议解析器 */
1114
+ private protocolResolver?;
1115
+ /** XDG_DATA_HOME 缓存(用于测试注入) */
1116
+ private __xdgDataHome?;
1117
+ /** Source 注册配置(延迟创建) */
1118
+ private pendingSources;
1119
+ /** Source 唯一标识缓存:type:relativePath -> true */
1120
+ private sourceKeys;
1121
+ /**
1122
+ * 获取 XDG_DATA_HOME(动态获取,支持运行时覆盖)
1123
+ */
1124
+ getXdgDataHome(): string;
1125
+ /**
1126
+ * 设置 XDG_DATA_HOME(用于测试)
1127
+ */
1128
+ setXdgDataHome(path: string): void;
1129
+ constructor(options?: ConfigComponentOptions);
1130
+ /**
1131
+ * 注册 Component Schema
1132
+ */
1133
+ register(schema: ComponentSchema): void;
1134
+ /**
1135
+ * 注销 Component
1136
+ */
1137
+ unregister(name: string): boolean;
1138
+ /**
1139
+ * 检查是否已注册
1140
+ */
1141
+ isRegistered(name: string): boolean;
1142
+ /**
1143
+ * 列出所有注册的 Component
1144
+ */
1145
+ listComponents(): string[];
1146
+ /**
1147
+ * 获取配置值(支持 xxx.yyy.zzz 嵌套访问)
1148
+ * 支持两种模式:
1149
+ * 1. 完整 key:直接查找,如 "component.key" -> 读取 "component.key"
1150
+ * 2. 嵌套对象:查找 component 对象后从中获取属性,如 "component.nested.deep"
1151
+ * -> 先找 "component.nested",再获取其 "deep" 属性
1152
+ */
1153
+ get(key: string): unknown;
1154
+ /**
1155
+ * 批量获取
1156
+ */
1157
+ getMany(keys: string[]): Record<string, unknown>;
1158
+ /**
1159
+ * 设置配置值(内存级别 + 异步持久化)
1160
+ */
1161
+ set(key: string, value: unknown): Promise<void>;
1162
+ /**
1163
+ * 批量设置
1164
+ */
1165
+ setMany(config: Record<string, unknown>): Promise<void>;
1166
+ /**
1167
+ * 获取 Component 的路径
1168
+ */
1169
+ getPath(component: string, subPath?: string): string;
1170
+ /**
1171
+ * 手动持久化
1172
+ */
1173
+ save(key?: string): Promise<void>;
1174
+ /**
1175
+ * 重置到默认值
1176
+ */
1177
+ reset(key: string): Promise<void>;
1178
+ /**
1179
+ * 监听配置变更
1180
+ */
1181
+ watch(pattern: string, callback: ComponentWatchCallback): () => void;
1182
+ /**
1183
+ * 添加配置源
1184
+ */
1185
+ addSource(source: ConfigSource): void;
1186
+ /**
1187
+ * 获取 Source 唯一标识
1188
+ */
1189
+ private getSourceKey;
1190
+ /**
1191
+ * 移除配置源
1192
+ */
1193
+ removeSource(name: ConfigSourceType): boolean;
1194
+ /**
1195
+ * 关闭所有配置源的 watcher(用于优雅退出)
1196
+ */
1197
+ unwatchAll(): void;
1198
+ /**
1199
+ * 获取配置源列表
1200
+ */
1201
+ getSources(): ConfigSource[];
1202
+ private getNestedValue;
1203
+ private notifyWatchers;
1204
+ private persistToFile;
1205
+ /**
1206
+ * 注册配置 Source
1207
+ *
1208
+ * Source 会在首次 load() 时被创建
1209
+ *
1210
+ * @example
1211
+ * config.registerSource({ type: "file", relativePath: "llm-config.jsonc" });
1212
+ * config.registerSource({ type: "env", envPrefix: "LLM" });
1213
+ */
1214
+ registerSource(registration: SourceRegistration): void;
1215
+ /**
1216
+ * 创建 Source 实例
1217
+ */
1218
+ private createSource;
1219
+ /**
1220
+ * 确保所有待创建的 Source 已被创建
1221
+ */
1222
+ private ensureSourcesCreated;
1223
+ /**
1224
+ * 注册配置 Key
1225
+ *
1226
+ * @example
1227
+ * config.registerKeys([
1228
+ * { key: "llm.provider", sources: ["file", "memory"], default: "openai" },
1229
+ * { key: "llm.model", sources: ["file"] },
1230
+ * ]);
1231
+ */
1232
+ registerKeys(keys: KeyRegistration[]): void;
1233
+ /**
1234
+ * 注册 Component(一次性注册 Source 和 Keys)
1235
+ *
1236
+ * @example
1237
+ * config.registerComponent({
1238
+ * name: "llm",
1239
+ * sources: [
1240
+ * { type: "file", relativePath: "llm-config.jsonc" },
1241
+ * { type: "env", envPrefix: "LLM" },
1242
+ * ],
1243
+ * keys: [
1244
+ * { key: "llm.provider", sources: ["file", "env", "memory"] },
1245
+ * ],
1246
+ * });
1247
+ */
1248
+ registerComponent(registration: ComponentRegistration): void;
1249
+ /**
1250
+ * 按需加载配置
1251
+ *
1252
+ * 从注册的 Source 加载已注册 Keys 的配置值到内存中
1253
+ *
1254
+ * @param componentName - 可选,指定要加载的 Component 名称
1255
+ *
1256
+ * @example
1257
+ * // 加载所有已注册的 Keys
1258
+ * await config.load();
1259
+ *
1260
+ * // 只加载指定 Component 的 Keys
1261
+ * await config.load("llm");
1262
+ */
1263
+ load(componentName?: string): Promise<void>;
1264
+ /**
1265
+ * 获取已注册的 Source 列表
1266
+ */
1267
+ getRegisteredSources(): ConfigSource[];
1268
+ }
1269
+ /**
1270
+ * 服务配置接口
1271
+ */
1272
+ interface ServiceConfig {
1273
+ /** 环境配置 */
1274
+ environment?: {
1275
+ name?: string;
1276
+ version?: string;
1277
+ };
1278
+ /** 组件配置映射 */
1279
+ components?: Record<string, ComponentConfigEntry>;
1280
+ }
1281
+ /**
1282
+ * 组件配置项
1283
+ */
1284
+ interface ComponentConfigEntry {
1285
+ /** 配置文件路径(可选) */
1286
+ configPath?: string;
1287
+ /** 环境变量前缀(可选) */
1288
+ envPrefix?: string;
1289
+ /** 直接配置对象(可选) */
1290
+ config?: Record<string, unknown>;
1291
+ /** 是否启用(可选,默认 true) */
1292
+ enabled?: boolean;
1293
+ }
1294
+ /**
1295
+ * 组件初始化选项(由 generateComponentOptions 生成)
1296
+ */
1297
+ interface GeneratedComponentOptions {
1298
+ /** ConfigComponent 实例 */
1299
+ configComponent: ConfigComponent;
1300
+ /** 配置文件路径 */
1301
+ configPath?: string;
1302
+ /** 环境变量前缀 */
1303
+ envPrefix?: string;
1304
+ /** 直接配置对象 */
1305
+ config?: Record<string, unknown>;
1306
+ /** 允许的其他属性 */
1307
+ [key: string]: unknown;
1308
+ }
1309
+ /**
1310
+ * Environment 接口
1311
+ *
1312
+ * 定义 Agent 运行时的核心能力接口。
1313
+ */
1314
+ interface Environment {
1315
+ /** 环境名称 */
1316
+ readonly name: string;
1317
+ /** 环境版本 */
1318
+ readonly version: string;
1319
+ /** 获取环境配置 */
1320
+ getConfig(): EnvironmentConfig;
1321
+ /**
1322
+ * 注册组件
1323
+ */
1324
+ registerComponent(component: Component): void;
1325
+ /**
1326
+ * 注销组件
1327
+ */
1328
+ unregisterComponent(name: string): void;
1329
+ /**
1330
+ * 获取组件
1331
+ */
1332
+ getComponent<T extends Component>(name: string): T | undefined;
1333
+ /**
1334
+ * 列出所有组件
1335
+ */
1336
+ listComponents(): Component[];
1337
+ /**
1338
+ * 处理自然语言查询(核心入口)
1339
+ *
1340
+ * @param query 用户查询文本
1341
+ * @param context 执行上下文(可选)
1342
+ * @returns 完整的文本响应
1343
+ */
1344
+ handle_query(query: string, context?: AgentContext): Promise<string>;
1345
+ /**
1346
+ * 处理动作
1347
+ */
1348
+ handle_action(action: Action, context: Context): Promise<ToolResult>;
1349
+ /**
1350
+ * 订阅 EnvEvent(返回取消订阅函数)
1351
+ */
1352
+ subscribe(handler: EnvEventHandler): () => void;
1353
+ /**
1354
+ * 订阅指定类型事件
1355
+ */
1356
+ subscribeTo(eventType: string | string[], handler: EnvEventHandler): () => void;
1357
+ /**
1358
+ * 订阅所有事件(通配符)
1359
+ */
1360
+ subscribeAll(handler: EnvEventHandler): () => void;
1361
+ /**
1362
+ * 推送事件
1363
+ *
1364
+ * @param event EnvEvent 或 EnvEventInput(部分属性)
1365
+ */
1366
+ pushEnvEvent(event: EnvEvent | EnvEventInput): void;
1367
+ /**
1368
+ * 从配置文件加载服务配置
1369
+ *
1370
+ * @param configPath 配置文件路径(基于 XDG_DATA_HOME)
1371
+ * @returns 解析后的服务配置对象
1372
+ */
1373
+ loadServiceConfig(configPath: string): Promise<ServiceConfig>;
1374
+ /**
1375
+ * 生成组件初始化选项
1376
+ *
1377
+ * 根据组件配置项生成可用于初始化的选项对象。
1378
+ *
1379
+ * @param componentName 组件名称
1380
+ * @param configEntry 组件配置项
1381
+ * @returns 生成的组件初始化选项
1382
+ */
1383
+ generateComponentOptions(componentName: string, configEntry: ComponentConfigEntry): GeneratedComponentOptions;
1384
+ /**
1385
+ * 注册组件并用配置初始化
1386
+ *
1387
+ * 便捷方法:注册组件并通过 ConfigComponent 初始化。
1388
+ *
1389
+ * @param component 要注册的组件实例
1390
+ * @param configEntry 组件配置项
1391
+ */
1392
+ registerComponentWithConfig(component: Component, configEntry: ComponentConfigEntry): Promise<void>;
1393
+ /**
1394
+ * 从配置文件初始化整个环境
1395
+ *
1396
+ * 加载服务配置,注册并初始化所有配置的组件。
1397
+ *
1398
+ * @param configPath 配置文件路径(基于 XDG_DATA_HOME)
1399
+ */
1400
+ initFromConfig(configPath: string): Promise<void>;
1401
+ }
1402
+ /**
1403
+ * Component 状态
1404
+ */
1405
+ type ComponentStatus = "created" | "initializing" | "running" | "stopping" | "stopped" | "error";
1406
+ /**
1407
+ * Component 配置
1408
+ */
1409
+ interface ComponentConfig {
1410
+ name: string;
1411
+ version: string;
1412
+ enabled: boolean;
1413
+ options?: Record<string, unknown>;
1414
+ /**
1415
+ * Environment 实例(可选)
1416
+ *
1417
+ * 推荐在 init 时接收 env 实例,以便:
1418
+ * - 通过 env.getComponent() 获取其他组件
1419
+ * - 通过 env.pushEnvEvent() 发布事件
1420
+ * - 通过 env.handle_query() 处理查询
1421
+ *
1422
+ * 如果未提供,部分 Component 仍可正常工作(如测试场景)
1423
+ *
1424
+ * @example
1425
+ * ```typescript
1426
+ * async onInit(): Promise<void> {
1427
+ * if (this.env) {
1428
+ * const configComp = this.env.getComponent("config");
1429
+ * }
1430
+ * }
1431
+ * ```
1432
+ */
1433
+ env?: Environment;
1434
+ }
1435
+ /**
1436
+ * Component 路径声明
1437
+ */
1438
+ interface ComponentPaths {
1439
+ base: "config" | "state" | "data" | "cache";
1440
+ subPath: string;
1441
+ isDirectory: boolean;
1442
+ }
1443
+ /**
1444
+ * Component Schema(注册配置)
1445
+ */
1446
+ interface ComponentSchema {
1447
+ /** Component 名称 */
1448
+ name: string;
1449
+ /** 配置 Schema(Zod) */
1450
+ schema: ZodType3<unknown, ZodTypeDef2, unknown>;
1451
+ /** 默认值(从 Schema 提取) */
1452
+ defaults: Record<string, unknown>;
1453
+ /** 路径声明 */
1454
+ paths?: Record<string, ComponentPaths>;
1455
+ /** 元信息 */
1456
+ metadata?: {
1457
+ description?: string;
1458
+ version?: string;
1459
+ author?: string;
1460
+ };
1461
+ }
1462
+ /**
1463
+ * Component 配置变更事件
1464
+ */
1465
+ interface ComponentConfigChangeEvent {
1466
+ /** Component 名称 */
1467
+ component: string;
1468
+ /** 变更的配置键 */
1469
+ key: string;
1470
+ /** 旧值 */
1471
+ oldValue: unknown;
1472
+ /** 新值 */
1473
+ newValue: unknown;
1474
+ /** 是否持久化 */
1475
+ persisted: boolean;
1476
+ /** 时间戳 */
1477
+ timestamp: number;
1478
+ }
1479
+ /**
1480
+ * Component 配置监听回调
1481
+ */
1482
+ type ComponentWatchCallback = (event: ComponentConfigChangeEvent) => void | Promise<void>;
1483
+ /**
1484
+ * Component 基类接口
1485
+ */
1486
+ interface Component {
1487
+ /** Component 名称 */
1488
+ readonly name: string;
1489
+ /** Component 版本 */
1490
+ readonly version: string;
1491
+ /** 获取状态 */
1492
+ getStatus(): ComponentStatus;
1493
+ /** 获取配置 */
1494
+ getConfig(): ComponentConfig;
1495
+ /** 初始化 */
1496
+ init(config: ComponentConfig): Promise<void>;
1497
+ /** 启动 */
1498
+ start(): Promise<void>;
1499
+ /** 停止 */
1500
+ stop(): Promise<void>;
1501
+ }
1502
+ /**
1503
+ * Component 基类实现
1504
+ */
1505
+ declare abstract class BaseComponent implements Component {
1506
+ abstract readonly name: string;
1507
+ abstract readonly version: string;
1508
+ protected _status: ComponentStatus;
1509
+ protected _enabled: boolean;
1510
+ protected env: Environment;
1511
+ /** Hook 管理器 - 子类初始化时由构造函数设置 */
1512
+ readonly hookManager: HookManager;
1513
+ constructor();
1514
+ getStatus(): ComponentStatus;
1515
+ getConfig(): ComponentConfig;
1516
+ /**
1517
+ * 检查 Environment 是否已初始化
1518
+ */
1519
+ protected isEnvInitialized(): boolean;
1520
+ protected setStatus(status: ComponentStatus): void;
1521
+ /**
1522
+ * 获取 Environment 实例
1523
+ */
1524
+ protected getEnv(): Environment;
1525
+ init(config?: ComponentConfig): Promise<void>;
1526
+ start(): Promise<void>;
1527
+ stop(): Promise<void>;
1528
+ /**
1529
+ * 子类可覆盖的钩子方法
1530
+ */
1531
+ protected onInit(): Promise<void>;
1532
+ protected onStart(): Promise<void>;
1533
+ protected onStop(): Promise<void>;
1534
+ /**
1535
+ * 注册 Hook
1536
+ *
1537
+ * @param hookPoint Hook 点名称
1538
+ * @param hook Hook 实例
1539
+ */
1540
+ protected registerHook<T = unknown>(hookPoint: string, hook: Hook<T>): void;
1541
+ /**
1542
+ * 便捷方法:快速注册 Hook
1543
+ */
1544
+ protected addHook<T = unknown>(hookPoint: string, name: string, fn: HookFn<T>, priority?: number): void;
1545
+ /**
1546
+ * 便捷方法:按名称取消注册 Hook
1547
+ */
1548
+ protected removeHook(hookPoint: string, name: string): boolean;
1549
+ /**
1550
+ * 执行 Hook
1551
+ */
1552
+ protected executeHooks<T = unknown>(hookPoint: string, data: T, metadata?: Record<string, unknown>): Promise<void>;
1553
+ /**
1554
+ * 获取 ConfigComponent 实例
1555
+ *
1556
+ * 子类可使用此方法获取配置组件,支持热更新
1557
+ */
1558
+ protected getConfigComponent(): {
1559
+ get(key: string): unknown;
1560
+ } | undefined;
1561
+ /**
1562
+ * 动态获取运行时配置值
1563
+ *
1564
+ * 优先从 ConfigComponent 获取(支持热更新),否则返回默认值
1565
+ * 适用于运行时可能变化的配置(如 API Keys、模型参数)
1566
+ *
1567
+ * @param key 配置键(支持点号分隔的路径,如 "llm.temperature")
1568
+ * @param defaultValue 默认值
1569
+ * @returns 配置值或默认值
1570
+ *
1571
+ * @example
1572
+ * ```typescript
1573
+ * // 获取 LLM 温度配置(可能支持热更新)
1574
+ * const temperature = this.getRuntimeConfig("llm.temperature", 0.7);
1575
+ *
1576
+ * // 获取 API Key
1577
+ * const apiKey = this.getRuntimeConfig<string>("llm.providers.openai.apiKey");
1578
+ * ```
1579
+ */
1580
+ protected getRuntimeConfig<T>(key: string, defaultValue: T): T;
1581
+ }
1582
+ import { ModelMessage as ModelMessage2 } from "ai";
1583
+ import { z as z2 } from "zod";
1584
+ /**
1585
+ * AgentComponent 配置 Schema
1586
+ */
1587
+ declare const AgentComponentConfigSchema: unknown;
1588
+ type AgentComponentConfig = z2.infer<typeof AgentComponentConfigSchema>;
1589
+ /**
1590
+ * AgentComponent
1591
+ *
1592
+ * 提供 Agent 执行能力和 Hook 扩展机制
1593
+ */
1594
+ declare class AgentComponent extends BaseComponent {
1595
+ readonly name = "agent";
1596
+ readonly version = "1.0.0";
1597
+ private agents;
1598
+ private config;
1599
+ private llmComponent;
1600
+ private toolComponent;
1601
+ private aborted;
1602
+ private abortControllers;
1603
+ private defaultTools;
1604
+ private doomLoopCaches;
1605
+ /** ConfigComponent 用于配置管理 */
1606
+ private configComponent?;
1607
+ /** 配置变更 watcher 清理函数 */
1608
+ private configWatcher?;
1609
+ /** Session 消息转换器 */
1610
+ private messageConverter;
1611
+ /** 构造函数传入的配置(最高优先级) */
1612
+ private _constructorConfig?;
1613
+ /** Run ID 计数器(用于生成唯一的 runId) */
1614
+ private runCounter;
1615
+ constructor(options?: {
1616
+ config?: Partial<AgentComponentConfig>;
1617
+ });
1618
+ /**
1619
+ * 初始化组件
1620
+ *
1621
+ * 配置加载优先级(从高到低):
1622
+ * 1. Object - 直接传入的 config 对象
1623
+ * 2. Env - 环境变量(通过 envPrefix 配置前缀)
1624
+ * 3. File - 配置文件(通过 configPath 指定)
1625
+ */
1626
+ init(config: ComponentConfig): Promise<void>;
1627
+ /**
1628
+ * 注册配置到 ConfigComponent
1629
+ *
1630
+ * 配置加载顺序(优先级从低到高):
1631
+ * 1. Defaults - 默认值(最低)
1632
+ * 2. FileSource - 从配置文件加载
1633
+ * 3. EnvSource - 从环境变量加载
1634
+ * 4. config 对象 - 直接传入的配置(最高)
1635
+ */
1636
+ private registerConfig;
1637
+ /**
1638
+ * 将配置对象展平为点号路径
1639
+ */
1640
+ private flattenConfig;
1641
+ /**
1642
+ * 注册配置热更新监听
1643
+ */
1644
+ private registerConfigWatcher;
1645
+ /**
1646
+ * 处理配置变更
1647
+ */
1648
+ protected onConfigChange(event: {
1649
+ key: string;
1650
+ oldValue?: unknown;
1651
+ newValue?: unknown;
1652
+ }): void;
1653
+ /**
1654
+ * 组件停止时的清理逻辑
1655
+ *
1656
+ * 清理内容:
1657
+ * 1. 终止所有正在运行的 Agent(调用 abort() 以中断 LLM 调用)
1658
+ * 2. 清空 AbortController 注册表
1659
+ * 3. 清空 Doom Loop 缓存
1660
+ * 4. 取消配置变更监听
1661
+ * 5. 清空 Agent 注册表
1662
+ */
1663
+ protected onStop(): Promise<void>;
1664
+ /**
1665
+ * 刷新依赖组件引用
1666
+ *
1667
+ * 当组件被添加到 Environment 后调用,以确保获取到最新的组件引用
1668
+ */
1669
+ refreshDependencies(): void;
1670
+ /**
1671
+ * 获取 Agent 实例
1672
+ */
1673
+ getAgent(agentName: string): AgentInstance | undefined;
1674
+ /**
1675
+ * 获取所有 Agent 实例
1676
+ */
1677
+ listAgents(): AgentInstance[];
1678
+ /**
1679
+ * Add placeholder tool results for remaining unprocessed tool calls
1680
+ *
1681
+ * This ensures that the message history maintains a 1:1 correspondence
1682
+ * between tool-calls and tool-results, which is required by AI SDK v6.
1683
+ *
1684
+ * @param ctx - Hook context containing messages
1685
+ * @param allToolCalls - All tool calls from the LLM response
1686
+ * @param processedCount - Number of tool calls that have been processed
1687
+ * @param reason - Reason for the abort/interruption
1688
+ */
1689
+ private addRemainingToolResults;
1690
+ /**
1691
+ * 注册 Agent
1692
+ *
1693
+ * 配置优先级(从高到低):
1694
+ * 1. registerAgent 传入的 config(来自 PromptComponent)
1695
+ * 2. defaultAgent 配置(来自 ConfigComponent,作为 fallback)
1696
+ */
1697
+ registerAgent(name: string, config: Partial<AgentInstanceConfig>): AgentInstance;
1698
+ /**
1699
+ * 注销 Agent
1700
+ */
1701
+ unregisterAgent(name: string): boolean;
1702
+ /**
1703
+ * 注册 Plugin 到指定 Agent
1704
+ */
1705
+ registerPlugin(agentName: string, plugin: Plugin): void;
1706
+ /**
1707
+ * 注销 Plugin
1708
+ */
1709
+ unregisterPlugin(agentName: string, pluginName: string): boolean;
1710
+ /**
1711
+ * 设置默认工具列表
1712
+ */
1713
+ setDefaultTools(tools: Tool[]): void;
1714
+ /**
1715
+ * 获取可用工具
1716
+ */
1717
+ getAvailableTools(agent: AgentInstance, context?: AgentContext): Tool[];
1718
+ /**
1719
+ * 执行 Agent
1720
+ *
1721
+ * @param agentName Agent 名称
1722
+ * @param query 用户查询
1723
+ * @param context 执行上下文
1724
+ * @returns Agent 执行结果
1725
+ */
1726
+ run(agentName: string, query: string, context?: AgentContext): Promise<AgentRunResult>;
1727
+ private pushMessage;
1728
+ /**
1729
+ * 执行 Agent(核心方法,内部实现)
1730
+ */
1731
+ private _run;
1732
+ /**
1733
+ * 中止 Agent
1734
+ *
1735
+ * 调用此方法会:
1736
+ * 1. 设置 aborted 标志(用于 ReAct 循环检查)
1737
+ * 2. 触发 AbortController.abort()(用于中断正在进行的 LLM 调用)
1738
+ *
1739
+ * 注意:不直接修改 agent.status,由 _run() 的 finally 统一处理
1740
+ */
1741
+ abort(agentName: string): void;
1742
+ /**
1743
+ * 构建消息列表
1744
+ *
1745
+ * 注意:ctx.messages 已经包含了历史消息(包括 system prompt 和 user query)
1746
+ * 这里只需要构建当前请求的消息,不重复添加
1747
+ */
1748
+ private buildMessages;
1749
+ /**
1750
+ * 调用 LLM
1751
+ *
1752
+ * 流式事件由 LLMComponent 通过 env.pushEnvEvent() 发布
1753
+ */
1754
+ private invokeLLM;
1755
+ /**
1756
+ * 执行工具
1757
+ */
1758
+ private executeTool;
1759
+ /**
1760
+ * 执行 Plugin Hooks (Agent 特有的 Hook 执行机制)
1761
+ */
1762
+ private executePluginHooks;
1763
+ /**
1764
+ * 处理 Hook 动作
1765
+ */
1766
+ private handleHookAction;
1767
+ /**
1768
+ * 通知消息添加(扩展点)
1769
+ */
1770
+ protected notifyMessageAdded(message: ModelMessage2): void;
1771
+ /**
1772
+ * 结束结果
1773
+ */
1774
+ private finalizeResult;
1775
+ /**
1776
+ * Record messages to session
1777
+ *
1778
+ * 存储策略:
1779
+ * 1. 存储 allMessages 中的所有 user、assistant、tool 消息
1780
+ * 2. 跳过 system 消息
1781
+ * 3. 跳过空内容的 assistant/tool 消息
1782
+ */
1783
+ private recordSessionMessages;
1784
+ /**
1785
+ * 检查消息内容是否为空
1786
+ *
1787
+ * 空内容定义:
1788
+ * - 空字符串 ""
1789
+ * - 空数组 []
1790
+ * - null
1791
+ * - undefined
1792
+ *
1793
+ * @param content - 消息内容
1794
+ * @returns 是否为空
1795
+ */
1796
+ private isEmptyMessage;
1797
+ /**
1798
+ * 通过 Environment 发送事件
1799
+ */
1800
+ private pushEnvEvent;
1801
+ /**
1802
+ * 获取会话对话历史
1803
+ *
1804
+ * 核心逻辑:
1805
+ * 1. 使用 sessionComponent.getContext() 获取消息(自动从 checkpoint 开始)
1806
+ * 2. 根据 messageLimit 限制消息数量(向后兼容 minUserMessages)
1807
+ * 3. 过滤掉 tool_call/tool_result 消息(如果 filterHistory=true)
1808
+ *
1809
+ * 关键改进:利用 checkpoint 概念,避免重复获取已压缩的历史消息,
1810
+ * 从而减少重复触发压缩的问题。
1811
+ *
1812
+ * @param sessionComponent - SessionComponent 实例
1813
+ * @param sessionId - 会话 ID
1814
+ * @param options - 配置选项
1815
+ * @param options.minUserMessages - 最少包含的 user 消息数量(默认 100)
1816
+ * @param options.filterHistory - 是否过滤 tool 消息(默认 true)
1817
+ * @returns 过滤后的会话消息列表
1818
+ */
1819
+ private getConversationHistory;
1820
+ }
1821
+ /**
1822
+ * Prompt 来源类型
1823
+ */
1824
+ type PromptSource = "built-in" | "file" | "directory" | "inline";
1825
+ /**
1826
+ * Prompt 条目
1827
+ */
1828
+ interface PromptEntry {
1829
+ /** 唯一名称 */
1830
+ name: string;
1831
+ /** Prompt 内容 */
1832
+ content: string;
1833
+ /** 来源 */
1834
+ source: PromptSource;
1835
+ /** 原始文件路径 */
1836
+ filePath?: string;
1837
+ /** 是否可覆盖(内置 prompt 不可覆盖) */
1838
+ overridable: boolean;
1839
+ /** 加载时间 */
1840
+ loadedAt: number;
1841
+ }
1842
+ /**
1843
+ * Prompt Hook 上下文(用于统一 Hook 系统)
1844
+ */
1845
+ interface PromptHookContext {
1846
+ /** Prompt 名称 */
1847
+ name: string;
1848
+ /** 原始内容 */
1849
+ originalContent: string;
1850
+ /** 渲染后的内容(after hook 可修改) */
1851
+ renderedContent: string;
1852
+ /** 渲染变量 */
1853
+ variables: Record<string, string>;
1854
+ }
1855
+ /**
1856
+ * PromptComponent
1857
+ *
1858
+ * 管理提示词存储、加载和渲染
1859
+ * 配置通过 ConfigComponent 实时获取,支持配置热更新
1860
+ */
1861
+ declare class PromptComponent extends BaseComponent {
1862
+ readonly name = "prompt";
1863
+ readonly version = "1.0.0";
1864
+ private prompts;
1865
+ private config?;
1866
+ private configComponent?;
1867
+ private renderer;
1868
+ /** 配置变更 watcher 清理函数 */
1869
+ private configWatcher?;
1870
+ constructor();
1871
+ /**
1872
+ * 初始化组件
1873
+ *
1874
+ * 配置加载优先级(从高到低):
1875
+ * 1. Object - 直接传入的 config 对象
1876
+ * 2. Env - 环境变量(通过 envPrefix 配置前缀)
1877
+ * 3. File - 配置文件(通过 configPath 指定)
1878
+ */
1879
+ init(config: ComponentConfig): Promise<void>;
1880
+ /**
1881
+ * 注册配置到 ConfigComponent
1882
+ *
1883
+ * 配置加载顺序(优先级从低到高):
1884
+ * 1. Env - 环境变量(通过 load 加载 + 直接读取)
1885
+ * 2. Defaults - 默认值(在环境变量检查之后设置)
1886
+ * 3. Config Object - 直接配置对象(最高优先级)
1887
+ */
1888
+ private registerConfig;
1889
+ /**
1890
+ * 将配置对象展平为点号路径
1891
+ */
1892
+ private flattenConfig;
1893
+ /**
1894
+ * 注册配置热更新监听
1895
+ */
1896
+ private registerConfigWatcher;
1897
+ /**
1898
+ * 处理配置变更
1899
+ */
1900
+ private onConfigChange;
1901
+ private initRenderer;
1902
+ onStart(): Promise<void>;
1903
+ onStop(): Promise<void>;
1904
+ private getPromptConfig;
1905
+ /**
1906
+ * 添加 prompt
1907
+ */
1908
+ add(name: string, content: string, source?: PromptEntry["source"]): void;
1909
+ /**
1910
+ * 获取 prompt 原始内容
1911
+ */
1912
+ get(name: string): string | undefined;
1913
+ /**
1914
+ * 获取 prompt 条目
1915
+ */
1916
+ getEntry(name: string): PromptEntry | undefined;
1917
+ /**
1918
+ * 检查 prompt 是否存在
1919
+ */
1920
+ has(name: string): boolean;
1921
+ /**
1922
+ * 删除 prompt
1923
+ */
1924
+ delete(name: string): boolean;
1925
+ /**
1926
+ * 列出所有 prompt 名称
1927
+ */
1928
+ list(): string[];
1929
+ /**
1930
+ * 获取数量
1931
+ */
1932
+ size(): number;
1933
+ /**
1934
+ * 获取并渲染 prompt
1935
+ */
1936
+ getPrompt(name: string, variables?: Record<string, string>): Promise<string>;
1937
+ /**
1938
+ * 渲染内容(异步方法)
1939
+ */
1940
+ render(content: string, variables?: Record<string, string>, context?: Partial<PromptHookContext>): Promise<string>;
1941
+ /**
1942
+ * 提取内容中的变量
1943
+ */
1944
+ extractVariables(content: string): string[];
1945
+ /**
1946
+ * 加载所有 prompts(内置 + 外部)
1947
+ */
1948
+ private loadPrompts;
1949
+ /**
1950
+ * 加载内置 prompts
1951
+ *
1952
+ * 使用嵌入的 prompts(在构建时打包到 bundle 中)
1953
+ */
1954
+ private loadBuiltInPrompts;
1955
+ /**
1956
+ * 加载外部 prompts
1957
+ */
1958
+ private loadExternalPrompts;
1959
+ /**
1960
+ * 从文件加载单个 prompt
1961
+ */
1962
+ loadFromFile(filePath: string, name?: string): Promise<boolean>;
1963
+ /**
1964
+ * 从目录加载多个 prompt
1965
+ */
1966
+ loadFromDirectory(directory: string, extension?: string, recursive?: boolean): Promise<number>;
1967
+ private findFiles;
1968
+ /**
1969
+ * 注册 Prompt Hook(公开便捷方法,供外部调用)
1970
+ *
1971
+ * 现在使用全局 hookManager,允许外部通过 globalHookManager 注册
1972
+ */
1973
+ registerPromptHook(hookPoint: string, name: string, fn: (ctx: {
1974
+ data: PromptHookContext;
1975
+ phase: "before" | "after";
1976
+ }) => void | Promise<void>, priority?: number): void;
1977
+ /**
1978
+ * 取消注册 Prompt Hook
1979
+ */
1980
+ unregisterPromptHook(hookPoint: string, name: string): boolean;
1981
+ /**
1982
+ * 获取 Prompt Hook 点列表
1983
+ */
1984
+ getHookPoints(): string[];
1985
+ }
1986
+ /**
1987
+ * @fileoverview 统一错误类型体系
1988
+ *
1989
+ * 提供 AgentError, ToolError, LLMError, ComponentError, ContextError 等错误类型
1990
+ * 每个错误包含 code(错误码)和 details(额外信息)
1991
+ */
1992
+ /**
1993
+ * 错误码枚举
1994
+ */
1995
+ declare const ErrorCodes: {
1996
+ readonly AGENT_NOT_FOUND: "AGENT_001";
1997
+ readonly AGENT_RUN_FAILED: "AGENT_002";
1998
+ readonly AGENT_TIMEOUT: "AGENT_003";
1999
+ readonly AGENT_CONFIG_INVALID: "AGENT_004";
2000
+ readonly TOOL_NOT_FOUND: "TOOL_001";
2001
+ readonly TOOL_DISABLED: "TOOL_002";
2002
+ readonly TOOL_EXECUTION_FAILED: "TOOL_003";
2003
+ readonly TOOL_VALIDATION_FAILED: "TOOL_004";
2004
+ readonly LLM_PROVIDER_NOT_FOUND: "LLM_001";
2005
+ readonly LLM_INVOCATION_FAILED: "LLM_002";
2006
+ readonly LLM_TIMEOUT: "LLM_003";
2007
+ readonly LLM_CONFIG_INVALID: "LLM_004";
2008
+ readonly COMPONENT_NOT_FOUND: "COMP_001";
2009
+ readonly COMPONENT_INIT_FAILED: "COMP_002";
2010
+ readonly COMPONENT_NOT_RUNNING: "COMP_003";
2011
+ readonly CONFIG_NOT_FOUND: "CONFIG_001";
2012
+ readonly CONFIG_INVALID: "CONFIG_002";
2013
+ readonly CONFIG_SOURCE_ERROR: "CONFIG_003";
2014
+ readonly CONTEXT_THRESHOLD_EXCEEDED: "CTX_001";
2015
+ readonly CONTEXT_COMPACTION_FAILED: "CTX_002";
2016
+ readonly MCP_INIT_FAILED: "MCP_001";
2017
+ readonly MCP_SERVER_ERROR: "MCP_002";
2018
+ readonly MCP_TOOL_ERROR: "MCP_003";
2019
+ };
2020
+ type ErrorCode = typeof ErrorCodes[keyof typeof ErrorCodes];
2021
+ /**
2022
+ * RoyError 基类
2023
+ */
2024
+ declare class RoyError extends Error {
2025
+ readonly code: ErrorCode;
2026
+ readonly details?: unknown;
2027
+ constructor(message: string, code: ErrorCode, details?: unknown);
2028
+ }
2029
+ /**
2030
+ * LLM Output 结构(用于 ContextError 携带 assistant message)
2031
+ *
2032
+ * 这是简化版本,仅包含保存到 session 所需的最少字段。
2033
+ * 与 llm/types.ts 中的 LLMOutput 不同,那个版本包含完整的 LLM 调用结果。
2034
+ */
2035
+ interface LLMOutput2 {
2036
+ content?: string;
2037
+ reasoning?: string;
2038
+ toolCalls?: Array<{
2039
+ id: string;
2040
+ function?: {
2041
+ name: string;
2042
+ arguments: string;
2043
+ };
2044
+ name?: string;
2045
+ arguments?: unknown;
2046
+ }>;
2047
+ usage?: {
2048
+ promptTokens: number;
2049
+ completionTokens: number;
2050
+ totalTokens: number;
2051
+ };
2052
+ }
2053
+ /**
2054
+ * Context 相关错误
2055
+ */
2056
+ declare class ContextError extends RoyError {
2057
+ readonly sessionId?: string | undefined;
2058
+ readonly usage?: {
2059
+ promptTokens: number;
2060
+ completionTokens: number;
2061
+ totalTokens: number;
2062
+ } | undefined;
2063
+ readonly contextWindow?: number | undefined;
2064
+ readonly llmOutput?: LLMOutput2;
2065
+ constructor(message: string, code?: ErrorCode, sessionId?: string | undefined, usage?: {
2066
+ promptTokens: number;
2067
+ completionTokens: number;
2068
+ totalTokens: number;
2069
+ } | undefined, contextWindow?: number | undefined, llmOutput?: LLMOutput2, details?: unknown);
2070
+ }
2071
+ /**
2072
+ * LLMComponent
2073
+ * 管理 LLM 调用,使用 AI SDK 进行实际调用
2074
+ *
2075
+ * 配置通过 ConfigComponent 实时获取,支持配置热更新
2076
+ *
2077
+ * @example
2078
+ * // 通过 ConfigComponent 加载配置
2079
+ * const configComponent = new ConfigComponent();
2080
+ * configComponent.setXdgDataHome(__dirname);
2081
+ *
2082
+ * const llmComponent = new LLMComponent();
2083
+ * await llmComponent.init({
2084
+ * env, // Environment 实例(必填)
2085
+ * options: {
2086
+ * configComponent, // ConfigComponent 实例
2087
+ * configPath: "config.jsonc" // 配置文件路径
2088
+ * }
2089
+ * });
2090
+ *
2091
+ * // 配置更新后,下次调用会自动获取最新配置
2092
+ * configComponent.set("llm.temperature", 0.5);
2093
+ */
2094
+ declare class LLMComponent extends BaseComponent {
2095
+ readonly name = "llm";
2096
+ readonly version = "2.0.0";
2097
+ private configComponent?;
2098
+ /** 配置变更 watcher 清理函数 */
2099
+ private configWatcher?;
2100
+ constructor();
2101
+ /**
2102
+ * 初始化组件
2103
+ *
2104
+ * 配置加载优先级(从高到低):
2105
+ * 1. Object - 直接传入的 config 对象
2106
+ * 2. Env - 环境变量(通过 envPrefix 配置前缀)
2107
+ * 3. File - 配置文件(通过 configPath 指定)
2108
+ */
2109
+ init(config: ComponentConfig): Promise<void>;
2110
+ /**
2111
+ * 注册配置到 ConfigComponent
2112
+ *
2113
+ * 配置加载顺序(优先级从低到高):
2114
+ * 1. FileSource - 从配置文件加载(最低)
2115
+ * 2. EnvSource - 从环境变量加载(中等)
2116
+ * 3. MemorySource - 直接配置对象(最高)
2117
+ */
2118
+ private registerConfig;
2119
+ /**
2120
+ * 将配置对象展平为点号路径
2121
+ *
2122
+ * @example
2123
+ * flattenConfig({ providers: { openai: { apiKey: "xxx" } } })
2124
+ * -> { "llm.providers.openai.apiKey": "xxx" }
2125
+ */
2126
+ private flattenConfig;
2127
+ /**
2128
+ * 注册配置热更新监听
2129
+ */
2130
+ private registerConfigWatcher;
2131
+ /**
2132
+ * 处理配置变更(支持热更新)
2133
+ */
2134
+ protected onConfigChange(event: {
2135
+ key: string;
2136
+ oldValue?: unknown;
2137
+ newValue?: unknown;
2138
+ }): void;
2139
+ /**
2140
+ * 启动组件
2141
+ */
2142
+ onStart(): Promise<void>;
2143
+ /**
2144
+ * 停止组件
2145
+ */
2146
+ onStop(): Promise<void>;
2147
+ /**
2148
+ * 从配置源获取配置值
2149
+ * 优先从 ConfigComponent 获取(支持热更新),否则返回默认值
2150
+ */
2151
+ private getLLMConfig;
2152
+ /**
2153
+ * 解析请求,构建调用上下文(invoke 和 invokeStream 共享)
2154
+ */
2155
+ private resolveRequest;
2156
+ /**
2157
+ * 调用 LLM
2158
+ *
2159
+ * 流式事件通过 env.pushEnvEvent() 发布
2160
+ */
2161
+ invoke(request: LLMInvokeRequest): Promise<LLMInvokeResult>;
2162
+ /**
2163
+ * 获取 Provider API Key
2164
+ */
2165
+ private getApiKey;
2166
+ /**
2167
+ * 获取 Provider
2168
+ */
2169
+ getProvider(id: string);
2170
+ /**
2171
+ * 列出所有 Provider
2172
+ */
2173
+ listProviders();
2174
+ /**
2175
+ * 获取模型的限制配置
2176
+ *
2177
+ * @param providerId - Provider ID
2178
+ * @param modelId - 模型 ID(可选,默认使用 provider 的 defaultModel)
2179
+ * @returns 模型限制配置,如果没有配置则返回 undefined
2180
+ */
2181
+ getModelLimits(providerId: string, modelId?: string): ModelLimits | undefined;
2182
+ /**
2183
+ * 获取上下文窗口阈值配置
2184
+ *
2185
+ * @param providerId - Provider ID
2186
+ * @param modelId - 模型 ID
2187
+ * @returns 包含 contextWindow 和 thresholdRatio 的对象
2188
+ */
2189
+ getContextThresholdConfig(providerId: string, modelId?: string): {
2190
+ contextWindow: number;
2191
+ thresholdRatio: number;
2192
+ };
2193
+ /**
2194
+ * 检测上下文阈值是否超过
2195
+ *
2196
+ * @param usage - LLM 使用量信息
2197
+ * @param providerId - Provider ID
2198
+ * @param modelId - 模型 ID
2199
+ * @param sessionId - Session ID(用于错误信息)
2200
+ * @returns 如果超过阈值,返回 ContextError;否则返回 undefined
2201
+ */
2202
+ checkContextThreshold(usage: {
2203
+ totalTokens: number;
2204
+ promptTokens?: number;
2205
+ completionTokens?: number;
2206
+ } | undefined, providerId: string, modelId: string | undefined, sessionId?: string): ContextError | undefined;
2207
+ }
2208
+ /**
2209
+ * Summary Agent
2210
+ *
2211
+ * 专门用于生成 session checkpoint 的 Agent
2212
+ */
2213
+ declare class SummaryAgent {
2214
+ private promptComponent;
2215
+ private llmComponent;
2216
+ private config;
2217
+ constructor(promptComponent: PromptComponent, llmComponent: LLMComponent, config?: Partial<SummaryAgentConfig>);
2218
+ /**
2219
+ * 生成场景化压缩提示 (Compact Hint)
2220
+ *
2221
+ * 根据会话历史分析,生成一个场景化的压缩提示,用于指导后续的 checkpoint 生成。
2222
+ * 这个方法用于自动触发压缩的两阶段流程中的第一阶段。
2223
+ *
2224
+ * @param options 生成选项
2225
+ * @returns 场景化的压缩提示
2226
+ */
2227
+ generateCompactHint(options: CompactHintGenerationOptions): Promise<CompactHintGenerationResult>;
2228
+ /**
2229
+ * 运行 Summary Agent
2230
+ *
2231
+ * @param options 运行选项
2232
+ * @returns Summary Agent 结果
2233
+ */
2234
+ run(options: SummaryAgentRunOptions): Promise<SummaryAgentResult>;
2235
+ /**
2236
+ * 获取系统提示
2237
+ *
2238
+ * @param scenarioHint 场景化提示(可选)
2239
+ */
2240
+ private getSystemPrompt;
2241
+ /**
2242
+ * 获取提示生成的系统提示
2243
+ *
2244
+ * 这个方法生成的 hint 是一个 "压缩指导 Prompt",用于告诉 Compact 阶段:
2245
+ * "在当前这个场景/目标/任务下,生成 checkpoint 时应该重点关注什么"
2246
+ *
2247
+ * 注意:这里生成的是"指导 Prompt",不是"压缩要点本身"
2248
+ *
2249
+ * 指导 Prompt 应包含:
2250
+ * 1. 当前正在进行什么工作
2251
+ * 2. 进展是什么
2252
+ * 3. 压缩时需要专注什么信息的提取
2253
+ * 4. 这些信息对后续任务推进很重要,不能丢失
2254
+ */
2255
+ private getHintGenerationSystemPrompt;
2256
+ /**
2257
+ * 构建提示生成的用户提示
2258
+ */
2259
+ private buildHintGenerationUserPrompt;
2260
+ /**
2261
+ * 构建用户提示
2262
+ */
2263
+ private buildUserPrompt;
2264
+ /**
2265
+ * 格式化消息用于 prompt
2266
+ */
2267
+ private formatMessages;
2268
+ /**
2269
+ * 解析 JSON 响应
2270
+ */
2271
+ private parseJsonResponse;
2272
+ /**
2273
+ * 解析提示响应
2274
+ *
2275
+ * 直接返回清理后的自然语言提示
2276
+ */
2277
+ private parseHintResponse;
2278
+ }
2279
+ export { ToolResult2 as ToolResult, ToolCallResult, ToolCall2 as ToolCall, Tool, SummaryAgentRunOptions, SummaryAgentResult, SummaryAgentConfig, SummaryAgent, ProjectSummary, Plugin, LLMOutput, LLMCallResult, HookResult2 as HookResult, HookPoint, HookContext2 as HookContext, HookActionType2 as HookActionType, CompactHintGenerationResult, CompactHintGenerationOptions, AgentType, AgentStatus, AgentRunResult, AgentReactContext, AgentLLMOutput, AgentInstanceConfig, AgentInstance, AgentContext, AgentComponentConfigSchema, AgentComponentConfig, AgentComponent };