@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/CHANGELOG.md +10 -0
- package/dist/index.d.ts +151 -145
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +362 -501
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
package/dist/index.js
CHANGED
|
@@ -1,512 +1,373 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
95
|
+
return `${baseUrl}/workflows/${modelId}/get_result`;
|
|
141
96
|
}
|
|
142
|
-
var FireworksImageModel = class
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
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
|
-
|
|
302
|
+
const defaultBaseURL = "https://api.fireworks.ai/inference/v1";
|
|
416
303
|
function createFireworks(options = {}) {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
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
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
createFireworks,
|
|
510
|
-
fireworks
|
|
511
|
-
};
|
|
369
|
+
const fireworks = createFireworks();
|
|
370
|
+
//#endregion
|
|
371
|
+
export { FireworksImageModel, VERSION, createFireworks, fireworks };
|
|
372
|
+
|
|
512
373
|
//# sourceMappingURL=index.js.map
|