@librechat/agents 3.1.36 → 3.1.38
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/agents/AgentContext.cjs +3 -0
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +38 -29
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/stream.cjs +2 -1
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/tools/ToolNode.cjs +90 -14
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +25 -8
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +3 -0
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +38 -29
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/stream.mjs +2 -1
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/tools/ToolNode.mjs +90 -14
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +25 -8
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +2 -0
- package/dist/types/tools/ToolNode.d.ts +10 -0
- package/dist/types/types/tools.d.ts +7 -1
- package/package.json +1 -1
- package/src/agents/AgentContext.ts +3 -0
- package/src/graphs/Graph.ts +41 -36
- package/src/scripts/bedrock-content-aggregation-test.ts +265 -0
- package/src/scripts/bedrock-parallel-tools-test.ts +203 -0
- package/src/scripts/tools.ts +3 -12
- package/src/stream.ts +2 -1
- package/src/tools/ToolNode.ts +120 -14
- package/src/tools/__tests__/ToolNode.session.test.ts +465 -0
- package/src/tools/__tests__/handlers.test.ts +994 -0
- package/src/tools/handlers.ts +32 -13
- package/src/types/tools.ts +7 -1
|
@@ -0,0 +1,994 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, jest } from '@jest/globals';
|
|
2
|
+
import type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';
|
|
3
|
+
import type { StandardGraph } from '@/graphs';
|
|
4
|
+
import type { AgentContext } from '@/agents/AgentContext';
|
|
5
|
+
import type * as t from '@/types';
|
|
6
|
+
import { StepTypes, ToolCallTypes, Providers, GraphEvents } from '@/common';
|
|
7
|
+
import {
|
|
8
|
+
handleToolCallChunks,
|
|
9
|
+
handleToolCalls,
|
|
10
|
+
handleServerToolResult,
|
|
11
|
+
} from '../handlers';
|
|
12
|
+
|
|
13
|
+
type MockGraph = {
|
|
14
|
+
getStepKey: jest.Mock;
|
|
15
|
+
getStepIdByKey: jest.Mock;
|
|
16
|
+
getRunStep: jest.Mock;
|
|
17
|
+
dispatchRunStep: jest.Mock;
|
|
18
|
+
dispatchRunStepDelta: jest.Mock;
|
|
19
|
+
toolCallStepIds: Map<string, string>;
|
|
20
|
+
messageStepHasToolCalls: Map<string, boolean>;
|
|
21
|
+
messageIdsByStepKey: Map<string, string>;
|
|
22
|
+
prelimMessageIdsByStepKey: Map<string, string>;
|
|
23
|
+
invokedToolIds?: Set<string>;
|
|
24
|
+
handlerRegistry?: {
|
|
25
|
+
getHandler: jest.Mock;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
function createMockGraph(overrides?: Partial<MockGraph>): MockGraph {
|
|
30
|
+
let stepCounter = 0;
|
|
31
|
+
return {
|
|
32
|
+
getStepKey: jest.fn<() => string>().mockReturnValue('step-key'),
|
|
33
|
+
getStepIdByKey: jest.fn<() => string>().mockReturnValue('prev-step-id'),
|
|
34
|
+
getRunStep: jest
|
|
35
|
+
.fn<() => t.RunStep | undefined>()
|
|
36
|
+
.mockReturnValue(undefined),
|
|
37
|
+
dispatchRunStep: jest
|
|
38
|
+
.fn<() => Promise<string>>()
|
|
39
|
+
.mockImplementation(async () => `new-step-${++stepCounter}`),
|
|
40
|
+
dispatchRunStepDelta: jest
|
|
41
|
+
.fn<() => Promise<void>>()
|
|
42
|
+
.mockResolvedValue(undefined),
|
|
43
|
+
toolCallStepIds: new Map(),
|
|
44
|
+
messageStepHasToolCalls: new Map(),
|
|
45
|
+
messageIdsByStepKey: new Map(),
|
|
46
|
+
prelimMessageIdsByStepKey: new Map(),
|
|
47
|
+
invokedToolIds: undefined,
|
|
48
|
+
handlerRegistry: undefined,
|
|
49
|
+
...overrides,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function makeRunStep(
|
|
54
|
+
type: StepTypes,
|
|
55
|
+
opts?: { tool_calls?: t.AgentToolCall[]; id?: string; index?: number }
|
|
56
|
+
): t.RunStep {
|
|
57
|
+
const stepDetails: t.StepDetails =
|
|
58
|
+
type === StepTypes.MESSAGE_CREATION
|
|
59
|
+
? {
|
|
60
|
+
type: StepTypes.MESSAGE_CREATION,
|
|
61
|
+
message_creation: { message_id: 'msg-1' },
|
|
62
|
+
}
|
|
63
|
+
: { type: StepTypes.TOOL_CALLS, tool_calls: opts?.tool_calls ?? [] };
|
|
64
|
+
return {
|
|
65
|
+
type,
|
|
66
|
+
id: opts?.id ?? 'run-step-1',
|
|
67
|
+
index: opts?.index ?? 0,
|
|
68
|
+
stepDetails,
|
|
69
|
+
usage: null,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function makeToolCall(id: string, name = 'calculator'): ToolCall {
|
|
74
|
+
return { id, name, args: {}, type: 'tool_call' };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function makeToolCallChunk(opts?: {
|
|
78
|
+
id?: string;
|
|
79
|
+
name?: string;
|
|
80
|
+
index?: number;
|
|
81
|
+
}): ToolCallChunk {
|
|
82
|
+
return {
|
|
83
|
+
id: opts?.id,
|
|
84
|
+
name: opts?.name,
|
|
85
|
+
args: '',
|
|
86
|
+
index: opts?.index ?? 0,
|
|
87
|
+
type: 'tool_call_chunk',
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const defaultMetadata = { run_id: 'test-run' };
|
|
92
|
+
|
|
93
|
+
describe('handleToolCallChunks', () => {
|
|
94
|
+
let graph: MockGraph;
|
|
95
|
+
|
|
96
|
+
beforeEach(() => {
|
|
97
|
+
graph = createMockGraph();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('creates TOOL_CALLS step when previous step is MESSAGE_CREATION', async () => {
|
|
101
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
102
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
103
|
+
|
|
104
|
+
const chunks = [makeToolCallChunk({ index: 2 })];
|
|
105
|
+
await handleToolCallChunks({
|
|
106
|
+
graph: graph as unknown as StandardGraph,
|
|
107
|
+
stepKey: 'step-key',
|
|
108
|
+
toolCallChunks: chunks,
|
|
109
|
+
metadata: defaultMetadata,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const dispatchCalls = graph.dispatchRunStep.mock.calls;
|
|
113
|
+
expect(dispatchCalls).toHaveLength(1);
|
|
114
|
+
expect(dispatchCalls[0][1]).toEqual(
|
|
115
|
+
expect.objectContaining({ type: StepTypes.TOOL_CALLS })
|
|
116
|
+
);
|
|
117
|
+
expect(graph.messageStepHasToolCalls.has('prev-step-id')).toBe(true);
|
|
118
|
+
expect(graph.dispatchRunStepDelta).toHaveBeenCalledTimes(1);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('reuses existing TOOL_CALLS step without dispatching a new one', async () => {
|
|
122
|
+
const toolStep = makeRunStep(StepTypes.TOOL_CALLS);
|
|
123
|
+
graph.getRunStep.mockReturnValue(toolStep);
|
|
124
|
+
|
|
125
|
+
const chunks = [makeToolCallChunk({ index: 2 })];
|
|
126
|
+
await handleToolCallChunks({
|
|
127
|
+
graph: graph as unknown as StandardGraph,
|
|
128
|
+
stepKey: 'step-key',
|
|
129
|
+
toolCallChunks: chunks,
|
|
130
|
+
metadata: defaultMetadata,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
134
|
+
expect(graph.dispatchRunStepDelta).toHaveBeenCalledTimes(1);
|
|
135
|
+
expect(graph.dispatchRunStepDelta).toHaveBeenCalledWith(
|
|
136
|
+
'prev-step-id',
|
|
137
|
+
expect.objectContaining({ type: StepTypes.TOOL_CALLS })
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('creates MESSAGE_CREATION when no previous step exists', async () => {
|
|
142
|
+
let callCount = 0;
|
|
143
|
+
graph.getStepIdByKey.mockImplementation(() => {
|
|
144
|
+
callCount++;
|
|
145
|
+
if (callCount === 1) {
|
|
146
|
+
throw new Error('No step found');
|
|
147
|
+
}
|
|
148
|
+
return 'new-step-1';
|
|
149
|
+
});
|
|
150
|
+
graph.getRunStep.mockReturnValue(makeRunStep(StepTypes.MESSAGE_CREATION));
|
|
151
|
+
|
|
152
|
+
const chunks = [makeToolCallChunk({ index: 0 })];
|
|
153
|
+
await handleToolCallChunks({
|
|
154
|
+
graph: graph as unknown as StandardGraph,
|
|
155
|
+
stepKey: 'step-key',
|
|
156
|
+
toolCallChunks: chunks,
|
|
157
|
+
metadata: defaultMetadata,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const dispatchCalls = graph.dispatchRunStep.mock.calls;
|
|
161
|
+
expect(dispatchCalls.length).toBeGreaterThanOrEqual(2);
|
|
162
|
+
expect(dispatchCalls[0][1]).toEqual(
|
|
163
|
+
expect.objectContaining({ type: StepTypes.MESSAGE_CREATION })
|
|
164
|
+
);
|
|
165
|
+
expect(dispatchCalls[1][1]).toEqual(
|
|
166
|
+
expect.objectContaining({ type: StepTypes.TOOL_CALLS })
|
|
167
|
+
);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('skips TOOL_CALLS dispatch when already dispatched for this MESSAGE_CREATION', async () => {
|
|
171
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
172
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
173
|
+
graph.messageStepHasToolCalls.set('prev-step-id', true);
|
|
174
|
+
|
|
175
|
+
const chunks = [makeToolCallChunk({ index: 2 })];
|
|
176
|
+
await handleToolCallChunks({
|
|
177
|
+
graph: graph as unknown as StandardGraph,
|
|
178
|
+
stepKey: 'step-key',
|
|
179
|
+
toolCallChunks: chunks,
|
|
180
|
+
metadata: defaultMetadata,
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
184
|
+
expect(graph.dispatchRunStepDelta).toHaveBeenCalledTimes(1);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('sanitizes empty string id and name to undefined', async () => {
|
|
188
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
189
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
190
|
+
|
|
191
|
+
const chunk = makeToolCallChunk({ id: '', name: '' });
|
|
192
|
+
await handleToolCallChunks({
|
|
193
|
+
graph: graph as unknown as StandardGraph,
|
|
194
|
+
stepKey: 'step-key',
|
|
195
|
+
toolCallChunks: [chunk],
|
|
196
|
+
metadata: defaultMetadata,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
expect(chunk.id).toBeUndefined();
|
|
200
|
+
expect(chunk.name).toBeUndefined();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('populates tool_calls when chunk has valid id and name', async () => {
|
|
204
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
205
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
206
|
+
|
|
207
|
+
const chunks = [
|
|
208
|
+
makeToolCallChunk({ id: 'tooluse_abc', name: 'calculator', index: 2 }),
|
|
209
|
+
];
|
|
210
|
+
await handleToolCallChunks({
|
|
211
|
+
graph: graph as unknown as StandardGraph,
|
|
212
|
+
stepKey: 'step-key',
|
|
213
|
+
toolCallChunks: chunks,
|
|
214
|
+
metadata: defaultMetadata,
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
const toolCallsArg = graph.dispatchRunStep.mock
|
|
218
|
+
.calls[0][1] as t.ToolCallsDetails;
|
|
219
|
+
expect(toolCallsArg.tool_calls).toEqual([
|
|
220
|
+
expect.objectContaining({
|
|
221
|
+
id: 'tooluse_abc',
|
|
222
|
+
name: 'calculator',
|
|
223
|
+
type: ToolCallTypes.TOOL_CALL,
|
|
224
|
+
}),
|
|
225
|
+
]);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('never dispatches empty text block alongside TOOL_CALLS step', async () => {
|
|
229
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
230
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
231
|
+
|
|
232
|
+
const chunks = [
|
|
233
|
+
makeToolCallChunk({ id: 'tooluse_abc', name: 'calculator', index: 2 }),
|
|
234
|
+
];
|
|
235
|
+
await handleToolCallChunks({
|
|
236
|
+
graph: graph as unknown as StandardGraph,
|
|
237
|
+
stepKey: 'step-key',
|
|
238
|
+
toolCallChunks: chunks,
|
|
239
|
+
metadata: defaultMetadata,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
const allDispatches = graph.dispatchRunStep.mock.calls;
|
|
243
|
+
expect(allDispatches).toHaveLength(1);
|
|
244
|
+
const stepDetails = allDispatches[0][1] as t.StepDetails;
|
|
245
|
+
expect(stepDetails.type).toBe(StepTypes.TOOL_CALLS);
|
|
246
|
+
expect(stepDetails).not.toHaveProperty('content');
|
|
247
|
+
expect(stepDetails).not.toHaveProperty('text');
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('dispatches delta even when chunks lack id/name (Bedrock pattern)', async () => {
|
|
251
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
252
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
253
|
+
|
|
254
|
+
const chunks = [makeToolCallChunk({ index: 2 })];
|
|
255
|
+
await handleToolCallChunks({
|
|
256
|
+
graph: graph as unknown as StandardGraph,
|
|
257
|
+
stepKey: 'step-key',
|
|
258
|
+
toolCallChunks: chunks,
|
|
259
|
+
metadata: defaultMetadata,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
expect(graph.dispatchRunStepDelta).toHaveBeenCalledTimes(1);
|
|
263
|
+
const toolCallsArg = graph.dispatchRunStep.mock
|
|
264
|
+
.calls[0][1] as t.ToolCallsDetails;
|
|
265
|
+
expect(toolCallsArg.tool_calls).toEqual([]);
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
describe('handleToolCalls', () => {
|
|
270
|
+
let graph: MockGraph;
|
|
271
|
+
|
|
272
|
+
beforeEach(() => {
|
|
273
|
+
graph = createMockGraph();
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('returns early when metadata is missing', async () => {
|
|
277
|
+
await handleToolCalls(
|
|
278
|
+
[makeToolCall('id-1')],
|
|
279
|
+
undefined,
|
|
280
|
+
graph as unknown as StandardGraph
|
|
281
|
+
);
|
|
282
|
+
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('returns early when toolCalls is undefined', async () => {
|
|
286
|
+
await handleToolCalls(
|
|
287
|
+
undefined,
|
|
288
|
+
defaultMetadata,
|
|
289
|
+
graph as unknown as StandardGraph
|
|
290
|
+
);
|
|
291
|
+
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('returns early when toolCalls is empty', async () => {
|
|
295
|
+
await handleToolCalls(
|
|
296
|
+
[],
|
|
297
|
+
defaultMetadata,
|
|
298
|
+
graph as unknown as StandardGraph
|
|
299
|
+
);
|
|
300
|
+
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it('skips tool call when id already in toolCallStepIds', async () => {
|
|
304
|
+
graph.toolCallStepIds.set('id-1', 'existing-step');
|
|
305
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
306
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
307
|
+
|
|
308
|
+
await handleToolCalls(
|
|
309
|
+
[makeToolCall('id-1')],
|
|
310
|
+
defaultMetadata,
|
|
311
|
+
graph as unknown as StandardGraph
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it('assigns fallback id when tool_call.id is undefined', async () => {
|
|
318
|
+
const tc: ToolCall = {
|
|
319
|
+
id: undefined as unknown as string,
|
|
320
|
+
name: 'calc',
|
|
321
|
+
args: {},
|
|
322
|
+
type: 'tool_call',
|
|
323
|
+
};
|
|
324
|
+
graph.getStepIdByKey.mockImplementation(() => {
|
|
325
|
+
throw new Error('no step');
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
await handleToolCalls(
|
|
329
|
+
[tc],
|
|
330
|
+
defaultMetadata,
|
|
331
|
+
graph as unknown as StandardGraph
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
expect(tc.id).toBeDefined();
|
|
335
|
+
expect(tc.id!.startsWith('toolu_')).toBe(true);
|
|
336
|
+
expect(graph.dispatchRunStep).toHaveBeenCalled();
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it('flags messageStepHasToolCalls and dispatches TOOL_CALLS when prev step is MESSAGE_CREATION', async () => {
|
|
340
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
341
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
342
|
+
|
|
343
|
+
await handleToolCalls(
|
|
344
|
+
[makeToolCall('id-1')],
|
|
345
|
+
defaultMetadata,
|
|
346
|
+
graph as unknown as StandardGraph
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
expect(graph.messageStepHasToolCalls.get('prev-step-id')).toBe(true);
|
|
350
|
+
const calls = graph.dispatchRunStep.mock.calls;
|
|
351
|
+
expect(calls).toHaveLength(1);
|
|
352
|
+
expect(calls[0][1]).toEqual(
|
|
353
|
+
expect.objectContaining({ type: StepTypes.TOOL_CALLS })
|
|
354
|
+
);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it('creates MESSAGE_CREATION when no previous step exists', async () => {
|
|
358
|
+
graph.getStepIdByKey.mockImplementation(() => {
|
|
359
|
+
throw new Error('no step');
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
await handleToolCalls(
|
|
363
|
+
[makeToolCall('id-1')],
|
|
364
|
+
defaultMetadata,
|
|
365
|
+
graph as unknown as StandardGraph
|
|
366
|
+
);
|
|
367
|
+
|
|
368
|
+
const calls = graph.dispatchRunStep.mock.calls;
|
|
369
|
+
expect(calls).toHaveLength(2);
|
|
370
|
+
expect(calls[0][1]).toEqual(
|
|
371
|
+
expect.objectContaining({ type: StepTypes.MESSAGE_CREATION })
|
|
372
|
+
);
|
|
373
|
+
expect(calls[1][1]).toEqual(
|
|
374
|
+
expect.objectContaining({ type: StepTypes.TOOL_CALLS })
|
|
375
|
+
);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it('reuses empty TOOL_CALLS step exactly once', async () => {
|
|
379
|
+
const emptyToolStep = makeRunStep(StepTypes.TOOL_CALLS, {
|
|
380
|
+
id: 'empty-step',
|
|
381
|
+
tool_calls: [],
|
|
382
|
+
});
|
|
383
|
+
graph.getRunStep.mockReturnValue(emptyToolStep);
|
|
384
|
+
graph.getStepIdByKey.mockReturnValue('empty-step-id');
|
|
385
|
+
|
|
386
|
+
await handleToolCalls(
|
|
387
|
+
[makeToolCall('id-1'), makeToolCall('id-2')],
|
|
388
|
+
defaultMetadata,
|
|
389
|
+
graph as unknown as StandardGraph
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
expect(graph.toolCallStepIds.get('id-1')).toBe('empty-step-id');
|
|
393
|
+
|
|
394
|
+
const calls = graph.dispatchRunStep.mock.calls;
|
|
395
|
+
expect(calls).toHaveLength(1);
|
|
396
|
+
expect(calls[0][1]).toEqual(
|
|
397
|
+
expect.objectContaining({
|
|
398
|
+
type: StepTypes.TOOL_CALLS,
|
|
399
|
+
tool_calls: [expect.objectContaining({ id: 'id-2' })],
|
|
400
|
+
})
|
|
401
|
+
);
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
it('gives each parallel tool call its own step (3 tool calls)', async () => {
|
|
405
|
+
const emptyToolStep = makeRunStep(StepTypes.TOOL_CALLS, { tool_calls: [] });
|
|
406
|
+
graph.getStepIdByKey.mockReturnValue('chunk-step-id');
|
|
407
|
+
|
|
408
|
+
let callCount = 0;
|
|
409
|
+
graph.getRunStep.mockImplementation(() => {
|
|
410
|
+
if (callCount === 0) {
|
|
411
|
+
callCount++;
|
|
412
|
+
return emptyToolStep;
|
|
413
|
+
}
|
|
414
|
+
return makeRunStep(StepTypes.TOOL_CALLS, {
|
|
415
|
+
tool_calls: [
|
|
416
|
+
{
|
|
417
|
+
id: 'prev',
|
|
418
|
+
name: 'calc',
|
|
419
|
+
args: {},
|
|
420
|
+
type: 'tool_call',
|
|
421
|
+
} as t.AgentToolCall,
|
|
422
|
+
],
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
await handleToolCalls(
|
|
427
|
+
[makeToolCall('id-1'), makeToolCall('id-2'), makeToolCall('id-3')],
|
|
428
|
+
defaultMetadata,
|
|
429
|
+
graph as unknown as StandardGraph
|
|
430
|
+
);
|
|
431
|
+
|
|
432
|
+
expect(graph.toolCallStepIds.get('id-1')).toBe('chunk-step-id');
|
|
433
|
+
|
|
434
|
+
const calls = graph.dispatchRunStep.mock.calls;
|
|
435
|
+
expect(calls).toHaveLength(2);
|
|
436
|
+
expect((calls[0][1] as t.ToolCallsDetails).tool_calls![0].id).toBe('id-2');
|
|
437
|
+
expect((calls[1][1] as t.ToolCallsDetails).tool_calls![0].id).toBe('id-3');
|
|
438
|
+
});
|
|
439
|
+
|
|
440
|
+
it('never creates MESSAGE_CREATION for parallel tool calls after TOOL_CALLS prev', async () => {
|
|
441
|
+
const emptyToolStep = makeRunStep(StepTypes.TOOL_CALLS, { tool_calls: [] });
|
|
442
|
+
graph.getStepIdByKey.mockReturnValue('chunk-step-id');
|
|
443
|
+
|
|
444
|
+
let callCount = 0;
|
|
445
|
+
graph.getRunStep.mockImplementation(() => {
|
|
446
|
+
if (callCount === 0) {
|
|
447
|
+
callCount++;
|
|
448
|
+
return emptyToolStep;
|
|
449
|
+
}
|
|
450
|
+
return makeRunStep(StepTypes.TOOL_CALLS, {
|
|
451
|
+
tool_calls: [
|
|
452
|
+
{
|
|
453
|
+
id: 'prev',
|
|
454
|
+
name: 'calc',
|
|
455
|
+
args: {},
|
|
456
|
+
type: 'tool_call',
|
|
457
|
+
} as t.AgentToolCall,
|
|
458
|
+
],
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
await handleToolCalls(
|
|
463
|
+
[makeToolCall('id-1'), makeToolCall('id-2'), makeToolCall('id-3')],
|
|
464
|
+
defaultMetadata,
|
|
465
|
+
graph as unknown as StandardGraph
|
|
466
|
+
);
|
|
467
|
+
|
|
468
|
+
const msgCreationCalls = graph.dispatchRunStep.mock.calls.filter(
|
|
469
|
+
(call) => (call[1] as t.StepDetails).type === StepTypes.MESSAGE_CREATION
|
|
470
|
+
);
|
|
471
|
+
expect(msgCreationCalls).toHaveLength(0);
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
it('dispatches new TOOL_CALLS directly when prev TOOL_CALLS has existing data', async () => {
|
|
475
|
+
const populatedToolStep = makeRunStep(StepTypes.TOOL_CALLS, {
|
|
476
|
+
tool_calls: [
|
|
477
|
+
{
|
|
478
|
+
id: 'existing',
|
|
479
|
+
name: 'calc',
|
|
480
|
+
args: {},
|
|
481
|
+
type: 'tool_call',
|
|
482
|
+
} as t.AgentToolCall,
|
|
483
|
+
],
|
|
484
|
+
});
|
|
485
|
+
graph.getRunStep.mockReturnValue(populatedToolStep);
|
|
486
|
+
|
|
487
|
+
await handleToolCalls(
|
|
488
|
+
[makeToolCall('id-1')],
|
|
489
|
+
defaultMetadata,
|
|
490
|
+
graph as unknown as StandardGraph
|
|
491
|
+
);
|
|
492
|
+
|
|
493
|
+
const calls = graph.dispatchRunStep.mock.calls;
|
|
494
|
+
expect(calls).toHaveLength(1);
|
|
495
|
+
expect(calls[0][1]).toEqual(
|
|
496
|
+
expect.objectContaining({ type: StepTypes.TOOL_CALLS })
|
|
497
|
+
);
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
it('never dispatches empty text block with tool_call_ids (MESSAGE_CREATION path)', async () => {
|
|
501
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
502
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
503
|
+
|
|
504
|
+
await handleToolCalls(
|
|
505
|
+
[makeToolCall('id-1')],
|
|
506
|
+
defaultMetadata,
|
|
507
|
+
graph as unknown as StandardGraph
|
|
508
|
+
);
|
|
509
|
+
|
|
510
|
+
for (const call of graph.dispatchRunStep.mock.calls) {
|
|
511
|
+
const stepDetails = call[1] as t.StepDetails;
|
|
512
|
+
if (stepDetails.type === StepTypes.TOOL_CALLS) {
|
|
513
|
+
expect(stepDetails).not.toHaveProperty('content');
|
|
514
|
+
expect(stepDetails).not.toHaveProperty('text');
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it('never dispatches empty text block with tool_call_ids (no prev step path)', async () => {
|
|
520
|
+
graph.getStepIdByKey.mockImplementation(() => {
|
|
521
|
+
throw new Error('no step');
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
await handleToolCalls(
|
|
525
|
+
[makeToolCall('id-1')],
|
|
526
|
+
defaultMetadata,
|
|
527
|
+
graph as unknown as StandardGraph
|
|
528
|
+
);
|
|
529
|
+
|
|
530
|
+
for (const call of graph.dispatchRunStep.mock.calls) {
|
|
531
|
+
const stepDetails = call[1] as t.StepDetails;
|
|
532
|
+
if (stepDetails.type === StepTypes.TOOL_CALLS) {
|
|
533
|
+
expect(stepDetails).not.toHaveProperty('content');
|
|
534
|
+
expect(stepDetails).not.toHaveProperty('text');
|
|
535
|
+
}
|
|
536
|
+
if (stepDetails.type === StepTypes.MESSAGE_CREATION) {
|
|
537
|
+
const msgDetails = stepDetails as t.MessageCreationDetails;
|
|
538
|
+
expect(msgDetails.message_creation.message_id).toBeDefined();
|
|
539
|
+
expect(msgDetails).not.toHaveProperty('tool_call_ids');
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
describe('handleToolCallChunks + handleToolCalls integration', () => {
|
|
546
|
+
let graph: MockGraph;
|
|
547
|
+
const stepKey = 'step-key';
|
|
548
|
+
|
|
549
|
+
beforeEach(() => {
|
|
550
|
+
graph = createMockGraph();
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it('Bedrock single tool: chunks create empty TOOL_CALLS, then handleToolCalls reuses it', async () => {
|
|
554
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
555
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
556
|
+
|
|
557
|
+
await handleToolCallChunks({
|
|
558
|
+
graph: graph as unknown as StandardGraph,
|
|
559
|
+
stepKey,
|
|
560
|
+
toolCallChunks: [makeToolCallChunk({ index: 2 })],
|
|
561
|
+
metadata: defaultMetadata,
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
const chunkStepId = graph.dispatchRunStep.mock.results[0].value as string;
|
|
565
|
+
const resolvedChunkStepId = await chunkStepId;
|
|
566
|
+
|
|
567
|
+
const emptyToolStep = makeRunStep(StepTypes.TOOL_CALLS, { tool_calls: [] });
|
|
568
|
+
graph.getRunStep.mockReturnValue(emptyToolStep);
|
|
569
|
+
graph.getStepIdByKey.mockReturnValue(resolvedChunkStepId);
|
|
570
|
+
graph.dispatchRunStep.mockClear();
|
|
571
|
+
|
|
572
|
+
await handleToolCalls(
|
|
573
|
+
[makeToolCall('tooluse_abc')],
|
|
574
|
+
defaultMetadata,
|
|
575
|
+
graph as unknown as StandardGraph
|
|
576
|
+
);
|
|
577
|
+
|
|
578
|
+
expect(graph.toolCallStepIds.get('tooluse_abc')).toBe(resolvedChunkStepId);
|
|
579
|
+
expect(graph.dispatchRunStep).not.toHaveBeenCalled();
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
it('Bedrock parallel: 3 chunks then 3 tool calls yields 3 unique step IDs', async () => {
|
|
583
|
+
const msgStep = makeRunStep(StepTypes.MESSAGE_CREATION);
|
|
584
|
+
graph.getRunStep.mockReturnValue(msgStep);
|
|
585
|
+
|
|
586
|
+
await handleToolCallChunks({
|
|
587
|
+
graph: graph as unknown as StandardGraph,
|
|
588
|
+
stepKey,
|
|
589
|
+
toolCallChunks: [makeToolCallChunk({ index: 2 })],
|
|
590
|
+
metadata: defaultMetadata,
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
const chunkStepId = await (graph.dispatchRunStep.mock.results[0]
|
|
594
|
+
.value as Promise<string>);
|
|
595
|
+
|
|
596
|
+
const emptyToolStep = makeRunStep(StepTypes.TOOL_CALLS, { tool_calls: [] });
|
|
597
|
+
graph.getStepIdByKey.mockReturnValue(chunkStepId);
|
|
598
|
+
|
|
599
|
+
let callIdx = 0;
|
|
600
|
+
graph.getRunStep.mockImplementation(() => {
|
|
601
|
+
if (callIdx === 0) {
|
|
602
|
+
callIdx++;
|
|
603
|
+
return emptyToolStep;
|
|
604
|
+
}
|
|
605
|
+
return makeRunStep(StepTypes.TOOL_CALLS, {
|
|
606
|
+
tool_calls: [
|
|
607
|
+
{
|
|
608
|
+
id: 'prev',
|
|
609
|
+
name: 'calc',
|
|
610
|
+
args: {},
|
|
611
|
+
type: 'tool_call',
|
|
612
|
+
} as t.AgentToolCall,
|
|
613
|
+
],
|
|
614
|
+
});
|
|
615
|
+
});
|
|
616
|
+
graph.dispatchRunStep.mockClear();
|
|
617
|
+
|
|
618
|
+
let newStepCounter = 10;
|
|
619
|
+
graph.dispatchRunStep.mockImplementation(
|
|
620
|
+
async () => `new-step-${++newStepCounter}`
|
|
621
|
+
);
|
|
622
|
+
|
|
623
|
+
await handleToolCalls(
|
|
624
|
+
[makeToolCall('id-1'), makeToolCall('id-2'), makeToolCall('id-3')],
|
|
625
|
+
defaultMetadata,
|
|
626
|
+
graph as unknown as StandardGraph
|
|
627
|
+
);
|
|
628
|
+
|
|
629
|
+
expect(graph.toolCallStepIds.get('id-1')).toBe(chunkStepId);
|
|
630
|
+
|
|
631
|
+
const dispatchedIds = graph.dispatchRunStep.mock.calls.map(
|
|
632
|
+
(_, i) => graph.dispatchRunStep.mock.results[i].value
|
|
633
|
+
);
|
|
634
|
+
expect(dispatchedIds).toHaveLength(2);
|
|
635
|
+
|
|
636
|
+
const allStepIds = new Set([
|
|
637
|
+
chunkStepId,
|
|
638
|
+
graph.toolCallStepIds.get('id-1'),
|
|
639
|
+
...graph.dispatchRunStep.mock.calls.map((call) => {
|
|
640
|
+
const tc = (call[1] as t.ToolCallsDetails).tool_calls;
|
|
641
|
+
return tc?.[0]?.id;
|
|
642
|
+
}),
|
|
643
|
+
]);
|
|
644
|
+
|
|
645
|
+
expect(graph.toolCallStepIds.get('id-1')).toBe(chunkStepId);
|
|
646
|
+
expect(allStepIds.size).toBeGreaterThanOrEqual(2);
|
|
647
|
+
|
|
648
|
+
const msgCreationCalls = graph.dispatchRunStep.mock.calls.filter(
|
|
649
|
+
(call) => (call[1] as t.StepDetails).type === StepTypes.MESSAGE_CREATION
|
|
650
|
+
);
|
|
651
|
+
expect(msgCreationCalls).toHaveLength(0);
|
|
652
|
+
});
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
describe('handleServerToolResult', () => {
|
|
656
|
+
let graph: MockGraph;
|
|
657
|
+
const anthropicContext = { provider: Providers.ANTHROPIC } as AgentContext;
|
|
658
|
+
|
|
659
|
+
beforeEach(() => {
|
|
660
|
+
graph = createMockGraph();
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
it('returns false when provider is not Anthropic', async () => {
|
|
664
|
+
const result = await handleServerToolResult({
|
|
665
|
+
graph: graph as unknown as StandardGraph,
|
|
666
|
+
content: [{ type: 'tool_result', tool_use_id: 'tu-1', content: 'ok' }],
|
|
667
|
+
agentContext: { provider: Providers.OPENAI } as AgentContext,
|
|
668
|
+
});
|
|
669
|
+
expect(result).toBe(false);
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
it('returns false when content is a string', async () => {
|
|
673
|
+
const result = await handleServerToolResult({
|
|
674
|
+
graph: graph as unknown as StandardGraph,
|
|
675
|
+
content: 'plain text',
|
|
676
|
+
agentContext: anthropicContext,
|
|
677
|
+
});
|
|
678
|
+
expect(result).toBe(false);
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
it('returns false when content is null/undefined', async () => {
|
|
682
|
+
const result = await handleServerToolResult({
|
|
683
|
+
graph: graph as unknown as StandardGraph,
|
|
684
|
+
content: undefined,
|
|
685
|
+
agentContext: anthropicContext,
|
|
686
|
+
});
|
|
687
|
+
expect(result).toBe(false);
|
|
688
|
+
});
|
|
689
|
+
|
|
690
|
+
it('returns false when content is empty array', async () => {
|
|
691
|
+
const result = await handleServerToolResult({
|
|
692
|
+
graph: graph as unknown as StandardGraph,
|
|
693
|
+
content: [],
|
|
694
|
+
agentContext: anthropicContext,
|
|
695
|
+
});
|
|
696
|
+
expect(result).toBe(false);
|
|
697
|
+
});
|
|
698
|
+
|
|
699
|
+
it('returns false when single content item has no tool_use_id', async () => {
|
|
700
|
+
const result = await handleServerToolResult({
|
|
701
|
+
graph: graph as unknown as StandardGraph,
|
|
702
|
+
content: [{ type: 'tool_result', content: 'ok' } as t.ToolResultContent],
|
|
703
|
+
agentContext: anthropicContext,
|
|
704
|
+
});
|
|
705
|
+
expect(result).toBe(false);
|
|
706
|
+
});
|
|
707
|
+
|
|
708
|
+
it('skips content parts with empty tool_use_id', async () => {
|
|
709
|
+
const result = await handleServerToolResult({
|
|
710
|
+
graph: graph as unknown as StandardGraph,
|
|
711
|
+
content: [
|
|
712
|
+
{ type: 'tool_result', tool_use_id: '', content: 'ok' },
|
|
713
|
+
{ type: 'tool_result', tool_use_id: 'tu-valid', content: 'ok' },
|
|
714
|
+
] as t.MessageContentComplex[],
|
|
715
|
+
agentContext: anthropicContext,
|
|
716
|
+
});
|
|
717
|
+
expect(result).toBe(false);
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
it('warns and skips when toolCallStepIds has no mapping for tool_use_id', async () => {
|
|
721
|
+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
722
|
+
const result = await handleServerToolResult({
|
|
723
|
+
graph: graph as unknown as StandardGraph,
|
|
724
|
+
content: [
|
|
725
|
+
{ type: 'tool_result', tool_use_id: 'tu-missing', content: 'ok' },
|
|
726
|
+
] as t.MessageContentComplex[],
|
|
727
|
+
agentContext: anthropicContext,
|
|
728
|
+
});
|
|
729
|
+
expect(result).toBe(false);
|
|
730
|
+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('tu-missing'));
|
|
731
|
+
warnSpy.mockRestore();
|
|
732
|
+
});
|
|
733
|
+
|
|
734
|
+
it('warns when run step does not exist for stepId', async () => {
|
|
735
|
+
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
736
|
+
graph.getRunStep.mockReturnValue(undefined);
|
|
737
|
+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
738
|
+
|
|
739
|
+
const result = await handleServerToolResult({
|
|
740
|
+
graph: graph as unknown as StandardGraph,
|
|
741
|
+
content: [
|
|
742
|
+
{ type: 'tool_result', tool_use_id: 'tu-1', content: 'ok' },
|
|
743
|
+
] as t.MessageContentComplex[],
|
|
744
|
+
agentContext: anthropicContext,
|
|
745
|
+
});
|
|
746
|
+
expect(result).toBe(false);
|
|
747
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
748
|
+
expect.stringContaining('does not exist')
|
|
749
|
+
);
|
|
750
|
+
warnSpy.mockRestore();
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
it('warns when run step is not a TOOL_CALLS type', async () => {
|
|
754
|
+
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
755
|
+
graph.getRunStep.mockReturnValue(makeRunStep(StepTypes.MESSAGE_CREATION));
|
|
756
|
+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
757
|
+
|
|
758
|
+
const result = await handleServerToolResult({
|
|
759
|
+
graph: graph as unknown as StandardGraph,
|
|
760
|
+
content: [
|
|
761
|
+
{ type: 'tool_result', tool_use_id: 'tu-1', content: 'ok' },
|
|
762
|
+
] as t.MessageContentComplex[],
|
|
763
|
+
agentContext: anthropicContext,
|
|
764
|
+
});
|
|
765
|
+
expect(result).toBe(false);
|
|
766
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
767
|
+
expect.stringContaining('not a tool call step')
|
|
768
|
+
);
|
|
769
|
+
warnSpy.mockRestore();
|
|
770
|
+
});
|
|
771
|
+
|
|
772
|
+
it('skips when no matching tool call found in step details', async () => {
|
|
773
|
+
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
774
|
+
graph.getRunStep.mockReturnValue(
|
|
775
|
+
makeRunStep(StepTypes.TOOL_CALLS, {
|
|
776
|
+
tool_calls: [
|
|
777
|
+
{
|
|
778
|
+
id: 'tu-other',
|
|
779
|
+
name: 'calc',
|
|
780
|
+
args: {},
|
|
781
|
+
type: 'tool_call',
|
|
782
|
+
} as t.AgentToolCall,
|
|
783
|
+
],
|
|
784
|
+
})
|
|
785
|
+
);
|
|
786
|
+
|
|
787
|
+
const result = await handleServerToolResult({
|
|
788
|
+
graph: graph as unknown as StandardGraph,
|
|
789
|
+
content: [
|
|
790
|
+
{ type: 'tool_result', tool_use_id: 'tu-1', content: 'ok' },
|
|
791
|
+
] as t.MessageContentComplex[],
|
|
792
|
+
agentContext: anthropicContext,
|
|
793
|
+
});
|
|
794
|
+
expect(result).toBe(false);
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
it('returns true and sets skipHandling when a valid tool result is found', async () => {
|
|
798
|
+
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
799
|
+
graph.getRunStep.mockReturnValue(
|
|
800
|
+
makeRunStep(StepTypes.TOOL_CALLS, {
|
|
801
|
+
tool_calls: [
|
|
802
|
+
{
|
|
803
|
+
id: 'tu-1',
|
|
804
|
+
name: 'calc',
|
|
805
|
+
args: {},
|
|
806
|
+
type: 'tool_call',
|
|
807
|
+
} as t.AgentToolCall,
|
|
808
|
+
],
|
|
809
|
+
})
|
|
810
|
+
);
|
|
811
|
+
|
|
812
|
+
const result = await handleServerToolResult({
|
|
813
|
+
graph: graph as unknown as StandardGraph,
|
|
814
|
+
content: [
|
|
815
|
+
{ type: 'tool_result', tool_use_id: 'tu-1', content: 'ok' },
|
|
816
|
+
] as t.MessageContentComplex[],
|
|
817
|
+
agentContext: anthropicContext,
|
|
818
|
+
});
|
|
819
|
+
expect(result).toBe(true);
|
|
820
|
+
});
|
|
821
|
+
|
|
822
|
+
it('calls handleAnthropicSearchResults for web_search_result type', async () => {
|
|
823
|
+
const mockToolEndHandle = jest
|
|
824
|
+
.fn<(...args: unknown[]) => Promise<void>>()
|
|
825
|
+
.mockResolvedValue(undefined);
|
|
826
|
+
graph.handlerRegistry = {
|
|
827
|
+
getHandler: jest.fn().mockReturnValue({ handle: mockToolEndHandle }),
|
|
828
|
+
};
|
|
829
|
+
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
830
|
+
graph.getRunStep.mockReturnValue(
|
|
831
|
+
makeRunStep(StepTypes.TOOL_CALLS, {
|
|
832
|
+
tool_calls: [
|
|
833
|
+
{
|
|
834
|
+
id: 'tu-1',
|
|
835
|
+
name: 'web_search',
|
|
836
|
+
args: { query: 'test' },
|
|
837
|
+
type: 'tool_call',
|
|
838
|
+
} as t.AgentToolCall,
|
|
839
|
+
],
|
|
840
|
+
})
|
|
841
|
+
);
|
|
842
|
+
|
|
843
|
+
const webSearchContent: t.ToolResultContent = {
|
|
844
|
+
type: 'web_search_result',
|
|
845
|
+
tool_use_id: 'tu-1',
|
|
846
|
+
content: [
|
|
847
|
+
{
|
|
848
|
+
type: 'web_search_result',
|
|
849
|
+
url: 'https://example.com',
|
|
850
|
+
title: 'Example',
|
|
851
|
+
encrypted_index: 'abc',
|
|
852
|
+
page_age: '2024-01-01',
|
|
853
|
+
},
|
|
854
|
+
],
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
const result = await handleServerToolResult({
|
|
858
|
+
graph: graph as unknown as StandardGraph,
|
|
859
|
+
content: [webSearchContent] as t.MessageContentComplex[],
|
|
860
|
+
metadata: defaultMetadata,
|
|
861
|
+
agentContext: anthropicContext,
|
|
862
|
+
});
|
|
863
|
+
|
|
864
|
+
expect(result).toBe(true);
|
|
865
|
+
expect(mockToolEndHandle).toHaveBeenCalledWith(
|
|
866
|
+
GraphEvents.TOOL_END,
|
|
867
|
+
expect.objectContaining({ input: { query: 'test' } }),
|
|
868
|
+
defaultMetadata,
|
|
869
|
+
graph
|
|
870
|
+
);
|
|
871
|
+
expect(graph.invokedToolIds).toBeDefined();
|
|
872
|
+
expect(graph.invokedToolIds!.has('tu-1')).toBe(true);
|
|
873
|
+
});
|
|
874
|
+
|
|
875
|
+
it('initializes invokedToolIds set when null', async () => {
|
|
876
|
+
const mockToolEndHandle = jest
|
|
877
|
+
.fn<(...args: unknown[]) => Promise<void>>()
|
|
878
|
+
.mockResolvedValue(undefined);
|
|
879
|
+
graph.handlerRegistry = {
|
|
880
|
+
getHandler: jest.fn().mockReturnValue({ handle: mockToolEndHandle }),
|
|
881
|
+
};
|
|
882
|
+
graph.invokedToolIds = undefined;
|
|
883
|
+
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
884
|
+
graph.getRunStep.mockReturnValue(
|
|
885
|
+
makeRunStep(StepTypes.TOOL_CALLS, {
|
|
886
|
+
tool_calls: [
|
|
887
|
+
{
|
|
888
|
+
id: 'tu-1',
|
|
889
|
+
name: 'web_search',
|
|
890
|
+
args: {},
|
|
891
|
+
type: 'tool_call',
|
|
892
|
+
} as t.AgentToolCall,
|
|
893
|
+
],
|
|
894
|
+
})
|
|
895
|
+
);
|
|
896
|
+
|
|
897
|
+
const webSearchContent: t.ToolResultContent = {
|
|
898
|
+
type: 'web_search_tool_result',
|
|
899
|
+
tool_use_id: 'tu-1',
|
|
900
|
+
content: [
|
|
901
|
+
{
|
|
902
|
+
type: 'web_search_result',
|
|
903
|
+
url: 'https://example.com',
|
|
904
|
+
title: 'Test',
|
|
905
|
+
encrypted_index: 'x',
|
|
906
|
+
},
|
|
907
|
+
],
|
|
908
|
+
};
|
|
909
|
+
|
|
910
|
+
await handleServerToolResult({
|
|
911
|
+
graph: graph as unknown as StandardGraph,
|
|
912
|
+
content: [webSearchContent] as t.MessageContentComplex[],
|
|
913
|
+
metadata: defaultMetadata,
|
|
914
|
+
agentContext: anthropicContext,
|
|
915
|
+
});
|
|
916
|
+
|
|
917
|
+
expect(graph.invokedToolIds).toBeInstanceOf(Set);
|
|
918
|
+
expect(graph.invokedToolIds!.has('tu-1')).toBe(true);
|
|
919
|
+
});
|
|
920
|
+
|
|
921
|
+
it('warns when web search content is not an array', async () => {
|
|
922
|
+
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
923
|
+
graph.getRunStep.mockReturnValue(
|
|
924
|
+
makeRunStep(StepTypes.TOOL_CALLS, {
|
|
925
|
+
tool_calls: [
|
|
926
|
+
{
|
|
927
|
+
id: 'tu-1',
|
|
928
|
+
name: 'web_search',
|
|
929
|
+
args: {},
|
|
930
|
+
type: 'tool_call',
|
|
931
|
+
} as t.AgentToolCall,
|
|
932
|
+
],
|
|
933
|
+
})
|
|
934
|
+
);
|
|
935
|
+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
936
|
+
|
|
937
|
+
const webSearchContent: t.ToolResultContent = {
|
|
938
|
+
type: 'web_search_result',
|
|
939
|
+
tool_use_id: 'tu-1',
|
|
940
|
+
content: 'not an array',
|
|
941
|
+
};
|
|
942
|
+
|
|
943
|
+
const result = await handleServerToolResult({
|
|
944
|
+
graph: graph as unknown as StandardGraph,
|
|
945
|
+
content: [webSearchContent] as t.MessageContentComplex[],
|
|
946
|
+
metadata: defaultMetadata,
|
|
947
|
+
agentContext: anthropicContext,
|
|
948
|
+
});
|
|
949
|
+
|
|
950
|
+
expect(result).toBe(true);
|
|
951
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
952
|
+
expect.stringContaining('Expected content to be an array')
|
|
953
|
+
);
|
|
954
|
+
warnSpy.mockRestore();
|
|
955
|
+
});
|
|
956
|
+
|
|
957
|
+
it('warns when content is not an Anthropic web search result', async () => {
|
|
958
|
+
graph.toolCallStepIds.set('tu-1', 'step-1');
|
|
959
|
+
graph.getRunStep.mockReturnValue(
|
|
960
|
+
makeRunStep(StepTypes.TOOL_CALLS, {
|
|
961
|
+
tool_calls: [
|
|
962
|
+
{
|
|
963
|
+
id: 'tu-1',
|
|
964
|
+
name: 'web_search',
|
|
965
|
+
args: {},
|
|
966
|
+
type: 'tool_call',
|
|
967
|
+
} as t.AgentToolCall,
|
|
968
|
+
],
|
|
969
|
+
})
|
|
970
|
+
);
|
|
971
|
+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
972
|
+
|
|
973
|
+
const webSearchContent: t.ToolResultContent = {
|
|
974
|
+
type: 'web_search_result',
|
|
975
|
+
tool_use_id: 'tu-1',
|
|
976
|
+
content: [{ type: 'text', text: 'not a search result' }],
|
|
977
|
+
};
|
|
978
|
+
|
|
979
|
+
const result = await handleServerToolResult({
|
|
980
|
+
graph: graph as unknown as StandardGraph,
|
|
981
|
+
content: [webSearchContent] as t.MessageContentComplex[],
|
|
982
|
+
metadata: defaultMetadata,
|
|
983
|
+
agentContext: anthropicContext,
|
|
984
|
+
});
|
|
985
|
+
|
|
986
|
+
expect(result).toBe(true);
|
|
987
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
988
|
+
expect.stringContaining(
|
|
989
|
+
'Expected content to be an Anthropic web search result'
|
|
990
|
+
)
|
|
991
|
+
);
|
|
992
|
+
warnSpy.mockRestore();
|
|
993
|
+
});
|
|
994
|
+
});
|