@yourgpt/llm-sdk 2.5.0 → 2.5.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/index.d.mts +4 -4
- package/dist/adapters/index.d.ts +4 -4
- package/dist/adapters/index.js +156 -13
- package/dist/adapters/index.mjs +156 -13
- package/dist/base-C58Dsr9p.d.ts +259 -0
- package/dist/base-tNgbBaSo.d.mts +259 -0
- package/dist/fallback/index.d.mts +4 -4
- package/dist/fallback/index.d.ts +4 -4
- package/dist/index.d.mts +8 -7
- package/dist/index.d.ts +8 -7
- package/dist/index.js +12 -0
- package/dist/index.mjs +12 -0
- package/dist/providers/anthropic/index.d.mts +3 -3
- package/dist/providers/anthropic/index.d.ts +3 -3
- package/dist/providers/anthropic/index.js +271 -195
- package/dist/providers/anthropic/index.mjs +271 -195
- package/dist/providers/azure/index.d.mts +3 -3
- package/dist/providers/azure/index.d.ts +3 -3
- package/dist/providers/azure/index.js +49 -1
- package/dist/providers/azure/index.mjs +49 -1
- package/dist/providers/fireworks/index.d.mts +1 -1
- package/dist/providers/fireworks/index.d.ts +1 -1
- package/dist/providers/fireworks/index.js +56 -0
- package/dist/providers/fireworks/index.mjs +56 -0
- package/dist/providers/google/index.d.mts +3 -3
- package/dist/providers/google/index.d.ts +3 -3
- package/dist/providers/google/index.js +252 -205
- package/dist/providers/google/index.mjs +252 -205
- package/dist/providers/ollama/index.d.mts +4 -4
- package/dist/providers/ollama/index.d.ts +4 -4
- package/dist/providers/ollama/index.js +10 -2
- package/dist/providers/ollama/index.mjs +10 -2
- package/dist/providers/openai/index.d.mts +3 -3
- package/dist/providers/openai/index.d.ts +3 -3
- package/dist/providers/openai/index.js +267 -214
- package/dist/providers/openai/index.mjs +267 -214
- package/dist/providers/openrouter/index.d.mts +3 -3
- package/dist/providers/openrouter/index.d.ts +3 -3
- package/dist/providers/openrouter/index.js +257 -204
- package/dist/providers/openrouter/index.mjs +257 -204
- package/dist/providers/togetherai/index.d.mts +3 -3
- package/dist/providers/togetherai/index.d.ts +3 -3
- package/dist/providers/togetherai/index.js +257 -204
- package/dist/providers/togetherai/index.mjs +257 -204
- package/dist/providers/xai/index.d.mts +3 -3
- package/dist/providers/xai/index.d.ts +3 -3
- package/dist/providers/xai/index.js +256 -208
- package/dist/providers/xai/index.mjs +256 -208
- package/dist/{types-D4YfrQJR.d.mts → types-B6dhnguR.d.mts} +1 -1
- package/dist/{types-DRqxMIjF.d.mts → types-BQ31QIsA.d.ts} +2 -1
- package/dist/{types-BctsnC3g.d.ts → types-BSSiJW2o.d.mts} +2 -1
- package/dist/{base-D-U61JaB.d.mts → types-BkQCSiIt.d.mts} +388 -213
- package/dist/{base-iGi9Va6Z.d.ts → types-BkQCSiIt.d.ts} +388 -213
- package/dist/{types-38yolWJn.d.ts → types-CCxPmkmK.d.ts} +1 -1
- package/dist/yourgpt/index.d.mts +1 -1
- package/dist/yourgpt/index.d.ts +1 -1
- package/package.json +1 -1
- package/dist/types-CR8mi9I0.d.mts +0 -417
- package/dist/types-CR8mi9I0.d.ts +0 -417
|
@@ -1,5 +1,234 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
// src/adapters/base.ts
|
|
4
|
+
function stringifyForDebug(value) {
|
|
5
|
+
return JSON.stringify(
|
|
6
|
+
value,
|
|
7
|
+
(_key, currentValue) => {
|
|
8
|
+
if (typeof currentValue === "bigint") {
|
|
9
|
+
return currentValue.toString();
|
|
10
|
+
}
|
|
11
|
+
if (currentValue instanceof Error) {
|
|
12
|
+
return {
|
|
13
|
+
name: currentValue.name,
|
|
14
|
+
message: currentValue.message,
|
|
15
|
+
stack: currentValue.stack
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
return currentValue;
|
|
19
|
+
},
|
|
20
|
+
2
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
function logProviderPayload(provider, label, payload, enabled) {
|
|
24
|
+
if (!enabled) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (label.toLowerCase().includes("stream ")) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
console.log(
|
|
32
|
+
`[llm-sdk:${provider}] ${label}
|
|
33
|
+
${stringifyForDebug(payload)}`
|
|
34
|
+
);
|
|
35
|
+
} catch (error) {
|
|
36
|
+
console.log(
|
|
37
|
+
`[llm-sdk:${provider}] ${label} (failed to stringify payload)`,
|
|
38
|
+
error
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function parameterToJsonSchema(param) {
|
|
43
|
+
const schema = {
|
|
44
|
+
type: param.type
|
|
45
|
+
};
|
|
46
|
+
if (param.description) {
|
|
47
|
+
schema.description = param.description;
|
|
48
|
+
}
|
|
49
|
+
if (param.enum) {
|
|
50
|
+
schema.enum = param.enum;
|
|
51
|
+
}
|
|
52
|
+
if (param.type === "array" && param.items) {
|
|
53
|
+
schema.items = parameterToJsonSchema(
|
|
54
|
+
param.items
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
if (param.type === "object" && param.properties) {
|
|
58
|
+
schema.properties = Object.fromEntries(
|
|
59
|
+
Object.entries(param.properties).map(([key, prop]) => [
|
|
60
|
+
key,
|
|
61
|
+
parameterToJsonSchema(
|
|
62
|
+
prop
|
|
63
|
+
)
|
|
64
|
+
])
|
|
65
|
+
);
|
|
66
|
+
schema.additionalProperties = false;
|
|
67
|
+
}
|
|
68
|
+
return schema;
|
|
69
|
+
}
|
|
70
|
+
function normalizeObjectJsonSchema(schema) {
|
|
71
|
+
if (!schema || typeof schema !== "object") {
|
|
72
|
+
return {
|
|
73
|
+
type: "object",
|
|
74
|
+
properties: {},
|
|
75
|
+
required: [],
|
|
76
|
+
additionalProperties: false
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const normalized = { ...schema };
|
|
80
|
+
const type = normalized.type;
|
|
81
|
+
if (type === "object") {
|
|
82
|
+
const properties = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) ? normalized.properties : {};
|
|
83
|
+
normalized.properties = Object.fromEntries(
|
|
84
|
+
Object.entries(properties).map(([key, value]) => [
|
|
85
|
+
key,
|
|
86
|
+
normalizeObjectJsonSchema(value)
|
|
87
|
+
])
|
|
88
|
+
);
|
|
89
|
+
const propertyKeys = Object.keys(properties);
|
|
90
|
+
const required = Array.isArray(normalized.required) ? normalized.required.filter(
|
|
91
|
+
(value) => typeof value === "string"
|
|
92
|
+
) : [];
|
|
93
|
+
normalized.required = Array.from(/* @__PURE__ */ new Set([...required, ...propertyKeys]));
|
|
94
|
+
if (normalized.additionalProperties === void 0) {
|
|
95
|
+
normalized.additionalProperties = false;
|
|
96
|
+
}
|
|
97
|
+
} else if (type === "array" && normalized.items && typeof normalized.items === "object") {
|
|
98
|
+
normalized.items = normalizeObjectJsonSchema(
|
|
99
|
+
normalized.items
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return normalized;
|
|
103
|
+
}
|
|
104
|
+
function isOpenAIReasoningModel(modelId) {
|
|
105
|
+
if (!modelId) return false;
|
|
106
|
+
return /^(o1|o3|o4|gpt-5)/i.test(modelId);
|
|
107
|
+
}
|
|
108
|
+
function buildOpenAITokenParams(modelId, maxTokens, temperature) {
|
|
109
|
+
if (isOpenAIReasoningModel(modelId)) {
|
|
110
|
+
return { max_completion_tokens: maxTokens };
|
|
111
|
+
}
|
|
112
|
+
return { max_tokens: maxTokens, temperature };
|
|
113
|
+
}
|
|
114
|
+
function toOpenAIResponseFormat(rf) {
|
|
115
|
+
if (!rf) return void 0;
|
|
116
|
+
if (rf.type === "json_object") return { type: "json_object" };
|
|
117
|
+
return {
|
|
118
|
+
type: "json_schema",
|
|
119
|
+
json_schema: {
|
|
120
|
+
name: rf.json_schema.name,
|
|
121
|
+
schema: normalizeObjectJsonSchema(rf.json_schema.schema),
|
|
122
|
+
strict: rf.json_schema.strict ?? true
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function toOpenAIResponsesTextFormat(rf) {
|
|
127
|
+
if (!rf || rf.type !== "json_schema") return void 0;
|
|
128
|
+
return {
|
|
129
|
+
type: "json_schema",
|
|
130
|
+
name: rf.json_schema.name,
|
|
131
|
+
schema: normalizeObjectJsonSchema(rf.json_schema.schema),
|
|
132
|
+
strict: rf.json_schema.strict ?? true
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
function formatTools(actions) {
|
|
136
|
+
return actions.map((action) => ({
|
|
137
|
+
type: "function",
|
|
138
|
+
function: {
|
|
139
|
+
name: action.name,
|
|
140
|
+
description: action.description,
|
|
141
|
+
parameters: {
|
|
142
|
+
type: "object",
|
|
143
|
+
properties: action.parameters ? Object.fromEntries(
|
|
144
|
+
Object.entries(action.parameters).map(([key, param]) => [
|
|
145
|
+
key,
|
|
146
|
+
parameterToJsonSchema(param)
|
|
147
|
+
])
|
|
148
|
+
) : {},
|
|
149
|
+
required: action.parameters ? Object.entries(action.parameters).filter(([, param]) => param.required).map(([key]) => key) : [],
|
|
150
|
+
additionalProperties: false
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}));
|
|
154
|
+
}
|
|
155
|
+
function hasImageAttachments(message) {
|
|
156
|
+
const attachments = message.metadata?.attachments;
|
|
157
|
+
return attachments?.some((a) => a.type === "image") ?? false;
|
|
158
|
+
}
|
|
159
|
+
function attachmentToOpenAIImage(attachment) {
|
|
160
|
+
if (attachment.type !== "image") return null;
|
|
161
|
+
let imageUrl;
|
|
162
|
+
if (attachment.url) {
|
|
163
|
+
imageUrl = attachment.url;
|
|
164
|
+
} else if (attachment.data) {
|
|
165
|
+
imageUrl = attachment.data.startsWith("data:") ? attachment.data : `data:${attachment.mimeType || "image/png"};base64,${attachment.data}`;
|
|
166
|
+
} else {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
type: "image_url",
|
|
171
|
+
image_url: {
|
|
172
|
+
url: imageUrl,
|
|
173
|
+
detail: "auto"
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function messageToOpenAIContent(message) {
|
|
178
|
+
const attachments = message.metadata?.attachments;
|
|
179
|
+
const content = message.content ?? "";
|
|
180
|
+
if (!hasImageAttachments(message)) {
|
|
181
|
+
return content;
|
|
182
|
+
}
|
|
183
|
+
const blocks = [];
|
|
184
|
+
if (content) {
|
|
185
|
+
blocks.push({ type: "text", text: content });
|
|
186
|
+
}
|
|
187
|
+
if (attachments) {
|
|
188
|
+
for (const attachment of attachments) {
|
|
189
|
+
const imageBlock = attachmentToOpenAIImage(attachment);
|
|
190
|
+
if (imageBlock) {
|
|
191
|
+
blocks.push(imageBlock);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return blocks;
|
|
196
|
+
}
|
|
197
|
+
function formatMessagesForOpenAI(messages, systemPrompt) {
|
|
198
|
+
const formatted = [];
|
|
199
|
+
if (systemPrompt) {
|
|
200
|
+
formatted.push({ role: "system", content: systemPrompt });
|
|
201
|
+
}
|
|
202
|
+
for (const msg of messages) {
|
|
203
|
+
if (msg.role === "system") {
|
|
204
|
+
formatted.push({ role: "system", content: msg.content ?? "" });
|
|
205
|
+
} else if (msg.role === "user") {
|
|
206
|
+
formatted.push({
|
|
207
|
+
role: "user",
|
|
208
|
+
content: messageToOpenAIContent(msg)
|
|
209
|
+
});
|
|
210
|
+
} else if (msg.role === "assistant") {
|
|
211
|
+
const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0;
|
|
212
|
+
const assistantMsg = {
|
|
213
|
+
role: "assistant",
|
|
214
|
+
// Gemini/xAI (OpenAI-compatible) reject content: "" on assistant messages with tool_calls
|
|
215
|
+
content: hasToolCalls ? msg.content || null : msg.content
|
|
216
|
+
};
|
|
217
|
+
if (hasToolCalls) {
|
|
218
|
+
assistantMsg.tool_calls = msg.tool_calls;
|
|
219
|
+
}
|
|
220
|
+
formatted.push(assistantMsg);
|
|
221
|
+
} else if (msg.role === "tool" && msg.tool_call_id) {
|
|
222
|
+
formatted.push({
|
|
223
|
+
role: "tool",
|
|
224
|
+
content: msg.content ?? "",
|
|
225
|
+
tool_call_id: msg.tool_call_id
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return formatted;
|
|
230
|
+
}
|
|
231
|
+
|
|
3
232
|
// src/providers/openai/provider.ts
|
|
4
233
|
var OPENAI_MODELS = {
|
|
5
234
|
// GPT-4o series
|
|
@@ -94,13 +323,17 @@ function openai(modelId, options = {}) {
|
|
|
94
323
|
},
|
|
95
324
|
async doGenerate(params) {
|
|
96
325
|
const client2 = await getClient();
|
|
97
|
-
const messages =
|
|
326
|
+
const messages = formatMessagesForOpenAI2(params.messages);
|
|
98
327
|
const response = await client2.chat.completions.create({
|
|
99
328
|
model: modelId,
|
|
100
329
|
messages,
|
|
101
330
|
tools: params.tools,
|
|
102
|
-
|
|
103
|
-
|
|
331
|
+
...buildOpenAITokenParams(
|
|
332
|
+
modelId,
|
|
333
|
+
params.maxTokens,
|
|
334
|
+
params.temperature
|
|
335
|
+
),
|
|
336
|
+
response_format: toOpenAIResponseFormat(params.responseFormat)
|
|
104
337
|
});
|
|
105
338
|
const choice = response.choices[0];
|
|
106
339
|
const message = choice.message;
|
|
@@ -125,13 +358,17 @@ function openai(modelId, options = {}) {
|
|
|
125
358
|
},
|
|
126
359
|
async *doStream(params) {
|
|
127
360
|
const client2 = await getClient();
|
|
128
|
-
const messages =
|
|
361
|
+
const messages = formatMessagesForOpenAI2(params.messages);
|
|
129
362
|
const stream = await client2.chat.completions.create({
|
|
130
363
|
model: modelId,
|
|
131
364
|
messages,
|
|
132
365
|
tools: params.tools,
|
|
133
|
-
|
|
134
|
-
|
|
366
|
+
...buildOpenAITokenParams(
|
|
367
|
+
modelId,
|
|
368
|
+
params.maxTokens,
|
|
369
|
+
params.temperature
|
|
370
|
+
),
|
|
371
|
+
response_format: toOpenAIResponseFormat(params.responseFormat),
|
|
135
372
|
stream: true
|
|
136
373
|
});
|
|
137
374
|
let currentToolCall = null;
|
|
@@ -215,7 +452,7 @@ function mapFinishReason(reason) {
|
|
|
215
452
|
return "unknown";
|
|
216
453
|
}
|
|
217
454
|
}
|
|
218
|
-
function
|
|
455
|
+
function formatMessagesForOpenAI2(messages) {
|
|
219
456
|
return messages.map((msg) => {
|
|
220
457
|
switch (msg.role) {
|
|
221
458
|
case "system":
|
|
@@ -277,204 +514,6 @@ function generateToolCallId() {
|
|
|
277
514
|
return generateId("call");
|
|
278
515
|
}
|
|
279
516
|
|
|
280
|
-
// src/adapters/base.ts
|
|
281
|
-
function stringifyForDebug(value) {
|
|
282
|
-
return JSON.stringify(
|
|
283
|
-
value,
|
|
284
|
-
(_key, currentValue) => {
|
|
285
|
-
if (typeof currentValue === "bigint") {
|
|
286
|
-
return currentValue.toString();
|
|
287
|
-
}
|
|
288
|
-
if (currentValue instanceof Error) {
|
|
289
|
-
return {
|
|
290
|
-
name: currentValue.name,
|
|
291
|
-
message: currentValue.message,
|
|
292
|
-
stack: currentValue.stack
|
|
293
|
-
};
|
|
294
|
-
}
|
|
295
|
-
return currentValue;
|
|
296
|
-
},
|
|
297
|
-
2
|
|
298
|
-
);
|
|
299
|
-
}
|
|
300
|
-
function logProviderPayload(provider, label, payload, enabled) {
|
|
301
|
-
if (!enabled) {
|
|
302
|
-
return;
|
|
303
|
-
}
|
|
304
|
-
if (label.toLowerCase().includes("stream ")) {
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
try {
|
|
308
|
-
console.log(
|
|
309
|
-
`[llm-sdk:${provider}] ${label}
|
|
310
|
-
${stringifyForDebug(payload)}`
|
|
311
|
-
);
|
|
312
|
-
} catch (error) {
|
|
313
|
-
console.log(
|
|
314
|
-
`[llm-sdk:${provider}] ${label} (failed to stringify payload)`,
|
|
315
|
-
error
|
|
316
|
-
);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
function parameterToJsonSchema(param) {
|
|
320
|
-
const schema = {
|
|
321
|
-
type: param.type
|
|
322
|
-
};
|
|
323
|
-
if (param.description) {
|
|
324
|
-
schema.description = param.description;
|
|
325
|
-
}
|
|
326
|
-
if (param.enum) {
|
|
327
|
-
schema.enum = param.enum;
|
|
328
|
-
}
|
|
329
|
-
if (param.type === "array" && param.items) {
|
|
330
|
-
schema.items = parameterToJsonSchema(
|
|
331
|
-
param.items
|
|
332
|
-
);
|
|
333
|
-
}
|
|
334
|
-
if (param.type === "object" && param.properties) {
|
|
335
|
-
schema.properties = Object.fromEntries(
|
|
336
|
-
Object.entries(param.properties).map(([key, prop]) => [
|
|
337
|
-
key,
|
|
338
|
-
parameterToJsonSchema(
|
|
339
|
-
prop
|
|
340
|
-
)
|
|
341
|
-
])
|
|
342
|
-
);
|
|
343
|
-
schema.additionalProperties = false;
|
|
344
|
-
}
|
|
345
|
-
return schema;
|
|
346
|
-
}
|
|
347
|
-
function normalizeObjectJsonSchema(schema) {
|
|
348
|
-
if (!schema || typeof schema !== "object") {
|
|
349
|
-
return {
|
|
350
|
-
type: "object",
|
|
351
|
-
properties: {},
|
|
352
|
-
required: [],
|
|
353
|
-
additionalProperties: false
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
const normalized = { ...schema };
|
|
357
|
-
const type = normalized.type;
|
|
358
|
-
if (type === "object") {
|
|
359
|
-
const properties = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) ? normalized.properties : {};
|
|
360
|
-
normalized.properties = Object.fromEntries(
|
|
361
|
-
Object.entries(properties).map(([key, value]) => [
|
|
362
|
-
key,
|
|
363
|
-
normalizeObjectJsonSchema(value)
|
|
364
|
-
])
|
|
365
|
-
);
|
|
366
|
-
const propertyKeys = Object.keys(properties);
|
|
367
|
-
const required = Array.isArray(normalized.required) ? normalized.required.filter(
|
|
368
|
-
(value) => typeof value === "string"
|
|
369
|
-
) : [];
|
|
370
|
-
normalized.required = Array.from(/* @__PURE__ */ new Set([...required, ...propertyKeys]));
|
|
371
|
-
if (normalized.additionalProperties === void 0) {
|
|
372
|
-
normalized.additionalProperties = false;
|
|
373
|
-
}
|
|
374
|
-
} else if (type === "array" && normalized.items && typeof normalized.items === "object") {
|
|
375
|
-
normalized.items = normalizeObjectJsonSchema(
|
|
376
|
-
normalized.items
|
|
377
|
-
);
|
|
378
|
-
}
|
|
379
|
-
return normalized;
|
|
380
|
-
}
|
|
381
|
-
function formatTools(actions) {
|
|
382
|
-
return actions.map((action) => ({
|
|
383
|
-
type: "function",
|
|
384
|
-
function: {
|
|
385
|
-
name: action.name,
|
|
386
|
-
description: action.description,
|
|
387
|
-
parameters: {
|
|
388
|
-
type: "object",
|
|
389
|
-
properties: action.parameters ? Object.fromEntries(
|
|
390
|
-
Object.entries(action.parameters).map(([key, param]) => [
|
|
391
|
-
key,
|
|
392
|
-
parameterToJsonSchema(param)
|
|
393
|
-
])
|
|
394
|
-
) : {},
|
|
395
|
-
required: action.parameters ? Object.entries(action.parameters).filter(([, param]) => param.required).map(([key]) => key) : [],
|
|
396
|
-
additionalProperties: false
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
}));
|
|
400
|
-
}
|
|
401
|
-
function hasImageAttachments(message) {
|
|
402
|
-
const attachments = message.metadata?.attachments;
|
|
403
|
-
return attachments?.some((a) => a.type === "image") ?? false;
|
|
404
|
-
}
|
|
405
|
-
function attachmentToOpenAIImage(attachment) {
|
|
406
|
-
if (attachment.type !== "image") return null;
|
|
407
|
-
let imageUrl;
|
|
408
|
-
if (attachment.url) {
|
|
409
|
-
imageUrl = attachment.url;
|
|
410
|
-
} else if (attachment.data) {
|
|
411
|
-
imageUrl = attachment.data.startsWith("data:") ? attachment.data : `data:${attachment.mimeType || "image/png"};base64,${attachment.data}`;
|
|
412
|
-
} else {
|
|
413
|
-
return null;
|
|
414
|
-
}
|
|
415
|
-
return {
|
|
416
|
-
type: "image_url",
|
|
417
|
-
image_url: {
|
|
418
|
-
url: imageUrl,
|
|
419
|
-
detail: "auto"
|
|
420
|
-
}
|
|
421
|
-
};
|
|
422
|
-
}
|
|
423
|
-
function messageToOpenAIContent(message) {
|
|
424
|
-
const attachments = message.metadata?.attachments;
|
|
425
|
-
const content = message.content ?? "";
|
|
426
|
-
if (!hasImageAttachments(message)) {
|
|
427
|
-
return content;
|
|
428
|
-
}
|
|
429
|
-
const blocks = [];
|
|
430
|
-
if (content) {
|
|
431
|
-
blocks.push({ type: "text", text: content });
|
|
432
|
-
}
|
|
433
|
-
if (attachments) {
|
|
434
|
-
for (const attachment of attachments) {
|
|
435
|
-
const imageBlock = attachmentToOpenAIImage(attachment);
|
|
436
|
-
if (imageBlock) {
|
|
437
|
-
blocks.push(imageBlock);
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
return blocks;
|
|
442
|
-
}
|
|
443
|
-
function formatMessagesForOpenAI2(messages, systemPrompt) {
|
|
444
|
-
const formatted = [];
|
|
445
|
-
if (systemPrompt) {
|
|
446
|
-
formatted.push({ role: "system", content: systemPrompt });
|
|
447
|
-
}
|
|
448
|
-
for (const msg of messages) {
|
|
449
|
-
if (msg.role === "system") {
|
|
450
|
-
formatted.push({ role: "system", content: msg.content ?? "" });
|
|
451
|
-
} else if (msg.role === "user") {
|
|
452
|
-
formatted.push({
|
|
453
|
-
role: "user",
|
|
454
|
-
content: messageToOpenAIContent(msg)
|
|
455
|
-
});
|
|
456
|
-
} else if (msg.role === "assistant") {
|
|
457
|
-
const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0;
|
|
458
|
-
const assistantMsg = {
|
|
459
|
-
role: "assistant",
|
|
460
|
-
// Gemini/xAI (OpenAI-compatible) reject content: "" on assistant messages with tool_calls
|
|
461
|
-
content: hasToolCalls ? msg.content || null : msg.content
|
|
462
|
-
};
|
|
463
|
-
if (hasToolCalls) {
|
|
464
|
-
assistantMsg.tool_calls = msg.tool_calls;
|
|
465
|
-
}
|
|
466
|
-
formatted.push(assistantMsg);
|
|
467
|
-
} else if (msg.role === "tool" && msg.tool_call_id) {
|
|
468
|
-
formatted.push({
|
|
469
|
-
role: "tool",
|
|
470
|
-
content: msg.content ?? "",
|
|
471
|
-
tool_call_id: msg.tool_call_id
|
|
472
|
-
});
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
return formatted;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
517
|
// src/adapters/openai.ts
|
|
479
518
|
var OpenAIAdapter = class _OpenAIAdapter {
|
|
480
519
|
constructor(config) {
|
|
@@ -503,7 +542,7 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
503
542
|
return request.providerToolOptions?.openai?.nativeToolSearch?.enabled === true && request.providerToolOptions.openai.nativeToolSearch.useResponsesApi !== false && Array.isArray(request.toolDefinitions) && request.toolDefinitions.length > 0;
|
|
504
543
|
}
|
|
505
544
|
buildResponsesInput(request) {
|
|
506
|
-
const sourceMessages = request.rawMessages && request.rawMessages.length > 0 ? request.rawMessages :
|
|
545
|
+
const sourceMessages = request.rawMessages && request.rawMessages.length > 0 ? request.rawMessages : formatMessagesForOpenAI(request.messages, void 0);
|
|
507
546
|
const input = [];
|
|
508
547
|
for (const message of sourceMessages) {
|
|
509
548
|
if (message.role === "system") {
|
|
@@ -589,6 +628,9 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
589
628
|
async completeWithResponses(request) {
|
|
590
629
|
const client = await this.getClient();
|
|
591
630
|
const openaiToolOptions = request.providerToolOptions?.openai;
|
|
631
|
+
const responsesTextFormat = toOpenAIResponsesTextFormat(
|
|
632
|
+
request.config?.responseFormat
|
|
633
|
+
);
|
|
592
634
|
const payload = {
|
|
593
635
|
model: request.config?.model || this.model,
|
|
594
636
|
instructions: request.systemPrompt,
|
|
@@ -598,6 +640,7 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
598
640
|
parallel_tool_calls: openaiToolOptions?.parallelToolCalls,
|
|
599
641
|
temperature: request.config?.temperature ?? this.config.temperature,
|
|
600
642
|
max_output_tokens: request.config?.maxTokens ?? this.config.maxTokens,
|
|
643
|
+
...responsesTextFormat ? { text: { format: responsesTextFormat } } : {},
|
|
601
644
|
stream: false
|
|
602
645
|
};
|
|
603
646
|
logProviderPayload("openai", "request payload", payload, request.debug);
|
|
@@ -692,7 +735,7 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
692
735
|
messages = processedMessages;
|
|
693
736
|
}
|
|
694
737
|
} else {
|
|
695
|
-
messages =
|
|
738
|
+
messages = formatMessagesForOpenAI(
|
|
696
739
|
request.messages,
|
|
697
740
|
request.systemPrompt
|
|
698
741
|
);
|
|
@@ -719,14 +762,19 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
719
762
|
name: openaiToolOptions.toolChoice.name
|
|
720
763
|
}
|
|
721
764
|
} : openaiToolOptions?.toolChoice;
|
|
765
|
+
const modelIdForPayload = request.config?.model || this.model;
|
|
722
766
|
const payload = {
|
|
723
|
-
model:
|
|
767
|
+
model: modelIdForPayload,
|
|
724
768
|
messages,
|
|
725
769
|
tools: tools.length > 0 ? tools : void 0,
|
|
726
770
|
tool_choice: tools.length > 0 ? toolChoice : void 0,
|
|
727
771
|
parallel_tool_calls: tools.length > 0 ? openaiToolOptions?.parallelToolCalls : void 0,
|
|
728
|
-
|
|
729
|
-
|
|
772
|
+
...buildOpenAITokenParams(
|
|
773
|
+
modelIdForPayload,
|
|
774
|
+
request.config?.maxTokens ?? this.config.maxTokens,
|
|
775
|
+
request.config?.temperature ?? this.config.temperature
|
|
776
|
+
),
|
|
777
|
+
response_format: toOpenAIResponseFormat(request.config?.responseFormat),
|
|
730
778
|
stream: true,
|
|
731
779
|
stream_options: { include_usage: true }
|
|
732
780
|
};
|
|
@@ -855,7 +903,7 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
855
903
|
messages = sanitized;
|
|
856
904
|
}
|
|
857
905
|
} else {
|
|
858
|
-
messages =
|
|
906
|
+
messages = formatMessagesForOpenAI(
|
|
859
907
|
request.messages,
|
|
860
908
|
request.systemPrompt
|
|
861
909
|
);
|
|
@@ -868,14 +916,19 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
868
916
|
name: openaiToolOptions.toolChoice.name
|
|
869
917
|
}
|
|
870
918
|
} : openaiToolOptions?.toolChoice;
|
|
919
|
+
const modelIdForCompletePayload = request.config?.model || this.model;
|
|
871
920
|
const payload = {
|
|
872
|
-
model:
|
|
921
|
+
model: modelIdForCompletePayload,
|
|
873
922
|
messages,
|
|
874
923
|
tools: tools.length > 0 ? tools : void 0,
|
|
875
924
|
tool_choice: tools.length > 0 ? toolChoice : void 0,
|
|
876
925
|
parallel_tool_calls: tools.length > 0 ? openaiToolOptions?.parallelToolCalls : void 0,
|
|
877
|
-
|
|
878
|
-
|
|
926
|
+
...buildOpenAITokenParams(
|
|
927
|
+
modelIdForCompletePayload,
|
|
928
|
+
request.config?.maxTokens ?? this.config.maxTokens,
|
|
929
|
+
request.config?.temperature ?? this.config.temperature
|
|
930
|
+
),
|
|
931
|
+
response_format: toOpenAIResponseFormat(request.config?.responseFormat),
|
|
879
932
|
stream: false
|
|
880
933
|
};
|
|
881
934
|
logProviderPayload("openai", "request payload", payload, request.debug);
|