@faapi/faapi 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/cli/index.js CHANGED
@@ -2550,6 +2550,8 @@ var init_resolveInjection = __esm({
2550
2550
  // 任务子系统:TaskClient(入队/查询)
2551
2551
  log: "log",
2552
2552
  // 请求级日志器(ctx.log 同一实例:scope http,自动带 requestId 等字段)
2553
+ llm: "llm",
2554
+ // 轻量 LLM 补全通道(@faapi/agent 插件注册到 registries.llm;未加载时 undefined)
2553
2555
  // raw 系:恒原始输入(无 schema、无校验、管线永不转换),与 query/params/body
2554
2556
  // 的「校验转换后」口径二分互补
2555
2557
  rawQuery: "rawQuery",
@@ -3237,9 +3239,20 @@ function extractAgentMetadata(program, filePath, pathMeta) {
3237
3239
  agents,
3238
3240
  model,
3239
3241
  maxTurns,
3240
- inputDescription
3242
+ inputDescription,
3243
+ inputTypeName: findInputDeclaration(sourceFile) ? "Input" : void 0
3241
3244
  };
3242
3245
  }
3246
+ function findInputDeclaration(sourceFile) {
3247
+ let found = false;
3248
+ ts11.forEachChild(sourceFile, (node) => {
3249
+ if (found) return;
3250
+ if ((ts11.isInterfaceDeclaration(node) || ts11.isTypeAliasDeclaration(node)) && node.name.text === "Input") {
3251
+ found = true;
3252
+ }
3253
+ });
3254
+ return found;
3255
+ }
3243
3256
  function findRunExportOwner(sourceFile) {
3244
3257
  let result = null;
3245
3258
  ts11.forEachChild(sourceFile, (node) => {
@@ -3464,6 +3477,7 @@ function serializeAgents(agents, dist = "dist") {
3464
3477
  model: a.model,
3465
3478
  maxTurns: a.maxTurns,
3466
3479
  inputDescription: a.inputDescription,
3480
+ inputTypeName: a.inputTypeName,
3467
3481
  filePath: toProdFilePath(a.filePath, dist)
3468
3482
  }));
3469
3483
  }
@@ -3484,7 +3498,8 @@ function hydrateAgents(manifest) {
3484
3498
  agents: a.agents ?? void 0,
3485
3499
  model: a.model ?? void 0,
3486
3500
  maxTurns: a.maxTurns ?? void 0,
3487
- inputDescription: a.inputDescription ?? void 0
3501
+ inputDescription: a.inputDescription ?? void 0,
3502
+ inputTypeName: a.inputTypeName ?? void 0
3488
3503
  }));
3489
3504
  }
3490
3505
  function validateAgentList(metadata) {
@@ -3530,8 +3545,66 @@ async function generateAgentArtifacts(agents, rootDir, dist) {
3530
3545
  const serialized = serializeAgents(metadata, dist);
3531
3546
  const agentsPath = path15.resolve(rootDir, dist, AGENTS_FILE);
3532
3547
  await writeAgentsModule(serialized, agentsPath);
3548
+ await generateAgentZodArtifacts(metadata, programByFile, rootDir, dist);
3533
3549
  return metadata;
3534
3550
  }
3551
+ async function generateAgentZodArtifacts(metadata, programByFile, rootDir, dist) {
3552
+ const declared = metadata.filter((a) => a.inputTypeName);
3553
+ if (declared.length === 0) return;
3554
+ const resolversByFile = /* @__PURE__ */ new Map();
3555
+ const sources = [];
3556
+ for (const agent of declared) {
3557
+ const absPath = path15.resolve(rootDir, agent.filePath);
3558
+ const program = programByFile.get(absPath);
3559
+ if (!program) {
3560
+ throw new Error(
3561
+ `agent "${agent.name}" \u6E90\u6587\u4EF6\u4E0D\u5728 Program \u4E2D: ${agent.filePath}\u2014\u2014\u65E0\u6CD5\u751F\u6210\u5165\u53C2 schema`
3562
+ );
3563
+ }
3564
+ if (!resolversByFile.has(absPath)) {
3565
+ resolversByFile.set(absPath, createLazyTypeResolver(program, absPath));
3566
+ }
3567
+ const typeInfo = extractTypeInfo(program, absPath, agent.inputTypeName);
3568
+ sources.push({
3569
+ name: agent.name,
3570
+ filePath: absPath,
3571
+ schemaName: agent.inputTypeName,
3572
+ typeInfo
3573
+ });
3574
+ }
3575
+ await generateZodArtifacts(sources, {
3576
+ generateFileSource: generateAgentSchemaFileSource,
3577
+ resolversByFile,
3578
+ rootDir,
3579
+ dist
3580
+ });
3581
+ }
3582
+ function generateAgentSchemaFileSource(sources, resolveType, helpersImportPath) {
3583
+ const lines = ["import { z } from 'zod';"];
3584
+ const schemaBlocks = [];
3585
+ for (const source of sources) {
3586
+ if (!source.typeInfo) continue;
3587
+ const block = [`// agent ${source.name} \u2192 ${source.schemaName}`];
3588
+ const schemaCode = generateZodSchemaSource(
3589
+ source.typeInfo,
3590
+ resolveType,
3591
+ source.schemaName,
3592
+ false
3593
+ ).replace(/^import \{ z \} from 'zod';\s*\n\s*\n/, "");
3594
+ block.push(schemaCode);
3595
+ block.push("");
3596
+ schemaBlocks.push(block.join("\n"));
3597
+ }
3598
+ const allSchemaCode = schemaBlocks.join("\n");
3599
+ if (helpersImportPath && usesCoerceHelpers(allSchemaCode)) {
3600
+ lines.push(
3601
+ `import { coerceNumber, coerceBoolean, coerceMap, coerceSet } from '${helpersImportPath}';`
3602
+ );
3603
+ }
3604
+ lines.push("");
3605
+ lines.push(...schemaBlocks);
3606
+ return lines.join("\n").replace(/\n+$/, "\n");
3607
+ }
3535
3608
  var AGENTS_FILE;
3536
3609
  var init_generateAgentArtifacts = __esm({
3537
3610
  "src/cli/generateAgentArtifacts.ts"() {
@@ -3540,6 +3613,9 @@ var init_generateAgentArtifacts = __esm({
3540
3613
  init_createProgram();
3541
3614
  init_prodPaths();
3542
3615
  init_atomicWrite();
3616
+ init_extractHandlerTypes();
3617
+ init_generateZodSchema();
3618
+ init_generateZodArtifacts();
3543
3619
  AGENTS_FILE = "faapi-agents.js";
3544
3620
  }
3545
3621
  });
@@ -7724,6 +7800,20 @@ function createAgentHandleStore() {
7724
7800
  }
7725
7801
  };
7726
7802
  }
7803
+ function createLlmChannelStore() {
7804
+ let current = null;
7805
+ return {
7806
+ register(channel) {
7807
+ current = channel;
7808
+ },
7809
+ get() {
7810
+ return current ?? void 0;
7811
+ },
7812
+ clear() {
7813
+ current = null;
7814
+ }
7815
+ };
7816
+ }
7727
7817
  function createTaskHandleStore() {
7728
7818
  let currentFactory = null;
7729
7819
  return {
@@ -7780,7 +7870,8 @@ function createAppRegistries() {
7780
7870
  const task = createTaskRegistry();
7781
7871
  const agentHandle = createAgentHandleStore();
7782
7872
  const taskHandle = createTaskHandleStore();
7783
- return { tool, agent, skill, task, agentHandle, taskHandle };
7873
+ const llm = createLlmChannelStore();
7874
+ return { tool, agent, skill, task, agentHandle, taskHandle, llm };
7784
7875
  }
7785
7876
  var defaultRegistries;
7786
7877
  var init_registries = __esm({
@@ -7864,6 +7955,10 @@ function getBuiltinInjectionValue(type, ctx, body) {
7864
7955
  // 任务子系统:注入 TaskClient(入队/查询);未注册工厂(无 app 编排)时 undefined
7865
7956
  case "tasks":
7866
7957
  return ctx.registries ? ctx.registries.taskHandle.get(ctx) : void 0;
7958
+ // 轻量 LLM 补全通道:@faapi/agent 插件注册到 registries.llm;与请求上下文无关,
7959
+ // get 无参。插件未加载(或编程式直调 ctx 无 registries)时 undefined
7960
+ case "llm":
7961
+ return ctx.registries ? ctx.registries.llm.get() : void 0;
7867
7962
  // 请求级日志器:与 ctx.log 同一实例(scope http,自动带 requestId/method/path 字段)
7868
7963
  case "log":
7869
7964
  return ctx.log;
@@ -9497,6 +9592,7 @@ async function runTaskInWorker(options) {
9497
9592
  job: taskCtx.job
9498
9593
  },
9499
9594
  registries: options.registries,
9595
+ llms: options.llms,
9500
9596
  log: options.log
9501
9597
  });
9502
9598
  } catch (err) {
@@ -9625,6 +9721,9 @@ function createTaskQueue(deps) {
9625
9721
  job: { id: job.id, name: job.name, attempt: job.attempt }
9626
9722
  },
9627
9723
  registries: snapshotRegistries(),
9724
+ // agent.llms 纯数据快照随 workerData 下发,worker 内动态加载 @faapi/agent
9725
+ // 重建轻量补全通道(taskCtx.llm)——llms 未配置时不传
9726
+ llms: deps.llms,
9628
9727
  // 日志配置随 workerData 下发(纯数据),worker 内联重建 taskCtx.log,
9629
9728
  // 条目回传宿主走统一管道(scope/fields 与进程内路径一致)
9630
9729
  log: {
@@ -9654,6 +9753,8 @@ function createTaskQueue(deps) {
9654
9753
  config: deps.config,
9655
9754
  job: { id: job.id, name: job.name, attempt: job.attempt },
9656
9755
  registries: registriesView,
9756
+ // 轻量补全通道惰性读取(插件晚于队列构造注册,执行时刻取值才可见)
9757
+ llm: deps.llm?.get(),
9657
9758
  log: createLogger(`task:${job.name}`, {
9658
9759
  fields: { jobId: job.id, task: job.name, attempt: job.attempt }
9659
9760
  }),
@@ -10330,6 +10431,10 @@ async function createAppBase(options) {
10330
10431
  config,
10331
10432
  resourcesDir,
10332
10433
  registries: createTaskRegistriesView(registries),
10434
+ // 轻量补全通道:进程内路径经 store 惰性读取(插件注册后的活引用);
10435
+ // 隔离路径传 agent.llms 纯数据快照(worker 内重建)
10436
+ llm: registries.llm,
10437
+ llms: config?.agent?.llms,
10333
10438
  driver: taskDriver,
10334
10439
  onFailed: config?.task?.onFailed
10335
10440
  });
@@ -10467,6 +10572,7 @@ async function createAppBase(options) {
10467
10572
  registries.task.clear();
10468
10573
  registries.agentHandle.clear();
10469
10574
  registries.taskHandle.clear();
10575
+ registries.llm.clear();
10470
10576
  setActiveResourcesDir(null);
10471
10577
  if (!server.listening) {
10472
10578
  app.server = null;