@ai-sdk/xai 4.0.0-beta.6 → 4.0.0-beta.75
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 +667 -9
- package/README.md +2 -0
- package/dist/index.d.ts +218 -68
- package/dist/index.js +2081 -779
- package/dist/index.js.map +1 -1
- package/docs/01-xai.mdx +445 -54
- package/package.json +15 -15
- package/src/convert-to-xai-chat-messages.ts +48 -27
- package/src/convert-xai-chat-usage.ts +3 -3
- package/src/files/xai-files-api.ts +16 -0
- package/src/files/xai-files-options.ts +19 -0
- package/src/files/xai-files.ts +94 -0
- package/src/index.ts +9 -4
- package/src/map-xai-finish-reason.ts +2 -2
- package/src/realtime/index.ts +2 -0
- package/src/realtime/xai-realtime-event-mapper.ts +399 -0
- package/src/realtime/xai-realtime-model-options.ts +3 -0
- package/src/realtime/xai-realtime-model.ts +101 -0
- package/src/remove-additional-properties.ts +24 -0
- package/src/responses/convert-to-xai-responses-input.ts +100 -23
- package/src/responses/convert-xai-responses-usage.ts +3 -3
- package/src/responses/map-xai-responses-finish-reason.ts +3 -2
- package/src/responses/xai-responses-api.ts +34 -1
- package/src/responses/{xai-responses-options.ts → xai-responses-language-model-options.ts} +13 -7
- package/src/responses/xai-responses-language-model.ts +173 -64
- package/src/responses/xai-responses-prepare-tools.ts +10 -8
- package/src/tool/code-execution.ts +2 -2
- package/src/tool/file-search.ts +2 -2
- package/src/tool/mcp-server.ts +2 -2
- package/src/tool/view-image.ts +2 -2
- package/src/tool/view-x-video.ts +2 -2
- package/src/tool/web-search.ts +4 -2
- package/src/tool/x-search.ts +2 -2
- package/src/{xai-chat-options.ts → xai-chat-language-model-options.ts} +28 -13
- package/src/xai-chat-language-model.ts +65 -29
- package/src/xai-chat-prompt.ts +2 -1
- package/src/xai-error.ts +13 -3
- package/src/xai-image-model.ts +28 -11
- package/src/xai-prepare-tools.ts +9 -8
- package/src/xai-provider.ts +115 -19
- package/src/xai-speech-model-options.ts +55 -0
- package/src/xai-speech-model.ts +167 -0
- package/src/xai-transcription-model-options.ts +70 -0
- package/src/xai-transcription-model.ts +166 -0
- package/src/xai-video-model-options.ts +145 -0
- package/src/xai-video-model.ts +129 -22
- package/dist/index.d.mts +0 -372
- package/dist/index.mjs +0 -3061
- package/dist/index.mjs.map +0 -1
- package/src/xai-video-options.ts +0 -23
- /package/src/{xai-image-options.ts → xai-image-model-options.ts} +0 -0
package/src/tool/view-x-video.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createProviderExecutedToolFactory } from '@ai-sdk/provider-utils';
|
|
2
2
|
import { z } from 'zod/v4';
|
|
3
3
|
|
|
4
4
|
const viewXVideoOutputSchema = z.object({
|
|
@@ -7,7 +7,7 @@ const viewXVideoOutputSchema = z.object({
|
|
|
7
7
|
duration: z.number().optional().describe('duration in seconds'),
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
const viewXVideoToolFactory =
|
|
10
|
+
const viewXVideoToolFactory = createProviderExecutedToolFactory({
|
|
11
11
|
id: 'xai.view_x_video',
|
|
12
12
|
inputSchema: z.object({}).describe('no input parameters'),
|
|
13
13
|
outputSchema: viewXVideoOutputSchema,
|
package/src/tool/web-search.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
createProviderExecutedToolFactory,
|
|
3
3
|
lazySchema,
|
|
4
4
|
zodSchema,
|
|
5
5
|
} from '@ai-sdk/provider-utils';
|
|
@@ -10,6 +10,7 @@ export const webSearchArgsSchema = lazySchema(() =>
|
|
|
10
10
|
z.object({
|
|
11
11
|
allowedDomains: z.array(z.string()).max(5).optional(),
|
|
12
12
|
excludedDomains: z.array(z.string()).max(5).optional(),
|
|
13
|
+
enableImageSearch: z.boolean().optional(),
|
|
13
14
|
enableImageUnderstanding: z.boolean().optional(),
|
|
14
15
|
}),
|
|
15
16
|
),
|
|
@@ -30,7 +31,7 @@ const webSearchOutputSchema = lazySchema(() =>
|
|
|
30
31
|
),
|
|
31
32
|
);
|
|
32
33
|
|
|
33
|
-
const webSearchToolFactory =
|
|
34
|
+
const webSearchToolFactory = createProviderExecutedToolFactory<
|
|
34
35
|
{},
|
|
35
36
|
{
|
|
36
37
|
query: string;
|
|
@@ -43,6 +44,7 @@ const webSearchToolFactory = createProviderToolFactoryWithOutputSchema<
|
|
|
43
44
|
{
|
|
44
45
|
allowedDomains?: string[];
|
|
45
46
|
excludedDomains?: string[];
|
|
47
|
+
enableImageSearch?: boolean;
|
|
46
48
|
enableImageUnderstanding?: boolean;
|
|
47
49
|
}
|
|
48
50
|
>({
|
package/src/tool/x-search.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
createProviderExecutedToolFactory,
|
|
3
3
|
lazySchema,
|
|
4
4
|
zodSchema,
|
|
5
5
|
} from '@ai-sdk/provider-utils';
|
|
@@ -34,7 +34,7 @@ const xSearchOutputSchema = lazySchema(() =>
|
|
|
34
34
|
),
|
|
35
35
|
);
|
|
36
36
|
|
|
37
|
-
const xSearchToolFactory =
|
|
37
|
+
const xSearchToolFactory = createProviderExecutedToolFactory<
|
|
38
38
|
{},
|
|
39
39
|
{
|
|
40
40
|
query: string;
|
|
@@ -2,18 +2,10 @@ import { z } from 'zod/v4';
|
|
|
2
2
|
|
|
3
3
|
// https://docs.x.ai/docs/models
|
|
4
4
|
export type XaiChatModelId =
|
|
5
|
-
| 'grok-4-
|
|
6
|
-
| 'grok-4-
|
|
7
|
-
| 'grok-4
|
|
8
|
-
| 'grok-
|
|
9
|
-
| 'grok-code-fast-1'
|
|
10
|
-
| 'grok-4'
|
|
11
|
-
| 'grok-4-0709'
|
|
12
|
-
| 'grok-4-latest'
|
|
13
|
-
| 'grok-3'
|
|
14
|
-
| 'grok-3-latest'
|
|
15
|
-
| 'grok-3-mini'
|
|
16
|
-
| 'grok-3-mini-latest'
|
|
5
|
+
| 'grok-4.20-non-reasoning'
|
|
6
|
+
| 'grok-4.20-reasoning'
|
|
7
|
+
| 'grok-4.3'
|
|
8
|
+
| 'grok-latest'
|
|
17
9
|
| (string & {});
|
|
18
10
|
|
|
19
11
|
// search source schemas
|
|
@@ -58,7 +50,21 @@ const searchSourceSchema = z.discriminatedUnion('type', [
|
|
|
58
50
|
|
|
59
51
|
// xai-specific provider options
|
|
60
52
|
export const xaiLanguageModelChatOptions = z.object({
|
|
61
|
-
|
|
53
|
+
/**
|
|
54
|
+
* Constrains how hard a reasoning model thinks before responding.
|
|
55
|
+
*
|
|
56
|
+
* - `none`: Disables reasoning entirely (supported by `grok-4.3` and newer
|
|
57
|
+
* reasoning models). When set, no thinking tokens are used.
|
|
58
|
+
* - `low` (default): Uses some reasoning tokens, but still fast.
|
|
59
|
+
* - `medium`: More thinking for less-latency-sensitive applications.
|
|
60
|
+
* - `high`: Uses more reasoning tokens for deeper thinking.
|
|
61
|
+
*
|
|
62
|
+
* Note: Not every Grok model accepts every value. Refer to xAI's docs for
|
|
63
|
+
* the values supported by your selected model.
|
|
64
|
+
*
|
|
65
|
+
* @see https://docs.x.ai/docs/guides/reasoning
|
|
66
|
+
*/
|
|
67
|
+
reasoningEffort: z.enum(['none', 'low', 'medium', 'high']).optional(),
|
|
62
68
|
logprobs: z.boolean().optional(),
|
|
63
69
|
topLogprobs: z.number().int().min(0).max(8).optional(),
|
|
64
70
|
|
|
@@ -70,6 +76,15 @@ export const xaiLanguageModelChatOptions = z.object({
|
|
|
70
76
|
*/
|
|
71
77
|
parallel_function_calling: z.boolean().optional(),
|
|
72
78
|
|
|
79
|
+
/**
|
|
80
|
+
* @deprecated xAI has deprecated Live Search (`search_parameters`) in favor
|
|
81
|
+
* of the Agent Tools API. Requests using this option now return a "Live
|
|
82
|
+
* search is deprecated" error. Use the `web_search` / `x_search` tools
|
|
83
|
+
* instead (e.g. `xai.tools.webSearch()`, `xai.tools.xSearch()`) with
|
|
84
|
+
* `xai.responses(modelId)`.
|
|
85
|
+
*
|
|
86
|
+
* @see https://docs.x.ai/docs/guides/tools/overview
|
|
87
|
+
*/
|
|
73
88
|
searchParameters: z
|
|
74
89
|
.object({
|
|
75
90
|
/**
|
|
@@ -1,25 +1,30 @@
|
|
|
1
1
|
import {
|
|
2
2
|
APICallError,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
type LanguageModelV4,
|
|
4
|
+
type LanguageModelV4CallOptions,
|
|
5
|
+
type LanguageModelV4Content,
|
|
6
|
+
type LanguageModelV4FinishReason,
|
|
7
|
+
type LanguageModelV4GenerateResult,
|
|
8
|
+
type LanguageModelV4StreamPart,
|
|
9
|
+
type LanguageModelV4StreamResult,
|
|
10
|
+
type LanguageModelV4Usage,
|
|
11
|
+
type SharedV4Warning,
|
|
12
12
|
} from '@ai-sdk/provider';
|
|
13
13
|
import {
|
|
14
14
|
combineHeaders,
|
|
15
15
|
createEventSourceResponseHandler,
|
|
16
16
|
createJsonResponseHandler,
|
|
17
17
|
extractResponseHeaders,
|
|
18
|
-
|
|
18
|
+
isCustomReasoning,
|
|
19
|
+
mapReasoningToProviderEffort,
|
|
19
20
|
parseProviderOptions,
|
|
20
|
-
ParseResult,
|
|
21
21
|
postJsonToApi,
|
|
22
22
|
safeParseJSON,
|
|
23
|
+
serializeModelOptions,
|
|
24
|
+
WORKFLOW_SERIALIZE,
|
|
25
|
+
WORKFLOW_DESERIALIZE,
|
|
26
|
+
type FetchFunction,
|
|
27
|
+
type ParseResult,
|
|
23
28
|
} from '@ai-sdk/provider-utils';
|
|
24
29
|
import { z } from 'zod/v4';
|
|
25
30
|
import { convertToXaiChatMessages } from './convert-to-xai-chat-messages';
|
|
@@ -27,27 +32,41 @@ import { convertXaiChatUsage } from './convert-xai-chat-usage';
|
|
|
27
32
|
import { getResponseMetadata } from './get-response-metadata';
|
|
28
33
|
import { mapXaiFinishReason } from './map-xai-finish-reason';
|
|
29
34
|
import {
|
|
30
|
-
XaiChatModelId,
|
|
31
35
|
xaiLanguageModelChatOptions,
|
|
32
|
-
|
|
36
|
+
type XaiChatModelId,
|
|
37
|
+
} from './xai-chat-language-model-options';
|
|
33
38
|
import { xaiFailedResponseHandler } from './xai-error';
|
|
34
39
|
import { prepareTools } from './xai-prepare-tools';
|
|
35
40
|
|
|
36
41
|
type XaiChatConfig = {
|
|
37
42
|
provider: string;
|
|
38
43
|
baseURL: string | undefined;
|
|
39
|
-
headers
|
|
44
|
+
headers?: () => Record<string, string | undefined>;
|
|
40
45
|
generateId: () => string;
|
|
41
46
|
fetch?: FetchFunction;
|
|
42
47
|
};
|
|
43
48
|
|
|
44
|
-
export class XaiChatLanguageModel implements
|
|
45
|
-
readonly specificationVersion = '
|
|
49
|
+
export class XaiChatLanguageModel implements LanguageModelV4 {
|
|
50
|
+
readonly specificationVersion = 'v4';
|
|
46
51
|
|
|
47
52
|
readonly modelId: XaiChatModelId;
|
|
48
53
|
|
|
49
54
|
private readonly config: XaiChatConfig;
|
|
50
55
|
|
|
56
|
+
static [WORKFLOW_SERIALIZE](model: XaiChatLanguageModel) {
|
|
57
|
+
return serializeModelOptions({
|
|
58
|
+
modelId: model.modelId,
|
|
59
|
+
config: model.config,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static [WORKFLOW_DESERIALIZE](options: {
|
|
64
|
+
modelId: XaiChatModelId;
|
|
65
|
+
config: XaiChatConfig;
|
|
66
|
+
}) {
|
|
67
|
+
return new XaiChatLanguageModel(options.modelId, options.config);
|
|
68
|
+
}
|
|
69
|
+
|
|
51
70
|
constructor(modelId: XaiChatModelId, config: XaiChatConfig) {
|
|
52
71
|
this.modelId = modelId;
|
|
53
72
|
this.config = config;
|
|
@@ -71,12 +90,13 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
|
|
|
71
90
|
presencePenalty,
|
|
72
91
|
stopSequences,
|
|
73
92
|
seed,
|
|
93
|
+
reasoning,
|
|
74
94
|
responseFormat,
|
|
75
95
|
providerOptions,
|
|
76
96
|
tools,
|
|
77
97
|
toolChoice,
|
|
78
|
-
}:
|
|
79
|
-
const warnings:
|
|
98
|
+
}: LanguageModelV4CallOptions) {
|
|
99
|
+
const warnings: SharedV4Warning[] = [];
|
|
80
100
|
|
|
81
101
|
// parse xai-specific provider options
|
|
82
102
|
const options =
|
|
@@ -133,7 +153,23 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
|
|
|
133
153
|
temperature,
|
|
134
154
|
top_p: topP,
|
|
135
155
|
seed,
|
|
136
|
-
reasoning_effort:
|
|
156
|
+
reasoning_effort:
|
|
157
|
+
options.reasoningEffort ??
|
|
158
|
+
(isCustomReasoning(reasoning)
|
|
159
|
+
? reasoning === 'none'
|
|
160
|
+
? undefined
|
|
161
|
+
: mapReasoningToProviderEffort({
|
|
162
|
+
reasoning,
|
|
163
|
+
effortMap: {
|
|
164
|
+
minimal: 'low',
|
|
165
|
+
low: 'low',
|
|
166
|
+
medium: 'medium',
|
|
167
|
+
high: 'high',
|
|
168
|
+
xhigh: 'high',
|
|
169
|
+
},
|
|
170
|
+
warnings,
|
|
171
|
+
})
|
|
172
|
+
: undefined),
|
|
137
173
|
|
|
138
174
|
// parallel function calling
|
|
139
175
|
parallel_function_calling: options.parallel_function_calling,
|
|
@@ -202,8 +238,8 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
|
|
|
202
238
|
}
|
|
203
239
|
|
|
204
240
|
async doGenerate(
|
|
205
|
-
options:
|
|
206
|
-
): Promise<
|
|
241
|
+
options: LanguageModelV4CallOptions,
|
|
242
|
+
): Promise<LanguageModelV4GenerateResult> {
|
|
207
243
|
const { args: body, warnings } = await this.getArgs(options);
|
|
208
244
|
|
|
209
245
|
const url = `${this.config.baseURL ?? 'https://api.x.ai/v1'}/chat/completions`;
|
|
@@ -214,7 +250,7 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
|
|
|
214
250
|
rawValue: rawResponse,
|
|
215
251
|
} = await postJsonToApi({
|
|
216
252
|
url,
|
|
217
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
253
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
218
254
|
body,
|
|
219
255
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
220
256
|
successfulResponseHandler: createJsonResponseHandler(
|
|
@@ -237,7 +273,7 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
|
|
|
237
273
|
}
|
|
238
274
|
|
|
239
275
|
const choice = response.choices![0];
|
|
240
|
-
const content: Array<
|
|
276
|
+
const content: Array<LanguageModelV4Content> = [];
|
|
241
277
|
|
|
242
278
|
// extract text content
|
|
243
279
|
if (choice.message.content != null && choice.message.content.length > 0) {
|
|
@@ -312,8 +348,8 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
|
|
|
312
348
|
}
|
|
313
349
|
|
|
314
350
|
async doStream(
|
|
315
|
-
options:
|
|
316
|
-
): Promise<
|
|
351
|
+
options: LanguageModelV4CallOptions,
|
|
352
|
+
): Promise<LanguageModelV4StreamResult> {
|
|
317
353
|
const { args, warnings } = await this.getArgs(options);
|
|
318
354
|
const body = {
|
|
319
355
|
...args,
|
|
@@ -327,7 +363,7 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
|
|
|
327
363
|
|
|
328
364
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
329
365
|
url,
|
|
330
|
-
headers: combineHeaders(this.config.headers(), options.headers),
|
|
366
|
+
headers: combineHeaders(this.config.headers?.(), options.headers),
|
|
331
367
|
body,
|
|
332
368
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
333
369
|
successfulResponseHandler: async ({ response }) => {
|
|
@@ -375,11 +411,11 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
|
|
|
375
411
|
fetch: this.config.fetch,
|
|
376
412
|
});
|
|
377
413
|
|
|
378
|
-
let finishReason:
|
|
414
|
+
let finishReason: LanguageModelV4FinishReason = {
|
|
379
415
|
unified: 'other',
|
|
380
416
|
raw: undefined,
|
|
381
417
|
};
|
|
382
|
-
let usage:
|
|
418
|
+
let usage: LanguageModelV4Usage | undefined = undefined;
|
|
383
419
|
let isFirstChunk = true;
|
|
384
420
|
const contentBlocks: Record<
|
|
385
421
|
string,
|
|
@@ -394,7 +430,7 @@ export class XaiChatLanguageModel implements LanguageModelV3 {
|
|
|
394
430
|
stream: response.pipeThrough(
|
|
395
431
|
new TransformStream<
|
|
396
432
|
ParseResult<z.infer<typeof xaiChatChunkSchema>>,
|
|
397
|
-
|
|
433
|
+
LanguageModelV4StreamPart
|
|
398
434
|
>({
|
|
399
435
|
start(controller) {
|
|
400
436
|
controller.enqueue({ type: 'stream-start', warnings });
|
package/src/xai-chat-prompt.ts
CHANGED
|
@@ -18,7 +18,8 @@ export interface XaiUserMessage {
|
|
|
18
18
|
|
|
19
19
|
export type XaiUserMessageContent =
|
|
20
20
|
| { type: 'text'; text: string }
|
|
21
|
-
| { type: 'image_url'; image_url: { url: string } }
|
|
21
|
+
| { type: 'image_url'; image_url: { url: string } }
|
|
22
|
+
| { type: 'file'; file: { file_id: string } };
|
|
22
23
|
|
|
23
24
|
export interface XaiAssistantMessage {
|
|
24
25
|
role: 'assistant';
|
package/src/xai-error.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils';
|
|
2
2
|
import { z } from 'zod/v4';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
export const xaiErrorDataSchema = z.object({
|
|
4
|
+
const chatCompletionsErrorSchema = z.object({
|
|
6
5
|
error: z.object({
|
|
7
6
|
message: z.string(),
|
|
8
7
|
type: z.string().nullish(),
|
|
@@ -11,9 +10,20 @@ export const xaiErrorDataSchema = z.object({
|
|
|
11
10
|
}),
|
|
12
11
|
});
|
|
13
12
|
|
|
13
|
+
const responsesErrorSchema = z.object({
|
|
14
|
+
code: z.string(),
|
|
15
|
+
error: z.string(),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
export const xaiErrorDataSchema = z.union([
|
|
19
|
+
chatCompletionsErrorSchema,
|
|
20
|
+
responsesErrorSchema,
|
|
21
|
+
]);
|
|
22
|
+
|
|
14
23
|
export type XaiErrorData = z.infer<typeof xaiErrorDataSchema>;
|
|
15
24
|
|
|
16
25
|
export const xaiFailedResponseHandler = createJsonErrorResponseHandler({
|
|
17
26
|
errorSchema: xaiErrorDataSchema,
|
|
18
|
-
errorToMessage: data =>
|
|
27
|
+
errorToMessage: data =>
|
|
28
|
+
'code' in data ? `${data.code}: ${data.error}` : data.error.message,
|
|
19
29
|
});
|
package/src/xai-image-model.ts
CHANGED
|
@@ -1,38 +1,55 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ImageModelV4, SharedV4Warning } from '@ai-sdk/provider';
|
|
2
2
|
import {
|
|
3
3
|
combineHeaders,
|
|
4
4
|
convertImageModelFileToDataUri,
|
|
5
5
|
createBinaryResponseHandler,
|
|
6
6
|
createJsonResponseHandler,
|
|
7
7
|
createStatusCodeErrorResponseHandler,
|
|
8
|
-
FetchFunction,
|
|
9
8
|
getFromApi,
|
|
10
9
|
parseProviderOptions,
|
|
11
10
|
postJsonToApi,
|
|
11
|
+
serializeModelOptions,
|
|
12
|
+
WORKFLOW_SERIALIZE,
|
|
13
|
+
WORKFLOW_DESERIALIZE,
|
|
14
|
+
type FetchFunction,
|
|
12
15
|
} from '@ai-sdk/provider-utils';
|
|
13
16
|
import { z } from 'zod/v4';
|
|
14
17
|
import { xaiFailedResponseHandler } from './xai-error';
|
|
15
|
-
import { xaiImageModelOptions } from './xai-image-options';
|
|
16
|
-
import { XaiImageModelId } from './xai-image-settings';
|
|
18
|
+
import { xaiImageModelOptions } from './xai-image-model-options';
|
|
19
|
+
import type { XaiImageModelId } from './xai-image-settings';
|
|
17
20
|
|
|
18
21
|
interface XaiImageModelConfig {
|
|
19
22
|
provider: string;
|
|
20
23
|
baseURL: string | undefined;
|
|
21
|
-
headers
|
|
24
|
+
headers?: () => Record<string, string | undefined>;
|
|
22
25
|
fetch?: FetchFunction;
|
|
23
26
|
_internal?: {
|
|
24
27
|
currentDate?: () => Date;
|
|
25
28
|
};
|
|
26
29
|
}
|
|
27
30
|
|
|
28
|
-
export class XaiImageModel implements
|
|
29
|
-
readonly specificationVersion = '
|
|
31
|
+
export class XaiImageModel implements ImageModelV4 {
|
|
32
|
+
readonly specificationVersion = 'v4';
|
|
30
33
|
readonly maxImagesPerCall = 3;
|
|
31
34
|
|
|
32
35
|
get provider(): string {
|
|
33
36
|
return this.config.provider;
|
|
34
37
|
}
|
|
35
38
|
|
|
39
|
+
static [WORKFLOW_SERIALIZE](model: XaiImageModel) {
|
|
40
|
+
return serializeModelOptions({
|
|
41
|
+
modelId: model.modelId,
|
|
42
|
+
config: model.config,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static [WORKFLOW_DESERIALIZE](options: {
|
|
47
|
+
modelId: XaiImageModelId;
|
|
48
|
+
config: XaiImageModelConfig;
|
|
49
|
+
}) {
|
|
50
|
+
return new XaiImageModel(options.modelId, options.config);
|
|
51
|
+
}
|
|
52
|
+
|
|
36
53
|
constructor(
|
|
37
54
|
readonly modelId: XaiImageModelId,
|
|
38
55
|
private config: XaiImageModelConfig,
|
|
@@ -49,10 +66,10 @@ export class XaiImageModel implements ImageModelV3 {
|
|
|
49
66
|
abortSignal,
|
|
50
67
|
files,
|
|
51
68
|
mask,
|
|
52
|
-
}: Parameters<
|
|
53
|
-
Awaited<ReturnType<
|
|
69
|
+
}: Parameters<ImageModelV4['doGenerate']>[0]): Promise<
|
|
70
|
+
Awaited<ReturnType<ImageModelV4['doGenerate']>>
|
|
54
71
|
> {
|
|
55
|
-
const warnings: Array<
|
|
72
|
+
const warnings: Array<SharedV4Warning> = [];
|
|
56
73
|
|
|
57
74
|
if (size != null) {
|
|
58
75
|
warnings.push({
|
|
@@ -135,7 +152,7 @@ export class XaiImageModel implements ImageModelV3 {
|
|
|
135
152
|
const currentDate = this.config._internal?.currentDate?.() ?? new Date();
|
|
136
153
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
137
154
|
url: `${baseURL}${endpoint}`,
|
|
138
|
-
headers: combineHeaders(this.config.headers(), headers),
|
|
155
|
+
headers: combineHeaders(this.config.headers?.(), headers),
|
|
139
156
|
body,
|
|
140
157
|
failedResponseHandler: xaiFailedResponseHandler,
|
|
141
158
|
successfulResponseHandler: createJsonResponseHandler(
|
package/src/xai-prepare-tools.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import {
|
|
2
|
-
LanguageModelV3CallOptions,
|
|
3
|
-
SharedV3Warning,
|
|
4
2
|
UnsupportedFunctionalityError,
|
|
3
|
+
type LanguageModelV4CallOptions,
|
|
4
|
+
type SharedV4Warning,
|
|
5
5
|
} from '@ai-sdk/provider';
|
|
6
|
-
import {
|
|
6
|
+
import { removeAdditionalPropertiesFalse } from './remove-additional-properties';
|
|
7
|
+
import type { XaiToolChoice } from './xai-chat-prompt';
|
|
7
8
|
|
|
8
9
|
export function prepareTools({
|
|
9
10
|
tools,
|
|
10
11
|
toolChoice,
|
|
11
12
|
}: {
|
|
12
|
-
tools:
|
|
13
|
-
toolChoice?:
|
|
13
|
+
tools: LanguageModelV4CallOptions['tools'];
|
|
14
|
+
toolChoice?: LanguageModelV4CallOptions['toolChoice'];
|
|
14
15
|
}): {
|
|
15
16
|
tools:
|
|
16
17
|
| Array<{
|
|
@@ -24,12 +25,12 @@ export function prepareTools({
|
|
|
24
25
|
}>
|
|
25
26
|
| undefined;
|
|
26
27
|
toolChoice: XaiToolChoice | undefined;
|
|
27
|
-
toolWarnings:
|
|
28
|
+
toolWarnings: SharedV4Warning[];
|
|
28
29
|
} {
|
|
29
30
|
// when the tools array is empty, change it to undefined to prevent errors
|
|
30
31
|
tools = tools?.length ? tools : undefined;
|
|
31
32
|
|
|
32
|
-
const toolWarnings:
|
|
33
|
+
const toolWarnings: SharedV4Warning[] = [];
|
|
33
34
|
|
|
34
35
|
if (tools == null) {
|
|
35
36
|
return { tools: undefined, toolChoice: undefined, toolWarnings };
|
|
@@ -58,7 +59,7 @@ export function prepareTools({
|
|
|
58
59
|
function: {
|
|
59
60
|
name: tool.name,
|
|
60
61
|
description: tool.description,
|
|
61
|
-
parameters: tool.inputSchema,
|
|
62
|
+
parameters: removeAdditionalPropertiesFalse(tool.inputSchema),
|
|
62
63
|
...(tool.strict != null ? { strict: tool.strict } : {}),
|
|
63
64
|
},
|
|
64
65
|
});
|