@illuma-ai/agents 1.1.21 → 1.1.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/graphs/Graph.cjs +12 -1
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +105 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/run.cjs +20 -9
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/utils/llm.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +12 -1
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +105 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/run.mjs +20 -9
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/utils/llm.mjs.map +1 -1
- package/dist/types/graphs/MultiAgentGraph.d.ts +17 -0
- package/package.json +1 -1
- package/src/graphs/Graph.ts +13 -1
- package/src/graphs/MultiAgentGraph.ts +128 -1
- package/src/graphs/__tests__/multi-agent-delegate.test.ts +205 -0
- package/src/run.ts +20 -11
- package/src/scripts/test-bedrock-handoff-autonomous.ts +231 -0
- package/src/utils/llm.ts +1 -0
- package/src/agents/AgentContext.js +0 -782
- package/src/agents/AgentContext.test.js +0 -421
- package/src/agents/__tests__/AgentContext.test.js +0 -678
- package/src/agents/__tests__/resolveStructuredOutputMode.test.js +0 -117
- package/src/common/enum.js +0 -192
- package/src/common/index.js +0 -3
- package/src/events.js +0 -166
- package/src/graphs/Graph.js +0 -1857
- package/src/graphs/MultiAgentGraph.js +0 -1092
- package/src/graphs/__tests__/structured-output.integration.test.js +0 -624
- package/src/graphs/__tests__/structured-output.test.js +0 -144
- package/src/graphs/contextManagement.e2e.test.js +0 -718
- package/src/graphs/contextManagement.test.js +0 -485
- package/src/graphs/handoffValidation.test.js +0 -276
- package/src/graphs/index.js +0 -3
- package/src/index.js +0 -28
- package/src/instrumentation.js +0 -21
- package/src/llm/anthropic/index.js +0 -319
- package/src/llm/anthropic/types.js +0 -46
- package/src/llm/anthropic/utils/message_inputs.js +0 -627
- package/src/llm/anthropic/utils/message_outputs.js +0 -290
- package/src/llm/anthropic/utils/output_parsers.js +0 -89
- package/src/llm/anthropic/utils/tools.js +0 -25
- package/src/llm/bedrock/__tests__/bedrock-caching.test.js +0 -392
- package/src/llm/bedrock/index.js +0 -303
- package/src/llm/bedrock/types.js +0 -2
- package/src/llm/bedrock/utils/index.js +0 -6
- package/src/llm/bedrock/utils/message_inputs.js +0 -463
- package/src/llm/bedrock/utils/message_outputs.js +0 -269
- package/src/llm/fake.js +0 -92
- package/src/llm/google/index.js +0 -215
- package/src/llm/google/types.js +0 -12
- package/src/llm/google/utils/common.js +0 -670
- package/src/llm/google/utils/tools.js +0 -111
- package/src/llm/google/utils/zod_to_genai_parameters.js +0 -47
- package/src/llm/openai/index.js +0 -1033
- package/src/llm/openai/types.js +0 -2
- package/src/llm/openai/utils/index.js +0 -756
- package/src/llm/openai/utils/isReasoningModel.test.js +0 -79
- package/src/llm/openrouter/index.js +0 -261
- package/src/llm/openrouter/reasoning.test.js +0 -181
- package/src/llm/providers.js +0 -36
- package/src/llm/text.js +0 -65
- package/src/llm/vertexai/index.js +0 -402
- package/src/messages/__tests__/tools.test.js +0 -392
- package/src/messages/cache.js +0 -404
- package/src/messages/cache.test.js +0 -1167
- package/src/messages/content.js +0 -48
- package/src/messages/content.test.js +0 -314
- package/src/messages/core.js +0 -359
- package/src/messages/ensureThinkingBlock.test.js +0 -997
- package/src/messages/format.js +0 -973
- package/src/messages/formatAgentMessages.test.js +0 -2278
- package/src/messages/formatAgentMessages.tools.test.js +0 -362
- package/src/messages/formatMessage.test.js +0 -608
- package/src/messages/ids.js +0 -18
- package/src/messages/index.js +0 -9
- package/src/messages/labelContentByAgent.test.js +0 -725
- package/src/messages/prune.js +0 -438
- package/src/messages/reducer.js +0 -60
- package/src/messages/shiftIndexTokenCountMap.test.js +0 -63
- package/src/messages/summarize.js +0 -146
- package/src/messages/summarize.test.js +0 -332
- package/src/messages/tools.js +0 -90
- package/src/mockStream.js +0 -81
- package/src/prompts/collab.js +0 -7
- package/src/prompts/index.js +0 -3
- package/src/prompts/taskmanager.js +0 -58
- package/src/run.js +0 -427
- package/src/schemas/index.js +0 -3
- package/src/schemas/schema-preparation.test.js +0 -370
- package/src/schemas/validate.js +0 -314
- package/src/schemas/validate.test.js +0 -264
- package/src/scripts/abort.js +0 -127
- package/src/scripts/ant_web_search.js +0 -130
- package/src/scripts/ant_web_search_edge_case.js +0 -133
- package/src/scripts/ant_web_search_error_edge_case.js +0 -119
- package/src/scripts/args.js +0 -41
- package/src/scripts/bedrock-cache-debug.js +0 -186
- package/src/scripts/bedrock-content-aggregation-test.js +0 -195
- package/src/scripts/bedrock-merge-test.js +0 -80
- package/src/scripts/bedrock-parallel-tools-test.js +0 -150
- package/src/scripts/caching.js +0 -106
- package/src/scripts/cli.js +0 -152
- package/src/scripts/cli2.js +0 -119
- package/src/scripts/cli3.js +0 -163
- package/src/scripts/cli4.js +0 -165
- package/src/scripts/cli5.js +0 -165
- package/src/scripts/code_exec.js +0 -171
- package/src/scripts/code_exec_files.js +0 -180
- package/src/scripts/code_exec_multi_session.js +0 -185
- package/src/scripts/code_exec_ptc.js +0 -265
- package/src/scripts/code_exec_session.js +0 -217
- package/src/scripts/code_exec_simple.js +0 -120
- package/src/scripts/content.js +0 -111
- package/src/scripts/empty_input.js +0 -125
- package/src/scripts/handoff-test.js +0 -96
- package/src/scripts/image.js +0 -138
- package/src/scripts/memory.js +0 -83
- package/src/scripts/multi-agent-chain.js +0 -271
- package/src/scripts/multi-agent-conditional.js +0 -185
- package/src/scripts/multi-agent-document-review-chain.js +0 -171
- package/src/scripts/multi-agent-hybrid-flow.js +0 -264
- package/src/scripts/multi-agent-parallel-start.js +0 -214
- package/src/scripts/multi-agent-parallel.js +0 -346
- package/src/scripts/multi-agent-sequence.js +0 -184
- package/src/scripts/multi-agent-supervisor.js +0 -324
- package/src/scripts/multi-agent-test.js +0 -147
- package/src/scripts/parallel-asymmetric-tools-test.js +0 -202
- package/src/scripts/parallel-full-metadata-test.js +0 -176
- package/src/scripts/parallel-tools-test.js +0 -256
- package/src/scripts/programmatic_exec.js +0 -277
- package/src/scripts/programmatic_exec_agent.js +0 -168
- package/src/scripts/search.js +0 -118
- package/src/scripts/sequential-full-metadata-test.js +0 -143
- package/src/scripts/simple.js +0 -174
- package/src/scripts/single-agent-metadata-test.js +0 -152
- package/src/scripts/stream.js +0 -113
- package/src/scripts/test-custom-prompt-key.js +0 -132
- package/src/scripts/test-handoff-input.js +0 -143
- package/src/scripts/test-handoff-preamble.js +0 -227
- package/src/scripts/test-handoff-steering.js +0 -353
- package/src/scripts/test-multi-agent-list-handoff.js +0 -318
- package/src/scripts/test-parallel-agent-labeling.js +0 -253
- package/src/scripts/test-parallel-handoffs.js +0 -229
- package/src/scripts/test-thinking-handoff-bedrock.js +0 -132
- package/src/scripts/test-thinking-handoff.js +0 -132
- package/src/scripts/test-thinking-to-thinking-handoff-bedrock.js +0 -140
- package/src/scripts/test-tool-before-handoff-role-order.js +0 -223
- package/src/scripts/test-tools-before-handoff.js +0 -187
- package/src/scripts/test_code_api.js +0 -263
- package/src/scripts/thinking-bedrock.js +0 -128
- package/src/scripts/thinking-vertexai.js +0 -130
- package/src/scripts/thinking.js +0 -134
- package/src/scripts/tool_search.js +0 -114
- package/src/scripts/tools.js +0 -125
- package/src/specs/agent-handoffs-bedrock.integration.test.js +0 -280
- package/src/specs/agent-handoffs.test.js +0 -924
- package/src/specs/anthropic.simple.test.js +0 -287
- package/src/specs/azure.simple.test.js +0 -381
- package/src/specs/cache.simple.test.js +0 -282
- package/src/specs/custom-event-await.test.js +0 -148
- package/src/specs/deepseek.simple.test.js +0 -189
- package/src/specs/emergency-prune.test.js +0 -308
- package/src/specs/moonshot.simple.test.js +0 -237
- package/src/specs/observability.integration.test.js +0 -1337
- package/src/specs/openai.simple.test.js +0 -233
- package/src/specs/openrouter.simple.test.js +0 -202
- package/src/specs/prune.test.js +0 -733
- package/src/specs/reasoning.test.js +0 -144
- package/src/specs/spec.utils.js +0 -4
- package/src/specs/thinking-handoff.test.js +0 -486
- package/src/specs/thinking-prune.test.js +0 -600
- package/src/specs/token-distribution-edge-case.test.js +0 -246
- package/src/specs/token-memoization.test.js +0 -32
- package/src/specs/tokens.test.js +0 -49
- package/src/specs/tool-error.test.js +0 -139
- package/src/splitStream.js +0 -204
- package/src/splitStream.test.js +0 -504
- package/src/stream.js +0 -650
- package/src/stream.test.js +0 -225
- package/src/test/mockTools.js +0 -340
- package/src/tools/BrowserTools.js +0 -245
- package/src/tools/Calculator.js +0 -38
- package/src/tools/Calculator.test.js +0 -225
- package/src/tools/CodeExecutor.js +0 -233
- package/src/tools/ProgrammaticToolCalling.js +0 -602
- package/src/tools/StreamingToolCallBuffer.js +0 -179
- package/src/tools/ToolNode.js +0 -930
- package/src/tools/ToolSearch.js +0 -904
- package/src/tools/__tests__/BrowserTools.test.js +0 -306
- package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.js +0 -276
- package/src/tools/__tests__/ProgrammaticToolCalling.test.js +0 -807
- package/src/tools/__tests__/StreamingToolCallBuffer.test.js +0 -175
- package/src/tools/__tests__/ToolApproval.test.js +0 -675
- package/src/tools/__tests__/ToolNode.recovery.test.js +0 -200
- package/src/tools/__tests__/ToolNode.session.test.js +0 -319
- package/src/tools/__tests__/ToolSearch.integration.test.js +0 -125
- package/src/tools/__tests__/ToolSearch.test.js +0 -812
- package/src/tools/__tests__/handlers.test.js +0 -799
- package/src/tools/__tests__/truncation-recovery.integration.test.js +0 -362
- package/src/tools/handlers.js +0 -306
- package/src/tools/schema.js +0 -25
- package/src/tools/search/anthropic.js +0 -34
- package/src/tools/search/content.js +0 -116
- package/src/tools/search/content.test.js +0 -133
- package/src/tools/search/firecrawl.js +0 -173
- package/src/tools/search/format.js +0 -198
- package/src/tools/search/highlights.js +0 -241
- package/src/tools/search/index.js +0 -3
- package/src/tools/search/jina-reranker.test.js +0 -106
- package/src/tools/search/rerankers.js +0 -165
- package/src/tools/search/schema.js +0 -102
- package/src/tools/search/search.js +0 -561
- package/src/tools/search/serper-scraper.js +0 -126
- package/src/tools/search/test.js +0 -129
- package/src/tools/search/tool.js +0 -453
- package/src/tools/search/types.js +0 -2
- package/src/tools/search/utils.js +0 -59
- package/src/types/graph.js +0 -24
- package/src/types/graph.test.js +0 -192
- package/src/types/index.js +0 -7
- package/src/types/llm.js +0 -2
- package/src/types/messages.js +0 -2
- package/src/types/run.js +0 -2
- package/src/types/stream.js +0 -2
- package/src/types/tools.js +0 -2
- package/src/utils/contextAnalytics.js +0 -79
- package/src/utils/contextAnalytics.test.js +0 -166
- package/src/utils/events.js +0 -26
- package/src/utils/graph.js +0 -11
- package/src/utils/handlers.js +0 -65
- package/src/utils/index.js +0 -10
- package/src/utils/llm.js +0 -21
- package/src/utils/llmConfig.js +0 -205
- package/src/utils/logging.js +0 -37
- package/src/utils/misc.js +0 -51
- package/src/utils/run.js +0 -69
- package/src/utils/schema.js +0 -21
- package/src/utils/title.js +0 -119
- package/src/utils/tokens.js +0 -92
- package/src/utils/toonFormat.js +0 -379
|
@@ -1,799 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach, jest } from '@jest/globals';
|
|
2
|
-
import { StepTypes, ToolCallTypes, Providers, GraphEvents } from '@/common';
|
|
3
|
-
import { handleToolCallChunks, handleToolCalls, handleServerToolResult, } from '../handlers';
|
|
4
|
-
function createMockGraph(overrides) {
|
|
5
|
-
let stepCounter = 0;
|
|
6
|
-
return {
|
|
7
|
-
getStepKey: jest.fn().mockReturnValue('step-key'),
|
|
8
|
-
getStepIdByKey: jest.fn().mockReturnValue('prev-step-id'),
|
|
9
|
-
getRunStep: jest
|
|
10
|
-
.fn()
|
|
11
|
-
.mockReturnValue(undefined),
|
|
12
|
-
dispatchRunStep: jest
|
|
13
|
-
.fn()
|
|
14
|
-
.mockImplementation(async () => `new-step-${++stepCounter}`),
|
|
15
|
-
dispatchRunStepDelta: jest
|
|
16
|
-
.fn()
|
|
17
|
-
.mockResolvedValue(undefined),
|
|
18
|
-
toolCallStepIds: new Map(),
|
|
19
|
-
messageStepHasToolCalls: new Map(),
|
|
20
|
-
messageIdsByStepKey: new Map(),
|
|
21
|
-
prelimMessageIdsByStepKey: new Map(),
|
|
22
|
-
invokedToolIds: undefined,
|
|
23
|
-
handlerRegistry: undefined,
|
|
24
|
-
streamingToolCallBuffer: {
|
|
25
|
-
append: jest.fn(),
|
|
26
|
-
setToolName: jest.fn(),
|
|
27
|
-
getToolName: jest.fn(),
|
|
28
|
-
getRawArgs: jest.fn(),
|
|
29
|
-
extractFieldValue: jest.fn(),
|
|
30
|
-
has: jest.fn().mockReturnValue(false),
|
|
31
|
-
clear: jest.fn(),
|
|
32
|
-
clearAll: jest.fn(),
|
|
33
|
-
setIndexMapping: jest.fn(),
|
|
34
|
-
getIdByIndex: jest.fn().mockReturnValue(undefined),
|
|
35
|
-
},
|
|
36
|
-
...overrides,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
function makeRunStep(type, opts) {
|
|
40
|
-
const stepDetails = type === StepTypes.MESSAGE_CREATION
|
|
41
|
-
? {
|
|
42
|
-
type: StepTypes.MESSAGE_CREATION,
|
|
43
|
-
message_creation: { message_id: 'msg-1' },
|
|
44
|
-
}
|
|
45
|
-
: { type: StepTypes.TOOL_CALLS, tool_calls: opts?.tool_calls ?? [] };
|
|
46
|
-
return {
|
|
47
|
-
type,
|
|
48
|
-
id: opts?.id ?? 'run-step-1',
|
|
49
|
-
index: opts?.index ?? 0,
|
|
50
|
-
stepDetails,
|
|
51
|
-
usage: null,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
function makeToolCall(id, name = 'calculator') {
|
|
55
|
-
return { id, name, args: {}, type: 'tool_call' };
|
|
56
|
-
}
|
|
57
|
-
function makeToolCallChunk(opts) {
|
|
58
|
-
return {
|
|
59
|
-
id: opts?.id,
|
|
60
|
-
name: opts?.name,
|
|
61
|
-
args: '',
|
|
62
|
-
index: opts?.index ?? 0,
|
|
63
|
-
type: 'tool_call_chunk',
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
const defaultMetadata = { run_id: 'test-run' };
|
|
67
|
-
describe('handleToolCallChunks', () => {
|
|
68
|
-
let graph;
|
|
69
|
-
beforeEach(() => {
|
|
70
|
-
graph = createMockGraph();
|
|
71
|
-
});
|
|
72
|
-
it('creates TOOL_CALLS step when previous step is MESSAGE_CREATION', async () => {
|
|
73
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
74
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
75
|
-
const chunks = [makeToolCallChunk({ index: 2 })];
|
|
76
|
-
await handleToolCallChunks({
|
|
77
|
-
graph: graph,
|
|
78
|
-
stepKey: 'step-key',
|
|
79
|
-
toolCallChunks: chunks,
|
|
80
|
-
metadata: defaultMetadata,
|
|
81
|
-
});
|
|
82
|
-
const dispatchCalls = graph.dispatchRunStep.mock.calls;
|
|
83
|
-
expect(dispatchCalls).toHaveLength(1);
|
|
84
|
-
expect(dispatchCalls[0][1]).toEqual(expect.objectContaining({ type: StepTypes.TOOL_CALLS }));
|
|
85
|
-
expect(graph.messageStepHasToolCalls.has('prev-step-id')).toBe(true);
|
|
86
|
-
expect(graph.dispatchRunStepDelta).toHaveBeenCalledTimes(1);
|
|
87
|
-
});
|
|
88
|
-
it('reuses existing TOOL_CALLS step without dispatching a new one', async () => {
|
|
89
|
-
const toolStep = makeRunStep(StepTypes.TOOL_CALLS);
|
|
90
|
-
graph.getRunStep.mockReturnValue(toolStep);
|
|
91
|
-
const chunks = [makeToolCallChunk({ index: 2 })];
|
|
92
|
-
await handleToolCallChunks({
|
|
93
|
-
graph: graph,
|
|
94
|
-
stepKey: 'step-key',
|
|
95
|
-
toolCallChunks: chunks,
|
|
96
|
-
metadata: defaultMetadata,
|
|
97
|
-
});
|
|
98
|
-
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
99
|
-
expect(graph.dispatchRunStepDelta).toHaveBeenCalledTimes(1);
|
|
100
|
-
expect(graph.dispatchRunStepDelta).toHaveBeenCalledWith('prev-step-id', expect.objectContaining({ type: StepTypes.TOOL_CALLS }));
|
|
101
|
-
});
|
|
102
|
-
it('creates MESSAGE_CREATION when no previous step exists', async () => {
|
|
103
|
-
let callCount = 0;
|
|
104
|
-
graph.getStepIdByKey.mockImplementation(() => {
|
|
105
|
-
callCount++;
|
|
106
|
-
if (callCount === 1) {
|
|
107
|
-
throw new Error('No step found');
|
|
108
|
-
}
|
|
109
|
-
return 'new-step-1';
|
|
110
|
-
});
|
|
111
|
-
graph.getRunStep.mockReturnValue(makeRunStep(StepTypes.MESSAGE_CREATION));
|
|
112
|
-
const chunks = [makeToolCallChunk({ index: 0 })];
|
|
113
|
-
await handleToolCallChunks({
|
|
114
|
-
graph: graph,
|
|
115
|
-
stepKey: 'step-key',
|
|
116
|
-
toolCallChunks: chunks,
|
|
117
|
-
metadata: defaultMetadata,
|
|
118
|
-
});
|
|
119
|
-
const dispatchCalls = graph.dispatchRunStep.mock.calls;
|
|
120
|
-
expect(dispatchCalls.length).toBeGreaterThanOrEqual(2);
|
|
121
|
-
expect(dispatchCalls[0][1]).toEqual(expect.objectContaining({ type: StepTypes.MESSAGE_CREATION }));
|
|
122
|
-
expect(dispatchCalls[1][1]).toEqual(expect.objectContaining({ type: StepTypes.TOOL_CALLS }));
|
|
123
|
-
});
|
|
124
|
-
it('skips TOOL_CALLS dispatch when already dispatched for this MESSAGE_CREATION', async () => {
|
|
125
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
126
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
127
|
-
graph.messageStepHasToolCalls.set('prev-step-id', true);
|
|
128
|
-
const chunks = [makeToolCallChunk({ index: 2 })];
|
|
129
|
-
await handleToolCallChunks({
|
|
130
|
-
graph: graph,
|
|
131
|
-
stepKey: 'step-key',
|
|
132
|
-
toolCallChunks: chunks,
|
|
133
|
-
metadata: defaultMetadata,
|
|
134
|
-
});
|
|
135
|
-
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
136
|
-
expect(graph.dispatchRunStepDelta).toHaveBeenCalledTimes(1);
|
|
137
|
-
});
|
|
138
|
-
it('sanitizes empty string id and name to undefined', async () => {
|
|
139
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
140
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
141
|
-
const chunk = makeToolCallChunk({ id: '', name: '' });
|
|
142
|
-
await handleToolCallChunks({
|
|
143
|
-
graph: graph,
|
|
144
|
-
stepKey: 'step-key',
|
|
145
|
-
toolCallChunks: [chunk],
|
|
146
|
-
metadata: defaultMetadata,
|
|
147
|
-
});
|
|
148
|
-
expect(chunk.id).toBeUndefined();
|
|
149
|
-
expect(chunk.name).toBeUndefined();
|
|
150
|
-
});
|
|
151
|
-
it('populates tool_calls when chunk has valid id and name', async () => {
|
|
152
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
153
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
154
|
-
const chunks = [
|
|
155
|
-
makeToolCallChunk({ id: 'tooluse_abc', name: 'calculator', index: 2 }),
|
|
156
|
-
];
|
|
157
|
-
await handleToolCallChunks({
|
|
158
|
-
graph: graph,
|
|
159
|
-
stepKey: 'step-key',
|
|
160
|
-
toolCallChunks: chunks,
|
|
161
|
-
metadata: defaultMetadata,
|
|
162
|
-
});
|
|
163
|
-
const toolCallsArg = graph.dispatchRunStep.mock
|
|
164
|
-
.calls[0][1];
|
|
165
|
-
expect(toolCallsArg.tool_calls).toEqual([
|
|
166
|
-
expect.objectContaining({
|
|
167
|
-
id: 'tooluse_abc',
|
|
168
|
-
name: 'calculator',
|
|
169
|
-
type: ToolCallTypes.TOOL_CALL,
|
|
170
|
-
}),
|
|
171
|
-
]);
|
|
172
|
-
});
|
|
173
|
-
it('never dispatches empty text block alongside TOOL_CALLS step', async () => {
|
|
174
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
175
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
176
|
-
const chunks = [
|
|
177
|
-
makeToolCallChunk({ id: 'tooluse_abc', name: 'calculator', index: 2 }),
|
|
178
|
-
];
|
|
179
|
-
await handleToolCallChunks({
|
|
180
|
-
graph: graph,
|
|
181
|
-
stepKey: 'step-key',
|
|
182
|
-
toolCallChunks: chunks,
|
|
183
|
-
metadata: defaultMetadata,
|
|
184
|
-
});
|
|
185
|
-
const allDispatches = graph.dispatchRunStep.mock.calls;
|
|
186
|
-
expect(allDispatches).toHaveLength(1);
|
|
187
|
-
const stepDetails = allDispatches[0][1];
|
|
188
|
-
expect(stepDetails.type).toBe(StepTypes.TOOL_CALLS);
|
|
189
|
-
expect(stepDetails).not.toHaveProperty('content');
|
|
190
|
-
expect(stepDetails).not.toHaveProperty('text');
|
|
191
|
-
});
|
|
192
|
-
it('feeds buffer with id+args on same chunk (OpenAI/Anthropic pattern)', async () => {
|
|
193
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
194
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
195
|
-
const chunk = {
|
|
196
|
-
id: 'tc_1',
|
|
197
|
-
name: 'content_tool',
|
|
198
|
-
args: '{"action":"wri',
|
|
199
|
-
index: 0,
|
|
200
|
-
type: 'tool_call_chunk',
|
|
201
|
-
};
|
|
202
|
-
await handleToolCallChunks({
|
|
203
|
-
graph: graph,
|
|
204
|
-
stepKey: 'step-key',
|
|
205
|
-
toolCallChunks: [chunk],
|
|
206
|
-
metadata: defaultMetadata,
|
|
207
|
-
});
|
|
208
|
-
expect(graph.streamingToolCallBuffer.setIndexMapping).toHaveBeenCalledWith(0, 'tc_1');
|
|
209
|
-
expect(graph.streamingToolCallBuffer.setToolName).toHaveBeenCalledWith('tc_1', 'content_tool');
|
|
210
|
-
expect(graph.streamingToolCallBuffer.append).toHaveBeenCalledWith('tc_1', '{"action":"wri');
|
|
211
|
-
});
|
|
212
|
-
it('feeds buffer via index lookup for Bedrock DELTA chunks (no id)', async () => {
|
|
213
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
214
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
215
|
-
// Simulate: index 0 was previously mapped to 'tc_1'
|
|
216
|
-
graph.streamingToolCallBuffer.getIdByIndex.mockReturnValue('tc_1');
|
|
217
|
-
const deltaChunk = {
|
|
218
|
-
id: undefined,
|
|
219
|
-
name: undefined,
|
|
220
|
-
args: 'te","content":"hello',
|
|
221
|
-
index: 0,
|
|
222
|
-
type: 'tool_call_chunk',
|
|
223
|
-
};
|
|
224
|
-
await handleToolCallChunks({
|
|
225
|
-
graph: graph,
|
|
226
|
-
stepKey: 'step-key',
|
|
227
|
-
toolCallChunks: [deltaChunk],
|
|
228
|
-
metadata: defaultMetadata,
|
|
229
|
-
});
|
|
230
|
-
expect(graph.streamingToolCallBuffer.getIdByIndex).toHaveBeenCalledWith(0);
|
|
231
|
-
expect(graph.streamingToolCallBuffer.append).toHaveBeenCalledWith('tc_1', 'te","content":"hello');
|
|
232
|
-
});
|
|
233
|
-
it('does not feed buffer when DELTA chunk has no index mapping', async () => {
|
|
234
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
235
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
236
|
-
// No index mapping exists
|
|
237
|
-
graph.streamingToolCallBuffer.getIdByIndex.mockReturnValue(undefined);
|
|
238
|
-
const deltaChunk = {
|
|
239
|
-
id: undefined,
|
|
240
|
-
name: undefined,
|
|
241
|
-
args: 'some args',
|
|
242
|
-
index: 5,
|
|
243
|
-
type: 'tool_call_chunk',
|
|
244
|
-
};
|
|
245
|
-
await handleToolCallChunks({
|
|
246
|
-
graph: graph,
|
|
247
|
-
stepKey: 'step-key',
|
|
248
|
-
toolCallChunks: [deltaChunk],
|
|
249
|
-
metadata: defaultMetadata,
|
|
250
|
-
});
|
|
251
|
-
expect(graph.streamingToolCallBuffer.append).not.toHaveBeenCalled();
|
|
252
|
-
});
|
|
253
|
-
it('dispatches delta even when chunks lack id/name (Bedrock pattern)', async () => {
|
|
254
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
255
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
256
|
-
const chunks = [makeToolCallChunk({ index: 2 })];
|
|
257
|
-
await handleToolCallChunks({
|
|
258
|
-
graph: graph,
|
|
259
|
-
stepKey: 'step-key',
|
|
260
|
-
toolCallChunks: chunks,
|
|
261
|
-
metadata: defaultMetadata,
|
|
262
|
-
});
|
|
263
|
-
expect(graph.dispatchRunStepDelta).toHaveBeenCalledTimes(1);
|
|
264
|
-
const toolCallsArg = graph.dispatchRunStep.mock
|
|
265
|
-
.calls[0][1];
|
|
266
|
-
expect(toolCallsArg.tool_calls).toEqual([]);
|
|
267
|
-
});
|
|
268
|
-
});
|
|
269
|
-
describe('handleToolCalls', () => {
|
|
270
|
-
let graph;
|
|
271
|
-
beforeEach(() => {
|
|
272
|
-
graph = createMockGraph();
|
|
273
|
-
});
|
|
274
|
-
it('returns early when metadata is missing', async () => {
|
|
275
|
-
await handleToolCalls([makeToolCall('id-1')], undefined, graph);
|
|
276
|
-
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
277
|
-
});
|
|
278
|
-
it('returns early when toolCalls is undefined', async () => {
|
|
279
|
-
await handleToolCalls(undefined, defaultMetadata, graph);
|
|
280
|
-
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
281
|
-
});
|
|
282
|
-
it('returns early when toolCalls is empty', async () => {
|
|
283
|
-
await handleToolCalls([], defaultMetadata, graph);
|
|
284
|
-
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
285
|
-
});
|
|
286
|
-
it('skips tool call when id already in toolCallStepIds', async () => {
|
|
287
|
-
graph.toolCallStepIds.set('id-1', 'existing-step');
|
|
288
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
289
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
290
|
-
await handleToolCalls([makeToolCall('id-1')], defaultMetadata, graph);
|
|
291
|
-
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
292
|
-
});
|
|
293
|
-
it('assigns fallback id when tool_call.id is undefined', async () => {
|
|
294
|
-
const tc = {
|
|
295
|
-
id: undefined,
|
|
296
|
-
name: 'calc',
|
|
297
|
-
args: {},
|
|
298
|
-
type: 'tool_call',
|
|
299
|
-
};
|
|
300
|
-
graph.getStepIdByKey.mockImplementation(() => {
|
|
301
|
-
throw new Error('no step');
|
|
302
|
-
});
|
|
303
|
-
await handleToolCalls([tc], defaultMetadata, graph);
|
|
304
|
-
expect(tc.id).toBeDefined();
|
|
305
|
-
expect(tc.id.startsWith('toolu_')).toBe(true);
|
|
306
|
-
expect(graph.dispatchRunStep).toHaveBeenCalled();
|
|
307
|
-
});
|
|
308
|
-
it('flags messageStepHasToolCalls and dispatches TOOL_CALLS when prev step is MESSAGE_CREATION', async () => {
|
|
309
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
310
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
311
|
-
await handleToolCalls([makeToolCall('id-1')], defaultMetadata, graph);
|
|
312
|
-
expect(graph.messageStepHasToolCalls.get('prev-step-id')).toBe(true);
|
|
313
|
-
const calls = graph.dispatchRunStep.mock.calls;
|
|
314
|
-
expect(calls).toHaveLength(1);
|
|
315
|
-
expect(calls[0][1]).toEqual(expect.objectContaining({ type: StepTypes.TOOL_CALLS }));
|
|
316
|
-
});
|
|
317
|
-
it('creates MESSAGE_CREATION when no previous step exists', async () => {
|
|
318
|
-
graph.getStepIdByKey.mockImplementation(() => {
|
|
319
|
-
throw new Error('no step');
|
|
320
|
-
});
|
|
321
|
-
await handleToolCalls([makeToolCall('id-1')], defaultMetadata, graph);
|
|
322
|
-
const calls = graph.dispatchRunStep.mock.calls;
|
|
323
|
-
expect(calls).toHaveLength(2);
|
|
324
|
-
expect(calls[0][1]).toEqual(expect.objectContaining({ type: StepTypes.MESSAGE_CREATION }));
|
|
325
|
-
expect(calls[1][1]).toEqual(expect.objectContaining({ type: StepTypes.TOOL_CALLS }));
|
|
326
|
-
});
|
|
327
|
-
it('reuses empty TOOL_CALLS step exactly once', async () => {
|
|
328
|
-
const emptyToolStep = makeRunStep(StepTypes.TOOL_CALLS, {
|
|
329
|
-
id: 'empty-step',
|
|
330
|
-
tool_calls: [],
|
|
331
|
-
});
|
|
332
|
-
graph.getRunStep.mockReturnValue(emptyToolStep);
|
|
333
|
-
graph.getStepIdByKey.mockReturnValue('empty-step-id');
|
|
334
|
-
await handleToolCalls([makeToolCall('id-1'), makeToolCall('id-2')], defaultMetadata, graph);
|
|
335
|
-
expect(graph.toolCallStepIds.get('id-1')).toBe('empty-step-id');
|
|
336
|
-
const calls = graph.dispatchRunStep.mock.calls;
|
|
337
|
-
expect(calls).toHaveLength(1);
|
|
338
|
-
expect(calls[0][1]).toEqual(expect.objectContaining({
|
|
339
|
-
type: StepTypes.TOOL_CALLS,
|
|
340
|
-
tool_calls: [expect.objectContaining({ id: 'id-2' })],
|
|
341
|
-
}));
|
|
342
|
-
});
|
|
343
|
-
it('gives each parallel tool call its own step (3 tool calls)', async () => {
|
|
344
|
-
const emptyToolStep = makeRunStep(StepTypes.TOOL_CALLS, { tool_calls: [] });
|
|
345
|
-
graph.getStepIdByKey.mockReturnValue('chunk-step-id');
|
|
346
|
-
let callCount = 0;
|
|
347
|
-
graph.getRunStep.mockImplementation(() => {
|
|
348
|
-
if (callCount === 0) {
|
|
349
|
-
callCount++;
|
|
350
|
-
return emptyToolStep;
|
|
351
|
-
}
|
|
352
|
-
return makeRunStep(StepTypes.TOOL_CALLS, {
|
|
353
|
-
tool_calls: [
|
|
354
|
-
{
|
|
355
|
-
id: 'prev',
|
|
356
|
-
name: 'calc',
|
|
357
|
-
args: {},
|
|
358
|
-
type: 'tool_call',
|
|
359
|
-
},
|
|
360
|
-
],
|
|
361
|
-
});
|
|
362
|
-
});
|
|
363
|
-
await handleToolCalls([makeToolCall('id-1'), makeToolCall('id-2'), makeToolCall('id-3')], defaultMetadata, graph);
|
|
364
|
-
expect(graph.toolCallStepIds.get('id-1')).toBe('chunk-step-id');
|
|
365
|
-
const calls = graph.dispatchRunStep.mock.calls;
|
|
366
|
-
expect(calls).toHaveLength(2);
|
|
367
|
-
expect(calls[0][1].tool_calls[0].id).toBe('id-2');
|
|
368
|
-
expect(calls[1][1].tool_calls[0].id).toBe('id-3');
|
|
369
|
-
});
|
|
370
|
-
it('never creates MESSAGE_CREATION for parallel tool calls after TOOL_CALLS prev', async () => {
|
|
371
|
-
const emptyToolStep = makeRunStep(StepTypes.TOOL_CALLS, { tool_calls: [] });
|
|
372
|
-
graph.getStepIdByKey.mockReturnValue('chunk-step-id');
|
|
373
|
-
let callCount = 0;
|
|
374
|
-
graph.getRunStep.mockImplementation(() => {
|
|
375
|
-
if (callCount === 0) {
|
|
376
|
-
callCount++;
|
|
377
|
-
return emptyToolStep;
|
|
378
|
-
}
|
|
379
|
-
return makeRunStep(StepTypes.TOOL_CALLS, {
|
|
380
|
-
tool_calls: [
|
|
381
|
-
{
|
|
382
|
-
id: 'prev',
|
|
383
|
-
name: 'calc',
|
|
384
|
-
args: {},
|
|
385
|
-
type: 'tool_call',
|
|
386
|
-
},
|
|
387
|
-
],
|
|
388
|
-
});
|
|
389
|
-
});
|
|
390
|
-
await handleToolCalls([makeToolCall('id-1'), makeToolCall('id-2'), makeToolCall('id-3')], defaultMetadata, graph);
|
|
391
|
-
const msgCreationCalls = graph.dispatchRunStep.mock.calls.filter((call) => call[1].type === StepTypes.MESSAGE_CREATION);
|
|
392
|
-
expect(msgCreationCalls).toHaveLength(0);
|
|
393
|
-
});
|
|
394
|
-
it('dispatches new TOOL_CALLS directly when prev TOOL_CALLS has existing data', async () => {
|
|
395
|
-
const populatedToolStep = makeRunStep(StepTypes.TOOL_CALLS, {
|
|
396
|
-
tool_calls: [
|
|
397
|
-
{
|
|
398
|
-
id: 'existing',
|
|
399
|
-
name: 'calc',
|
|
400
|
-
args: {},
|
|
401
|
-
type: 'tool_call',
|
|
402
|
-
},
|
|
403
|
-
],
|
|
404
|
-
});
|
|
405
|
-
graph.getRunStep.mockReturnValue(populatedToolStep);
|
|
406
|
-
await handleToolCalls([makeToolCall('id-1')], defaultMetadata, graph);
|
|
407
|
-
const calls = graph.dispatchRunStep.mock.calls;
|
|
408
|
-
expect(calls).toHaveLength(1);
|
|
409
|
-
expect(calls[0][1]).toEqual(expect.objectContaining({ type: StepTypes.TOOL_CALLS }));
|
|
410
|
-
});
|
|
411
|
-
it('never dispatches empty text block with tool_call_ids (MESSAGE_CREATION path)', async () => {
|
|
412
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
413
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
414
|
-
await handleToolCalls([makeToolCall('id-1')], defaultMetadata, graph);
|
|
415
|
-
for (const call of graph.dispatchRunStep.mock.calls) {
|
|
416
|
-
const stepDetails = call[1];
|
|
417
|
-
if (stepDetails.type === StepTypes.TOOL_CALLS) {
|
|
418
|
-
expect(stepDetails).not.toHaveProperty('content');
|
|
419
|
-
expect(stepDetails).not.toHaveProperty('text');
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
});
|
|
423
|
-
it('never dispatches empty text block with tool_call_ids (no prev step path)', async () => {
|
|
424
|
-
graph.getStepIdByKey.mockImplementation(() => {
|
|
425
|
-
throw new Error('no step');
|
|
426
|
-
});
|
|
427
|
-
await handleToolCalls([makeToolCall('id-1')], defaultMetadata, graph);
|
|
428
|
-
for (const call of graph.dispatchRunStep.mock.calls) {
|
|
429
|
-
const stepDetails = call[1];
|
|
430
|
-
if (stepDetails.type === StepTypes.TOOL_CALLS) {
|
|
431
|
-
expect(stepDetails).not.toHaveProperty('content');
|
|
432
|
-
expect(stepDetails).not.toHaveProperty('text');
|
|
433
|
-
}
|
|
434
|
-
if (stepDetails.type === StepTypes.MESSAGE_CREATION) {
|
|
435
|
-
const msgDetails = stepDetails;
|
|
436
|
-
expect(msgDetails.message_creation.message_id).toBeDefined();
|
|
437
|
-
expect(msgDetails).not.toHaveProperty('tool_call_ids');
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
});
|
|
441
|
-
});
|
|
442
|
-
describe('handleToolCallChunks + handleToolCalls integration', () => {
|
|
443
|
-
let graph;
|
|
444
|
-
const stepKey = 'step-key';
|
|
445
|
-
beforeEach(() => {
|
|
446
|
-
graph = createMockGraph();
|
|
447
|
-
});
|
|
448
|
-
it('Bedrock single tool: chunks create empty TOOL_CALLS, then handleToolCalls reuses it', async () => {
|
|
449
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
450
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
451
|
-
await handleToolCallChunks({
|
|
452
|
-
graph: graph,
|
|
453
|
-
stepKey,
|
|
454
|
-
toolCallChunks: [makeToolCallChunk({ index: 2 })],
|
|
455
|
-
metadata: defaultMetadata,
|
|
456
|
-
});
|
|
457
|
-
const chunkStepId = graph.dispatchRunStep.mock.results[0].value;
|
|
458
|
-
const resolvedChunkStepId = await chunkStepId;
|
|
459
|
-
const emptyToolStep = makeRunStep(StepTypes.TOOL_CALLS, { tool_calls: [] });
|
|
460
|
-
graph.getRunStep.mockReturnValue(emptyToolStep);
|
|
461
|
-
graph.getStepIdByKey.mockReturnValue(resolvedChunkStepId);
|
|
462
|
-
graph.dispatchRunStep.mockClear();
|
|
463
|
-
await handleToolCalls([makeToolCall('tooluse_abc')], defaultMetadata, graph);
|
|
464
|
-
expect(graph.toolCallStepIds.get('tooluse_abc')).toBe(resolvedChunkStepId);
|
|
465
|
-
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
466
|
-
});
|
|
467
|
-
it('Bedrock parallel: 3 chunks then 3 tool calls yields 3 unique step IDs', async () => {
|
|
468
|
-
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
469
|
-
graph.getRunStep.mockReturnValue(msgStep);
|
|
470
|
-
await handleToolCallChunks({
|
|
471
|
-
graph: graph,
|
|
472
|
-
stepKey,
|
|
473
|
-
toolCallChunks: [makeToolCallChunk({ index: 2 })],
|
|
474
|
-
metadata: defaultMetadata,
|
|
475
|
-
});
|
|
476
|
-
const chunkStepId = await graph.dispatchRunStep.mock.results[0]
|
|
477
|
-
.value;
|
|
478
|
-
const emptyToolStep = makeRunStep(StepTypes.TOOL_CALLS, { tool_calls: [] });
|
|
479
|
-
graph.getStepIdByKey.mockReturnValue(chunkStepId);
|
|
480
|
-
let callIdx = 0;
|
|
481
|
-
graph.getRunStep.mockImplementation(() => {
|
|
482
|
-
if (callIdx === 0) {
|
|
483
|
-
callIdx++;
|
|
484
|
-
return emptyToolStep;
|
|
485
|
-
}
|
|
486
|
-
return makeRunStep(StepTypes.TOOL_CALLS, {
|
|
487
|
-
tool_calls: [
|
|
488
|
-
{
|
|
489
|
-
id: 'prev',
|
|
490
|
-
name: 'calc',
|
|
491
|
-
args: {},
|
|
492
|
-
type: 'tool_call',
|
|
493
|
-
},
|
|
494
|
-
],
|
|
495
|
-
});
|
|
496
|
-
});
|
|
497
|
-
graph.dispatchRunStep.mockClear();
|
|
498
|
-
let newStepCounter = 10;
|
|
499
|
-
graph.dispatchRunStep.mockImplementation(async () => `new-step-${++newStepCounter}`);
|
|
500
|
-
await handleToolCalls([makeToolCall('id-1'), makeToolCall('id-2'), makeToolCall('id-3')], defaultMetadata, graph);
|
|
501
|
-
expect(graph.toolCallStepIds.get('id-1')).toBe(chunkStepId);
|
|
502
|
-
const dispatchedIds = graph.dispatchRunStep.mock.calls.map((_, i) => graph.dispatchRunStep.mock.results[i].value);
|
|
503
|
-
expect(dispatchedIds).toHaveLength(2);
|
|
504
|
-
const allStepIds = new Set([
|
|
505
|
-
chunkStepId,
|
|
506
|
-
graph.toolCallStepIds.get('id-1'),
|
|
507
|
-
...graph.dispatchRunStep.mock.calls.map((call) => {
|
|
508
|
-
const tc = call[1].tool_calls;
|
|
509
|
-
return tc?.[0]?.id;
|
|
510
|
-
}),
|
|
511
|
-
]);
|
|
512
|
-
expect(graph.toolCallStepIds.get('id-1')).toBe(chunkStepId);
|
|
513
|
-
expect(allStepIds.size).toBeGreaterThanOrEqual(2);
|
|
514
|
-
const msgCreationCalls = graph.dispatchRunStep.mock.calls.filter((call) => call[1].type === StepTypes.MESSAGE_CREATION);
|
|
515
|
-
expect(msgCreationCalls).toHaveLength(0);
|
|
516
|
-
});
|
|
517
|
-
});
|
|
518
|
-
describe('handleServerToolResult', () => {
|
|
519
|
-
let graph;
|
|
520
|
-
const anthropicContext = { provider: Providers.ANTHROPIC };
|
|
521
|
-
beforeEach(() => {
|
|
522
|
-
graph = createMockGraph();
|
|
523
|
-
});
|
|
524
|
-
it('returns false when provider is not Anthropic', async () => {
|
|
525
|
-
const result = await handleServerToolResult({
|
|
526
|
-
graph: graph,
|
|
527
|
-
content: [{ type: 'tool_result', tool_use_id: 'tu-1', content: 'ok' }],
|
|
528
|
-
agentContext: { provider: Providers.OPENAI },
|
|
529
|
-
});
|
|
530
|
-
expect(result).toBe(false);
|
|
531
|
-
});
|
|
532
|
-
it('returns false when content is a string', async () => {
|
|
533
|
-
const result = await handleServerToolResult({
|
|
534
|
-
graph: graph,
|
|
535
|
-
content: 'plain text',
|
|
536
|
-
agentContext: anthropicContext,
|
|
537
|
-
});
|
|
538
|
-
expect(result).toBe(false);
|
|
539
|
-
});
|
|
540
|
-
it('returns false when content is null/undefined', async () => {
|
|
541
|
-
const result = await handleServerToolResult({
|
|
542
|
-
graph: graph,
|
|
543
|
-
content: undefined,
|
|
544
|
-
agentContext: anthropicContext,
|
|
545
|
-
});
|
|
546
|
-
expect(result).toBe(false);
|
|
547
|
-
});
|
|
548
|
-
it('returns false when content is empty array', async () => {
|
|
549
|
-
const result = await handleServerToolResult({
|
|
550
|
-
graph: graph,
|
|
551
|
-
content: [],
|
|
552
|
-
agentContext: anthropicContext,
|
|
553
|
-
});
|
|
554
|
-
expect(result).toBe(false);
|
|
555
|
-
});
|
|
556
|
-
it('returns false when single content item has no tool_use_id', async () => {
|
|
557
|
-
const result = await handleServerToolResult({
|
|
558
|
-
graph: graph,
|
|
559
|
-
content: [{ type: 'tool_result', content: 'ok' }],
|
|
560
|
-
agentContext: anthropicContext,
|
|
561
|
-
});
|
|
562
|
-
expect(result).toBe(false);
|
|
563
|
-
});
|
|
564
|
-
it('skips content parts with empty tool_use_id', async () => {
|
|
565
|
-
const result = await handleServerToolResult({
|
|
566
|
-
graph: graph,
|
|
567
|
-
content: [
|
|
568
|
-
{ type: 'tool_result', tool_use_id: '', content: 'ok' },
|
|
569
|
-
{ type: 'tool_result', tool_use_id: 'tu-valid', content: 'ok' },
|
|
570
|
-
],
|
|
571
|
-
agentContext: anthropicContext,
|
|
572
|
-
});
|
|
573
|
-
expect(result).toBe(false);
|
|
574
|
-
});
|
|
575
|
-
it('warns and skips when toolCallStepIds has no mapping for tool_use_id', async () => {
|
|
576
|
-
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => { });
|
|
577
|
-
const result = await handleServerToolResult({
|
|
578
|
-
graph: graph,
|
|
579
|
-
content: [
|
|
580
|
-
{ type: 'tool_result', tool_use_id: 'tu-missing', content: 'ok' },
|
|
581
|
-
],
|
|
582
|
-
agentContext: anthropicContext,
|
|
583
|
-
});
|
|
584
|
-
expect(result).toBe(false);
|
|
585
|
-
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('tu-missing'));
|
|
586
|
-
warnSpy.mockRestore();
|
|
587
|
-
});
|
|
588
|
-
it('warns when run step does not exist for stepId', async () => {
|
|
589
|
-
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
590
|
-
graph.getRunStep.mockReturnValue(undefined);
|
|
591
|
-
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => { });
|
|
592
|
-
const result = await handleServerToolResult({
|
|
593
|
-
graph: graph,
|
|
594
|
-
content: [
|
|
595
|
-
{ type: 'tool_result', tool_use_id: 'tu-1', content: 'ok' },
|
|
596
|
-
],
|
|
597
|
-
agentContext: anthropicContext,
|
|
598
|
-
});
|
|
599
|
-
expect(result).toBe(false);
|
|
600
|
-
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('does not exist'));
|
|
601
|
-
warnSpy.mockRestore();
|
|
602
|
-
});
|
|
603
|
-
it('warns when run step is not a TOOL_CALLS type', async () => {
|
|
604
|
-
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
605
|
-
graph.getRunStep.mockReturnValue(makeRunStep(StepTypes.MESSAGE_CREATION));
|
|
606
|
-
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => { });
|
|
607
|
-
const result = await handleServerToolResult({
|
|
608
|
-
graph: graph,
|
|
609
|
-
content: [
|
|
610
|
-
{ type: 'tool_result', tool_use_id: 'tu-1', content: 'ok' },
|
|
611
|
-
],
|
|
612
|
-
agentContext: anthropicContext,
|
|
613
|
-
});
|
|
614
|
-
expect(result).toBe(false);
|
|
615
|
-
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('not a tool call step'));
|
|
616
|
-
warnSpy.mockRestore();
|
|
617
|
-
});
|
|
618
|
-
it('skips when no matching tool call found in step details', async () => {
|
|
619
|
-
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
620
|
-
graph.getRunStep.mockReturnValue(makeRunStep(StepTypes.TOOL_CALLS, {
|
|
621
|
-
tool_calls: [
|
|
622
|
-
{
|
|
623
|
-
id: 'tu-other',
|
|
624
|
-
name: 'calc',
|
|
625
|
-
args: {},
|
|
626
|
-
type: 'tool_call',
|
|
627
|
-
},
|
|
628
|
-
],
|
|
629
|
-
}));
|
|
630
|
-
const result = await handleServerToolResult({
|
|
631
|
-
graph: graph,
|
|
632
|
-
content: [
|
|
633
|
-
{ type: 'tool_result', tool_use_id: 'tu-1', content: 'ok' },
|
|
634
|
-
],
|
|
635
|
-
agentContext: anthropicContext,
|
|
636
|
-
});
|
|
637
|
-
expect(result).toBe(false);
|
|
638
|
-
});
|
|
639
|
-
it('returns true and sets skipHandling when a valid tool result is found', async () => {
|
|
640
|
-
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
641
|
-
graph.getRunStep.mockReturnValue(makeRunStep(StepTypes.TOOL_CALLS, {
|
|
642
|
-
tool_calls: [
|
|
643
|
-
{
|
|
644
|
-
id: 'tu-1',
|
|
645
|
-
name: 'calc',
|
|
646
|
-
args: {},
|
|
647
|
-
type: 'tool_call',
|
|
648
|
-
},
|
|
649
|
-
],
|
|
650
|
-
}));
|
|
651
|
-
const result = await handleServerToolResult({
|
|
652
|
-
graph: graph,
|
|
653
|
-
content: [
|
|
654
|
-
{ type: 'tool_result', tool_use_id: 'tu-1', content: 'ok' },
|
|
655
|
-
],
|
|
656
|
-
agentContext: anthropicContext,
|
|
657
|
-
});
|
|
658
|
-
expect(result).toBe(true);
|
|
659
|
-
});
|
|
660
|
-
it('calls handleAnthropicSearchResults for web_search_result type', async () => {
|
|
661
|
-
const mockToolEndHandle = jest
|
|
662
|
-
.fn()
|
|
663
|
-
.mockResolvedValue(undefined);
|
|
664
|
-
graph.handlerRegistry = {
|
|
665
|
-
getHandler: jest.fn().mockReturnValue({ handle: mockToolEndHandle }),
|
|
666
|
-
};
|
|
667
|
-
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
668
|
-
graph.getRunStep.mockReturnValue(makeRunStep(StepTypes.TOOL_CALLS, {
|
|
669
|
-
tool_calls: [
|
|
670
|
-
{
|
|
671
|
-
id: 'tu-1',
|
|
672
|
-
name: 'web_search',
|
|
673
|
-
args: { query: 'test' },
|
|
674
|
-
type: 'tool_call',
|
|
675
|
-
},
|
|
676
|
-
],
|
|
677
|
-
}));
|
|
678
|
-
const webSearchContent = {
|
|
679
|
-
type: 'web_search_result',
|
|
680
|
-
tool_use_id: 'tu-1',
|
|
681
|
-
content: [
|
|
682
|
-
{
|
|
683
|
-
type: 'web_search_result',
|
|
684
|
-
url: 'https://example.com',
|
|
685
|
-
title: 'Example',
|
|
686
|
-
encrypted_index: 'abc',
|
|
687
|
-
page_age: '2024-01-01',
|
|
688
|
-
},
|
|
689
|
-
],
|
|
690
|
-
};
|
|
691
|
-
const result = await handleServerToolResult({
|
|
692
|
-
graph: graph,
|
|
693
|
-
content: [webSearchContent],
|
|
694
|
-
metadata: defaultMetadata,
|
|
695
|
-
agentContext: anthropicContext,
|
|
696
|
-
});
|
|
697
|
-
expect(result).toBe(true);
|
|
698
|
-
expect(mockToolEndHandle).toHaveBeenCalledWith(GraphEvents.TOOL_END, expect.objectContaining({ input: { query: 'test' } }), defaultMetadata, graph);
|
|
699
|
-
expect(graph.invokedToolIds).toBeDefined();
|
|
700
|
-
expect(graph.invokedToolIds.has('tu-1')).toBe(true);
|
|
701
|
-
});
|
|
702
|
-
it('initializes invokedToolIds set when null', async () => {
|
|
703
|
-
const mockToolEndHandle = jest
|
|
704
|
-
.fn()
|
|
705
|
-
.mockResolvedValue(undefined);
|
|
706
|
-
graph.handlerRegistry = {
|
|
707
|
-
getHandler: jest.fn().mockReturnValue({ handle: mockToolEndHandle }),
|
|
708
|
-
};
|
|
709
|
-
graph.invokedToolIds = undefined;
|
|
710
|
-
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
711
|
-
graph.getRunStep.mockReturnValue(makeRunStep(StepTypes.TOOL_CALLS, {
|
|
712
|
-
tool_calls: [
|
|
713
|
-
{
|
|
714
|
-
id: 'tu-1',
|
|
715
|
-
name: 'web_search',
|
|
716
|
-
args: {},
|
|
717
|
-
type: 'tool_call',
|
|
718
|
-
},
|
|
719
|
-
],
|
|
720
|
-
}));
|
|
721
|
-
const webSearchContent = {
|
|
722
|
-
type: 'web_search_tool_result',
|
|
723
|
-
tool_use_id: 'tu-1',
|
|
724
|
-
content: [
|
|
725
|
-
{
|
|
726
|
-
type: 'web_search_result',
|
|
727
|
-
url: 'https://example.com',
|
|
728
|
-
title: 'Test',
|
|
729
|
-
encrypted_index: 'x',
|
|
730
|
-
},
|
|
731
|
-
],
|
|
732
|
-
};
|
|
733
|
-
await handleServerToolResult({
|
|
734
|
-
graph: graph,
|
|
735
|
-
content: [webSearchContent],
|
|
736
|
-
metadata: defaultMetadata,
|
|
737
|
-
agentContext: anthropicContext,
|
|
738
|
-
});
|
|
739
|
-
expect(graph.invokedToolIds).toBeInstanceOf(Set);
|
|
740
|
-
expect(graph.invokedToolIds.has('tu-1')).toBe(true);
|
|
741
|
-
});
|
|
742
|
-
it('warns when web search content is not an array', async () => {
|
|
743
|
-
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
744
|
-
graph.getRunStep.mockReturnValue(makeRunStep(StepTypes.TOOL_CALLS, {
|
|
745
|
-
tool_calls: [
|
|
746
|
-
{
|
|
747
|
-
id: 'tu-1',
|
|
748
|
-
name: 'web_search',
|
|
749
|
-
args: {},
|
|
750
|
-
type: 'tool_call',
|
|
751
|
-
},
|
|
752
|
-
],
|
|
753
|
-
}));
|
|
754
|
-
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => { });
|
|
755
|
-
const webSearchContent = {
|
|
756
|
-
type: 'web_search_result',
|
|
757
|
-
tool_use_id: 'tu-1',
|
|
758
|
-
content: 'not an array',
|
|
759
|
-
};
|
|
760
|
-
const result = await handleServerToolResult({
|
|
761
|
-
graph: graph,
|
|
762
|
-
content: [webSearchContent],
|
|
763
|
-
metadata: defaultMetadata,
|
|
764
|
-
agentContext: anthropicContext,
|
|
765
|
-
});
|
|
766
|
-
expect(result).toBe(true);
|
|
767
|
-
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Expected content to be an array'));
|
|
768
|
-
warnSpy.mockRestore();
|
|
769
|
-
});
|
|
770
|
-
it('warns when content is not an Anthropic web search result', async () => {
|
|
771
|
-
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
772
|
-
graph.getRunStep.mockReturnValue(makeRunStep(StepTypes.TOOL_CALLS, {
|
|
773
|
-
tool_calls: [
|
|
774
|
-
{
|
|
775
|
-
id: 'tu-1',
|
|
776
|
-
name: 'web_search',
|
|
777
|
-
args: {},
|
|
778
|
-
type: 'tool_call',
|
|
779
|
-
},
|
|
780
|
-
],
|
|
781
|
-
}));
|
|
782
|
-
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => { });
|
|
783
|
-
const webSearchContent = {
|
|
784
|
-
type: 'web_search_result',
|
|
785
|
-
tool_use_id: 'tu-1',
|
|
786
|
-
content: [{ type: 'text', text: 'not a search result' }],
|
|
787
|
-
};
|
|
788
|
-
const result = await handleServerToolResult({
|
|
789
|
-
graph: graph,
|
|
790
|
-
content: [webSearchContent],
|
|
791
|
-
metadata: defaultMetadata,
|
|
792
|
-
agentContext: anthropicContext,
|
|
793
|
-
});
|
|
794
|
-
expect(result).toBe(true);
|
|
795
|
-
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Expected content to be an Anthropic web search result'));
|
|
796
|
-
warnSpy.mockRestore();
|
|
797
|
-
});
|
|
798
|
-
});
|
|
799
|
-
//# sourceMappingURL=handlers.test.js.map
|