@faapi/agent 6.16.0 → 6.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +132 -27
- package/dist/index.js +229 -69
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -242,9 +242,10 @@ interface AgentTrace {
|
|
|
242
242
|
startedAt: number;
|
|
243
243
|
/** 总耗时 ms(done 时填充,抛错时也填充) */
|
|
244
244
|
durationMs?: number;
|
|
245
|
-
/** 总轮数(与 ReactLoopResult.turns
|
|
245
|
+
/** 总轮数(与 ReactLoopResult.turns 一致,整树口径——含全部 sub-agent 循环轮数;
|
|
246
|
+
* 事件 turn 序号是主循环口径,与此独立) */
|
|
246
247
|
turns: number;
|
|
247
|
-
/** 累计 token 用量(与 ReactLoopResult.usage
|
|
248
|
+
/** 累计 token 用量(与 ReactLoopResult.usage 一致,整树口径——含全部 sub-agent 循环) */
|
|
248
249
|
usage?: LLMUsage;
|
|
249
250
|
/** 最终停止原因(与 ReactLoopResult.stopReason 一致) */
|
|
250
251
|
stopReason?: LLMStopReason;
|
|
@@ -269,7 +270,7 @@ type AgentTraceEvent = LlmCallEvent | ToolCallEvent | SubAgentCallEvent;
|
|
|
269
270
|
*/
|
|
270
271
|
interface LlmCallEvent {
|
|
271
272
|
type: 'llm_call';
|
|
272
|
-
/** 第几轮(从 1
|
|
273
|
+
/** 第几轮(从 1 开始;主循环口径,sub-agent 内部轮在各层 sub-trace 里) */
|
|
273
274
|
turn: number;
|
|
274
275
|
startedAt: number;
|
|
275
276
|
durationMs?: number;
|
|
@@ -304,12 +305,16 @@ interface ToolCallEvent {
|
|
|
304
305
|
error?: string;
|
|
305
306
|
}
|
|
306
307
|
/**
|
|
307
|
-
* sub-agent 调用事件——executeTool 返回
|
|
308
|
+
* sub-agent 调用事件——executeTool 返回 SubAgentToolResult(trace 存在时)
|
|
309
|
+
* 或旧 TracingToolResult 时触发
|
|
308
310
|
*
|
|
309
|
-
* sub-agent 的 trace 嵌入 `trace`
|
|
311
|
+
* sub-agent 的 trace 嵌入 `trace` 字段(递归结构),业务方可还原完整调用树;
|
|
312
|
+
* sub 循环的 usage/turns 已由 reactLoop 上卷进父 run 台账(整树口径),
|
|
313
|
+
* 逐层明细经各层 sub-trace 还原。
|
|
310
314
|
*/
|
|
311
315
|
interface SubAgentCallEvent {
|
|
312
316
|
type: 'subagent_call';
|
|
317
|
+
/** 第几轮(从 1 开始;主循环口径) */
|
|
313
318
|
turn: number;
|
|
314
319
|
startedAt: number;
|
|
315
320
|
durationMs?: number;
|
|
@@ -328,12 +333,17 @@ interface SubAgentCallEvent {
|
|
|
328
333
|
/**
|
|
329
334
|
* sub-agent 调用的特殊返回值——reactLoop 据此识别 sub-agent 调用并发出 subagent_call 事件
|
|
330
335
|
*
|
|
331
|
-
*
|
|
332
|
-
*
|
|
336
|
+
* **旧版结构(兼容保留)**:无用量字段、不上卷 usage/turns。框架内部的
|
|
337
|
+
* [Agent.executeSubAgent](./agent.md) 已改用 [SubAgentToolResult](./reactLoop.md)
|
|
338
|
+
* (`__subAgent` 标记,携带子循环整树 `usage` / `turns`,tracing 开启时附 `trace`)——
|
|
339
|
+
* 新代码应使用 SubAgentToolResult。reactLoop 对两者都识别(存量业务方直接调
|
|
340
|
+
* `reactLoop` 自定义 `executeTool` 构造本类型的代码不受影响)。
|
|
333
341
|
*
|
|
334
342
|
* `__trace` 是标记字段,避免与普通对象返回值冲突。reactLoop 通过
|
|
335
343
|
* `typeof result === 'object' && result !== null && result.__trace === true`
|
|
336
344
|
* 判断是否为 TracingToolResult。
|
|
345
|
+
*
|
|
346
|
+
* @deprecated 改用 SubAgentToolResult(reactLoop.ts)——本类型仅为存量兼容保留
|
|
337
347
|
*/
|
|
338
348
|
interface TracingToolResult {
|
|
339
349
|
/** 标记字段(避免与普通对象返回值冲突) */
|
|
@@ -348,6 +358,9 @@ interface TracingToolResult {
|
|
|
348
358
|
*
|
|
349
359
|
* reactLoop 用此函数区分 sub-agent 调用(发出 subagent_call 事件)
|
|
350
360
|
* 与常规 tool 调用(发出 tool_call 事件)。
|
|
361
|
+
*
|
|
362
|
+
* @deprecated 框架内部已改用 isSubAgentToolResult(reactLoop.ts);本守卫仅为
|
|
363
|
+
* 存量自定义 executeTool 的兼容识别保留
|
|
351
364
|
*/
|
|
352
365
|
declare function isTracingToolResult(value: unknown): value is TracingToolResult;
|
|
353
366
|
|
|
@@ -364,14 +377,66 @@ declare function isTracingToolResult(value: unknown): value is TracingToolResult
|
|
|
364
377
|
* 由 [Agent 类](./agent.md)提供——reactLoop 不关心 tool 如何被找到和执行。
|
|
365
378
|
* Agent 类的 `executeTool` 实现:
|
|
366
379
|
* - 常规 tool → `loadToolModule` 加载 handler 并调用
|
|
367
|
-
* - agent-as-tool(`agent.` 前缀)→ 递归调子 agent 的 reactLoop
|
|
380
|
+
* - agent-as-tool(`agent.` 前缀)→ 递归调子 agent 的 reactLoop(`maxAgentDepth` 防护由 Agent 类在 `executeTool` 内实现),
|
|
381
|
+
* 返回 [SubAgentToolResult](#subagenttoolresult)(携带子循环整树 `usage` / `turns` 供父循环上卷)
|
|
368
382
|
*
|
|
369
383
|
* 返回值可以是任意类型——非 string 会被 JSON.stringify 后回传 LLM。
|
|
370
384
|
*
|
|
371
|
-
* sub-agent
|
|
372
|
-
*
|
|
385
|
+
* sub-agent 调用时,`SubAgentToolResult.trace` 存在(`enableTracing=true`)时
|
|
386
|
+
* reactLoop 发出 `subagent_call` 事件(嵌套递归 trace);旧 `TracingToolResult`
|
|
387
|
+
* (`__trace` 标记,无用量字段)仍兼容识别,但不上卷用量。
|
|
388
|
+
*/
|
|
389
|
+
/**
|
|
390
|
+
* 嵌套子代理循环的增量(流式父循环冒泡透出)
|
|
391
|
+
*
|
|
392
|
+
* 仅流式路径产生;`depth` 与 `maxAgentDepth` 口径一致(根循环 = 1,首次嵌套的
|
|
393
|
+
* 子代理 = 2)。`deltaContent` / `deltaReasoning` 至少存在其一。
|
|
394
|
+
*/
|
|
395
|
+
interface SubAgentDelta {
|
|
396
|
+
/** 发起调用的 tool 名(agent.<name>) */
|
|
397
|
+
name: string;
|
|
398
|
+
/** 该子代理循环的递归深度(根 = 1) */
|
|
399
|
+
depth: number;
|
|
400
|
+
deltaContent?: string;
|
|
401
|
+
deltaReasoning?: string;
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* delta 冒泡出口:executeTool 执行 sub-agent 时回调透出嵌套循环增量
|
|
405
|
+
*
|
|
406
|
+
* 仅流式父循环传入(`reactLoopStream` 的 tool 执行段构造 emitter 经第三参下发);
|
|
407
|
+
* 非流式 `reactLoop` 不传(结果一次性返回,无冒泡需求)。
|
|
408
|
+
*/
|
|
409
|
+
interface SubAgentDeltaEmitter {
|
|
410
|
+
onSubAgentDelta(delta: SubAgentDelta): void;
|
|
411
|
+
}
|
|
412
|
+
type ToolExecutor = (name: string, args: Record<string, unknown>, deltaEmitter?: SubAgentDeltaEmitter) => Promise<unknown | SubAgentToolResult>;
|
|
413
|
+
/**
|
|
414
|
+
* sub-agent tool 的结构化返回值——reactLoop 据此上卷子循环用量、剥壳回传、发 subagent_call 事件
|
|
415
|
+
*
|
|
416
|
+
* [Agent.executeSubAgent](./agent.md) 在 sub-agent 走默认 reactLoop 时统一构造
|
|
417
|
+
* (无论 tracing 开关——用量上卷不依赖 tracing)。reactLoop 通过
|
|
418
|
+
* `isSubAgentToolResult` 识别后:`usage` / `turns` 累加进父循环(整树口径,
|
|
419
|
+
* 详见 reactLoop.md「usage 与 turns 的整树口径」),随后剥壳取 `result` 作为
|
|
420
|
+
* tool 消息回传 LLM;`trace` 存在时再发 `subagent_call` 事件。
|
|
421
|
+
*
|
|
422
|
+
* `__subAgent` 是标记字段,避免与普通对象返回值冲突。
|
|
423
|
+
*/
|
|
424
|
+
interface SubAgentToolResult {
|
|
425
|
+
/** 标记字段(避免与普通对象返回值冲突) */
|
|
426
|
+
__subAgent: true;
|
|
427
|
+
/** 子代理返回的业务结果(stringifyResult 后作为 tool 消息内容回传 LLM) */
|
|
428
|
+
result: unknown;
|
|
429
|
+
/** 子循环整树 token 用量(自定义 run 的 sub-agent 无结构化用量,缺省 = 计 0) */
|
|
430
|
+
usage?: LLMUsage;
|
|
431
|
+
/** 子循环整树轮数(缺省 = 计 0) */
|
|
432
|
+
turns?: number;
|
|
433
|
+
/** 子循环 trace(`enableTracing=true` 时携带,reactLoop 发 subagent_call 事件并嵌入) */
|
|
434
|
+
trace?: AgentTrace;
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* 类型守卫:判断 ToolExecutor 返回值是否为 SubAgentToolResult
|
|
373
438
|
*/
|
|
374
|
-
|
|
439
|
+
declare function isSubAgentToolResult(value: unknown): value is SubAgentToolResult;
|
|
375
440
|
/**
|
|
376
441
|
* reactLoop 配置
|
|
377
442
|
*
|
|
@@ -420,10 +485,11 @@ interface ReactLoopConfig {
|
|
|
420
485
|
*/
|
|
421
486
|
messages?: LLMMessage[];
|
|
422
487
|
/**
|
|
423
|
-
* 启用 tracing(默认
|
|
424
|
-
* `ReactLoopStreamChunk.traceEvent`,详见 [trace.md](./trace.md)。
|
|
488
|
+
* 启用 tracing(默认 false——opt-in,不开启零开销)。开启时填充
|
|
489
|
+
* `ReactLoopResult.trace` / `ReactLoopStreamChunk.traceEvent`,详见 [trace.md](./trace.md)。
|
|
425
490
|
*
|
|
426
|
-
*
|
|
491
|
+
* 与 `AgentRuntimeConfig.enableTracing` / `AgentRunOptions.enableTracing` 同语义:
|
|
492
|
+
* 优先级 options > 全局 config > 默认 false。
|
|
427
493
|
*/
|
|
428
494
|
enableTracing?: boolean;
|
|
429
495
|
}
|
|
@@ -441,11 +507,18 @@ interface ReactLoopResult {
|
|
|
441
507
|
reasoning?: string;
|
|
442
508
|
/** 完整对话历史(system + user + assistant + tool 消息) */
|
|
443
509
|
messages: LLMMessage[];
|
|
444
|
-
/**
|
|
510
|
+
/**
|
|
511
|
+
* 使用的轮数(整树口径:主循环轮数 + 全部 sub-agent 循环轮数)。
|
|
512
|
+
* `maxTurns` 循环控制与 trace 事件的 `turn` 序号只按主循环计(子代理轮数不挤占父循环预算)。
|
|
513
|
+
*/
|
|
445
514
|
turns: number;
|
|
446
515
|
/** 最终轮的停止原因 */
|
|
447
516
|
stopReason: LLMStopReason;
|
|
448
|
-
/**
|
|
517
|
+
/**
|
|
518
|
+
* 累计 token 用量(整树口径:本次 run 全部 `llm_call` usage 之和,含全部层级的
|
|
519
|
+
* sub-agent 循环,自定义 run 的 sub-agent 计 0;provider 不返回时为 `undefined`)。
|
|
520
|
+
* 详见 reactLoop.md「usage 与 turns 的整树口径」。
|
|
521
|
+
*/
|
|
449
522
|
usage?: LLMUsage;
|
|
450
523
|
/**
|
|
451
524
|
* 结构化调用明细(`enableTracing=true` 时填充,否则 `undefined` 零开销)。
|
|
@@ -479,9 +552,14 @@ interface ReactLoopStreamChunk {
|
|
|
479
552
|
name: string;
|
|
480
553
|
result: string;
|
|
481
554
|
};
|
|
555
|
+
/**
|
|
556
|
+
* 嵌套子代理循环的增量(冒泡透传;仅流式路径)。
|
|
557
|
+
* 详见 reactLoop.md「子代理 delta 冒泡」章节。
|
|
558
|
+
*/
|
|
559
|
+
subagentDelta?: SubAgentDelta;
|
|
482
560
|
/**
|
|
483
561
|
* trace 事件(`enableTracing=true` 时增量推送)。
|
|
484
|
-
* 与 deltaContent / deltaReasoning / toolCall / toolResult / done 互斥,一个 chunk 至多一个字段。
|
|
562
|
+
* 与 deltaContent / deltaReasoning / toolCall / toolResult / subagentDelta / done 互斥,一个 chunk 至多一个字段。
|
|
485
563
|
*/
|
|
486
564
|
traceEvent?: AgentTraceEvent;
|
|
487
565
|
/** 循环结束 */
|
|
@@ -489,8 +567,10 @@ interface ReactLoopStreamChunk {
|
|
|
489
567
|
content: string;
|
|
490
568
|
/** 最终轮的完整推理内容(thinking 模型;无推理内容时不存在) */
|
|
491
569
|
reasoning?: string;
|
|
570
|
+
/** 整树口径(主循环 + 全部 sub-agent 循环),与 ReactLoopResult.turns 一致 */
|
|
492
571
|
turns: number;
|
|
493
572
|
stopReason: LLMStopReason;
|
|
573
|
+
/** 整树口径(全部 llm_call 之和),与 ReactLoopResult.usage 一致 */
|
|
494
574
|
usage?: LLMUsage;
|
|
495
575
|
};
|
|
496
576
|
}
|
|
@@ -719,6 +799,13 @@ interface AgentRuntimeConfig {
|
|
|
719
799
|
maxAgentDepth?: number;
|
|
720
800
|
/** 发送给 LLM 的历史 token 预算(近似估算,未设置 = 不裁剪)——透传 reactLoop,见 reactLoop.md 历史裁剪章节 */
|
|
721
801
|
maxHistoryTokens?: number;
|
|
802
|
+
/**
|
|
803
|
+
* 单次 tool 执行的超时毫秒数(未设置 = 不限时)。超时抛 `AgentToolTimeoutError`,
|
|
804
|
+
* 被 reactLoop 按既有 tool 错误路径回传 LLM(LLM 可决定重试或换路)——挂死的
|
|
805
|
+
* tool handler(如无超时的内部 fetch)此前会让整个 run 永久挂起,且 run 的
|
|
806
|
+
* abort signal 对 tool 执行无效
|
|
807
|
+
*/
|
|
808
|
+
toolTimeoutMs?: number;
|
|
722
809
|
/**
|
|
723
810
|
* 启用 tracing 的全局默认值(默认 false——opt-in,不开启零开销)。
|
|
724
811
|
*
|
|
@@ -843,6 +930,14 @@ declare class AgentError extends Error {
|
|
|
843
930
|
*
|
|
844
931
|
* 被 [reactLoop](./reactLoop.md) catch 后错误消息回传 LLM,LLM 可据此调整策略。
|
|
845
932
|
*/
|
|
933
|
+
/** tool 执行超时(toolTimeoutMs)——被 reactLoop catch 后回传 LLM,不终止整个 run */
|
|
934
|
+
declare class AgentToolTimeoutError extends AgentError {
|
|
935
|
+
/** 超时的 tool 名 */
|
|
936
|
+
readonly toolName: string;
|
|
937
|
+
/** 配置的超时毫秒数 */
|
|
938
|
+
readonly timeoutMs: number;
|
|
939
|
+
constructor(toolName: string, timeoutMs: number);
|
|
940
|
+
}
|
|
846
941
|
declare class AgentRecursionError extends AgentError {
|
|
847
942
|
/** 配置的 maxAgentDepth 值 */
|
|
848
943
|
readonly maxDepth: number;
|
|
@@ -951,6 +1046,13 @@ declare class Agent {
|
|
|
951
1046
|
* `options.messages` 提供时先经 `validateResumeHistory` 结构校验,非法抛
|
|
952
1047
|
* `AgentError`,不发起 LLM 请求。
|
|
953
1048
|
*/
|
|
1049
|
+
/**
|
|
1050
|
+
* 构建执行白名单:agent 声明的 tools + sub-agents(`agent.` 前缀)
|
|
1051
|
+
*
|
|
1052
|
+
* 每次 run 构建一次存入 callCtx(executeTool 复用)——reload 场景注册表换代后
|
|
1053
|
+
* 新 run 重新构建,声明变化自然生效
|
|
1054
|
+
*/
|
|
1055
|
+
private buildDeclaredTools;
|
|
954
1056
|
private buildLoopConfig;
|
|
955
1057
|
/**
|
|
956
1058
|
* 解析外部 provider(`options.provider`)→ provider + model
|
|
@@ -1005,7 +1107,7 @@ declare class Agent {
|
|
|
1005
1107
|
/**
|
|
1006
1108
|
* tool 执行路由(由 reactLoop 调用)
|
|
1007
1109
|
*
|
|
1008
|
-
* - `agent.` 前缀 → {@link executeSubAgent} 递归(含
|
|
1110
|
+
* - `agent.` 前缀 → {@link executeSubAgent} 递归(含 usage/turns 上卷 + tracing 包装)
|
|
1009
1111
|
* - 常规 tool → `loadToolModule` 加载 handler + 可选 input 校验 → 调用
|
|
1010
1112
|
*
|
|
1011
1113
|
* `callCtx` 由 [buildLoopConfig](#buildLoopConfig) 闭包捕获传入——本次调用的有效
|
|
@@ -1022,19 +1124,22 @@ declare class Agent {
|
|
|
1022
1124
|
* sub-agent 递归执行
|
|
1023
1125
|
*
|
|
1024
1126
|
* 1. `maxAgentDepth` 防护——超限抛 {@link AgentRecursionError}
|
|
1025
|
-
* 2. sub-agent handler 导出 `run` 时调自定义 `mod.run(args)`(无 trace
|
|
1127
|
+
* 2. sub-agent handler 导出 `run` 时调自定义 `mod.run(args)`(无 trace、无结构化
|
|
1128
|
+
* usage 可卷——直接返回业务结果,其 token 不进入父 run 台账)
|
|
1026
1129
|
* 3. 无 `run` 时调 `subAgent.run(stringify(args), { agent, provider, model, enableTracing })`
|
|
1027
1130
|
* 走默认 reactLoop——继承父调用的 provider,sub 元数据声明 `model` 时优先用自身的,
|
|
1028
1131
|
* 未声明时沿用父 model
|
|
1029
1132
|
*
|
|
1030
|
-
*
|
|
1031
|
-
*
|
|
1032
|
-
* reactLoop
|
|
1033
|
-
*
|
|
1034
|
-
*
|
|
1133
|
+
* **返回值统一包装为 [SubAgentToolResult](./reactLoop.md)**(无论 tracing 开关——
|
|
1134
|
+
* 用量上卷不依赖 tracing):`usage` / `turns` 是子循环整树口径(sub-sub 已在子循环
|
|
1135
|
+
* 上卷),reactLoop 识别后累加进父循环,父 run 的 usage 台账 = 全部 `llm_call` 之和。
|
|
1136
|
+
* `enableTracing=true` 时再附 `trace`(agentName 已被 `Agent.run` 填为 subName),
|
|
1137
|
+
* reactLoop 据此发出 `subagent_call` 事件,嵌入 sub-trace(递归结构,业务方可还原
|
|
1138
|
+
* 完整调用树)。详见 reactLoop.md「usage 与 turns 的整树口径」。
|
|
1035
1139
|
*
|
|
1036
|
-
* **自定义 run 无 trace
|
|
1037
|
-
* 内部明细——需 trace
|
|
1140
|
+
* **自定义 run 无 trace、无用量上卷**:业务方导出 `run` 函数时直接返回业务结果,
|
|
1141
|
+
* 无法采集 sub-agent 内部明细——需 trace / 用量统计时让 sub-agent 走默认 reactLoop
|
|
1142
|
+
* (不导出 `run`)。
|
|
1038
1143
|
*
|
|
1039
1144
|
* 自定义 run 接收原始 args 对象;默认 reactLoop 的 user 消息:args 恰为单字段
|
|
1040
1145
|
* `{ input: <string> }`(与显式入参 schema 形状一致)时直传字符串,其余形状
|
|
@@ -1132,4 +1237,4 @@ declare function createToolSchemaResolver(options?: {
|
|
|
1132
1237
|
*/
|
|
1133
1238
|
declare const agentPlugin: FaapiPlugin;
|
|
1134
1239
|
|
|
1135
|
-
export { Agent, AgentAbortError, type AgentDeps, AgentError, type AgentHandle, AgentRecursionError, type AgentRunOptions, type AgentRuntimeConfig, type AgentTrace, type AgentTraceEvent, type LLMCompleteRequest, type LLMMessage, type LLMProvider, LLMProviderError, type LLMResponse, type LLMStopReason, type LLMStreamChunk, type LLMToolCall, type LLMToolDefinition, type LLMUsage, type LlmCallEvent, type ReactLoopConfig, ReactLoopError, type ReactLoopResult, type ReactLoopStreamChunk, type SubAgentCallEvent, type ToolCallEvent, type ToolExecutor, type ToolSchemaResolution, type TracingToolResult, createOpenAIProvider, createProvider, createToolSchemaResolver, agentPlugin as default, isTracingToolResult, reactLoop, reactLoopStream };
|
|
1240
|
+
export { Agent, AgentAbortError, type AgentDeps, AgentError, type AgentHandle, AgentRecursionError, type AgentRunOptions, type AgentRuntimeConfig, AgentToolTimeoutError, type AgentTrace, type AgentTraceEvent, type LLMCompleteRequest, type LLMMessage, type LLMProvider, LLMProviderError, type LLMResponse, type LLMStopReason, type LLMStreamChunk, type LLMToolCall, type LLMToolDefinition, type LLMUsage, type LlmCallEvent, type ReactLoopConfig, ReactLoopError, type ReactLoopResult, type ReactLoopStreamChunk, type SubAgentCallEvent, type SubAgentDelta, type SubAgentDeltaEmitter, type SubAgentToolResult, type ToolCallEvent, type ToolExecutor, type ToolSchemaResolution, type TracingToolResult, createOpenAIProvider, createProvider, createToolSchemaResolver, agentPlugin as default, isSubAgentToolResult, isTracingToolResult, reactLoop, reactLoopStream };
|