@providerprotocol/ai 0.0.34 → 0.0.36
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/README.md +546 -3
- package/dist/anthropic/index.d.ts +2 -1
- package/dist/anthropic/index.js +151 -145
- package/dist/anthropic/index.js.map +1 -1
- package/dist/cerebras/index.d.ts +392 -0
- package/dist/cerebras/index.js +648 -0
- package/dist/cerebras/index.js.map +1 -0
- package/dist/chunk-2YXFLRQ6.js +49 -0
- package/dist/chunk-2YXFLRQ6.js.map +1 -0
- package/dist/chunk-4OGB7JZA.js +157 -0
- package/dist/chunk-4OGB7JZA.js.map +1 -0
- package/dist/chunk-4RX4VQCB.js +31 -0
- package/dist/chunk-4RX4VQCB.js.map +1 -0
- package/dist/chunk-5IWHCXKN.js +30 -0
- package/dist/chunk-5IWHCXKN.js.map +1 -0
- package/dist/{chunk-3C7O2RNO.js → chunk-A2IM7PGT.js} +6 -4
- package/dist/{chunk-3C7O2RNO.js.map → chunk-A2IM7PGT.js.map} +1 -1
- package/dist/{chunk-3D6XGGVG.js → chunk-ARVM24K2.js} +2 -2
- package/dist/{chunk-4J6OFUKX.js → chunk-AY55T37A.js} +70 -162
- package/dist/chunk-AY55T37A.js.map +1 -0
- package/dist/{chunk-ILR2D5PN.js → chunk-BRP5XJ6Q.js} +2 -86
- package/dist/chunk-BRP5XJ6Q.js.map +1 -0
- package/dist/chunk-C4JP64VW.js +298 -0
- package/dist/chunk-C4JP64VW.js.map +1 -0
- package/dist/chunk-COS4ON4G.js +111 -0
- package/dist/chunk-COS4ON4G.js.map +1 -0
- package/dist/chunk-CRP6Y7NF.js +31 -0
- package/dist/chunk-CRP6Y7NF.js.map +1 -0
- package/dist/chunk-EPB3GQNL.js +118 -0
- package/dist/chunk-EPB3GQNL.js.map +1 -0
- package/dist/chunk-ETBFOLQN.js +34 -0
- package/dist/chunk-ETBFOLQN.js.map +1 -0
- package/dist/chunk-I53CI6ZZ.js +142 -0
- package/dist/chunk-I53CI6ZZ.js.map +1 -0
- package/dist/chunk-IDZOVWP3.js +29 -0
- package/dist/chunk-IDZOVWP3.js.map +1 -0
- package/dist/chunk-JA3UZALR.js +88 -0
- package/dist/chunk-JA3UZALR.js.map +1 -0
- package/dist/{chunk-WAKD3OO5.js → chunk-N5DX5JW3.js} +31 -31
- package/dist/chunk-N5DX5JW3.js.map +1 -0
- package/dist/chunk-OIEWDFQU.js +97 -0
- package/dist/chunk-OIEWDFQU.js.map +1 -0
- package/dist/{chunk-TOJCZMVU.js → chunk-PMK5LZ5Z.js} +40 -40
- package/dist/chunk-PMK5LZ5Z.js.map +1 -0
- package/dist/{chunk-6S222DHN.js → chunk-RJGTRQ47.js} +20 -1
- package/dist/chunk-RJGTRQ47.js.map +1 -0
- package/dist/chunk-UFFJDYCE.js +94 -0
- package/dist/chunk-UFFJDYCE.js.map +1 -0
- package/dist/chunk-VGKZIGVI.js +222 -0
- package/dist/chunk-VGKZIGVI.js.map +1 -0
- package/dist/{chunk-KUPF5KHT.js → chunk-Y5H7C5J4.js} +2 -2
- package/dist/{embedding-D2BYIehX.d.ts → embedding-BXA72PlJ.d.ts} +1 -1
- package/dist/google/index.d.ts +2 -1
- package/dist/google/index.js +202 -199
- package/dist/google/index.js.map +1 -1
- package/dist/groq/index.d.ts +410 -0
- package/dist/groq/index.js +649 -0
- package/dist/groq/index.js.map +1 -0
- package/dist/http/index.d.ts +3 -2
- package/dist/http/index.js +5 -4
- package/dist/image-stream-CCgwB7ve.d.ts +11 -0
- package/dist/index.d.ts +8 -118
- package/dist/index.js +520 -769
- package/dist/index.js.map +1 -1
- package/dist/{llm-BQJZj3cD.d.ts → llm-ByUFPcFH.d.ts} +12 -1632
- package/dist/middleware/logging/index.d.ts +76 -0
- package/dist/middleware/logging/index.js +74 -0
- package/dist/middleware/logging/index.js.map +1 -0
- package/dist/middleware/parsed-object/index.d.ts +45 -0
- package/dist/middleware/parsed-object/index.js +73 -0
- package/dist/middleware/parsed-object/index.js.map +1 -0
- package/dist/middleware/pubsub/index.d.ts +97 -0
- package/dist/middleware/pubsub/index.js +160 -0
- package/dist/middleware/pubsub/index.js.map +1 -0
- package/dist/middleware/pubsub/server/express/index.d.ts +66 -0
- package/dist/middleware/pubsub/server/express/index.js +11 -0
- package/dist/middleware/pubsub/server/express/index.js.map +1 -0
- package/dist/middleware/pubsub/server/fastify/index.d.ts +67 -0
- package/dist/middleware/pubsub/server/fastify/index.js +11 -0
- package/dist/middleware/pubsub/server/fastify/index.js.map +1 -0
- package/dist/middleware/pubsub/server/h3/index.d.ts +70 -0
- package/dist/middleware/pubsub/server/h3/index.js +11 -0
- package/dist/middleware/pubsub/server/h3/index.js.map +1 -0
- package/dist/middleware/pubsub/server/index.d.ts +78 -0
- package/dist/middleware/pubsub/server/index.js +34 -0
- package/dist/middleware/pubsub/server/index.js.map +1 -0
- package/dist/middleware/pubsub/server/webapi/index.d.ts +63 -0
- package/dist/middleware/pubsub/server/webapi/index.js +11 -0
- package/dist/middleware/pubsub/server/webapi/index.js.map +1 -0
- package/dist/ollama/index.d.ts +2 -1
- package/dist/ollama/index.js +48 -45
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +2 -1
- package/dist/openai/index.js +319 -313
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +2 -1
- package/dist/openrouter/index.js +381 -385
- package/dist/openrouter/index.js.map +1 -1
- package/dist/proxy/index.d.ts +10 -914
- package/dist/proxy/index.js +275 -1007
- package/dist/proxy/index.js.map +1 -1
- package/dist/proxy/server/express/index.d.ts +161 -0
- package/dist/proxy/server/express/index.js +24 -0
- package/dist/proxy/server/express/index.js.map +1 -0
- package/dist/proxy/server/fastify/index.d.ts +162 -0
- package/dist/proxy/server/fastify/index.js +24 -0
- package/dist/proxy/server/fastify/index.js.map +1 -0
- package/dist/proxy/server/h3/index.d.ts +189 -0
- package/dist/proxy/server/h3/index.js +28 -0
- package/dist/proxy/server/h3/index.js.map +1 -0
- package/dist/proxy/server/index.d.ts +151 -0
- package/dist/proxy/server/index.js +48 -0
- package/dist/proxy/server/index.js.map +1 -0
- package/dist/proxy/server/webapi/index.d.ts +278 -0
- package/dist/proxy/server/webapi/index.js +32 -0
- package/dist/proxy/server/webapi/index.js.map +1 -0
- package/dist/responses/index.d.ts +650 -0
- package/dist/responses/index.js +930 -0
- package/dist/responses/index.js.map +1 -0
- package/dist/{retry-8Ch-WWgX.d.ts → retry-BDMo4AVu.d.ts} +1 -1
- package/dist/stream-S7nwQRqM.d.ts +1643 -0
- package/dist/types-CE4B7pno.d.ts +96 -0
- package/dist/utils/index.d.ts +53 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/xai/index.d.ts +2 -1
- package/dist/xai/index.js +310 -310
- package/dist/xai/index.js.map +1 -1
- package/package.json +82 -4
- package/dist/chunk-4J6OFUKX.js.map +0 -1
- package/dist/chunk-6S222DHN.js.map +0 -1
- package/dist/chunk-ILR2D5PN.js.map +0 -1
- package/dist/chunk-TOJCZMVU.js.map +0 -1
- package/dist/chunk-WAKD3OO5.js.map +0 -1
- /package/dist/{chunk-3D6XGGVG.js.map → chunk-ARVM24K2.js.map} +0 -0
- /package/dist/{chunk-KUPF5KHT.js.map → chunk-Y5H7C5J4.js.map} +0 -0
package/dist/anthropic/index.js
CHANGED
|
@@ -1,33 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseSSEStream
|
|
3
|
+
} from "../chunk-PMK5LZ5Z.js";
|
|
1
4
|
import {
|
|
2
5
|
parseJsonResponse
|
|
3
|
-
} from "../chunk-
|
|
6
|
+
} from "../chunk-A2IM7PGT.js";
|
|
4
7
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
+
resolveApiKey
|
|
9
|
+
} from "../chunk-ARVM24K2.js";
|
|
10
|
+
import {
|
|
11
|
+
createProvider
|
|
12
|
+
} from "../chunk-JA3UZALR.js";
|
|
8
13
|
import {
|
|
9
14
|
AssistantMessage,
|
|
10
|
-
createProvider,
|
|
11
15
|
generateId,
|
|
12
16
|
isAssistantMessage,
|
|
13
17
|
isToolResultMessage,
|
|
14
18
|
isUserMessage
|
|
15
|
-
} from "../chunk-
|
|
16
|
-
import {
|
|
17
|
-
parseSSEStream
|
|
18
|
-
} from "../chunk-TOJCZMVU.js";
|
|
19
|
-
import {
|
|
20
|
-
resolveApiKey
|
|
21
|
-
} from "../chunk-3D6XGGVG.js";
|
|
19
|
+
} from "../chunk-BRP5XJ6Q.js";
|
|
22
20
|
import {
|
|
23
|
-
ErrorCode,
|
|
24
|
-
ModalityType,
|
|
25
|
-
UPPError,
|
|
26
21
|
doFetch,
|
|
27
22
|
doStreamFetch,
|
|
28
23
|
normalizeHttpError,
|
|
29
24
|
toError
|
|
30
|
-
} from "../chunk-
|
|
25
|
+
} from "../chunk-AY55T37A.js";
|
|
26
|
+
import {
|
|
27
|
+
ErrorCode,
|
|
28
|
+
ModalityType,
|
|
29
|
+
UPPError
|
|
30
|
+
} from "../chunk-COS4ON4G.js";
|
|
31
|
+
import {
|
|
32
|
+
StreamEventType,
|
|
33
|
+
objectDelta
|
|
34
|
+
} from "../chunk-RJGTRQ47.js";
|
|
31
35
|
|
|
32
36
|
// src/providers/anthropic/types.ts
|
|
33
37
|
var betas = {
|
|
@@ -154,59 +158,14 @@ var tools = {
|
|
|
154
158
|
};
|
|
155
159
|
|
|
156
160
|
// src/providers/anthropic/transform.ts
|
|
157
|
-
function
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
messages: request.messages.map(transformMessage)
|
|
164
|
-
};
|
|
165
|
-
const normalizedSystem = normalizeSystem(request.system);
|
|
166
|
-
if (normalizedSystem !== void 0) {
|
|
167
|
-
anthropicRequest.system = normalizedSystem;
|
|
168
|
-
}
|
|
169
|
-
const allTools = [];
|
|
170
|
-
if (request.tools && request.tools.length > 0) {
|
|
171
|
-
allTools.push(...request.tools.map(transformTool));
|
|
172
|
-
}
|
|
173
|
-
if (builtInTools && builtInTools.length > 0) {
|
|
174
|
-
allTools.push(...builtInTools);
|
|
175
|
-
}
|
|
176
|
-
if (allTools.length > 0) {
|
|
177
|
-
anthropicRequest.tools = allTools;
|
|
178
|
-
if (!anthropicRequest.tool_choice) {
|
|
179
|
-
anthropicRequest.tool_choice = { type: "auto" };
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
if (request.structure) {
|
|
183
|
-
if (useNativeStructuredOutput) {
|
|
184
|
-
const outputFormat = {
|
|
185
|
-
type: "json_schema",
|
|
186
|
-
schema: {
|
|
187
|
-
type: "object",
|
|
188
|
-
properties: request.structure.properties,
|
|
189
|
-
required: request.structure.required,
|
|
190
|
-
additionalProperties: false
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
anthropicRequest.output_format = outputFormat;
|
|
194
|
-
} else {
|
|
195
|
-
const structuredTool = {
|
|
196
|
-
name: "json_response",
|
|
197
|
-
description: "Return the response in the specified JSON format. You MUST use this tool to provide your response.",
|
|
198
|
-
input_schema: {
|
|
199
|
-
type: "object",
|
|
200
|
-
properties: request.structure.properties,
|
|
201
|
-
required: request.structure.required,
|
|
202
|
-
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : {}
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
anthropicRequest.tools = [...anthropicRequest.tools ?? [], structuredTool];
|
|
206
|
-
anthropicRequest.tool_choice = { type: "tool", name: "json_response" };
|
|
207
|
-
}
|
|
161
|
+
function isValidCacheControl(value) {
|
|
162
|
+
if (!value || typeof value !== "object") return false;
|
|
163
|
+
const candidate = value;
|
|
164
|
+
if (candidate.type !== "ephemeral") return false;
|
|
165
|
+
if (candidate.ttl !== void 0 && candidate.ttl !== "5m" && candidate.ttl !== "1h") {
|
|
166
|
+
return false;
|
|
208
167
|
}
|
|
209
|
-
return
|
|
168
|
+
return true;
|
|
210
169
|
}
|
|
211
170
|
function normalizeSystem(system) {
|
|
212
171
|
if (system === void 0 || system === null) return void 0;
|
|
@@ -250,15 +209,6 @@ function normalizeSystem(system) {
|
|
|
250
209
|
}
|
|
251
210
|
return blocks.length > 0 ? blocks : void 0;
|
|
252
211
|
}
|
|
253
|
-
function isValidCacheControl(value) {
|
|
254
|
-
if (!value || typeof value !== "object") return false;
|
|
255
|
-
const candidate = value;
|
|
256
|
-
if (candidate.type !== "ephemeral") return false;
|
|
257
|
-
if (candidate.ttl !== void 0 && candidate.ttl !== "5m" && candidate.ttl !== "1h") {
|
|
258
|
-
return false;
|
|
259
|
-
}
|
|
260
|
-
return true;
|
|
261
|
-
}
|
|
262
212
|
function filterValidContent(content) {
|
|
263
213
|
return content.filter((c) => c && typeof c.type === "string");
|
|
264
214
|
}
|
|
@@ -266,72 +216,6 @@ function extractCacheControl(message) {
|
|
|
266
216
|
const anthropicMeta = message.metadata?.anthropic;
|
|
267
217
|
return anthropicMeta?.cache_control;
|
|
268
218
|
}
|
|
269
|
-
function transformMessage(message) {
|
|
270
|
-
const cacheControl = extractCacheControl(message);
|
|
271
|
-
if (isUserMessage(message)) {
|
|
272
|
-
const validContent = filterValidContent(message.content);
|
|
273
|
-
const contentBlocks = validContent.map(
|
|
274
|
-
(block, index, arr) => transformContentBlock(block, index === arr.length - 1 ? cacheControl : void 0)
|
|
275
|
-
);
|
|
276
|
-
return {
|
|
277
|
-
role: "user",
|
|
278
|
-
content: contentBlocks
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
if (isAssistantMessage(message)) {
|
|
282
|
-
const validContent = filterValidContent(message.content);
|
|
283
|
-
const content = [];
|
|
284
|
-
const anthropicMeta = message.metadata?.anthropic;
|
|
285
|
-
const thinkingSignatures = anthropicMeta?.thinkingSignatures;
|
|
286
|
-
let reasoningIndex = 0;
|
|
287
|
-
for (let i = 0; i < validContent.length; i++) {
|
|
288
|
-
const block = validContent[i];
|
|
289
|
-
const isLastNonToolBlock = i === validContent.length - 1 && !message.toolCalls?.length;
|
|
290
|
-
if (block.type === "reasoning") {
|
|
291
|
-
const signatureFromArray = thinkingSignatures?.[reasoningIndex];
|
|
292
|
-
const signature = Array.isArray(thinkingSignatures) ? typeof signatureFromArray === "string" ? signatureFromArray : void 0 : anthropicMeta?.thinkingSignature;
|
|
293
|
-
reasoningIndex += 1;
|
|
294
|
-
content.push({
|
|
295
|
-
type: "thinking",
|
|
296
|
-
thinking: block.text,
|
|
297
|
-
...signature ? { signature } : {}
|
|
298
|
-
});
|
|
299
|
-
} else {
|
|
300
|
-
content.push(transformContentBlock(block, isLastNonToolBlock ? cacheControl : void 0));
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
if (message.toolCalls) {
|
|
304
|
-
for (let i = 0; i < message.toolCalls.length; i++) {
|
|
305
|
-
const call = message.toolCalls[i];
|
|
306
|
-
const isLast = i === message.toolCalls.length - 1;
|
|
307
|
-
content.push({
|
|
308
|
-
type: "tool_use",
|
|
309
|
-
id: call.toolCallId,
|
|
310
|
-
name: call.toolName,
|
|
311
|
-
input: call.arguments,
|
|
312
|
-
...isLast && cacheControl ? { cache_control: cacheControl } : {}
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
return {
|
|
317
|
-
role: "assistant",
|
|
318
|
-
content
|
|
319
|
-
};
|
|
320
|
-
}
|
|
321
|
-
if (isToolResultMessage(message)) {
|
|
322
|
-
return {
|
|
323
|
-
role: "user",
|
|
324
|
-
content: message.results.map((result, index, arr) => ({
|
|
325
|
-
type: "tool_result",
|
|
326
|
-
tool_use_id: result.toolCallId,
|
|
327
|
-
content: typeof result.result === "string" ? result.result : JSON.stringify(result.result),
|
|
328
|
-
is_error: result.isError,
|
|
329
|
-
...index === arr.length - 1 && cacheControl ? { cache_control: cacheControl } : {}
|
|
330
|
-
}))
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
throw new Error(`Unknown message type: ${message.type}`);
|
|
334
|
-
}
|
|
335
219
|
function transformContentBlock(block, cacheControl) {
|
|
336
220
|
switch (block.type) {
|
|
337
221
|
case "text":
|
|
@@ -417,6 +301,72 @@ function transformContentBlock(block, cacheControl) {
|
|
|
417
301
|
throw new Error(`Unsupported content type: ${block.type}`);
|
|
418
302
|
}
|
|
419
303
|
}
|
|
304
|
+
function transformMessage(message) {
|
|
305
|
+
const cacheControl = extractCacheControl(message);
|
|
306
|
+
if (isUserMessage(message)) {
|
|
307
|
+
const validContent = filterValidContent(message.content);
|
|
308
|
+
const contentBlocks = validContent.map(
|
|
309
|
+
(block, index, arr) => transformContentBlock(block, index === arr.length - 1 ? cacheControl : void 0)
|
|
310
|
+
);
|
|
311
|
+
return {
|
|
312
|
+
role: "user",
|
|
313
|
+
content: contentBlocks
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
if (isAssistantMessage(message)) {
|
|
317
|
+
const validContent = filterValidContent(message.content);
|
|
318
|
+
const content = [];
|
|
319
|
+
const anthropicMeta = message.metadata?.anthropic;
|
|
320
|
+
const thinkingSignatures = anthropicMeta?.thinkingSignatures;
|
|
321
|
+
let reasoningIndex = 0;
|
|
322
|
+
for (let i = 0; i < validContent.length; i++) {
|
|
323
|
+
const block = validContent[i];
|
|
324
|
+
const isLastNonToolBlock = i === validContent.length - 1 && !message.toolCalls?.length;
|
|
325
|
+
if (block.type === "reasoning") {
|
|
326
|
+
const signatureFromArray = thinkingSignatures?.[reasoningIndex];
|
|
327
|
+
const signature = Array.isArray(thinkingSignatures) ? typeof signatureFromArray === "string" ? signatureFromArray : void 0 : anthropicMeta?.thinkingSignature;
|
|
328
|
+
reasoningIndex += 1;
|
|
329
|
+
content.push({
|
|
330
|
+
type: "thinking",
|
|
331
|
+
thinking: block.text,
|
|
332
|
+
...signature ? { signature } : {}
|
|
333
|
+
});
|
|
334
|
+
} else {
|
|
335
|
+
content.push(transformContentBlock(block, isLastNonToolBlock ? cacheControl : void 0));
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (message.toolCalls) {
|
|
339
|
+
for (let i = 0; i < message.toolCalls.length; i++) {
|
|
340
|
+
const call = message.toolCalls[i];
|
|
341
|
+
const isLast = i === message.toolCalls.length - 1;
|
|
342
|
+
content.push({
|
|
343
|
+
type: "tool_use",
|
|
344
|
+
id: call.toolCallId,
|
|
345
|
+
name: call.toolName,
|
|
346
|
+
input: call.arguments,
|
|
347
|
+
...isLast && cacheControl ? { cache_control: cacheControl } : {}
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
role: "assistant",
|
|
353
|
+
content
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
if (isToolResultMessage(message)) {
|
|
357
|
+
return {
|
|
358
|
+
role: "user",
|
|
359
|
+
content: message.results.map((result, index, arr) => ({
|
|
360
|
+
type: "tool_result",
|
|
361
|
+
tool_use_id: result.toolCallId,
|
|
362
|
+
content: typeof result.result === "string" ? result.result : JSON.stringify(result.result),
|
|
363
|
+
is_error: result.isError,
|
|
364
|
+
...index === arr.length - 1 && cacheControl ? { cache_control: cacheControl } : {}
|
|
365
|
+
}))
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
throw new Error(`Unknown message type: ${message.type}`);
|
|
369
|
+
}
|
|
420
370
|
function extractToolCacheControl(tool) {
|
|
421
371
|
const anthropicMeta = tool.metadata?.anthropic;
|
|
422
372
|
return anthropicMeta?.cache_control;
|
|
@@ -435,6 +385,60 @@ function transformTool(tool) {
|
|
|
435
385
|
...cacheControl ? { cache_control: cacheControl } : {}
|
|
436
386
|
};
|
|
437
387
|
}
|
|
388
|
+
function transformRequest(request, modelId, useNativeStructuredOutput = false) {
|
|
389
|
+
const params = request.params ?? {};
|
|
390
|
+
const { tools: builtInTools, ...restParams } = params;
|
|
391
|
+
const anthropicRequest = {
|
|
392
|
+
...restParams,
|
|
393
|
+
model: modelId,
|
|
394
|
+
messages: request.messages.map(transformMessage)
|
|
395
|
+
};
|
|
396
|
+
const normalizedSystem = normalizeSystem(request.system);
|
|
397
|
+
if (normalizedSystem !== void 0) {
|
|
398
|
+
anthropicRequest.system = normalizedSystem;
|
|
399
|
+
}
|
|
400
|
+
const allTools = [];
|
|
401
|
+
if (request.tools && request.tools.length > 0) {
|
|
402
|
+
allTools.push(...request.tools.map(transformTool));
|
|
403
|
+
}
|
|
404
|
+
if (builtInTools && builtInTools.length > 0) {
|
|
405
|
+
allTools.push(...builtInTools);
|
|
406
|
+
}
|
|
407
|
+
if (allTools.length > 0) {
|
|
408
|
+
anthropicRequest.tools = allTools;
|
|
409
|
+
if (!anthropicRequest.tool_choice) {
|
|
410
|
+
anthropicRequest.tool_choice = { type: "auto" };
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (request.structure) {
|
|
414
|
+
if (useNativeStructuredOutput) {
|
|
415
|
+
const outputFormat = {
|
|
416
|
+
type: "json_schema",
|
|
417
|
+
schema: {
|
|
418
|
+
type: "object",
|
|
419
|
+
properties: request.structure.properties,
|
|
420
|
+
required: request.structure.required,
|
|
421
|
+
additionalProperties: false
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
anthropicRequest.output_format = outputFormat;
|
|
425
|
+
} else {
|
|
426
|
+
const structuredTool = {
|
|
427
|
+
name: "json_response",
|
|
428
|
+
description: "Return the response in the specified JSON format. You MUST use this tool to provide your response.",
|
|
429
|
+
input_schema: {
|
|
430
|
+
type: "object",
|
|
431
|
+
properties: request.structure.properties,
|
|
432
|
+
required: request.structure.required,
|
|
433
|
+
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : {}
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
anthropicRequest.tools = [...anthropicRequest.tools ?? [], structuredTool];
|
|
437
|
+
anthropicRequest.tool_choice = { type: "tool", name: "json_response" };
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return anthropicRequest;
|
|
441
|
+
}
|
|
438
442
|
function transformResponse(data, useNativeStructuredOutput = false) {
|
|
439
443
|
const reasoningContent = [];
|
|
440
444
|
const textContent = [];
|
|
@@ -883,10 +887,12 @@ function createLLMHandler() {
|
|
|
883
887
|
}
|
|
884
888
|
const uppEvents = transformStreamEvent(event, state);
|
|
885
889
|
for (const uppEvent of uppEvents) {
|
|
890
|
+
yield uppEvent;
|
|
886
891
|
if (request.structure && uppEvent.type === StreamEventType.TextDelta) {
|
|
887
892
|
yield objectDelta(uppEvent.delta.text ?? "", uppEvent.index);
|
|
888
|
-
}
|
|
889
|
-
|
|
893
|
+
}
|
|
894
|
+
if (request.structure && uppEvent.type === StreamEventType.ToolCallDelta && uppEvent.delta.argumentsJson) {
|
|
895
|
+
yield objectDelta(uppEvent.delta.argumentsJson, uppEvent.index);
|
|
890
896
|
}
|
|
891
897
|
}
|
|
892
898
|
}
|