@robota-sdk/agent-provider-openai 3.0.7 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +53 -0
- package/README.md +34 -5
- package/dist/index.cjs +3 -0
- package/dist/{node/index.d.cts → index.d.cts} +12 -10
- package/dist/index.d.cts.map +1 -0
- package/dist/{node/index.d.ts → index.d.ts} +12 -10
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/loggers/index.cjs +1 -0
- package/dist/loggers/index.d.cts +43 -0
- package/dist/loggers/index.d.cts.map +1 -0
- package/dist/loggers/index.d.ts +43 -0
- package/dist/loggers/index.d.ts.map +1 -0
- package/dist/loggers/index.js +2 -0
- package/dist/loggers/index.js.map +1 -0
- package/dist/node.cjs +1 -0
- package/dist/node.d.cts +54 -0
- package/dist/node.d.cts.map +1 -0
- package/dist/node.d.ts +54 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +2 -0
- package/dist/node.js.map +1 -0
- package/dist/payload-logger-n89AQdUn.d.cts.map +1 -0
- package/dist/payload-logger-n89AQdUn.d.ts.map +1 -0
- package/dist/sanitize-openai-log-data-CZ9jMqeC.cjs +1 -0
- package/dist/sanitize-openai-log-data-DI2gyZvi.js +2 -0
- package/dist/sanitize-openai-log-data-DI2gyZvi.js.map +1 -0
- package/package.json +29 -41
- package/src/node.ts +9 -0
- package/src/openai/__tests__/native-web-search.test.ts +941 -0
- package/src/openai/__tests__/strict-tools-closure.test.ts +42 -0
- package/src/openai/__tests__/tool-schema-projection.test.ts +35 -1
- package/src/openai/loggers/file-payload-logger.ts +3 -3
- package/src/openai/loggers/index.ts +1 -5
- package/src/openai/provider-definition.test.ts +3 -4
- package/src/openai/provider-definition.ts +22 -50
- package/src/openai/provider.ts +81 -25
- package/src/openai/responses-chat.ts +51 -21
- package/src/openai/responses-converter.ts +31 -8
- package/src/openai/responses-parser.ts +126 -6
- package/src/openai/responses-types.ts +22 -2
- package/src/openai/responses-web-search.ts +157 -0
- package/src/openai/types.ts +7 -8
- package/dist/node/index.cjs +0 -3
- package/dist/node/index.d.cts.map +0 -1
- package/dist/node/index.d.ts.map +0 -1
- package/dist/node/index.js +0 -4
- package/dist/node/index.js.map +0 -1
- package/dist/node/loggers/index.cjs +0 -1
- package/dist/node/loggers/index.d.cts +0 -94
- package/dist/node/loggers/index.d.cts.map +0 -1
- package/dist/node/loggers/index.d.ts +0 -94
- package/dist/node/loggers/index.d.ts.map +0 -1
- package/dist/node/loggers/index.js +0 -2
- package/dist/node/loggers/index.js.map +0 -1
- package/dist/node/payload-logger-n89AQdUn.d.cts.map +0 -1
- package/dist/node/payload-logger-n89AQdUn.d.ts.map +0 -1
- package/src/openai/loggers/file.ts +0 -9
- /package/dist/{node/payload-logger-n89AQdUn.d.cts → payload-logger-n89AQdUn.d.cts} +0 -0
- /package/dist/{node/payload-logger-n89AQdUn.d.ts → payload-logger-n89AQdUn.d.ts} +0 -0
- /package/dist/{node/rolldown-runtime-CMqjfN_6.cjs → rolldown-runtime-CMqjfN_6.cjs} +0 -0
|
@@ -181,3 +181,45 @@ describe('strictTools on the Chat Completions surface (#3209)', () => {
|
|
|
181
181
|
expect(tool?.function.parameters).toEqual(NESTED_TOOL.parameters);
|
|
182
182
|
});
|
|
183
183
|
});
|
|
184
|
+
|
|
185
|
+
describe('strict tools — size bounds the strict wire refuses', () => {
|
|
186
|
+
const BOUNDED_TOOL: IToolSchema = {
|
|
187
|
+
name: 'search',
|
|
188
|
+
description: 'Searches',
|
|
189
|
+
parameters: {
|
|
190
|
+
type: 'object',
|
|
191
|
+
properties: {
|
|
192
|
+
query: { type: 'string', minLength: 1, maxLength: 4096 },
|
|
193
|
+
tags: { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 5 },
|
|
194
|
+
labels: { type: 'object', properties: {}, minProperties: 1, maxProperties: 9 },
|
|
195
|
+
},
|
|
196
|
+
required: ['query', 'tags', 'labels'],
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
it.each([
|
|
201
|
+
['Responses', sendChat, {}],
|
|
202
|
+
['Chat Completions', sendChatCompletions, { baseURL: 'https://example.test/v1' }],
|
|
203
|
+
] as const)('%s receives a schema strict mode accepts', async (_surface, send, options) => {
|
|
204
|
+
const provider = new OpenAIProvider({ apiKey: 'sk-test', strictTools: true, ...options });
|
|
205
|
+
const requestParams = await send(provider, [BOUNDED_TOOL]);
|
|
206
|
+
const [tool] = requestParams.tools as Array<Record<string, unknown>>;
|
|
207
|
+
const wire = JSON.stringify(tool);
|
|
208
|
+
|
|
209
|
+
for (const refused of ['minLength', 'maxLength', 'minProperties', 'maxProperties']) {
|
|
210
|
+
expect(wire, refused).not.toContain(refused);
|
|
211
|
+
}
|
|
212
|
+
expect(wire).toContain('"minItems":1');
|
|
213
|
+
expect(wire).toContain('"maxItems":5');
|
|
214
|
+
expect(wire).toContain('Schema note:');
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it('forwards them all verbatim with strict tools off', async () => {
|
|
218
|
+
const provider = new OpenAIProvider({ apiKey: 'sk-test' });
|
|
219
|
+
const requestParams = await sendChat(provider, [BOUNDED_TOOL]);
|
|
220
|
+
const [tool] = requestParams.tools as Array<Record<string, unknown>>;
|
|
221
|
+
|
|
222
|
+
expect(tool.parameters).toEqual(BOUNDED_TOOL.parameters);
|
|
223
|
+
expect(tool.description).toBe(BOUNDED_TOOL.description);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
@@ -30,7 +30,12 @@ vi.mock('openai', () => {
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
const fixtures = loadToolSchemaProjectionFixtures();
|
|
33
|
-
const PERMISSIVE_PROFILE = {
|
|
33
|
+
const PERMISSIVE_PROFILE = {
|
|
34
|
+
...PERMISSIVE_TOOL_SCHEMA_PROFILE,
|
|
35
|
+
providerName: 'openai',
|
|
36
|
+
arraysDeclareItems: true,
|
|
37
|
+
objectsDeclareProperties: true,
|
|
38
|
+
};
|
|
34
39
|
const STRICT_PROFILE = { ...STRICT_TOOL_SCHEMA_PROFILE, providerName: 'openai' };
|
|
35
40
|
|
|
36
41
|
function recordingSink(): { sink: ILogger; lines: string[] } {
|
|
@@ -314,3 +319,32 @@ describe.each([
|
|
|
314
319
|
});
|
|
315
320
|
},
|
|
316
321
|
);
|
|
322
|
+
|
|
323
|
+
describe('the containers OpenAI refuses bare', () => {
|
|
324
|
+
const ANY_SCALAR = { anyOf: [{ type: 'string' }, { type: 'number' }, { type: 'boolean' }] };
|
|
325
|
+
const bareContainers: IToolSchema = {
|
|
326
|
+
name: 'bare',
|
|
327
|
+
description: 'A tool.',
|
|
328
|
+
parameters: {
|
|
329
|
+
type: 'object',
|
|
330
|
+
properties: { list: { type: 'array' }, shape: { type: 'object' } },
|
|
331
|
+
required: ['list', 'shape'],
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
it.each([
|
|
336
|
+
['without strictTools', false],
|
|
337
|
+
['with strictTools', true],
|
|
338
|
+
])('Responses declares both on the wire %s', async (_name, strictTools) => {
|
|
339
|
+
const provider = new OpenAIProvider({ apiKey: 'sk-test', strictTools });
|
|
340
|
+
const client = fakeClient(provider);
|
|
341
|
+
respondResponses(client);
|
|
342
|
+
await provider.chat([createUserMessage('hi')], { model: 'gpt-4o', tools: [bareContainers] });
|
|
343
|
+
const sent = client.responses.create.mock.calls[0]?.[0] as {
|
|
344
|
+
tools: { parameters: { properties: Record<string, unknown> } }[];
|
|
345
|
+
};
|
|
346
|
+
const properties = sent.tools[0]?.parameters.properties ?? {};
|
|
347
|
+
expect(JSON.stringify(properties)).toContain(JSON.stringify(ANY_SCALAR));
|
|
348
|
+
expect(properties['shape']).toHaveProperty('properties', {});
|
|
349
|
+
});
|
|
350
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
3
|
|
|
4
4
|
import { SilentLogger } from '@robota-sdk/agent-core';
|
|
5
5
|
|
|
@@ -21,7 +21,7 @@ const OWNER_ONLY_DIR_MODE = 0o700;
|
|
|
21
21
|
*
|
|
22
22
|
* @example
|
|
23
23
|
* ```typescript
|
|
24
|
-
* import { FilePayloadLogger } from '@robota-sdk/agent-provider-openai/
|
|
24
|
+
* import { FilePayloadLogger } from '@robota-sdk/agent-provider-openai/node';
|
|
25
25
|
*
|
|
26
26
|
* const logger = new FilePayloadLogger({
|
|
27
27
|
* logDir: './logs/api-payloads',
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* IPayloadLogger implementations
|
|
2
|
+
* Platform-neutral IPayloadLogger implementations. The file logger is Node-only (`./node`).
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
// Interfaces
|
|
6
6
|
export type { IPayloadLogger, IPayloadLoggerOptions } from '../interfaces/payload-logger';
|
|
7
7
|
|
|
8
|
-
// Node.js implementation
|
|
9
|
-
export { FilePayloadLogger } from './file-payload-logger';
|
|
10
|
-
|
|
11
|
-
// Browser implementation
|
|
12
8
|
export { ConsolePayloadLogger } from './console-payload-logger';
|
|
@@ -51,7 +51,7 @@ describe('createOpenAIProviderDefinition', () => {
|
|
|
51
51
|
expect(provider.name).toBe('openai');
|
|
52
52
|
});
|
|
53
53
|
|
|
54
|
-
it('rejects
|
|
54
|
+
it('rejects hosted web search for OpenAI-compatible baseURL profiles', () => {
|
|
55
55
|
const definition = createOpenAIProviderDefinition();
|
|
56
56
|
|
|
57
57
|
expect(() =>
|
|
@@ -61,14 +61,13 @@ describe('createOpenAIProviderDefinition', () => {
|
|
|
61
61
|
apiKey: 'lm-studio',
|
|
62
62
|
baseURL: 'http://localhost:1234/v1',
|
|
63
63
|
options: {
|
|
64
|
-
|
|
64
|
+
nativeWebTools: {
|
|
65
65
|
webSearch: true,
|
|
66
|
-
webFetch: true,
|
|
67
66
|
},
|
|
68
67
|
},
|
|
69
68
|
}),
|
|
70
69
|
).toThrow(
|
|
71
|
-
|
|
70
|
+
"Provider openai profile uses an OpenAI-compatible endpoint or Chat Completions; hosted web search runs only on OpenAI's own Responses API.",
|
|
72
71
|
);
|
|
73
72
|
});
|
|
74
73
|
});
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
import { readNativeWebSearchOption } from '@robota-sdk/agent-core';
|
|
1
2
|
import { probeOpenAICompatibleProfile } from '@robota-sdk/agent-provider-openai-compatible/shared';
|
|
2
3
|
|
|
3
4
|
import { parseExtraBody } from './extra-body';
|
|
4
5
|
import { OpenAIProvider } from './provider';
|
|
5
6
|
import { parseParallelToolCalls } from './parallel-tool-policy';
|
|
6
7
|
|
|
7
|
-
import type {
|
|
8
|
-
import type {
|
|
8
|
+
import type { TOpenAIApiSurface } from './types';
|
|
9
|
+
import type {
|
|
10
|
+
IProviderDefinition,
|
|
11
|
+
IProviderDefinitionConfig,
|
|
12
|
+
TUniversalValue,
|
|
13
|
+
} from '@robota-sdk/agent-core';
|
|
9
14
|
|
|
10
15
|
export const DEFAULT_OPENAI_PROVIDER_MODEL: string | undefined = undefined;
|
|
11
16
|
export const DEFAULT_OPENAI_PROVIDER_API_KEY_REFERENCE = '$ENV:OPENAI_API_KEY';
|
|
@@ -60,6 +65,9 @@ export function createOpenAIProviderDefinition(): IProviderDefinition {
|
|
|
60
65
|
// The OpenAI SDK falls back to the base URL variable when none is configured, and sends the
|
|
61
66
|
// organization and project the others name.
|
|
62
67
|
destinationEnvironment: ['OPENAI_BASE_URL', 'OPENAI_ORG_ID', 'OPENAI_PROJECT_ID'],
|
|
68
|
+
// The SDK falls back to OPENAI_BASE_URL: with it set, the endpoint is one we cannot vouch for.
|
|
69
|
+
nativeWebTools: (profile, environment) =>
|
|
70
|
+
runsOpenAIHostedTools(profile) && !environment['OPENAI_BASE_URL'] ? ['webSearch'] : [],
|
|
63
71
|
createProvider: (config) => {
|
|
64
72
|
const parallelToolCalls = parseParallelToolCalls(
|
|
65
73
|
config.options?.parallelToolCalls,
|
|
@@ -67,8 +75,12 @@ export function createOpenAIProviderDefinition(): IProviderDefinition {
|
|
|
67
75
|
);
|
|
68
76
|
const extraBody = parseExtraBody(config.options?.extraBody);
|
|
69
77
|
const apiSurface = readApiSurface(config.options);
|
|
70
|
-
const
|
|
71
|
-
|
|
78
|
+
const webSearch = readNativeWebSearchOption('openai', config.options);
|
|
79
|
+
if (webSearch && !runsOpenAIHostedTools(config)) {
|
|
80
|
+
throw new Error(
|
|
81
|
+
"Provider openai profile uses an OpenAI-compatible endpoint or Chat Completions; hosted web search runs only on OpenAI's own Responses API. Connect an MCP server that offers web search.",
|
|
82
|
+
);
|
|
83
|
+
}
|
|
72
84
|
return new OpenAIProvider({
|
|
73
85
|
apiKey: requireApiKey(config.apiKey),
|
|
74
86
|
...(config.baseURL !== undefined && { baseURL: config.baseURL }),
|
|
@@ -76,7 +88,7 @@ export function createOpenAIProviderDefinition(): IProviderDefinition {
|
|
|
76
88
|
...(apiSurface !== undefined && { apiSurface }),
|
|
77
89
|
...(parallelToolCalls !== undefined && { parallelToolCalls }),
|
|
78
90
|
...(extraBody !== undefined && { extraBody }),
|
|
79
|
-
...(
|
|
91
|
+
...(webSearch && { nativeWebTools: { webSearch: true } }),
|
|
80
92
|
...(config.options?.durableOperations === true && { durableOperations: true }),
|
|
81
93
|
defaultModel: config.model,
|
|
82
94
|
});
|
|
@@ -101,49 +113,9 @@ function readApiSurface(
|
|
|
101
113
|
return undefined;
|
|
102
114
|
}
|
|
103
115
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
);
|
|
116
|
+
/** Hosted tools run only on OpenAI's own Responses API: no custom endpoint, not Chat Completions. */
|
|
117
|
+
function runsOpenAIHostedTools(
|
|
118
|
+
profile: Pick<IProviderDefinitionConfig, 'baseURL' | 'options'>,
|
|
119
|
+
): boolean {
|
|
120
|
+
return profile.baseURL === undefined && readApiSurface(profile.options) !== 'chat-completions';
|
|
149
121
|
}
|
package/src/openai/provider.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AbstractAIProvider } from '@robota-sdk/agent-core';
|
|
1
|
+
import { AbstractAIProvider, randomId } from '@robota-sdk/agent-core';
|
|
3
2
|
import { createModelEffortOutcome } from '@robota-sdk/agent-core';
|
|
4
3
|
import { PERMISSIVE_TOOL_SCHEMA_PROFILE, STRICT_TOOL_SCHEMA_PROFILE } from '@robota-sdk/agent-core';
|
|
5
4
|
import { resolveModelEffort } from '@robota-sdk/agent-core';
|
|
@@ -16,6 +15,7 @@ import { OPENAI_MODEL_EFFORT_TABLE } from './model-effort-table';
|
|
|
16
15
|
import { assertParallelToolCallsExecutor, parseParallelToolCalls } from './parallel-tool-policy';
|
|
17
16
|
import { OpenAIResponseParser } from './parsers/response-parser';
|
|
18
17
|
import { chatStreamWithOpenAIResponsesApi, chatWithOpenAIResponsesApi } from './responses-chat';
|
|
18
|
+
import { modelOffersWebSearch } from './responses-web-search';
|
|
19
19
|
|
|
20
20
|
import type { IPayloadLogger } from './interfaces/payload-logger';
|
|
21
21
|
import type { IOpenAIProviderOptions, TOpenAIApiSurface, TOpenAIExtraBody } from './types';
|
|
@@ -25,6 +25,7 @@ import type {
|
|
|
25
25
|
IAssistantMessage,
|
|
26
26
|
IProviderCapabilities,
|
|
27
27
|
IProviderModelEffortTable,
|
|
28
|
+
IProviderNativeWebToolCapability,
|
|
28
29
|
IToolSchemaProjectionProfile,
|
|
29
30
|
TTextDeltaCallback,
|
|
30
31
|
} from '@robota-sdk/agent-core';
|
|
@@ -48,6 +49,10 @@ export class OpenAIProvider extends AbstractAIProvider {
|
|
|
48
49
|
private readonly extraBody: TOpenAIExtraBody | undefined;
|
|
49
50
|
private readonly payloadLogger: IPayloadLogger | undefined;
|
|
50
51
|
private readonly responseParser: OpenAIResponseParser;
|
|
52
|
+
// A reply's items belong to the account that made them and OpenAI keeps them for a limited time,
|
|
53
|
+
// so only the instance that recorded them sends them back, which also leaves out a process that
|
|
54
|
+
// restarted.
|
|
55
|
+
private readonly replayOwner = randomId();
|
|
51
56
|
|
|
52
57
|
/**
|
|
53
58
|
* Optional callback for text deltas during streaming.
|
|
@@ -65,7 +70,7 @@ export class OpenAIProvider extends AbstractAIProvider {
|
|
|
65
70
|
this.extraBody = parseExtraBody(options.extraBody);
|
|
66
71
|
assertExtraBodyExecutor(this.extraBody, options.executor !== undefined);
|
|
67
72
|
this.apiSurface = resolveApiSurface(options);
|
|
68
|
-
validateOpenAIProviderNativeWebTools(this.apiSurface, options
|
|
73
|
+
validateOpenAIProviderNativeWebTools(this.apiSurface, options);
|
|
69
74
|
|
|
70
75
|
if (options.executor) {
|
|
71
76
|
this.executor = options.executor;
|
|
@@ -126,6 +131,7 @@ export class OpenAIProvider extends AbstractAIProvider {
|
|
|
126
131
|
extraBody: this.extraBody,
|
|
127
132
|
onTextDelta: this.onTextDelta,
|
|
128
133
|
requestHeaders: this.traceRequestHeaders(resolvedOptions),
|
|
134
|
+
replayOwner: this.replayOwner,
|
|
129
135
|
});
|
|
130
136
|
this.publishModelEffortOutcome(resolvedOptions);
|
|
131
137
|
return response;
|
|
@@ -199,6 +205,7 @@ export class OpenAIProvider extends AbstractAIProvider {
|
|
|
199
205
|
extraBody: this.extraBody,
|
|
200
206
|
onTextDelta: this.onTextDelta,
|
|
201
207
|
requestHeaders: this.traceRequestHeaders(resolvedOptions),
|
|
208
|
+
replayOwner: this.replayOwner,
|
|
202
209
|
});
|
|
203
210
|
this.publishModelEffortOutcome(resolvedOptions);
|
|
204
211
|
return;
|
|
@@ -251,7 +258,7 @@ export class OpenAIProvider extends AbstractAIProvider {
|
|
|
251
258
|
...(this.canPropagateTraceContext()
|
|
252
259
|
? traceHeadersFor(this.effectiveBaseUrl(), options?.outboundTraceContext)
|
|
253
260
|
: {}),
|
|
254
|
-
...(this.options.durableOperations === true ? { 'Idempotency-Key':
|
|
261
|
+
...(this.options.durableOperations === true ? { 'Idempotency-Key': randomId() } : {}),
|
|
255
262
|
};
|
|
256
263
|
}
|
|
257
264
|
|
|
@@ -272,13 +279,20 @@ export class OpenAIProvider extends AbstractAIProvider {
|
|
|
272
279
|
}
|
|
273
280
|
|
|
274
281
|
/**
|
|
275
|
-
* MCP-005: strict mode's supported subset is documented; non-strict accepts standard JSON Schema
|
|
276
|
-
*
|
|
282
|
+
* MCP-005: strict mode's supported subset is documented; non-strict accepts standard JSON Schema
|
|
283
|
+
* except that the API answers `array schema missing items` and `object schema missing properties`
|
|
284
|
+
* in either mode. Both the Responses and Chat Completions surfaces honor `strictTools` identically
|
|
285
|
+
* (§ Profiles).
|
|
277
286
|
*/
|
|
278
287
|
protected override projectionProfile(): IToolSchemaProjectionProfile | undefined {
|
|
279
288
|
return this.options.strictTools
|
|
280
289
|
? { ...STRICT_TOOL_SCHEMA_PROFILE, providerName: 'openai' }
|
|
281
|
-
: {
|
|
290
|
+
: {
|
|
291
|
+
...PERMISSIVE_TOOL_SCHEMA_PROFILE,
|
|
292
|
+
providerName: 'openai',
|
|
293
|
+
arraysDeclareItems: true,
|
|
294
|
+
objectsDeclareProperties: true,
|
|
295
|
+
};
|
|
282
296
|
}
|
|
283
297
|
|
|
284
298
|
/**
|
|
@@ -345,17 +359,15 @@ export class OpenAIProvider extends AbstractAIProvider {
|
|
|
345
359
|
return {
|
|
346
360
|
functionCalling: { supported: true },
|
|
347
361
|
nativeWebTools: {
|
|
348
|
-
webSearch:
|
|
349
|
-
supported: false,
|
|
350
|
-
enabled: false,
|
|
351
|
-
source,
|
|
352
|
-
reason: getOpenAIUnsupportedNativeWebReason(this.apiSurface, 'search'),
|
|
353
|
-
},
|
|
362
|
+
webSearch: describeWebSearch(this.apiSurface, this.options, source),
|
|
354
363
|
webFetch: {
|
|
355
364
|
supported: false,
|
|
356
365
|
enabled: false,
|
|
357
366
|
source,
|
|
358
|
-
reason:
|
|
367
|
+
reason:
|
|
368
|
+
this.apiSurface === 'chat-completions'
|
|
369
|
+
? getOpenAIChatCompletionsNativeWebReason('fetch')
|
|
370
|
+
: 'OpenAI provider exposes hosted web search only.',
|
|
359
371
|
},
|
|
360
372
|
},
|
|
361
373
|
};
|
|
@@ -394,24 +406,68 @@ function resolveApiSurface(options: IOpenAIProviderOptions): TOpenAIApiSurface {
|
|
|
394
406
|
return options.baseURL ? 'chat-completions' : 'responses';
|
|
395
407
|
}
|
|
396
408
|
|
|
397
|
-
function
|
|
409
|
+
function getOpenAIChatCompletionsNativeWebReason(toolKind: 'search' | 'fetch'): string {
|
|
410
|
+
return `OpenAI-compatible Chat Completions endpoints support declared function tools, not provider-native web ${toolKind}.`;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Hosted web search runs only on OpenAI's own Responses API, so it is offered on the vendor endpoint
|
|
415
|
+
* with that surface and nowhere else.
|
|
416
|
+
*/
|
|
417
|
+
function describeWebSearch(
|
|
398
418
|
apiSurface: TOpenAIApiSurface,
|
|
399
|
-
|
|
400
|
-
|
|
419
|
+
options: IOpenAIProviderOptions,
|
|
420
|
+
source: string,
|
|
421
|
+
): IProviderNativeWebToolCapability {
|
|
401
422
|
if (apiSurface === 'chat-completions') {
|
|
402
|
-
return
|
|
423
|
+
return {
|
|
424
|
+
supported: false,
|
|
425
|
+
enabled: false,
|
|
426
|
+
source,
|
|
427
|
+
reason: getOpenAIChatCompletionsNativeWebReason('search'),
|
|
428
|
+
};
|
|
403
429
|
}
|
|
404
|
-
|
|
430
|
+
if (options.baseURL !== undefined) {
|
|
431
|
+
return {
|
|
432
|
+
supported: false,
|
|
433
|
+
enabled: false,
|
|
434
|
+
source,
|
|
435
|
+
reason: "Hosted web search runs only on OpenAI's own endpoint, not a custom baseURL.",
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
if (options.nativeWebTools?.webSearch !== true) {
|
|
439
|
+
return {
|
|
440
|
+
supported: true,
|
|
441
|
+
enabled: false,
|
|
442
|
+
source,
|
|
443
|
+
reason: 'Set the provider profile option nativeWebTools.webSearch to true.',
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
const model = options.defaultModel;
|
|
447
|
+
if (model !== undefined && !modelOffersWebSearch(model)) {
|
|
448
|
+
return {
|
|
449
|
+
supported: true,
|
|
450
|
+
enabled: false,
|
|
451
|
+
source,
|
|
452
|
+
reason: `Model ${model} does not offer web search; requests go without it.`,
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
return { supported: true, enabled: true, source };
|
|
405
456
|
}
|
|
406
457
|
|
|
407
458
|
function validateOpenAIProviderNativeWebTools(
|
|
408
459
|
apiSurface: TOpenAIApiSurface,
|
|
409
|
-
|
|
460
|
+
options: IOpenAIProviderOptions,
|
|
410
461
|
): void {
|
|
411
|
-
if (nativeWebTools?.
|
|
412
|
-
|
|
462
|
+
if (options.nativeWebTools?.webFetch === true) {
|
|
463
|
+
throw new Error('Provider openai does not support native web fetch.');
|
|
464
|
+
}
|
|
465
|
+
if (
|
|
466
|
+
options.nativeWebTools?.webSearch === true &&
|
|
467
|
+
(apiSurface !== 'responses' || options.baseURL !== undefined)
|
|
468
|
+
) {
|
|
469
|
+
throw new Error(
|
|
470
|
+
`Provider openai hosted web search runs only on OpenAI's own Responses API, not on apiSurface ${apiSurface}${options.baseURL !== undefined ? ' with a custom baseURL' : ''}.`,
|
|
471
|
+
);
|
|
413
472
|
}
|
|
414
|
-
throw new Error(
|
|
415
|
-
`Provider openai native web search/fetch is not supported for apiSurface ${apiSurface} in this provider version.`,
|
|
416
|
-
);
|
|
417
473
|
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { toProviderError } from '@robota-sdk/agent-core';
|
|
1
|
+
import { toProviderError, randomId } from '@robota-sdk/agent-core';
|
|
4
2
|
|
|
5
3
|
import {
|
|
6
4
|
observeProviderNativeRawPayloadStream,
|
|
@@ -22,11 +20,13 @@ import {
|
|
|
22
20
|
} from './responses-converter';
|
|
23
21
|
import { openAIRequestOptions } from './request-options';
|
|
24
22
|
import { assembleOpenAIResponsesStream, parseOpenAIResponsesResponse } from './responses-parser';
|
|
23
|
+
import { includesHostedWebSearch } from './responses-web-search';
|
|
25
24
|
|
|
26
25
|
import type {
|
|
27
26
|
IOpenAIResponsesRequestNonStreaming,
|
|
28
27
|
IOpenAIResponsesRequestStreaming,
|
|
29
28
|
TOpenAIResponsesStreamEvent,
|
|
29
|
+
TOpenAIResponsesTool,
|
|
30
30
|
} from './responses-types';
|
|
31
31
|
import type { IOpenAIProviderOptions, TOpenAIExtraBody } from './types';
|
|
32
32
|
import type { IChatOptions, TTextDeltaCallback, TUniversalMessage } from '@robota-sdk/agent-core';
|
|
@@ -42,6 +42,16 @@ export interface IOpenAIResponsesChatOptions {
|
|
|
42
42
|
onTextDelta?: TTextDeltaCallback;
|
|
43
43
|
/** Per-request headers (trusted `traceparent`), sent after the raw request payload is captured. */
|
|
44
44
|
requestHeaders?: Readonly<Record<string, string>>;
|
|
45
|
+
/** Names the provider instance, so a reply's items are sent back only by the one that recorded them. */
|
|
46
|
+
replayOwner: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A request's wire body, with the decisions about it the response parser also needs. */
|
|
50
|
+
interface IResponsesRequest<TParams> {
|
|
51
|
+
params: TParams;
|
|
52
|
+
hostedWebSearch: boolean;
|
|
53
|
+
/** Who may send this reply's items back: this instance on this model. Absent when unstored. */
|
|
54
|
+
replayScope: string | undefined;
|
|
45
55
|
}
|
|
46
56
|
|
|
47
57
|
interface IResponsesStreamMessageQueue {
|
|
@@ -70,7 +80,7 @@ export async function chatWithOpenAIResponsesApi(
|
|
|
70
80
|
}
|
|
71
81
|
|
|
72
82
|
try {
|
|
73
|
-
const requestParams =
|
|
83
|
+
const { params: requestParams, hostedWebSearch, replayScope } = buildResponsesRequest(input);
|
|
74
84
|
input.chatOptions?.onProviderNativeRawPayload?.({
|
|
75
85
|
provider: 'openai',
|
|
76
86
|
apiSurface: 'responses',
|
|
@@ -88,7 +98,7 @@ export async function chatWithOpenAIResponsesApi(
|
|
|
88
98
|
payload: response,
|
|
89
99
|
});
|
|
90
100
|
return withProviderRequestId(
|
|
91
|
-
parseOpenAIResponsesResponse(response),
|
|
101
|
+
parseOpenAIResponsesResponse(response, { hostedWebSearch, replayScope }),
|
|
92
102
|
readOpenAIRequestId(response),
|
|
93
103
|
);
|
|
94
104
|
} catch (error) {
|
|
@@ -126,7 +136,11 @@ async function chatWithOpenAIResponsesStreamingAssembly(
|
|
|
126
136
|
}
|
|
127
137
|
|
|
128
138
|
try {
|
|
129
|
-
const
|
|
139
|
+
const {
|
|
140
|
+
params: requestParams,
|
|
141
|
+
hostedWebSearch,
|
|
142
|
+
replayScope,
|
|
143
|
+
} = buildResponsesStreamingRequest(input);
|
|
130
144
|
input.chatOptions?.onProviderNativeRawPayload?.({
|
|
131
145
|
provider: 'openai',
|
|
132
146
|
apiSurface: 'responses',
|
|
@@ -151,6 +165,8 @@ async function chatWithOpenAIResponsesStreamingAssembly(
|
|
|
151
165
|
onTextDelta: input.chatOptions?.onTextDelta,
|
|
152
166
|
onThinkingDelta: input.chatOptions?.onThinkingDelta,
|
|
153
167
|
signal: input.chatOptions?.signal,
|
|
168
|
+
hostedWebSearch,
|
|
169
|
+
replayScope,
|
|
154
170
|
});
|
|
155
171
|
return withProviderRequestId(assembled, providerRequestId);
|
|
156
172
|
} catch (error) {
|
|
@@ -158,9 +174,9 @@ async function chatWithOpenAIResponsesStreamingAssembly(
|
|
|
158
174
|
}
|
|
159
175
|
}
|
|
160
176
|
|
|
161
|
-
function
|
|
177
|
+
function buildResponsesRequest(
|
|
162
178
|
input: IOpenAIResponsesChatOptions,
|
|
163
|
-
): IOpenAIResponsesRequestNonStreaming {
|
|
179
|
+
): IResponsesRequest<IOpenAIResponsesRequestNonStreaming> {
|
|
164
180
|
const model = input.chatOptions?.model ?? input.providerOptions.defaultModel;
|
|
165
181
|
if (!model) {
|
|
166
182
|
throw new Error(
|
|
@@ -168,10 +184,9 @@ function buildResponsesRequestParams(
|
|
|
168
184
|
);
|
|
169
185
|
}
|
|
170
186
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
input.providerOptions.
|
|
174
|
-
);
|
|
187
|
+
// OpenAI has no items for an unstored response, so there is nothing to send back for one.
|
|
188
|
+
const replayScope =
|
|
189
|
+
input.providerOptions.store === false ? undefined : `${input.replayOwner}/${model}`;
|
|
175
190
|
const textConfig = buildOpenAIResponsesTextConfig(input.providerOptions);
|
|
176
191
|
// Native effort mapping: thread the per-call reasoning-effort dial onto the Responses
|
|
177
192
|
// API `reasoning.effort` parameter, merged with any static reasoning options.
|
|
@@ -179,11 +194,27 @@ function buildResponsesRequestParams(
|
|
|
179
194
|
input.providerOptions.reasoning,
|
|
180
195
|
input.chatOptions?.effortResolution,
|
|
181
196
|
);
|
|
182
|
-
|
|
197
|
+
const hostedWebSearch = includesHostedWebSearch({
|
|
198
|
+
providerOptions: input.providerOptions,
|
|
199
|
+
chatOptions: input.chatOptions,
|
|
200
|
+
model,
|
|
201
|
+
reasoning,
|
|
202
|
+
});
|
|
203
|
+
const functionTools = convertToOpenAIResponsesTools(
|
|
204
|
+
input.chatOptions?.tools,
|
|
205
|
+
input.providerOptions.strictTools,
|
|
206
|
+
);
|
|
207
|
+
// Hosted search goes last, after the caller's functions; a request with neither sends no tools.
|
|
208
|
+
const tools: TOpenAIResponsesTool[] | undefined = hostedWebSearch
|
|
209
|
+
? [...(functionTools ?? []), { type: 'web_search' }]
|
|
210
|
+
: functionTools;
|
|
211
|
+
const params: IOpenAIResponsesRequestNonStreaming = {
|
|
183
212
|
...extraBodyWire(input.extraBody),
|
|
184
213
|
model,
|
|
185
214
|
...parallelToolCallsWire(input.parallelToolCalls),
|
|
186
|
-
|
|
215
|
+
// Search items go back only on a request that declares the tool: OpenAI backends are reported to
|
|
216
|
+
// refuse them otherwise, and a forced summary, the last-resort answer, must not fail on that.
|
|
217
|
+
input: convertToOpenAIResponsesInput(input.messages, hostedWebSearch ? replayScope : undefined),
|
|
187
218
|
...(tools !== undefined && {
|
|
188
219
|
tools,
|
|
189
220
|
tool_choice: toOpenAIResponsesToolChoice(input.chatOptions?.toolChoice),
|
|
@@ -203,20 +234,19 @@ function buildResponsesRequestParams(
|
|
|
203
234
|
}),
|
|
204
235
|
...(input.providerOptions.store !== undefined && { store: input.providerOptions.store }),
|
|
205
236
|
};
|
|
237
|
+
return { params, hostedWebSearch, replayScope };
|
|
206
238
|
}
|
|
207
239
|
|
|
208
|
-
function
|
|
240
|
+
function buildResponsesStreamingRequest(
|
|
209
241
|
input: IOpenAIResponsesChatOptions,
|
|
210
|
-
): IOpenAIResponsesRequestStreaming {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
stream: true,
|
|
214
|
-
};
|
|
242
|
+
): IResponsesRequest<IOpenAIResponsesRequestStreaming> {
|
|
243
|
+
const { params, ...request } = buildResponsesRequest(input);
|
|
244
|
+
return { params: { ...params, stream: true }, ...request };
|
|
215
245
|
}
|
|
216
246
|
|
|
217
247
|
function createStreamDeltaMessage(delta: string): TUniversalMessage {
|
|
218
248
|
return {
|
|
219
|
-
id:
|
|
249
|
+
id: randomId(),
|
|
220
250
|
role: 'assistant',
|
|
221
251
|
content: delta,
|
|
222
252
|
state: 'complete',
|