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