@providerprotocol/ai 0.0.21 → 0.0.22
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/anthropic/index.d.ts +1 -1
- package/dist/anthropic/index.js +100 -29
- package/dist/anthropic/index.js.map +1 -1
- package/dist/{chunk-Y3GBJNA2.js → chunk-7WYBJPJJ.js} +2 -2
- package/dist/chunk-I2VHCGQE.js +49 -0
- package/dist/chunk-I2VHCGQE.js.map +1 -0
- package/dist/{chunk-SKY2JLA7.js → chunk-MKDLXV4O.js} +1 -1
- package/dist/chunk-MKDLXV4O.js.map +1 -0
- package/dist/{chunk-Z7RBRCRN.js → chunk-NWS5IKNR.js} +37 -11
- package/dist/chunk-NWS5IKNR.js.map +1 -0
- package/dist/{chunk-EDENPF3E.js → chunk-RFWLEFAB.js} +96 -42
- package/dist/chunk-RFWLEFAB.js.map +1 -0
- package/dist/{chunk-Z4ILICF5.js → chunk-RS7C25LS.js} +35 -10
- package/dist/chunk-RS7C25LS.js.map +1 -0
- package/dist/google/index.d.ts +20 -6
- package/dist/google/index.js +261 -65
- package/dist/google/index.js.map +1 -1
- package/dist/http/index.d.ts +3 -3
- package/dist/http/index.js +4 -4
- package/dist/index.d.ts +7 -5
- package/dist/index.js +286 -119
- package/dist/index.js.map +1 -1
- package/dist/ollama/index.d.ts +1 -1
- package/dist/ollama/index.js +66 -12
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +1 -1
- package/dist/openai/index.js +183 -43
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +1 -1
- package/dist/openrouter/index.js +161 -31
- package/dist/openrouter/index.js.map +1 -1
- package/dist/{provider-DGQHYE6I.d.ts → provider-DWEAzeM5.d.ts} +11 -1
- package/dist/proxy/index.d.ts +2 -2
- package/dist/proxy/index.js +171 -12
- package/dist/proxy/index.js.map +1 -1
- package/dist/{retry-Pcs3hnbu.d.ts → retry-DmPmqZL6.d.ts} +11 -2
- package/dist/{stream-Di9acos2.d.ts → stream-DbkLOIbJ.d.ts} +15 -5
- package/dist/xai/index.d.ts +1 -1
- package/dist/xai/index.js +139 -30
- package/dist/xai/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-EDENPF3E.js.map +0 -1
- package/dist/chunk-SKY2JLA7.js.map +0 -1
- package/dist/chunk-Z4ILICF5.js.map +0 -1
- package/dist/chunk-Z7RBRCRN.js.map +0 -1
- /package/dist/{chunk-Y3GBJNA2.js.map → chunk-7WYBJPJJ.js.map} +0 -0
package/dist/anthropic/index.js
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseJsonResponse
|
|
3
|
+
} from "../chunk-I2VHCGQE.js";
|
|
1
4
|
import {
|
|
2
5
|
AssistantMessage,
|
|
3
6
|
createProvider,
|
|
7
|
+
generateId,
|
|
4
8
|
isAssistantMessage,
|
|
5
9
|
isToolResultMessage,
|
|
6
10
|
isUserMessage
|
|
7
11
|
} from "../chunk-M4BMM5IB.js";
|
|
8
12
|
import {
|
|
9
13
|
parseSSEStream
|
|
10
|
-
} from "../chunk-
|
|
14
|
+
} from "../chunk-NWS5IKNR.js";
|
|
11
15
|
import {
|
|
12
16
|
resolveApiKey
|
|
13
|
-
} from "../chunk-
|
|
17
|
+
} from "../chunk-7WYBJPJJ.js";
|
|
14
18
|
import {
|
|
15
19
|
UPPError,
|
|
16
20
|
doFetch,
|
|
17
21
|
doStreamFetch,
|
|
18
|
-
normalizeHttpError
|
|
19
|
-
|
|
22
|
+
normalizeHttpError,
|
|
23
|
+
toError
|
|
24
|
+
} from "../chunk-RFWLEFAB.js";
|
|
20
25
|
|
|
21
26
|
// src/providers/anthropic/types.ts
|
|
22
27
|
var betas = {
|
|
@@ -151,8 +156,9 @@ function transformRequest(request, modelId, useNativeStructuredOutput = false) {
|
|
|
151
156
|
model: modelId,
|
|
152
157
|
messages: request.messages.map(transformMessage)
|
|
153
158
|
};
|
|
154
|
-
|
|
155
|
-
|
|
159
|
+
const normalizedSystem = normalizeSystem(request.system);
|
|
160
|
+
if (normalizedSystem !== void 0) {
|
|
161
|
+
anthropicRequest.system = normalizedSystem;
|
|
156
162
|
}
|
|
157
163
|
const allTools = [];
|
|
158
164
|
if (request.tools && request.tools.length > 0) {
|
|
@@ -184,7 +190,8 @@ function transformRequest(request, modelId, useNativeStructuredOutput = false) {
|
|
|
184
190
|
input_schema: {
|
|
185
191
|
type: "object",
|
|
186
192
|
properties: request.structure.properties,
|
|
187
|
-
required: request.structure.required
|
|
193
|
+
required: request.structure.required,
|
|
194
|
+
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : {}
|
|
188
195
|
}
|
|
189
196
|
};
|
|
190
197
|
anthropicRequest.tools = [...anthropicRequest.tools ?? [], structuredTool];
|
|
@@ -193,6 +200,57 @@ function transformRequest(request, modelId, useNativeStructuredOutput = false) {
|
|
|
193
200
|
}
|
|
194
201
|
return anthropicRequest;
|
|
195
202
|
}
|
|
203
|
+
function normalizeSystem(system) {
|
|
204
|
+
if (system === void 0 || system === null) return void 0;
|
|
205
|
+
if (typeof system === "string") return system;
|
|
206
|
+
if (!Array.isArray(system)) {
|
|
207
|
+
throw new UPPError(
|
|
208
|
+
"System prompt must be a string or an array of text blocks",
|
|
209
|
+
"INVALID_REQUEST",
|
|
210
|
+
"anthropic",
|
|
211
|
+
"llm"
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
const blocks = [];
|
|
215
|
+
for (const block of system) {
|
|
216
|
+
if (!block || typeof block !== "object") {
|
|
217
|
+
throw new UPPError(
|
|
218
|
+
'System prompt array must contain objects with type "text"',
|
|
219
|
+
"INVALID_REQUEST",
|
|
220
|
+
"anthropic",
|
|
221
|
+
"llm"
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
const candidate = block;
|
|
225
|
+
if (candidate.type !== "text" || typeof candidate.text !== "string") {
|
|
226
|
+
throw new UPPError(
|
|
227
|
+
'Anthropic system blocks must be of type "text" with a string text field',
|
|
228
|
+
"INVALID_REQUEST",
|
|
229
|
+
"anthropic",
|
|
230
|
+
"llm"
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
if (candidate.cache_control !== void 0 && !isValidCacheControl(candidate.cache_control)) {
|
|
234
|
+
throw new UPPError(
|
|
235
|
+
"Invalid cache_control for Anthropic system prompt",
|
|
236
|
+
"INVALID_REQUEST",
|
|
237
|
+
"anthropic",
|
|
238
|
+
"llm"
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
blocks.push(block);
|
|
242
|
+
}
|
|
243
|
+
return blocks.length > 0 ? blocks : void 0;
|
|
244
|
+
}
|
|
245
|
+
function isValidCacheControl(value) {
|
|
246
|
+
if (!value || typeof value !== "object") return false;
|
|
247
|
+
const candidate = value;
|
|
248
|
+
if (candidate.type !== "ephemeral") return false;
|
|
249
|
+
if (candidate.ttl !== void 0 && candidate.ttl !== "5m" && candidate.ttl !== "1h") {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
196
254
|
function filterValidContent(content) {
|
|
197
255
|
return content.filter((c) => c && typeof c.type === "string");
|
|
198
256
|
}
|
|
@@ -312,7 +370,8 @@ function transformTool(tool) {
|
|
|
312
370
|
input_schema: {
|
|
313
371
|
type: "object",
|
|
314
372
|
properties: tool.parameters.properties,
|
|
315
|
-
required: tool.parameters.required
|
|
373
|
+
required: tool.parameters.required,
|
|
374
|
+
...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
|
|
316
375
|
},
|
|
317
376
|
...cacheControl ? { cache_control: cacheControl } : {}
|
|
318
377
|
};
|
|
@@ -396,6 +455,7 @@ function createStreamState() {
|
|
|
396
455
|
};
|
|
397
456
|
}
|
|
398
457
|
function transformStreamEvent(event, state) {
|
|
458
|
+
const events = [];
|
|
399
459
|
switch (event.type) {
|
|
400
460
|
case "message_start":
|
|
401
461
|
state.messageId = event.message.id;
|
|
@@ -403,7 +463,8 @@ function transformStreamEvent(event, state) {
|
|
|
403
463
|
state.inputTokens = event.message.usage.input_tokens;
|
|
404
464
|
state.cacheReadTokens = event.message.usage.cache_read_input_tokens ?? 0;
|
|
405
465
|
state.cacheWriteTokens = event.message.usage.cache_creation_input_tokens ?? 0;
|
|
406
|
-
|
|
466
|
+
events.push({ type: "message_start", index: 0, delta: {} });
|
|
467
|
+
break;
|
|
407
468
|
case "content_block_start":
|
|
408
469
|
if (event.content_block.type === "text") {
|
|
409
470
|
state.content[event.index] = { type: "text", text: "" };
|
|
@@ -436,24 +497,26 @@ function transformStreamEvent(event, state) {
|
|
|
436
497
|
fileContent: resultBlock.content?.content ?? ""
|
|
437
498
|
};
|
|
438
499
|
}
|
|
439
|
-
|
|
500
|
+
events.push({ type: "content_block_start", index: event.index, delta: {} });
|
|
501
|
+
break;
|
|
440
502
|
case "content_block_delta": {
|
|
441
503
|
const delta = event.delta;
|
|
442
504
|
if (delta.type === "text_delta") {
|
|
443
505
|
if (state.content[event.index]) {
|
|
444
506
|
state.content[event.index].text = (state.content[event.index].text ?? "") + delta.text;
|
|
445
507
|
}
|
|
446
|
-
|
|
508
|
+
events.push({
|
|
447
509
|
type: "text_delta",
|
|
448
510
|
index: event.index,
|
|
449
511
|
delta: { text: delta.text }
|
|
450
|
-
};
|
|
512
|
+
});
|
|
513
|
+
break;
|
|
451
514
|
}
|
|
452
515
|
if (delta.type === "input_json_delta") {
|
|
453
516
|
if (state.content[event.index]) {
|
|
454
517
|
state.content[event.index].input = (state.content[event.index].input ?? "") + delta.partial_json;
|
|
455
518
|
}
|
|
456
|
-
|
|
519
|
+
events.push({
|
|
457
520
|
type: "tool_call_delta",
|
|
458
521
|
index: event.index,
|
|
459
522
|
delta: {
|
|
@@ -461,31 +524,36 @@ function transformStreamEvent(event, state) {
|
|
|
461
524
|
toolCallId: state.content[event.index]?.id,
|
|
462
525
|
toolName: state.content[event.index]?.name
|
|
463
526
|
}
|
|
464
|
-
};
|
|
527
|
+
});
|
|
528
|
+
break;
|
|
465
529
|
}
|
|
466
530
|
if (delta.type === "thinking_delta") {
|
|
467
|
-
|
|
531
|
+
events.push({
|
|
468
532
|
type: "reasoning_delta",
|
|
469
533
|
index: event.index,
|
|
470
534
|
delta: { text: delta.thinking }
|
|
471
|
-
};
|
|
535
|
+
});
|
|
536
|
+
break;
|
|
472
537
|
}
|
|
473
|
-
|
|
538
|
+
break;
|
|
474
539
|
}
|
|
475
540
|
case "content_block_stop":
|
|
476
|
-
|
|
541
|
+
events.push({ type: "content_block_stop", index: event.index, delta: {} });
|
|
542
|
+
break;
|
|
477
543
|
case "message_delta":
|
|
478
544
|
state.stopReason = event.delta.stop_reason;
|
|
479
545
|
state.outputTokens = event.usage.output_tokens;
|
|
480
|
-
return
|
|
546
|
+
return [];
|
|
481
547
|
case "message_stop":
|
|
482
|
-
|
|
548
|
+
events.push({ type: "message_stop", index: 0, delta: {} });
|
|
549
|
+
break;
|
|
483
550
|
case "ping":
|
|
484
551
|
case "error":
|
|
485
|
-
return
|
|
552
|
+
return [];
|
|
486
553
|
default:
|
|
487
|
-
|
|
554
|
+
break;
|
|
488
555
|
}
|
|
556
|
+
return events;
|
|
489
557
|
}
|
|
490
558
|
function buildResponseFromState(state, useNativeStructuredOutput = false) {
|
|
491
559
|
const textContent = [];
|
|
@@ -529,11 +597,12 @@ ${block.fileContent}\`\`\`
|
|
|
529
597
|
` });
|
|
530
598
|
}
|
|
531
599
|
}
|
|
600
|
+
const messageId = state.messageId || generateId();
|
|
532
601
|
const message = new AssistantMessage(
|
|
533
602
|
textContent,
|
|
534
603
|
toolCalls.length > 0 ? toolCalls : void 0,
|
|
535
604
|
{
|
|
536
|
-
id:
|
|
605
|
+
id: messageId,
|
|
537
606
|
metadata: {
|
|
538
607
|
anthropic: {
|
|
539
608
|
stop_reason: state.stopReason,
|
|
@@ -636,7 +705,7 @@ function createLLMHandler() {
|
|
|
636
705
|
"anthropic",
|
|
637
706
|
"llm"
|
|
638
707
|
);
|
|
639
|
-
const data = await response
|
|
708
|
+
const data = await parseJsonResponse(response, "anthropic", "llm");
|
|
640
709
|
return transformResponse(data, useNativeStructuredOutput);
|
|
641
710
|
},
|
|
642
711
|
stream(request) {
|
|
@@ -665,7 +734,8 @@ function createLLMHandler() {
|
|
|
665
734
|
const headers = {
|
|
666
735
|
"Content-Type": "application/json",
|
|
667
736
|
"x-api-key": apiKey,
|
|
668
|
-
"anthropic-version": request.config.apiVersion ?? ANTHROPIC_VERSION
|
|
737
|
+
"anthropic-version": request.config.apiVersion ?? ANTHROPIC_VERSION,
|
|
738
|
+
Accept: "text/event-stream"
|
|
669
739
|
};
|
|
670
740
|
if (request.config.headers) {
|
|
671
741
|
for (const [key, value] of Object.entries(request.config.headers)) {
|
|
@@ -714,16 +784,17 @@ function createLLMHandler() {
|
|
|
714
784
|
responseReject(error);
|
|
715
785
|
throw error;
|
|
716
786
|
}
|
|
717
|
-
const
|
|
718
|
-
|
|
787
|
+
const uppEvents = transformStreamEvent(event, state);
|
|
788
|
+
for (const uppEvent of uppEvents) {
|
|
719
789
|
yield uppEvent;
|
|
720
790
|
}
|
|
721
791
|
}
|
|
722
792
|
}
|
|
723
793
|
responseResolve(buildResponseFromState(state, useNativeStructuredOutput));
|
|
724
794
|
} catch (error) {
|
|
725
|
-
|
|
726
|
-
|
|
795
|
+
const err = toError(error);
|
|
796
|
+
responseReject(err);
|
|
797
|
+
throw err;
|
|
727
798
|
}
|
|
728
799
|
}
|
|
729
800
|
return {
|