@ai-sdk/xai 4.0.58 → 5.0.0

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.
@@ -1,48 +0,0 @@
1
- export type XaiChatPrompt = Array<XaiChatMessage>;
2
-
3
- export type XaiChatMessage =
4
- | XaiSystemMessage
5
- | XaiUserMessage
6
- | XaiAssistantMessage
7
- | XaiToolMessage;
8
-
9
- export interface XaiSystemMessage {
10
- role: 'system';
11
- content: string;
12
- }
13
-
14
- export interface XaiUserMessage {
15
- role: 'user';
16
- content: string | Array<XaiUserMessageContent>;
17
- }
18
-
19
- export type XaiUserMessageContent =
20
- | { type: 'text'; text: string }
21
- | {
22
- type: 'image_url';
23
- image_url: { url: string; detail?: 'low' | 'high' | 'auto' };
24
- }
25
- | { type: 'file'; file: { file_id: string } };
26
-
27
- export interface XaiAssistantMessage {
28
- role: 'assistant';
29
- content: string;
30
- tool_calls?: Array<{
31
- id: string;
32
- type: 'function';
33
- function: { name: string; arguments: string };
34
- }>;
35
- }
36
-
37
- export interface XaiToolMessage {
38
- role: 'tool';
39
- tool_call_id: string;
40
- content: string;
41
- }
42
-
43
- // xai tool choice
44
- export type XaiToolChoice =
45
- | 'auto'
46
- | 'none'
47
- | 'required'
48
- | { type: 'function'; function: { name: string } };
@@ -1,99 +0,0 @@
1
- import {
2
- UnsupportedFunctionalityError,
3
- type LanguageModelV4CallOptions,
4
- type SharedV4Warning,
5
- } from '@ai-sdk/provider';
6
- import { removeAdditionalPropertiesFalse } from './remove-additional-properties';
7
- import type { XaiToolChoice } from './xai-chat-prompt';
8
-
9
- export function prepareTools({
10
- tools,
11
- toolChoice,
12
- }: {
13
- tools: LanguageModelV4CallOptions['tools'];
14
- toolChoice?: LanguageModelV4CallOptions['toolChoice'];
15
- }): {
16
- tools:
17
- | Array<{
18
- type: 'function';
19
- function: {
20
- name: string;
21
- description: string | undefined;
22
- parameters: unknown;
23
- strict?: boolean;
24
- };
25
- }>
26
- | undefined;
27
- toolChoice: XaiToolChoice | undefined;
28
- toolWarnings: SharedV4Warning[];
29
- } {
30
- // when the tools array is empty, change it to undefined to prevent errors
31
- tools = tools?.length ? tools : undefined;
32
-
33
- const toolWarnings: SharedV4Warning[] = [];
34
-
35
- if (tools == null) {
36
- return { tools: undefined, toolChoice: undefined, toolWarnings };
37
- }
38
-
39
- // convert ai sdk tools to xai format
40
- const xaiTools: Array<{
41
- type: 'function';
42
- function: {
43
- name: string;
44
- description: string | undefined;
45
- parameters: unknown;
46
- strict?: boolean;
47
- };
48
- }> = [];
49
-
50
- for (const tool of tools) {
51
- if (tool.type === 'provider') {
52
- toolWarnings.push({
53
- type: 'unsupported',
54
- feature: `provider-defined tool ${tool.name}`,
55
- });
56
- } else {
57
- xaiTools.push({
58
- type: 'function',
59
- function: {
60
- name: tool.name,
61
- description: tool.description,
62
- parameters: removeAdditionalPropertiesFalse(tool.inputSchema),
63
- ...(tool.strict != null ? { strict: tool.strict } : {}),
64
- },
65
- });
66
- }
67
- }
68
-
69
- if (toolChoice == null) {
70
- return { tools: xaiTools, toolChoice: undefined, toolWarnings };
71
- }
72
-
73
- const type = toolChoice.type;
74
-
75
- switch (type) {
76
- case 'auto':
77
- case 'none':
78
- return { tools: xaiTools, toolChoice: type, toolWarnings };
79
- case 'required':
80
- // xai supports 'required' directly
81
- return { tools: xaiTools, toolChoice: 'required', toolWarnings };
82
- case 'tool':
83
- // xai supports specific tool selection
84
- return {
85
- tools: xaiTools,
86
- toolChoice: {
87
- type: 'function',
88
- function: { name: toolChoice.toolName },
89
- },
90
- toolWarnings,
91
- };
92
- default: {
93
- const _exhaustiveCheck: never = type;
94
- throw new UnsupportedFunctionalityError({
95
- functionality: `tool choice type: ${_exhaustiveCheck}`,
96
- });
97
- }
98
- }
99
- }