agentfootprint 2.11.0 → 2.11.2

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.
Files changed (110) hide show
  1. package/README.md +2 -1
  2. package/dist/core/Agent.js +89 -1341
  3. package/dist/core/Agent.js.map +1 -1
  4. package/dist/core/agent/AgentBuilder.js +489 -0
  5. package/dist/core/agent/AgentBuilder.js.map +1 -0
  6. package/dist/core/agent/buildAgentChart.js +227 -0
  7. package/dist/core/agent/buildAgentChart.js.map +1 -0
  8. package/dist/core/agent/buildToolRegistry.js +115 -0
  9. package/dist/core/agent/buildToolRegistry.js.map +1 -0
  10. package/dist/core/agent/stages/breakFinal.js +28 -0
  11. package/dist/core/agent/stages/breakFinal.js.map +1 -0
  12. package/dist/core/agent/stages/callLLM.js +129 -0
  13. package/dist/core/agent/stages/callLLM.js.map +1 -0
  14. package/dist/core/agent/stages/iterationStart.js +24 -0
  15. package/dist/core/agent/stages/iterationStart.js.map +1 -0
  16. package/dist/core/agent/stages/prepareFinal.js +45 -0
  17. package/dist/core/agent/stages/prepareFinal.js.map +1 -0
  18. package/dist/core/agent/stages/route.js +36 -0
  19. package/dist/core/agent/stages/route.js.map +1 -0
  20. package/dist/core/agent/stages/seed.js +95 -0
  21. package/dist/core/agent/stages/seed.js.map +1 -0
  22. package/dist/core/agent/stages/toolCalls.js +250 -0
  23. package/dist/core/agent/stages/toolCalls.js.map +1 -0
  24. package/dist/core/agent/types.js +12 -0
  25. package/dist/core/agent/types.js.map +1 -0
  26. package/dist/core/agent/validators.js +131 -0
  27. package/dist/core/agent/validators.js.map +1 -0
  28. package/dist/esm/core/Agent.js +87 -1338
  29. package/dist/esm/core/Agent.js.map +1 -1
  30. package/dist/esm/core/agent/AgentBuilder.js +485 -0
  31. package/dist/esm/core/agent/AgentBuilder.js.map +1 -0
  32. package/dist/esm/core/agent/buildAgentChart.js +223 -0
  33. package/dist/esm/core/agent/buildAgentChart.js.map +1 -0
  34. package/dist/esm/core/agent/buildToolRegistry.js +111 -0
  35. package/dist/esm/core/agent/buildToolRegistry.js.map +1 -0
  36. package/dist/esm/core/agent/stages/breakFinal.js +24 -0
  37. package/dist/esm/core/agent/stages/breakFinal.js.map +1 -0
  38. package/dist/esm/core/agent/stages/callLLM.js +125 -0
  39. package/dist/esm/core/agent/stages/callLLM.js.map +1 -0
  40. package/dist/esm/core/agent/stages/iterationStart.js +20 -0
  41. package/dist/esm/core/agent/stages/iterationStart.js.map +1 -0
  42. package/dist/esm/core/agent/stages/prepareFinal.js +41 -0
  43. package/dist/esm/core/agent/stages/prepareFinal.js.map +1 -0
  44. package/dist/esm/core/agent/stages/route.js +32 -0
  45. package/dist/esm/core/agent/stages/route.js.map +1 -0
  46. package/dist/esm/core/agent/stages/seed.js +91 -0
  47. package/dist/esm/core/agent/stages/seed.js.map +1 -0
  48. package/dist/esm/core/agent/stages/toolCalls.js +246 -0
  49. package/dist/esm/core/agent/stages/toolCalls.js.map +1 -0
  50. package/dist/esm/core/agent/types.js +11 -0
  51. package/dist/esm/core/agent/types.js.map +1 -0
  52. package/dist/esm/core/agent/validators.js +124 -0
  53. package/dist/esm/core/agent/validators.js.map +1 -0
  54. package/dist/esm/reliability/CircuitBreaker.js +156 -0
  55. package/dist/esm/reliability/CircuitBreaker.js.map +1 -0
  56. package/dist/esm/reliability/buildReliabilityGateChart.js +359 -0
  57. package/dist/esm/reliability/buildReliabilityGateChart.js.map +1 -0
  58. package/dist/esm/reliability/classifyError.js +56 -0
  59. package/dist/esm/reliability/classifyError.js.map +1 -0
  60. package/dist/esm/reliability/index.js +36 -0
  61. package/dist/esm/reliability/index.js.map +1 -0
  62. package/dist/esm/reliability/types.js +44 -0
  63. package/dist/esm/reliability/types.js.map +1 -0
  64. package/dist/reliability/CircuitBreaker.js +165 -0
  65. package/dist/reliability/CircuitBreaker.js.map +1 -0
  66. package/dist/reliability/buildReliabilityGateChart.js +363 -0
  67. package/dist/reliability/buildReliabilityGateChart.js.map +1 -0
  68. package/dist/reliability/classifyError.js +60 -0
  69. package/dist/reliability/classifyError.js.map +1 -0
  70. package/dist/reliability/index.js +42 -0
  71. package/dist/reliability/index.js.map +1 -0
  72. package/dist/reliability/types.js +48 -0
  73. package/dist/reliability/types.js.map +1 -0
  74. package/dist/types/core/Agent.d.ts +7 -400
  75. package/dist/types/core/Agent.d.ts.map +1 -1
  76. package/dist/types/core/agent/AgentBuilder.d.ts +348 -0
  77. package/dist/types/core/agent/AgentBuilder.d.ts.map +1 -0
  78. package/dist/types/core/agent/buildAgentChart.d.ts +74 -0
  79. package/dist/types/core/agent/buildAgentChart.d.ts.map +1 -0
  80. package/dist/types/core/agent/buildToolRegistry.d.ts +62 -0
  81. package/dist/types/core/agent/buildToolRegistry.d.ts.map +1 -0
  82. package/dist/types/core/agent/stages/breakFinal.d.ts +23 -0
  83. package/dist/types/core/agent/stages/breakFinal.d.ts.map +1 -0
  84. package/dist/types/core/agent/stages/callLLM.d.ts +54 -0
  85. package/dist/types/core/agent/stages/callLLM.d.ts.map +1 -0
  86. package/dist/types/core/agent/stages/iterationStart.d.ts +16 -0
  87. package/dist/types/core/agent/stages/iterationStart.d.ts.map +1 -0
  88. package/dist/types/core/agent/stages/prepareFinal.d.ts +20 -0
  89. package/dist/types/core/agent/stages/prepareFinal.d.ts.map +1 -0
  90. package/dist/types/core/agent/stages/route.d.ts +19 -0
  91. package/dist/types/core/agent/stages/route.d.ts.map +1 -0
  92. package/dist/types/core/agent/stages/seed.d.ts +54 -0
  93. package/dist/types/core/agent/stages/seed.d.ts.map +1 -0
  94. package/dist/types/core/agent/stages/toolCalls.d.ts +50 -0
  95. package/dist/types/core/agent/stages/toolCalls.d.ts.map +1 -0
  96. package/dist/types/core/agent/types.d.ts +154 -0
  97. package/dist/types/core/agent/types.d.ts.map +1 -0
  98. package/dist/types/core/agent/validators.d.ts +48 -0
  99. package/dist/types/core/agent/validators.d.ts.map +1 -0
  100. package/dist/types/reliability/CircuitBreaker.d.ts +76 -0
  101. package/dist/types/reliability/CircuitBreaker.d.ts.map +1 -0
  102. package/dist/types/reliability/buildReliabilityGateChart.d.ts +54 -0
  103. package/dist/types/reliability/buildReliabilityGateChart.d.ts.map +1 -0
  104. package/dist/types/reliability/classifyError.d.ts +29 -0
  105. package/dist/types/reliability/classifyError.d.ts.map +1 -0
  106. package/dist/types/reliability/index.d.ts +34 -0
  107. package/dist/types/reliability/index.d.ts.map +1 -0
  108. package/dist/types/reliability/types.d.ts +256 -0
  109. package/dist/types/reliability/types.d.ts.map +1 -0
  110. package/package.json +1 -1
@@ -15,23 +15,11 @@
15
15
  * agentfootprint.context.* (via ContextRecorder)
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.AgentBuilder = exports.Agent = void 0;
18
+ exports.Agent = exports.AgentBuilder = void 0;
19
19
  const footprintjs_1 = require("footprintjs");
20
- // ArrayMergeMode lives on footprintjs's `advanced` subpath, not its
21
- // main barrel. Used to set `arrayMerge: Replace` on subflow output
22
- // mapping for the Tools slot — the slot's deduped tool list must
23
- // REPLACE the parent's `dynamicToolSchemas` rather than concatenate
24
- // with it (default behavior re-introduces duplicate tool names that
25
- // LLM providers reject).
26
- const advanced_1 = require("footprintjs/advanced");
27
20
  const CacheDecisionSubflow_js_1 = require("../cache/CacheDecisionSubflow.js");
28
21
  const CacheGateDecider_js_1 = require("../cache/CacheGateDecider.js");
29
22
  const strategyRegistry_js_1 = require("../cache/strategyRegistry.js");
30
- const pause_js_1 = require("./pause.js");
31
- const cost_js_1 = require("./cost.js");
32
- const conventions_js_1 = require("../conventions.js");
33
- const commentaryTemplates_js_1 = require("../recorders/observability/commentary/commentaryTemplates.js");
34
- const thinkingTemplates_js_1 = require("../recorders/observability/thinking/thinkingTemplates.js");
35
23
  const ContextRecorder_js_1 = require("../recorders/core/ContextRecorder.js");
36
24
  const StreamRecorder_js_1 = require("../recorders/core/StreamRecorder.js");
37
25
  const AgentRecorder_js_1 = require("../recorders/core/AgentRecorder.js");
@@ -40,20 +28,27 @@ const PermissionRecorder_js_1 = require("../recorders/core/PermissionRecorder.js
40
28
  const EvalRecorder_js_1 = require("../recorders/core/EvalRecorder.js");
41
29
  const MemoryRecorder_js_1 = require("../recorders/core/MemoryRecorder.js");
42
30
  const SkillRecorder_js_1 = require("../recorders/core/SkillRecorder.js");
43
- const typedEmit_js_1 = require("../recorders/core/typedEmit.js");
44
- const define_types_js_1 = require("../memory/define.types.js");
45
- const define_js_1 = require("../memory/define.js");
46
- const mountMemoryPipeline_js_1 = require("../memory/wire/mountMemoryPipeline.js");
47
31
  const buildSystemPromptSlot_js_1 = require("./slots/buildSystemPromptSlot.js");
48
32
  const buildMessagesSlot_js_1 = require("./slots/buildMessagesSlot.js");
49
33
  const buildToolsSlot_js_1 = require("./slots/buildToolsSlot.js");
50
34
  const buildInjectionEngineSubflow_js_1 = require("../lib/injection-engine/buildInjectionEngineSubflow.js");
51
- const skillTools_js_1 = require("../lib/injection-engine/skillTools.js");
52
- const defineInstruction_js_1 = require("../lib/injection-engine/factories/defineInstruction.js");
53
35
  const outputFallback_js_1 = require("./outputFallback.js");
54
36
  const runCheckpoint_js_1 = require("./runCheckpoint.js");
55
37
  const outputSchema_js_1 = require("./outputSchema.js");
56
38
  const RunnerBase_js_1 = require("./RunnerBase.js");
39
+ const validators_js_1 = require("./agent/validators.js");
40
+ const iterationStart_js_1 = require("./agent/stages/iterationStart.js");
41
+ const route_js_1 = require("./agent/stages/route.js");
42
+ const seed_js_1 = require("./agent/stages/seed.js");
43
+ const callLLM_js_1 = require("./agent/stages/callLLM.js");
44
+ const toolCalls_js_1 = require("./agent/stages/toolCalls.js");
45
+ const buildAgentChart_js_1 = require("./agent/buildAgentChart.js");
46
+ const buildToolRegistry_js_1 = require("./agent/buildToolRegistry.js");
47
+ const AgentBuilder_js_1 = require("./agent/AgentBuilder.js");
48
+ Object.defineProperty(exports, "AgentBuilder", { enumerable: true, get: function () { return AgentBuilder_js_1.AgentBuilder; } });
49
+ // Public types (AgentOptions, AgentInput, AgentOutput) extracted to
50
+ // ./agent/types.ts and re-exported above (v2.11.1).
51
+ // AgentState extracted to ./agent/types.ts (v2.11.1).
57
52
  class Agent extends RunnerBase_js_1.RunnerBase {
58
53
  name;
59
54
  id;
@@ -167,7 +162,7 @@ class Agent extends RunnerBase_js_1.RunnerBase {
167
162
  this.model = opts.model;
168
163
  this.temperature = opts.temperature;
169
164
  this.maxTokens = opts.maxTokens;
170
- this.maxIterations = clampIterations(opts.maxIterations ?? 10);
165
+ this.maxIterations = (0, validators_js_1.clampIterations)(opts.maxIterations ?? 10);
171
166
  this.systemPromptValue = systemPromptValue;
172
167
  this.systemPromptCachePolicy = systemPromptCachePolicy;
173
168
  this.cachingDisabledValue = cachingDisabled;
@@ -184,10 +179,10 @@ class Agent extends RunnerBase_js_1.RunnerBase {
184
179
  // every Skill.inject.tools — the LLM dispatches by name. Runs in
185
180
  // constructor so `Agent.build()` throws immediately on collision,
186
181
  // not at first run().
187
- validateToolNameUniqueness(registry, injections);
182
+ (0, validators_js_1.validateToolNameUniqueness)(registry, injections);
188
183
  // Eager validation: memory ids must be unique so per-id scope keys
189
184
  // (`memoryInjection_${id}`) don't collide.
190
- validateMemoryIdUniqueness(memories);
185
+ (0, validators_js_1.validateMemoryIdUniqueness)(memories);
191
186
  if (opts.pricingTable)
192
187
  this.pricingTable = opts.pricingTable;
193
188
  if (opts.costBudget !== undefined)
@@ -199,7 +194,7 @@ class Agent extends RunnerBase_js_1.RunnerBase {
199
194
  this.thinkingTemplates = voice.thinkingTemplates;
200
195
  }
201
196
  static create(opts) {
202
- return new AgentBuilder(opts);
197
+ return new AgentBuilder_js_1.AgentBuilder(opts);
203
198
  }
204
199
  toFlowChart() {
205
200
  return this.buildChart();
@@ -508,168 +503,33 @@ class Agent extends RunnerBase_js_1.RunnerBase {
508
503
  const systemPromptCachePolicy = this.systemPromptCachePolicy;
509
504
  const cachingDisabled = this.cachingDisabledValue;
510
505
  const cacheStrategy = this.cacheStrategy;
511
- const seed = (scope) => {
512
- const args = scope.$getArgs();
513
- scope.userMessage = args.message;
514
- // If `resumeOnError(...)` set the side channel, restore the
515
- // checkpointed conversation history. The next iteration sees
516
- // the prior messages and continues from the failure point.
517
- // We always clear the field after reading so subsequent runs
518
- // (without resumeOnError) start fresh.
519
- if (this.pendingResumeHistory && this.pendingResumeHistory.length > 0) {
520
- scope.history = [...this.pendingResumeHistory];
506
+ // seed extracted to ./agent/stages/seed.ts (v2.11.2). Factory takes
507
+ // chart-build-time constants + per-run mutable accessors so the
508
+ // resume side-channel and current run id remain dynamic.
509
+ // toolSchemas is finalized further down; pass a getter that reads
510
+ // the eventual const at stage-execution time.
511
+ let toolSchemasResolved = [];
512
+ const seed = (0, seed_js_1.buildSeedStage)({
513
+ maxIterations,
514
+ cachingDisabled,
515
+ get toolSchemas() {
516
+ return toolSchemasResolved;
517
+ },
518
+ consumePendingResumeHistory: () => {
519
+ const h = this.pendingResumeHistory;
521
520
  this.pendingResumeHistory = undefined;
522
- }
523
- else {
524
- scope.history = [{ role: 'user', content: args.message }];
525
- }
526
- // Default identity uses the runId so multi-run isolation works
527
- // without consumer changes; explicit identity (multi-tenant)
528
- // overrides via `agent.run({ identity })`.
529
- scope.runIdentity = args.identity ?? {
530
- conversationId: this.currentRunContext?.runId ?? 'default',
531
- };
532
- scope.newMessages = [];
533
- scope.turnNumber = 1;
534
- // Permissive default — explicit cap will land when PricingTable
535
- // gets a context-window field. Memory pickByBudget treats anything
536
- // ≥ minimumTokens as "fits", so this just enables the budget path.
537
- scope.contextTokensRemaining = 32_000;
538
- scope.iteration = 1;
539
- scope.maxIterations = maxIterations;
540
- scope.finalContent = '';
541
- scope.totalInputTokens = 0;
542
- scope.totalOutputTokens = 0;
543
- scope.turnStartMs = Date.now();
544
- scope.systemPromptInjections = [];
545
- scope.messagesInjections = [];
546
- scope.toolsInjections = [];
547
- scope.llmLatestContent = '';
548
- scope.llmLatestToolCalls = [];
549
- scope.pausedToolCallId = '';
550
- scope.pausedToolName = '';
551
- scope.pausedToolStartMs = 0;
552
- scope.cumTokensInput = 0;
553
- scope.cumTokensOutput = 0;
554
- scope.cumEstimatedUsd = 0;
555
- scope.costBudgetHit = false;
556
- scope.activeInjections = [];
557
- scope.activatedInjectionIds = [];
558
- scope.dynamicToolSchemas = toolSchemas;
559
- // Cache layer state (v2.6) — initialized to inert defaults.
560
- // CacheDecision subflow populates `cacheMarkers` per iteration;
561
- // UpdateSkillHistory + CacheGate consume `cachingDisabled`,
562
- // `recentHitRate`, `skillHistory`. Empty defaults mean the
563
- // CacheGate falls through to 'apply-markers' on iter 1 (no
564
- // history yet → no churn detected; recentHitRate undefined →
565
- // hit-rate floor doesn't fire).
566
- scope.cacheMarkers = [];
567
- scope.cachingDisabled = cachingDisabled;
568
- scope.recentHitRate = undefined;
569
- scope.skillHistory = [];
570
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.agent.turn_start', {
571
- turnIndex: 0,
572
- userPrompt: args.message,
573
- });
574
- };
575
- // Tool registry composition — three sources:
576
- //
577
- // 1. Static registry: tools registered via `.tool()`. Always
578
- // visible to the LLM; always executable.
579
- // 2. `read_skill` (auto-attached when ≥1 Skill is registered):
580
- // activation tool for LLM-guided Skills.
581
- // 3. Skill-supplied tools (`Skill.inject.tools[]`): visible only
582
- // when the Skill is active (filtered by tools slot subflow);
583
- // MUST always be in the executor registry so when the LLM
584
- // calls one, the tool-calls handler can dispatch.
585
- //
586
- // Tool-name uniqueness is enforced across all three sources at
587
- // build time. The LLM only sees `tool.schema.name` (no ids), so
588
- // names ARE the runtime dispatch key — collisions break the LLM's
589
- // ability to call the right tool. Throw early instead of subtly
590
- // shadowing.
591
- const skills = this.injections.filter((i) => i.flavor === 'skill');
592
- // Collect skill tools, deduping by name when the SAME Tool reference
593
- // is shared across skills. Different Tool implementations under the
594
- // same name throws (already validated upstream by
595
- // validateToolNameUniqueness) — we keep the runtime check as
596
- // belt-and-suspenders.
597
- //
598
- // Block C runtime — `autoActivate: 'currentSkill'` semantics:
599
- // When a skill's `defineSkill({ autoActivate: 'currentSkill' })`
600
- // is set, its tools are EXCLUDED from the static registry. They
601
- // flow into the LLM's tool list ONLY through `dynamicSchemas`
602
- // (the buildToolsSlot path that reads activeInjections), which
603
- // means they're visible ONLY on iterations after the skill is
604
- // activated by `read_skill('id')`. Without this, the LLM sees
605
- // every skill's tools on every iteration and the
606
- // per-skill-narrowing autoActivate promised in `defineSkill`
607
- // doesn't actually narrow anything. Skills WITHOUT autoActivate
608
- // keep the v2.4 behavior (tools always visible) for back-compat.
609
- const skillToolEntries = [];
610
- const sharedSkillTools = new Map();
611
- for (const skill of skills) {
612
- const meta = skill.metadata;
613
- const isAutoActivate = meta?.autoActivate === 'currentSkill';
614
- const toolsFromSkill = skill.inject.tools ?? [];
615
- for (const tool of toolsFromSkill) {
616
- const name = tool.schema.name;
617
- const existing = sharedSkillTools.get(name);
618
- if (existing) {
619
- if (existing !== tool) {
620
- throw new Error(`Agent: tool name '${name}' is declared by multiple skills with different ` +
621
- `Tool implementations. Skills MAY share the SAME Tool reference; they may ` +
622
- `NOT register different functions under the same name.`);
623
- }
624
- continue; // dedupe — same reference already added
625
- }
626
- sharedSkillTools.set(name, tool);
627
- // autoActivate skills: their tools come ONLY through
628
- // dynamicSchemas (buildToolsSlot.ts pulls them from
629
- // activeInjections.inject.tools when the skill is active).
630
- // Don't pre-load them in the static registry.
631
- if (isAutoActivate)
632
- continue;
633
- skillToolEntries.push({ name, tool });
634
- }
635
- }
636
- // buildReadSkillTool returns undefined when skills is empty; the
637
- // length check below short-circuits so the non-null assertion is safe.
638
- const readSkillEntries = skills.length > 0 ? [{ name: 'read_skill', tool: (0, skillTools_js_1.buildReadSkillTool)(skills) }] : [];
639
- const augmentedRegistry = [
640
- ...registry,
641
- ...readSkillEntries,
642
- ...skillToolEntries,
643
- ];
644
- // Final cross-source name-uniqueness check: static .tool() vs
645
- // read_skill vs (deduped) skill tools. After the dedupe above this
646
- // catches collisions BETWEEN sources (e.g., a static .tool('foo')
647
- // colliding with a Skill's foo) which are real bugs.
648
- const seenNames = new Set();
649
- for (const entry of augmentedRegistry) {
650
- if (seenNames.has(entry.name)) {
651
- throw new Error(`Agent: duplicate tool name '${entry.name}'. Tool names must be unique ` +
652
- `across .tool() registrations and Skills' inject.tools (after deduping ` +
653
- `same-reference shares across skills). The LLM dispatches by name; ` +
654
- `collisions break tool routing.`);
655
- }
656
- seenNames.add(entry.name);
657
- }
658
- const registryByName = new Map(augmentedRegistry.map((e) => [e.name, e.tool]));
659
- // Block C runtime — autoActivate skill tools live OUTSIDE the LLM-
660
- // visible registry (so they don't pollute the per-iteration tool
661
- // list before the skill activates), but they MUST still be findable
662
- // by the dispatch handler — the LLM calls them by name once the
663
- // skill is active, and dispatch looks up by name. Add them to the
664
- // dispatch map so `lookupTool` resolves correctly. Using the Map
665
- // backing the static registryByName means autoActivate tools share
666
- // the same `.execute` wiring as normal tools — no special path.
667
- for (const [name, tool] of sharedSkillTools.entries()) {
668
- if (!registryByName.has(name)) {
669
- registryByName.set(name, tool);
670
- }
671
- }
672
- const toolSchemas = augmentedRegistry.map((e) => e.tool.schema);
521
+ return h;
522
+ },
523
+ getCurrentRunId: () => this.currentRunContext?.runId,
524
+ });
525
+ // Tool registry composition extracted to ./agent/buildToolRegistry.ts.
526
+ // Composes static .tool() registry + auto-attached read_skill +
527
+ // skill-supplied tools (with autoActivate scoping); validates
528
+ // name uniqueness; produces the dispatch map.
529
+ const { registryByName, toolSchemas } = (0, buildToolRegistry_js_1.buildToolRegistry)(registry, this.injections);
530
+ // Late-bind toolSchemas into the seed stage's deps (the factory was
531
+ // built earlier with a getter; this resolves the actual value).
532
+ toolSchemasResolved = toolSchemas;
673
533
  const injectionEngineSubflow = (0, buildInjectionEngineSubflow_js_1.buildInjectionEngineSubflow)({
674
534
  injections: this.injections,
675
535
  });
@@ -682,1165 +542,53 @@ class Agent extends RunnerBase_js_1.RunnerBase {
682
542
  tools: toolSchemas,
683
543
  ...(this.externalToolProvider && { toolProvider: this.externalToolProvider }),
684
544
  });
685
- const iterationStart = (scope) => {
686
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.agent.iteration_start', {
687
- turnIndex: 0,
688
- iterIndex: scope.iteration,
689
- });
690
- };
691
- const callLLM = async (scope) => {
692
- const systemPromptInjections = scope.systemPromptInjections ?? [];
693
- // `scope.messagesInjections` is read by ContextRecorder for
694
- // observability; the LLM-wire path now reads scope.history
695
- // directly (see below for rationale).
696
- const iteration = scope.iteration;
697
- const systemPrompt = systemPromptInjections
698
- .map((r) => r.rawContent ?? '')
699
- .filter((s) => s.length > 0)
700
- .join('\n\n');
701
- // Read the LLM message stream from `scope.history` directly.
702
- // The `messagesInjections` projection is for observability
703
- // (ContextRecorder, Lens) — it flattens InjectionRecords for
704
- // event reporting and doesn't carry the full LLM-protocol
705
- // shape (assistant `toolCalls[]`, etc.). For Anthropic's API
706
- // contract we need the original LLMMessage with `toolCalls`
707
- // intact so tool_use → tool_result correlation survives.
708
- const messages = scope.history ?? [];
709
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.stream.llm_start', {
710
- iteration,
711
- provider: provider.name,
712
- model,
713
- systemPromptChars: systemPrompt.length,
714
- messagesCount: messages.length,
715
- toolsCount: toolSchemas.length,
716
- ...(temperature !== undefined && { temperature }),
717
- });
718
- const startMs = Date.now();
719
- // Use dynamic schemas — registry tools + injection-supplied
720
- // tools (Skills' `inject.tools` when their Injection is active).
721
- // Falls back to the static schemas at startup before the tools
722
- // slot has run for the first time.
723
- const activeToolSchemas = scope.dynamicToolSchemas ?? toolSchemas;
724
- const baseRequest = {
725
- ...(systemPrompt.length > 0 && { systemPrompt }),
726
- messages,
727
- ...(activeToolSchemas.length > 0 && { tools: activeToolSchemas }),
728
- model,
729
- ...(temperature !== undefined && { temperature }),
730
- ...(maxTokens !== undefined && { maxTokens }),
731
- };
732
- // v2.6+ — call cache strategy to attach provider-specific cache
733
- // hints. CacheGate has already routed (apply-markers / no-markers)
734
- // and populated scope.cacheMarkers accordingly. Strategy.prepareRequest
735
- // is a pass-through for empty markers.
736
- const cacheMarkers = scope.cacheMarkers ?? [];
737
- const cachePrepared = await cacheStrategy.prepareRequest(baseRequest, cacheMarkers, {
738
- iteration,
739
- iterationsRemaining: Math.max(0, maxIterations - iteration),
740
- recentHitRate: scope.recentHitRate,
741
- cachingDisabled: scope.cachingDisabled ?? false,
742
- });
743
- const llmRequest = cachePrepared.request;
744
- // Streaming-first: when the provider implements `stream()` we
745
- // consume chunk-by-chunk so consumers (Lens commentary, chat
746
- // UIs) see tokens as they arrive instead of waiting for the
747
- // full LLM call to finish. Each non-terminal chunk fires
748
- // `agentfootprint.stream.token` with the token text + index.
749
- //
750
- // The terminal chunk SHOULD carry the authoritative
751
- // `LLMResponse` (toolCalls + usage + stopReason); when it does
752
- // we use it directly. When it doesn't (older providers, partial
753
- // implementations) we fall back to `complete()` for the
754
- // authoritative payload — keeping the ReAct loop deterministic.
755
- let response;
756
- if (provider.stream) {
757
- for await (const chunk of provider.stream(llmRequest)) {
758
- if (chunk.done) {
759
- if (chunk.response)
760
- response = chunk.response;
761
- break;
762
- }
763
- if (chunk.content.length > 0) {
764
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.stream.token', {
765
- iteration,
766
- tokenIndex: chunk.tokenIndex,
767
- content: chunk.content,
768
- });
769
- }
770
- }
771
- }
772
- if (!response) {
773
- // No `stream()` OR stream finished without a response payload.
774
- response = await provider.complete(llmRequest);
775
- }
776
- const durationMs = Date.now() - startMs;
777
- scope.totalInputTokens = scope.totalInputTokens + response.usage.input;
778
- scope.totalOutputTokens = scope.totalOutputTokens + response.usage.output;
779
- scope.llmLatestContent = response.content;
780
- scope.llmLatestToolCalls = response.toolCalls;
781
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.stream.llm_end', {
782
- iteration,
783
- content: response.content,
784
- toolCallCount: response.toolCalls.length,
785
- usage: response.usage,
786
- stopReason: response.stopReason,
787
- durationMs,
788
- });
789
- (0, cost_js_1.emitCostTick)(scope, pricingTable, costBudget, model, response.usage);
790
- };
791
- /** Decides the next branch: 'tool-calls' or 'final'. */
792
- const routeDecider = (scope) => {
793
- const toolCalls = scope.llmLatestToolCalls;
794
- const iteration = scope.iteration;
795
- const chosen = toolCalls.length > 0 && iteration < scope.maxIterations ? 'tool-calls' : 'final';
796
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.agent.route_decided', {
797
- turnIndex: 0,
798
- iterIndex: iteration,
799
- chosen,
800
- rationale: chosen === 'tool-calls'
801
- ? `LLM requested ${toolCalls.length} tool call(s)`
802
- : iteration >= scope.maxIterations
803
- ? 'maxIterations reached — forcing final'
804
- : 'LLM produced no tool calls — final answer',
805
- });
806
- return chosen;
807
- };
808
- /**
809
- * Pausable tool-call handler.
810
- *
811
- * `execute` iterates the LLM-requested tool calls. If a tool throws
812
- * `PauseRequest` via `pauseHere()`, we save the remaining work into
813
- * scope and return the pause data — footprintjs captures a checkpoint
814
- * and bubbles it up. The outer `Agent.run()` surfaces it as a
815
- * `RunnerPauseOutcome`.
816
- *
817
- * `resume` is called when the consumer provides the human's answer.
818
- * We treat that answer as the paused tool's result and append it to
819
- * history, then continue the ReAct iteration loop.
820
- */
821
- const toolCallsHandler = {
822
- execute: async (scope) => {
823
- const toolCalls = scope.llmLatestToolCalls;
824
- const iteration = scope.iteration;
825
- const newHistory = [...scope.history];
826
- // ALWAYS push the assistant turn when there are tool calls — even
827
- // if the content was empty — so providers (Anthropic, OpenAI) can
828
- // round-trip the tool_use blocks via `LLMMessage.toolCalls`.
829
- // Without this, the next iteration's request lacks the assistant
830
- // turn that initiated the tool call, and the API rejects the
831
- // following tool_result with "preceding tool_use missing".
832
- if (scope.llmLatestContent || toolCalls.length > 0) {
833
- newHistory.push({
834
- role: 'assistant',
835
- content: scope.llmLatestContent ?? '',
836
- ...(toolCalls.length > 0 && { toolCalls }),
837
- });
838
- }
839
- // Resolve a tool by name, consulting the external ToolProvider
840
- // if one was wired via `.toolProvider()` and the static
841
- // registry doesn't carry the tool. The provider sees the same
842
- // ctx the Tools slot used, so dispatch + visibility stay
843
- // consistent within the iteration.
844
- const externalToolProvider = this.externalToolProvider;
845
- const lookupTool = (toolName) => {
846
- const fromRegistry = registryByName.get(toolName);
847
- if (fromRegistry)
848
- return fromRegistry;
849
- if (!externalToolProvider)
850
- return undefined;
851
- const activatedIds = scope.activatedInjectionIds ?? [];
852
- const identity = scope.runIdentity;
853
- const ctx = {
854
- iteration: scope.iteration,
855
- ...(activatedIds.length > 0 && {
856
- activeSkillId: activatedIds[activatedIds.length - 1],
857
- }),
858
- ...(identity && { identity }),
859
- };
860
- const visible = externalToolProvider.list(ctx);
861
- return visible.find((t) => t.schema.name === toolName);
862
- };
863
- for (const tc of toolCalls) {
864
- const tool = lookupTool(tc.name);
865
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.stream.tool_start', {
866
- toolName: tc.name,
867
- toolCallId: tc.id,
868
- args: tc.args,
869
- ...(toolCalls.length > 1 && { parallelCount: toolCalls.length }),
870
- });
871
- const startMs = Date.now();
872
- let result;
873
- let error;
874
- // Permission gate — when a checker is configured, evaluate BEFORE
875
- // executing the tool. Emits `permission.check` with the decision.
876
- // On 'deny', the tool is not executed and its result is a
877
- // synthetic denial string; on 'allow'/'gate_open', execution
878
- // proceeds normally (the gate is informational — the consumer's
879
- // checker is responsible for any gate-open side effects).
880
- let denied = false;
881
- if (permissionChecker) {
882
- try {
883
- const decision = await permissionChecker.check({
884
- capability: 'tool_call',
885
- actor: 'agent',
886
- target: tc.name,
887
- context: tc.args,
888
- });
889
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.permission.check', {
890
- capability: 'tool_call',
891
- actor: 'agent',
892
- target: tc.name,
893
- result: decision.result,
894
- ...(decision.policyRuleId !== undefined && { policyRuleId: decision.policyRuleId }),
895
- ...(decision.rationale !== undefined && { rationale: decision.rationale }),
896
- });
897
- if (decision.result === 'deny') {
898
- denied = true;
899
- result = `[permission denied: ${decision.rationale ?? 'policy'}]`;
900
- }
901
- }
902
- catch (permErr) {
903
- // A checker that throws is treated as deny-by-default. The
904
- // denial message records the thrown error so consumers can
905
- // debug policy-adapter failures without losing the run.
906
- denied = true;
907
- const msg = permErr instanceof Error ? permErr.message : String(permErr);
908
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.permission.check', {
909
- capability: 'tool_call',
910
- actor: 'agent',
911
- target: tc.name,
912
- result: 'deny',
913
- rationale: `permission-checker threw: ${msg}`,
914
- });
915
- result = `[permission denied: checker error: ${msg}]`;
916
- }
917
- }
918
- if (!denied) {
919
- try {
920
- if (!tool)
921
- throw new Error(`Unknown tool: ${tc.name}`);
922
- result = await tool.execute(tc.args, {
923
- toolCallId: tc.id,
924
- iteration,
925
- });
926
- }
927
- catch (err) {
928
- if ((0, pause_js_1.isPauseRequest)(err)) {
929
- // Commit partial state so resume() can find history intact.
930
- scope.history = newHistory;
931
- scope.pausedToolCallId = tc.id;
932
- scope.pausedToolName = tc.name;
933
- scope.pausedToolStartMs = startMs;
934
- // Returning a defined value triggers footprintjs pause —
935
- // the returned object becomes the checkpoint's pauseData.
936
- return {
937
- toolCallId: tc.id,
938
- toolName: tc.name,
939
- ...(typeof err.data === 'object' && err.data !== null
940
- ? err.data
941
- : { data: err.data }),
942
- };
943
- }
944
- error = true;
945
- result = err instanceof Error ? err.message : String(err);
946
- }
947
- }
948
- const durationMs = Date.now() - startMs;
949
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.stream.tool_end', {
950
- toolCallId: tc.id,
951
- result,
952
- durationMs,
953
- ...(error === true && { error: true }),
954
- });
955
- const resultStr = typeof result === 'string' ? result : safeStringify(result);
956
- newHistory.push({
957
- role: 'tool',
958
- content: resultStr,
959
- toolCallId: tc.id,
960
- toolName: tc.name,
961
- });
962
- // ── Dynamic ReAct wiring ───────────────────────────────
963
- //
964
- // (1) `lastToolResult` drives `on-tool-return` Injection
965
- // triggers — the InjectionEngine's NEXT pass will see
966
- // this and activate any matching Instructions.
967
- scope.lastToolResult = { toolName: tc.name, result: resultStr };
968
- // (2) `read_skill` is the auto-attached activation tool.
969
- // When the LLM calls it with a valid Skill id, append
970
- // to `activatedInjectionIds` so the InjectionEngine's
971
- // NEXT pass activates that Skill (lifetime: turn — stays
972
- // active until the turn ends).
973
- if (tc.name === 'read_skill' && !error && !denied) {
974
- const requestedId = tc.args.id;
975
- if (typeof requestedId === 'string' && requestedId.length > 0) {
976
- const current = scope.activatedInjectionIds;
977
- if (!current.includes(requestedId)) {
978
- scope.activatedInjectionIds = [...current, requestedId];
979
- }
980
- }
981
- }
982
- }
983
- scope.history = newHistory;
984
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.agent.iteration_end', {
985
- turnIndex: 0,
986
- iterIndex: iteration,
987
- toolCallCount: toolCalls.length,
988
- history: scope.history,
989
- });
990
- scope.iteration = iteration + 1;
991
- return undefined; // explicit: no pause, flow continues to loopTo
992
- },
993
- resume: (scope, input) => {
994
- // Consumer-supplied resume input becomes the paused tool's result.
995
- // The subflow's pre-pause scope is restored automatically by
996
- // footprintjs 4.17.0 via `checkpoint.subflowStates`, so
997
- // `scope.history` and `scope.pausedToolCallId` read back cleanly
998
- // across same-executor AND cross-executor resume.
999
- const toolCallId = scope.pausedToolCallId;
1000
- const toolName = scope.pausedToolName;
1001
- const startMs = scope.pausedToolStartMs;
1002
- const resultStr = typeof input === 'string' ? input : safeStringify(input);
1003
- const newHistory = [
1004
- ...scope.history,
1005
- {
1006
- role: 'tool',
1007
- content: resultStr,
1008
- toolCallId,
1009
- toolName,
1010
- },
1011
- ];
1012
- scope.history = newHistory;
1013
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.stream.tool_end', {
1014
- toolCallId,
1015
- result: input,
1016
- durationMs: Date.now() - startMs,
1017
- });
1018
- const iteration = scope.iteration;
1019
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.agent.iteration_end', {
1020
- turnIndex: 0,
1021
- iterIndex: iteration,
1022
- toolCallCount: 1,
1023
- history: scope.history,
1024
- });
1025
- scope.iteration = iteration + 1;
1026
- // Clear pause checkpoint fields.
1027
- scope.pausedToolCallId = '';
1028
- scope.pausedToolName = '';
1029
- scope.pausedToolStartMs = 0;
1030
- },
1031
- };
1032
- // Final branch is split so memory-write subflows can mount BETWEEN
1033
- // setting `finalContent` and breaking the ReAct loop. PrepareFinal
1034
- // captures the turn payload; BreakFinal terminates the loop.
1035
- const prepareFinalStage = (scope) => {
1036
- const iteration = scope.iteration;
1037
- scope.finalContent = scope.llmLatestContent;
1038
- // The turn payload memory writes persist: the user's message
1039
- // paired with the agent's final answer.
1040
- scope.newMessages = [
1041
- { role: 'user', content: scope.userMessage },
1042
- { role: 'assistant', content: scope.finalContent },
1043
- ];
1044
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.agent.iteration_end', {
1045
- turnIndex: 0,
1046
- iterIndex: iteration,
1047
- toolCallCount: 0,
1048
- });
1049
- (0, typedEmit_js_1.typedEmit)(scope, 'agentfootprint.agent.turn_end', {
1050
- turnIndex: 0,
1051
- finalContent: scope.finalContent,
1052
- totalInputTokens: scope.totalInputTokens,
1053
- totalOutputTokens: scope.totalOutputTokens,
1054
- iterationCount: iteration,
1055
- durationMs: Date.now() - scope.turnStartMs,
1056
- });
1057
- };
1058
- const breakFinalStage = (scope) => {
1059
- // $break terminates the flow before loopTo fires, ending the
1060
- // ReAct iteration once memory writes (if any) have persisted.
1061
- scope.$break();
1062
- return scope.finalContent;
1063
- };
1064
- // Compose the final branch as its own subflow so memory write
1065
- // subflows mount as visible siblings in narrative + Lens.
1066
- let finalBranchBuilder = (0, footprintjs_1.flowChart)('PrepareFinal', prepareFinalStage, 'prepare-final', undefined, 'Capture turn payload (finalContent + newMessages)');
1067
- for (const m of this.memories) {
1068
- if (m.write) {
1069
- finalBranchBuilder = (0, mountMemoryPipeline_js_1.mountMemoryWrite)(finalBranchBuilder, {
1070
- pipeline: {
1071
- read: (0, define_js_1.unwrapMemoryFlowChart)(m.read),
1072
- write: (0, define_js_1.unwrapMemoryFlowChart)(m.write),
1073
- },
1074
- identityKey: 'runIdentity',
1075
- turnNumberKey: 'turnNumber',
1076
- contextTokensKey: 'contextTokensRemaining',
1077
- newMessagesKey: 'newMessages',
1078
- writeSubflowId: `sf-memory-write-${m.id}`,
1079
- });
1080
- }
1081
- }
1082
- const finalBranchChart = finalBranchBuilder
1083
- .addFunction('BreakFinal', breakFinalStage, 'break-final', 'Terminate the ReAct loop')
1084
- .build();
1085
- // Description prefix `Agent:` is a taxonomy marker — consumers
1086
- // (Lens + FlowchartRecorder) detect Agent-primitive subflows via
1087
- // this prefix and flag them as true agent boundaries (separate
1088
- // from LLMCall subflows which use `LLMCall:` prefix).
1089
- let builder = (0, footprintjs_1.flowChart)('Seed', seed, conventions_js_1.STAGE_IDS.SEED, undefined, 'Agent: ReAct loop');
1090
- // Memory READ subflows — mounted between Seed and InjectionEngine
1091
- // for TURN_START timing (default). Each memory writes to its own
1092
- // scope key (`memoryInjection_${id}`) so multiple `.memory()`
1093
- // registrations layer without colliding.
1094
- for (const m of this.memories) {
1095
- builder = (0, mountMemoryPipeline_js_1.mountMemoryRead)(builder, {
1096
- pipeline: {
1097
- read: (0, define_js_1.unwrapMemoryFlowChart)(m.read),
1098
- ...(m.write !== undefined && { write: (0, define_js_1.unwrapMemoryFlowChart)(m.write) }),
1099
- },
1100
- identityKey: 'runIdentity',
1101
- turnNumberKey: 'turnNumber',
1102
- contextTokensKey: 'contextTokensRemaining',
1103
- injectionKey: (0, define_types_js_1.memoryInjectionKey)(m.id),
1104
- readSubflowId: `sf-memory-read-${m.id}`,
1105
- });
1106
- }
1107
- builder = builder
1108
- // Injection Engine — evaluates every Injection's trigger once
1109
- // per iteration; writes activeInjections[] to parent scope for
1110
- // the slot subflows to consume. Skipped if no injections were
1111
- // registered (no observable difference, just one more no-op
1112
- // subflow boundary).
1113
- .addSubFlowChartNext(conventions_js_1.SUBFLOW_IDS.INJECTION_ENGINE, injectionEngineSubflow, 'Injection Engine', {
1114
- inputMapper: (parent) => ({
1115
- iteration: parent.iteration,
1116
- userMessage: parent.userMessage,
1117
- history: parent.history,
1118
- lastToolResult: parent.lastToolResult,
1119
- activatedInjectionIds: parent.activatedInjectionIds ?? [],
1120
- }),
1121
- outputMapper: (sf) => ({ activeInjections: sf.activeInjections }),
1122
- // CRITICAL: footprintjs's default `applyOutputMapping`
1123
- // CONCATENATES arrays from subflow output with the parent's
1124
- // existing array values. Without `Replace`, the parent's
1125
- // `activeInjections` from iter N gets CONCATENATED with the
1126
- // subflow's iter N+1 fresh evaluation — producing
1127
- // 8 → 16 → 24 → 32 cumulative injections per turn instead of
1128
- // the intended ~8-per-iter.
1129
- //
1130
- // The slot subflows below (SystemPrompt, Messages, Tools) all
1131
- // read `activeInjections` and render every entry, so without
1132
- // Replace the system prompt grows linearly with iteration
1133
- // count. This was the root-cause of Dynamic-mode costing
1134
- // ~2x more input tokens than Classic in the v2.5.0 Neo
1135
- // benchmarks — the InjectionEngine's intended per-iter
1136
- // recomposition wasn't happening; it was per-iter ACCUMULATION.
1137
- arrayMerge: advanced_1.ArrayMergeMode.Replace,
1138
- })
1139
- .addSubFlowChartNext(conventions_js_1.SUBFLOW_IDS.SYSTEM_PROMPT, systemPromptSubflow, 'System Prompt', {
1140
- inputMapper: (parent) => ({
1141
- userMessage: parent.userMessage,
1142
- iteration: parent.iteration,
1143
- activeInjections: parent.activeInjections,
1144
- }),
1145
- outputMapper: (sf) => ({ systemPromptInjections: sf.systemPromptInjections }),
1146
- // See Tools-subflow comment below — same array-concat hazard.
1147
- // Without Replace, iter N+1's systemPromptInjections gets
1148
- // CONCATENATED with iter N's, multiplying the system prompt
1149
- // each iteration.
1150
- arrayMerge: advanced_1.ArrayMergeMode.Replace,
1151
- })
1152
- .addSubFlowChartNext(conventions_js_1.SUBFLOW_IDS.MESSAGES, messagesSubflow, 'Messages', {
1153
- inputMapper: (parent) => ({
1154
- messages: parent.history,
1155
- iteration: parent.iteration,
1156
- activeInjections: parent.activeInjections,
1157
- }),
1158
- outputMapper: (sf) => ({ messagesInjections: sf.messagesInjections }),
1159
- // Same array-concat hazard. messagesInjections is consumer-
1160
- // facing observability metadata (ContextRecorder, Lens) — must
1161
- // reflect THIS iteration's history, not be appended to last
1162
- // iteration's. CallLLM no longer reads this for the wire
1163
- // request (uses scope.history directly), so the LLM-protocol
1164
- // bug is fixed independently — but consumers of the
1165
- // messagesInjections stream still expect the per-iteration
1166
- // semantics.
1167
- arrayMerge: advanced_1.ArrayMergeMode.Replace,
1168
- })
1169
- .addSubFlowChartNext(conventions_js_1.SUBFLOW_IDS.TOOLS, toolsSubflow, 'Tools', {
1170
- inputMapper: (parent) => ({
1171
- iteration: parent.iteration,
1172
- activeInjections: parent.activeInjections,
1173
- // The slot subflow reads these to build the per-iteration
1174
- // ToolDispatchContext when an external `.toolProvider()` is
1175
- // configured. Without them the provider sees activeSkillId
1176
- // = undefined every iteration, breaking skillScopedTools etc.
1177
- activatedInjectionIds: parent.activatedInjectionIds,
1178
- runIdentity: parent.runIdentity,
1179
- }),
1180
- outputMapper: (sf) => ({
1181
- toolsInjections: sf.toolsInjections,
1182
- // Pass merged tool schemas (registry + injection-supplied)
1183
- // back up so callLLM uses the right list for THIS iteration.
1184
- dynamicToolSchemas: sf.toolSchemas,
1185
- }),
1186
- // CRITICAL: footprintjs's default `applyOutputMapping`
1187
- // CONCATENATES arrays from subflow output with the parent's
1188
- // existing array values. Without `Replace`, the parent's
1189
- // `dynamicToolSchemas` (carrying the iter N value) gets
1190
- // concatenated with the slot's iter N+1 deduped list,
1191
- // re-introducing duplicate tool names that Anthropic's API
1192
- // rejects with "tools: Tool names must be unique." The slot's
1193
- // toolSchemas IS the authoritative list — replace, don't
1194
- // concatenate.
1195
- arrayMerge: advanced_1.ArrayMergeMode.Replace,
1196
- })
1197
- // ── Cache layer (v2.6) ─────────────────────────────────────
1198
- // CacheDecision subflow walks `activeInjections` + evaluates
1199
- // each `cache:` directive, emits provider-agnostic
1200
- // `CacheMarker[]` to scope. Pure transform; no IO.
1201
- //
1202
- // CRITICAL: arrayMerge: ArrayMergeMode.Replace — same lesson
1203
- // as the v2.5.1 InjectionEngine fix. The default footprintjs
1204
- // behavior CONCATENATES arrays from child to parent;
1205
- // `cacheMarkers` MUST replace each iteration, not accumulate.
1206
- .addSubFlowChartNext(conventions_js_1.SUBFLOW_IDS.CACHE_DECISION, CacheDecisionSubflow_js_1.cacheDecisionSubflow, 'CacheDecision', {
1207
- inputMapper: (parent) => ({
1208
- activeInjections: parent.activeInjections ?? [],
1209
- iteration: parent.iteration ?? 1,
1210
- maxIterations: parent.maxIterations ?? maxIterations,
1211
- userMessage: parent.userMessage ?? '',
1212
- ...(parent.lastToolResult !== undefined && {
1213
- lastToolName: parent.lastToolResult?.toolName,
1214
- }),
1215
- cumulativeInputTokens: parent.totalInputTokens ?? 0,
1216
- systemPromptCachePolicy,
1217
- cachingDisabled: parent.cachingDisabled ?? false,
1218
- }),
1219
- outputMapper: (sf) => ({ cacheMarkers: sf.cacheMarkers }),
1220
- arrayMerge: advanced_1.ArrayMergeMode.Replace,
1221
- })
1222
- .addFunction('UpdateSkillHistory', CacheGateDecider_js_1.updateSkillHistory, conventions_js_1.STAGE_IDS.UPDATE_SKILL_HISTORY, 'Update skill-history rolling window for CacheGate churn detection')
1223
- .addDeciderFunction('CacheGate', CacheGateDecider_js_1.cacheGateDecide, conventions_js_1.STAGE_IDS.CACHE_GATE, 'Gate cache-marker application: kill switch / hit-rate / skill-churn')
1224
- .addFunctionBranch(conventions_js_1.STAGE_IDS.APPLY_MARKERS, 'ApplyMarkers',
1225
- // Pass-through stage — markers stay in scope as-is.
1226
- // BuildLLMRequest (Phase 7+) reads them on the next stage.
1227
- () => undefined, 'Proceed with cache markers from CacheDecision')
1228
- .addFunctionBranch(conventions_js_1.STAGE_IDS.SKIP_CACHING, 'SkipCaching',
1229
- // Clear markers so BuildLLMRequest sees an empty list and
1230
- // makes the request unmodified.
1231
- (scope) => {
1232
- scope.cacheMarkers = [];
1233
- }, 'Skip caching this iteration')
1234
- .end()
1235
- .addFunction('IterationStart', iterationStart, 'iteration-start', 'Iteration begin marker')
1236
- .addFunction('CallLLM', callLLM, conventions_js_1.STAGE_IDS.CALL_LLM, 'LLM invocation')
1237
- .addDeciderFunction('Route', routeDecider, conventions_js_1.SUBFLOW_IDS.ROUTE, 'ReAct routing')
1238
- .addPausableFunctionBranch('tool-calls', 'ToolCalls', toolCallsHandler, 'Tool execution (pausable via pauseHere)')
1239
- .addSubFlowChartBranch('final', finalBranchChart, 'Final', {
1240
- // Pass through the read-only state the sub-chart needs;
1241
- // OMIT keys the sub-chart writes (finalContent, newMessages)
1242
- // — passing those via inputMapper would freeze them as args.
1243
- inputMapper: (parent) => {
1244
- const { finalContent: _f, newMessages: _nm, ...rest } = parent;
1245
- void _f;
1246
- void _nm;
1247
- return rest;
545
+ // iterationStart extracted to ./agent/stages/iterationStart.ts (v2.11.2).
546
+ const iterationStart = iterationStart_js_1.iterationStartStage;
547
+ // callLLM extracted to ./agent/stages/callLLM.ts (v2.11.2). Same
548
+ // late-binding pattern as seed for toolSchemas (computed below).
549
+ const callLLM = (0, callLLM_js_1.buildCallLLMStage)({
550
+ provider,
551
+ model,
552
+ ...(temperature !== undefined && { temperature }),
553
+ ...(maxTokens !== undefined && { maxTokens }),
554
+ ...(pricingTable !== undefined && { pricingTable }),
555
+ ...(costBudget !== undefined && { costBudget }),
556
+ maxIterations,
557
+ cacheStrategy,
558
+ get toolSchemas() {
559
+ return toolSchemasResolved;
1248
560
  },
1249
- outputMapper: (sf) => ({
1250
- finalContent: sf.finalContent,
1251
- }),
1252
- // BreakFinal's $break() must reach the outer loopTo so the
1253
- // ReAct iteration terminates; without this the inner break
1254
- // only exits the sub-chart and the outer loop continues.
1255
- propagateBreak: true,
1256
- })
1257
- .setDefault('final')
1258
- .end()
1259
- // Dynamic ReAct: loop back to the InjectionEngine so EVERY iteration
1260
- // re-evaluates triggers (rule predicates, on-tool-return, llm-activated)
1261
- // against the freshest context (the just-appended tool result).
1262
- // Without this, the InjectionEngine runs ONCE per turn and:
1263
- // - on-tool-return predicates never fire on iter 2+
1264
- // - read_skill('X') activations are never picked up next iteration
1265
- // - autoActivate per-skill tool gating is structurally impossible
1266
- // - tools / system-prompt slots stay frozen at iter 1 content
1267
- // The v2.4 default of loopTo(MESSAGES) bypassed all four — quietly
1268
- // breaking the framework's "Dynamic ReAct" claim. v2.5 restores the
1269
- // v1 behavior that documents promise.
1270
- .loopTo(conventions_js_1.SUBFLOW_IDS.INJECTION_ENGINE);
1271
- return builder.build();
561
+ });
562
+ // routeDecider extracted to ./agent/stages/route.ts (v2.11.2).
563
+ const routeDecider = route_js_1.routeDeciderStage;
564
+ // toolCallsHandler extracted to ./agent/stages/toolCalls.ts (v2.11.2).
565
+ const toolCallsHandler = (0, toolCalls_js_1.buildToolCallsHandler)({
566
+ registryByName,
567
+ ...(this.externalToolProvider && { externalToolProvider: this.externalToolProvider }),
568
+ ...(permissionChecker && { permissionChecker }),
569
+ });
570
+ // Chart composition extracted to ./agent/buildAgentChart.ts (v2.11.2).
571
+ return (0, buildAgentChart_js_1.buildAgentChart)({
572
+ memories: this.memories,
573
+ systemPromptCachePolicy,
574
+ maxIterations,
575
+ seed,
576
+ iterationStart,
577
+ callLLM,
578
+ routeDecider,
579
+ toolCallsHandler,
580
+ injectionEngineSubflow,
581
+ systemPromptSubflow,
582
+ messagesSubflow,
583
+ toolsSubflow,
584
+ cacheDecisionSubflow: CacheDecisionSubflow_js_1.cacheDecisionSubflow,
585
+ updateSkillHistoryStage: CacheGateDecider_js_1.updateSkillHistory,
586
+ cacheGateDecide: CacheGateDecider_js_1.cacheGateDecide,
587
+ });
1272
588
  }
1273
589
  }
1274
590
  exports.Agent = Agent;
1275
- /**
1276
- * Fluent builder. `tool()` accepts any Tool<TArgs, TResult> and registers
1277
- * it by its schema.name. Duplicate names throw at build time.
1278
- */
1279
- class AgentBuilder {
1280
- opts;
1281
- systemPromptValue = '';
1282
- /**
1283
- * Cache policy for the base system prompt. Set via the optional
1284
- * 2nd argument to `.system(text, { cache })`. Default `'always'` —
1285
- * the base prompt is stable per-turn and an ideal cache anchor.
1286
- */
1287
- systemPromptCachePolicy = 'always';
1288
- /**
1289
- * Global cache kill switch. Set via `Agent.create({ caching: 'off' })`
1290
- * (handled in `AgentOptions` propagation). Defaults to `false`
1291
- * (caching enabled). When `true`, the CacheGate decider routes to
1292
- * `'no-markers'` every iteration regardless of other rules.
1293
- */
1294
- cachingDisabledValue = false;
1295
- /**
1296
- * Optional explicit CacheStrategy override. Default: undefined,
1297
- * which means the agent auto-resolves from
1298
- * `getDefaultCacheStrategy(provider.name)` at construction. Power
1299
- * users override here for custom backends or test mocks.
1300
- */
1301
- cacheStrategyOverride;
1302
- registry = [];
1303
- injectionList = [];
1304
- memoryList = [];
1305
- /**
1306
- * Optional terminal contract — see `outputSchema()`. Stored on the
1307
- * builder, propagated to the Agent at `.build()` time.
1308
- */
1309
- outputSchemaParser;
1310
- /** 3-tier output fallback chain — set via `.outputFallback({...})`.
1311
- * Optional; absent = current throw-on-validation-failure behavior. */
1312
- outputFallbackCfg;
1313
- /**
1314
- * Optional `ToolProvider` set via `.toolProvider()`. Propagated to
1315
- * the Agent's Tools slot subflow + tool-call dispatcher; consulted
1316
- * per iteration so dynamic chains (`gatedTools`, `skillScopedTools`)
1317
- * react to current activation state.
1318
- */
1319
- toolProviderRef;
1320
- /**
1321
- * Optional override for `AgentOptions.maxIterations`. When set via
1322
- * the `.maxIterations()` builder method, takes precedence over the
1323
- * value passed to `Agent.create({ maxIterations })`.
1324
- */
1325
- maxIterationsOverride;
1326
- /**
1327
- * Recorders collected via `.recorder()`. Attached to the built Agent
1328
- * before `build()` returns (each via `agent.attach(rec)`).
1329
- */
1330
- recorderList = [];
1331
- // Voice config — defaults until the consumer calls .appName() /
1332
- // .commentaryTemplates() / .thinkingTemplates(). Stored as plain
1333
- // dicts (Record<string, string>) so the builder doesn't depend on
1334
- // the template-engine modules at compile time; the runtime types
1335
- // come from the agentfootprint barrel exports.
1336
- appNameValue = 'Chatbot';
1337
- commentaryOverrides = {};
1338
- thinkingOverrides = {};
1339
- constructor(opts) {
1340
- this.opts = opts;
1341
- // Cache layer: opts.caching === 'off' propagates to scope's
1342
- // `cachingDisabled` kill switch read by CacheGate. opts.cacheStrategy
1343
- // overrides the registry-resolved default.
1344
- if (opts.caching === 'off')
1345
- this.cachingDisabledValue = true;
1346
- if (opts.cacheStrategy !== undefined)
1347
- this.cacheStrategyOverride = opts.cacheStrategy;
1348
- }
1349
- /**
1350
- * Set the base system prompt.
1351
- *
1352
- * @param prompt - The system prompt text. Stable per-turn.
1353
- * @param options - Optional config. `cache` controls how the
1354
- * CacheDecision subflow treats this prompt block:
1355
- * - `'always'` (default) — cache the base prompt as a stable
1356
- * prefix anchor. Highest cache-hit rate; recommended for
1357
- * production agents whose system prompt rarely changes.
1358
- * - `'never'` — skip caching. Use if the prompt contains volatile
1359
- * content (timestamps, per-request user IDs).
1360
- * - `'while-active'` — semantically equivalent to `'always'` for
1361
- * the base prompt (it's always active by definition).
1362
- * - `{ until }` — conditional invalidation (e.g., flush after iter 5).
1363
- */
1364
- system(prompt, options) {
1365
- this.systemPromptValue = prompt;
1366
- if (options?.cache !== undefined) {
1367
- this.systemPromptCachePolicy = options.cache;
1368
- }
1369
- return this;
1370
- }
1371
- tool(tool) {
1372
- const name = tool.schema.name;
1373
- if (this.registry.some((e) => e.name === name)) {
1374
- throw new Error(`Agent.tool(): duplicate tool name '${name}'`);
1375
- }
1376
- this.registry.push({ name, tool: tool });
1377
- return this;
1378
- }
1379
- /**
1380
- * Register many tools at once. Convenience for tool sources that
1381
- * return a list (e.g., `await mcpClient(...).tools()`). Each tool
1382
- * is registered via `.tool()` so duplicate-name validation still
1383
- * fires per-entry.
1384
- */
1385
- tools(tools) {
1386
- for (const t of tools)
1387
- this.tool(t);
1388
- return this;
1389
- }
1390
- /**
1391
- * Wire a chainable `ToolProvider` (from `agentfootprint/tool-providers`)
1392
- * as the agent's per-iteration tool source.
1393
- *
1394
- * The provider is consulted EVERY iteration via `provider.list(ctx)`
1395
- * with `ctx = { iteration, activeSkillId, identity }`. Tools the
1396
- * provider emits flow into the Tools slot alongside any static
1397
- * tools registered via `.tool()` / `.tools()`. The tool-call
1398
- * dispatcher also consults the provider so dynamic chains
1399
- * (`gatedTools`, `skillScopedTools`) dispatch correctly when their
1400
- * visible-set changes mid-turn.
1401
- *
1402
- * Throws if called more than once on the same builder (avoids
1403
- * silent override surprises).
1404
- *
1405
- * @example Permission-gated baseline
1406
- * import { gatedTools, staticTools } from 'agentfootprint/tool-providers';
1407
- * import { PermissionPolicy } from 'agentfootprint/security';
1408
- *
1409
- * const policy = PermissionPolicy.fromRoles({
1410
- * readonly: ['lookup', 'list_skills', 'read_skill'],
1411
- * admin: ['lookup', 'list_skills', 'read_skill', 'delete'],
1412
- * }, 'readonly');
1413
- *
1414
- * const provider = gatedTools(
1415
- * staticTools(allTools),
1416
- * (toolName) => policy.isAllowed(toolName),
1417
- * );
1418
- *
1419
- * const agent = Agent.create({ provider: llm, model })
1420
- * .system('You answer.')
1421
- * .toolProvider(provider)
1422
- * .build();
1423
- */
1424
- toolProvider(provider) {
1425
- if (this.toolProviderRef) {
1426
- throw new Error('AgentBuilder.toolProvider: already set. Each agent has at most one external ToolProvider.');
1427
- }
1428
- this.toolProviderRef = provider;
1429
- return this;
1430
- }
1431
- /**
1432
- * Override the ReAct iteration cap set via `Agent.create({
1433
- * maxIterations })`. Convenience for builder-style code that prefers
1434
- * fluent setters over constructor opts. Last call wins.
1435
- *
1436
- * Throws if `n` is not a positive integer or exceeds the hard cap
1437
- * (`clampIterations`'s upper bound).
1438
- */
1439
- maxIterations(n) {
1440
- if (!Number.isInteger(n) || n <= 0) {
1441
- throw new Error(`AgentBuilder.maxIterations: expected a positive integer, got ${n}.`);
1442
- }
1443
- this.maxIterationsOverride = n;
1444
- return this;
1445
- }
1446
- /**
1447
- * Attach a footprintjs `CombinedRecorder` to the built Agent. Wired
1448
- * via `agent.attach(rec)` immediately after construction, so the
1449
- * recorder sees every event from the very first run.
1450
- *
1451
- * Equivalent to calling `agent.attach(rec)` post-build; the builder
1452
- * method is a convenience for codebases that prefer fully-fluent
1453
- * agent assembly. Multiple recorders are supported (each gets its
1454
- * own `attach()` call).
1455
- */
1456
- recorder(rec) {
1457
- this.recorderList.push(rec);
1458
- return this;
1459
- }
1460
- /**
1461
- * Set the agent's display name — substituted as `{{appName}}` in
1462
- * commentary + thinking templates. Same place to brand a tenant
1463
- * ("Acme Bot"), distinguish multi-agent roles ("Triage" vs
1464
- * "Reviewer"), or localize ("Asistente"). Default: `'Chatbot'`.
1465
- */
1466
- appName(name) {
1467
- this.appNameValue = name;
1468
- return this;
1469
- }
1470
- /**
1471
- * Override agentfootprint's bundled commentary templates. Spread on
1472
- * top of `defaultCommentaryTemplates`; missing keys fall back. Same
1473
- * `Record<string, string>` shape with `{{vars}}` substitution as
1474
- * the bundled defaults — see `defaultCommentaryTemplates` for the
1475
- * full key list.
1476
- *
1477
- * Use cases: i18n (`'agent.turn_start': 'El usuario...'`), brand
1478
- * voice ("You: {{userPrompt}}"), per-tenant customization.
1479
- */
1480
- commentaryTemplates(templates) {
1481
- this.commentaryOverrides = { ...this.commentaryOverrides, ...templates };
1482
- return this;
1483
- }
1484
- /**
1485
- * Override agentfootprint's bundled thinking templates. Same
1486
- * contract shape as commentary; different vocabulary — first-person
1487
- * status the chat bubble shows mid-call. Per-tool overrides go via
1488
- * `tool.<toolName>` keys (e.g., `'tool.weather': 'Looking up the
1489
- * weather…'`). See `defaultThinkingTemplates` for the full key list.
1490
- */
1491
- thinkingTemplates(templates) {
1492
- this.thinkingOverrides = { ...this.thinkingOverrides, ...templates };
1493
- return this;
1494
- }
1495
- // ─── Injection sugar — context engineering surface ───────────
1496
- //
1497
- // ALL of these push into the same `injectionList`. The Injection
1498
- // primitive is identical across flavors; the methods are just
1499
- // narrative-friendly aliases. Duplicate ids throw at build time.
1500
- /**
1501
- * Register any `Injection`. Use this for power-user / custom flavors;
1502
- * for built-in flavors use the typed sugar (`.skill`, `.steering`,
1503
- * `.instruction`, `.fact`).
1504
- */
1505
- injection(injection) {
1506
- if (this.injectionList.some((i) => i.id === injection.id)) {
1507
- throw new Error(`Agent.injection(): duplicate id '${injection.id}'`);
1508
- }
1509
- this.injectionList.push(injection);
1510
- return this;
1511
- }
1512
- /**
1513
- * Register a Skill — LLM-activated, system-prompt + tools.
1514
- * Auto-attaches the `read_skill` activation tool to the agent.
1515
- * Skill stays active for the rest of the turn once activated.
1516
- */
1517
- skill(injection) {
1518
- return this.injection(injection);
1519
- }
1520
- /**
1521
- * Bulk-register every Skill in a `SkillRegistry`. Use for shared
1522
- * skill catalogs across multiple Agents — register skills once on
1523
- * the registry; attach the same registry to every consumer Agent.
1524
- *
1525
- * @example
1526
- * const registry = new SkillRegistry();
1527
- * registry.register(billingSkill).register(refundSkill);
1528
- * const supportAgent = Agent.create({ provider }).skills(registry).build();
1529
- * const escalationAgent = Agent.create({ provider }).skills(registry).build();
1530
- */
1531
- skills(registry) {
1532
- for (const skill of registry.list())
1533
- this.injection(skill);
1534
- return this;
1535
- }
1536
- /**
1537
- * Register a Steering doc — always-on system-prompt rule.
1538
- * Use for invariant guidance: output format, persona, safety policies.
1539
- */
1540
- steering(injection) {
1541
- return this.injection(injection);
1542
- }
1543
- /**
1544
- * Register an Instruction — rule-based system-prompt guidance.
1545
- * Predicate runs each iteration. Use for context-dependent rules
1546
- * including the "Dynamic ReAct" `on-tool-return` pattern.
1547
- */
1548
- instruction(injection) {
1549
- return this.injection(injection);
1550
- }
1551
- /**
1552
- * Bulk-register many instructions at once. Convenience for consumer
1553
- * code that organizes its instruction set in a flat array (`const
1554
- * instructions = [outputFormat, dataRouting, ...]`). Each element
1555
- * is registered via `.instruction()` so duplicate-id checks still
1556
- * fire per-entry.
1557
- */
1558
- instructions(injections) {
1559
- for (const i of injections)
1560
- this.instruction(i);
1561
- return this;
1562
- }
1563
- /**
1564
- * Register a Fact — developer-supplied data the LLM should see.
1565
- * User profile, env info, computed summary, current time, …
1566
- * Distinct from Skills (LLM-activated guidance) and Steering
1567
- * (always-on rules) in INTENT — the engine treats them all alike.
1568
- */
1569
- fact(injection) {
1570
- return this.injection(injection);
1571
- }
1572
- /**
1573
- * Register a Memory subsystem — load/persist conversation context,
1574
- * facts, narrative beats, or causal snapshots across runs.
1575
- *
1576
- * The `MemoryDefinition` is produced by `defineMemory({ type, strategy,
1577
- * store })`. Multiple memories layer cleanly via per-id scope keys
1578
- * (`memoryInjection_${id}`):
1579
- *
1580
- * ```ts
1581
- * Agent.create({ provider })
1582
- * .memory(defineMemory({ id: 'short', type: MEMORY_TYPES.EPISODIC,
1583
- * strategy: { kind: MEMORY_STRATEGIES.WINDOW, size: 10 },
1584
- * store }))
1585
- * .memory(defineMemory({ id: 'facts', type: MEMORY_TYPES.SEMANTIC,
1586
- * strategy: { kind: MEMORY_STRATEGIES.EXTRACT,
1587
- * extractor: 'pattern' }, store }))
1588
- * .build();
1589
- * ```
1590
- *
1591
- * The READ subflow runs at the configured `timing` (default
1592
- * `MEMORY_TIMING.TURN_START`) and writes its formatted output to the
1593
- * `memoryInjection_${id}` scope key for the slot subflows to consume.
1594
- */
1595
- memory(definition) {
1596
- if (this.memoryList.some((m) => m.id === definition.id)) {
1597
- throw new Error(`Agent.memory(): duplicate id '${definition.id}' — each memory needs a unique id ` +
1598
- 'to keep its scope key (`memoryInjection_${id}`) collision-free.');
1599
- }
1600
- this.memoryList.push(definition);
1601
- return this;
1602
- }
1603
- /**
1604
- * Register a RAG retriever — semantic search over a vector-indexed
1605
- * corpus. Identical plumbing to `.memory()` (RAG resolves to a
1606
- * `MemoryDefinition` produced by `defineRAG()`); this alias exists
1607
- * so the consumer's intent reads clearly:
1608
- *
1609
- * ```ts
1610
- * agent
1611
- * .memory(shortTermConversation) // remembers what the USER said
1612
- * .rag(productDocs) // retrieves what the CORPUS says
1613
- * .build();
1614
- * ```
1615
- *
1616
- * Both end up as memory subflows, but the alias separates "user
1617
- * conversation memory" from "document corpus retrieval" in code
1618
- * intent, ids, and Lens chips.
1619
- */
1620
- rag(definition) {
1621
- return this.memory(definition);
1622
- }
1623
- /**
1624
- * Declarative terminal contract. The agent's final answer must be
1625
- * JSON matching `parser`. Auto-injects a system-prompt instruction
1626
- * telling the LLM the shape, and exposes `agent.runTyped()` /
1627
- * `agent.parseOutput()` for parse + validate at the call site.
1628
- *
1629
- * The `parser` is duck-typed: any object with a `parse(unknown): T`
1630
- * method works (Zod, Valibot, ArkType, hand-written). The optional
1631
- * `description` field on the parser drives the auto-generated
1632
- * instruction; consumers can also override via `opts.instruction`.
1633
- *
1634
- * Throws if called more than once on the same builder (avoids
1635
- * silent override surprises).
1636
- *
1637
- * @param parser Validation strategy that throws on shape failure.
1638
- * @param opts Optional `{ name, instruction }` to customize.
1639
- *
1640
- * @example
1641
- * import { z } from 'zod';
1642
- * const Output = z.object({
1643
- * status: z.enum(['ok', 'err']),
1644
- * items: z.array(z.string()),
1645
- * }).describe('A status enum + an array of strings.');
1646
- *
1647
- * const agent = Agent.create({...})
1648
- * .outputSchema(Output)
1649
- * .build();
1650
- *
1651
- * const typed = await agent.runTyped({ message: '...' });
1652
- * typed.status; // narrowed to 'ok' | 'err'
1653
- */
1654
- outputSchema(parser, opts) {
1655
- if (this.outputSchemaParser) {
1656
- throw new Error('AgentBuilder.outputSchema: already set. Each agent has at most one terminal contract.');
1657
- }
1658
- this.outputSchemaParser = parser;
1659
- const instructionText = opts?.instruction ?? (0, outputSchema_js_1.buildDefaultInstruction)(parser);
1660
- const id = opts?.name ?? 'output-schema';
1661
- // Always-on system-slot instruction. Activates every iteration so
1662
- // long runs keep the contract present (recency-first redundancy).
1663
- this.injectionList.push((0, defineInstruction_js_1.defineInstruction)({
1664
- id,
1665
- activeWhen: () => true,
1666
- prompt: instructionText,
1667
- }));
1668
- return this;
1669
- }
1670
- /**
1671
- * 3-tier degradation for output-schema validation failures. Pairs
1672
- * with `.outputSchema()` — calling `.outputFallback()` without an
1673
- * `outputSchema` first throws (the fallback has nothing to validate).
1674
- *
1675
- * Three tiers:
1676
- *
1677
- * 1. **Primary** — LLM emitted schema-valid JSON. Caller gets it.
1678
- * 2. **Fallback** — `OutputSchemaError` thrown. The async
1679
- * `fallback(error, raw)` runs; its return is re-validated.
1680
- * 3. **Canned** — static safety-net value. NEVER throws when set.
1681
- *
1682
- * `canned` is validated against the schema at builder time —
1683
- * fail-fast on misconfig (a `canned` that doesn't validate would
1684
- * defeat the fail-open guarantee).
1685
- *
1686
- * Two typed events fire on tier transitions for observability:
1687
- * - `agentfootprint.resilience.output_fallback_triggered`
1688
- * - `agentfootprint.resilience.output_canned_used`
1689
- *
1690
- * @example
1691
- * ```ts
1692
- * import { z } from 'zod';
1693
- * const Refund = z.object({ amount: z.number(), reason: z.string() });
1694
- *
1695
- * const agent = Agent.create({...})
1696
- * .outputSchema(Refund)
1697
- * .outputFallback({
1698
- * fallback: async (err, raw) => ({ amount: 0, reason: 'manual review' }),
1699
- * canned: { amount: 0, reason: 'unable to process' },
1700
- * })
1701
- * .build();
1702
- * ```
1703
- */
1704
- outputFallback(options) {
1705
- if (!this.outputSchemaParser) {
1706
- throw new Error('AgentBuilder.outputFallback: call .outputSchema(parser) FIRST. ' +
1707
- 'outputFallback supplements outputSchema; one without the other is incoherent.');
1708
- }
1709
- if (this.outputFallbackCfg) {
1710
- throw new Error('AgentBuilder.outputFallback: already set. Each agent has at most one fallback chain.');
1711
- }
1712
- // Build-time validation — canned MUST satisfy the schema.
1713
- if (options.canned !== undefined) {
1714
- (0, outputFallback_js_1.validateCannedAgainstSchema)(options.canned, this.outputSchemaParser);
1715
- }
1716
- this.outputFallbackCfg = {
1717
- fallback: options.fallback,
1718
- ...(options.canned !== undefined && { canned: options.canned }),
1719
- hasCanned: options.canned !== undefined,
1720
- };
1721
- return this;
1722
- }
1723
- build() {
1724
- // Resolve the voice config: bundled defaults + consumer overrides.
1725
- // Templates flow through the same barrel exports the rest of the
1726
- // library uses, so a future locale-pack swap is a single import.
1727
- const voice = {
1728
- appName: this.appNameValue,
1729
- commentaryTemplates: { ...commentaryTemplates_js_1.defaultCommentaryTemplates, ...this.commentaryOverrides },
1730
- thinkingTemplates: { ...thinkingTemplates_js_1.defaultThinkingTemplates, ...this.thinkingOverrides },
1731
- };
1732
- const opts = this.maxIterationsOverride !== undefined
1733
- ? { ...this.opts, maxIterations: this.maxIterationsOverride }
1734
- : this.opts;
1735
- const agent = new Agent(opts, this.systemPromptValue, this.registry, voice, this.injectionList, this.memoryList, this.outputSchemaParser, this.toolProviderRef, this.systemPromptCachePolicy, this.cachingDisabledValue, this.cacheStrategyOverride, this.outputFallbackCfg);
1736
- // Attach builder-collected recorders so they receive events from
1737
- // the very first run. Mirrors what consumers would do post-build
1738
- // via `agent.attach(rec)`; the builder method is purely sugar.
1739
- for (const rec of this.recorderList) {
1740
- agent.attach(rec);
1741
- }
1742
- return agent;
1743
- }
1744
- }
1745
- exports.AgentBuilder = AgentBuilder;
1746
- function validateMemoryIdUniqueness(memories) {
1747
- const seen = new Set();
1748
- for (const m of memories) {
1749
- if (seen.has(m.id)) {
1750
- throw new Error(`Agent: duplicate memory id '${m.id}'. Each memory needs a unique id to keep ` +
1751
- 'its scope key (`memoryInjection_${id}`) collision-free.');
1752
- }
1753
- seen.add(m.id);
1754
- }
1755
- }
1756
- function clampIterations(n) {
1757
- if (!Number.isInteger(n) || n < 1)
1758
- return 1;
1759
- if (n > 50)
1760
- return 50;
1761
- return n;
1762
- }
1763
- /**
1764
- * Validate tool-name uniqueness across `.tool()`-registered tools +
1765
- * every Skill's `inject.tools[]`. The LLM dispatches by `tool.schema.name`
1766
- * (the wire format), so any collision silently shadows execution.
1767
- *
1768
- * Called eagerly in the Agent constructor so `Agent.build()` throws
1769
- * immediately, not on first `run()`.
1770
- *
1771
- * `read_skill` is reserved when ≥1 Skill is registered — collisions
1772
- * with consumer tools throw.
1773
- */
1774
- function validateToolNameUniqueness(registry, injections) {
1775
- // Static registry: unique within itself. The Agent.tool() builder
1776
- // method already throws on per-call duplicates; this is the
1777
- // belt-and-suspenders check at build time.
1778
- const staticNames = new Set();
1779
- for (const entry of registry) {
1780
- if (staticNames.has(entry.name)) {
1781
- throw new Error(`Agent: duplicate tool name '${entry.name}' in .tool() registry. ` +
1782
- `Tool names must be unique within the static registry.`);
1783
- }
1784
- staticNames.add(entry.name);
1785
- }
1786
- // `read_skill` is reserved when any Skill is registered. Collisions
1787
- // with consumer-supplied tools break the auto-attach path.
1788
- const skills = injections.filter((i) => i.flavor === 'skill');
1789
- if (skills.length > 0 && staticNames.has('read_skill')) {
1790
- throw new Error(`Agent: tool name 'read_skill' is reserved when ≥1 Skill is registered. ` +
1791
- `Rename your custom 'read_skill' tool or unregister it.`);
1792
- }
1793
- // Per-skill check: a skill's `inject.tools` array must be internally
1794
- // unique (no duplicate names within the same skill — that's a
1795
- // skill authoring bug). Across skills, sharing a Tool reference is
1796
- // EXPECTED and supported — common tools (e.g., a `flogi_lookup`
1797
- // used by multiple investigation skills) appear in multiple skills'
1798
- // tool arrays. Only one skill is active at a time (or, when several
1799
- // are active, deduped by name + reference at runtime). Sharing the
1800
- // same Tool object across skills is the supported pattern; sharing
1801
- // a Tool NAME with a DIFFERENT execute function is the actual bug —
1802
- // we detect that here too.
1803
- const seenByName = new Map();
1804
- for (const skill of skills) {
1805
- const intraSkill = new Set();
1806
- for (const tool of skill.inject.tools ?? []) {
1807
- const name = tool.schema.name;
1808
- if (intraSkill.has(name)) {
1809
- throw new Error(`Agent: skill '${skill.id}' lists tool '${name}' more than once in its ` +
1810
- `inject.tools array. Each skill's tools must be unique within itself.`);
1811
- }
1812
- intraSkill.add(name);
1813
- // Skill tools collide with the static .tool() registry → ambiguous dispatch
1814
- if (staticNames.has(name)) {
1815
- throw new Error(`Agent: skill '${skill.id}' tool '${name}' collides with the static .tool() ` +
1816
- `registry. Either rename the skill's tool or remove the static registration.`);
1817
- }
1818
- // Same name across skills with DIFFERENT Tool objects = ambiguous when
1819
- // both skills active. Same name + SAME Tool reference = supported sharing.
1820
- const prior = seenByName.get(name);
1821
- if (prior && prior !== tool) {
1822
- throw new Error(`Agent: tool name '${name}' is declared by multiple skills with different ` +
1823
- `Tool implementations. Skills MAY share the SAME Tool reference across ` +
1824
- `their inject.tools arrays (deduped at dispatch); they may NOT register ` +
1825
- `different functions under the same name (ambiguous dispatch).`);
1826
- }
1827
- seenByName.set(name, tool);
1828
- }
1829
- }
1830
- }
1831
- /**
1832
- * JSON.stringify with circular-ref protection. Tool results are untrusted —
1833
- * a hostile/buggy tool returning a cyclic object must not crash the run.
1834
- * Falls back to '[unstringifiable: <reason>]' so the LLM still sees that
1835
- * the tool ran and produced something unusable.
1836
- */
1837
- function safeStringify(value) {
1838
- try {
1839
- return JSON.stringify(value);
1840
- }
1841
- catch (err) {
1842
- const reason = err instanceof Error ? err.message : String(err);
1843
- return `[unstringifiable: ${reason}]`;
1844
- }
1845
- }
591
+ // AgentBuilder extracted to ./agent/AgentBuilder.ts (v2.11.2).
592
+ // Re-export so the 28+ existing import sites continue to work unchanged.
593
+ // Validators + helpers extracted to ./agent/validators.ts (v2.11.1).
1846
594
  //# sourceMappingURL=Agent.js.map