@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
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { buildFalUsage, takeBillableUnits } from "../utils/billing.js";
|
|
2
1
|
import { createAdapterFalClient, deriveAudioContentType, generateId } from "../utils/client.js";
|
|
2
|
+
import { buildFalUsage, takeBillableUnits } from "../utils/billing.js";
|
|
3
3
|
import { BaseAudioAdapter } from "@tanstack/ai/adapters";
|
|
4
|
-
|
|
4
|
+
// src/adapters/audio.ts
|
|
5
5
|
/**
|
|
6
6
|
* Fal audio models don't agree on the name for "how long should this be."
|
|
7
7
|
* Most accept seconds via `duration`, but a few use model-specific fields —
|
|
@@ -101,7 +101,6 @@ var FalAudioAdapter = class extends BaseAudioAdapter {
|
|
|
101
101
|
function falAudio(model, config) {
|
|
102
102
|
return new FalAudioAdapter(model, config);
|
|
103
103
|
}
|
|
104
|
-
//#endregion
|
|
105
104
|
export { FalAudioAdapter, falAudio };
|
|
106
105
|
|
|
107
106
|
//# sourceMappingURL=audio.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"audio.js","names":[],"sources":["../../../src/adapters/audio.ts"],"sourcesContent":["import { BaseAudioAdapter } from '@tanstack/ai/adapters'\nimport {\n createAdapterFalClient,\n deriveAudioContentType,\n generateId as utilGenerateId,\n} from '../utils/client'\nimport { buildFalUsage, takeBillableUnits } from '../utils/billing'\nimport type { OutputType, Result } from '@fal-ai/client'\nimport type {\n AudioGenerationOptions,\n AudioGenerationResult,\n} from '@tanstack/ai'\nimport type { FalClientConfig } from '../utils/client'\nimport type { FalClient } from '../client/index'\nimport type { FalModel, FalModelInput } from '../model-meta'\n\n/**\n * Provider options for audio generation, excluding fields TanStack AI handles.\n */\nexport type FalAudioProviderOptions<TModel extends string> = Omit<\n FalModelInput<TModel>,\n 'prompt'\n>\n\n/**\n * Fal audio models don't agree on the name for \"how long should this be.\"\n * Most accept seconds via `duration`, but a few use model-specific fields —\n * e.g. ElevenLabs wants milliseconds via `music_length_ms`, Stable Audio wants\n * `seconds_total`. Explicit per-model entries override the default; user\n * `modelOptions` still win over either.\n */\nconst DURATION_FRAGMENT_BUILDERS: Record<\n string,\n (seconds: number) => Record<string, unknown>\n> = {\n 'fal-ai/elevenlabs/music': (seconds) => ({ music_length_ms: seconds * 1000 }),\n 'fal-ai/stable-audio-25/text-to-audio': (seconds) => ({\n seconds_total: seconds,\n }),\n}\n\nfunction buildDurationFragment(\n model: string,\n duration: number | undefined,\n): Record<string, unknown> {\n if (duration == null) return {}\n const builder = DURATION_FRAGMENT_BUILDERS[model]\n return builder ? builder(duration) : { duration }\n}\n\n/**\n * fal.ai audio generation adapter.\n *\n * Supports fal.ai audio models like diffrhythm (music), sound effects, etc.\n *\n * @example\n * ```typescript\n * const adapter = falAudio('fal-ai/diffrhythm')\n * const result = await generateAudio({\n * adapter,\n * prompt: 'An upbeat electronic track with synths',\n * duration: 10,\n * })\n * ```\n */\nexport class FalAudioAdapter<TModel extends FalModel> extends BaseAudioAdapter<\n TModel,\n FalAudioProviderOptions<TModel>\n> {\n readonly name = 'fal' as const\n private readonly client: FalClient\n\n constructor(model: TModel, config?: FalClientConfig) {\n super(model, {})\n this.client = createAdapterFalClient(config)\n }\n\n async generateAudio(\n options: AudioGenerationOptions<FalAudioProviderOptions<TModel>>,\n ): Promise<AudioGenerationResult> {\n const { logger } = options\n logger.request(`activity=generateAudio provider=fal model=${this.model}`, {\n provider: 'fal',\n model: this.model,\n })\n let requestId: string | undefined\n try {\n const input = this.buildInput(options)\n // Pass the abortSignal per request, not in the client config.\n const result = await this.client.subscribe(this.model, {\n input,\n ...(options.abortSignal ? { abortSignal: options.abortSignal } : {}),\n onEnqueue: (id) => {\n requestId = id\n },\n })\n return this.transformResponse(result)\n } catch (error) {\n // The SDK stops waiting on abort but leaves the queued job running.\n // Cancel it so it stops billing.\n if (requestId && options.abortSignal?.aborted) {\n this.client.queue.cancel(this.model, { requestId }).catch(() => {})\n }\n logger.errors('fal.generateAudio fatal', {\n error,\n source: 'fal.generateAudio',\n })\n throw error\n }\n }\n\n private buildInput(\n options: AudioGenerationOptions<FalAudioProviderOptions<TModel>>,\n ): FalModelInput<TModel> {\n return {\n ...buildDurationFragment(this.model, options.duration),\n ...options.modelOptions,\n prompt: options.prompt,\n } as FalModelInput<TModel>\n }\n\n protected override generateId(): string {\n return utilGenerateId(this.name)\n }\n\n private transformResponse(\n response: Result<OutputType<TModel>>,\n ): AudioGenerationResult {\n const data = response.data as Record<string, unknown>\n\n // fal returns { audio: { url, content_type } } or { audio_url: string }\n let audioUrl: string | undefined\n let contentType: string | undefined\n\n if (\n data['audio'] &&\n typeof data['audio'] === 'object' &&\n 'url' in data['audio']\n ) {\n const audioObj = data['audio'] as { url: string; content_type?: string }\n audioUrl = audioObj.url\n contentType = audioObj.content_type\n } else if (typeof data.audio_url === 'string') {\n audioUrl = data.audio_url\n }\n\n if (!audioUrl) {\n throw new Error('Audio URL not found in fal audio generation response')\n }\n\n const usage = buildFalUsage(takeBillableUnits(response.requestId))\n\n return {\n id: response.requestId || this.generateId(),\n model: this.model,\n audio: {\n url: audioUrl,\n contentType: deriveAudioContentType(contentType, audioUrl),\n },\n ...(usage ? { usage } : {}),\n }\n }\n}\n\nexport function falAudio<TModel extends FalModel>(\n model: TModel,\n config?: FalClientConfig,\n): FalAudioAdapter<TModel> {\n return new FalAudioAdapter(model, config)\n}\n"],"mappings":";;;;;;;;;;;AA+BA,IAAM,6BAGF;CACF,4BAA4B,aAAa,EAAE,iBAAiB,UAAU,IAAK;CAC3E,yCAAyC,aAAa,EACpD,eAAe,QACjB;AACF;AAEA,SAAS,sBACP,OACA,UACyB;CACzB,IAAI,YAAY,MAAM,OAAO,CAAC;CAC9B,MAAM,UAAU,2BAA2B;CAC3C,OAAO,UAAU,QAAQ,QAAQ,IAAI,EAAE,SAAS;AAClD;;;;;;;;;;;;;;;;AAiBA,IAAa,kBAAb,cAA8D,iBAG5D;CACA,OAAgB;CAChB;CAEA,YAAY,OAAe,QAA0B;EACnD,MAAM,OAAO,CAAC,CAAC;EACf,KAAK,SAAS,uBAAuB,MAAM;CAC7C;CAEA,MAAM,cACJ,SACgC;EAChC,MAAM,EAAE,WAAW;EACnB,OAAO,QAAQ,6CAA6C,KAAK,SAAS;GACxE,UAAU;GACV,OAAO,KAAK;EACd,CAAC;EACD,IAAI;EACJ,IAAI;GACF,MAAM,QAAQ,KAAK,WAAW,OAAO;GAErC,MAAM,SAAS,MAAM,KAAK,OAAO,UAAU,KAAK,OAAO;IACrD;IACA,GAAI,QAAQ,cAAc,EAAE,aAAa,QAAQ,YAAY,IAAI,CAAC;IAClE,YAAY,OAAO;KACjB,YAAY;IACd;GACF,CAAC;GACD,OAAO,KAAK,kBAAkB,MAAM;EACtC,SAAS,OAAO;GAGd,IAAI,aAAa,QAAQ,aAAa,SACpC,KAAK,OAAO,MAAM,OAAO,KAAK,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;GAEpE,OAAO,OAAO,2BAA2B;IACvC;IACA,QAAQ;GACV,CAAC;GACD,MAAM;EACR;CACF;CAEA,WACE,SACuB;EACvB,OAAO;GACL,GAAG,sBAAsB,KAAK,OAAO,QAAQ,QAAQ;GACrD,GAAG,QAAQ;GACX,QAAQ,QAAQ;EAClB;CACF;CAEA,aAAwC;EACtC,
|
|
1
|
+
{"version":3,"file":"audio.js","names":["utilGenerateId"],"sources":["../../../src/adapters/audio.ts"],"sourcesContent":["import { BaseAudioAdapter } from '@tanstack/ai/adapters'\nimport {\n createAdapterFalClient,\n deriveAudioContentType,\n generateId as utilGenerateId,\n} from '../utils/client'\nimport { buildFalUsage, takeBillableUnits } from '../utils/billing'\nimport type { OutputType, Result } from '@fal-ai/client'\nimport type {\n AudioGenerationOptions,\n AudioGenerationResult,\n} from '@tanstack/ai'\nimport type { FalClientConfig } from '../utils/client'\nimport type { FalClient } from '../client/index'\nimport type { FalModel, FalModelInput } from '../model-meta'\n\n/**\n * Provider options for audio generation, excluding fields TanStack AI handles.\n */\nexport type FalAudioProviderOptions<TModel extends string> = Omit<\n FalModelInput<TModel>,\n 'prompt'\n>\n\n/**\n * Fal audio models don't agree on the name for \"how long should this be.\"\n * Most accept seconds via `duration`, but a few use model-specific fields —\n * e.g. ElevenLabs wants milliseconds via `music_length_ms`, Stable Audio wants\n * `seconds_total`. Explicit per-model entries override the default; user\n * `modelOptions` still win over either.\n */\nconst DURATION_FRAGMENT_BUILDERS: Record<\n string,\n (seconds: number) => Record<string, unknown>\n> = {\n 'fal-ai/elevenlabs/music': (seconds) => ({ music_length_ms: seconds * 1000 }),\n 'fal-ai/stable-audio-25/text-to-audio': (seconds) => ({\n seconds_total: seconds,\n }),\n}\n\nfunction buildDurationFragment(\n model: string,\n duration: number | undefined,\n): Record<string, unknown> {\n if (duration == null) return {}\n const builder = DURATION_FRAGMENT_BUILDERS[model]\n return builder ? builder(duration) : { duration }\n}\n\n/**\n * fal.ai audio generation adapter.\n *\n * Supports fal.ai audio models like diffrhythm (music), sound effects, etc.\n *\n * @example\n * ```typescript\n * const adapter = falAudio('fal-ai/diffrhythm')\n * const result = await generateAudio({\n * adapter,\n * prompt: 'An upbeat electronic track with synths',\n * duration: 10,\n * })\n * ```\n */\nexport class FalAudioAdapter<TModel extends FalModel> extends BaseAudioAdapter<\n TModel,\n FalAudioProviderOptions<TModel>\n> {\n readonly name = 'fal' as const\n private readonly client: FalClient\n\n constructor(model: TModel, config?: FalClientConfig) {\n super(model, {})\n this.client = createAdapterFalClient(config)\n }\n\n async generateAudio(\n options: AudioGenerationOptions<FalAudioProviderOptions<TModel>>,\n ): Promise<AudioGenerationResult> {\n const { logger } = options\n logger.request(`activity=generateAudio provider=fal model=${this.model}`, {\n provider: 'fal',\n model: this.model,\n })\n let requestId: string | undefined\n try {\n const input = this.buildInput(options)\n // Pass the abortSignal per request, not in the client config.\n const result = await this.client.subscribe(this.model, {\n input,\n ...(options.abortSignal ? { abortSignal: options.abortSignal } : {}),\n onEnqueue: (id) => {\n requestId = id\n },\n })\n return this.transformResponse(result)\n } catch (error) {\n // The SDK stops waiting on abort but leaves the queued job running.\n // Cancel it so it stops billing.\n if (requestId && options.abortSignal?.aborted) {\n this.client.queue.cancel(this.model, { requestId }).catch(() => {})\n }\n logger.errors('fal.generateAudio fatal', {\n error,\n source: 'fal.generateAudio',\n })\n throw error\n }\n }\n\n private buildInput(\n options: AudioGenerationOptions<FalAudioProviderOptions<TModel>>,\n ): FalModelInput<TModel> {\n return {\n ...buildDurationFragment(this.model, options.duration),\n ...options.modelOptions,\n prompt: options.prompt,\n } as FalModelInput<TModel>\n }\n\n protected override generateId(): string {\n return utilGenerateId(this.name)\n }\n\n private transformResponse(\n response: Result<OutputType<TModel>>,\n ): AudioGenerationResult {\n const data = response.data as Record<string, unknown>\n\n // fal returns { audio: { url, content_type } } or { audio_url: string }\n let audioUrl: string | undefined\n let contentType: string | undefined\n\n if (\n data['audio'] &&\n typeof data['audio'] === 'object' &&\n 'url' in data['audio']\n ) {\n const audioObj = data['audio'] as { url: string; content_type?: string }\n audioUrl = audioObj.url\n contentType = audioObj.content_type\n } else if (typeof data.audio_url === 'string') {\n audioUrl = data.audio_url\n }\n\n if (!audioUrl) {\n throw new Error('Audio URL not found in fal audio generation response')\n }\n\n const usage = buildFalUsage(takeBillableUnits(response.requestId))\n\n return {\n id: response.requestId || this.generateId(),\n model: this.model,\n audio: {\n url: audioUrl,\n contentType: deriveAudioContentType(contentType, audioUrl),\n },\n ...(usage ? { usage } : {}),\n }\n }\n}\n\nexport function falAudio<TModel extends FalModel>(\n model: TModel,\n config?: FalClientConfig,\n): FalAudioAdapter<TModel> {\n return new FalAudioAdapter(model, config)\n}\n"],"mappings":";;;;;;;;;;;AA+BA,IAAM,6BAGF;CACF,4BAA4B,aAAa,EAAE,iBAAiB,UAAU,IAAK;CAC3E,yCAAyC,aAAa,EACpD,eAAe,QACjB;AACF;AAEA,SAAS,sBACP,OACA,UACyB;CACzB,IAAI,YAAY,MAAM,OAAO,CAAC;CAC9B,MAAM,UAAU,2BAA2B;CAC3C,OAAO,UAAU,QAAQ,QAAQ,IAAI,EAAE,SAAS;AAClD;;;;;;;;;;;;;;;;AAiBA,IAAa,kBAAb,cAA8D,iBAG5D;CACA,OAAgB;CAChB;CAEA,YAAY,OAAe,QAA0B;EACnD,MAAM,OAAO,CAAC,CAAC;EACf,KAAK,SAAS,uBAAuB,MAAM;CAC7C;CAEA,MAAM,cACJ,SACgC;EAChC,MAAM,EAAE,WAAW;EACnB,OAAO,QAAQ,6CAA6C,KAAK,SAAS;GACxE,UAAU;GACV,OAAO,KAAK;EACd,CAAC;EACD,IAAI;EACJ,IAAI;GACF,MAAM,QAAQ,KAAK,WAAW,OAAO;GAErC,MAAM,SAAS,MAAM,KAAK,OAAO,UAAU,KAAK,OAAO;IACrD;IACA,GAAI,QAAQ,cAAc,EAAE,aAAa,QAAQ,YAAY,IAAI,CAAC;IAClE,YAAY,OAAO;KACjB,YAAY;IACd;GACF,CAAC;GACD,OAAO,KAAK,kBAAkB,MAAM;EACtC,SAAS,OAAO;GAGd,IAAI,aAAa,QAAQ,aAAa,SACpC,KAAK,OAAO,MAAM,OAAO,KAAK,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;GAEpE,OAAO,OAAO,2BAA2B;IACvC;IACA,QAAQ;GACV,CAAC;GACD,MAAM;EACR;CACF;CAEA,WACE,SACuB;EACvB,OAAO;GACL,GAAG,sBAAsB,KAAK,OAAO,QAAQ,QAAQ;GACrD,GAAG,QAAQ;GACX,QAAQ,QAAQ;EAClB;CACF;CAEA,aAAwC;EACtC,OAAOA,WAAe,KAAK,IAAI;CACjC;CAEA,kBACE,UACuB;EACvB,MAAM,OAAO,SAAS;EAGtB,IAAI;EACJ,IAAI;EAEJ,IACE,KAAK,YACL,OAAO,KAAK,aAAa,YACzB,SAAS,KAAK,UACd;GACA,MAAM,WAAW,KAAK;GACtB,WAAW,SAAS;GACpB,cAAc,SAAS;EACzB,OAAO,IAAI,OAAO,KAAK,cAAc,UACnC,WAAW,KAAK;EAGlB,IAAI,CAAC,UACH,MAAM,IAAI,MAAM,sDAAsD;EAGxE,MAAM,QAAQ,cAAc,kBAAkB,SAAS,SAAS,CAAC;EAEjE,OAAO;GACL,IAAI,SAAS,aAAa,KAAK,WAAW;GAC1C,OAAO,KAAK;GACZ,OAAO;IACL,KAAK;IACL,aAAa,uBAAuB,aAAa,QAAQ;GAC3D;GACA,GAAI,QAAQ,EAAE,MAAM,IAAI,CAAC;EAC3B;CACF;AACF;AAEA,SAAgB,SACd,OACA,QACyB;CACzB,OAAO,IAAI,gBAAgB,OAAO,MAAM;AAC1C"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createAdapterFalClient } from "../utils/client.js";
|
|
2
2
|
import { BaseFilesAdapter, normalizeFileUploadInput } from "@tanstack/ai/adapters";
|
|
3
|
-
|
|
3
|
+
// src/adapters/files.ts
|
|
4
4
|
/**
|
|
5
5
|
* fal Files adapter — uploads media to fal storage via `fal.storage.upload`.
|
|
6
6
|
* The handle is a storage URL, so `fileSourceFromHandle(handle)` references it
|
|
@@ -34,7 +34,6 @@ var FalFilesAdapter = class extends BaseFilesAdapter {
|
|
|
34
34
|
function falFiles(config) {
|
|
35
35
|
return new FalFilesAdapter(config);
|
|
36
36
|
}
|
|
37
|
-
//#endregion
|
|
38
37
|
export { FalFilesAdapter, falFiles };
|
|
39
38
|
|
|
40
39
|
//# sourceMappingURL=files.js.map
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { buildFalUsage, takeBillableUnits } from "../utils/billing.js";
|
|
2
1
|
import { createAdapterFalClient, generateId } from "../utils/client.js";
|
|
2
|
+
import { buildFalUsage, takeBillableUnits } from "../utils/billing.js";
|
|
3
3
|
import { mapSizeToFalFormat } from "../image/image-provider-options.js";
|
|
4
4
|
import { mapImageInputsToFalFields } from "../image/image-inputs.js";
|
|
5
5
|
import { resolveMediaPrompt } from "@tanstack/ai";
|
|
6
6
|
import { BaseImageAdapter } from "@tanstack/ai/adapters";
|
|
7
|
-
|
|
7
|
+
// src/adapters/image.ts
|
|
8
8
|
/**
|
|
9
9
|
* fal.ai image generation adapter with full type inference.
|
|
10
10
|
*
|
|
@@ -109,7 +109,6 @@ function createFalImage(model, config) {
|
|
|
109
109
|
function falImage(model, config) {
|
|
110
110
|
return createFalImage(model, config);
|
|
111
111
|
}
|
|
112
|
-
//#endregion
|
|
113
112
|
export { FalImageAdapter, createFalImage, falImage };
|
|
114
113
|
|
|
115
114
|
//# sourceMappingURL=image.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image.js","names":[],"sources":["../../../src/adapters/image.ts"],"sourcesContent":["import { resolveMediaPrompt } from '@tanstack/ai'\nimport { BaseImageAdapter } from '@tanstack/ai/adapters'\nimport {\n createAdapterFalClient,\n generateId as utilGenerateId,\n} from '../utils/client'\nimport { buildFalUsage, takeBillableUnits } from '../utils/billing'\nimport { mapSizeToFalFormat } from '../image/image-provider-options'\nimport { mapImageInputsToFalFields } from '../image/image-inputs'\nimport type { OutputType, Result } from '@fal-ai/client'\nimport type { FalClientConfig } from '../utils/client'\nimport type { FalClient } from '../client/index'\nimport type {\n GeneratedImage,\n ImageGenerationOptions,\n ImageGenerationResult,\n ResolvedMediaPrompt,\n} from '@tanstack/ai'\nimport type {\n FalImagePromptModalitiesFor,\n FalImageProviderOptions,\n FalModel,\n FalModelImageSize,\n FalModelInput,\n} from '../model-meta'\n\n/**\n * fal.ai image generation adapter with full type inference.\n *\n * Uses fal.ai's comprehensive type system to provide autocomplete\n *\n * and type safety for all 600+ supported models.\n *\n * @example\n * ```typescript\n * const adapter = falImage('fal-ai/flux/dev')\n * const result = await adapter.generateImages({\n * model: 'fal-ai/flux/dev',\n * prompt: 'a cat',\n * modelOptions: {\n * num_inference_steps: 28, // Type-safe! Autocomplete works\n * guidance_scale: 3.5,\n * },\n * })\n * ```\n */\nexport class FalImageAdapter<TModel extends FalModel> extends BaseImageAdapter<\n TModel,\n FalImageProviderOptions<TModel>,\n Record<TModel, FalImageProviderOptions<TModel>>,\n Record<TModel, FalModelImageSize<TModel>>,\n Record<TModel, FalImagePromptModalitiesFor<TModel>>\n> {\n override readonly kind = 'image' as const\n readonly name = 'fal' as const\n private readonly client: FalClient\n // Consumes fal storage URLs uploaded via falFiles().\n override readonly supportsFileSources = true\n\n constructor(model: TModel, config?: FalClientConfig) {\n super(model, {})\n this.client = createAdapterFalClient(config)\n }\n\n async generateImages(\n options: ImageGenerationOptions<\n FalImageProviderOptions<TModel>,\n FalModelImageSize<TModel>\n >,\n ): Promise<ImageGenerationResult> {\n const { logger } = options\n\n logger.request(`activity=generateImage provider=fal model=${this.model}`, {\n provider: 'fal',\n model: this.model,\n })\n\n const resolved = resolveMediaPrompt(options.prompt)\n\n if (resolved.videos.length > 0) {\n throw new Error(\n `fal.generateImages does not support video prompt parts on model ${this.model}.`,\n )\n }\n if (resolved.audios.length > 0) {\n throw new Error(\n `fal.generateImages does not support audio prompt parts on model ${this.model}.`,\n )\n }\n\n let requestId: string | undefined\n try {\n const input = this.buildInput(options, resolved)\n // Pass the abortSignal per request, not in the client config, so it\n // cannot cancel other generations.\n const result = await this.client.subscribe(this.model, {\n input,\n ...(options.abortSignal ? { abortSignal: options.abortSignal } : {}),\n onEnqueue: (id) => {\n requestId = id\n },\n })\n return this.transformResponse(result)\n } catch (error) {\n // The SDK stops waiting on abort but leaves the queued job running.\n // Cancel it so it stops billing.\n if (requestId && options.abortSignal?.aborted) {\n this.client.queue.cancel(this.model, { requestId }).catch(() => {})\n }\n logger.errors('fal.generateImage fatal', {\n error,\n source: 'fal.generateImage',\n })\n throw error\n }\n }\n\n private buildInput(\n options: ImageGenerationOptions<\n FalImageProviderOptions<TModel>,\n FalModelImageSize<TModel>\n >,\n resolved: ResolvedMediaPrompt,\n ): FalModelInput<TModel> {\n const sizeParams = mapSizeToFalFormat(options.size)\n // Order matters: size and derived image-input fields first, then\n // modelOptions (so explicit user overrides win for mask_url /\n // control_image_url / reference_image_urls), then the call-controlled\n // prompt / num_images, which always take precedence.\n const inputFields = mapImageInputsToFalFields(this.model, resolved.images)\n const input = {\n ...sizeParams,\n ...inputFields,\n ...options.modelOptions,\n // Media-only prompts (e.g. upscalers, background removal) omit the\n // prompt field entirely rather than sending an empty string.\n ...(resolved.text ? { prompt: resolved.text } : {}),\n num_images: options.numberOfImages,\n } as FalModelInput<TModel>\n return input\n }\n\n protected override generateId(): string {\n return utilGenerateId(this.name)\n }\n\n private transformResponse(\n response: Result<OutputType<TModel>>,\n ): ImageGenerationResult {\n const images: Array<GeneratedImage> = []\n const data = response.data\n\n // Handle array of images (most models return { images: [...] })\n if ('images' in data && Array.isArray(data.images)) {\n for (const img of data.images) {\n images.push(this.parseImage(img))\n }\n }\n // Handle single image response (some models return { image: {...} })\n else if ('image' in data && data.image && typeof data.image === 'object') {\n images.push(this.parseImage(data.image))\n }\n\n if (images.length === 0) {\n throw new Error(\n 'Unexpected fal image response shape. Expected images[] or image{}. Got keys: ' +\n Object.keys(data).join(','),\n )\n }\n\n const usage = buildFalUsage(takeBillableUnits(response.requestId))\n\n return {\n id: response.requestId || this.generateId(),\n model: this.model,\n images,\n ...(usage ? { usage } : {}),\n }\n }\n\n private parseImage(img: unknown): GeneratedImage {\n let url: string\n if (typeof img === 'string') {\n url = img\n } else if (\n img &&\n typeof img === 'object' &&\n 'url' in img &&\n typeof img.url === 'string'\n ) {\n url = (img as { url: string }).url\n } else {\n throw new Error(\n `Invalid image payload from fal response: expected string or { url: string }, received ${\n img === null ? 'null' : typeof img\n }`,\n )\n }\n\n if (url.startsWith('data:')) {\n const base64Match = url.match(/^data:image\\/[^;]+;base64,(.+)$/)\n if (base64Match && base64Match[1]) {\n return { b64Json: base64Match[1] }\n }\n }\n return { url }\n }\n}\n\nexport function createFalImage<TModel extends FalModel>(\n model: TModel,\n config?: FalClientConfig,\n): FalImageAdapter<TModel> {\n return new FalImageAdapter(model, config)\n}\n\nexport function falImage<TModel extends FalModel>(\n model: TModel,\n config?: FalClientConfig,\n): FalImageAdapter<TModel> {\n return createFalImage(model, config)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,IAAa,kBAAb,cAA8D,iBAM5D;CACA,OAAyB;CACzB,OAAgB;CAChB;CAEA,sBAAwC;CAExC,YAAY,OAAe,QAA0B;EACnD,MAAM,OAAO,CAAC,CAAC;EACf,KAAK,SAAS,uBAAuB,MAAM;CAC7C;CAEA,MAAM,eACJ,SAIgC;EAChC,MAAM,EAAE,WAAW;EAEnB,OAAO,QAAQ,6CAA6C,KAAK,SAAS;GACxE,UAAU;GACV,OAAO,KAAK;EACd,CAAC;EAED,MAAM,WAAW,mBAAmB,QAAQ,MAAM;EAElD,IAAI,SAAS,OAAO,SAAS,GAC3B,MAAM,IAAI,MACR,mEAAmE,KAAK,MAAM,EAChF;EAEF,IAAI,SAAS,OAAO,SAAS,GAC3B,MAAM,IAAI,MACR,mEAAmE,KAAK,MAAM,EAChF;EAGF,IAAI;EACJ,IAAI;GACF,MAAM,QAAQ,KAAK,WAAW,SAAS,QAAQ;GAG/C,MAAM,SAAS,MAAM,KAAK,OAAO,UAAU,KAAK,OAAO;IACrD;IACA,GAAI,QAAQ,cAAc,EAAE,aAAa,QAAQ,YAAY,IAAI,CAAC;IAClE,YAAY,OAAO;KACjB,YAAY;IACd;GACF,CAAC;GACD,OAAO,KAAK,kBAAkB,MAAM;EACtC,SAAS,OAAO;GAGd,IAAI,aAAa,QAAQ,aAAa,SACpC,KAAK,OAAO,MAAM,OAAO,KAAK,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;GAEpE,OAAO,OAAO,2BAA2B;IACvC;IACA,QAAQ;GACV,CAAC;GACD,MAAM;EACR;CACF;CAEA,WACE,SAIA,UACuB;EACvB,MAAM,aAAa,mBAAmB,QAAQ,IAAI;EAKlD,MAAM,cAAc,0BAA0B,KAAK,OAAO,SAAS,MAAM;EAUzE,OAAO;GARL,GAAG;GACH,GAAG;GACH,GAAG,QAAQ;GAGX,GAAI,SAAS,OAAO,EAAE,QAAQ,SAAS,KAAK,IAAI,CAAC;GACjD,YAAY,QAAQ;EAEf;CACT;CAEA,aAAwC;EACtC,
|
|
1
|
+
{"version":3,"file":"image.js","names":["utilGenerateId"],"sources":["../../../src/adapters/image.ts"],"sourcesContent":["import { resolveMediaPrompt } from '@tanstack/ai'\nimport { BaseImageAdapter } from '@tanstack/ai/adapters'\nimport {\n createAdapterFalClient,\n generateId as utilGenerateId,\n} from '../utils/client'\nimport { buildFalUsage, takeBillableUnits } from '../utils/billing'\nimport { mapSizeToFalFormat } from '../image/image-provider-options'\nimport { mapImageInputsToFalFields } from '../image/image-inputs'\nimport type { OutputType, Result } from '@fal-ai/client'\nimport type { FalClientConfig } from '../utils/client'\nimport type { FalClient } from '../client/index'\nimport type {\n GeneratedImage,\n ImageGenerationOptions,\n ImageGenerationResult,\n ResolvedMediaPrompt,\n} from '@tanstack/ai'\nimport type {\n FalImagePromptModalitiesFor,\n FalImageProviderOptions,\n FalModel,\n FalModelImageSize,\n FalModelInput,\n} from '../model-meta'\n\n/**\n * fal.ai image generation adapter with full type inference.\n *\n * Uses fal.ai's comprehensive type system to provide autocomplete\n *\n * and type safety for all 600+ supported models.\n *\n * @example\n * ```typescript\n * const adapter = falImage('fal-ai/flux/dev')\n * const result = await adapter.generateImages({\n * model: 'fal-ai/flux/dev',\n * prompt: 'a cat',\n * modelOptions: {\n * num_inference_steps: 28, // Type-safe! Autocomplete works\n * guidance_scale: 3.5,\n * },\n * })\n * ```\n */\nexport class FalImageAdapter<TModel extends FalModel> extends BaseImageAdapter<\n TModel,\n FalImageProviderOptions<TModel>,\n Record<TModel, FalImageProviderOptions<TModel>>,\n Record<TModel, FalModelImageSize<TModel>>,\n Record<TModel, FalImagePromptModalitiesFor<TModel>>\n> {\n override readonly kind = 'image' as const\n readonly name = 'fal' as const\n private readonly client: FalClient\n // Consumes fal storage URLs uploaded via falFiles().\n override readonly supportsFileSources = true\n\n constructor(model: TModel, config?: FalClientConfig) {\n super(model, {})\n this.client = createAdapterFalClient(config)\n }\n\n async generateImages(\n options: ImageGenerationOptions<\n FalImageProviderOptions<TModel>,\n FalModelImageSize<TModel>\n >,\n ): Promise<ImageGenerationResult> {\n const { logger } = options\n\n logger.request(`activity=generateImage provider=fal model=${this.model}`, {\n provider: 'fal',\n model: this.model,\n })\n\n const resolved = resolveMediaPrompt(options.prompt)\n\n if (resolved.videos.length > 0) {\n throw new Error(\n `fal.generateImages does not support video prompt parts on model ${this.model}.`,\n )\n }\n if (resolved.audios.length > 0) {\n throw new Error(\n `fal.generateImages does not support audio prompt parts on model ${this.model}.`,\n )\n }\n\n let requestId: string | undefined\n try {\n const input = this.buildInput(options, resolved)\n // Pass the abortSignal per request, not in the client config, so it\n // cannot cancel other generations.\n const result = await this.client.subscribe(this.model, {\n input,\n ...(options.abortSignal ? { abortSignal: options.abortSignal } : {}),\n onEnqueue: (id) => {\n requestId = id\n },\n })\n return this.transformResponse(result)\n } catch (error) {\n // The SDK stops waiting on abort but leaves the queued job running.\n // Cancel it so it stops billing.\n if (requestId && options.abortSignal?.aborted) {\n this.client.queue.cancel(this.model, { requestId }).catch(() => {})\n }\n logger.errors('fal.generateImage fatal', {\n error,\n source: 'fal.generateImage',\n })\n throw error\n }\n }\n\n private buildInput(\n options: ImageGenerationOptions<\n FalImageProviderOptions<TModel>,\n FalModelImageSize<TModel>\n >,\n resolved: ResolvedMediaPrompt,\n ): FalModelInput<TModel> {\n const sizeParams = mapSizeToFalFormat(options.size)\n // Order matters: size and derived image-input fields first, then\n // modelOptions (so explicit user overrides win for mask_url /\n // control_image_url / reference_image_urls), then the call-controlled\n // prompt / num_images, which always take precedence.\n const inputFields = mapImageInputsToFalFields(this.model, resolved.images)\n const input = {\n ...sizeParams,\n ...inputFields,\n ...options.modelOptions,\n // Media-only prompts (e.g. upscalers, background removal) omit the\n // prompt field entirely rather than sending an empty string.\n ...(resolved.text ? { prompt: resolved.text } : {}),\n num_images: options.numberOfImages,\n } as FalModelInput<TModel>\n return input\n }\n\n protected override generateId(): string {\n return utilGenerateId(this.name)\n }\n\n private transformResponse(\n response: Result<OutputType<TModel>>,\n ): ImageGenerationResult {\n const images: Array<GeneratedImage> = []\n const data = response.data\n\n // Handle array of images (most models return { images: [...] })\n if ('images' in data && Array.isArray(data.images)) {\n for (const img of data.images) {\n images.push(this.parseImage(img))\n }\n }\n // Handle single image response (some models return { image: {...} })\n else if ('image' in data && data.image && typeof data.image === 'object') {\n images.push(this.parseImage(data.image))\n }\n\n if (images.length === 0) {\n throw new Error(\n 'Unexpected fal image response shape. Expected images[] or image{}. Got keys: ' +\n Object.keys(data).join(','),\n )\n }\n\n const usage = buildFalUsage(takeBillableUnits(response.requestId))\n\n return {\n id: response.requestId || this.generateId(),\n model: this.model,\n images,\n ...(usage ? { usage } : {}),\n }\n }\n\n private parseImage(img: unknown): GeneratedImage {\n let url: string\n if (typeof img === 'string') {\n url = img\n } else if (\n img &&\n typeof img === 'object' &&\n 'url' in img &&\n typeof img.url === 'string'\n ) {\n url = (img as { url: string }).url\n } else {\n throw new Error(\n `Invalid image payload from fal response: expected string or { url: string }, received ${\n img === null ? 'null' : typeof img\n }`,\n )\n }\n\n if (url.startsWith('data:')) {\n const base64Match = url.match(/^data:image\\/[^;]+;base64,(.+)$/)\n if (base64Match && base64Match[1]) {\n return { b64Json: base64Match[1] }\n }\n }\n return { url }\n }\n}\n\nexport function createFalImage<TModel extends FalModel>(\n model: TModel,\n config?: FalClientConfig,\n): FalImageAdapter<TModel> {\n return new FalImageAdapter(model, config)\n}\n\nexport function falImage<TModel extends FalModel>(\n model: TModel,\n config?: FalClientConfig,\n): FalImageAdapter<TModel> {\n return createFalImage(model, config)\n}\n"],"mappings":"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"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getFalApiKeyFromEnv } from "../utils/client.js";
|
|
2
2
|
import { BaseLiveVideoAdapter } from "@tanstack/ai/adapters";
|
|
3
3
|
import { generateId } from "@tanstack/ai-utils";
|
|
4
|
-
|
|
4
|
+
// src/adapters/live.ts
|
|
5
5
|
var DEFAULT_FAL_TOKEN_URL = "https://rest.fal.ai/tokens/";
|
|
6
6
|
var DEFAULT_TOKEN_DURATION_SECONDS = 300;
|
|
7
7
|
var FAL_LIVE_VIDEO_MODELS = ["minimax/h3-max/director"];
|
|
@@ -143,7 +143,6 @@ function createFalLiveVideo(model, apiKey, config) {
|
|
|
143
143
|
apiKey
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
|
-
//#endregion
|
|
147
146
|
export { FAL_LIVE_VIDEO_APP, FAL_LIVE_VIDEO_MODELS, FalLiveVideoAdapter, allowedFalLiveVideoProxyTarget, createFalLiveVideo, falLiveVideo, isFalLiveVideoModel };
|
|
148
147
|
|
|
149
148
|
//# sourceMappingURL=live.js.map
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { BaseModel3DAdapter } from '@tanstack/ai/adapters';
|
|
2
|
+
import { Model3DGenerationOptions } from '@tanstack/ai';
|
|
3
|
+
import { Fal3DModel, Fal3DModelProviderOptions, Fal3DPromptModalitiesFor } from '../model-meta.js';
|
|
4
|
+
import { FalClientConfig } from '../utils/client.js';
|
|
5
|
+
/**
|
|
6
|
+
* fal 3D model adapter. It runs any fal 3D endpoint (Hunyuan3D, TRELLIS,
|
|
7
|
+
* Rodin, Tripo, Meshy, SAM-3 3D, and others) as a queue job.
|
|
8
|
+
*
|
|
9
|
+
* The prompt text, image parts, and `model3d` parts go to the endpoint input
|
|
10
|
+
* fields. `modelOptions` is typed from the endpoint input, without those
|
|
11
|
+
* fields. The result has the best GLB in `url`, and every mesh file and
|
|
12
|
+
* texture file in `files`.
|
|
13
|
+
*
|
|
14
|
+
* @experimental 3D model generation is experimental and may change.
|
|
15
|
+
*/
|
|
16
|
+
export declare class Fal3DModelAdapter<TModel extends Fal3DModel> extends BaseModel3DAdapter<TModel, Fal3DModelProviderOptions<TModel>, Record<TModel, Fal3DModelProviderOptions<TModel>>, Record<TModel, Fal3DPromptModalitiesFor<TModel>>> {
|
|
17
|
+
readonly name: "fal";
|
|
18
|
+
readonly supportsFileSources = true;
|
|
19
|
+
private readonly client;
|
|
20
|
+
constructor(model: TModel, config?: FalClientConfig);
|
|
21
|
+
create3DModelJob(options: Model3DGenerationOptions<Fal3DModelProviderOptions<TModel>>): Promise<{
|
|
22
|
+
jobId: string;
|
|
23
|
+
model: TModel;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* Reads the queue status. fal has no failed queue status: a failed job is
|
|
27
|
+
* `COMPLETED` with an `error` message, and this maps it to `failed`.
|
|
28
|
+
*/
|
|
29
|
+
get3DModelStatus(jobId: string, options?: {
|
|
30
|
+
abortSignal?: AbortSignal;
|
|
31
|
+
}): Promise<{
|
|
32
|
+
jobId: string;
|
|
33
|
+
status: "pending";
|
|
34
|
+
error?: undefined;
|
|
35
|
+
} | {
|
|
36
|
+
jobId: string;
|
|
37
|
+
status: "processing";
|
|
38
|
+
error?: undefined;
|
|
39
|
+
} | {
|
|
40
|
+
jobId: string;
|
|
41
|
+
status: "failed";
|
|
42
|
+
error: string;
|
|
43
|
+
} | {
|
|
44
|
+
jobId: string;
|
|
45
|
+
status: "completed";
|
|
46
|
+
error?: undefined;
|
|
47
|
+
}>;
|
|
48
|
+
/**
|
|
49
|
+
* Reads the finished job and maps it to one `Model3DResult`. A failed job
|
|
50
|
+
* throws an `Error` with the message from fal.
|
|
51
|
+
*/
|
|
52
|
+
get3DModel(jobId: string, options?: {
|
|
53
|
+
abortSignal?: AbortSignal;
|
|
54
|
+
}): Promise<{
|
|
55
|
+
usage?: import('@tanstack/ai-event-client').TokenUsage<import('@tanstack/ai-event-client').ProviderUsageDetails> | undefined;
|
|
56
|
+
previewImageUrl?: string | undefined;
|
|
57
|
+
jobId: string;
|
|
58
|
+
url: string;
|
|
59
|
+
format: "glb" | "obj" | "fbx" | "usdz" | "stl" | "gltf";
|
|
60
|
+
files: import('@tanstack/ai').Model3DFile[];
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* Cancels a queued job with the fal queue cancel endpoint. fal can refuse
|
|
64
|
+
* when the job already runs, and then this rejects.
|
|
65
|
+
*/
|
|
66
|
+
cancel3DModelJob(jobId: string, options?: {
|
|
67
|
+
abortSignal?: AbortSignal;
|
|
68
|
+
}): Promise<void>;
|
|
69
|
+
protected generateId(): string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Create a fal 3D model adapter. It reads the API key from `config.apiKey`,
|
|
73
|
+
* or from the `FAL_KEY` environment variable.
|
|
74
|
+
*
|
|
75
|
+
* @param model - A fal 3D endpoint id, for example
|
|
76
|
+
* `'fal-ai/hunyuan3d-v3/image-to-3d'`. Known ids autocomplete and type
|
|
77
|
+
* `modelOptions`. Other ids work untyped.
|
|
78
|
+
* @param config - Optional fal client config (API key, proxy URL, fetch).
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* import { generate3DModel } from '@tanstack/ai'
|
|
83
|
+
* import { fal3DModel } from '@tanstack/ai-fal'
|
|
84
|
+
*
|
|
85
|
+
* const { url, format } = await generate3DModel({
|
|
86
|
+
* adapter: fal3DModel('fal-ai/hunyuan3d-v3/image-to-3d'),
|
|
87
|
+
* threadId: 'thread-1',
|
|
88
|
+
* prompt: [
|
|
89
|
+
* { type: 'image', source: { type: 'url', value: 'https://example.com/chair.png' } },
|
|
90
|
+
* ],
|
|
91
|
+
* modelOptions: { face_count: 50000 },
|
|
92
|
+
* wait: true,
|
|
93
|
+
* })
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* @experimental 3D model generation is experimental and may change.
|
|
97
|
+
*/
|
|
98
|
+
export declare function fal3DModel<TModel extends Fal3DModel>(model: TModel, config?: FalClientConfig): Fal3DModelAdapter<TModel>;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { createAdapterFalClient, generateId } from "../utils/client.js";
|
|
2
|
+
import { buildFalUsage, takeBillableUnits } from "../utils/billing.js";
|
|
3
|
+
import { ApiError, ValidationError } from "../client/index.js";
|
|
4
|
+
import { mapModel3DPromptToFalFields } from "../model3d/model3d-inputs.js";
|
|
5
|
+
import { mapFalModel3DOutput } from "../model3d/model3d-output.js";
|
|
6
|
+
import { BaseModel3DAdapter } from "@tanstack/ai/adapters";
|
|
7
|
+
// src/adapters/model3d.ts
|
|
8
|
+
/**
|
|
9
|
+
* Read the message of a failed fal request. fal puts the reason in
|
|
10
|
+
* `body.detail`: a string, or a list of field errors for a 422.
|
|
11
|
+
*/
|
|
12
|
+
function falErrorMessage(error) {
|
|
13
|
+
if (error instanceof ApiError) {
|
|
14
|
+
const body = error.body;
|
|
15
|
+
const detail = typeof body === "object" && body !== null && "detail" in body ? body.detail : void 0;
|
|
16
|
+
if (typeof detail === "string") return detail;
|
|
17
|
+
if (error instanceof ValidationError && error.fieldErrors.length > 0) return error.fieldErrors.map(({ loc, msg }) => loc.length > 0 ? `${loc.join(".")}: ${msg}` : msg).join("; ");
|
|
18
|
+
}
|
|
19
|
+
return error instanceof Error ? error.message : String(error);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* fal 3D model adapter. It runs any fal 3D endpoint (Hunyuan3D, TRELLIS,
|
|
23
|
+
* Rodin, Tripo, Meshy, SAM-3 3D, and others) as a queue job.
|
|
24
|
+
*
|
|
25
|
+
* The prompt text, image parts, and `model3d` parts go to the endpoint input
|
|
26
|
+
* fields. `modelOptions` is typed from the endpoint input, without those
|
|
27
|
+
* fields. The result has the best GLB in `url`, and every mesh file and
|
|
28
|
+
* texture file in `files`.
|
|
29
|
+
*
|
|
30
|
+
* @experimental 3D model generation is experimental and may change.
|
|
31
|
+
*/
|
|
32
|
+
var Fal3DModelAdapter = class extends BaseModel3DAdapter {
|
|
33
|
+
name = "fal";
|
|
34
|
+
supportsFileSources = true;
|
|
35
|
+
client;
|
|
36
|
+
constructor(model, config) {
|
|
37
|
+
super({}, model);
|
|
38
|
+
this.client = createAdapterFalClient(config);
|
|
39
|
+
}
|
|
40
|
+
async create3DModelJob(options) {
|
|
41
|
+
const { modelOptions, logger } = options;
|
|
42
|
+
logger.request(`activity=generate3DModel provider=fal model=${this.model}`, {
|
|
43
|
+
provider: "fal",
|
|
44
|
+
model: this.model
|
|
45
|
+
});
|
|
46
|
+
try {
|
|
47
|
+
const endpoint = this.model;
|
|
48
|
+
const promptFields = mapModel3DPromptToFalFields(this.model, options.prompt);
|
|
49
|
+
const { request_id } = await this.client.queue.submit(endpoint, {
|
|
50
|
+
input: {
|
|
51
|
+
...modelOptions,
|
|
52
|
+
...promptFields
|
|
53
|
+
},
|
|
54
|
+
...options.abortSignal ? { abortSignal: options.abortSignal } : {}
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
jobId: request_id,
|
|
58
|
+
model: this.model
|
|
59
|
+
};
|
|
60
|
+
} catch (error) {
|
|
61
|
+
logger.errors("fal.create3DModelJob fatal", {
|
|
62
|
+
error,
|
|
63
|
+
source: "fal.create3DModelJob"
|
|
64
|
+
});
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Reads the queue status. fal has no failed queue status: a failed job is
|
|
70
|
+
* `COMPLETED` with an `error` message, and this maps it to `failed`.
|
|
71
|
+
*/
|
|
72
|
+
async get3DModelStatus(jobId, options) {
|
|
73
|
+
const endpoint = this.model;
|
|
74
|
+
const falStatus = await this.client.queue.status(endpoint, {
|
|
75
|
+
requestId: jobId,
|
|
76
|
+
...options?.abortSignal ? { abortSignal: options.abortSignal } : {}
|
|
77
|
+
});
|
|
78
|
+
switch (falStatus.status) {
|
|
79
|
+
case "IN_QUEUE": return {
|
|
80
|
+
jobId,
|
|
81
|
+
status: "pending"
|
|
82
|
+
};
|
|
83
|
+
case "IN_PROGRESS": return {
|
|
84
|
+
jobId,
|
|
85
|
+
status: "processing"
|
|
86
|
+
};
|
|
87
|
+
case "COMPLETED": {
|
|
88
|
+
const error = "error" in falStatus && typeof falStatus.error === "string" ? falStatus.error : void 0;
|
|
89
|
+
return error ? {
|
|
90
|
+
jobId,
|
|
91
|
+
status: "failed",
|
|
92
|
+
error
|
|
93
|
+
} : {
|
|
94
|
+
jobId,
|
|
95
|
+
status: "completed"
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
default: throw new Error(`fal: unknown queue status ${JSON.stringify(falStatus)}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Reads the finished job and maps it to one `Model3DResult`. A failed job
|
|
103
|
+
* throws an `Error` with the message from fal.
|
|
104
|
+
*/
|
|
105
|
+
async get3DModel(jobId, options) {
|
|
106
|
+
const endpoint = this.model;
|
|
107
|
+
let result;
|
|
108
|
+
try {
|
|
109
|
+
result = await this.client.queue.result(endpoint, {
|
|
110
|
+
requestId: jobId,
|
|
111
|
+
...options?.abortSignal ? { abortSignal: options.abortSignal } : {}
|
|
112
|
+
});
|
|
113
|
+
} catch (error) {
|
|
114
|
+
if (options?.abortSignal?.aborted) throw error;
|
|
115
|
+
throw new Error(`fal: 3D model job ${jobId} on ${this.model} failed: ${falErrorMessage(error)}`);
|
|
116
|
+
}
|
|
117
|
+
const usage = buildFalUsage(takeBillableUnits(result.requestId));
|
|
118
|
+
return {
|
|
119
|
+
...mapFalModel3DOutput(jobId, result.data),
|
|
120
|
+
...usage ? { usage } : {}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Cancels a queued job with the fal queue cancel endpoint. fal can refuse
|
|
125
|
+
* when the job already runs, and then this rejects.
|
|
126
|
+
*/
|
|
127
|
+
async cancel3DModelJob(jobId, options) {
|
|
128
|
+
const endpoint = this.model;
|
|
129
|
+
await this.client.queue.cancel(endpoint, {
|
|
130
|
+
requestId: jobId,
|
|
131
|
+
...options?.abortSignal ? { abortSignal: options.abortSignal } : {}
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
generateId() {
|
|
135
|
+
return generateId(this.name);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* Create a fal 3D model adapter. It reads the API key from `config.apiKey`,
|
|
140
|
+
* or from the `FAL_KEY` environment variable.
|
|
141
|
+
*
|
|
142
|
+
* @param model - A fal 3D endpoint id, for example
|
|
143
|
+
* `'fal-ai/hunyuan3d-v3/image-to-3d'`. Known ids autocomplete and type
|
|
144
|
+
* `modelOptions`. Other ids work untyped.
|
|
145
|
+
* @param config - Optional fal client config (API key, proxy URL, fetch).
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { generate3DModel } from '@tanstack/ai'
|
|
150
|
+
* import { fal3DModel } from '@tanstack/ai-fal'
|
|
151
|
+
*
|
|
152
|
+
* const { url, format } = await generate3DModel({
|
|
153
|
+
* adapter: fal3DModel('fal-ai/hunyuan3d-v3/image-to-3d'),
|
|
154
|
+
* threadId: 'thread-1',
|
|
155
|
+
* prompt: [
|
|
156
|
+
* { type: 'image', source: { type: 'url', value: 'https://example.com/chair.png' } },
|
|
157
|
+
* ],
|
|
158
|
+
* modelOptions: { face_count: 50000 },
|
|
159
|
+
* wait: true,
|
|
160
|
+
* })
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* @experimental 3D model generation is experimental and may change.
|
|
164
|
+
*/
|
|
165
|
+
function fal3DModel(model, config) {
|
|
166
|
+
return new Fal3DModelAdapter(model, config);
|
|
167
|
+
}
|
|
168
|
+
export { Fal3DModelAdapter, fal3DModel };
|
|
169
|
+
|
|
170
|
+
//# sourceMappingURL=model3d.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model3d.js","names":["unhandled","utilGenerateId"],"sources":["../../../src/adapters/model3d.ts"],"sourcesContent":["import { BaseModel3DAdapter } from '@tanstack/ai/adapters'\nimport {\n createAdapterFalClient,\n generateId as utilGenerateId,\n} from '../utils/client'\nimport { buildFalUsage, takeBillableUnits } from '../utils/billing'\nimport { ApiError, ValidationError } from '../client/index'\nimport { mapModel3DPromptToFalFields } from '../model3d/model3d-inputs'\nimport { mapFalModel3DOutput } from '../model3d/model3d-output'\nimport type { Model3DGenerationOptions } from '@tanstack/ai'\nimport type {\n Fal3DModel,\n Fal3DModelProviderOptions,\n Fal3DPromptModalitiesFor,\n} from '../model-meta'\nimport type { FalClientConfig } from '../utils/client'\nimport type { FalClient } from '../client/index'\n\n/**\n * Read the message of a failed fal request. fal puts the reason in\n * `body.detail`: a string, or a list of field errors for a 422.\n */\nfunction falErrorMessage(error: unknown) {\n if (error instanceof ApiError) {\n const body: unknown = error.body\n const detail =\n typeof body === 'object' && body !== null && 'detail' in body\n ? body.detail\n : undefined\n if (typeof detail === 'string') return detail\n if (error instanceof ValidationError && error.fieldErrors.length > 0) {\n return error.fieldErrors\n .map(({ loc, msg }) =>\n loc.length > 0 ? `${loc.join('.')}: ${msg}` : msg,\n )\n .join('; ')\n }\n }\n return error instanceof Error ? error.message : String(error)\n}\n\n/**\n * fal 3D model adapter. It runs any fal 3D endpoint (Hunyuan3D, TRELLIS,\n * Rodin, Tripo, Meshy, SAM-3 3D, and others) as a queue job.\n *\n * The prompt text, image parts, and `model3d` parts go to the endpoint input\n * fields. `modelOptions` is typed from the endpoint input, without those\n * fields. The result has the best GLB in `url`, and every mesh file and\n * texture file in `files`.\n *\n * @experimental 3D model generation is experimental and may change.\n */\nexport class Fal3DModelAdapter<\n TModel extends Fal3DModel,\n> extends BaseModel3DAdapter<\n TModel,\n Fal3DModelProviderOptions<TModel>,\n Record<TModel, Fal3DModelProviderOptions<TModel>>,\n Record<TModel, Fal3DPromptModalitiesFor<TModel>>\n> {\n readonly name = 'fal' as const\n // Consumes fal storage URLs uploaded via falFiles().\n override readonly supportsFileSources = true\n private readonly client: FalClient\n\n constructor(model: TModel, config?: FalClientConfig) {\n super({}, model)\n this.client = createAdapterFalClient(config)\n }\n\n async create3DModelJob(\n options: Model3DGenerationOptions<Fal3DModelProviderOptions<TModel>>,\n ) {\n const { modelOptions, logger } = options\n\n logger.request(\n `activity=generate3DModel provider=fal model=${this.model}`,\n { provider: 'fal', model: this.model },\n )\n\n try {\n // A plain string id, so the fal client takes any input object.\n const endpoint: string = this.model\n const promptFields = mapModel3DPromptToFalFields(\n this.model,\n options.prompt,\n )\n // Submit to the queue. Pass the abortSignal per request, not in the\n // client config, so it cannot cancel other jobs.\n const { request_id } = await this.client.queue.submit(endpoint, {\n input: { ...modelOptions, ...promptFields },\n ...(options.abortSignal ? { abortSignal: options.abortSignal } : {}),\n })\n return { jobId: request_id, model: this.model }\n } catch (error) {\n logger.errors('fal.create3DModelJob fatal', {\n error,\n source: 'fal.create3DModelJob',\n })\n throw error\n }\n }\n\n /**\n * Reads the queue status. fal has no failed queue status: a failed job is\n * `COMPLETED` with an `error` message, and this maps it to `failed`.\n */\n async get3DModelStatus(\n jobId: string,\n options?: { abortSignal?: AbortSignal },\n ) {\n const endpoint: string = this.model\n const falStatus = await this.client.queue.status(endpoint, {\n requestId: jobId,\n ...(options?.abortSignal ? { abortSignal: options.abortSignal } : {}),\n })\n switch (falStatus.status) {\n case 'IN_QUEUE':\n return { jobId, status: 'pending' as const }\n case 'IN_PROGRESS':\n return { jobId, status: 'processing' as const }\n case 'COMPLETED': {\n const error =\n 'error' in falStatus && typeof falStatus.error === 'string'\n ? falStatus.error\n : undefined\n return error\n ? { jobId, status: 'failed' as const, error }\n : { jobId, status: 'completed' as const }\n }\n default: {\n const unhandled: never = falStatus\n throw new Error(\n `fal: unknown queue status ${JSON.stringify(unhandled)}`,\n )\n }\n }\n }\n\n /**\n * Reads the finished job and maps it to one `Model3DResult`. A failed job\n * throws an `Error` with the message from fal.\n */\n async get3DModel(jobId: string, options?: { abortSignal?: AbortSignal }) {\n const endpoint: string = this.model\n let result\n try {\n result = await this.client.queue.result(endpoint, {\n requestId: jobId,\n ...(options?.abortSignal ? { abortSignal: options.abortSignal } : {}),\n })\n } catch (error: unknown) {\n // A cancelled call is not a provider failure: keep the abort error.\n if (options?.abortSignal?.aborted) throw error\n throw new Error(\n `fal: 3D model job ${jobId} on ${this.model} failed: ${falErrorMessage(error)}`,\n )\n }\n\n const usage = buildFalUsage(takeBillableUnits(result.requestId))\n return {\n ...mapFalModel3DOutput(jobId, result.data),\n ...(usage ? { usage } : {}),\n }\n }\n\n /**\n * Cancels a queued job with the fal queue cancel endpoint. fal can refuse\n * when the job already runs, and then this rejects.\n */\n async cancel3DModelJob(\n jobId: string,\n options?: { abortSignal?: AbortSignal },\n ) {\n const endpoint: string = this.model\n await this.client.queue.cancel(endpoint, {\n requestId: jobId,\n ...(options?.abortSignal ? { abortSignal: options.abortSignal } : {}),\n })\n }\n\n protected override generateId() {\n return utilGenerateId(this.name)\n }\n}\n\n/**\n * Create a fal 3D model adapter. It reads the API key from `config.apiKey`,\n * or from the `FAL_KEY` environment variable.\n *\n * @param model - A fal 3D endpoint id, for example\n * `'fal-ai/hunyuan3d-v3/image-to-3d'`. Known ids autocomplete and type\n * `modelOptions`. Other ids work untyped.\n * @param config - Optional fal client config (API key, proxy URL, fetch).\n *\n * @example\n * ```ts\n * import { generate3DModel } from '@tanstack/ai'\n * import { fal3DModel } from '@tanstack/ai-fal'\n *\n * const { url, format } = await generate3DModel({\n * adapter: fal3DModel('fal-ai/hunyuan3d-v3/image-to-3d'),\n * threadId: 'thread-1',\n * prompt: [\n * { type: 'image', source: { type: 'url', value: 'https://example.com/chair.png' } },\n * ],\n * modelOptions: { face_count: 50000 },\n * wait: true,\n * })\n * ```\n *\n * @experimental 3D model generation is experimental and may change.\n */\nexport function fal3DModel<TModel extends Fal3DModel>(\n model: TModel,\n config?: FalClientConfig,\n) {\n return new Fal3DModelAdapter(model, config)\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { BaseMotionAdapter } from '@tanstack/ai/adapters';
|
|
2
|
+
import { MotionGenerationOptions, MotionGenerationResult, MotionJobResult, MotionStatusResult } from '@tanstack/ai';
|
|
3
|
+
import { FalModelInput } from '../model-meta.js';
|
|
4
|
+
import { FalClientConfig } from '../utils/client.js';
|
|
5
|
+
/**
|
|
6
|
+
* fal motion models. Both Hunyuan Motion endpoints return an FBX file and
|
|
7
|
+
* take the same input. Any other fal endpoint id is accepted as a string.
|
|
8
|
+
*/
|
|
9
|
+
export type FalMotionModel = 'fal-ai/hunyuan-motion' | 'fal-ai/hunyuan-motion/fast' | (string & {});
|
|
10
|
+
/**
|
|
11
|
+
* Hunyuan Motion options: `seed`, `guidance_scale` (1 to 10, default 5), and
|
|
12
|
+
* `output_format` (`'fbx'` default, or `'dict'` for raw JSON). `prompt` and
|
|
13
|
+
* `duration` are top-level `generateMotion()` options.
|
|
14
|
+
*/
|
|
15
|
+
export type FalHunyuanMotionProviderOptions = Omit<FalModelInput<'fal-ai/hunyuan-motion'>, 'prompt' | 'duration'>;
|
|
16
|
+
/** Provider options for a fal motion model. */
|
|
17
|
+
export type FalMotionProviderOptions<TModel extends string> = TModel extends 'fal-ai/hunyuan-motion' | 'fal-ai/hunyuan-motion/fast' ? FalHunyuanMotionProviderOptions : Record<string, unknown>;
|
|
18
|
+
/**
|
|
19
|
+
* fal motion adapter. Runs text-to-motion models on the fal queue and
|
|
20
|
+
* returns the skeletal animation files they produce.
|
|
21
|
+
*
|
|
22
|
+
* fal file URLs follow the account's media expiry setting, so copy files
|
|
23
|
+
* you want to keep.
|
|
24
|
+
*
|
|
25
|
+
* @experimental Motion generation is an experimental feature and may change.
|
|
26
|
+
*/
|
|
27
|
+
export declare class FalMotionAdapter<TModel extends FalMotionModel> extends BaseMotionAdapter<TModel, FalMotionProviderOptions<TModel>> {
|
|
28
|
+
readonly name: "fal";
|
|
29
|
+
private readonly client;
|
|
30
|
+
constructor(model: TModel, config?: FalClientConfig);
|
|
31
|
+
createMotionJob(options: MotionGenerationOptions<FalMotionProviderOptions<TModel>>): Promise<MotionJobResult>;
|
|
32
|
+
getMotionStatus(jobId: string, options?: {
|
|
33
|
+
abortSignal?: AbortSignal;
|
|
34
|
+
}): Promise<MotionStatusResult>;
|
|
35
|
+
getMotion(jobId: string, options?: {
|
|
36
|
+
abortSignal?: AbortSignal;
|
|
37
|
+
}): Promise<MotionGenerationResult>;
|
|
38
|
+
/**
|
|
39
|
+
* Uses the fal queue cancel endpoint. fal removes a queued request. A
|
|
40
|
+
* request that already runs gets a cancel request and may still finish.
|
|
41
|
+
* fal rejects a cancel for a request that already completed.
|
|
42
|
+
*/
|
|
43
|
+
cancelMotionJob(jobId: string, options?: {
|
|
44
|
+
abortSignal?: AbortSignal;
|
|
45
|
+
}): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Create a fal motion adapter. Reads `config.apiKey`, or `FAL_KEY` from the
|
|
49
|
+
* environment.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const adapter = falMotion('fal-ai/hunyuan-motion')
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @experimental Motion generation is an experimental feature and may change.
|
|
57
|
+
*/
|
|
58
|
+
export declare function falMotion<TModel extends FalMotionModel>(model: TModel, config?: FalClientConfig): FalMotionAdapter<TModel>;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { createAdapterFalClient, extractUrlExtension, falValidationMessage } from "../utils/client.js";
|
|
2
|
+
import { buildFalUsage, takeBillableUnits } from "../utils/billing.js";
|
|
3
|
+
import { BaseMotionAdapter } from "@tanstack/ai/adapters";
|
|
4
|
+
// src/adapters/motion.ts
|
|
5
|
+
function isFalFile(value) {
|
|
6
|
+
return typeof value === "object" && value !== null && "url" in value && typeof value.url === "string";
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Every file in a fal motion output becomes an asset. The format comes from
|
|
10
|
+
* the file name or URL extension, then from the field name
|
|
11
|
+
* (`fbx_file` → `fbx`, `motion_json` → `json`).
|
|
12
|
+
*/
|
|
13
|
+
function toMotionAssets(output) {
|
|
14
|
+
const assets = [];
|
|
15
|
+
for (const [field, value] of Object.entries(output)) {
|
|
16
|
+
if (!isFalFile(value)) continue;
|
|
17
|
+
const format = extractUrlExtension(value.file_name ?? value.url)?.toLowerCase() ?? (field === "motion_json" ? "json" : field.replace(/_file$/, ""));
|
|
18
|
+
assets.push({
|
|
19
|
+
url: value.url,
|
|
20
|
+
format,
|
|
21
|
+
...value.content_type ? { contentType: value.content_type } : {},
|
|
22
|
+
...value.file_name ? { fileName: value.file_name } : {},
|
|
23
|
+
...value.file_size !== void 0 ? { fileSize: value.file_size } : {}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return assets;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* fal motion adapter. Runs text-to-motion models on the fal queue and
|
|
30
|
+
* returns the skeletal animation files they produce.
|
|
31
|
+
*
|
|
32
|
+
* fal file URLs follow the account's media expiry setting, so copy files
|
|
33
|
+
* you want to keep.
|
|
34
|
+
*
|
|
35
|
+
* @experimental Motion generation is an experimental feature and may change.
|
|
36
|
+
*/
|
|
37
|
+
var FalMotionAdapter = class extends BaseMotionAdapter {
|
|
38
|
+
name = "fal";
|
|
39
|
+
client;
|
|
40
|
+
constructor(model, config) {
|
|
41
|
+
super(model);
|
|
42
|
+
this.client = createAdapterFalClient(config);
|
|
43
|
+
}
|
|
44
|
+
async createMotionJob(options) {
|
|
45
|
+
const { logger } = options;
|
|
46
|
+
logger.request(`activity=generateMotion provider=fal model=${this.model}`, {
|
|
47
|
+
provider: "fal",
|
|
48
|
+
model: this.model
|
|
49
|
+
});
|
|
50
|
+
try {
|
|
51
|
+
const { request_id } = await this.client.queue.submit(this.model, {
|
|
52
|
+
input: {
|
|
53
|
+
...options.modelOptions,
|
|
54
|
+
prompt: options.prompt,
|
|
55
|
+
...options.duration !== void 0 ? { duration: options.duration } : {}
|
|
56
|
+
},
|
|
57
|
+
...options.abortSignal ? { abortSignal: options.abortSignal } : {}
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
jobId: request_id,
|
|
61
|
+
model: this.model
|
|
62
|
+
};
|
|
63
|
+
} catch (error) {
|
|
64
|
+
logger.errors("fal.createMotionJob fatal", {
|
|
65
|
+
error,
|
|
66
|
+
source: "fal.createMotionJob"
|
|
67
|
+
});
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async getMotionStatus(jobId, options) {
|
|
72
|
+
const response = await this.client.queue.status(this.model, {
|
|
73
|
+
requestId: jobId,
|
|
74
|
+
logs: false,
|
|
75
|
+
...options?.abortSignal ? { abortSignal: options.abortSignal } : {}
|
|
76
|
+
});
|
|
77
|
+
switch (response.status) {
|
|
78
|
+
case "IN_QUEUE": return {
|
|
79
|
+
jobId,
|
|
80
|
+
status: "pending",
|
|
81
|
+
...response.queue_position != null ? { queuePosition: response.queue_position } : {}
|
|
82
|
+
};
|
|
83
|
+
case "IN_PROGRESS": return {
|
|
84
|
+
jobId,
|
|
85
|
+
status: "processing"
|
|
86
|
+
};
|
|
87
|
+
case "COMPLETED": return response.error ? {
|
|
88
|
+
jobId,
|
|
89
|
+
status: "failed",
|
|
90
|
+
error: response.error
|
|
91
|
+
} : {
|
|
92
|
+
jobId,
|
|
93
|
+
status: "completed"
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async getMotion(jobId, options) {
|
|
98
|
+
let result;
|
|
99
|
+
try {
|
|
100
|
+
result = await this.client.queue.result(this.model, {
|
|
101
|
+
requestId: jobId,
|
|
102
|
+
...options?.abortSignal ? { abortSignal: options.abortSignal } : {}
|
|
103
|
+
});
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (options?.abortSignal?.aborted) throw error;
|
|
106
|
+
const validation = falValidationMessage(error);
|
|
107
|
+
if (validation) throw new Error(`Motion generation failed: ${validation}`);
|
|
108
|
+
throw error;
|
|
109
|
+
}
|
|
110
|
+
const output = result.data ?? {};
|
|
111
|
+
const assets = toMotionAssets(output);
|
|
112
|
+
if (assets.length === 0) throw new Error(`fal returned no motion files for request ${jobId}`);
|
|
113
|
+
const usage = buildFalUsage(takeBillableUnits(result.requestId));
|
|
114
|
+
return {
|
|
115
|
+
jobId,
|
|
116
|
+
model: this.model,
|
|
117
|
+
assets,
|
|
118
|
+
...typeof output.seed === "number" ? { seed: output.seed } : {},
|
|
119
|
+
...usage ? { usage } : {}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Uses the fal queue cancel endpoint. fal removes a queued request. A
|
|
124
|
+
* request that already runs gets a cancel request and may still finish.
|
|
125
|
+
* fal rejects a cancel for a request that already completed.
|
|
126
|
+
*/
|
|
127
|
+
async cancelMotionJob(jobId, options) {
|
|
128
|
+
await this.client.queue.cancel(this.model, {
|
|
129
|
+
requestId: jobId,
|
|
130
|
+
...options?.abortSignal ? { abortSignal: options.abortSignal } : {}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* Create a fal motion adapter. Reads `config.apiKey`, or `FAL_KEY` from the
|
|
136
|
+
* environment.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* const adapter = falMotion('fal-ai/hunyuan-motion')
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* @experimental Motion generation is an experimental feature and may change.
|
|
144
|
+
*/
|
|
145
|
+
function falMotion(model, config) {
|
|
146
|
+
return new FalMotionAdapter(model, config);
|
|
147
|
+
}
|
|
148
|
+
export { FalMotionAdapter, falMotion };
|
|
149
|
+
|
|
150
|
+
//# sourceMappingURL=motion.js.map
|