@providerprotocol/ai 0.0.4 → 0.0.6

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