agentfootprint 2.11.1 → 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 (67) hide show
  1. package/dist/core/Agent.js +81 -1238
  2. package/dist/core/Agent.js.map +1 -1
  3. package/dist/core/agent/AgentBuilder.js +489 -0
  4. package/dist/core/agent/AgentBuilder.js.map +1 -0
  5. package/dist/core/agent/buildAgentChart.js +227 -0
  6. package/dist/core/agent/buildAgentChart.js.map +1 -0
  7. package/dist/core/agent/buildToolRegistry.js +115 -0
  8. package/dist/core/agent/buildToolRegistry.js.map +1 -0
  9. package/dist/core/agent/stages/breakFinal.js +28 -0
  10. package/dist/core/agent/stages/breakFinal.js.map +1 -0
  11. package/dist/core/agent/stages/callLLM.js +129 -0
  12. package/dist/core/agent/stages/callLLM.js.map +1 -0
  13. package/dist/core/agent/stages/iterationStart.js +24 -0
  14. package/dist/core/agent/stages/iterationStart.js.map +1 -0
  15. package/dist/core/agent/stages/prepareFinal.js +45 -0
  16. package/dist/core/agent/stages/prepareFinal.js.map +1 -0
  17. package/dist/core/agent/stages/route.js +36 -0
  18. package/dist/core/agent/stages/route.js.map +1 -0
  19. package/dist/core/agent/stages/seed.js +95 -0
  20. package/dist/core/agent/stages/seed.js.map +1 -0
  21. package/dist/core/agent/stages/toolCalls.js +250 -0
  22. package/dist/core/agent/stages/toolCalls.js.map +1 -0
  23. package/dist/esm/core/Agent.js +83 -1239
  24. package/dist/esm/core/Agent.js.map +1 -1
  25. package/dist/esm/core/agent/AgentBuilder.js +485 -0
  26. package/dist/esm/core/agent/AgentBuilder.js.map +1 -0
  27. package/dist/esm/core/agent/buildAgentChart.js +223 -0
  28. package/dist/esm/core/agent/buildAgentChart.js.map +1 -0
  29. package/dist/esm/core/agent/buildToolRegistry.js +111 -0
  30. package/dist/esm/core/agent/buildToolRegistry.js.map +1 -0
  31. package/dist/esm/core/agent/stages/breakFinal.js +24 -0
  32. package/dist/esm/core/agent/stages/breakFinal.js.map +1 -0
  33. package/dist/esm/core/agent/stages/callLLM.js +125 -0
  34. package/dist/esm/core/agent/stages/callLLM.js.map +1 -0
  35. package/dist/esm/core/agent/stages/iterationStart.js +20 -0
  36. package/dist/esm/core/agent/stages/iterationStart.js.map +1 -0
  37. package/dist/esm/core/agent/stages/prepareFinal.js +41 -0
  38. package/dist/esm/core/agent/stages/prepareFinal.js.map +1 -0
  39. package/dist/esm/core/agent/stages/route.js +32 -0
  40. package/dist/esm/core/agent/stages/route.js.map +1 -0
  41. package/dist/esm/core/agent/stages/seed.js +91 -0
  42. package/dist/esm/core/agent/stages/seed.js.map +1 -0
  43. package/dist/esm/core/agent/stages/toolCalls.js +246 -0
  44. package/dist/esm/core/agent/stages/toolCalls.js.map +1 -0
  45. package/dist/types/core/Agent.d.ts +5 -333
  46. package/dist/types/core/Agent.d.ts.map +1 -1
  47. package/dist/types/core/agent/AgentBuilder.d.ts +348 -0
  48. package/dist/types/core/agent/AgentBuilder.d.ts.map +1 -0
  49. package/dist/types/core/agent/buildAgentChart.d.ts +74 -0
  50. package/dist/types/core/agent/buildAgentChart.d.ts.map +1 -0
  51. package/dist/types/core/agent/buildToolRegistry.d.ts +62 -0
  52. package/dist/types/core/agent/buildToolRegistry.d.ts.map +1 -0
  53. package/dist/types/core/agent/stages/breakFinal.d.ts +23 -0
  54. package/dist/types/core/agent/stages/breakFinal.d.ts.map +1 -0
  55. package/dist/types/core/agent/stages/callLLM.d.ts +54 -0
  56. package/dist/types/core/agent/stages/callLLM.d.ts.map +1 -0
  57. package/dist/types/core/agent/stages/iterationStart.d.ts +16 -0
  58. package/dist/types/core/agent/stages/iterationStart.d.ts.map +1 -0
  59. package/dist/types/core/agent/stages/prepareFinal.d.ts +20 -0
  60. package/dist/types/core/agent/stages/prepareFinal.d.ts.map +1 -0
  61. package/dist/types/core/agent/stages/route.d.ts +19 -0
  62. package/dist/types/core/agent/stages/route.d.ts.map +1 -0
  63. package/dist/types/core/agent/stages/seed.d.ts +54 -0
  64. package/dist/types/core/agent/stages/seed.d.ts.map +1 -0
  65. package/dist/types/core/agent/stages/toolCalls.d.ts +50 -0
  66. package/dist/types/core/agent/stages/toolCalls.d.ts.map +1 -0
  67. package/package.json +1 -1
@@ -13,22 +13,10 @@
13
13
  * agentfootprint.stream.tool_start / tool_end
14
14
  * agentfootprint.context.* (via ContextRecorder)
15
15
  */
16
- import { FlowChartExecutor, flowChart, } from 'footprintjs';
17
- // ArrayMergeMode lives on footprintjs's `advanced` subpath, not its
18
- // main barrel. Used to set `arrayMerge: Replace` on subflow output
19
- // mapping for the Tools slot — the slot's deduped tool list must
20
- // REPLACE the parent's `dynamicToolSchemas` rather than concatenate
21
- // with it (default behavior re-introduces duplicate tool names that
22
- // LLM providers reject).
23
- import { ArrayMergeMode } from 'footprintjs/advanced';
16
+ import { FlowChartExecutor, } from 'footprintjs';
24
17
  import { cacheDecisionSubflow } from '../cache/CacheDecisionSubflow.js';
25
18
  import { cacheGateDecide, updateSkillHistory as updateSkillHistoryStage, } from '../cache/CacheGateDecider.js';
26
19
  import { getDefaultCacheStrategy } from '../cache/strategyRegistry.js';
27
- import { isPauseRequest } from './pause.js';
28
- import { emitCostTick } from './cost.js';
29
- import { STAGE_IDS, SUBFLOW_IDS } from '../conventions.js';
30
- import { defaultCommentaryTemplates } from '../recorders/observability/commentary/commentaryTemplates.js';
31
- import { defaultThinkingTemplates } from '../recorders/observability/thinking/thinkingTemplates.js';
32
20
  import { ContextRecorder } from '../recorders/core/ContextRecorder.js';
33
21
  import { streamRecorder } from '../recorders/core/StreamRecorder.js';
34
22
  import { agentRecorder } from '../recorders/core/AgentRecorder.js';
@@ -37,21 +25,24 @@ import { permissionRecorder } from '../recorders/core/PermissionRecorder.js';
37
25
  import { evalRecorder } from '../recorders/core/EvalRecorder.js';
38
26
  import { memoryRecorder } from '../recorders/core/MemoryRecorder.js';
39
27
  import { skillRecorder } from '../recorders/core/SkillRecorder.js';
40
- import { typedEmit } from '../recorders/core/typedEmit.js';
41
- import { memoryInjectionKey } from '../memory/define.types.js';
42
- import { unwrapMemoryFlowChart } from '../memory/define.js';
43
- import { mountMemoryRead, mountMemoryWrite } from '../memory/wire/mountMemoryPipeline.js';
44
28
  import { buildSystemPromptSlot } from './slots/buildSystemPromptSlot.js';
45
29
  import { buildMessagesSlot } from './slots/buildMessagesSlot.js';
46
30
  import { buildToolsSlot } from './slots/buildToolsSlot.js';
47
31
  import { buildInjectionEngineSubflow } from '../lib/injection-engine/buildInjectionEngineSubflow.js';
48
- import { buildReadSkillTool } from '../lib/injection-engine/skillTools.js';
49
- import { defineInstruction } from '../lib/injection-engine/factories/defineInstruction.js';
50
- import { applyOutputFallback, validateCannedAgainstSchema, } from './outputFallback.js';
32
+ import { applyOutputFallback } from './outputFallback.js';
51
33
  import { buildCheckpoint, classifyFailurePhase, RunCheckpointError, validateCheckpoint, } from './runCheckpoint.js';
52
- import { applyOutputSchema, buildDefaultInstruction, OutputSchemaError, } from './outputSchema.js';
34
+ import { applyOutputSchema, OutputSchemaError } from './outputSchema.js';
53
35
  import { RunnerBase, makeRunId } from './RunnerBase.js';
54
- import { clampIterations, safeStringify, validateMemoryIdUniqueness, validateToolNameUniqueness, } from './agent/validators.js';
36
+ import { clampIterations, validateMemoryIdUniqueness, validateToolNameUniqueness, } from './agent/validators.js';
37
+ import { iterationStartStage } from './agent/stages/iterationStart.js';
38
+ import { routeDeciderStage } from './agent/stages/route.js';
39
+ import { buildSeedStage } from './agent/stages/seed.js';
40
+ import { buildCallLLMStage } from './agent/stages/callLLM.js';
41
+ import { buildToolCallsHandler } from './agent/stages/toolCalls.js';
42
+ import { buildAgentChart } from './agent/buildAgentChart.js';
43
+ import { buildToolRegistry } from './agent/buildToolRegistry.js';
44
+ import { AgentBuilder } from './agent/AgentBuilder.js';
45
+ export { AgentBuilder };
55
46
  // Public types (AgentOptions, AgentInput, AgentOutput) extracted to
56
47
  // ./agent/types.ts and re-exported above (v2.11.1).
57
48
  // AgentState extracted to ./agent/types.ts (v2.11.1).
@@ -509,168 +500,33 @@ export class Agent extends RunnerBase {
509
500
  const systemPromptCachePolicy = this.systemPromptCachePolicy;
510
501
  const cachingDisabled = this.cachingDisabledValue;
511
502
  const cacheStrategy = this.cacheStrategy;
512
- const seed = (scope) => {
513
- const args = scope.$getArgs();
514
- scope.userMessage = args.message;
515
- // If `resumeOnError(...)` set the side channel, restore the
516
- // checkpointed conversation history. The next iteration sees
517
- // the prior messages and continues from the failure point.
518
- // We always clear the field after reading so subsequent runs
519
- // (without resumeOnError) start fresh.
520
- if (this.pendingResumeHistory && this.pendingResumeHistory.length > 0) {
521
- scope.history = [...this.pendingResumeHistory];
503
+ // seed extracted to ./agent/stages/seed.ts (v2.11.2). Factory takes
504
+ // chart-build-time constants + per-run mutable accessors so the
505
+ // resume side-channel and current run id remain dynamic.
506
+ // toolSchemas is finalized further down; pass a getter that reads
507
+ // the eventual const at stage-execution time.
508
+ let toolSchemasResolved = [];
509
+ const seed = buildSeedStage({
510
+ maxIterations,
511
+ cachingDisabled,
512
+ get toolSchemas() {
513
+ return toolSchemasResolved;
514
+ },
515
+ consumePendingResumeHistory: () => {
516
+ const h = this.pendingResumeHistory;
522
517
  this.pendingResumeHistory = undefined;
523
- }
524
- else {
525
- scope.history = [{ role: 'user', content: args.message }];
526
- }
527
- // Default identity uses the runId so multi-run isolation works
528
- // without consumer changes; explicit identity (multi-tenant)
529
- // overrides via `agent.run({ identity })`.
530
- scope.runIdentity = args.identity ?? {
531
- conversationId: this.currentRunContext?.runId ?? 'default',
532
- };
533
- scope.newMessages = [];
534
- scope.turnNumber = 1;
535
- // Permissive default — explicit cap will land when PricingTable
536
- // gets a context-window field. Memory pickByBudget treats anything
537
- // ≥ minimumTokens as "fits", so this just enables the budget path.
538
- scope.contextTokensRemaining = 32_000;
539
- scope.iteration = 1;
540
- scope.maxIterations = maxIterations;
541
- scope.finalContent = '';
542
- scope.totalInputTokens = 0;
543
- scope.totalOutputTokens = 0;
544
- scope.turnStartMs = Date.now();
545
- scope.systemPromptInjections = [];
546
- scope.messagesInjections = [];
547
- scope.toolsInjections = [];
548
- scope.llmLatestContent = '';
549
- scope.llmLatestToolCalls = [];
550
- scope.pausedToolCallId = '';
551
- scope.pausedToolName = '';
552
- scope.pausedToolStartMs = 0;
553
- scope.cumTokensInput = 0;
554
- scope.cumTokensOutput = 0;
555
- scope.cumEstimatedUsd = 0;
556
- scope.costBudgetHit = false;
557
- scope.activeInjections = [];
558
- scope.activatedInjectionIds = [];
559
- scope.dynamicToolSchemas = toolSchemas;
560
- // Cache layer state (v2.6) — initialized to inert defaults.
561
- // CacheDecision subflow populates `cacheMarkers` per iteration;
562
- // UpdateSkillHistory + CacheGate consume `cachingDisabled`,
563
- // `recentHitRate`, `skillHistory`. Empty defaults mean the
564
- // CacheGate falls through to 'apply-markers' on iter 1 (no
565
- // history yet → no churn detected; recentHitRate undefined →
566
- // hit-rate floor doesn't fire).
567
- scope.cacheMarkers = [];
568
- scope.cachingDisabled = cachingDisabled;
569
- scope.recentHitRate = undefined;
570
- scope.skillHistory = [];
571
- typedEmit(scope, 'agentfootprint.agent.turn_start', {
572
- turnIndex: 0,
573
- userPrompt: args.message,
574
- });
575
- };
576
- // Tool registry composition — three sources:
577
- //
578
- // 1. Static registry: tools registered via `.tool()`. Always
579
- // visible to the LLM; always executable.
580
- // 2. `read_skill` (auto-attached when ≥1 Skill is registered):
581
- // activation tool for LLM-guided Skills.
582
- // 3. Skill-supplied tools (`Skill.inject.tools[]`): visible only
583
- // when the Skill is active (filtered by tools slot subflow);
584
- // MUST always be in the executor registry so when the LLM
585
- // calls one, the tool-calls handler can dispatch.
586
- //
587
- // Tool-name uniqueness is enforced across all three sources at
588
- // build time. The LLM only sees `tool.schema.name` (no ids), so
589
- // names ARE the runtime dispatch key — collisions break the LLM's
590
- // ability to call the right tool. Throw early instead of subtly
591
- // shadowing.
592
- const skills = this.injections.filter((i) => i.flavor === 'skill');
593
- // Collect skill tools, deduping by name when the SAME Tool reference
594
- // is shared across skills. Different Tool implementations under the
595
- // same name throws (already validated upstream by
596
- // validateToolNameUniqueness) — we keep the runtime check as
597
- // belt-and-suspenders.
598
- //
599
- // Block C runtime — `autoActivate: 'currentSkill'` semantics:
600
- // When a skill's `defineSkill({ autoActivate: 'currentSkill' })`
601
- // is set, its tools are EXCLUDED from the static registry. They
602
- // flow into the LLM's tool list ONLY through `dynamicSchemas`
603
- // (the buildToolsSlot path that reads activeInjections), which
604
- // means they're visible ONLY on iterations after the skill is
605
- // activated by `read_skill('id')`. Without this, the LLM sees
606
- // every skill's tools on every iteration and the
607
- // per-skill-narrowing autoActivate promised in `defineSkill`
608
- // doesn't actually narrow anything. Skills WITHOUT autoActivate
609
- // keep the v2.4 behavior (tools always visible) for back-compat.
610
- const skillToolEntries = [];
611
- const sharedSkillTools = new Map();
612
- for (const skill of skills) {
613
- const meta = skill.metadata;
614
- const isAutoActivate = meta?.autoActivate === 'currentSkill';
615
- const toolsFromSkill = skill.inject.tools ?? [];
616
- for (const tool of toolsFromSkill) {
617
- const name = tool.schema.name;
618
- const existing = sharedSkillTools.get(name);
619
- if (existing) {
620
- if (existing !== tool) {
621
- throw new Error(`Agent: tool name '${name}' is declared by multiple skills with different ` +
622
- `Tool implementations. Skills MAY share the SAME Tool reference; they may ` +
623
- `NOT register different functions under the same name.`);
624
- }
625
- continue; // dedupe — same reference already added
626
- }
627
- sharedSkillTools.set(name, tool);
628
- // autoActivate skills: their tools come ONLY through
629
- // dynamicSchemas (buildToolsSlot.ts pulls them from
630
- // activeInjections.inject.tools when the skill is active).
631
- // Don't pre-load them in the static registry.
632
- if (isAutoActivate)
633
- continue;
634
- skillToolEntries.push({ name, tool });
635
- }
636
- }
637
- // buildReadSkillTool returns undefined when skills is empty; the
638
- // length check below short-circuits so the non-null assertion is safe.
639
- const readSkillEntries = skills.length > 0 ? [{ name: 'read_skill', tool: buildReadSkillTool(skills) }] : [];
640
- const augmentedRegistry = [
641
- ...registry,
642
- ...readSkillEntries,
643
- ...skillToolEntries,
644
- ];
645
- // Final cross-source name-uniqueness check: static .tool() vs
646
- // read_skill vs (deduped) skill tools. After the dedupe above this
647
- // catches collisions BETWEEN sources (e.g., a static .tool('foo')
648
- // colliding with a Skill's foo) which are real bugs.
649
- const seenNames = new Set();
650
- for (const entry of augmentedRegistry) {
651
- if (seenNames.has(entry.name)) {
652
- throw new Error(`Agent: duplicate tool name '${entry.name}'. Tool names must be unique ` +
653
- `across .tool() registrations and Skills' inject.tools (after deduping ` +
654
- `same-reference shares across skills). The LLM dispatches by name; ` +
655
- `collisions break tool routing.`);
656
- }
657
- seenNames.add(entry.name);
658
- }
659
- const registryByName = new Map(augmentedRegistry.map((e) => [e.name, e.tool]));
660
- // Block C runtime — autoActivate skill tools live OUTSIDE the LLM-
661
- // visible registry (so they don't pollute the per-iteration tool
662
- // list before the skill activates), but they MUST still be findable
663
- // by the dispatch handler — the LLM calls them by name once the
664
- // skill is active, and dispatch looks up by name. Add them to the
665
- // dispatch map so `lookupTool` resolves correctly. Using the Map
666
- // backing the static registryByName means autoActivate tools share
667
- // the same `.execute` wiring as normal tools — no special path.
668
- for (const [name, tool] of sharedSkillTools.entries()) {
669
- if (!registryByName.has(name)) {
670
- registryByName.set(name, tool);
671
- }
672
- }
673
- const toolSchemas = augmentedRegistry.map((e) => e.tool.schema);
518
+ return h;
519
+ },
520
+ getCurrentRunId: () => this.currentRunContext?.runId,
521
+ });
522
+ // Tool registry composition extracted to ./agent/buildToolRegistry.ts.
523
+ // Composes static .tool() registry + auto-attached read_skill +
524
+ // skill-supplied tools (with autoActivate scoping); validates
525
+ // name uniqueness; produces the dispatch map.
526
+ const { registryByName, toolSchemas } = buildToolRegistry(registry, this.injections);
527
+ // Late-bind toolSchemas into the seed stage's deps (the factory was
528
+ // built earlier with a getter; this resolves the actual value).
529
+ toolSchemasResolved = toolSchemas;
674
530
  const injectionEngineSubflow = buildInjectionEngineSubflow({
675
531
  injections: this.injections,
676
532
  });
@@ -683,1064 +539,52 @@ export class Agent extends RunnerBase {
683
539
  tools: toolSchemas,
684
540
  ...(this.externalToolProvider && { toolProvider: this.externalToolProvider }),
685
541
  });
686
- const iterationStart = (scope) => {
687
- typedEmit(scope, 'agentfootprint.agent.iteration_start', {
688
- turnIndex: 0,
689
- iterIndex: scope.iteration,
690
- });
691
- };
692
- const callLLM = async (scope) => {
693
- const systemPromptInjections = scope.systemPromptInjections ?? [];
694
- // `scope.messagesInjections` is read by ContextRecorder for
695
- // observability; the LLM-wire path now reads scope.history
696
- // directly (see below for rationale).
697
- const iteration = scope.iteration;
698
- const systemPrompt = systemPromptInjections
699
- .map((r) => r.rawContent ?? '')
700
- .filter((s) => s.length > 0)
701
- .join('\n\n');
702
- // Read the LLM message stream from `scope.history` directly.
703
- // The `messagesInjections` projection is for observability
704
- // (ContextRecorder, Lens) — it flattens InjectionRecords for
705
- // event reporting and doesn't carry the full LLM-protocol
706
- // shape (assistant `toolCalls[]`, etc.). For Anthropic's API
707
- // contract we need the original LLMMessage with `toolCalls`
708
- // intact so tool_use → tool_result correlation survives.
709
- const messages = scope.history ?? [];
710
- typedEmit(scope, 'agentfootprint.stream.llm_start', {
711
- iteration,
712
- provider: provider.name,
713
- model,
714
- systemPromptChars: systemPrompt.length,
715
- messagesCount: messages.length,
716
- toolsCount: toolSchemas.length,
717
- ...(temperature !== undefined && { temperature }),
718
- });
719
- const startMs = Date.now();
720
- // Use dynamic schemas — registry tools + injection-supplied
721
- // tools (Skills' `inject.tools` when their Injection is active).
722
- // Falls back to the static schemas at startup before the tools
723
- // slot has run for the first time.
724
- const activeToolSchemas = scope.dynamicToolSchemas ?? toolSchemas;
725
- const baseRequest = {
726
- ...(systemPrompt.length > 0 && { systemPrompt }),
727
- messages,
728
- ...(activeToolSchemas.length > 0 && { tools: activeToolSchemas }),
729
- model,
730
- ...(temperature !== undefined && { temperature }),
731
- ...(maxTokens !== undefined && { maxTokens }),
732
- };
733
- // v2.6+ — call cache strategy to attach provider-specific cache
734
- // hints. CacheGate has already routed (apply-markers / no-markers)
735
- // and populated scope.cacheMarkers accordingly. Strategy.prepareRequest
736
- // is a pass-through for empty markers.
737
- const cacheMarkers = scope.cacheMarkers ?? [];
738
- const cachePrepared = await cacheStrategy.prepareRequest(baseRequest, cacheMarkers, {
739
- iteration,
740
- iterationsRemaining: Math.max(0, maxIterations - iteration),
741
- recentHitRate: scope.recentHitRate,
742
- cachingDisabled: scope.cachingDisabled ?? false,
743
- });
744
- const llmRequest = cachePrepared.request;
745
- // Streaming-first: when the provider implements `stream()` we
746
- // consume chunk-by-chunk so consumers (Lens commentary, chat
747
- // UIs) see tokens as they arrive instead of waiting for the
748
- // full LLM call to finish. Each non-terminal chunk fires
749
- // `agentfootprint.stream.token` with the token text + index.
750
- //
751
- // The terminal chunk SHOULD carry the authoritative
752
- // `LLMResponse` (toolCalls + usage + stopReason); when it does
753
- // we use it directly. When it doesn't (older providers, partial
754
- // implementations) we fall back to `complete()` for the
755
- // authoritative payload — keeping the ReAct loop deterministic.
756
- let response;
757
- if (provider.stream) {
758
- for await (const chunk of provider.stream(llmRequest)) {
759
- if (chunk.done) {
760
- if (chunk.response)
761
- response = chunk.response;
762
- break;
763
- }
764
- if (chunk.content.length > 0) {
765
- typedEmit(scope, 'agentfootprint.stream.token', {
766
- iteration,
767
- tokenIndex: chunk.tokenIndex,
768
- content: chunk.content,
769
- });
770
- }
771
- }
772
- }
773
- if (!response) {
774
- // No `stream()` OR stream finished without a response payload.
775
- response = await provider.complete(llmRequest);
776
- }
777
- const durationMs = Date.now() - startMs;
778
- scope.totalInputTokens = scope.totalInputTokens + response.usage.input;
779
- scope.totalOutputTokens = scope.totalOutputTokens + response.usage.output;
780
- scope.llmLatestContent = response.content;
781
- scope.llmLatestToolCalls = response.toolCalls;
782
- typedEmit(scope, 'agentfootprint.stream.llm_end', {
783
- iteration,
784
- content: response.content,
785
- toolCallCount: response.toolCalls.length,
786
- usage: response.usage,
787
- stopReason: response.stopReason,
788
- durationMs,
789
- });
790
- emitCostTick(scope, pricingTable, costBudget, model, response.usage);
791
- };
792
- /** Decides the next branch: 'tool-calls' or 'final'. */
793
- const routeDecider = (scope) => {
794
- const toolCalls = scope.llmLatestToolCalls;
795
- const iteration = scope.iteration;
796
- const chosen = toolCalls.length > 0 && iteration < scope.maxIterations ? 'tool-calls' : 'final';
797
- typedEmit(scope, 'agentfootprint.agent.route_decided', {
798
- turnIndex: 0,
799
- iterIndex: iteration,
800
- chosen,
801
- rationale: chosen === 'tool-calls'
802
- ? `LLM requested ${toolCalls.length} tool call(s)`
803
- : iteration >= scope.maxIterations
804
- ? 'maxIterations reached — forcing final'
805
- : 'LLM produced no tool calls — final answer',
806
- });
807
- return chosen;
808
- };
809
- /**
810
- * Pausable tool-call handler.
811
- *
812
- * `execute` iterates the LLM-requested tool calls. If a tool throws
813
- * `PauseRequest` via `pauseHere()`, we save the remaining work into
814
- * scope and return the pause data — footprintjs captures a checkpoint
815
- * and bubbles it up. The outer `Agent.run()` surfaces it as a
816
- * `RunnerPauseOutcome`.
817
- *
818
- * `resume` is called when the consumer provides the human's answer.
819
- * We treat that answer as the paused tool's result and append it to
820
- * history, then continue the ReAct iteration loop.
821
- */
822
- const toolCallsHandler = {
823
- execute: async (scope) => {
824
- const toolCalls = scope.llmLatestToolCalls;
825
- const iteration = scope.iteration;
826
- const newHistory = [...scope.history];
827
- // ALWAYS push the assistant turn when there are tool calls — even
828
- // if the content was empty — so providers (Anthropic, OpenAI) can
829
- // round-trip the tool_use blocks via `LLMMessage.toolCalls`.
830
- // Without this, the next iteration's request lacks the assistant
831
- // turn that initiated the tool call, and the API rejects the
832
- // following tool_result with "preceding tool_use missing".
833
- if (scope.llmLatestContent || toolCalls.length > 0) {
834
- newHistory.push({
835
- role: 'assistant',
836
- content: scope.llmLatestContent ?? '',
837
- ...(toolCalls.length > 0 && { toolCalls }),
838
- });
839
- }
840
- // Resolve a tool by name, consulting the external ToolProvider
841
- // if one was wired via `.toolProvider()` and the static
842
- // registry doesn't carry the tool. The provider sees the same
843
- // ctx the Tools slot used, so dispatch + visibility stay
844
- // consistent within the iteration.
845
- const externalToolProvider = this.externalToolProvider;
846
- const lookupTool = (toolName) => {
847
- const fromRegistry = registryByName.get(toolName);
848
- if (fromRegistry)
849
- return fromRegistry;
850
- if (!externalToolProvider)
851
- return undefined;
852
- const activatedIds = scope.activatedInjectionIds ?? [];
853
- const identity = scope.runIdentity;
854
- const ctx = {
855
- iteration: scope.iteration,
856
- ...(activatedIds.length > 0 && {
857
- activeSkillId: activatedIds[activatedIds.length - 1],
858
- }),
859
- ...(identity && { identity }),
860
- };
861
- const visible = externalToolProvider.list(ctx);
862
- return visible.find((t) => t.schema.name === toolName);
863
- };
864
- for (const tc of toolCalls) {
865
- const tool = lookupTool(tc.name);
866
- typedEmit(scope, 'agentfootprint.stream.tool_start', {
867
- toolName: tc.name,
868
- toolCallId: tc.id,
869
- args: tc.args,
870
- ...(toolCalls.length > 1 && { parallelCount: toolCalls.length }),
871
- });
872
- const startMs = Date.now();
873
- let result;
874
- let error;
875
- // Permission gate — when a checker is configured, evaluate BEFORE
876
- // executing the tool. Emits `permission.check` with the decision.
877
- // On 'deny', the tool is not executed and its result is a
878
- // synthetic denial string; on 'allow'/'gate_open', execution
879
- // proceeds normally (the gate is informational — the consumer's
880
- // checker is responsible for any gate-open side effects).
881
- let denied = false;
882
- if (permissionChecker) {
883
- try {
884
- const decision = await permissionChecker.check({
885
- capability: 'tool_call',
886
- actor: 'agent',
887
- target: tc.name,
888
- context: tc.args,
889
- });
890
- typedEmit(scope, 'agentfootprint.permission.check', {
891
- capability: 'tool_call',
892
- actor: 'agent',
893
- target: tc.name,
894
- result: decision.result,
895
- ...(decision.policyRuleId !== undefined && { policyRuleId: decision.policyRuleId }),
896
- ...(decision.rationale !== undefined && { rationale: decision.rationale }),
897
- });
898
- if (decision.result === 'deny') {
899
- denied = true;
900
- result = `[permission denied: ${decision.rationale ?? 'policy'}]`;
901
- }
902
- }
903
- catch (permErr) {
904
- // A checker that throws is treated as deny-by-default. The
905
- // denial message records the thrown error so consumers can
906
- // debug policy-adapter failures without losing the run.
907
- denied = true;
908
- const msg = permErr instanceof Error ? permErr.message : String(permErr);
909
- typedEmit(scope, 'agentfootprint.permission.check', {
910
- capability: 'tool_call',
911
- actor: 'agent',
912
- target: tc.name,
913
- result: 'deny',
914
- rationale: `permission-checker threw: ${msg}`,
915
- });
916
- result = `[permission denied: checker error: ${msg}]`;
917
- }
918
- }
919
- if (!denied) {
920
- try {
921
- if (!tool)
922
- throw new Error(`Unknown tool: ${tc.name}`);
923
- result = await tool.execute(tc.args, {
924
- toolCallId: tc.id,
925
- iteration,
926
- });
927
- }
928
- catch (err) {
929
- if (isPauseRequest(err)) {
930
- // Commit partial state so resume() can find history intact.
931
- scope.history = newHistory;
932
- scope.pausedToolCallId = tc.id;
933
- scope.pausedToolName = tc.name;
934
- scope.pausedToolStartMs = startMs;
935
- // Returning a defined value triggers footprintjs pause —
936
- // the returned object becomes the checkpoint's pauseData.
937
- return {
938
- toolCallId: tc.id,
939
- toolName: tc.name,
940
- ...(typeof err.data === 'object' && err.data !== null
941
- ? err.data
942
- : { data: err.data }),
943
- };
944
- }
945
- error = true;
946
- result = err instanceof Error ? err.message : String(err);
947
- }
948
- }
949
- const durationMs = Date.now() - startMs;
950
- typedEmit(scope, 'agentfootprint.stream.tool_end', {
951
- toolCallId: tc.id,
952
- result,
953
- durationMs,
954
- ...(error === true && { error: true }),
955
- });
956
- const resultStr = typeof result === 'string' ? result : safeStringify(result);
957
- newHistory.push({
958
- role: 'tool',
959
- content: resultStr,
960
- toolCallId: tc.id,
961
- toolName: tc.name,
962
- });
963
- // ── Dynamic ReAct wiring ───────────────────────────────
964
- //
965
- // (1) `lastToolResult` drives `on-tool-return` Injection
966
- // triggers — the InjectionEngine's NEXT pass will see
967
- // this and activate any matching Instructions.
968
- scope.lastToolResult = { toolName: tc.name, result: resultStr };
969
- // (2) `read_skill` is the auto-attached activation tool.
970
- // When the LLM calls it with a valid Skill id, append
971
- // to `activatedInjectionIds` so the InjectionEngine's
972
- // NEXT pass activates that Skill (lifetime: turn — stays
973
- // active until the turn ends).
974
- if (tc.name === 'read_skill' && !error && !denied) {
975
- const requestedId = tc.args.id;
976
- if (typeof requestedId === 'string' && requestedId.length > 0) {
977
- const current = scope.activatedInjectionIds;
978
- if (!current.includes(requestedId)) {
979
- scope.activatedInjectionIds = [...current, requestedId];
980
- }
981
- }
982
- }
983
- }
984
- scope.history = newHistory;
985
- typedEmit(scope, 'agentfootprint.agent.iteration_end', {
986
- turnIndex: 0,
987
- iterIndex: iteration,
988
- toolCallCount: toolCalls.length,
989
- history: scope.history,
990
- });
991
- scope.iteration = iteration + 1;
992
- return undefined; // explicit: no pause, flow continues to loopTo
993
- },
994
- resume: (scope, input) => {
995
- // Consumer-supplied resume input becomes the paused tool's result.
996
- // The subflow's pre-pause scope is restored automatically by
997
- // footprintjs 4.17.0 via `checkpoint.subflowStates`, so
998
- // `scope.history` and `scope.pausedToolCallId` read back cleanly
999
- // across same-executor AND cross-executor resume.
1000
- const toolCallId = scope.pausedToolCallId;
1001
- const toolName = scope.pausedToolName;
1002
- const startMs = scope.pausedToolStartMs;
1003
- const resultStr = typeof input === 'string' ? input : safeStringify(input);
1004
- const newHistory = [
1005
- ...scope.history,
1006
- {
1007
- role: 'tool',
1008
- content: resultStr,
1009
- toolCallId,
1010
- toolName,
1011
- },
1012
- ];
1013
- scope.history = newHistory;
1014
- typedEmit(scope, 'agentfootprint.stream.tool_end', {
1015
- toolCallId,
1016
- result: input,
1017
- durationMs: Date.now() - startMs,
1018
- });
1019
- const iteration = scope.iteration;
1020
- typedEmit(scope, 'agentfootprint.agent.iteration_end', {
1021
- turnIndex: 0,
1022
- iterIndex: iteration,
1023
- toolCallCount: 1,
1024
- history: scope.history,
1025
- });
1026
- scope.iteration = iteration + 1;
1027
- // Clear pause checkpoint fields.
1028
- scope.pausedToolCallId = '';
1029
- scope.pausedToolName = '';
1030
- scope.pausedToolStartMs = 0;
1031
- },
1032
- };
1033
- // Final branch is split so memory-write subflows can mount BETWEEN
1034
- // setting `finalContent` and breaking the ReAct loop. PrepareFinal
1035
- // captures the turn payload; BreakFinal terminates the loop.
1036
- const prepareFinalStage = (scope) => {
1037
- const iteration = scope.iteration;
1038
- scope.finalContent = scope.llmLatestContent;
1039
- // The turn payload memory writes persist: the user's message
1040
- // paired with the agent's final answer.
1041
- scope.newMessages = [
1042
- { role: 'user', content: scope.userMessage },
1043
- { role: 'assistant', content: scope.finalContent },
1044
- ];
1045
- typedEmit(scope, 'agentfootprint.agent.iteration_end', {
1046
- turnIndex: 0,
1047
- iterIndex: iteration,
1048
- toolCallCount: 0,
1049
- });
1050
- typedEmit(scope, 'agentfootprint.agent.turn_end', {
1051
- turnIndex: 0,
1052
- finalContent: scope.finalContent,
1053
- totalInputTokens: scope.totalInputTokens,
1054
- totalOutputTokens: scope.totalOutputTokens,
1055
- iterationCount: iteration,
1056
- durationMs: Date.now() - scope.turnStartMs,
1057
- });
1058
- };
1059
- const breakFinalStage = (scope) => {
1060
- // $break terminates the flow before loopTo fires, ending the
1061
- // ReAct iteration once memory writes (if any) have persisted.
1062
- scope.$break();
1063
- return scope.finalContent;
1064
- };
1065
- // Compose the final branch as its own subflow so memory write
1066
- // subflows mount as visible siblings in narrative + Lens.
1067
- let finalBranchBuilder = flowChart('PrepareFinal', prepareFinalStage, 'prepare-final', undefined, 'Capture turn payload (finalContent + newMessages)');
1068
- for (const m of this.memories) {
1069
- if (m.write) {
1070
- finalBranchBuilder = mountMemoryWrite(finalBranchBuilder, {
1071
- pipeline: {
1072
- read: unwrapMemoryFlowChart(m.read),
1073
- write: unwrapMemoryFlowChart(m.write),
1074
- },
1075
- identityKey: 'runIdentity',
1076
- turnNumberKey: 'turnNumber',
1077
- contextTokensKey: 'contextTokensRemaining',
1078
- newMessagesKey: 'newMessages',
1079
- writeSubflowId: `sf-memory-write-${m.id}`,
1080
- });
1081
- }
1082
- }
1083
- const finalBranchChart = finalBranchBuilder
1084
- .addFunction('BreakFinal', breakFinalStage, 'break-final', 'Terminate the ReAct loop')
1085
- .build();
1086
- // Description prefix `Agent:` is a taxonomy marker — consumers
1087
- // (Lens + FlowchartRecorder) detect Agent-primitive subflows via
1088
- // this prefix and flag them as true agent boundaries (separate
1089
- // from LLMCall subflows which use `LLMCall:` prefix).
1090
- let builder = flowChart('Seed', seed, STAGE_IDS.SEED, undefined, 'Agent: ReAct loop');
1091
- // Memory READ subflows — mounted between Seed and InjectionEngine
1092
- // for TURN_START timing (default). Each memory writes to its own
1093
- // scope key (`memoryInjection_${id}`) so multiple `.memory()`
1094
- // registrations layer without colliding.
1095
- for (const m of this.memories) {
1096
- builder = mountMemoryRead(builder, {
1097
- pipeline: {
1098
- read: unwrapMemoryFlowChart(m.read),
1099
- ...(m.write !== undefined && { write: unwrapMemoryFlowChart(m.write) }),
1100
- },
1101
- identityKey: 'runIdentity',
1102
- turnNumberKey: 'turnNumber',
1103
- contextTokensKey: 'contextTokensRemaining',
1104
- injectionKey: memoryInjectionKey(m.id),
1105
- readSubflowId: `sf-memory-read-${m.id}`,
1106
- });
1107
- }
1108
- builder = builder
1109
- // Injection Engine — evaluates every Injection's trigger once
1110
- // per iteration; writes activeInjections[] to parent scope for
1111
- // the slot subflows to consume. Skipped if no injections were
1112
- // registered (no observable difference, just one more no-op
1113
- // subflow boundary).
1114
- .addSubFlowChartNext(SUBFLOW_IDS.INJECTION_ENGINE, injectionEngineSubflow, 'Injection Engine', {
1115
- inputMapper: (parent) => ({
1116
- iteration: parent.iteration,
1117
- userMessage: parent.userMessage,
1118
- history: parent.history,
1119
- lastToolResult: parent.lastToolResult,
1120
- activatedInjectionIds: parent.activatedInjectionIds ?? [],
1121
- }),
1122
- outputMapper: (sf) => ({ activeInjections: sf.activeInjections }),
1123
- // CRITICAL: footprintjs's default `applyOutputMapping`
1124
- // CONCATENATES arrays from subflow output with the parent's
1125
- // existing array values. Without `Replace`, the parent's
1126
- // `activeInjections` from iter N gets CONCATENATED with the
1127
- // subflow's iter N+1 fresh evaluation — producing
1128
- // 8 → 16 → 24 → 32 cumulative injections per turn instead of
1129
- // the intended ~8-per-iter.
1130
- //
1131
- // The slot subflows below (SystemPrompt, Messages, Tools) all
1132
- // read `activeInjections` and render every entry, so without
1133
- // Replace the system prompt grows linearly with iteration
1134
- // count. This was the root-cause of Dynamic-mode costing
1135
- // ~2x more input tokens than Classic in the v2.5.0 Neo
1136
- // benchmarks — the InjectionEngine's intended per-iter
1137
- // recomposition wasn't happening; it was per-iter ACCUMULATION.
1138
- arrayMerge: ArrayMergeMode.Replace,
1139
- })
1140
- .addSubFlowChartNext(SUBFLOW_IDS.SYSTEM_PROMPT, systemPromptSubflow, 'System Prompt', {
1141
- inputMapper: (parent) => ({
1142
- userMessage: parent.userMessage,
1143
- iteration: parent.iteration,
1144
- activeInjections: parent.activeInjections,
1145
- }),
1146
- outputMapper: (sf) => ({ systemPromptInjections: sf.systemPromptInjections }),
1147
- // See Tools-subflow comment below — same array-concat hazard.
1148
- // Without Replace, iter N+1's systemPromptInjections gets
1149
- // CONCATENATED with iter N's, multiplying the system prompt
1150
- // each iteration.
1151
- arrayMerge: ArrayMergeMode.Replace,
1152
- })
1153
- .addSubFlowChartNext(SUBFLOW_IDS.MESSAGES, messagesSubflow, 'Messages', {
1154
- inputMapper: (parent) => ({
1155
- messages: parent.history,
1156
- iteration: parent.iteration,
1157
- activeInjections: parent.activeInjections,
1158
- }),
1159
- outputMapper: (sf) => ({ messagesInjections: sf.messagesInjections }),
1160
- // Same array-concat hazard. messagesInjections is consumer-
1161
- // facing observability metadata (ContextRecorder, Lens) — must
1162
- // reflect THIS iteration's history, not be appended to last
1163
- // iteration's. CallLLM no longer reads this for the wire
1164
- // request (uses scope.history directly), so the LLM-protocol
1165
- // bug is fixed independently — but consumers of the
1166
- // messagesInjections stream still expect the per-iteration
1167
- // semantics.
1168
- arrayMerge: ArrayMergeMode.Replace,
1169
- })
1170
- .addSubFlowChartNext(SUBFLOW_IDS.TOOLS, toolsSubflow, 'Tools', {
1171
- inputMapper: (parent) => ({
1172
- iteration: parent.iteration,
1173
- activeInjections: parent.activeInjections,
1174
- // The slot subflow reads these to build the per-iteration
1175
- // ToolDispatchContext when an external `.toolProvider()` is
1176
- // configured. Without them the provider sees activeSkillId
1177
- // = undefined every iteration, breaking skillScopedTools etc.
1178
- activatedInjectionIds: parent.activatedInjectionIds,
1179
- runIdentity: parent.runIdentity,
1180
- }),
1181
- outputMapper: (sf) => ({
1182
- toolsInjections: sf.toolsInjections,
1183
- // Pass merged tool schemas (registry + injection-supplied)
1184
- // back up so callLLM uses the right list for THIS iteration.
1185
- dynamicToolSchemas: sf.toolSchemas,
1186
- }),
1187
- // CRITICAL: footprintjs's default `applyOutputMapping`
1188
- // CONCATENATES arrays from subflow output with the parent's
1189
- // existing array values. Without `Replace`, the parent's
1190
- // `dynamicToolSchemas` (carrying the iter N value) gets
1191
- // concatenated with the slot's iter N+1 deduped list,
1192
- // re-introducing duplicate tool names that Anthropic's API
1193
- // rejects with "tools: Tool names must be unique." The slot's
1194
- // toolSchemas IS the authoritative list — replace, don't
1195
- // concatenate.
1196
- arrayMerge: ArrayMergeMode.Replace,
1197
- })
1198
- // ── Cache layer (v2.6) ─────────────────────────────────────
1199
- // CacheDecision subflow walks `activeInjections` + evaluates
1200
- // each `cache:` directive, emits provider-agnostic
1201
- // `CacheMarker[]` to scope. Pure transform; no IO.
1202
- //
1203
- // CRITICAL: arrayMerge: ArrayMergeMode.Replace — same lesson
1204
- // as the v2.5.1 InjectionEngine fix. The default footprintjs
1205
- // behavior CONCATENATES arrays from child to parent;
1206
- // `cacheMarkers` MUST replace each iteration, not accumulate.
1207
- .addSubFlowChartNext(SUBFLOW_IDS.CACHE_DECISION, cacheDecisionSubflow, 'CacheDecision', {
1208
- inputMapper: (parent) => ({
1209
- activeInjections: parent.activeInjections ?? [],
1210
- iteration: parent.iteration ?? 1,
1211
- maxIterations: parent.maxIterations ?? maxIterations,
1212
- userMessage: parent.userMessage ?? '',
1213
- ...(parent.lastToolResult !== undefined && {
1214
- lastToolName: parent.lastToolResult?.toolName,
1215
- }),
1216
- cumulativeInputTokens: parent.totalInputTokens ?? 0,
1217
- systemPromptCachePolicy,
1218
- cachingDisabled: parent.cachingDisabled ?? false,
1219
- }),
1220
- outputMapper: (sf) => ({ cacheMarkers: sf.cacheMarkers }),
1221
- arrayMerge: ArrayMergeMode.Replace,
1222
- })
1223
- .addFunction('UpdateSkillHistory', updateSkillHistoryStage, STAGE_IDS.UPDATE_SKILL_HISTORY, 'Update skill-history rolling window for CacheGate churn detection')
1224
- .addDeciderFunction('CacheGate', cacheGateDecide, STAGE_IDS.CACHE_GATE, 'Gate cache-marker application: kill switch / hit-rate / skill-churn')
1225
- .addFunctionBranch(STAGE_IDS.APPLY_MARKERS, 'ApplyMarkers',
1226
- // Pass-through stage — markers stay in scope as-is.
1227
- // BuildLLMRequest (Phase 7+) reads them on the next stage.
1228
- () => undefined, 'Proceed with cache markers from CacheDecision')
1229
- .addFunctionBranch(STAGE_IDS.SKIP_CACHING, 'SkipCaching',
1230
- // Clear markers so BuildLLMRequest sees an empty list and
1231
- // makes the request unmodified.
1232
- (scope) => {
1233
- scope.cacheMarkers = [];
1234
- }, 'Skip caching this iteration')
1235
- .end()
1236
- .addFunction('IterationStart', iterationStart, 'iteration-start', 'Iteration begin marker')
1237
- .addFunction('CallLLM', callLLM, STAGE_IDS.CALL_LLM, 'LLM invocation')
1238
- .addDeciderFunction('Route', routeDecider, SUBFLOW_IDS.ROUTE, 'ReAct routing')
1239
- .addPausableFunctionBranch('tool-calls', 'ToolCalls', toolCallsHandler, 'Tool execution (pausable via pauseHere)')
1240
- .addSubFlowChartBranch('final', finalBranchChart, 'Final', {
1241
- // Pass through the read-only state the sub-chart needs;
1242
- // OMIT keys the sub-chart writes (finalContent, newMessages)
1243
- // — passing those via inputMapper would freeze them as args.
1244
- inputMapper: (parent) => {
1245
- const { finalContent: _f, newMessages: _nm, ...rest } = parent;
1246
- void _f;
1247
- void _nm;
1248
- return rest;
542
+ // iterationStart extracted to ./agent/stages/iterationStart.ts (v2.11.2).
543
+ const iterationStart = iterationStartStage;
544
+ // callLLM extracted to ./agent/stages/callLLM.ts (v2.11.2). Same
545
+ // late-binding pattern as seed for toolSchemas (computed below).
546
+ const callLLM = buildCallLLMStage({
547
+ provider,
548
+ model,
549
+ ...(temperature !== undefined && { temperature }),
550
+ ...(maxTokens !== undefined && { maxTokens }),
551
+ ...(pricingTable !== undefined && { pricingTable }),
552
+ ...(costBudget !== undefined && { costBudget }),
553
+ maxIterations,
554
+ cacheStrategy,
555
+ get toolSchemas() {
556
+ return toolSchemasResolved;
1249
557
  },
1250
- outputMapper: (sf) => ({
1251
- finalContent: sf.finalContent,
1252
- }),
1253
- // BreakFinal's $break() must reach the outer loopTo so the
1254
- // ReAct iteration terminates; without this the inner break
1255
- // only exits the sub-chart and the outer loop continues.
1256
- propagateBreak: true,
1257
- })
1258
- .setDefault('final')
1259
- .end()
1260
- // Dynamic ReAct: loop back to the InjectionEngine so EVERY iteration
1261
- // re-evaluates triggers (rule predicates, on-tool-return, llm-activated)
1262
- // against the freshest context (the just-appended tool result).
1263
- // Without this, the InjectionEngine runs ONCE per turn and:
1264
- // - on-tool-return predicates never fire on iter 2+
1265
- // - read_skill('X') activations are never picked up next iteration
1266
- // - autoActivate per-skill tool gating is structurally impossible
1267
- // - tools / system-prompt slots stay frozen at iter 1 content
1268
- // The v2.4 default of loopTo(MESSAGES) bypassed all four — quietly
1269
- // breaking the framework's "Dynamic ReAct" claim. v2.5 restores the
1270
- // v1 behavior that documents promise.
1271
- .loopTo(SUBFLOW_IDS.INJECTION_ENGINE);
1272
- return builder.build();
1273
- }
1274
- }
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
- export 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 ?? 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(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
- 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: { ...defaultCommentaryTemplates, ...this.commentaryOverrides },
1730
- thinkingTemplates: { ...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;
558
+ });
559
+ // routeDecider extracted to ./agent/stages/route.ts (v2.11.2).
560
+ const routeDecider = routeDeciderStage;
561
+ // toolCallsHandler extracted to ./agent/stages/toolCalls.ts (v2.11.2).
562
+ const toolCallsHandler = buildToolCallsHandler({
563
+ registryByName,
564
+ ...(this.externalToolProvider && { externalToolProvider: this.externalToolProvider }),
565
+ ...(permissionChecker && { permissionChecker }),
566
+ });
567
+ // Chart composition extracted to ./agent/buildAgentChart.ts (v2.11.2).
568
+ return buildAgentChart({
569
+ memories: this.memories,
570
+ systemPromptCachePolicy,
571
+ maxIterations,
572
+ seed,
573
+ iterationStart,
574
+ callLLM,
575
+ routeDecider,
576
+ toolCallsHandler,
577
+ injectionEngineSubflow,
578
+ systemPromptSubflow,
579
+ messagesSubflow,
580
+ toolsSubflow,
581
+ cacheDecisionSubflow,
582
+ updateSkillHistoryStage,
583
+ cacheGateDecide,
584
+ });
1743
585
  }
1744
586
  }
587
+ // AgentBuilder extracted to ./agent/AgentBuilder.ts (v2.11.2).
588
+ // Re-export so the 28+ existing import sites continue to work unchanged.
1745
589
  // Validators + helpers extracted to ./agent/validators.ts (v2.11.1).
1746
590
  //# sourceMappingURL=Agent.js.map