@faapi/agent 6.30.0 → 6.32.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
@@ -1,4 +1,5 @@
1
- import { LlmConfig, AgentToolDescriptor, FaapiContext, AgentCore, AgentMetadata, ToolMetadata, ToolModule, FaapiPlugin } from '@faapi/faapi';
1
+ import { LlmConfig, LlmComplete, AgentToolDescriptor, FaapiContext, AgentCore, AgentMetadata, ToolMetadata, ToolModule, FaapiPlugin } from '@faapi/faapi';
2
+ export { LlmComplete, LlmCompleteOptions } from '@faapi/faapi';
2
3
 
3
4
  /**
4
5
  * LLM Provider 错误
@@ -11,12 +12,31 @@ declare class LLMProviderError extends Error {
11
12
  readonly status?: number;
12
13
  /** 响应体摘要(前 500 字符,便于诊断) */
13
14
  readonly body?: string;
15
+ /**
16
+ * 实际发起的 HTTP 尝试次数(≥1,含失败尝试)
17
+ *
18
+ * 重试耗尽抛出时由重试循环回填(构造时未知,故非 readonly);
19
+ * 首次尝试即失败的确定性错误(4xx 直抛)为 1,未回填时视为 1。
20
+ */
21
+ attempts?: number;
14
22
  constructor(message: string, options?: {
15
23
  status?: number;
16
24
  body?: string;
17
25
  cause?: unknown;
18
26
  });
19
27
  }
28
+ /**
29
+ * LLM 请求超时
30
+ *
31
+ * {@link LLMProviderError} 的超时子类——`instanceof LLMTimeoutError` 可编程区分
32
+ * 「超时」与「网络错误」(两者 status 均为 undefined,仅靠 message 字符串无法区分)。
33
+ * 超时计入重试(与 429/5xx/网络错误同策略)。
34
+ */
35
+ declare class LLMTimeoutError extends LLMProviderError {
36
+ constructor(message: string, options?: {
37
+ cause?: unknown;
38
+ });
39
+ }
20
40
  /**
21
41
  * 创建 OpenAI 兼容 LLMProvider
22
42
  *
@@ -121,11 +141,23 @@ interface LLMCompleteRequest {
121
141
  temperature?: number;
122
142
  /** 最大生成 token 数 */
123
143
  maxTokens?: number;
144
+ /**
145
+ * 本次请求的超时(毫秒,调用级覆盖)
146
+ *
147
+ * 缺省回落 `LlmConfig.timeoutMs`,两者皆未设置时无超时。
148
+ * 超时触发抛 `LLMTimeoutError`(计入重试,与 429/5xx/网络错误同策略)。
149
+ */
150
+ timeoutMs?: number;
151
+ /**
152
+ * 本次请求的重试上限(调用级覆盖)
153
+ *
154
+ * 缺省回落 `LlmConfig.maxRetries`(默认 2)。仅 429/5xx/网络错误/超时计入重试。
155
+ */
156
+ maxRetries?: number;
124
157
  /**
125
158
  * 取消信号(透传到底层 HTTP 请求)
126
159
  *
127
- * abort 时请求中断并抛 `AgentAbortError`;与 `LlmConfig.timeoutMs` 的
128
- * 超时信号组合生效(任一触发即中断)。
160
+ * abort 时请求中断并抛 `AgentAbortError`;与超时信号组合生效(任一触发即中断)。
129
161
  */
130
162
  signal?: AbortSignal;
131
163
  }
@@ -168,6 +200,12 @@ interface LLMResponse {
168
200
  stopReason: LLMStopReason;
169
201
  /** token 用量(部分 provider 不返回) */
170
202
  usage?: LLMUsage;
203
+ /**
204
+ * 实际发起的 HTTP 尝试次数(≥1,含失败尝试)
205
+ *
206
+ * 供失败钩子/日志观测重试消耗;provider 未提供时视为 1。
207
+ */
208
+ attempts?: number;
171
209
  }
172
210
  /**
173
211
  * Token 用量(OpenAI chat completions 规范形)
@@ -228,6 +266,20 @@ interface LLMProvider {
228
266
  */
229
267
  declare function createProvider(config: LlmConfig): LLMProvider;
230
268
 
269
+ /**
270
+ * 创建轻量补全通道
271
+ *
272
+ * @param deps.llms provider 配置映射(来自 `config.agent.llms`,同源零新增配置)
273
+ * @param deps.providers 已构建的 provider 实例映射(插件传入——与 agent 循环共享
274
+ * 同一单例);缺省时按 `llms` 逐项 `createProvider` 现场构建
275
+ * (隔离 worker 重建 / 测试形态)
276
+ * @returns 补全通道(`complete` 方法)
277
+ */
278
+ declare function createLightComplete(deps: {
279
+ llms: Record<string, LlmConfig>;
280
+ providers?: Map<string, LLMProvider>;
281
+ }): LlmComplete;
282
+
231
283
  /**
232
284
  * 单次 agent.run() / agent.stream() 的结构化调用明细
233
285
  *
@@ -918,6 +970,16 @@ interface AgentDeps {
918
970
  loadToolModule: (filePath: string, functionName: string) => Promise<ToolModule>;
919
971
  /** tool input 的 schema 解析(Phase 3.5 实现,可选) */
920
972
  resolveToolSchema?: (tool: ToolMetadata) => Promise<ToolSchemaResolution | undefined>;
973
+ /**
974
+ * sub-agent 派发入参 schema 解析(可选)
975
+ *
976
+ * sub 元数据声明 `inputTypeName`(handler.ts 顶层 `Input` 导出,构建期生成 zod.js)
977
+ * 时解析其 JSON Schema + 校验函数——`buildToolDefinitions` 用作派发工具 parameters、
978
+ * `executeSubAgent` 执行前校验。通常与 `resolveToolSchema` 是同一实现
979
+ * ([createToolSchemaResolver](./toolSchemaResolver.md) 返回值同时满足两个签名)。
980
+ * 未提供时派发一律走单字段 `input` 模式(编程式组装的向后兼容)。
981
+ */
982
+ resolveAgentInputSchema?: (agent: AgentMetadata) => Promise<ToolSchemaResolution | undefined>;
921
983
  }
922
984
  /**
923
985
  * Agent 系统级错误
@@ -1086,19 +1148,8 @@ declare class Agent {
1086
1148
  */
1087
1149
  private resolveExternalProvider;
1088
1150
  /**
1089
- * 解析 `options.model` 字符串 key → provider + model
1090
- *
1091
- * 规则见 [agentHandle.md](./agentHandle.md) 的「`options.model` 字符串 key 解析规则」。
1092
- * 无默认 provider——`key` 未传时用 agent 元数据 `config.model` 作为缺省 key;
1093
- * 两者皆无抛 `AgentError`(要求调用方传 `options.model` 或 `options.provider`)。
1094
- * 1. 精确匹配 `deps.providers` 的 key → 该 provider + 其 `models` 第一个 key
1095
- * (该 provider 未声明 `models` 时回落 `meta.model`)
1096
- * 2. 含 `/` → `provider/model` 形式,`deps.providers.get(provider)` + 该 model
1097
- * (要求该 model 在 `deps.llms[provider].models` 里)
1098
- * 3. 不含 `/` 且非 provider key → 在所有 provider 的 `models` 里按 model 名查找
1099
- * - 唯一 → 该 provider + 该 model
1100
- * - 多个 → 抛 `AgentError`(要求用 `provider/model` 消歧)
1101
- * - 无 → 抛 `AgentError`
1151
+ * 解析 `options.model` 字符串 key → provider + model(模块级 [resolveModelKey] 的
1152
+ * Agent 方法包装——meta.model 作缺省 key,与 [lightComplete](./lightComplete.md) 共用同一解析实现)
1102
1153
  *
1103
1154
  * @throws {AgentError} key 与 `meta.model` 均缺省;key 解析失败(provider/model
1104
1155
  * 不存在或歧义)
@@ -1115,11 +1166,29 @@ declare class Agent {
1115
1166
  * - `resolveToolSchema` 提供 → 用其 `jsonSchema`
1116
1167
  * - 未提供 / tool 无 `inputTypeName` → 自由 schema `{ type: 'object' }`
1117
1168
  *
1118
- * sub-agent 的 `function.parameters` 为显式单字段 `input` schema(string,必填)——
1119
- * 严格遵循 JSON schema 的模型对无属性 `{ type: 'object' }` 只回 `{}`,派发上下文
1120
- * 传不进子代理;description 用 sub 元数据 `inputDescription`,未声明用默认文案。
1169
+ * sub-agent 的 `function.parameters` 按「派发入参 schema 声明」二选一:
1170
+ * - **富 schema 模式**——sub 元数据声明 `inputTypeName`(handler.ts 顶层 `Input`
1171
+ * 导出)且 `resolveAgentInputSchema` 已接线 → 用解析出的 JSON Schema(结构性
1172
+ * 交接单,字段 JSDoc 即主控可见参数描述)。声明了但解析为 `undefined` 是产物
1173
+ * 异常(dev/prod 全量生成下 zod.js 不可能合法缺失),抛 `AgentError` 不静默降级
1174
+ * - **单字段 `input` 模式**——未声明 `Input` 或 resolver 未接线 → 显式单字段
1175
+ * schema(string,必填)——严格遵循 JSON schema 的模型对无属性 `{ type: 'object' }`
1176
+ * 只回 `{}`,派发上下文传不进子代理;description 用 sub 元数据 `inputDescription`,
1177
+ * 未声明用默认文案
1121
1178
  */
1122
1179
  private buildToolDefinitions;
1180
+ /**
1181
+ * 解析 sub-agent 派发工具的 parameters(富 schema / 单字段 input 二选一)
1182
+ *
1183
+ * 富 schema 判定需要完整元数据的 `inputTypeName`(`AgentCore` 不含)——经
1184
+ * `getAgentEntry` 查询;未声明或 `resolveAgentInputSchema` 未接线时返回单字段
1185
+ * `input` schema(历史行为,完全向后兼容)。`inputDescription` 仍从 `AgentCore`
1186
+ * 读取(LLM 可见字段的既定来源,DB skill 同样可声明)。
1187
+ *
1188
+ * @throws {AgentError} 声明了 `inputTypeName` 且 resolver 已接线但解析为
1189
+ * `undefined`(zod.js 缺失/损坏的产物异常,不静默退回单字段模式)
1190
+ */
1191
+ private getSubAgentParameters;
1123
1192
  /**
1124
1193
  * tool 执行路由(由 reactLoop 调用)
1125
1194
  *
@@ -1142,9 +1211,15 @@ declare class Agent {
1142
1211
  * sub-agent 递归执行
1143
1212
  *
1144
1213
  * 1. `maxAgentDepth` 防护——超限抛 {@link AgentRecursionError}
1145
- * 2. sub-agent handler 导出 `run` 时调自定义 `mod.run(args)`(无 trace、无结构化
1214
+ * 2. **派发入参校验(富 schema 模式)**——sub 元数据声明 `inputTypeName` 且
1215
+ * `resolveAgentInputSchema` 已接线时执行前 `validate(args)`:失败返回
1216
+ * `{ error }` 回灌主控 LLM 重试(与常规 tool 校验失败同语义,不进入子循环——
1217
+ * 省掉一次注定失败的子 agent 轮次);通过后以 coerce 后的 value 继续传导。
1218
+ * 声明了但解析为 `undefined`(zod.js 缺失/损坏)抛 `AgentError` 显式失败;
1219
+ * resolver 未接线或未声明 `Input` 时跳过校验,行为与历史版本一致
1220
+ * 3. sub-agent handler 导出 `run` 时调自定义 `mod.run(args)`(无 trace、无结构化
1146
1221
  * usage 可卷——直接返回业务结果,其 token 不进入父 run 台账)
1147
- * 3. 无 `run` 时调 `subAgent.run(stringify(args), { agent, provider, model, enableTracing })`
1222
+ * 4. 无 `run` 时调 `subAgent.run(stringify(args), { agent, provider, model, enableTracing })`
1148
1223
  * 走默认 reactLoop——继承父调用的 provider,sub 元数据声明 `model` 时优先用自身的,
1149
1224
  * 未声明时沿用父 model
1150
1225
  *
@@ -1176,27 +1251,40 @@ declare class Agent {
1176
1251
  */
1177
1252
 
1178
1253
  /**
1179
- * 创建带 mtime 缓存的 tool schema 解析器
1254
+ * schema 定位的最小来源结构——tool 元数据(`ToolMetadata`)与 agent 完整元数据
1255
+ * (`AgentMetadata`,派发入参 schema 声明场景)均满足,同一 resolver 服务两类来源
1256
+ * (`AgentDeps.resolveToolSchema` + `AgentDeps.resolveAgentInputSchema`),缓存按
1257
+ * zod.js 路径天然分流
1258
+ */
1259
+ type SchemaSourceRef = {
1260
+ filePath: string;
1261
+ inputTypeName?: string;
1262
+ };
1263
+ /**
1264
+ * 创建带 mtime 缓存的 schema 解析器(tool input 与 agent 派发入参共用)
1180
1265
  *
1181
- * 返回的函数满足 `AgentDeps.resolveToolSchema` 签名,供两处共用:
1182
- * - `@faapi/agent` 插件 setup(传 `ctx.rootDir`,root + sub-agent 共享同一闭包缓存)
1266
+ * 返回的函数满足 `AgentDeps.resolveToolSchema` / `AgentDeps.resolveAgentInputSchema`
1267
+ * 两个签名(参数为最小结构 `SchemaSourceRef`),供三处共用:
1268
+ * - `@faapi/agent` 插件 setup(传 `ctx.rootDir`,同一实例注入两个 deps,root +
1269
+ * sub-agent 共享同一闭包缓存)
1183
1270
  * - 任务内组装 [Agent](./agent.md)(`TaskContext` 无 rootDir,缺省 `process.cwd()`——
1184
1271
  * faapi 服务进程 cwd 即项目根;建议任务文件模块级创建一次)
1185
1272
  *
1186
1273
  * **缓存语义**(闭包级 `Map<key, { mtimeMs, resolution }>`):
1187
1274
  * - 缓存键 `zodPath#inputTypeName`,每次查找 `statSync` 一次做 mtime 自校验——
1188
- * mtime 变化即重新解析(dev reloadTools 重生成 zod.js 后自愈,prod 产物固化永远命中)
1189
- * - in-flight Promise 直接入缓存:同一 tool 的并发调用共享同一次解析
1275
+ * mtime 变化即重新解析(dev reload 重生成 zod.js 后自愈,prod 产物固化永远命中)
1276
+ * - in-flight Promise 直接入缓存:同一来源的并发调用共享同一次解析
1190
1277
  * - 每次 `createToolSchemaResolver` 调用返回独立缓存的 resolver
1191
1278
  *
1192
- * zod.js 缺失 / tool 无 `inputTypeName` 时解析结果为 `undefined`——agent 用
1193
- * 自由 schema `{ type: 'object' }`,LLM 自由传参。
1279
+ * zod.js 缺失 / 无 `inputTypeName` 时解析结果为 `undefined`——tool 侧用自由 schema
1280
+ * `{ type: 'object' }`;agent 派发侧对「声明了 `inputTypeName` 但解析为 `undefined`」
1281
+ * 的产物异常语义(显式抛错)由 [agent.ts](./agent.ts) 定义,本工厂只如实返回。
1194
1282
  *
1195
1283
  * @param options.rootDir 项目根目录(缺省 `process.cwd()`)
1196
1284
  */
1197
1285
  declare function createToolSchemaResolver(options?: {
1198
1286
  rootDir?: string;
1199
- }): (tool: ToolMetadata) => Promise<ToolSchemaResolution | undefined>;
1287
+ }): (ref: SchemaSourceRef) => Promise<ToolSchemaResolution | undefined>;
1200
1288
 
1201
1289
  /**
1202
1290
  * @faapi/agent faapi 插件——注册 agent handle 工厂,让 handler 的 `agent` 参数注入可用的 Agent 实例
@@ -1247,4 +1335,4 @@ declare function createToolSchemaResolver(options?: {
1247
1335
  */
1248
1336
  declare const agentPlugin: FaapiPlugin;
1249
1337
 
1250
- 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 };
1338
+ 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, LLMTimeoutError, type LLMToolCall, type LLMToolDefinition, type LLMUsage, type LlmCallEvent, type ReactLoopConfig, ReactLoopError, type ReactLoopResult, type ReactLoopStreamChunk, type SchemaSourceRef, type SubAgentCallEvent, type SubAgentDelta, type SubAgentDeltaEmitter, type SubAgentToolResult, type ToolCallEvent, type ToolExecutor, type ToolSchemaResolution, type TracingToolResult, createLightComplete, createOpenAIProvider, createProvider, createToolSchemaResolver, agentPlugin as default, isSubAgentToolResult, isTracingToolResult, reactLoop, reactLoopStream };