@yourgpt/llm-sdk 2.1.10-alpha.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 -38
- package/dist/adapters/index.d.ts +4 -38
- package/dist/adapters/index.js +158 -325
- package/dist/adapters/index.mjs +158 -325
- 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 +35 -43
- package/dist/index.mjs +35 -43
- 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 -212
- package/dist/providers/anthropic/index.mjs +271 -212
- 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 +254 -510
- package/dist/providers/google/index.mjs +254 -510
- 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 +269 -529
- package/dist/providers/openai/index.mjs +269 -529
- package/dist/providers/openrouter/index.d.mts +3 -7
- package/dist/providers/openrouter/index.d.ts +3 -7
- package/dist/providers/openrouter/index.js +365 -902
- package/dist/providers/openrouter/index.mjs +365 -902
- package/dist/providers/togetherai/index.d.mts +3 -3
- package/dist/providers/togetherai/index.d.ts +3 -3
- package/dist/providers/togetherai/index.js +259 -509
- package/dist/providers/togetherai/index.mjs +259 -509
- package/dist/providers/xai/index.d.mts +3 -3
- package/dist/providers/xai/index.d.ts +3 -3
- package/dist/providers/xai/index.js +258 -513
- package/dist/providers/xai/index.mjs +258 -513
- package/dist/{types-BNCmlJMs.d.mts → types-B6dhnguR.d.mts} +1 -1
- package/dist/{types-DhktekQ3.d.ts → types-BQ31QIsA.d.ts} +2 -1
- package/dist/{types-CMMQ8s2O.d.mts → types-BSSiJW2o.d.mts} +2 -1
- package/dist/{base-DN1EfKnE.d.mts → types-BkQCSiIt.d.mts} +388 -214
- package/dist/{base-DuUNxtVg.d.ts → types-BkQCSiIt.d.ts} +388 -214
- package/dist/{types-Pj-vpmoT.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-CMvvDo-E.d.mts +0 -428
- package/dist/types-CMvvDo-E.d.ts +0 -428
|
@@ -1,5 +1,243 @@
|
|
|
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 stripSchemaKeys(schema, keysToDrop, options = {}) {
|
|
43
|
+
if (Array.isArray(schema)) {
|
|
44
|
+
return schema.map((item) => stripSchemaKeys(item, keysToDrop, options));
|
|
45
|
+
}
|
|
46
|
+
if (!schema || typeof schema !== "object") return schema;
|
|
47
|
+
const out = {};
|
|
48
|
+
for (const [key, value] of Object.entries(
|
|
49
|
+
schema
|
|
50
|
+
)) {
|
|
51
|
+
if (keysToDrop.has(key)) continue;
|
|
52
|
+
const renamed = options.renameKeys?.[key] ?? key;
|
|
53
|
+
out[renamed] = stripSchemaKeys(value, keysToDrop, options);
|
|
54
|
+
}
|
|
55
|
+
if (options.forceAdditionalPropertiesFalse && out.type === "object") {
|
|
56
|
+
out.additionalProperties = false;
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
var ANTHROPIC_UNSUPPORTED_KEYS = /* @__PURE__ */ new Set([
|
|
61
|
+
"minimum",
|
|
62
|
+
"maximum",
|
|
63
|
+
"exclusiveMinimum",
|
|
64
|
+
"exclusiveMaximum",
|
|
65
|
+
"multipleOf",
|
|
66
|
+
"minLength",
|
|
67
|
+
"maxLength",
|
|
68
|
+
"minItems",
|
|
69
|
+
"maxItems",
|
|
70
|
+
"minProperties",
|
|
71
|
+
"maxProperties",
|
|
72
|
+
"pattern",
|
|
73
|
+
"$schema"
|
|
74
|
+
]);
|
|
75
|
+
function toAnthropicOutputConfig(rf) {
|
|
76
|
+
if (!rf || rf.type !== "json_schema") return void 0;
|
|
77
|
+
const schema = stripSchemaKeys(
|
|
78
|
+
rf.json_schema.schema,
|
|
79
|
+
ANTHROPIC_UNSUPPORTED_KEYS,
|
|
80
|
+
{
|
|
81
|
+
forceAdditionalPropertiesFalse: true,
|
|
82
|
+
renameKeys: { oneOf: "anyOf" }
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
return {
|
|
86
|
+
format: {
|
|
87
|
+
type: "json_schema",
|
|
88
|
+
schema
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function hasMediaAttachments(message) {
|
|
93
|
+
const attachments = message.metadata?.attachments;
|
|
94
|
+
return attachments?.some(
|
|
95
|
+
(a) => a.type === "image" || a.type === "file" && a.mimeType === "application/pdf"
|
|
96
|
+
) ?? false;
|
|
97
|
+
}
|
|
98
|
+
function attachmentToAnthropicImage(attachment) {
|
|
99
|
+
if (attachment.type !== "image") return null;
|
|
100
|
+
if (attachment.url) {
|
|
101
|
+
return {
|
|
102
|
+
type: "image",
|
|
103
|
+
source: {
|
|
104
|
+
type: "url",
|
|
105
|
+
url: attachment.url
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if (!attachment.data) return null;
|
|
110
|
+
let base64Data = attachment.data;
|
|
111
|
+
if (base64Data.startsWith("data:")) {
|
|
112
|
+
const commaIndex = base64Data.indexOf(",");
|
|
113
|
+
if (commaIndex !== -1) {
|
|
114
|
+
base64Data = base64Data.slice(commaIndex + 1);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
type: "image",
|
|
119
|
+
source: {
|
|
120
|
+
type: "base64",
|
|
121
|
+
media_type: attachment.mimeType || "image/png",
|
|
122
|
+
data: base64Data
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
function attachmentToAnthropicDocument(attachment) {
|
|
127
|
+
if (attachment.type !== "file" || attachment.mimeType !== "application/pdf") {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
if (attachment.url) {
|
|
131
|
+
return {
|
|
132
|
+
type: "document",
|
|
133
|
+
source: {
|
|
134
|
+
type: "url",
|
|
135
|
+
url: attachment.url
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (!attachment.data) return null;
|
|
140
|
+
let base64Data = attachment.data;
|
|
141
|
+
if (base64Data.startsWith("data:")) {
|
|
142
|
+
const commaIndex = base64Data.indexOf(",");
|
|
143
|
+
if (commaIndex !== -1) {
|
|
144
|
+
base64Data = base64Data.slice(commaIndex + 1);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
type: "document",
|
|
149
|
+
source: {
|
|
150
|
+
type: "base64",
|
|
151
|
+
media_type: "application/pdf",
|
|
152
|
+
data: base64Data
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function messageToAnthropicContent(message) {
|
|
157
|
+
const attachments = message.metadata?.attachments;
|
|
158
|
+
const content = message.content ?? "";
|
|
159
|
+
if (!hasMediaAttachments(message)) {
|
|
160
|
+
return content;
|
|
161
|
+
}
|
|
162
|
+
const blocks = [];
|
|
163
|
+
if (attachments) {
|
|
164
|
+
for (const attachment of attachments) {
|
|
165
|
+
const imageBlock = attachmentToAnthropicImage(attachment);
|
|
166
|
+
if (imageBlock) {
|
|
167
|
+
blocks.push(imageBlock);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
const docBlock = attachmentToAnthropicDocument(attachment);
|
|
171
|
+
if (docBlock) {
|
|
172
|
+
blocks.push(docBlock);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (content) {
|
|
177
|
+
blocks.push({ type: "text", text: content });
|
|
178
|
+
}
|
|
179
|
+
return blocks;
|
|
180
|
+
}
|
|
181
|
+
function formatMessagesForAnthropic(messages, systemPrompt) {
|
|
182
|
+
const formatted = [];
|
|
183
|
+
for (let i = 0; i < messages.length; i++) {
|
|
184
|
+
const msg = messages[i];
|
|
185
|
+
if (msg.role === "system") continue;
|
|
186
|
+
if (msg.role === "assistant") {
|
|
187
|
+
const content = [];
|
|
188
|
+
if (msg.content) {
|
|
189
|
+
content.push({ type: "text", text: msg.content });
|
|
190
|
+
}
|
|
191
|
+
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
192
|
+
for (const tc of msg.tool_calls) {
|
|
193
|
+
content.push({
|
|
194
|
+
type: "tool_use",
|
|
195
|
+
id: tc.id,
|
|
196
|
+
name: tc.function.name,
|
|
197
|
+
input: JSON.parse(tc.function.arguments)
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
formatted.push({
|
|
202
|
+
role: "assistant",
|
|
203
|
+
content: content.length === 1 && content[0].type === "text" ? content[0].text : content
|
|
204
|
+
});
|
|
205
|
+
} else if (msg.role === "tool" && msg.tool_call_id) {
|
|
206
|
+
const toolResults = [
|
|
207
|
+
{
|
|
208
|
+
type: "tool_result",
|
|
209
|
+
tool_use_id: msg.tool_call_id,
|
|
210
|
+
content: msg.content ?? ""
|
|
211
|
+
}
|
|
212
|
+
];
|
|
213
|
+
while (i + 1 < messages.length && messages[i + 1].role === "tool") {
|
|
214
|
+
i++;
|
|
215
|
+
const nextTool = messages[i];
|
|
216
|
+
if (nextTool.tool_call_id) {
|
|
217
|
+
toolResults.push({
|
|
218
|
+
type: "tool_result",
|
|
219
|
+
tool_use_id: nextTool.tool_call_id,
|
|
220
|
+
content: nextTool.content ?? ""
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
formatted.push({
|
|
225
|
+
role: "user",
|
|
226
|
+
content: toolResults
|
|
227
|
+
});
|
|
228
|
+
} else if (msg.role === "user") {
|
|
229
|
+
formatted.push({
|
|
230
|
+
role: "user",
|
|
231
|
+
content: messageToAnthropicContent(msg)
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
system: "",
|
|
237
|
+
messages: formatted
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
3
241
|
// src/providers/anthropic/provider.ts
|
|
4
242
|
var ANTHROPIC_MODELS = {
|
|
5
243
|
// Claude 4 series
|
|
@@ -8,6 +246,7 @@ var ANTHROPIC_MODELS = {
|
|
|
8
246
|
tools: true,
|
|
9
247
|
thinking: true,
|
|
10
248
|
pdf: true,
|
|
249
|
+
jsonMode: true,
|
|
11
250
|
maxTokens: 2e5
|
|
12
251
|
},
|
|
13
252
|
"claude-opus-4-20250514": {
|
|
@@ -15,6 +254,7 @@ var ANTHROPIC_MODELS = {
|
|
|
15
254
|
tools: true,
|
|
16
255
|
thinking: true,
|
|
17
256
|
pdf: true,
|
|
257
|
+
jsonMode: true,
|
|
18
258
|
maxTokens: 2e5
|
|
19
259
|
},
|
|
20
260
|
// Claude 3.7 series
|
|
@@ -23,6 +263,7 @@ var ANTHROPIC_MODELS = {
|
|
|
23
263
|
tools: true,
|
|
24
264
|
thinking: true,
|
|
25
265
|
pdf: true,
|
|
266
|
+
jsonMode: true,
|
|
26
267
|
maxTokens: 2e5
|
|
27
268
|
},
|
|
28
269
|
"claude-3-7-sonnet-latest": {
|
|
@@ -30,6 +271,7 @@ var ANTHROPIC_MODELS = {
|
|
|
30
271
|
tools: true,
|
|
31
272
|
thinking: true,
|
|
32
273
|
pdf: true,
|
|
274
|
+
jsonMode: true,
|
|
33
275
|
maxTokens: 2e5
|
|
34
276
|
},
|
|
35
277
|
// Claude 3.5 series
|
|
@@ -38,6 +280,7 @@ var ANTHROPIC_MODELS = {
|
|
|
38
280
|
tools: true,
|
|
39
281
|
thinking: false,
|
|
40
282
|
pdf: true,
|
|
283
|
+
jsonMode: true,
|
|
41
284
|
maxTokens: 2e5
|
|
42
285
|
},
|
|
43
286
|
"claude-3-5-sonnet-latest": {
|
|
@@ -45,6 +288,7 @@ var ANTHROPIC_MODELS = {
|
|
|
45
288
|
tools: true,
|
|
46
289
|
thinking: false,
|
|
47
290
|
pdf: true,
|
|
291
|
+
jsonMode: true,
|
|
48
292
|
maxTokens: 2e5
|
|
49
293
|
},
|
|
50
294
|
"claude-3-5-haiku-20241022": {
|
|
@@ -52,6 +296,7 @@ var ANTHROPIC_MODELS = {
|
|
|
52
296
|
tools: true,
|
|
53
297
|
thinking: false,
|
|
54
298
|
pdf: false,
|
|
299
|
+
jsonMode: true,
|
|
55
300
|
maxTokens: 2e5
|
|
56
301
|
},
|
|
57
302
|
"claude-3-5-haiku-latest": {
|
|
@@ -59,6 +304,7 @@ var ANTHROPIC_MODELS = {
|
|
|
59
304
|
tools: true,
|
|
60
305
|
thinking: false,
|
|
61
306
|
pdf: false,
|
|
307
|
+
jsonMode: true,
|
|
62
308
|
maxTokens: 2e5
|
|
63
309
|
},
|
|
64
310
|
// Claude 3 series
|
|
@@ -67,6 +313,7 @@ var ANTHROPIC_MODELS = {
|
|
|
67
313
|
tools: true,
|
|
68
314
|
thinking: false,
|
|
69
315
|
pdf: false,
|
|
316
|
+
jsonMode: false,
|
|
70
317
|
maxTokens: 2e5
|
|
71
318
|
},
|
|
72
319
|
"claude-3-sonnet-20240229": {
|
|
@@ -74,6 +321,7 @@ var ANTHROPIC_MODELS = {
|
|
|
74
321
|
tools: true,
|
|
75
322
|
thinking: false,
|
|
76
323
|
pdf: false,
|
|
324
|
+
jsonMode: false,
|
|
77
325
|
maxTokens: 2e5
|
|
78
326
|
},
|
|
79
327
|
"claude-3-haiku-20240307": {
|
|
@@ -81,6 +329,7 @@ var ANTHROPIC_MODELS = {
|
|
|
81
329
|
tools: true,
|
|
82
330
|
thinking: false,
|
|
83
331
|
pdf: false,
|
|
332
|
+
jsonMode: false,
|
|
84
333
|
maxTokens: 2e5
|
|
85
334
|
}
|
|
86
335
|
};
|
|
@@ -105,7 +354,7 @@ function anthropic(modelId, options = {}) {
|
|
|
105
354
|
supportsVision: modelConfig.vision,
|
|
106
355
|
supportsTools: modelConfig.tools,
|
|
107
356
|
supportsStreaming: true,
|
|
108
|
-
supportsJsonMode:
|
|
357
|
+
supportsJsonMode: modelConfig.jsonMode,
|
|
109
358
|
supportsThinking: modelConfig.thinking,
|
|
110
359
|
supportsPDF: modelConfig.pdf,
|
|
111
360
|
maxTokens: modelConfig.maxTokens,
|
|
@@ -113,7 +362,7 @@ function anthropic(modelId, options = {}) {
|
|
|
113
362
|
},
|
|
114
363
|
async doGenerate(params) {
|
|
115
364
|
const client2 = await getClient();
|
|
116
|
-
const { system, messages } =
|
|
365
|
+
const { system, messages } = formatMessagesForAnthropic2(params.messages);
|
|
117
366
|
const requestOptions = {
|
|
118
367
|
model: modelId,
|
|
119
368
|
max_tokens: params.maxTokens ?? 4096,
|
|
@@ -130,6 +379,10 @@ function anthropic(modelId, options = {}) {
|
|
|
130
379
|
budget_tokens: options.thinking.budgetTokens ?? 1e4
|
|
131
380
|
};
|
|
132
381
|
}
|
|
382
|
+
const outputConfig = toAnthropicOutputConfig(params.responseFormat);
|
|
383
|
+
if (outputConfig) {
|
|
384
|
+
requestOptions.output_config = outputConfig;
|
|
385
|
+
}
|
|
133
386
|
const response = await client2.messages.create(requestOptions);
|
|
134
387
|
let text = "";
|
|
135
388
|
const toolCalls = [];
|
|
@@ -158,7 +411,7 @@ function anthropic(modelId, options = {}) {
|
|
|
158
411
|
},
|
|
159
412
|
async *doStream(params) {
|
|
160
413
|
const client2 = await getClient();
|
|
161
|
-
const { system, messages } =
|
|
414
|
+
const { system, messages } = formatMessagesForAnthropic2(params.messages);
|
|
162
415
|
const requestOptions = {
|
|
163
416
|
model: modelId,
|
|
164
417
|
max_tokens: params.maxTokens ?? 4096,
|
|
@@ -175,6 +428,10 @@ function anthropic(modelId, options = {}) {
|
|
|
175
428
|
budget_tokens: options.thinking.budgetTokens ?? 1e4
|
|
176
429
|
};
|
|
177
430
|
}
|
|
431
|
+
const outputConfig = toAnthropicOutputConfig(params.responseFormat);
|
|
432
|
+
if (outputConfig) {
|
|
433
|
+
requestOptions.output_config = outputConfig;
|
|
434
|
+
}
|
|
178
435
|
const stream = await client2.messages.stream(requestOptions);
|
|
179
436
|
let currentToolUse = null;
|
|
180
437
|
let inputTokens = 0;
|
|
@@ -197,11 +454,6 @@ function anthropic(modelId, options = {}) {
|
|
|
197
454
|
name: event.content_block.name,
|
|
198
455
|
input: ""
|
|
199
456
|
};
|
|
200
|
-
yield {
|
|
201
|
-
type: "tool-call-start",
|
|
202
|
-
toolCallId: event.content_block.id,
|
|
203
|
-
toolName: event.content_block.name
|
|
204
|
-
};
|
|
205
457
|
}
|
|
206
458
|
break;
|
|
207
459
|
case "content_block_delta":
|
|
@@ -209,11 +461,6 @@ function anthropic(modelId, options = {}) {
|
|
|
209
461
|
yield { type: "text-delta", text: event.delta.text };
|
|
210
462
|
} else if (event.delta?.type === "input_json_delta" && currentToolUse) {
|
|
211
463
|
currentToolUse.input += event.delta.partial_json;
|
|
212
|
-
yield {
|
|
213
|
-
type: "tool-call-delta",
|
|
214
|
-
toolCallId: currentToolUse.id,
|
|
215
|
-
argsText: currentToolUse.input
|
|
216
|
-
};
|
|
217
464
|
}
|
|
218
465
|
break;
|
|
219
466
|
case "content_block_stop":
|
|
@@ -263,7 +510,7 @@ function mapFinishReason(reason) {
|
|
|
263
510
|
return "unknown";
|
|
264
511
|
}
|
|
265
512
|
}
|
|
266
|
-
function
|
|
513
|
+
function formatMessagesForAnthropic2(messages) {
|
|
267
514
|
let system = "";
|
|
268
515
|
const formatted = [];
|
|
269
516
|
const pendingToolResults = [];
|
|
@@ -372,194 +619,6 @@ function generateMessageId() {
|
|
|
372
619
|
return generateId("msg");
|
|
373
620
|
}
|
|
374
621
|
|
|
375
|
-
// src/adapters/base.ts
|
|
376
|
-
function stringifyForDebug(value) {
|
|
377
|
-
return JSON.stringify(
|
|
378
|
-
value,
|
|
379
|
-
(_key, currentValue) => {
|
|
380
|
-
if (typeof currentValue === "bigint") {
|
|
381
|
-
return currentValue.toString();
|
|
382
|
-
}
|
|
383
|
-
if (currentValue instanceof Error) {
|
|
384
|
-
return {
|
|
385
|
-
name: currentValue.name,
|
|
386
|
-
message: currentValue.message,
|
|
387
|
-
stack: currentValue.stack
|
|
388
|
-
};
|
|
389
|
-
}
|
|
390
|
-
return currentValue;
|
|
391
|
-
},
|
|
392
|
-
2
|
|
393
|
-
);
|
|
394
|
-
}
|
|
395
|
-
function logProviderPayload(provider, label, payload, enabled) {
|
|
396
|
-
if (!enabled) {
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
if (label.toLowerCase().includes("stream ")) {
|
|
400
|
-
return;
|
|
401
|
-
}
|
|
402
|
-
try {
|
|
403
|
-
console.log(
|
|
404
|
-
`[llm-sdk:${provider}] ${label}
|
|
405
|
-
${stringifyForDebug(payload)}`
|
|
406
|
-
);
|
|
407
|
-
} catch (error) {
|
|
408
|
-
console.log(
|
|
409
|
-
`[llm-sdk:${provider}] ${label} (failed to stringify payload)`,
|
|
410
|
-
error
|
|
411
|
-
);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
function hasMediaAttachments(message) {
|
|
415
|
-
const attachments = message.metadata?.attachments;
|
|
416
|
-
return attachments?.some(
|
|
417
|
-
(a) => a.type === "image" || a.type === "file" && a.mimeType === "application/pdf"
|
|
418
|
-
) ?? false;
|
|
419
|
-
}
|
|
420
|
-
function attachmentToAnthropicImage(attachment) {
|
|
421
|
-
if (attachment.type !== "image") return null;
|
|
422
|
-
if (attachment.url) {
|
|
423
|
-
return {
|
|
424
|
-
type: "image",
|
|
425
|
-
source: {
|
|
426
|
-
type: "url",
|
|
427
|
-
url: attachment.url
|
|
428
|
-
}
|
|
429
|
-
};
|
|
430
|
-
}
|
|
431
|
-
if (!attachment.data) return null;
|
|
432
|
-
let base64Data = attachment.data;
|
|
433
|
-
if (base64Data.startsWith("data:")) {
|
|
434
|
-
const commaIndex = base64Data.indexOf(",");
|
|
435
|
-
if (commaIndex !== -1) {
|
|
436
|
-
base64Data = base64Data.slice(commaIndex + 1);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
return {
|
|
440
|
-
type: "image",
|
|
441
|
-
source: {
|
|
442
|
-
type: "base64",
|
|
443
|
-
media_type: attachment.mimeType || "image/png",
|
|
444
|
-
data: base64Data
|
|
445
|
-
}
|
|
446
|
-
};
|
|
447
|
-
}
|
|
448
|
-
function attachmentToAnthropicDocument(attachment) {
|
|
449
|
-
if (attachment.type !== "file" || attachment.mimeType !== "application/pdf") {
|
|
450
|
-
return null;
|
|
451
|
-
}
|
|
452
|
-
if (attachment.url) {
|
|
453
|
-
return {
|
|
454
|
-
type: "document",
|
|
455
|
-
source: {
|
|
456
|
-
type: "url",
|
|
457
|
-
url: attachment.url
|
|
458
|
-
}
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
if (!attachment.data) return null;
|
|
462
|
-
let base64Data = attachment.data;
|
|
463
|
-
if (base64Data.startsWith("data:")) {
|
|
464
|
-
const commaIndex = base64Data.indexOf(",");
|
|
465
|
-
if (commaIndex !== -1) {
|
|
466
|
-
base64Data = base64Data.slice(commaIndex + 1);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
return {
|
|
470
|
-
type: "document",
|
|
471
|
-
source: {
|
|
472
|
-
type: "base64",
|
|
473
|
-
media_type: "application/pdf",
|
|
474
|
-
data: base64Data
|
|
475
|
-
}
|
|
476
|
-
};
|
|
477
|
-
}
|
|
478
|
-
function messageToAnthropicContent(message) {
|
|
479
|
-
const attachments = message.metadata?.attachments;
|
|
480
|
-
const content = message.content ?? "";
|
|
481
|
-
if (!hasMediaAttachments(message)) {
|
|
482
|
-
return content;
|
|
483
|
-
}
|
|
484
|
-
const blocks = [];
|
|
485
|
-
if (attachments) {
|
|
486
|
-
for (const attachment of attachments) {
|
|
487
|
-
const imageBlock = attachmentToAnthropicImage(attachment);
|
|
488
|
-
if (imageBlock) {
|
|
489
|
-
blocks.push(imageBlock);
|
|
490
|
-
continue;
|
|
491
|
-
}
|
|
492
|
-
const docBlock = attachmentToAnthropicDocument(attachment);
|
|
493
|
-
if (docBlock) {
|
|
494
|
-
blocks.push(docBlock);
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
if (content) {
|
|
499
|
-
blocks.push({ type: "text", text: content });
|
|
500
|
-
}
|
|
501
|
-
return blocks;
|
|
502
|
-
}
|
|
503
|
-
function formatMessagesForAnthropic2(messages, systemPrompt) {
|
|
504
|
-
const formatted = [];
|
|
505
|
-
for (let i = 0; i < messages.length; i++) {
|
|
506
|
-
const msg = messages[i];
|
|
507
|
-
if (msg.role === "system") continue;
|
|
508
|
-
if (msg.role === "assistant") {
|
|
509
|
-
const content = [];
|
|
510
|
-
if (msg.content) {
|
|
511
|
-
content.push({ type: "text", text: msg.content });
|
|
512
|
-
}
|
|
513
|
-
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
514
|
-
for (const tc of msg.tool_calls) {
|
|
515
|
-
content.push({
|
|
516
|
-
type: "tool_use",
|
|
517
|
-
id: tc.id,
|
|
518
|
-
name: tc.function.name,
|
|
519
|
-
input: JSON.parse(tc.function.arguments)
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
formatted.push({
|
|
524
|
-
role: "assistant",
|
|
525
|
-
content: content.length === 1 && content[0].type === "text" ? content[0].text : content
|
|
526
|
-
});
|
|
527
|
-
} else if (msg.role === "tool" && msg.tool_call_id) {
|
|
528
|
-
const toolResults = [
|
|
529
|
-
{
|
|
530
|
-
type: "tool_result",
|
|
531
|
-
tool_use_id: msg.tool_call_id,
|
|
532
|
-
content: msg.content ?? ""
|
|
533
|
-
}
|
|
534
|
-
];
|
|
535
|
-
while (i + 1 < messages.length && messages[i + 1].role === "tool") {
|
|
536
|
-
i++;
|
|
537
|
-
const nextTool = messages[i];
|
|
538
|
-
if (nextTool.tool_call_id) {
|
|
539
|
-
toolResults.push({
|
|
540
|
-
type: "tool_result",
|
|
541
|
-
tool_use_id: nextTool.tool_call_id,
|
|
542
|
-
content: nextTool.content ?? ""
|
|
543
|
-
});
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
formatted.push({
|
|
547
|
-
role: "user",
|
|
548
|
-
content: toolResults
|
|
549
|
-
});
|
|
550
|
-
} else if (msg.role === "user") {
|
|
551
|
-
formatted.push({
|
|
552
|
-
role: "user",
|
|
553
|
-
content: messageToAnthropicContent(msg)
|
|
554
|
-
});
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
return {
|
|
558
|
-
system: "",
|
|
559
|
-
messages: formatted
|
|
560
|
-
};
|
|
561
|
-
}
|
|
562
|
-
|
|
563
622
|
// src/adapters/anthropic.ts
|
|
564
623
|
var AnthropicAdapter = class {
|
|
565
624
|
constructor(config) {
|
|
@@ -782,12 +841,14 @@ var AnthropicAdapter = class {
|
|
|
782
841
|
* Build common request options for both streaming and non-streaming
|
|
783
842
|
*/
|
|
784
843
|
buildRequestOptions(request) {
|
|
785
|
-
const
|
|
844
|
+
const responseFormat = request.config?.responseFormat;
|
|
845
|
+
const jsonObjectSuffix = responseFormat?.type === "json_object" ? "\n\nRespond with a single JSON object and no other text." : "";
|
|
846
|
+
const systemMessage = (request.systemPrompt || "") + jsonObjectSuffix;
|
|
786
847
|
let messages;
|
|
787
848
|
if (request.rawMessages && request.rawMessages.length > 0) {
|
|
788
849
|
messages = this.convertToAnthropicMessages(request.rawMessages);
|
|
789
850
|
} else {
|
|
790
|
-
const formatted =
|
|
851
|
+
const formatted = formatMessagesForAnthropic(request.messages);
|
|
791
852
|
messages = formatted.messages;
|
|
792
853
|
}
|
|
793
854
|
const anthropicNativeSearch = request.providerToolOptions?.anthropic?.nativeToolSearch;
|
|
@@ -863,6 +924,10 @@ var AnthropicAdapter = class {
|
|
|
863
924
|
if (serverToolConfiguration) {
|
|
864
925
|
options.server_tool_configuration = serverToolConfiguration;
|
|
865
926
|
}
|
|
927
|
+
const outputConfig = toAnthropicOutputConfig(responseFormat);
|
|
928
|
+
if (outputConfig) {
|
|
929
|
+
options.output_config = outputConfig;
|
|
930
|
+
}
|
|
866
931
|
if (this.config.thinking?.type === "enabled") {
|
|
867
932
|
options.thinking = {
|
|
868
933
|
type: "enabled",
|
|
@@ -1017,13 +1082,6 @@ var AnthropicAdapter = class {
|
|
|
1017
1082
|
yield { type: "thinking:delta", content: event.delta.thinking };
|
|
1018
1083
|
} else if (event.delta.type === "input_json_delta" && currentToolUse) {
|
|
1019
1084
|
currentToolUse.input += event.delta.partial_json;
|
|
1020
|
-
if (currentToolUse.name !== "web_search") {
|
|
1021
|
-
yield {
|
|
1022
|
-
type: "action:args",
|
|
1023
|
-
id: currentToolUse.id,
|
|
1024
|
-
args: currentToolUse.input
|
|
1025
|
-
};
|
|
1026
|
-
}
|
|
1027
1085
|
}
|
|
1028
1086
|
break;
|
|
1029
1087
|
case "content_block_stop":
|
|
@@ -1198,7 +1256,8 @@ function createAnthropic(config = {}) {
|
|
|
1198
1256
|
"image/gif",
|
|
1199
1257
|
"image/webp"
|
|
1200
1258
|
],
|
|
1201
|
-
|
|
1259
|
+
// Native `output_config.format` — GA on Claude 3.5 and newer.
|
|
1260
|
+
supportsJsonMode: true,
|
|
1202
1261
|
supportsSystemMessages: true
|
|
1203
1262
|
};
|
|
1204
1263
|
};
|