@providerprotocol/ai 0.0.34 → 0.0.35
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 +542 -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-3GWM5GR3.js +153 -0
- package/dist/chunk-3GWM5GR3.js.map +1 -0
- package/dist/chunk-4OGB7JZA.js +157 -0
- package/dist/chunk-4OGB7JZA.js.map +1 -0
- package/dist/chunk-7DXVRILR.js +49 -0
- package/dist/chunk-7DXVRILR.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-ETBFOLQN.js +34 -0
- package/dist/chunk-ETBFOLQN.js.map +1 -0
- package/dist/chunk-HB4ZIH3T.js +31 -0
- package/dist/chunk-HB4ZIH3T.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-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-VOEWHQUB.js +31 -0
- package/dist/chunk-VOEWHQUB.js.map +1 -0
- package/dist/{chunk-KUPF5KHT.js → chunk-Y5H7C5J4.js} +2 -2
- package/dist/chunk-ZI67WIQS.js +30 -0
- package/dist/chunk-ZI67WIQS.js.map +1 -0
- package/dist/{embedding-D2BYIehX.d.ts → embedding-CW6SaOOz.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-C0ciACM2.d.ts +11 -0
- package/dist/index.d.ts +8 -118
- package/dist/index.js +518 -767
- package/dist/index.js.map +1 -1
- package/dist/{llm-BQJZj3cD.d.ts → llm-DwbUK7un.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 +104 -0
- package/dist/middleware/pubsub/index.js +230 -0
- package/dist/middleware/pubsub/index.js.map +1 -0
- package/dist/middleware/pubsub/server/express/index.d.ts +52 -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 +53 -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 +56 -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 +53 -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 +379 -383
- 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-YayV42GV.d.ts} +1 -1
- package/dist/stream-CecfVCPO.d.ts +1632 -0
- package/dist/types-C8Gciizr.d.ts +168 -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-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/google/index.js
CHANGED
|
@@ -1,85 +1,41 @@
|
|
|
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
|
isAssistantMessage,
|
|
15
19
|
isToolResultMessage,
|
|
16
20
|
isUserMessage
|
|
17
|
-
} from "../chunk-
|
|
18
|
-
import {
|
|
19
|
-
parseSSEStream
|
|
20
|
-
} from "../chunk-TOJCZMVU.js";
|
|
21
|
+
} from "../chunk-BRP5XJ6Q.js";
|
|
21
22
|
import {
|
|
22
|
-
resolveApiKey
|
|
23
|
-
} from "../chunk-3D6XGGVG.js";
|
|
24
|
-
import {
|
|
25
|
-
ErrorCode,
|
|
26
|
-
ModalityType,
|
|
27
|
-
UPPError,
|
|
28
23
|
doFetch,
|
|
29
24
|
doStreamFetch,
|
|
30
25
|
normalizeHttpError,
|
|
31
26
|
toError
|
|
32
|
-
} from "../chunk-
|
|
27
|
+
} from "../chunk-AY55T37A.js";
|
|
28
|
+
import {
|
|
29
|
+
ErrorCode,
|
|
30
|
+
ModalityType,
|
|
31
|
+
UPPError
|
|
32
|
+
} from "../chunk-COS4ON4G.js";
|
|
33
|
+
import {
|
|
34
|
+
StreamEventType,
|
|
35
|
+
objectDelta
|
|
36
|
+
} from "../chunk-6S222DHN.js";
|
|
33
37
|
|
|
34
38
|
// src/providers/google/transform.ts
|
|
35
|
-
function transformRequest(request, modelId) {
|
|
36
|
-
const params = request.params ?? {};
|
|
37
|
-
const { cachedContent, tools: builtInTools, toolConfig, ...generationParams } = params;
|
|
38
|
-
const googleRequest = {
|
|
39
|
-
contents: transformMessages(request.messages)
|
|
40
|
-
};
|
|
41
|
-
const normalizedSystem = normalizeSystem(request.system);
|
|
42
|
-
if (normalizedSystem !== void 0) {
|
|
43
|
-
if (typeof normalizedSystem === "string") {
|
|
44
|
-
googleRequest.systemInstruction = {
|
|
45
|
-
parts: [{ text: normalizedSystem }]
|
|
46
|
-
};
|
|
47
|
-
} else if (normalizedSystem.length > 0) {
|
|
48
|
-
googleRequest.systemInstruction = {
|
|
49
|
-
parts: normalizedSystem
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
const generationConfig = {
|
|
54
|
-
...generationParams
|
|
55
|
-
};
|
|
56
|
-
if (request.structure) {
|
|
57
|
-
generationConfig.responseMimeType = "application/json";
|
|
58
|
-
generationConfig.responseSchema = request.structure;
|
|
59
|
-
}
|
|
60
|
-
if (Object.keys(generationConfig).length > 0) {
|
|
61
|
-
googleRequest.generationConfig = generationConfig;
|
|
62
|
-
}
|
|
63
|
-
const requestTools = [];
|
|
64
|
-
if (request.tools && request.tools.length > 0) {
|
|
65
|
-
requestTools.push({
|
|
66
|
-
functionDeclarations: request.tools.map(transformTool)
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
if (builtInTools && builtInTools.length > 0) {
|
|
70
|
-
requestTools.push(...builtInTools);
|
|
71
|
-
}
|
|
72
|
-
if (requestTools.length > 0) {
|
|
73
|
-
googleRequest.tools = requestTools;
|
|
74
|
-
}
|
|
75
|
-
if (toolConfig) {
|
|
76
|
-
googleRequest.toolConfig = toolConfig;
|
|
77
|
-
}
|
|
78
|
-
if (cachedContent) {
|
|
79
|
-
googleRequest.cachedContent = cachedContent;
|
|
80
|
-
}
|
|
81
|
-
return googleRequest;
|
|
82
|
-
}
|
|
83
39
|
function normalizeSystem(system) {
|
|
84
40
|
if (system === void 0 || system === null) return void 0;
|
|
85
41
|
if (typeof system === "string") return system;
|
|
@@ -133,6 +89,67 @@ function extractGoogleToolName(toolCallId) {
|
|
|
133
89
|
function filterValidContent(content) {
|
|
134
90
|
return content.filter((c) => c && typeof c.type === "string");
|
|
135
91
|
}
|
|
92
|
+
function uint8ArrayToBase64(bytes) {
|
|
93
|
+
return Buffer.from(bytes).toString("base64");
|
|
94
|
+
}
|
|
95
|
+
function transformContentBlock(block) {
|
|
96
|
+
switch (block.type) {
|
|
97
|
+
case "text":
|
|
98
|
+
return { text: block.text };
|
|
99
|
+
case "image": {
|
|
100
|
+
const imageBlock = block;
|
|
101
|
+
let data;
|
|
102
|
+
if (imageBlock.source.type === "base64") {
|
|
103
|
+
data = imageBlock.source.data;
|
|
104
|
+
} else if (imageBlock.source.type === "bytes") {
|
|
105
|
+
data = uint8ArrayToBase64(imageBlock.source.data);
|
|
106
|
+
} else {
|
|
107
|
+
throw new Error("Google API does not support URL image sources directly");
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
inlineData: {
|
|
111
|
+
mimeType: imageBlock.mimeType,
|
|
112
|
+
data
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
case "document": {
|
|
117
|
+
const documentBlock = block;
|
|
118
|
+
if (documentBlock.source.type === "base64") {
|
|
119
|
+
return {
|
|
120
|
+
inlineData: {
|
|
121
|
+
mimeType: documentBlock.mimeType,
|
|
122
|
+
data: documentBlock.source.data
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
if (documentBlock.source.type === "text") {
|
|
127
|
+
return { text: documentBlock.source.data };
|
|
128
|
+
}
|
|
129
|
+
throw new Error("Google API does not support URL document sources directly");
|
|
130
|
+
}
|
|
131
|
+
case "audio": {
|
|
132
|
+
const audioBlock = block;
|
|
133
|
+
return {
|
|
134
|
+
inlineData: {
|
|
135
|
+
mimeType: audioBlock.mimeType,
|
|
136
|
+
data: uint8ArrayToBase64(audioBlock.data)
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
case "video": {
|
|
141
|
+
const videoBlock = block;
|
|
142
|
+
return {
|
|
143
|
+
inlineData: {
|
|
144
|
+
mimeType: videoBlock.mimeType,
|
|
145
|
+
data: uint8ArrayToBase64(videoBlock.data)
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
default:
|
|
150
|
+
throw new Error(`Unsupported content type: ${block.type}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
136
153
|
function transformMessages(messages) {
|
|
137
154
|
const contents = [];
|
|
138
155
|
for (const msg of messages) {
|
|
@@ -205,67 +222,6 @@ function transformMessages(messages) {
|
|
|
205
222
|
}
|
|
206
223
|
return contents;
|
|
207
224
|
}
|
|
208
|
-
function uint8ArrayToBase64(bytes) {
|
|
209
|
-
return Buffer.from(bytes).toString("base64");
|
|
210
|
-
}
|
|
211
|
-
function transformContentBlock(block) {
|
|
212
|
-
switch (block.type) {
|
|
213
|
-
case "text":
|
|
214
|
-
return { text: block.text };
|
|
215
|
-
case "image": {
|
|
216
|
-
const imageBlock = block;
|
|
217
|
-
let data;
|
|
218
|
-
if (imageBlock.source.type === "base64") {
|
|
219
|
-
data = imageBlock.source.data;
|
|
220
|
-
} else if (imageBlock.source.type === "bytes") {
|
|
221
|
-
data = uint8ArrayToBase64(imageBlock.source.data);
|
|
222
|
-
} else {
|
|
223
|
-
throw new Error("Google API does not support URL image sources directly");
|
|
224
|
-
}
|
|
225
|
-
return {
|
|
226
|
-
inlineData: {
|
|
227
|
-
mimeType: imageBlock.mimeType,
|
|
228
|
-
data
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
case "document": {
|
|
233
|
-
const documentBlock = block;
|
|
234
|
-
if (documentBlock.source.type === "base64") {
|
|
235
|
-
return {
|
|
236
|
-
inlineData: {
|
|
237
|
-
mimeType: documentBlock.mimeType,
|
|
238
|
-
data: documentBlock.source.data
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
if (documentBlock.source.type === "text") {
|
|
243
|
-
return { text: documentBlock.source.data };
|
|
244
|
-
}
|
|
245
|
-
throw new Error("Google API does not support URL document sources directly");
|
|
246
|
-
}
|
|
247
|
-
case "audio": {
|
|
248
|
-
const audioBlock = block;
|
|
249
|
-
return {
|
|
250
|
-
inlineData: {
|
|
251
|
-
mimeType: audioBlock.mimeType,
|
|
252
|
-
data: uint8ArrayToBase64(audioBlock.data)
|
|
253
|
-
}
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
case "video": {
|
|
257
|
-
const videoBlock = block;
|
|
258
|
-
return {
|
|
259
|
-
inlineData: {
|
|
260
|
-
mimeType: videoBlock.mimeType,
|
|
261
|
-
data: uint8ArrayToBase64(videoBlock.data)
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
|
-
default:
|
|
266
|
-
throw new Error(`Unsupported content type: ${block.type}`);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
225
|
function transformTool(tool) {
|
|
270
226
|
return {
|
|
271
227
|
name: tool.name,
|
|
@@ -277,6 +233,71 @@ function transformTool(tool) {
|
|
|
277
233
|
}
|
|
278
234
|
};
|
|
279
235
|
}
|
|
236
|
+
function transformRequest(request, modelId) {
|
|
237
|
+
const params = request.params ?? {};
|
|
238
|
+
const { cachedContent, tools: builtInTools, toolConfig, ...generationParams } = params;
|
|
239
|
+
const googleRequest = {
|
|
240
|
+
contents: transformMessages(request.messages)
|
|
241
|
+
};
|
|
242
|
+
const normalizedSystem = normalizeSystem(request.system);
|
|
243
|
+
if (normalizedSystem !== void 0) {
|
|
244
|
+
if (typeof normalizedSystem === "string") {
|
|
245
|
+
googleRequest.systemInstruction = {
|
|
246
|
+
parts: [{ text: normalizedSystem }]
|
|
247
|
+
};
|
|
248
|
+
} else if (normalizedSystem.length > 0) {
|
|
249
|
+
googleRequest.systemInstruction = {
|
|
250
|
+
parts: normalizedSystem
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const generationConfig = {
|
|
255
|
+
...generationParams
|
|
256
|
+
};
|
|
257
|
+
if (request.structure) {
|
|
258
|
+
generationConfig.responseMimeType = "application/json";
|
|
259
|
+
generationConfig.responseSchema = request.structure;
|
|
260
|
+
}
|
|
261
|
+
if (Object.keys(generationConfig).length > 0) {
|
|
262
|
+
googleRequest.generationConfig = generationConfig;
|
|
263
|
+
}
|
|
264
|
+
const requestTools = [];
|
|
265
|
+
if (request.tools && request.tools.length > 0) {
|
|
266
|
+
requestTools.push({
|
|
267
|
+
functionDeclarations: request.tools.map(transformTool)
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
if (builtInTools && builtInTools.length > 0) {
|
|
271
|
+
requestTools.push(...builtInTools);
|
|
272
|
+
}
|
|
273
|
+
if (requestTools.length > 0) {
|
|
274
|
+
googleRequest.tools = requestTools;
|
|
275
|
+
}
|
|
276
|
+
if (toolConfig) {
|
|
277
|
+
googleRequest.toolConfig = toolConfig;
|
|
278
|
+
}
|
|
279
|
+
if (cachedContent) {
|
|
280
|
+
googleRequest.cachedContent = cachedContent;
|
|
281
|
+
}
|
|
282
|
+
return googleRequest;
|
|
283
|
+
}
|
|
284
|
+
function normalizeStopReason(reason) {
|
|
285
|
+
switch (reason) {
|
|
286
|
+
case "STOP":
|
|
287
|
+
return "end_turn";
|
|
288
|
+
case "MAX_TOKENS":
|
|
289
|
+
return "max_tokens";
|
|
290
|
+
case "SAFETY":
|
|
291
|
+
case "RECITATION":
|
|
292
|
+
return "content_filter";
|
|
293
|
+
case "TOOL_USE":
|
|
294
|
+
return "tool_use";
|
|
295
|
+
case "OTHER":
|
|
296
|
+
return "end_turn";
|
|
297
|
+
default:
|
|
298
|
+
return "end_turn";
|
|
299
|
+
}
|
|
300
|
+
}
|
|
280
301
|
function transformResponse(data) {
|
|
281
302
|
const candidate = data.candidates?.[0];
|
|
282
303
|
if (!candidate) {
|
|
@@ -381,6 +402,14 @@ function createStreamState() {
|
|
|
381
402
|
isFirstChunk: true
|
|
382
403
|
};
|
|
383
404
|
}
|
|
405
|
+
function decodeBase64(base64) {
|
|
406
|
+
const binaryString = atob(base64);
|
|
407
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
408
|
+
for (let i = 0; i < binaryString.length; i += 1) {
|
|
409
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
410
|
+
}
|
|
411
|
+
return bytes;
|
|
412
|
+
}
|
|
384
413
|
function transformStreamChunk(chunk, state) {
|
|
385
414
|
const events = [];
|
|
386
415
|
if (state.isFirstChunk) {
|
|
@@ -535,31 +564,6 @@ function buildResponseFromState(state) {
|
|
|
535
564
|
data: structuredData
|
|
536
565
|
};
|
|
537
566
|
}
|
|
538
|
-
function normalizeStopReason(reason) {
|
|
539
|
-
switch (reason) {
|
|
540
|
-
case "STOP":
|
|
541
|
-
return "end_turn";
|
|
542
|
-
case "MAX_TOKENS":
|
|
543
|
-
return "max_tokens";
|
|
544
|
-
case "SAFETY":
|
|
545
|
-
case "RECITATION":
|
|
546
|
-
return "content_filter";
|
|
547
|
-
case "TOOL_USE":
|
|
548
|
-
return "tool_use";
|
|
549
|
-
case "OTHER":
|
|
550
|
-
return "end_turn";
|
|
551
|
-
default:
|
|
552
|
-
return "end_turn";
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
function decodeBase64(base64) {
|
|
556
|
-
const binaryString = atob(base64);
|
|
557
|
-
const bytes = new Uint8Array(binaryString.length);
|
|
558
|
-
for (let i = 0; i < binaryString.length; i += 1) {
|
|
559
|
-
bytes[i] = binaryString.charCodeAt(i);
|
|
560
|
-
}
|
|
561
|
-
return bytes;
|
|
562
|
-
}
|
|
563
567
|
|
|
564
568
|
// src/providers/google/llm.ts
|
|
565
569
|
var GOOGLE_API_BASE = "https://generativelanguage.googleapis.com/v1beta";
|
|
@@ -704,10 +708,9 @@ function createLLMHandler() {
|
|
|
704
708
|
}
|
|
705
709
|
const events = transformStreamChunk(chunk, state);
|
|
706
710
|
for (const event of events) {
|
|
711
|
+
yield event;
|
|
707
712
|
if (request.structure && event.type === StreamEventType.TextDelta) {
|
|
708
713
|
yield objectDelta(event.delta.text ?? "", event.index);
|
|
709
|
-
} else {
|
|
710
|
-
yield event;
|
|
711
714
|
}
|
|
712
715
|
}
|
|
713
716
|
}
|
|
@@ -835,33 +838,27 @@ function getCapabilities() {
|
|
|
835
838
|
maxImages: 4
|
|
836
839
|
};
|
|
837
840
|
}
|
|
838
|
-
function
|
|
839
|
-
|
|
841
|
+
function buildParameters(params) {
|
|
842
|
+
return params ? { ...params } : {};
|
|
843
|
+
}
|
|
844
|
+
function transformResponse2(data) {
|
|
845
|
+
if (!data.predictions || data.predictions.length === 0) {
|
|
846
|
+
throw new UPPError(
|
|
847
|
+
"No images in response",
|
|
848
|
+
ErrorCode.ProviderError,
|
|
849
|
+
"google",
|
|
850
|
+
ModalityType.Image
|
|
851
|
+
);
|
|
852
|
+
}
|
|
853
|
+
const images = data.predictions.map((prediction) => {
|
|
854
|
+
const mimeType = prediction.mimeType ?? "image/png";
|
|
855
|
+
const image = Image.fromBase64(prediction.bytesBase64Encoded, mimeType);
|
|
856
|
+
return { image };
|
|
857
|
+
});
|
|
840
858
|
return {
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
bind(modelId) {
|
|
845
|
-
if (!providerRef) {
|
|
846
|
-
throw new UPPError(
|
|
847
|
-
"Provider reference not set. Handler must be used with createProvider().",
|
|
848
|
-
ErrorCode.InvalidRequest,
|
|
849
|
-
"google",
|
|
850
|
-
ModalityType.Image
|
|
851
|
-
);
|
|
852
|
-
}
|
|
853
|
-
const capabilities = getCapabilities();
|
|
854
|
-
const model = {
|
|
855
|
-
modelId,
|
|
856
|
-
capabilities,
|
|
857
|
-
get provider() {
|
|
858
|
-
return providerRef;
|
|
859
|
-
},
|
|
860
|
-
async generate(request) {
|
|
861
|
-
return executeGenerate(modelId, request);
|
|
862
|
-
}
|
|
863
|
-
};
|
|
864
|
-
return model;
|
|
859
|
+
images,
|
|
860
|
+
usage: {
|
|
861
|
+
imagesGenerated: images.length
|
|
865
862
|
}
|
|
866
863
|
};
|
|
867
864
|
}
|
|
@@ -900,27 +897,33 @@ async function executeGenerate(modelId, request) {
|
|
|
900
897
|
const data = await parseJsonResponse(response, "google", "image");
|
|
901
898
|
return transformResponse2(data);
|
|
902
899
|
}
|
|
903
|
-
function
|
|
904
|
-
|
|
905
|
-
}
|
|
906
|
-
function transformResponse2(data) {
|
|
907
|
-
if (!data.predictions || data.predictions.length === 0) {
|
|
908
|
-
throw new UPPError(
|
|
909
|
-
"No images in response",
|
|
910
|
-
ErrorCode.ProviderError,
|
|
911
|
-
"google",
|
|
912
|
-
ModalityType.Image
|
|
913
|
-
);
|
|
914
|
-
}
|
|
915
|
-
const images = data.predictions.map((prediction) => {
|
|
916
|
-
const mimeType = prediction.mimeType ?? "image/png";
|
|
917
|
-
const image = Image.fromBase64(prediction.bytesBase64Encoded, mimeType);
|
|
918
|
-
return { image };
|
|
919
|
-
});
|
|
900
|
+
function createImageHandler() {
|
|
901
|
+
let providerRef = null;
|
|
920
902
|
return {
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
903
|
+
_setProvider(provider) {
|
|
904
|
+
providerRef = provider;
|
|
905
|
+
},
|
|
906
|
+
bind(modelId) {
|
|
907
|
+
if (!providerRef) {
|
|
908
|
+
throw new UPPError(
|
|
909
|
+
"Provider reference not set. Handler must be used with createProvider().",
|
|
910
|
+
ErrorCode.InvalidRequest,
|
|
911
|
+
"google",
|
|
912
|
+
ModalityType.Image
|
|
913
|
+
);
|
|
914
|
+
}
|
|
915
|
+
const capabilities = getCapabilities();
|
|
916
|
+
const model = {
|
|
917
|
+
modelId,
|
|
918
|
+
capabilities,
|
|
919
|
+
get provider() {
|
|
920
|
+
return providerRef;
|
|
921
|
+
},
|
|
922
|
+
async generate(request) {
|
|
923
|
+
return executeGenerate(modelId, request);
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
return model;
|
|
924
927
|
}
|
|
925
928
|
};
|
|
926
929
|
}
|