@providerprotocol/ai 0.0.4 → 0.0.5

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.
@@ -0,0 +1,908 @@
1
+ /**
2
+ * xAI-specific LLM parameters
3
+ * These are passed through to the relevant xAI APIs (Completions, Responses, Messages)
4
+ *
5
+ * xAI's APIs are compatible with OpenAI and Anthropic SDKs, supporting three API modes:
6
+ * - Chat Completions API (OpenAI-compatible)
7
+ * - Responses API (OpenAI Responses-compatible, with stateful conversations)
8
+ * - Messages API (Anthropic-compatible)
9
+ */
10
+ export interface XAILLMParams {
11
+ /** Maximum number of tokens to generate */
12
+ max_tokens?: number;
13
+
14
+ /** Maximum completion tokens (Chat Completions API) */
15
+ max_completion_tokens?: number;
16
+
17
+ /** Maximum output tokens (Responses API) */
18
+ max_output_tokens?: number;
19
+
20
+ /** Temperature for randomness (0.0 - 2.0) */
21
+ temperature?: number;
22
+
23
+ /** Top-p (nucleus) sampling (0.0 - 1.0) */
24
+ top_p?: number;
25
+
26
+ /** Top-k sampling (Messages API only) */
27
+ top_k?: number;
28
+
29
+ /** Frequency penalty (-2.0 - 2.0) */
30
+ frequency_penalty?: number;
31
+
32
+ /** Presence penalty (-2.0 - 2.0) */
33
+ presence_penalty?: number;
34
+
35
+ /** Custom stop sequences */
36
+ stop?: string | string[];
37
+
38
+ /** Stop sequences (Messages API) */
39
+ stop_sequences?: string[];
40
+
41
+ /** Number of completions to generate */
42
+ n?: number;
43
+
44
+ /** Enable logprobs */
45
+ logprobs?: boolean;
46
+
47
+ /** Number of top logprobs to return (0-20) */
48
+ top_logprobs?: number;
49
+
50
+ /** Seed for deterministic sampling */
51
+ seed?: number;
52
+
53
+ /** User identifier for abuse detection */
54
+ user?: string;
55
+
56
+ /** Logit bias map (Chat Completions API) */
57
+ logit_bias?: Record<string, number>;
58
+
59
+ /** Whether to enable parallel tool calls */
60
+ parallel_tool_calls?: boolean;
61
+
62
+ /**
63
+ * Reasoning effort for Grok 3 Mini models
64
+ * Note: Only 'low' and 'high' are supported by xAI
65
+ * Grok 4 does not support this parameter
66
+ */
67
+ reasoning_effort?: 'low' | 'high';
68
+
69
+ /** Reasoning configuration (Responses API) */
70
+ reasoning?: {
71
+ effort?: 'low' | 'high';
72
+ /** Include encrypted reasoning content for continuation */
73
+ encrypted_content?: boolean;
74
+ };
75
+
76
+ /** Truncation strategy (Responses API) */
77
+ truncation?: 'auto' | 'disabled';
78
+
79
+ /** Fields to include in Responses API output */
80
+ include?: string[];
81
+
82
+ /** Continue from a previous response (Responses API) */
83
+ previous_response_id?: string;
84
+
85
+ /** Store response for continuation (Responses API) */
86
+ store?: boolean;
87
+
88
+ /** Store messages on xAI servers (default: true for Responses API) */
89
+ store_messages?: boolean;
90
+
91
+ /** Metadata key-value pairs */
92
+ metadata?: Record<string, string>;
93
+
94
+ /** Response format for structured output (Chat Completions API only) */
95
+ response_format?: XAIResponseFormat;
96
+
97
+ /**
98
+ * Live Search parameters (deprecated, will be removed Dec 15, 2025)
99
+ * Use Agent Tools API instead for new implementations
100
+ */
101
+ search_parameters?: XAISearchParameters;
102
+
103
+ /**
104
+ * Server-side agentic tools (Agent Tools API)
105
+ * These are executed server-side by xAI
106
+ */
107
+ agent_tools?: XAIAgentTool[];
108
+
109
+ /** Metadata for the request (Messages API) */
110
+ messages_metadata?: {
111
+ user_id?: string;
112
+ };
113
+
114
+ /** Extended thinking configuration (Messages API) */
115
+ thinking?: {
116
+ type: 'enabled';
117
+ budget_tokens: number;
118
+ };
119
+ }
120
+
121
+ /**
122
+ * API mode for xAI provider
123
+ */
124
+ export type XAIAPIMode = 'completions' | 'responses' | 'messages';
125
+
126
+ /**
127
+ * Model options when creating a model reference
128
+ */
129
+ export interface XAIModelOptions {
130
+ /** Which API to use */
131
+ api?: XAIAPIMode;
132
+ }
133
+
134
+ /**
135
+ * Model reference with xAI-specific options
136
+ */
137
+ export interface XAIModelReference {
138
+ modelId: string;
139
+ options?: XAIModelOptions;
140
+ }
141
+
142
+ /**
143
+ * xAI provider configuration
144
+ */
145
+ export interface XAIConfig {
146
+ /** Which API to use: 'completions', 'responses', or 'messages' */
147
+ api?: XAIAPIMode;
148
+ }
149
+
150
+ /**
151
+ * Live Search parameters (deprecated)
152
+ */
153
+ export interface XAISearchParameters {
154
+ /** Search mode */
155
+ mode?: 'auto' | 'on' | 'off';
156
+ /** Limit search to specific date range */
157
+ from_date?: string;
158
+ /** End date for search range */
159
+ to_date?: string;
160
+ /** Sources to search */
161
+ sources?: Array<'web' | 'x' | 'news' | 'rss'>;
162
+ /** Maximum number of search results */
163
+ max_search_results?: number;
164
+ }
165
+
166
+ /**
167
+ * Server-side agentic tools
168
+ */
169
+ export interface XAIAgentTool {
170
+ type: 'web_search' | 'x_search' | 'code_execution';
171
+ }
172
+
173
+ // ============================================
174
+ // Chat Completions API Types (OpenAI-compatible)
175
+ // ============================================
176
+
177
+ /**
178
+ * Chat Completions API request body
179
+ */
180
+ export interface XAICompletionsRequest {
181
+ model: string;
182
+ messages: XAICompletionsMessage[];
183
+ temperature?: number;
184
+ top_p?: number;
185
+ n?: number;
186
+ stream?: boolean;
187
+ stream_options?: { include_usage?: boolean };
188
+ stop?: string | string[];
189
+ max_tokens?: number;
190
+ max_completion_tokens?: number;
191
+ presence_penalty?: number;
192
+ frequency_penalty?: number;
193
+ logit_bias?: Record<string, number>;
194
+ logprobs?: boolean;
195
+ top_logprobs?: number;
196
+ user?: string;
197
+ seed?: number;
198
+ tools?: XAICompletionsTool[];
199
+ tool_choice?: XAIToolChoice;
200
+ parallel_tool_calls?: boolean;
201
+ response_format?: XAIResponseFormat;
202
+ reasoning_effort?: string;
203
+ store?: boolean;
204
+ metadata?: Record<string, string>;
205
+ search_parameters?: XAISearchParameters;
206
+ }
207
+
208
+ /**
209
+ * Chat Completions message format
210
+ */
211
+ export type XAICompletionsMessage =
212
+ | XAISystemMessage
213
+ | XAIUserMessage
214
+ | XAIAssistantMessage
215
+ | XAIToolMessage;
216
+
217
+ export interface XAISystemMessage {
218
+ role: 'system';
219
+ content: string;
220
+ name?: string;
221
+ }
222
+
223
+ export interface XAIUserMessage {
224
+ role: 'user';
225
+ content: string | XAIUserContent[];
226
+ name?: string;
227
+ }
228
+
229
+ export interface XAIAssistantMessage {
230
+ role: 'assistant';
231
+ content?: string | null;
232
+ name?: string;
233
+ tool_calls?: XAIToolCall[];
234
+ refusal?: string | null;
235
+ }
236
+
237
+ export interface XAIToolMessage {
238
+ role: 'tool';
239
+ content: string;
240
+ tool_call_id: string;
241
+ }
242
+
243
+ /**
244
+ * User content types
245
+ */
246
+ export type XAIUserContent = XAITextContent | XAIImageContent;
247
+
248
+ export interface XAITextContent {
249
+ type: 'text';
250
+ text: string;
251
+ }
252
+
253
+ export interface XAIImageContent {
254
+ type: 'image_url';
255
+ image_url: {
256
+ url: string;
257
+ detail?: 'auto' | 'low' | 'high';
258
+ };
259
+ }
260
+
261
+ /**
262
+ * Tool call format
263
+ */
264
+ export interface XAIToolCall {
265
+ id: string;
266
+ type: 'function';
267
+ function: {
268
+ name: string;
269
+ arguments: string;
270
+ };
271
+ }
272
+
273
+ /**
274
+ * Tool definition for Chat Completions
275
+ */
276
+ export interface XAICompletionsTool {
277
+ type: 'function';
278
+ function: {
279
+ name: string;
280
+ description: string;
281
+ parameters: {
282
+ type: 'object';
283
+ properties: Record<string, unknown>;
284
+ required?: string[];
285
+ additionalProperties?: boolean;
286
+ };
287
+ strict?: boolean;
288
+ };
289
+ }
290
+
291
+ /**
292
+ * Tool choice options
293
+ */
294
+ export type XAIToolChoice =
295
+ | 'none'
296
+ | 'auto'
297
+ | 'required'
298
+ | { type: 'function'; function: { name: string } };
299
+
300
+ /**
301
+ * Response format
302
+ */
303
+ export type XAIResponseFormat =
304
+ | { type: 'text' }
305
+ | { type: 'json_object' }
306
+ | {
307
+ type: 'json_schema';
308
+ json_schema: {
309
+ name: string;
310
+ description?: string;
311
+ schema: Record<string, unknown>;
312
+ strict?: boolean;
313
+ };
314
+ };
315
+
316
+ /**
317
+ * Chat Completions response format
318
+ */
319
+ export interface XAICompletionsResponse {
320
+ id: string;
321
+ object: 'chat.completion';
322
+ created: number;
323
+ model: string;
324
+ choices: XAICompletionsChoice[];
325
+ usage: XAIUsage;
326
+ system_fingerprint?: string;
327
+ /** Citations from live search */
328
+ citations?: string[];
329
+ /** Inline citations in response */
330
+ inline_citations?: Array<{ text: string; url: string }>;
331
+ }
332
+
333
+ export interface XAICompletionsChoice {
334
+ index: number;
335
+ message: XAIAssistantMessage;
336
+ finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | null;
337
+ logprobs?: XAILogprobs | null;
338
+ }
339
+
340
+ export interface XAILogprobs {
341
+ content?: Array<{
342
+ token: string;
343
+ logprob: number;
344
+ bytes?: number[];
345
+ top_logprobs?: Array<{
346
+ token: string;
347
+ logprob: number;
348
+ bytes?: number[];
349
+ }>;
350
+ }>;
351
+ }
352
+
353
+ export interface XAIUsage {
354
+ prompt_tokens: number;
355
+ completion_tokens: number;
356
+ total_tokens: number;
357
+ prompt_tokens_details?: {
358
+ cached_tokens?: number;
359
+ audio_tokens?: number;
360
+ };
361
+ completion_tokens_details?: {
362
+ reasoning_tokens?: number;
363
+ audio_tokens?: number;
364
+ };
365
+ }
366
+
367
+ /**
368
+ * Chat Completions streaming event types
369
+ */
370
+ export interface XAICompletionsStreamChunk {
371
+ id: string;
372
+ object: 'chat.completion.chunk';
373
+ created: number;
374
+ model: string;
375
+ choices: XAICompletionsStreamChoice[];
376
+ usage?: XAIUsage | null;
377
+ system_fingerprint?: string;
378
+ }
379
+
380
+ export interface XAICompletionsStreamChoice {
381
+ index: number;
382
+ delta: XAICompletionsStreamDelta;
383
+ finish_reason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | null;
384
+ logprobs?: XAILogprobs | null;
385
+ }
386
+
387
+ export interface XAICompletionsStreamDelta {
388
+ role?: 'assistant';
389
+ content?: string | null;
390
+ tool_calls?: XAIStreamToolCall[];
391
+ refusal?: string | null;
392
+ }
393
+
394
+ export interface XAIStreamToolCall {
395
+ index: number;
396
+ id?: string;
397
+ type?: 'function';
398
+ function?: {
399
+ name?: string;
400
+ arguments?: string;
401
+ };
402
+ }
403
+
404
+ // ============================================
405
+ // Responses API Types (OpenAI Responses-compatible)
406
+ // ============================================
407
+
408
+ /**
409
+ * Responses API request body
410
+ */
411
+ export interface XAIResponsesRequest {
412
+ model: string;
413
+ input: string | XAIResponsesInputItem[];
414
+ instructions?: string;
415
+ max_output_tokens?: number;
416
+ temperature?: number;
417
+ top_p?: number;
418
+ stream?: boolean;
419
+ tools?: XAIResponsesTool[];
420
+ tool_choice?: XAIResponsesToolChoice;
421
+ parallel_tool_calls?: boolean;
422
+ text?: XAIResponsesTextConfig;
423
+ truncation?: 'auto' | 'disabled';
424
+ store?: boolean;
425
+ metadata?: Record<string, string>;
426
+ reasoning?: {
427
+ effort?: 'low' | 'high';
428
+ };
429
+ include?: string[];
430
+ previous_response_id?: string;
431
+ search_parameters?: XAISearchParameters;
432
+ }
433
+
434
+ /**
435
+ * Responses API input item
436
+ */
437
+ export type XAIResponsesInputItem =
438
+ | XAIResponsesSystemItem
439
+ | XAIResponsesUserItem
440
+ | XAIResponsesAssistantItem
441
+ | XAIResponsesFunctionCallInputItem
442
+ | XAIResponsesToolResultItem;
443
+
444
+ export interface XAIResponsesSystemItem {
445
+ type: 'message';
446
+ role: 'system' | 'developer';
447
+ content: string | XAIResponsesContentPart[];
448
+ }
449
+
450
+ export interface XAIResponsesUserItem {
451
+ type: 'message';
452
+ role: 'user';
453
+ content: string | XAIResponsesContentPart[];
454
+ }
455
+
456
+ export interface XAIResponsesAssistantItem {
457
+ type: 'message';
458
+ role: 'assistant';
459
+ content: string | XAIResponsesContentPart[];
460
+ }
461
+
462
+ export interface XAIResponsesFunctionCallInputItem {
463
+ type: 'function_call';
464
+ id: string;
465
+ call_id: string;
466
+ name: string;
467
+ arguments: string;
468
+ }
469
+
470
+ export interface XAIResponsesToolResultItem {
471
+ type: 'function_call_output';
472
+ call_id: string;
473
+ output: string;
474
+ }
475
+
476
+ /**
477
+ * Content parts for Responses API
478
+ */
479
+ export type XAIResponsesContentPart =
480
+ | XAIResponsesTextPart
481
+ | XAIResponsesImagePart
482
+ | XAIResponsesFunctionCallPart;
483
+
484
+ export interface XAIResponsesTextPart {
485
+ type: 'input_text' | 'output_text';
486
+ text: string;
487
+ }
488
+
489
+ export interface XAIResponsesImagePart {
490
+ type: 'input_image';
491
+ image_url?: string;
492
+ image?: string; // base64
493
+ detail?: 'auto' | 'low' | 'high';
494
+ }
495
+
496
+ export interface XAIResponsesFunctionCallPart {
497
+ type: 'function_call';
498
+ id: string;
499
+ call_id: string;
500
+ name: string;
501
+ arguments: string;
502
+ }
503
+
504
+ /**
505
+ * Tool definition for Responses API
506
+ */
507
+ export interface XAIResponsesTool {
508
+ type: 'function';
509
+ name: string;
510
+ description: string;
511
+ parameters: {
512
+ type: 'object';
513
+ properties: Record<string, unknown>;
514
+ required?: string[];
515
+ additionalProperties?: boolean;
516
+ };
517
+ strict?: boolean;
518
+ }
519
+
520
+ /**
521
+ * Tool choice for Responses API
522
+ */
523
+ export type XAIResponsesToolChoice =
524
+ | 'none'
525
+ | 'auto'
526
+ | 'required'
527
+ | { type: 'function'; name: string };
528
+
529
+ /**
530
+ * Text configuration for structured output
531
+ */
532
+ export interface XAIResponsesTextConfig {
533
+ format?:
534
+ | { type: 'text' }
535
+ | { type: 'json_object' }
536
+ | {
537
+ type: 'json_schema';
538
+ name: string;
539
+ description?: string;
540
+ schema: Record<string, unknown>;
541
+ strict?: boolean;
542
+ };
543
+ }
544
+
545
+ /**
546
+ * Responses API response format
547
+ */
548
+ export interface XAIResponsesResponse {
549
+ id: string;
550
+ object: 'response';
551
+ created_at: number;
552
+ model: string;
553
+ output: XAIResponsesOutputItem[];
554
+ usage: XAIResponsesUsage;
555
+ status: 'completed' | 'failed' | 'incomplete' | 'in_progress';
556
+ error?: {
557
+ code: string;
558
+ message: string;
559
+ };
560
+ incomplete_details?: {
561
+ reason: string;
562
+ };
563
+ /** Citations from live search */
564
+ citations?: string[];
565
+ /** Inline citations in response */
566
+ inline_citations?: Array<{ text: string; url: string }>;
567
+ }
568
+
569
+ export type XAIResponsesOutputItem =
570
+ | XAIResponsesMessageOutput
571
+ | XAIResponsesFunctionCallOutput;
572
+
573
+ export interface XAIResponsesMessageOutput {
574
+ type: 'message';
575
+ id: string;
576
+ role: 'assistant';
577
+ content: XAIResponsesOutputContent[];
578
+ status: 'completed' | 'in_progress';
579
+ }
580
+
581
+ export interface XAIResponsesFunctionCallOutput {
582
+ type: 'function_call';
583
+ id: string;
584
+ call_id: string;
585
+ name: string;
586
+ arguments: string;
587
+ status: 'completed' | 'in_progress';
588
+ }
589
+
590
+ export type XAIResponsesOutputContent =
591
+ | { type: 'output_text'; text: string; annotations?: unknown[] }
592
+ | { type: 'refusal'; refusal: string };
593
+
594
+ export interface XAIResponsesUsage {
595
+ input_tokens: number;
596
+ output_tokens: number;
597
+ total_tokens: number;
598
+ input_tokens_details?: {
599
+ cached_tokens?: number;
600
+ text_tokens?: number;
601
+ image_tokens?: number;
602
+ audio_tokens?: number;
603
+ };
604
+ output_tokens_details?: {
605
+ text_tokens?: number;
606
+ reasoning_tokens?: number;
607
+ audio_tokens?: number;
608
+ };
609
+ }
610
+
611
+ /**
612
+ * Responses API streaming event types
613
+ */
614
+ export type XAIResponsesStreamEvent =
615
+ | XAIResponseCreatedEvent
616
+ | XAIResponseInProgressEvent
617
+ | XAIResponseCompletedEvent
618
+ | XAIResponseFailedEvent
619
+ | XAIResponseOutputItemAddedEvent
620
+ | XAIResponseOutputItemDoneEvent
621
+ | XAIResponseContentPartAddedEvent
622
+ | XAIResponseContentPartDoneEvent
623
+ | XAIResponseTextDeltaEvent
624
+ | XAIResponseTextDoneEvent
625
+ | XAIResponseRefusalDeltaEvent
626
+ | XAIResponseRefusalDoneEvent
627
+ | XAIResponseFunctionCallArgumentsDeltaEvent
628
+ | XAIResponseFunctionCallArgumentsDoneEvent
629
+ | XAIResponseErrorEvent;
630
+
631
+ export interface XAIResponseCreatedEvent {
632
+ type: 'response.created';
633
+ response: XAIResponsesResponse;
634
+ }
635
+
636
+ export interface XAIResponseInProgressEvent {
637
+ type: 'response.in_progress';
638
+ response: XAIResponsesResponse;
639
+ }
640
+
641
+ export interface XAIResponseCompletedEvent {
642
+ type: 'response.completed';
643
+ response: XAIResponsesResponse;
644
+ }
645
+
646
+ export interface XAIResponseFailedEvent {
647
+ type: 'response.failed';
648
+ response: XAIResponsesResponse;
649
+ }
650
+
651
+ export interface XAIResponseOutputItemAddedEvent {
652
+ type: 'response.output_item.added';
653
+ output_index: number;
654
+ item: XAIResponsesOutputItem;
655
+ }
656
+
657
+ export interface XAIResponseOutputItemDoneEvent {
658
+ type: 'response.output_item.done';
659
+ output_index: number;
660
+ item: XAIResponsesOutputItem;
661
+ }
662
+
663
+ export interface XAIResponseContentPartAddedEvent {
664
+ type: 'response.content_part.added';
665
+ output_index: number;
666
+ content_index: number;
667
+ part: XAIResponsesOutputContent;
668
+ }
669
+
670
+ export interface XAIResponseContentPartDoneEvent {
671
+ type: 'response.content_part.done';
672
+ output_index: number;
673
+ content_index: number;
674
+ part: XAIResponsesOutputContent;
675
+ }
676
+
677
+ export interface XAIResponseTextDeltaEvent {
678
+ type: 'response.output_text.delta';
679
+ output_index: number;
680
+ content_index: number;
681
+ delta: string;
682
+ }
683
+
684
+ export interface XAIResponseTextDoneEvent {
685
+ type: 'response.output_text.done';
686
+ output_index: number;
687
+ content_index: number;
688
+ text: string;
689
+ }
690
+
691
+ export interface XAIResponseRefusalDeltaEvent {
692
+ type: 'response.refusal.delta';
693
+ output_index: number;
694
+ content_index: number;
695
+ delta: string;
696
+ }
697
+
698
+ export interface XAIResponseRefusalDoneEvent {
699
+ type: 'response.refusal.done';
700
+ output_index: number;
701
+ content_index: number;
702
+ refusal: string;
703
+ }
704
+
705
+ export interface XAIResponseFunctionCallArgumentsDeltaEvent {
706
+ type: 'response.function_call_arguments.delta';
707
+ output_index: number;
708
+ item_id: string;
709
+ delta: string;
710
+ call_id?: string;
711
+ }
712
+
713
+ export interface XAIResponseFunctionCallArgumentsDoneEvent {
714
+ type: 'response.function_call_arguments.done';
715
+ output_index: number;
716
+ item_id: string;
717
+ name: string;
718
+ arguments: string;
719
+ call_id?: string;
720
+ }
721
+
722
+ export interface XAIResponseErrorEvent {
723
+ type: 'error';
724
+ error: {
725
+ type: string;
726
+ code?: string;
727
+ message: string;
728
+ };
729
+ }
730
+
731
+ // ============================================
732
+ // Messages API Types (Anthropic-compatible)
733
+ // ============================================
734
+
735
+ /**
736
+ * Messages API request body
737
+ */
738
+ export interface XAIMessagesRequest {
739
+ model: string;
740
+ max_tokens?: number;
741
+ messages: XAIMessagesMessage[];
742
+ system?: string;
743
+ temperature?: number;
744
+ top_p?: number;
745
+ top_k?: number;
746
+ stop_sequences?: string[];
747
+ stream?: boolean;
748
+ tools?: XAIMessagesTool[];
749
+ tool_choice?: { type: 'auto' | 'any' | 'tool'; name?: string };
750
+ metadata?: { user_id?: string };
751
+ thinking?: { type: 'enabled'; budget_tokens: number };
752
+ }
753
+
754
+ /**
755
+ * Messages API message format
756
+ */
757
+ export interface XAIMessagesMessage {
758
+ role: 'user' | 'assistant';
759
+ content: XAIMessagesContent[] | string;
760
+ }
761
+
762
+ /**
763
+ * Messages API content types
764
+ */
765
+ export type XAIMessagesContent =
766
+ | XAIMessagesTextContent
767
+ | XAIMessagesImageContent
768
+ | XAIMessagesToolUseContent
769
+ | XAIMessagesToolResultContent;
770
+
771
+ export interface XAIMessagesTextContent {
772
+ type: 'text';
773
+ text: string;
774
+ }
775
+
776
+ export interface XAIMessagesImageContent {
777
+ type: 'image';
778
+ source: {
779
+ type: 'base64' | 'url';
780
+ media_type?: string;
781
+ data?: string;
782
+ url?: string;
783
+ };
784
+ }
785
+
786
+ export interface XAIMessagesToolUseContent {
787
+ type: 'tool_use';
788
+ id: string;
789
+ name: string;
790
+ input: Record<string, unknown>;
791
+ }
792
+
793
+ export interface XAIMessagesToolResultContent {
794
+ type: 'tool_result';
795
+ tool_use_id: string;
796
+ content: string | XAIMessagesContent[];
797
+ is_error?: boolean;
798
+ }
799
+
800
+ /**
801
+ * Messages API tool format
802
+ */
803
+ export interface XAIMessagesTool {
804
+ name: string;
805
+ description: string;
806
+ input_schema: {
807
+ type: 'object';
808
+ properties: Record<string, unknown>;
809
+ required?: string[];
810
+ };
811
+ }
812
+
813
+ /**
814
+ * Messages API response format
815
+ */
816
+ export interface XAIMessagesResponse {
817
+ id: string;
818
+ type: 'message';
819
+ role: 'assistant';
820
+ content: XAIMessagesResponseContent[];
821
+ model: string;
822
+ stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'pause_turn' | 'refusal' | null;
823
+ stop_sequence: string | null;
824
+ usage: {
825
+ input_tokens: number;
826
+ output_tokens: number;
827
+ cache_creation_input_tokens?: number;
828
+ cache_read_input_tokens?: number;
829
+ };
830
+ }
831
+
832
+ export type XAIMessagesResponseContent =
833
+ | XAIMessagesTextContent
834
+ | XAIMessagesToolUseContent
835
+ | XAIMessagesThinkingContent;
836
+
837
+ export interface XAIMessagesThinkingContent {
838
+ type: 'thinking';
839
+ thinking: string;
840
+ signature?: string;
841
+ }
842
+
843
+ /**
844
+ * Messages API streaming event types
845
+ */
846
+ export type XAIMessagesStreamEvent =
847
+ | XAIMessagesMessageStartEvent
848
+ | XAIMessagesContentBlockStartEvent
849
+ | XAIMessagesContentBlockDeltaEvent
850
+ | XAIMessagesContentBlockStopEvent
851
+ | XAIMessagesMessageDeltaEvent
852
+ | XAIMessagesMessageStopEvent
853
+ | XAIMessagesPingEvent
854
+ | XAIMessagesErrorEvent;
855
+
856
+ export interface XAIMessagesMessageStartEvent {
857
+ type: 'message_start';
858
+ message: XAIMessagesResponse;
859
+ }
860
+
861
+ export interface XAIMessagesContentBlockStartEvent {
862
+ type: 'content_block_start';
863
+ index: number;
864
+ content_block: XAIMessagesResponseContent;
865
+ }
866
+
867
+ export interface XAIMessagesContentBlockDeltaEvent {
868
+ type: 'content_block_delta';
869
+ /** Index may be omitted by xAI (unlike Anthropic) - use tracked currentIndex as fallback */
870
+ index?: number;
871
+ delta:
872
+ | { type: 'text_delta'; text: string }
873
+ | { type: 'thinking_delta'; thinking: string }
874
+ | { type: 'signature_delta'; signature: string }
875
+ | { type: 'input_json_delta'; partial_json: string };
876
+ }
877
+
878
+ export interface XAIMessagesContentBlockStopEvent {
879
+ type: 'content_block_stop';
880
+ index: number;
881
+ }
882
+
883
+ export interface XAIMessagesMessageDeltaEvent {
884
+ type: 'message_delta';
885
+ delta: {
886
+ stop_reason: string | null;
887
+ stop_sequence: string | null;
888
+ };
889
+ usage: {
890
+ output_tokens: number;
891
+ };
892
+ }
893
+
894
+ export interface XAIMessagesMessageStopEvent {
895
+ type: 'message_stop';
896
+ }
897
+
898
+ export interface XAIMessagesPingEvent {
899
+ type: 'ping';
900
+ }
901
+
902
+ export interface XAIMessagesErrorEvent {
903
+ type: 'error';
904
+ error: {
905
+ type: string;
906
+ message: string;
907
+ };
908
+ }