@ai-sdk/xai 3.0.11 → 3.0.13
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/CHANGELOG.md +12 -0
- package/dist/index.js +97 -22
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +98 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -13,12 +13,17 @@ import {
|
|
|
13
13
|
} from "@ai-sdk/provider-utils";
|
|
14
14
|
|
|
15
15
|
// src/xai-chat-language-model.ts
|
|
16
|
+
import {
|
|
17
|
+
APICallError
|
|
18
|
+
} from "@ai-sdk/provider";
|
|
16
19
|
import {
|
|
17
20
|
combineHeaders,
|
|
18
21
|
createEventSourceResponseHandler,
|
|
19
22
|
createJsonResponseHandler,
|
|
23
|
+
extractResponseHeaders,
|
|
20
24
|
parseProviderOptions,
|
|
21
|
-
postJsonToApi
|
|
25
|
+
postJsonToApi,
|
|
26
|
+
safeParseJSON
|
|
22
27
|
} from "@ai-sdk/provider-utils";
|
|
23
28
|
import { z as z3 } from "zod/v4";
|
|
24
29
|
|
|
@@ -473,12 +478,13 @@ var XaiChatLanguageModel = class {
|
|
|
473
478
|
async doGenerate(options) {
|
|
474
479
|
var _a, _b;
|
|
475
480
|
const { args: body, warnings } = await this.getArgs(options);
|
|
481
|
+
const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
|
|
476
482
|
const {
|
|
477
483
|
responseHeaders,
|
|
478
484
|
value: response,
|
|
479
485
|
rawValue: rawResponse
|
|
480
486
|
} = await postJsonToApi({
|
|
481
|
-
url
|
|
487
|
+
url,
|
|
482
488
|
headers: combineHeaders(this.config.headers(), options.headers),
|
|
483
489
|
body,
|
|
484
490
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
@@ -488,6 +494,17 @@ var XaiChatLanguageModel = class {
|
|
|
488
494
|
abortSignal: options.abortSignal,
|
|
489
495
|
fetch: this.config.fetch
|
|
490
496
|
});
|
|
497
|
+
if (response.error != null) {
|
|
498
|
+
throw new APICallError({
|
|
499
|
+
message: response.error,
|
|
500
|
+
url,
|
|
501
|
+
requestBodyValues: body,
|
|
502
|
+
statusCode: 200,
|
|
503
|
+
responseHeaders,
|
|
504
|
+
responseBody: JSON.stringify(rawResponse),
|
|
505
|
+
isRetryable: response.code === "The service is currently unavailable"
|
|
506
|
+
});
|
|
507
|
+
}
|
|
491
508
|
const choice = response.choices[0];
|
|
492
509
|
const content = [];
|
|
493
510
|
if (choice.message.content != null && choice.message.content.length > 0) {
|
|
@@ -517,12 +534,12 @@ var XaiChatLanguageModel = class {
|
|
|
517
534
|
}
|
|
518
535
|
}
|
|
519
536
|
if (response.citations != null) {
|
|
520
|
-
for (const
|
|
537
|
+
for (const url2 of response.citations) {
|
|
521
538
|
content.push({
|
|
522
539
|
type: "source",
|
|
523
540
|
sourceType: "url",
|
|
524
541
|
id: this.config.generateId(),
|
|
525
|
-
url
|
|
542
|
+
url: url2
|
|
526
543
|
});
|
|
527
544
|
}
|
|
528
545
|
}
|
|
@@ -533,6 +550,7 @@ var XaiChatLanguageModel = class {
|
|
|
533
550
|
raw: (_b = choice.finish_reason) != null ? _b : void 0
|
|
534
551
|
},
|
|
535
552
|
usage: convertXaiChatUsage(response.usage),
|
|
553
|
+
// defined when there is no error
|
|
536
554
|
request: { body },
|
|
537
555
|
response: {
|
|
538
556
|
...getResponseMetadata(response),
|
|
@@ -552,12 +570,47 @@ var XaiChatLanguageModel = class {
|
|
|
552
570
|
include_usage: true
|
|
553
571
|
}
|
|
554
572
|
};
|
|
573
|
+
const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
|
|
555
574
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
556
|
-
url
|
|
575
|
+
url,
|
|
557
576
|
headers: combineHeaders(this.config.headers(), options.headers),
|
|
558
577
|
body,
|
|
559
578
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
560
|
-
successfulResponseHandler:
|
|
579
|
+
successfulResponseHandler: async ({ response: response2 }) => {
|
|
580
|
+
const responseHeaders2 = extractResponseHeaders(response2);
|
|
581
|
+
const contentType = response2.headers.get("content-type");
|
|
582
|
+
if (contentType == null ? void 0 : contentType.includes("application/json")) {
|
|
583
|
+
const responseBody = await response2.text();
|
|
584
|
+
const parsedError = await safeParseJSON({
|
|
585
|
+
text: responseBody,
|
|
586
|
+
schema: xaiStreamErrorSchema
|
|
587
|
+
});
|
|
588
|
+
if (parsedError.success) {
|
|
589
|
+
throw new APICallError({
|
|
590
|
+
message: parsedError.value.error,
|
|
591
|
+
url,
|
|
592
|
+
requestBodyValues: body,
|
|
593
|
+
statusCode: 200,
|
|
594
|
+
responseHeaders: responseHeaders2,
|
|
595
|
+
responseBody,
|
|
596
|
+
isRetryable: parsedError.value.code === "The service is currently unavailable"
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
throw new APICallError({
|
|
600
|
+
message: "Invalid JSON response",
|
|
601
|
+
url,
|
|
602
|
+
requestBodyValues: body,
|
|
603
|
+
statusCode: 200,
|
|
604
|
+
responseHeaders: responseHeaders2,
|
|
605
|
+
responseBody
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
return createEventSourceResponseHandler(xaiChatChunkSchema)({
|
|
609
|
+
response: response2,
|
|
610
|
+
url,
|
|
611
|
+
requestBodyValues: body
|
|
612
|
+
});
|
|
613
|
+
},
|
|
561
614
|
abortSignal: options.abortSignal,
|
|
562
615
|
fetch: this.config.fetch
|
|
563
616
|
});
|
|
@@ -569,6 +622,7 @@ var XaiChatLanguageModel = class {
|
|
|
569
622
|
let isFirstChunk = true;
|
|
570
623
|
const contentBlocks = {};
|
|
571
624
|
const lastReasoningDeltas = {};
|
|
625
|
+
let activeReasoningBlockId = void 0;
|
|
572
626
|
const self = this;
|
|
573
627
|
return {
|
|
574
628
|
stream: response.pipeThrough(
|
|
@@ -593,12 +647,12 @@ var XaiChatLanguageModel = class {
|
|
|
593
647
|
isFirstChunk = false;
|
|
594
648
|
}
|
|
595
649
|
if (value.citations != null) {
|
|
596
|
-
for (const
|
|
650
|
+
for (const url2 of value.citations) {
|
|
597
651
|
controller.enqueue({
|
|
598
652
|
type: "source",
|
|
599
653
|
sourceType: "url",
|
|
600
654
|
id: self.config.generateId(),
|
|
601
|
-
url
|
|
655
|
+
url: url2
|
|
602
656
|
});
|
|
603
657
|
}
|
|
604
658
|
}
|
|
@@ -619,13 +673,21 @@ var XaiChatLanguageModel = class {
|
|
|
619
673
|
const choiceIndex = choice.index;
|
|
620
674
|
if (delta.content != null && delta.content.length > 0) {
|
|
621
675
|
const textContent = delta.content;
|
|
676
|
+
if (activeReasoningBlockId != null && !contentBlocks[activeReasoningBlockId].ended) {
|
|
677
|
+
controller.enqueue({
|
|
678
|
+
type: "reasoning-end",
|
|
679
|
+
id: activeReasoningBlockId
|
|
680
|
+
});
|
|
681
|
+
contentBlocks[activeReasoningBlockId].ended = true;
|
|
682
|
+
activeReasoningBlockId = void 0;
|
|
683
|
+
}
|
|
622
684
|
const lastMessage = body.messages[body.messages.length - 1];
|
|
623
685
|
if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && textContent === lastMessage.content) {
|
|
624
686
|
return;
|
|
625
687
|
}
|
|
626
688
|
const blockId = `text-${value.id || choiceIndex}`;
|
|
627
689
|
if (contentBlocks[blockId] == null) {
|
|
628
|
-
contentBlocks[blockId] = { type: "text" };
|
|
690
|
+
contentBlocks[blockId] = { type: "text", ended: false };
|
|
629
691
|
controller.enqueue({
|
|
630
692
|
type: "text-start",
|
|
631
693
|
id: blockId
|
|
@@ -644,7 +706,8 @@ var XaiChatLanguageModel = class {
|
|
|
644
706
|
}
|
|
645
707
|
lastReasoningDeltas[blockId] = delta.reasoning_content;
|
|
646
708
|
if (contentBlocks[blockId] == null) {
|
|
647
|
-
contentBlocks[blockId] = { type: "reasoning" };
|
|
709
|
+
contentBlocks[blockId] = { type: "reasoning", ended: false };
|
|
710
|
+
activeReasoningBlockId = blockId;
|
|
648
711
|
controller.enqueue({
|
|
649
712
|
type: "reasoning-start",
|
|
650
713
|
id: blockId
|
|
@@ -657,6 +720,14 @@ var XaiChatLanguageModel = class {
|
|
|
657
720
|
});
|
|
658
721
|
}
|
|
659
722
|
if (delta.tool_calls != null) {
|
|
723
|
+
if (activeReasoningBlockId != null && !contentBlocks[activeReasoningBlockId].ended) {
|
|
724
|
+
controller.enqueue({
|
|
725
|
+
type: "reasoning-end",
|
|
726
|
+
id: activeReasoningBlockId
|
|
727
|
+
});
|
|
728
|
+
contentBlocks[activeReasoningBlockId].ended = true;
|
|
729
|
+
activeReasoningBlockId = void 0;
|
|
730
|
+
}
|
|
660
731
|
for (const toolCall of delta.tool_calls) {
|
|
661
732
|
const toolCallId = toolCall.id;
|
|
662
733
|
controller.enqueue({
|
|
@@ -684,10 +755,12 @@ var XaiChatLanguageModel = class {
|
|
|
684
755
|
},
|
|
685
756
|
flush(controller) {
|
|
686
757
|
for (const [blockId, block] of Object.entries(contentBlocks)) {
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
758
|
+
if (!block.ended) {
|
|
759
|
+
controller.enqueue({
|
|
760
|
+
type: block.type === "text" ? "text-end" : "reasoning-end",
|
|
761
|
+
id: blockId
|
|
762
|
+
});
|
|
763
|
+
}
|
|
691
764
|
}
|
|
692
765
|
controller.enqueue({ type: "finish", finishReason, usage });
|
|
693
766
|
}
|
|
@@ -739,10 +812,12 @@ var xaiChatResponseSchema = z3.object({
|
|
|
739
812
|
index: z3.number(),
|
|
740
813
|
finish_reason: z3.string().nullish()
|
|
741
814
|
})
|
|
742
|
-
),
|
|
743
|
-
object: z3.literal("chat.completion"),
|
|
744
|
-
usage: xaiUsageSchema,
|
|
745
|
-
citations: z3.array(z3.string().url()).nullish()
|
|
815
|
+
).nullish(),
|
|
816
|
+
object: z3.literal("chat.completion").nullish(),
|
|
817
|
+
usage: xaiUsageSchema.nullish(),
|
|
818
|
+
citations: z3.array(z3.string().url()).nullish(),
|
|
819
|
+
code: z3.string().nullish(),
|
|
820
|
+
error: z3.string().nullish()
|
|
746
821
|
});
|
|
747
822
|
var xaiChatChunkSchema = z3.object({
|
|
748
823
|
id: z3.string().nullish(),
|
|
@@ -772,6 +847,10 @@ var xaiChatChunkSchema = z3.object({
|
|
|
772
847
|
usage: xaiUsageSchema.nullish(),
|
|
773
848
|
citations: z3.array(z3.string().url()).nullish()
|
|
774
849
|
});
|
|
850
|
+
var xaiStreamErrorSchema = z3.object({
|
|
851
|
+
code: z3.string(),
|
|
852
|
+
error: z3.string()
|
|
853
|
+
});
|
|
775
854
|
|
|
776
855
|
// src/responses/xai-responses-language-model.ts
|
|
777
856
|
import {
|
|
@@ -2056,7 +2135,7 @@ var xaiTools = {
|
|
|
2056
2135
|
};
|
|
2057
2136
|
|
|
2058
2137
|
// src/version.ts
|
|
2059
|
-
var VERSION = true ? "3.0.
|
|
2138
|
+
var VERSION = true ? "3.0.13" : "0.0.0-test";
|
|
2060
2139
|
|
|
2061
2140
|
// src/xai-provider.ts
|
|
2062
2141
|
var xaiErrorStructure = {
|