@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/openai/provider.ts
|
|
2
231
|
var OPENAI_MODELS = {
|
|
3
232
|
// GPT-4o series
|
|
@@ -92,13 +321,17 @@ function openai(modelId, options = {}) {
|
|
|
92
321
|
},
|
|
93
322
|
async doGenerate(params) {
|
|
94
323
|
const client2 = await getClient();
|
|
95
|
-
const messages =
|
|
324
|
+
const messages = formatMessagesForOpenAI2(params.messages);
|
|
96
325
|
const response = await client2.chat.completions.create({
|
|
97
326
|
model: modelId,
|
|
98
327
|
messages,
|
|
99
328
|
tools: params.tools,
|
|
100
|
-
|
|
101
|
-
|
|
329
|
+
...buildOpenAITokenParams(
|
|
330
|
+
modelId,
|
|
331
|
+
params.maxTokens,
|
|
332
|
+
params.temperature
|
|
333
|
+
),
|
|
334
|
+
response_format: toOpenAIResponseFormat(params.responseFormat)
|
|
102
335
|
});
|
|
103
336
|
const choice = response.choices[0];
|
|
104
337
|
const message = choice.message;
|
|
@@ -123,13 +356,17 @@ function openai(modelId, options = {}) {
|
|
|
123
356
|
},
|
|
124
357
|
async *doStream(params) {
|
|
125
358
|
const client2 = await getClient();
|
|
126
|
-
const messages =
|
|
359
|
+
const messages = formatMessagesForOpenAI2(params.messages);
|
|
127
360
|
const stream = await client2.chat.completions.create({
|
|
128
361
|
model: modelId,
|
|
129
362
|
messages,
|
|
130
363
|
tools: params.tools,
|
|
131
|
-
|
|
132
|
-
|
|
364
|
+
...buildOpenAITokenParams(
|
|
365
|
+
modelId,
|
|
366
|
+
params.maxTokens,
|
|
367
|
+
params.temperature
|
|
368
|
+
),
|
|
369
|
+
response_format: toOpenAIResponseFormat(params.responseFormat),
|
|
133
370
|
stream: true
|
|
134
371
|
});
|
|
135
372
|
let currentToolCall = null;
|
|
@@ -213,7 +450,7 @@ function mapFinishReason(reason) {
|
|
|
213
450
|
return "unknown";
|
|
214
451
|
}
|
|
215
452
|
}
|
|
216
|
-
function
|
|
453
|
+
function formatMessagesForOpenAI2(messages) {
|
|
217
454
|
return messages.map((msg) => {
|
|
218
455
|
switch (msg.role) {
|
|
219
456
|
case "system":
|
|
@@ -275,204 +512,6 @@ function generateToolCallId() {
|
|
|
275
512
|
return generateId("call");
|
|
276
513
|
}
|
|
277
514
|
|
|
278
|
-
// src/adapters/base.ts
|
|
279
|
-
function stringifyForDebug(value) {
|
|
280
|
-
return JSON.stringify(
|
|
281
|
-
value,
|
|
282
|
-
(_key, currentValue) => {
|
|
283
|
-
if (typeof currentValue === "bigint") {
|
|
284
|
-
return currentValue.toString();
|
|
285
|
-
}
|
|
286
|
-
if (currentValue instanceof Error) {
|
|
287
|
-
return {
|
|
288
|
-
name: currentValue.name,
|
|
289
|
-
message: currentValue.message,
|
|
290
|
-
stack: currentValue.stack
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
return currentValue;
|
|
294
|
-
},
|
|
295
|
-
2
|
|
296
|
-
);
|
|
297
|
-
}
|
|
298
|
-
function logProviderPayload(provider, label, payload, enabled) {
|
|
299
|
-
if (!enabled) {
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
if (label.toLowerCase().includes("stream ")) {
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
try {
|
|
306
|
-
console.log(
|
|
307
|
-
`[llm-sdk:${provider}] ${label}
|
|
308
|
-
${stringifyForDebug(payload)}`
|
|
309
|
-
);
|
|
310
|
-
} catch (error) {
|
|
311
|
-
console.log(
|
|
312
|
-
`[llm-sdk:${provider}] ${label} (failed to stringify payload)`,
|
|
313
|
-
error
|
|
314
|
-
);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
function parameterToJsonSchema(param) {
|
|
318
|
-
const schema = {
|
|
319
|
-
type: param.type
|
|
320
|
-
};
|
|
321
|
-
if (param.description) {
|
|
322
|
-
schema.description = param.description;
|
|
323
|
-
}
|
|
324
|
-
if (param.enum) {
|
|
325
|
-
schema.enum = param.enum;
|
|
326
|
-
}
|
|
327
|
-
if (param.type === "array" && param.items) {
|
|
328
|
-
schema.items = parameterToJsonSchema(
|
|
329
|
-
param.items
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
if (param.type === "object" && param.properties) {
|
|
333
|
-
schema.properties = Object.fromEntries(
|
|
334
|
-
Object.entries(param.properties).map(([key, prop]) => [
|
|
335
|
-
key,
|
|
336
|
-
parameterToJsonSchema(
|
|
337
|
-
prop
|
|
338
|
-
)
|
|
339
|
-
])
|
|
340
|
-
);
|
|
341
|
-
schema.additionalProperties = false;
|
|
342
|
-
}
|
|
343
|
-
return schema;
|
|
344
|
-
}
|
|
345
|
-
function normalizeObjectJsonSchema(schema) {
|
|
346
|
-
if (!schema || typeof schema !== "object") {
|
|
347
|
-
return {
|
|
348
|
-
type: "object",
|
|
349
|
-
properties: {},
|
|
350
|
-
required: [],
|
|
351
|
-
additionalProperties: false
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
const normalized = { ...schema };
|
|
355
|
-
const type = normalized.type;
|
|
356
|
-
if (type === "object") {
|
|
357
|
-
const properties = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) ? normalized.properties : {};
|
|
358
|
-
normalized.properties = Object.fromEntries(
|
|
359
|
-
Object.entries(properties).map(([key, value]) => [
|
|
360
|
-
key,
|
|
361
|
-
normalizeObjectJsonSchema(value)
|
|
362
|
-
])
|
|
363
|
-
);
|
|
364
|
-
const propertyKeys = Object.keys(properties);
|
|
365
|
-
const required = Array.isArray(normalized.required) ? normalized.required.filter(
|
|
366
|
-
(value) => typeof value === "string"
|
|
367
|
-
) : [];
|
|
368
|
-
normalized.required = Array.from(/* @__PURE__ */ new Set([...required, ...propertyKeys]));
|
|
369
|
-
if (normalized.additionalProperties === void 0) {
|
|
370
|
-
normalized.additionalProperties = false;
|
|
371
|
-
}
|
|
372
|
-
} else if (type === "array" && normalized.items && typeof normalized.items === "object") {
|
|
373
|
-
normalized.items = normalizeObjectJsonSchema(
|
|
374
|
-
normalized.items
|
|
375
|
-
);
|
|
376
|
-
}
|
|
377
|
-
return normalized;
|
|
378
|
-
}
|
|
379
|
-
function formatTools(actions) {
|
|
380
|
-
return actions.map((action) => ({
|
|
381
|
-
type: "function",
|
|
382
|
-
function: {
|
|
383
|
-
name: action.name,
|
|
384
|
-
description: action.description,
|
|
385
|
-
parameters: {
|
|
386
|
-
type: "object",
|
|
387
|
-
properties: action.parameters ? Object.fromEntries(
|
|
388
|
-
Object.entries(action.parameters).map(([key, param]) => [
|
|
389
|
-
key,
|
|
390
|
-
parameterToJsonSchema(param)
|
|
391
|
-
])
|
|
392
|
-
) : {},
|
|
393
|
-
required: action.parameters ? Object.entries(action.parameters).filter(([, param]) => param.required).map(([key]) => key) : [],
|
|
394
|
-
additionalProperties: false
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
}));
|
|
398
|
-
}
|
|
399
|
-
function hasImageAttachments(message) {
|
|
400
|
-
const attachments = message.metadata?.attachments;
|
|
401
|
-
return attachments?.some((a) => a.type === "image") ?? false;
|
|
402
|
-
}
|
|
403
|
-
function attachmentToOpenAIImage(attachment) {
|
|
404
|
-
if (attachment.type !== "image") return null;
|
|
405
|
-
let imageUrl;
|
|
406
|
-
if (attachment.url) {
|
|
407
|
-
imageUrl = attachment.url;
|
|
408
|
-
} else if (attachment.data) {
|
|
409
|
-
imageUrl = attachment.data.startsWith("data:") ? attachment.data : `data:${attachment.mimeType || "image/png"};base64,${attachment.data}`;
|
|
410
|
-
} else {
|
|
411
|
-
return null;
|
|
412
|
-
}
|
|
413
|
-
return {
|
|
414
|
-
type: "image_url",
|
|
415
|
-
image_url: {
|
|
416
|
-
url: imageUrl,
|
|
417
|
-
detail: "auto"
|
|
418
|
-
}
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
|
-
function messageToOpenAIContent(message) {
|
|
422
|
-
const attachments = message.metadata?.attachments;
|
|
423
|
-
const content = message.content ?? "";
|
|
424
|
-
if (!hasImageAttachments(message)) {
|
|
425
|
-
return content;
|
|
426
|
-
}
|
|
427
|
-
const blocks = [];
|
|
428
|
-
if (content) {
|
|
429
|
-
blocks.push({ type: "text", text: content });
|
|
430
|
-
}
|
|
431
|
-
if (attachments) {
|
|
432
|
-
for (const attachment of attachments) {
|
|
433
|
-
const imageBlock = attachmentToOpenAIImage(attachment);
|
|
434
|
-
if (imageBlock) {
|
|
435
|
-
blocks.push(imageBlock);
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
return blocks;
|
|
440
|
-
}
|
|
441
|
-
function formatMessagesForOpenAI2(messages, systemPrompt) {
|
|
442
|
-
const formatted = [];
|
|
443
|
-
if (systemPrompt) {
|
|
444
|
-
formatted.push({ role: "system", content: systemPrompt });
|
|
445
|
-
}
|
|
446
|
-
for (const msg of messages) {
|
|
447
|
-
if (msg.role === "system") {
|
|
448
|
-
formatted.push({ role: "system", content: msg.content ?? "" });
|
|
449
|
-
} else if (msg.role === "user") {
|
|
450
|
-
formatted.push({
|
|
451
|
-
role: "user",
|
|
452
|
-
content: messageToOpenAIContent(msg)
|
|
453
|
-
});
|
|
454
|
-
} else if (msg.role === "assistant") {
|
|
455
|
-
const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0;
|
|
456
|
-
const assistantMsg = {
|
|
457
|
-
role: "assistant",
|
|
458
|
-
// Gemini/xAI (OpenAI-compatible) reject content: "" on assistant messages with tool_calls
|
|
459
|
-
content: hasToolCalls ? msg.content || null : msg.content
|
|
460
|
-
};
|
|
461
|
-
if (hasToolCalls) {
|
|
462
|
-
assistantMsg.tool_calls = msg.tool_calls;
|
|
463
|
-
}
|
|
464
|
-
formatted.push(assistantMsg);
|
|
465
|
-
} else if (msg.role === "tool" && msg.tool_call_id) {
|
|
466
|
-
formatted.push({
|
|
467
|
-
role: "tool",
|
|
468
|
-
content: msg.content ?? "",
|
|
469
|
-
tool_call_id: msg.tool_call_id
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
return formatted;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
515
|
// src/adapters/openai.ts
|
|
477
516
|
var OpenAIAdapter = class _OpenAIAdapter {
|
|
478
517
|
constructor(config) {
|
|
@@ -501,7 +540,7 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
501
540
|
return request.providerToolOptions?.openai?.nativeToolSearch?.enabled === true && request.providerToolOptions.openai.nativeToolSearch.useResponsesApi !== false && Array.isArray(request.toolDefinitions) && request.toolDefinitions.length > 0;
|
|
502
541
|
}
|
|
503
542
|
buildResponsesInput(request) {
|
|
504
|
-
const sourceMessages = request.rawMessages && request.rawMessages.length > 0 ? request.rawMessages :
|
|
543
|
+
const sourceMessages = request.rawMessages && request.rawMessages.length > 0 ? request.rawMessages : formatMessagesForOpenAI(request.messages, void 0);
|
|
505
544
|
const input = [];
|
|
506
545
|
for (const message of sourceMessages) {
|
|
507
546
|
if (message.role === "system") {
|
|
@@ -587,6 +626,9 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
587
626
|
async completeWithResponses(request) {
|
|
588
627
|
const client = await this.getClient();
|
|
589
628
|
const openaiToolOptions = request.providerToolOptions?.openai;
|
|
629
|
+
const responsesTextFormat = toOpenAIResponsesTextFormat(
|
|
630
|
+
request.config?.responseFormat
|
|
631
|
+
);
|
|
590
632
|
const payload = {
|
|
591
633
|
model: request.config?.model || this.model,
|
|
592
634
|
instructions: request.systemPrompt,
|
|
@@ -596,6 +638,7 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
596
638
|
parallel_tool_calls: openaiToolOptions?.parallelToolCalls,
|
|
597
639
|
temperature: request.config?.temperature ?? this.config.temperature,
|
|
598
640
|
max_output_tokens: request.config?.maxTokens ?? this.config.maxTokens,
|
|
641
|
+
...responsesTextFormat ? { text: { format: responsesTextFormat } } : {},
|
|
599
642
|
stream: false
|
|
600
643
|
};
|
|
601
644
|
logProviderPayload("openai", "request payload", payload, request.debug);
|
|
@@ -690,7 +733,7 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
690
733
|
messages = processedMessages;
|
|
691
734
|
}
|
|
692
735
|
} else {
|
|
693
|
-
messages =
|
|
736
|
+
messages = formatMessagesForOpenAI(
|
|
694
737
|
request.messages,
|
|
695
738
|
request.systemPrompt
|
|
696
739
|
);
|
|
@@ -717,14 +760,19 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
717
760
|
name: openaiToolOptions.toolChoice.name
|
|
718
761
|
}
|
|
719
762
|
} : openaiToolOptions?.toolChoice;
|
|
763
|
+
const modelIdForPayload = request.config?.model || this.model;
|
|
720
764
|
const payload = {
|
|
721
|
-
model:
|
|
765
|
+
model: modelIdForPayload,
|
|
722
766
|
messages,
|
|
723
767
|
tools: tools.length > 0 ? tools : void 0,
|
|
724
768
|
tool_choice: tools.length > 0 ? toolChoice : void 0,
|
|
725
769
|
parallel_tool_calls: tools.length > 0 ? openaiToolOptions?.parallelToolCalls : void 0,
|
|
726
|
-
|
|
727
|
-
|
|
770
|
+
...buildOpenAITokenParams(
|
|
771
|
+
modelIdForPayload,
|
|
772
|
+
request.config?.maxTokens ?? this.config.maxTokens,
|
|
773
|
+
request.config?.temperature ?? this.config.temperature
|
|
774
|
+
),
|
|
775
|
+
response_format: toOpenAIResponseFormat(request.config?.responseFormat),
|
|
728
776
|
stream: true,
|
|
729
777
|
stream_options: { include_usage: true }
|
|
730
778
|
};
|
|
@@ -853,7 +901,7 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
853
901
|
messages = sanitized;
|
|
854
902
|
}
|
|
855
903
|
} else {
|
|
856
|
-
messages =
|
|
904
|
+
messages = formatMessagesForOpenAI(
|
|
857
905
|
request.messages,
|
|
858
906
|
request.systemPrompt
|
|
859
907
|
);
|
|
@@ -866,14 +914,19 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
866
914
|
name: openaiToolOptions.toolChoice.name
|
|
867
915
|
}
|
|
868
916
|
} : openaiToolOptions?.toolChoice;
|
|
917
|
+
const modelIdForCompletePayload = request.config?.model || this.model;
|
|
869
918
|
const payload = {
|
|
870
|
-
model:
|
|
919
|
+
model: modelIdForCompletePayload,
|
|
871
920
|
messages,
|
|
872
921
|
tools: tools.length > 0 ? tools : void 0,
|
|
873
922
|
tool_choice: tools.length > 0 ? toolChoice : void 0,
|
|
874
923
|
parallel_tool_calls: tools.length > 0 ? openaiToolOptions?.parallelToolCalls : void 0,
|
|
875
|
-
|
|
876
|
-
|
|
924
|
+
...buildOpenAITokenParams(
|
|
925
|
+
modelIdForCompletePayload,
|
|
926
|
+
request.config?.maxTokens ?? this.config.maxTokens,
|
|
927
|
+
request.config?.temperature ?? this.config.temperature
|
|
928
|
+
),
|
|
929
|
+
response_format: toOpenAIResponseFormat(request.config?.responseFormat),
|
|
877
930
|
stream: false
|
|
878
931
|
};
|
|
879
932
|
logProviderPayload("openai", "request payload", payload, request.debug);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { L as LanguageModel } from '../../types-
|
|
2
|
-
import { A as AIProvider } from '../../types-
|
|
1
|
+
import { L as LanguageModel } from '../../types-BkQCSiIt.mjs';
|
|
2
|
+
import { A as AIProvider } from '../../types-BSSiJW2o.mjs';
|
|
3
3
|
import 'zod';
|
|
4
|
-
import '../../base-
|
|
4
|
+
import '../../base-tNgbBaSo.mjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* OpenRouter Provider - Modern Pattern
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { L as LanguageModel } from '../../types-
|
|
2
|
-
import { A as AIProvider } from '../../types-
|
|
1
|
+
import { L as LanguageModel } from '../../types-BkQCSiIt.js';
|
|
2
|
+
import { A as AIProvider } from '../../types-BQ31QIsA.js';
|
|
3
3
|
import 'zod';
|
|
4
|
-
import '../../base-
|
|
4
|
+
import '../../base-C58Dsr9p.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* OpenRouter Provider - Modern Pattern
|