@providerprotocol/ai 0.0.34 → 0.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +546 -3
- package/dist/anthropic/index.d.ts +2 -1
- package/dist/anthropic/index.js +151 -145
- package/dist/anthropic/index.js.map +1 -1
- package/dist/cerebras/index.d.ts +392 -0
- package/dist/cerebras/index.js +648 -0
- package/dist/cerebras/index.js.map +1 -0
- package/dist/chunk-2YXFLRQ6.js +49 -0
- package/dist/chunk-2YXFLRQ6.js.map +1 -0
- package/dist/chunk-4OGB7JZA.js +157 -0
- package/dist/chunk-4OGB7JZA.js.map +1 -0
- package/dist/chunk-4RX4VQCB.js +31 -0
- package/dist/chunk-4RX4VQCB.js.map +1 -0
- package/dist/chunk-5IWHCXKN.js +30 -0
- package/dist/chunk-5IWHCXKN.js.map +1 -0
- package/dist/{chunk-3C7O2RNO.js → chunk-A2IM7PGT.js} +6 -4
- package/dist/{chunk-3C7O2RNO.js.map → chunk-A2IM7PGT.js.map} +1 -1
- package/dist/{chunk-3D6XGGVG.js → chunk-ARVM24K2.js} +2 -2
- package/dist/{chunk-4J6OFUKX.js → chunk-AY55T37A.js} +70 -162
- package/dist/chunk-AY55T37A.js.map +1 -0
- package/dist/{chunk-ILR2D5PN.js → chunk-BRP5XJ6Q.js} +2 -86
- package/dist/chunk-BRP5XJ6Q.js.map +1 -0
- package/dist/chunk-C4JP64VW.js +298 -0
- package/dist/chunk-C4JP64VW.js.map +1 -0
- package/dist/chunk-COS4ON4G.js +111 -0
- package/dist/chunk-COS4ON4G.js.map +1 -0
- package/dist/chunk-CRP6Y7NF.js +31 -0
- package/dist/chunk-CRP6Y7NF.js.map +1 -0
- package/dist/chunk-EPB3GQNL.js +118 -0
- package/dist/chunk-EPB3GQNL.js.map +1 -0
- package/dist/chunk-ETBFOLQN.js +34 -0
- package/dist/chunk-ETBFOLQN.js.map +1 -0
- package/dist/chunk-I53CI6ZZ.js +142 -0
- package/dist/chunk-I53CI6ZZ.js.map +1 -0
- package/dist/chunk-IDZOVWP3.js +29 -0
- package/dist/chunk-IDZOVWP3.js.map +1 -0
- package/dist/chunk-JA3UZALR.js +88 -0
- package/dist/chunk-JA3UZALR.js.map +1 -0
- package/dist/{chunk-WAKD3OO5.js → chunk-N5DX5JW3.js} +31 -31
- package/dist/chunk-N5DX5JW3.js.map +1 -0
- package/dist/chunk-OIEWDFQU.js +97 -0
- package/dist/chunk-OIEWDFQU.js.map +1 -0
- package/dist/{chunk-TOJCZMVU.js → chunk-PMK5LZ5Z.js} +40 -40
- package/dist/chunk-PMK5LZ5Z.js.map +1 -0
- package/dist/{chunk-6S222DHN.js → chunk-RJGTRQ47.js} +20 -1
- package/dist/chunk-RJGTRQ47.js.map +1 -0
- package/dist/chunk-UFFJDYCE.js +94 -0
- package/dist/chunk-UFFJDYCE.js.map +1 -0
- package/dist/chunk-VGKZIGVI.js +222 -0
- package/dist/chunk-VGKZIGVI.js.map +1 -0
- package/dist/{chunk-KUPF5KHT.js → chunk-Y5H7C5J4.js} +2 -2
- package/dist/{embedding-D2BYIehX.d.ts → embedding-BXA72PlJ.d.ts} +1 -1
- package/dist/google/index.d.ts +2 -1
- package/dist/google/index.js +202 -199
- package/dist/google/index.js.map +1 -1
- package/dist/groq/index.d.ts +410 -0
- package/dist/groq/index.js +649 -0
- package/dist/groq/index.js.map +1 -0
- package/dist/http/index.d.ts +3 -2
- package/dist/http/index.js +5 -4
- package/dist/image-stream-CCgwB7ve.d.ts +11 -0
- package/dist/index.d.ts +8 -118
- package/dist/index.js +520 -769
- package/dist/index.js.map +1 -1
- package/dist/{llm-BQJZj3cD.d.ts → llm-ByUFPcFH.d.ts} +12 -1632
- package/dist/middleware/logging/index.d.ts +76 -0
- package/dist/middleware/logging/index.js +74 -0
- package/dist/middleware/logging/index.js.map +1 -0
- package/dist/middleware/parsed-object/index.d.ts +45 -0
- package/dist/middleware/parsed-object/index.js +73 -0
- package/dist/middleware/parsed-object/index.js.map +1 -0
- package/dist/middleware/pubsub/index.d.ts +97 -0
- package/dist/middleware/pubsub/index.js +160 -0
- package/dist/middleware/pubsub/index.js.map +1 -0
- package/dist/middleware/pubsub/server/express/index.d.ts +66 -0
- package/dist/middleware/pubsub/server/express/index.js +11 -0
- package/dist/middleware/pubsub/server/express/index.js.map +1 -0
- package/dist/middleware/pubsub/server/fastify/index.d.ts +67 -0
- package/dist/middleware/pubsub/server/fastify/index.js +11 -0
- package/dist/middleware/pubsub/server/fastify/index.js.map +1 -0
- package/dist/middleware/pubsub/server/h3/index.d.ts +70 -0
- package/dist/middleware/pubsub/server/h3/index.js +11 -0
- package/dist/middleware/pubsub/server/h3/index.js.map +1 -0
- package/dist/middleware/pubsub/server/index.d.ts +78 -0
- package/dist/middleware/pubsub/server/index.js +34 -0
- package/dist/middleware/pubsub/server/index.js.map +1 -0
- package/dist/middleware/pubsub/server/webapi/index.d.ts +63 -0
- package/dist/middleware/pubsub/server/webapi/index.js +11 -0
- package/dist/middleware/pubsub/server/webapi/index.js.map +1 -0
- package/dist/ollama/index.d.ts +2 -1
- package/dist/ollama/index.js +48 -45
- package/dist/ollama/index.js.map +1 -1
- package/dist/openai/index.d.ts +2 -1
- package/dist/openai/index.js +319 -313
- package/dist/openai/index.js.map +1 -1
- package/dist/openrouter/index.d.ts +2 -1
- package/dist/openrouter/index.js +381 -385
- package/dist/openrouter/index.js.map +1 -1
- package/dist/proxy/index.d.ts +10 -914
- package/dist/proxy/index.js +275 -1007
- package/dist/proxy/index.js.map +1 -1
- package/dist/proxy/server/express/index.d.ts +161 -0
- package/dist/proxy/server/express/index.js +24 -0
- package/dist/proxy/server/express/index.js.map +1 -0
- package/dist/proxy/server/fastify/index.d.ts +162 -0
- package/dist/proxy/server/fastify/index.js +24 -0
- package/dist/proxy/server/fastify/index.js.map +1 -0
- package/dist/proxy/server/h3/index.d.ts +189 -0
- package/dist/proxy/server/h3/index.js +28 -0
- package/dist/proxy/server/h3/index.js.map +1 -0
- package/dist/proxy/server/index.d.ts +151 -0
- package/dist/proxy/server/index.js +48 -0
- package/dist/proxy/server/index.js.map +1 -0
- package/dist/proxy/server/webapi/index.d.ts +278 -0
- package/dist/proxy/server/webapi/index.js +32 -0
- package/dist/proxy/server/webapi/index.js.map +1 -0
- package/dist/responses/index.d.ts +650 -0
- package/dist/responses/index.js +930 -0
- package/dist/responses/index.js.map +1 -0
- package/dist/{retry-8Ch-WWgX.d.ts → retry-BDMo4AVu.d.ts} +1 -1
- package/dist/stream-S7nwQRqM.d.ts +1643 -0
- package/dist/types-CE4B7pno.d.ts +96 -0
- package/dist/utils/index.d.ts +53 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/xai/index.d.ts +2 -1
- package/dist/xai/index.js +310 -310
- package/dist/xai/index.js.map +1 -1
- package/package.json +82 -4
- package/dist/chunk-4J6OFUKX.js.map +0 -1
- package/dist/chunk-6S222DHN.js.map +0 -1
- package/dist/chunk-ILR2D5PN.js.map +0 -1
- package/dist/chunk-TOJCZMVU.js.map +0 -1
- package/dist/chunk-WAKD3OO5.js.map +0 -1
- /package/dist/{chunk-3D6XGGVG.js.map → chunk-ARVM24K2.js.map} +0 -0
- /package/dist/{chunk-KUPF5KHT.js.map → chunk-Y5H7C5J4.js.map} +0 -0
package/dist/xai/index.js
CHANGED
|
@@ -1,70 +1,42 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Image
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-N5DX5JW3.js";
|
|
4
|
+
import {
|
|
5
|
+
parseSSEStream
|
|
6
|
+
} from "../chunk-PMK5LZ5Z.js";
|
|
4
7
|
import {
|
|
5
8
|
parseJsonResponse
|
|
6
|
-
} from "../chunk-
|
|
9
|
+
} from "../chunk-A2IM7PGT.js";
|
|
7
10
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
resolveApiKey
|
|
12
|
+
} from "../chunk-ARVM24K2.js";
|
|
13
|
+
import {
|
|
14
|
+
createProvider
|
|
15
|
+
} from "../chunk-JA3UZALR.js";
|
|
11
16
|
import {
|
|
12
17
|
AssistantMessage,
|
|
13
|
-
createProvider,
|
|
14
18
|
generateId,
|
|
15
19
|
isAssistantMessage,
|
|
16
20
|
isToolResultMessage,
|
|
17
21
|
isUserMessage
|
|
18
|
-
} from "../chunk-
|
|
22
|
+
} from "../chunk-BRP5XJ6Q.js";
|
|
19
23
|
import {
|
|
20
|
-
parseSSEStream
|
|
21
|
-
} from "../chunk-TOJCZMVU.js";
|
|
22
|
-
import {
|
|
23
|
-
resolveApiKey
|
|
24
|
-
} from "../chunk-3D6XGGVG.js";
|
|
25
|
-
import {
|
|
26
|
-
ErrorCode,
|
|
27
|
-
ModalityType,
|
|
28
|
-
UPPError,
|
|
29
24
|
doFetch,
|
|
30
25
|
doStreamFetch,
|
|
31
26
|
normalizeHttpError,
|
|
32
27
|
toError
|
|
33
|
-
} from "../chunk-
|
|
28
|
+
} from "../chunk-AY55T37A.js";
|
|
29
|
+
import {
|
|
30
|
+
ErrorCode,
|
|
31
|
+
ModalityType,
|
|
32
|
+
UPPError
|
|
33
|
+
} from "../chunk-COS4ON4G.js";
|
|
34
|
+
import {
|
|
35
|
+
StreamEventType,
|
|
36
|
+
objectDelta
|
|
37
|
+
} from "../chunk-RJGTRQ47.js";
|
|
34
38
|
|
|
35
39
|
// src/providers/xai/transform.completions.ts
|
|
36
|
-
function transformRequest(request, modelId) {
|
|
37
|
-
const params = request.params ?? {};
|
|
38
|
-
const xaiRequest = {
|
|
39
|
-
...params,
|
|
40
|
-
model: modelId,
|
|
41
|
-
messages: transformMessages(request.messages, request.system)
|
|
42
|
-
};
|
|
43
|
-
if (request.tools && request.tools.length > 0) {
|
|
44
|
-
xaiRequest.tools = request.tools.map(transformTool);
|
|
45
|
-
}
|
|
46
|
-
if (request.structure) {
|
|
47
|
-
const schema = {
|
|
48
|
-
type: "object",
|
|
49
|
-
properties: request.structure.properties,
|
|
50
|
-
required: request.structure.required,
|
|
51
|
-
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
|
|
52
|
-
};
|
|
53
|
-
if (request.structure.description) {
|
|
54
|
-
schema.description = request.structure.description;
|
|
55
|
-
}
|
|
56
|
-
xaiRequest.response_format = {
|
|
57
|
-
type: "json_schema",
|
|
58
|
-
json_schema: {
|
|
59
|
-
name: "json_response",
|
|
60
|
-
description: request.structure.description,
|
|
61
|
-
schema,
|
|
62
|
-
strict: true
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
return xaiRequest;
|
|
67
|
-
}
|
|
68
40
|
function normalizeSystem(system) {
|
|
69
41
|
if (system === void 0 || system === null) return void 0;
|
|
70
42
|
if (typeof system === "string") return system;
|
|
@@ -101,30 +73,34 @@ function normalizeSystem(system) {
|
|
|
101
73
|
}
|
|
102
74
|
return texts.length > 0 ? texts.join("\n\n") : void 0;
|
|
103
75
|
}
|
|
104
|
-
function
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
76
|
+
function filterValidContent(content) {
|
|
77
|
+
return content.filter((c) => c && typeof c.type === "string");
|
|
78
|
+
}
|
|
79
|
+
function transformContentBlock(block) {
|
|
80
|
+
switch (block.type) {
|
|
81
|
+
case "text":
|
|
82
|
+
return { type: "text", text: block.text };
|
|
83
|
+
case "image": {
|
|
84
|
+
const imageBlock = block;
|
|
85
|
+
let url;
|
|
86
|
+
if (imageBlock.source.type === "base64") {
|
|
87
|
+
url = `data:${imageBlock.mimeType};base64,${imageBlock.source.data}`;
|
|
88
|
+
} else if (imageBlock.source.type === "url") {
|
|
89
|
+
url = imageBlock.source.url;
|
|
90
|
+
} else if (imageBlock.source.type === "bytes") {
|
|
91
|
+
const base64 = Buffer.from(imageBlock.source.data).toString("base64");
|
|
92
|
+
url = `data:${imageBlock.mimeType};base64,${base64}`;
|
|
93
|
+
} else {
|
|
94
|
+
throw new Error("Unknown image source type");
|
|
121
95
|
}
|
|
96
|
+
return {
|
|
97
|
+
type: "image_url",
|
|
98
|
+
image_url: { url }
|
|
99
|
+
};
|
|
122
100
|
}
|
|
101
|
+
default:
|
|
102
|
+
throw new Error(`Unsupported content type: ${block.type}`);
|
|
123
103
|
}
|
|
124
|
-
return result;
|
|
125
|
-
}
|
|
126
|
-
function filterValidContent(content) {
|
|
127
|
-
return content.filter((c) => c && typeof c.type === "string");
|
|
128
104
|
}
|
|
129
105
|
function transformMessage(message) {
|
|
130
106
|
if (isUserMessage(message)) {
|
|
@@ -181,31 +157,27 @@ function transformToolResults(message) {
|
|
|
181
157
|
content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
|
|
182
158
|
}));
|
|
183
159
|
}
|
|
184
|
-
function
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
160
|
+
function transformMessages(messages, system) {
|
|
161
|
+
const result = [];
|
|
162
|
+
const normalizedSystem = normalizeSystem(system);
|
|
163
|
+
if (normalizedSystem) {
|
|
164
|
+
result.push({
|
|
165
|
+
role: "system",
|
|
166
|
+
content: normalizedSystem
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
for (const message of messages) {
|
|
170
|
+
if (isToolResultMessage(message)) {
|
|
171
|
+
const toolMessages = transformToolResults(message);
|
|
172
|
+
result.push(...toolMessages);
|
|
173
|
+
} else {
|
|
174
|
+
const transformed = transformMessage(message);
|
|
175
|
+
if (transformed) {
|
|
176
|
+
result.push(transformed);
|
|
200
177
|
}
|
|
201
|
-
return {
|
|
202
|
-
type: "image_url",
|
|
203
|
-
image_url: { url }
|
|
204
|
-
};
|
|
205
178
|
}
|
|
206
|
-
default:
|
|
207
|
-
throw new Error(`Unsupported content type: ${block.type}`);
|
|
208
179
|
}
|
|
180
|
+
return result;
|
|
209
181
|
}
|
|
210
182
|
function transformTool(tool) {
|
|
211
183
|
return {
|
|
@@ -222,6 +194,38 @@ function transformTool(tool) {
|
|
|
222
194
|
}
|
|
223
195
|
};
|
|
224
196
|
}
|
|
197
|
+
function transformRequest(request, modelId) {
|
|
198
|
+
const params = request.params ?? {};
|
|
199
|
+
const xaiRequest = {
|
|
200
|
+
...params,
|
|
201
|
+
model: modelId,
|
|
202
|
+
messages: transformMessages(request.messages, request.system)
|
|
203
|
+
};
|
|
204
|
+
if (request.tools && request.tools.length > 0) {
|
|
205
|
+
xaiRequest.tools = request.tools.map(transformTool);
|
|
206
|
+
}
|
|
207
|
+
if (request.structure) {
|
|
208
|
+
const schema = {
|
|
209
|
+
type: "object",
|
|
210
|
+
properties: request.structure.properties,
|
|
211
|
+
required: request.structure.required,
|
|
212
|
+
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
|
|
213
|
+
};
|
|
214
|
+
if (request.structure.description) {
|
|
215
|
+
schema.description = request.structure.description;
|
|
216
|
+
}
|
|
217
|
+
xaiRequest.response_format = {
|
|
218
|
+
type: "json_schema",
|
|
219
|
+
json_schema: {
|
|
220
|
+
name: "json_response",
|
|
221
|
+
description: request.structure.description,
|
|
222
|
+
schema,
|
|
223
|
+
strict: true
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
return xaiRequest;
|
|
228
|
+
}
|
|
225
229
|
function transformResponse(data) {
|
|
226
230
|
const choice = data.choices[0];
|
|
227
231
|
if (!choice) {
|
|
@@ -618,10 +622,9 @@ function createCompletionsLLMHandler() {
|
|
|
618
622
|
}
|
|
619
623
|
const uppEvents = transformStreamEvent(chunk, state);
|
|
620
624
|
for (const event of uppEvents) {
|
|
625
|
+
yield event;
|
|
621
626
|
if (request.structure && event.type === StreamEventType.TextDelta) {
|
|
622
627
|
yield objectDelta(event.delta.text ?? "", event.index);
|
|
623
|
-
} else {
|
|
624
|
-
yield event;
|
|
625
628
|
}
|
|
626
629
|
}
|
|
627
630
|
}
|
|
@@ -647,44 +650,6 @@ function createCompletionsLLMHandler() {
|
|
|
647
650
|
}
|
|
648
651
|
|
|
649
652
|
// src/providers/xai/transform.responses.ts
|
|
650
|
-
function transformRequest2(request, modelId) {
|
|
651
|
-
const params = request.params ?? {};
|
|
652
|
-
const { tools: builtInTools, ...restParams } = params;
|
|
653
|
-
const xaiRequest = {
|
|
654
|
-
...restParams,
|
|
655
|
-
model: modelId,
|
|
656
|
-
input: transformInputItems(request.messages, request.system)
|
|
657
|
-
};
|
|
658
|
-
const functionTools = request.tools?.map(transformTool2) ?? [];
|
|
659
|
-
const allTools = [
|
|
660
|
-
...functionTools,
|
|
661
|
-
...builtInTools ?? []
|
|
662
|
-
];
|
|
663
|
-
if (allTools.length > 0) {
|
|
664
|
-
xaiRequest.tools = allTools;
|
|
665
|
-
}
|
|
666
|
-
if (request.structure) {
|
|
667
|
-
const schema = {
|
|
668
|
-
type: "object",
|
|
669
|
-
properties: request.structure.properties,
|
|
670
|
-
required: request.structure.required,
|
|
671
|
-
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
|
|
672
|
-
};
|
|
673
|
-
if (request.structure.description) {
|
|
674
|
-
schema.description = request.structure.description;
|
|
675
|
-
}
|
|
676
|
-
xaiRequest.text = {
|
|
677
|
-
format: {
|
|
678
|
-
type: "json_schema",
|
|
679
|
-
name: "json_response",
|
|
680
|
-
description: request.structure.description,
|
|
681
|
-
schema,
|
|
682
|
-
strict: true
|
|
683
|
-
}
|
|
684
|
-
};
|
|
685
|
-
}
|
|
686
|
-
return xaiRequest;
|
|
687
|
-
}
|
|
688
653
|
function normalizeSystem2(system) {
|
|
689
654
|
if (system === void 0 || system === null) return void 0;
|
|
690
655
|
if (typeof system === "string") return system;
|
|
@@ -721,31 +686,40 @@ function normalizeSystem2(system) {
|
|
|
721
686
|
}
|
|
722
687
|
return texts.length > 0 ? texts.join("\n\n") : void 0;
|
|
723
688
|
}
|
|
724
|
-
function transformInputItems(messages, system) {
|
|
725
|
-
const result = [];
|
|
726
|
-
const normalizedSystem = normalizeSystem2(system);
|
|
727
|
-
if (normalizedSystem) {
|
|
728
|
-
result.push({
|
|
729
|
-
type: "message",
|
|
730
|
-
role: "system",
|
|
731
|
-
content: normalizedSystem
|
|
732
|
-
});
|
|
733
|
-
}
|
|
734
|
-
for (const message of messages) {
|
|
735
|
-
const items = transformMessage2(message);
|
|
736
|
-
result.push(...items);
|
|
737
|
-
}
|
|
738
|
-
if (result.length === 1 && result[0]?.type === "message") {
|
|
739
|
-
const item = result[0];
|
|
740
|
-
if (item.role === "user" && typeof item.content === "string") {
|
|
741
|
-
return item.content;
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
return result;
|
|
745
|
-
}
|
|
746
689
|
function filterValidContent2(content) {
|
|
747
690
|
return content.filter((c) => c && typeof c.type === "string");
|
|
748
691
|
}
|
|
692
|
+
function transformContentPart(block) {
|
|
693
|
+
switch (block.type) {
|
|
694
|
+
case "text":
|
|
695
|
+
return { type: "input_text", text: block.text };
|
|
696
|
+
case "image": {
|
|
697
|
+
const imageBlock = block;
|
|
698
|
+
if (imageBlock.source.type === "base64") {
|
|
699
|
+
return {
|
|
700
|
+
type: "input_image",
|
|
701
|
+
image_url: `data:${imageBlock.mimeType};base64,${imageBlock.source.data}`
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
if (imageBlock.source.type === "url") {
|
|
705
|
+
return {
|
|
706
|
+
type: "input_image",
|
|
707
|
+
image_url: imageBlock.source.url
|
|
708
|
+
};
|
|
709
|
+
}
|
|
710
|
+
if (imageBlock.source.type === "bytes") {
|
|
711
|
+
const base64 = Buffer.from(imageBlock.source.data).toString("base64");
|
|
712
|
+
return {
|
|
713
|
+
type: "input_image",
|
|
714
|
+
image_url: `data:${imageBlock.mimeType};base64,${base64}`
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
throw new Error("Unknown image source type");
|
|
718
|
+
}
|
|
719
|
+
default:
|
|
720
|
+
throw new Error(`Unsupported content type: ${block.type}`);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
749
723
|
function transformMessage2(message) {
|
|
750
724
|
if (isUserMessage(message)) {
|
|
751
725
|
const validContent = filterValidContent2(message.content);
|
|
@@ -828,36 +802,27 @@ function transformMessage2(message) {
|
|
|
828
802
|
}
|
|
829
803
|
return [];
|
|
830
804
|
}
|
|
831
|
-
function
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
if (imageBlock.source.type === "bytes") {
|
|
850
|
-
const base64 = Buffer.from(imageBlock.source.data).toString("base64");
|
|
851
|
-
return {
|
|
852
|
-
type: "input_image",
|
|
853
|
-
image_url: `data:${imageBlock.mimeType};base64,${base64}`
|
|
854
|
-
};
|
|
855
|
-
}
|
|
856
|
-
throw new Error("Unknown image source type");
|
|
805
|
+
function transformInputItems(messages, system) {
|
|
806
|
+
const result = [];
|
|
807
|
+
const normalizedSystem = normalizeSystem2(system);
|
|
808
|
+
if (normalizedSystem) {
|
|
809
|
+
result.push({
|
|
810
|
+
type: "message",
|
|
811
|
+
role: "system",
|
|
812
|
+
content: normalizedSystem
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
for (const message of messages) {
|
|
816
|
+
const items = transformMessage2(message);
|
|
817
|
+
result.push(...items);
|
|
818
|
+
}
|
|
819
|
+
if (result.length === 1 && result[0]?.type === "message") {
|
|
820
|
+
const item = result[0];
|
|
821
|
+
if (item.role === "user" && typeof item.content === "string") {
|
|
822
|
+
return item.content;
|
|
857
823
|
}
|
|
858
|
-
default:
|
|
859
|
-
throw new Error(`Unsupported content type: ${block.type}`);
|
|
860
824
|
}
|
|
825
|
+
return result;
|
|
861
826
|
}
|
|
862
827
|
function transformTool2(tool) {
|
|
863
828
|
return {
|
|
@@ -872,6 +837,44 @@ function transformTool2(tool) {
|
|
|
872
837
|
}
|
|
873
838
|
};
|
|
874
839
|
}
|
|
840
|
+
function transformRequest2(request, modelId) {
|
|
841
|
+
const params = request.params ?? {};
|
|
842
|
+
const { tools: builtInTools, ...restParams } = params;
|
|
843
|
+
const xaiRequest = {
|
|
844
|
+
...restParams,
|
|
845
|
+
model: modelId,
|
|
846
|
+
input: transformInputItems(request.messages, request.system)
|
|
847
|
+
};
|
|
848
|
+
const functionTools = request.tools?.map(transformTool2) ?? [];
|
|
849
|
+
const allTools = [
|
|
850
|
+
...functionTools,
|
|
851
|
+
...builtInTools ?? []
|
|
852
|
+
];
|
|
853
|
+
if (allTools.length > 0) {
|
|
854
|
+
xaiRequest.tools = allTools;
|
|
855
|
+
}
|
|
856
|
+
if (request.structure) {
|
|
857
|
+
const schema = {
|
|
858
|
+
type: "object",
|
|
859
|
+
properties: request.structure.properties,
|
|
860
|
+
required: request.structure.required,
|
|
861
|
+
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
|
|
862
|
+
};
|
|
863
|
+
if (request.structure.description) {
|
|
864
|
+
schema.description = request.structure.description;
|
|
865
|
+
}
|
|
866
|
+
xaiRequest.text = {
|
|
867
|
+
format: {
|
|
868
|
+
type: "json_schema",
|
|
869
|
+
name: "json_response",
|
|
870
|
+
description: request.structure.description,
|
|
871
|
+
schema,
|
|
872
|
+
strict: true
|
|
873
|
+
}
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
return xaiRequest;
|
|
877
|
+
}
|
|
875
878
|
function transformResponse2(data) {
|
|
876
879
|
const content = [];
|
|
877
880
|
const toolCalls = [];
|
|
@@ -1392,10 +1395,9 @@ function createResponsesLLMHandler() {
|
|
|
1392
1395
|
}
|
|
1393
1396
|
const uppEvents = transformStreamEvent2(event, state);
|
|
1394
1397
|
for (const uppEvent of uppEvents) {
|
|
1398
|
+
yield uppEvent;
|
|
1395
1399
|
if (request.structure && uppEvent.type === StreamEventType.TextDelta) {
|
|
1396
1400
|
yield objectDelta(uppEvent.delta.text ?? "", uppEvent.index);
|
|
1397
|
-
} else {
|
|
1398
|
-
yield uppEvent;
|
|
1399
1401
|
}
|
|
1400
1402
|
}
|
|
1401
1403
|
}
|
|
@@ -1457,41 +1459,51 @@ function normalizeSystem3(system) {
|
|
|
1457
1459
|
}
|
|
1458
1460
|
return texts.length > 0 ? texts.join("\n\n") : void 0;
|
|
1459
1461
|
}
|
|
1460
|
-
function transformRequest3(request, modelId) {
|
|
1461
|
-
const params = request.params ?? {};
|
|
1462
|
-
const normalizedSystem = normalizeSystem3(request.system);
|
|
1463
|
-
const xaiRequest = {
|
|
1464
|
-
...params,
|
|
1465
|
-
model: modelId,
|
|
1466
|
-
messages: request.messages.map(transformMessage3)
|
|
1467
|
-
};
|
|
1468
|
-
if (normalizedSystem) {
|
|
1469
|
-
xaiRequest.system = normalizedSystem;
|
|
1470
|
-
}
|
|
1471
|
-
if (request.tools && request.tools.length > 0) {
|
|
1472
|
-
xaiRequest.tools = request.tools.map(transformTool3);
|
|
1473
|
-
if (!xaiRequest.tool_choice) {
|
|
1474
|
-
xaiRequest.tool_choice = { type: "auto" };
|
|
1475
|
-
}
|
|
1476
|
-
}
|
|
1477
|
-
if (request.structure) {
|
|
1478
|
-
const structuredTool = {
|
|
1479
|
-
name: "json_response",
|
|
1480
|
-
description: "Return the response in the specified JSON format. You MUST use this tool to provide your response.",
|
|
1481
|
-
input_schema: {
|
|
1482
|
-
type: "object",
|
|
1483
|
-
properties: request.structure.properties,
|
|
1484
|
-
required: request.structure.required
|
|
1485
|
-
}
|
|
1486
|
-
};
|
|
1487
|
-
xaiRequest.tools = [...xaiRequest.tools ?? [], structuredTool];
|
|
1488
|
-
xaiRequest.tool_choice = { type: "tool", name: "json_response" };
|
|
1489
|
-
}
|
|
1490
|
-
return xaiRequest;
|
|
1491
|
-
}
|
|
1492
1462
|
function filterValidContent3(content) {
|
|
1493
1463
|
return content.filter((c) => c && typeof c.type === "string");
|
|
1494
1464
|
}
|
|
1465
|
+
function transformContentBlock2(block) {
|
|
1466
|
+
switch (block.type) {
|
|
1467
|
+
case "text":
|
|
1468
|
+
return { type: "text", text: block.text };
|
|
1469
|
+
case "image": {
|
|
1470
|
+
const imageBlock = block;
|
|
1471
|
+
if (imageBlock.source.type === "base64") {
|
|
1472
|
+
return {
|
|
1473
|
+
type: "image",
|
|
1474
|
+
source: {
|
|
1475
|
+
type: "base64",
|
|
1476
|
+
media_type: imageBlock.mimeType,
|
|
1477
|
+
data: imageBlock.source.data
|
|
1478
|
+
}
|
|
1479
|
+
};
|
|
1480
|
+
}
|
|
1481
|
+
if (imageBlock.source.type === "url") {
|
|
1482
|
+
return {
|
|
1483
|
+
type: "image",
|
|
1484
|
+
source: {
|
|
1485
|
+
type: "url",
|
|
1486
|
+
url: imageBlock.source.url
|
|
1487
|
+
}
|
|
1488
|
+
};
|
|
1489
|
+
}
|
|
1490
|
+
if (imageBlock.source.type === "bytes") {
|
|
1491
|
+
const base64 = Buffer.from(imageBlock.source.data).toString("base64");
|
|
1492
|
+
return {
|
|
1493
|
+
type: "image",
|
|
1494
|
+
source: {
|
|
1495
|
+
type: "base64",
|
|
1496
|
+
media_type: imageBlock.mimeType,
|
|
1497
|
+
data: base64
|
|
1498
|
+
}
|
|
1499
|
+
};
|
|
1500
|
+
}
|
|
1501
|
+
throw new Error(`Unknown image source type`);
|
|
1502
|
+
}
|
|
1503
|
+
default:
|
|
1504
|
+
throw new Error(`Unsupported content type: ${block.type}`);
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1495
1507
|
function transformMessage3(message) {
|
|
1496
1508
|
if (isUserMessage(message)) {
|
|
1497
1509
|
const validContent = filterValidContent3(message.content);
|
|
@@ -1546,48 +1558,6 @@ function transformMessage3(message) {
|
|
|
1546
1558
|
}
|
|
1547
1559
|
throw new Error(`Unknown message type: ${message.type}`);
|
|
1548
1560
|
}
|
|
1549
|
-
function transformContentBlock2(block) {
|
|
1550
|
-
switch (block.type) {
|
|
1551
|
-
case "text":
|
|
1552
|
-
return { type: "text", text: block.text };
|
|
1553
|
-
case "image": {
|
|
1554
|
-
const imageBlock = block;
|
|
1555
|
-
if (imageBlock.source.type === "base64") {
|
|
1556
|
-
return {
|
|
1557
|
-
type: "image",
|
|
1558
|
-
source: {
|
|
1559
|
-
type: "base64",
|
|
1560
|
-
media_type: imageBlock.mimeType,
|
|
1561
|
-
data: imageBlock.source.data
|
|
1562
|
-
}
|
|
1563
|
-
};
|
|
1564
|
-
}
|
|
1565
|
-
if (imageBlock.source.type === "url") {
|
|
1566
|
-
return {
|
|
1567
|
-
type: "image",
|
|
1568
|
-
source: {
|
|
1569
|
-
type: "url",
|
|
1570
|
-
url: imageBlock.source.url
|
|
1571
|
-
}
|
|
1572
|
-
};
|
|
1573
|
-
}
|
|
1574
|
-
if (imageBlock.source.type === "bytes") {
|
|
1575
|
-
const base64 = Buffer.from(imageBlock.source.data).toString("base64");
|
|
1576
|
-
return {
|
|
1577
|
-
type: "image",
|
|
1578
|
-
source: {
|
|
1579
|
-
type: "base64",
|
|
1580
|
-
media_type: imageBlock.mimeType,
|
|
1581
|
-
data: base64
|
|
1582
|
-
}
|
|
1583
|
-
};
|
|
1584
|
-
}
|
|
1585
|
-
throw new Error(`Unknown image source type`);
|
|
1586
|
-
}
|
|
1587
|
-
default:
|
|
1588
|
-
throw new Error(`Unsupported content type: ${block.type}`);
|
|
1589
|
-
}
|
|
1590
|
-
}
|
|
1591
1561
|
function transformTool3(tool) {
|
|
1592
1562
|
return {
|
|
1593
1563
|
name: tool.name,
|
|
@@ -1599,6 +1569,38 @@ function transformTool3(tool) {
|
|
|
1599
1569
|
}
|
|
1600
1570
|
};
|
|
1601
1571
|
}
|
|
1572
|
+
function transformRequest3(request, modelId) {
|
|
1573
|
+
const params = request.params ?? {};
|
|
1574
|
+
const normalizedSystem = normalizeSystem3(request.system);
|
|
1575
|
+
const xaiRequest = {
|
|
1576
|
+
...params,
|
|
1577
|
+
model: modelId,
|
|
1578
|
+
messages: request.messages.map(transformMessage3)
|
|
1579
|
+
};
|
|
1580
|
+
if (normalizedSystem) {
|
|
1581
|
+
xaiRequest.system = normalizedSystem;
|
|
1582
|
+
}
|
|
1583
|
+
if (request.tools && request.tools.length > 0) {
|
|
1584
|
+
xaiRequest.tools = request.tools.map(transformTool3);
|
|
1585
|
+
if (!xaiRequest.tool_choice) {
|
|
1586
|
+
xaiRequest.tool_choice = { type: "auto" };
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
if (request.structure) {
|
|
1590
|
+
const structuredTool = {
|
|
1591
|
+
name: "json_response",
|
|
1592
|
+
description: "Return the response in the specified JSON format. You MUST use this tool to provide your response.",
|
|
1593
|
+
input_schema: {
|
|
1594
|
+
type: "object",
|
|
1595
|
+
properties: request.structure.properties,
|
|
1596
|
+
required: request.structure.required
|
|
1597
|
+
}
|
|
1598
|
+
};
|
|
1599
|
+
xaiRequest.tools = [...xaiRequest.tools ?? [], structuredTool];
|
|
1600
|
+
xaiRequest.tool_choice = { type: "tool", name: "json_response" };
|
|
1601
|
+
}
|
|
1602
|
+
return xaiRequest;
|
|
1603
|
+
}
|
|
1602
1604
|
function transformResponse3(data) {
|
|
1603
1605
|
const content = [];
|
|
1604
1606
|
const toolCalls = [];
|
|
@@ -1956,10 +1958,9 @@ function createMessagesLLMHandler() {
|
|
|
1956
1958
|
}
|
|
1957
1959
|
const uppEvent = transformStreamEvent3(event, state);
|
|
1958
1960
|
if (uppEvent) {
|
|
1961
|
+
yield uppEvent;
|
|
1959
1962
|
if (request.structure && uppEvent.type === StreamEventType.TextDelta) {
|
|
1960
1963
|
yield objectDelta(uppEvent.delta.text ?? "", uppEvent.index);
|
|
1961
|
-
} else {
|
|
1962
|
-
yield uppEvent;
|
|
1963
1964
|
}
|
|
1964
1965
|
}
|
|
1965
1966
|
}
|
|
@@ -1994,33 +1995,30 @@ function getCapabilities(modelId) {
|
|
|
1994
1995
|
maxImages: 10
|
|
1995
1996
|
};
|
|
1996
1997
|
}
|
|
1997
|
-
function
|
|
1998
|
-
|
|
1998
|
+
function transformResponse4(data) {
|
|
1999
|
+
const images = data.data.map((item) => {
|
|
2000
|
+
let image;
|
|
2001
|
+
if (item.b64_json) {
|
|
2002
|
+
image = Image.fromBase64(item.b64_json, "image/jpeg");
|
|
2003
|
+
} else if (item.url) {
|
|
2004
|
+
image = Image.fromUrl(item.url, "image/jpeg");
|
|
2005
|
+
} else {
|
|
2006
|
+
throw new UPPError(
|
|
2007
|
+
"No image data in response",
|
|
2008
|
+
ErrorCode.ProviderError,
|
|
2009
|
+
"xai",
|
|
2010
|
+
ModalityType.Image
|
|
2011
|
+
);
|
|
2012
|
+
}
|
|
2013
|
+
return {
|
|
2014
|
+
image,
|
|
2015
|
+
metadata: item.revised_prompt ? { xai: { revised_prompt: item.revised_prompt } } : void 0
|
|
2016
|
+
};
|
|
2017
|
+
});
|
|
1999
2018
|
return {
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
bind(modelId) {
|
|
2004
|
-
if (!providerRef) {
|
|
2005
|
-
throw new UPPError(
|
|
2006
|
-
"Provider reference not set. Handler must be used with createProvider().",
|
|
2007
|
-
ErrorCode.InvalidRequest,
|
|
2008
|
-
"xai",
|
|
2009
|
-
ModalityType.Image
|
|
2010
|
-
);
|
|
2011
|
-
}
|
|
2012
|
-
const capabilities = getCapabilities(modelId);
|
|
2013
|
-
const model = {
|
|
2014
|
-
modelId,
|
|
2015
|
-
capabilities,
|
|
2016
|
-
get provider() {
|
|
2017
|
-
return providerRef;
|
|
2018
|
-
},
|
|
2019
|
-
async generate(request) {
|
|
2020
|
-
return executeGenerate(modelId, request);
|
|
2021
|
-
}
|
|
2022
|
-
};
|
|
2023
|
-
return model;
|
|
2019
|
+
images,
|
|
2020
|
+
usage: {
|
|
2021
|
+
imagesGenerated: images.length
|
|
2024
2022
|
}
|
|
2025
2023
|
};
|
|
2026
2024
|
}
|
|
@@ -2062,31 +2060,33 @@ async function executeGenerate(modelId, request) {
|
|
|
2062
2060
|
const data = await parseJsonResponse(response, "xai", "image");
|
|
2063
2061
|
return transformResponse4(data);
|
|
2064
2062
|
}
|
|
2065
|
-
function
|
|
2066
|
-
|
|
2067
|
-
let image;
|
|
2068
|
-
if (item.b64_json) {
|
|
2069
|
-
image = Image.fromBase64(item.b64_json, "image/jpeg");
|
|
2070
|
-
} else if (item.url) {
|
|
2071
|
-
image = Image.fromUrl(item.url, "image/jpeg");
|
|
2072
|
-
} else {
|
|
2073
|
-
throw new UPPError(
|
|
2074
|
-
"No image data in response",
|
|
2075
|
-
ErrorCode.ProviderError,
|
|
2076
|
-
"xai",
|
|
2077
|
-
ModalityType.Image
|
|
2078
|
-
);
|
|
2079
|
-
}
|
|
2080
|
-
return {
|
|
2081
|
-
image,
|
|
2082
|
-
// Per-image metadata namespaced under provider (Spec 15.4)
|
|
2083
|
-
metadata: item.revised_prompt ? { xai: { revised_prompt: item.revised_prompt } } : void 0
|
|
2084
|
-
};
|
|
2085
|
-
});
|
|
2063
|
+
function createImageHandler() {
|
|
2064
|
+
let providerRef = null;
|
|
2086
2065
|
return {
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2066
|
+
_setProvider(provider) {
|
|
2067
|
+
providerRef = provider;
|
|
2068
|
+
},
|
|
2069
|
+
bind(modelId) {
|
|
2070
|
+
if (!providerRef) {
|
|
2071
|
+
throw new UPPError(
|
|
2072
|
+
"Provider reference not set. Handler must be used with createProvider().",
|
|
2073
|
+
ErrorCode.InvalidRequest,
|
|
2074
|
+
"xai",
|
|
2075
|
+
ModalityType.Image
|
|
2076
|
+
);
|
|
2077
|
+
}
|
|
2078
|
+
const capabilities = getCapabilities(modelId);
|
|
2079
|
+
const model = {
|
|
2080
|
+
modelId,
|
|
2081
|
+
capabilities,
|
|
2082
|
+
get provider() {
|
|
2083
|
+
return providerRef;
|
|
2084
|
+
},
|
|
2085
|
+
async generate(request) {
|
|
2086
|
+
return executeGenerate(modelId, request);
|
|
2087
|
+
}
|
|
2088
|
+
};
|
|
2089
|
+
return model;
|
|
2090
2090
|
}
|
|
2091
2091
|
};
|
|
2092
2092
|
}
|