@illuma-ai/agents 1.5.1 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -62
- package/dist/cjs/agents/AgentContext.cjs +160 -259
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +12 -12
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +30 -13
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/graphs/phases/memoryFlushPhase.cjs +1 -1
- package/dist/cjs/graphs/phases/memoryFlushPhase.cjs.map +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
- package/dist/cjs/hooks/matchers.cjs +2 -2
- package/dist/cjs/hooks/matchers.cjs.map +1 -1
- package/dist/cjs/hooks/types.cjs +1 -1
- package/dist/cjs/hooks/types.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +1 -5
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/bedrock/index.cjs +33 -61
- package/dist/cjs/llm/bedrock/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +1 -1
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs +10 -27
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +3 -84
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/memory/citations.cjs +4 -4
- package/dist/cjs/memory/citations.cjs.map +1 -1
- package/dist/cjs/memory/constants.cjs +17 -17
- package/dist/cjs/memory/constants.cjs.map +1 -1
- package/dist/cjs/memory/mmr.cjs +1 -1
- package/dist/cjs/memory/mmr.cjs.map +1 -1
- package/dist/cjs/memory/paths.cjs +1 -1
- package/dist/cjs/memory/paths.cjs.map +1 -1
- package/dist/cjs/memory/recallTracking.cjs +3 -3
- package/dist/cjs/memory/recallTracking.cjs.map +1 -1
- package/dist/cjs/memory/temporalDecay.cjs +2 -2
- package/dist/cjs/memory/temporalDecay.cjs.map +1 -1
- package/dist/cjs/messages/cache.cjs +0 -89
- package/dist/cjs/messages/cache.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +13 -71
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +11 -21
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -1
- package/dist/cjs/tools/CodeExecutor.cjs +13 -41
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +11 -16
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +78 -13
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/memory/memoryAppendTool.cjs +1 -1
- package/dist/cjs/tools/memory/memoryAppendTool.cjs.map +1 -1
- package/dist/cjs/tools/memory/memoryGetTool.cjs +2 -2
- package/dist/cjs/tools/memory/memoryGetTool.cjs.map +1 -1
- package/dist/cjs/tools/memory/memorySearchTool.cjs +3 -3
- package/dist/cjs/tools/memory/memorySearchTool.cjs.map +1 -1
- package/dist/cjs/tools/memory/shared.cjs +1 -1
- package/dist/cjs/tools/memory/shared.cjs.map +1 -1
- package/dist/cjs/tools/search/search.cjs +3 -11
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +4 -28
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/search/utils.cjs +3 -10
- package/dist/cjs/tools/search/utils.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +48 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/types/graph.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +160 -259
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +12 -12
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +30 -13
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/graphs/phases/memoryFlushPhase.mjs +1 -1
- package/dist/esm/graphs/phases/memoryFlushPhase.mjs.map +1 -1
- package/dist/esm/hooks/HookRegistry.mjs +1 -1
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
- package/dist/esm/hooks/matchers.mjs +2 -2
- package/dist/esm/hooks/matchers.mjs.map +1 -1
- package/dist/esm/hooks/types.mjs +1 -1
- package/dist/esm/hooks/types.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +1 -5
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/bedrock/index.mjs +34 -61
- package/dist/esm/llm/bedrock/index.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +1 -1
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs +10 -27
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/main.mjs +1 -5
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/memory/citations.mjs +4 -4
- package/dist/esm/memory/citations.mjs.map +1 -1
- package/dist/esm/memory/constants.mjs +17 -17
- package/dist/esm/memory/constants.mjs.map +1 -1
- package/dist/esm/memory/mmr.mjs +1 -1
- package/dist/esm/memory/mmr.mjs.map +1 -1
- package/dist/esm/memory/paths.mjs +1 -1
- package/dist/esm/memory/paths.mjs.map +1 -1
- package/dist/esm/memory/recallTracking.mjs +3 -3
- package/dist/esm/memory/recallTracking.mjs.map +1 -1
- package/dist/esm/memory/temporalDecay.mjs +2 -2
- package/dist/esm/memory/temporalDecay.mjs.map +1 -1
- package/dist/esm/messages/cache.mjs +0 -89
- package/dist/esm/messages/cache.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +13 -71
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +12 -22
- package/dist/esm/tools/BashExecutor.mjs.map +1 -1
- package/dist/esm/tools/CodeExecutor.mjs +14 -41
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +12 -17
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +78 -13
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/memory/memoryAppendTool.mjs +1 -1
- package/dist/esm/tools/memory/memoryAppendTool.mjs.map +1 -1
- package/dist/esm/tools/memory/memoryGetTool.mjs +2 -2
- package/dist/esm/tools/memory/memoryGetTool.mjs.map +1 -1
- package/dist/esm/tools/memory/memorySearchTool.mjs +3 -3
- package/dist/esm/tools/memory/memorySearchTool.mjs.map +1 -1
- package/dist/esm/tools/memory/shared.mjs +1 -1
- package/dist/esm/tools/memory/shared.mjs.map +1 -1
- package/dist/esm/tools/search/search.mjs +3 -11
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +4 -28
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/search/utils.mjs +3 -10
- package/dist/esm/tools/search/utils.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +48 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/types/graph.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +25 -95
- package/dist/types/common/enum.d.ts +12 -12
- package/dist/types/graphs/Graph.d.ts +2 -2
- package/dist/types/graphs/phases/memoryFlushPhase.d.ts +2 -2
- package/dist/types/hooks/HookRegistry.d.ts +1 -1
- package/dist/types/hooks/matchers.d.ts +2 -2
- package/dist/types/hooks/types.d.ts +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/llm/bedrock/index.d.ts +1 -54
- package/dist/types/llm/openai/index.d.ts +1 -1
- package/dist/types/memory/citations.d.ts +4 -4
- package/dist/types/memory/constants.d.ts +17 -17
- package/dist/types/memory/mmr.d.ts +3 -3
- package/dist/types/memory/paths.d.ts +1 -1
- package/dist/types/memory/temporalDecay.d.ts +2 -2
- package/dist/types/memory/types.d.ts +3 -3
- package/dist/types/messages/format.d.ts +2 -5
- package/dist/types/tools/CodeExecutor.d.ts +0 -6
- package/dist/types/tools/ToolNode.d.ts +3 -3
- package/dist/types/tools/memory/shared.d.ts +1 -1
- package/dist/types/tools/search/test.d.ts +1 -0
- package/dist/types/tools/search/types.d.ts +5 -99
- package/dist/types/tools/search/utils.d.ts +2 -2
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +29 -0
- package/dist/types/types/graph.d.ts +30 -34
- package/dist/types/types/index.d.ts +0 -1
- package/dist/types/types/messages.d.ts +1 -1
- package/dist/types/types/run.d.ts +1 -3
- package/dist/types/types/tools.d.ts +5 -14
- package/package.json +1 -61
- package/src/agents/AgentContext.test.ts +176 -0
- package/src/agents/AgentContext.ts +179 -305
- package/src/agents/__tests__/AgentContext.test.ts +0 -632
- package/src/common/__tests__/enum.test.ts +1 -1
- package/src/common/enum.ts +12 -12
- package/src/graphs/Graph.ts +32 -13
- package/src/graphs/MultiAgentGraph.ts +1 -1
- package/src/graphs/gapFeatures.test.ts +1 -1
- package/src/graphs/phases/__tests__/memoryFlushPhase.test.ts +1 -1
- package/src/graphs/phases/memoryFlushPhase.ts +2 -2
- package/src/hooks/HookRegistry.ts +1 -1
- package/src/hooks/index.ts +1 -1
- package/src/hooks/matchers.ts +2 -2
- package/src/hooks/types.ts +1 -1
- package/src/index.ts +0 -6
- package/src/llm/anthropic/utils/message_inputs.ts +1 -10
- package/src/llm/bedrock/__tests__/bedrock-caching.test.ts +18 -166
- package/src/llm/bedrock/index.ts +41 -116
- package/src/llm/openai/index.ts +2 -2
- package/src/llm/openai/utils/index.ts +14 -31
- package/src/memory/citations.ts +4 -4
- package/src/memory/constants.ts +17 -17
- package/src/memory/mmr.ts +3 -3
- package/src/memory/paths.ts +1 -1
- package/src/memory/recallTracking.ts +3 -3
- package/src/memory/temporalDecay.ts +2 -2
- package/src/memory/types.ts +3 -3
- package/src/messages/cache.test.ts +24 -62
- package/src/messages/cache.ts +0 -112
- package/src/messages/ensureThinkingBlock.test.ts +1 -1
- package/src/messages/format.ts +13 -92
- package/src/messages/formatAgentMessages.test.ts +1 -1
- package/src/scripts/subagent-configurable-inheritance.ts +263 -0
- package/src/scripts/subagent-event-driven-debug.ts +2 -2
- package/src/specs/anthropic.simple.test.ts +0 -61
- package/src/specs/prune.orphans.test.ts +1 -1
- package/src/tools/BashExecutor.ts +13 -37
- package/src/tools/CodeExecutor.ts +14 -59
- package/src/tools/ProgrammaticToolCalling.ts +14 -29
- package/src/tools/ToolNode.ts +75 -14
- package/src/tools/__tests__/CodeExecutor.test.ts +3 -3
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -60
- package/src/tools/__tests__/SubagentExecutor.test.ts +157 -0
- package/src/tools/memory/memoryAppendTool.ts +1 -1
- package/src/tools/memory/memoryGetTool.ts +2 -2
- package/src/tools/memory/memorySearchTool.ts +3 -3
- package/src/tools/memory/shared.ts +1 -1
- package/src/tools/search/output.md +2775 -0
- package/src/tools/search/search.ts +2 -12
- package/src/tools/search/test.html +884 -0
- package/src/tools/search/test.md +643 -0
- package/src/tools/search/test.ts +159 -0
- package/src/tools/search/tool.ts +2 -36
- package/src/tools/search/types.ts +8 -133
- package/src/tools/search/utils.ts +5 -13
- package/src/tools/subagent/SubagentExecutor.ts +78 -0
- package/src/types/graph.ts +27 -34
- package/src/types/index.ts +0 -1
- package/src/types/messages.ts +1 -1
- package/src/types/run.ts +1 -3
- package/src/types/tools.ts +5 -14
- package/dist/cjs/langchain/google-common.cjs +0 -3
- package/dist/cjs/langchain/google-common.cjs.map +0 -1
- package/dist/cjs/langchain/index.cjs +0 -86
- package/dist/cjs/langchain/index.cjs.map +0 -1
- package/dist/cjs/langchain/language_models/chat_models.cjs +0 -3
- package/dist/cjs/langchain/language_models/chat_models.cjs.map +0 -1
- package/dist/cjs/langchain/messages/tool.cjs +0 -3
- package/dist/cjs/langchain/messages/tool.cjs.map +0 -1
- package/dist/cjs/langchain/messages.cjs +0 -51
- package/dist/cjs/langchain/messages.cjs.map +0 -1
- package/dist/cjs/langchain/openai.cjs +0 -3
- package/dist/cjs/langchain/openai.cjs.map +0 -1
- package/dist/cjs/langchain/prompts.cjs +0 -11
- package/dist/cjs/langchain/prompts.cjs.map +0 -1
- package/dist/cjs/langchain/runnables.cjs +0 -19
- package/dist/cjs/langchain/runnables.cjs.map +0 -1
- package/dist/cjs/langchain/tools.cjs +0 -23
- package/dist/cjs/langchain/tools.cjs.map +0 -1
- package/dist/cjs/langchain/utils/env.cjs +0 -11
- package/dist/cjs/langchain/utils/env.cjs.map +0 -1
- package/dist/cjs/llm/bedrock/cacheSupport.cjs +0 -55
- package/dist/cjs/llm/bedrock/cacheSupport.cjs.map +0 -1
- package/dist/cjs/tools/search/tavily-scraper.cjs +0 -189
- package/dist/cjs/tools/search/tavily-scraper.cjs.map +0 -1
- package/dist/cjs/tools/search/tavily-search.cjs +0 -372
- package/dist/cjs/tools/search/tavily-search.cjs.map +0 -1
- package/dist/cjs/types/agent-cache.cjs +0 -54
- package/dist/cjs/types/agent-cache.cjs.map +0 -1
- package/dist/esm/langchain/google-common.mjs +0 -2
- package/dist/esm/langchain/google-common.mjs.map +0 -1
- package/dist/esm/langchain/index.mjs +0 -5
- package/dist/esm/langchain/index.mjs.map +0 -1
- package/dist/esm/langchain/language_models/chat_models.mjs +0 -2
- package/dist/esm/langchain/language_models/chat_models.mjs.map +0 -1
- package/dist/esm/langchain/messages/tool.mjs +0 -2
- package/dist/esm/langchain/messages/tool.mjs.map +0 -1
- package/dist/esm/langchain/messages.mjs +0 -2
- package/dist/esm/langchain/messages.mjs.map +0 -1
- package/dist/esm/langchain/openai.mjs +0 -2
- package/dist/esm/langchain/openai.mjs.map +0 -1
- package/dist/esm/langchain/prompts.mjs +0 -2
- package/dist/esm/langchain/prompts.mjs.map +0 -1
- package/dist/esm/langchain/runnables.mjs +0 -2
- package/dist/esm/langchain/runnables.mjs.map +0 -1
- package/dist/esm/langchain/tools.mjs +0 -2
- package/dist/esm/langchain/tools.mjs.map +0 -1
- package/dist/esm/langchain/utils/env.mjs +0 -2
- package/dist/esm/langchain/utils/env.mjs.map +0 -1
- package/dist/esm/llm/bedrock/cacheSupport.mjs +0 -52
- package/dist/esm/llm/bedrock/cacheSupport.mjs.map +0 -1
- package/dist/esm/tools/search/tavily-scraper.mjs +0 -186
- package/dist/esm/tools/search/tavily-scraper.mjs.map +0 -1
- package/dist/esm/tools/search/tavily-search.mjs +0 -370
- package/dist/esm/tools/search/tavily-search.mjs.map +0 -1
- package/dist/esm/types/agent-cache.mjs +0 -52
- package/dist/esm/types/agent-cache.mjs.map +0 -1
- package/dist/types/langchain/google-common.d.ts +0 -1
- package/dist/types/langchain/index.d.ts +0 -8
- package/dist/types/langchain/language_models/chat_models.d.ts +0 -1
- package/dist/types/langchain/messages/tool.d.ts +0 -1
- package/dist/types/langchain/messages.d.ts +0 -2
- package/dist/types/langchain/openai.d.ts +0 -1
- package/dist/types/langchain/prompts.d.ts +0 -1
- package/dist/types/langchain/runnables.d.ts +0 -2
- package/dist/types/langchain/tools.d.ts +0 -2
- package/dist/types/langchain/utils/env.d.ts +0 -1
- package/dist/types/llm/bedrock/cacheSupport.d.ts +0 -35
- package/dist/types/tools/search/tavily-scraper.d.ts +0 -19
- package/dist/types/tools/search/tavily-search.d.ts +0 -4
- package/dist/types/tools/subagent/types.d.ts +0 -84
- package/dist/types/types/agent-cache.d.ts +0 -71
- package/src/agents/__tests__/AgentContext.cacheTtl.live.test.ts +0 -259
- package/src/agents/__tests__/AgentContext.crossAgentTier1.live.test.ts +0 -266
- package/src/agents/__tests__/AgentContext.crossUserCache.live.test.ts +0 -342
- package/src/langchain/google-common.ts +0 -1
- package/src/langchain/index.ts +0 -8
- package/src/langchain/language_models/chat_models.ts +0 -1
- package/src/langchain/messages/tool.ts +0 -5
- package/src/langchain/messages.ts +0 -21
- package/src/langchain/openai.ts +0 -1
- package/src/langchain/prompts.ts +0 -1
- package/src/langchain/runnables.ts +0 -7
- package/src/langchain/tools.ts +0 -8
- package/src/langchain/utils/env.ts +0 -1
- package/src/llm/anthropic/utils/server-tool-inputs.test.ts +0 -436
- package/src/llm/bedrock/cacheSupport.test.ts +0 -99
- package/src/llm/bedrock/cacheSupport.ts +0 -53
- package/src/tools/search/tavily-scraper.ts +0 -235
- package/src/tools/search/tavily-search.ts +0 -424
- package/src/tools/search/tavily.test.ts +0 -965
- package/src/tools/subagent/types.test.ts +0 -70
- package/src/tools/subagent/types.ts +0 -115
- package/src/types/agent-cache.ts +0 -74
package/README.md
CHANGED
|
@@ -398,68 +398,6 @@ Caching is **automatic** for Anthropic and Bedrock providers:
|
|
|
398
398
|
}
|
|
399
399
|
```
|
|
400
400
|
|
|
401
|
-
### Multi-block system caching (cross-tenant prefix sharing)
|
|
402
|
-
|
|
403
|
-
For multi-agent platforms where a stable prefix is shared across many
|
|
404
|
-
agents, use `system_cache_blocks` to emit additional cachePoint markers.
|
|
405
|
-
A cacheable block placed BEFORE `instructions` is cached at provider
|
|
406
|
-
level (Anthropic workspace, Bedrock account) and shared across every
|
|
407
|
-
agent that emits the same byte-identical prefix:
|
|
408
|
-
|
|
409
|
-
```typescript
|
|
410
|
-
{
|
|
411
|
-
system_cache_blocks: [
|
|
412
|
-
{ text: PLATFORM_WIDE_RULES, ttl: '5m' }, // shared across all agents
|
|
413
|
-
],
|
|
414
|
-
instructions: agentSpecificInstructions, // per-agent stable
|
|
415
|
-
additional_instructions: perRequestContext, // never cached
|
|
416
|
-
}
|
|
417
|
-
```
|
|
418
|
-
|
|
419
|
-
This produces (Bedrock):
|
|
420
|
-
|
|
421
|
-
```
|
|
422
|
-
[PLATFORM_WIDE_RULES]
|
|
423
|
-
[cachePoint: 5m] ← cross-agent share point
|
|
424
|
-
[agentSpecificInstructions]
|
|
425
|
-
[cachePoint: 5m] ← per-agent cache
|
|
426
|
-
[perRequestContext] ← uncached
|
|
427
|
-
```
|
|
428
|
-
|
|
429
|
-
Capped at `MAX_SYSTEM_CACHE_BLOCKS` (2 entries) to stay within Bedrock's
|
|
430
|
-
4-cachePoint per-request budget. See
|
|
431
|
-
[`docs/prompt-cache-architecture.md`](docs/prompt-cache-architecture.md)
|
|
432
|
-
for the full architecture.
|
|
433
|
-
|
|
434
|
-
### Tool array cachePoint strategies (Bedrock)
|
|
435
|
-
|
|
436
|
-
```typescript
|
|
437
|
-
new IllumaBedrockConverse({
|
|
438
|
-
promptCache: true,
|
|
439
|
-
toolCacheStrategy: 'split', // 'single' (default) | 'split' | 'none'
|
|
440
|
-
toolGroupSelector: (toolName) =>
|
|
441
|
-
toolName.includes('_mcp_') ? 'mcp' : 'core',
|
|
442
|
-
});
|
|
443
|
-
```
|
|
444
|
-
|
|
445
|
-
- `'single'`: one cachePoint at the end of the tools array (default)
|
|
446
|
-
- `'split'`: separate cachePoints per group (use when groups have
|
|
447
|
-
different volatility)
|
|
448
|
-
- `'none'`: no cachePoint on tools
|
|
449
|
-
|
|
450
|
-
### Extending the Bedrock model allowlist
|
|
451
|
-
|
|
452
|
-
```typescript
|
|
453
|
-
new IllumaBedrockConverse({
|
|
454
|
-
model: 'mycorp.future-model-v1:0',
|
|
455
|
-
promptCache: true,
|
|
456
|
-
bedrockCacheModelPatterns: [/^mycorp\.future-/i],
|
|
457
|
-
});
|
|
458
|
-
```
|
|
459
|
-
|
|
460
|
-
Useful when AWS adds a new cache-supporting model family before the
|
|
461
|
-
library's allowlist updates.
|
|
462
|
-
|
|
463
401
|
---
|
|
464
402
|
|
|
465
403
|
## Structured Output Modes
|
|
@@ -5,8 +5,6 @@ var runnables = require('@langchain/core/runnables');
|
|
|
5
5
|
var schema$1 = require('../tools/schema.cjs');
|
|
6
6
|
var _enum = require('../common/enum.cjs');
|
|
7
7
|
require('../tools/approval/constants.cjs');
|
|
8
|
-
var agentCache = require('../types/agent-cache.cjs');
|
|
9
|
-
var cacheSupport = require('../llm/bedrock/cacheSupport.cjs');
|
|
10
8
|
var schema = require('../utils/schema.cjs');
|
|
11
9
|
|
|
12
10
|
/* eslint-disable no-console */
|
|
@@ -19,7 +17,7 @@ class AgentContext {
|
|
|
19
17
|
* Create an AgentContext from configuration with token accounting initialization
|
|
20
18
|
*/
|
|
21
19
|
static fromConfig(agentConfig, tokenCounter, indexTokenCountMap) {
|
|
22
|
-
const { agentId, name, description, provider, clientOptions, tools, toolMap, toolEnd, toolRegistry, toolDefinitions,
|
|
20
|
+
const { agentId, name, description, provider, clientOptions, tools, toolMap, toolEnd, toolRegistry, toolDefinitions, instructions, additional_instructions, streamBuffer, maxContextTokens, reasoningKey, useLegacyContent, dynamicContext, structuredOutput: structuredOutputCamel, structured_output: structuredOutputSnake, discoveredTools, summarizeCallback, persistedSummary, summarizationConfig, fileManifest, system_cache_blocks, instructions_cache_ttl, } = agentConfig;
|
|
23
21
|
// Normalize structured output: support both camelCase and snake_case inputs
|
|
24
22
|
// Priority: structuredOutput (camelCase) > structured_output (snake_case with enabled check)
|
|
25
23
|
let structuredOutput;
|
|
@@ -49,9 +47,7 @@ class AgentContext {
|
|
|
49
47
|
toolMap,
|
|
50
48
|
toolRegistry,
|
|
51
49
|
toolDefinitions,
|
|
52
|
-
systemCacheBlocks: system_cache_blocks,
|
|
53
50
|
instructions,
|
|
54
|
-
instructionsCacheTtl: instructions_cache_ttl,
|
|
55
51
|
additionalInstructions: additional_instructions,
|
|
56
52
|
reasoningKey,
|
|
57
53
|
toolEnd,
|
|
@@ -65,9 +61,11 @@ class AgentContext {
|
|
|
65
61
|
persistedSummary,
|
|
66
62
|
summarizationConfig,
|
|
67
63
|
fileManifest,
|
|
64
|
+
systemCacheBlocks: system_cache_blocks,
|
|
65
|
+
instructionsCacheTtl: instructions_cache_ttl,
|
|
68
66
|
});
|
|
69
67
|
/**
|
|
70
|
-
* Track subagent inputs on the context. `_sourceInputs`
|
|
68
|
+
* Track upstream-aligned subagent inputs on the context. `_sourceInputs`
|
|
71
69
|
* preserves the original AgentInputs so SubagentExecutor can self-spawn
|
|
72
70
|
* (`SubagentConfig.self === true`) without separate config; the other
|
|
73
71
|
* two flow straight from agentConfig.
|
|
@@ -145,18 +143,9 @@ class AgentContext {
|
|
|
145
143
|
toolDefinitions;
|
|
146
144
|
/** Set of tool names discovered via tool search (to be loaded) */
|
|
147
145
|
discoveredToolNames = new Set();
|
|
148
|
-
/**
|
|
149
|
-
* Cacheable system content blocks emitted before `instructions`. Each
|
|
150
|
-
* gets its own cache marker (cachePoint on Bedrock, cache_control on
|
|
151
|
-
* Anthropic). Earlier entries = wider cache key (best for cross-tenant
|
|
152
|
-
* sharing). See `src/types/agent-cache.ts`.
|
|
153
|
-
*/
|
|
154
|
-
systemCacheBlocks;
|
|
155
|
-
/** Stable/cacheable instructions for this agent (gets trailing marker). */
|
|
146
|
+
/** Instructions for this agent */
|
|
156
147
|
instructions;
|
|
157
|
-
/**
|
|
158
|
-
instructionsCacheTtl;
|
|
159
|
-
/** Dynamic system tail (per-user / per-message — never cached). */
|
|
148
|
+
/** Additional instructions for this agent */
|
|
160
149
|
additionalInstructions;
|
|
161
150
|
/**
|
|
162
151
|
* Dynamic context that changes per-request (e.g., current time, user info).
|
|
@@ -218,13 +207,22 @@ class AgentContext {
|
|
|
218
207
|
summarizationConfig;
|
|
219
208
|
/** Lightweight file manifest for file-aware compaction (IDs and names only, no content) */
|
|
220
209
|
fileManifest;
|
|
210
|
+
/**
|
|
211
|
+
* Workspace-shared system-message tiers. When set, each entry becomes a
|
|
212
|
+
* separate text block in the SystemMessage with its own cachePoint /
|
|
213
|
+
* cache_control marker, BEFORE the per-agent `instructions` block.
|
|
214
|
+
* See {@link t.AgentInputs.system_cache_blocks} for full semantics.
|
|
215
|
+
*/
|
|
216
|
+
systemCacheBlocks;
|
|
217
|
+
/** TTL hint for the per-agent instructions cache marker. Defaults to '5m'. */
|
|
218
|
+
instructionsCacheTtl;
|
|
221
219
|
/** Original AgentInputs used to create this context — used for self-spawn subagent resolution. */
|
|
222
220
|
_sourceInputs;
|
|
223
221
|
/** Subagent configurations for hierarchical delegation. */
|
|
224
222
|
subagentConfigs;
|
|
225
223
|
/** Maximum subagent nesting depth. */
|
|
226
224
|
maxSubagentDepth;
|
|
227
|
-
constructor({ agentId, name, description, provider, clientOptions, maxContextTokens, streamBuffer, tokenCounter, tools, toolMap, toolRegistry, toolDefinitions,
|
|
225
|
+
constructor({ agentId, name, description, provider, clientOptions, maxContextTokens, streamBuffer, tokenCounter, tools, toolMap, toolRegistry, toolDefinitions, instructions, additionalInstructions, dynamicContext, reasoningKey, toolEnd, instructionTokens, useLegacyContent, structuredOutput, discoveredTools, summarizeCallback, persistedSummary, summarizationConfig, fileManifest, systemCacheBlocks, instructionsCacheTtl, }) {
|
|
228
226
|
this.agentId = agentId;
|
|
229
227
|
this.name = name;
|
|
230
228
|
this.description = description;
|
|
@@ -237,16 +235,7 @@ class AgentContext {
|
|
|
237
235
|
this.toolMap = toolMap;
|
|
238
236
|
this.toolRegistry = toolRegistry;
|
|
239
237
|
this.toolDefinitions = toolDefinitions;
|
|
240
|
-
if (systemCacheBlocks &&
|
|
241
|
-
systemCacheBlocks.length > agentCache.MAX_SYSTEM_CACHE_BLOCKS) {
|
|
242
|
-
throw new Error(`system_cache_blocks supports at most ${agentCache.MAX_SYSTEM_CACHE_BLOCKS} entries ` +
|
|
243
|
-
`(received ${systemCacheBlocks.length}); excess entries would exceed ` +
|
|
244
|
-
`Bedrock's 4-cachePoint per-request budget once the tools array and ` +
|
|
245
|
-
`trailing 'instructions' marker are counted. See src/types/agent-cache.ts.`);
|
|
246
|
-
}
|
|
247
|
-
this.systemCacheBlocks = systemCacheBlocks;
|
|
248
238
|
this.instructions = instructions;
|
|
249
|
-
this.instructionsCacheTtl = instructionsCacheTtl;
|
|
250
239
|
this.additionalInstructions = additionalInstructions;
|
|
251
240
|
this.dynamicContext = dynamicContext;
|
|
252
241
|
this.structuredOutput = structuredOutput;
|
|
@@ -254,6 +243,12 @@ class AgentContext {
|
|
|
254
243
|
this.persistedSummary = persistedSummary;
|
|
255
244
|
this.summarizationConfig = summarizationConfig;
|
|
256
245
|
this.fileManifest = fileManifest;
|
|
246
|
+
if (systemCacheBlocks && systemCacheBlocks.length > 0) {
|
|
247
|
+
this.systemCacheBlocks = systemCacheBlocks;
|
|
248
|
+
}
|
|
249
|
+
if (instructionsCacheTtl) {
|
|
250
|
+
this.instructionsCacheTtl = instructionsCacheTtl;
|
|
251
|
+
}
|
|
257
252
|
if (reasoningKey) {
|
|
258
253
|
this.reasoningKey = reasoningKey;
|
|
259
254
|
}
|
|
@@ -437,11 +432,8 @@ class AgentContext {
|
|
|
437
432
|
return this.cachedSystemRunnable;
|
|
438
433
|
}
|
|
439
434
|
// Stale or first access - rebuild
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
stableInstructions: this.buildStableInstructionsString(),
|
|
443
|
-
dynamicInstructions: this.buildDynamicInstructionsString(),
|
|
444
|
-
});
|
|
435
|
+
const instructionsString = this.buildInstructionsString();
|
|
436
|
+
this.cachedSystemRunnable = this.buildSystemRunnable(instructionsString);
|
|
445
437
|
this.systemRunnableStale = false;
|
|
446
438
|
return this.cachedSystemRunnable;
|
|
447
439
|
}
|
|
@@ -451,26 +443,16 @@ class AgentContext {
|
|
|
451
443
|
*/
|
|
452
444
|
initializeSystemRunnable() {
|
|
453
445
|
if (this.systemRunnableStale || this.cachedSystemRunnable === undefined) {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
stableInstructions: this.buildStableInstructionsString(),
|
|
457
|
-
dynamicInstructions: this.buildDynamicInstructionsString(),
|
|
458
|
-
});
|
|
446
|
+
const instructionsString = this.buildInstructionsString();
|
|
447
|
+
this.cachedSystemRunnable = this.buildSystemRunnable(instructionsString);
|
|
459
448
|
this.systemRunnableStale = false;
|
|
460
449
|
}
|
|
461
450
|
}
|
|
462
451
|
/**
|
|
463
|
-
* Builds the
|
|
464
|
-
* Includes agent identity preamble
|
|
465
|
-
* programmatic-only tool documentation. This is the part of the system
|
|
466
|
-
* message that stays byte-stable across turns and across users for the
|
|
467
|
-
* same agent — the prompt cache prefix.
|
|
468
|
-
*
|
|
469
|
-
* Per-user/per-message dynamic context belongs in
|
|
470
|
-
* `buildDynamicInstructionsString()` so it does not invalidate the cache
|
|
471
|
-
* marker. (See `feedback_cache_stability_invariant` for the rule.)
|
|
452
|
+
* Builds the raw instructions string (without creating SystemMessage).
|
|
453
|
+
* Includes agent identity preamble and handoff context when available.
|
|
472
454
|
*/
|
|
473
|
-
|
|
455
|
+
buildInstructionsString() {
|
|
474
456
|
const parts = [];
|
|
475
457
|
/** Build agent identity and handoff context preamble */
|
|
476
458
|
const identityPreamble = this.buildIdentityPreamble();
|
|
@@ -481,6 +463,11 @@ class AgentContext {
|
|
|
481
463
|
if (this.instructions != null && this.instructions !== '') {
|
|
482
464
|
parts.push(this.instructions);
|
|
483
465
|
}
|
|
466
|
+
/** Add additional instructions */
|
|
467
|
+
if (this.additionalInstructions != null &&
|
|
468
|
+
this.additionalInstructions !== '') {
|
|
469
|
+
parts.push(this.additionalInstructions);
|
|
470
|
+
}
|
|
484
471
|
/** Add programmatic tools documentation */
|
|
485
472
|
const programmaticToolsDoc = this.buildProgrammaticOnlyToolsInstructions();
|
|
486
473
|
if (programmaticToolsDoc) {
|
|
@@ -488,23 +475,6 @@ class AgentContext {
|
|
|
488
475
|
}
|
|
489
476
|
return parts.join('\n\n');
|
|
490
477
|
}
|
|
491
|
-
/**
|
|
492
|
-
* Builds the dynamic system-tail string (without creating SystemMessage).
|
|
493
|
-
* Keep this out of prompt-cache-marked content so volatile per-call
|
|
494
|
-
* context does not invalidate the stable prefix.
|
|
495
|
-
*
|
|
496
|
-
* `additional_instructions` is treated as dynamic (per-user/per-message
|
|
497
|
-
* memory, runtime context, etc.) and intentionally excluded from the
|
|
498
|
-
* cacheable prefix.
|
|
499
|
-
*/
|
|
500
|
-
buildDynamicInstructionsString() {
|
|
501
|
-
const parts = [];
|
|
502
|
-
if (this.additionalInstructions != null &&
|
|
503
|
-
this.additionalInstructions !== '') {
|
|
504
|
-
parts.push(this.additionalInstructions);
|
|
505
|
-
}
|
|
506
|
-
return parts.join('\n\n');
|
|
507
|
-
}
|
|
508
478
|
/**
|
|
509
479
|
* Builds the agent identity preamble including handoff context if present.
|
|
510
480
|
* This helps the agent understand its role in the multi-agent workflow.
|
|
@@ -542,136 +512,107 @@ class AgentContext {
|
|
|
542
512
|
return lines.join('\n');
|
|
543
513
|
}
|
|
544
514
|
/**
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
* marker on the stable instructions prefix.
|
|
515
|
+
* Build system runnable from pre-built instructions string.
|
|
516
|
+
* Only called when content has actually changed.
|
|
548
517
|
*/
|
|
549
|
-
|
|
550
|
-
if (
|
|
551
|
-
return false;
|
|
552
|
-
}
|
|
553
|
-
const anthropicOptions = this.clientOptions;
|
|
554
|
-
return anthropicOptions?.promptCache === true;
|
|
555
|
-
}
|
|
556
|
-
/**
|
|
557
|
-
* True when Bedrock prompt caching is enabled for this agent AND the
|
|
558
|
-
* configured model supports `cachePoint` blocks. Only Claude (and Nova)
|
|
559
|
-
* models on Bedrock honour cachePoint — Llama / Titan reject it.
|
|
560
|
-
*
|
|
561
|
-
* Used by `buildSystemRunnable` to inline a `cachePoint` block right
|
|
562
|
-
* after the stable system text so the system prefix is cached at the
|
|
563
|
-
* AWS account level (cross-user, cross-conversation).
|
|
564
|
-
*/
|
|
565
|
-
hasBedrockPromptCache() {
|
|
566
|
-
if (this.provider !== _enum.Providers.BEDROCK) {
|
|
567
|
-
return false;
|
|
568
|
-
}
|
|
569
|
-
const bedrockOptions = this.clientOptions;
|
|
570
|
-
if (bedrockOptions?.promptCache !== true) {
|
|
571
|
-
return false;
|
|
572
|
-
}
|
|
573
|
-
/* Allowlist-based check (see src/llm/bedrock/cacheSupport.ts). The
|
|
574
|
-
* `bedrockCacheModelPatterns` clientOption lets consumers add new
|
|
575
|
-
* model patterns without forking the library — useful when AWS adds
|
|
576
|
-
* a new family before the next library release. */
|
|
577
|
-
return cacheSupport.isBedrockCacheSupported(bedrockOptions.model, bedrockOptions.bedrockCacheModelPatterns);
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
* Build system runnable from cacheable blocks + the trailing
|
|
581
|
-
* `instructions` block + optional dynamic tail.
|
|
582
|
-
*
|
|
583
|
-
* ┌──────────────────────────────────────────┐
|
|
584
|
-
* │ system_cache_blocks[0].text │ ← consumer-defined block 0
|
|
585
|
-
* ├──── cache marker (TTL = blocks[0].ttl) ──┤
|
|
586
|
-
* │ system_cache_blocks[1].text │ ← consumer-defined block 1
|
|
587
|
-
* ├──── cache marker (TTL = blocks[1].ttl) ──┤
|
|
588
|
-
* │ instructions │ ← per-agent stable
|
|
589
|
-
* ├──── cache marker (TTL = instructionsCacheTtl) ┤
|
|
590
|
-
* │ additional_instructions │ ← dynamic (uncached)
|
|
591
|
-
* └──────────────────────────────────────────┘
|
|
592
|
-
*
|
|
593
|
-
* If `system_cache_blocks` is empty, behavior reduces to the 2-tier
|
|
594
|
-
* (instructions + dynamic) path used by simpler consumers.
|
|
595
|
-
*
|
|
596
|
-
* Provider-specific cache marker:
|
|
597
|
-
* - Anthropic: `cache_control: { type: 'ephemeral', ttl?: '1h'|'5m' }` on
|
|
598
|
-
* each cacheable text block. Up to 4 cache breakpoints per workspace.
|
|
599
|
-
* - Bedrock (Claude/Nova): a `{ cachePoint: { type: 'default', ttl?: '1h'|'5m' } }`
|
|
600
|
-
* block inserted after each cacheable section. Up to 4 cachePoints per
|
|
601
|
-
* request, of which the tools array can consume up to 2.
|
|
602
|
-
*
|
|
603
|
-
* Cache key composition: every byte from message start to a given cache
|
|
604
|
-
* marker forms that marker's cache key. Earlier blocks = wider cache key
|
|
605
|
-
* = more cross-tenant share. Place the most stable content first.
|
|
606
|
-
*/
|
|
607
|
-
buildSystemRunnable({ systemCacheBlocks, stableInstructions, dynamicInstructions, }) {
|
|
608
|
-
const hasAnyCacheBlocks = systemCacheBlocks.length > 0;
|
|
609
|
-
if (!hasAnyCacheBlocks && !stableInstructions && !dynamicInstructions) {
|
|
518
|
+
buildSystemRunnable(instructionsString) {
|
|
519
|
+
if (!instructionsString) {
|
|
610
520
|
// Remove previous tokens if we had a system message before
|
|
611
521
|
this.instructionTokens -= this.systemMessageTokens;
|
|
612
522
|
this.systemMessageTokens = 0;
|
|
613
523
|
return undefined;
|
|
614
524
|
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
525
|
+
let finalInstructions = instructionsString;
|
|
526
|
+
/**
|
|
527
|
+
* Tiered system-message assembly. When `systemCacheBlocks` is set, build
|
|
528
|
+
* a multi-block SystemMessage so each tier has its own cache breakpoint:
|
|
529
|
+
*
|
|
530
|
+
* [tier1_block_1][tier1_cachePoint]...[tier1_block_N][tier1_cachePoint]
|
|
531
|
+
* [instructions][instructions_cachePoint]
|
|
532
|
+
*
|
|
533
|
+
* Forward-prefix-hash means agents in the same workspace whose tier-1
|
|
534
|
+
* bytes are identical share the platform cache entry; only the per-agent
|
|
535
|
+
* `instructions` block invalidates per-agent.
|
|
536
|
+
*
|
|
537
|
+
* - Bedrock: emit `cachePoint: { type: 'default' }` blocks (handled by
|
|
538
|
+
* `convertSystemMessageToConverseMessage`).
|
|
539
|
+
* - Anthropic: emit `cache_control: { type: 'ephemeral' }` on each text
|
|
540
|
+
* block. (TTL hints are dropped for Anthropic — the SDK currently
|
|
541
|
+
* only supports `'ephemeral'`. Bedrock cachePoint has no TTL knob.)
|
|
542
|
+
*/
|
|
543
|
+
const hasTieredCache = Array.isArray(this.systemCacheBlocks) &&
|
|
544
|
+
this.systemCacheBlocks.length > 0;
|
|
545
|
+
const isBedrock = this.provider === _enum.Providers.BEDROCK;
|
|
546
|
+
const isAnthropic = this.provider === _enum.Providers.ANTHROPIC;
|
|
547
|
+
const anthropicCacheEnabled = isAnthropic &&
|
|
548
|
+
this.clientOptions
|
|
549
|
+
?.promptCache === true;
|
|
550
|
+
/**
|
|
551
|
+
* Bedrock cachePoint is Claude-only — Nova/Llama/Titan reject it.
|
|
552
|
+
* Mirrors the model check in `IllumaBedrockConverse.invocationParams`
|
|
553
|
+
* (src/llm/bedrock/index.ts:186-189).
|
|
554
|
+
*/
|
|
555
|
+
const bedrockCacheEnabled = isBedrock &&
|
|
556
|
+
(() => {
|
|
557
|
+
const opts = this.clientOptions;
|
|
558
|
+
const modelId = (opts?.model ?? '').toLowerCase();
|
|
559
|
+
const isClaudeModel = modelId.includes('claude') || modelId.includes('anthropic');
|
|
560
|
+
return opts?.promptCache === true && isClaudeModel;
|
|
561
|
+
})();
|
|
562
|
+
if (hasTieredCache && (bedrockCacheEnabled || anthropicCacheEnabled)) {
|
|
563
|
+
/**
|
|
564
|
+
* Anthropic / Bedrock cap cache breakpoints at 4 per request. The
|
|
565
|
+
* lib already emits one for tools and up to two for messages, so we
|
|
566
|
+
* have at most 1 left for the system block. We spend it on Tier 1
|
|
567
|
+
* (the workspace-shared bytes) — per-agent Tier 2 caching is still
|
|
568
|
+
* achieved implicitly via the tools breakpoint that follows, since
|
|
569
|
+
* cache lookups are forward-prefix-hash.
|
|
570
|
+
*
|
|
571
|
+
* Tier 1 may itself be emitted as multiple text blocks (one per
|
|
572
|
+
* `systemCacheBlocks` entry); only the LAST gets the cache marker,
|
|
573
|
+
* the rest are plain text inside the same cached prefix.
|
|
574
|
+
*/
|
|
575
|
+
const contentBlocks = [];
|
|
576
|
+
const tier1Blocks = this.systemCacheBlocks.filter((b) => b?.text);
|
|
577
|
+
tier1Blocks.forEach((block, idx) => {
|
|
578
|
+
const isLast = idx === tier1Blocks.length - 1;
|
|
579
|
+
if (bedrockCacheEnabled) {
|
|
580
|
+
contentBlocks.push({ type: 'text', text: block.text });
|
|
581
|
+
if (isLast) {
|
|
582
|
+
contentBlocks.push({ cachePoint: { type: 'default' } });
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
else if (isLast) {
|
|
586
|
+
contentBlocks.push({
|
|
587
|
+
type: 'text',
|
|
588
|
+
text: block.text,
|
|
589
|
+
cache_control: { type: 'ephemeral' },
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
else {
|
|
593
|
+
contentBlocks.push({ type: 'text', text: block.text });
|
|
594
|
+
}
|
|
595
|
+
});
|
|
596
|
+
if (instructionsString) {
|
|
597
|
+
// No cache marker on the trailing per-agent block — tools'
|
|
598
|
+
// breakpoint covers it.
|
|
599
|
+
contentBlocks.push({ type: 'text', text: instructionsString });
|
|
664
600
|
}
|
|
665
|
-
finalInstructions = {
|
|
601
|
+
finalInstructions = {
|
|
602
|
+
content: contentBlocks,
|
|
603
|
+
};
|
|
666
604
|
}
|
|
667
|
-
else {
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
605
|
+
else if (anthropicCacheEnabled) {
|
|
606
|
+
// Legacy single-block Anthropic caching (preserved for back-compat).
|
|
607
|
+
finalInstructions = {
|
|
608
|
+
content: [
|
|
609
|
+
{
|
|
610
|
+
type: 'text',
|
|
611
|
+
text: instructionsString,
|
|
612
|
+
cache_control: { type: 'ephemeral' },
|
|
613
|
+
},
|
|
614
|
+
],
|
|
615
|
+
};
|
|
675
616
|
}
|
|
676
617
|
const systemMessage = new messages.SystemMessage(finalInstructions);
|
|
677
618
|
// Update token counts (subtract old, add new)
|
|
@@ -740,45 +681,6 @@ class AgentContext {
|
|
|
740
681
|
this.indexTokenCountMap = { ...baseTokenMap };
|
|
741
682
|
}
|
|
742
683
|
}
|
|
743
|
-
/** Active tool definitions for token accounting (excludes deferred-and-undiscovered entries
|
|
744
|
-
* and definitions whose `allowed_callers` exclude `'direct'`). Mirrors the gate
|
|
745
|
-
* `getEventDrivenToolsForBinding` applies so accounting and binding stay aligned. */
|
|
746
|
-
getActiveToolDefinitions() {
|
|
747
|
-
if (!this.toolDefinitions) {
|
|
748
|
-
return [];
|
|
749
|
-
}
|
|
750
|
-
return this.toolDefinitions.filter((def) => {
|
|
751
|
-
const allowedCallers = def.allowed_callers ?? ['direct'];
|
|
752
|
-
if (!allowedCallers.includes('direct')) {
|
|
753
|
-
return false;
|
|
754
|
-
}
|
|
755
|
-
return (def.defer_loading !== true || this.discoveredToolNames.has(def.name));
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
/**
|
|
759
|
-
* Single source of truth for "which entries of `this.tools` should be
|
|
760
|
-
* treated as actually bound". Callers:
|
|
761
|
-
* - `getToolsForBinding` (non-event-driven branch)
|
|
762
|
-
* - `getEventDrivenToolsForBinding` (appends instance tools alongside
|
|
763
|
-
* schema-only definitions)
|
|
764
|
-
* - `calculateInstructionTokens` (counts schema bytes for accounting)
|
|
765
|
-
*
|
|
766
|
-
* In event-driven mode (`toolDefinitions` present) instance tools are
|
|
767
|
-
* appended unfiltered; outside event-driven mode they pass through
|
|
768
|
-
* `filterToolsForBinding`. Centralizing the decision here prevents the
|
|
769
|
-
* accounting/binding paths from drifting apart, which was the root
|
|
770
|
-
* cause of the original miscount (Fixes #121).
|
|
771
|
-
*/
|
|
772
|
-
getEffectiveInstanceTools() {
|
|
773
|
-
if (!this.tools) {
|
|
774
|
-
return undefined;
|
|
775
|
-
}
|
|
776
|
-
const isEventDriven = (this.toolDefinitions?.length ?? 0) > 0;
|
|
777
|
-
if (isEventDriven || !this.toolRegistry) {
|
|
778
|
-
return this.tools;
|
|
779
|
-
}
|
|
780
|
-
return this.filterToolsForBinding(this.tools);
|
|
781
|
-
}
|
|
782
684
|
/**
|
|
783
685
|
* Calculate tool tokens and add to instruction tokens
|
|
784
686
|
* Note: System message tokens are calculated during systemRunnable creation
|
|
@@ -792,16 +694,9 @@ class AgentContext {
|
|
|
792
694
|
const countedToolNames = new Set();
|
|
793
695
|
// Reset per-tool breakdown
|
|
794
696
|
this.toolsDetail = [];
|
|
795
|
-
/* Use `getEffectiveInstanceTools()` so accounting reflects exactly the
|
|
796
|
-
* subset that `getToolsForBinding` would emit — preventing the
|
|
797
|
-
* worst-case-ceiling miscount that triggered spurious `empty_messages`
|
|
798
|
-
* preflight rejections at low `maxContextTokens`. Deferred and
|
|
799
|
-
* non-`'direct'` `toolDefinitions` are excluded by
|
|
800
|
-
* `getActiveToolDefinitions()` below. */
|
|
801
|
-
const effectiveInstanceTools = this.getEffectiveInstanceTools();
|
|
802
697
|
// Count tokens for bound tools (StructuredTool instances with .schema)
|
|
803
|
-
if (
|
|
804
|
-
for (const tool of
|
|
698
|
+
if (this.tools && this.tools.length > 0) {
|
|
699
|
+
for (const tool of this.tools) {
|
|
805
700
|
const genericTool = tool;
|
|
806
701
|
if (genericTool.schema != null &&
|
|
807
702
|
typeof genericTool.schema === 'object') {
|
|
@@ -820,25 +715,23 @@ class AgentContext {
|
|
|
820
715
|
// Count tokens for tool definitions (MCP / event-driven tools).
|
|
821
716
|
// These are sent to the provider API as tool schemas alongside bound tools.
|
|
822
717
|
// Both can be populated simultaneously (graph tools + MCP tools).
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
718
|
+
if (this.toolDefinitions && this.toolDefinitions.length > 0) {
|
|
719
|
+
for (const def of this.toolDefinitions) {
|
|
720
|
+
if (countedToolNames.has(def.name)) {
|
|
721
|
+
continue; // Already counted via this.tools
|
|
722
|
+
}
|
|
723
|
+
const schema = {
|
|
724
|
+
name: def.name,
|
|
725
|
+
description: def.description ?? '',
|
|
726
|
+
parameters: def.parameters ?? {},
|
|
727
|
+
};
|
|
728
|
+
const defTokens = tokenCounter(new messages.SystemMessage(JSON.stringify(schema)));
|
|
729
|
+
this.toolsDetail.push({
|
|
730
|
+
name: def.name || 'unknown',
|
|
731
|
+
tokens: defTokens,
|
|
732
|
+
});
|
|
733
|
+
toolTokens += defTokens;
|
|
830
734
|
}
|
|
831
|
-
const schema = {
|
|
832
|
-
name: def.name,
|
|
833
|
-
description: def.description ?? '',
|
|
834
|
-
parameters: def.parameters ?? {},
|
|
835
|
-
};
|
|
836
|
-
const defTokens = tokenCounter(new messages.SystemMessage(JSON.stringify(schema)));
|
|
837
|
-
this.toolsDetail.push({
|
|
838
|
-
name: def.name || 'unknown',
|
|
839
|
-
tokens: defTokens,
|
|
840
|
-
});
|
|
841
|
-
toolTokens += defTokens;
|
|
842
735
|
}
|
|
843
736
|
// Store total tool tokens for breakdown reporting
|
|
844
737
|
this.toolTokensTotal = toolTokens;
|
|
@@ -970,7 +863,9 @@ class AgentContext {
|
|
|
970
863
|
return this.getEventDrivenToolsForBinding();
|
|
971
864
|
}
|
|
972
865
|
/** Traditional mode: filter actual tool instances */
|
|
973
|
-
const filtered = this.
|
|
866
|
+
const filtered = !this.tools || !this.toolRegistry
|
|
867
|
+
? this.tools
|
|
868
|
+
: this.filterToolsForBinding(this.tools);
|
|
974
869
|
if (this.graphTools && this.graphTools.length > 0) {
|
|
975
870
|
return [...(filtered ?? []), ...this.graphTools];
|
|
976
871
|
}
|
|
@@ -981,16 +876,22 @@ class AgentContext {
|
|
|
981
876
|
if (!this.toolDefinitions) {
|
|
982
877
|
return this.graphTools ?? [];
|
|
983
878
|
}
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
879
|
+
const defsToInclude = this.toolDefinitions.filter((def) => {
|
|
880
|
+
const allowedCallers = def.allowed_callers ?? ['direct'];
|
|
881
|
+
if (!allowedCallers.includes('direct')) {
|
|
882
|
+
return false;
|
|
883
|
+
}
|
|
884
|
+
if (def.defer_loading === true &&
|
|
885
|
+
!this.discoveredToolNames.has(def.name)) {
|
|
886
|
+
return false;
|
|
887
|
+
}
|
|
888
|
+
return true;
|
|
889
|
+
});
|
|
890
|
+
const schemaTools = schema$1.createSchemaOnlyTools(defsToInclude);
|
|
987
891
|
const allTools = [...schemaTools];
|
|
988
892
|
if (this.graphTools && this.graphTools.length > 0) {
|
|
989
893
|
allTools.push(...this.graphTools);
|
|
990
894
|
}
|
|
991
|
-
/* In event-driven mode, instance tools are appended UNFILTERED (matching
|
|
992
|
-
* `getEffectiveInstanceTools()`'s event-driven branch). Deferred /
|
|
993
|
-
* non-direct logic is represented in `toolDefinitions`, not here. */
|
|
994
895
|
if (this.tools && this.tools.length > 0) {
|
|
995
896
|
allTools.push(...this.tools);
|
|
996
897
|
}
|