@aaac/runtime 0.1.22 → 0.1.24

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.js CHANGED
@@ -23,7 +23,7 @@ import {
23
23
  emitToolExecOpen,
24
24
  emitToolchainClose,
25
25
  emitToolchainOpen
26
- } from "./chunk-FNB3PE74.js";
26
+ } from "./chunk-3HT5BILT.js";
27
27
  import {
28
28
  buildSdkFunctionTools
29
29
  } from "./chunk-LONLL3K2.js";
@@ -912,9 +912,19 @@ async function runTask(adapter, taskId, context, options) {
912
912
  if (spanClosed) return;
913
913
  spanClosed = true;
914
914
  const durationMs = Date.now() - taskExecStart;
915
+ const lastTokenUsage = adapter.getLastTokenUsage?.();
915
916
  taskEnvelope.execution_metadata = {
916
917
  ...taskEnvelope.execution_metadata ?? {},
917
- duration_ms: durationMs
918
+ duration_ms: durationMs,
919
+ ...lastTokenUsage ? {
920
+ token_usage: {
921
+ input_tokens: lastTokenUsage.inputTokens,
922
+ output_tokens: lastTokenUsage.outputTokens,
923
+ ...lastTokenUsage.totalTokens !== void 0 ? { total_tokens: lastTokenUsage.totalTokens } : {},
924
+ ...lastTokenUsage.cacheReadTokens !== void 0 ? { cache_read_input_tokens: lastTokenUsage.cacheReadTokens } : {},
925
+ ...lastTokenUsage.cacheCreationTokens !== void 0 ? { cache_creation_input_tokens: lastTokenUsage.cacheCreationTokens } : {}
926
+ }
927
+ } : {}
918
928
  };
919
929
  emitSubagentClose(
920
930
  options?.observer,
@@ -1011,7 +1021,8 @@ Please re-execute the entire task.`
1011
1021
  outcome = await pluginRegistry.runAfterTask(taskId, outcome);
1012
1022
  }
1013
1023
  closeStatus = outcome.status;
1014
- return { outcome, follow_ups_used: followUpsUsed, retries_used: retriesUsed };
1024
+ const finalTokenUsage = adapter.getLastTokenUsage?.() ?? void 0;
1025
+ return { outcome, follow_ups_used: followUpsUsed, retries_used: retriesUsed, tokenUsage: finalTokenUsage };
1015
1026
  } catch (err) {
1016
1027
  closeStatus = "error";
1017
1028
  closeCause = err.message;
@@ -1204,7 +1215,8 @@ function resolveInvocation(invocation) {
1204
1215
  onOptionalStep: invocation.hooks?.onOptionalStep,
1205
1216
  progressOutput: invocation.progressOutput,
1206
1217
  observer: invocation.observer,
1207
- toolchain: invocation.toolchain
1218
+ toolchain: invocation.toolchain,
1219
+ libraryTools: invocation.libraryTools
1208
1220
  }
1209
1221
  };
1210
1222
  }
@@ -1257,6 +1269,8 @@ async function runWorkflow(adapterOrFactory, workflowIdOrInvocation, optionsOrRe
1257
1269
  const workflowStart = Date.now();
1258
1270
  let wfResult;
1259
1271
  let sessionError;
1272
+ const tokenAccumulator = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
1273
+ let hasTokenData = false;
1260
1274
  try {
1261
1275
  let extractPath2 = function(obj, path) {
1262
1276
  const cleanPath = path.startsWith("$.") ? path.slice(2) : path;
@@ -1349,7 +1363,8 @@ async function runWorkflow(adapterOrFactory, workflowIdOrInvocation, optionsOrRe
1349
1363
  handoffSchemas: registries?.handoffSchemas,
1350
1364
  progressOutput: options.progressOutput,
1351
1365
  observer: options.observer,
1352
- parentSpanId: iterationSpanId ?? sessionSpanId
1366
+ parentSpanId: iterationSpanId ?? sessionSpanId,
1367
+ libraryTools: options.libraryTools
1353
1368
  });
1354
1369
  return {
1355
1370
  step_index: i,
@@ -1357,7 +1372,8 @@ async function runWorkflow(adapterOrFactory, workflowIdOrInvocation, optionsOrRe
1357
1372
  outcome: taskResult2.outcome,
1358
1373
  follow_ups_used: taskResult2.follow_ups_used,
1359
1374
  retries_used: taskResult2.retries_used,
1360
- elapsed_ms: Date.now() - stepStart
1375
+ elapsed_ms: Date.now() - stepStart,
1376
+ tokenUsage: taskResult2.tokenUsage
1361
1377
  };
1362
1378
  }
1363
1379
  const delegateStep = step;
@@ -1408,11 +1424,9 @@ async function runWorkflow(adapterOrFactory, workflowIdOrInvocation, optionsOrRe
1408
1424
  handoffSchemas: registries?.handoffSchemas,
1409
1425
  retry: step.retry,
1410
1426
  progressOutput: options.progressOutput,
1411
- // ── Phase 1-2: Observer Bridge ────────────────────────────────────────
1412
- // Thread observer and session span ID so task-runner can emit agent.subagent
1413
- // spans correlated to this session (parent_run_id → parentSpanId).
1414
1427
  observer: options.observer,
1415
- parentSpanId: sessionSpanId
1428
+ parentSpanId: sessionSpanId,
1429
+ libraryTools: options.libraryTools
1416
1430
  });
1417
1431
  return {
1418
1432
  step_index: i,
@@ -1420,7 +1434,8 @@ async function runWorkflow(adapterOrFactory, workflowIdOrInvocation, optionsOrRe
1420
1434
  outcome: taskResult.outcome,
1421
1435
  follow_ups_used: taskResult.follow_ups_used,
1422
1436
  retries_used: taskResult.retries_used,
1423
- elapsed_ms: Date.now() - stepStart
1437
+ elapsed_ms: Date.now() - stepStart,
1438
+ tokenUsage: taskResult.tokenUsage
1424
1439
  };
1425
1440
  }
1426
1441
  while (completed.size < workflow.steps.length) {
@@ -1460,6 +1475,18 @@ async function runWorkflow(adapterOrFactory, workflowIdOrInvocation, optionsOrRe
1460
1475
  const i = ready[b];
1461
1476
  const result = batchResults[b];
1462
1477
  const step = workflow.steps[i];
1478
+ if (result.tokenUsage) {
1479
+ tokenAccumulator.inputTokens += result.tokenUsage.inputTokens;
1480
+ tokenAccumulator.outputTokens += result.tokenUsage.outputTokens;
1481
+ tokenAccumulator.totalTokens = (tokenAccumulator.totalTokens ?? 0) + (result.tokenUsage.totalTokens ?? 0);
1482
+ if (result.tokenUsage.cacheReadTokens) {
1483
+ tokenAccumulator.cacheReadTokens = (tokenAccumulator.cacheReadTokens ?? 0) + result.tokenUsage.cacheReadTokens;
1484
+ }
1485
+ if (result.tokenUsage.cacheCreationTokens) {
1486
+ tokenAccumulator.cacheCreationTokens = (tokenAccumulator.cacheCreationTokens ?? 0) + result.tokenUsage.cacheCreationTokens;
1487
+ }
1488
+ hasTokenData = true;
1489
+ }
1463
1490
  if (step.type === "evaluate") {
1464
1491
  const evaluateStep = step;
1465
1492
  const data = result.outcome.status === "success" ? result.outcome.data : void 0;
@@ -1624,7 +1651,18 @@ ${JSON.stringify(data, null, 2)}`;
1624
1651
  } finally {
1625
1652
  if (sessionSpanId) {
1626
1653
  const durationMs = wfResult?.total_elapsed_ms ?? Date.now() - workflowStart;
1627
- sessionEnvelope.execution_metadata = { duration_ms: durationMs };
1654
+ sessionEnvelope.execution_metadata = {
1655
+ duration_ms: durationMs,
1656
+ ...hasTokenData ? {
1657
+ token_usage: {
1658
+ input_tokens: tokenAccumulator.inputTokens,
1659
+ output_tokens: tokenAccumulator.outputTokens,
1660
+ total_tokens: tokenAccumulator.totalTokens ?? tokenAccumulator.inputTokens + tokenAccumulator.outputTokens,
1661
+ ...tokenAccumulator.cacheReadTokens ? { cache_read_input_tokens: tokenAccumulator.cacheReadTokens } : {},
1662
+ ...tokenAccumulator.cacheCreationTokens ? { cache_creation_input_tokens: tokenAccumulator.cacheCreationTokens } : {}
1663
+ }
1664
+ } : {}
1665
+ };
1628
1666
  emitSessionClose(
1629
1667
  options.observer,
1630
1668
  sessionSpanId,
@@ -2718,6 +2756,38 @@ function createLibraryToolRegistry() {
2718
2756
  }
2719
2757
  };
2720
2758
  }
2759
+ function createRegistryFromSdkTools(tools) {
2760
+ const handlers = /* @__PURE__ */ new Map();
2761
+ const definitions = /* @__PURE__ */ new Map();
2762
+ for (const tool of tools) {
2763
+ handlers.set(tool.name, tool);
2764
+ definitions.set(tool.name, {
2765
+ toolId: tool.name,
2766
+ description: tool.description,
2767
+ module: "__inline__",
2768
+ exportName: "__handler__",
2769
+ inputSchema: tool.inputSchema
2770
+ });
2771
+ }
2772
+ return {
2773
+ register(def) {
2774
+ definitions.set(def.toolId, def);
2775
+ },
2776
+ get(toolId) {
2777
+ return definitions.get(toolId);
2778
+ },
2779
+ getAll() {
2780
+ return [...definitions.values()];
2781
+ },
2782
+ async execute(toolId, input) {
2783
+ const handler = handlers.get(toolId);
2784
+ if (handler) {
2785
+ return handler.handler(input ?? {});
2786
+ }
2787
+ throw new Error(`Library tool not found: ${toolId}`);
2788
+ }
2789
+ };
2790
+ }
2721
2791
 
2722
2792
  // src/lib/adapter-factory.ts
2723
2793
  var ADAPTER_PACKAGES = {
@@ -2845,6 +2915,11 @@ function injectGuardrailHooks(adapterOptions, execCtx, targetAgentId) {
2845
2915
  adapterOptions.guardrailHooks = createGuardrailHooksFromRules(rules);
2846
2916
  }
2847
2917
  }
2918
+ function buildAnalyzerToolRegistry() {
2919
+ const bridge = createAnalyzerBridge({ projectRoot: process.cwd() });
2920
+ const tools = bridge.buildAnalyzerTools();
2921
+ return createRegistryFromSdkTools(tools);
2922
+ }
2848
2923
  async function executeTask(taskId, options) {
2849
2924
  const execCtx = await resolveExecutionContext(options, { tasks: [taskId] });
2850
2925
  const targetAgentId = execCtx.registries.taskRegistry?.[taskId]?.target_agent;
@@ -2857,6 +2932,7 @@ async function executeTask(taskId, options) {
2857
2932
  const progressSink = createProgressSink(
2858
2933
  options.logFile ? { stderr: true, file: resolve3(options.logFile), naming: "single" } : { stderr: true }
2859
2934
  );
2935
+ const analyzerTools = buildAnalyzerToolRegistry();
2860
2936
  try {
2861
2937
  if (options.hooks?.beforeTask) {
2862
2938
  await options.hooks.beforeTask({ adapter, registries: execCtx.registries, taskId });
@@ -2866,6 +2942,7 @@ async function executeTask(taskId, options) {
2866
2942
  maxRetries: options.maxRetries ?? 0,
2867
2943
  progressOutput: progressSink,
2868
2944
  observer: options.observer,
2945
+ libraryTools: analyzerTools,
2869
2946
  ...execCtx.registries
2870
2947
  });
2871
2948
  if (options.hooks?.afterTask) {
@@ -2887,6 +2964,7 @@ async function executeWorkflow(workflowId, options) {
2887
2964
  const progressSink = createProgressSink(
2888
2965
  options.logFile ? { stderr: true, file: resolve3(options.logFile), naming: "single" } : { stderr: true }
2889
2966
  );
2967
+ const analyzerTools = buildAnalyzerToolRegistry();
2890
2968
  try {
2891
2969
  if (options.hooks?.beforeWorkflow) {
2892
2970
  await options.hooks.beforeWorkflow({ adapter, registries: execCtx.registries, workflowId });
@@ -2898,6 +2976,7 @@ async function executeWorkflow(workflowId, options) {
2898
2976
  progressOutput: progressSink,
2899
2977
  observer: options.observer,
2900
2978
  toolchain: options.toolchain,
2979
+ libraryTools: analyzerTools,
2901
2980
  runtime: {
2902
2981
  maxFollowUps: options.maxFollowUps ?? 2,
2903
2982
  maxRetries: options.maxRetries ?? 0
@@ -3623,6 +3702,7 @@ export {
3623
3702
  createModelResolver,
3624
3703
  createProgressSink,
3625
3704
  createProjectRuntime,
3705
+ createRegistryFromSdkTools,
3626
3706
  createRuntime,
3627
3707
  createRuntimeBinding,
3628
3708
  deriveToolchainsForAgents,