@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,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;
|
|
@@ -195,11 +452,6 @@ function anthropic(modelId, options = {}) {
|
|
|
195
452
|
name: event.content_block.name,
|
|
196
453
|
input: ""
|
|
197
454
|
};
|
|
198
|
-
yield {
|
|
199
|
-
type: "tool-call-start",
|
|
200
|
-
toolCallId: event.content_block.id,
|
|
201
|
-
toolName: event.content_block.name
|
|
202
|
-
};
|
|
203
455
|
}
|
|
204
456
|
break;
|
|
205
457
|
case "content_block_delta":
|
|
@@ -207,11 +459,6 @@ function anthropic(modelId, options = {}) {
|
|
|
207
459
|
yield { type: "text-delta", text: event.delta.text };
|
|
208
460
|
} else if (event.delta?.type === "input_json_delta" && currentToolUse) {
|
|
209
461
|
currentToolUse.input += event.delta.partial_json;
|
|
210
|
-
yield {
|
|
211
|
-
type: "tool-call-delta",
|
|
212
|
-
toolCallId: currentToolUse.id,
|
|
213
|
-
argsText: currentToolUse.input
|
|
214
|
-
};
|
|
215
462
|
}
|
|
216
463
|
break;
|
|
217
464
|
case "content_block_stop":
|
|
@@ -261,7 +508,7 @@ function mapFinishReason(reason) {
|
|
|
261
508
|
return "unknown";
|
|
262
509
|
}
|
|
263
510
|
}
|
|
264
|
-
function
|
|
511
|
+
function formatMessagesForAnthropic2(messages) {
|
|
265
512
|
let system = "";
|
|
266
513
|
const formatted = [];
|
|
267
514
|
const pendingToolResults = [];
|
|
@@ -370,194 +617,6 @@ function generateMessageId() {
|
|
|
370
617
|
return generateId("msg");
|
|
371
618
|
}
|
|
372
619
|
|
|
373
|
-
// src/adapters/base.ts
|
|
374
|
-
function stringifyForDebug(value) {
|
|
375
|
-
return JSON.stringify(
|
|
376
|
-
value,
|
|
377
|
-
(_key, currentValue) => {
|
|
378
|
-
if (typeof currentValue === "bigint") {
|
|
379
|
-
return currentValue.toString();
|
|
380
|
-
}
|
|
381
|
-
if (currentValue instanceof Error) {
|
|
382
|
-
return {
|
|
383
|
-
name: currentValue.name,
|
|
384
|
-
message: currentValue.message,
|
|
385
|
-
stack: currentValue.stack
|
|
386
|
-
};
|
|
387
|
-
}
|
|
388
|
-
return currentValue;
|
|
389
|
-
},
|
|
390
|
-
2
|
|
391
|
-
);
|
|
392
|
-
}
|
|
393
|
-
function logProviderPayload(provider, label, payload, enabled) {
|
|
394
|
-
if (!enabled) {
|
|
395
|
-
return;
|
|
396
|
-
}
|
|
397
|
-
if (label.toLowerCase().includes("stream ")) {
|
|
398
|
-
return;
|
|
399
|
-
}
|
|
400
|
-
try {
|
|
401
|
-
console.log(
|
|
402
|
-
`[llm-sdk:${provider}] ${label}
|
|
403
|
-
${stringifyForDebug(payload)}`
|
|
404
|
-
);
|
|
405
|
-
} catch (error) {
|
|
406
|
-
console.log(
|
|
407
|
-
`[llm-sdk:${provider}] ${label} (failed to stringify payload)`,
|
|
408
|
-
error
|
|
409
|
-
);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
function hasMediaAttachments(message) {
|
|
413
|
-
const attachments = message.metadata?.attachments;
|
|
414
|
-
return attachments?.some(
|
|
415
|
-
(a) => a.type === "image" || a.type === "file" && a.mimeType === "application/pdf"
|
|
416
|
-
) ?? false;
|
|
417
|
-
}
|
|
418
|
-
function attachmentToAnthropicImage(attachment) {
|
|
419
|
-
if (attachment.type !== "image") return null;
|
|
420
|
-
if (attachment.url) {
|
|
421
|
-
return {
|
|
422
|
-
type: "image",
|
|
423
|
-
source: {
|
|
424
|
-
type: "url",
|
|
425
|
-
url: attachment.url
|
|
426
|
-
}
|
|
427
|
-
};
|
|
428
|
-
}
|
|
429
|
-
if (!attachment.data) return null;
|
|
430
|
-
let base64Data = attachment.data;
|
|
431
|
-
if (base64Data.startsWith("data:")) {
|
|
432
|
-
const commaIndex = base64Data.indexOf(",");
|
|
433
|
-
if (commaIndex !== -1) {
|
|
434
|
-
base64Data = base64Data.slice(commaIndex + 1);
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
return {
|
|
438
|
-
type: "image",
|
|
439
|
-
source: {
|
|
440
|
-
type: "base64",
|
|
441
|
-
media_type: attachment.mimeType || "image/png",
|
|
442
|
-
data: base64Data
|
|
443
|
-
}
|
|
444
|
-
};
|
|
445
|
-
}
|
|
446
|
-
function attachmentToAnthropicDocument(attachment) {
|
|
447
|
-
if (attachment.type !== "file" || attachment.mimeType !== "application/pdf") {
|
|
448
|
-
return null;
|
|
449
|
-
}
|
|
450
|
-
if (attachment.url) {
|
|
451
|
-
return {
|
|
452
|
-
type: "document",
|
|
453
|
-
source: {
|
|
454
|
-
type: "url",
|
|
455
|
-
url: attachment.url
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
}
|
|
459
|
-
if (!attachment.data) return null;
|
|
460
|
-
let base64Data = attachment.data;
|
|
461
|
-
if (base64Data.startsWith("data:")) {
|
|
462
|
-
const commaIndex = base64Data.indexOf(",");
|
|
463
|
-
if (commaIndex !== -1) {
|
|
464
|
-
base64Data = base64Data.slice(commaIndex + 1);
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
return {
|
|
468
|
-
type: "document",
|
|
469
|
-
source: {
|
|
470
|
-
type: "base64",
|
|
471
|
-
media_type: "application/pdf",
|
|
472
|
-
data: base64Data
|
|
473
|
-
}
|
|
474
|
-
};
|
|
475
|
-
}
|
|
476
|
-
function messageToAnthropicContent(message) {
|
|
477
|
-
const attachments = message.metadata?.attachments;
|
|
478
|
-
const content = message.content ?? "";
|
|
479
|
-
if (!hasMediaAttachments(message)) {
|
|
480
|
-
return content;
|
|
481
|
-
}
|
|
482
|
-
const blocks = [];
|
|
483
|
-
if (attachments) {
|
|
484
|
-
for (const attachment of attachments) {
|
|
485
|
-
const imageBlock = attachmentToAnthropicImage(attachment);
|
|
486
|
-
if (imageBlock) {
|
|
487
|
-
blocks.push(imageBlock);
|
|
488
|
-
continue;
|
|
489
|
-
}
|
|
490
|
-
const docBlock = attachmentToAnthropicDocument(attachment);
|
|
491
|
-
if (docBlock) {
|
|
492
|
-
blocks.push(docBlock);
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
if (content) {
|
|
497
|
-
blocks.push({ type: "text", text: content });
|
|
498
|
-
}
|
|
499
|
-
return blocks;
|
|
500
|
-
}
|
|
501
|
-
function formatMessagesForAnthropic2(messages, systemPrompt) {
|
|
502
|
-
const formatted = [];
|
|
503
|
-
for (let i = 0; i < messages.length; i++) {
|
|
504
|
-
const msg = messages[i];
|
|
505
|
-
if (msg.role === "system") continue;
|
|
506
|
-
if (msg.role === "assistant") {
|
|
507
|
-
const content = [];
|
|
508
|
-
if (msg.content) {
|
|
509
|
-
content.push({ type: "text", text: msg.content });
|
|
510
|
-
}
|
|
511
|
-
if (msg.tool_calls && msg.tool_calls.length > 0) {
|
|
512
|
-
for (const tc of msg.tool_calls) {
|
|
513
|
-
content.push({
|
|
514
|
-
type: "tool_use",
|
|
515
|
-
id: tc.id,
|
|
516
|
-
name: tc.function.name,
|
|
517
|
-
input: JSON.parse(tc.function.arguments)
|
|
518
|
-
});
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
formatted.push({
|
|
522
|
-
role: "assistant",
|
|
523
|
-
content: content.length === 1 && content[0].type === "text" ? content[0].text : content
|
|
524
|
-
});
|
|
525
|
-
} else if (msg.role === "tool" && msg.tool_call_id) {
|
|
526
|
-
const toolResults = [
|
|
527
|
-
{
|
|
528
|
-
type: "tool_result",
|
|
529
|
-
tool_use_id: msg.tool_call_id,
|
|
530
|
-
content: msg.content ?? ""
|
|
531
|
-
}
|
|
532
|
-
];
|
|
533
|
-
while (i + 1 < messages.length && messages[i + 1].role === "tool") {
|
|
534
|
-
i++;
|
|
535
|
-
const nextTool = messages[i];
|
|
536
|
-
if (nextTool.tool_call_id) {
|
|
537
|
-
toolResults.push({
|
|
538
|
-
type: "tool_result",
|
|
539
|
-
tool_use_id: nextTool.tool_call_id,
|
|
540
|
-
content: nextTool.content ?? ""
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
formatted.push({
|
|
545
|
-
role: "user",
|
|
546
|
-
content: toolResults
|
|
547
|
-
});
|
|
548
|
-
} else if (msg.role === "user") {
|
|
549
|
-
formatted.push({
|
|
550
|
-
role: "user",
|
|
551
|
-
content: messageToAnthropicContent(msg)
|
|
552
|
-
});
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
return {
|
|
556
|
-
system: "",
|
|
557
|
-
messages: formatted
|
|
558
|
-
};
|
|
559
|
-
}
|
|
560
|
-
|
|
561
620
|
// src/adapters/anthropic.ts
|
|
562
621
|
var AnthropicAdapter = class {
|
|
563
622
|
constructor(config) {
|
|
@@ -780,12 +839,14 @@ var AnthropicAdapter = class {
|
|
|
780
839
|
* Build common request options for both streaming and non-streaming
|
|
781
840
|
*/
|
|
782
841
|
buildRequestOptions(request) {
|
|
783
|
-
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;
|
|
784
845
|
let messages;
|
|
785
846
|
if (request.rawMessages && request.rawMessages.length > 0) {
|
|
786
847
|
messages = this.convertToAnthropicMessages(request.rawMessages);
|
|
787
848
|
} else {
|
|
788
|
-
const formatted =
|
|
849
|
+
const formatted = formatMessagesForAnthropic(request.messages);
|
|
789
850
|
messages = formatted.messages;
|
|
790
851
|
}
|
|
791
852
|
const anthropicNativeSearch = request.providerToolOptions?.anthropic?.nativeToolSearch;
|
|
@@ -861,6 +922,10 @@ var AnthropicAdapter = class {
|
|
|
861
922
|
if (serverToolConfiguration) {
|
|
862
923
|
options.server_tool_configuration = serverToolConfiguration;
|
|
863
924
|
}
|
|
925
|
+
const outputConfig = toAnthropicOutputConfig(responseFormat);
|
|
926
|
+
if (outputConfig) {
|
|
927
|
+
options.output_config = outputConfig;
|
|
928
|
+
}
|
|
864
929
|
if (this.config.thinking?.type === "enabled") {
|
|
865
930
|
options.thinking = {
|
|
866
931
|
type: "enabled",
|
|
@@ -1015,13 +1080,6 @@ var AnthropicAdapter = class {
|
|
|
1015
1080
|
yield { type: "thinking:delta", content: event.delta.thinking };
|
|
1016
1081
|
} else if (event.delta.type === "input_json_delta" && currentToolUse) {
|
|
1017
1082
|
currentToolUse.input += event.delta.partial_json;
|
|
1018
|
-
if (currentToolUse.name !== "web_search") {
|
|
1019
|
-
yield {
|
|
1020
|
-
type: "action:args",
|
|
1021
|
-
id: currentToolUse.id,
|
|
1022
|
-
args: currentToolUse.input
|
|
1023
|
-
};
|
|
1024
|
-
}
|
|
1025
1083
|
}
|
|
1026
1084
|
break;
|
|
1027
1085
|
case "content_block_stop":
|
|
@@ -1196,7 +1254,8 @@ function createAnthropic(config = {}) {
|
|
|
1196
1254
|
"image/gif",
|
|
1197
1255
|
"image/webp"
|
|
1198
1256
|
],
|
|
1199
|
-
|
|
1257
|
+
// Native `output_config.format` — GA on Claude 3.5 and newer.
|
|
1258
|
+
supportsJsonMode: true,
|
|
1200
1259
|
supportsSystemMessages: true
|
|
1201
1260
|
};
|
|
1202
1261
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { b as AzureProviderConfig, A as AIProvider } from '../../types-
|
|
2
|
-
import '../../
|
|
3
|
-
import '../../types-CMvvDo-E.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-CMvvDo-E.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
|