@robota-sdk/agent-provider-openai 3.0.0-beta.100
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +417 -0
- package/LICENSE +661 -0
- package/README.md +198 -0
- package/dist/node/index.cjs +3 -0
- package/dist/node/index.d.cts +332 -0
- package/dist/node/index.d.cts.map +1 -0
- package/dist/node/index.d.ts +332 -0
- package/dist/node/index.d.ts.map +1 -0
- package/dist/node/index.js +4 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/loggers/index.cjs +1 -0
- package/dist/node/loggers/index.d.cts +94 -0
- package/dist/node/loggers/index.d.cts.map +1 -0
- package/dist/node/loggers/index.d.ts +94 -0
- package/dist/node/loggers/index.d.ts.map +1 -0
- package/dist/node/loggers/index.js +2 -0
- package/dist/node/loggers/index.js.map +1 -0
- package/dist/node/payload-logger-n89AQdUn.d.cts +63 -0
- package/dist/node/payload-logger-n89AQdUn.d.cts.map +1 -0
- package/dist/node/payload-logger-n89AQdUn.d.ts +63 -0
- package/dist/node/payload-logger-n89AQdUn.d.ts.map +1 -0
- package/dist/node/rolldown-runtime-CMqjfN_6.cjs +1 -0
- package/package.json +104 -0
- package/src/index.ts +1 -0
- package/src/openai/__tests__/abort-signal-wire.test.ts +210 -0
- package/src/openai/__tests__/durable-operation-wire.test.ts +38 -0
- package/src/openai/__tests__/endpoint-provenance.test.ts +44 -0
- package/src/openai/__tests__/extra-body.test.ts +507 -0
- package/src/openai/__tests__/mixed-tool-content.test.ts +103 -0
- package/src/openai/__tests__/parallel-tool-policy.test.ts +398 -0
- package/src/openai/__tests__/provider-errors.test.ts +143 -0
- package/src/openai/__tests__/request-format.test.ts +59 -0
- package/src/openai/__tests__/strict-tools-closure.test.ts +183 -0
- package/src/openai/__tests__/tool-schema-projection.test.ts +316 -0
- package/src/openai/__tests__/trace-context-wire.test.ts +114 -0
- package/src/openai/adapter.test.ts +494 -0
- package/src/openai/adapter.ts +145 -0
- package/src/openai/chat-completions-chat.ts +232 -0
- package/src/openai/executor-integration.test.ts +213 -0
- package/src/openai/extra-body.ts +120 -0
- package/src/openai/index.ts +19 -0
- package/src/openai/interfaces/payload-logger.ts +50 -0
- package/src/openai/loggers/console-payload-logger.test.ts +173 -0
- package/src/openai/loggers/console-payload-logger.ts +96 -0
- package/src/openai/loggers/console.ts +9 -0
- package/src/openai/loggers/file-payload-logger.test.ts +243 -0
- package/src/openai/loggers/file-payload-logger.ts +125 -0
- package/src/openai/loggers/file.ts +9 -0
- package/src/openai/loggers/index.ts +12 -0
- package/src/openai/loggers/sanitize-openai-log-data.test.ts +89 -0
- package/src/openai/loggers/sanitize-openai-log-data.ts +14 -0
- package/src/openai/message-converter.ts +27 -0
- package/src/openai/model-effort-table.ts +19 -0
- package/src/openai/model-effort-verification-config.test.ts +45 -0
- package/src/openai/model-effort-verification-config.ts +35 -0
- package/src/openai/openai-request-format.ts +116 -0
- package/src/openai/parallel-tool-policy.ts +25 -0
- package/src/openai/parsers/response-parser.test.ts +407 -0
- package/src/openai/parsers/response-parser.ts +48 -0
- package/src/openai/provider-definition.test.ts +74 -0
- package/src/openai/provider-definition.ts +149 -0
- package/src/openai/provider.test.ts +1717 -0
- package/src/openai/provider.ts +417 -0
- package/src/openai/reasoning-effort.test.ts +272 -0
- package/src/openai/reasoning-effort.ts +49 -0
- package/src/openai/request-id.ts +39 -0
- package/src/openai/request-options.ts +14 -0
- package/src/openai/responses-chat.ts +292 -0
- package/src/openai/responses-converter.ts +132 -0
- package/src/openai/responses-parser.test.ts +219 -0
- package/src/openai/responses-parser.ts +361 -0
- package/src/openai/responses-stream-utils.ts +45 -0
- package/src/openai/responses-types.ts +219 -0
- package/src/openai/streaming/stream-assembler.ts +3 -0
- package/src/openai/types/api-types.ts +113 -0
- package/src/openai/types.ts +263 -0
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
import { OpenAIResponseParser } from './response-parser';
|
|
3
|
+
import type { ILogger } from '@robota-sdk/agent-core';
|
|
4
|
+
import type OpenAI from 'openai';
|
|
5
|
+
|
|
6
|
+
function createMockLogger(): ILogger {
|
|
7
|
+
return {
|
|
8
|
+
info: vi.fn(),
|
|
9
|
+
warn: vi.fn(),
|
|
10
|
+
error: vi.fn(),
|
|
11
|
+
debug: vi.fn(),
|
|
12
|
+
log: vi.fn(),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function createChatCompletion(
|
|
17
|
+
overrides: Partial<OpenAI.Chat.ChatCompletion> = {},
|
|
18
|
+
): OpenAI.Chat.ChatCompletion {
|
|
19
|
+
return {
|
|
20
|
+
id: 'chatcmpl-test',
|
|
21
|
+
object: 'chat.completion',
|
|
22
|
+
created: Date.now(),
|
|
23
|
+
model: 'gpt-4',
|
|
24
|
+
choices: [
|
|
25
|
+
{
|
|
26
|
+
index: 0,
|
|
27
|
+
message: {
|
|
28
|
+
role: 'assistant',
|
|
29
|
+
content: 'Hello!',
|
|
30
|
+
refusal: null,
|
|
31
|
+
},
|
|
32
|
+
finish_reason: 'stop',
|
|
33
|
+
logprobs: null,
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
...overrides,
|
|
37
|
+
} as OpenAI.Chat.ChatCompletion;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function createStreamChunk(
|
|
41
|
+
overrides: Record<string, unknown> = {},
|
|
42
|
+
): OpenAI.Chat.ChatCompletionChunk {
|
|
43
|
+
return {
|
|
44
|
+
id: 'chatcmpl-chunk',
|
|
45
|
+
object: 'chat.completion.chunk',
|
|
46
|
+
created: Date.now(),
|
|
47
|
+
model: 'gpt-4',
|
|
48
|
+
choices: [
|
|
49
|
+
{
|
|
50
|
+
index: 0,
|
|
51
|
+
delta: { content: 'Hi' },
|
|
52
|
+
finish_reason: null,
|
|
53
|
+
logprobs: null,
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
...overrides,
|
|
57
|
+
} as OpenAI.Chat.ChatCompletionChunk;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe('OpenAIResponseParser', () => {
|
|
61
|
+
describe('constructor', () => {
|
|
62
|
+
it('should use SilentLogger when no logger is provided', () => {
|
|
63
|
+
const parser = new OpenAIResponseParser();
|
|
64
|
+
// Parser should work without throwing
|
|
65
|
+
const response = createChatCompletion();
|
|
66
|
+
const result = parser.parseResponse(response);
|
|
67
|
+
expect(result.role).toBe('assistant');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should accept a custom logger', () => {
|
|
71
|
+
const logger = createMockLogger();
|
|
72
|
+
const parser = new OpenAIResponseParser(logger);
|
|
73
|
+
const response = createChatCompletion();
|
|
74
|
+
const result = parser.parseResponse(response);
|
|
75
|
+
expect(result.role).toBe('assistant');
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('parseResponse', () => {
|
|
80
|
+
it('should parse a simple text response', () => {
|
|
81
|
+
const parser = new OpenAIResponseParser();
|
|
82
|
+
const response = createChatCompletion();
|
|
83
|
+
const result = parser.parseResponse(response);
|
|
84
|
+
|
|
85
|
+
expect(result.role).toBe('assistant');
|
|
86
|
+
expect(result.content).toBe('Hello!');
|
|
87
|
+
expect(result.timestamp).toBeInstanceOf(Date);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('should parse response with empty content as empty string', () => {
|
|
91
|
+
const parser = new OpenAIResponseParser();
|
|
92
|
+
const response = createChatCompletion({
|
|
93
|
+
choices: [
|
|
94
|
+
{
|
|
95
|
+
index: 0,
|
|
96
|
+
message: { role: 'assistant', content: null, refusal: null },
|
|
97
|
+
finish_reason: 'stop',
|
|
98
|
+
logprobs: null,
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
});
|
|
102
|
+
const result = parser.parseResponse(response);
|
|
103
|
+
expect(result.content).toBe('');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('should parse response with tool calls', () => {
|
|
107
|
+
const parser = new OpenAIResponseParser();
|
|
108
|
+
const response = createChatCompletion({
|
|
109
|
+
choices: [
|
|
110
|
+
{
|
|
111
|
+
index: 0,
|
|
112
|
+
message: {
|
|
113
|
+
role: 'assistant',
|
|
114
|
+
content: null,
|
|
115
|
+
refusal: null,
|
|
116
|
+
tool_calls: [
|
|
117
|
+
{
|
|
118
|
+
id: 'call_abc',
|
|
119
|
+
type: 'function',
|
|
120
|
+
function: {
|
|
121
|
+
name: 'calculate',
|
|
122
|
+
arguments: '{"a":1,"b":2}',
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
},
|
|
127
|
+
finish_reason: 'tool_calls',
|
|
128
|
+
logprobs: null,
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const result = parser.parseResponse(response);
|
|
134
|
+
expect(result.role).toBe('assistant');
|
|
135
|
+
expect(result.content).toBe('');
|
|
136
|
+
expect(result).toHaveProperty('toolCalls');
|
|
137
|
+
const assistantResult = result as {
|
|
138
|
+
toolCalls: Array<{
|
|
139
|
+
id: string;
|
|
140
|
+
type: string;
|
|
141
|
+
function: { name: string; arguments: string };
|
|
142
|
+
}>;
|
|
143
|
+
};
|
|
144
|
+
expect(assistantResult.toolCalls).toHaveLength(1);
|
|
145
|
+
expect(assistantResult.toolCalls[0].id).toBe('call_abc');
|
|
146
|
+
expect(assistantResult.toolCalls[0].function.name).toBe('calculate');
|
|
147
|
+
expect(assistantResult.toolCalls[0].function.arguments).toBe('{"a":1,"b":2}');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should parse response with usage information', () => {
|
|
151
|
+
const parser = new OpenAIResponseParser();
|
|
152
|
+
const response = createChatCompletion({
|
|
153
|
+
usage: {
|
|
154
|
+
prompt_tokens: 10,
|
|
155
|
+
completion_tokens: 20,
|
|
156
|
+
total_tokens: 30,
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const result = parser.parseResponse(response);
|
|
161
|
+
expect(result).toHaveProperty('usage');
|
|
162
|
+
const withUsage = result as unknown as {
|
|
163
|
+
usage: { promptTokens: number; completionTokens: number; totalTokens: number };
|
|
164
|
+
};
|
|
165
|
+
expect(withUsage.usage.promptTokens).toBe(10);
|
|
166
|
+
expect(withUsage.usage.completionTokens).toBe(20);
|
|
167
|
+
expect(withUsage.usage.totalTokens).toBe(30);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('should include finishReason in metadata', () => {
|
|
171
|
+
const parser = new OpenAIResponseParser();
|
|
172
|
+
const response = createChatCompletion();
|
|
173
|
+
const result = parser.parseResponse(response);
|
|
174
|
+
expect(result.metadata?.finishReason).toBe('stop');
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('should throw when response has no choices', () => {
|
|
178
|
+
const parser = new OpenAIResponseParser();
|
|
179
|
+
const response = createChatCompletion({ choices: [] });
|
|
180
|
+
|
|
181
|
+
expect(() => parser.parseResponse(response)).toThrow('OpenAI response parsing failed');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should throw with descriptive error and log on parsing failure', () => {
|
|
185
|
+
const logger = createMockLogger();
|
|
186
|
+
const parser = new OpenAIResponseParser(logger);
|
|
187
|
+
const response = createChatCompletion({ choices: [] });
|
|
188
|
+
|
|
189
|
+
expect(() => parser.parseResponse(response)).toThrow(
|
|
190
|
+
'OpenAI response parsing failed: No choices found in OpenAI response',
|
|
191
|
+
);
|
|
192
|
+
expect(logger.error).toHaveBeenCalled();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('should handle response without usage field', () => {
|
|
196
|
+
const parser = new OpenAIResponseParser();
|
|
197
|
+
const response = createChatCompletion();
|
|
198
|
+
// Default createChatCompletion has no usage
|
|
199
|
+
const result = parser.parseResponse(response);
|
|
200
|
+
expect(result).not.toHaveProperty('usage');
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('should parse response with multiple tool calls', () => {
|
|
204
|
+
const parser = new OpenAIResponseParser();
|
|
205
|
+
const response = createChatCompletion({
|
|
206
|
+
choices: [
|
|
207
|
+
{
|
|
208
|
+
index: 0,
|
|
209
|
+
message: {
|
|
210
|
+
role: 'assistant',
|
|
211
|
+
content: null,
|
|
212
|
+
refusal: null,
|
|
213
|
+
tool_calls: [
|
|
214
|
+
{
|
|
215
|
+
id: 'call_1',
|
|
216
|
+
type: 'function',
|
|
217
|
+
function: { name: 'fn1', arguments: '{}' },
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
id: 'call_2',
|
|
221
|
+
type: 'function',
|
|
222
|
+
function: { name: 'fn2', arguments: '{"x":1}' },
|
|
223
|
+
},
|
|
224
|
+
],
|
|
225
|
+
},
|
|
226
|
+
finish_reason: 'tool_calls',
|
|
227
|
+
logprobs: null,
|
|
228
|
+
},
|
|
229
|
+
],
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
const result = parser.parseResponse(response);
|
|
233
|
+
const assistantResult = result as { toolCalls: Array<{ id: string }> };
|
|
234
|
+
expect(assistantResult.toolCalls).toHaveLength(2);
|
|
235
|
+
expect(assistantResult.toolCalls[0].id).toBe('call_1');
|
|
236
|
+
expect(assistantResult.toolCalls[1].id).toBe('call_2');
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
describe('parseStreamingChunk', () => {
|
|
241
|
+
it('should parse a chunk with content', () => {
|
|
242
|
+
const parser = new OpenAIResponseParser();
|
|
243
|
+
const chunk = createStreamChunk();
|
|
244
|
+
const result = parser.parseStreamingChunk(chunk);
|
|
245
|
+
|
|
246
|
+
expect(result).not.toBeNull();
|
|
247
|
+
expect(result!.role).toBe('assistant');
|
|
248
|
+
expect(result!.content).toBe('Hi');
|
|
249
|
+
expect(result!.metadata?.isStreamChunk).toBe(true);
|
|
250
|
+
expect(result!.metadata?.isComplete).toBe(false);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('should return null when chunk has no choices', () => {
|
|
254
|
+
const parser = new OpenAIResponseParser();
|
|
255
|
+
const chunk = createStreamChunk({ choices: [] });
|
|
256
|
+
const result = parser.parseStreamingChunk(chunk);
|
|
257
|
+
expect(result).toBeNull();
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('should handle chunk with empty content', () => {
|
|
261
|
+
const parser = new OpenAIResponseParser();
|
|
262
|
+
const chunk = createStreamChunk({
|
|
263
|
+
choices: [
|
|
264
|
+
{
|
|
265
|
+
index: 0,
|
|
266
|
+
delta: { content: '' },
|
|
267
|
+
finish_reason: null,
|
|
268
|
+
},
|
|
269
|
+
],
|
|
270
|
+
});
|
|
271
|
+
const result = parser.parseStreamingChunk(chunk);
|
|
272
|
+
expect(result).not.toBeNull();
|
|
273
|
+
expect(result!.content).toBe('');
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('should mark chunk as complete when finish_reason is stop', () => {
|
|
277
|
+
const parser = new OpenAIResponseParser();
|
|
278
|
+
const chunk = createStreamChunk({
|
|
279
|
+
choices: [
|
|
280
|
+
{
|
|
281
|
+
index: 0,
|
|
282
|
+
delta: { content: '' },
|
|
283
|
+
finish_reason: 'stop',
|
|
284
|
+
},
|
|
285
|
+
],
|
|
286
|
+
});
|
|
287
|
+
const result = parser.parseStreamingChunk(chunk);
|
|
288
|
+
expect(result!.metadata?.isComplete).toBe(true);
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it('should mark chunk as complete when finish_reason is tool_calls', () => {
|
|
292
|
+
const parser = new OpenAIResponseParser();
|
|
293
|
+
const chunk = createStreamChunk({
|
|
294
|
+
choices: [
|
|
295
|
+
{
|
|
296
|
+
index: 0,
|
|
297
|
+
delta: { content: '' },
|
|
298
|
+
finish_reason: 'tool_calls',
|
|
299
|
+
},
|
|
300
|
+
],
|
|
301
|
+
});
|
|
302
|
+
const result = parser.parseStreamingChunk(chunk);
|
|
303
|
+
expect(result!.metadata?.isComplete).toBe(true);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('should parse chunk with tool calls', () => {
|
|
307
|
+
const parser = new OpenAIResponseParser();
|
|
308
|
+
const chunk = createStreamChunk({
|
|
309
|
+
choices: [
|
|
310
|
+
{
|
|
311
|
+
index: 0,
|
|
312
|
+
delta: {
|
|
313
|
+
tool_calls: [
|
|
314
|
+
{
|
|
315
|
+
index: 0,
|
|
316
|
+
id: 'call_stream_1',
|
|
317
|
+
type: 'function',
|
|
318
|
+
function: {
|
|
319
|
+
name: 'search',
|
|
320
|
+
arguments: '{"q":"test"}',
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
],
|
|
324
|
+
},
|
|
325
|
+
finish_reason: null,
|
|
326
|
+
},
|
|
327
|
+
],
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const result = parser.parseStreamingChunk(chunk);
|
|
331
|
+
expect(result).not.toBeNull();
|
|
332
|
+
expect(result!.role).toBe('assistant');
|
|
333
|
+
expect(result!.content).toBe('');
|
|
334
|
+
const withToolCalls = result as unknown as {
|
|
335
|
+
toolCalls: Array<{ id: string; function: { name: string; arguments: string } }>;
|
|
336
|
+
};
|
|
337
|
+
expect(withToolCalls.toolCalls).toHaveLength(1);
|
|
338
|
+
expect(withToolCalls.toolCalls[0].id).toBe('call_stream_1');
|
|
339
|
+
expect(withToolCalls.toolCalls[0].function.name).toBe('search');
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it('should handle chunk with tool calls missing optional fields', () => {
|
|
343
|
+
const parser = new OpenAIResponseParser();
|
|
344
|
+
const chunk = createStreamChunk({
|
|
345
|
+
choices: [
|
|
346
|
+
{
|
|
347
|
+
index: 0,
|
|
348
|
+
delta: {
|
|
349
|
+
tool_calls: [
|
|
350
|
+
{
|
|
351
|
+
index: 0,
|
|
352
|
+
// Missing id, function.name, function.arguments
|
|
353
|
+
},
|
|
354
|
+
],
|
|
355
|
+
},
|
|
356
|
+
finish_reason: null,
|
|
357
|
+
},
|
|
358
|
+
],
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
const result = parser.parseStreamingChunk(chunk);
|
|
362
|
+
expect(result).not.toBeNull();
|
|
363
|
+
const withToolCalls = result as unknown as {
|
|
364
|
+
toolCalls: Array<{ id: string; function: { name: string; arguments: string } }>;
|
|
365
|
+
};
|
|
366
|
+
expect(withToolCalls.toolCalls[0].id).toBe('');
|
|
367
|
+
expect(withToolCalls.toolCalls[0].function.name).toBe('');
|
|
368
|
+
expect(withToolCalls.toolCalls[0].function.arguments).toBe('');
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
it('should handle chunk with no delta content (undefined)', () => {
|
|
372
|
+
const parser = new OpenAIResponseParser();
|
|
373
|
+
const chunk = createStreamChunk({
|
|
374
|
+
choices: [
|
|
375
|
+
{
|
|
376
|
+
index: 0,
|
|
377
|
+
delta: {},
|
|
378
|
+
finish_reason: null,
|
|
379
|
+
},
|
|
380
|
+
],
|
|
381
|
+
});
|
|
382
|
+
const result = parser.parseStreamingChunk(chunk);
|
|
383
|
+
expect(result).not.toBeNull();
|
|
384
|
+
expect(result!.content).toBe('');
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it('should throw with descriptive error on parsing failure', () => {
|
|
388
|
+
const logger = createMockLogger();
|
|
389
|
+
const parser = new OpenAIResponseParser(logger);
|
|
390
|
+
|
|
391
|
+
// Force an error by passing a chunk that causes internal error
|
|
392
|
+
const badChunk = {
|
|
393
|
+
choices: [
|
|
394
|
+
{
|
|
395
|
+
get delta(): never {
|
|
396
|
+
throw new Error('Access error');
|
|
397
|
+
},
|
|
398
|
+
finish_reason: null,
|
|
399
|
+
},
|
|
400
|
+
],
|
|
401
|
+
} as unknown as OpenAI.Chat.ChatCompletionChunk;
|
|
402
|
+
|
|
403
|
+
expect(() => parser.parseStreamingChunk(badChunk)).toThrow('OpenAI chunk parsing failed');
|
|
404
|
+
expect(logger.error).toHaveBeenCalled();
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { OpenAICompatibleResponseParser } from '@robota-sdk/agent-provider-openai-compatible/shared';
|
|
2
|
+
|
|
3
|
+
import type { ILogger, TUniversalMessage } from '@robota-sdk/agent-core';
|
|
4
|
+
import type OpenAI from 'openai';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* OpenAI response parser compatibility wrapper.
|
|
8
|
+
*
|
|
9
|
+
* The implementation delegates to the shared OpenAI-compatible parser while
|
|
10
|
+
* preserving the OpenAI-branded error messages that are part of this package's
|
|
11
|
+
* current test contract.
|
|
12
|
+
*/
|
|
13
|
+
export class OpenAIResponseParser {
|
|
14
|
+
private readonly parser: OpenAICompatibleResponseParser;
|
|
15
|
+
|
|
16
|
+
constructor(logger?: ILogger) {
|
|
17
|
+
this.parser = new OpenAICompatibleResponseParser({ logger });
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
parseResponse(response: OpenAI.Chat.ChatCompletion): TUniversalMessage {
|
|
21
|
+
try {
|
|
22
|
+
return this.parser.parseResponse(response);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
const message = normalizeParserMessage(
|
|
25
|
+
error instanceof Error ? error.message : 'OpenAI response parsing failed',
|
|
26
|
+
);
|
|
27
|
+
throw new Error(`OpenAI response parsing failed: ${message}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
parseStreamingChunk(chunk: OpenAI.Chat.ChatCompletionChunk): TUniversalMessage | null {
|
|
32
|
+
try {
|
|
33
|
+
return this.parser.parseStreamingChunk(chunk);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
const message = normalizeParserMessage(
|
|
36
|
+
error instanceof Error ? error.message : 'OpenAI chunk parsing failed',
|
|
37
|
+
);
|
|
38
|
+
throw new Error(`OpenAI chunk parsing failed: ${message}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function normalizeParserMessage(message: string): string {
|
|
44
|
+
return message
|
|
45
|
+
.replace(/^OpenAI-compatible response parsing failed: /, '')
|
|
46
|
+
.replace(/^OpenAI-compatible chunk parsing failed: /, '')
|
|
47
|
+
.replace('OpenAI-compatible response', 'OpenAI response');
|
|
48
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
createOpenAIProviderDefinition,
|
|
4
|
+
DEFAULT_OPENAI_PROVIDER_API_KEY_REFERENCE,
|
|
5
|
+
DEFAULT_OPENAI_PROVIDER_MODEL,
|
|
6
|
+
} from './index';
|
|
7
|
+
|
|
8
|
+
describe('createOpenAIProviderDefinition', () => {
|
|
9
|
+
it('uses official OpenAI setup defaults without a model-family default', () => {
|
|
10
|
+
const definition = createOpenAIProviderDefinition();
|
|
11
|
+
const modelStep = definition.setupSteps?.find((step) => step.key === 'model');
|
|
12
|
+
const apiKeyStep = definition.setupSteps?.find((step) => step.key === 'apiKey');
|
|
13
|
+
|
|
14
|
+
expect(DEFAULT_OPENAI_PROVIDER_MODEL).toBeUndefined();
|
|
15
|
+
expect(definition.defaults?.model).toBeUndefined();
|
|
16
|
+
expect(definition.defaults?.apiKey).toBe(DEFAULT_OPENAI_PROVIDER_API_KEY_REFERENCE);
|
|
17
|
+
expect(definition.defaults?.baseURL).toBeUndefined();
|
|
18
|
+
expect(definition.displayName).toBe('OpenAI');
|
|
19
|
+
expect(definition.setupHelpLinks).toEqual([
|
|
20
|
+
{
|
|
21
|
+
kind: 'api-key',
|
|
22
|
+
label: 'OpenAI API keys',
|
|
23
|
+
url: 'https://platform.openai.com/api-keys',
|
|
24
|
+
sourceUrl: 'https://developers.openai.com/api/reference/overview#authentication',
|
|
25
|
+
lastVerifiedAt: '2026-05-08',
|
|
26
|
+
},
|
|
27
|
+
]);
|
|
28
|
+
expect(modelStep).toMatchObject({
|
|
29
|
+
key: 'model',
|
|
30
|
+
title: 'OpenAI model',
|
|
31
|
+
required: true,
|
|
32
|
+
});
|
|
33
|
+
expect(modelStep?.defaultValue).toBeUndefined();
|
|
34
|
+
expect(apiKeyStep).toMatchObject({
|
|
35
|
+
key: 'apiKey',
|
|
36
|
+
title: 'OpenAI API key',
|
|
37
|
+
defaultValue: '$ENV:OPENAI_API_KEY',
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('passes explicit apiSurface provider options into construction', () => {
|
|
42
|
+
const definition = createOpenAIProviderDefinition();
|
|
43
|
+
|
|
44
|
+
const provider = definition.createProvider({
|
|
45
|
+
name: 'openai',
|
|
46
|
+
model: 'gpt-4o',
|
|
47
|
+
apiKey: 'sk-test',
|
|
48
|
+
options: { apiSurface: 'chat-completions' },
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
expect(provider.name).toBe('openai');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('rejects native web tools for OpenAI-compatible baseURL profiles', () => {
|
|
55
|
+
const definition = createOpenAIProviderDefinition();
|
|
56
|
+
|
|
57
|
+
expect(() =>
|
|
58
|
+
definition.createProvider({
|
|
59
|
+
name: 'openai',
|
|
60
|
+
model: 'local-model',
|
|
61
|
+
apiKey: 'lm-studio',
|
|
62
|
+
baseURL: 'http://localhost:1234/v1',
|
|
63
|
+
options: {
|
|
64
|
+
builtInWebTools: {
|
|
65
|
+
webSearch: true,
|
|
66
|
+
webFetch: true,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
).toThrow(
|
|
71
|
+
'Provider openai profile uses an OpenAI-compatible Chat Completions endpoint; native web search/fetch is not supported for this profile.',
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { probeOpenAICompatibleProfile } from '@robota-sdk/agent-provider-openai-compatible/shared';
|
|
2
|
+
|
|
3
|
+
import { parseExtraBody } from './extra-body';
|
|
4
|
+
import { OpenAIProvider } from './provider';
|
|
5
|
+
import { parseParallelToolCalls } from './parallel-tool-policy';
|
|
6
|
+
|
|
7
|
+
import type { IOpenAINativeWebToolsOptions, TOpenAIApiSurface } from './types';
|
|
8
|
+
import type { IProviderDefinition, TUniversalValue } from '@robota-sdk/agent-core';
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_OPENAI_PROVIDER_MODEL: string | undefined = undefined;
|
|
11
|
+
export const DEFAULT_OPENAI_PROVIDER_API_KEY_REFERENCE = '$ENV:OPENAI_API_KEY';
|
|
12
|
+
const OPENAI_API_KEY_URL = 'https://platform.openai.com/api-keys';
|
|
13
|
+
const OPENAI_SETUP_SOURCE_URL =
|
|
14
|
+
'https://developers.openai.com/api/reference/overview#authentication';
|
|
15
|
+
const OPENAI_SETUP_LAST_VERIFIED_AT = '2026-05-08';
|
|
16
|
+
const OPENAI_SETUP_HELP_LINKS: NonNullable<IProviderDefinition['setupHelpLinks']> = [
|
|
17
|
+
{
|
|
18
|
+
kind: 'api-key',
|
|
19
|
+
label: 'OpenAI API keys',
|
|
20
|
+
url: OPENAI_API_KEY_URL,
|
|
21
|
+
sourceUrl: OPENAI_SETUP_SOURCE_URL,
|
|
22
|
+
lastVerifiedAt: OPENAI_SETUP_LAST_VERIFIED_AT,
|
|
23
|
+
},
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
export function createOpenAIProviderDefinition(): IProviderDefinition {
|
|
27
|
+
return {
|
|
28
|
+
type: 'openai',
|
|
29
|
+
displayName: 'OpenAI',
|
|
30
|
+
description: 'GPT series — general-purpose assistant. API key required.',
|
|
31
|
+
category: 'cloud-paid',
|
|
32
|
+
// Interim provider-level cost (ARCH-PROVIDER-003), migrated from the former llm-text-openai node.
|
|
33
|
+
costPerTokenUsd: 0.001,
|
|
34
|
+
defaults: {
|
|
35
|
+
apiKey: DEFAULT_OPENAI_PROVIDER_API_KEY_REFERENCE,
|
|
36
|
+
},
|
|
37
|
+
// OBSERVABILITY-1991: the SDK embeds this endpoint; declared here for the pre-session doctor only.
|
|
38
|
+
endpoint: { host: 'api.openai.com', port: 443 },
|
|
39
|
+
modelCatalog: {
|
|
40
|
+
status: 'unavailable',
|
|
41
|
+
sourceUrl: 'https://platform.openai.com/docs/api-reference/models/list',
|
|
42
|
+
message: 'OpenAI model availability should be discovered live from GET /v1/models.',
|
|
43
|
+
},
|
|
44
|
+
setupHelpLinks: OPENAI_SETUP_HELP_LINKS,
|
|
45
|
+
setupSteps: [
|
|
46
|
+
{
|
|
47
|
+
key: 'model',
|
|
48
|
+
title: 'OpenAI model',
|
|
49
|
+
required: true,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
key: 'apiKey',
|
|
53
|
+
title: 'OpenAI API key',
|
|
54
|
+
defaultValue: DEFAULT_OPENAI_PROVIDER_API_KEY_REFERENCE,
|
|
55
|
+
masked: true,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
requiresApiKey: true,
|
|
59
|
+
probeProfile: probeOpenAICompatibleProfile,
|
|
60
|
+
// The OpenAI SDK falls back to the base URL variable when none is configured, and sends the
|
|
61
|
+
// organization and project the others name.
|
|
62
|
+
destinationEnvironment: ['OPENAI_BASE_URL', 'OPENAI_ORG_ID', 'OPENAI_PROJECT_ID'],
|
|
63
|
+
createProvider: (config) => {
|
|
64
|
+
const parallelToolCalls = parseParallelToolCalls(
|
|
65
|
+
config.options?.parallelToolCalls,
|
|
66
|
+
config.options?.apiSurface,
|
|
67
|
+
);
|
|
68
|
+
const extraBody = parseExtraBody(config.options?.extraBody);
|
|
69
|
+
const apiSurface = readApiSurface(config.options);
|
|
70
|
+
const nativeWebTools = readNativeWebTools(config.options);
|
|
71
|
+
validateOpenAINativeWebTools(config.baseURL, apiSurface, nativeWebTools);
|
|
72
|
+
return new OpenAIProvider({
|
|
73
|
+
apiKey: requireApiKey(config.apiKey),
|
|
74
|
+
...(config.baseURL !== undefined && { baseURL: config.baseURL }),
|
|
75
|
+
...(config.timeout !== undefined && { timeout: config.timeout }),
|
|
76
|
+
...(apiSurface !== undefined && { apiSurface }),
|
|
77
|
+
...(parallelToolCalls !== undefined && { parallelToolCalls }),
|
|
78
|
+
...(extraBody !== undefined && { extraBody }),
|
|
79
|
+
...(nativeWebTools !== undefined && { nativeWebTools }),
|
|
80
|
+
...(config.options?.durableOperations === true && { durableOperations: true }),
|
|
81
|
+
defaultModel: config.model,
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function requireApiKey(apiKey: string | undefined): string {
|
|
88
|
+
if (!apiKey) {
|
|
89
|
+
throw new Error('Provider openai requires apiKey');
|
|
90
|
+
}
|
|
91
|
+
return apiKey;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function readApiSurface(
|
|
95
|
+
options: Record<string, TUniversalValue> | undefined,
|
|
96
|
+
): TOpenAIApiSurface | undefined {
|
|
97
|
+
const apiSurface = options?.['apiSurface'];
|
|
98
|
+
if (apiSurface === 'responses' || apiSurface === 'chat-completions') {
|
|
99
|
+
return apiSurface;
|
|
100
|
+
}
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function readNativeWebTools(
|
|
105
|
+
options: Record<string, TUniversalValue> | undefined,
|
|
106
|
+
): IOpenAINativeWebToolsOptions | undefined {
|
|
107
|
+
const nativeWebTools =
|
|
108
|
+
readNativeWebToolsRecord(options?.['nativeWebTools']) ??
|
|
109
|
+
readNativeWebToolsRecord(options?.['builtInWebTools']);
|
|
110
|
+
if (nativeWebTools === undefined) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
return nativeWebTools;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function readNativeWebToolsRecord(
|
|
117
|
+
value: TUniversalValue | undefined,
|
|
118
|
+
): IOpenAINativeWebToolsOptions | undefined {
|
|
119
|
+
if (value === null || value === undefined || value instanceof Date || Array.isArray(value)) {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
if (typeof value !== 'object') {
|
|
123
|
+
return undefined;
|
|
124
|
+
}
|
|
125
|
+
const webSearch = value['webSearch'];
|
|
126
|
+
const webFetch = value['webFetch'];
|
|
127
|
+
return {
|
|
128
|
+
...(typeof webSearch === 'boolean' && { webSearch }),
|
|
129
|
+
...(typeof webFetch === 'boolean' && { webFetch }),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function validateOpenAINativeWebTools(
|
|
134
|
+
baseURL: string | undefined,
|
|
135
|
+
apiSurface: TOpenAIApiSurface | undefined,
|
|
136
|
+
nativeWebTools: IOpenAINativeWebToolsOptions | undefined,
|
|
137
|
+
): void {
|
|
138
|
+
if (nativeWebTools?.webSearch !== true && nativeWebTools?.webFetch !== true) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (baseURL !== undefined || apiSurface === 'chat-completions') {
|
|
142
|
+
throw new Error(
|
|
143
|
+
'Provider openai profile uses an OpenAI-compatible Chat Completions endpoint; native web search/fetch is not supported for this profile. Use local WebSearch/WebFetch tools or a provider with documented hosted web support.',
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
throw new Error(
|
|
147
|
+
'Provider openai native web search/fetch is not wired in this provider version. Use local WebSearch/WebFetch tools or a provider with documented hosted web support.',
|
|
148
|
+
);
|
|
149
|
+
}
|