@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,3 +1,232 @@
|
|
|
1
|
+
// src/adapters/base.ts
|
|
2
|
+
function stringifyForDebug(value) {
|
|
3
|
+
return JSON.stringify(
|
|
4
|
+
value,
|
|
5
|
+
(_key, currentValue) => {
|
|
6
|
+
if (typeof currentValue === "bigint") {
|
|
7
|
+
return currentValue.toString();
|
|
8
|
+
}
|
|
9
|
+
if (currentValue instanceof Error) {
|
|
10
|
+
return {
|
|
11
|
+
name: currentValue.name,
|
|
12
|
+
message: currentValue.message,
|
|
13
|
+
stack: currentValue.stack
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
return currentValue;
|
|
17
|
+
},
|
|
18
|
+
2
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
function logProviderPayload(provider, label, payload, enabled) {
|
|
22
|
+
if (!enabled) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (label.toLowerCase().includes("stream ")) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
console.log(
|
|
30
|
+
`[llm-sdk:${provider}] ${label}
|
|
31
|
+
${stringifyForDebug(payload)}`
|
|
32
|
+
);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.log(
|
|
35
|
+
`[llm-sdk:${provider}] ${label} (failed to stringify payload)`,
|
|
36
|
+
error
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function parameterToJsonSchema(param) {
|
|
41
|
+
const schema = {
|
|
42
|
+
type: param.type
|
|
43
|
+
};
|
|
44
|
+
if (param.description) {
|
|
45
|
+
schema.description = param.description;
|
|
46
|
+
}
|
|
47
|
+
if (param.enum) {
|
|
48
|
+
schema.enum = param.enum;
|
|
49
|
+
}
|
|
50
|
+
if (param.type === "array" && param.items) {
|
|
51
|
+
schema.items = parameterToJsonSchema(
|
|
52
|
+
param.items
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
if (param.type === "object" && param.properties) {
|
|
56
|
+
schema.properties = Object.fromEntries(
|
|
57
|
+
Object.entries(param.properties).map(([key, prop]) => [
|
|
58
|
+
key,
|
|
59
|
+
parameterToJsonSchema(
|
|
60
|
+
prop
|
|
61
|
+
)
|
|
62
|
+
])
|
|
63
|
+
);
|
|
64
|
+
schema.additionalProperties = false;
|
|
65
|
+
}
|
|
66
|
+
return schema;
|
|
67
|
+
}
|
|
68
|
+
function normalizeObjectJsonSchema(schema) {
|
|
69
|
+
if (!schema || typeof schema !== "object") {
|
|
70
|
+
return {
|
|
71
|
+
type: "object",
|
|
72
|
+
properties: {},
|
|
73
|
+
required: [],
|
|
74
|
+
additionalProperties: false
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
const normalized = { ...schema };
|
|
78
|
+
const type = normalized.type;
|
|
79
|
+
if (type === "object") {
|
|
80
|
+
const properties = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) ? normalized.properties : {};
|
|
81
|
+
normalized.properties = Object.fromEntries(
|
|
82
|
+
Object.entries(properties).map(([key, value]) => [
|
|
83
|
+
key,
|
|
84
|
+
normalizeObjectJsonSchema(value)
|
|
85
|
+
])
|
|
86
|
+
);
|
|
87
|
+
const propertyKeys = Object.keys(properties);
|
|
88
|
+
const required = Array.isArray(normalized.required) ? normalized.required.filter(
|
|
89
|
+
(value) => typeof value === "string"
|
|
90
|
+
) : [];
|
|
91
|
+
normalized.required = Array.from(/* @__PURE__ */ new Set([...required, ...propertyKeys]));
|
|
92
|
+
if (normalized.additionalProperties === void 0) {
|
|
93
|
+
normalized.additionalProperties = false;
|
|
94
|
+
}
|
|
95
|
+
} else if (type === "array" && normalized.items && typeof normalized.items === "object") {
|
|
96
|
+
normalized.items = normalizeObjectJsonSchema(
|
|
97
|
+
normalized.items
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return normalized;
|
|
101
|
+
}
|
|
102
|
+
function isOpenAIReasoningModel(modelId) {
|
|
103
|
+
if (!modelId) return false;
|
|
104
|
+
return /^(o1|o3|o4|gpt-5)/i.test(modelId);
|
|
105
|
+
}
|
|
106
|
+
function buildOpenAITokenParams(modelId, maxTokens, temperature) {
|
|
107
|
+
if (isOpenAIReasoningModel(modelId)) {
|
|
108
|
+
return { max_completion_tokens: maxTokens };
|
|
109
|
+
}
|
|
110
|
+
return { max_tokens: maxTokens, temperature };
|
|
111
|
+
}
|
|
112
|
+
function toOpenAIResponseFormat(rf) {
|
|
113
|
+
if (!rf) return void 0;
|
|
114
|
+
if (rf.type === "json_object") return { type: "json_object" };
|
|
115
|
+
return {
|
|
116
|
+
type: "json_schema",
|
|
117
|
+
json_schema: {
|
|
118
|
+
name: rf.json_schema.name,
|
|
119
|
+
schema: normalizeObjectJsonSchema(rf.json_schema.schema),
|
|
120
|
+
strict: rf.json_schema.strict ?? true
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function toOpenAIResponsesTextFormat(rf) {
|
|
125
|
+
if (!rf || rf.type !== "json_schema") return void 0;
|
|
126
|
+
return {
|
|
127
|
+
type: "json_schema",
|
|
128
|
+
name: rf.json_schema.name,
|
|
129
|
+
schema: normalizeObjectJsonSchema(rf.json_schema.schema),
|
|
130
|
+
strict: rf.json_schema.strict ?? true
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function formatTools(actions) {
|
|
134
|
+
return actions.map((action) => ({
|
|
135
|
+
type: "function",
|
|
136
|
+
function: {
|
|
137
|
+
name: action.name,
|
|
138
|
+
description: action.description,
|
|
139
|
+
parameters: {
|
|
140
|
+
type: "object",
|
|
141
|
+
properties: action.parameters ? Object.fromEntries(
|
|
142
|
+
Object.entries(action.parameters).map(([key, param]) => [
|
|
143
|
+
key,
|
|
144
|
+
parameterToJsonSchema(param)
|
|
145
|
+
])
|
|
146
|
+
) : {},
|
|
147
|
+
required: action.parameters ? Object.entries(action.parameters).filter(([, param]) => param.required).map(([key]) => key) : [],
|
|
148
|
+
additionalProperties: false
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
function hasImageAttachments(message) {
|
|
154
|
+
const attachments = message.metadata?.attachments;
|
|
155
|
+
return attachments?.some((a) => a.type === "image") ?? false;
|
|
156
|
+
}
|
|
157
|
+
function attachmentToOpenAIImage(attachment) {
|
|
158
|
+
if (attachment.type !== "image") return null;
|
|
159
|
+
let imageUrl;
|
|
160
|
+
if (attachment.url) {
|
|
161
|
+
imageUrl = attachment.url;
|
|
162
|
+
} else if (attachment.data) {
|
|
163
|
+
imageUrl = attachment.data.startsWith("data:") ? attachment.data : `data:${attachment.mimeType || "image/png"};base64,${attachment.data}`;
|
|
164
|
+
} else {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
return {
|
|
168
|
+
type: "image_url",
|
|
169
|
+
image_url: {
|
|
170
|
+
url: imageUrl,
|
|
171
|
+
detail: "auto"
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
function messageToOpenAIContent(message) {
|
|
176
|
+
const attachments = message.metadata?.attachments;
|
|
177
|
+
const content = message.content ?? "";
|
|
178
|
+
if (!hasImageAttachments(message)) {
|
|
179
|
+
return content;
|
|
180
|
+
}
|
|
181
|
+
const blocks = [];
|
|
182
|
+
if (content) {
|
|
183
|
+
blocks.push({ type: "text", text: content });
|
|
184
|
+
}
|
|
185
|
+
if (attachments) {
|
|
186
|
+
for (const attachment of attachments) {
|
|
187
|
+
const imageBlock = attachmentToOpenAIImage(attachment);
|
|
188
|
+
if (imageBlock) {
|
|
189
|
+
blocks.push(imageBlock);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return blocks;
|
|
194
|
+
}
|
|
195
|
+
function formatMessagesForOpenAI(messages, systemPrompt) {
|
|
196
|
+
const formatted = [];
|
|
197
|
+
if (systemPrompt) {
|
|
198
|
+
formatted.push({ role: "system", content: systemPrompt });
|
|
199
|
+
}
|
|
200
|
+
for (const msg of messages) {
|
|
201
|
+
if (msg.role === "system") {
|
|
202
|
+
formatted.push({ role: "system", content: msg.content ?? "" });
|
|
203
|
+
} else if (msg.role === "user") {
|
|
204
|
+
formatted.push({
|
|
205
|
+
role: "user",
|
|
206
|
+
content: messageToOpenAIContent(msg)
|
|
207
|
+
});
|
|
208
|
+
} else if (msg.role === "assistant") {
|
|
209
|
+
const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0;
|
|
210
|
+
const assistantMsg = {
|
|
211
|
+
role: "assistant",
|
|
212
|
+
// Gemini/xAI (OpenAI-compatible) reject content: "" on assistant messages with tool_calls
|
|
213
|
+
content: hasToolCalls ? msg.content || null : msg.content
|
|
214
|
+
};
|
|
215
|
+
if (hasToolCalls) {
|
|
216
|
+
assistantMsg.tool_calls = msg.tool_calls;
|
|
217
|
+
}
|
|
218
|
+
formatted.push(assistantMsg);
|
|
219
|
+
} else if (msg.role === "tool" && msg.tool_call_id) {
|
|
220
|
+
formatted.push({
|
|
221
|
+
role: "tool",
|
|
222
|
+
content: msg.content ?? "",
|
|
223
|
+
tool_call_id: msg.tool_call_id
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return formatted;
|
|
228
|
+
}
|
|
229
|
+
|
|
1
230
|
// src/providers/xai/provider.ts
|
|
2
231
|
var XAI_MODELS = {
|
|
3
232
|
// Grok 4.1 Fast (Latest - December 2025)
|
|
@@ -51,8 +280,8 @@ function xai(modelId, options = {}) {
|
|
|
51
280
|
supportsVision: modelConfig.vision,
|
|
52
281
|
supportsTools: modelConfig.tools,
|
|
53
282
|
supportsStreaming: true,
|
|
54
|
-
supportsJsonMode:
|
|
55
|
-
//
|
|
283
|
+
supportsJsonMode: true,
|
|
284
|
+
// OpenAI-compatible `response_format`
|
|
56
285
|
supportsThinking: false,
|
|
57
286
|
supportsPDF: false,
|
|
58
287
|
maxTokens: modelConfig.maxTokens,
|
|
@@ -66,7 +295,8 @@ function xai(modelId, options = {}) {
|
|
|
66
295
|
messages,
|
|
67
296
|
tools: params.tools,
|
|
68
297
|
temperature: params.temperature,
|
|
69
|
-
max_tokens: params.maxTokens
|
|
298
|
+
max_tokens: params.maxTokens,
|
|
299
|
+
response_format: toOpenAIResponseFormat(params.responseFormat)
|
|
70
300
|
});
|
|
71
301
|
const choice = response.choices[0];
|
|
72
302
|
const message = choice.message;
|
|
@@ -98,6 +328,7 @@ function xai(modelId, options = {}) {
|
|
|
98
328
|
tools: params.tools,
|
|
99
329
|
temperature: params.temperature,
|
|
100
330
|
max_tokens: params.maxTokens,
|
|
331
|
+
response_format: toOpenAIResponseFormat(params.responseFormat),
|
|
101
332
|
stream: true
|
|
102
333
|
});
|
|
103
334
|
let currentToolCall = null;
|
|
@@ -243,204 +474,6 @@ function generateToolCallId() {
|
|
|
243
474
|
return generateId("call");
|
|
244
475
|
}
|
|
245
476
|
|
|
246
|
-
// src/adapters/base.ts
|
|
247
|
-
function stringifyForDebug(value) {
|
|
248
|
-
return JSON.stringify(
|
|
249
|
-
value,
|
|
250
|
-
(_key, currentValue) => {
|
|
251
|
-
if (typeof currentValue === "bigint") {
|
|
252
|
-
return currentValue.toString();
|
|
253
|
-
}
|
|
254
|
-
if (currentValue instanceof Error) {
|
|
255
|
-
return {
|
|
256
|
-
name: currentValue.name,
|
|
257
|
-
message: currentValue.message,
|
|
258
|
-
stack: currentValue.stack
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
|
-
return currentValue;
|
|
262
|
-
},
|
|
263
|
-
2
|
|
264
|
-
);
|
|
265
|
-
}
|
|
266
|
-
function logProviderPayload(provider, label, payload, enabled) {
|
|
267
|
-
if (!enabled) {
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
if (label.toLowerCase().includes("stream ")) {
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
try {
|
|
274
|
-
console.log(
|
|
275
|
-
`[llm-sdk:${provider}] ${label}
|
|
276
|
-
${stringifyForDebug(payload)}`
|
|
277
|
-
);
|
|
278
|
-
} catch (error) {
|
|
279
|
-
console.log(
|
|
280
|
-
`[llm-sdk:${provider}] ${label} (failed to stringify payload)`,
|
|
281
|
-
error
|
|
282
|
-
);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
function parameterToJsonSchema(param) {
|
|
286
|
-
const schema = {
|
|
287
|
-
type: param.type
|
|
288
|
-
};
|
|
289
|
-
if (param.description) {
|
|
290
|
-
schema.description = param.description;
|
|
291
|
-
}
|
|
292
|
-
if (param.enum) {
|
|
293
|
-
schema.enum = param.enum;
|
|
294
|
-
}
|
|
295
|
-
if (param.type === "array" && param.items) {
|
|
296
|
-
schema.items = parameterToJsonSchema(
|
|
297
|
-
param.items
|
|
298
|
-
);
|
|
299
|
-
}
|
|
300
|
-
if (param.type === "object" && param.properties) {
|
|
301
|
-
schema.properties = Object.fromEntries(
|
|
302
|
-
Object.entries(param.properties).map(([key, prop]) => [
|
|
303
|
-
key,
|
|
304
|
-
parameterToJsonSchema(
|
|
305
|
-
prop
|
|
306
|
-
)
|
|
307
|
-
])
|
|
308
|
-
);
|
|
309
|
-
schema.additionalProperties = false;
|
|
310
|
-
}
|
|
311
|
-
return schema;
|
|
312
|
-
}
|
|
313
|
-
function normalizeObjectJsonSchema(schema) {
|
|
314
|
-
if (!schema || typeof schema !== "object") {
|
|
315
|
-
return {
|
|
316
|
-
type: "object",
|
|
317
|
-
properties: {},
|
|
318
|
-
required: [],
|
|
319
|
-
additionalProperties: false
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
const normalized = { ...schema };
|
|
323
|
-
const type = normalized.type;
|
|
324
|
-
if (type === "object") {
|
|
325
|
-
const properties = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) ? normalized.properties : {};
|
|
326
|
-
normalized.properties = Object.fromEntries(
|
|
327
|
-
Object.entries(properties).map(([key, value]) => [
|
|
328
|
-
key,
|
|
329
|
-
normalizeObjectJsonSchema(value)
|
|
330
|
-
])
|
|
331
|
-
);
|
|
332
|
-
const propertyKeys = Object.keys(properties);
|
|
333
|
-
const required = Array.isArray(normalized.required) ? normalized.required.filter(
|
|
334
|
-
(value) => typeof value === "string"
|
|
335
|
-
) : [];
|
|
336
|
-
normalized.required = Array.from(/* @__PURE__ */ new Set([...required, ...propertyKeys]));
|
|
337
|
-
if (normalized.additionalProperties === void 0) {
|
|
338
|
-
normalized.additionalProperties = false;
|
|
339
|
-
}
|
|
340
|
-
} else if (type === "array" && normalized.items && typeof normalized.items === "object") {
|
|
341
|
-
normalized.items = normalizeObjectJsonSchema(
|
|
342
|
-
normalized.items
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
return normalized;
|
|
346
|
-
}
|
|
347
|
-
function formatTools(actions) {
|
|
348
|
-
return actions.map((action) => ({
|
|
349
|
-
type: "function",
|
|
350
|
-
function: {
|
|
351
|
-
name: action.name,
|
|
352
|
-
description: action.description,
|
|
353
|
-
parameters: {
|
|
354
|
-
type: "object",
|
|
355
|
-
properties: action.parameters ? Object.fromEntries(
|
|
356
|
-
Object.entries(action.parameters).map(([key, param]) => [
|
|
357
|
-
key,
|
|
358
|
-
parameterToJsonSchema(param)
|
|
359
|
-
])
|
|
360
|
-
) : {},
|
|
361
|
-
required: action.parameters ? Object.entries(action.parameters).filter(([, param]) => param.required).map(([key]) => key) : [],
|
|
362
|
-
additionalProperties: false
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
}));
|
|
366
|
-
}
|
|
367
|
-
function hasImageAttachments(message) {
|
|
368
|
-
const attachments = message.metadata?.attachments;
|
|
369
|
-
return attachments?.some((a) => a.type === "image") ?? false;
|
|
370
|
-
}
|
|
371
|
-
function attachmentToOpenAIImage(attachment) {
|
|
372
|
-
if (attachment.type !== "image") return null;
|
|
373
|
-
let imageUrl;
|
|
374
|
-
if (attachment.url) {
|
|
375
|
-
imageUrl = attachment.url;
|
|
376
|
-
} else if (attachment.data) {
|
|
377
|
-
imageUrl = attachment.data.startsWith("data:") ? attachment.data : `data:${attachment.mimeType || "image/png"};base64,${attachment.data}`;
|
|
378
|
-
} else {
|
|
379
|
-
return null;
|
|
380
|
-
}
|
|
381
|
-
return {
|
|
382
|
-
type: "image_url",
|
|
383
|
-
image_url: {
|
|
384
|
-
url: imageUrl,
|
|
385
|
-
detail: "auto"
|
|
386
|
-
}
|
|
387
|
-
};
|
|
388
|
-
}
|
|
389
|
-
function messageToOpenAIContent(message) {
|
|
390
|
-
const attachments = message.metadata?.attachments;
|
|
391
|
-
const content = message.content ?? "";
|
|
392
|
-
if (!hasImageAttachments(message)) {
|
|
393
|
-
return content;
|
|
394
|
-
}
|
|
395
|
-
const blocks = [];
|
|
396
|
-
if (content) {
|
|
397
|
-
blocks.push({ type: "text", text: content });
|
|
398
|
-
}
|
|
399
|
-
if (attachments) {
|
|
400
|
-
for (const attachment of attachments) {
|
|
401
|
-
const imageBlock = attachmentToOpenAIImage(attachment);
|
|
402
|
-
if (imageBlock) {
|
|
403
|
-
blocks.push(imageBlock);
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
}
|
|
407
|
-
return blocks;
|
|
408
|
-
}
|
|
409
|
-
function formatMessagesForOpenAI(messages, systemPrompt) {
|
|
410
|
-
const formatted = [];
|
|
411
|
-
if (systemPrompt) {
|
|
412
|
-
formatted.push({ role: "system", content: systemPrompt });
|
|
413
|
-
}
|
|
414
|
-
for (const msg of messages) {
|
|
415
|
-
if (msg.role === "system") {
|
|
416
|
-
formatted.push({ role: "system", content: msg.content ?? "" });
|
|
417
|
-
} else if (msg.role === "user") {
|
|
418
|
-
formatted.push({
|
|
419
|
-
role: "user",
|
|
420
|
-
content: messageToOpenAIContent(msg)
|
|
421
|
-
});
|
|
422
|
-
} else if (msg.role === "assistant") {
|
|
423
|
-
const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0;
|
|
424
|
-
const assistantMsg = {
|
|
425
|
-
role: "assistant",
|
|
426
|
-
// Gemini/xAI (OpenAI-compatible) reject content: "" on assistant messages with tool_calls
|
|
427
|
-
content: hasToolCalls ? msg.content || null : msg.content
|
|
428
|
-
};
|
|
429
|
-
if (hasToolCalls) {
|
|
430
|
-
assistantMsg.tool_calls = msg.tool_calls;
|
|
431
|
-
}
|
|
432
|
-
formatted.push(assistantMsg);
|
|
433
|
-
} else if (msg.role === "tool" && msg.tool_call_id) {
|
|
434
|
-
formatted.push({
|
|
435
|
-
role: "tool",
|
|
436
|
-
content: msg.content ?? "",
|
|
437
|
-
tool_call_id: msg.tool_call_id
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
return formatted;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
477
|
// src/adapters/openai.ts
|
|
445
478
|
var OpenAIAdapter = class _OpenAIAdapter {
|
|
446
479
|
constructor(config) {
|
|
@@ -555,6 +588,9 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
555
588
|
async completeWithResponses(request) {
|
|
556
589
|
const client = await this.getClient();
|
|
557
590
|
const openaiToolOptions = request.providerToolOptions?.openai;
|
|
591
|
+
const responsesTextFormat = toOpenAIResponsesTextFormat(
|
|
592
|
+
request.config?.responseFormat
|
|
593
|
+
);
|
|
558
594
|
const payload = {
|
|
559
595
|
model: request.config?.model || this.model,
|
|
560
596
|
instructions: request.systemPrompt,
|
|
@@ -564,6 +600,7 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
564
600
|
parallel_tool_calls: openaiToolOptions?.parallelToolCalls,
|
|
565
601
|
temperature: request.config?.temperature ?? this.config.temperature,
|
|
566
602
|
max_output_tokens: request.config?.maxTokens ?? this.config.maxTokens,
|
|
603
|
+
...responsesTextFormat ? { text: { format: responsesTextFormat } } : {},
|
|
567
604
|
stream: false
|
|
568
605
|
};
|
|
569
606
|
logProviderPayload("openai", "request payload", payload, request.debug);
|
|
@@ -685,14 +722,19 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
685
722
|
name: openaiToolOptions.toolChoice.name
|
|
686
723
|
}
|
|
687
724
|
} : openaiToolOptions?.toolChoice;
|
|
725
|
+
const modelIdForPayload = request.config?.model || this.model;
|
|
688
726
|
const payload = {
|
|
689
|
-
model:
|
|
727
|
+
model: modelIdForPayload,
|
|
690
728
|
messages,
|
|
691
729
|
tools: tools.length > 0 ? tools : void 0,
|
|
692
730
|
tool_choice: tools.length > 0 ? toolChoice : void 0,
|
|
693
731
|
parallel_tool_calls: tools.length > 0 ? openaiToolOptions?.parallelToolCalls : void 0,
|
|
694
|
-
|
|
695
|
-
|
|
732
|
+
...buildOpenAITokenParams(
|
|
733
|
+
modelIdForPayload,
|
|
734
|
+
request.config?.maxTokens ?? this.config.maxTokens,
|
|
735
|
+
request.config?.temperature ?? this.config.temperature
|
|
736
|
+
),
|
|
737
|
+
response_format: toOpenAIResponseFormat(request.config?.responseFormat),
|
|
696
738
|
stream: true,
|
|
697
739
|
stream_options: { include_usage: true }
|
|
698
740
|
};
|
|
@@ -834,14 +876,19 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
834
876
|
name: openaiToolOptions.toolChoice.name
|
|
835
877
|
}
|
|
836
878
|
} : openaiToolOptions?.toolChoice;
|
|
879
|
+
const modelIdForCompletePayload = request.config?.model || this.model;
|
|
837
880
|
const payload = {
|
|
838
|
-
model:
|
|
881
|
+
model: modelIdForCompletePayload,
|
|
839
882
|
messages,
|
|
840
883
|
tools: tools.length > 0 ? tools : void 0,
|
|
841
884
|
tool_choice: tools.length > 0 ? toolChoice : void 0,
|
|
842
885
|
parallel_tool_calls: tools.length > 0 ? openaiToolOptions?.parallelToolCalls : void 0,
|
|
843
|
-
|
|
844
|
-
|
|
886
|
+
...buildOpenAITokenParams(
|
|
887
|
+
modelIdForCompletePayload,
|
|
888
|
+
request.config?.maxTokens ?? this.config.maxTokens,
|
|
889
|
+
request.config?.temperature ?? this.config.temperature
|
|
890
|
+
),
|
|
891
|
+
response_format: toOpenAIResponseFormat(request.config?.responseFormat),
|
|
845
892
|
stream: false
|
|
846
893
|
};
|
|
847
894
|
logProviderPayload("openai", "request payload", payload, request.debug);
|
|
@@ -1037,7 +1084,8 @@ function createXAI(config = {}) {
|
|
|
1037
1084
|
supportsVideo: false,
|
|
1038
1085
|
maxTokens: model.maxTokens,
|
|
1039
1086
|
supportedImageTypes: model.vision ? ["image/png", "image/jpeg", "image/gif", "image/webp"] : [],
|
|
1040
|
-
|
|
1087
|
+
// xAI accepts OpenAI-compatible `response_format` on grok-2-1212+.
|
|
1088
|
+
supportsJsonMode: true,
|
|
1041
1089
|
supportsSystemMessages: true
|
|
1042
1090
|
};
|
|
1043
1091
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { d as ToolDefinition, Z as UnifiedToolCall, _ as UnifiedToolResult } from './types-BkQCSiIt.js';
|
|
2
|
+
import { L as LLMAdapter } from './base-C58Dsr9p.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Provider Types
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { d as ToolDefinition, Z as UnifiedToolCall, _ as UnifiedToolResult } from './types-BkQCSiIt.mjs';
|
|
2
|
+
import { L as LLMAdapter } from './base-tNgbBaSo.mjs';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Provider Types
|