@faapi/faapi 3.0.0 → 3.2.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/cli/index.js +381 -250
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +287 -95
- package/dist/index.js +336 -219
- package/dist/index.js.map +1 -1
- package/dist/testing.js +334 -221
- package/dist/testing.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -178,13 +178,48 @@ interface ResponseConfig {
|
|
|
178
178
|
}) => unknown;
|
|
179
179
|
}
|
|
180
180
|
/**
|
|
181
|
-
*
|
|
181
|
+
* model 级配置(Phase 3.5)
|
|
182
182
|
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
* 额外字段透传给 LLM API(如 temperature / max_tokens)。
|
|
183
|
+
* 挂在 provider 下的单个 model 配置,model 特定字段透传给 LLM API
|
|
184
|
+
* (覆盖 provider 级同名字段)。空对象 `{}` 表示用 provider 级默认。
|
|
186
185
|
*
|
|
187
|
-
*
|
|
186
|
+
* ```ts
|
|
187
|
+
* models: {
|
|
188
|
+
* 'gpt-4o': {}, // 用 provider 级默认
|
|
189
|
+
* 'gpt-4o-mini': { temperature: 0.5 }, // 覆盖 temperature
|
|
190
|
+
* }
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
interface LlmModelConfig {
|
|
194
|
+
[key: string]: unknown;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* LLM provider 配置(Phase 2.4,Phase 3.5 改为嵌套级联结构)
|
|
198
|
+
*
|
|
199
|
+
* 嵌套级联:provider 在外层,model 在 `models` 下挂多个。
|
|
200
|
+
* provider 级字段(`apiKey` / `baseURL`)共享给所有 model;
|
|
201
|
+
* model 级字段在 `models[modelName]` 里覆盖 provider 级同名字段。
|
|
202
|
+
*
|
|
203
|
+
* `config.agent.llms` 的 key 是 provider 名(如 `'openai'` / `'anthropic'`),
|
|
204
|
+
* `config.agent.defaultLlm` 指定默认 provider key(不传时用 `llms` 第一个 key)。
|
|
205
|
+
*
|
|
206
|
+
* 由 Phase 3.2 的 `@faapi/agent` 插件读取,调 `createProvider` 创建实例存 Map。
|
|
207
|
+
*
|
|
208
|
+
* ```ts
|
|
209
|
+
* llms: {
|
|
210
|
+
* openai: {
|
|
211
|
+
* provider: 'openai',
|
|
212
|
+
* apiKey: process.env.OPENAI_API_KEY,
|
|
213
|
+
* baseURL: 'https://api.openai.com/v1',
|
|
214
|
+
* models: { 'gpt-4o': {}, 'gpt-4o-mini': { temperature: 0.5 } },
|
|
215
|
+
* },
|
|
216
|
+
* anthropic: {
|
|
217
|
+
* provider: 'anthropic',
|
|
218
|
+
* apiKey: process.env.ANTHROPIC_API_KEY,
|
|
219
|
+
* models: { 'claude-3-5-sonnet': {} },
|
|
220
|
+
* },
|
|
221
|
+
* }
|
|
222
|
+
* ```
|
|
188
223
|
*/
|
|
189
224
|
interface LlmConfig {
|
|
190
225
|
/**
|
|
@@ -199,10 +234,6 @@ interface LlmConfig {
|
|
|
199
234
|
* 如 `process.env.OPENAI_API_KEY`。
|
|
200
235
|
*/
|
|
201
236
|
apiKey?: string;
|
|
202
|
-
/**
|
|
203
|
-
* 默认模型(如 'gpt-4o'),agent 自身 `config.model` 优先
|
|
204
|
-
*/
|
|
205
|
-
model?: string;
|
|
206
237
|
/**
|
|
207
238
|
* API 基础 URL(可选,用于 OpenAI 兼容 API 如 Azure OpenAI / 中转服务)
|
|
208
239
|
*
|
|
@@ -210,14 +241,24 @@ interface LlmConfig {
|
|
|
210
241
|
*/
|
|
211
242
|
baseURL?: string;
|
|
212
243
|
/**
|
|
213
|
-
*
|
|
244
|
+
* 该 provider 下挂的 model 列表(key 是 model 名)
|
|
245
|
+
*
|
|
246
|
+
* handler 通过 `agent.run(input, { model: 'gpt-4o' })` 切换 model,
|
|
247
|
+
* 框架按 model 名在所有 provider 的 `models` 里查找定位 provider(详见
|
|
248
|
+
* [agentHandle](../../agent/src/agentHandle.md) 的 Run-level 覆盖优先级表)。
|
|
249
|
+
* model 级字段(如 `temperature`)覆盖 provider 级同名字段。
|
|
250
|
+
*/
|
|
251
|
+
models: Record<string, LlmModelConfig>;
|
|
252
|
+
/**
|
|
253
|
+
* 其他透传参数(provider 级,如 temperature / top_p / max_tokens)
|
|
214
254
|
*
|
|
215
255
|
* 这些字段原样传给 LLM API,由 provider 适配器处理。
|
|
256
|
+
* model 级 `models[modelName]` 的同名字段优先。
|
|
216
257
|
*/
|
|
217
258
|
[key: string]: unknown;
|
|
218
259
|
}
|
|
219
260
|
/**
|
|
220
|
-
* agent 子系统全局配置(Phase 2.4
|
|
261
|
+
* agent 子系统全局配置(Phase 2.4,Phase 3.5 LLM 配置改为嵌套级联)
|
|
221
262
|
*
|
|
222
263
|
* 提供 agent 子系统的全局默认值,所有字段均可选,未设置时用框架默认值。
|
|
223
264
|
* agent 自身 `config.maxTurns` / `config.model` 优先于全局配置。
|
|
@@ -226,11 +267,17 @@ interface LlmConfig {
|
|
|
226
267
|
* import type { FaapiConfig } from '@faapi/faapi';
|
|
227
268
|
* export default {
|
|
228
269
|
* agent: {
|
|
229
|
-
*
|
|
270
|
+
* llms: {
|
|
271
|
+
* openai: {
|
|
272
|
+
* provider: 'openai',
|
|
273
|
+
* apiKey: process.env.OPENAI_API_KEY,
|
|
274
|
+
* models: { 'gpt-4o': {}, 'gpt-4o-mini': { temperature: 0.5 } },
|
|
275
|
+
* },
|
|
276
|
+
* },
|
|
277
|
+
* defaultLlm: 'openai',
|
|
230
278
|
* defaultAgent: 'researcher',
|
|
231
279
|
* maxTurns: 10,
|
|
232
280
|
* maxAgentDepth: 3,
|
|
233
|
-
* defaultTools: ['weather.getWeather'],
|
|
234
281
|
* },
|
|
235
282
|
* } satisfies FaapiConfig;
|
|
236
283
|
* ```
|
|
@@ -239,11 +286,22 @@ interface LlmConfig {
|
|
|
239
286
|
*/
|
|
240
287
|
interface AgentConfig {
|
|
241
288
|
/**
|
|
242
|
-
* LLM
|
|
289
|
+
* LLM provider 配置映射(Phase 3.5 改为嵌套级联结构,key 是 provider 名)
|
|
290
|
+
*
|
|
291
|
+
* 值是 [LlmConfig](含 `models`)。plugin setup 时遍历每个 LlmConfig 调
|
|
292
|
+
* `createProvider` 创建实例存 Map,handler 通过 `agent.run(input, { model })`
|
|
293
|
+
* 切换 provider + model(详见 [agentHandle](../../agent/src/agentHandle.md))。
|
|
243
294
|
*
|
|
244
295
|
* 未设置时 Phase 3.x 插件无法调用 LLM,agent 的 `run` 函数仍可手动实现。
|
|
245
296
|
*/
|
|
246
|
-
|
|
297
|
+
llms?: Record<string, LlmConfig>;
|
|
298
|
+
/**
|
|
299
|
+
* 默认 provider key(Phase 3.5)
|
|
300
|
+
*
|
|
301
|
+
* `agent.run` 不传 `options.model` 时用此 key 对应的 provider 实例。
|
|
302
|
+
* 未设置时用 `llms` 的第一个 key(`Object.keys(llms)[0]`)。
|
|
303
|
+
*/
|
|
304
|
+
defaultLlm?: string;
|
|
247
305
|
/**
|
|
248
306
|
* 默认 agent 名,用于 `agent` 参数注入([injectParams](../injection/injectParams.md) Phase 2.3)
|
|
249
307
|
*
|
|
@@ -252,13 +310,6 @@ interface AgentConfig {
|
|
|
252
310
|
* 注入 `AgentHandle`(含可调用 `run`)。
|
|
253
311
|
*/
|
|
254
312
|
defaultAgent?: string;
|
|
255
|
-
/**
|
|
256
|
-
* 默认 tool 列表,所有 agent 都可用(无需在每个 agent 的 `tools` 重复声明)
|
|
257
|
-
*
|
|
258
|
-
* 与 agent 自身 `tools` 合并(都加入可用 tool 集合,去重)。
|
|
259
|
-
* 由 `@faapi/agent` 插件在 setup 时合并到 agent 的 tool 引用列表。
|
|
260
|
-
*/
|
|
261
|
-
defaultTools?: string[];
|
|
262
313
|
/**
|
|
263
314
|
* 默认最大对话轮数(覆盖 agent 自身 `config.maxTurns`,agent 自身配置优先)
|
|
264
315
|
*
|
|
@@ -271,6 +322,31 @@ interface AgentConfig {
|
|
|
271
322
|
* 默认值由 Phase 3.x 的 @faapi/agent 插件定义(如 3)。
|
|
272
323
|
*/
|
|
273
324
|
maxAgentDepth?: number;
|
|
325
|
+
/**
|
|
326
|
+
* 启用 tracing 的全局默认值(默认 true)
|
|
327
|
+
*
|
|
328
|
+
* 开启时 `agent.run()` / `agent.stream()` 返回的 `result.trace` /
|
|
329
|
+
* `chunk.traceEvent` 填充结构化调用明细(按轮次组织的 LLM 调用、tool 调用、
|
|
330
|
+
* sub-agent 嵌套调用事件,含 timing 与 token 用量)。
|
|
331
|
+
*
|
|
332
|
+
* 三层覆盖优先级:`AgentRunOptions.enableTracing` > agent 自身配置 >
|
|
333
|
+
* 此全局配置 > 默认 `true`。
|
|
334
|
+
*
|
|
335
|
+
* 业务方在生产主路径(高 QPS 端点)显式设 `false` 关闭以零开销运行:
|
|
336
|
+
*
|
|
337
|
+
* ```ts
|
|
338
|
+
* import type { FaapiConfig } from '@faapi/faapi';
|
|
339
|
+
* export default {
|
|
340
|
+
* agent: {
|
|
341
|
+
* enableTracing: false,
|
|
342
|
+
* llms: { openai: { provider: 'openai', apiKey: '...', models: { 'gpt-4o': {} } } },
|
|
343
|
+
* },
|
|
344
|
+
* } satisfies FaapiConfig;
|
|
345
|
+
* ```
|
|
346
|
+
*
|
|
347
|
+
* 详见 `@faapi/agent` 的 [trace](../../agent/src/trace.md) 文档。
|
|
348
|
+
*/
|
|
349
|
+
enableTracing?: boolean;
|
|
274
350
|
}
|
|
275
351
|
/**
|
|
276
352
|
* faapi 配置文件类型
|
|
@@ -403,19 +479,25 @@ interface FaapiConfig {
|
|
|
403
479
|
/**
|
|
404
480
|
* agent 子系统全局配置(Phase 2.4)
|
|
405
481
|
*
|
|
406
|
-
* 提供 agent 子系统的全局默认值:LLM 提供方、默认 agent
|
|
482
|
+
* 提供 agent 子系统的全局默认值:LLM 提供方、默认 agent、
|
|
407
483
|
* 最大对话轮数、agent 调用 agent 的最大递归深度。
|
|
408
484
|
*
|
|
409
485
|
* agent 自身 `config.maxTurns` / `config.model` 优先于全局配置。
|
|
410
|
-
*
|
|
486
|
+
* tool 引用列表只在每个 agent 自身的 `config.tools` 里声明(无全局共享 defaultTools)。
|
|
411
487
|
*
|
|
412
488
|
* ```ts
|
|
413
489
|
* import type { FaapiConfig } from '@faapi/faapi';
|
|
414
490
|
* export default {
|
|
415
491
|
* agent: {
|
|
416
|
-
*
|
|
492
|
+
* llms: {
|
|
493
|
+
* openai: {
|
|
494
|
+
* provider: 'openai',
|
|
495
|
+
* apiKey: process.env.OPENAI_API_KEY,
|
|
496
|
+
* models: { 'gpt-4o': {} },
|
|
497
|
+
* },
|
|
498
|
+
* },
|
|
499
|
+
* defaultLlm: 'openai',
|
|
417
500
|
* defaultAgent: 'researcher',
|
|
418
|
-
* defaultTools: ['weather.getWeather'],
|
|
419
501
|
* maxTurns: 10,
|
|
420
502
|
* maxAgentDepth: 3,
|
|
421
503
|
* },
|
|
@@ -775,47 +857,65 @@ declare function collectRouteSchemaSources(routes: RouteManifest, rootDir?: stri
|
|
|
775
857
|
};
|
|
776
858
|
|
|
777
859
|
/**
|
|
778
|
-
* Agent
|
|
860
|
+
* Agent 的 LLM 可见核心字段
|
|
779
861
|
*
|
|
780
|
-
*
|
|
781
|
-
* (
|
|
782
|
-
* (JSDoc 描述、`@agent` 覆盖名、config 块字段)。
|
|
862
|
+
* 描述"agent 是什么"——LLM 真正需要消费的字段,**不含**代码本体加载细节
|
|
863
|
+
* (filePath / hasRun)。文件型 agent 与 DB-driven skill 都实现此接口。
|
|
783
864
|
*
|
|
784
|
-
*
|
|
785
|
-
*
|
|
865
|
+
* - 文件型 agent:由 [AgentMetadata](./extractAgentMetadata.md) 继承扩展,
|
|
866
|
+
* 额外含 `filePath` / `hasRun`(代码本体加载用)
|
|
867
|
+
* - DB-driven skill:业务方 plugin 从 DB 字段映射到本接口即可,无需填占位值
|
|
868
|
+
* (skill 无源文件,不走 `loadAgentModule`,自然不读 filePath / hasRun)
|
|
786
869
|
*
|
|
787
|
-
*
|
|
788
|
-
*
|
|
789
|
-
* - `filePath` / `hasConfig` / `hasRun` — 由 `pathMeta` 透传
|
|
790
|
-
* - `description` — JSDoc 注释块自由文本(对 LLM 可见)
|
|
791
|
-
* - `systemPrompt` / `tools` / `agents` / `model` / `maxTurns` — config 块字面量提取
|
|
870
|
+
* `@faapi/agent` 子包的 `Agent` 类、`agentRegistry` 查询入口、`asTool` 包装
|
|
871
|
+
* 都消费 `AgentCore`,实现"agent 与 skill 走同一运行时链路"。
|
|
792
872
|
*/
|
|
793
|
-
interface
|
|
873
|
+
interface AgentCore {
|
|
794
874
|
/** agent 名(`@agent` JSDoc 覆盖值 或 目录推导值) */
|
|
795
875
|
name: string;
|
|
796
|
-
/** JSDoc 描述(agent
|
|
876
|
+
/** JSDoc 描述(agent 描述,对 LLM 可见),无 JSDoc 或 JSDoc 无自由文本时为 `undefined` */
|
|
797
877
|
description?: string;
|
|
798
|
-
/**
|
|
799
|
-
filePath: string;
|
|
800
|
-
/** 是否导出 config 块(从 `pathMeta` 透传) */
|
|
801
|
-
hasConfig: boolean;
|
|
802
|
-
/** 是否导出 run 函数(从 `pathMeta` 透传) */
|
|
803
|
-
hasRun: boolean;
|
|
804
|
-
/** 系统提示词(config 块字面量提取),无/非字面量时为 `undefined` */
|
|
878
|
+
/** 系统提示词(config 块字面量提取),无/非字面量时为 `undefined` */
|
|
805
879
|
systemPrompt?: string;
|
|
806
|
-
/** agent 显式声明可用的 tool 引用列表(config 块字面量提取)
|
|
880
|
+
/** agent 显式声明可用的 tool 引用列表(config 块字面量提取),无/含非字面量元素时为 `undefined` */
|
|
807
881
|
tools?: string[];
|
|
808
|
-
/** 可调用的其他 agent 名列表(config 块字面量提取)
|
|
882
|
+
/** 可调用的其他 agent 名列表(config 块字面量提取),无/含非字面量元素时为 `undefined` */
|
|
809
883
|
agents?: string[];
|
|
810
|
-
/** LLM 模型名(config 块字面量提取)
|
|
884
|
+
/** LLM 模型名(config 块字面量提取),无/非字面量时为 `undefined` */
|
|
811
885
|
model?: string;
|
|
812
|
-
/** 最大对话轮数(config 块字面量提取)
|
|
886
|
+
/** 最大对话轮数(config 块字面量提取),无/非字面量时为 `undefined` */
|
|
813
887
|
maxTurns?: number;
|
|
814
888
|
}
|
|
889
|
+
/**
|
|
890
|
+
* Agent 完整元数据(文件型 agent)
|
|
891
|
+
*
|
|
892
|
+
* 继承 [AgentCore](./extractAgentMetadata.md) 的 LLM 字段,额外扩展**代码本体加载细节**:
|
|
893
|
+
* - `filePath` — `loadAgentModule` 加载 `handler.js` 产物提取 `run` 函数用
|
|
894
|
+
* - `hasRun` — 是否导出 `run` 函数(`Agent.executeSubAgent` 据此决定走自定义 run
|
|
895
|
+
* 还是默认 reactLoop)
|
|
896
|
+
*
|
|
897
|
+
* DB-driven skill 不实现此接口(无源文件,无需加载),只实现 `AgentCore`。
|
|
898
|
+
*
|
|
899
|
+
* 由 [extractAgentMetadata](./extractAgentMetadata.md) 产出,合并路径推导字段
|
|
900
|
+
* (来自 [scanAgents](../agents/scanAgents.md) 的 `AgentManifest`)与 AST 提取字段
|
|
901
|
+
* (JSDoc 描述、`@agent` 覆盖名、config 块字段)。
|
|
902
|
+
*
|
|
903
|
+
* 字段来源:
|
|
904
|
+
* - `name` — `@agent` JSDoc 覆盖值,或 `pathMeta.name`(目录推导)
|
|
905
|
+
* - `filePath` / `hasRun` — 由 `pathMeta` 透传
|
|
906
|
+
* - `description` — JSDoc 注释块自由文本(对 LLM 可见)
|
|
907
|
+
* - `systemPrompt` / `tools` / `agents` / `model` / `maxTurns` — config 块字面量提取
|
|
908
|
+
*/
|
|
909
|
+
interface AgentMetadata extends AgentCore {
|
|
910
|
+
/** 源码相对路径(从 `pathMeta` 透传),`loadAgentModule` 据此加载 `handler.js` 提取 `run` */
|
|
911
|
+
filePath: string;
|
|
912
|
+
/** 是否导出 `run` 函数(从 `pathMeta` 透传),`Agent.executeSubAgent` 据此选择自定义 run / 默认 reactLoop */
|
|
913
|
+
hasRun: boolean;
|
|
914
|
+
}
|
|
815
915
|
/**
|
|
816
916
|
* 路径推导的 agent 元数据(由 [scanAgents](../agents/scanAgents.ts) 计算)
|
|
817
917
|
*
|
|
818
|
-
* 透传到 [AgentMetadata](./extractAgentMetadata.ts)
|
|
918
|
+
* 透传到 [AgentMetadata](./extractAgentMetadata.ts) 输出,与 AST 提取字段合并。
|
|
819
919
|
* 与 [ToolPathMeta](./extractToolMetadata.md) 对称。
|
|
820
920
|
*/
|
|
821
921
|
interface AgentPathMeta {
|
|
@@ -823,14 +923,37 @@ interface AgentPathMeta {
|
|
|
823
923
|
name: string;
|
|
824
924
|
/** 源码相对路径(如 `src/agents/researcher/handler.ts`) */
|
|
825
925
|
filePath: string;
|
|
826
|
-
/** 是否导出
|
|
827
|
-
hasConfig: boolean;
|
|
828
|
-
/** 是否导出 run 函数(scanAgents 正则检测) */
|
|
926
|
+
/** 是否导出 `run` 函数(scanAgents 正则检测) */
|
|
829
927
|
hasRun: boolean;
|
|
830
928
|
}
|
|
831
929
|
|
|
832
930
|
/**
|
|
833
|
-
* Tool
|
|
931
|
+
* Tool 的 LLM 可见核心字段
|
|
932
|
+
*
|
|
933
|
+
* 描述"tool 是什么"——LLM 真正需要消费的字段(发往 LLM 的 tool 定义只含
|
|
934
|
+
* `name` / `description` / input schema),**不含**代码本体加载细节
|
|
935
|
+
* (`filePath` / `functionName` / `inputTypeName`)。
|
|
936
|
+
*
|
|
937
|
+
* 与 [AgentCore](./extractAgentMetadata.md) 对称——LLM-facing 字段与代码加载
|
|
938
|
+
* 细节分离,便于未来扩展(如 DB-driven tool 只实现 `ToolCore` 即可)。
|
|
939
|
+
*
|
|
940
|
+
* `toolRegistry` 查询入口 / `@faapi/agent` 子包的 `buildToolDefinitions`
|
|
941
|
+
* 都消费 `ToolCore` 字段组装 LLM tool 列表。
|
|
942
|
+
*/
|
|
943
|
+
interface ToolCore {
|
|
944
|
+
/** tool 名(`@tool` JSDoc 覆盖值 或 路径推导值) */
|
|
945
|
+
name: string;
|
|
946
|
+
/** JSDoc 描述(tool 描述,对 LLM 可见),无 JSDoc 或 JSDoc 无自由文本时为 `undefined` */
|
|
947
|
+
description?: string;
|
|
948
|
+
}
|
|
949
|
+
/**
|
|
950
|
+
* Tool 完整元数据(文件型 tool)
|
|
951
|
+
*
|
|
952
|
+
* 继承 [ToolCore](./extractToolMetadata.md) 的 LLM 字段,额外扩展**代码本体加载细节**:
|
|
953
|
+
* - `filePath` — `loadToolModule` 加载 `handler.js` 产物定位函数用
|
|
954
|
+
* - `functionName` — 源码导出函数名(不受 `@tool` 覆盖影响,AST 定位 + 运行时 resolveExport 用)
|
|
955
|
+
* - `inputTypeName` — 第一个参数的 TypeReference 名(供 [extractTypeInfo](./extractHandlerTypes.md)
|
|
956
|
+
* 生成 zod schema;运行时 `resolveToolSchema` 据此定位 `zod.js`)
|
|
834
957
|
*
|
|
835
958
|
* 由 [extractToolMetadata](./extractToolMetadata.md) 产出,合并路径推导字段
|
|
836
959
|
* (来自 [scanTools](../tools/scanTools.md) 的 `ToolManifest`)与 AST 提取字段
|
|
@@ -838,15 +961,11 @@ interface AgentPathMeta {
|
|
|
838
961
|
*
|
|
839
962
|
* 字段来源:
|
|
840
963
|
* - `name` — `@tool` JSDoc 覆盖值,或 `pathMeta.name`(路径推导)
|
|
841
|
-
* - `filePath` / `functionName` — 由 `pathMeta` 透传
|
|
842
964
|
* - `description` — JSDoc 注释块自由文本(对 LLM 可见)
|
|
965
|
+
* - `filePath` / `functionName` — 由 `pathMeta` 透传
|
|
843
966
|
* - `inputTypeName` — 第一个参数的 TypeReference 名(供 [extractTypeInfo](./extractHandlerTypes.md) 生成 zod schema)
|
|
844
967
|
*/
|
|
845
|
-
interface ToolMetadata {
|
|
846
|
-
/** tool 名(`@tool` JSDoc 覆盖值 或 路径推导值) */
|
|
847
|
-
name: string;
|
|
848
|
-
/** JSDoc 描述(tool 描述,对 LLM 可见),无 JSDoc 或 JSDoc 无自由文本时为 `undefined` */
|
|
849
|
-
description?: string;
|
|
968
|
+
interface ToolMetadata extends ToolCore {
|
|
850
969
|
/** 第一个参数的 interface/type 名(用于生成 zod schema),
|
|
851
970
|
* 无参数/参数无类型标注/参数为内联类型字面量时为 `undefined` */
|
|
852
971
|
inputTypeName?: string;
|
|
@@ -868,12 +987,36 @@ interface ToolPathMeta {
|
|
|
868
987
|
}
|
|
869
988
|
|
|
870
989
|
/**
|
|
871
|
-
* 按名查找单个 agent
|
|
990
|
+
* 按名查找单个 agent 的 LLM 可见元数据
|
|
991
|
+
*
|
|
992
|
+
* 返回 [AgentCore](../ast/extractAgentMetadata.md) 字段(name / description /
|
|
993
|
+
* systemPrompt / tools / agents / model / maxTurns),**不含** `filePath` / `hasRun`。
|
|
994
|
+
*
|
|
995
|
+
* 仅查文件 registry(编译期 `faapi-agents.js` 产物来源)。**不 fallback 到
|
|
996
|
+
* [skillRegistry](./skillRegistry.ts)**——skill 与 agent 职责正交不耦合,
|
|
997
|
+
* skill 由业务方 plugin 内部使用,不参与 agent 查询链路。
|
|
998
|
+
*
|
|
999
|
+
* 用于 LLM-facing 场景(`agents` 参数注入、`asTool` 描述、
|
|
1000
|
+
* `resolveAgentTools` / `resolveSubAgents` 解析)。
|
|
1001
|
+
* 加载 handler.js 执行 `run` 函数请用 [getAgentEntry](#getAgentEntry)。
|
|
872
1002
|
*
|
|
873
1003
|
* @param name agent 名(如 `researcher`,含 `@agent` 覆盖值)
|
|
1004
|
+
* @returns `AgentCore` 或 `undefined`(未注册)
|
|
1005
|
+
*/
|
|
1006
|
+
declare function getAgent(name: string): AgentCore | undefined;
|
|
1007
|
+
/**
|
|
1008
|
+
* 按名查找单个 agent 的完整元数据(含代码加载细节)
|
|
1009
|
+
*
|
|
1010
|
+
* 返回 [AgentMetadata](../ast/extractAgentMetadata.md) —— 继承 AgentCore
|
|
1011
|
+
* 额外含 `filePath` / `hasRun`,供 `@faapi/agent` 子包 `loadAgentModule`
|
|
1012
|
+
* 加载 handler.js 执行自定义 `run` 函数。
|
|
1013
|
+
*
|
|
1014
|
+
* 调用方需先判断 `entry?.hasRun` 再决定是否加载 handler.js。
|
|
1015
|
+
*
|
|
1016
|
+
* @param name agent 名
|
|
874
1017
|
* @returns `AgentMetadata` 或 `undefined`(未注册)
|
|
875
1018
|
*/
|
|
876
|
-
declare function
|
|
1019
|
+
declare function getAgentEntry(name: string): AgentMetadata | undefined;
|
|
877
1020
|
/**
|
|
878
1021
|
* agent 包装为 tool 的描述符
|
|
879
1022
|
*
|
|
@@ -884,6 +1027,10 @@ declare function getAgent(name: string): AgentMetadata | undefined;
|
|
|
884
1027
|
*
|
|
885
1028
|
* `name` 加 `agent.` 前缀避免与常规 tool 冲突,reactLoop 据此识别 sub-agent 递归。
|
|
886
1029
|
* 不含 input schema——agent `run` 函数参数为开放式(任意 JSON),无类型约束。
|
|
1030
|
+
*
|
|
1031
|
+
* `metadata` 为 [AgentCore](../ast/extractAgentMetadata.md) 类型——reactLoop 只消费
|
|
1032
|
+
* LLM-facing 字段(systemPrompt / model / maxTurns);加载 handler.js 执行 `run`
|
|
1033
|
+
* 函数由 `@faapi/agent` 子包通过 [getAgentEntry](#getAgentEntry) 单独获取。
|
|
887
1034
|
*/
|
|
888
1035
|
interface AgentToolDescriptor {
|
|
889
1036
|
/** 标识此 tool 实际是 agent(reactLoop 据此走 sub-agent 递归) */
|
|
@@ -894,19 +1041,20 @@ interface AgentToolDescriptor {
|
|
|
894
1041
|
agentName: string;
|
|
895
1042
|
/** 描述(对 LLM 可见,来自 `agent.description`),无 JSDoc 描述时为 `undefined` */
|
|
896
1043
|
description?: string;
|
|
897
|
-
/** agent
|
|
898
|
-
metadata:
|
|
1044
|
+
/** agent LLM 可见元数据引用(reactLoop 取 `systemPrompt` / `model` / `maxTurns`) */
|
|
1045
|
+
metadata: AgentCore;
|
|
899
1046
|
}
|
|
900
1047
|
/**
|
|
901
1048
|
* 解析 agent 可用 tool 集合
|
|
902
1049
|
*
|
|
903
1050
|
* 只返回 agent 显式声明的 tool(agent config 块的 `tools` 字段)。
|
|
904
|
-
* 不在此处合并全局 `defaultTools`——`defaultTools` 的合并由 `@faapi/agent` 的
|
|
905
|
-
* `Agent.buildToolDefinitions` 在更上层完成(与 sub-agent 一起按 `name` 去重)。
|
|
906
1051
|
* `resolveAgentTools` 只关心 agent 自身显式声明的部分,职责单一。
|
|
1052
|
+
* sub-agent 的合并由 `@faapi/agent` 的 `Agent.buildToolDefinitions` 在更上层完成(按 `name` 去重)。
|
|
907
1053
|
*
|
|
908
1054
|
* agent 必须显式声明用哪些 tool,显式优于隐式。
|
|
909
1055
|
*
|
|
1056
|
+
* 通过 [getAgent](#getAgent) 查询文件型 agent,不 fallback 到 skillRegistry。
|
|
1057
|
+
*
|
|
910
1058
|
* `tools` 中未在 toolRegistry 找到的 tool 名静默跳过(tool 可选可用,不强制存在)。
|
|
911
1059
|
*
|
|
912
1060
|
* 跨注册表依赖 [toolRegistry](./toolRegistry.ts) 的 `getTool`,
|
|
@@ -920,7 +1068,14 @@ declare function resolveAgentTools(name: string): ToolMetadata[];
|
|
|
920
1068
|
* 解析 agent 可调用的子 agent 集合
|
|
921
1069
|
*
|
|
922
1070
|
* 读 `agent.agents` 字段([extractAgentMetadata](../ast/extractAgentMetadata.md)
|
|
923
|
-
* 提取的 `config.agents`
|
|
1071
|
+
* 提取的 `config.agents` 字面量列表),按名查找已注册文件型 agent。
|
|
1072
|
+
*
|
|
1073
|
+
* 不 fallback 到 skillRegistry——skill 不参与 sub-agent 递归,
|
|
1074
|
+
* 父 agent 的 `agents` 列表只能引用文件型 agent 名。
|
|
1075
|
+
*
|
|
1076
|
+
* 返回 `AgentCore[]`(LLM-facing 字段,供 `@faapi/agent` 子包包装为
|
|
1077
|
+
* `AgentToolDescriptor` 发给 LLM)。加载 sub-agent handler.js 执行 `run` 函数
|
|
1078
|
+
* 由 `@faapi/agent` 子包通过 [getAgentEntry](#getAgentEntry) 单独获取。
|
|
924
1079
|
*
|
|
925
1080
|
* reactLoop 组装 LLM tool 列表:
|
|
926
1081
|
* ```ts
|
|
@@ -931,34 +1086,26 @@ declare function resolveAgentTools(name: string): ToolMetadata[];
|
|
|
931
1086
|
* ```
|
|
932
1087
|
*
|
|
933
1088
|
* @param name agent 名
|
|
934
|
-
* @returns `
|
|
1089
|
+
* @returns `AgentCore[]`(`agents` 未设置 / agent 未注册返回空数组)
|
|
935
1090
|
*/
|
|
936
|
-
declare function resolveSubAgents(name: string):
|
|
1091
|
+
declare function resolveSubAgents(name: string): AgentCore[];
|
|
937
1092
|
|
|
938
1093
|
/**
|
|
939
1094
|
* 加载后的 agent 模块
|
|
940
1095
|
*
|
|
941
|
-
* 与 [ToolModule](./loadToolModule.md) 对称——agent
|
|
942
|
-
*
|
|
1096
|
+
* 与 [ToolModule](./loadToolModule.md) 对称——agent 的代码本体只有可选的 `run` 函数
|
|
1097
|
+
* (自定义 agent 运行逻辑,替代默认 reactLoop)。
|
|
943
1098
|
*
|
|
944
|
-
*
|
|
945
|
-
*
|
|
946
|
-
*
|
|
1099
|
+
* > `config` 字段已移除——`AgentMetadata` 已含 AST 提取的字面量字段
|
|
1100
|
+
* > (systemPrompt / tools / agents / model / maxTurns),`AgentModule.config`
|
|
1101
|
+
* > 原本用于运行时拿到完整 config 对象(含动态字段),但 `executeSubAgent`
|
|
1102
|
+
* > 拿到 `mod.config` 后从不读取(run 函数在自己模块内直接引用 config 变量),
|
|
1103
|
+
* > 属于死链路,故移除。
|
|
947
1104
|
*
|
|
948
|
-
* `AgentMetadata`(从 `faapi-agents.js`
|
|
949
|
-
*
|
|
1105
|
+
* `AgentMetadata`(从 `faapi-agents.js` 水合)已含字面量字段,本模块仅用于
|
|
1106
|
+
* 在运行时拿到 `run` 函数引用。
|
|
950
1107
|
*/
|
|
951
1108
|
interface AgentModule {
|
|
952
|
-
/**
|
|
953
|
-
* agent 配置对象(含运行时字段)
|
|
954
|
-
*
|
|
955
|
-
* `hasConfig` 为 true 时一定存在;为 false 时为 `undefined`。
|
|
956
|
-
* 可能是对象字面量(`export const config = {...}`)或函数返回值(`export function config() { return {...} }`)。
|
|
957
|
-
*
|
|
958
|
-
* 函数形式:本模块调用 `config()` 拿到返回值(无参调用,与 AST 阶段的字面量提取不同——
|
|
959
|
-
* 运行时可拿到动态求值结果)。
|
|
960
|
-
*/
|
|
961
|
-
config: Record<string, unknown> | undefined;
|
|
962
1109
|
/**
|
|
963
1110
|
* 自定义 agent 运行函数(可选)
|
|
964
1111
|
*
|
|
@@ -968,7 +1115,7 @@ interface AgentModule {
|
|
|
968
1115
|
run: ((...args: unknown[]) => unknown) | undefined;
|
|
969
1116
|
}
|
|
970
1117
|
/**
|
|
971
|
-
* 动态 import agent handler 文件并提取 `
|
|
1118
|
+
* 动态 import agent handler 文件并提取 `run` 导出
|
|
972
1119
|
*
|
|
973
1120
|
* Dev 按需编译模式(Vite 风格):先 `ensureCompiled` 确保产物存在再 import,
|
|
974
1121
|
* 避免 import 不存在的文件污染 Vite SSR 内部状态(详见 [loadRouteModule](./loadRouteModule.md))。
|
|
@@ -976,20 +1123,18 @@ interface AgentModule {
|
|
|
976
1123
|
*
|
|
977
1124
|
* 与 [loadToolModule](./loadToolModule.md) 的差异:
|
|
978
1125
|
* - tool 按 `functionName` 提取单个函数(校验为 function)
|
|
979
|
-
* - agent
|
|
980
|
-
*
|
|
981
|
-
* 本模块自动调用拿到返回值(与 AST 阶段仅提字面量不同——运行时拿动态值)
|
|
1126
|
+
* - agent 只提取 `run`(函数,可选)——config 块字段已在 AST 阶段提取为字面量,
|
|
1127
|
+
* 运行时无需再加载 config 对象
|
|
982
1128
|
*
|
|
983
1129
|
* 错误传递:
|
|
984
1130
|
* - 编译失败 → 抛 "Failed to compile agent module"
|
|
985
1131
|
* - import 失败 → 抛 "Failed to load agent module"
|
|
986
1132
|
*
|
|
987
1133
|
* @param filePath agent handler 文件的绝对路径(产物形式,如 `dist/agents/researcher/handler.js`)
|
|
988
|
-
* @param hasConfig 是否应提取 config 导出(来自 `AgentMetadata.hasConfig`)
|
|
989
1134
|
* @param hasRun 是否应提取 run 导出(来自 `AgentMetadata.hasRun`)
|
|
990
1135
|
* @param rootDir 项目根目录(按需编译模式用,可选)
|
|
991
1136
|
*/
|
|
992
|
-
declare function loadAgentModule(filePath: string,
|
|
1137
|
+
declare function loadAgentModule(filePath: string, hasRun: boolean, rootDir?: string): Promise<AgentModule>;
|
|
993
1138
|
|
|
994
1139
|
/**
|
|
995
1140
|
* 加载后的 tool 模块
|
|
@@ -1064,6 +1209,52 @@ declare function loadToolSchema(tool: ToolMetadata, rootDir?: string): Promise<T
|
|
|
1064
1209
|
*/
|
|
1065
1210
|
declare function getTool(name: string): ToolMetadata | undefined;
|
|
1066
1211
|
|
|
1212
|
+
/**
|
|
1213
|
+
* 水合 skill 注册表(全量替换)
|
|
1214
|
+
*
|
|
1215
|
+
* 业务方 plugin `lifecycle.onReady` 启动期调用:全量查 DB → 转 `AgentCore[]`
|
|
1216
|
+
* → 调本函数灌入。与 `hydrateAgentRegistry` 同构,全量替换而非增量。
|
|
1217
|
+
*
|
|
1218
|
+
* 运行时增量更新场景(DB change stream)用 [upsertSkill](#upsertSkill) /
|
|
1219
|
+
* [removeSkill](#removeSkill),不走本函数。
|
|
1220
|
+
*
|
|
1221
|
+
* @param skills 从 DB / 外部源加载并转好的 `AgentCore[]`
|
|
1222
|
+
*/
|
|
1223
|
+
declare function hydrateSkillRegistry(skills: AgentCore[]): void;
|
|
1224
|
+
/**
|
|
1225
|
+
* 单条增改 skill(运行时增量)
|
|
1226
|
+
*
|
|
1227
|
+
* 监听 DB change stream 的 `insert` / `update` 事件时调用。
|
|
1228
|
+
* `Map.set` 原子操作,并发安全(多请求同时 upsert 最后一次 wins)。
|
|
1229
|
+
*
|
|
1230
|
+
* 同名 skill 覆盖(更新),不重复累积。
|
|
1231
|
+
*
|
|
1232
|
+
* @param core skill 的 LLM 可见元数据
|
|
1233
|
+
*/
|
|
1234
|
+
declare function upsertSkill(core: AgentCore): void;
|
|
1235
|
+
/**
|
|
1236
|
+
* 单条删除 skill(运行时增量)
|
|
1237
|
+
*
|
|
1238
|
+
* 监听 DB change stream 的 `delete` 事件时调用。
|
|
1239
|
+
* 幂等:删除不存在的 name 静默无操作,不抛错。
|
|
1240
|
+
*
|
|
1241
|
+
* @param name skill 名
|
|
1242
|
+
*/
|
|
1243
|
+
declare function removeSkill(name: string): void;
|
|
1244
|
+
/**
|
|
1245
|
+
* 按名查单个 skill
|
|
1246
|
+
*
|
|
1247
|
+
* @param name skill 名
|
|
1248
|
+
* @returns `AgentCore` 或 `undefined`(未注册)
|
|
1249
|
+
*/
|
|
1250
|
+
declare function getSkill(name: string): AgentCore | undefined;
|
|
1251
|
+
/**
|
|
1252
|
+
* 返回所有已注册 skill
|
|
1253
|
+
*
|
|
1254
|
+
* 返回副本,调用方修改不影响内部状态(与 `listAgents` / `listTools` 同构)。
|
|
1255
|
+
*/
|
|
1256
|
+
declare function listSkills(): AgentCore[];
|
|
1257
|
+
|
|
1067
1258
|
/**
|
|
1068
1259
|
* agent handle 工厂注册表(单例)
|
|
1069
1260
|
*
|
|
@@ -1140,7 +1331,8 @@ declare const ROUTE_NOT_FOUND = "ROUTE_NOT_FOUND";
|
|
|
1140
1331
|
declare const METHOD_NOT_ALLOWED = "METHOD_NOT_ALLOWED";
|
|
1141
1332
|
declare const INTERNAL_ERROR = "INTERNAL_ERROR";
|
|
1142
1333
|
declare const MODULE_LOAD_ERROR = "MODULE_LOAD_ERROR";
|
|
1143
|
-
|
|
1334
|
+
declare const PAYLOAD_TOO_LARGE = "PAYLOAD_TOO_LARGE";
|
|
1335
|
+
type ErrorCode = typeof VALIDATION_ERROR | typeof ROUTE_NOT_FOUND | typeof METHOD_NOT_ALLOWED | typeof INTERNAL_ERROR | typeof MODULE_LOAD_ERROR | typeof PAYLOAD_TOO_LARGE;
|
|
1144
1336
|
|
|
1145
1337
|
declare class FaapiError extends Error {
|
|
1146
1338
|
readonly code: ErrorCode;
|
|
@@ -1317,4 +1509,4 @@ type ProdApp = AppBase;
|
|
|
1317
1509
|
*/
|
|
1318
1510
|
declare function createProdApp(options?: CreateAppOptions): Promise<ProdApp>;
|
|
1319
1511
|
|
|
1320
|
-
export { type AgentConfig, type AgentHandleFactory, type AgentMetadata, type AgentModule, type AgentPathMeta, type AgentToolDescriptor, type ProdApp as App, CorsOptions, type CreateAppOptions, type DevApp, type FaapiConfig, FaapiContext, FaapiError, FaapiMiddleware, type FaapiPlugin, type HandlerTypeInfo, HelmetOptions, type InjectOptions, type InjectResponse, InjectorMap, InternalError, type LifecycleContext, type LifecycleHooks, type LlmConfig, LoggerOptions, MethodNotAllowedError, ModuleLoadError, type PluginContext, type PluginDeclaration, type ProdApp, type PropertyType, type RequestHandler, type ResponseConfig, RouteManifest, RouteNotFoundError, type RouteSchemaSource, type RuntimeType, SchemaExtractionError, type ToolMetadata, type ToolModule, type ToolPathMeta, type ToolSchemaModule, type TypeConstraint, type UpgradeHandler, ValidationError, type ValidationErrorCode, type ValidationIssue, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, clearAgentHandleFactory, collectRouteSchemaSources, createProdApp as createApp, createDevApp, createProdApp, createProgram, extractTypeInfo, getAgent, getApp, getInputTypeForMethod, getTool, invalidateProgramCache, loadAgentModule, loadConfig, loadEnv, loadToolModule, loadToolSchema, registerAgentHandleFactory, resolveAgentTools, resolveSubAgents, resolveTypeNode };
|
|
1512
|
+
export { type AgentConfig, type AgentCore, type AgentHandleFactory, type AgentMetadata, type AgentModule, type AgentPathMeta, type AgentToolDescriptor, type ProdApp as App, CorsOptions, type CreateAppOptions, type DevApp, type FaapiConfig, FaapiContext, FaapiError, FaapiMiddleware, type FaapiPlugin, type HandlerTypeInfo, HelmetOptions, type InjectOptions, type InjectResponse, InjectorMap, InternalError, type LifecycleContext, type LifecycleHooks, type LlmConfig, type LlmModelConfig, LoggerOptions, MethodNotAllowedError, ModuleLoadError, type PluginContext, type PluginDeclaration, type ProdApp, type PropertyType, type RequestHandler, type ResponseConfig, RouteManifest, RouteNotFoundError, type RouteSchemaSource, type RuntimeType, SchemaExtractionError, type ToolCore, type ToolMetadata, type ToolModule, type ToolPathMeta, type ToolSchemaModule, type TypeConstraint, type UpgradeHandler, ValidationError, type ValidationErrorCode, type ValidationIssue, type WsContext, type WsEventHandlers, type WsHandler, type WsSocket, clearAgentHandleFactory, collectRouteSchemaSources, createProdApp as createApp, createDevApp, createProdApp, createProgram, extractTypeInfo, getAgent, getAgentEntry, getApp, getInputTypeForMethod, getSkill, getTool, hydrateSkillRegistry, invalidateProgramCache, listSkills, loadAgentModule, loadConfig, loadEnv, loadToolModule, loadToolSchema, registerAgentHandleFactory, removeSkill, resolveAgentTools, resolveSubAgents, resolveTypeNode, upsertSkill };
|