@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,1812 @@
1
+ import {
2
+ AssistantMessage,
3
+ isAssistantMessage,
4
+ isToolResultMessage,
5
+ isUserMessage
6
+ } from "../chunk-QUUX4G7U.js";
7
+ import {
8
+ parseSSEStream
9
+ } from "../chunk-X5G4EHL7.js";
10
+ import {
11
+ UPPError,
12
+ doFetch,
13
+ doStreamFetch,
14
+ normalizeHttpError,
15
+ resolveApiKey
16
+ } from "../chunk-SUNYWHTH.js";
17
+
18
+ // src/providers/xai/transform.completions.ts
19
+ function transformRequest(request, modelId) {
20
+ const params = request.params ?? {};
21
+ const xaiRequest = {
22
+ model: modelId,
23
+ messages: transformMessages(request.messages, request.system)
24
+ };
25
+ if (params.temperature !== void 0) {
26
+ xaiRequest.temperature = params.temperature;
27
+ }
28
+ if (params.top_p !== void 0) {
29
+ xaiRequest.top_p = params.top_p;
30
+ }
31
+ if (params.max_completion_tokens !== void 0) {
32
+ xaiRequest.max_completion_tokens = params.max_completion_tokens;
33
+ } else if (params.max_tokens !== void 0) {
34
+ xaiRequest.max_tokens = params.max_tokens;
35
+ }
36
+ if (params.frequency_penalty !== void 0) {
37
+ xaiRequest.frequency_penalty = params.frequency_penalty;
38
+ }
39
+ if (params.presence_penalty !== void 0) {
40
+ xaiRequest.presence_penalty = params.presence_penalty;
41
+ }
42
+ if (params.stop !== void 0) {
43
+ xaiRequest.stop = params.stop;
44
+ }
45
+ if (params.n !== void 0) {
46
+ xaiRequest.n = params.n;
47
+ }
48
+ if (params.logprobs !== void 0) {
49
+ xaiRequest.logprobs = params.logprobs;
50
+ }
51
+ if (params.top_logprobs !== void 0) {
52
+ xaiRequest.top_logprobs = params.top_logprobs;
53
+ }
54
+ if (params.seed !== void 0) {
55
+ xaiRequest.seed = params.seed;
56
+ }
57
+ if (params.user !== void 0) {
58
+ xaiRequest.user = params.user;
59
+ }
60
+ if (params.logit_bias !== void 0) {
61
+ xaiRequest.logit_bias = params.logit_bias;
62
+ }
63
+ if (params.reasoning_effort !== void 0) {
64
+ xaiRequest.reasoning_effort = params.reasoning_effort;
65
+ }
66
+ if (params.store !== void 0) {
67
+ xaiRequest.store = params.store;
68
+ }
69
+ if (params.metadata !== void 0) {
70
+ xaiRequest.metadata = params.metadata;
71
+ }
72
+ if (params.search_parameters !== void 0) {
73
+ xaiRequest.search_parameters = params.search_parameters;
74
+ }
75
+ if (request.tools && request.tools.length > 0) {
76
+ xaiRequest.tools = request.tools.map(transformTool);
77
+ if (params.parallel_tool_calls !== void 0) {
78
+ xaiRequest.parallel_tool_calls = params.parallel_tool_calls;
79
+ }
80
+ }
81
+ if (request.structure) {
82
+ const schema = {
83
+ type: "object",
84
+ properties: request.structure.properties,
85
+ required: request.structure.required,
86
+ ...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
87
+ };
88
+ if (request.structure.description) {
89
+ schema.description = request.structure.description;
90
+ }
91
+ xaiRequest.response_format = {
92
+ type: "json_schema",
93
+ json_schema: {
94
+ name: "json_response",
95
+ description: request.structure.description,
96
+ schema,
97
+ strict: true
98
+ }
99
+ };
100
+ } else if (params.response_format !== void 0) {
101
+ xaiRequest.response_format = params.response_format;
102
+ }
103
+ return xaiRequest;
104
+ }
105
+ function transformMessages(messages, system) {
106
+ const result = [];
107
+ if (system) {
108
+ result.push({
109
+ role: "system",
110
+ content: system
111
+ });
112
+ }
113
+ for (const message of messages) {
114
+ if (isToolResultMessage(message)) {
115
+ const toolMessages = transformToolResults(message);
116
+ result.push(...toolMessages);
117
+ } else {
118
+ const transformed = transformMessage(message);
119
+ if (transformed) {
120
+ result.push(transformed);
121
+ }
122
+ }
123
+ }
124
+ return result;
125
+ }
126
+ function filterValidContent(content) {
127
+ return content.filter((c) => c && typeof c.type === "string");
128
+ }
129
+ function transformMessage(message) {
130
+ if (isUserMessage(message)) {
131
+ const validContent = filterValidContent(message.content);
132
+ if (validContent.length === 1 && validContent[0]?.type === "text") {
133
+ return {
134
+ role: "user",
135
+ content: validContent[0].text
136
+ };
137
+ }
138
+ return {
139
+ role: "user",
140
+ content: validContent.map(transformContentBlock)
141
+ };
142
+ }
143
+ if (isAssistantMessage(message)) {
144
+ const validContent = filterValidContent(message.content);
145
+ const textContent = validContent.filter((c) => c.type === "text").map((c) => c.text).join("");
146
+ const hasToolCalls = message.toolCalls && message.toolCalls.length > 0;
147
+ const assistantMessage = {
148
+ role: "assistant",
149
+ // xAI/OpenAI: content should be null when tool_calls are present and there's no text
150
+ content: hasToolCalls && !textContent ? null : textContent
151
+ };
152
+ if (hasToolCalls) {
153
+ assistantMessage.tool_calls = message.toolCalls.map((call) => ({
154
+ id: call.toolCallId,
155
+ type: "function",
156
+ function: {
157
+ name: call.toolName,
158
+ arguments: JSON.stringify(call.arguments)
159
+ }
160
+ }));
161
+ }
162
+ return assistantMessage;
163
+ }
164
+ if (isToolResultMessage(message)) {
165
+ const results = message.results.map((result) => ({
166
+ role: "tool",
167
+ tool_call_id: result.toolCallId,
168
+ content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
169
+ }));
170
+ return results[0] ?? null;
171
+ }
172
+ return null;
173
+ }
174
+ function transformToolResults(message) {
175
+ if (!isToolResultMessage(message)) {
176
+ const single = transformMessage(message);
177
+ return single ? [single] : [];
178
+ }
179
+ return message.results.map((result) => ({
180
+ role: "tool",
181
+ tool_call_id: result.toolCallId,
182
+ content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
183
+ }));
184
+ }
185
+ function transformContentBlock(block) {
186
+ switch (block.type) {
187
+ case "text":
188
+ return { type: "text", text: block.text };
189
+ case "image": {
190
+ const imageBlock = block;
191
+ let url;
192
+ if (imageBlock.source.type === "base64") {
193
+ url = `data:${imageBlock.mimeType};base64,${imageBlock.source.data}`;
194
+ } else if (imageBlock.source.type === "url") {
195
+ url = imageBlock.source.url;
196
+ } else if (imageBlock.source.type === "bytes") {
197
+ const base64 = btoa(
198
+ Array.from(imageBlock.source.data).map((b) => String.fromCharCode(b)).join("")
199
+ );
200
+ url = `data:${imageBlock.mimeType};base64,${base64}`;
201
+ } else {
202
+ throw new Error("Unknown image source type");
203
+ }
204
+ return {
205
+ type: "image_url",
206
+ image_url: { url }
207
+ };
208
+ }
209
+ default:
210
+ throw new Error(`Unsupported content type: ${block.type}`);
211
+ }
212
+ }
213
+ function transformTool(tool) {
214
+ return {
215
+ type: "function",
216
+ function: {
217
+ name: tool.name,
218
+ description: tool.description,
219
+ parameters: {
220
+ type: "object",
221
+ properties: tool.parameters.properties,
222
+ required: tool.parameters.required,
223
+ ...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
224
+ }
225
+ }
226
+ };
227
+ }
228
+ function transformResponse(data) {
229
+ const choice = data.choices[0];
230
+ if (!choice) {
231
+ throw new Error("No choices in xAI response");
232
+ }
233
+ const textContent = [];
234
+ let structuredData;
235
+ if (choice.message.content) {
236
+ textContent.push({ type: "text", text: choice.message.content });
237
+ try {
238
+ structuredData = JSON.parse(choice.message.content);
239
+ } catch {
240
+ }
241
+ }
242
+ let hadRefusal = false;
243
+ if (choice.message.refusal) {
244
+ textContent.push({ type: "text", text: choice.message.refusal });
245
+ hadRefusal = true;
246
+ }
247
+ const toolCalls = [];
248
+ if (choice.message.tool_calls) {
249
+ for (const call of choice.message.tool_calls) {
250
+ let args = {};
251
+ try {
252
+ args = JSON.parse(call.function.arguments);
253
+ } catch {
254
+ }
255
+ toolCalls.push({
256
+ toolCallId: call.id,
257
+ toolName: call.function.name,
258
+ arguments: args
259
+ });
260
+ }
261
+ }
262
+ const message = new AssistantMessage(
263
+ textContent,
264
+ toolCalls.length > 0 ? toolCalls : void 0,
265
+ {
266
+ id: data.id,
267
+ metadata: {
268
+ xai: {
269
+ model: data.model,
270
+ finish_reason: choice.finish_reason,
271
+ system_fingerprint: data.system_fingerprint,
272
+ citations: data.citations,
273
+ inline_citations: data.inline_citations
274
+ }
275
+ }
276
+ }
277
+ );
278
+ const usage = {
279
+ inputTokens: data.usage.prompt_tokens,
280
+ outputTokens: data.usage.completion_tokens,
281
+ totalTokens: data.usage.total_tokens
282
+ };
283
+ let stopReason = "end_turn";
284
+ switch (choice.finish_reason) {
285
+ case "stop":
286
+ stopReason = "end_turn";
287
+ break;
288
+ case "length":
289
+ stopReason = "max_tokens";
290
+ break;
291
+ case "tool_calls":
292
+ stopReason = "tool_use";
293
+ break;
294
+ case "content_filter":
295
+ stopReason = "content_filter";
296
+ break;
297
+ }
298
+ if (hadRefusal && stopReason !== "content_filter") {
299
+ stopReason = "content_filter";
300
+ }
301
+ return {
302
+ message,
303
+ usage,
304
+ stopReason,
305
+ data: structuredData
306
+ };
307
+ }
308
+ function createStreamState() {
309
+ return {
310
+ id: "",
311
+ model: "",
312
+ text: "",
313
+ toolCalls: /* @__PURE__ */ new Map(),
314
+ finishReason: null,
315
+ inputTokens: 0,
316
+ outputTokens: 0,
317
+ hadRefusal: false
318
+ };
319
+ }
320
+ function transformStreamEvent(chunk, state) {
321
+ const events = [];
322
+ if (chunk.id && !state.id) {
323
+ state.id = chunk.id;
324
+ events.push({ type: "message_start", index: 0, delta: {} });
325
+ }
326
+ if (chunk.model) {
327
+ state.model = chunk.model;
328
+ }
329
+ const choice = chunk.choices[0];
330
+ if (choice) {
331
+ if (choice.delta.content) {
332
+ state.text += choice.delta.content;
333
+ events.push({
334
+ type: "text_delta",
335
+ index: 0,
336
+ delta: { text: choice.delta.content }
337
+ });
338
+ }
339
+ if (choice.delta.refusal) {
340
+ state.hadRefusal = true;
341
+ state.text += choice.delta.refusal;
342
+ events.push({
343
+ type: "text_delta",
344
+ index: 0,
345
+ delta: { text: choice.delta.refusal }
346
+ });
347
+ }
348
+ if (choice.delta.tool_calls) {
349
+ for (const toolCallDelta of choice.delta.tool_calls) {
350
+ const index = toolCallDelta.index;
351
+ let toolCall = state.toolCalls.get(index);
352
+ if (!toolCall) {
353
+ toolCall = { id: "", name: "", arguments: "" };
354
+ state.toolCalls.set(index, toolCall);
355
+ }
356
+ if (toolCallDelta.id) {
357
+ toolCall.id = toolCallDelta.id;
358
+ }
359
+ if (toolCallDelta.function?.name) {
360
+ toolCall.name = toolCallDelta.function.name;
361
+ }
362
+ if (toolCallDelta.function?.arguments) {
363
+ toolCall.arguments += toolCallDelta.function.arguments;
364
+ events.push({
365
+ type: "tool_call_delta",
366
+ index,
367
+ delta: {
368
+ toolCallId: toolCall.id,
369
+ toolName: toolCall.name,
370
+ argumentsJson: toolCallDelta.function.arguments
371
+ }
372
+ });
373
+ }
374
+ }
375
+ }
376
+ if (choice.finish_reason) {
377
+ state.finishReason = choice.finish_reason;
378
+ events.push({ type: "message_stop", index: 0, delta: {} });
379
+ }
380
+ }
381
+ if (chunk.usage) {
382
+ state.inputTokens = chunk.usage.prompt_tokens;
383
+ state.outputTokens = chunk.usage.completion_tokens;
384
+ }
385
+ return events;
386
+ }
387
+ function buildResponseFromState(state) {
388
+ const textContent = [];
389
+ let structuredData;
390
+ if (state.text) {
391
+ textContent.push({ type: "text", text: state.text });
392
+ try {
393
+ structuredData = JSON.parse(state.text);
394
+ } catch {
395
+ }
396
+ }
397
+ const toolCalls = [];
398
+ for (const [, toolCall] of state.toolCalls) {
399
+ let args = {};
400
+ if (toolCall.arguments) {
401
+ try {
402
+ args = JSON.parse(toolCall.arguments);
403
+ } catch {
404
+ }
405
+ }
406
+ toolCalls.push({
407
+ toolCallId: toolCall.id,
408
+ toolName: toolCall.name,
409
+ arguments: args
410
+ });
411
+ }
412
+ const message = new AssistantMessage(
413
+ textContent,
414
+ toolCalls.length > 0 ? toolCalls : void 0,
415
+ {
416
+ id: state.id,
417
+ metadata: {
418
+ xai: {
419
+ model: state.model,
420
+ finish_reason: state.finishReason
421
+ }
422
+ }
423
+ }
424
+ );
425
+ const usage = {
426
+ inputTokens: state.inputTokens,
427
+ outputTokens: state.outputTokens,
428
+ totalTokens: state.inputTokens + state.outputTokens
429
+ };
430
+ let stopReason = "end_turn";
431
+ switch (state.finishReason) {
432
+ case "stop":
433
+ stopReason = "end_turn";
434
+ break;
435
+ case "length":
436
+ stopReason = "max_tokens";
437
+ break;
438
+ case "tool_calls":
439
+ stopReason = "tool_use";
440
+ break;
441
+ case "content_filter":
442
+ stopReason = "content_filter";
443
+ break;
444
+ }
445
+ if (state.hadRefusal && stopReason !== "content_filter") {
446
+ stopReason = "content_filter";
447
+ }
448
+ return {
449
+ message,
450
+ usage,
451
+ stopReason,
452
+ data: structuredData
453
+ };
454
+ }
455
+
456
+ // src/providers/xai/llm.completions.ts
457
+ var XAI_COMPLETIONS_API_URL = "https://api.x.ai/v1/chat/completions";
458
+ var XAI_COMPLETIONS_CAPABILITIES = {
459
+ streaming: true,
460
+ tools: true,
461
+ structuredOutput: true,
462
+ imageInput: true,
463
+ videoInput: false,
464
+ audioInput: false
465
+ };
466
+ function createCompletionsLLMHandler() {
467
+ let providerRef = null;
468
+ return {
469
+ _setProvider(provider) {
470
+ providerRef = provider;
471
+ },
472
+ bind(modelId) {
473
+ if (!providerRef) {
474
+ throw new UPPError(
475
+ "Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
476
+ "INVALID_REQUEST",
477
+ "xai",
478
+ "llm"
479
+ );
480
+ }
481
+ const model = {
482
+ modelId,
483
+ capabilities: XAI_COMPLETIONS_CAPABILITIES,
484
+ get provider() {
485
+ return providerRef;
486
+ },
487
+ async complete(request) {
488
+ const apiKey = await resolveApiKey(
489
+ request.config,
490
+ "XAI_API_KEY",
491
+ "xai",
492
+ "llm"
493
+ );
494
+ const baseUrl = request.config.baseUrl ?? XAI_COMPLETIONS_API_URL;
495
+ const body = transformRequest(request, modelId);
496
+ const response = await doFetch(
497
+ baseUrl,
498
+ {
499
+ method: "POST",
500
+ headers: {
501
+ "Content-Type": "application/json",
502
+ Authorization: `Bearer ${apiKey}`
503
+ },
504
+ body: JSON.stringify(body),
505
+ signal: request.signal
506
+ },
507
+ request.config,
508
+ "xai",
509
+ "llm"
510
+ );
511
+ const data = await response.json();
512
+ return transformResponse(data);
513
+ },
514
+ stream(request) {
515
+ const state = createStreamState();
516
+ let responseResolve;
517
+ let responseReject;
518
+ const responsePromise = new Promise((resolve, reject) => {
519
+ responseResolve = resolve;
520
+ responseReject = reject;
521
+ });
522
+ async function* generateEvents() {
523
+ try {
524
+ const apiKey = await resolveApiKey(
525
+ request.config,
526
+ "XAI_API_KEY",
527
+ "xai",
528
+ "llm"
529
+ );
530
+ const baseUrl = request.config.baseUrl ?? XAI_COMPLETIONS_API_URL;
531
+ const body = transformRequest(request, modelId);
532
+ body.stream = true;
533
+ body.stream_options = { include_usage: true };
534
+ const response = await doStreamFetch(
535
+ baseUrl,
536
+ {
537
+ method: "POST",
538
+ headers: {
539
+ "Content-Type": "application/json",
540
+ Authorization: `Bearer ${apiKey}`
541
+ },
542
+ body: JSON.stringify(body),
543
+ signal: request.signal
544
+ },
545
+ request.config,
546
+ "xai",
547
+ "llm"
548
+ );
549
+ if (!response.ok) {
550
+ const error = await normalizeHttpError(response, "xai", "llm");
551
+ responseReject(error);
552
+ throw error;
553
+ }
554
+ if (!response.body) {
555
+ const error = new UPPError(
556
+ "No response body for streaming request",
557
+ "PROVIDER_ERROR",
558
+ "xai",
559
+ "llm"
560
+ );
561
+ responseReject(error);
562
+ throw error;
563
+ }
564
+ for await (const data of parseSSEStream(response.body)) {
565
+ if (data === "[DONE]") {
566
+ continue;
567
+ }
568
+ if (typeof data === "object" && data !== null) {
569
+ const chunk = data;
570
+ if ("error" in chunk && chunk.error) {
571
+ const errorData = chunk.error;
572
+ const error = new UPPError(
573
+ errorData.message ?? "Unknown error",
574
+ "PROVIDER_ERROR",
575
+ "xai",
576
+ "llm"
577
+ );
578
+ responseReject(error);
579
+ throw error;
580
+ }
581
+ const uppEvents = transformStreamEvent(chunk, state);
582
+ for (const event of uppEvents) {
583
+ yield event;
584
+ }
585
+ }
586
+ }
587
+ responseResolve(buildResponseFromState(state));
588
+ } catch (error) {
589
+ responseReject(error);
590
+ throw error;
591
+ }
592
+ }
593
+ return {
594
+ [Symbol.asyncIterator]() {
595
+ return generateEvents();
596
+ },
597
+ response: responsePromise
598
+ };
599
+ }
600
+ };
601
+ return model;
602
+ }
603
+ };
604
+ }
605
+
606
+ // src/providers/xai/transform.responses.ts
607
+ function transformRequest2(request, modelId) {
608
+ const params = request.params ?? {};
609
+ const xaiRequest = {
610
+ model: modelId,
611
+ input: transformInputItems(request.messages, request.system)
612
+ };
613
+ if (params.temperature !== void 0) {
614
+ xaiRequest.temperature = params.temperature;
615
+ }
616
+ if (params.top_p !== void 0) {
617
+ xaiRequest.top_p = params.top_p;
618
+ }
619
+ if (params.max_output_tokens !== void 0) {
620
+ xaiRequest.max_output_tokens = params.max_output_tokens;
621
+ } else if (params.max_completion_tokens !== void 0) {
622
+ xaiRequest.max_output_tokens = params.max_completion_tokens;
623
+ } else if (params.max_tokens !== void 0) {
624
+ xaiRequest.max_output_tokens = params.max_tokens;
625
+ }
626
+ if (params.store !== void 0) {
627
+ xaiRequest.store = params.store;
628
+ }
629
+ if (params.metadata !== void 0) {
630
+ xaiRequest.metadata = params.metadata;
631
+ }
632
+ if (params.truncation !== void 0) {
633
+ xaiRequest.truncation = params.truncation;
634
+ }
635
+ if (params.include !== void 0) {
636
+ xaiRequest.include = params.include;
637
+ }
638
+ if (params.previous_response_id !== void 0) {
639
+ xaiRequest.previous_response_id = params.previous_response_id;
640
+ }
641
+ if (params.reasoning !== void 0) {
642
+ xaiRequest.reasoning = { ...params.reasoning };
643
+ }
644
+ if (params.reasoning_effort !== void 0) {
645
+ xaiRequest.reasoning = {
646
+ ...xaiRequest.reasoning ?? {},
647
+ effort: params.reasoning_effort
648
+ };
649
+ }
650
+ if (params.search_parameters !== void 0) {
651
+ xaiRequest.search_parameters = params.search_parameters;
652
+ }
653
+ if (request.tools && request.tools.length > 0) {
654
+ xaiRequest.tools = request.tools.map(transformTool2);
655
+ if (params.parallel_tool_calls !== void 0) {
656
+ xaiRequest.parallel_tool_calls = params.parallel_tool_calls;
657
+ }
658
+ }
659
+ if (request.structure) {
660
+ const schema = {
661
+ type: "object",
662
+ properties: request.structure.properties,
663
+ required: request.structure.required,
664
+ ...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
665
+ };
666
+ if (request.structure.description) {
667
+ schema.description = request.structure.description;
668
+ }
669
+ xaiRequest.text = {
670
+ format: {
671
+ type: "json_schema",
672
+ name: "json_response",
673
+ description: request.structure.description,
674
+ schema,
675
+ strict: true
676
+ }
677
+ };
678
+ }
679
+ return xaiRequest;
680
+ }
681
+ function transformInputItems(messages, system) {
682
+ const result = [];
683
+ if (system) {
684
+ result.push({
685
+ type: "message",
686
+ role: "system",
687
+ content: system
688
+ });
689
+ }
690
+ for (const message of messages) {
691
+ const items = transformMessage2(message);
692
+ result.push(...items);
693
+ }
694
+ if (result.length === 1 && result[0]?.type === "message") {
695
+ const item = result[0];
696
+ if (item.role === "user" && typeof item.content === "string") {
697
+ return item.content;
698
+ }
699
+ }
700
+ return result;
701
+ }
702
+ function filterValidContent2(content) {
703
+ return content.filter((c) => c && typeof c.type === "string");
704
+ }
705
+ function transformMessage2(message) {
706
+ if (isUserMessage(message)) {
707
+ const validContent = filterValidContent2(message.content);
708
+ if (validContent.length === 1 && validContent[0]?.type === "text") {
709
+ return [
710
+ {
711
+ type: "message",
712
+ role: "user",
713
+ content: validContent[0].text
714
+ }
715
+ ];
716
+ }
717
+ return [
718
+ {
719
+ type: "message",
720
+ role: "user",
721
+ content: validContent.map(transformContentPart)
722
+ }
723
+ ];
724
+ }
725
+ if (isAssistantMessage(message)) {
726
+ const validContent = filterValidContent2(message.content);
727
+ const items = [];
728
+ const textContent = validContent.filter((c) => c.type === "text").map((c) => c.text).join("\n\n");
729
+ if (textContent) {
730
+ items.push({
731
+ type: "message",
732
+ role: "assistant",
733
+ content: textContent
734
+ });
735
+ }
736
+ const xaiMeta = message.metadata?.xai;
737
+ const functionCallItems = xaiMeta?.functionCallItems;
738
+ if (functionCallItems && functionCallItems.length > 0) {
739
+ for (const fc of functionCallItems) {
740
+ items.push({
741
+ type: "function_call",
742
+ id: fc.id,
743
+ call_id: fc.call_id,
744
+ name: fc.name,
745
+ arguments: fc.arguments
746
+ });
747
+ }
748
+ } else if (message.toolCalls && message.toolCalls.length > 0) {
749
+ for (const call of message.toolCalls) {
750
+ items.push({
751
+ type: "function_call",
752
+ id: `fc_${call.toolCallId}`,
753
+ call_id: call.toolCallId,
754
+ name: call.toolName,
755
+ arguments: JSON.stringify(call.arguments)
756
+ });
757
+ }
758
+ }
759
+ return items;
760
+ }
761
+ if (isToolResultMessage(message)) {
762
+ return message.results.map((result) => ({
763
+ type: "function_call_output",
764
+ call_id: result.toolCallId,
765
+ output: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
766
+ }));
767
+ }
768
+ return [];
769
+ }
770
+ function transformContentPart(block) {
771
+ switch (block.type) {
772
+ case "text":
773
+ return { type: "input_text", text: block.text };
774
+ case "image": {
775
+ const imageBlock = block;
776
+ if (imageBlock.source.type === "base64") {
777
+ return {
778
+ type: "input_image",
779
+ image_url: `data:${imageBlock.mimeType};base64,${imageBlock.source.data}`
780
+ };
781
+ }
782
+ if (imageBlock.source.type === "url") {
783
+ return {
784
+ type: "input_image",
785
+ image_url: imageBlock.source.url
786
+ };
787
+ }
788
+ if (imageBlock.source.type === "bytes") {
789
+ const base64 = btoa(
790
+ Array.from(imageBlock.source.data).map((b) => String.fromCharCode(b)).join("")
791
+ );
792
+ return {
793
+ type: "input_image",
794
+ image_url: `data:${imageBlock.mimeType};base64,${base64}`
795
+ };
796
+ }
797
+ throw new Error("Unknown image source type");
798
+ }
799
+ default:
800
+ throw new Error(`Unsupported content type: ${block.type}`);
801
+ }
802
+ }
803
+ function transformTool2(tool) {
804
+ return {
805
+ type: "function",
806
+ name: tool.name,
807
+ description: tool.description,
808
+ parameters: {
809
+ type: "object",
810
+ properties: tool.parameters.properties,
811
+ required: tool.parameters.required,
812
+ ...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
813
+ }
814
+ };
815
+ }
816
+ function transformResponse2(data) {
817
+ const textContent = [];
818
+ const toolCalls = [];
819
+ const functionCallItems = [];
820
+ let hadRefusal = false;
821
+ let structuredData;
822
+ for (const item of data.output) {
823
+ if (item.type === "message") {
824
+ const messageItem = item;
825
+ for (const content of messageItem.content) {
826
+ if (content.type === "output_text") {
827
+ textContent.push({ type: "text", text: content.text });
828
+ if (structuredData === void 0) {
829
+ try {
830
+ structuredData = JSON.parse(content.text);
831
+ } catch {
832
+ }
833
+ }
834
+ } else if (content.type === "refusal") {
835
+ textContent.push({ type: "text", text: content.refusal });
836
+ hadRefusal = true;
837
+ }
838
+ }
839
+ } else if (item.type === "function_call") {
840
+ const functionCall = item;
841
+ let args = {};
842
+ try {
843
+ args = JSON.parse(functionCall.arguments);
844
+ } catch {
845
+ }
846
+ toolCalls.push({
847
+ toolCallId: functionCall.call_id,
848
+ toolName: functionCall.name,
849
+ arguments: args
850
+ });
851
+ functionCallItems.push({
852
+ id: functionCall.id,
853
+ call_id: functionCall.call_id,
854
+ name: functionCall.name,
855
+ arguments: functionCall.arguments
856
+ });
857
+ }
858
+ }
859
+ const message = new AssistantMessage(
860
+ textContent,
861
+ toolCalls.length > 0 ? toolCalls : void 0,
862
+ {
863
+ id: data.id,
864
+ metadata: {
865
+ xai: {
866
+ model: data.model,
867
+ status: data.status,
868
+ // Store response_id for multi-turn tool calling
869
+ response_id: data.id,
870
+ functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0,
871
+ citations: data.citations,
872
+ inline_citations: data.inline_citations
873
+ }
874
+ }
875
+ }
876
+ );
877
+ const usage = {
878
+ inputTokens: data.usage.input_tokens,
879
+ outputTokens: data.usage.output_tokens,
880
+ totalTokens: data.usage.total_tokens
881
+ };
882
+ let stopReason = "end_turn";
883
+ if (data.status === "completed") {
884
+ stopReason = toolCalls.length > 0 ? "tool_use" : "end_turn";
885
+ } else if (data.status === "incomplete") {
886
+ stopReason = data.incomplete_details?.reason === "max_output_tokens" ? "max_tokens" : "end_turn";
887
+ } else if (data.status === "failed") {
888
+ stopReason = "error";
889
+ }
890
+ if (hadRefusal && stopReason !== "error") {
891
+ stopReason = "content_filter";
892
+ }
893
+ return {
894
+ message,
895
+ usage,
896
+ stopReason,
897
+ data: structuredData
898
+ };
899
+ }
900
+ function createStreamState2() {
901
+ return {
902
+ id: "",
903
+ model: "",
904
+ textByIndex: /* @__PURE__ */ new Map(),
905
+ toolCalls: /* @__PURE__ */ new Map(),
906
+ status: "in_progress",
907
+ inputTokens: 0,
908
+ outputTokens: 0,
909
+ hadRefusal: false
910
+ };
911
+ }
912
+ function transformStreamEvent2(event, state) {
913
+ const events = [];
914
+ switch (event.type) {
915
+ case "response.created":
916
+ state.id = event.response.id;
917
+ state.model = event.response.model;
918
+ events.push({ type: "message_start", index: 0, delta: {} });
919
+ break;
920
+ case "response.in_progress":
921
+ state.status = "in_progress";
922
+ break;
923
+ case "response.completed":
924
+ state.status = "completed";
925
+ if (event.response.usage) {
926
+ state.inputTokens = event.response.usage.input_tokens;
927
+ state.outputTokens = event.response.usage.output_tokens;
928
+ }
929
+ events.push({ type: "message_stop", index: 0, delta: {} });
930
+ break;
931
+ case "response.failed":
932
+ state.status = "failed";
933
+ events.push({ type: "message_stop", index: 0, delta: {} });
934
+ break;
935
+ case "response.output_item.added":
936
+ if (event.item.type === "function_call") {
937
+ const functionCall = event.item;
938
+ const existing = state.toolCalls.get(event.output_index) ?? {
939
+ arguments: ""
940
+ };
941
+ existing.itemId = functionCall.id;
942
+ existing.callId = functionCall.call_id;
943
+ existing.name = functionCall.name;
944
+ if (functionCall.arguments) {
945
+ existing.arguments = functionCall.arguments;
946
+ }
947
+ state.toolCalls.set(event.output_index, existing);
948
+ }
949
+ events.push({
950
+ type: "content_block_start",
951
+ index: event.output_index,
952
+ delta: {}
953
+ });
954
+ break;
955
+ case "response.output_item.done":
956
+ if (event.item.type === "function_call") {
957
+ const functionCall = event.item;
958
+ const existing = state.toolCalls.get(event.output_index) ?? {
959
+ arguments: ""
960
+ };
961
+ existing.itemId = functionCall.id;
962
+ existing.callId = functionCall.call_id;
963
+ existing.name = functionCall.name;
964
+ if (functionCall.arguments) {
965
+ existing.arguments = functionCall.arguments;
966
+ }
967
+ state.toolCalls.set(event.output_index, existing);
968
+ }
969
+ events.push({
970
+ type: "content_block_stop",
971
+ index: event.output_index,
972
+ delta: {}
973
+ });
974
+ break;
975
+ case "response.output_text.delta":
976
+ const currentText = state.textByIndex.get(event.output_index) ?? "";
977
+ state.textByIndex.set(event.output_index, currentText + event.delta);
978
+ events.push({
979
+ type: "text_delta",
980
+ index: event.output_index,
981
+ delta: { text: event.delta }
982
+ });
983
+ break;
984
+ case "response.output_text.done":
985
+ state.textByIndex.set(event.output_index, event.text);
986
+ break;
987
+ case "response.refusal.delta": {
988
+ state.hadRefusal = true;
989
+ const currentRefusal = state.textByIndex.get(event.output_index) ?? "";
990
+ state.textByIndex.set(event.output_index, currentRefusal + event.delta);
991
+ events.push({
992
+ type: "text_delta",
993
+ index: event.output_index,
994
+ delta: { text: event.delta }
995
+ });
996
+ break;
997
+ }
998
+ case "response.refusal.done":
999
+ state.hadRefusal = true;
1000
+ state.textByIndex.set(event.output_index, event.refusal);
1001
+ break;
1002
+ case "response.function_call_arguments.delta": {
1003
+ let toolCall = state.toolCalls.get(event.output_index);
1004
+ if (!toolCall) {
1005
+ toolCall = { arguments: "" };
1006
+ state.toolCalls.set(event.output_index, toolCall);
1007
+ }
1008
+ if (event.item_id && !toolCall.itemId) {
1009
+ toolCall.itemId = event.item_id;
1010
+ }
1011
+ if (event.call_id && !toolCall.callId) {
1012
+ toolCall.callId = event.call_id;
1013
+ }
1014
+ toolCall.arguments += event.delta;
1015
+ events.push({
1016
+ type: "tool_call_delta",
1017
+ index: event.output_index,
1018
+ delta: {
1019
+ toolCallId: toolCall.callId ?? toolCall.itemId ?? "",
1020
+ toolName: toolCall.name,
1021
+ argumentsJson: event.delta
1022
+ }
1023
+ });
1024
+ break;
1025
+ }
1026
+ case "response.function_call_arguments.done": {
1027
+ let toolCall = state.toolCalls.get(event.output_index);
1028
+ if (!toolCall) {
1029
+ toolCall = { arguments: "" };
1030
+ state.toolCalls.set(event.output_index, toolCall);
1031
+ }
1032
+ if (event.item_id) {
1033
+ toolCall.itemId = event.item_id;
1034
+ }
1035
+ if (event.call_id) {
1036
+ toolCall.callId = event.call_id;
1037
+ }
1038
+ toolCall.name = event.name;
1039
+ toolCall.arguments = event.arguments;
1040
+ break;
1041
+ }
1042
+ case "error":
1043
+ break;
1044
+ default:
1045
+ break;
1046
+ }
1047
+ return events;
1048
+ }
1049
+ function buildResponseFromState2(state) {
1050
+ const textContent = [];
1051
+ let structuredData;
1052
+ for (const [, text] of state.textByIndex) {
1053
+ if (text) {
1054
+ textContent.push({ type: "text", text });
1055
+ if (structuredData === void 0) {
1056
+ try {
1057
+ structuredData = JSON.parse(text);
1058
+ } catch {
1059
+ }
1060
+ }
1061
+ }
1062
+ }
1063
+ const toolCalls = [];
1064
+ const functionCallItems = [];
1065
+ for (const [, toolCall] of state.toolCalls) {
1066
+ let args = {};
1067
+ if (toolCall.arguments) {
1068
+ try {
1069
+ args = JSON.parse(toolCall.arguments);
1070
+ } catch {
1071
+ }
1072
+ }
1073
+ const itemId = toolCall.itemId ?? "";
1074
+ const callId = toolCall.callId ?? toolCall.itemId ?? "";
1075
+ const name = toolCall.name ?? "";
1076
+ toolCalls.push({
1077
+ toolCallId: callId,
1078
+ toolName: name,
1079
+ arguments: args
1080
+ });
1081
+ if (itemId && callId && name) {
1082
+ functionCallItems.push({
1083
+ id: itemId,
1084
+ call_id: callId,
1085
+ name,
1086
+ arguments: toolCall.arguments
1087
+ });
1088
+ }
1089
+ }
1090
+ const message = new AssistantMessage(
1091
+ textContent,
1092
+ toolCalls.length > 0 ? toolCalls : void 0,
1093
+ {
1094
+ id: state.id,
1095
+ metadata: {
1096
+ xai: {
1097
+ model: state.model,
1098
+ status: state.status,
1099
+ // Store response_id for multi-turn tool calling
1100
+ response_id: state.id,
1101
+ functionCallItems: functionCallItems.length > 0 ? functionCallItems : void 0
1102
+ }
1103
+ }
1104
+ }
1105
+ );
1106
+ const usage = {
1107
+ inputTokens: state.inputTokens,
1108
+ outputTokens: state.outputTokens,
1109
+ totalTokens: state.inputTokens + state.outputTokens
1110
+ };
1111
+ let stopReason = "end_turn";
1112
+ if (state.status === "completed") {
1113
+ stopReason = toolCalls.length > 0 ? "tool_use" : "end_turn";
1114
+ } else if (state.status === "failed") {
1115
+ stopReason = "error";
1116
+ }
1117
+ if (state.hadRefusal && stopReason !== "error") {
1118
+ stopReason = "content_filter";
1119
+ }
1120
+ return {
1121
+ message,
1122
+ usage,
1123
+ stopReason,
1124
+ data: structuredData
1125
+ };
1126
+ }
1127
+
1128
+ // src/providers/xai/llm.responses.ts
1129
+ var XAI_RESPONSES_API_URL = "https://api.x.ai/v1/responses";
1130
+ var XAI_RESPONSES_CAPABILITIES = {
1131
+ streaming: true,
1132
+ tools: true,
1133
+ structuredOutput: true,
1134
+ imageInput: true,
1135
+ videoInput: false,
1136
+ audioInput: false
1137
+ };
1138
+ function createResponsesLLMHandler() {
1139
+ let providerRef = null;
1140
+ return {
1141
+ _setProvider(provider) {
1142
+ providerRef = provider;
1143
+ },
1144
+ bind(modelId) {
1145
+ if (!providerRef) {
1146
+ throw new UPPError(
1147
+ "Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
1148
+ "INVALID_REQUEST",
1149
+ "xai",
1150
+ "llm"
1151
+ );
1152
+ }
1153
+ const model = {
1154
+ modelId,
1155
+ capabilities: XAI_RESPONSES_CAPABILITIES,
1156
+ get provider() {
1157
+ return providerRef;
1158
+ },
1159
+ async complete(request) {
1160
+ const apiKey = await resolveApiKey(
1161
+ request.config,
1162
+ "XAI_API_KEY",
1163
+ "xai",
1164
+ "llm"
1165
+ );
1166
+ const baseUrl = request.config.baseUrl ?? XAI_RESPONSES_API_URL;
1167
+ const body = transformRequest2(request, modelId);
1168
+ const response = await doFetch(
1169
+ baseUrl,
1170
+ {
1171
+ method: "POST",
1172
+ headers: {
1173
+ "Content-Type": "application/json",
1174
+ Authorization: `Bearer ${apiKey}`
1175
+ },
1176
+ body: JSON.stringify(body),
1177
+ signal: request.signal
1178
+ },
1179
+ request.config,
1180
+ "xai",
1181
+ "llm"
1182
+ );
1183
+ const data = await response.json();
1184
+ if (data.status === "failed" && data.error) {
1185
+ throw new UPPError(
1186
+ data.error.message,
1187
+ "PROVIDER_ERROR",
1188
+ "xai",
1189
+ "llm"
1190
+ );
1191
+ }
1192
+ return transformResponse2(data);
1193
+ },
1194
+ stream(request) {
1195
+ const state = createStreamState2();
1196
+ let responseResolve;
1197
+ let responseReject;
1198
+ const responsePromise = new Promise((resolve, reject) => {
1199
+ responseResolve = resolve;
1200
+ responseReject = reject;
1201
+ });
1202
+ async function* generateEvents() {
1203
+ try {
1204
+ const apiKey = await resolveApiKey(
1205
+ request.config,
1206
+ "XAI_API_KEY",
1207
+ "xai",
1208
+ "llm"
1209
+ );
1210
+ const baseUrl = request.config.baseUrl ?? XAI_RESPONSES_API_URL;
1211
+ const body = transformRequest2(request, modelId);
1212
+ body.stream = true;
1213
+ const response = await doStreamFetch(
1214
+ baseUrl,
1215
+ {
1216
+ method: "POST",
1217
+ headers: {
1218
+ "Content-Type": "application/json",
1219
+ Authorization: `Bearer ${apiKey}`
1220
+ },
1221
+ body: JSON.stringify(body),
1222
+ signal: request.signal
1223
+ },
1224
+ request.config,
1225
+ "xai",
1226
+ "llm"
1227
+ );
1228
+ if (!response.ok) {
1229
+ const error = await normalizeHttpError(response, "xai", "llm");
1230
+ responseReject(error);
1231
+ throw error;
1232
+ }
1233
+ if (!response.body) {
1234
+ const error = new UPPError(
1235
+ "No response body for streaming request",
1236
+ "PROVIDER_ERROR",
1237
+ "xai",
1238
+ "llm"
1239
+ );
1240
+ responseReject(error);
1241
+ throw error;
1242
+ }
1243
+ for await (const data of parseSSEStream(response.body)) {
1244
+ if (data === "[DONE]") {
1245
+ continue;
1246
+ }
1247
+ if (typeof data === "object" && data !== null) {
1248
+ const event = data;
1249
+ if (event.type === "error") {
1250
+ const errorEvent = event;
1251
+ const error = new UPPError(
1252
+ errorEvent.error.message,
1253
+ "PROVIDER_ERROR",
1254
+ "xai",
1255
+ "llm"
1256
+ );
1257
+ responseReject(error);
1258
+ throw error;
1259
+ }
1260
+ const uppEvents = transformStreamEvent2(event, state);
1261
+ for (const uppEvent of uppEvents) {
1262
+ yield uppEvent;
1263
+ }
1264
+ }
1265
+ }
1266
+ responseResolve(buildResponseFromState2(state));
1267
+ } catch (error) {
1268
+ responseReject(error);
1269
+ throw error;
1270
+ }
1271
+ }
1272
+ return {
1273
+ [Symbol.asyncIterator]() {
1274
+ return generateEvents();
1275
+ },
1276
+ response: responsePromise
1277
+ };
1278
+ }
1279
+ };
1280
+ return model;
1281
+ }
1282
+ };
1283
+ }
1284
+
1285
+ // src/providers/xai/transform.messages.ts
1286
+ function transformRequest3(request, modelId) {
1287
+ const params = request.params ?? {};
1288
+ const xaiRequest = {
1289
+ model: modelId,
1290
+ messages: request.messages.map(transformMessage3)
1291
+ };
1292
+ if (params.max_tokens !== void 0) {
1293
+ xaiRequest.max_tokens = params.max_tokens;
1294
+ }
1295
+ if (request.system) {
1296
+ xaiRequest.system = request.system;
1297
+ }
1298
+ if (params.temperature !== void 0) {
1299
+ xaiRequest.temperature = params.temperature;
1300
+ }
1301
+ if (params.top_p !== void 0) {
1302
+ xaiRequest.top_p = params.top_p;
1303
+ }
1304
+ if (params.top_k !== void 0) {
1305
+ xaiRequest.top_k = params.top_k;
1306
+ }
1307
+ if (params.stop_sequences) {
1308
+ xaiRequest.stop_sequences = params.stop_sequences;
1309
+ }
1310
+ if (params.messages_metadata) {
1311
+ xaiRequest.metadata = params.messages_metadata;
1312
+ }
1313
+ if (params.thinking) {
1314
+ xaiRequest.thinking = params.thinking;
1315
+ }
1316
+ if (request.tools && request.tools.length > 0) {
1317
+ xaiRequest.tools = request.tools.map(transformTool3);
1318
+ xaiRequest.tool_choice = { type: "auto" };
1319
+ }
1320
+ if (request.structure) {
1321
+ const structuredTool = {
1322
+ name: "json_response",
1323
+ description: "Return the response in the specified JSON format. You MUST use this tool to provide your response.",
1324
+ input_schema: {
1325
+ type: "object",
1326
+ properties: request.structure.properties,
1327
+ required: request.structure.required
1328
+ }
1329
+ };
1330
+ xaiRequest.tools = [...xaiRequest.tools ?? [], structuredTool];
1331
+ xaiRequest.tool_choice = { type: "tool", name: "json_response" };
1332
+ }
1333
+ return xaiRequest;
1334
+ }
1335
+ function filterValidContent3(content) {
1336
+ return content.filter((c) => c && typeof c.type === "string");
1337
+ }
1338
+ function transformMessage3(message) {
1339
+ if (isUserMessage(message)) {
1340
+ const validContent = filterValidContent3(message.content);
1341
+ return {
1342
+ role: "user",
1343
+ content: validContent.map(transformContentBlock2)
1344
+ };
1345
+ }
1346
+ if (isAssistantMessage(message)) {
1347
+ const validContent = filterValidContent3(message.content);
1348
+ const content = validContent.map(transformContentBlock2);
1349
+ if (message.toolCalls) {
1350
+ for (const call of message.toolCalls) {
1351
+ content.push({
1352
+ type: "tool_use",
1353
+ id: call.toolCallId,
1354
+ name: call.toolName,
1355
+ input: call.arguments
1356
+ });
1357
+ }
1358
+ }
1359
+ if (content.length === 0) {
1360
+ content.push({ type: "text", text: "" });
1361
+ }
1362
+ return {
1363
+ role: "assistant",
1364
+ content
1365
+ };
1366
+ }
1367
+ if (isToolResultMessage(message)) {
1368
+ return {
1369
+ role: "user",
1370
+ content: message.results.map((result) => ({
1371
+ type: "tool_result",
1372
+ tool_use_id: result.toolCallId,
1373
+ content: typeof result.result === "string" ? result.result : JSON.stringify(result.result),
1374
+ is_error: result.isError
1375
+ }))
1376
+ };
1377
+ }
1378
+ throw new Error(`Unknown message type: ${message.type}`);
1379
+ }
1380
+ function transformContentBlock2(block) {
1381
+ switch (block.type) {
1382
+ case "text":
1383
+ return { type: "text", text: block.text };
1384
+ case "image": {
1385
+ const imageBlock = block;
1386
+ if (imageBlock.source.type === "base64") {
1387
+ return {
1388
+ type: "image",
1389
+ source: {
1390
+ type: "base64",
1391
+ media_type: imageBlock.mimeType,
1392
+ data: imageBlock.source.data
1393
+ }
1394
+ };
1395
+ }
1396
+ if (imageBlock.source.type === "url") {
1397
+ return {
1398
+ type: "image",
1399
+ source: {
1400
+ type: "url",
1401
+ url: imageBlock.source.url
1402
+ }
1403
+ };
1404
+ }
1405
+ if (imageBlock.source.type === "bytes") {
1406
+ const base64 = btoa(
1407
+ Array.from(imageBlock.source.data).map((b) => String.fromCharCode(b)).join("")
1408
+ );
1409
+ return {
1410
+ type: "image",
1411
+ source: {
1412
+ type: "base64",
1413
+ media_type: imageBlock.mimeType,
1414
+ data: base64
1415
+ }
1416
+ };
1417
+ }
1418
+ throw new Error(`Unknown image source type`);
1419
+ }
1420
+ default:
1421
+ throw new Error(`Unsupported content type: ${block.type}`);
1422
+ }
1423
+ }
1424
+ function transformTool3(tool) {
1425
+ return {
1426
+ name: tool.name,
1427
+ description: tool.description,
1428
+ input_schema: {
1429
+ type: "object",
1430
+ properties: tool.parameters.properties,
1431
+ required: tool.parameters.required
1432
+ }
1433
+ };
1434
+ }
1435
+ function transformResponse3(data) {
1436
+ const textContent = [];
1437
+ const toolCalls = [];
1438
+ let structuredData;
1439
+ for (const block of data.content) {
1440
+ if (block.type === "text") {
1441
+ textContent.push({ type: "text", text: block.text });
1442
+ } else if (block.type === "tool_use") {
1443
+ if (block.name === "json_response") {
1444
+ structuredData = block.input;
1445
+ }
1446
+ toolCalls.push({
1447
+ toolCallId: block.id,
1448
+ toolName: block.name,
1449
+ arguments: block.input
1450
+ });
1451
+ }
1452
+ }
1453
+ const message = new AssistantMessage(
1454
+ textContent,
1455
+ toolCalls.length > 0 ? toolCalls : void 0,
1456
+ {
1457
+ id: data.id,
1458
+ metadata: {
1459
+ xai: {
1460
+ stop_reason: data.stop_reason,
1461
+ stop_sequence: data.stop_sequence,
1462
+ model: data.model
1463
+ }
1464
+ }
1465
+ }
1466
+ );
1467
+ const usage = {
1468
+ inputTokens: data.usage.input_tokens,
1469
+ outputTokens: data.usage.output_tokens,
1470
+ totalTokens: data.usage.input_tokens + data.usage.output_tokens
1471
+ };
1472
+ return {
1473
+ message,
1474
+ usage,
1475
+ stopReason: data.stop_reason ?? "end_turn",
1476
+ data: structuredData
1477
+ };
1478
+ }
1479
+ function createStreamState3() {
1480
+ return {
1481
+ messageId: "",
1482
+ model: "",
1483
+ content: [],
1484
+ stopReason: null,
1485
+ inputTokens: 0,
1486
+ outputTokens: 0,
1487
+ currentIndex: 0
1488
+ };
1489
+ }
1490
+ function transformStreamEvent3(event, state) {
1491
+ switch (event.type) {
1492
+ case "message_start":
1493
+ state.messageId = event.message.id;
1494
+ state.model = event.message.model;
1495
+ state.inputTokens = event.message.usage.input_tokens;
1496
+ return { type: "message_start", index: 0, delta: {} };
1497
+ case "content_block_start":
1498
+ state.currentIndex = event.index;
1499
+ if (event.content_block.type === "text") {
1500
+ state.content[event.index] = { type: "text", text: "" };
1501
+ } else if (event.content_block.type === "tool_use") {
1502
+ state.content[event.index] = {
1503
+ type: "tool_use",
1504
+ id: event.content_block.id,
1505
+ name: event.content_block.name,
1506
+ input: ""
1507
+ };
1508
+ }
1509
+ return { type: "content_block_start", index: event.index, delta: {} };
1510
+ case "content_block_delta": {
1511
+ const delta = event.delta;
1512
+ const index = event.index ?? state.currentIndex;
1513
+ if (delta.type === "text_delta") {
1514
+ if (!state.content[index]) {
1515
+ state.content[index] = { type: "text", text: "" };
1516
+ }
1517
+ state.content[index].text = (state.content[index].text ?? "") + delta.text;
1518
+ return {
1519
+ type: "text_delta",
1520
+ index,
1521
+ delta: { text: delta.text }
1522
+ };
1523
+ }
1524
+ if (delta.type === "input_json_delta") {
1525
+ if (!state.content[index]) {
1526
+ state.content[index] = { type: "tool_use", id: "", name: "", input: "" };
1527
+ }
1528
+ state.content[index].input = (state.content[index].input ?? "") + delta.partial_json;
1529
+ return {
1530
+ type: "tool_call_delta",
1531
+ index,
1532
+ delta: {
1533
+ argumentsJson: delta.partial_json,
1534
+ toolCallId: state.content[index]?.id,
1535
+ toolName: state.content[index]?.name
1536
+ }
1537
+ };
1538
+ }
1539
+ if (delta.type === "thinking_delta") {
1540
+ return {
1541
+ type: "reasoning_delta",
1542
+ index,
1543
+ delta: { text: delta.thinking }
1544
+ };
1545
+ }
1546
+ return null;
1547
+ }
1548
+ case "content_block_stop":
1549
+ return { type: "content_block_stop", index: event.index ?? state.currentIndex, delta: {} };
1550
+ case "message_delta":
1551
+ state.stopReason = event.delta.stop_reason;
1552
+ state.outputTokens = event.usage.output_tokens;
1553
+ return null;
1554
+ case "message_stop":
1555
+ return { type: "message_stop", index: 0, delta: {} };
1556
+ case "ping":
1557
+ case "error":
1558
+ return null;
1559
+ default:
1560
+ return null;
1561
+ }
1562
+ }
1563
+ function buildResponseFromState3(state) {
1564
+ const textContent = [];
1565
+ const toolCalls = [];
1566
+ let structuredData;
1567
+ for (const block of state.content) {
1568
+ if (block.type === "text" && block.text) {
1569
+ textContent.push({ type: "text", text: block.text });
1570
+ } else if (block.type === "tool_use" && block.id && block.name) {
1571
+ let args = {};
1572
+ if (block.input) {
1573
+ try {
1574
+ args = JSON.parse(block.input);
1575
+ } catch {
1576
+ }
1577
+ }
1578
+ if (block.name === "json_response") {
1579
+ structuredData = args;
1580
+ }
1581
+ toolCalls.push({
1582
+ toolCallId: block.id,
1583
+ toolName: block.name,
1584
+ arguments: args
1585
+ });
1586
+ }
1587
+ }
1588
+ const message = new AssistantMessage(
1589
+ textContent,
1590
+ toolCalls.length > 0 ? toolCalls : void 0,
1591
+ {
1592
+ id: state.messageId,
1593
+ metadata: {
1594
+ xai: {
1595
+ stop_reason: state.stopReason,
1596
+ model: state.model
1597
+ }
1598
+ }
1599
+ }
1600
+ );
1601
+ const usage = {
1602
+ inputTokens: state.inputTokens,
1603
+ outputTokens: state.outputTokens,
1604
+ totalTokens: state.inputTokens + state.outputTokens
1605
+ };
1606
+ return {
1607
+ message,
1608
+ usage,
1609
+ stopReason: state.stopReason ?? "end_turn",
1610
+ data: structuredData
1611
+ };
1612
+ }
1613
+
1614
+ // src/providers/xai/llm.messages.ts
1615
+ var XAI_MESSAGES_API_URL = "https://api.x.ai/v1/messages";
1616
+ var XAI_MESSAGES_CAPABILITIES = {
1617
+ streaming: true,
1618
+ tools: true,
1619
+ structuredOutput: true,
1620
+ imageInput: true,
1621
+ videoInput: false,
1622
+ audioInput: false
1623
+ };
1624
+ function createMessagesLLMHandler() {
1625
+ let providerRef = null;
1626
+ return {
1627
+ _setProvider(provider) {
1628
+ providerRef = provider;
1629
+ },
1630
+ bind(modelId) {
1631
+ if (!providerRef) {
1632
+ throw new UPPError(
1633
+ "Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
1634
+ "INVALID_REQUEST",
1635
+ "xai",
1636
+ "llm"
1637
+ );
1638
+ }
1639
+ const model = {
1640
+ modelId,
1641
+ capabilities: XAI_MESSAGES_CAPABILITIES,
1642
+ get provider() {
1643
+ return providerRef;
1644
+ },
1645
+ async complete(request) {
1646
+ const apiKey = await resolveApiKey(
1647
+ request.config,
1648
+ "XAI_API_KEY",
1649
+ "xai",
1650
+ "llm"
1651
+ );
1652
+ const baseUrl = request.config.baseUrl ?? XAI_MESSAGES_API_URL;
1653
+ const body = transformRequest3(request, modelId);
1654
+ const response = await doFetch(
1655
+ baseUrl,
1656
+ {
1657
+ method: "POST",
1658
+ headers: {
1659
+ "Content-Type": "application/json",
1660
+ "x-api-key": apiKey,
1661
+ "anthropic-version": "2023-06-01"
1662
+ },
1663
+ body: JSON.stringify(body),
1664
+ signal: request.signal
1665
+ },
1666
+ request.config,
1667
+ "xai",
1668
+ "llm"
1669
+ );
1670
+ const data = await response.json();
1671
+ return transformResponse3(data);
1672
+ },
1673
+ stream(request) {
1674
+ const state = createStreamState3();
1675
+ let responseResolve;
1676
+ let responseReject;
1677
+ const responsePromise = new Promise((resolve, reject) => {
1678
+ responseResolve = resolve;
1679
+ responseReject = reject;
1680
+ });
1681
+ async function* generateEvents() {
1682
+ try {
1683
+ const apiKey = await resolveApiKey(
1684
+ request.config,
1685
+ "XAI_API_KEY",
1686
+ "xai",
1687
+ "llm"
1688
+ );
1689
+ const baseUrl = request.config.baseUrl ?? XAI_MESSAGES_API_URL;
1690
+ const body = transformRequest3(request, modelId);
1691
+ body.stream = true;
1692
+ const response = await doStreamFetch(
1693
+ baseUrl,
1694
+ {
1695
+ method: "POST",
1696
+ headers: {
1697
+ "Content-Type": "application/json",
1698
+ "x-api-key": apiKey,
1699
+ "anthropic-version": "2023-06-01"
1700
+ },
1701
+ body: JSON.stringify(body),
1702
+ signal: request.signal
1703
+ },
1704
+ request.config,
1705
+ "xai",
1706
+ "llm"
1707
+ );
1708
+ if (!response.ok) {
1709
+ const error = await normalizeHttpError(response, "xai", "llm");
1710
+ responseReject(error);
1711
+ throw error;
1712
+ }
1713
+ if (!response.body) {
1714
+ const error = new UPPError(
1715
+ "No response body for streaming request",
1716
+ "PROVIDER_ERROR",
1717
+ "xai",
1718
+ "llm"
1719
+ );
1720
+ responseReject(error);
1721
+ throw error;
1722
+ }
1723
+ for await (const data of parseSSEStream(response.body)) {
1724
+ if (typeof data === "object" && data !== null && "type" in data) {
1725
+ const event = data;
1726
+ if (event.type === "error") {
1727
+ const error = new UPPError(
1728
+ event.error.message,
1729
+ "PROVIDER_ERROR",
1730
+ "xai",
1731
+ "llm"
1732
+ );
1733
+ responseReject(error);
1734
+ throw error;
1735
+ }
1736
+ const uppEvent = transformStreamEvent3(event, state);
1737
+ if (uppEvent) {
1738
+ yield uppEvent;
1739
+ }
1740
+ }
1741
+ }
1742
+ responseResolve(buildResponseFromState3(state));
1743
+ } catch (error) {
1744
+ responseReject(error);
1745
+ throw error;
1746
+ }
1747
+ }
1748
+ return {
1749
+ [Symbol.asyncIterator]() {
1750
+ return generateEvents();
1751
+ },
1752
+ response: responsePromise
1753
+ };
1754
+ }
1755
+ };
1756
+ return model;
1757
+ }
1758
+ };
1759
+ }
1760
+
1761
+ // src/providers/xai/index.ts
1762
+ function createXAIProvider() {
1763
+ let currentApiMode = "completions";
1764
+ const completionsHandler = createCompletionsLLMHandler();
1765
+ const responsesHandler = createResponsesLLMHandler();
1766
+ const messagesHandler = createMessagesLLMHandler();
1767
+ const fn = function(modelId, options) {
1768
+ const apiMode = options?.api ?? "completions";
1769
+ currentApiMode = apiMode;
1770
+ return { modelId, provider };
1771
+ };
1772
+ const modalities = {
1773
+ get llm() {
1774
+ switch (currentApiMode) {
1775
+ case "responses":
1776
+ return responsesHandler;
1777
+ case "messages":
1778
+ return messagesHandler;
1779
+ case "completions":
1780
+ default:
1781
+ return completionsHandler;
1782
+ }
1783
+ }
1784
+ };
1785
+ Object.defineProperties(fn, {
1786
+ name: {
1787
+ value: "xai",
1788
+ writable: false,
1789
+ configurable: true
1790
+ },
1791
+ version: {
1792
+ value: "1.0.0",
1793
+ writable: false,
1794
+ configurable: true
1795
+ },
1796
+ modalities: {
1797
+ value: modalities,
1798
+ writable: false,
1799
+ configurable: true
1800
+ }
1801
+ });
1802
+ const provider = fn;
1803
+ completionsHandler._setProvider?.(provider);
1804
+ responsesHandler._setProvider?.(provider);
1805
+ messagesHandler._setProvider?.(provider);
1806
+ return provider;
1807
+ }
1808
+ var xai = createXAIProvider();
1809
+ export {
1810
+ xai
1811
+ };
1812
+ //# sourceMappingURL=index.js.map