@tanstack/ai-fal 1.1.1 → 1.2.0
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/esm/adapters/audio.js +2 -3
- package/dist/esm/adapters/audio.js.map +1 -1
- package/dist/esm/adapters/files.js +1 -2
- package/dist/esm/adapters/image.js +2 -3
- package/dist/esm/adapters/image.js.map +1 -1
- package/dist/esm/adapters/live.js +1 -2
- package/dist/esm/adapters/model3d.d.ts +98 -0
- package/dist/esm/adapters/model3d.js +170 -0
- package/dist/esm/adapters/model3d.js.map +1 -0
- package/dist/esm/adapters/motion.d.ts +58 -0
- package/dist/esm/adapters/motion.js +150 -0
- package/dist/esm/adapters/motion.js.map +1 -0
- package/dist/esm/adapters/speech.js +2 -3
- package/dist/esm/adapters/speech.js.map +1 -1
- package/dist/esm/adapters/transcription.js +2 -3
- package/dist/esm/adapters/transcription.js.map +1 -1
- package/dist/esm/adapters/video.js +6 -11
- package/dist/esm/adapters/video.js.map +1 -1
- package/dist/esm/byok.js +1 -2
- package/dist/esm/client/index.js +1 -2
- package/dist/esm/image/generated/image-field-overrides.d.ts +459 -294
- package/dist/esm/image/generated/image-field-overrides.js +312 -190
- package/dist/esm/image/generated/image-field-overrides.js.map +1 -1
- package/dist/esm/image/image-inputs.js +7 -4
- package/dist/esm/image/image-inputs.js.map +1 -1
- package/dist/esm/image/image-provider-options.js +1 -2
- package/dist/esm/image/legacy-image-field-overrides.d.ts +13 -0
- package/dist/esm/image/legacy-image-field-overrides.js +91 -0
- package/dist/esm/image/legacy-image-field-overrides.js.map +1 -0
- package/dist/esm/index.d.ts +4 -1
- package/dist/esm/index.js +4 -2
- package/dist/esm/model-meta.d.ts +49 -0
- package/dist/esm/model3d/generated/mesh-field-overrides.d.ts +20 -0
- package/dist/esm/model3d/generated/mesh-field-overrides.js +20 -0
- package/dist/esm/model3d/generated/mesh-field-overrides.js.map +1 -0
- package/dist/esm/model3d/model3d-inputs.d.ts +39 -0
- package/dist/esm/model3d/model3d-inputs.js +206 -0
- package/dist/esm/model3d/model3d-inputs.js.map +1 -0
- package/dist/esm/model3d/model3d-output.d.ts +25 -0
- package/dist/esm/model3d/model3d-output.js +181 -0
- package/dist/esm/model3d/model3d-output.js.map +1 -0
- package/dist/esm/utils/billing.js +1 -2
- package/dist/esm/utils/client.d.ts +6 -0
- package/dist/esm/utils/client.js +19 -3
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/configure.js +1 -2
- package/dist/esm/video/video-provider-options.js +20 -31
- package/dist/esm/video/video-provider-options.js.map +1 -1
- package/package.json +6 -6
- package/src/adapters/model3d.ts +219 -0
- package/src/adapters/motion.ts +237 -0
- package/src/adapters/video.ts +5 -8
- package/src/image/generated/image-field-overrides.ts +159 -110
- package/src/image/image-inputs.ts +9 -4
- package/src/image/legacy-image-field-overrides.ts +94 -0
- package/src/index.ts +19 -0
- package/src/model-meta.ts +106 -0
- package/src/model3d/generated/mesh-field-overrides.ts +46 -0
- package/src/model3d/model3d-inputs.ts +232 -0
- package/src/model3d/model3d-output.ts +216 -0
- package/src/utils/client.ts +25 -0
- package/src/video/video-provider-options.ts +9 -27
package/dist/esm/utils/client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createFalClient } from "../client/index.js";
|
|
2
2
|
import { createBillingFetch } from "./billing.js";
|
|
3
3
|
import { generateId, getApiKeyFromEnv } from "@tanstack/ai-utils";
|
|
4
|
-
|
|
4
|
+
// src/utils/client.ts
|
|
5
5
|
function getFalApiKeyFromEnv() {
|
|
6
6
|
return getApiKeyFromEnv("FAL_KEY");
|
|
7
7
|
}
|
|
@@ -19,6 +19,23 @@ function toFalClientOptions(config) {
|
|
|
19
19
|
function createAdapterFalClient(config) {
|
|
20
20
|
return createFalClient(toFalClientOptions(config));
|
|
21
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* fal can report a request COMPLETED and then answer the result fetch with a
|
|
24
|
+
* 422. Returns its `detail` text, or joins a detail list as `field: message`
|
|
25
|
+
* pairs. Returns undefined when the error has no detail.
|
|
26
|
+
*/
|
|
27
|
+
function falValidationMessage(error) {
|
|
28
|
+
if (typeof error !== "object" || error === null || !("body" in error)) return;
|
|
29
|
+
const body = error.body;
|
|
30
|
+
if (typeof body !== "object" || body === null || !("detail" in body)) return;
|
|
31
|
+
const detail = body.detail;
|
|
32
|
+
if (typeof detail === "string") return detail || void 0;
|
|
33
|
+
if (!Array.isArray(detail)) return void 0;
|
|
34
|
+
return detail.map((d) => {
|
|
35
|
+
const msg = d.msg ?? "invalid value";
|
|
36
|
+
return d.loc ? `${d.loc.join(".")}: ${msg}` : msg;
|
|
37
|
+
}).join("; ") || void 0;
|
|
38
|
+
}
|
|
22
39
|
function generateId$1(prefix) {
|
|
23
40
|
return generateId(prefix);
|
|
24
41
|
}
|
|
@@ -104,7 +121,6 @@ function arrayBufferToBase64(bytes) {
|
|
|
104
121
|
for (const byte of view) binary += String.fromCharCode(byte);
|
|
105
122
|
return btoa(binary);
|
|
106
123
|
}
|
|
107
|
-
|
|
108
|
-
export { arrayBufferToBase64, createAdapterFalClient, dataUrlToBlob, deriveAudioContentType, extractUrlExtension, generateId$1 as generateId, getFalApiKeyFromEnv, toFalClientOptions };
|
|
124
|
+
export { arrayBufferToBase64, createAdapterFalClient, dataUrlToBlob, deriveAudioContentType, extractUrlExtension, falValidationMessage, generateId$1 as generateId, getFalApiKeyFromEnv, toFalClientOptions };
|
|
109
125
|
|
|
110
126
|
//# sourceMappingURL=client.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","names":[],"sources":["../../../src/utils/client.ts"],"sourcesContent":["import { generateId as _generateId, getApiKeyFromEnv } from '@tanstack/ai-utils'\nimport { createFalClient } from '../client/index'\nimport { createBillingFetch } from './billing'\n\nexport interface FalClientConfig {\n apiKey: string\n proxyUrl?: string\n /**\n * Override the underlying fetch used for fal requests. The adapter wraps it to\n * read the `x-fal-billable-units` header (see ./billing.ts), so usage capture\n * still works. Defaults to the global `fetch`. Useful for proxying,\n * instrumentation, or pointing requests at a mock in tests.\n */\n fetch?: typeof fetch\n}\n\nexport function getFalApiKeyFromEnv(): string {\n return getApiKeyFromEnv('FAL_KEY')\n}\n\nexport function toFalClientOptions(config?: FalClientConfig) {\n const apiKey = config?.apiKey ?? getFalApiKeyFromEnv()\n return {\n credentials: apiKey,\n // Wrap the (optionally overridden) fetch to read fal's\n // `x-fal-billable-units` header off every response so adapters can surface\n // the billed quantity as `result.usage`. See ./billing.ts.\n fetch: createBillingFetch(config?.fetch),\n ...(config?.proxyUrl ? { proxyUrl: config.proxyUrl } : {}),\n }\n}\n\n/**\n * Create the fal client for one adapter. Each adapter has its own client, so\n * two adapters with different API keys do not change each other.\n */\nexport function createAdapterFalClient(config?: FalClientConfig) {\n return createFalClient(toFalClientOptions(config))\n}\n\nexport function generateId(prefix: string): string {\n return _generateId(prefix)\n}\n\n/**\n * Extract a safe file extension from a URL. Strips query strings, URL\n * fragments, and any trailing slashes, and only returns the extension when\n * it looks like a real one (2-5 alphanumeric chars). Returns undefined\n * otherwise so callers can fall back to a default.\n */\nexport function extractUrlExtension(url: string): string | undefined {\n // Parse via URL when possible so we only look at the pathname and never\n // mistake a TLD (e.g. the `.com` in `https://x.com/`) for a file extension.\n let pathname: string\n try {\n const parsed = new URL(url)\n pathname = parsed.pathname\n } catch {\n // Fall back to treating the input as a raw path when URL parsing fails\n // (e.g. the caller passed a bare path). Still strip ?query and #fragment.\n pathname = url.split('?')[0]?.split('#')[0] ?? url\n }\n // Drop trailing slashes so `/path/audio.mp3/` still yields `mp3`.\n const normalized = pathname.replace(/\\/+$/, '')\n // Require at least one `/` — otherwise we're looking at an empty pathname\n // (bare-host URLs like `https://x.com/` land here after stripping the\n // trailing slash).\n if (!normalized.includes('/')) return undefined\n const lastSegment = normalized.split('/').pop()\n if (!lastSegment) return undefined\n const extension = lastSegment.split('.').pop()\n if (!extension || extension === lastSegment) return undefined\n return /^[a-z0-9]{2,5}$/i.test(extension) ? extension : undefined\n}\n\n/**\n * Derive a reasonable audio content-type. Prefers the explicit MIME (stripped\n * of parameters), then an extension-based lookup for common audio formats,\n * otherwise falls back to audio/mpeg — fal URLs virtually always serve mp3.\n */\nexport function deriveAudioContentType(\n explicitContentType: string | undefined,\n url: string,\n): string {\n const stripped = explicitContentType?.split(';')[0]?.trim()\n if (stripped) return stripped\n\n const ext = extractUrlExtension(url)?.toLowerCase()\n switch (ext) {\n case 'mp3':\n return 'audio/mpeg'\n case 'wav':\n return 'audio/wav'\n case 'ogg':\n case 'oga':\n return 'audio/ogg'\n case 'flac':\n return 'audio/flac'\n case 'aac':\n return 'audio/aac'\n case 'm4a':\n case 'mp4':\n return 'audio/mp4'\n case 'webm':\n return 'audio/webm'\n case undefined:\n default:\n return 'audio/mpeg'\n }\n}\n\n/**\n * Decode a `data:` URL into a Blob so fal-client can auto-upload it via\n * `fal.storage.upload`. fal's inference API rejects data URLs with a 422\n * \"Unsupported data URL\", so we convert them before handing them off.\n *\n * Supports both base64 and URL-encoded data URLs. Returns `undefined` for\n * anything that isn't a data URL, so callers can fall through to other\n * handling (http URLs are passed to fal as-is).\n */\nexport function dataUrlToBlob(value: string): Blob | undefined {\n if (!value.startsWith('data:')) return undefined\n const commaIndex = value.indexOf(',')\n if (commaIndex === -1) return undefined\n\n const header = value.slice(5, commaIndex)\n const payload = value.slice(commaIndex + 1)\n const isBase64 = /;base64$/i.test(header)\n const mimeType = header.split(';')[0] || 'application/octet-stream'\n\n if (isBase64) {\n const binary = atob(payload)\n const bytes = new Uint8Array(binary.length)\n for (let i = 0; i < binary.length; i += 1) {\n bytes[i] = binary.charCodeAt(i)\n }\n return new Blob([bytes], { type: mimeType })\n }\n\n return new Blob([decodeURIComponent(payload)], { type: mimeType })\n}\n\n/**\n * Convert an ArrayBuffer to base64 in a cross-runtime way.\n *\n * The naive `btoa(String.fromCharCode(...bytes))` form blows up V8's argument\n * limit (~65k) on realistic audio payloads, so we either use `Buffer` (Node /\n * Bun) or walk the byte array in a single loop (browser).\n */\nexport function arrayBufferToBase64(bytes: ArrayBuffer): string {\n if (typeof Buffer !== 'undefined' && typeof Buffer.from === 'function') {\n return Buffer.from(bytes).toString('base64')\n }\n const view = new Uint8Array(bytes)\n let binary = ''\n for (const byte of view) {\n binary += String.fromCharCode(byte)\n }\n return btoa(binary)\n}\n"],"mappings":";;;;AAgBA,SAAgB,sBAA8B;CAC5C,OAAO,iBAAiB,SAAS;AACnC;AAEA,SAAgB,mBAAmB,QAA0B;CAE3D,OAAO;EACL,aAFa,QAAQ,UAAU,oBAAoB;EAMnD,OAAO,mBAAmB,QAAQ,KAAK;EACvC,GAAI,QAAQ,WAAW,EAAE,UAAU,OAAO,SAAS,IAAI,CAAC;CAC1D;AACF;;;;;AAMA,SAAgB,uBAAuB,QAA0B;CAC/D,OAAO,gBAAgB,mBAAmB,MAAM,CAAC;AACnD;AAEA,
|
|
1
|
+
{"version":3,"file":"client.js","names":["generateId","_generateId"],"sources":["../../../src/utils/client.ts"],"sourcesContent":["import { generateId as _generateId, getApiKeyFromEnv } from '@tanstack/ai-utils'\nimport { createFalClient } from '../client/index'\nimport { createBillingFetch } from './billing'\n\nexport interface FalClientConfig {\n apiKey: string\n proxyUrl?: string\n /**\n * Override the underlying fetch used for fal requests. The adapter wraps it to\n * read the `x-fal-billable-units` header (see ./billing.ts), so usage capture\n * still works. Defaults to the global `fetch`. Useful for proxying,\n * instrumentation, or pointing requests at a mock in tests.\n */\n fetch?: typeof fetch\n}\n\nexport function getFalApiKeyFromEnv(): string {\n return getApiKeyFromEnv('FAL_KEY')\n}\n\nexport function toFalClientOptions(config?: FalClientConfig) {\n const apiKey = config?.apiKey ?? getFalApiKeyFromEnv()\n return {\n credentials: apiKey,\n // Wrap the (optionally overridden) fetch to read fal's\n // `x-fal-billable-units` header off every response so adapters can surface\n // the billed quantity as `result.usage`. See ./billing.ts.\n fetch: createBillingFetch(config?.fetch),\n ...(config?.proxyUrl ? { proxyUrl: config.proxyUrl } : {}),\n }\n}\n\n/**\n * Create the fal client for one adapter. Each adapter has its own client, so\n * two adapters with different API keys do not change each other.\n */\nexport function createAdapterFalClient(config?: FalClientConfig) {\n return createFalClient(toFalClientOptions(config))\n}\n\n/**\n * fal can report a request COMPLETED and then answer the result fetch with a\n * 422. Returns its `detail` text, or joins a detail list as `field: message`\n * pairs. Returns undefined when the error has no detail.\n */\nexport function falValidationMessage(error: unknown): string | undefined {\n if (typeof error !== 'object' || error === null || !('body' in error)) {\n return undefined\n }\n const body = error.body\n if (typeof body !== 'object' || body === null || !('detail' in body)) {\n return undefined\n }\n const detail = body.detail\n if (typeof detail === 'string') return detail || undefined\n if (!Array.isArray(detail)) return undefined\n const message = detail\n .map((d: { msg?: string; loc?: Array<string> }) => {\n const msg = d.msg ?? 'invalid value'\n return d.loc ? `${d.loc.join('.')}: ${msg}` : msg\n })\n .join('; ')\n return message || undefined\n}\n\nexport function generateId(prefix: string): string {\n return _generateId(prefix)\n}\n\n/**\n * Extract a safe file extension from a URL. Strips query strings, URL\n * fragments, and any trailing slashes, and only returns the extension when\n * it looks like a real one (2-5 alphanumeric chars). Returns undefined\n * otherwise so callers can fall back to a default.\n */\nexport function extractUrlExtension(url: string): string | undefined {\n // Parse via URL when possible so we only look at the pathname and never\n // mistake a TLD (e.g. the `.com` in `https://x.com/`) for a file extension.\n let pathname: string\n try {\n const parsed = new URL(url)\n pathname = parsed.pathname\n } catch {\n // Fall back to treating the input as a raw path when URL parsing fails\n // (e.g. the caller passed a bare path). Still strip ?query and #fragment.\n pathname = url.split('?')[0]?.split('#')[0] ?? url\n }\n // Drop trailing slashes so `/path/audio.mp3/` still yields `mp3`.\n const normalized = pathname.replace(/\\/+$/, '')\n // Require at least one `/` — otherwise we're looking at an empty pathname\n // (bare-host URLs like `https://x.com/` land here after stripping the\n // trailing slash).\n if (!normalized.includes('/')) return undefined\n const lastSegment = normalized.split('/').pop()\n if (!lastSegment) return undefined\n const extension = lastSegment.split('.').pop()\n if (!extension || extension === lastSegment) return undefined\n return /^[a-z0-9]{2,5}$/i.test(extension) ? extension : undefined\n}\n\n/**\n * Derive a reasonable audio content-type. Prefers the explicit MIME (stripped\n * of parameters), then an extension-based lookup for common audio formats,\n * otherwise falls back to audio/mpeg — fal URLs virtually always serve mp3.\n */\nexport function deriveAudioContentType(\n explicitContentType: string | undefined,\n url: string,\n): string {\n const stripped = explicitContentType?.split(';')[0]?.trim()\n if (stripped) return stripped\n\n const ext = extractUrlExtension(url)?.toLowerCase()\n switch (ext) {\n case 'mp3':\n return 'audio/mpeg'\n case 'wav':\n return 'audio/wav'\n case 'ogg':\n case 'oga':\n return 'audio/ogg'\n case 'flac':\n return 'audio/flac'\n case 'aac':\n return 'audio/aac'\n case 'm4a':\n case 'mp4':\n return 'audio/mp4'\n case 'webm':\n return 'audio/webm'\n case undefined:\n default:\n return 'audio/mpeg'\n }\n}\n\n/**\n * Decode a `data:` URL into a Blob so fal-client can auto-upload it via\n * `fal.storage.upload`. fal's inference API rejects data URLs with a 422\n * \"Unsupported data URL\", so we convert them before handing them off.\n *\n * Supports both base64 and URL-encoded data URLs. Returns `undefined` for\n * anything that isn't a data URL, so callers can fall through to other\n * handling (http URLs are passed to fal as-is).\n */\nexport function dataUrlToBlob(value: string): Blob | undefined {\n if (!value.startsWith('data:')) return undefined\n const commaIndex = value.indexOf(',')\n if (commaIndex === -1) return undefined\n\n const header = value.slice(5, commaIndex)\n const payload = value.slice(commaIndex + 1)\n const isBase64 = /;base64$/i.test(header)\n const mimeType = header.split(';')[0] || 'application/octet-stream'\n\n if (isBase64) {\n const binary = atob(payload)\n const bytes = new Uint8Array(binary.length)\n for (let i = 0; i < binary.length; i += 1) {\n bytes[i] = binary.charCodeAt(i)\n }\n return new Blob([bytes], { type: mimeType })\n }\n\n return new Blob([decodeURIComponent(payload)], { type: mimeType })\n}\n\n/**\n * Convert an ArrayBuffer to base64 in a cross-runtime way.\n *\n * The naive `btoa(String.fromCharCode(...bytes))` form blows up V8's argument\n * limit (~65k) on realistic audio payloads, so we either use `Buffer` (Node /\n * Bun) or walk the byte array in a single loop (browser).\n */\nexport function arrayBufferToBase64(bytes: ArrayBuffer): string {\n if (typeof Buffer !== 'undefined' && typeof Buffer.from === 'function') {\n return Buffer.from(bytes).toString('base64')\n }\n const view = new Uint8Array(bytes)\n let binary = ''\n for (const byte of view) {\n binary += String.fromCharCode(byte)\n }\n return btoa(binary)\n}\n"],"mappings":"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"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { toFalClientOptions } from "./client.js";
|
|
2
2
|
import { fal } from "@fal-ai/client";
|
|
3
|
-
|
|
3
|
+
// src/utils/configure.ts
|
|
4
4
|
/**
|
|
5
5
|
* Configure the global `fal` client of `@fal-ai/client` with this config, for
|
|
6
6
|
* apps that call `fal` themselves. The adapters do not use the global client.
|
|
@@ -14,7 +14,6 @@ import { fal } from "@fal-ai/client";
|
|
|
14
14
|
function configureFalClient(config) {
|
|
15
15
|
fal.config(toFalClientOptions(config));
|
|
16
16
|
}
|
|
17
|
-
//#endregion
|
|
18
17
|
export { configureFalClient };
|
|
19
18
|
|
|
20
19
|
//# sourceMappingURL=configure.js.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
// src/video/video-provider-options.ts
|
|
2
2
|
function mapVideoSizeToFalFormat(size) {
|
|
3
3
|
if (!size) return {};
|
|
4
4
|
if (size.includes("_")) {
|
|
@@ -33,9 +33,22 @@ var VEO_4_6_8 = {
|
|
|
33
33
|
var LTX_6_8_10 = {
|
|
34
34
|
kind: "discrete",
|
|
35
35
|
values: [
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
6,
|
|
37
|
+
8,
|
|
38
|
+
10
|
|
39
|
+
]
|
|
40
|
+
};
|
|
41
|
+
var LTX_FAST_6_20 = {
|
|
42
|
+
kind: "discrete",
|
|
43
|
+
values: [
|
|
44
|
+
6,
|
|
45
|
+
8,
|
|
46
|
+
10,
|
|
47
|
+
12,
|
|
48
|
+
14,
|
|
49
|
+
16,
|
|
50
|
+
18,
|
|
51
|
+
20
|
|
39
52
|
]
|
|
40
53
|
};
|
|
41
54
|
var KLING_3_15 = {
|
|
@@ -73,11 +86,6 @@ var FAL_VIDEO_DURATIONS = durationMap({
|
|
|
73
86
|
"fal-ai/kling-video/v2.6/pro/image-to-video": KLING_5_10,
|
|
74
87
|
"fal-ai/kling-video/v3/pro/text-to-video": KLING_3_15,
|
|
75
88
|
"fal-ai/kling-video/v3/pro/image-to-video": KLING_3_15,
|
|
76
|
-
"fal-ai/pika/v2.2/text-to-video": KLING_5_10,
|
|
77
|
-
"fal-ai/luma-dream-machine/ray-2": {
|
|
78
|
-
kind: "discrete",
|
|
79
|
-
values: ["5s", "9s"]
|
|
80
|
-
},
|
|
81
89
|
"fal-ai/veo3": VEO_4_6_8,
|
|
82
90
|
"fal-ai/veo3/image-to-video": VEO_4_6_8,
|
|
83
91
|
"fal-ai/veo3.1": VEO_4_6_8,
|
|
@@ -85,35 +93,16 @@ var FAL_VIDEO_DURATIONS = durationMap({
|
|
|
85
93
|
"fal-ai/veo3.1/fast": VEO_4_6_8,
|
|
86
94
|
"fal-ai/veo3.1/fast/image-to-video": VEO_4_6_8,
|
|
87
95
|
"fal-ai/ltx-2.3/text-to-video": LTX_6_8_10,
|
|
88
|
-
"fal-ai/ltx-2.3/text-to-video/fast":
|
|
96
|
+
"fal-ai/ltx-2.3/text-to-video/fast": LTX_FAST_6_20,
|
|
89
97
|
"fal-ai/ltx-2.3/image-to-video": LTX_6_8_10,
|
|
90
|
-
"fal-ai/ltx-2.3/image-to-video/fast":
|
|
91
|
-
"fal-ai/wan-25-preview/text-to-video":
|
|
92
|
-
kind: "discrete",
|
|
93
|
-
values: [
|
|
94
|
-
"2",
|
|
95
|
-
"3",
|
|
96
|
-
"4",
|
|
97
|
-
"5",
|
|
98
|
-
"6",
|
|
99
|
-
"7",
|
|
100
|
-
"8",
|
|
101
|
-
"9",
|
|
102
|
-
"10",
|
|
103
|
-
"11",
|
|
104
|
-
"12",
|
|
105
|
-
"13",
|
|
106
|
-
"14",
|
|
107
|
-
"15"
|
|
108
|
-
]
|
|
109
|
-
},
|
|
98
|
+
"fal-ai/ltx-2.3/image-to-video/fast": LTX_FAST_6_20,
|
|
99
|
+
"fal-ai/wan-25-preview/text-to-video": KLING_5_10,
|
|
110
100
|
"fal-ai/minimax/video-01": { kind: "none" },
|
|
111
101
|
"fal-ai/hunyuan-video-v1.5/text-to-video": { kind: "none" }
|
|
112
102
|
});
|
|
113
103
|
function getFalVideoDurationOptions(model) {
|
|
114
104
|
return FAL_VIDEO_DURATIONS[model] ?? { kind: "none" };
|
|
115
105
|
}
|
|
116
|
-
//#endregion
|
|
117
106
|
export { getFalVideoDurationOptions, mapVideoSizeToFalFormat };
|
|
118
107
|
|
|
119
108
|
//# sourceMappingURL=video-provider-options.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"video-provider-options.js","names":[],"sources":["../../../src/video/video-provider-options.ts"],"sourcesContent":["import type { DurationOptions } from '@tanstack/ai/adapters'\nimport type {\n FalModelVideoDuration,\n FalModelVideoSize,\n FalModelVideoSizeInput,\n} from '../model-meta'\n\nexport function mapVideoSizeToFalFormat<TModel extends string>(\n size?: FalModelVideoSize<TModel>,\n): FalModelVideoSizeInput<TModel> {\n if (!size) {\n return {} as FalModelVideoSizeInput<TModel>\n }\n\n if (size.includes('_')) {\n const [aspectRatio, resolution] = size.split('_')\n return {\n aspect_ratio: aspectRatio,\n resolution,\n } as FalModelVideoSizeInput<TModel>\n }\n\n if (size.includes(':')) {\n return { aspect_ratio: size } as FalModelVideoSizeInput<TModel>\n }\n\n return { resolution: size } as FalModelVideoSizeInput<TModel>\n}\n\n/**\n * Identity helper that checks each entry against the SDK's `duration` union\n * for its own key, so a typo'd endpoint id or a stale union fails to compile.\n */\nfunction durationMap<\n const T extends {\n [K in keyof T & string]: DurationOptions<FalModelVideoDuration<K>>\n },\n>(map: T): T {\n return map\n}\n\nconst KLING_5_10 = { kind: 'discrete', values: ['5', '10'] } as const\nconst VEO_4_6_8 = { kind: 'discrete', values: ['4s', '6s', '8s'] } as const\nconst LTX_6_8_10 = { kind: 'discrete', values: [
|
|
1
|
+
{"version":3,"file":"video-provider-options.js","names":[],"sources":["../../../src/video/video-provider-options.ts"],"sourcesContent":["import type { DurationOptions } from '@tanstack/ai/adapters'\nimport type {\n FalModelVideoDuration,\n FalModelVideoSize,\n FalModelVideoSizeInput,\n} from '../model-meta'\n\nexport function mapVideoSizeToFalFormat<TModel extends string>(\n size?: FalModelVideoSize<TModel>,\n): FalModelVideoSizeInput<TModel> {\n if (!size) {\n return {} as FalModelVideoSizeInput<TModel>\n }\n\n if (size.includes('_')) {\n const [aspectRatio, resolution] = size.split('_')\n return {\n aspect_ratio: aspectRatio,\n resolution,\n } as FalModelVideoSizeInput<TModel>\n }\n\n if (size.includes(':')) {\n return { aspect_ratio: size } as FalModelVideoSizeInput<TModel>\n }\n\n return { resolution: size } as FalModelVideoSizeInput<TModel>\n}\n\n/**\n * Identity helper that checks each entry against the SDK's `duration` union\n * for its own key, so a typo'd endpoint id or a stale union fails to compile.\n */\nfunction durationMap<\n const T extends {\n [K in keyof T & string]: DurationOptions<FalModelVideoDuration<K>>\n },\n>(map: T): T {\n return map\n}\n\nconst KLING_5_10 = { kind: 'discrete', values: ['5', '10'] } as const\nconst VEO_4_6_8 = { kind: 'discrete', values: ['4s', '6s', '8s'] } as const\nconst LTX_6_8_10 = { kind: 'discrete', values: [6, 8, 10] } as const\n// Durations over 10 seconds need 25 FPS and 1080p.\nconst LTX_FAST_6_20 = {\n kind: 'discrete',\n values: [6, 8, 10, 12, 14, 16, 18, 20],\n} as const\nconst KLING_3_15 = {\n kind: 'discrete',\n values: [\n '3',\n '4',\n '5',\n '6',\n '7',\n '8',\n '9',\n '10',\n '11',\n '12',\n '13',\n '14',\n '15',\n ],\n} as const\n\n/**\n * Curated map of per-model duration options for popular fal.ai video models.\n * Values are checked at compile time against `@fal-ai/client`'s\n * `EndpointTypeMap` input types via `durationMap`.\n *\n * Models not listed here fall back to `{ kind: 'none' }` — honest \"we don't\n * know\" rather than guessing. The type-level `FalModelVideoDuration<TModel>`\n * still derives from the SDK types, so autocomplete works for SDK-known\n * models that aren't curated here.\n */\nconst FAL_VIDEO_DURATIONS = durationMap({\n 'fal-ai/kling-video/v1.6/standard/text-to-video': KLING_5_10,\n 'fal-ai/kling-video/v1.6/pro/text-to-video': KLING_5_10,\n 'fal-ai/kling-video/v2.6/pro/text-to-video': KLING_5_10,\n 'fal-ai/kling-video/v2.6/pro/image-to-video': KLING_5_10,\n 'fal-ai/kling-video/v3/pro/text-to-video': KLING_3_15,\n 'fal-ai/kling-video/v3/pro/image-to-video': KLING_3_15,\n 'fal-ai/veo3': VEO_4_6_8,\n 'fal-ai/veo3/image-to-video': VEO_4_6_8,\n 'fal-ai/veo3.1': VEO_4_6_8,\n 'fal-ai/veo3.1/image-to-video': VEO_4_6_8,\n 'fal-ai/veo3.1/fast': VEO_4_6_8,\n 'fal-ai/veo3.1/fast/image-to-video': VEO_4_6_8,\n 'fal-ai/ltx-2.3/text-to-video': LTX_6_8_10,\n 'fal-ai/ltx-2.3/text-to-video/fast': LTX_FAST_6_20,\n 'fal-ai/ltx-2.3/image-to-video': LTX_6_8_10,\n 'fal-ai/ltx-2.3/image-to-video/fast': LTX_FAST_6_20,\n 'fal-ai/wan-25-preview/text-to-video': KLING_5_10,\n 'fal-ai/minimax/video-01': { kind: 'none' },\n 'fal-ai/hunyuan-video-v1.5/text-to-video': { kind: 'none' },\n})\n\nexport function getFalVideoDurationOptions<TModel extends string>(\n model: TModel,\n): DurationOptions<FalModelVideoDuration<TModel>> {\n const entry = (FAL_VIDEO_DURATIONS as Record<string, unknown>)[model]\n // The map is keyed by literal ids but the adapter's TModel is an open string;\n // the lookup result can't be correlated by TS, hence the one cast here.\n return (entry ?? { kind: 'none' }) as DurationOptions<\n FalModelVideoDuration<TModel>\n >\n}\n"],"mappings":";AAOA,SAAgB,wBACd,MACgC;CAChC,IAAI,CAAC,MACH,OAAO,CAAC;CAGV,IAAI,KAAK,SAAS,GAAG,GAAG;EACtB,MAAM,CAAC,aAAa,cAAc,KAAK,MAAM,GAAG;EAChD,OAAO;GACL,cAAc;GACd;EACF;CACF;CAEA,IAAI,KAAK,SAAS,GAAG,GACnB,OAAO,EAAE,cAAc,KAAK;CAG9B,OAAO,EAAE,YAAY,KAAK;AAC5B;;;;;AAMA,SAAS,YAIP,KAAW;CACX,OAAO;AACT;AAEA,IAAM,aAAa;CAAE,MAAM;CAAY,QAAQ,CAAC,KAAK,IAAI;AAAE;AAC3D,IAAM,YAAY;CAAE,MAAM;CAAY,QAAQ;EAAC;EAAM;EAAM;CAAI;AAAE;AACjE,IAAM,aAAa;CAAE,MAAM;CAAY,QAAQ;EAAC;EAAG;EAAG;CAAE;AAAE;AAE1D,IAAM,gBAAgB;CACpB,MAAM;CACN,QAAQ;EAAC;EAAG;EAAG;EAAI;EAAI;EAAI;EAAI;EAAI;CAAE;AACvC;AACA,IAAM,aAAa;CACjB,MAAM;CACN,QAAQ;EACN;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF;AACF;;;;;;;;;;;AAYA,IAAM,sBAAsB,YAAY;CACtC,kDAAkD;CAClD,6CAA6C;CAC7C,6CAA6C;CAC7C,8CAA8C;CAC9C,2CAA2C;CAC3C,4CAA4C;CAC5C,eAAe;CACf,8BAA8B;CAC9B,iBAAiB;CACjB,gCAAgC;CAChC,sBAAsB;CACtB,qCAAqC;CACrC,gCAAgC;CAChC,qCAAqC;CACrC,iCAAiC;CACjC,sCAAsC;CACtC,uCAAuC;CACvC,2BAA2B,EAAE,MAAM,OAAO;CAC1C,2CAA2C,EAAE,MAAM,OAAO;AAC5D,CAAC;AAED,SAAgB,2BACd,OACgD;CAIhD,OAHe,oBAAgD,UAG9C,EAAE,MAAM,OAAO;AAGlC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-fal",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "fal.ai adapter for TanStack AI image, video, live, audio, speech, and transcription generation.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -54,16 +54,16 @@
|
|
|
54
54
|
"media-generation"
|
|
55
55
|
],
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@fal-ai/client": "^1.
|
|
57
|
+
"@fal-ai/client": "^1.11.0",
|
|
58
58
|
"@tanstack/ai-utils": "^1.1.0"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@vitest/coverage-v8": "4.1.
|
|
62
|
-
"vite": "^8.
|
|
63
|
-
"@tanstack/ai": "1.
|
|
61
|
+
"@vitest/coverage-v8": "4.1.12",
|
|
62
|
+
"vite": "^8.3.8",
|
|
63
|
+
"@tanstack/ai": "1.7.0"
|
|
64
64
|
},
|
|
65
65
|
"peerDependencies": {
|
|
66
|
-
"@tanstack/ai": "^1.
|
|
66
|
+
"@tanstack/ai": "^1.7.0"
|
|
67
67
|
},
|
|
68
68
|
"scripts": {
|
|
69
69
|
"build": "vite build",
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { BaseModel3DAdapter } from '@tanstack/ai/adapters'
|
|
2
|
+
import {
|
|
3
|
+
createAdapterFalClient,
|
|
4
|
+
generateId as utilGenerateId,
|
|
5
|
+
} from '../utils/client'
|
|
6
|
+
import { buildFalUsage, takeBillableUnits } from '../utils/billing'
|
|
7
|
+
import { ApiError, ValidationError } from '../client/index'
|
|
8
|
+
import { mapModel3DPromptToFalFields } from '../model3d/model3d-inputs'
|
|
9
|
+
import { mapFalModel3DOutput } from '../model3d/model3d-output'
|
|
10
|
+
import type { Model3DGenerationOptions } from '@tanstack/ai'
|
|
11
|
+
import type {
|
|
12
|
+
Fal3DModel,
|
|
13
|
+
Fal3DModelProviderOptions,
|
|
14
|
+
Fal3DPromptModalitiesFor,
|
|
15
|
+
} from '../model-meta'
|
|
16
|
+
import type { FalClientConfig } from '../utils/client'
|
|
17
|
+
import type { FalClient } from '../client/index'
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Read the message of a failed fal request. fal puts the reason in
|
|
21
|
+
* `body.detail`: a string, or a list of field errors for a 422.
|
|
22
|
+
*/
|
|
23
|
+
function falErrorMessage(error: unknown) {
|
|
24
|
+
if (error instanceof ApiError) {
|
|
25
|
+
const body: unknown = error.body
|
|
26
|
+
const detail =
|
|
27
|
+
typeof body === 'object' && body !== null && 'detail' in body
|
|
28
|
+
? body.detail
|
|
29
|
+
: undefined
|
|
30
|
+
if (typeof detail === 'string') return detail
|
|
31
|
+
if (error instanceof ValidationError && error.fieldErrors.length > 0) {
|
|
32
|
+
return error.fieldErrors
|
|
33
|
+
.map(({ loc, msg }) =>
|
|
34
|
+
loc.length > 0 ? `${loc.join('.')}: ${msg}` : msg,
|
|
35
|
+
)
|
|
36
|
+
.join('; ')
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return error instanceof Error ? error.message : String(error)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* fal 3D model adapter. It runs any fal 3D endpoint (Hunyuan3D, TRELLIS,
|
|
44
|
+
* Rodin, Tripo, Meshy, SAM-3 3D, and others) as a queue job.
|
|
45
|
+
*
|
|
46
|
+
* The prompt text, image parts, and `model3d` parts go to the endpoint input
|
|
47
|
+
* fields. `modelOptions` is typed from the endpoint input, without those
|
|
48
|
+
* fields. The result has the best GLB in `url`, and every mesh file and
|
|
49
|
+
* texture file in `files`.
|
|
50
|
+
*
|
|
51
|
+
* @experimental 3D model generation is experimental and may change.
|
|
52
|
+
*/
|
|
53
|
+
export class Fal3DModelAdapter<
|
|
54
|
+
TModel extends Fal3DModel,
|
|
55
|
+
> extends BaseModel3DAdapter<
|
|
56
|
+
TModel,
|
|
57
|
+
Fal3DModelProviderOptions<TModel>,
|
|
58
|
+
Record<TModel, Fal3DModelProviderOptions<TModel>>,
|
|
59
|
+
Record<TModel, Fal3DPromptModalitiesFor<TModel>>
|
|
60
|
+
> {
|
|
61
|
+
readonly name = 'fal' as const
|
|
62
|
+
// Consumes fal storage URLs uploaded via falFiles().
|
|
63
|
+
override readonly supportsFileSources = true
|
|
64
|
+
private readonly client: FalClient
|
|
65
|
+
|
|
66
|
+
constructor(model: TModel, config?: FalClientConfig) {
|
|
67
|
+
super({}, model)
|
|
68
|
+
this.client = createAdapterFalClient(config)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async create3DModelJob(
|
|
72
|
+
options: Model3DGenerationOptions<Fal3DModelProviderOptions<TModel>>,
|
|
73
|
+
) {
|
|
74
|
+
const { modelOptions, logger } = options
|
|
75
|
+
|
|
76
|
+
logger.request(
|
|
77
|
+
`activity=generate3DModel provider=fal model=${this.model}`,
|
|
78
|
+
{ provider: 'fal', model: this.model },
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
// A plain string id, so the fal client takes any input object.
|
|
83
|
+
const endpoint: string = this.model
|
|
84
|
+
const promptFields = mapModel3DPromptToFalFields(
|
|
85
|
+
this.model,
|
|
86
|
+
options.prompt,
|
|
87
|
+
)
|
|
88
|
+
// Submit to the queue. Pass the abortSignal per request, not in the
|
|
89
|
+
// client config, so it cannot cancel other jobs.
|
|
90
|
+
const { request_id } = await this.client.queue.submit(endpoint, {
|
|
91
|
+
input: { ...modelOptions, ...promptFields },
|
|
92
|
+
...(options.abortSignal ? { abortSignal: options.abortSignal } : {}),
|
|
93
|
+
})
|
|
94
|
+
return { jobId: request_id, model: this.model }
|
|
95
|
+
} catch (error) {
|
|
96
|
+
logger.errors('fal.create3DModelJob fatal', {
|
|
97
|
+
error,
|
|
98
|
+
source: 'fal.create3DModelJob',
|
|
99
|
+
})
|
|
100
|
+
throw error
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Reads the queue status. fal has no failed queue status: a failed job is
|
|
106
|
+
* `COMPLETED` with an `error` message, and this maps it to `failed`.
|
|
107
|
+
*/
|
|
108
|
+
async get3DModelStatus(
|
|
109
|
+
jobId: string,
|
|
110
|
+
options?: { abortSignal?: AbortSignal },
|
|
111
|
+
) {
|
|
112
|
+
const endpoint: string = this.model
|
|
113
|
+
const falStatus = await this.client.queue.status(endpoint, {
|
|
114
|
+
requestId: jobId,
|
|
115
|
+
...(options?.abortSignal ? { abortSignal: options.abortSignal } : {}),
|
|
116
|
+
})
|
|
117
|
+
switch (falStatus.status) {
|
|
118
|
+
case 'IN_QUEUE':
|
|
119
|
+
return { jobId, status: 'pending' as const }
|
|
120
|
+
case 'IN_PROGRESS':
|
|
121
|
+
return { jobId, status: 'processing' as const }
|
|
122
|
+
case 'COMPLETED': {
|
|
123
|
+
const error =
|
|
124
|
+
'error' in falStatus && typeof falStatus.error === 'string'
|
|
125
|
+
? falStatus.error
|
|
126
|
+
: undefined
|
|
127
|
+
return error
|
|
128
|
+
? { jobId, status: 'failed' as const, error }
|
|
129
|
+
: { jobId, status: 'completed' as const }
|
|
130
|
+
}
|
|
131
|
+
default: {
|
|
132
|
+
const unhandled: never = falStatus
|
|
133
|
+
throw new Error(
|
|
134
|
+
`fal: unknown queue status ${JSON.stringify(unhandled)}`,
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Reads the finished job and maps it to one `Model3DResult`. A failed job
|
|
142
|
+
* throws an `Error` with the message from fal.
|
|
143
|
+
*/
|
|
144
|
+
async get3DModel(jobId: string, options?: { abortSignal?: AbortSignal }) {
|
|
145
|
+
const endpoint: string = this.model
|
|
146
|
+
let result
|
|
147
|
+
try {
|
|
148
|
+
result = await this.client.queue.result(endpoint, {
|
|
149
|
+
requestId: jobId,
|
|
150
|
+
...(options?.abortSignal ? { abortSignal: options.abortSignal } : {}),
|
|
151
|
+
})
|
|
152
|
+
} catch (error: unknown) {
|
|
153
|
+
// A cancelled call is not a provider failure: keep the abort error.
|
|
154
|
+
if (options?.abortSignal?.aborted) throw error
|
|
155
|
+
throw new Error(
|
|
156
|
+
`fal: 3D model job ${jobId} on ${this.model} failed: ${falErrorMessage(error)}`,
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const usage = buildFalUsage(takeBillableUnits(result.requestId))
|
|
161
|
+
return {
|
|
162
|
+
...mapFalModel3DOutput(jobId, result.data),
|
|
163
|
+
...(usage ? { usage } : {}),
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Cancels a queued job with the fal queue cancel endpoint. fal can refuse
|
|
169
|
+
* when the job already runs, and then this rejects.
|
|
170
|
+
*/
|
|
171
|
+
async cancel3DModelJob(
|
|
172
|
+
jobId: string,
|
|
173
|
+
options?: { abortSignal?: AbortSignal },
|
|
174
|
+
) {
|
|
175
|
+
const endpoint: string = this.model
|
|
176
|
+
await this.client.queue.cancel(endpoint, {
|
|
177
|
+
requestId: jobId,
|
|
178
|
+
...(options?.abortSignal ? { abortSignal: options.abortSignal } : {}),
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
protected override generateId() {
|
|
183
|
+
return utilGenerateId(this.name)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Create a fal 3D model adapter. It reads the API key from `config.apiKey`,
|
|
189
|
+
* or from the `FAL_KEY` environment variable.
|
|
190
|
+
*
|
|
191
|
+
* @param model - A fal 3D endpoint id, for example
|
|
192
|
+
* `'fal-ai/hunyuan3d-v3/image-to-3d'`. Known ids autocomplete and type
|
|
193
|
+
* `modelOptions`. Other ids work untyped.
|
|
194
|
+
* @param config - Optional fal client config (API key, proxy URL, fetch).
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* import { generate3DModel } from '@tanstack/ai'
|
|
199
|
+
* import { fal3DModel } from '@tanstack/ai-fal'
|
|
200
|
+
*
|
|
201
|
+
* const { url, format } = await generate3DModel({
|
|
202
|
+
* adapter: fal3DModel('fal-ai/hunyuan3d-v3/image-to-3d'),
|
|
203
|
+
* threadId: 'thread-1',
|
|
204
|
+
* prompt: [
|
|
205
|
+
* { type: 'image', source: { type: 'url', value: 'https://example.com/chair.png' } },
|
|
206
|
+
* ],
|
|
207
|
+
* modelOptions: { face_count: 50000 },
|
|
208
|
+
* wait: true,
|
|
209
|
+
* })
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* @experimental 3D model generation is experimental and may change.
|
|
213
|
+
*/
|
|
214
|
+
export function fal3DModel<TModel extends Fal3DModel>(
|
|
215
|
+
model: TModel,
|
|
216
|
+
config?: FalClientConfig,
|
|
217
|
+
) {
|
|
218
|
+
return new Fal3DModelAdapter(model, config)
|
|
219
|
+
}
|