@theia/ai-openai 1.77.0-next.99 → 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.
Files changed (108) hide show
  1. package/lib/browser/openai-frontend-application-contribution.d.ts +1 -1
  2. package/lib/browser/openai-frontend-application-contribution.d.ts.map +1 -1
  3. package/lib/browser/openai-frontend-application-contribution.js +55 -60
  4. package/lib/browser/openai-frontend-application-contribution.js.map +1 -1
  5. package/lib/browser/openai-frontend-application-contribution.spec.js +13 -15
  6. package/lib/browser/openai-frontend-application-contribution.spec.js.map +1 -1
  7. package/lib/browser/openai-frontend-module.js +13 -15
  8. package/lib/browser/openai-frontend-module.js.map +1 -1
  9. package/lib/common/index.d.ts +1 -1
  10. package/lib/common/index.d.ts.map +1 -1
  11. package/lib/common/index.js +1 -4
  12. package/lib/common/index.js.map +1 -1
  13. package/lib/common/openai-language-models-manager.js +3 -6
  14. package/lib/common/openai-language-models-manager.js.map +1 -1
  15. package/lib/common/openai-preferences.js +52 -55
  16. package/lib/common/openai-preferences.js.map +1 -1
  17. package/lib/node/openai-backend-module.d.ts.map +1 -1
  18. package/lib/node/openai-backend-module.js +31 -31
  19. package/lib/node/openai-backend-module.js.map +1 -1
  20. package/lib/node/openai-chat-completion-stream.d.ts +6 -9
  21. package/lib/node/openai-chat-completion-stream.d.ts.map +1 -1
  22. package/lib/node/openai-chat-completion-stream.js +34 -43
  23. package/lib/node/openai-chat-completion-stream.js.map +1 -1
  24. package/lib/node/openai-chat-completion-stream.spec.js +159 -76
  25. package/lib/node/openai-chat-completion-stream.spec.js.map +1 -1
  26. package/lib/node/openai-language-model.d.ts +4 -3
  27. package/lib/node/openai-language-model.d.ts.map +1 -1
  28. package/lib/node/openai-language-model.js +68 -76
  29. package/lib/node/openai-language-model.js.map +1 -1
  30. package/lib/node/openai-language-model.spec.js +137 -75
  31. package/lib/node/openai-language-model.spec.js.map +1 -1
  32. package/lib/node/openai-language-models-manager-impl.d.ts +3 -3
  33. package/lib/node/openai-language-models-manager-impl.d.ts.map +1 -1
  34. package/lib/node/openai-language-models-manager-impl.js +29 -36
  35. package/lib/node/openai-language-models-manager-impl.js.map +1 -1
  36. package/lib/node/openai-language-models-manager-impl.spec.js +34 -36
  37. package/lib/node/openai-language-models-manager-impl.spec.js.map +1 -1
  38. package/lib/node/openai-model-defaults.d.ts +1 -1
  39. package/lib/node/openai-model-defaults.d.ts.map +1 -1
  40. package/lib/node/openai-model-defaults.js +1 -4
  41. package/lib/node/openai-model-defaults.js.map +1 -1
  42. package/lib/node/openai-model-defaults.spec.js +52 -54
  43. package/lib/node/openai-model-defaults.spec.js.map +1 -1
  44. package/lib/node/openai-model-utils.d.ts +1 -1
  45. package/lib/node/openai-model-utils.d.ts.map +1 -1
  46. package/lib/node/openai-model-utils.js +14 -17
  47. package/lib/node/openai-model-utils.js.map +1 -1
  48. package/lib/node/openai-model-utils.spec.js +45 -47
  49. package/lib/node/openai-model-utils.spec.js.map +1 -1
  50. package/lib/node/openai-reasoning.js +1 -4
  51. package/lib/node/openai-reasoning.js.map +1 -1
  52. package/lib/node/openai-request-settings.js +9 -13
  53. package/lib/node/openai-request-settings.js.map +1 -1
  54. package/lib/node/openai-response-api-utils.d.ts +5 -3
  55. package/lib/node/openai-response-api-utils.d.ts.map +1 -1
  56. package/lib/node/openai-response-api-utils.js +84 -70
  57. package/lib/node/openai-response-api-utils.js.map +1 -1
  58. package/lib/node/openai-response-api-utils.spec.js +320 -166
  59. package/lib/node/openai-response-api-utils.spec.js.map +1 -1
  60. package/lib/node/openai-server-tools.js +4 -7
  61. package/lib/node/openai-server-tools.js.map +1 -1
  62. package/lib/node/openai-streaming-iterator.d.ts +2 -1
  63. package/lib/node/openai-streaming-iterator.d.ts.map +1 -1
  64. package/lib/node/openai-streaming-iterator.js +5 -8
  65. package/lib/node/openai-streaming-iterator.js.map +1 -1
  66. package/lib/node/openai-streaming-iterator.spec.js +37 -40
  67. package/lib/node/openai-streaming-iterator.spec.js.map +1 -1
  68. package/lib/node/openai-streaming-retry.d.ts +8 -0
  69. package/lib/node/openai-streaming-retry.d.ts.map +1 -0
  70. package/lib/node/openai-streaming-retry.js +108 -0
  71. package/lib/node/openai-streaming-retry.js.map +1 -0
  72. package/lib/node/openai-streaming-retry.spec.d.ts +2 -0
  73. package/lib/node/openai-streaming-retry.spec.d.ts.map +1 -0
  74. package/lib/node/openai-streaming-retry.spec.js +182 -0
  75. package/lib/node/openai-streaming-retry.spec.js.map +1 -0
  76. package/lib/node/streaming-response-iterator.js +5 -9
  77. package/lib/node/streaming-response-iterator.js.map +1 -1
  78. package/lib/node/streaming-response-iterator.spec.js +6 -8
  79. package/lib/node/streaming-response-iterator.spec.js.map +1 -1
  80. package/package.json +16 -7
  81. package/src/browser/openai-frontend-application-contribution.spec.ts +3 -3
  82. package/src/browser/openai-frontend-application-contribution.ts +2 -2
  83. package/src/browser/openai-frontend-module.ts +3 -3
  84. package/src/common/index.ts +1 -1
  85. package/src/node/openai-backend-module.ts +9 -7
  86. package/src/node/openai-chat-completion-stream.spec.ts +88 -3
  87. package/src/node/openai-chat-completion-stream.ts +20 -15
  88. package/src/node/openai-language-model.spec.ts +84 -7
  89. package/src/node/openai-language-model.ts +37 -32
  90. package/src/node/openai-language-models-manager-impl.spec.ts +3 -3
  91. package/src/node/openai-language-models-manager-impl.ts +4 -4
  92. package/src/node/openai-model-defaults.spec.ts +1 -1
  93. package/src/node/openai-model-defaults.ts +1 -1
  94. package/src/node/openai-model-utils.spec.ts +2 -2
  95. package/src/node/openai-model-utils.ts +2 -2
  96. package/src/node/openai-request-settings.ts +1 -1
  97. package/src/node/openai-response-api-utils.spec.ts +180 -27
  98. package/src/node/openai-response-api-utils.ts +45 -21
  99. package/src/node/openai-streaming-iterator.spec.ts +1 -1
  100. package/src/node/openai-streaming-iterator.ts +2 -1
  101. package/src/node/openai-streaming-retry.spec.ts +195 -0
  102. package/src/node/openai-streaming-retry.ts +110 -0
  103. package/src/node/streaming-response-iterator.spec.ts +1 -1
  104. package/lib/node/openai-request-api-context.d.ts +0 -4
  105. package/lib/node/openai-request-api-context.d.ts.map +0 -1
  106. package/lib/node/openai-request-api-context.js +0 -19
  107. package/lib/node/openai-request-api-context.js.map +0 -1
  108. 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
- Object.defineProperty(exports, "__esModule", { value: true });
18
- const tslib_1 = require("tslib");
19
- const chai_1 = require("chai");
20
- const sinon = tslib_1.__importStar(require("sinon"));
21
- const ai_core_1 = require("@theia/ai-core");
22
- const core_1 = require("@theia/core");
23
- const inversify_1 = require("@theia/core/shared/inversify");
24
- const mock_logger_1 = require("@theia/core/lib/common/test/mock-logger");
25
- const promise_util_1 = require("@theia/core/lib/common/promise-util");
26
- const openai_model_utils_1 = require("./openai-model-utils");
27
- const openai_1 = require("openai");
28
- const openai_response_api_utils_1 = require("./openai-response-api-utils");
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 inversify_1.Container();
46
- container.bind(core_1.ILogger).to(mock_logger_1.MockLogger);
47
- container.bind(ai_core_1.ToolCallExecutor).to(ai_core_1.ToolCallExecutorImpl).inSingletonScope();
48
- container.bind(openai_response_api_utils_1.OpenAiResponseApiUtils).toSelf();
49
- return container.get(openai_response_api_utils_1.OpenAiResponseApiUtils);
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
- (0, chai_1.expect)(converted).to.have.lengthOf(1);
71
+ expect(converted).to.have.lengthOf(1);
73
72
  const convertedTool = converted?.[0];
74
- (0, chai_1.expect)(convertedTool).to.include({
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
- (0, chai_1.expect)(convertedTool.parameters).to.equal(parameters);
82
+ expect(convertedTool.parameters).to.equal(parameters);
84
83
  });
85
84
  it('adds native web search without requiring client tools', () => {
86
- (0, chai_1.expect)(utils.convertToolsForResponseApi(undefined, undefined, [openai_server_tools_1.OPENAI_WEB_SEARCH])).to.deep.equal([
85
+ expect(utils.convertToolsForResponseApi(undefined, undefined, [OPENAI_WEB_SEARCH])).to.deep.equal([
87
86
  { type: 'web_search' }
88
87
  ]);
89
- (0, chai_1.expect)(utils.convertToolsForResponseApi()).to.equal(undefined);
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'], [openai_server_tools_1.OPENAI_WEB_SEARCH]);
98
- (0, chai_1.expect)(tools?.map(tool => tool.type)).to.deep.equal(['function', 'tool_search', 'web_search']);
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
- (0, chai_1.expect)(JSON.parse(JSON.stringify(input))).to.deep.equal([
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 { output: payloads.length === 1 ? functionCalls : [], output_text: payloads.length === 1 ? '' : 'done' };
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', isStreaming);
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
- (0, chai_1.expect)(parts.filter(ai_core_1.isTextResponsePart)).to.deep.equal([{ content: 'done' }]);
168
- (0, chai_1.expect)(payloads).to.have.length(2);
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
- (0, chai_1.expect)(input.filter(item => item.type === 'function_call')).to.deep.equal(functionCalls);
171
- (0, chai_1.expect)(input.filter(item => item.type === 'function_call_output')).to.deep.equal([
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
- stream: (responseRequest) => {
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: [openai_server_tools_1.OPENAI_WEB_SEARCH],
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
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
- (0, chai_1.expect)(parts.filter(ai_core_1.isUsageResponsePart)).to.deep.equal([
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
- (0, chai_1.expect)(streamRequests).to.have.length(2);
246
- (0, chai_1.expect)(streamRequests.every(streamRequest => JSON.stringify(streamRequest.include) === JSON.stringify(['web_search_call.action.sources', 'reasoning.encrypted_content']))).to.equal(true);
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
- stream: (responseRequest) => {
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: [openai_server_tools_1.OPENAI_WEB_SEARCH],
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
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
- (0, chai_1.expect)(parts).to.not.be.empty;
293
- (0, chai_1.expect)(streamRequests).to.have.length(2);
294
- (0, chai_1.expect)(streamRequests[1].input).to.deep.include.members([reasoningItem, searchCall]);
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
- (0, chai_1.expect)(input.indexOf(reasoningItem)).to.be.lessThan(input.indexOf(searchCall));
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
- stream: (payload) => {
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
314
+ const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
319
315
  const parts = [];
320
- (0, chai_1.expect)('stream' in response).to.equal(true);
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(ai_core_1.isCompactionResponsePart);
327
- (0, chai_1.expect)(compactionParts).to.deep.equal([{
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
334
+ const replay = await utils.handleRequest(openai, replayRequest, {}, 'gpt-5', new OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
339
335
  const replayParts = [];
340
- (0, chai_1.expect)('stream' in replay).to.equal(true);
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
- (0, chai_1.expect)(replayParts.filter(ai_core_1.isTextResponsePart)).to.deep.equal([{ content: 'Continued answer' }]);
347
- (0, chai_1.expect)(payloads).to.have.lengthOf(2);
348
- (0, chai_1.expect)(payloads[1].input).to.deep.equal([
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
- stream: (payload) => {
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
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(ai_core_1.isServerToolCallResponsePart).flatMap(p => p.server_tool_calls);
394
+ const calls = parts.filter(isServerToolCallResponsePart).flatMap(p => p.server_tool_calls);
399
395
  const running = calls.find(c => !c.finished);
400
- (0, chai_1.expect)(running).to.not.be.undefined;
401
- (0, chai_1.expect)(running.id).to.equal('ts-1');
402
- (0, chai_1.expect)(running.name).to.equal('tool_search');
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
- (0, chai_1.expect)(finished).to.not.be.undefined;
405
- (0, chai_1.expect)(finished.id).to.equal('ts-1');
406
- (0, chai_1.expect)(finished.name).to.equal('tool_search');
407
- (0, chai_1.expect)(finished.result).to.deep.equal({ content: [{ type: 'text', text: 'Found 2 tools.' }] });
408
- (0, chai_1.expect)(payloads[1].input).to.deep.equal([
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
- stream: () => toStream([
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: [openai_server_tools_1.OPENAI_WEB_SEARCH]
437
+ serverTools: [OPENAI_WEB_SEARCH]
442
438
  };
443
- const response = await utils.handleRequest(openai, request, {}, 'gpt-5', new openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
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(ai_core_1.isServerToolCallResponsePart).flatMap(part => part.server_tool_calls);
451
- (0, chai_1.expect)(calls).to.have.length(2);
452
- (0, chai_1.expect)(calls[0]).to.deep.include({ id: 'ws-1', name: openai_server_tools_1.OPENAI_WEB_SEARCH, finished: false });
453
- (0, chai_1.expect)(calls[1]).to.deep.include({ id: 'ws-1', name: openai_server_tools_1.OPENAI_WEB_SEARCH, finished: true });
454
- (0, chai_1.expect)(calls[1].result).to.deep.equal({ content: [{ type: 'text', text: 'Web search failed.' }] });
455
- (0, chai_1.expect)(calls[1].data).to.deep.equal({
456
- [openai_server_tools_1.OPENAI_WEB_SEARCH_REPLAY_DATA_KEY]: JSON.stringify([reasoningItem, searchCall])
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: [openai_server_tools_1.OPENAI_WEB_SEARCH]
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', false);
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(ai_core_1.isServerToolCallResponsePart).flatMap(part => part.server_tool_calls);
486
- (0, chai_1.expect)(calls).to.have.length(1);
487
- (0, chai_1.expect)(calls[0]).to.deep.include({ id: 'ws-1', name: openai_server_tools_1.OPENAI_WEB_SEARCH, finished: true });
488
- (0, chai_1.expect)(createRequest?.include).to.deep.equal(['file_search_call.results', 'web_search_call.action.sources', 'reasoning.encrypted_content']);
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: openai_server_tools_1.OPENAI_WEB_SEARCH,
507
+ name: OPENAI_WEB_SEARCH,
512
508
  input: searchCall.action,
513
509
  result: { content: [{ type: 'text', text: 'Web search completed.' }] },
514
510
  data: {
515
- [openai_server_tools_1.OPENAI_WEB_SEARCH_REPLAY_DATA_KEY]: JSON.stringify([reasoningItem, searchCall])
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
- (0, chai_1.expect)(input).to.deep.equal([reasoningItem, searchCall]);
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
- stream: (responseRequest) => {
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
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
- (0, chai_1.expect)(requests[0].include).to.deep.equal(['reasoning.encrypted_content']);
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
- (0, chai_1.expect)(completedCall?.data).to.deep.equal({
563
- [openai_response_api_utils_1.OPENAI_FUNCTION_CALL_REASONING_DATA_KEY]: JSON.stringify([reasoningItem])
558
+ expect(completedCall?.data).to.deep.equal({
559
+ [OPENAI_FUNCTION_CALL_REASONING_DATA_KEY]: JSON.stringify([reasoningItem])
564
560
  });
565
- (0, chai_1.expect)(requests[1].input).to.deep.include.members([reasoningItem]);
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
- (0, chai_1.expect)(nextInput.indexOf(reasoningItem)).to.be.lessThan(nextInput.indexOf(replayedCall));
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
- (0, chai_1.expect)(input).to.deep.equal([reasoningItem, {
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(ai_core_1.isThinkingResponsePart).map(part => part.thought).join('');
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
- stream: () => toStream([...summaryEvents, { type: 'response.output_text.delta', delta: 'Answer' }])
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true));
617
- (0, chai_1.expect)(thoughts(parts)).to.equal('Weighing options\n\nDeciding');
618
- (0, chai_1.expect)(parts.filter(ai_core_1.isTextResponsePart).map(part => part.content).join('')).to.equal('Answer');
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
- stream: () => toStream(streams.shift() ?? [])
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true));
653
- (0, chai_1.expect)(thoughts(parts)).to.equal('Weighing options\n\nDeciding');
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 openai_1.OpenAI.BadRequestError(400, { message, param }, message, new Headers());
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
- stream: (params) => {
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true));
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
- (0, chai_1.expect)(parts.filter(ai_core_1.isTextResponsePart).map(part => part.content).join('')).to.equal('Answer');
688
- (0, chai_1.expect)(sent.map(summaryOf)).to.deep.equal(['auto', undefined, undefined]);
689
- (0, chai_1.expect)(sent[1].reasoning?.effort).to.equal('medium');
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', false));
709
- (0, chai_1.expect)(parts.filter(ai_core_1.isTextResponsePart).map(part => part.content).join('')).to.equal('done');
710
- (0, chai_1.expect)(sent.map(summaryOf)).to.deep.equal(['auto', undefined]);
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', false);
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
- (0, chai_1.expect)(error).to.be.instanceOf(openai_1.OpenAI.BadRequestError);
730
- (0, chai_1.expect)(calls).to.equal(1);
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
- stream: (payload) => {
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', isStreaming);
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
- (0, chai_1.expect)(executions).to.equal(3);
812
- (0, chai_1.expect)(parts.filter(ai_core_1.isCompactionResponsePart).map(part => part.compaction.data)).to.have.deep.members(output.filter(item => item.type === 'compaction').map(item => ({ id: item.id, encrypted_content: 'encrypted' })));
813
- (0, chai_1.expect)(payloads).to.have.lengthOf(3);
814
- (0, chai_1.expect)(payloads[1].instructions).to.equal('Instructions');
815
- (0, chai_1.expect)(payloads[1].input).to.deep.equal([
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
- (0, chai_1.expect)(payloads[2].input).to.deep.equal([
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
- (0, chai_1.expect)(compaction, 'compaction item present').to.not.equal(undefined);
856
- (0, chai_1.expect)(compaction.encrypted_content).to.equal('enc1');
857
- (0, chai_1.expect)(input[0]).to.deep.include({ type: 'compaction', encrypted_content: 'enc1' });
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
- (0, chai_1.expect)(serialized).to.contain('user C');
860
- (0, chai_1.expect)(serialized).to.not.contain('user A');
861
- (0, chai_1.expect)(serialized).to.not.contain('ai B');
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
- (0, chai_1.expect)(compactionItems).to.have.length(1);
876
- (0, chai_1.expect)(compactionItems[0].encrypted_content).to.equal('enc2');
1029
+ expect(compactionItems).to.have.length(1);
1030
+ expect(compactionItems[0].encrypted_content).to.equal('enc2');
877
1031
  const serialized = textOf(input);
878
- (0, chai_1.expect)(serialized).to.contain('user C');
879
- (0, chai_1.expect)(serialized).to.not.contain('user A');
880
- (0, chai_1.expect)(serialized).to.not.contain('user B');
881
- (0, chai_1.expect)(serialized).to.not.contain('enc1');
882
- (0, chai_1.expect)(serialized).to.not.contain('foreign');
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
- (0, chai_1.expect)(findCompactionItem(input), 'no compaction item for foreign provider').to.equal(undefined);
1045
+ expect(findCompactionItem(input), 'no compaction item for foreign provider').to.equal(undefined);
892
1046
  const serialized = textOf(input);
893
- (0, chai_1.expect)(serialized).to.contain('user A');
894
- (0, chai_1.expect)(serialized).to.contain('user B');
895
- (0, chai_1.expect)(serialized).to.not.contain('enc1');
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
- (0, chai_1.expect)(findCompactionItem(input)).to.equal(undefined);
1054
+ expect(findCompactionItem(input)).to.equal(undefined);
901
1055
  const serialized = textOf(input);
902
- (0, chai_1.expect)(serialized).to.contain('user A');
903
- (0, chai_1.expect)(serialized).to.contain('ai B');
904
- (0, chai_1.expect)(serialized).to.contain('user C');
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
- stream: (payload) => {
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
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
- (0, chai_1.expect)(handler.firstCall.args[0]).to.equal(item.arguments);
947
- const finished = parts.filter(ai_core_1.isToolCallResponsePart).flatMap(part => part.tool_calls).find(call => call.finished);
948
- (0, chai_1.expect)(finished?.function?.name).to.equal('lookup');
949
- (0, chai_1.expect)(finished?.result).to.equal('found');
950
- (0, chai_1.expect)(payloads).to.have.lengthOf(2);
951
- (0, chai_1.expect)(payloads[1].input).to.deep.include(item);
952
- (0, chai_1.expect)(payloads[1].input).to.deep.include({ type: 'function_call_output', call_id: item.call_id, output: 'found' });
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
- stream: () => toStream(streams.shift() ?? [])
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 promise_util_1.Deferred();
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 openai_model_utils_1.OpenAiModelUtils(), 'developer', 'openai/gpt-5', true);
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(ai_core_1.isToolCallResponsePart)
1145
+ .filter(isToolCallResponsePart)
992
1146
  .flatMap(part => part.tool_calls)
993
1147
  .filter(call => call.finished)
994
1148
  .map(call => call.result);
995
- (0, chai_1.expect)(finishedResults).to.have.members(['a-result', 'b-result']);
1149
+ expect(finishedResults).to.have.members(['a-result', 'b-result']);
996
1150
  });
997
1151
  });
998
1152
  //# sourceMappingURL=openai-response-api-utils.spec.js.map