@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,219 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IOpenAIJsonSchemaDefinition,
|
|
3
|
+
IOpenAIResponsesReasoningOptions,
|
|
4
|
+
TOpenAIProviderOptionValue,
|
|
5
|
+
} from './types';
|
|
6
|
+
import type { IToolSchema } from '@robota-sdk/agent-core';
|
|
7
|
+
|
|
8
|
+
export type TOpenAIResponsesInputRole = 'user' | 'assistant' | 'system' | 'developer';
|
|
9
|
+
|
|
10
|
+
export interface IOpenAIResponsesInputTextContent {
|
|
11
|
+
type: 'input_text';
|
|
12
|
+
text: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface IOpenAIResponsesInputImageContent {
|
|
16
|
+
type: 'input_image';
|
|
17
|
+
image_url: string;
|
|
18
|
+
detail?: 'auto' | 'low' | 'high';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type TOpenAIResponsesInputContent =
|
|
22
|
+
IOpenAIResponsesInputTextContent | IOpenAIResponsesInputImageContent;
|
|
23
|
+
|
|
24
|
+
export interface IOpenAIResponsesMessageInput {
|
|
25
|
+
type?: 'message';
|
|
26
|
+
role: TOpenAIResponsesInputRole;
|
|
27
|
+
content: string | TOpenAIResponsesInputContent[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface IOpenAIResponsesFunctionCallInput {
|
|
31
|
+
type: 'function_call';
|
|
32
|
+
call_id: string;
|
|
33
|
+
name: string;
|
|
34
|
+
arguments: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface IOpenAIResponsesFunctionCallOutputInput {
|
|
38
|
+
type: 'function_call_output';
|
|
39
|
+
call_id: string;
|
|
40
|
+
output: string | TOpenAIResponsesInputContent[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export type TOpenAIResponsesInputItem =
|
|
44
|
+
| IOpenAIResponsesMessageInput
|
|
45
|
+
| IOpenAIResponsesFunctionCallInput
|
|
46
|
+
| IOpenAIResponsesFunctionCallOutputInput;
|
|
47
|
+
|
|
48
|
+
export interface IOpenAIResponsesFunctionTool {
|
|
49
|
+
type: 'function';
|
|
50
|
+
name: string;
|
|
51
|
+
parameters: IToolSchema['parameters'];
|
|
52
|
+
strict: boolean | null;
|
|
53
|
+
description?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type TOpenAIResponsesTool = IOpenAIResponsesFunctionTool;
|
|
57
|
+
|
|
58
|
+
export interface IOpenAIResponsesTextFormatText {
|
|
59
|
+
type: 'text';
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface IOpenAIResponsesTextFormatJsonObject {
|
|
63
|
+
type: 'json_object';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface IOpenAIResponsesTextFormatJsonSchema extends IOpenAIJsonSchemaDefinition {
|
|
67
|
+
type: 'json_schema';
|
|
68
|
+
schema: Record<string, TOpenAIProviderOptionValue>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type TOpenAIResponsesTextFormat =
|
|
72
|
+
| IOpenAIResponsesTextFormatText
|
|
73
|
+
| IOpenAIResponsesTextFormatJsonObject
|
|
74
|
+
| IOpenAIResponsesTextFormatJsonSchema;
|
|
75
|
+
|
|
76
|
+
export interface IOpenAIResponsesTextConfig {
|
|
77
|
+
format: TOpenAIResponsesTextFormat;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface IOpenAIResponsesRequestBase {
|
|
81
|
+
model: string;
|
|
82
|
+
parallel_tool_calls?: boolean;
|
|
83
|
+
input: TOpenAIResponsesInputItem[];
|
|
84
|
+
tools?: TOpenAIResponsesTool[];
|
|
85
|
+
tool_choice?: 'auto' | 'none' | 'required' | { type: 'function'; name: string };
|
|
86
|
+
text?: IOpenAIResponsesTextConfig;
|
|
87
|
+
temperature?: number;
|
|
88
|
+
max_output_tokens?: number;
|
|
89
|
+
reasoning?: IOpenAIResponsesReasoningOptions;
|
|
90
|
+
include?: 'reasoning.encrypted_content'[];
|
|
91
|
+
store?: boolean;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface IOpenAIResponsesRequestNonStreaming extends IOpenAIResponsesRequestBase {
|
|
95
|
+
stream?: false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface IOpenAIResponsesRequestStreaming extends IOpenAIResponsesRequestBase {
|
|
99
|
+
stream: true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface IOpenAIResponsesTextOutputContent {
|
|
103
|
+
type: 'output_text' | 'text';
|
|
104
|
+
text: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface IOpenAIResponsesRefusalOutputContent {
|
|
108
|
+
type: 'refusal';
|
|
109
|
+
refusal: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export type TOpenAIResponsesOutputContent =
|
|
113
|
+
IOpenAIResponsesTextOutputContent | IOpenAIResponsesRefusalOutputContent;
|
|
114
|
+
|
|
115
|
+
export interface IOpenAIResponsesMessageOutputItem {
|
|
116
|
+
type: 'message';
|
|
117
|
+
role?: 'assistant';
|
|
118
|
+
content: TOpenAIResponsesOutputContent[];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface IOpenAIResponsesFunctionCallOutputItem {
|
|
122
|
+
type: 'function_call';
|
|
123
|
+
call_id: string;
|
|
124
|
+
name: string;
|
|
125
|
+
arguments: string;
|
|
126
|
+
id?: string;
|
|
127
|
+
status?: string;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface IOpenAIResponsesReasoningOutputItem {
|
|
131
|
+
type: 'reasoning';
|
|
132
|
+
summary?: Array<{ text?: string }>;
|
|
133
|
+
encrypted_content?: string;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export interface IOpenAIResponsesGenericOutputItem {
|
|
137
|
+
type: string;
|
|
138
|
+
id?: string;
|
|
139
|
+
status?: string;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export type TOpenAIResponsesOutputItem =
|
|
143
|
+
| IOpenAIResponsesMessageOutputItem
|
|
144
|
+
| IOpenAIResponsesFunctionCallOutputItem
|
|
145
|
+
| IOpenAIResponsesReasoningOutputItem
|
|
146
|
+
| IOpenAIResponsesGenericOutputItem;
|
|
147
|
+
|
|
148
|
+
export interface IOpenAIResponsesUsage {
|
|
149
|
+
input_tokens?: number;
|
|
150
|
+
output_tokens?: number;
|
|
151
|
+
total_tokens?: number;
|
|
152
|
+
/** `cached_tokens` is the part of `input_tokens` served from the prompt cache. */
|
|
153
|
+
input_tokens_details?: { cached_tokens?: number } | null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export interface IOpenAIResponsesErrorBody {
|
|
157
|
+
message?: string;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export interface IOpenAIResponsesResponse {
|
|
161
|
+
id?: string;
|
|
162
|
+
model?: string;
|
|
163
|
+
output_text?: string;
|
|
164
|
+
output?: TOpenAIResponsesOutputItem[];
|
|
165
|
+
usage?: IOpenAIResponsesUsage;
|
|
166
|
+
status?: string;
|
|
167
|
+
error?: IOpenAIResponsesErrorBody | null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface IOpenAIResponsesTextDeltaEvent {
|
|
171
|
+
type: 'response.output_text.delta';
|
|
172
|
+
delta: string;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* A reasoning item's summary is streamed in parts, each announced by this event before its text.
|
|
177
|
+
* `item_id` and `summary_index` name the part; a stream that omits them cannot be told apart.
|
|
178
|
+
*/
|
|
179
|
+
export interface IOpenAIResponsesReasoningSummaryPartAddedEvent {
|
|
180
|
+
type: 'response.reasoning_summary_part.added';
|
|
181
|
+
item_id?: string;
|
|
182
|
+
summary_index?: number;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Text of a reasoning summary part. The API streams it only when the request asked for a summary
|
|
187
|
+
* (`reasoning.summary`); it is never part of the reply text.
|
|
188
|
+
*/
|
|
189
|
+
export interface IOpenAIResponsesReasoningSummaryTextDeltaEvent {
|
|
190
|
+
type: 'response.reasoning_summary_text.delta';
|
|
191
|
+
item_id?: string;
|
|
192
|
+
summary_index?: number;
|
|
193
|
+
delta: string;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface IOpenAIResponsesCompletedEvent {
|
|
197
|
+
type: 'response.completed';
|
|
198
|
+
response: IOpenAIResponsesResponse;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export interface IOpenAIResponsesOutputItemDoneEvent {
|
|
202
|
+
type: 'response.output_item.done';
|
|
203
|
+
item: TOpenAIResponsesOutputItem;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export interface IOpenAIResponsesErrorEvent {
|
|
207
|
+
type: 'response.error' | 'response.failed' | 'response.incomplete';
|
|
208
|
+
message?: string;
|
|
209
|
+
error?: IOpenAIResponsesErrorBody;
|
|
210
|
+
response?: IOpenAIResponsesResponse;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export type TOpenAIResponsesStreamEvent =
|
|
214
|
+
| IOpenAIResponsesTextDeltaEvent
|
|
215
|
+
| IOpenAIResponsesReasoningSummaryPartAddedEvent
|
|
216
|
+
| IOpenAIResponsesReasoningSummaryTextDeltaEvent
|
|
217
|
+
| IOpenAIResponsesCompletedEvent
|
|
218
|
+
| IOpenAIResponsesOutputItemDoneEvent
|
|
219
|
+
| IOpenAIResponsesErrorEvent;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type OpenAI from 'openai';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* OpenAI API Request Parameters
|
|
5
|
+
* Replaces any types with specific OpenAI API structure
|
|
6
|
+
*/
|
|
7
|
+
export interface IOpenAIChatRequestParams {
|
|
8
|
+
model: string;
|
|
9
|
+
messages: OpenAI.Chat.ChatCompletionMessageParam[];
|
|
10
|
+
temperature?: number | undefined;
|
|
11
|
+
max_tokens?: number | undefined;
|
|
12
|
+
tools?: OpenAI.Chat.ChatCompletionTool[] | undefined;
|
|
13
|
+
tool_choice?: 'auto' | 'none' | OpenAI.Chat.ChatCompletionNamedToolChoice | undefined;
|
|
14
|
+
stream?: boolean | undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* OpenAI API streaming request parameters
|
|
19
|
+
*/
|
|
20
|
+
export interface IOpenAIStreamRequestParams extends Omit<IOpenAIChatRequestParams, 'stream'> {
|
|
21
|
+
stream: true;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* OpenAI API tool call structure
|
|
26
|
+
*/
|
|
27
|
+
export interface IOpenAIToolCall {
|
|
28
|
+
id: string;
|
|
29
|
+
type: 'function';
|
|
30
|
+
function: {
|
|
31
|
+
name: string;
|
|
32
|
+
arguments: string;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* OpenAI API message structure with tool calls
|
|
38
|
+
*/
|
|
39
|
+
export interface IOpenAIAssistantMessage {
|
|
40
|
+
role: 'assistant';
|
|
41
|
+
content: string | null;
|
|
42
|
+
tool_calls?: IOpenAIToolCall[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* OpenAI API tool message structure
|
|
47
|
+
*/
|
|
48
|
+
export interface IOpenAIToolMessage {
|
|
49
|
+
role: 'tool';
|
|
50
|
+
content: string;
|
|
51
|
+
tool_call_id: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* OpenAI streaming chunk delta structure
|
|
56
|
+
*/
|
|
57
|
+
export interface IOpenAIStreamDelta {
|
|
58
|
+
role?: 'assistant';
|
|
59
|
+
content?: string | null;
|
|
60
|
+
tool_calls?: Array<{
|
|
61
|
+
index: number;
|
|
62
|
+
id?: string;
|
|
63
|
+
type?: 'function';
|
|
64
|
+
function?: {
|
|
65
|
+
name?: string;
|
|
66
|
+
arguments?: string;
|
|
67
|
+
};
|
|
68
|
+
}>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* OpenAI streaming chunk structure
|
|
73
|
+
*/
|
|
74
|
+
export interface IOpenAIStreamChunk {
|
|
75
|
+
id: string;
|
|
76
|
+
object: 'chat.completion.chunk';
|
|
77
|
+
created: number;
|
|
78
|
+
model: string;
|
|
79
|
+
choices: Array<{
|
|
80
|
+
index: number;
|
|
81
|
+
delta: IOpenAIStreamDelta;
|
|
82
|
+
finish_reason?: string | null;
|
|
83
|
+
}>;
|
|
84
|
+
usage?: {
|
|
85
|
+
prompt_tokens: number;
|
|
86
|
+
completion_tokens: number;
|
|
87
|
+
total_tokens: number;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* OpenAI error structure for type-safe error handling
|
|
93
|
+
*/
|
|
94
|
+
export interface IOpenAIError {
|
|
95
|
+
message: string;
|
|
96
|
+
status?: number;
|
|
97
|
+
type?: string;
|
|
98
|
+
param?: string | null;
|
|
99
|
+
code?: string | null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Payload logging data structure
|
|
104
|
+
*/
|
|
105
|
+
export interface IOpenAILogData {
|
|
106
|
+
model: string;
|
|
107
|
+
messagesCount: number;
|
|
108
|
+
hasTools: boolean;
|
|
109
|
+
temperature?: number | undefined;
|
|
110
|
+
maxTokens?: number | undefined;
|
|
111
|
+
timestamp: string;
|
|
112
|
+
requestId?: string | undefined;
|
|
113
|
+
}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import type { IPayloadLogger } from './interfaces/payload-logger';
|
|
2
|
+
import type { IExecutor, ILogger, TProviderOptionValueBase } from '@robota-sdk/agent-core';
|
|
3
|
+
import type OpenAI from 'openai';
|
|
4
|
+
|
|
5
|
+
export type TOpenAIApiSurface = 'responses' | 'chat-completions';
|
|
6
|
+
|
|
7
|
+
export interface IOpenAIJsonSchemaDefinition {
|
|
8
|
+
name: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
schema?: Record<string, TOpenAIProviderOptionValue>;
|
|
11
|
+
strict?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface IOpenAIResponsesReasoningOptions {
|
|
15
|
+
effort?: 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max';
|
|
16
|
+
summary?: 'auto' | 'concise' | 'detailed';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface IOpenAINativeWebToolsOptions {
|
|
20
|
+
webSearch?: boolean;
|
|
21
|
+
webFetch?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A JSON value sent through {@link IOpenAIProviderOptions.extraBody}. */
|
|
25
|
+
export type TOpenAIExtraBodyValue =
|
|
26
|
+
| string
|
|
27
|
+
| number
|
|
28
|
+
| boolean
|
|
29
|
+
| null
|
|
30
|
+
| readonly TOpenAIExtraBodyValue[]
|
|
31
|
+
| { readonly [key: string]: TOpenAIExtraBodyValue };
|
|
32
|
+
|
|
33
|
+
/** Top-level request-body fields sent through {@link IOpenAIProviderOptions.extraBody}. */
|
|
34
|
+
export type TOpenAIExtraBody = Readonly<Record<string, TOpenAIExtraBodyValue>>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Valid provider option value types
|
|
38
|
+
*/
|
|
39
|
+
export type TOpenAIProviderOptionValue =
|
|
40
|
+
| string
|
|
41
|
+
| number
|
|
42
|
+
| boolean
|
|
43
|
+
| undefined
|
|
44
|
+
| null
|
|
45
|
+
| IOpenAIJsonSchemaDefinition
|
|
46
|
+
| IOpenAINativeWebToolsOptions
|
|
47
|
+
| IOpenAIResponsesReasoningOptions
|
|
48
|
+
| TOpenAIExtraBody
|
|
49
|
+
| OpenAI
|
|
50
|
+
| IPayloadLogger
|
|
51
|
+
| ILogger
|
|
52
|
+
| IExecutor
|
|
53
|
+
| TProviderOptionValueBase
|
|
54
|
+
| TOpenAIProviderOptionValue[]
|
|
55
|
+
| { [key: string]: TOpenAIProviderOptionValue };
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* OpenAI provider options
|
|
59
|
+
*/
|
|
60
|
+
export interface IOpenAIProviderOptions {
|
|
61
|
+
/** Emit one operation ID per model request, preserved by SDK retries, for durable company gateways. */
|
|
62
|
+
durableOperations?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Additional provider-specific options
|
|
65
|
+
*/
|
|
66
|
+
[key: string]: TOpenAIProviderOptionValue;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* OpenAI API key (required when client is not provided)
|
|
70
|
+
*/
|
|
71
|
+
apiKey?: string;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* OpenAI organization ID (optional)
|
|
75
|
+
*/
|
|
76
|
+
organization?: string;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* API request timeout (milliseconds)
|
|
80
|
+
*/
|
|
81
|
+
timeout?: number;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Whether to request token usage on streaming turns via
|
|
85
|
+
* `stream_options: { include_usage: true }` (default `true`). Set `false` for
|
|
86
|
+
* OpenAI-compatible endpoints that reject `stream_options`; usage is then absent
|
|
87
|
+
* on streaming turns for this provider.
|
|
88
|
+
*/
|
|
89
|
+
includeStreamUsage?: boolean;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* API base URL (default: `'https://api.openai.com/v1'`).
|
|
93
|
+
*
|
|
94
|
+
* Point this at ANY OpenAI-compatible endpoint — this provider is a protocol
|
|
95
|
+
* client, not an OpenAI-vendor lock:
|
|
96
|
+
*
|
|
97
|
+
* - AI gateways: Vercel AI Gateway, LiteLLM, OpenRouter, …
|
|
98
|
+
* - Hosted compatibles: Azure OpenAI, Groq, Together, …
|
|
99
|
+
* - Local/self-hosted: vLLM, Ollama, LM Studio, …
|
|
100
|
+
*
|
|
101
|
+
* Setting `baseURL` switches the default {@link apiSurface} to
|
|
102
|
+
* `'chat-completions'` for broad endpoint compatibility (official OpenAI calls
|
|
103
|
+
* default to the Responses API). Model slugs are passed through verbatim, so
|
|
104
|
+
* gateway-namespaced ids like `anthropic/claude-sonnet-4-5` or
|
|
105
|
+
* `meta-llama/llama-3.1-70b` work as `defaultModel`/`model` values — streaming
|
|
106
|
+
* and tool calling ride the same chat-completions protocol.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```ts
|
|
110
|
+
* // Vercel AI Gateway with a non-OpenAI model slug
|
|
111
|
+
* new OpenAIProvider({
|
|
112
|
+
* apiKey: process.env.AI_GATEWAY_API_KEY,
|
|
113
|
+
* baseURL: 'https://ai-gateway.vercel.sh/v1',
|
|
114
|
+
* defaultModel: 'anthropic/claude-sonnet-4-5',
|
|
115
|
+
* });
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
baseURL?: string;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Default model used when chat options do not provide a model.
|
|
122
|
+
*/
|
|
123
|
+
defaultModel?: string;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* API surface to use for direct OpenAI calls.
|
|
127
|
+
*
|
|
128
|
+
* Defaults to Responses for official OpenAI calls. Profiles with baseURL use
|
|
129
|
+
* Chat Completions by default for OpenAI-compatible endpoint compatibility.
|
|
130
|
+
*/
|
|
131
|
+
apiSurface?: TOpenAIApiSurface;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Response format (default: 'text')
|
|
135
|
+
* - 'text': Plain text response
|
|
136
|
+
* - 'json_object': JSON object mode (requires system message)
|
|
137
|
+
* - 'json_schema': Structured Outputs with schema validation
|
|
138
|
+
*/
|
|
139
|
+
responseFormat?: 'text' | 'json_object' | 'json_schema';
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* JSON schema for structured outputs (required when responseFormat is 'json_schema')
|
|
143
|
+
*/
|
|
144
|
+
jsonSchema?: IOpenAIJsonSchemaDefinition;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Responses API reasoning controls. Hidden reasoning is never exposed in message
|
|
148
|
+
* content; only explicit summaries/encrypted items requested here are represented.
|
|
149
|
+
*/
|
|
150
|
+
reasoning?: IOpenAIResponsesReasoningOptions;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Whether OpenAI should store Responses API results. Defaults to OpenAI API behavior.
|
|
154
|
+
*/
|
|
155
|
+
store?: boolean;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Include encrypted reasoning items for stateless reasoning continuation.
|
|
159
|
+
*/
|
|
160
|
+
includeEncryptedReasoning?: boolean;
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Opt into strict custom function parameter validation where supported.
|
|
164
|
+
*
|
|
165
|
+
* PROV-007: the tool schema is rewritten for strict mode on the way out — every object node,
|
|
166
|
+
* nested ones included, is closed and given a complete `required` list. That transformation is
|
|
167
|
+
* lossy, so it runs ONLY when this flag is on; with it off the schema is forwarded exactly as
|
|
168
|
+
* authored.
|
|
169
|
+
*
|
|
170
|
+
* What "lossy" means for a caller: strict mode has no way to say "optional", so a field the
|
|
171
|
+
* schema marked optional becomes required and gains a `null` branch. The model must then supply
|
|
172
|
+
* the key explicitly, with `null` standing for "not provided". A tool whose handler distinguishes
|
|
173
|
+
* an absent key from a null value will see the difference.
|
|
174
|
+
*
|
|
175
|
+
* Both surfaces declare the functions strict (`strict: true` on each tool), so an OpenAI-compatible
|
|
176
|
+
* endpoint that rejects that field needs `strictTools` left off.
|
|
177
|
+
*/
|
|
178
|
+
strictTools?: boolean;
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Requests the native parallel function-call generation policy: on Chat Completions only with
|
|
182
|
+
* tools, on Responses with every request.
|
|
183
|
+
* Captured at construction; undefined omits the wire field. Does not guarantee call count,
|
|
184
|
+
* argument validity or execution order. Explicit values cannot be used with an executor.
|
|
185
|
+
*/
|
|
186
|
+
readonly parallelToolCalls?: boolean;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* JSON fields merged as top-level fields into every request body on both API surfaces, for
|
|
190
|
+
* endpoint-specific options this provider does not model (for example a gateway's upstream
|
|
191
|
+
* options). Copied at construction; an empty object is the same as omitting it. Fields the
|
|
192
|
+
* provider manages and non-JSON values are rejected, and it cannot be used with an executor.
|
|
193
|
+
*/
|
|
194
|
+
readonly extraBody?: TOpenAIExtraBody;
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Provider-native hosted web tool request from provider profile options.
|
|
198
|
+
*
|
|
199
|
+
* OpenAI-compatible Chat Completions endpoints do not support this ConversationAgent
|
|
200
|
+
* native web contract. The provider rejects unsupported configurations before
|
|
201
|
+
* any model request is sent.
|
|
202
|
+
*/
|
|
203
|
+
nativeWebTools?: IOpenAINativeWebToolsOptions;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* OpenAI client instance (optional: will be created from apiKey if not provided)
|
|
207
|
+
*/
|
|
208
|
+
client?: OpenAI;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Payload logger that receives a summary of each Chat Completions request (model, message
|
|
212
|
+
* count, whether tools were sent, temperature, max tokens) for debugging — not prompt or
|
|
213
|
+
* response content. Not called on the Responses API surface.
|
|
214
|
+
*
|
|
215
|
+
* Use different implementations based on your environment:
|
|
216
|
+
* - FilePayloadLogger: Node.js file-based logging
|
|
217
|
+
* - ConsolePayloadLogger: Browser console-based logging
|
|
218
|
+
* - Custom: Implement IPayloadLogger interface
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```typescript
|
|
222
|
+
* // Node.js
|
|
223
|
+
* import { FilePayloadLogger } from '@robota-sdk/agent-provider-openai/loggers';
|
|
224
|
+
* const provider = new OpenAIProvider({
|
|
225
|
+
* client: openaiClient,
|
|
226
|
+
* payloadLogger: new FilePayloadLogger({ logDir: './logs/openai' })
|
|
227
|
+
* });
|
|
228
|
+
*
|
|
229
|
+
* // Browser
|
|
230
|
+
* import { ConsolePayloadLogger } from '@robota-sdk/agent-provider-openai/loggers';
|
|
231
|
+
* const provider = new OpenAIProvider({
|
|
232
|
+
* client: openaiClient,
|
|
233
|
+
* payloadLogger: new ConsolePayloadLogger()
|
|
234
|
+
* });
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
payloadLogger?: IPayloadLogger;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Optional executor for handling AI requests
|
|
241
|
+
*
|
|
242
|
+
* When provided, the provider delegates every chat call to this executor instead of calling the
|
|
243
|
+
* API itself — for example to route calls through your own server. Implement `IExecutor` from
|
|
244
|
+
* `@robota-sdk/agent-core`; `LocalExecutor` is the in-process one.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```typescript
|
|
248
|
+
* import { LocalExecutor } from '@robota-sdk/agent-core';
|
|
249
|
+
*
|
|
250
|
+
* const executor = new LocalExecutor();
|
|
251
|
+
* executor.registerProvider('openai', new OpenAIProvider({ apiKey: 'sk-...' }));
|
|
252
|
+
*
|
|
253
|
+
* const provider = new OpenAIProvider({ executor });
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
executor?: IExecutor;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Logger instance for internal OpenAI provider logging
|
|
260
|
+
* @defaultValue SilentLogger
|
|
261
|
+
*/
|
|
262
|
+
logger?: ILogger;
|
|
263
|
+
}
|