@faapi/agent 3.2.1 → 4.0.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 +119 -14
- package/dist/index.js +331 -98
- package/dist/index.js.map +1 -1
- package/package.json +8 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LlmConfig, AgentToolDescriptor, AgentCore, AgentMetadata, ToolMetadata, ToolModule, AgentModule, FaapiPlugin } from '@faapi/faapi';
|
|
1
|
+
import { LlmConfig, AgentToolDescriptor, FaapiContext, AgentCore, AgentMetadata, ToolMetadata, ToolModule, AgentModule, FaapiPlugin } from '@faapi/faapi';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* LLM Provider 错误
|
|
@@ -100,6 +100,23 @@ interface LLMCompleteRequest {
|
|
|
100
100
|
temperature?: number;
|
|
101
101
|
/** 最大生成 token 数 */
|
|
102
102
|
maxTokens?: number;
|
|
103
|
+
/**
|
|
104
|
+
* 取消信号(透传到底层 HTTP 请求)
|
|
105
|
+
*
|
|
106
|
+
* abort 时请求中断并抛 `AgentAbortError`;与 `LlmConfig.timeoutMs` 的
|
|
107
|
+
* 超时信号组合生效(任一触发即中断)。
|
|
108
|
+
*/
|
|
109
|
+
signal?: AbortSignal;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Agent 执行被取消
|
|
113
|
+
*
|
|
114
|
+
* 外部 `AbortSignal` 触发时抛出(请求前预检查或请求中断)。
|
|
115
|
+
* 业务方通过 `instanceof AgentAbortError` 区分「用户取消」与真实错误——
|
|
116
|
+
* 取消不是故障,不应触发告警/重试逻辑。
|
|
117
|
+
*/
|
|
118
|
+
declare class AgentAbortError extends Error {
|
|
119
|
+
constructor(message?: string);
|
|
103
120
|
}
|
|
104
121
|
/**
|
|
105
122
|
* LLM 响应的停止原因
|
|
@@ -180,8 +197,9 @@ declare function createProvider(config: LlmConfig): LLMProvider;
|
|
|
180
197
|
/**
|
|
181
198
|
* 单次 agent.run() / agent.stream() 的结构化调用明细
|
|
182
199
|
*
|
|
183
|
-
*
|
|
184
|
-
* `enableTracing:
|
|
200
|
+
* 默认关闭(`enableTracing` 默认 `false`——opt-in,不开启零开销),
|
|
201
|
+
* 通过 `config.agent.enableTracing: true` 全局开启或单次调用
|
|
202
|
+
* `agent.run(input, { enableTracing: true })` 开启。详见 [trace.md](./trace.md)。
|
|
185
203
|
*/
|
|
186
204
|
interface AgentTrace {
|
|
187
205
|
/** agent 名 */
|
|
@@ -342,6 +360,21 @@ interface ReactLoopConfig {
|
|
|
342
360
|
temperature?: number;
|
|
343
361
|
/** 最大生成 token 数 */
|
|
344
362
|
maxTokens?: number;
|
|
363
|
+
/**
|
|
364
|
+
* 取消信号(透传到每轮 LLM 请求)
|
|
365
|
+
*
|
|
366
|
+
* 循环每轮开始前预检查:已取消时抛 `AgentAbortError`(不再发起 LLM 调用);
|
|
367
|
+
* 执行中取消由 provider 的请求中断传播。tool 执行不被取消(业务自决)。
|
|
368
|
+
*/
|
|
369
|
+
signal?: AbortSignal;
|
|
370
|
+
/**
|
|
371
|
+
* 发送给 LLM 的历史 token 预算(近似估算:字符数 / 2;未设置 = 不裁剪,向后兼容)
|
|
372
|
+
*
|
|
373
|
+
* 超预算时从最旧的「轮组」(assistant + 其后全部 tool 结果)开始裁剪,
|
|
374
|
+
* system 与初始 user 永不裁剪,至少保留最近一轮。裁剪只作用于发给 LLM 的
|
|
375
|
+
* 消息副本,本地 `messages` 与 trace 不受影响。详见 reactLoop.md 的历史裁剪章节。
|
|
376
|
+
*/
|
|
377
|
+
maxHistoryTokens?: number;
|
|
345
378
|
/**
|
|
346
379
|
* 启用 tracing(默认 true)。开启时填充 `ReactLoopResult.trace` /
|
|
347
380
|
* `ReactLoopStreamChunk.traceEvent`,详见 [trace.md](./trace.md)。
|
|
@@ -482,6 +515,9 @@ declare function reactLoopStream(input: string, config: ReactLoopConfig): AsyncI
|
|
|
482
515
|
* `AgentRuntimeConfig` / `defaultLlm` provider。详见 [agentHandle.md](./agentHandle.md) 的
|
|
483
516
|
* Run-level 覆盖优先级表。
|
|
484
517
|
*
|
|
518
|
+
* `agent` 字段覆盖本次调用的 agent 名(不传时用 `config.agent.defaultAgent`,
|
|
519
|
+
* 未设 defaultAgent 时必须显式传入)。
|
|
520
|
+
*
|
|
485
521
|
* @example
|
|
486
522
|
* ```ts
|
|
487
523
|
* // 按请求切模型(纯 model 名,在 llms 里唯一时切到对应 provider)
|
|
@@ -489,9 +525,26 @@ declare function reactLoopStream(input: string, config: ReactLoopConfig): AsyncI
|
|
|
489
525
|
*
|
|
490
526
|
* // provider/model 一体化形式(精确切换)
|
|
491
527
|
* await agent.run(input, { model: 'anthropic/claude-3-5-sonnet' });
|
|
528
|
+
*
|
|
529
|
+
* // 指定 agent(不依赖 defaultAgent 配置)
|
|
530
|
+
* await agent.run(input, { agent: 'researcher' });
|
|
492
531
|
* ```
|
|
493
532
|
*/
|
|
494
533
|
interface AgentRunOptions {
|
|
534
|
+
/**
|
|
535
|
+
* 取消信号(透传到每轮 LLM 请求)
|
|
536
|
+
*
|
|
537
|
+
* abort 后当前轮请求中断并抛 `AgentAbortError`,循环不再进入下一轮。
|
|
538
|
+
* 业务方(如 SSE/WS 客户端断开)可通过 `req.signal` 等接入取消链路。
|
|
539
|
+
*/
|
|
540
|
+
signal?: AbortSignal;
|
|
541
|
+
/**
|
|
542
|
+
* 覆盖本次调用的 agent 名(从 agentRegistry 查找对应元数据 / tools / sub-agents)
|
|
543
|
+
*
|
|
544
|
+
* 不传时用 `config.agent.defaultAgent`。`defaultAgent` 未设时必须显式传入,
|
|
545
|
+
* 否则抛 `AgentError`。
|
|
546
|
+
*/
|
|
547
|
+
agent?: string;
|
|
495
548
|
/**
|
|
496
549
|
* 切换 provider + model 的字符串 key(支持 llms key / `provider/model` / 纯 model 名)
|
|
497
550
|
*
|
|
@@ -503,12 +556,11 @@ interface AgentRunOptions {
|
|
|
503
556
|
/** 最大生成 token 数(透传给 LLM API) */
|
|
504
557
|
maxTokens?: number;
|
|
505
558
|
/**
|
|
506
|
-
* 启用 tracing(默认沿用全局 `config.agent.enableTracing`,全局默认 `
|
|
559
|
+
* 启用 tracing(默认沿用全局 `config.agent.enableTracing`,全局默认 `false`——
|
|
560
|
+
* opt-in,不开启零开销)。
|
|
507
561
|
*
|
|
508
562
|
* 开启时 `ReactLoopResult.trace` / `ReactLoopStreamChunk.traceEvent` 填充
|
|
509
563
|
* 结构化调用明细,详见 [trace.md](./trace.md)。
|
|
510
|
-
*
|
|
511
|
-
* 业务方在生产主路径显式传 `false` 关闭以零开销运行。
|
|
512
564
|
*/
|
|
513
565
|
enableTracing?: boolean;
|
|
514
566
|
}
|
|
@@ -565,17 +617,54 @@ interface AgentRuntimeConfig {
|
|
|
565
617
|
maxTurns?: number;
|
|
566
618
|
/** agent 调用 agent 的最大递归深度(默认 3) */
|
|
567
619
|
maxAgentDepth?: number;
|
|
620
|
+
/** 发送给 LLM 的历史 token 预算(近似估算,未设置 = 不裁剪)——透传 reactLoop,见 reactLoop.md 历史裁剪章节 */
|
|
621
|
+
maxHistoryTokens?: number;
|
|
568
622
|
/**
|
|
569
|
-
* 启用 tracing 的全局默认值(默认
|
|
623
|
+
* 启用 tracing 的全局默认值(默认 false——opt-in,不开启零开销)。
|
|
570
624
|
*
|
|
571
625
|
* 开启时 `ReactLoopResult.trace` / `ReactLoopStreamChunk.traceEvent` 填充
|
|
572
626
|
* 结构化调用明细,详见 [trace.md](./trace.md)。
|
|
573
627
|
*
|
|
574
628
|
* 单次调用可通过 `AgentRunOptions.enableTracing` 覆盖。
|
|
575
|
-
* 业务方在生产主路径显式设 `false` 关闭以零开销运行。
|
|
576
629
|
*/
|
|
577
630
|
enableTracing?: boolean;
|
|
631
|
+
/**
|
|
632
|
+
* 执行守卫(authHooks,见 [authHooks.md](./authHooks.md))
|
|
633
|
+
*
|
|
634
|
+
* `executeTool` 最开头调用(`agent.` 分流之前)——同时覆盖常规 tool 与
|
|
635
|
+
* sub-agent 递归。三种返回:`void` 放行;`{ error }` 拒绝(不执行 handler,
|
|
636
|
+
* error 回传 LLM);`{ args }` 改写后放行(多租户场景强制注入可信值,
|
|
637
|
+
* 不信 LLM 传入的标识参数)。
|
|
638
|
+
*/
|
|
639
|
+
beforeToolCall?: ToolCallGuardHook;
|
|
640
|
+
/**
|
|
641
|
+
* 审计钩子(authHooks):tool / sub-agent 成功返回后调用,返回值忽略。
|
|
642
|
+
* 异常路径不调用。
|
|
643
|
+
*/
|
|
644
|
+
afterToolCall?: AfterToolCallHook;
|
|
645
|
+
/**
|
|
646
|
+
* 可见性过滤(authHooks):`buildToolDefinitions` 组装完 LLM 可见 tools 后
|
|
647
|
+
* 调用,返回过滤后的数组。每次 `run` / `stream` 生效,含 agent-as-tool 项。
|
|
648
|
+
*/
|
|
649
|
+
filterTools?: FilterToolsHook;
|
|
578
650
|
}
|
|
651
|
+
/**
|
|
652
|
+
* beforeToolCall 的返回守卫
|
|
653
|
+
*
|
|
654
|
+
* - `{ error }`:拒绝执行,error 字符串回传 LLM
|
|
655
|
+
* - `{ args }`:以改写后的参数继续执行
|
|
656
|
+
*/
|
|
657
|
+
type ToolCallGuard = {
|
|
658
|
+
error: string;
|
|
659
|
+
} | {
|
|
660
|
+
args: Record<string, unknown>;
|
|
661
|
+
};
|
|
662
|
+
/** 执行守卫钩子签名(ctx 为请求上下文,编程式直调可能为 undefined) */
|
|
663
|
+
type ToolCallGuardHook = (name: string, args: Record<string, unknown>, ctx: FaapiContext | undefined) => void | ToolCallGuard;
|
|
664
|
+
/** 审计钩子签名(仅成功路径调用) */
|
|
665
|
+
type AfterToolCallHook = (name: string, args: Record<string, unknown>, result: unknown, ctx: FaapiContext | undefined) => void;
|
|
666
|
+
/** 可见性过滤钩子签名 */
|
|
667
|
+
type FilterToolsHook = (tools: LLMToolDefinition[], ctx: FaapiContext | undefined) => LLMToolDefinition[];
|
|
579
668
|
/**
|
|
580
669
|
* tool schema 解析结果
|
|
581
670
|
*
|
|
@@ -621,6 +710,14 @@ interface AgentDeps {
|
|
|
621
710
|
rootDir: string;
|
|
622
711
|
/** 全局 agent 配置覆盖 */
|
|
623
712
|
config?: AgentRuntimeConfig;
|
|
713
|
+
/**
|
|
714
|
+
* 请求上下文(authHooks ctx 传递链,见 [authHooks.md](./authHooks.md))
|
|
715
|
+
*
|
|
716
|
+
* 由 @faapi/agent 工厂捕获(AgentHandleFactory 签名本就接收 ctx)。
|
|
717
|
+
* 编程式直调(测试/自定义启动器)不传,钩子收到 undefined。
|
|
718
|
+
* sub-agent 递归经 subDeps 展开自动传导(同一 HTTP 请求内 ctx 不变)。
|
|
719
|
+
*/
|
|
720
|
+
ctx?: FaapiContext;
|
|
624
721
|
/** 查 agent LLM 可见元数据(对应 agentRegistry.getAgent,返回 AgentCore) */
|
|
625
722
|
getAgent: (name: string) => AgentCore | undefined;
|
|
626
723
|
/** 查 agent 完整元数据(对应 agentRegistry.getAgentEntry,返回 AgentMetadata 含 filePath/hasRun) */
|
|
@@ -732,15 +829,20 @@ declare class Agent {
|
|
|
732
829
|
/**
|
|
733
830
|
* 组装 ReactLoopConfig
|
|
734
831
|
*
|
|
735
|
-
* 1.
|
|
832
|
+
* 1. 解析有效 agent 名:`options.agent` > `deps.agentName`(`config.agent.defaultAgent`)
|
|
833
|
+
* 2. 查 agent 元数据(未注册抛 AgentError)——用 `getAgent` 拿 AgentCore
|
|
736
834
|
* (LLM-facing 字段:systemPrompt / model / maxTurns)
|
|
737
|
-
*
|
|
738
|
-
*
|
|
835
|
+
* 3. buildToolDefinitions 组装 tool 列表(用有效 agent 名查 tools / sub-agents)
|
|
836
|
+
* 4. config 字段优先级(高 → 低):`options` > agent 元数据 > 全局 AgentRuntimeConfig / deps.defaultProvider
|
|
739
837
|
*
|
|
740
838
|
* `options.model` 是字符串 key,由 {@link resolveModelKey} 解析为 provider + model
|
|
741
839
|
* (支持 llms key 精确匹配 / `provider/model` 一体化 / 纯 model 名模糊匹配)。
|
|
742
840
|
* 不传 `options.model` 时用 `deps.defaultProvider` + agent 元数据 `config.model`。
|
|
743
841
|
* 详见 [agentHandle.md](./agentHandle.md) 的「`options.model` 字符串 key 解析规则」。
|
|
842
|
+
*
|
|
843
|
+
* `options.agent` 覆盖本次调用的 agent 名——不传时用 `deps.agentName`(来自
|
|
844
|
+
* `config.agent.defaultAgent`)。`defaultAgent` 未设且 `options.agent` 未传时抛
|
|
845
|
+
* `AgentError`。
|
|
744
846
|
*/
|
|
745
847
|
private buildLoopConfig;
|
|
746
848
|
/**
|
|
@@ -842,15 +944,18 @@ declare class Agent {
|
|
|
842
944
|
* 插件 setup 时:
|
|
843
945
|
* 1. 遍历 `config.agent.llms` → 每项调 `createProvider` → `Map<providerKey, LLMProvider>`
|
|
844
946
|
* 2. 读 `config.agent.defaultLlm` → `defaultProvider`(未设时用 `llms` 第一个 key)
|
|
845
|
-
* 3. 读 `config.agent.defaultAgent
|
|
947
|
+
* 3. 读 `config.agent.defaultAgent`(可选) / `maxTurns` / `maxAgentDepth`
|
|
846
948
|
* 4. 从 `@faapi/faapi` import 注册表/加载器访问器(getAgent / getTool / resolveAgentTools /
|
|
847
949
|
* resolveSubAgents / loadAgentModule / loadToolModule)
|
|
848
950
|
* 5. `registerAgentHandleFactory` 注册工厂——每次请求时构造 [Agent](./agent.md) 实例注入到
|
|
849
951
|
* handler 的 `agent` 参数
|
|
850
952
|
*
|
|
851
|
-
* 配置缺失时(`agent.llms`
|
|
953
|
+
* 配置缺失时(`agent.llms` 未设置)跳过工厂注册并打印警告,
|
|
852
954
|
* handler 的 `agent` 参数注入 `undefined`。
|
|
853
955
|
*
|
|
956
|
+
* `defaultAgent` 可选——未设时 handler 需通过 `agent.run(input, { agent: 'name' })`
|
|
957
|
+
* 显式指定 agent 名。
|
|
958
|
+
*
|
|
854
959
|
* 详见 [plugin.md](./plugin.md)。
|
|
855
960
|
*/
|
|
856
961
|
|
|
@@ -862,4 +967,4 @@ declare class Agent {
|
|
|
862
967
|
*/
|
|
863
968
|
declare const agentPlugin: FaapiPlugin;
|
|
864
969
|
|
|
865
|
-
export { Agent, 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, agentPlugin as default, isTracingToolResult, reactLoop, reactLoopStream };
|
|
970
|
+
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, agentPlugin as default, isTracingToolResult, reactLoop, reactLoopStream };
|