@ai-sdk/fireworks 3.0.62 → 3.0.63

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.js CHANGED
@@ -1,512 +1,373 @@
1
- // src/fireworks-image-model.ts
2
- import {
3
- combineHeaders,
4
- convertImageModelFileToDataUri,
5
- createBinaryResponseHandler,
6
- createJsonResponseHandler,
7
- createStatusCodeErrorResponseHandler,
8
- delay,
9
- getFromApi,
10
- parseProviderOptions,
11
- postJsonToApi,
12
- serializeModelOptions,
13
- WORKFLOW_SERIALIZE,
14
- WORKFLOW_DESERIALIZE
15
- } from "@ai-sdk/provider-utils";
16
-
17
- // src/fireworks-image-api.ts
18
- import { lazySchema, zodSchema } from "@ai-sdk/provider-utils";
1
+ import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertImageModelFileToDataUri, createBinaryResponseHandler, createJsonResponseHandler, createStatusCodeErrorResponseHandler, delay, getFromApi, lazySchema, loadApiKey, parseProviderOptions, postJsonToApi, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
19
2
  import { z } from "zod/v4";
20
- var asyncSubmitResponseSchema = lazySchema(
21
- () => zodSchema(
22
- z.object({
23
- request_id: z.string()
24
- })
25
- )
26
- );
27
- var asyncPollResponseSchema = lazySchema(
28
- () => zodSchema(
29
- z.object({
30
- id: z.string(),
31
- status: z.string(),
32
- result: z.object({
33
- sample: z.string().optional()
34
- }).nullable()
35
- })
36
- )
37
- );
38
-
39
- // src/fireworks-image-model-options.ts
40
- import {
41
- lazySchema as lazySchema2,
42
- zodSchema as zodSchema2
43
- } from "@ai-sdk/provider-utils";
44
- import { z as z2 } from "zod/v4";
45
- var fireworksImageModelOptionsSchema = lazySchema2(
46
- () => zodSchema2(
47
- z2.looseObject({
48
- /**
49
- * Classifier-free guidance scale for the image diffusion process.
50
- */
51
- guidance_scale: z2.number().optional(),
52
- /**
53
- * Number of denoising steps for the image generation process.
54
- */
55
- num_inference_steps: z2.number().int().optional(),
56
- /**
57
- * Desired output image format.
58
- */
59
- output_format: z2.enum(["jpeg", "png"]).optional(),
60
- /**
61
- * URL to receive webhook notifications for async Kontext requests.
62
- */
63
- webhook_url: z2.string().optional(),
64
- /**
65
- * Secret for webhook signature verification.
66
- */
67
- webhook_secret: z2.string().optional(),
68
- /**
69
- * Whether Fireworks should automatically modify the prompt for more creative generation.
70
- */
71
- prompt_upsampling: z2.boolean().optional(),
72
- /**
73
- * Moderation tolerance level for inputs and outputs.
74
- */
75
- safety_tolerance: z2.number().int().min(0).max(6).optional(),
76
- /**
77
- * Guidance scale for legacy image_generation models.
78
- */
79
- cfg_scale: z2.number().optional(),
80
- /**
81
- * Number of generation steps for legacy image_generation models.
82
- */
83
- steps: z2.number().int().optional()
84
- })
85
- )
86
- );
87
-
88
- // src/fireworks-image-model.ts
89
- var DEFAULT_POLL_INTERVAL_MILLIS = 500;
90
- var DEFAULT_POLL_TIMEOUT_MILLIS = 12e4;
91
- var modelToBackendConfig = {
92
- "accounts/fireworks/models/flux-1-dev-fp8": {
93
- urlFormat: "workflows"
94
- },
95
- "accounts/fireworks/models/flux-1-schnell-fp8": {
96
- urlFormat: "workflows"
97
- },
98
- "accounts/fireworks/models/flux-kontext-pro": {
99
- urlFormat: "workflows_async",
100
- supportsEditing: true
101
- },
102
- "accounts/fireworks/models/flux-kontext-max": {
103
- urlFormat: "workflows_async",
104
- supportsEditing: true
105
- },
106
- "accounts/fireworks/models/playground-v2-5-1024px-aesthetic": {
107
- urlFormat: "image_generation",
108
- supportsSize: true
109
- },
110
- "accounts/fireworks/models/japanese-stable-diffusion-xl": {
111
- urlFormat: "image_generation",
112
- supportsSize: true
113
- },
114
- "accounts/fireworks/models/playground-v2-1024px-aesthetic": {
115
- urlFormat: "image_generation",
116
- supportsSize: true
117
- },
118
- "accounts/fireworks/models/stable-diffusion-xl-1024-v1-0": {
119
- urlFormat: "image_generation",
120
- supportsSize: true
121
- },
122
- "accounts/fireworks/models/SSD-1B": {
123
- urlFormat: "image_generation",
124
- supportsSize: true
125
- }
3
+ import { OpenAICompatibleChatLanguageModel, OpenAICompatibleCompletionLanguageModel, OpenAICompatibleEmbeddingModel } from "@ai-sdk/openai-compatible";
4
+ //#region src/fireworks-image-api.ts
5
+ const asyncSubmitResponseSchema = lazySchema(() => zodSchema(z.object({ request_id: z.string() })));
6
+ const asyncPollResponseSchema = lazySchema(() => zodSchema(z.object({
7
+ id: z.string(),
8
+ status: z.string(),
9
+ result: z.object({ sample: z.string().optional() }).nullable()
10
+ })));
11
+ //#endregion
12
+ //#region src/fireworks-image-model-options.ts
13
+ const fireworksImageModelOptionsSchema = lazySchema(() => zodSchema(z.looseObject({
14
+ /**
15
+ * Classifier-free guidance scale for the image diffusion process.
16
+ */
17
+ guidance_scale: z.number().optional(),
18
+ /**
19
+ * Number of denoising steps for the image generation process.
20
+ */
21
+ num_inference_steps: z.number().int().optional(),
22
+ /**
23
+ * Desired output image format.
24
+ */
25
+ output_format: z.enum(["jpeg", "png"]).optional(),
26
+ /**
27
+ * URL to receive webhook notifications for async Kontext requests.
28
+ */
29
+ webhook_url: z.string().optional(),
30
+ /**
31
+ * Secret for webhook signature verification.
32
+ */
33
+ webhook_secret: z.string().optional(),
34
+ /**
35
+ * Whether Fireworks should automatically modify the prompt for more creative generation.
36
+ */
37
+ prompt_upsampling: z.boolean().optional(),
38
+ /**
39
+ * Moderation tolerance level for inputs and outputs.
40
+ */
41
+ safety_tolerance: z.number().int().min(0).max(6).optional(),
42
+ /**
43
+ * Guidance scale for legacy image_generation models.
44
+ */
45
+ cfg_scale: z.number().optional(),
46
+ /**
47
+ * Number of generation steps for legacy image_generation models.
48
+ */
49
+ steps: z.number().int().optional()
50
+ })));
51
+ //#endregion
52
+ //#region src/fireworks-image-model.ts
53
+ const DEFAULT_POLL_INTERVAL_MILLIS = 500;
54
+ const DEFAULT_POLL_TIMEOUT_MILLIS = 12e4;
55
+ const modelToBackendConfig = {
56
+ "accounts/fireworks/models/flux-1-dev-fp8": { urlFormat: "workflows" },
57
+ "accounts/fireworks/models/flux-1-schnell-fp8": { urlFormat: "workflows" },
58
+ "accounts/fireworks/models/flux-kontext-pro": {
59
+ urlFormat: "workflows_async",
60
+ supportsEditing: true
61
+ },
62
+ "accounts/fireworks/models/flux-kontext-max": {
63
+ urlFormat: "workflows_async",
64
+ supportsEditing: true
65
+ },
66
+ "accounts/fireworks/models/playground-v2-5-1024px-aesthetic": {
67
+ urlFormat: "image_generation",
68
+ supportsSize: true
69
+ },
70
+ "accounts/fireworks/models/japanese-stable-diffusion-xl": {
71
+ urlFormat: "image_generation",
72
+ supportsSize: true
73
+ },
74
+ "accounts/fireworks/models/playground-v2-1024px-aesthetic": {
75
+ urlFormat: "image_generation",
76
+ supportsSize: true
77
+ },
78
+ "accounts/fireworks/models/stable-diffusion-xl-1024-v1-0": {
79
+ urlFormat: "image_generation",
80
+ supportsSize: true
81
+ },
82
+ "accounts/fireworks/models/SSD-1B": {
83
+ urlFormat: "image_generation",
84
+ supportsSize: true
85
+ }
126
86
  };
127
87
  function getUrlForModel(baseUrl, modelId) {
128
- const config = modelToBackendConfig[modelId];
129
- switch (config?.urlFormat) {
130
- case "image_generation":
131
- return `${baseUrl}/image_generation/${modelId}`;
132
- case "workflows_async":
133
- return `${baseUrl}/workflows/${modelId}`;
134
- case "workflows":
135
- default:
136
- return `${baseUrl}/workflows/${modelId}/text_to_image`;
137
- }
88
+ switch (modelToBackendConfig[modelId]?.urlFormat) {
89
+ case "image_generation": return `${baseUrl}/image_generation/${modelId}`;
90
+ case "workflows_async": return `${baseUrl}/workflows/${modelId}`;
91
+ default: return `${baseUrl}/workflows/${modelId}/text_to_image`;
92
+ }
138
93
  }
139
94
  function getPollUrlForModel(baseUrl, modelId) {
140
- return `${baseUrl}/workflows/${modelId}/get_result`;
95
+ return `${baseUrl}/workflows/${modelId}/get_result`;
141
96
  }
142
- var FireworksImageModel = class _FireworksImageModel {
143
- constructor(modelId, config) {
144
- this.modelId = modelId;
145
- this.config = config;
146
- this.specificationVersion = "v4";
147
- this.maxImagesPerCall = 1;
148
- }
149
- get supportsFileInputs() {
150
- if (this.modelId === "accounts/fireworks/models/flux-kontext-pro" || this.modelId === "accounts/fireworks/models/flux-kontext-max") {
151
- return true;
152
- }
153
- return [
154
- "accounts/fireworks/models/flux-1-dev-fp8",
155
- "accounts/fireworks/models/flux-1-schnell-fp8",
156
- "accounts/fireworks/models/playground-v2-5-1024px-aesthetic",
157
- "accounts/fireworks/models/japanese-stable-diffusion-xl",
158
- "accounts/fireworks/models/playground-v2-1024px-aesthetic",
159
- "accounts/fireworks/models/SSD-1B",
160
- "accounts/fireworks/models/stable-diffusion-xl-1024-v1-0"
161
- ].includes(this.modelId) ? false : void 0;
162
- }
163
- get supportsMaskInputs() {
164
- return this.supportsFileInputs == null ? void 0 : false;
165
- }
166
- get provider() {
167
- return this.config.provider;
168
- }
169
- static [WORKFLOW_SERIALIZE](model) {
170
- return serializeModelOptions({
171
- modelId: model.modelId,
172
- config: model.config
173
- });
174
- }
175
- static [WORKFLOW_DESERIALIZE](options) {
176
- return new _FireworksImageModel(options.modelId, options.config);
177
- }
178
- async doGenerate({
179
- prompt,
180
- n,
181
- size,
182
- aspectRatio,
183
- seed,
184
- providerOptions,
185
- headers,
186
- abortSignal,
187
- files,
188
- mask
189
- }) {
190
- const warnings = [];
191
- const backendConfig = modelToBackendConfig[this.modelId];
192
- if (!backendConfig?.supportsSize && size != null) {
193
- warnings.push({
194
- type: "unsupported",
195
- feature: "size",
196
- details: "This model does not support the `size` option. Use `aspectRatio` instead."
197
- });
198
- }
199
- if (backendConfig?.supportsSize && aspectRatio != null) {
200
- warnings.push({
201
- type: "unsupported",
202
- feature: "aspectRatio",
203
- details: "This model does not support the `aspectRatio` option."
204
- });
205
- }
206
- const hasInputImage = files != null && files.length > 0;
207
- let inputImage;
208
- if (hasInputImage) {
209
- inputImage = convertImageModelFileToDataUri(files[0]);
210
- if (files.length > 1) {
211
- warnings.push({
212
- type: "other",
213
- message: "Fireworks only supports a single input image. Additional images are ignored."
214
- });
215
- }
216
- }
217
- if (mask != null) {
218
- warnings.push({
219
- type: "unsupported",
220
- feature: "mask",
221
- details: "Fireworks Kontext models do not support explicit masks. Use the prompt to describe the areas to edit."
222
- });
223
- }
224
- const splitSize = size?.split("x");
225
- const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
226
- const combinedHeaders = combineHeaders(this.config.headers?.(), headers);
227
- const fireworksOptions = await parseProviderOptions({
228
- provider: "fireworks",
229
- providerOptions,
230
- schema: fireworksImageModelOptionsSchema
231
- }) ?? {};
232
- const body = {
233
- prompt,
234
- aspect_ratio: aspectRatio,
235
- seed,
236
- samples: n,
237
- ...inputImage && { input_image: inputImage },
238
- ...splitSize && { width: splitSize[0], height: splitSize[1] },
239
- ...fireworksOptions
240
- };
241
- if (backendConfig?.urlFormat === "workflows_async") {
242
- return this.doGenerateAsync({
243
- body,
244
- headers: combinedHeaders,
245
- abortSignal,
246
- warnings,
247
- currentDate
248
- });
249
- }
250
- const { value: response, responseHeaders } = await postJsonToApi({
251
- url: getUrlForModel(this.config.baseURL, this.modelId),
252
- headers: combinedHeaders,
253
- body,
254
- failedResponseHandler: createStatusCodeErrorResponseHandler(),
255
- successfulResponseHandler: createBinaryResponseHandler(),
256
- abortSignal,
257
- fetch: this.config.fetch
258
- });
259
- return {
260
- images: [response],
261
- warnings,
262
- response: {
263
- timestamp: currentDate,
264
- modelId: this.modelId,
265
- headers: responseHeaders
266
- }
267
- };
268
- }
269
- /**
270
- * Handles async image generation for models like flux-kontext-* that return
271
- * a request_id and require polling for results.
272
- */
273
- async doGenerateAsync({
274
- body,
275
- headers,
276
- abortSignal,
277
- warnings,
278
- currentDate
279
- }) {
280
- const { value: submitResponse } = await postJsonToApi({
281
- url: getUrlForModel(this.config.baseURL, this.modelId),
282
- headers,
283
- body,
284
- failedResponseHandler: createStatusCodeErrorResponseHandler(),
285
- successfulResponseHandler: createJsonResponseHandler(
286
- asyncSubmitResponseSchema
287
- ),
288
- abortSignal,
289
- fetch: this.config.fetch
290
- });
291
- const requestId = submitResponse.request_id;
292
- const imageUrl = await this.pollForImageUrl({
293
- requestId,
294
- headers,
295
- abortSignal
296
- });
297
- const { value: imageBytes, responseHeaders } = await getFromApi({
298
- url: imageUrl,
299
- // imageUrl comes from the provider response body.
300
- validateUrl: true,
301
- credentialedOrigin: this.config.baseURL,
302
- trustedOrigin: this.config.baseURL,
303
- headers,
304
- abortSignal,
305
- failedResponseHandler: createStatusCodeErrorResponseHandler(),
306
- successfulResponseHandler: createBinaryResponseHandler(),
307
- fetch: this.config.fetch
308
- });
309
- return {
310
- images: [imageBytes],
311
- warnings,
312
- response: {
313
- timestamp: currentDate,
314
- modelId: this.modelId,
315
- headers: responseHeaders
316
- }
317
- };
318
- }
319
- /**
320
- * Polls the get_result endpoint until the image is ready.
321
- */
322
- async pollForImageUrl({
323
- requestId,
324
- headers,
325
- abortSignal
326
- }) {
327
- const pollIntervalMillis = this.config.pollIntervalMillis ?? DEFAULT_POLL_INTERVAL_MILLIS;
328
- const pollTimeoutMillis = this.config.pollTimeoutMillis ?? DEFAULT_POLL_TIMEOUT_MILLIS;
329
- const pollUrl = getPollUrlForModel(this.config.baseURL, this.modelId);
330
- const timeoutController = new AbortController();
331
- let didTimeout = false;
332
- const timeoutId = setTimeout(() => {
333
- didTimeout = true;
334
- timeoutController.abort();
335
- }, pollTimeoutMillis);
336
- const pollingAbortSignal = abortSignal == null ? timeoutController.signal : AbortSignal.any([abortSignal, timeoutController.signal]);
337
- try {
338
- while (true) {
339
- const { value: pollResponse } = await postJsonToApi({
340
- url: pollUrl,
341
- headers,
342
- body: { id: requestId },
343
- failedResponseHandler: createStatusCodeErrorResponseHandler(),
344
- successfulResponseHandler: createJsonResponseHandler(
345
- asyncPollResponseSchema
346
- ),
347
- abortSignal: pollingAbortSignal,
348
- fetch: this.config.fetch
349
- });
350
- const status = pollResponse.status;
351
- if (status === "Ready") {
352
- const imageUrl = pollResponse.result?.sample;
353
- if (typeof imageUrl === "string") {
354
- return imageUrl;
355
- }
356
- throw new Error(
357
- "Fireworks poll response is Ready but missing result.sample"
358
- );
359
- }
360
- if (status === "Error" || status === "Failed") {
361
- throw new Error(
362
- `Fireworks image generation failed with status: ${status}`
363
- );
364
- }
365
- await delay(pollIntervalMillis, {
366
- abortSignal: pollingAbortSignal
367
- });
368
- }
369
- } catch (error) {
370
- if (didTimeout) {
371
- throw new Error(
372
- `Fireworks image generation timed out after ${pollTimeoutMillis}ms`
373
- );
374
- }
375
- throw error;
376
- } finally {
377
- clearTimeout(timeoutId);
378
- }
379
- }
97
+ var FireworksImageModel = class FireworksImageModel {
98
+ get supportsFileInputs() {
99
+ if (this.modelId === "accounts/fireworks/models/flux-kontext-pro" || this.modelId === "accounts/fireworks/models/flux-kontext-max") return true;
100
+ return [
101
+ "accounts/fireworks/models/flux-1-dev-fp8",
102
+ "accounts/fireworks/models/flux-1-schnell-fp8",
103
+ "accounts/fireworks/models/playground-v2-5-1024px-aesthetic",
104
+ "accounts/fireworks/models/japanese-stable-diffusion-xl",
105
+ "accounts/fireworks/models/playground-v2-1024px-aesthetic",
106
+ "accounts/fireworks/models/SSD-1B",
107
+ "accounts/fireworks/models/stable-diffusion-xl-1024-v1-0"
108
+ ].includes(this.modelId) ? false : void 0;
109
+ }
110
+ get supportsMaskInputs() {
111
+ return this.supportsFileInputs == null ? void 0 : false;
112
+ }
113
+ get provider() {
114
+ return this.config.provider;
115
+ }
116
+ static [WORKFLOW_SERIALIZE](model) {
117
+ return serializeModelOptions({
118
+ modelId: model.modelId,
119
+ config: model.config
120
+ });
121
+ }
122
+ static [WORKFLOW_DESERIALIZE](options) {
123
+ return new FireworksImageModel(options.modelId, options.config);
124
+ }
125
+ constructor(modelId, config) {
126
+ this.modelId = modelId;
127
+ this.config = config;
128
+ this.specificationVersion = "v4";
129
+ this.maxImagesPerCall = 1;
130
+ }
131
+ async doGenerate({ prompt, n, size, aspectRatio, seed, providerOptions, headers, abortSignal, files, mask }) {
132
+ const warnings = [];
133
+ const backendConfig = modelToBackendConfig[this.modelId];
134
+ if (!backendConfig?.supportsSize && size != null) warnings.push({
135
+ type: "unsupported",
136
+ feature: "size",
137
+ details: "This model does not support the `size` option. Use `aspectRatio` instead."
138
+ });
139
+ if (backendConfig?.supportsSize && aspectRatio != null) warnings.push({
140
+ type: "unsupported",
141
+ feature: "aspectRatio",
142
+ details: "This model does not support the `aspectRatio` option."
143
+ });
144
+ const hasInputImage = files != null && files.length > 0;
145
+ let inputImage;
146
+ if (hasInputImage) {
147
+ inputImage = convertImageModelFileToDataUri(files[0]);
148
+ if (files.length > 1) warnings.push({
149
+ type: "other",
150
+ message: "Fireworks only supports a single input image. Additional images are ignored."
151
+ });
152
+ }
153
+ if (mask != null) warnings.push({
154
+ type: "unsupported",
155
+ feature: "mask",
156
+ details: "Fireworks Kontext models do not support explicit masks. Use the prompt to describe the areas to edit."
157
+ });
158
+ const splitSize = size?.split("x");
159
+ const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
160
+ const combinedHeaders = combineHeaders(this.config.headers?.(), headers);
161
+ const fireworksOptions = await parseProviderOptions({
162
+ provider: "fireworks",
163
+ providerOptions,
164
+ schema: fireworksImageModelOptionsSchema
165
+ }) ?? {};
166
+ const body = {
167
+ prompt,
168
+ aspect_ratio: aspectRatio,
169
+ seed,
170
+ samples: n,
171
+ ...inputImage && { input_image: inputImage },
172
+ ...splitSize && {
173
+ width: splitSize[0],
174
+ height: splitSize[1]
175
+ },
176
+ ...fireworksOptions
177
+ };
178
+ if (backendConfig?.urlFormat === "workflows_async") return this.doGenerateAsync({
179
+ body,
180
+ headers: combinedHeaders,
181
+ abortSignal,
182
+ warnings,
183
+ currentDate
184
+ });
185
+ const { value: response, responseHeaders } = await postJsonToApi({
186
+ url: getUrlForModel(this.config.baseURL, this.modelId),
187
+ headers: combinedHeaders,
188
+ body,
189
+ failedResponseHandler: createStatusCodeErrorResponseHandler(),
190
+ successfulResponseHandler: createBinaryResponseHandler(),
191
+ abortSignal,
192
+ fetch: this.config.fetch
193
+ });
194
+ return {
195
+ images: [response],
196
+ warnings,
197
+ response: {
198
+ timestamp: currentDate,
199
+ modelId: this.modelId,
200
+ headers: responseHeaders
201
+ }
202
+ };
203
+ }
204
+ /**
205
+ * Handles async image generation for models like flux-kontext-* that return
206
+ * a request_id and require polling for results.
207
+ */
208
+ async doGenerateAsync({ body, headers, abortSignal, warnings, currentDate }) {
209
+ const { value: submitResponse } = await postJsonToApi({
210
+ url: getUrlForModel(this.config.baseURL, this.modelId),
211
+ headers,
212
+ body,
213
+ failedResponseHandler: createStatusCodeErrorResponseHandler(),
214
+ successfulResponseHandler: createJsonResponseHandler(asyncSubmitResponseSchema),
215
+ abortSignal,
216
+ fetch: this.config.fetch
217
+ });
218
+ const requestId = submitResponse.request_id;
219
+ const imageUrl = await this.pollForImageUrl({
220
+ requestId,
221
+ headers,
222
+ abortSignal
223
+ });
224
+ const { value: imageBytes, responseHeaders } = await getFromApi({
225
+ url: imageUrl,
226
+ validateUrl: true,
227
+ credentialedOrigin: this.config.baseURL,
228
+ trustedOrigin: this.config.baseURL,
229
+ headers,
230
+ abortSignal,
231
+ failedResponseHandler: createStatusCodeErrorResponseHandler(),
232
+ successfulResponseHandler: createBinaryResponseHandler(),
233
+ fetch: this.config.fetch
234
+ });
235
+ return {
236
+ images: [imageBytes],
237
+ warnings,
238
+ response: {
239
+ timestamp: currentDate,
240
+ modelId: this.modelId,
241
+ headers: responseHeaders
242
+ }
243
+ };
244
+ }
245
+ /**
246
+ * Polls the get_result endpoint until the image is ready.
247
+ */
248
+ async pollForImageUrl({ requestId, headers, abortSignal }) {
249
+ const pollIntervalMillis = this.config.pollIntervalMillis ?? DEFAULT_POLL_INTERVAL_MILLIS;
250
+ const pollTimeoutMillis = this.config.pollTimeoutMillis ?? DEFAULT_POLL_TIMEOUT_MILLIS;
251
+ const pollUrl = getPollUrlForModel(this.config.baseURL, this.modelId);
252
+ const timeoutController = new AbortController();
253
+ let didTimeout = false;
254
+ const timeoutId = setTimeout(() => {
255
+ didTimeout = true;
256
+ timeoutController.abort();
257
+ }, pollTimeoutMillis);
258
+ const pollingAbortSignal = abortSignal == null ? timeoutController.signal : AbortSignal.any([abortSignal, timeoutController.signal]);
259
+ try {
260
+ while (true) {
261
+ const { value: pollResponse } = await postJsonToApi({
262
+ url: pollUrl,
263
+ headers,
264
+ body: { id: requestId },
265
+ failedResponseHandler: createStatusCodeErrorResponseHandler(),
266
+ successfulResponseHandler: createJsonResponseHandler(asyncPollResponseSchema),
267
+ abortSignal: pollingAbortSignal,
268
+ fetch: this.config.fetch
269
+ });
270
+ const status = pollResponse.status;
271
+ if (status === "Ready") {
272
+ const imageUrl = pollResponse.result?.sample;
273
+ if (typeof imageUrl === "string") return imageUrl;
274
+ throw new Error("Fireworks poll response is Ready but missing result.sample");
275
+ }
276
+ if (status === "Error" || status === "Failed") throw new Error(`Fireworks image generation failed with status: ${status}`);
277
+ await delay(pollIntervalMillis, { abortSignal: pollingAbortSignal });
278
+ }
279
+ } catch (error) {
280
+ if (didTimeout) throw new Error(`Fireworks image generation timed out after ${pollTimeoutMillis}ms`);
281
+ throw error;
282
+ } finally {
283
+ clearTimeout(timeoutId);
284
+ }
285
+ }
380
286
  };
381
-
382
- // src/fireworks-provider.ts
383
- import {
384
- OpenAICompatibleChatLanguageModel,
385
- OpenAICompatibleCompletionLanguageModel,
386
- OpenAICompatibleEmbeddingModel
387
- } from "@ai-sdk/openai-compatible";
388
- import {
389
- loadApiKey,
390
- withoutTrailingSlash,
391
- withUserAgentSuffix
392
- } from "@ai-sdk/provider-utils";
393
- import { z as z3 } from "zod/v4";
394
-
395
- // src/version.ts
396
- var VERSION = true ? "3.0.62" : "0.0.0-test";
397
-
398
- // src/fireworks-provider.ts
399
- var fireworksErrorSchema = z3.object({
400
- error: z3.union([
401
- z3.string(),
402
- z3.object({
403
- message: z3.string(),
404
- object: z3.string().nullish(),
405
- type: z3.string().nullish(),
406
- param: z3.any().nullish(),
407
- code: z3.union([z3.string(), z3.number()]).nullish()
408
- })
409
- ])
410
- });
411
- var fireworksErrorStructure = {
412
- errorSchema: fireworksErrorSchema,
413
- errorToMessage: (data) => typeof data.error === "string" ? data.error : data.error.message
287
+ //#endregion
288
+ //#region src/version.ts
289
+ const VERSION = "3.0.63";
290
+ //#endregion
291
+ //#region src/fireworks-provider.ts
292
+ const fireworksErrorStructure = {
293
+ errorSchema: z.object({ error: z.union([z.string(), z.object({
294
+ message: z.string(),
295
+ object: z.string().nullish(),
296
+ type: z.string().nullish(),
297
+ param: z.any().nullish(),
298
+ code: z.union([z.string(), z.number()]).nullish()
299
+ })]) }),
300
+ errorToMessage: (data) => typeof data.error === "string" ? data.error : data.error.message
414
301
  };
415
- var defaultBaseURL = "https://api.fireworks.ai/inference/v1";
302
+ const defaultBaseURL = "https://api.fireworks.ai/inference/v1";
416
303
  function createFireworks(options = {}) {
417
- const baseURL = withoutTrailingSlash(options.baseURL ?? defaultBaseURL);
418
- const getHeaders = () => withUserAgentSuffix(
419
- {
420
- Authorization: `Bearer ${loadApiKey({
421
- apiKey: options.apiKey,
422
- environmentVariableName: "FIREWORKS_API_KEY",
423
- description: "Fireworks API key"
424
- })}`,
425
- ...options.headers
426
- },
427
- `ai-sdk-fireworks/${VERSION}`
428
- );
429
- const getCommonModelConfig = (modelType) => ({
430
- provider: `fireworks.${modelType}`,
431
- url: ({ path }) => `${baseURL}${path}`,
432
- headers: getHeaders,
433
- fetch: options.fetch
434
- });
435
- const createChatModel = (modelId) => {
436
- return new OpenAICompatibleChatLanguageModel(modelId, {
437
- ...getCommonModelConfig("chat"),
438
- includeUsage: true,
439
- errorStructure: fireworksErrorStructure,
440
- supportsStructuredOutputs: true,
441
- transformRequestBody: (args) => {
442
- const thinking = args.thinking;
443
- const reasoningHistory = args.reasoningHistory;
444
- const promptCacheKey = args.promptCacheKey;
445
- const serviceTier = args.serviceTier;
446
- const {
447
- thinking: _,
448
- reasoningHistory: __,
449
- promptCacheKey: ___,
450
- serviceTier: ____,
451
- reasoning_effort,
452
- ...rest
453
- } = args;
454
- return {
455
- ...rest,
456
- ...reasoning_effort != null && {
457
- // Workaround since OpenAI spec allows for 5 reasoning levels, but Fireworks only supports 3 of them.
458
- reasoning_effort: reasoning_effort === "minimal" ? "low" : reasoning_effort === "xhigh" ? "high" : reasoning_effort
459
- },
460
- ...promptCacheKey !== void 0 && {
461
- prompt_cache_key: promptCacheKey
462
- },
463
- ...serviceTier !== void 0 && {
464
- service_tier: serviceTier
465
- },
466
- ...thinking && {
467
- thinking: {
468
- type: thinking.type,
469
- ...thinking.budgetTokens !== void 0 && {
470
- budget_tokens: thinking.budgetTokens
471
- }
472
- }
473
- },
474
- ...reasoningHistory && {
475
- reasoning_history: reasoningHistory
476
- }
477
- };
478
- }
479
- });
480
- };
481
- const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
482
- ...getCommonModelConfig("completion"),
483
- includeUsage: true,
484
- errorStructure: fireworksErrorStructure
485
- });
486
- const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
487
- ...getCommonModelConfig("embedding"),
488
- errorStructure: fireworksErrorStructure
489
- });
490
- const createImageModel = (modelId) => new FireworksImageModel(modelId, {
491
- ...getCommonModelConfig("image"),
492
- baseURL: baseURL ?? defaultBaseURL
493
- });
494
- const provider = (modelId) => createChatModel(modelId);
495
- provider.specificationVersion = "v4";
496
- provider.completionModel = createCompletionModel;
497
- provider.chatModel = createChatModel;
498
- provider.languageModel = createChatModel;
499
- provider.embeddingModel = createEmbeddingModel;
500
- provider.textEmbeddingModel = createEmbeddingModel;
501
- provider.image = createImageModel;
502
- provider.imageModel = createImageModel;
503
- return provider;
304
+ const baseURL = withoutTrailingSlash(options.baseURL ?? defaultBaseURL);
305
+ const getHeaders = () => withUserAgentSuffix({
306
+ Authorization: `Bearer ${loadApiKey({
307
+ apiKey: options.apiKey,
308
+ environmentVariableName: "FIREWORKS_API_KEY",
309
+ description: "Fireworks API key"
310
+ })}`,
311
+ ...options.headers
312
+ }, `ai-sdk-fireworks/${VERSION}`);
313
+ const getCommonModelConfig = (modelType) => ({
314
+ provider: `fireworks.${modelType}`,
315
+ url: ({ path }) => `${baseURL}${path}`,
316
+ headers: getHeaders,
317
+ fetch: options.fetch
318
+ });
319
+ const createChatModel = (modelId) => {
320
+ return new OpenAICompatibleChatLanguageModel(modelId, {
321
+ ...getCommonModelConfig("chat"),
322
+ includeUsage: true,
323
+ errorStructure: fireworksErrorStructure,
324
+ supportsStructuredOutputs: true,
325
+ transformRequestBody: (args) => {
326
+ const thinking = args.thinking;
327
+ const reasoningHistory = args.reasoningHistory;
328
+ const promptCacheKey = args.promptCacheKey;
329
+ const serviceTier = args.serviceTier;
330
+ const { thinking: _, reasoningHistory: __, promptCacheKey: ___, serviceTier: ____, reasoning_effort, ...rest } = args;
331
+ return {
332
+ ...rest,
333
+ ...reasoning_effort != null && { reasoning_effort: reasoning_effort === "minimal" ? "low" : reasoning_effort === "xhigh" ? "high" : reasoning_effort },
334
+ ...promptCacheKey !== void 0 && { prompt_cache_key: promptCacheKey },
335
+ ...serviceTier !== void 0 && { service_tier: serviceTier },
336
+ ...thinking && { thinking: {
337
+ type: thinking.type,
338
+ ...thinking.budgetTokens !== void 0 && { budget_tokens: thinking.budgetTokens }
339
+ } },
340
+ ...reasoningHistory && { reasoning_history: reasoningHistory }
341
+ };
342
+ }
343
+ });
344
+ };
345
+ const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
346
+ ...getCommonModelConfig("completion"),
347
+ includeUsage: true,
348
+ errorStructure: fireworksErrorStructure
349
+ });
350
+ const createEmbeddingModel = (modelId) => new OpenAICompatibleEmbeddingModel(modelId, {
351
+ ...getCommonModelConfig("embedding"),
352
+ errorStructure: fireworksErrorStructure
353
+ });
354
+ const createImageModel = (modelId) => new FireworksImageModel(modelId, {
355
+ ...getCommonModelConfig("image"),
356
+ baseURL: baseURL ?? defaultBaseURL
357
+ });
358
+ const provider = (modelId) => createChatModel(modelId);
359
+ provider.specificationVersion = "v4";
360
+ provider.completionModel = createCompletionModel;
361
+ provider.chatModel = createChatModel;
362
+ provider.languageModel = createChatModel;
363
+ provider.embeddingModel = createEmbeddingModel;
364
+ provider.textEmbeddingModel = createEmbeddingModel;
365
+ provider.image = createImageModel;
366
+ provider.imageModel = createImageModel;
367
+ return provider;
504
368
  }
505
- var fireworks = createFireworks();
506
- export {
507
- FireworksImageModel,
508
- VERSION,
509
- createFireworks,
510
- fireworks
511
- };
369
+ const fireworks = createFireworks();
370
+ //#endregion
371
+ export { FireworksImageModel, VERSION, createFireworks, fireworks };
372
+
512
373
  //# sourceMappingURL=index.js.map