@ai-sdk/xai 2.0.48 → 2.0.50
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 +112 -28
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +113 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
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
|
|
|
@@ -452,14 +457,15 @@ var XaiChatLanguageModel = class {
|
|
|
452
457
|
};
|
|
453
458
|
}
|
|
454
459
|
async doGenerate(options) {
|
|
455
|
-
var _a, _b, _c, _d, _e;
|
|
460
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
456
461
|
const { args: body, warnings } = await this.getArgs(options);
|
|
462
|
+
const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
|
|
457
463
|
const {
|
|
458
464
|
responseHeaders,
|
|
459
465
|
value: response,
|
|
460
466
|
rawValue: rawResponse
|
|
461
467
|
} = await postJsonToApi({
|
|
462
|
-
url
|
|
468
|
+
url,
|
|
463
469
|
headers: combineHeaders(this.config.headers(), options.headers),
|
|
464
470
|
body,
|
|
465
471
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
@@ -469,6 +475,27 @@ var XaiChatLanguageModel = class {
|
|
|
469
475
|
abortSignal: options.abortSignal,
|
|
470
476
|
fetch: this.config.fetch
|
|
471
477
|
});
|
|
478
|
+
if (response.error != null) {
|
|
479
|
+
throw new APICallError({
|
|
480
|
+
message: response.error,
|
|
481
|
+
url,
|
|
482
|
+
requestBodyValues: body,
|
|
483
|
+
statusCode: 200,
|
|
484
|
+
responseHeaders,
|
|
485
|
+
responseBody: JSON.stringify(rawResponse),
|
|
486
|
+
isRetryable: response.code === "The service is currently unavailable"
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
if (!response.choices || response.choices.length === 0) {
|
|
490
|
+
throw new APICallError({
|
|
491
|
+
message: "No choices returned from the API",
|
|
492
|
+
url,
|
|
493
|
+
requestBodyValues: body,
|
|
494
|
+
statusCode: 200,
|
|
495
|
+
responseHeaders,
|
|
496
|
+
responseBody: JSON.stringify(rawResponse)
|
|
497
|
+
});
|
|
498
|
+
}
|
|
472
499
|
const choice = response.choices[0];
|
|
473
500
|
const content = [];
|
|
474
501
|
if (choice.message.content != null && choice.message.content.length > 0) {
|
|
@@ -498,12 +525,12 @@ var XaiChatLanguageModel = class {
|
|
|
498
525
|
}
|
|
499
526
|
}
|
|
500
527
|
if (response.citations != null) {
|
|
501
|
-
for (const
|
|
528
|
+
for (const url2 of response.citations) {
|
|
502
529
|
content.push({
|
|
503
530
|
type: "source",
|
|
504
531
|
sourceType: "url",
|
|
505
532
|
id: this.config.generateId(),
|
|
506
|
-
url
|
|
533
|
+
url: url2
|
|
507
534
|
});
|
|
508
535
|
}
|
|
509
536
|
}
|
|
@@ -511,11 +538,11 @@ var XaiChatLanguageModel = class {
|
|
|
511
538
|
content,
|
|
512
539
|
finishReason: mapXaiFinishReason(choice.finish_reason),
|
|
513
540
|
usage: {
|
|
514
|
-
inputTokens: response.usage.prompt_tokens,
|
|
515
|
-
outputTokens: response.usage.completion_tokens,
|
|
516
|
-
totalTokens: response.usage.total_tokens,
|
|
517
|
-
reasoningTokens: (
|
|
518
|
-
cachedInputTokens: (
|
|
541
|
+
inputTokens: (_b = response.usage) == null ? void 0 : _b.prompt_tokens,
|
|
542
|
+
outputTokens: (_c = response.usage) == null ? void 0 : _c.completion_tokens,
|
|
543
|
+
totalTokens: (_d = response.usage) == null ? void 0 : _d.total_tokens,
|
|
544
|
+
reasoningTokens: (_g = (_f = (_e = response.usage) == null ? void 0 : _e.completion_tokens_details) == null ? void 0 : _f.reasoning_tokens) != null ? _g : void 0,
|
|
545
|
+
cachedInputTokens: (_j = (_i = (_h = response.usage) == null ? void 0 : _h.prompt_tokens_details) == null ? void 0 : _i.cached_tokens) != null ? _j : void 0
|
|
519
546
|
},
|
|
520
547
|
request: { body },
|
|
521
548
|
response: {
|
|
@@ -536,12 +563,47 @@ var XaiChatLanguageModel = class {
|
|
|
536
563
|
include_usage: true
|
|
537
564
|
}
|
|
538
565
|
};
|
|
566
|
+
const url = `${(_a = this.config.baseURL) != null ? _a : "https://api.x.ai/v1"}/chat/completions`;
|
|
539
567
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
540
|
-
url
|
|
568
|
+
url,
|
|
541
569
|
headers: combineHeaders(this.config.headers(), options.headers),
|
|
542
570
|
body,
|
|
543
571
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
544
|
-
successfulResponseHandler:
|
|
572
|
+
successfulResponseHandler: async ({ response: response2 }) => {
|
|
573
|
+
const responseHeaders2 = extractResponseHeaders(response2);
|
|
574
|
+
const contentType = response2.headers.get("content-type");
|
|
575
|
+
if (contentType == null ? void 0 : contentType.includes("application/json")) {
|
|
576
|
+
const responseBody = await response2.text();
|
|
577
|
+
const parsedError = await safeParseJSON({
|
|
578
|
+
text: responseBody,
|
|
579
|
+
schema: xaiStreamErrorSchema
|
|
580
|
+
});
|
|
581
|
+
if (parsedError.success) {
|
|
582
|
+
throw new APICallError({
|
|
583
|
+
message: parsedError.value.error,
|
|
584
|
+
url,
|
|
585
|
+
requestBodyValues: body,
|
|
586
|
+
statusCode: 200,
|
|
587
|
+
responseHeaders: responseHeaders2,
|
|
588
|
+
responseBody,
|
|
589
|
+
isRetryable: parsedError.value.code === "The service is currently unavailable"
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
throw new APICallError({
|
|
593
|
+
message: "Invalid JSON response",
|
|
594
|
+
url,
|
|
595
|
+
requestBodyValues: body,
|
|
596
|
+
statusCode: 200,
|
|
597
|
+
responseHeaders: responseHeaders2,
|
|
598
|
+
responseBody
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
return createEventSourceResponseHandler(xaiChatChunkSchema)({
|
|
602
|
+
response: response2,
|
|
603
|
+
url,
|
|
604
|
+
requestBodyValues: body
|
|
605
|
+
});
|
|
606
|
+
},
|
|
545
607
|
abortSignal: options.abortSignal,
|
|
546
608
|
fetch: this.config.fetch
|
|
547
609
|
});
|
|
@@ -556,6 +618,7 @@ var XaiChatLanguageModel = class {
|
|
|
556
618
|
let isFirstChunk = true;
|
|
557
619
|
const contentBlocks = {};
|
|
558
620
|
const lastReasoningDeltas = {};
|
|
621
|
+
let activeReasoningBlockId = void 0;
|
|
559
622
|
const self = this;
|
|
560
623
|
return {
|
|
561
624
|
stream: response.pipeThrough(
|
|
@@ -581,12 +644,12 @@ var XaiChatLanguageModel = class {
|
|
|
581
644
|
isFirstChunk = false;
|
|
582
645
|
}
|
|
583
646
|
if (value.citations != null) {
|
|
584
|
-
for (const
|
|
647
|
+
for (const url2 of value.citations) {
|
|
585
648
|
controller.enqueue({
|
|
586
649
|
type: "source",
|
|
587
650
|
sourceType: "url",
|
|
588
651
|
id: self.config.generateId(),
|
|
589
|
-
url
|
|
652
|
+
url: url2
|
|
590
653
|
});
|
|
591
654
|
}
|
|
592
655
|
}
|
|
@@ -608,13 +671,21 @@ var XaiChatLanguageModel = class {
|
|
|
608
671
|
const choiceIndex = choice.index;
|
|
609
672
|
if (delta.content != null && delta.content.length > 0) {
|
|
610
673
|
const textContent = delta.content;
|
|
674
|
+
if (activeReasoningBlockId != null && !contentBlocks[activeReasoningBlockId].ended) {
|
|
675
|
+
controller.enqueue({
|
|
676
|
+
type: "reasoning-end",
|
|
677
|
+
id: activeReasoningBlockId
|
|
678
|
+
});
|
|
679
|
+
contentBlocks[activeReasoningBlockId].ended = true;
|
|
680
|
+
activeReasoningBlockId = void 0;
|
|
681
|
+
}
|
|
611
682
|
const lastMessage = body.messages[body.messages.length - 1];
|
|
612
683
|
if ((lastMessage == null ? void 0 : lastMessage.role) === "assistant" && textContent === lastMessage.content) {
|
|
613
684
|
return;
|
|
614
685
|
}
|
|
615
686
|
const blockId = `text-${value.id || choiceIndex}`;
|
|
616
687
|
if (contentBlocks[blockId] == null) {
|
|
617
|
-
contentBlocks[blockId] = { type: "text" };
|
|
688
|
+
contentBlocks[blockId] = { type: "text", ended: false };
|
|
618
689
|
controller.enqueue({
|
|
619
690
|
type: "text-start",
|
|
620
691
|
id: blockId
|
|
@@ -633,7 +704,8 @@ var XaiChatLanguageModel = class {
|
|
|
633
704
|
}
|
|
634
705
|
lastReasoningDeltas[blockId] = delta.reasoning_content;
|
|
635
706
|
if (contentBlocks[blockId] == null) {
|
|
636
|
-
contentBlocks[blockId] = { type: "reasoning" };
|
|
707
|
+
contentBlocks[blockId] = { type: "reasoning", ended: false };
|
|
708
|
+
activeReasoningBlockId = blockId;
|
|
637
709
|
controller.enqueue({
|
|
638
710
|
type: "reasoning-start",
|
|
639
711
|
id: blockId
|
|
@@ -646,6 +718,14 @@ var XaiChatLanguageModel = class {
|
|
|
646
718
|
});
|
|
647
719
|
}
|
|
648
720
|
if (delta.tool_calls != null) {
|
|
721
|
+
if (activeReasoningBlockId != null && !contentBlocks[activeReasoningBlockId].ended) {
|
|
722
|
+
controller.enqueue({
|
|
723
|
+
type: "reasoning-end",
|
|
724
|
+
id: activeReasoningBlockId
|
|
725
|
+
});
|
|
726
|
+
contentBlocks[activeReasoningBlockId].ended = true;
|
|
727
|
+
activeReasoningBlockId = void 0;
|
|
728
|
+
}
|
|
649
729
|
for (const toolCall of delta.tool_calls) {
|
|
650
730
|
const toolCallId = toolCall.id;
|
|
651
731
|
controller.enqueue({
|
|
@@ -673,10 +753,12 @@ var XaiChatLanguageModel = class {
|
|
|
673
753
|
},
|
|
674
754
|
flush(controller) {
|
|
675
755
|
for (const [blockId, block] of Object.entries(contentBlocks)) {
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
756
|
+
if (!block.ended) {
|
|
757
|
+
controller.enqueue({
|
|
758
|
+
type: block.type === "text" ? "text-end" : "reasoning-end",
|
|
759
|
+
id: blockId
|
|
760
|
+
});
|
|
761
|
+
}
|
|
680
762
|
}
|
|
681
763
|
controller.enqueue({ type: "finish", finishReason, usage });
|
|
682
764
|
}
|
|
@@ -728,10 +810,12 @@ var xaiChatResponseSchema = z3.object({
|
|
|
728
810
|
index: z3.number(),
|
|
729
811
|
finish_reason: z3.string().nullish()
|
|
730
812
|
})
|
|
731
|
-
),
|
|
732
|
-
object: z3.literal("chat.completion"),
|
|
733
|
-
usage: xaiUsageSchema,
|
|
734
|
-
citations: z3.array(z3.string().url()).nullish()
|
|
813
|
+
).nullish(),
|
|
814
|
+
object: z3.literal("chat.completion").nullish(),
|
|
815
|
+
usage: xaiUsageSchema.nullish(),
|
|
816
|
+
citations: z3.array(z3.string().url()).nullish(),
|
|
817
|
+
code: z3.string().nullish(),
|
|
818
|
+
error: z3.string().nullish()
|
|
735
819
|
});
|
|
736
820
|
var xaiChatChunkSchema = z3.object({
|
|
737
821
|
id: z3.string().nullish(),
|
|
@@ -761,6 +845,10 @@ var xaiChatChunkSchema = z3.object({
|
|
|
761
845
|
usage: xaiUsageSchema.nullish(),
|
|
762
846
|
citations: z3.array(z3.string().url()).nullish()
|
|
763
847
|
});
|
|
848
|
+
var xaiStreamErrorSchema = z3.object({
|
|
849
|
+
code: z3.string(),
|
|
850
|
+
error: z3.string()
|
|
851
|
+
});
|
|
764
852
|
|
|
765
853
|
// src/responses/xai-responses-language-model.ts
|
|
766
854
|
import {
|
|
@@ -2025,7 +2113,7 @@ var xaiTools = {
|
|
|
2025
2113
|
};
|
|
2026
2114
|
|
|
2027
2115
|
// src/version.ts
|
|
2028
|
-
var VERSION = true ? "2.0.
|
|
2116
|
+
var VERSION = true ? "2.0.50" : "0.0.0-test";
|
|
2029
2117
|
|
|
2030
2118
|
// src/xai-provider.ts
|
|
2031
2119
|
var xaiErrorStructure = {
|