@simulacra-ai/fireworksai 0.0.5

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/dist/index.cjs ADDED
@@ -0,0 +1,593 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ FIREWORKS_BASE_URL: () => FIREWORKS_BASE_URL,
24
+ FireworksAIProvider: () => FireworksAIProvider,
25
+ createFireworksAIClient: () => createFireworksAIClient
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/fireworksai-provider.ts
30
+ var import_openai = require("openai");
31
+ var FIREWORKS_BASE_URL = "https://api.fireworks.ai/inference/v1";
32
+ function createFireworksAIClient(apiKey) {
33
+ return new import_openai.OpenAI({
34
+ apiKey,
35
+ baseURL: FIREWORKS_BASE_URL
36
+ });
37
+ }
38
+ var FireworksAIProvider = class _FireworksAIProvider {
39
+ #sdk;
40
+ #config;
41
+ context_transformers;
42
+ /**
43
+ * Creates a new FireworksAI provider instance.
44
+ *
45
+ * @param sdk - An OpenAI SDK client configured for the Fireworks endpoint (see `createFireworksAIClient`).
46
+ * @param config - Configuration options for the provider.
47
+ * @param context_transformers - Provider-level context transformers.
48
+ */
49
+ constructor(sdk, config, context_transformers = []) {
50
+ this.#sdk = sdk;
51
+ this.#config = config;
52
+ this.context_transformers = context_transformers;
53
+ }
54
+ /**
55
+ * Executes a model request and streams the response through the provided receiver.
56
+ *
57
+ * @param request - The request containing messages, tools, and system prompt.
58
+ * @param receiver - The receiver that handles streaming events.
59
+ * @param cancellation - Token to signal cancellation of the request.
60
+ * @returns A promise that resolves when the request completes.
61
+ */
62
+ async execute_request(request, receiver, cancellation) {
63
+ const { model, max_tokens, ...api_extras } = this.#config;
64
+ const params = {
65
+ ...api_extras,
66
+ model,
67
+ stream: true,
68
+ max_tokens,
69
+ ...request.tools.length > 0 ? {
70
+ tool_choice: "auto",
71
+ tools: request.tools.map((t) => to_fireworksai_tool(t))
72
+ } : {},
73
+ messages: [
74
+ ...get_system_message(request.system),
75
+ ...request.messages.flatMap((m) => to_fireworksai_messages(m))
76
+ ],
77
+ stream_options: {
78
+ include_usage: true
79
+ }
80
+ };
81
+ receiver.before_request({ params });
82
+ receiver.request_raw(params);
83
+ const stream = await this.#sdk.chat.completions.create(params);
84
+ this.#stream_response(stream, receiver, cancellation);
85
+ }
86
+ /**
87
+ * Creates a clone of this provider with the same configuration.
88
+ *
89
+ * @returns A new provider instance with identical configuration.
90
+ */
91
+ clone() {
92
+ return new _FireworksAIProvider(this.#sdk, this.#config, this.context_transformers);
93
+ }
94
+ async #stream_response(stream, receiver, cancellation) {
95
+ try {
96
+ let response;
97
+ for await (const response_chunk of stream) {
98
+ if (cancellation.is_cancellation_requested) {
99
+ receiver.cancel();
100
+ return;
101
+ }
102
+ receiver.stream_raw(response_chunk);
103
+ const { choices: choices_chunk, ...rest } = response_chunk;
104
+ response = {
105
+ ...response,
106
+ ...rest,
107
+ choices: response?.choices ?? []
108
+ };
109
+ for (const choice_chunk of choices_chunk) {
110
+ if (!response.choices[choice_chunk.index]) {
111
+ response.choices[choice_chunk.index] = choice_chunk;
112
+ const message2 = from_fireworksai_completion(response_chunk, choice_chunk);
113
+ for (const content of message2.content) {
114
+ receiver.start_content({ content, message: message2, usage: {} });
115
+ }
116
+ receiver.start_message({ message: message2, usage: {} });
117
+ continue;
118
+ }
119
+ const { delta: delta_chunk, ...rest2 } = choice_chunk;
120
+ const choice = response.choices[choice_chunk.index] = {
121
+ ...response.choices[choice_chunk.index],
122
+ ...rest2,
123
+ delta: {
124
+ ...response.choices[choice_chunk.index]?.delta
125
+ }
126
+ };
127
+ if (delta_chunk.role) {
128
+ choice.delta.role = delta_chunk.role;
129
+ }
130
+ if (delta_chunk.refusal) {
131
+ if (!choice.delta.refusal) {
132
+ choice.delta.refusal = "";
133
+ }
134
+ choice.delta.refusal += delta_chunk.refusal;
135
+ }
136
+ if (delta_chunk.content) {
137
+ if (!choice.delta.content) {
138
+ choice.delta.content = delta_chunk.content;
139
+ receiver.start_content({
140
+ content: from_fireworksai_content(choice.delta),
141
+ message: from_fireworksai_completion(response_chunk, choice),
142
+ usage: response?.usage ? from_fireworksai_usage(response.usage) : {}
143
+ });
144
+ receiver.update_message({
145
+ message: from_fireworksai_completion(response_chunk, choice),
146
+ usage: response?.usage ? from_fireworksai_usage(response.usage) : {}
147
+ });
148
+ } else {
149
+ choice.delta.content += delta_chunk.content;
150
+ receiver.update_content({
151
+ content: from_fireworksai_content(choice.delta),
152
+ message: from_fireworksai_completion(response_chunk, choice),
153
+ usage: response?.usage ? from_fireworksai_usage(response.usage) : {}
154
+ });
155
+ }
156
+ }
157
+ if (delta_chunk.tool_calls) {
158
+ if (!choice.delta.tool_calls) {
159
+ choice.delta.tool_calls = [];
160
+ }
161
+ for (const tool_call_chunk of delta_chunk.tool_calls) {
162
+ if (!choice.delta.tool_calls[tool_call_chunk.index]) {
163
+ choice.delta.tool_calls[tool_call_chunk.index] = tool_call_chunk;
164
+ receiver.start_content({
165
+ content: from_fireworksai_tool_call(tool_call_chunk),
166
+ message: from_fireworksai_completion(response_chunk, choice),
167
+ usage: response?.usage ? from_fireworksai_usage(response.usage) : {}
168
+ });
169
+ receiver.update_message({
170
+ message: from_fireworksai_completion(response_chunk, choice),
171
+ usage: response?.usage ? from_fireworksai_usage(response.usage) : {}
172
+ });
173
+ } else {
174
+ const tool_call = choice.delta.tool_calls[tool_call_chunk.index];
175
+ if (tool_call_chunk.id) {
176
+ tool_call.id = tool_call_chunk.id;
177
+ }
178
+ if (tool_call_chunk.type) {
179
+ tool_call.type = tool_call_chunk.type;
180
+ }
181
+ if (tool_call_chunk.function) {
182
+ if (!tool_call.function) {
183
+ tool_call.function = tool_call_chunk.function;
184
+ } else {
185
+ if (tool_call_chunk.function.name) {
186
+ tool_call.function.name = tool_call_chunk.function.name;
187
+ }
188
+ if (tool_call_chunk.function.arguments) {
189
+ if (!tool_call.function.arguments) {
190
+ tool_call.function.arguments = "";
191
+ }
192
+ tool_call.function.arguments += tool_call_chunk.function.arguments;
193
+ }
194
+ }
195
+ }
196
+ receiver.update_content({
197
+ content: from_fireworksai_tool_call(tool_call),
198
+ message: from_fireworksai_completion(response_chunk, choice),
199
+ usage: response?.usage ? from_fireworksai_usage(response.usage) : {}
200
+ });
201
+ receiver.update_message({
202
+ message: from_fireworksai_completion(response_chunk, choice),
203
+ usage: response?.usage ? from_fireworksai_usage(response.usage) : {}
204
+ });
205
+ }
206
+ }
207
+ }
208
+ }
209
+ }
210
+ if (!response || !response.choices?.[0]) {
211
+ throw new Error("no data");
212
+ }
213
+ receiver.response_raw({ ...response });
214
+ const message = from_fireworksai_completion(response, response.choices[0]);
215
+ const usage = response?.usage ? from_fireworksai_usage(response.usage) : {};
216
+ for (const content of message.content) {
217
+ receiver.complete_content({ content, message, usage });
218
+ }
219
+ receiver.complete_message({ message, usage, ...map_stop_reason(response) });
220
+ } catch (error) {
221
+ receiver.error(error);
222
+ }
223
+ }
224
+ };
225
+ function get_system_message(system) {
226
+ if (!system) {
227
+ return [];
228
+ }
229
+ return [
230
+ {
231
+ role: "system",
232
+ content: system
233
+ }
234
+ ];
235
+ }
236
+ function to_fireworksai_tool(tool) {
237
+ function map_parameter_type(parameter) {
238
+ switch (parameter.type) {
239
+ case "object":
240
+ return {
241
+ type: parameter.required ? parameter.type : [parameter.type, "null"],
242
+ description: parameter.description,
243
+ properties: Object.fromEntries(
244
+ Object.entries(parameter.properties).map(([k, v]) => [k, map_parameter_type(v)])
245
+ ),
246
+ additionalProperties: false,
247
+ required: Object.entries(parameter.properties).map(([k]) => k)
248
+ };
249
+ case "array":
250
+ return {
251
+ type: parameter.required ? parameter.type : [parameter.type, "null"],
252
+ description: parameter.description,
253
+ items: map_parameter_type(parameter.items)
254
+ };
255
+ default:
256
+ return {
257
+ type: parameter.required ? parameter.type : [parameter.type, "null"],
258
+ description: parameter.default !== void 0 ? parameter.description ? `${parameter.description} (default: ${parameter.default})` : `default: ${parameter.default}` : parameter.description,
259
+ enum: "enum" in parameter ? parameter.enum : void 0
260
+ };
261
+ }
262
+ }
263
+ return {
264
+ type: "function",
265
+ function: {
266
+ name: tool.name,
267
+ description: tool.description,
268
+ parameters: map_parameter_type({
269
+ type: "object",
270
+ required: true,
271
+ properties: Object.fromEntries(
272
+ tool.parameters.map(({ name, ...parameter }) => [name, parameter])
273
+ )
274
+ })
275
+ }
276
+ };
277
+ }
278
+ function from_fireworksai_completion(completion, choice) {
279
+ let contents = [];
280
+ for (const k in choice.delta) {
281
+ const key = k;
282
+ if (key === "role") {
283
+ continue;
284
+ }
285
+ if (key === "content" && choice.delta.content) {
286
+ contents = [...contents, from_fireworksai_content(choice.delta)];
287
+ } else if (key === "refusal" && choice.delta.refusal) {
288
+ contents = [...contents, from_fireworksai_refusal(choice.delta)];
289
+ } else if (key === "tool_calls" && choice.delta.tool_calls) {
290
+ contents = [
291
+ ...contents,
292
+ ...choice.delta.tool_calls.map((t) => from_fireworksai_tool_call(t))
293
+ ];
294
+ } else if (choice.delta[key] !== void 0 && choice.delta[key] !== null) {
295
+ const { [key]: data } = choice.delta;
296
+ contents = [
297
+ ...contents,
298
+ {
299
+ type: "raw",
300
+ model_kind: "fireworksai",
301
+ data: JSON.stringify({ [key]: data })
302
+ }
303
+ ];
304
+ }
305
+ }
306
+ return {
307
+ id: completion.id,
308
+ timestamp: completion.created,
309
+ role: map_role(choice),
310
+ content: contents
311
+ };
312
+ }
313
+ function from_fireworksai_refusal(content) {
314
+ const { refusal, tool_calls: _, function_call: __, content: ___, role: ____, ...rest } = content;
315
+ return {
316
+ type: "text",
317
+ text: refusal,
318
+ extended: {
319
+ ...rest,
320
+ fireworksai_refusal: true
321
+ }
322
+ };
323
+ }
324
+ function from_fireworksai_content(content) {
325
+ const {
326
+ content: c,
327
+ tool_calls: _,
328
+ function_call: __,
329
+ refusal: ___,
330
+ role: ____,
331
+ ...rest
332
+ } = content;
333
+ return {
334
+ type: "text",
335
+ text: c,
336
+ extended: rest
337
+ };
338
+ }
339
+ function from_fireworksai_tool_call(tool_call) {
340
+ const { id: tool_request_id, function: fn, type: _, index: __, ...extended } = tool_call;
341
+ let params;
342
+ try {
343
+ params = JSON.parse(fn?.arguments ?? "{}");
344
+ } catch {
345
+ params = fn?.arguments;
346
+ }
347
+ return {
348
+ tool_request_id,
349
+ type: "tool",
350
+ tool: fn?.name,
351
+ params,
352
+ extended
353
+ };
354
+ }
355
+ function to_fireworksai_messages(message) {
356
+ if (message.role === "assistant") {
357
+ return [to_fireworksai_assistant_message(message)];
358
+ }
359
+ const tool_result_content = message.content.filter((c) => c.type === "tool_result");
360
+ const other_content = message.content.filter((c) => c.type !== "tool_result");
361
+ const ordered_content = [...tool_result_content, ...other_content];
362
+ const results = [];
363
+ let result;
364
+ for (const content of ordered_content) {
365
+ if (content.type === "text") {
366
+ if (!result) {
367
+ result = {
368
+ role: "user",
369
+ content: content.text
370
+ };
371
+ } else if (result.role === "tool") {
372
+ results.push(result);
373
+ result = {
374
+ role: "user",
375
+ content: content.text
376
+ };
377
+ } else {
378
+ if (typeof result.content === "string") {
379
+ result.content = [
380
+ {
381
+ type: "text",
382
+ text: result.content
383
+ }
384
+ ];
385
+ }
386
+ if (!result.content) {
387
+ result.content = [
388
+ {
389
+ type: "text",
390
+ text: content.text
391
+ }
392
+ ];
393
+ } else {
394
+ result.content.push({
395
+ type: "text",
396
+ text: content.text
397
+ });
398
+ }
399
+ }
400
+ } else if (content.type === "tool_result") {
401
+ if (!result) {
402
+ result = {
403
+ role: "tool",
404
+ tool_call_id: content.tool_request_id,
405
+ content: JSON.stringify(content.result)
406
+ };
407
+ } else if (result.role !== "tool" || result.tool_call_id !== content.tool_request_id) {
408
+ results.push(result);
409
+ result = {
410
+ role: "tool",
411
+ tool_call_id: content.tool_request_id,
412
+ content: JSON.stringify(content.result)
413
+ };
414
+ } else {
415
+ if (typeof result.content === "string") {
416
+ result.content = [
417
+ {
418
+ type: "text",
419
+ text: result.content
420
+ }
421
+ ];
422
+ }
423
+ result.content.push({
424
+ type: "text",
425
+ text: JSON.stringify(content.result)
426
+ });
427
+ }
428
+ } else if (content.type === "raw") {
429
+ result = {
430
+ ...result ?? {},
431
+ ...JSON.parse(content.data)
432
+ };
433
+ }
434
+ }
435
+ if (result) {
436
+ results.push(result);
437
+ }
438
+ return results;
439
+ }
440
+ function to_fireworksai_assistant_message(message) {
441
+ let result = {
442
+ role: "assistant"
443
+ };
444
+ for (const content of message.content) {
445
+ switch (content.type) {
446
+ case "text":
447
+ if (content.extended && (content.extended.fireworksai_refusal === true || content.extended.openai_refusal === true)) {
448
+ result.refusal = content.text;
449
+ } else {
450
+ if (typeof result.content === "string") {
451
+ result.content = [
452
+ {
453
+ type: "text",
454
+ text: result.content
455
+ }
456
+ ];
457
+ }
458
+ if (!result.content) {
459
+ result.content = content.text;
460
+ } else {
461
+ result.content.push({
462
+ type: "text",
463
+ text: content.text
464
+ });
465
+ }
466
+ }
467
+ break;
468
+ case "tool":
469
+ if (!result.tool_calls) {
470
+ result.tool_calls = [];
471
+ }
472
+ result.tool_calls.push({
473
+ id: content.tool_request_id,
474
+ type: "function",
475
+ function: {
476
+ name: content.tool,
477
+ arguments: JSON.stringify(content.params)
478
+ }
479
+ });
480
+ break;
481
+ case "raw":
482
+ if (content.model_kind !== "fireworksai" && content.model_kind !== "openai") {
483
+ if (typeof result.content === "string") {
484
+ result.content = [
485
+ {
486
+ type: "text",
487
+ text: result.content
488
+ }
489
+ ];
490
+ }
491
+ if (!result.content) {
492
+ result.content = content.data;
493
+ } else {
494
+ result.content.push({
495
+ type: "text",
496
+ text: content.data
497
+ });
498
+ }
499
+ break;
500
+ }
501
+ result = {
502
+ ...result,
503
+ ...JSON.parse(content.data)
504
+ };
505
+ break;
506
+ case "thinking":
507
+ if (typeof result.content === "string") {
508
+ result.content = [
509
+ {
510
+ type: "text",
511
+ text: result.content
512
+ }
513
+ ];
514
+ }
515
+ if (!result.content) {
516
+ result.content = content.thought;
517
+ } else {
518
+ result.content.push({
519
+ type: "text",
520
+ text: content.thought
521
+ });
522
+ }
523
+ break;
524
+ default:
525
+ throw new Error("unexpected content type");
526
+ }
527
+ }
528
+ if (result.tool_calls && result.content === void 0) {
529
+ result.content = null;
530
+ }
531
+ return result;
532
+ }
533
+ function from_fireworksai_usage(usage) {
534
+ return {
535
+ input_tokens: usage?.prompt_tokens,
536
+ output_tokens: usage?.completion_tokens
537
+ };
538
+ }
539
+ function map_stop_reason(completion) {
540
+ for (const choice of completion.choices) {
541
+ switch (choice.finish_reason) {
542
+ case "content_filter":
543
+ return {
544
+ stop_reason: "error",
545
+ stop_details: choice.finish_reason
546
+ };
547
+ case "function_call":
548
+ return {
549
+ stop_reason: "tool_use"
550
+ };
551
+ case "length":
552
+ return {
553
+ stop_reason: "max_tokens"
554
+ };
555
+ case "stop":
556
+ return {
557
+ stop_reason: "end_turn"
558
+ };
559
+ case "tool_calls":
560
+ return {
561
+ stop_reason: "tool_use"
562
+ };
563
+ default:
564
+ return {
565
+ stop_reason: "other",
566
+ stop_details: `${choice.finish_reason}`
567
+ };
568
+ }
569
+ }
570
+ return {
571
+ stop_reason: "other"
572
+ };
573
+ }
574
+ function map_role(choice) {
575
+ switch (choice.delta.role) {
576
+ case "user":
577
+ case "developer":
578
+ case "system":
579
+ return "user";
580
+ case "assistant":
581
+ case "tool":
582
+ return "assistant";
583
+ default:
584
+ throw new Error("invalid role");
585
+ }
586
+ }
587
+ // Annotate the CommonJS export names for ESM import in node:
588
+ 0 && (module.exports = {
589
+ FIREWORKS_BASE_URL,
590
+ FireworksAIProvider,
591
+ createFireworksAIClient
592
+ });
593
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/fireworksai-provider.ts"],"sourcesContent":["export {\n FireworksAIProvider,\n createFireworksAIClient,\n FIREWORKS_BASE_URL,\n type FireworksAIProviderConfig,\n} from \"./fireworksai-provider.ts\";\n","import { OpenAI } from \"openai\";\n\nimport type {\n AssistantContent,\n AssistantMessage,\n CancellationToken,\n CompletionResponseData,\n Content,\n Message,\n ModelProvider,\n ModelRequest,\n ParameterType,\n ProviderContextTransformer,\n StreamReceiver,\n ToolContent,\n ToolDefinition,\n Usage,\n} from \"@simulacra-ai/core\";\n\ntype Prettify<T> = { [K in keyof T]: T[K] } & {};\n\nexport const FIREWORKS_BASE_URL = \"https://api.fireworks.ai/inference/v1\";\n\n/**\n * Configuration options for the FireworksAI provider.\n */\nexport type FireworksAIProviderConfig = Record<string, unknown> & {\n /** The model identifier to use (e.g., \"accounts/fireworks/models/llama-v3p1-8b-instruct\"). */\n model: string;\n /** The maximum number of tokens to generate in the response. */\n max_tokens?: number;\n}\n\n/**\n * Creates an OpenAI SDK client configured to target the FireworksAI API.\n *\n * @param apiKey - Your Fireworks AI API key.\n * @returns A configured OpenAI client instance pointed at the Fireworks endpoint.\n */\nexport function createFireworksAIClient(apiKey: string): OpenAI {\n return new OpenAI({\n apiKey,\n baseURL: FIREWORKS_BASE_URL,\n });\n}\n\n/**\n * Model provider implementation for FireworksAI's OpenAI-compatible chat completion API.\n *\n * FireworksAI exposes an OpenAI-compatible endpoint, so this provider uses the\n * OpenAI SDK under the hood (pointed at the Fireworks base URL via `createFireworksAIClient`).\n * It handles message formatting, content streaming, and usage tracking according to\n * the ModelProvider interface. System prompts always use the standard `system` role.\n */\nexport class FireworksAIProvider implements ModelProvider {\n readonly #sdk: OpenAI;\n readonly #config: FireworksAIProviderConfig;\n readonly context_transformers: ProviderContextTransformer[];\n\n /**\n * Creates a new FireworksAI provider instance.\n *\n * @param sdk - An OpenAI SDK client configured for the Fireworks endpoint (see `createFireworksAIClient`).\n * @param config - Configuration options for the provider.\n * @param context_transformers - Provider-level context transformers.\n */\n constructor(\n sdk: OpenAI,\n config: FireworksAIProviderConfig,\n context_transformers: ProviderContextTransformer[] = [],\n ) {\n this.#sdk = sdk;\n this.#config = config;\n this.context_transformers = context_transformers;\n }\n\n /**\n * Executes a model request and streams the response through the provided receiver.\n *\n * @param request - The request containing messages, tools, and system prompt.\n * @param receiver - The receiver that handles streaming events.\n * @param cancellation - Token to signal cancellation of the request.\n * @returns A promise that resolves when the request completes.\n */\n async execute_request(\n request: ModelRequest,\n receiver: StreamReceiver,\n cancellation: CancellationToken,\n ): Promise<void> {\n const { model, max_tokens, ...api_extras } = this.#config;\n const params: OpenAI.ChatCompletionCreateParamsStreaming = {\n ...api_extras,\n model,\n stream: true,\n max_tokens,\n ...(request.tools.length > 0\n ? {\n tool_choice: \"auto\",\n tools: request.tools.map((t) => to_fireworksai_tool(t)),\n }\n : {}),\n messages: [\n ...get_system_message(request.system),\n ...request.messages.flatMap((m) => to_fireworksai_messages(m)),\n ],\n stream_options: {\n include_usage: true,\n },\n };\n\n receiver.before_request({ params });\n receiver.request_raw(params);\n\n const stream = await this.#sdk.chat.completions.create(params);\n\n // Intentionally not awaited. Streaming is event-driven through the receiver.\n this.#stream_response(stream, receiver, cancellation);\n }\n\n /**\n * Creates a clone of this provider with the same configuration.\n *\n * @returns A new provider instance with identical configuration.\n */\n clone(): ModelProvider {\n return new FireworksAIProvider(this.#sdk, this.#config, this.context_transformers);\n }\n\n async #stream_response(\n stream: AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>,\n receiver: StreamReceiver,\n cancellation: CancellationToken,\n ) {\n try {\n let response: OpenAI.Chat.Completions.ChatCompletionChunk | undefined;\n for await (const response_chunk of stream) {\n if (cancellation.is_cancellation_requested) {\n receiver.cancel();\n return;\n }\n receiver.stream_raw(response_chunk);\n\n const { choices: choices_chunk, ...rest } = response_chunk;\n response = {\n ...response,\n ...rest,\n choices: response?.choices ?? [],\n };\n\n for (const choice_chunk of choices_chunk) {\n if (!response.choices[choice_chunk.index]) {\n response.choices[choice_chunk.index] = choice_chunk;\n const message = from_fireworksai_completion(response_chunk, choice_chunk);\n for (const content of message.content) {\n receiver.start_content({ content, message, usage: {} });\n }\n receiver.start_message({ message, usage: {} });\n continue;\n }\n\n const { delta: delta_chunk, ...rest } = choice_chunk;\n const choice = (response.choices[choice_chunk.index] = {\n ...response.choices[choice_chunk.index],\n ...rest,\n delta: {\n ...response.choices[choice_chunk.index]?.delta,\n },\n });\n\n if (delta_chunk.role) {\n choice.delta.role = delta_chunk.role;\n }\n if (delta_chunk.refusal) {\n if (!choice.delta.refusal) {\n choice.delta.refusal = \"\";\n }\n choice.delta.refusal += delta_chunk.refusal;\n }\n if (delta_chunk.content) {\n if (!choice.delta.content) {\n choice.delta.content = delta_chunk.content;\n receiver.start_content({\n content: from_fireworksai_content(choice.delta) as AssistantContent,\n message: from_fireworksai_completion(response_chunk, choice),\n usage: response?.usage ? from_fireworksai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_fireworksai_completion(response_chunk, choice),\n usage: response?.usage ? from_fireworksai_usage(response.usage) : {},\n });\n } else {\n choice.delta.content += delta_chunk.content;\n receiver.update_content({\n content: from_fireworksai_content(choice.delta) as AssistantContent,\n message: from_fireworksai_completion(response_chunk, choice),\n usage: response?.usage ? from_fireworksai_usage(response.usage) : {},\n });\n }\n }\n if (delta_chunk.tool_calls) {\n if (!choice.delta.tool_calls) {\n choice.delta.tool_calls = [];\n }\n for (const tool_call_chunk of delta_chunk.tool_calls) {\n if (!choice.delta.tool_calls[tool_call_chunk.index]) {\n choice.delta.tool_calls[tool_call_chunk.index] = tool_call_chunk;\n receiver.start_content({\n content: from_fireworksai_tool_call(tool_call_chunk),\n message: from_fireworksai_completion(response_chunk, choice),\n usage: response?.usage ? from_fireworksai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_fireworksai_completion(response_chunk, choice),\n usage: response?.usage ? from_fireworksai_usage(response.usage) : {},\n });\n } else {\n const tool_call = choice.delta.tool_calls[tool_call_chunk.index];\n\n if (tool_call_chunk.id) {\n tool_call.id = tool_call_chunk.id;\n }\n if (tool_call_chunk.type) {\n tool_call.type = tool_call_chunk.type;\n }\n if (tool_call_chunk.function) {\n if (!tool_call.function) {\n tool_call.function = tool_call_chunk.function;\n } else {\n if (tool_call_chunk.function.name) {\n tool_call.function.name = tool_call_chunk.function.name;\n }\n if (tool_call_chunk.function.arguments) {\n if (!tool_call.function.arguments) {\n tool_call.function.arguments = \"\";\n }\n tool_call.function.arguments += tool_call_chunk.function.arguments;\n }\n }\n }\n receiver.update_content({\n content: from_fireworksai_tool_call(tool_call),\n message: from_fireworksai_completion(response_chunk, choice),\n usage: response?.usage ? from_fireworksai_usage(response.usage) : {},\n });\n receiver.update_message({\n message: from_fireworksai_completion(response_chunk, choice),\n usage: response?.usage ? from_fireworksai_usage(response.usage) : {},\n });\n }\n }\n }\n }\n }\n if (!response || !response.choices?.[0]) {\n throw new Error(\"no data\");\n }\n receiver.response_raw({ ...response });\n\n const message = from_fireworksai_completion(response, response.choices[0]);\n const usage = response?.usage ? from_fireworksai_usage(response.usage) : {};\n for (const content of message.content) {\n receiver.complete_content({ content, message, usage });\n }\n receiver.complete_message({ message, usage, ...map_stop_reason(response) });\n } catch (error) {\n receiver.error(error);\n }\n }\n}\n\nfunction get_system_message(system?: string): OpenAI.ChatCompletionMessageParam[] {\n if (!system) {\n return [];\n }\n return [\n {\n role: \"system\",\n content: system,\n } as OpenAI.ChatCompletionSystemMessageParam,\n ];\n}\n\nfunction to_fireworksai_tool(tool: ToolDefinition): OpenAI.Chat.ChatCompletionTool {\n function map_parameter_type(\n parameter: Prettify<ParameterType & { description?: string }>,\n ): OpenAI.FunctionParameters {\n switch (parameter.type) {\n case \"object\":\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description: parameter.description,\n properties: Object.fromEntries(\n Object.entries(parameter.properties).map(([k, v]) => [k, map_parameter_type(v)]),\n ),\n additionalProperties: false,\n required: Object.entries(parameter.properties).map(([k]) => k),\n };\n case \"array\":\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description: parameter.description,\n items: map_parameter_type(parameter.items),\n };\n default:\n return {\n type: parameter.required ? parameter.type : [parameter.type, \"null\"],\n description:\n parameter.default !== undefined\n ? parameter.description\n ? `${parameter.description} (default: ${parameter.default})`\n : `default: ${parameter.default}`\n : parameter.description,\n enum: \"enum\" in parameter ? parameter.enum : undefined,\n };\n }\n }\n return {\n type: \"function\",\n function: {\n name: tool.name,\n description: tool.description,\n parameters: map_parameter_type({\n type: \"object\",\n required: true,\n properties: Object.fromEntries(\n tool.parameters.map(({ name, ...parameter }) => [name, parameter]),\n ),\n }),\n },\n };\n}\n\nfunction from_fireworksai_completion(\n completion: OpenAI.Chat.Completions.ChatCompletionChunk,\n choice: OpenAI.Chat.Completions.ChatCompletionChunk.Choice,\n) {\n let contents: Content[] = [];\n for (const k in choice.delta) {\n const key = k as keyof typeof choice.delta;\n if (key === \"role\") {\n continue;\n }\n if (key === \"content\" && choice.delta.content) {\n contents = [...contents, from_fireworksai_content(choice.delta)];\n } else if (key === \"refusal\" && choice.delta.refusal) {\n contents = [...contents, from_fireworksai_refusal(choice.delta)];\n } else if (key === \"tool_calls\" && choice.delta.tool_calls) {\n contents = [\n ...contents,\n ...choice.delta.tool_calls.map((t) => from_fireworksai_tool_call(t)),\n ];\n } else if (choice.delta[key] !== undefined && choice.delta[key] !== null) {\n const { [key]: data } = choice.delta;\n contents = [\n ...contents,\n {\n type: \"raw\",\n model_kind: \"fireworksai\",\n data: JSON.stringify({ [key]: data }),\n },\n ];\n }\n }\n return {\n id: completion.id,\n timestamp: completion.created,\n role: map_role(choice),\n content: contents,\n } as AssistantMessage;\n}\n\nfunction from_fireworksai_refusal(\n content: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta,\n) {\n const { refusal, tool_calls: _, function_call: __, content: ___, role: ____, ...rest } = content;\n return {\n type: \"text\",\n text: refusal,\n extended: {\n ...rest,\n fireworksai_refusal: true,\n },\n } as Content;\n}\n\nfunction from_fireworksai_content(\n content: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta,\n) {\n const {\n content: c,\n tool_calls: _,\n function_call: __,\n refusal: ___,\n role: ____,\n ...rest\n } = content;\n return {\n type: \"text\",\n text: c,\n extended: rest,\n } as Content;\n}\n\nfunction from_fireworksai_tool_call(\n tool_call: OpenAI.Chat.Completions.ChatCompletionChunk.Choice.Delta.ToolCall,\n) {\n const { id: tool_request_id, function: fn, type: _, index: __, ...extended } = tool_call;\n let params: unknown;\n try {\n params = JSON.parse(fn?.arguments ?? \"{}\");\n } catch {\n params = fn?.arguments;\n }\n return {\n tool_request_id,\n type: \"tool\",\n tool: fn?.name,\n params,\n extended,\n } as ToolContent;\n}\n\nfunction to_fireworksai_messages(message: Message) {\n if (message.role === \"assistant\") {\n return [to_fireworksai_assistant_message(message)];\n }\n // Partition content so tool_result blocks come before non-tool_result blocks.\n // FireworksAI requires all tool-role messages immediately after the assistant message\n // containing the corresponding tool_calls; interleaving user messages between\n // tool messages causes a validation error.\n const tool_result_content = message.content.filter((c) => c.type === \"tool_result\");\n const other_content = message.content.filter((c) => c.type !== \"tool_result\");\n const ordered_content = [...tool_result_content, ...other_content];\n\n const results: OpenAI.ChatCompletionMessageParam[] = [];\n let result: OpenAI.ChatCompletionMessageParam | undefined;\n for (const content of ordered_content) {\n if (content.type === \"text\") {\n if (!result) {\n result = {\n role: \"user\",\n content: content.text,\n };\n } else if (result.role === \"tool\") {\n results.push(result);\n result = {\n role: \"user\",\n content: content.text,\n };\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = [\n {\n type: \"text\",\n text: content.text,\n },\n ];\n } else {\n result.content.push({\n type: \"text\",\n text: content.text,\n });\n }\n }\n } else if (content.type === \"tool_result\") {\n if (!result) {\n result = {\n role: \"tool\",\n tool_call_id: content.tool_request_id,\n content: JSON.stringify(content.result),\n };\n } else if (result.role !== \"tool\" || result.tool_call_id !== content.tool_request_id) {\n results.push(result);\n result = {\n role: \"tool\",\n tool_call_id: content.tool_request_id,\n content: JSON.stringify(content.result),\n };\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n result.content.push({\n type: \"text\",\n text: JSON.stringify(content.result),\n });\n }\n } else if (content.type === \"raw\") {\n result = {\n ...(result ?? {}),\n ...JSON.parse(content.data),\n };\n }\n }\n if (result) {\n results.push(result);\n }\n return results;\n}\n\nfunction to_fireworksai_assistant_message(message: AssistantMessage) {\n let result: OpenAI.ChatCompletionAssistantMessageParam = {\n role: \"assistant\",\n };\n for (const content of message.content) {\n switch (content.type) {\n case \"text\":\n if (\n content.extended &&\n (content.extended.fireworksai_refusal === true ||\n content.extended.openai_refusal === true)\n ) {\n result.refusal = content.text;\n } else {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.text;\n } else {\n result.content.push({\n type: \"text\",\n text: content.text,\n });\n }\n }\n break;\n case \"tool\":\n if (!result.tool_calls) {\n result.tool_calls = [];\n }\n result.tool_calls.push({\n id: content.tool_request_id,\n type: \"function\",\n function: {\n name: content.tool,\n arguments: JSON.stringify(content.params),\n },\n });\n break;\n case \"raw\":\n // Handle raw content from fireworksai or openai providers (format-compatible)\n if (content.model_kind !== \"fireworksai\" && content.model_kind !== \"openai\") {\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.data;\n } else {\n result.content.push({\n type: \"text\",\n text: content.data,\n });\n }\n break;\n }\n result = {\n ...result,\n ...JSON.parse(content.data),\n };\n break;\n case \"thinking\":\n if (typeof result.content === \"string\") {\n result.content = [\n {\n type: \"text\",\n text: result.content,\n },\n ];\n }\n if (!result.content) {\n result.content = content.thought;\n } else {\n result.content.push({\n type: \"text\",\n text: content.thought,\n });\n }\n break;\n default:\n throw new Error(\"unexpected content type\");\n }\n }\n // Some OpenAI-compatible backends (e.g. Mixtral via Fireworks Jinja templates) require\n // the `content` key to be present on assistant messages even when it is null. The OpenAI\n // API accepts a missing key, but other backends reject it with a 400 template error.\n if (result.tool_calls && result.content === undefined) {\n result.content = null;\n }\n return result;\n}\n\nfunction from_fireworksai_usage(usage: OpenAI.CompletionUsage | null | undefined) {\n return {\n input_tokens: usage?.prompt_tokens,\n output_tokens: usage?.completion_tokens,\n } as Usage;\n}\n\nfunction map_stop_reason(\n completion: OpenAI.ChatCompletionChunk,\n): Pick<CompletionResponseData, \"stop_reason\" | \"stop_details\"> {\n for (const choice of completion.choices) {\n switch (choice.finish_reason) {\n case \"content_filter\":\n return {\n stop_reason: \"error\",\n stop_details: choice.finish_reason,\n };\n case \"function_call\":\n return {\n stop_reason: \"tool_use\",\n };\n case \"length\":\n return {\n stop_reason: \"max_tokens\",\n };\n case \"stop\":\n return {\n stop_reason: \"end_turn\",\n };\n case \"tool_calls\":\n return {\n stop_reason: \"tool_use\",\n };\n default:\n return {\n stop_reason: \"other\",\n stop_details: `${choice.finish_reason}`,\n };\n }\n }\n return {\n stop_reason: \"other\",\n };\n}\n\nfunction map_role(choice: OpenAI.Chat.Completions.ChatCompletionChunk.Choice) {\n switch (choice.delta.role) {\n case \"user\":\n case \"developer\":\n case \"system\":\n return \"user\";\n case \"assistant\":\n case \"tool\":\n return \"assistant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