@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,241 @@
|
|
|
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 stripSchemaKeys(schema, keysToDrop, options = {}) {
|
|
41
|
+
if (Array.isArray(schema)) {
|
|
42
|
+
return schema.map((item) => stripSchemaKeys(item, keysToDrop, options));
|
|
43
|
+
}
|
|
44
|
+
if (!schema || typeof schema !== "object") return schema;
|
|
45
|
+
const out = {};
|
|
46
|
+
for (const [key, value] of Object.entries(
|
|
47
|
+
schema
|
|
48
|
+
)) {
|
|
49
|
+
if (keysToDrop.has(key)) continue;
|
|
50
|
+
const renamed = options.renameKeys?.[key] ?? key;
|
|
51
|
+
out[renamed] = stripSchemaKeys(value, keysToDrop, options);
|
|
52
|
+
}
|
|
53
|
+
if (options.forceAdditionalPropertiesFalse && out.type === "object") {
|
|
54
|
+
out.additionalProperties = false;
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
var ANTHROPIC_UNSUPPORTED_KEYS = /* @__PURE__ */ new Set([
|
|
59
|
+
"minimum",
|
|
60
|
+
"maximum",
|
|
61
|
+
"exclusiveMinimum",
|
|
62
|
+
"exclusiveMaximum",
|
|
63
|
+
"multipleOf",
|
|
64
|
+
"minLength",
|
|
65
|
+
"maxLength",
|
|
66
|
+
"minItems",
|
|
67
|
+
"maxItems",
|
|
68
|
+
"minProperties",
|
|
69
|
+
"maxProperties",
|
|
70
|
+
"pattern",
|
|
71
|
+
"$schema"
|
|
72
|
+
]);
|
|
73
|
+
function toAnthropicOutputConfig(rf) {
|
|
74
|
+
if (!rf || rf.type !== "json_schema") return void 0;
|
|
75
|
+
const schema = stripSchemaKeys(
|
|
76
|
+
rf.json_schema.schema,
|
|
77
|
+
ANTHROPIC_UNSUPPORTED_KEYS,
|
|
78
|
+
{
|
|
79
|
+
forceAdditionalPropertiesFalse: true,
|
|
80
|
+
renameKeys: { oneOf: "anyOf" }
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
return {
|
|
84
|
+
format: {
|
|
85
|
+
type: "json_schema",
|
|
86
|
+
schema
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function hasMediaAttachments(message) {
|
|
91
|
+
const attachments = message.metadata?.attachments;
|
|
92
|
+
return attachments?.some(
|
|
93
|
+
(a) => a.type === "image" || a.type === "file" && a.mimeType === "application/pdf"
|
|
94
|
+
) ?? false;
|
|
95
|
+
}
|
|
96
|
+
function attachmentToAnthropicImage(attachment) {
|
|
97
|
+
if (attachment.type !== "image") return null;
|
|
98
|
+
if (attachment.url) {
|
|
99
|
+
return {
|
|
100
|
+
type: "image",
|
|
101
|
+
source: {
|
|
102
|
+
type: "url",
|
|
103
|
+
url: attachment.url
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
if (!attachment.data) return null;
|
|
108
|
+
let base64Data = attachment.data;
|
|
109
|
+
if (base64Data.startsWith("data:")) {
|
|
110
|
+
const commaIndex = base64Data.indexOf(",");
|
|
111
|
+
if (commaIndex !== -1) {
|
|
112
|
+
base64Data = base64Data.slice(commaIndex + 1);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
type: "image",
|
|
117
|
+
source: {
|
|
118
|
+
type: "base64",
|
|
119
|
+
media_type: attachment.mimeType || "image/png",
|
|
120
|
+
data: base64Data
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function attachmentToAnthropicDocument(attachment) {
|
|
125
|
+
if (attachment.type !== "file" || attachment.mimeType !== "application/pdf") {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
if (attachment.url) {
|
|
129
|
+
return {
|
|
130
|
+
type: "document",
|
|
131
|
+
source: {
|
|
132
|
+
type: "url",
|
|
133
|
+
url: attachment.url
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
if (!attachment.data) return null;
|
|
138
|
+
let base64Data = attachment.data;
|
|
139
|
+
if (base64Data.startsWith("data:")) {
|
|
140
|
+
const commaIndex = base64Data.indexOf(",");
|
|
141
|
+
if (commaIndex !== -1) {
|
|
142
|
+
base64Data = base64Data.slice(commaIndex + 1);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
type: "document",
|
|
147
|
+
source: {
|
|
148
|
+
type: "base64",
|
|
149
|
+
media_type: "application/pdf",
|
|
150
|
+
data: base64Data
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function messageToAnthropicContent(message) {
|
|
155
|
+
const attachments = message.metadata?.attachments;
|
|
156
|
+
const content = message.content ?? "";
|
|
157
|
+
if (!hasMediaAttachments(message)) {
|
|
158
|
+
return content;
|
|
159
|
+
}
|
|
160
|
+
const blocks = [];
|
|
161
|
+
if (attachments) {
|
|
162
|
+
for (const attachment of attachments) {
|
|
163
|
+
const imageBlock = attachmentToAnthropicImage(attachment);
|
|
164
|
+
if (imageBlock) {
|
|
165
|
+
blocks.push(imageBlock);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const docBlock = attachmentToAnthropicDocument(attachment);
|
|
169
|
+
if (docBlock) {
|
|
170
|
+
blocks.push(docBlock);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (content) {
|
|
175
|
+
blocks.push({ type: "text", text: content });
|
|
176
|
+
}
|
|
177
|
+
return blocks;
|
|
178
|
+
}
|
|
179
|
+
function formatMessagesForAnthropic(messages, systemPrompt) {
|
|
180
|
+
const formatted = [];
|
|
181
|
+
for (let i = 0; i < messages.length; i++) {
|
|
182
|
+
const msg = messages[i];
|
|
183
|
+
if (msg.role === "system") continue;
|
|
184
|
+
if (msg.role === "assistant") {
|
|
185
|
+
const content = [];
|
|
186
|
+
if (msg.content) {
|
|
187
|
+
content.push({ type: "text", text: msg.content });
|
|
188
|
+
}
|
|
189
|
+
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
190
|
+
for (const tc of msg.tool_calls) {
|
|
191
|
+
content.push({
|
|
192
|
+
type: "tool_use",
|
|
193
|
+
id: tc.id,
|
|
194
|
+
name: tc.function.name,
|
|
195
|
+
input: JSON.parse(tc.function.arguments)
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
formatted.push({
|
|
200
|
+
role: "assistant",
|
|
201
|
+
content: content.length === 1 && content[0].type === "text" ? content[0].text : content
|
|
202
|
+
});
|
|
203
|
+
} else if (msg.role === "tool" && msg.tool_call_id) {
|
|
204
|
+
const toolResults = [
|
|
205
|
+
{
|
|
206
|
+
type: "tool_result",
|
|
207
|
+
tool_use_id: msg.tool_call_id,
|
|
208
|
+
content: msg.content ?? ""
|
|
209
|
+
}
|
|
210
|
+
];
|
|
211
|
+
while (i + 1 < messages.length && messages[i + 1].role === "tool") {
|
|
212
|
+
i++;
|
|
213
|
+
const nextTool = messages[i];
|
|
214
|
+
if (nextTool.tool_call_id) {
|
|
215
|
+
toolResults.push({
|
|
216
|
+
type: "tool_result",
|
|
217
|
+
tool_use_id: nextTool.tool_call_id,
|
|
218
|
+
content: nextTool.content ?? ""
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
formatted.push({
|
|
223
|
+
role: "user",
|
|
224
|
+
content: toolResults
|
|
225
|
+
});
|
|
226
|
+
} else if (msg.role === "user") {
|
|
227
|
+
formatted.push({
|
|
228
|
+
role: "user",
|
|
229
|
+
content: messageToAnthropicContent(msg)
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
system: "",
|
|
235
|
+
messages: formatted
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
1
239
|
// src/providers/anthropic/provider.ts
|
|
2
240
|
var ANTHROPIC_MODELS = {
|
|
3
241
|
// Claude 4 series
|
|
@@ -6,6 +244,7 @@ var ANTHROPIC_MODELS = {
|
|
|
6
244
|
tools: true,
|
|
7
245
|
thinking: true,
|
|
8
246
|
pdf: true,
|
|
247
|
+
jsonMode: true,
|
|
9
248
|
maxTokens: 2e5
|
|
10
249
|
},
|
|
11
250
|
"claude-opus-4-20250514": {
|
|
@@ -13,6 +252,7 @@ var ANTHROPIC_MODELS = {
|
|
|
13
252
|
tools: true,
|
|
14
253
|
thinking: true,
|
|
15
254
|
pdf: true,
|
|
255
|
+
jsonMode: true,
|
|
16
256
|
maxTokens: 2e5
|
|
17
257
|
},
|
|
18
258
|
// Claude 3.7 series
|
|
@@ -21,6 +261,7 @@ var ANTHROPIC_MODELS = {
|
|
|
21
261
|
tools: true,
|
|
22
262
|
thinking: true,
|
|
23
263
|
pdf: true,
|
|
264
|
+
jsonMode: true,
|
|
24
265
|
maxTokens: 2e5
|
|
25
266
|
},
|
|
26
267
|
"claude-3-7-sonnet-latest": {
|
|
@@ -28,6 +269,7 @@ var ANTHROPIC_MODELS = {
|
|
|
28
269
|
tools: true,
|
|
29
270
|
thinking: true,
|
|
30
271
|
pdf: true,
|
|
272
|
+
jsonMode: true,
|
|
31
273
|
maxTokens: 2e5
|
|
32
274
|
},
|
|
33
275
|
// Claude 3.5 series
|
|
@@ -36,6 +278,7 @@ var ANTHROPIC_MODELS = {
|
|
|
36
278
|
tools: true,
|
|
37
279
|
thinking: false,
|
|
38
280
|
pdf: true,
|
|
281
|
+
jsonMode: true,
|
|
39
282
|
maxTokens: 2e5
|
|
40
283
|
},
|
|
41
284
|
"claude-3-5-sonnet-latest": {
|
|
@@ -43,6 +286,7 @@ var ANTHROPIC_MODELS = {
|
|
|
43
286
|
tools: true,
|
|
44
287
|
thinking: false,
|
|
45
288
|
pdf: true,
|
|
289
|
+
jsonMode: true,
|
|
46
290
|
maxTokens: 2e5
|
|
47
291
|
},
|
|
48
292
|
"claude-3-5-haiku-20241022": {
|
|
@@ -50,6 +294,7 @@ var ANTHROPIC_MODELS = {
|
|
|
50
294
|
tools: true,
|
|
51
295
|
thinking: false,
|
|
52
296
|
pdf: false,
|
|
297
|
+
jsonMode: true,
|
|
53
298
|
maxTokens: 2e5
|
|
54
299
|
},
|
|
55
300
|
"claude-3-5-haiku-latest": {
|
|
@@ -57,6 +302,7 @@ var ANTHROPIC_MODELS = {
|
|
|
57
302
|
tools: true,
|
|
58
303
|
thinking: false,
|
|
59
304
|
pdf: false,
|
|
305
|
+
jsonMode: true,
|
|
60
306
|
maxTokens: 2e5
|
|
61
307
|
},
|
|
62
308
|
// Claude 3 series
|
|
@@ -65,6 +311,7 @@ var ANTHROPIC_MODELS = {
|
|
|
65
311
|
tools: true,
|
|
66
312
|
thinking: false,
|
|
67
313
|
pdf: false,
|
|
314
|
+
jsonMode: false,
|
|
68
315
|
maxTokens: 2e5
|
|
69
316
|
},
|
|
70
317
|
"claude-3-sonnet-20240229": {
|
|
@@ -72,6 +319,7 @@ var ANTHROPIC_MODELS = {
|
|
|
72
319
|
tools: true,
|
|
73
320
|
thinking: false,
|
|
74
321
|
pdf: false,
|
|
322
|
+
jsonMode: false,
|
|
75
323
|
maxTokens: 2e5
|
|
76
324
|
},
|
|
77
325
|
"claude-3-haiku-20240307": {
|
|
@@ -79,6 +327,7 @@ var ANTHROPIC_MODELS = {
|
|
|
79
327
|
tools: true,
|
|
80
328
|
thinking: false,
|
|
81
329
|
pdf: false,
|
|
330
|
+
jsonMode: false,
|
|
82
331
|
maxTokens: 2e5
|
|
83
332
|
}
|
|
84
333
|
};
|
|
@@ -103,7 +352,7 @@ function anthropic(modelId, options = {}) {
|
|
|
103
352
|
supportsVision: modelConfig.vision,
|
|
104
353
|
supportsTools: modelConfig.tools,
|
|
105
354
|
supportsStreaming: true,
|
|
106
|
-
supportsJsonMode:
|
|
355
|
+
supportsJsonMode: modelConfig.jsonMode,
|
|
107
356
|
supportsThinking: modelConfig.thinking,
|
|
108
357
|
supportsPDF: modelConfig.pdf,
|
|
109
358
|
maxTokens: modelConfig.maxTokens,
|
|
@@ -111,7 +360,7 @@ function anthropic(modelId, options = {}) {
|
|
|
111
360
|
},
|
|
112
361
|
async doGenerate(params) {
|
|
113
362
|
const client2 = await getClient();
|
|
114
|
-
const { system, messages } =
|
|
363
|
+
const { system, messages } = formatMessagesForAnthropic2(params.messages);
|
|
115
364
|
const requestOptions = {
|
|
116
365
|
model: modelId,
|
|
117
366
|
max_tokens: params.maxTokens ?? 4096,
|
|
@@ -128,6 +377,10 @@ function anthropic(modelId, options = {}) {
|
|
|
128
377
|
budget_tokens: options.thinking.budgetTokens ?? 1e4
|
|
129
378
|
};
|
|
130
379
|
}
|
|
380
|
+
const outputConfig = toAnthropicOutputConfig(params.responseFormat);
|
|
381
|
+
if (outputConfig) {
|
|
382
|
+
requestOptions.output_config = outputConfig;
|
|
383
|
+
}
|
|
131
384
|
const response = await client2.messages.create(requestOptions);
|
|
132
385
|
let text = "";
|
|
133
386
|
const toolCalls = [];
|
|
@@ -156,7 +409,7 @@ function anthropic(modelId, options = {}) {
|
|
|
156
409
|
},
|
|
157
410
|
async *doStream(params) {
|
|
158
411
|
const client2 = await getClient();
|
|
159
|
-
const { system, messages } =
|
|
412
|
+
const { system, messages } = formatMessagesForAnthropic2(params.messages);
|
|
160
413
|
const requestOptions = {
|
|
161
414
|
model: modelId,
|
|
162
415
|
max_tokens: params.maxTokens ?? 4096,
|
|
@@ -173,6 +426,10 @@ function anthropic(modelId, options = {}) {
|
|
|
173
426
|
budget_tokens: options.thinking.budgetTokens ?? 1e4
|
|
174
427
|
};
|
|
175
428
|
}
|
|
429
|
+
const outputConfig = toAnthropicOutputConfig(params.responseFormat);
|
|
430
|
+
if (outputConfig) {
|
|
431
|
+
requestOptions.output_config = outputConfig;
|
|
432
|
+
}
|
|
176
433
|
const stream = await client2.messages.stream(requestOptions);
|
|
177
434
|
let currentToolUse = null;
|
|
178
435
|
let inputTokens = 0;
|
|
@@ -251,7 +508,7 @@ function mapFinishReason(reason) {
|
|
|
251
508
|
return "unknown";
|
|
252
509
|
}
|
|
253
510
|
}
|
|
254
|
-
function
|
|
511
|
+
function formatMessagesForAnthropic2(messages) {
|
|
255
512
|
let system = "";
|
|
256
513
|
const formatted = [];
|
|
257
514
|
const pendingToolResults = [];
|
|
@@ -360,194 +617,6 @@ function generateMessageId() {
|
|
|
360
617
|
return generateId("msg");
|
|
361
618
|
}
|
|
362
619
|
|
|
363
|
-
// src/adapters/base.ts
|
|
364
|
-
function stringifyForDebug(value) {
|
|
365
|
-
return JSON.stringify(
|
|
366
|
-
value,
|
|
367
|
-
(_key, currentValue) => {
|
|
368
|
-
if (typeof currentValue === "bigint") {
|
|
369
|
-
return currentValue.toString();
|
|
370
|
-
}
|
|
371
|
-
if (currentValue instanceof Error) {
|
|
372
|
-
return {
|
|
373
|
-
name: currentValue.name,
|
|
374
|
-
message: currentValue.message,
|
|
375
|
-
stack: currentValue.stack
|
|
376
|
-
};
|
|
377
|
-
}
|
|
378
|
-
return currentValue;
|
|
379
|
-
},
|
|
380
|
-
2
|
|
381
|
-
);
|
|
382
|
-
}
|
|
383
|
-
function logProviderPayload(provider, label, payload, enabled) {
|
|
384
|
-
if (!enabled) {
|
|
385
|
-
return;
|
|
386
|
-
}
|
|
387
|
-
if (label.toLowerCase().includes("stream ")) {
|
|
388
|
-
return;
|
|
389
|
-
}
|
|
390
|
-
try {
|
|
391
|
-
console.log(
|
|
392
|
-
`[llm-sdk:${provider}] ${label}
|
|
393
|
-
${stringifyForDebug(payload)}`
|
|
394
|
-
);
|
|
395
|
-
} catch (error) {
|
|
396
|
-
console.log(
|
|
397
|
-
`[llm-sdk:${provider}] ${label} (failed to stringify payload)`,
|
|
398
|
-
error
|
|
399
|
-
);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
function hasMediaAttachments(message) {
|
|
403
|
-
const attachments = message.metadata?.attachments;
|
|
404
|
-
return attachments?.some(
|
|
405
|
-
(a) => a.type === "image" || a.type === "file" && a.mimeType === "application/pdf"
|
|
406
|
-
) ?? false;
|
|
407
|
-
}
|
|
408
|
-
function attachmentToAnthropicImage(attachment) {
|
|
409
|
-
if (attachment.type !== "image") return null;
|
|
410
|
-
if (attachment.url) {
|
|
411
|
-
return {
|
|
412
|
-
type: "image",
|
|
413
|
-
source: {
|
|
414
|
-
type: "url",
|
|
415
|
-
url: attachment.url
|
|
416
|
-
}
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
if (!attachment.data) return null;
|
|
420
|
-
let base64Data = attachment.data;
|
|
421
|
-
if (base64Data.startsWith("data:")) {
|
|
422
|
-
const commaIndex = base64Data.indexOf(",");
|
|
423
|
-
if (commaIndex !== -1) {
|
|
424
|
-
base64Data = base64Data.slice(commaIndex + 1);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
return {
|
|
428
|
-
type: "image",
|
|
429
|
-
source: {
|
|
430
|
-
type: "base64",
|
|
431
|
-
media_type: attachment.mimeType || "image/png",
|
|
432
|
-
data: base64Data
|
|
433
|
-
}
|
|
434
|
-
};
|
|
435
|
-
}
|
|
436
|
-
function attachmentToAnthropicDocument(attachment) {
|
|
437
|
-
if (attachment.type !== "file" || attachment.mimeType !== "application/pdf") {
|
|
438
|
-
return null;
|
|
439
|
-
}
|
|
440
|
-
if (attachment.url) {
|
|
441
|
-
return {
|
|
442
|
-
type: "document",
|
|
443
|
-
source: {
|
|
444
|
-
type: "url",
|
|
445
|
-
url: attachment.url
|
|
446
|
-
}
|
|
447
|
-
};
|
|
448
|
-
}
|
|
449
|
-
if (!attachment.data) return null;
|
|
450
|
-
let base64Data = attachment.data;
|
|
451
|
-
if (base64Data.startsWith("data:")) {
|
|
452
|
-
const commaIndex = base64Data.indexOf(",");
|
|
453
|
-
if (commaIndex !== -1) {
|
|
454
|
-
base64Data = base64Data.slice(commaIndex + 1);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
return {
|
|
458
|
-
type: "document",
|
|
459
|
-
source: {
|
|
460
|
-
type: "base64",
|
|
461
|
-
media_type: "application/pdf",
|
|
462
|
-
data: base64Data
|
|
463
|
-
}
|
|
464
|
-
};
|
|
465
|
-
}
|
|
466
|
-
function messageToAnthropicContent(message) {
|
|
467
|
-
const attachments = message.metadata?.attachments;
|
|
468
|
-
const content = message.content ?? "";
|
|
469
|
-
if (!hasMediaAttachments(message)) {
|
|
470
|
-
return content;
|
|
471
|
-
}
|
|
472
|
-
const blocks = [];
|
|
473
|
-
if (attachments) {
|
|
474
|
-
for (const attachment of attachments) {
|
|
475
|
-
const imageBlock = attachmentToAnthropicImage(attachment);
|
|
476
|
-
if (imageBlock) {
|
|
477
|
-
blocks.push(imageBlock);
|
|
478
|
-
continue;
|
|
479
|
-
}
|
|
480
|
-
const docBlock = attachmentToAnthropicDocument(attachment);
|
|
481
|
-
if (docBlock) {
|
|
482
|
-
blocks.push(docBlock);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
if (content) {
|
|
487
|
-
blocks.push({ type: "text", text: content });
|
|
488
|
-
}
|
|
489
|
-
return blocks;
|
|
490
|
-
}
|
|
491
|
-
function formatMessagesForAnthropic2(messages, systemPrompt) {
|
|
492
|
-
const formatted = [];
|
|
493
|
-
for (let i = 0; i < messages.length; i++) {
|
|
494
|
-
const msg = messages[i];
|
|
495
|
-
if (msg.role === "system") continue;
|
|
496
|
-
if (msg.role === "assistant") {
|
|
497
|
-
const content = [];
|
|
498
|
-
if (msg.content) {
|
|
499
|
-
content.push({ type: "text", text: msg.content });
|
|
500
|
-
}
|
|
501
|
-
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
502
|
-
for (const tc of msg.tool_calls) {
|
|
503
|
-
content.push({
|
|
504
|
-
type: "tool_use",
|
|
505
|
-
id: tc.id,
|
|
506
|
-
name: tc.function.name,
|
|
507
|
-
input: JSON.parse(tc.function.arguments)
|
|
508
|
-
});
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
formatted.push({
|
|
512
|
-
role: "assistant",
|
|
513
|
-
content: content.length === 1 && content[0].type === "text" ? content[0].text : content
|
|
514
|
-
});
|
|
515
|
-
} else if (msg.role === "tool" && msg.tool_call_id) {
|
|
516
|
-
const toolResults = [
|
|
517
|
-
{
|
|
518
|
-
type: "tool_result",
|
|
519
|
-
tool_use_id: msg.tool_call_id,
|
|
520
|
-
content: msg.content ?? ""
|
|
521
|
-
}
|
|
522
|
-
];
|
|
523
|
-
while (i + 1 < messages.length && messages[i + 1].role === "tool") {
|
|
524
|
-
i++;
|
|
525
|
-
const nextTool = messages[i];
|
|
526
|
-
if (nextTool.tool_call_id) {
|
|
527
|
-
toolResults.push({
|
|
528
|
-
type: "tool_result",
|
|
529
|
-
tool_use_id: nextTool.tool_call_id,
|
|
530
|
-
content: nextTool.content ?? ""
|
|
531
|
-
});
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
formatted.push({
|
|
535
|
-
role: "user",
|
|
536
|
-
content: toolResults
|
|
537
|
-
});
|
|
538
|
-
} else if (msg.role === "user") {
|
|
539
|
-
formatted.push({
|
|
540
|
-
role: "user",
|
|
541
|
-
content: messageToAnthropicContent(msg)
|
|
542
|
-
});
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
return {
|
|
546
|
-
system: "",
|
|
547
|
-
messages: formatted
|
|
548
|
-
};
|
|
549
|
-
}
|
|
550
|
-
|
|
551
620
|
// src/adapters/anthropic.ts
|
|
552
621
|
var AnthropicAdapter = class {
|
|
553
622
|
constructor(config) {
|
|
@@ -770,12 +839,14 @@ var AnthropicAdapter = class {
|
|
|
770
839
|
* Build common request options for both streaming and non-streaming
|
|
771
840
|
*/
|
|
772
841
|
buildRequestOptions(request) {
|
|
773
|
-
const
|
|
842
|
+
const responseFormat = request.config?.responseFormat;
|
|
843
|
+
const jsonObjectSuffix = responseFormat?.type === "json_object" ? "\n\nRespond with a single JSON object and no other text." : "";
|
|
844
|
+
const systemMessage = (request.systemPrompt || "") + jsonObjectSuffix;
|
|
774
845
|
let messages;
|
|
775
846
|
if (request.rawMessages && request.rawMessages.length > 0) {
|
|
776
847
|
messages = this.convertToAnthropicMessages(request.rawMessages);
|
|
777
848
|
} else {
|
|
778
|
-
const formatted =
|
|
849
|
+
const formatted = formatMessagesForAnthropic(request.messages);
|
|
779
850
|
messages = formatted.messages;
|
|
780
851
|
}
|
|
781
852
|
const anthropicNativeSearch = request.providerToolOptions?.anthropic?.nativeToolSearch;
|
|
@@ -851,6 +922,10 @@ var AnthropicAdapter = class {
|
|
|
851
922
|
if (serverToolConfiguration) {
|
|
852
923
|
options.server_tool_configuration = serverToolConfiguration;
|
|
853
924
|
}
|
|
925
|
+
const outputConfig = toAnthropicOutputConfig(responseFormat);
|
|
926
|
+
if (outputConfig) {
|
|
927
|
+
options.output_config = outputConfig;
|
|
928
|
+
}
|
|
854
929
|
if (this.config.thinking?.type === "enabled") {
|
|
855
930
|
options.thinking = {
|
|
856
931
|
type: "enabled",
|
|
@@ -1179,7 +1254,8 @@ function createAnthropic(config = {}) {
|
|
|
1179
1254
|
"image/gif",
|
|
1180
1255
|
"image/webp"
|
|
1181
1256
|
],
|
|
1182
|
-
|
|
1257
|
+
// Native `output_config.format` — GA on Claude 3.5 and newer.
|
|
1258
|
+
supportsJsonMode: true,
|
|
1183
1259
|
supportsSystemMessages: true
|
|
1184
1260
|
};
|
|
1185
1261
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { b as AzureProviderConfig, A as AIProvider } from '../../types-
|
|
2
|
-
import '../../
|
|
3
|
-
import '../../types-CR8mi9I0.mjs';
|
|
1
|
+
import { b as AzureProviderConfig, A as AIProvider } from '../../types-BSSiJW2o.mjs';
|
|
2
|
+
import '../../types-BkQCSiIt.mjs';
|
|
4
3
|
import 'zod';
|
|
4
|
+
import '../../base-tNgbBaSo.mjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Azure OpenAI Provider
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { b as AzureProviderConfig, A as AIProvider } from '../../types-
|
|
2
|
-
import '../../
|
|
3
|
-
import '../../types-CR8mi9I0.js';
|
|
1
|
+
import { b as AzureProviderConfig, A as AIProvider } from '../../types-BQ31QIsA.js';
|
|
2
|
+
import '../../types-BkQCSiIt.js';
|
|
4
3
|
import 'zod';
|
|
4
|
+
import '../../base-C58Dsr9p.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Azure OpenAI Provider
|
|
@@ -75,6 +75,52 @@ function parameterToJsonSchema(param) {
|
|
|
75
75
|
}
|
|
76
76
|
return schema;
|
|
77
77
|
}
|
|
78
|
+
function normalizeObjectJsonSchema(schema) {
|
|
79
|
+
if (!schema || typeof schema !== "object") {
|
|
80
|
+
return {
|
|
81
|
+
type: "object",
|
|
82
|
+
properties: {},
|
|
83
|
+
required: [],
|
|
84
|
+
additionalProperties: false
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
const normalized = { ...schema };
|
|
88
|
+
const type = normalized.type;
|
|
89
|
+
if (type === "object") {
|
|
90
|
+
const properties = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) ? normalized.properties : {};
|
|
91
|
+
normalized.properties = Object.fromEntries(
|
|
92
|
+
Object.entries(properties).map(([key, value]) => [
|
|
93
|
+
key,
|
|
94
|
+
normalizeObjectJsonSchema(value)
|
|
95
|
+
])
|
|
96
|
+
);
|
|
97
|
+
const propertyKeys = Object.keys(properties);
|
|
98
|
+
const required = Array.isArray(normalized.required) ? normalized.required.filter(
|
|
99
|
+
(value) => typeof value === "string"
|
|
100
|
+
) : [];
|
|
101
|
+
normalized.required = Array.from(/* @__PURE__ */ new Set([...required, ...propertyKeys]));
|
|
102
|
+
if (normalized.additionalProperties === void 0) {
|
|
103
|
+
normalized.additionalProperties = false;
|
|
104
|
+
}
|
|
105
|
+
} else if (type === "array" && normalized.items && typeof normalized.items === "object") {
|
|
106
|
+
normalized.items = normalizeObjectJsonSchema(
|
|
107
|
+
normalized.items
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
return normalized;
|
|
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
|
+
}
|
|
78
124
|
function formatTools(actions) {
|
|
79
125
|
return actions.map((action) => ({
|
|
80
126
|
type: "function",
|
|
@@ -256,6 +302,7 @@ var AzureAdapter = class {
|
|
|
256
302
|
tools,
|
|
257
303
|
temperature: request.config?.temperature ?? this.config.temperature,
|
|
258
304
|
max_tokens: request.config?.maxTokens ?? this.config.maxTokens,
|
|
305
|
+
response_format: toOpenAIResponseFormat(request.config?.responseFormat),
|
|
259
306
|
stream: true
|
|
260
307
|
};
|
|
261
308
|
logProviderPayload("azure", "request payload", payload, request.debug);
|
|
@@ -355,7 +402,8 @@ var AzureAdapter = class {
|
|
|
355
402
|
messages,
|
|
356
403
|
tools,
|
|
357
404
|
temperature: request.config?.temperature ?? this.config.temperature,
|
|
358
|
-
max_tokens: request.config?.maxTokens ?? this.config.maxTokens
|
|
405
|
+
max_tokens: request.config?.maxTokens ?? this.config.maxTokens,
|
|
406
|
+
response_format: toOpenAIResponseFormat(request.config?.responseFormat)
|
|
359
407
|
};
|
|
360
408
|
logProviderPayload("azure", "request payload", payload, request.debug);
|
|
361
409
|
const response = await client.chat.completions.create(payload);
|