@providerprotocol/ai 0.0.33 → 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 +94 -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
|
@@ -0,0 +1,649 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseSSEStream
|
|
3
|
+
} from "../chunk-PMK5LZ5Z.js";
|
|
4
|
+
import {
|
|
5
|
+
parseJsonResponse
|
|
6
|
+
} from "../chunk-A2IM7PGT.js";
|
|
7
|
+
import {
|
|
8
|
+
resolveApiKey
|
|
9
|
+
} from "../chunk-ARVM24K2.js";
|
|
10
|
+
import {
|
|
11
|
+
createProvider
|
|
12
|
+
} from "../chunk-JA3UZALR.js";
|
|
13
|
+
import {
|
|
14
|
+
AssistantMessage,
|
|
15
|
+
generateId,
|
|
16
|
+
isAssistantMessage,
|
|
17
|
+
isToolResultMessage,
|
|
18
|
+
isUserMessage
|
|
19
|
+
} from "../chunk-BRP5XJ6Q.js";
|
|
20
|
+
import {
|
|
21
|
+
doFetch,
|
|
22
|
+
doStreamFetch,
|
|
23
|
+
normalizeHttpError,
|
|
24
|
+
toError
|
|
25
|
+
} from "../chunk-AY55T37A.js";
|
|
26
|
+
import {
|
|
27
|
+
ErrorCode,
|
|
28
|
+
ModalityType,
|
|
29
|
+
UPPError
|
|
30
|
+
} from "../chunk-COS4ON4G.js";
|
|
31
|
+
import {
|
|
32
|
+
StreamEventType,
|
|
33
|
+
objectDelta
|
|
34
|
+
} from "../chunk-6S222DHN.js";
|
|
35
|
+
|
|
36
|
+
// src/providers/groq/transform.ts
|
|
37
|
+
function normalizeSystem(system) {
|
|
38
|
+
if (system === void 0 || system === null) return void 0;
|
|
39
|
+
if (typeof system === "string") return system;
|
|
40
|
+
if (!Array.isArray(system)) {
|
|
41
|
+
throw new UPPError(
|
|
42
|
+
"System prompt must be a string or an array of text blocks",
|
|
43
|
+
ErrorCode.InvalidRequest,
|
|
44
|
+
"groq",
|
|
45
|
+
ModalityType.LLM
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const texts = [];
|
|
49
|
+
for (const block of system) {
|
|
50
|
+
if (!block || typeof block !== "object" || !("text" in block)) {
|
|
51
|
+
throw new UPPError(
|
|
52
|
+
"System prompt array must contain objects with a text field",
|
|
53
|
+
ErrorCode.InvalidRequest,
|
|
54
|
+
"groq",
|
|
55
|
+
ModalityType.LLM
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
const textValue = block.text;
|
|
59
|
+
if (typeof textValue !== "string") {
|
|
60
|
+
throw new UPPError(
|
|
61
|
+
"System prompt text must be a string",
|
|
62
|
+
ErrorCode.InvalidRequest,
|
|
63
|
+
"groq",
|
|
64
|
+
ModalityType.LLM
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
if (textValue.length > 0) {
|
|
68
|
+
texts.push(textValue);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return texts.length > 0 ? texts.join("\n\n") : void 0;
|
|
72
|
+
}
|
|
73
|
+
function filterValidContent(content) {
|
|
74
|
+
return content.filter((c) => c && typeof c.type === "string");
|
|
75
|
+
}
|
|
76
|
+
function transformContentBlock(block) {
|
|
77
|
+
switch (block.type) {
|
|
78
|
+
case "text":
|
|
79
|
+
return { type: "text", text: block.text };
|
|
80
|
+
case "image": {
|
|
81
|
+
const imageBlock = block;
|
|
82
|
+
let url;
|
|
83
|
+
if (imageBlock.source.type === "base64") {
|
|
84
|
+
url = `data:${imageBlock.mimeType};base64,${imageBlock.source.data}`;
|
|
85
|
+
} else if (imageBlock.source.type === "url") {
|
|
86
|
+
url = imageBlock.source.url;
|
|
87
|
+
} else if (imageBlock.source.type === "bytes") {
|
|
88
|
+
const base64 = Buffer.from(imageBlock.source.data).toString("base64");
|
|
89
|
+
url = `data:${imageBlock.mimeType};base64,${base64}`;
|
|
90
|
+
} else {
|
|
91
|
+
throw new UPPError(
|
|
92
|
+
"Unknown image source type",
|
|
93
|
+
ErrorCode.InvalidRequest,
|
|
94
|
+
"groq",
|
|
95
|
+
ModalityType.LLM
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
type: "image_url",
|
|
100
|
+
image_url: { url }
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
case "document":
|
|
104
|
+
throw new UPPError(
|
|
105
|
+
"Groq does not support document input",
|
|
106
|
+
ErrorCode.InvalidRequest,
|
|
107
|
+
"groq",
|
|
108
|
+
ModalityType.LLM
|
|
109
|
+
);
|
|
110
|
+
default:
|
|
111
|
+
throw new UPPError(
|
|
112
|
+
`Unsupported content type: ${block.type}`,
|
|
113
|
+
ErrorCode.InvalidRequest,
|
|
114
|
+
"groq",
|
|
115
|
+
ModalityType.LLM
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function transformMessage(message) {
|
|
120
|
+
if (isUserMessage(message)) {
|
|
121
|
+
const validContent = filterValidContent(message.content);
|
|
122
|
+
if (validContent.length === 1 && validContent[0]?.type === "text") {
|
|
123
|
+
return {
|
|
124
|
+
role: "user",
|
|
125
|
+
content: validContent[0].text
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
role: "user",
|
|
130
|
+
content: validContent.map(transformContentBlock)
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
if (isAssistantMessage(message)) {
|
|
134
|
+
const validContent = filterValidContent(message.content);
|
|
135
|
+
const textContent = validContent.filter((c) => c.type === "text").map((c) => c.text).join("");
|
|
136
|
+
const assistantMessage = {
|
|
137
|
+
role: "assistant",
|
|
138
|
+
content: textContent || null
|
|
139
|
+
};
|
|
140
|
+
if (message.toolCalls && message.toolCalls.length > 0) {
|
|
141
|
+
assistantMessage.tool_calls = message.toolCalls.map((call) => ({
|
|
142
|
+
id: call.toolCallId,
|
|
143
|
+
type: "function",
|
|
144
|
+
function: {
|
|
145
|
+
name: call.toolName,
|
|
146
|
+
arguments: JSON.stringify(call.arguments)
|
|
147
|
+
}
|
|
148
|
+
}));
|
|
149
|
+
}
|
|
150
|
+
return assistantMessage;
|
|
151
|
+
}
|
|
152
|
+
if (isToolResultMessage(message)) {
|
|
153
|
+
const results = message.results.map((result) => ({
|
|
154
|
+
role: "tool",
|
|
155
|
+
tool_call_id: result.toolCallId,
|
|
156
|
+
content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
|
|
157
|
+
}));
|
|
158
|
+
return results[0] ?? null;
|
|
159
|
+
}
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
function transformToolResults(message) {
|
|
163
|
+
if (!isToolResultMessage(message)) {
|
|
164
|
+
const single = transformMessage(message);
|
|
165
|
+
return single ? [single] : [];
|
|
166
|
+
}
|
|
167
|
+
return message.results.map((result) => ({
|
|
168
|
+
role: "tool",
|
|
169
|
+
tool_call_id: result.toolCallId,
|
|
170
|
+
content: typeof result.result === "string" ? result.result : JSON.stringify(result.result)
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
function transformMessages(messages, system) {
|
|
174
|
+
const result = [];
|
|
175
|
+
const normalizedSystem = normalizeSystem(system);
|
|
176
|
+
if (normalizedSystem) {
|
|
177
|
+
result.push({
|
|
178
|
+
role: "system",
|
|
179
|
+
content: normalizedSystem
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
for (const message of messages) {
|
|
183
|
+
if (isToolResultMessage(message)) {
|
|
184
|
+
const toolMessages = transformToolResults(message);
|
|
185
|
+
result.push(...toolMessages);
|
|
186
|
+
} else {
|
|
187
|
+
const transformed = transformMessage(message);
|
|
188
|
+
if (transformed) {
|
|
189
|
+
result.push(transformed);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
function extractToolOptions(tool) {
|
|
196
|
+
const groqMeta = tool.metadata?.groq;
|
|
197
|
+
return { strict: groqMeta?.strict };
|
|
198
|
+
}
|
|
199
|
+
function transformTool(tool) {
|
|
200
|
+
const { strict } = extractToolOptions(tool);
|
|
201
|
+
return {
|
|
202
|
+
type: "function",
|
|
203
|
+
function: {
|
|
204
|
+
name: tool.name,
|
|
205
|
+
description: tool.description,
|
|
206
|
+
parameters: {
|
|
207
|
+
type: "object",
|
|
208
|
+
properties: tool.parameters.properties,
|
|
209
|
+
required: tool.parameters.required,
|
|
210
|
+
...tool.parameters.additionalProperties !== void 0 ? { additionalProperties: tool.parameters.additionalProperties } : {}
|
|
211
|
+
},
|
|
212
|
+
...strict !== void 0 ? { strict } : {}
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function transformRequest(request, modelId) {
|
|
217
|
+
const params = request.params ?? {};
|
|
218
|
+
const groqRequest = {
|
|
219
|
+
...params,
|
|
220
|
+
model: modelId,
|
|
221
|
+
messages: transformMessages(request.messages, request.system)
|
|
222
|
+
};
|
|
223
|
+
if (request.tools && request.tools.length > 0) {
|
|
224
|
+
groqRequest.tools = request.tools.map(transformTool);
|
|
225
|
+
}
|
|
226
|
+
if (request.structure) {
|
|
227
|
+
const schema = {
|
|
228
|
+
type: "object",
|
|
229
|
+
properties: request.structure.properties,
|
|
230
|
+
required: request.structure.required,
|
|
231
|
+
...request.structure.additionalProperties !== void 0 ? { additionalProperties: request.structure.additionalProperties } : { additionalProperties: false }
|
|
232
|
+
};
|
|
233
|
+
if (request.structure.description) {
|
|
234
|
+
schema.description = request.structure.description;
|
|
235
|
+
}
|
|
236
|
+
groqRequest.response_format = {
|
|
237
|
+
type: "json_schema",
|
|
238
|
+
json_schema: {
|
|
239
|
+
name: "json_response",
|
|
240
|
+
description: request.structure.description,
|
|
241
|
+
schema,
|
|
242
|
+
strict: true
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
return groqRequest;
|
|
247
|
+
}
|
|
248
|
+
function transformResponse(data) {
|
|
249
|
+
const choice = data.choices[0];
|
|
250
|
+
if (!choice) {
|
|
251
|
+
throw new UPPError(
|
|
252
|
+
"No choices in Groq response",
|
|
253
|
+
ErrorCode.InvalidResponse,
|
|
254
|
+
"groq",
|
|
255
|
+
ModalityType.LLM
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
const textContent = [];
|
|
259
|
+
let structuredData;
|
|
260
|
+
if (choice.message.content) {
|
|
261
|
+
textContent.push({ type: "text", text: choice.message.content });
|
|
262
|
+
try {
|
|
263
|
+
structuredData = JSON.parse(choice.message.content);
|
|
264
|
+
} catch {
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const toolCalls = [];
|
|
268
|
+
if (choice.message.tool_calls) {
|
|
269
|
+
for (const call of choice.message.tool_calls) {
|
|
270
|
+
let args = {};
|
|
271
|
+
try {
|
|
272
|
+
args = JSON.parse(call.function.arguments);
|
|
273
|
+
} catch {
|
|
274
|
+
}
|
|
275
|
+
toolCalls.push({
|
|
276
|
+
toolCallId: call.id,
|
|
277
|
+
toolName: call.function.name,
|
|
278
|
+
arguments: args
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const message = new AssistantMessage(
|
|
283
|
+
textContent,
|
|
284
|
+
toolCalls.length > 0 ? toolCalls : void 0,
|
|
285
|
+
{
|
|
286
|
+
id: data.id || generateId(),
|
|
287
|
+
metadata: {
|
|
288
|
+
groq: {
|
|
289
|
+
model: data.model,
|
|
290
|
+
finish_reason: choice.finish_reason,
|
|
291
|
+
system_fingerprint: data.system_fingerprint
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
);
|
|
296
|
+
const usage = {
|
|
297
|
+
inputTokens: data.usage.prompt_tokens,
|
|
298
|
+
outputTokens: data.usage.completion_tokens,
|
|
299
|
+
totalTokens: data.usage.total_tokens,
|
|
300
|
+
cacheReadTokens: data.usage.prompt_tokens_details?.cached_tokens ?? 0,
|
|
301
|
+
cacheWriteTokens: 0
|
|
302
|
+
};
|
|
303
|
+
let stopReason = "end_turn";
|
|
304
|
+
switch (choice.finish_reason) {
|
|
305
|
+
case "stop":
|
|
306
|
+
stopReason = "end_turn";
|
|
307
|
+
break;
|
|
308
|
+
case "length":
|
|
309
|
+
stopReason = "max_tokens";
|
|
310
|
+
break;
|
|
311
|
+
case "tool_calls":
|
|
312
|
+
stopReason = "tool_use";
|
|
313
|
+
break;
|
|
314
|
+
case "content_filter":
|
|
315
|
+
stopReason = "content_filter";
|
|
316
|
+
break;
|
|
317
|
+
}
|
|
318
|
+
return {
|
|
319
|
+
message,
|
|
320
|
+
usage,
|
|
321
|
+
stopReason,
|
|
322
|
+
data: structuredData
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
function createStreamState() {
|
|
326
|
+
return {
|
|
327
|
+
id: "",
|
|
328
|
+
model: "",
|
|
329
|
+
text: "",
|
|
330
|
+
toolCalls: /* @__PURE__ */ new Map(),
|
|
331
|
+
finishReason: null,
|
|
332
|
+
inputTokens: 0,
|
|
333
|
+
outputTokens: 0,
|
|
334
|
+
cacheReadTokens: 0
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function transformStreamEvent(chunk, state) {
|
|
338
|
+
const events = [];
|
|
339
|
+
if (chunk.id && !state.id) {
|
|
340
|
+
state.id = chunk.id;
|
|
341
|
+
events.push({ type: StreamEventType.MessageStart, index: 0, delta: {} });
|
|
342
|
+
}
|
|
343
|
+
if (chunk.model) {
|
|
344
|
+
state.model = chunk.model;
|
|
345
|
+
}
|
|
346
|
+
const choice = chunk.choices[0];
|
|
347
|
+
if (choice) {
|
|
348
|
+
if (choice.delta.content) {
|
|
349
|
+
state.text += choice.delta.content;
|
|
350
|
+
events.push({
|
|
351
|
+
type: StreamEventType.TextDelta,
|
|
352
|
+
index: 0,
|
|
353
|
+
delta: { text: choice.delta.content }
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
if (choice.delta.tool_calls) {
|
|
357
|
+
for (const toolCallDelta of choice.delta.tool_calls) {
|
|
358
|
+
const index = toolCallDelta.index;
|
|
359
|
+
let toolCall = state.toolCalls.get(index);
|
|
360
|
+
if (!toolCall) {
|
|
361
|
+
toolCall = { id: "", name: "", arguments: "" };
|
|
362
|
+
state.toolCalls.set(index, toolCall);
|
|
363
|
+
}
|
|
364
|
+
if (toolCallDelta.id) {
|
|
365
|
+
toolCall.id = toolCallDelta.id;
|
|
366
|
+
}
|
|
367
|
+
if (toolCallDelta.function?.name) {
|
|
368
|
+
toolCall.name = toolCallDelta.function.name;
|
|
369
|
+
}
|
|
370
|
+
if (toolCallDelta.function?.arguments) {
|
|
371
|
+
toolCall.arguments += toolCallDelta.function.arguments;
|
|
372
|
+
events.push({
|
|
373
|
+
type: StreamEventType.ToolCallDelta,
|
|
374
|
+
index,
|
|
375
|
+
delta: {
|
|
376
|
+
toolCallId: toolCall.id,
|
|
377
|
+
toolName: toolCall.name,
|
|
378
|
+
argumentsJson: toolCallDelta.function.arguments
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (choice.finish_reason) {
|
|
385
|
+
state.finishReason = choice.finish_reason;
|
|
386
|
+
events.push({ type: StreamEventType.MessageStop, index: 0, delta: {} });
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
const usageData = chunk.usage ?? chunk.x_groq?.usage;
|
|
390
|
+
if (usageData) {
|
|
391
|
+
state.inputTokens = usageData.prompt_tokens;
|
|
392
|
+
state.outputTokens = usageData.completion_tokens;
|
|
393
|
+
state.cacheReadTokens = usageData.prompt_tokens_details?.cached_tokens ?? 0;
|
|
394
|
+
}
|
|
395
|
+
return events;
|
|
396
|
+
}
|
|
397
|
+
function buildResponseFromState(state) {
|
|
398
|
+
const textContent = [];
|
|
399
|
+
let structuredData;
|
|
400
|
+
if (state.text) {
|
|
401
|
+
textContent.push({ type: "text", text: state.text });
|
|
402
|
+
try {
|
|
403
|
+
structuredData = JSON.parse(state.text);
|
|
404
|
+
} catch {
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
const toolCalls = [];
|
|
408
|
+
for (const [, toolCall] of state.toolCalls) {
|
|
409
|
+
let args = {};
|
|
410
|
+
if (toolCall.arguments) {
|
|
411
|
+
try {
|
|
412
|
+
args = JSON.parse(toolCall.arguments);
|
|
413
|
+
} catch {
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
toolCalls.push({
|
|
417
|
+
toolCallId: toolCall.id,
|
|
418
|
+
toolName: toolCall.name,
|
|
419
|
+
arguments: args
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
const messageId = state.id || generateId();
|
|
423
|
+
const message = new AssistantMessage(
|
|
424
|
+
textContent,
|
|
425
|
+
toolCalls.length > 0 ? toolCalls : void 0,
|
|
426
|
+
{
|
|
427
|
+
id: messageId,
|
|
428
|
+
metadata: {
|
|
429
|
+
groq: {
|
|
430
|
+
model: state.model,
|
|
431
|
+
finish_reason: state.finishReason
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
);
|
|
436
|
+
const usage = {
|
|
437
|
+
inputTokens: state.inputTokens,
|
|
438
|
+
outputTokens: state.outputTokens,
|
|
439
|
+
totalTokens: state.inputTokens + state.outputTokens,
|
|
440
|
+
cacheReadTokens: state.cacheReadTokens,
|
|
441
|
+
cacheWriteTokens: 0
|
|
442
|
+
};
|
|
443
|
+
let stopReason = "end_turn";
|
|
444
|
+
switch (state.finishReason) {
|
|
445
|
+
case "stop":
|
|
446
|
+
stopReason = "end_turn";
|
|
447
|
+
break;
|
|
448
|
+
case "length":
|
|
449
|
+
stopReason = "max_tokens";
|
|
450
|
+
break;
|
|
451
|
+
case "tool_calls":
|
|
452
|
+
stopReason = "tool_use";
|
|
453
|
+
break;
|
|
454
|
+
case "content_filter":
|
|
455
|
+
stopReason = "content_filter";
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
return {
|
|
459
|
+
message,
|
|
460
|
+
usage,
|
|
461
|
+
stopReason,
|
|
462
|
+
data: structuredData
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// src/providers/groq/llm.ts
|
|
467
|
+
var GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions";
|
|
468
|
+
var GROQ_CAPABILITIES = {
|
|
469
|
+
streaming: true,
|
|
470
|
+
tools: true,
|
|
471
|
+
structuredOutput: true,
|
|
472
|
+
imageInput: true,
|
|
473
|
+
documentInput: false,
|
|
474
|
+
videoInput: false,
|
|
475
|
+
audioInput: false
|
|
476
|
+
};
|
|
477
|
+
function createLLMHandler() {
|
|
478
|
+
let providerRef = null;
|
|
479
|
+
return {
|
|
480
|
+
_setProvider(provider) {
|
|
481
|
+
providerRef = provider;
|
|
482
|
+
},
|
|
483
|
+
bind(modelId) {
|
|
484
|
+
if (!providerRef) {
|
|
485
|
+
throw new UPPError(
|
|
486
|
+
"Provider reference not set. Handler must be used with createProvider() or have _setProvider called.",
|
|
487
|
+
ErrorCode.InvalidRequest,
|
|
488
|
+
"groq",
|
|
489
|
+
ModalityType.LLM
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
const model = {
|
|
493
|
+
modelId,
|
|
494
|
+
capabilities: GROQ_CAPABILITIES,
|
|
495
|
+
get provider() {
|
|
496
|
+
return providerRef;
|
|
497
|
+
},
|
|
498
|
+
async complete(request) {
|
|
499
|
+
const apiKey = await resolveApiKey(
|
|
500
|
+
request.config,
|
|
501
|
+
"GROQ_API_KEY",
|
|
502
|
+
"groq",
|
|
503
|
+
"llm"
|
|
504
|
+
);
|
|
505
|
+
const baseUrl = request.config.baseUrl ?? GROQ_API_URL;
|
|
506
|
+
const body = transformRequest(request, modelId);
|
|
507
|
+
const headers = {
|
|
508
|
+
"Content-Type": "application/json",
|
|
509
|
+
Authorization: `Bearer ${apiKey}`
|
|
510
|
+
};
|
|
511
|
+
if (request.config.headers) {
|
|
512
|
+
for (const [key, value] of Object.entries(request.config.headers)) {
|
|
513
|
+
if (value !== void 0) {
|
|
514
|
+
headers[key] = value;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const response = await doFetch(
|
|
519
|
+
baseUrl,
|
|
520
|
+
{
|
|
521
|
+
method: "POST",
|
|
522
|
+
headers,
|
|
523
|
+
body: JSON.stringify(body),
|
|
524
|
+
signal: request.signal
|
|
525
|
+
},
|
|
526
|
+
request.config,
|
|
527
|
+
"groq",
|
|
528
|
+
"llm"
|
|
529
|
+
);
|
|
530
|
+
const data = await parseJsonResponse(response, "groq", "llm");
|
|
531
|
+
return transformResponse(data);
|
|
532
|
+
},
|
|
533
|
+
stream(request) {
|
|
534
|
+
const state = createStreamState();
|
|
535
|
+
let responseResolve;
|
|
536
|
+
let responseReject;
|
|
537
|
+
const responsePromise = new Promise((resolve, reject) => {
|
|
538
|
+
responseResolve = resolve;
|
|
539
|
+
responseReject = reject;
|
|
540
|
+
});
|
|
541
|
+
async function* generateEvents() {
|
|
542
|
+
try {
|
|
543
|
+
const apiKey = await resolveApiKey(
|
|
544
|
+
request.config,
|
|
545
|
+
"GROQ_API_KEY",
|
|
546
|
+
"groq",
|
|
547
|
+
"llm"
|
|
548
|
+
);
|
|
549
|
+
const baseUrl = request.config.baseUrl ?? GROQ_API_URL;
|
|
550
|
+
const body = transformRequest(request, modelId);
|
|
551
|
+
body.stream = true;
|
|
552
|
+
body.stream_options = { include_usage: true };
|
|
553
|
+
const headers = {
|
|
554
|
+
"Content-Type": "application/json",
|
|
555
|
+
Authorization: `Bearer ${apiKey}`,
|
|
556
|
+
Accept: "text/event-stream"
|
|
557
|
+
};
|
|
558
|
+
if (request.config.headers) {
|
|
559
|
+
for (const [key, value] of Object.entries(request.config.headers)) {
|
|
560
|
+
if (value !== void 0) {
|
|
561
|
+
headers[key] = value;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const response = await doStreamFetch(
|
|
566
|
+
baseUrl,
|
|
567
|
+
{
|
|
568
|
+
method: "POST",
|
|
569
|
+
headers,
|
|
570
|
+
body: JSON.stringify(body),
|
|
571
|
+
signal: request.signal
|
|
572
|
+
},
|
|
573
|
+
request.config,
|
|
574
|
+
"groq",
|
|
575
|
+
"llm"
|
|
576
|
+
);
|
|
577
|
+
if (!response.ok) {
|
|
578
|
+
const error = await normalizeHttpError(response, "groq", "llm");
|
|
579
|
+
responseReject(error);
|
|
580
|
+
throw error;
|
|
581
|
+
}
|
|
582
|
+
if (!response.body) {
|
|
583
|
+
const error = new UPPError(
|
|
584
|
+
"No response body for streaming request",
|
|
585
|
+
ErrorCode.ProviderError,
|
|
586
|
+
"groq",
|
|
587
|
+
ModalityType.LLM
|
|
588
|
+
);
|
|
589
|
+
responseReject(error);
|
|
590
|
+
throw error;
|
|
591
|
+
}
|
|
592
|
+
for await (const data of parseSSEStream(response.body)) {
|
|
593
|
+
if (data === "[DONE]") {
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
if (typeof data === "object" && data !== null) {
|
|
597
|
+
const chunk = data;
|
|
598
|
+
if ("error" in chunk && chunk.error) {
|
|
599
|
+
const errorData = chunk.error;
|
|
600
|
+
const error = new UPPError(
|
|
601
|
+
errorData.message ?? "Unknown error",
|
|
602
|
+
ErrorCode.ProviderError,
|
|
603
|
+
"groq",
|
|
604
|
+
ModalityType.LLM
|
|
605
|
+
);
|
|
606
|
+
responseReject(error);
|
|
607
|
+
throw error;
|
|
608
|
+
}
|
|
609
|
+
const uppEvents = transformStreamEvent(chunk, state);
|
|
610
|
+
for (const event of uppEvents) {
|
|
611
|
+
yield event;
|
|
612
|
+
if (request.structure && event.type === StreamEventType.TextDelta) {
|
|
613
|
+
yield objectDelta(event.delta.text ?? "", event.index);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
responseResolve(buildResponseFromState(state));
|
|
619
|
+
} catch (error) {
|
|
620
|
+
const err = toError(error);
|
|
621
|
+
responseReject(err);
|
|
622
|
+
throw err;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
return {
|
|
626
|
+
[Symbol.asyncIterator]() {
|
|
627
|
+
return generateEvents();
|
|
628
|
+
},
|
|
629
|
+
response: responsePromise
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
return model;
|
|
634
|
+
}
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// src/providers/groq/index.ts
|
|
639
|
+
var groq = createProvider({
|
|
640
|
+
name: "groq",
|
|
641
|
+
version: "1.0.0",
|
|
642
|
+
handlers: {
|
|
643
|
+
llm: createLLMHandler()
|
|
644
|
+
}
|
|
645
|
+
});
|
|
646
|
+
export {
|
|
647
|
+
groq
|
|
648
|
+
};
|
|
649
|
+
//# sourceMappingURL=index.js.map
|