@robota-sdk/agent-provider-openai 3.0.7 → 3.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/CHANGELOG.md +53 -0
- package/README.md +34 -5
- package/dist/index.cjs +3 -0
- package/dist/{node/index.d.cts → index.d.cts} +12 -10
- package/dist/index.d.cts.map +1 -0
- package/dist/{node/index.d.ts → index.d.ts} +12 -10
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/loggers/index.cjs +1 -0
- package/dist/loggers/index.d.cts +43 -0
- package/dist/loggers/index.d.cts.map +1 -0
- package/dist/loggers/index.d.ts +43 -0
- package/dist/loggers/index.d.ts.map +1 -0
- package/dist/loggers/index.js +2 -0
- package/dist/loggers/index.js.map +1 -0
- package/dist/node.cjs +1 -0
- package/dist/node.d.cts +54 -0
- package/dist/node.d.cts.map +1 -0
- package/dist/node.d.ts +54 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +2 -0
- package/dist/node.js.map +1 -0
- package/dist/payload-logger-n89AQdUn.d.cts.map +1 -0
- package/dist/payload-logger-n89AQdUn.d.ts.map +1 -0
- package/dist/sanitize-openai-log-data-CZ9jMqeC.cjs +1 -0
- package/dist/sanitize-openai-log-data-DI2gyZvi.js +2 -0
- package/dist/sanitize-openai-log-data-DI2gyZvi.js.map +1 -0
- package/package.json +29 -41
- package/src/node.ts +9 -0
- package/src/openai/__tests__/native-web-search.test.ts +941 -0
- package/src/openai/__tests__/strict-tools-closure.test.ts +42 -0
- package/src/openai/__tests__/tool-schema-projection.test.ts +35 -1
- package/src/openai/loggers/file-payload-logger.ts +3 -3
- package/src/openai/loggers/index.ts +1 -5
- package/src/openai/provider-definition.test.ts +3 -4
- package/src/openai/provider-definition.ts +22 -50
- package/src/openai/provider.ts +81 -25
- package/src/openai/responses-chat.ts +51 -21
- package/src/openai/responses-converter.ts +31 -8
- package/src/openai/responses-parser.ts +126 -6
- package/src/openai/responses-types.ts +22 -2
- package/src/openai/responses-web-search.ts +157 -0
- package/src/openai/types.ts +7 -8
- package/dist/node/index.cjs +0 -3
- package/dist/node/index.d.cts.map +0 -1
- package/dist/node/index.d.ts.map +0 -1
- package/dist/node/index.js +0 -4
- package/dist/node/index.js.map +0 -1
- package/dist/node/loggers/index.cjs +0 -1
- package/dist/node/loggers/index.d.cts +0 -94
- package/dist/node/loggers/index.d.cts.map +0 -1
- package/dist/node/loggers/index.d.ts +0 -94
- package/dist/node/loggers/index.d.ts.map +0 -1
- package/dist/node/loggers/index.js +0 -2
- package/dist/node/loggers/index.js.map +0 -1
- package/dist/node/payload-logger-n89AQdUn.d.cts.map +0 -1
- package/dist/node/payload-logger-n89AQdUn.d.ts.map +0 -1
- package/src/openai/loggers/file.ts +0 -9
- /package/dist/{node/payload-logger-n89AQdUn.d.cts → payload-logger-n89AQdUn.d.cts} +0 -0
- /package/dist/{node/payload-logger-n89AQdUn.d.ts → payload-logger-n89AQdUn.d.ts} +0 -0
- /package/dist/{node/rolldown-runtime-CMqjfN_6.cjs → rolldown-runtime-CMqjfN_6.cjs} +0 -0
|
@@ -0,0 +1,941 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hosted web search on OpenAI's own Responses API: when a `web_search` tool joins a request, how the
|
|
3
|
+
* result reports it, and where the option is accepted at all.
|
|
4
|
+
*/
|
|
5
|
+
import {
|
|
6
|
+
AbstractTool,
|
|
7
|
+
ConversationAgent,
|
|
8
|
+
PROVIDER_CONTINUATION_METADATA_KEY,
|
|
9
|
+
} from '@robota-sdk/agent-core';
|
|
10
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
11
|
+
|
|
12
|
+
import { OpenAIProvider } from '../provider';
|
|
13
|
+
import { createOpenAIProviderDefinition } from '../provider-definition';
|
|
14
|
+
|
|
15
|
+
import type {
|
|
16
|
+
IAssistantMessage,
|
|
17
|
+
IChatOptions,
|
|
18
|
+
IToolResult,
|
|
19
|
+
IToolSchema,
|
|
20
|
+
TUniversalMessage,
|
|
21
|
+
} from '@robota-sdk/agent-core';
|
|
22
|
+
import type OpenAI from 'openai';
|
|
23
|
+
|
|
24
|
+
const WEB_SEARCH_TOOL = { type: 'web_search' };
|
|
25
|
+
|
|
26
|
+
const lookupTool: IToolSchema = {
|
|
27
|
+
name: 'lookup',
|
|
28
|
+
description: 'Look something up',
|
|
29
|
+
parameters: { type: 'object', properties: { key: { type: 'string' } }, required: ['key'] },
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const messages: TUniversalMessage[] = [
|
|
33
|
+
{ id: 'u1', role: 'user', content: 'hello', state: 'complete', timestamp: new Date() },
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
type TRequest = Record<string, unknown> & { tools?: unknown[]; tool_choice?: unknown };
|
|
37
|
+
|
|
38
|
+
function citedMessage(urls: readonly string[]): Record<string, unknown> {
|
|
39
|
+
return {
|
|
40
|
+
type: 'message',
|
|
41
|
+
role: 'assistant',
|
|
42
|
+
content: [
|
|
43
|
+
{
|
|
44
|
+
type: 'output_text',
|
|
45
|
+
text: 'answer',
|
|
46
|
+
annotations: [
|
|
47
|
+
...urls.map((url) => ({ type: 'url_citation', url, title: url })),
|
|
48
|
+
{ type: 'file_citation', file_id: 'file-1' },
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function completedResponse(output: unknown[] = []): Record<string, unknown> {
|
|
56
|
+
return { id: 'resp-1', model: 'gpt-4o', output_text: 'answer', output, status: 'completed' };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function* eventsFrom(events: unknown[]): AsyncIterable<unknown> {
|
|
60
|
+
for (const event of events) yield event;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function setup(
|
|
64
|
+
options: Record<string, unknown> = {},
|
|
65
|
+
result: unknown = completedResponse(),
|
|
66
|
+
): { provider: OpenAIProvider; create: ReturnType<typeof vi.fn> } {
|
|
67
|
+
const create = vi.fn().mockResolvedValue(result);
|
|
68
|
+
const client = { responses: { create } } as unknown as OpenAI;
|
|
69
|
+
const provider = new OpenAIProvider({ client, defaultModel: 'gpt-4o', ...options });
|
|
70
|
+
return { provider, create };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function searching(options: Record<string, unknown> = {}): Record<string, unknown> {
|
|
74
|
+
return { nativeWebTools: { webSearch: true }, ...options };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function sentRequest(
|
|
78
|
+
options: Record<string, unknown>,
|
|
79
|
+
chatOptions: IChatOptions = {},
|
|
80
|
+
): Promise<TRequest> {
|
|
81
|
+
const { provider, create } = setup(options);
|
|
82
|
+
await provider.chat(messages, chatOptions);
|
|
83
|
+
return create.mock.calls[0]?.[0] as TRequest;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
afterEach(() => {
|
|
87
|
+
vi.unstubAllEnvs();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('hosted web search request', () => {
|
|
91
|
+
it('adds the web_search tool after the function tools and keeps tool_choice auto', async () => {
|
|
92
|
+
const request = await sentRequest(searching(), { tools: [lookupTool] });
|
|
93
|
+
|
|
94
|
+
expect(request.tools).toEqual([
|
|
95
|
+
expect.objectContaining({ type: 'function', name: 'lookup' }),
|
|
96
|
+
WEB_SEARCH_TOOL,
|
|
97
|
+
]);
|
|
98
|
+
expect(request.tool_choice).toBe('auto');
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('sends web_search alone when the caller has no function tools', async () => {
|
|
102
|
+
const request = await sentRequest(searching());
|
|
103
|
+
|
|
104
|
+
expect(request.tools).toEqual([WEB_SEARCH_TOOL]);
|
|
105
|
+
expect(request.tool_choice).toBe('auto');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('sends the same tool on a streamed request', async () => {
|
|
109
|
+
const { provider, create } = setup(searching(), eventsFrom([]));
|
|
110
|
+
await provider.chat(messages, { onTextDelta: () => undefined });
|
|
111
|
+
|
|
112
|
+
const request = create.mock.calls[0]?.[0] as TRequest;
|
|
113
|
+
expect(request.stream).toBe(true);
|
|
114
|
+
expect(request.tools).toEqual([WEB_SEARCH_TOOL]);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('adds no web_search tool by default, and no tools field without function tools', async () => {
|
|
118
|
+
const request = await sentRequest({});
|
|
119
|
+
|
|
120
|
+
expect(request).not.toHaveProperty('tools');
|
|
121
|
+
expect(request).not.toHaveProperty('tool_choice');
|
|
122
|
+
|
|
123
|
+
const withFunction = await sentRequest({}, { tools: [lookupTool] });
|
|
124
|
+
expect(withFunction.tools).toEqual([expect.objectContaining({ type: 'function' })]);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe('withheld', () => {
|
|
128
|
+
const cases: Array<[string, IChatOptions, Record<string, unknown>]> = [
|
|
129
|
+
['toolChoice none', { toolChoice: 'none' }, {}],
|
|
130
|
+
['toolChoice required', { toolChoice: 'required' }, {}],
|
|
131
|
+
['a forced function', { toolChoice: { tool: 'x' } }, {}],
|
|
132
|
+
['a per-call webSearch false', { nativeWebTools: { webSearch: false } }, {}],
|
|
133
|
+
['minimal reasoning', {}, { reasoning: { effort: 'minimal' } }],
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
it.each(cases)('for %s', async (_label, chatOptions, providerOptions) => {
|
|
137
|
+
const request = await sentRequest(searching(providerOptions), {
|
|
138
|
+
tools: [lookupTool],
|
|
139
|
+
...chatOptions,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
expect(request.tools).toEqual([expect.objectContaining({ type: 'function' })]);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it.each([
|
|
146
|
+
'gpt-4.1-nano',
|
|
147
|
+
'gpt-4.1-nano-2025-04-14',
|
|
148
|
+
'o1',
|
|
149
|
+
'o1-pro',
|
|
150
|
+
'o3-mini',
|
|
151
|
+
'gpt-3.5-turbo',
|
|
152
|
+
'gpt-4',
|
|
153
|
+
'gpt-4-turbo',
|
|
154
|
+
])('for the model %s', async (model) => {
|
|
155
|
+
const request = await sentRequest(searching(), { model, tools: [lookupTool] });
|
|
156
|
+
|
|
157
|
+
expect(request.tools).toEqual([expect.objectContaining({ type: 'function' })]);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it.each([
|
|
162
|
+
'gpt-4o',
|
|
163
|
+
'gpt-4o-mini',
|
|
164
|
+
'gpt-4.1',
|
|
165
|
+
'gpt-4.1-mini',
|
|
166
|
+
'o3',
|
|
167
|
+
'o4-mini',
|
|
168
|
+
'gpt-5',
|
|
169
|
+
'gpt-5.5',
|
|
170
|
+
])('is included for the model %s', async (model) => {
|
|
171
|
+
const request = await sentRequest(searching(), { model });
|
|
172
|
+
|
|
173
|
+
expect(request.tools).toEqual([WEB_SEARCH_TOOL]);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('is included for an explicit auto tool choice', async () => {
|
|
177
|
+
const request = await sentRequest(searching(), { toolChoice: 'auto' });
|
|
178
|
+
|
|
179
|
+
expect(request.tools).toEqual([WEB_SEARCH_TOOL]);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('withholds the tool for the provider default model too', async () => {
|
|
183
|
+
const request = await sentRequest(searching({ defaultModel: 'o3-mini' }));
|
|
184
|
+
|
|
185
|
+
expect(request).not.toHaveProperty('tools');
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe('hosted web search result', () => {
|
|
190
|
+
const twoSearches = [
|
|
191
|
+
{
|
|
192
|
+
type: 'web_search_call',
|
|
193
|
+
id: 'ws1',
|
|
194
|
+
status: 'completed',
|
|
195
|
+
action: { type: 'search', query: 'a' },
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
type: 'web_search_call',
|
|
199
|
+
id: 'ws2',
|
|
200
|
+
status: 'completed',
|
|
201
|
+
action: { type: 'open_page', url: 'https://a.test' },
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
type: 'web_search_call',
|
|
205
|
+
id: 'ws3',
|
|
206
|
+
status: 'completed',
|
|
207
|
+
action: { type: 'search', query: 'b' },
|
|
208
|
+
},
|
|
209
|
+
citedMessage(['https://a.test', 'https://b.test', 'https://a.test']),
|
|
210
|
+
];
|
|
211
|
+
|
|
212
|
+
it('counts only search actions and lists each cited URL once, in order', async () => {
|
|
213
|
+
const { provider } = setup(searching(), completedResponse(twoSearches));
|
|
214
|
+
|
|
215
|
+
const message = await provider.chat(messages, {});
|
|
216
|
+
|
|
217
|
+
expect(message.metadata).toMatchObject({
|
|
218
|
+
providerBuiltInToolsEnabled: ['web_search'],
|
|
219
|
+
providerBuiltInToolsUsed: ['web_search'],
|
|
220
|
+
openaiWebSearchCalls: 2,
|
|
221
|
+
openaiWebSearchCitations: ['https://a.test', 'https://b.test'],
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('counts a call that names no action as a search', async () => {
|
|
226
|
+
const { provider } = setup(
|
|
227
|
+
searching(),
|
|
228
|
+
completedResponse([{ type: 'web_search_call', id: 'ws1' }]),
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
const message = await provider.chat(messages, {});
|
|
232
|
+
|
|
233
|
+
expect(message.metadata).toMatchObject({ openaiWebSearchCalls: 1 });
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('reports nothing when the request did not carry the tool', async () => {
|
|
237
|
+
const { provider } = setup({}, completedResponse(twoSearches));
|
|
238
|
+
|
|
239
|
+
const message = await provider.chat(messages, {});
|
|
240
|
+
|
|
241
|
+
for (const key of [
|
|
242
|
+
'providerBuiltInToolsEnabled',
|
|
243
|
+
'providerBuiltInToolsUsed',
|
|
244
|
+
'openaiWebSearchCalls',
|
|
245
|
+
'openaiWebSearchCitations',
|
|
246
|
+
]) {
|
|
247
|
+
expect(message.metadata).not.toHaveProperty(key);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it('reports a withheld tool as not carried', async () => {
|
|
252
|
+
const { provider } = setup(searching(), completedResponse(twoSearches));
|
|
253
|
+
|
|
254
|
+
const message = await provider.chat(messages, { toolChoice: 'none' });
|
|
255
|
+
|
|
256
|
+
expect(message.metadata).not.toHaveProperty('providerBuiltInToolsEnabled');
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it('reports zero searches without a used tool when the model did not search', async () => {
|
|
260
|
+
const { provider } = setup(searching(), completedResponse());
|
|
261
|
+
|
|
262
|
+
const message = await provider.chat(messages, {});
|
|
263
|
+
|
|
264
|
+
expect(message.metadata).toMatchObject({
|
|
265
|
+
providerBuiltInToolsEnabled: ['web_search'],
|
|
266
|
+
openaiWebSearchCalls: 0,
|
|
267
|
+
});
|
|
268
|
+
expect(message.metadata).not.toHaveProperty('providerBuiltInToolsUsed');
|
|
269
|
+
expect(message.metadata).not.toHaveProperty('openaiWebSearchCitations');
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
describe('streamed', () => {
|
|
273
|
+
const search = (id: string): Record<string, unknown> => ({
|
|
274
|
+
type: 'web_search_call',
|
|
275
|
+
id,
|
|
276
|
+
status: 'completed',
|
|
277
|
+
action: { type: 'search', query: id },
|
|
278
|
+
});
|
|
279
|
+
const completed = { type: 'response.completed', response: completedResponse() };
|
|
280
|
+
const itemEvents = [
|
|
281
|
+
{ type: 'response.output_item.done', item: search('ws1') },
|
|
282
|
+
{
|
|
283
|
+
type: 'response.output_item.done',
|
|
284
|
+
item: { ...search('ws2'), action: { type: 'find_in_page' } },
|
|
285
|
+
},
|
|
286
|
+
{ type: 'response.output_item.done', item: search('ws3') },
|
|
287
|
+
{ type: 'response.output_item.done', item: citedMessage(['https://a.test']) },
|
|
288
|
+
{ type: 'response.output_text.delta', delta: 'answer' },
|
|
289
|
+
];
|
|
290
|
+
|
|
291
|
+
it('reports the searches of a stream that completes', async () => {
|
|
292
|
+
const deltas: string[] = [];
|
|
293
|
+
const { provider } = setup(searching(), eventsFrom([...itemEvents, completed]));
|
|
294
|
+
|
|
295
|
+
const message = await provider.chat(messages, { onTextDelta: (delta) => deltas.push(delta) });
|
|
296
|
+
|
|
297
|
+
expect(deltas).toEqual(['answer']);
|
|
298
|
+
expect(message.metadata).toMatchObject({
|
|
299
|
+
providerBuiltInToolsEnabled: ['web_search'],
|
|
300
|
+
providerBuiltInToolsUsed: ['web_search'],
|
|
301
|
+
openaiWebSearchCalls: 2,
|
|
302
|
+
openaiWebSearchCitations: ['https://a.test'],
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('takes the larger count and the union when the completed response reports more', async () => {
|
|
307
|
+
const response = completedResponse([
|
|
308
|
+
search('ws1'),
|
|
309
|
+
search('ws2'),
|
|
310
|
+
search('ws3'),
|
|
311
|
+
search('ws4'),
|
|
312
|
+
citedMessage(['https://b.test']),
|
|
313
|
+
]);
|
|
314
|
+
const { provider } = setup(
|
|
315
|
+
searching(),
|
|
316
|
+
eventsFrom([...itemEvents, { type: 'response.completed', response }]),
|
|
317
|
+
);
|
|
318
|
+
|
|
319
|
+
const message = await provider.chat(messages, { onTextDelta: () => undefined });
|
|
320
|
+
|
|
321
|
+
expect(message.metadata).toMatchObject({
|
|
322
|
+
openaiWebSearchCalls: 4,
|
|
323
|
+
openaiWebSearchCitations: ['https://a.test', 'https://b.test'],
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it('still reports when the stream ends without response.completed', async () => {
|
|
328
|
+
const { provider } = setup(searching(), eventsFrom(itemEvents));
|
|
329
|
+
|
|
330
|
+
const message = await provider.chat(messages, { onTextDelta: () => undefined });
|
|
331
|
+
|
|
332
|
+
expect(message.content).toBe('answer');
|
|
333
|
+
expect(message.metadata).toMatchObject({
|
|
334
|
+
providerBuiltInToolsEnabled: ['web_search'],
|
|
335
|
+
providerBuiltInToolsUsed: ['web_search'],
|
|
336
|
+
openaiWebSearchCalls: 2,
|
|
337
|
+
openaiWebSearchCitations: ['https://a.test'],
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('reports nothing for a stream whose request did not carry the tool', async () => {
|
|
342
|
+
const { provider } = setup({}, eventsFrom([...itemEvents, completed]));
|
|
343
|
+
|
|
344
|
+
const message = await provider.chat(messages, { onTextDelta: () => undefined });
|
|
345
|
+
|
|
346
|
+
expect(message.metadata).not.toHaveProperty('openaiWebSearchCalls');
|
|
347
|
+
expect(message.metadata).not.toHaveProperty('providerBuiltInToolsEnabled');
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('reports through chatStream as well', async () => {
|
|
351
|
+
const { provider } = setup(searching(), eventsFrom([...itemEvents, completed]));
|
|
352
|
+
|
|
353
|
+
const chunks: TUniversalMessage[] = [];
|
|
354
|
+
for await (const chunk of provider.chatStream(messages, {})) chunks.push(chunk);
|
|
355
|
+
|
|
356
|
+
expect(chunks.at(-1)?.metadata).toMatchObject({ openaiWebSearchCalls: 2 });
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
describe('hosted web search capability', () => {
|
|
362
|
+
const source = 'openai-responses';
|
|
363
|
+
|
|
364
|
+
it('is supported and enabled on the vendor Responses API when the option is on', () => {
|
|
365
|
+
const { provider } = setup(searching());
|
|
366
|
+
|
|
367
|
+
expect(provider.getCapabilities().nativeWebTools).toEqual({
|
|
368
|
+
webSearch: { supported: true, enabled: true, source },
|
|
369
|
+
webFetch: {
|
|
370
|
+
supported: false,
|
|
371
|
+
enabled: false,
|
|
372
|
+
source,
|
|
373
|
+
reason: 'OpenAI provider exposes hosted web search only.',
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
it('is not enabled on a default model without web search, and says so', () => {
|
|
379
|
+
const { provider } = setup(searching({ defaultModel: 'gpt-4.1-nano' }));
|
|
380
|
+
const { webSearch } = provider.getCapabilities().nativeWebTools;
|
|
381
|
+
|
|
382
|
+
expect(webSearch).toMatchObject({ supported: true, enabled: false, source });
|
|
383
|
+
expect(webSearch.reason).toContain('gpt-4.1-nano');
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
it('fails a call that requires search where the model or its reasoning cannot run it', async () => {
|
|
387
|
+
const { provider, create } = setup(searching());
|
|
388
|
+
|
|
389
|
+
await expect(
|
|
390
|
+
provider.chat(messages, { model: 'o3-mini', nativeWebTools: { webSearch: true } }),
|
|
391
|
+
).rejects.toThrow(/cannot run hosted web search on model o3-mini/);
|
|
392
|
+
expect(create).not.toHaveBeenCalled();
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('is supported but not enabled, with the way to turn it on, when the option is off', () => {
|
|
396
|
+
const { provider } = setup();
|
|
397
|
+
|
|
398
|
+
expect(provider.getCapabilities().nativeWebTools.webSearch).toEqual({
|
|
399
|
+
supported: true,
|
|
400
|
+
enabled: false,
|
|
401
|
+
source,
|
|
402
|
+
reason: 'Set the provider profile option nativeWebTools.webSearch to true.',
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it('is unsupported on Chat Completions', () => {
|
|
407
|
+
const { provider } = setup({ apiSurface: 'chat-completions' });
|
|
408
|
+
|
|
409
|
+
expect(provider.getCapabilities().nativeWebTools).toEqual({
|
|
410
|
+
webSearch: {
|
|
411
|
+
supported: false,
|
|
412
|
+
enabled: false,
|
|
413
|
+
source: 'openai-compatible-chat-completions',
|
|
414
|
+
reason:
|
|
415
|
+
'OpenAI-compatible Chat Completions endpoints support declared function tools, not provider-native web search.',
|
|
416
|
+
},
|
|
417
|
+
webFetch: {
|
|
418
|
+
supported: false,
|
|
419
|
+
enabled: false,
|
|
420
|
+
source: 'openai-compatible-chat-completions',
|
|
421
|
+
reason:
|
|
422
|
+
'OpenAI-compatible Chat Completions endpoints support declared function tools, not provider-native web fetch.',
|
|
423
|
+
},
|
|
424
|
+
});
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
it('is unsupported on the Responses API behind a custom baseURL', () => {
|
|
428
|
+
const { provider } = setup({
|
|
429
|
+
apiSurface: 'responses',
|
|
430
|
+
baseURL: 'https://gateway.example.com/v1',
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
expect(provider.getCapabilities().nativeWebTools.webSearch).toEqual({
|
|
434
|
+
supported: false,
|
|
435
|
+
enabled: false,
|
|
436
|
+
source,
|
|
437
|
+
reason: "Hosted web search runs only on OpenAI's own endpoint, not a custom baseURL.",
|
|
438
|
+
});
|
|
439
|
+
expect(provider.getCapabilities().nativeWebTools.webFetch.reason).toBe(
|
|
440
|
+
'OpenAI provider exposes hosted web search only.',
|
|
441
|
+
);
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it('lets a call require search only once the profile turned it on', async () => {
|
|
445
|
+
const { provider } = setup();
|
|
446
|
+
|
|
447
|
+
await expect(provider.chat(messages, { nativeWebTools: { webSearch: true } })).rejects.toThrow(
|
|
448
|
+
'Provider openai supports native web search but it is not enabled. Set the provider profile option nativeWebTools.webSearch to true.',
|
|
449
|
+
);
|
|
450
|
+
await expect(
|
|
451
|
+
setup(searching()).provider.chat(messages, { nativeWebTools: { webSearch: true } }),
|
|
452
|
+
).resolves.toBeDefined();
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
describe('hosted web search construction', () => {
|
|
457
|
+
it('rejects web search on Chat Completions', () => {
|
|
458
|
+
expect(() => setup({ apiSurface: 'chat-completions', ...searching() })).toThrow(
|
|
459
|
+
"Provider openai hosted web search runs only on OpenAI's own Responses API, not on apiSurface chat-completions.",
|
|
460
|
+
);
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it('rejects web search behind a custom baseURL', () => {
|
|
464
|
+
expect(() => setup({ baseURL: 'https://gateway.example.com/v1', ...searching() })).toThrow(
|
|
465
|
+
'not on apiSurface chat-completions with a custom baseURL.',
|
|
466
|
+
);
|
|
467
|
+
expect(() =>
|
|
468
|
+
setup({ apiSurface: 'responses', baseURL: 'https://gateway.example.com/v1', ...searching() }),
|
|
469
|
+
).toThrow('not on apiSurface responses with a custom baseURL.');
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
it('rejects web fetch', () => {
|
|
473
|
+
expect(() => setup({ nativeWebTools: { webFetch: true } })).toThrow(
|
|
474
|
+
'Provider openai does not support native web fetch.',
|
|
475
|
+
);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
it('accepts an explicit false for either tool on any surface', () => {
|
|
479
|
+
expect(() =>
|
|
480
|
+
setup({
|
|
481
|
+
apiSurface: 'chat-completions',
|
|
482
|
+
nativeWebTools: { webSearch: false, webFetch: false },
|
|
483
|
+
}),
|
|
484
|
+
).not.toThrow();
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
describe('hosted web search in the provider definition', () => {
|
|
489
|
+
const definition = createOpenAIProviderDefinition();
|
|
490
|
+
const profile = { name: 'openai', model: 'gpt-4o', apiKey: 'sk-test' };
|
|
491
|
+
|
|
492
|
+
it('declares web search for a profile on the vendor Responses API', () => {
|
|
493
|
+
expect(definition.nativeWebTools?.({}, {})).toEqual(['webSearch']);
|
|
494
|
+
expect(definition.nativeWebTools?.({ options: { apiSurface: 'responses' } }, {})).toEqual([
|
|
495
|
+
'webSearch',
|
|
496
|
+
]);
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
it('declares nothing for a custom baseURL or Chat Completions', () => {
|
|
500
|
+
expect(definition.nativeWebTools?.({ baseURL: 'http://localhost:1234/v1' }, {})).toEqual([]);
|
|
501
|
+
expect(
|
|
502
|
+
definition.nativeWebTools?.({ options: { apiSurface: 'chat-completions' } }, {}),
|
|
503
|
+
).toEqual([]);
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it('declares nothing while OPENAI_BASE_URL redirects the SDK, and ignores an empty one', () => {
|
|
507
|
+
const redirected = { OPENAI_BASE_URL: 'https://gateway.example.com/v1' };
|
|
508
|
+
expect(definition.nativeWebTools?.({}, redirected)).toEqual([]);
|
|
509
|
+
|
|
510
|
+
expect(definition.nativeWebTools?.({}, { OPENAI_BASE_URL: '' })).toEqual(['webSearch']);
|
|
511
|
+
});
|
|
512
|
+
|
|
513
|
+
it('builds a provider with search on from the profile option', () => {
|
|
514
|
+
const provider = definition.createProvider({
|
|
515
|
+
...profile,
|
|
516
|
+
options: { nativeWebTools: { webSearch: true } },
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
expect(provider.getCapabilities?.().nativeWebTools.webSearch).toMatchObject({
|
|
520
|
+
supported: true,
|
|
521
|
+
enabled: true,
|
|
522
|
+
});
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
it('builds a provider with search off by default', () => {
|
|
526
|
+
expect(
|
|
527
|
+
definition.createProvider(profile).getCapabilities?.().nativeWebTools.webSearch,
|
|
528
|
+
).toMatchObject({
|
|
529
|
+
supported: true,
|
|
530
|
+
enabled: false,
|
|
531
|
+
});
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
it('does not read the old builtInWebTools spelling', () => {
|
|
535
|
+
const provider = definition.createProvider({
|
|
536
|
+
...profile,
|
|
537
|
+
options: { builtInWebTools: { webSearch: true } },
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
expect(provider.getCapabilities?.().nativeWebTools.webSearch.enabled).toBe(false);
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
it.each([
|
|
544
|
+
['a non-object', 'yes'],
|
|
545
|
+
['an array', [true]],
|
|
546
|
+
['an unknown key', { websearch: true }],
|
|
547
|
+
['a non-boolean flag', { webSearch: 'true' }],
|
|
548
|
+
['web fetch', { webFetch: true }],
|
|
549
|
+
])('rejects %s', (_label, nativeWebTools) => {
|
|
550
|
+
expect(() => definition.createProvider({ ...profile, options: { nativeWebTools } })).toThrow(
|
|
551
|
+
/Provider openai/,
|
|
552
|
+
);
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
it('rejects an explicit request on a baseURL profile or Chat Completions', () => {
|
|
556
|
+
const message =
|
|
557
|
+
"Provider openai profile uses an OpenAI-compatible endpoint or Chat Completions; hosted web search runs only on OpenAI's own Responses API. Connect an MCP server that offers web search.";
|
|
558
|
+
|
|
559
|
+
expect(() =>
|
|
560
|
+
definition.createProvider({
|
|
561
|
+
...profile,
|
|
562
|
+
baseURL: 'http://localhost:1234/v1',
|
|
563
|
+
options: { nativeWebTools: { webSearch: true } },
|
|
564
|
+
}),
|
|
565
|
+
).toThrow(message);
|
|
566
|
+
expect(() =>
|
|
567
|
+
definition.createProvider({
|
|
568
|
+
...profile,
|
|
569
|
+
options: { apiSurface: 'chat-completions', nativeWebTools: { webSearch: true } },
|
|
570
|
+
}),
|
|
571
|
+
).toThrow(message);
|
|
572
|
+
});
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
describe('sending a searching reply back', () => {
|
|
576
|
+
const REASONING = { type: 'reasoning', id: 'rs_1', summary: [] };
|
|
577
|
+
const SEARCH = {
|
|
578
|
+
type: 'web_search_call',
|
|
579
|
+
id: 'ws_1',
|
|
580
|
+
status: 'completed',
|
|
581
|
+
action: { type: 'search', query: 'node 24' },
|
|
582
|
+
};
|
|
583
|
+
const NARRATION = {
|
|
584
|
+
type: 'message',
|
|
585
|
+
id: 'msg_1',
|
|
586
|
+
role: 'assistant',
|
|
587
|
+
status: 'completed',
|
|
588
|
+
content: [
|
|
589
|
+
{
|
|
590
|
+
type: 'output_text',
|
|
591
|
+
text: 'Looking.',
|
|
592
|
+
annotations: [{ type: 'url_citation', url: 'https://example.com/a', title: 'a' }],
|
|
593
|
+
},
|
|
594
|
+
],
|
|
595
|
+
};
|
|
596
|
+
const CALL = {
|
|
597
|
+
type: 'function_call',
|
|
598
|
+
id: 'fc_1',
|
|
599
|
+
call_id: 'call_1',
|
|
600
|
+
name: 'lookup',
|
|
601
|
+
arguments: '{"key":"a"}',
|
|
602
|
+
status: 'completed',
|
|
603
|
+
};
|
|
604
|
+
const SEARCHING_OUTPUT = [REASONING, SEARCH, NARRATION, CALL];
|
|
605
|
+
|
|
606
|
+
function searchingReply(
|
|
607
|
+
output: unknown[] = SEARCHING_OUTPUT,
|
|
608
|
+
fields: Record<string, unknown> = {},
|
|
609
|
+
): Record<string, unknown> {
|
|
610
|
+
return {
|
|
611
|
+
id: 'resp-2',
|
|
612
|
+
model: 'gpt-4o',
|
|
613
|
+
output_text: 'Looking.',
|
|
614
|
+
output,
|
|
615
|
+
status: 'completed',
|
|
616
|
+
...fields,
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
const toolResult: TUniversalMessage = {
|
|
621
|
+
id: 't1',
|
|
622
|
+
role: 'tool',
|
|
623
|
+
content: 'found',
|
|
624
|
+
toolCallId: 'call_1',
|
|
625
|
+
state: 'complete',
|
|
626
|
+
timestamp: new Date(),
|
|
627
|
+
};
|
|
628
|
+
const nextUser: TUniversalMessage = {
|
|
629
|
+
id: 'u2',
|
|
630
|
+
role: 'user',
|
|
631
|
+
content: 'And Node 25?',
|
|
632
|
+
state: 'complete',
|
|
633
|
+
timestamp: new Date(),
|
|
634
|
+
};
|
|
635
|
+
const toolOutput = { type: 'function_call_output', call_id: 'call_1', output: 'found' };
|
|
636
|
+
|
|
637
|
+
// What a reply is rebuilt as: its text, then its calls without the IDs OpenAI gave them.
|
|
638
|
+
const REBUILT = [
|
|
639
|
+
{ role: 'assistant', content: 'Looking.' },
|
|
640
|
+
{ type: 'function_call', call_id: 'call_1', name: 'lookup', arguments: '{"key":"a"}' },
|
|
641
|
+
];
|
|
642
|
+
|
|
643
|
+
/** The `input` of the last request `history` produces. */
|
|
644
|
+
async function inputAfter(
|
|
645
|
+
{ provider, create }: ReturnType<typeof setup>,
|
|
646
|
+
history: TUniversalMessage[],
|
|
647
|
+
chatOptions: IChatOptions = {},
|
|
648
|
+
): Promise<Array<Record<string, unknown>>> {
|
|
649
|
+
await provider.chat(history, { tools: [lookupTool], ...chatOptions });
|
|
650
|
+
return (create.mock.calls.at(-1)?.[0] as { input: Array<Record<string, unknown>> }).input;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
async function firstReply(
|
|
654
|
+
target: ReturnType<typeof setup>,
|
|
655
|
+
chatOptions: IChatOptions = {},
|
|
656
|
+
): Promise<IAssistantMessage> {
|
|
657
|
+
return (await target.provider.chat(messages, {
|
|
658
|
+
tools: [lookupTool],
|
|
659
|
+
...chatOptions,
|
|
660
|
+
})) as IAssistantMessage;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
class LookupTool extends AbstractTool {
|
|
664
|
+
readonly schema = lookupTool;
|
|
665
|
+
|
|
666
|
+
protected async executeImpl(): Promise<IToolResult> {
|
|
667
|
+
return { success: true, data: 'found' };
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
it('returns the whole reply on the next request of a tool round', async () => {
|
|
672
|
+
// The agent streams its requests, so each answer is a stream.
|
|
673
|
+
const target = setup(searching());
|
|
674
|
+
target.create
|
|
675
|
+
.mockResolvedValueOnce(
|
|
676
|
+
eventsFrom([
|
|
677
|
+
{ type: 'response.output_text.delta', delta: 'Looking.' },
|
|
678
|
+
{ type: 'response.completed', response: searchingReply() },
|
|
679
|
+
]),
|
|
680
|
+
)
|
|
681
|
+
.mockResolvedValueOnce(
|
|
682
|
+
eventsFrom([
|
|
683
|
+
{ type: 'response.output_text.delta', delta: 'Done.' },
|
|
684
|
+
{ type: 'response.completed', response: completedResponse() },
|
|
685
|
+
]),
|
|
686
|
+
);
|
|
687
|
+
const agent = new ConversationAgent({
|
|
688
|
+
name: 'wire',
|
|
689
|
+
aiProviders: [target.provider],
|
|
690
|
+
defaultModel: { provider: target.provider.name, model: 'gpt-4o' },
|
|
691
|
+
tools: [new LookupTool()],
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
await agent.run('What changed in Node 24?');
|
|
695
|
+
|
|
696
|
+
expect(target.create).toHaveBeenCalledTimes(2);
|
|
697
|
+
const { input } = target.create.mock.calls[1]?.[0] as { input: unknown[] };
|
|
698
|
+
const start = input.findIndex((item) => (item as { type?: string }).type === 'reasoning');
|
|
699
|
+
expect(input.slice(start, start + SEARCHING_OUTPUT.length)).toEqual(SEARCHING_OUTPUT);
|
|
700
|
+
expect(input.slice(start + SEARCHING_OUTPUT.length)).toEqual([toolOutput]);
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
it('records the same reply from a stream and sends it back whole', async () => {
|
|
704
|
+
const target = setup(searching(), completedResponse());
|
|
705
|
+
target.create.mockResolvedValueOnce(
|
|
706
|
+
eventsFrom([
|
|
707
|
+
{ type: 'response.output_text.delta', delta: 'Looking.' },
|
|
708
|
+
{ type: 'response.completed', response: searchingReply() },
|
|
709
|
+
]),
|
|
710
|
+
);
|
|
711
|
+
const deltas: string[] = [];
|
|
712
|
+
|
|
713
|
+
const first = await firstReply(target, { onTextDelta: (delta) => deltas.push(delta) });
|
|
714
|
+
|
|
715
|
+
expect(deltas).toEqual(['Looking.']);
|
|
716
|
+
const recorded = first.metadata?.[PROVIDER_CONTINUATION_METADATA_KEY] as string;
|
|
717
|
+
expect(JSON.parse(recorded).openaiOutput).toEqual(SEARCHING_OUTPUT);
|
|
718
|
+
expect(await inputAfter(target, [...messages, first, toolResult])).toEqual([
|
|
719
|
+
expect.objectContaining({ role: 'user' }),
|
|
720
|
+
...SEARCHING_OUTPUT,
|
|
721
|
+
toolOutput,
|
|
722
|
+
]);
|
|
723
|
+
});
|
|
724
|
+
|
|
725
|
+
it('rebuilds the reply from its text once a new user message follows it', async () => {
|
|
726
|
+
const target = setup(searching(), searchingReply());
|
|
727
|
+
const first = await firstReply(target);
|
|
728
|
+
|
|
729
|
+
expect(await inputAfter(target, [...messages, first, toolResult])).toContainEqual(REASONING);
|
|
730
|
+
expect(await inputAfter(target, [...messages, first, toolResult, nextUser])).toEqual([
|
|
731
|
+
{ role: 'user', content: 'hello' },
|
|
732
|
+
...REBUILT,
|
|
733
|
+
toolOutput,
|
|
734
|
+
{ role: 'user', content: 'And Node 25?' },
|
|
735
|
+
]);
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
it.each([
|
|
739
|
+
['the provider option', { store: false }, {}],
|
|
740
|
+
['the response', {}, { store: false }],
|
|
741
|
+
])('keeps nothing of a reply that %s says was not stored', async (_label, options, fields) => {
|
|
742
|
+
const control = await firstReply(setup(searching(), searchingReply()));
|
|
743
|
+
expect(control.metadata).toHaveProperty(PROVIDER_CONTINUATION_METADATA_KEY);
|
|
744
|
+
|
|
745
|
+
const target = setup(searching(options), searchingReply(SEARCHING_OUTPUT, fields));
|
|
746
|
+
const first = await firstReply(target);
|
|
747
|
+
|
|
748
|
+
expect(first.metadata).not.toHaveProperty(PROVIDER_CONTINUATION_METADATA_KEY);
|
|
749
|
+
expect(await inputAfter(target, [...messages, first, toolResult])).toEqual([
|
|
750
|
+
{ role: 'user', content: 'hello' },
|
|
751
|
+
...REBUILT,
|
|
752
|
+
toolOutput,
|
|
753
|
+
]);
|
|
754
|
+
});
|
|
755
|
+
|
|
756
|
+
describe('keeps nothing of a reply', () => {
|
|
757
|
+
it('that searched without calling a function', async () => {
|
|
758
|
+
const first = await firstReply(
|
|
759
|
+
setup(searching(), searchingReply([REASONING, SEARCH, NARRATION])),
|
|
760
|
+
);
|
|
761
|
+
|
|
762
|
+
expect(first.metadata).not.toHaveProperty(PROVIDER_CONTINUATION_METADATA_KEY);
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
it('that called a function without searching', async () => {
|
|
766
|
+
const first = await firstReply(
|
|
767
|
+
setup(searching(), searchingReply([REASONING, NARRATION, CALL])),
|
|
768
|
+
);
|
|
769
|
+
|
|
770
|
+
expect(first.metadata).not.toHaveProperty(PROVIDER_CONTINUATION_METADATA_KEY);
|
|
771
|
+
});
|
|
772
|
+
|
|
773
|
+
it('from a stream when the provider option says it was not stored', async () => {
|
|
774
|
+
const target = setup(
|
|
775
|
+
searching({ store: false }),
|
|
776
|
+
eventsFrom([
|
|
777
|
+
{ type: 'response.output_text.delta', delta: 'Looking.' },
|
|
778
|
+
{ type: 'response.completed', response: searchingReply() },
|
|
779
|
+
]),
|
|
780
|
+
);
|
|
781
|
+
|
|
782
|
+
const first = await firstReply(target, { onTextDelta: () => undefined });
|
|
783
|
+
|
|
784
|
+
expect(first.toolCalls).toHaveLength(1);
|
|
785
|
+
expect(first.metadata).not.toHaveProperty(PROVIDER_CONTINUATION_METADATA_KEY);
|
|
786
|
+
});
|
|
787
|
+
|
|
788
|
+
it('from a stream that ended without a completed response', async () => {
|
|
789
|
+
const target = setup(
|
|
790
|
+
searching(),
|
|
791
|
+
eventsFrom([
|
|
792
|
+
{ type: 'response.output_item.done', item: SEARCH },
|
|
793
|
+
{ type: 'response.output_item.done', item: CALL },
|
|
794
|
+
{ type: 'response.output_text.delta', delta: 'Looking.' },
|
|
795
|
+
]),
|
|
796
|
+
);
|
|
797
|
+
|
|
798
|
+
const first = await firstReply(target, { onTextDelta: () => undefined });
|
|
799
|
+
|
|
800
|
+
expect(first.toolCalls).toHaveLength(1);
|
|
801
|
+
expect(first.metadata).not.toHaveProperty(PROVIDER_CONTINUATION_METADATA_KEY);
|
|
802
|
+
});
|
|
803
|
+
});
|
|
804
|
+
|
|
805
|
+
describe('a recorded reply the conversation no longer agrees with', () => {
|
|
806
|
+
/** A real reply, changed after it was recorded, sent back on the instance that recorded it. */
|
|
807
|
+
async function inputWith(
|
|
808
|
+
change: (reply: IAssistantMessage) => IAssistantMessage,
|
|
809
|
+
): Promise<Array<Record<string, unknown>>> {
|
|
810
|
+
const target = setup(searching(), searchingReply());
|
|
811
|
+
const first = await firstReply(target);
|
|
812
|
+
return inputAfter(target, [...messages, change(first), toolResult]);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
function withContinuation(reply: IAssistantMessage, encoded: string): IAssistantMessage {
|
|
816
|
+
return {
|
|
817
|
+
...reply,
|
|
818
|
+
metadata: { ...reply.metadata, [PROVIDER_CONTINUATION_METADATA_KEY]: encoded },
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
it('goes back whole while it still agrees', async () => {
|
|
823
|
+
const input = await inputWith((reply) => reply);
|
|
824
|
+
|
|
825
|
+
expect(input).toContainEqual(SEARCH);
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
it.each<[string, (reply: IAssistantMessage) => IAssistantMessage]>([
|
|
829
|
+
['text', (reply) => ({ ...reply, content: 'edited' })],
|
|
830
|
+
[
|
|
831
|
+
'call arguments',
|
|
832
|
+
(reply) => ({
|
|
833
|
+
...reply,
|
|
834
|
+
toolCalls: [
|
|
835
|
+
{
|
|
836
|
+
id: 'call_1',
|
|
837
|
+
type: 'function',
|
|
838
|
+
function: { name: 'lookup', arguments: '{"key":"REDACTED"}' },
|
|
839
|
+
},
|
|
840
|
+
],
|
|
841
|
+
}),
|
|
842
|
+
],
|
|
843
|
+
['owner', (reply) => withContinuation(reply, '{"geminiParts":[]}')],
|
|
844
|
+
[
|
|
845
|
+
'scope',
|
|
846
|
+
(reply) =>
|
|
847
|
+
withContinuation(
|
|
848
|
+
reply,
|
|
849
|
+
JSON.stringify({ replayScope: 'another/gpt-4o', openaiOutput: SEARCHING_OUTPUT }),
|
|
850
|
+
),
|
|
851
|
+
],
|
|
852
|
+
['encoding', (reply) => withContinuation(reply, 'not json')],
|
|
853
|
+
])('is rebuilt when its %s differs', async (_label, change) => {
|
|
854
|
+
const input = await inputWith(change);
|
|
855
|
+
|
|
856
|
+
expect(input.map((item) => item['type'])).not.toContain('web_search_call');
|
|
857
|
+
expect(input.map((item) => item['type'])).not.toContain('reasoning');
|
|
858
|
+
expect(input).toContainEqual(
|
|
859
|
+
expect.objectContaining({ type: 'function_call', call_id: 'call_1' }),
|
|
860
|
+
);
|
|
861
|
+
});
|
|
862
|
+
});
|
|
863
|
+
|
|
864
|
+
describe('a reply recorded for another sender', () => {
|
|
865
|
+
const rebuilt = [{ role: 'user', content: 'hello' }, ...REBUILT, toolOutput];
|
|
866
|
+
const whole = [{ role: 'user', content: 'hello' }, ...SEARCHING_OUTPUT, toolOutput];
|
|
867
|
+
|
|
868
|
+
it('is rebuilt on another model, and goes back whole on the model that recorded it', async () => {
|
|
869
|
+
const target = setup(searching(), searchingReply());
|
|
870
|
+
const first = await firstReply(target);
|
|
871
|
+
const history = [...messages, first, toolResult];
|
|
872
|
+
|
|
873
|
+
expect(await inputAfter(target, history, { model: 'gpt-4.1' })).toEqual(rebuilt);
|
|
874
|
+
expect((target.create.mock.calls.at(-1)?.[0] as { model: string }).model).toBe('gpt-4.1');
|
|
875
|
+
expect(await inputAfter(target, history, { model: 'gpt-4o' })).toEqual(whole);
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
it("is rebuilt by another provider instance: another profile's account, or a restarted process", async () => {
|
|
879
|
+
const recorder = setup(searching(), searchingReply());
|
|
880
|
+
const first = await firstReply(recorder);
|
|
881
|
+
const history = [...messages, first, toolResult];
|
|
882
|
+
|
|
883
|
+
expect(await inputAfter(setup(searching()), history)).toEqual(rebuilt);
|
|
884
|
+
expect(await inputAfter(recorder, history)).toEqual(whole);
|
|
885
|
+
});
|
|
886
|
+
});
|
|
887
|
+
|
|
888
|
+
it.each<[string, IChatOptions]>([
|
|
889
|
+
['withholds search', { nativeWebTools: { webSearch: false } }],
|
|
890
|
+
['forbids tools', { toolChoice: 'none' }],
|
|
891
|
+
['forces a function', { toolChoice: 'required' }],
|
|
892
|
+
])(
|
|
893
|
+
'rebuilds the reply on a request that %s, which does not declare the tool',
|
|
894
|
+
async (_l, options) => {
|
|
895
|
+
const target = setup(searching(), searchingReply());
|
|
896
|
+
const first = await firstReply(target);
|
|
897
|
+
const history = [...messages, first, toolResult];
|
|
898
|
+
|
|
899
|
+
expect(await inputAfter(target, history)).toContainEqual(SEARCH);
|
|
900
|
+
expect(await inputAfter(target, history, options)).toEqual([
|
|
901
|
+
{ role: 'user', content: 'hello' },
|
|
902
|
+
...REBUILT,
|
|
903
|
+
toolOutput,
|
|
904
|
+
]);
|
|
905
|
+
},
|
|
906
|
+
);
|
|
907
|
+
|
|
908
|
+
it('rebuilds the reply for the forced summary, the last-resort answer', async () => {
|
|
909
|
+
const target = setup(searching());
|
|
910
|
+
target.create
|
|
911
|
+
.mockResolvedValueOnce(
|
|
912
|
+
eventsFrom([
|
|
913
|
+
{ type: 'response.output_text.delta', delta: 'Looking.' },
|
|
914
|
+
{ type: 'response.completed', response: searchingReply() },
|
|
915
|
+
]),
|
|
916
|
+
)
|
|
917
|
+
.mockResolvedValueOnce(
|
|
918
|
+
eventsFrom([
|
|
919
|
+
{ type: 'response.output_text.delta', delta: 'Summary.' },
|
|
920
|
+
{ type: 'response.completed', response: completedResponse() },
|
|
921
|
+
]),
|
|
922
|
+
);
|
|
923
|
+
const agent = new ConversationAgent({
|
|
924
|
+
name: 'wire',
|
|
925
|
+
aiProviders: [target.provider],
|
|
926
|
+
defaultModel: { provider: target.provider.name, model: 'gpt-4o' },
|
|
927
|
+
tools: [new LookupTool()],
|
|
928
|
+
});
|
|
929
|
+
|
|
930
|
+
await agent.run('What changed in Node 24?', { maxExecutionRounds: 1 });
|
|
931
|
+
|
|
932
|
+
expect(target.create).toHaveBeenCalledTimes(2);
|
|
933
|
+
const request = target.create.mock.calls[1]?.[0] as TRequest & { input: unknown[] };
|
|
934
|
+
expect(request.tools ?? []).not.toContainEqual(WEB_SEARCH_TOOL);
|
|
935
|
+
expect(request.input.slice(-4)).toEqual([
|
|
936
|
+
...REBUILT,
|
|
937
|
+
toolOutput,
|
|
938
|
+
{ role: 'user', content: expect.stringContaining('Tool round limit reached') },
|
|
939
|
+
]);
|
|
940
|
+
});
|
|
941
|
+
});
|