@faapi/agent 5.4.0 → 6.1.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 CHANGED
@@ -36,52 +36,73 @@ declare function createOpenAIProvider(config: LlmConfig): LLMProvider;
36
36
  * 详见 [provider.md](./provider.md)。
37
37
  */
38
38
  /**
39
- * 对话消息
39
+ * 对话消息(OpenAI chat completions 规范形)
40
40
  *
41
41
  * 四种 role 与 OpenAI chat completions 一致:
42
42
  * - `system` —— 系统提示词(agent 的 systemPrompt)
43
43
  * - `user` —— 用户输入
44
- * - `assistant` —— LLM 回复(可能含 toolCalls
45
- * - `tool` —— tool 执行结果(需带 toolCallId 标识对应哪个 tool_call)
44
+ * - `assistant` —— LLM 回复(可能含 tool_calls
45
+ * - `tool` —— tool 执行结果(需带 tool_call_id 标识对应哪个 tool_call)
46
+ *
47
+ * 字段拼写与 OpenAI 线格式一致(`tool_calls` / `tool_call_id`),OpenAI provider
48
+ * 对 messages 恒等透传,观测/存储/展示消费方按 OpenAI 形状处理即可。
46
49
  */
47
50
  interface LLMMessage {
48
51
  role: 'system' | 'user' | 'assistant' | 'tool';
49
52
  /** 消息内容(assistant 角色 + tool_calls 时可能为空字符串) */
50
53
  content: string;
51
54
  /** role='tool' 时:对应的 tool_call ID(用于 LLM 关联 tool 结果) */
52
- toolCallId?: string;
55
+ tool_call_id?: string;
53
56
  /** role='assistant' 时:LLM 请求的 tool 调用(reactLoop 据此执行 tool) */
54
- toolCalls?: LLMToolCall[];
57
+ tool_calls?: LLMToolCall[];
58
+ /**
59
+ * role='assistant' 时:thinking 模型的推理内容(DeepSeek 线格式字段名)
60
+ *
61
+ * provider 响应解析产物(非流式含完整内容),供业务方展示/审计;
62
+ * **不回传 LLM API**——OpenAI provider 发送请求时剥离(DeepSeek 多轮回传
63
+ * 推理内容直接 400,OpenAI 等拒绝未知字段),reactLoop 组装历史时同样剥离。
64
+ * 兼容解析:线格式 `reasoning_content` 优先,缺失时读 `reasoning`(OpenRouter 形状)。
65
+ */
66
+ reasoning_content?: string;
55
67
  }
56
68
  /**
57
- * LLM 请求的 tool 调用
69
+ * assistant 消息内的 tool 调用(OpenAI chat completions 规范形)
58
70
  *
59
- * 由 LLM 在 assistant 消息中返回。`arguments` JSON.parse,
60
- * reactLoop 直接传给 tool 函数。
71
+ * 由 LLM 在 assistant 消息中返回。`function.arguments` 是线格式的 JSON **字符串**
72
+ * (不预解析)——消息历史可原样透传与持久化;[reactLoop](./reactLoop.md) 在执行前
73
+ * JSON.parse,tool 执行函数 / 鉴权钩子 / trace 事件拿到的都是已 parse 的对象。
61
74
  */
62
75
  interface LLMToolCall {
63
76
  /** tool call ID(provider 分配,用于匹配 tool 结果) */
64
77
  id: string;
65
- /** tool 名(匹配 LLMToolDefinition.name) */
66
- name: string;
67
- /** tool 参数(已 JSON.parse 的对象) */
68
- arguments: Record<string, unknown>;
78
+ /** 固定 `'function'`(OpenAI 线格式) */
79
+ type: 'function';
80
+ function: {
81
+ /** tool 名(匹配 LLMToolDefinition.function.name) */
82
+ name: string;
83
+ /** tool 参数(JSON 字符串,线格式原样) */
84
+ arguments: string;
85
+ };
69
86
  }
70
87
  /**
71
- * Tool 定义
88
+ * Tool 定义(OpenAI chat completions 规范形)
72
89
  *
73
90
  * 由 [reactLoop](./reactLoop.md) 从 [toolRegistry](../../faapi/src/injection/toolRegistry.md)
74
91
  * + [agentRegistry.resolveSubAgents](../../faapi/src/injection/agentRegistry.md) 组装:
75
- * - 常规 tool:`input` 来自 AST 提取的 zod schema(JSON Schema 形式)
76
- * - agent-as-tool:`input` 为自由 schema(agent 参数开放)
92
+ * - 常规 tool:`function.parameters` 来自 AST 提取的 zod schema(JSON Schema 形式)
93
+ * - agent-as-tool:`function.parameters` 为自由 schema(agent 参数开放)
77
94
  */
78
95
  interface LLMToolDefinition {
79
- /** tool 名(如 `weather.getWeather` `agent.researcher`) */
80
- name: string;
81
- /** tool 描述(对 LLM 可见,引导 LLM 选择调用) */
82
- description?: string;
83
- /** JSON Schema 对象(描述 tool 参数结构) */
84
- input: Record<string, unknown>;
96
+ /** 固定 `'function'`(OpenAI 线格式) */
97
+ type: 'function';
98
+ function: {
99
+ /** tool 名(如 `weather.getWeather` 或 `agent.researcher`) */
100
+ name: string;
101
+ /** tool 描述(对 LLM 可见,引导 LLM 选择调用) */
102
+ description?: string;
103
+ /** JSON Schema 对象(描述 tool 参数结构) */
104
+ parameters?: Record<string, unknown>;
105
+ };
85
106
  }
86
107
  /**
87
108
  * complete / stream 的入参
@@ -138,10 +159,10 @@ type LLMStopReason = 'stop' | 'tool_calls' | 'length' | 'content_filter' | 'othe
138
159
  /**
139
160
  * complete 的返回
140
161
  *
141
- * `message.toolCalls` 不为空时 stopReason 应为 `'tool_calls'`。
162
+ * `message.tool_calls` 不为空时 stopReason 应为 `'tool_calls'`。
142
163
  */
143
164
  interface LLMResponse {
144
- /** assistant 消息(含 content + 可选 toolCalls) */
165
+ /** assistant 消息(OpenAI 规范形,含 content + 可选 tool_calls) */
145
166
  message: LLMMessage;
146
167
  /** 停止原因(reactLoop 据此判断是否进入下一轮) */
147
168
  stopReason: LLMStopReason;
@@ -149,23 +170,29 @@ interface LLMResponse {
149
170
  usage?: LLMUsage;
150
171
  }
151
172
  /**
152
- * Token 用量
173
+ * Token 用量(OpenAI chat completions 规范形)
153
174
  */
154
175
  interface LLMUsage {
155
- promptTokens: number;
156
- completionTokens: number;
157
- totalTokens: number;
176
+ prompt_tokens: number;
177
+ completion_tokens: number;
178
+ total_tokens: number;
158
179
  }
159
180
  /**
160
181
  * stream 的单个 chunk
161
182
  *
183
+ * provider 内部流抽象(非 OpenAI 分片 delta 线格式):
162
184
  * - 内容流:`deltaContent` 为增量 token
163
- * - tool 调用:累积完成后在最终 chunk 一并 emit `toolCalls`
185
+ * - 推理内容流(thinking 模型):`deltaReasoning` 为推理增量(与 `deltaContent` 同为
186
+ * 增量语义,消费方按到达顺序各自拼接)
187
+ * - tool 调用:累积完成后在最终 chunk 一并 emit `toolCalls`(OpenAI 规范形,
188
+ * `function.arguments` 为 JSON 字符串)
164
189
  * - 结束:最终 chunk 含 `finishReason` + 可选 `usage`
165
190
  */
166
191
  interface LLMStreamChunk {
167
192
  /** 增量内容(streaming token) */
168
193
  deltaContent?: string;
194
+ /** 推理内容增量(thinking 模型,如 DeepSeek `reasoning_content` / OpenRouter `reasoning`) */
195
+ deltaReasoning?: string;
169
196
  /** 累积完成的 tool 调用(在最终 chunk 出现) */
170
197
  toolCalls?: LLMToolCall[];
171
198
  /** 结束原因(只在最终 chunk 出现) */
@@ -388,7 +415,7 @@ interface ReactLoopConfig {
388
415
  * 提供时以其为基础(历史应含 system):历史无 `system` 消息且配置了
389
416
  * `systemPrompt` 时自动在最前插入(agent 人格不因续跑丢失);`input` 非空时
390
417
  * 追加为新的 `user` 消息(多轮对话),为空时纯续跑。历史经 `Agent` 层结构校验
391
- * (assistant.toolCalls 与 tool 结果按 toolCallId 配对完整)。
418
+ * (assistant.tool_calls 与 tool 结果按 tool_call_id 配对完整)。
392
419
  * 续跑源见 [reactLoop.md](./reactLoop.md) 中断恢复章节。
393
420
  */
394
421
  messages?: LLMMessage[];
@@ -406,6 +433,12 @@ interface ReactLoopConfig {
406
433
  interface ReactLoopResult {
407
434
  /** 最终 assistant 消息内容 */
408
435
  content: string;
436
+ /**
437
+ * 最终 assistant 的推理内容(thinking 模型,多轮时中间轮的推理不保留;
438
+ * 无推理内容时不存在)。历史 messages 中的 assistant 消息已剥离推理内容,
439
+ * 仅此字段与 trace 的 `llm_call.response.reasoning_content` 可读。
440
+ */
441
+ reasoning?: string;
409
442
  /** 完整对话历史(system + user + assistant + tool 消息) */
410
443
  messages: LLMMessage[];
411
444
  /** 使用的轮数(含最终轮) */
@@ -425,6 +458,7 @@ interface ReactLoopResult {
425
458
  *
426
459
  * 每个 chunk 至多含一个字段:
427
460
  * - `deltaContent` — LLM 增量 token(多次 yield)
461
+ * - `deltaReasoning` — LLM 推理内容增量(thinking 模型,含中间 tool 轮)
428
462
  * - `toolCall` — tool 开始执行
429
463
  * - `toolResult` — tool 执行完成
430
464
  * - `traceEvent` — trace 事件(`enableTracing=true` 时增量推送,与上述字段互斥)
@@ -433,6 +467,8 @@ interface ReactLoopResult {
433
467
  interface ReactLoopStreamChunk {
434
468
  /** LLM 增量 token */
435
469
  deltaContent?: string;
470
+ /** LLM 推理内容增量(thinking 模型,见 reactLoop.md thinking 章节) */
471
+ deltaReasoning?: string;
436
472
  /** tool 开始执行(LLM 请求调用 tool) */
437
473
  toolCall?: {
438
474
  name: string;
@@ -445,12 +481,14 @@ interface ReactLoopStreamChunk {
445
481
  };
446
482
  /**
447
483
  * trace 事件(`enableTracing=true` 时增量推送)。
448
- * 与 deltaContent / toolCall / toolResult / done 互斥,一个 chunk 至多一个字段。
484
+ * 与 deltaContent / deltaReasoning / toolCall / toolResult / done 互斥,一个 chunk 至多一个字段。
449
485
  */
450
486
  traceEvent?: AgentTraceEvent;
451
487
  /** 循环结束 */
452
488
  done?: {
453
489
  content: string;
490
+ /** 最终轮的完整推理内容(thinking 模型;无推理内容时不存在) */
491
+ reasoning?: string;
454
492
  turns: number;
455
493
  stopReason: LLMStopReason;
456
494
  usage?: LLMUsage;
@@ -475,7 +513,7 @@ declare class ReactLoopError extends Error {
475
513
  *
476
514
  * @param input 用户输入(续跑场景可为空,历史经 `config.messages` 提供)
477
515
  * @param config 循环配置
478
- * @returns 最终结果(content + messages + turns + stopReason + usage)
516
+ * @returns 最终结果(content + reasoning + messages + turns + stopReason + usage)
479
517
  * @throws {ReactLoopError} 超出 maxTurns(`error.messages` 携带完整历史,可续跑)
480
518
  * @throws {AgentAbortError} 中断(`error.messages` 携带断点历史,可续跑)
481
519
  * @throws {Error} provider.complete 抛错时立即传播
@@ -484,7 +522,7 @@ declare function reactLoop(input: string | undefined, config: ReactLoopConfig):
484
522
  /**
485
523
  * 执行 ReAct 循环(流式)
486
524
  *
487
- * 使用 `provider.stream()` 异步迭代 chunks,yield `deltaContent` + `toolCall` + `toolResult` + `done`。
525
+ * 使用 `provider.stream()` 异步迭代 chunks,yield `deltaContent` + `deltaReasoning` + `toolCall` + `toolResult` + `done`。
488
526
  *
489
527
  * @param input 用户输入(续跑场景可为空,历史经 `config.messages` 提供)
490
528
  * @param config 循环配置
@@ -949,17 +987,17 @@ declare class Agent {
949
987
  */
950
988
  private resolveModelKey;
951
989
  /**
952
- * 组装 LLM 可见 tool 列表
990
+ * 组装 LLM 可见 tool 列表(OpenAI chat completions 规范形)
953
991
  *
954
- * 合并两个来源(按 `name` 去重,先入者保留):
992
+ * 合并两个来源(按 `function.name` 去重,先入者保留):
955
993
  * 1. **resolveAgentTools** —— agent 显式声明的 `tools` 引用
956
994
  * 2. **sub-agent** —— `resolveSubAgents` 每个包装为 `agent.<name>`
957
995
  *
958
- * 每个常规 tool 的 `input`:
996
+ * 每个常规 tool 的 `function.parameters`:
959
997
  * - `resolveToolSchema` 提供 → 用其 `jsonSchema`
960
998
  * - 未提供 / tool 无 `inputTypeName` → 自由 schema `{ type: 'object' }`
961
999
  *
962
- * sub-agent 的 `input` 始终为 `{ type: 'object' }`(agent 参数开放)。
1000
+ * sub-agent 的 `function.parameters` 始终为 `{ type: 'object' }`(agent 参数开放)。
963
1001
  */
964
1002
  private buildToolDefinitions;
965
1003
  /**