@theia/ai-openai 1.77.0-next.95 → 1.77.0
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/lib/browser/openai-frontend-application-contribution.d.ts +1 -1
- package/lib/browser/openai-frontend-application-contribution.d.ts.map +1 -1
- package/lib/browser/openai-frontend-application-contribution.js +55 -60
- package/lib/browser/openai-frontend-application-contribution.js.map +1 -1
- package/lib/browser/openai-frontend-application-contribution.spec.js +13 -15
- package/lib/browser/openai-frontend-application-contribution.spec.js.map +1 -1
- package/lib/browser/openai-frontend-module.js +13 -15
- package/lib/browser/openai-frontend-module.js.map +1 -1
- package/lib/common/index.d.ts +1 -1
- package/lib/common/index.d.ts.map +1 -1
- package/lib/common/index.js +1 -4
- package/lib/common/index.js.map +1 -1
- package/lib/common/openai-language-models-manager.js +3 -6
- package/lib/common/openai-language-models-manager.js.map +1 -1
- package/lib/common/openai-preferences.js +52 -55
- package/lib/common/openai-preferences.js.map +1 -1
- package/lib/node/openai-backend-module.d.ts.map +1 -1
- package/lib/node/openai-backend-module.js +31 -31
- package/lib/node/openai-backend-module.js.map +1 -1
- package/lib/node/openai-chat-completion-stream.d.ts +6 -9
- package/lib/node/openai-chat-completion-stream.d.ts.map +1 -1
- package/lib/node/openai-chat-completion-stream.js +34 -43
- package/lib/node/openai-chat-completion-stream.js.map +1 -1
- package/lib/node/openai-chat-completion-stream.spec.js +159 -76
- package/lib/node/openai-chat-completion-stream.spec.js.map +1 -1
- package/lib/node/openai-language-model.d.ts +4 -3
- package/lib/node/openai-language-model.d.ts.map +1 -1
- package/lib/node/openai-language-model.js +68 -76
- package/lib/node/openai-language-model.js.map +1 -1
- package/lib/node/openai-language-model.spec.js +137 -75
- package/lib/node/openai-language-model.spec.js.map +1 -1
- package/lib/node/openai-language-models-manager-impl.d.ts +3 -3
- package/lib/node/openai-language-models-manager-impl.d.ts.map +1 -1
- package/lib/node/openai-language-models-manager-impl.js +29 -36
- package/lib/node/openai-language-models-manager-impl.js.map +1 -1
- package/lib/node/openai-language-models-manager-impl.spec.js +34 -36
- package/lib/node/openai-language-models-manager-impl.spec.js.map +1 -1
- package/lib/node/openai-model-defaults.d.ts +1 -1
- package/lib/node/openai-model-defaults.d.ts.map +1 -1
- package/lib/node/openai-model-defaults.js +1 -4
- package/lib/node/openai-model-defaults.js.map +1 -1
- package/lib/node/openai-model-defaults.spec.js +52 -54
- package/lib/node/openai-model-defaults.spec.js.map +1 -1
- package/lib/node/openai-model-utils.d.ts +1 -1
- package/lib/node/openai-model-utils.d.ts.map +1 -1
- package/lib/node/openai-model-utils.js +14 -17
- package/lib/node/openai-model-utils.js.map +1 -1
- package/lib/node/openai-model-utils.spec.js +45 -47
- package/lib/node/openai-model-utils.spec.js.map +1 -1
- package/lib/node/openai-reasoning.js +1 -4
- package/lib/node/openai-reasoning.js.map +1 -1
- package/lib/node/openai-request-settings.js +9 -13
- package/lib/node/openai-request-settings.js.map +1 -1
- package/lib/node/openai-response-api-utils.d.ts +5 -3
- package/lib/node/openai-response-api-utils.d.ts.map +1 -1
- package/lib/node/openai-response-api-utils.js +84 -70
- package/lib/node/openai-response-api-utils.js.map +1 -1
- package/lib/node/openai-response-api-utils.spec.js +320 -166
- package/lib/node/openai-response-api-utils.spec.js.map +1 -1
- package/lib/node/openai-server-tools.js +4 -7
- package/lib/node/openai-server-tools.js.map +1 -1
- package/lib/node/openai-streaming-iterator.d.ts +2 -1
- package/lib/node/openai-streaming-iterator.d.ts.map +1 -1
- package/lib/node/openai-streaming-iterator.js +5 -8
- package/lib/node/openai-streaming-iterator.js.map +1 -1
- package/lib/node/openai-streaming-iterator.spec.js +37 -40
- package/lib/node/openai-streaming-iterator.spec.js.map +1 -1
- package/lib/node/openai-streaming-retry.d.ts +8 -0
- package/lib/node/openai-streaming-retry.d.ts.map +1 -0
- package/lib/node/openai-streaming-retry.js +108 -0
- package/lib/node/openai-streaming-retry.js.map +1 -0
- package/lib/node/openai-streaming-retry.spec.d.ts +2 -0
- package/lib/node/openai-streaming-retry.spec.d.ts.map +1 -0
- package/lib/node/openai-streaming-retry.spec.js +182 -0
- package/lib/node/openai-streaming-retry.spec.js.map +1 -0
- package/lib/node/streaming-response-iterator.js +5 -9
- package/lib/node/streaming-response-iterator.js.map +1 -1
- package/lib/node/streaming-response-iterator.spec.js +6 -8
- package/lib/node/streaming-response-iterator.spec.js.map +1 -1
- package/package.json +16 -7
- package/src/browser/openai-frontend-application-contribution.spec.ts +3 -3
- package/src/browser/openai-frontend-application-contribution.ts +2 -2
- package/src/browser/openai-frontend-module.ts +3 -3
- package/src/common/index.ts +1 -1
- package/src/node/openai-backend-module.ts +9 -7
- package/src/node/openai-chat-completion-stream.spec.ts +88 -3
- package/src/node/openai-chat-completion-stream.ts +20 -15
- package/src/node/openai-language-model.spec.ts +84 -7
- package/src/node/openai-language-model.ts +37 -32
- package/src/node/openai-language-models-manager-impl.spec.ts +3 -3
- package/src/node/openai-language-models-manager-impl.ts +4 -4
- package/src/node/openai-model-defaults.spec.ts +1 -1
- package/src/node/openai-model-defaults.ts +1 -1
- package/src/node/openai-model-utils.spec.ts +2 -2
- package/src/node/openai-model-utils.ts +2 -2
- package/src/node/openai-request-settings.ts +1 -1
- package/src/node/openai-response-api-utils.spec.ts +180 -27
- package/src/node/openai-response-api-utils.ts +45 -21
- package/src/node/openai-streaming-iterator.spec.ts +1 -1
- package/src/node/openai-streaming-iterator.ts +2 -1
- package/src/node/openai-streaming-retry.spec.ts +195 -0
- package/src/node/openai-streaming-retry.ts +110 -0
- package/src/node/streaming-response-iterator.spec.ts +1 -1
- package/lib/node/openai-request-api-context.d.ts +0 -4
- package/lib/node/openai-request-api-context.d.ts.map +0 -1
- package/lib/node/openai-request-api-context.js +0 -19
- package/lib/node/openai-request-api-context.js.map +0 -1
- package/src/node/openai-request-api-context.ts +0 -23
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
// *****************************************************************************
|
|
3
2
|
// Copyright (C) 2026 EclipseSource and others.
|
|
4
3
|
//
|
|
@@ -14,19 +13,18 @@
|
|
|
14
13
|
//
|
|
15
14
|
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
16
15
|
// *****************************************************************************
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const openai_server_tools_1 = require("./openai-server-tools");
|
|
16
|
+
import { expect } from 'chai';
|
|
17
|
+
import * as sinon from 'sinon';
|
|
18
|
+
import { isCompactionResponsePart, isServerToolCallResponsePart, isTextResponsePart, isThinkingResponsePart, isToolCallResponsePart, isUsageResponsePart, ToolCallExecutor, ToolCallExecutorImpl } from '@theia/ai-core';
|
|
19
|
+
import { CancellationError, CancellationTokenSource, ILogger } from '@theia/core';
|
|
20
|
+
import { Container } from '@theia/core/shared/inversify';
|
|
21
|
+
import { MockLogger } from '@theia/core/lib/common/test/mock-logger';
|
|
22
|
+
import { Deferred } from '@theia/core/lib/common/promise-util';
|
|
23
|
+
import { OpenAiModelUtils } from './openai-model-utils.js';
|
|
24
|
+
import { OpenAI } from 'openai';
|
|
25
|
+
import { OPENAI_FUNCTION_CALL_REASONING_DATA_KEY, OpenAiResponseApiUtils } from './openai-response-api-utils.js';
|
|
26
|
+
import { OPENAI_WEB_SEARCH, OPENAI_WEB_SEARCH_REPLAY_DATA_KEY } from './openai-server-tools.js';
|
|
27
|
+
import { OpenAiStreamingRetry } from './openai-streaming-retry.js';
|
|
30
28
|
async function* toStream(events) {
|
|
31
29
|
for (const event of events) {
|
|
32
30
|
yield event;
|
|
@@ -41,12 +39,13 @@ function outputItemEvents(item, outputIndex) {
|
|
|
41
39
|
{ type: 'response.output_item.done', output_index: outputIndex, item }
|
|
42
40
|
];
|
|
43
41
|
}
|
|
44
|
-
function createTestUtils() {
|
|
45
|
-
const container = new
|
|
46
|
-
container.bind(
|
|
47
|
-
container.bind(
|
|
48
|
-
container.bind(
|
|
49
|
-
|
|
42
|
+
function createTestUtils(logger = new MockLogger()) {
|
|
43
|
+
const container = new Container();
|
|
44
|
+
container.bind(ILogger).toConstantValue(logger);
|
|
45
|
+
container.bind(OpenAiStreamingRetry).toSelf();
|
|
46
|
+
container.bind(ToolCallExecutor).to(ToolCallExecutorImpl).inSingletonScope();
|
|
47
|
+
container.bind(OpenAiResponseApiUtils).toSelf();
|
|
48
|
+
return container.get(OpenAiResponseApiUtils);
|
|
50
49
|
}
|
|
51
50
|
describe('OpenAiResponseApiUtils', () => {
|
|
52
51
|
let utils;
|
|
@@ -69,9 +68,9 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
69
68
|
parameters,
|
|
70
69
|
handler: async () => 'result'
|
|
71
70
|
}]);
|
|
72
|
-
|
|
71
|
+
expect(converted).to.have.lengthOf(1);
|
|
73
72
|
const convertedTool = converted?.[0];
|
|
74
|
-
|
|
73
|
+
expect(convertedTool).to.include({
|
|
75
74
|
type: 'function',
|
|
76
75
|
name: 'lookup',
|
|
77
76
|
parameters,
|
|
@@ -80,13 +79,13 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
80
79
|
if (convertedTool?.type !== 'function') {
|
|
81
80
|
throw new Error('Expected a function tool');
|
|
82
81
|
}
|
|
83
|
-
|
|
82
|
+
expect(convertedTool.parameters).to.equal(parameters);
|
|
84
83
|
});
|
|
85
84
|
it('adds native web search without requiring client tools', () => {
|
|
86
|
-
|
|
85
|
+
expect(utils.convertToolsForResponseApi(undefined, undefined, [OPENAI_WEB_SEARCH])).to.deep.equal([
|
|
87
86
|
{ type: 'web_search' }
|
|
88
87
|
]);
|
|
89
|
-
|
|
88
|
+
expect(utils.convertToolsForResponseApi()).to.equal(undefined);
|
|
90
89
|
});
|
|
91
90
|
it('combines native web search with function and deferred-tool search tools', () => {
|
|
92
91
|
const tools = utils.convertToolsForResponseApi([{
|
|
@@ -94,8 +93,8 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
94
93
|
name: 'lookup',
|
|
95
94
|
parameters: { type: 'object', properties: {} },
|
|
96
95
|
handler: async () => 'result'
|
|
97
|
-
}], ['lookup'], [
|
|
98
|
-
|
|
96
|
+
}], ['lookup'], [OPENAI_WEB_SEARCH]);
|
|
97
|
+
expect(tools?.map(tool => tool.type)).to.deep.equal(['function', 'tool_search', 'web_search']);
|
|
99
98
|
});
|
|
100
99
|
it('preserves required output fields when serializing historical empty tool results', () => {
|
|
101
100
|
const messages = [
|
|
@@ -108,7 +107,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
108
107
|
];
|
|
109
108
|
const { input } = utils.processMessages(messages, 'developer', 'gpt-5');
|
|
110
109
|
// Assert the wire representation: JSON serialization must not drop the required output property.
|
|
111
|
-
|
|
110
|
+
expect(JSON.parse(JSON.stringify(input))).to.deep.equal([
|
|
112
111
|
{ type: 'function_call_output', call_id: 'missing', output: '' },
|
|
113
112
|
{ type: 'function_call_output', call_id: 'undefined', output: '' },
|
|
114
113
|
{ type: 'function_call_output', call_id: 'empty', output: '' },
|
|
@@ -132,15 +131,12 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
132
131
|
};
|
|
133
132
|
const openai = {
|
|
134
133
|
responses: {
|
|
135
|
-
stream: (payload) => {
|
|
136
|
-
recordRequest(payload);
|
|
137
|
-
return toStream(payloads.length === 1
|
|
138
|
-
? functionCalls.flatMap((item, index) => outputItemEvents(item, index))
|
|
139
|
-
: [{ type: 'response.output_text.delta', delta: 'done' }]);
|
|
140
|
-
},
|
|
141
134
|
create: async (payload) => {
|
|
142
135
|
recordRequest(payload);
|
|
143
|
-
return
|
|
136
|
+
return payload.stream ? toStream(payloads.length === 1
|
|
137
|
+
? functionCalls.flatMap((item, index) => outputItemEvents(item, index))
|
|
138
|
+
: [{ type: 'response.output_text.delta', delta: 'done' }])
|
|
139
|
+
: { output: payloads.length === 1 ? functionCalls : [], output_text: payloads.length === 1 ? '' : 'done' };
|
|
144
140
|
}
|
|
145
141
|
}
|
|
146
142
|
};
|
|
@@ -156,7 +152,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
156
152
|
{ id: 'failed', name: 'failed', parameters: { type: 'object', properties: {} }, handler: async () => { throw new Error('failed'); } }
|
|
157
153
|
]
|
|
158
154
|
};
|
|
159
|
-
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
155
|
+
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', isStreaming);
|
|
160
156
|
if (!('stream' in response)) {
|
|
161
157
|
throw new Error('Expected the tool loop to return a stream');
|
|
162
158
|
}
|
|
@@ -164,11 +160,11 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
164
160
|
for await (const part of response.stream) {
|
|
165
161
|
parts.push(part);
|
|
166
162
|
}
|
|
167
|
-
|
|
168
|
-
|
|
163
|
+
expect(parts.filter(isTextResponsePart)).to.deep.equal([{ content: 'done' }]);
|
|
164
|
+
expect(payloads).to.have.length(2);
|
|
169
165
|
const input = payloads[1].input;
|
|
170
|
-
|
|
171
|
-
|
|
166
|
+
expect(input.filter(item => item.type === 'function_call')).to.deep.equal(functionCalls);
|
|
167
|
+
expect(input.filter(item => item.type === 'function_call_output')).to.deep.equal([
|
|
172
168
|
{ type: 'function_call_output', call_id: 'call-0', output: '' },
|
|
173
169
|
{ type: 'function_call_output', call_id: 'call-1', output: '' },
|
|
174
170
|
{ type: 'function_call_output', call_id: 'call-2', output: '{"value":1}' },
|
|
@@ -213,7 +209,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
213
209
|
const streamRequests = [];
|
|
214
210
|
const openai = {
|
|
215
211
|
responses: {
|
|
216
|
-
|
|
212
|
+
create: async (responseRequest) => {
|
|
217
213
|
streamRequests.push(responseRequest);
|
|
218
214
|
return toStream(streams.shift() ?? []);
|
|
219
215
|
}
|
|
@@ -223,7 +219,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
223
219
|
sessionId: 'session-1',
|
|
224
220
|
requestId: 'request-1',
|
|
225
221
|
messages: [{ actor: 'user', type: 'text', text: 'hello' }],
|
|
226
|
-
serverTools: [
|
|
222
|
+
serverTools: [OPENAI_WEB_SEARCH],
|
|
227
223
|
tools: [{
|
|
228
224
|
id: 'lookup',
|
|
229
225
|
name: 'lookup',
|
|
@@ -231,19 +227,19 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
231
227
|
handler: async () => 'result'
|
|
232
228
|
}]
|
|
233
229
|
};
|
|
234
|
-
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
230
|
+
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
235
231
|
const parts = [];
|
|
236
232
|
if ('stream' in response) {
|
|
237
233
|
for await (const part of response.stream) {
|
|
238
234
|
parts.push(part);
|
|
239
235
|
}
|
|
240
236
|
}
|
|
241
|
-
|
|
237
|
+
expect(parts.filter(isUsageResponsePart)).to.deep.equal([
|
|
242
238
|
{ input_tokens: 100, output_tokens: 10 },
|
|
243
239
|
{ input_tokens: 200, output_tokens: 20 }
|
|
244
240
|
]);
|
|
245
|
-
|
|
246
|
-
|
|
241
|
+
expect(streamRequests).to.have.length(2);
|
|
242
|
+
expect(streamRequests.every(streamRequest => JSON.stringify(streamRequest.include) === JSON.stringify(['web_search_call.action.sources', 'reasoning.encrypted_content']))).to.equal(true);
|
|
247
243
|
});
|
|
248
244
|
it('preserves reasoning and web search items for the next function-tool iteration', async () => {
|
|
249
245
|
const reasoningItem = { id: 'rs-1', type: 'reasoning', summary: [], encrypted_content: 'encrypted-reasoning' };
|
|
@@ -264,7 +260,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
264
260
|
const streamRequests = [];
|
|
265
261
|
const openai = {
|
|
266
262
|
responses: {
|
|
267
|
-
|
|
263
|
+
create: async (responseRequest) => {
|
|
268
264
|
streamRequests.push(responseRequest);
|
|
269
265
|
return toStream(streams.shift() ?? []);
|
|
270
266
|
}
|
|
@@ -274,7 +270,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
274
270
|
sessionId: 'session-1',
|
|
275
271
|
requestId: 'request-1',
|
|
276
272
|
messages: [{ actor: 'user', type: 'text', text: 'hello' }],
|
|
277
|
-
serverTools: [
|
|
273
|
+
serverTools: [OPENAI_WEB_SEARCH],
|
|
278
274
|
tools: [{
|
|
279
275
|
id: 'lookup',
|
|
280
276
|
name: 'lookup',
|
|
@@ -282,25 +278,25 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
282
278
|
handler: async () => 'result'
|
|
283
279
|
}]
|
|
284
280
|
};
|
|
285
|
-
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
281
|
+
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
286
282
|
const parts = [];
|
|
287
283
|
if ('stream' in response) {
|
|
288
284
|
for await (const part of response.stream) {
|
|
289
285
|
parts.push(part);
|
|
290
286
|
}
|
|
291
287
|
}
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
288
|
+
expect(parts).to.not.be.empty;
|
|
289
|
+
expect(streamRequests).to.have.length(2);
|
|
290
|
+
expect(streamRequests[1].input).to.deep.include.members([reasoningItem, searchCall]);
|
|
295
291
|
const input = streamRequests[1].input;
|
|
296
|
-
|
|
292
|
+
expect(input.indexOf(reasoningItem)).to.be.lessThan(input.indexOf(searchCall));
|
|
297
293
|
});
|
|
298
294
|
for (const withTools of [false, true]) {
|
|
299
295
|
it(`emits and replays OpenAI compaction ${withTools ? 'with client tools' : 'without tools'}`, async () => {
|
|
300
296
|
const payloads = [];
|
|
301
297
|
const openai = {
|
|
302
298
|
responses: {
|
|
303
|
-
|
|
299
|
+
create: async (payload) => {
|
|
304
300
|
payloads.push(payload);
|
|
305
301
|
return toStream(payloads.length === 1 ? [
|
|
306
302
|
{ type: 'response.output_item.done', output_index: 0, item: { type: 'compaction', id: 'c1', encrypted_content: 'enc1' } },
|
|
@@ -315,16 +311,16 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
315
311
|
messages: [{ actor: 'user', type: 'text', text: 'hello' }],
|
|
316
312
|
tools: withTools ? [{ id: 'lookup', name: 'lookup', parameters: { type: 'object', properties: {} }, handler: async () => 'result' }] : undefined
|
|
317
313
|
};
|
|
318
|
-
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
314
|
+
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
319
315
|
const parts = [];
|
|
320
|
-
|
|
316
|
+
expect('stream' in response).to.equal(true);
|
|
321
317
|
if ('stream' in response) {
|
|
322
318
|
for await (const part of response.stream) {
|
|
323
319
|
parts.push(part);
|
|
324
320
|
}
|
|
325
321
|
}
|
|
326
|
-
const compactionParts = parts.filter(
|
|
327
|
-
|
|
322
|
+
const compactionParts = parts.filter(isCompactionResponsePart);
|
|
323
|
+
expect(compactionParts).to.deep.equal([{
|
|
328
324
|
compaction: { provider: 'openai-responses', data: { id: 'c1', encrypted_content: 'enc1' } }
|
|
329
325
|
}]);
|
|
330
326
|
const replayRequest = {
|
|
@@ -335,17 +331,17 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
335
331
|
{ actor: 'user', type: 'text', text: 'Next turn' }
|
|
336
332
|
]
|
|
337
333
|
};
|
|
338
|
-
const replay = await utils.handleRequest(openai, replayRequest, {}, 'gpt-5', new
|
|
334
|
+
const replay = await utils.handleRequest(openai, replayRequest, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
339
335
|
const replayParts = [];
|
|
340
|
-
|
|
336
|
+
expect('stream' in replay).to.equal(true);
|
|
341
337
|
if ('stream' in replay) {
|
|
342
338
|
for await (const part of replay.stream) {
|
|
343
339
|
replayParts.push(part);
|
|
344
340
|
}
|
|
345
341
|
}
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
342
|
+
expect(replayParts.filter(isTextResponsePart)).to.deep.equal([{ content: 'Continued answer' }]);
|
|
343
|
+
expect(payloads).to.have.lengthOf(2);
|
|
344
|
+
expect(payloads[1].input).to.deep.equal([
|
|
349
345
|
{ type: 'compaction', id: 'c1', encrypted_content: 'enc1' },
|
|
350
346
|
{ type: 'message', role: 'user', content: [{ type: 'input_text', text: 'Next turn' }] }
|
|
351
347
|
]);
|
|
@@ -370,7 +366,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
370
366
|
const payloads = [];
|
|
371
367
|
const openai = {
|
|
372
368
|
responses: {
|
|
373
|
-
|
|
369
|
+
create: async (payload) => {
|
|
374
370
|
payloads.push(payload);
|
|
375
371
|
return toStream(streams.shift() ?? []);
|
|
376
372
|
}
|
|
@@ -388,24 +384,24 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
388
384
|
handler: async () => 'result'
|
|
389
385
|
}]
|
|
390
386
|
};
|
|
391
|
-
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
387
|
+
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
392
388
|
const parts = [];
|
|
393
389
|
if ('stream' in response) {
|
|
394
390
|
for await (const part of response.stream) {
|
|
395
391
|
parts.push(part);
|
|
396
392
|
}
|
|
397
393
|
}
|
|
398
|
-
const calls = parts.filter(
|
|
394
|
+
const calls = parts.filter(isServerToolCallResponsePart).flatMap(p => p.server_tool_calls);
|
|
399
395
|
const running = calls.find(c => !c.finished);
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
396
|
+
expect(running).to.not.be.undefined;
|
|
397
|
+
expect(running.id).to.equal('ts-1');
|
|
398
|
+
expect(running.name).to.equal('tool_search');
|
|
403
399
|
const finished = calls.find(c => c.finished);
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
400
|
+
expect(finished).to.not.be.undefined;
|
|
401
|
+
expect(finished.id).to.equal('ts-1');
|
|
402
|
+
expect(finished.name).to.equal('tool_search');
|
|
403
|
+
expect(finished.result).to.deep.equal({ content: [{ type: 'text', text: 'Found 2 tools.' }] });
|
|
404
|
+
expect(payloads[1].input).to.deep.equal([
|
|
409
405
|
...payloads[0].input, searchCall, searchOutput, functionCall,
|
|
410
406
|
{ type: 'function_call_output', call_id: 'call-1', output: 'result' }
|
|
411
407
|
]);
|
|
@@ -425,7 +421,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
425
421
|
};
|
|
426
422
|
const openai = {
|
|
427
423
|
responses: {
|
|
428
|
-
|
|
424
|
+
create: async () => toStream([
|
|
429
425
|
{ type: 'response.output_item.done', output_index: 0, item: reasoningItem },
|
|
430
426
|
{ type: 'response.output_item.added', output_index: 1, item: { ...searchCall, status: 'in_progress' } },
|
|
431
427
|
{ type: 'response.output_item.done', output_index: 1, item: searchCall },
|
|
@@ -438,22 +434,22 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
438
434
|
sessionId: 'session-1',
|
|
439
435
|
requestId: 'request-1',
|
|
440
436
|
messages: [{ actor: 'user', type: 'text', text: 'What is new?' }],
|
|
441
|
-
serverTools: [
|
|
437
|
+
serverTools: [OPENAI_WEB_SEARCH]
|
|
442
438
|
};
|
|
443
|
-
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
439
|
+
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
444
440
|
const parts = [];
|
|
445
441
|
if ('stream' in response) {
|
|
446
442
|
for await (const part of response.stream) {
|
|
447
443
|
parts.push(part);
|
|
448
444
|
}
|
|
449
445
|
}
|
|
450
|
-
const calls = parts.filter(
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
[
|
|
446
|
+
const calls = parts.filter(isServerToolCallResponsePart).flatMap(part => part.server_tool_calls);
|
|
447
|
+
expect(calls).to.have.length(2);
|
|
448
|
+
expect(calls[0]).to.deep.include({ id: 'ws-1', name: OPENAI_WEB_SEARCH, finished: false });
|
|
449
|
+
expect(calls[1]).to.deep.include({ id: 'ws-1', name: OPENAI_WEB_SEARCH, finished: true });
|
|
450
|
+
expect(calls[1].result).to.deep.equal({ content: [{ type: 'text', text: 'Web search failed.' }] });
|
|
451
|
+
expect(calls[1].data).to.deep.equal({
|
|
452
|
+
[OPENAI_WEB_SEARCH_REPLAY_DATA_KEY]: JSON.stringify([reasoningItem, searchCall])
|
|
457
453
|
});
|
|
458
454
|
});
|
|
459
455
|
it('surfaces web search from a non-streaming response and requests sources', async () => {
|
|
@@ -473,19 +469,19 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
473
469
|
sessionId: 'session-1',
|
|
474
470
|
requestId: 'request-1',
|
|
475
471
|
messages: [{ actor: 'user', type: 'text', text: 'Search' }],
|
|
476
|
-
serverTools: [
|
|
472
|
+
serverTools: [OPENAI_WEB_SEARCH]
|
|
477
473
|
};
|
|
478
|
-
const response = await utils.handleRequest(openai, request, { include: ['file_search_call.results', 'web_search_call.action.sources'] }, 'gpt-5', new
|
|
474
|
+
const response = await utils.handleRequest(openai, request, { include: ['file_search_call.results', 'web_search_call.action.sources'] }, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', false);
|
|
479
475
|
const parts = [];
|
|
480
476
|
if ('stream' in response) {
|
|
481
477
|
for await (const part of response.stream) {
|
|
482
478
|
parts.push(part);
|
|
483
479
|
}
|
|
484
480
|
}
|
|
485
|
-
const calls = parts.filter(
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
481
|
+
const calls = parts.filter(isServerToolCallResponsePart).flatMap(part => part.server_tool_calls);
|
|
482
|
+
expect(calls).to.have.length(1);
|
|
483
|
+
expect(calls[0]).to.deep.include({ id: 'ws-1', name: OPENAI_WEB_SEARCH, finished: true });
|
|
484
|
+
expect(createRequest?.include).to.deep.equal(['file_search_call.results', 'web_search_call.action.sources', 'reasoning.encrypted_content']);
|
|
489
485
|
});
|
|
490
486
|
it('replays persisted OpenAI reasoning before its web search call', () => {
|
|
491
487
|
const reasoningItem = {
|
|
@@ -508,15 +504,15 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
508
504
|
actor: 'ai',
|
|
509
505
|
type: 'server_tool_use',
|
|
510
506
|
id: 'ws-1',
|
|
511
|
-
name:
|
|
507
|
+
name: OPENAI_WEB_SEARCH,
|
|
512
508
|
input: searchCall.action,
|
|
513
509
|
result: { content: [{ type: 'text', text: 'Web search completed.' }] },
|
|
514
510
|
data: {
|
|
515
|
-
[
|
|
511
|
+
[OPENAI_WEB_SEARCH_REPLAY_DATA_KEY]: JSON.stringify([reasoningItem, searchCall])
|
|
516
512
|
}
|
|
517
513
|
}];
|
|
518
514
|
const { input } = utils.processMessages(messages, 'developer', 'gpt-5');
|
|
519
|
-
|
|
515
|
+
expect(input).to.deep.equal([reasoningItem, searchCall]);
|
|
520
516
|
});
|
|
521
517
|
it('persists and replays reasoning before a function call', async () => {
|
|
522
518
|
const reasoningItem = { id: 'rs-1', type: 'reasoning', summary: [], encrypted_content: 'encrypted-reasoning' };
|
|
@@ -533,7 +529,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
533
529
|
];
|
|
534
530
|
const openai = {
|
|
535
531
|
responses: {
|
|
536
|
-
|
|
532
|
+
create: async (responseRequest) => {
|
|
537
533
|
requests.push(responseRequest);
|
|
538
534
|
return toStream(streams.shift() ?? []);
|
|
539
535
|
}
|
|
@@ -550,22 +546,22 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
550
546
|
handler: async () => 'result'
|
|
551
547
|
}]
|
|
552
548
|
};
|
|
553
|
-
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
549
|
+
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
554
550
|
const parts = [];
|
|
555
551
|
if ('stream' in response) {
|
|
556
552
|
for await (const part of response.stream) {
|
|
557
553
|
parts.push(part);
|
|
558
554
|
}
|
|
559
555
|
}
|
|
560
|
-
|
|
556
|
+
expect(requests[0].include).to.deep.equal(['reasoning.encrypted_content']);
|
|
561
557
|
const completedCall = parts.flatMap(part => 'tool_calls' in part ? part.tool_calls : []).find(call => call.finished);
|
|
562
|
-
|
|
563
|
-
[
|
|
558
|
+
expect(completedCall?.data).to.deep.equal({
|
|
559
|
+
[OPENAI_FUNCTION_CALL_REASONING_DATA_KEY]: JSON.stringify([reasoningItem])
|
|
564
560
|
});
|
|
565
|
-
|
|
561
|
+
expect(requests[1].input).to.deep.include.members([reasoningItem]);
|
|
566
562
|
const nextInput = requests[1].input;
|
|
567
563
|
const replayedCall = nextInput.find(item => item.type === 'function_call');
|
|
568
|
-
|
|
564
|
+
expect(nextInput.indexOf(reasoningItem)).to.be.lessThan(nextInput.indexOf(replayedCall));
|
|
569
565
|
const { input } = utils.processMessages([{
|
|
570
566
|
actor: 'ai',
|
|
571
567
|
type: 'tool_use',
|
|
@@ -574,13 +570,169 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
574
570
|
input: { query: 'test' },
|
|
575
571
|
data: completedCall?.data
|
|
576
572
|
}], 'developer', 'gpt-5');
|
|
577
|
-
|
|
573
|
+
expect(input).to.deep.equal([reasoningItem, {
|
|
578
574
|
type: 'function_call',
|
|
579
575
|
call_id: 'call-1',
|
|
580
576
|
name: 'lookup',
|
|
581
577
|
arguments: '{"query":"test"}'
|
|
582
578
|
}]);
|
|
583
579
|
});
|
|
580
|
+
describe('transient stream failures', () => {
|
|
581
|
+
let clock;
|
|
582
|
+
let logError;
|
|
583
|
+
const busy = { type: 'response.failed', response: { error: { code: 'server_error', message: 'The server is too busy.' } } };
|
|
584
|
+
const request = { sessionId: 's', requestId: 'r', messages: [{ actor: 'user', type: 'text', text: 'hello' }] };
|
|
585
|
+
beforeEach(() => {
|
|
586
|
+
clock = sinon.useFakeTimers();
|
|
587
|
+
const logger = new MockLogger();
|
|
588
|
+
logError = sinon.spy(logger, 'error');
|
|
589
|
+
utils = createTestUtils(logger);
|
|
590
|
+
});
|
|
591
|
+
afterEach(() => sinon.restore());
|
|
592
|
+
for (const withTools of [false, true]) {
|
|
593
|
+
it(`retries quietly after lifecycle events ${withTools ? 'with tools' : 'without tools'}`, async () => {
|
|
594
|
+
const payloads = [];
|
|
595
|
+
const openai = {
|
|
596
|
+
maxRetries: 1,
|
|
597
|
+
responses: {
|
|
598
|
+
create: async (payload) => {
|
|
599
|
+
payloads.push(payload);
|
|
600
|
+
return toStream(payloads.length === 1
|
|
601
|
+
? [{ type: 'response.created' }, { type: 'response.in_progress' }, busy]
|
|
602
|
+
: [{ type: 'response.output_text.delta', delta: 'done' }]);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
const response = await utils.handleRequest(openai, {
|
|
607
|
+
...request,
|
|
608
|
+
tools: withTools ? [{ id: 'lookup', name: 'lookup', parameters: { type: 'object', properties: {} }, handler: async () => 'result' }] : undefined
|
|
609
|
+
}, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
610
|
+
const parts = [];
|
|
611
|
+
const consume = (async () => {
|
|
612
|
+
if ('stream' in response) {
|
|
613
|
+
for await (const part of response.stream) {
|
|
614
|
+
parts.push(part);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
})();
|
|
618
|
+
await clock.runAllAsync();
|
|
619
|
+
await consume;
|
|
620
|
+
expect(parts).to.deep.equal([{ content: 'done' }]);
|
|
621
|
+
expect(payloads).to.have.length(2);
|
|
622
|
+
expect(payloads[1]).to.deep.equal(payloads[0]);
|
|
623
|
+
expect(logError.called).to.equal(false);
|
|
624
|
+
});
|
|
625
|
+
for (const event of [busy, { type: 'error', code: 'server_error', message: 'Stream failed' }]) {
|
|
626
|
+
it(`logs the terminal ${event.type} once after exhausting retries ${withTools ? 'with tools' : 'without tools'}`, async () => {
|
|
627
|
+
const create = sinon.stub().callsFake(async () => toStream([event]));
|
|
628
|
+
const response = await utils.handleRequest({ maxRetries: 1, responses: { create } }, {
|
|
629
|
+
...request,
|
|
630
|
+
tools: withTools ? [{ id: 'lookup', name: 'lookup', parameters: { type: 'object', properties: {} }, handler: async () => 'result' }] : undefined
|
|
631
|
+
}, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
632
|
+
const consume = (async () => {
|
|
633
|
+
if ('stream' in response) {
|
|
634
|
+
for await (const part of response.stream) {
|
|
635
|
+
expect.fail(`Unexpected output ${JSON.stringify(part)}`);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
})().then(() => undefined, caught => caught);
|
|
639
|
+
await clock.runAllAsync();
|
|
640
|
+
const error = await consume;
|
|
641
|
+
expect(error).to.be.instanceOf(Error).with.property('code', 'server_error');
|
|
642
|
+
expect(create.callCount).to.equal(2);
|
|
643
|
+
expect(logError.calledOnceWithExactly('Response API error:', error)).to.equal(true);
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
it(`cancels during backoff ${withTools ? 'with tools' : 'without tools'}`, async () => {
|
|
647
|
+
const source = new CancellationTokenSource();
|
|
648
|
+
const create = sinon.stub().callsFake(async () => toStream([busy]));
|
|
649
|
+
const response = await utils.handleRequest({ maxRetries: 3, responses: { create } }, {
|
|
650
|
+
...request,
|
|
651
|
+
tools: withTools ? [{ id: 'lookup', name: 'lookup', parameters: { type: 'object', properties: {} }, handler: async () => 'result' }] : undefined
|
|
652
|
+
}, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true, source.token);
|
|
653
|
+
const consume = (async () => {
|
|
654
|
+
try {
|
|
655
|
+
if ('stream' in response) {
|
|
656
|
+
for await (const part of response.stream) {
|
|
657
|
+
expect.fail(`Unexpected output ${JSON.stringify(part)}`);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
catch (error) {
|
|
662
|
+
return error;
|
|
663
|
+
}
|
|
664
|
+
return undefined;
|
|
665
|
+
})();
|
|
666
|
+
await clock.tickAsync(0);
|
|
667
|
+
source.cancel();
|
|
668
|
+
expect(await consume).to.be.instanceOf(CancellationError);
|
|
669
|
+
await clock.runAllAsync();
|
|
670
|
+
expect(create.callCount).to.equal(1);
|
|
671
|
+
expect(create.firstCall.args[1].signal.aborted).to.equal(true);
|
|
672
|
+
expect(logError.called).to.equal(false);
|
|
673
|
+
source.dispose();
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
it('reports cancellation when a simple upstream stream ends normally on abort', async () => {
|
|
677
|
+
const source = new CancellationTokenSource();
|
|
678
|
+
const create = sinon.stub().callsFake(async (_payload, options) => (async function* () {
|
|
679
|
+
yield { type: 'response.created' };
|
|
680
|
+
await new Promise(resolve => options.signal.addEventListener('abort', () => resolve(), { once: true }));
|
|
681
|
+
})());
|
|
682
|
+
const response = await utils.handleRequest({ maxRetries: 3, responses: { create } }, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true, source.token);
|
|
683
|
+
const consume = (async () => {
|
|
684
|
+
try {
|
|
685
|
+
if ('stream' in response) {
|
|
686
|
+
for await (const part of response.stream) {
|
|
687
|
+
expect.fail(`Unexpected output ${JSON.stringify(part)}`);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
catch (error) {
|
|
692
|
+
return error;
|
|
693
|
+
}
|
|
694
|
+
return undefined;
|
|
695
|
+
})();
|
|
696
|
+
await clock.tickAsync(0);
|
|
697
|
+
source.cancel();
|
|
698
|
+
expect(await consume).to.be.instanceOf(CancellationError);
|
|
699
|
+
expect(create.callCount).to.equal(1);
|
|
700
|
+
source.dispose();
|
|
701
|
+
});
|
|
702
|
+
it('retries the next model turn without repeating completed tools', async () => {
|
|
703
|
+
const handler = sinon.stub().resolves('result');
|
|
704
|
+
const payloads = [];
|
|
705
|
+
const openai = {
|
|
706
|
+
maxRetries: 1,
|
|
707
|
+
responses: {
|
|
708
|
+
create: async (payload) => {
|
|
709
|
+
payloads.push(payload);
|
|
710
|
+
return toStream(payloads.length === 1
|
|
711
|
+
? outputItemEvents({ id: 'call-1', call_id: 'call-1', type: 'function_call', name: 'lookup', arguments: '{}' }, 0)
|
|
712
|
+
: payloads.length === 2 ? [busy] : [{ type: 'response.output_text.delta', delta: 'done' }]);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
};
|
|
716
|
+
const response = await utils.handleRequest(openai, {
|
|
717
|
+
...request, tools: [{ id: 'lookup', name: 'lookup', parameters: { type: 'object', properties: {} }, handler }]
|
|
718
|
+
}, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
719
|
+
const parts = [];
|
|
720
|
+
const consume = (async () => {
|
|
721
|
+
if ('stream' in response) {
|
|
722
|
+
for await (const part of response.stream) {
|
|
723
|
+
parts.push(part);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
})();
|
|
727
|
+
await clock.runAllAsync();
|
|
728
|
+
await consume;
|
|
729
|
+
expect(handler.callCount).to.equal(1);
|
|
730
|
+
expect(parts.filter(isTextResponsePart)).to.deep.equal([{ content: 'done' }]);
|
|
731
|
+
expect(payloads).to.have.length(3);
|
|
732
|
+
expect(payloads[2]).to.deep.equal(payloads[1]);
|
|
733
|
+
expect(payloads[2].input).to.deep.include({ type: 'function_call_output', call_id: 'call-1', output: 'result' });
|
|
734
|
+
});
|
|
735
|
+
});
|
|
584
736
|
describe('reasoning summaries', () => {
|
|
585
737
|
// Two summary parts of one reasoning item, as the Responses API streams them with `reasoning.summary` set.
|
|
586
738
|
const summaryEvents = [
|
|
@@ -600,12 +752,12 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
600
752
|
return parts;
|
|
601
753
|
}
|
|
602
754
|
function thoughts(parts) {
|
|
603
|
-
return parts.filter(
|
|
755
|
+
return parts.filter(isThinkingResponsePart).map(part => part.thought).join('');
|
|
604
756
|
}
|
|
605
757
|
it('streams reasoning summaries as thoughts, separating summary parts', async () => {
|
|
606
758
|
const openai = {
|
|
607
759
|
responses: {
|
|
608
|
-
|
|
760
|
+
create: async () => toStream([...summaryEvents, { type: 'response.output_text.delta', delta: 'Answer' }])
|
|
609
761
|
}
|
|
610
762
|
};
|
|
611
763
|
const request = {
|
|
@@ -613,9 +765,9 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
613
765
|
requestId: 'request-1',
|
|
614
766
|
messages: [{ actor: 'user', type: 'text', text: 'hello' }]
|
|
615
767
|
};
|
|
616
|
-
const parts = await drain(await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
617
|
-
|
|
618
|
-
|
|
768
|
+
const parts = await drain(await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true));
|
|
769
|
+
expect(thoughts(parts)).to.equal('Weighing options\n\nDeciding');
|
|
770
|
+
expect(parts.filter(isTextResponsePart).map(part => part.content).join('')).to.equal('Answer');
|
|
619
771
|
});
|
|
620
772
|
it('streams reasoning summaries as thoughts while tool calling', async () => {
|
|
621
773
|
const streams = [
|
|
@@ -635,7 +787,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
635
787
|
];
|
|
636
788
|
const openai = {
|
|
637
789
|
responses: {
|
|
638
|
-
|
|
790
|
+
create: async () => toStream(streams.shift() ?? [])
|
|
639
791
|
}
|
|
640
792
|
};
|
|
641
793
|
const request = {
|
|
@@ -649,8 +801,8 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
649
801
|
handler: async () => 'result'
|
|
650
802
|
}]
|
|
651
803
|
};
|
|
652
|
-
const parts = await drain(await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
653
|
-
|
|
804
|
+
const parts = await drain(await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true));
|
|
805
|
+
expect(thoughts(parts)).to.equal('Weighing options\n\nDeciding');
|
|
654
806
|
});
|
|
655
807
|
describe('unverified organizations', () => {
|
|
656
808
|
const summarySettings = { reasoning: { effort: 'medium', summary: 'auto' } };
|
|
@@ -661,7 +813,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
661
813
|
};
|
|
662
814
|
function badRequest(param) {
|
|
663
815
|
const message = `400 Your organization must be verified to generate reasoning summaries (param: ${param})`;
|
|
664
|
-
return new
|
|
816
|
+
return new OpenAI.BadRequestError(400, { message, param }, message, new Headers());
|
|
665
817
|
}
|
|
666
818
|
async function* rejectingStream(error) {
|
|
667
819
|
throw error;
|
|
@@ -673,7 +825,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
673
825
|
const sent = [];
|
|
674
826
|
const openai = {
|
|
675
827
|
responses: {
|
|
676
|
-
|
|
828
|
+
create: async (params) => {
|
|
677
829
|
sent.push(params);
|
|
678
830
|
return summaryOf(params)
|
|
679
831
|
? rejectingStream(badRequest('reasoning.summary'))
|
|
@@ -681,12 +833,12 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
681
833
|
}
|
|
682
834
|
}
|
|
683
835
|
};
|
|
684
|
-
const send = async () => drain(await utils.handleRequest(openai, request, summarySettings, 'gpt-5', new
|
|
836
|
+
const send = async () => drain(await utils.handleRequest(openai, request, summarySettings, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true));
|
|
685
837
|
const parts = await send();
|
|
686
838
|
await send();
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
839
|
+
expect(parts.filter(isTextResponsePart).map(part => part.content).join('')).to.equal('Answer');
|
|
840
|
+
expect(sent.map(summaryOf)).to.deep.equal(['auto', undefined, undefined]);
|
|
841
|
+
expect(sent[1].reasoning?.effort).to.equal('medium');
|
|
690
842
|
});
|
|
691
843
|
it('retries a non-streaming tool-calling request without the summary', async () => {
|
|
692
844
|
const sent = [];
|
|
@@ -705,9 +857,9 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
705
857
|
...request,
|
|
706
858
|
tools: [{ id: 'lookup', name: 'lookup', parameters: { type: 'object', properties: {} }, handler: async () => 'result' }]
|
|
707
859
|
};
|
|
708
|
-
const parts = await drain(await utils.handleRequest(openai, toolRequest, summarySettings, 'gpt-5', new
|
|
709
|
-
|
|
710
|
-
|
|
860
|
+
const parts = await drain(await utils.handleRequest(openai, toolRequest, summarySettings, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', false));
|
|
861
|
+
expect(parts.filter(isTextResponsePart).map(part => part.content).join('')).to.equal('done');
|
|
862
|
+
expect(sent.map(summaryOf)).to.deep.equal(['auto', undefined]);
|
|
711
863
|
});
|
|
712
864
|
it('does not retry other bad requests', async () => {
|
|
713
865
|
let calls = 0;
|
|
@@ -721,13 +873,13 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
721
873
|
};
|
|
722
874
|
let error;
|
|
723
875
|
try {
|
|
724
|
-
await utils.handleRequest(openai, request, summarySettings, 'gpt-5', new
|
|
876
|
+
await utils.handleRequest(openai, request, summarySettings, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', false);
|
|
725
877
|
}
|
|
726
878
|
catch (e) {
|
|
727
879
|
error = e;
|
|
728
880
|
}
|
|
729
|
-
|
|
730
|
-
|
|
881
|
+
expect(error).to.be.instanceOf(OpenAI.BadRequestError);
|
|
882
|
+
expect(calls).to.equal(1);
|
|
731
883
|
});
|
|
732
884
|
});
|
|
733
885
|
});
|
|
@@ -772,8 +924,11 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
772
924
|
};
|
|
773
925
|
const openai = {
|
|
774
926
|
responses: {
|
|
775
|
-
|
|
927
|
+
create: async (payload) => {
|
|
776
928
|
const items = nextOutput(payload);
|
|
929
|
+
if (!payload.stream) {
|
|
930
|
+
return { output: items, output_text: '' };
|
|
931
|
+
}
|
|
777
932
|
return toStream([
|
|
778
933
|
...items.flatMap((item, output_index) => [
|
|
779
934
|
{ type: 'response.output_item.added', output_index, item },
|
|
@@ -785,8 +940,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
785
940
|
...items.map((item, output_index) => ({ type: 'response.output_item.done', output_index, item })).reverse(),
|
|
786
941
|
{ type: 'response.completed', response: { usage: { input_tokens: 10, output_tokens: 5 } } }
|
|
787
942
|
]);
|
|
788
|
-
}
|
|
789
|
-
create: async (payload) => ({ output: nextOutput(payload), output_text: '' })
|
|
943
|
+
}
|
|
790
944
|
}
|
|
791
945
|
};
|
|
792
946
|
const request = {
|
|
@@ -800,7 +954,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
800
954
|
handler: async () => { executions++; return executions === 2 ? undefined : 'result'; }
|
|
801
955
|
}]
|
|
802
956
|
};
|
|
803
|
-
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
957
|
+
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', isStreaming);
|
|
804
958
|
if (!('stream' in response)) {
|
|
805
959
|
throw new Error('Expected the tool loop to return a stream');
|
|
806
960
|
}
|
|
@@ -808,17 +962,17 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
808
962
|
for await (const part of response.stream) {
|
|
809
963
|
parts.push(part);
|
|
810
964
|
}
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
965
|
+
expect(executions).to.equal(3);
|
|
966
|
+
expect(parts.filter(isCompactionResponsePart).map(part => part.compaction.data)).to.have.deep.members(output.filter(item => item.type === 'compaction').map(item => ({ id: item.id, encrypted_content: 'encrypted' })));
|
|
967
|
+
expect(payloads).to.have.lengthOf(3);
|
|
968
|
+
expect(payloads[1].instructions).to.equal('Instructions');
|
|
969
|
+
expect(payloads[1].input).to.deep.equal([
|
|
816
970
|
...(output.some(item => item.type === 'compaction') ? [] : payloads[0].input),
|
|
817
971
|
...replay,
|
|
818
972
|
{ type: 'function_call_output', call_id: 'a', output: 'result' },
|
|
819
973
|
{ type: 'function_call_output', call_id: 'b', output: '' }
|
|
820
974
|
]);
|
|
821
|
-
|
|
975
|
+
expect(payloads[2].input).to.deep.equal([
|
|
822
976
|
...payloads[1].input,
|
|
823
977
|
call('c'),
|
|
824
978
|
{ type: 'function_call_output', call_id: 'c', output: 'result' }
|
|
@@ -852,13 +1006,13 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
852
1006
|
];
|
|
853
1007
|
const { input } = utils.processMessages(messages, 'developer', 'gpt-5');
|
|
854
1008
|
const compaction = findCompactionItem(input);
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
1009
|
+
expect(compaction, 'compaction item present').to.not.equal(undefined);
|
|
1010
|
+
expect(compaction.encrypted_content).to.equal('enc1');
|
|
1011
|
+
expect(input[0]).to.deep.include({ type: 'compaction', encrypted_content: 'enc1' });
|
|
858
1012
|
const serialized = textOf(input);
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
1013
|
+
expect(serialized).to.contain('user C');
|
|
1014
|
+
expect(serialized).to.not.contain('user A');
|
|
1015
|
+
expect(serialized).to.not.contain('ai B');
|
|
862
1016
|
});
|
|
863
1017
|
it('only replays the LAST openai-responses marker and drops everything before it', () => {
|
|
864
1018
|
const messages = [
|
|
@@ -872,14 +1026,14 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
872
1026
|
];
|
|
873
1027
|
const { input } = utils.processMessages(messages, 'developer', 'gpt-5');
|
|
874
1028
|
const compactionItems = input.filter(item => item.type === 'compaction');
|
|
875
|
-
|
|
876
|
-
|
|
1029
|
+
expect(compactionItems).to.have.length(1);
|
|
1030
|
+
expect(compactionItems[0].encrypted_content).to.equal('enc2');
|
|
877
1031
|
const serialized = textOf(input);
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
1032
|
+
expect(serialized).to.contain('user C');
|
|
1033
|
+
expect(serialized).to.not.contain('user A');
|
|
1034
|
+
expect(serialized).to.not.contain('user B');
|
|
1035
|
+
expect(serialized).to.not.contain('enc1');
|
|
1036
|
+
expect(serialized).to.not.contain('foreign');
|
|
883
1037
|
});
|
|
884
1038
|
it('skips a foreign-provider compaction marker without dropping the prefix', () => {
|
|
885
1039
|
const messages = [
|
|
@@ -888,20 +1042,20 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
888
1042
|
userMessage('user B')
|
|
889
1043
|
];
|
|
890
1044
|
const { input } = utils.processMessages(messages, 'developer', 'gpt-5');
|
|
891
|
-
|
|
1045
|
+
expect(findCompactionItem(input), 'no compaction item for foreign provider').to.equal(undefined);
|
|
892
1046
|
const serialized = textOf(input);
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
1047
|
+
expect(serialized).to.contain('user A');
|
|
1048
|
+
expect(serialized).to.contain('user B');
|
|
1049
|
+
expect(serialized).to.not.contain('enc1');
|
|
896
1050
|
});
|
|
897
1051
|
it('converts all messages unchanged when there is no compaction marker', () => {
|
|
898
1052
|
const messages = [userMessage('user A'), aiMessage('ai B'), userMessage('user C')];
|
|
899
1053
|
const { input } = utils.processMessages(messages, 'developer', 'gpt-5');
|
|
900
|
-
|
|
1054
|
+
expect(findCompactionItem(input)).to.equal(undefined);
|
|
901
1055
|
const serialized = textOf(input);
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
1056
|
+
expect(serialized).to.contain('user A');
|
|
1057
|
+
expect(serialized).to.contain('ai B');
|
|
1058
|
+
expect(serialized).to.contain('user C');
|
|
905
1059
|
});
|
|
906
1060
|
});
|
|
907
1061
|
for (const nameSource of ['added', 'done', 'arguments']) {
|
|
@@ -923,7 +1077,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
923
1077
|
];
|
|
924
1078
|
const openai = {
|
|
925
1079
|
responses: {
|
|
926
|
-
|
|
1080
|
+
create: async (payload) => {
|
|
927
1081
|
payloads.push(payload);
|
|
928
1082
|
return toStream(streams.shift() ?? []);
|
|
929
1083
|
}
|
|
@@ -935,7 +1089,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
935
1089
|
messages: [{ actor: 'user', type: 'text', text: 'hello' }],
|
|
936
1090
|
tools: [{ id: 'lookup', name: 'lookup', parameters: { type: 'object', properties: {} }, handler }]
|
|
937
1091
|
};
|
|
938
|
-
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
1092
|
+
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
939
1093
|
const parts = [];
|
|
940
1094
|
if ('stream' in response) {
|
|
941
1095
|
for await (const part of response.stream) {
|
|
@@ -943,13 +1097,13 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
943
1097
|
}
|
|
944
1098
|
}
|
|
945
1099
|
sinon.assert.calledOnce(handler);
|
|
946
|
-
|
|
947
|
-
const finished = parts.filter(
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
1100
|
+
expect(handler.firstCall.args[0]).to.equal(item.arguments);
|
|
1101
|
+
const finished = parts.filter(isToolCallResponsePart).flatMap(part => part.tool_calls).find(call => call.finished);
|
|
1102
|
+
expect(finished?.function?.name).to.equal('lookup');
|
|
1103
|
+
expect(finished?.result).to.equal('found');
|
|
1104
|
+
expect(payloads).to.have.lengthOf(2);
|
|
1105
|
+
expect(payloads[1].input).to.deep.include(item);
|
|
1106
|
+
expect(payloads[1].input).to.deep.include({ type: 'function_call_output', call_id: item.call_id, output: 'found' });
|
|
953
1107
|
});
|
|
954
1108
|
}
|
|
955
1109
|
it('executes the tool calls of a single turn concurrently', async () => {
|
|
@@ -966,11 +1120,11 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
966
1120
|
];
|
|
967
1121
|
const openai = {
|
|
968
1122
|
responses: {
|
|
969
|
-
|
|
1123
|
+
create: async () => toStream(streams.shift() ?? [])
|
|
970
1124
|
}
|
|
971
1125
|
};
|
|
972
1126
|
// `a` only resolves once `b` has started: a sequential implementation would deadlock here.
|
|
973
|
-
const bStarted = new
|
|
1127
|
+
const bStarted = new Deferred();
|
|
974
1128
|
const request = {
|
|
975
1129
|
sessionId: 'session-1',
|
|
976
1130
|
requestId: 'request-1',
|
|
@@ -980,7 +1134,7 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
980
1134
|
{ id: 'b', name: 'b', parameters: { type: 'object', properties: {} }, handler: async () => { bStarted.resolve(); return 'b-result'; } }
|
|
981
1135
|
]
|
|
982
1136
|
};
|
|
983
|
-
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new
|
|
1137
|
+
const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
|
|
984
1138
|
const parts = [];
|
|
985
1139
|
if ('stream' in response) {
|
|
986
1140
|
for await (const part of response.stream) {
|
|
@@ -988,11 +1142,11 @@ describe('OpenAiResponseApiUtils', () => {
|
|
|
988
1142
|
}
|
|
989
1143
|
}
|
|
990
1144
|
const finishedResults = parts
|
|
991
|
-
.filter(
|
|
1145
|
+
.filter(isToolCallResponsePart)
|
|
992
1146
|
.flatMap(part => part.tool_calls)
|
|
993
1147
|
.filter(call => call.finished)
|
|
994
1148
|
.map(call => call.result);
|
|
995
|
-
|
|
1149
|
+
expect(finishedResults).to.have.members(['a-result', 'b-result']);
|
|
996
1150
|
});
|
|
997
1151
|
});
|
|
998
1152
|
//# sourceMappingURL=openai-response-api-utils.spec.js.map
|