@ank1015/providers 0.0.37 → 0.0.39

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 (56) hide show
  1. package/dist/index.d.ts +3 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +3 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/llm.d.ts.map +1 -1
  6. package/dist/llm.js +19 -1
  7. package/dist/llm.js.map +1 -1
  8. package/dist/models.generated.d.ts +89 -0
  9. package/dist/models.generated.d.ts.map +1 -1
  10. package/dist/models.generated.js +94 -5
  11. package/dist/models.generated.js.map +1 -1
  12. package/dist/providers/cerebras/complete.d.ts +3 -0
  13. package/dist/providers/cerebras/complete.d.ts.map +1 -0
  14. package/dist/providers/cerebras/complete.js +58 -0
  15. package/dist/providers/cerebras/complete.js.map +1 -0
  16. package/dist/providers/cerebras/index.d.ts +5 -0
  17. package/dist/providers/cerebras/index.d.ts.map +1 -0
  18. package/dist/providers/cerebras/index.js +4 -0
  19. package/dist/providers/cerebras/index.js.map +1 -0
  20. package/dist/providers/cerebras/stream.d.ts +3 -0
  21. package/dist/providers/cerebras/stream.d.ts.map +1 -0
  22. package/dist/providers/cerebras/stream.js +321 -0
  23. package/dist/providers/cerebras/stream.js.map +1 -0
  24. package/dist/providers/cerebras/types.d.ts +14 -0
  25. package/dist/providers/cerebras/types.d.ts.map +1 -0
  26. package/dist/providers/cerebras/types.js +2 -0
  27. package/dist/providers/cerebras/types.js.map +1 -0
  28. package/dist/providers/cerebras/utils.d.ts +19 -0
  29. package/dist/providers/cerebras/utils.d.ts.map +1 -0
  30. package/dist/providers/cerebras/utils.js +274 -0
  31. package/dist/providers/cerebras/utils.js.map +1 -0
  32. package/dist/providers/kimi/complete.d.ts +3 -0
  33. package/dist/providers/kimi/complete.d.ts.map +1 -0
  34. package/dist/providers/kimi/complete.js +58 -0
  35. package/dist/providers/kimi/complete.js.map +1 -0
  36. package/dist/providers/kimi/index.d.ts +5 -0
  37. package/dist/providers/kimi/index.d.ts.map +1 -0
  38. package/dist/providers/kimi/index.js +4 -0
  39. package/dist/providers/kimi/index.js.map +1 -0
  40. package/dist/providers/kimi/stream.d.ts +3 -0
  41. package/dist/providers/kimi/stream.d.ts.map +1 -0
  42. package/dist/providers/kimi/stream.js +323 -0
  43. package/dist/providers/kimi/stream.js.map +1 -0
  44. package/dist/providers/kimi/types.d.ts +12 -0
  45. package/dist/providers/kimi/types.d.ts.map +1 -0
  46. package/dist/providers/kimi/types.js +2 -0
  47. package/dist/providers/kimi/types.js.map +1 -0
  48. package/dist/providers/kimi/utils.d.ts +18 -0
  49. package/dist/providers/kimi/utils.d.ts.map +1 -0
  50. package/dist/providers/kimi/utils.js +302 -0
  51. package/dist/providers/kimi/utils.js.map +1 -0
  52. package/dist/types.d.ts +7 -1
  53. package/dist/types.d.ts.map +1 -1
  54. package/dist/types.js +1 -1
  55. package/dist/types.js.map +1 -1
  56. package/package.json +1 -1
@@ -0,0 +1,321 @@
1
+ import { createClient, buildParams, mapStopReason, getMockCerebrasMessage } from "./utils.js";
2
+ import { AssistantMessageEventStream } from "../../utils/event-stream.js";
3
+ import { parseStreamingJson } from "../../utils/json-parse.js";
4
+ import { validateToolArguments } from "../../utils/validation.js";
5
+ import { calculateCost } from "../../models.js";
6
+ export const streamCerebras = (model, context, options, id) => {
7
+ const stream = new AssistantMessageEventStream();
8
+ (async () => {
9
+ const startTimestamp = Date.now();
10
+ const output = {
11
+ role: "assistant",
12
+ api: model.api,
13
+ model: model,
14
+ id,
15
+ content: [],
16
+ usage: {
17
+ input: 0,
18
+ output: 0,
19
+ cacheRead: 0,
20
+ cacheWrite: 0,
21
+ totalTokens: 0,
22
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
23
+ },
24
+ stopReason: "stop",
25
+ timestamp: startTimestamp,
26
+ duration: 0
27
+ };
28
+ // Build final ChatCompletion from streamed chunks
29
+ let finalResponse = getMockCerebrasMessage();
30
+ let accumulatedContent = '';
31
+ let accumulatedReasoning = '';
32
+ const accumulatedToolCalls = new Map();
33
+ try {
34
+ const client = createClient(model, options?.apiKey);
35
+ const params = buildParams(model, context, options);
36
+ const cerebrasStream = await client.chat.completions.create({ ...params, stream: true }, { signal: options?.signal });
37
+ stream.push({ type: "start", message: { ...output, timestamp: Date.now() } });
38
+ let currentBlock = null;
39
+ const blocks = output.content;
40
+ const blockIndex = () => blocks.length - 1;
41
+ // Track tool call states for streaming
42
+ const toolCallBlocks = new Map();
43
+ for await (const chunk of cerebrasStream) {
44
+ const choice = chunk.choices[0];
45
+ if (!choice)
46
+ continue;
47
+ const delta = choice.delta;
48
+ // Handle reasoning content (Cerebras uses 'reasoning' field when reasoning_format=parsed)
49
+ if (delta.reasoning) {
50
+ accumulatedReasoning += delta.reasoning;
51
+ // Start thinking block if needed
52
+ if (!currentBlock || currentBlock.type !== 'thinking') {
53
+ // End previous block if exists
54
+ if (currentBlock) {
55
+ if (currentBlock.type === 'response') {
56
+ stream.push({
57
+ type: "text_end",
58
+ contentIndex: blockIndex(),
59
+ content: currentBlock.content,
60
+ message: { ...output, timestamp: Date.now() },
61
+ });
62
+ }
63
+ }
64
+ currentBlock = { type: 'thinking', thinkingText: '' };
65
+ output.content.push(currentBlock);
66
+ stream.push({ type: "thinking_start", contentIndex: blockIndex(), message: { ...output, timestamp: Date.now() } });
67
+ }
68
+ if (currentBlock.type === 'thinking') {
69
+ currentBlock.thinkingText += delta.reasoning;
70
+ stream.push({
71
+ type: "thinking_delta",
72
+ contentIndex: blockIndex(),
73
+ delta: delta.reasoning,
74
+ message: { ...output, timestamp: Date.now() },
75
+ });
76
+ }
77
+ }
78
+ // Handle text content
79
+ if (delta.content) {
80
+ accumulatedContent += delta.content;
81
+ // Transition from thinking to response if needed
82
+ if (!currentBlock || currentBlock.type === 'thinking') {
83
+ // End thinking block if exists
84
+ if (currentBlock && currentBlock.type === 'thinking') {
85
+ stream.push({
86
+ type: "thinking_end",
87
+ contentIndex: blockIndex(),
88
+ content: currentBlock.thinkingText,
89
+ message: { ...output, timestamp: Date.now() },
90
+ });
91
+ }
92
+ currentBlock = { type: 'response', content: [{ type: 'text', content: '' }] };
93
+ output.content.push(currentBlock);
94
+ stream.push({ type: "text_start", contentIndex: blockIndex(), message: { ...output, timestamp: Date.now() } });
95
+ }
96
+ if (currentBlock.type === 'response') {
97
+ const index = currentBlock.content.findIndex((c) => c.type === 'text');
98
+ if (index !== -1) {
99
+ currentBlock.content[index].content += delta.content;
100
+ }
101
+ stream.push({
102
+ type: "text_delta",
103
+ contentIndex: blockIndex(),
104
+ delta: delta.content,
105
+ message: { ...output, timestamp: Date.now() },
106
+ });
107
+ }
108
+ }
109
+ // Handle tool calls
110
+ if (delta.tool_calls) {
111
+ // End current text/thinking block if we're starting tool calls
112
+ if (currentBlock && currentBlock.type !== 'toolCall') {
113
+ if (currentBlock.type === 'response') {
114
+ stream.push({
115
+ type: "text_end",
116
+ contentIndex: blockIndex(),
117
+ content: currentBlock.content,
118
+ message: { ...output, timestamp: Date.now() },
119
+ });
120
+ }
121
+ else if (currentBlock.type === 'thinking') {
122
+ stream.push({
123
+ type: "thinking_end",
124
+ contentIndex: blockIndex(),
125
+ content: currentBlock.thinkingText,
126
+ message: { ...output, timestamp: Date.now() },
127
+ });
128
+ }
129
+ currentBlock = null;
130
+ }
131
+ for (const toolCallDelta of delta.tool_calls) {
132
+ const toolIndex = toolCallDelta.index;
133
+ // Get or create accumulated tool call
134
+ if (!accumulatedToolCalls.has(toolIndex)) {
135
+ accumulatedToolCalls.set(toolIndex, {
136
+ id: toolCallDelta.id || '',
137
+ type: 'function',
138
+ function: { name: '', arguments: '' }
139
+ });
140
+ }
141
+ const accumulated = accumulatedToolCalls.get(toolIndex);
142
+ // Update accumulated data
143
+ if (toolCallDelta.id) {
144
+ accumulated.id = toolCallDelta.id;
145
+ }
146
+ if (toolCallDelta.function?.name) {
147
+ accumulated.function.name += toolCallDelta.function.name;
148
+ }
149
+ if (toolCallDelta.function?.arguments) {
150
+ accumulated.function.arguments += toolCallDelta.function.arguments;
151
+ }
152
+ // Get or create tool call block
153
+ if (!toolCallBlocks.has(toolIndex)) {
154
+ const toolBlock = {
155
+ type: 'toolCall',
156
+ toolCallId: accumulated.id,
157
+ name: accumulated.function.name,
158
+ arguments: {},
159
+ partialJson: ''
160
+ };
161
+ toolCallBlocks.set(toolIndex, toolBlock);
162
+ output.content.push(toolBlock);
163
+ currentBlock = toolBlock;
164
+ stream.push({ type: "toolcall_start", contentIndex: blockIndex(), message: { ...output, timestamp: Date.now() } });
165
+ }
166
+ const toolBlock = toolCallBlocks.get(toolIndex);
167
+ // Update tool block with new data
168
+ if (toolCallDelta.id) {
169
+ toolBlock.toolCallId = accumulated.id;
170
+ }
171
+ if (toolCallDelta.function?.name) {
172
+ toolBlock.name = accumulated.function.name;
173
+ }
174
+ if (toolCallDelta.function?.arguments) {
175
+ toolBlock.partialJson += toolCallDelta.function.arguments;
176
+ toolBlock.arguments = parseStreamingJson(toolBlock.partialJson);
177
+ stream.push({
178
+ type: "toolcall_delta",
179
+ contentIndex: blocks.indexOf(toolBlock),
180
+ delta: toolCallDelta.function.arguments,
181
+ message: { ...output, timestamp: Date.now() },
182
+ });
183
+ }
184
+ }
185
+ }
186
+ // Handle finish reason
187
+ if (choice.finish_reason) {
188
+ output.stopReason = mapStopReason(choice.finish_reason);
189
+ }
190
+ // Handle usage (typically in the last chunk)
191
+ if (chunk.usage) {
192
+ const usage = chunk.usage;
193
+ const cacheHitTokens = usage.prompt_tokens_details?.cached_tokens || 0;
194
+ output.usage = {
195
+ input: (usage.prompt_tokens || 0) - cacheHitTokens,
196
+ output: usage.completion_tokens || 0,
197
+ cacheRead: cacheHitTokens,
198
+ cacheWrite: 0,
199
+ totalTokens: usage.total_tokens || 0,
200
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
201
+ };
202
+ calculateCost(model, output.usage);
203
+ }
204
+ }
205
+ // End any remaining blocks
206
+ if (currentBlock) {
207
+ if (currentBlock.type === 'response') {
208
+ stream.push({
209
+ type: "text_end",
210
+ contentIndex: blockIndex(),
211
+ content: currentBlock.content,
212
+ message: { ...output, timestamp: Date.now() },
213
+ });
214
+ }
215
+ else if (currentBlock.type === 'thinking') {
216
+ stream.push({
217
+ type: "thinking_end",
218
+ contentIndex: blockIndex(),
219
+ content: currentBlock.thinkingText,
220
+ message: { ...output, timestamp: Date.now() },
221
+ });
222
+ }
223
+ }
224
+ // End tool call blocks and validate arguments
225
+ for (const [, toolBlock] of toolCallBlocks) {
226
+ // Parse final JSON
227
+ try {
228
+ toolBlock.arguments = JSON.parse(toolBlock.partialJson || '{}');
229
+ }
230
+ catch {
231
+ // Keep the parsed streaming result
232
+ }
233
+ // Validate tool arguments if tool definition is available
234
+ if (context.tools) {
235
+ const tool = context.tools.find((t) => t.name === toolBlock.name);
236
+ if (tool) {
237
+ toolBlock.arguments = validateToolArguments(tool, toolBlock);
238
+ }
239
+ }
240
+ // Clean up partialJson before sending final event
241
+ const { partialJson: _, ...cleanToolCall } = toolBlock;
242
+ stream.push({
243
+ type: "toolcall_end",
244
+ contentIndex: blocks.indexOf(toolBlock),
245
+ toolCall: cleanToolCall,
246
+ message: { ...output, timestamp: Date.now() }
247
+ });
248
+ }
249
+ // Clean up partialJson from output content
250
+ for (const block of output.content) {
251
+ if ('partialJson' in block) {
252
+ delete block.partialJson;
253
+ }
254
+ }
255
+ // Ensure stopReason is toolUse if we have tool calls
256
+ if (output.content.some((b) => b.type === 'toolCall') && output.stopReason === 'stop') {
257
+ output.stopReason = 'toolUse';
258
+ }
259
+ if (options?.signal?.aborted) {
260
+ throw new Error("Request was aborted");
261
+ }
262
+ if (output.stopReason === "aborted" || output.stopReason === "error") {
263
+ throw new Error(`Stream ended with status: ${output.stopReason}${output.errorMessage ? ` - ${output.errorMessage}` : ""}`);
264
+ }
265
+ // Build final ChatCompletion response
266
+ finalResponse = {
267
+ id: `chatcmpl-${id}`,
268
+ object: "chat.completion",
269
+ created: Math.floor(startTimestamp / 1000),
270
+ model: model.id,
271
+ choices: [{
272
+ index: 0,
273
+ message: {
274
+ role: "assistant",
275
+ content: accumulatedContent || null,
276
+ refusal: null,
277
+ ...(accumulatedToolCalls.size > 0 && {
278
+ tool_calls: Array.from(accumulatedToolCalls.values())
279
+ })
280
+ },
281
+ finish_reason: output.stopReason === 'toolUse' ? 'tool_calls' : 'stop',
282
+ logprobs: null
283
+ }],
284
+ usage: {
285
+ prompt_tokens: output.usage.input + output.usage.cacheRead,
286
+ completion_tokens: output.usage.output,
287
+ total_tokens: output.usage.totalTokens
288
+ }
289
+ };
290
+ // Add reasoning to the message if present
291
+ if (accumulatedReasoning) {
292
+ finalResponse.choices[0].message.reasoning = accumulatedReasoning;
293
+ }
294
+ stream.push({ type: "done", reason: output.stopReason, message: { ...output, timestamp: Date.now() } });
295
+ const baseAssistantMessage = {
296
+ ...output,
297
+ message: finalResponse,
298
+ timestamp: Date.now(),
299
+ duration: Date.now() - startTimestamp
300
+ };
301
+ stream.end(baseAssistantMessage);
302
+ }
303
+ catch (error) {
304
+ for (const block of output.content) {
305
+ delete block.partialJson;
306
+ }
307
+ output.stopReason = options?.signal?.aborted ? "aborted" : "error";
308
+ output.errorMessage = error instanceof Error ? error.message : JSON.stringify(error);
309
+ stream.push({ type: "error", reason: output.stopReason, message: { ...output, timestamp: Date.now() } });
310
+ const baseAssistantMessage = {
311
+ ...output,
312
+ message: finalResponse,
313
+ timestamp: Date.now(),
314
+ duration: Date.now() - startTimestamp
315
+ };
316
+ stream.end(baseAssistantMessage);
317
+ }
318
+ })();
319
+ return stream;
320
+ };
321
+ //# sourceMappingURL=stream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream.js","sourceRoot":"","sources":["../../../src/providers/cerebras/stream.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAC9F,OAAO,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E,GAAG,cAAc;aACrC,CAAA;YACD,MAAM,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;QAClC,CAAC;IAEF,CAAC,CAAC,EAAE,CAAC;IAEL,OAAO,MAAM,CAAC;AACf,CAAC,CAAA"}
@@ -0,0 +1,14 @@
1
+ import type { ChatCompletionCreateParamsBase } from "openai/resources/chat/completions.js";
2
+ export type CerebrasReasoningFormat = "parsed" | "raw" | "hidden" | "none";
3
+ export type CerebrasReasoningEffort = "low" | "medium" | "high";
4
+ type Props = {
5
+ apiKey?: string;
6
+ signal?: AbortSignal;
7
+ reasoning_format?: CerebrasReasoningFormat;
8
+ reasoning_effort?: CerebrasReasoningEffort;
9
+ disable_reasoning?: boolean;
10
+ clear_thinking?: boolean;
11
+ };
12
+ export type CerebrasProviderOptions = Omit<ChatCompletionCreateParamsBase, 'model' | 'messages'> & Props;
13
+ export {};
14
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/providers/cerebras/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,sCAAsC,CAAA;AAE1F,MAAM,MAAM,uBAAuB,GAAG,QAAQ,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;AAC3E,MAAM,MAAM,uBAAuB,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;AAEhE,KAAK,KAAK,GAAG;IACZ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,WAAW,CAAC;IAErB,gBAAgB,CAAC,EAAE,uBAAuB,CAAC;IAE3C,gBAAgB,CAAC,EAAE,uBAAuB,CAAC;IAE3C,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAE5B,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,CAAA;AAED,MAAM,MAAM,uBAAuB,GAAG,IAAI,CAAC,8BAA8B,EAAE,OAAO,GAAG,UAAU,CAAC,GAAG,KAAK,CAAA"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/providers/cerebras/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ import OpenAI from "openai";
2
+ import { AssistantResponse, Context, Model, StopReason, Tool, Usage } from "../../types.js";
3
+ import type { ChatCompletion, ChatCompletionMessageParam, ChatCompletionTool, ChatCompletionCreateParamsNonStreaming } from "openai/resources/chat/completions.js";
4
+ import { CerebrasProviderOptions } from "./types.js";
5
+ export declare function createClient(model: Model<"cerebras">, apiKey?: string): OpenAI;
6
+ export declare function getResponseAssistantResponse(response: ChatCompletion): AssistantResponse;
7
+ export declare function getAssistantStopReason(response: ChatCompletion): StopReason;
8
+ export declare function getResponseUsage(response: ChatCompletion, model: Model<'cerebras'>): Usage;
9
+ export declare function buildParams(model: Model<"cerebras">, context: Context, options: CerebrasProviderOptions): ChatCompletionCreateParamsNonStreaming & {
10
+ reasoning_format?: string;
11
+ reasoning_effort?: string;
12
+ disable_reasoning?: boolean;
13
+ clear_thinking?: boolean;
14
+ };
15
+ export declare function buildCerebrasMessages(_model: Model<'cerebras'>, context: Context): ChatCompletionMessageParam[];
16
+ export declare function convertTools(tools: readonly Tool[]): ChatCompletionTool[];
17
+ export declare function mapStopReason(finishReason: string | null | undefined): StopReason;
18
+ export declare function getMockCerebrasMessage(): ChatCompletion;
19
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/providers/cerebras/utils.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,iBAAiB,EAAwB,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,KAAK,EAAe,MAAM,gBAAgB,CAAC;AAC/H,OAAO,KAAK,EACX,cAAc,EACd,0BAA0B,EAC1B,kBAAkB,EAClB,sCAAsC,EAGtC,MAAM,sCAAsC,CAAC;AAG9C,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AAarD,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,UAkBrE;AAED,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,cAAc,GAAG,iBAAiB,CA4CxF;AAED,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,cAAc,GAAG,UAAU,CAG3E;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,cAAc,EAAE,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC,GAAG,KAAK,CAgB1F;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,uBAAuB;uBAcnF,MAAM;uBACN,MAAM;wBACL,OAAO;qBACV,OAAO;EAkDzB;AAED,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,KAAK,CAAC,UAAU,CAAC,EAAE,OAAO,EAAE,OAAO,GAAG,0BAA0B,EAAE,CAmG/G;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,GAAG,kBAAkB,EAAE,CAmBzE;AAED,wBAAgB,aAAa,CAAC,YAAY,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,UAAU,CAejF;AAED,wBAAgB,sBAAsB,IAAI,cAAc,CAsBvD"}
@@ -0,0 +1,274 @@
1
+ import OpenAI from "openai";
2
+ import { sanitizeSurrogates } from "../../utils/sanitize-unicode.js";
3
+ import { calculateCost } from "../../models.js";
4
+ export function createClient(model, apiKey) {
5
+ if (!apiKey) {
6
+ if (!process.env.CEREBRAS_API_KEY) {
7
+ throw new Error("Cerebras API key is required. Set CEREBRAS_API_KEY environment variable or pass it as an argument.");
8
+ }
9
+ apiKey = process.env.CEREBRAS_API_KEY;
10
+ }
11
+ return new OpenAI({
12
+ apiKey,
13
+ baseURL: model.baseUrl,
14
+ dangerouslyAllowBrowser: true,
15
+ defaultHeaders: {
16
+ "User-Agent": "cerebras-providers-sdk",
17
+ ...model.headers,
18
+ },
19
+ });
20
+ }
21
+ export function getResponseAssistantResponse(response) {
22
+ const assistantResponse = [];
23
+ const choice = response.choices[0];
24
+ if (!choice?.message) {
25
+ return assistantResponse;
26
+ }
27
+ const message = choice.message;
28
+ // Handle reasoning content (Cerebras uses 'reasoning' field when reasoning_format=parsed)
29
+ if (message.reasoning) {
30
+ assistantResponse.push({
31
+ type: 'thinking',
32
+ thinkingText: message.reasoning
33
+ });
34
+ }
35
+ // Handle text content
36
+ if (message.content) {
37
+ assistantResponse.push({
38
+ type: 'response',
39
+ content: [{
40
+ type: 'text',
41
+ content: message.content
42
+ }]
43
+ });
44
+ }
45
+ // Handle tool calls
46
+ if (message.tool_calls) {
47
+ for (const toolCall of message.tool_calls) {
48
+ if (toolCall.type === 'function') {
49
+ assistantResponse.push({
50
+ type: 'toolCall',
51
+ toolCallId: toolCall.id,
52
+ name: toolCall.function.name,
53
+ arguments: JSON.parse(toolCall.function.arguments || '{}')
54
+ });
55
+ }
56
+ }
57
+ }
58
+ return assistantResponse;
59
+ }
60
+ export function getAssistantStopReason(response) {
61
+ const finishReason = response.choices[0]?.finish_reason;
62
+ return mapStopReason(finishReason);
63
+ }
64
+ export function getResponseUsage(response, model) {
65
+ const responseUsage = response.usage;
66
+ // Cerebras reports cache hits via prompt_tokens_details.cached_tokens
67
+ const cacheHitTokens = responseUsage?.prompt_tokens_details?.cached_tokens || 0;
68
+ const usage = {
69
+ input: (responseUsage?.prompt_tokens || 0) - cacheHitTokens,
70
+ output: responseUsage?.completion_tokens || 0,
71
+ cacheRead: cacheHitTokens,
72
+ cacheWrite: 0,
73
+ totalTokens: responseUsage?.total_tokens || 0,
74
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
75
+ };
76
+ calculateCost(model, usage);
77
+ return usage;
78
+ }
79
+ export function buildParams(model, context, options) {
80
+ const messages = buildCerebrasMessages(model, context);
81
+ const { apiKey, signal, reasoning_format, reasoning_effort, disable_reasoning, clear_thinking, ...cerebrasOptions } = options;
82
+ const params = {
83
+ ...cerebrasOptions,
84
+ model: model.id,
85
+ messages,
86
+ stream: false
87
+ };
88
+ // Add reasoning format - default to 'parsed' to get structured reasoning
89
+ if (reasoning_format) {
90
+ params.reasoning_format = reasoning_format;
91
+ }
92
+ else if (model.reasoning) {
93
+ // Default to parsed for reasoning models
94
+ params.reasoning_format = 'parsed';
95
+ }
96
+ // Model-specific reasoning parameters
97
+ if (reasoning_effort !== undefined) {
98
+ // GPT-OSS specific
99
+ params.reasoning_effort = reasoning_effort;
100
+ }
101
+ if (disable_reasoning !== undefined) {
102
+ // GLM specific
103
+ params.disable_reasoning = disable_reasoning;
104
+ }
105
+ if (clear_thinking !== undefined) {
106
+ // GLM specific
107
+ params.clear_thinking = clear_thinking;
108
+ }
109
+ // Add tools if available and supported
110
+ if (context.tools && context.tools.length > 0 && model.tools.includes('function_calling')) {
111
+ const tools = [];
112
+ const convertedTools = convertTools(context.tools);
113
+ for (const convertedTool of convertedTools) {
114
+ tools.push(convertedTool);
115
+ }
116
+ if (cerebrasOptions.tools) {
117
+ for (const optionTool of cerebrasOptions.tools) {
118
+ tools.push(optionTool);
119
+ }
120
+ }
121
+ params.tools = tools;
122
+ }
123
+ return params;
124
+ }
125
+ export function buildCerebrasMessages(_model, context) {
126
+ const messages = [];
127
+ // Add system prompt
128
+ if (context.systemPrompt) {
129
+ messages.push({
130
+ role: 'system',
131
+ content: sanitizeSurrogates(context.systemPrompt)
132
+ });
133
+ }
134
+ for (const message of context.messages) {
135
+ // Handle user messages
136
+ if (message.role === 'user') {
137
+ // Cerebras chat completions API supports text content
138
+ const textContents = message.content
139
+ .filter(c => c.type === 'text')
140
+ .map(c => sanitizeSurrogates(c.content))
141
+ .join('\n');
142
+ if (textContents) {
143
+ messages.push({
144
+ role: 'user',
145
+ content: textContents
146
+ });
147
+ }
148
+ }
149
+ // Handle tool results
150
+ if (message.role === 'toolResult') {
151
+ const textContent = message.content
152
+ .filter(c => c.type === 'text')
153
+ .map(c => {
154
+ const text = c.content;
155
+ return message.isError ? `[TOOL ERROR] ${text}` : text;
156
+ })
157
+ .join('\n');
158
+ const toolMessage = {
159
+ role: 'tool',
160
+ tool_call_id: message.toolCallId,
161
+ content: sanitizeSurrogates(textContent || (message.isError ? '[TOOL ERROR]' : ''))
162
+ };
163
+ messages.push(toolMessage);
164
+ }
165
+ // Handle assistant messages
166
+ if (message.role === 'assistant') {
167
+ if (message.model.api === 'cerebras') {
168
+ // Native Cerebras message - reconstruct from original
169
+ const baseMessage = message;
170
+ const originalMessage = baseMessage.message.choices[0]?.message;
171
+ if (originalMessage) {
172
+ const assistantMessage = originalMessage;
173
+ messages.push(assistantMessage);
174
+ }
175
+ }
176
+ else {
177
+ // Convert from other providers using normalized content
178
+ let textContent = '';
179
+ const toolCalls = [];
180
+ for (const contentBlock of message.content) {
181
+ if (contentBlock.type === 'thinking') {
182
+ // For Cerebras, we include thinking in <think> tags for GLM/Qwen compatibility
183
+ textContent = `<think>${sanitizeSurrogates(contentBlock.thinkingText)}</think>` + textContent;
184
+ }
185
+ else if (contentBlock.type === 'response') {
186
+ const text = contentBlock.content
187
+ .filter(c => c.type === 'text')
188
+ .map(c => sanitizeSurrogates(c.content))
189
+ .join('');
190
+ textContent += text;
191
+ }
192
+ else if (contentBlock.type === 'toolCall') {
193
+ toolCalls.push({
194
+ id: contentBlock.toolCallId,
195
+ type: 'function',
196
+ function: {
197
+ name: contentBlock.name,
198
+ arguments: JSON.stringify(contentBlock.arguments)
199
+ }
200
+ });
201
+ }
202
+ }
203
+ const assistantMessage = {
204
+ role: 'assistant',
205
+ content: textContent || null
206
+ };
207
+ if (toolCalls.length > 0) {
208
+ assistantMessage.tool_calls = toolCalls;
209
+ }
210
+ messages.push(assistantMessage);
211
+ }
212
+ }
213
+ }
214
+ return messages;
215
+ }
216
+ export function convertTools(tools) {
217
+ return tools.map((tool) => {
218
+ const params = tool.parameters;
219
+ // Add additionalProperties: false for Cerebras strict mode compatibility
220
+ const parameters = {
221
+ ...params,
222
+ additionalProperties: false
223
+ };
224
+ return {
225
+ type: "function",
226
+ function: {
227
+ name: tool.name,
228
+ description: tool.description,
229
+ parameters,
230
+ strict: true
231
+ }
232
+ };
233
+ });
234
+ }
235
+ export function mapStopReason(finishReason) {
236
+ if (!finishReason)
237
+ return "stop";
238
+ switch (finishReason) {
239
+ case "stop":
240
+ return "stop";
241
+ case "length":
242
+ return "length";
243
+ case "tool_calls":
244
+ return "toolUse";
245
+ case "content_filter":
246
+ return "error";
247
+ default:
248
+ return "stop";
249
+ }
250
+ }
251
+ export function getMockCerebrasMessage() {
252
+ return {
253
+ id: "chatcmpl-123",
254
+ object: "chat.completion",
255
+ created: Math.floor(Date.now() / 1000),
256
+ model: "zai-glm-4.7",
257
+ choices: [{
258
+ index: 0,
259
+ message: {
260
+ role: "assistant",
261
+ content: "",
262
+ refusal: null
263
+ },
264
+ finish_reason: "stop",
265
+ logprobs: null
266
+ }],
267
+ usage: {
268
+ prompt_tokens: 0,
269
+ completion_tokens: 0,
270
+ total_tokens: 0
271
+ }
272
+ };
273
+ }
274
+ //# sourceMappingURL=utils.js.map