@tanstack/ai-fal 0.6.17 → 0.7.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.d.ts +32 -0
- package/dist/esm/adapters/audio.js +80 -0
- package/dist/esm/adapters/audio.js.map +1 -0
- package/dist/esm/adapters/image.js +18 -7
- package/dist/esm/adapters/image.js.map +1 -1
- package/dist/esm/adapters/speech.d.ts +32 -0
- package/dist/esm/adapters/speech.js +84 -0
- package/dist/esm/adapters/speech.js.map +1 -0
- package/dist/esm/adapters/transcription.d.ts +32 -0
- package/dist/esm/adapters/transcription.js +90 -0
- package/dist/esm/adapters/transcription.js.map +1 -0
- package/dist/esm/image/image-provider-options.js +10 -6
- package/dist/esm/image/image-provider-options.js.map +1 -1
- package/dist/esm/index.d.ts +4 -1
- package/dist/esm/index.js +9 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/model-meta.d.ts +15 -0
- package/dist/esm/utils/client.d.ts +31 -0
- package/dist/esm/utils/client.js +79 -13
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/index.d.ts +1 -1
- package/package.json +9 -4
- package/src/adapters/audio.ts +148 -0
- package/src/adapters/image.ts +29 -9
- package/src/adapters/speech.ts +147 -0
- package/src/adapters/transcription.ts +169 -0
- package/src/image/image-provider-options.ts +10 -6
- package/src/index.ts +24 -0
- package/src/model-meta.ts +27 -0
- package/src/utils/client.ts +121 -13
- package/src/utils/index.ts +4 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BaseAudioAdapter } from '@tanstack/ai/adapters';
|
|
2
|
+
import { AudioGenerationOptions, AudioGenerationResult } from '@tanstack/ai';
|
|
3
|
+
import { FalClientConfig } from '../utils.js';
|
|
4
|
+
import { FalModel, FalModelInput } from '../model-meta.js';
|
|
5
|
+
/**
|
|
6
|
+
* Provider options for audio generation, excluding fields TanStack AI handles.
|
|
7
|
+
*/
|
|
8
|
+
export type FalAudioProviderOptions<TModel extends string> = Omit<FalModelInput<TModel>, 'prompt'>;
|
|
9
|
+
/**
|
|
10
|
+
* fal.ai audio generation adapter.
|
|
11
|
+
*
|
|
12
|
+
* Supports fal.ai audio models like diffrhythm (music), sound effects, etc.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const adapter = falAudio('fal-ai/diffrhythm')
|
|
17
|
+
* const result = await generateAudio({
|
|
18
|
+
* adapter,
|
|
19
|
+
* prompt: 'An upbeat electronic track with synths',
|
|
20
|
+
* duration: 10,
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class FalAudioAdapter<TModel extends FalModel> extends BaseAudioAdapter<TModel, FalAudioProviderOptions<TModel>> {
|
|
25
|
+
readonly name: "fal";
|
|
26
|
+
constructor(model: TModel, config?: FalClientConfig);
|
|
27
|
+
generateAudio(options: AudioGenerationOptions<FalAudioProviderOptions<TModel>>): Promise<AudioGenerationResult>;
|
|
28
|
+
private buildInput;
|
|
29
|
+
protected generateId(): string;
|
|
30
|
+
private transformResponse;
|
|
31
|
+
}
|
|
32
|
+
export declare function falAudio<TModel extends FalModel>(model: TModel, config?: FalClientConfig): FalAudioAdapter<TModel>;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { fal } from "@fal-ai/client";
|
|
2
|
+
import { BaseAudioAdapter } from "@tanstack/ai/adapters";
|
|
3
|
+
import { configureFalClient, generateId, deriveAudioContentType } from "../utils/client.js";
|
|
4
|
+
const DURATION_FRAGMENT_BUILDERS = {
|
|
5
|
+
"fal-ai/elevenlabs/music": (seconds) => ({ music_length_ms: seconds * 1e3 }),
|
|
6
|
+
"fal-ai/stable-audio-25/text-to-audio": (seconds) => ({
|
|
7
|
+
seconds_total: seconds
|
|
8
|
+
})
|
|
9
|
+
};
|
|
10
|
+
function buildDurationFragment(model, duration) {
|
|
11
|
+
if (duration == null) return {};
|
|
12
|
+
const builder = DURATION_FRAGMENT_BUILDERS[model];
|
|
13
|
+
return builder ? builder(duration) : { duration };
|
|
14
|
+
}
|
|
15
|
+
class FalAudioAdapter extends BaseAudioAdapter {
|
|
16
|
+
constructor(model, config) {
|
|
17
|
+
super(model, {});
|
|
18
|
+
this.name = "fal";
|
|
19
|
+
configureFalClient(config);
|
|
20
|
+
}
|
|
21
|
+
async generateAudio(options) {
|
|
22
|
+
const { logger } = options;
|
|
23
|
+
logger.request(`activity=generateAudio provider=fal model=${this.model}`, {
|
|
24
|
+
provider: "fal",
|
|
25
|
+
model: this.model
|
|
26
|
+
});
|
|
27
|
+
try {
|
|
28
|
+
const input = this.buildInput(options);
|
|
29
|
+
const result = await fal.subscribe(this.model, { input });
|
|
30
|
+
return this.transformResponse(result);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
logger.errors("fal.generateAudio fatal", {
|
|
33
|
+
error,
|
|
34
|
+
source: "fal.generateAudio"
|
|
35
|
+
});
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
buildInput(options) {
|
|
40
|
+
return {
|
|
41
|
+
...buildDurationFragment(this.model, options.duration),
|
|
42
|
+
...options.modelOptions,
|
|
43
|
+
prompt: options.prompt
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
generateId() {
|
|
47
|
+
return generateId(this.name);
|
|
48
|
+
}
|
|
49
|
+
transformResponse(response) {
|
|
50
|
+
const data = response.data;
|
|
51
|
+
let audioUrl;
|
|
52
|
+
let contentType;
|
|
53
|
+
if (data.audio && typeof data.audio === "object" && "url" in data.audio) {
|
|
54
|
+
const audioObj = data.audio;
|
|
55
|
+
audioUrl = audioObj.url;
|
|
56
|
+
contentType = audioObj.content_type;
|
|
57
|
+
} else if (typeof data.audio_url === "string") {
|
|
58
|
+
audioUrl = data.audio_url;
|
|
59
|
+
}
|
|
60
|
+
if (!audioUrl) {
|
|
61
|
+
throw new Error("Audio URL not found in fal audio generation response");
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
id: response.requestId || this.generateId(),
|
|
65
|
+
model: this.model,
|
|
66
|
+
audio: {
|
|
67
|
+
url: audioUrl,
|
|
68
|
+
contentType: deriveAudioContentType(contentType, audioUrl)
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function falAudio(model, config) {
|
|
74
|
+
return new FalAudioAdapter(model, config);
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
FalAudioAdapter,
|
|
78
|
+
falAudio
|
|
79
|
+
};
|
|
80
|
+
//# sourceMappingURL=audio.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"audio.js","sources":["../../../src/adapters/audio.ts"],"sourcesContent":["import { fal } from '@fal-ai/client'\nimport { BaseAudioAdapter } from '@tanstack/ai/adapters'\nimport {\n configureFalClient,\n deriveAudioContentType,\n generateId as utilGenerateId,\n} from '../utils'\nimport type { OutputType, Result } from '@fal-ai/client'\nimport type {\n AudioGenerationOptions,\n AudioGenerationResult,\n} from '@tanstack/ai'\nimport type { FalClientConfig } from '../utils'\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\n constructor(model: TModel, config?: FalClientConfig) {\n super(model, {})\n configureFalClient(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 try {\n const input = this.buildInput(options)\n const result = await fal.subscribe(this.model, { input })\n return this.transformResponse(result)\n } catch (error) {\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 (data.audio && typeof data.audio === 'object' && 'url' in data.audio) {\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 return {\n id: response.requestId || this.generateId(),\n model: this.model,\n audio: {\n url: audioUrl,\n contentType: deriveAudioContentType(contentType, audioUrl),\n },\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"],"names":["utilGenerateId"],"mappings":";;;AA8BA,MAAM,6BAGF;AAAA,EACF,2BAA2B,CAAC,aAAa,EAAE,iBAAiB,UAAU,IAAA;AAAA,EACtE,wCAAwC,CAAC,aAAa;AAAA,IACpD,eAAe;AAAA,EAAA;AAEnB;AAEA,SAAS,sBACP,OACA,UACyB;AACzB,MAAI,YAAY,KAAM,QAAO,CAAA;AAC7B,QAAM,UAAU,2BAA2B,KAAK;AAChD,SAAO,UAAU,QAAQ,QAAQ,IAAI,EAAE,SAAA;AACzC;AAiBO,MAAM,wBAAiD,iBAG5D;AAAA,EAGA,YAAY,OAAe,QAA0B;AACnD,UAAM,OAAO,EAAE;AAHjB,SAAS,OAAO;AAId,uBAAmB,MAAM;AAAA,EAC3B;AAAA,EAEA,MAAM,cACJ,SACgC;AAChC,UAAM,EAAE,WAAW;AACnB,WAAO,QAAQ,6CAA6C,KAAK,KAAK,IAAI;AAAA,MACxE,UAAU;AAAA,MACV,OAAO,KAAK;AAAA,IAAA,CACb;AACD,QAAI;AACF,YAAM,QAAQ,KAAK,WAAW,OAAO;AACrC,YAAM,SAAS,MAAM,IAAI,UAAU,KAAK,OAAO,EAAE,OAAO;AACxD,aAAO,KAAK,kBAAkB,MAAM;AAAA,IACtC,SAAS,OAAO;AACd,aAAO,OAAO,2BAA2B;AAAA,QACvC;AAAA,QACA,QAAQ;AAAA,MAAA,CACT;AACD,YAAM;AAAA,IACR;AAAA,EACF;AAAA,EAEQ,WACN,SACuB;AACvB,WAAO;AAAA,MACL,GAAG,sBAAsB,KAAK,OAAO,QAAQ,QAAQ;AAAA,MACrD,GAAG,QAAQ;AAAA,MACX,QAAQ,QAAQ;AAAA,IAAA;AAAA,EAEpB;AAAA,EAEmB,aAAqB;AACtC,WAAOA,WAAe,KAAK,IAAI;AAAA,EACjC;AAAA,EAEQ,kBACN,UACuB;AACvB,UAAM,OAAO,SAAS;AAGtB,QAAI;AACJ,QAAI;AAEJ,QAAI,KAAK,SAAS,OAAO,KAAK,UAAU,YAAY,SAAS,KAAK,OAAO;AACvE,YAAM,WAAW,KAAK;AACtB,iBAAW,SAAS;AACpB,oBAAc,SAAS;AAAA,IACzB,WAAW,OAAO,KAAK,cAAc,UAAU;AAC7C,iBAAW,KAAK;AAAA,IAClB;AAEA,QAAI,CAAC,UAAU;AACb,YAAM,IAAI,MAAM,sDAAsD;AAAA,IACxE;AAEA,WAAO;AAAA,MACL,IAAI,SAAS,aAAa,KAAK,WAAA;AAAA,MAC/B,OAAO,KAAK;AAAA,MACZ,OAAO;AAAA,QACL,KAAK;AAAA,QACL,aAAa,uBAAuB,aAAa,QAAQ;AAAA,MAAA;AAAA,IAC3D;AAAA,EAEJ;AACF;AAEO,SAAS,SACd,OACA,QACyB;AACzB,SAAO,IAAI,gBAAgB,OAAO,MAAM;AAC1C;"}
|
|
@@ -4,7 +4,7 @@ import { configureFalClient, generateId } from "../utils/client.js";
|
|
|
4
4
|
import { mapSizeToFalFormat } from "../image/image-provider-options.js";
|
|
5
5
|
class FalImageAdapter extends BaseImageAdapter {
|
|
6
6
|
constructor(model, config) {
|
|
7
|
-
super({}
|
|
7
|
+
super(model, {});
|
|
8
8
|
this.kind = "image";
|
|
9
9
|
this.name = "fal";
|
|
10
10
|
configureFalClient(config);
|
|
@@ -50,6 +50,11 @@ class FalImageAdapter extends BaseImageAdapter {
|
|
|
50
50
|
} else if ("image" in data && data.image && typeof data.image === "object") {
|
|
51
51
|
images.push(this.parseImage(data.image));
|
|
52
52
|
}
|
|
53
|
+
if (images.length === 0) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
"Unexpected fal image response shape. Expected images[] or image{}. Got keys: " + Object.keys(data).join(",")
|
|
56
|
+
);
|
|
57
|
+
}
|
|
53
58
|
return {
|
|
54
59
|
id: response.requestId || this.generateId(),
|
|
55
60
|
model: this.model,
|
|
@@ -57,14 +62,20 @@ class FalImageAdapter extends BaseImageAdapter {
|
|
|
57
62
|
};
|
|
58
63
|
}
|
|
59
64
|
parseImage(img) {
|
|
60
|
-
|
|
65
|
+
let url;
|
|
66
|
+
if (typeof img === "string") {
|
|
67
|
+
url = img;
|
|
68
|
+
} else if (img && typeof img === "object" && "url" in img && typeof img.url === "string") {
|
|
69
|
+
url = img.url;
|
|
70
|
+
} else {
|
|
71
|
+
throw new Error(
|
|
72
|
+
`Invalid image payload from fal response: expected string or { url: string }, received ${img === null ? "null" : typeof img}`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
61
75
|
if (url.startsWith("data:")) {
|
|
62
76
|
const base64Match = url.match(/^data:image\/[^;]+;base64,(.+)$/);
|
|
63
|
-
if (base64Match) {
|
|
64
|
-
return {
|
|
65
|
-
b64Json: base64Match[1],
|
|
66
|
-
url
|
|
67
|
-
};
|
|
77
|
+
if (base64Match && base64Match[1]) {
|
|
78
|
+
return { b64Json: base64Match[1] };
|
|
68
79
|
}
|
|
69
80
|
}
|
|
70
81
|
return { url };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image.js","sources":["../../../src/adapters/image.ts"],"sourcesContent":["import { fal } from '@fal-ai/client'\nimport { BaseImageAdapter } from '@tanstack/ai/adapters'\nimport { configureFalClient, generateId as utilGenerateId } from '../utils'\nimport { mapSizeToFalFormat } from '../image/image-provider-options'\nimport type { OutputType, Result } from '@fal-ai/client'\nimport type { FalClientConfig } from '../utils'\nimport type {\n GeneratedImage,\n ImageGenerationOptions,\n ImageGenerationResult,\n} from '@tanstack/ai'\nimport type {\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> {\n readonly kind = 'image' as const\n readonly name = 'fal' as const\n\n constructor(model: TModel, config?: FalClientConfig) {\n super({}
|
|
1
|
+
{"version":3,"file":"image.js","sources":["../../../src/adapters/image.ts"],"sourcesContent":["import { fal } from '@fal-ai/client'\nimport { BaseImageAdapter } from '@tanstack/ai/adapters'\nimport { configureFalClient, generateId as utilGenerateId } from '../utils'\nimport { mapSizeToFalFormat } from '../image/image-provider-options'\nimport type { OutputType, Result } from '@fal-ai/client'\nimport type { FalClientConfig } from '../utils'\nimport type {\n GeneratedImage,\n ImageGenerationOptions,\n ImageGenerationResult,\n} from '@tanstack/ai'\nimport type {\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> {\n readonly kind = 'image' as const\n readonly name = 'fal' as const\n\n constructor(model: TModel, config?: FalClientConfig) {\n super(model, {})\n configureFalClient(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 try {\n const input = this.buildInput(options)\n const result = await fal.subscribe(this.model, { input })\n return this.transformResponse(result)\n } catch (error) {\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<FalImageProviderOptions<TModel>, string>,\n ): FalModelInput<TModel> {\n const sizeParams = mapSizeToFalFormat(options.size)\n const input = {\n ...options.modelOptions,\n ...sizeParams,\n prompt: options.prompt,\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 return {\n id: response.requestId || this.generateId(),\n model: this.model,\n images,\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 as { url: unknown }).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"],"names":["utilGenerateId"],"mappings":";;;;AAsCO,MAAM,wBAAiD,iBAK5D;AAAA,EAIA,YAAY,OAAe,QAA0B;AACnD,UAAM,OAAO,EAAE;AAJjB,SAAS,OAAO;AAChB,SAAS,OAAO;AAId,uBAAmB,MAAM;AAAA,EAC3B;AAAA,EAEA,MAAM,eACJ,SAIgC;AAChC,UAAM,EAAE,WAAW;AAEnB,WAAO,QAAQ,6CAA6C,KAAK,KAAK,IAAI;AAAA,MACxE,UAAU;AAAA,MACV,OAAO,KAAK;AAAA,IAAA,CACb;AAED,QAAI;AACF,YAAM,QAAQ,KAAK,WAAW,OAAO;AACrC,YAAM,SAAS,MAAM,IAAI,UAAU,KAAK,OAAO,EAAE,OAAO;AACxD,aAAO,KAAK,kBAAkB,MAAM;AAAA,IACtC,SAAS,OAAO;AACd,aAAO,OAAO,2BAA2B;AAAA,QACvC;AAAA,QACA,QAAQ;AAAA,MAAA,CACT;AACD,YAAM;AAAA,IACR;AAAA,EACF;AAAA,EAEQ,WACN,SACuB;AACvB,UAAM,aAAa,mBAAmB,QAAQ,IAAI;AAClD,UAAM,QAAQ;AAAA,MACZ,GAAG,QAAQ;AAAA,MACX,GAAG;AAAA,MACH,QAAQ,QAAQ;AAAA,MAChB,YAAY,QAAQ;AAAA,IAAA;AAEtB,WAAO;AAAA,EACT;AAAA,EAEmB,aAAqB;AACtC,WAAOA,WAAe,KAAK,IAAI;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M;AACrC;"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BaseTTSAdapter } from '@tanstack/ai/adapters';
|
|
2
|
+
import { TTSOptions, TTSResult } from '@tanstack/ai';
|
|
3
|
+
import { FalClientConfig } from '../utils.js';
|
|
4
|
+
import { FalModel, FalModelInput } from '../model-meta.js';
|
|
5
|
+
/**
|
|
6
|
+
* Provider options for TTS, excluding fields TanStack AI handles.
|
|
7
|
+
*/
|
|
8
|
+
export type FalSpeechProviderOptions<TModel extends string> = Omit<FalModelInput<TModel>, 'prompt' | 'text'>;
|
|
9
|
+
/**
|
|
10
|
+
* fal.ai text-to-speech adapter.
|
|
11
|
+
*
|
|
12
|
+
* Supports fal.ai TTS models like kokoro, elevenlabs, etc.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const adapter = falSpeech('fal-ai/kokoro/american-english')
|
|
17
|
+
* const result = await generateSpeech({
|
|
18
|
+
* adapter,
|
|
19
|
+
* text: 'Hello, world!',
|
|
20
|
+
* voice: 'af_heart',
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class FalSpeechAdapter<TModel extends FalModel> extends BaseTTSAdapter<TModel, FalSpeechProviderOptions<TModel>> {
|
|
25
|
+
readonly name: "fal";
|
|
26
|
+
constructor(model: TModel, config?: FalClientConfig);
|
|
27
|
+
generateSpeech(options: TTSOptions<FalSpeechProviderOptions<TModel>>): Promise<TTSResult>;
|
|
28
|
+
private buildInput;
|
|
29
|
+
protected generateId(): string;
|
|
30
|
+
private transformResponse;
|
|
31
|
+
}
|
|
32
|
+
export declare function falSpeech<TModel extends FalModel>(model: TModel, config?: FalClientConfig): FalSpeechAdapter<TModel>;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { fal } from "@fal-ai/client";
|
|
2
|
+
import { BaseTTSAdapter } from "@tanstack/ai/adapters";
|
|
3
|
+
import { configureFalClient, generateId, arrayBufferToBase64, extractUrlExtension } from "../utils/client.js";
|
|
4
|
+
class FalSpeechAdapter extends BaseTTSAdapter {
|
|
5
|
+
constructor(model, config) {
|
|
6
|
+
super(model, {});
|
|
7
|
+
this.name = "fal";
|
|
8
|
+
configureFalClient(config);
|
|
9
|
+
}
|
|
10
|
+
async generateSpeech(options) {
|
|
11
|
+
const { logger } = options;
|
|
12
|
+
logger.request(`activity=generateSpeech provider=fal model=${this.model}`, {
|
|
13
|
+
provider: "fal",
|
|
14
|
+
model: this.model
|
|
15
|
+
});
|
|
16
|
+
try {
|
|
17
|
+
const input = this.buildInput(options);
|
|
18
|
+
const result = await fal.subscribe(this.model, { input });
|
|
19
|
+
return await this.transformResponse(result);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
logger.errors("fal.generateSpeech fatal", {
|
|
22
|
+
error,
|
|
23
|
+
source: "fal.generateSpeech"
|
|
24
|
+
});
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
buildInput(options) {
|
|
29
|
+
const input = {
|
|
30
|
+
...options.modelOptions,
|
|
31
|
+
// Map text to both prompt and text fields (different models use different fields)
|
|
32
|
+
prompt: options.text,
|
|
33
|
+
text: options.text,
|
|
34
|
+
...options.voice ? { voice: options.voice } : {},
|
|
35
|
+
...options.speed ? { speed: options.speed } : {}
|
|
36
|
+
};
|
|
37
|
+
return input;
|
|
38
|
+
}
|
|
39
|
+
generateId() {
|
|
40
|
+
return generateId(this.name);
|
|
41
|
+
}
|
|
42
|
+
async transformResponse(response) {
|
|
43
|
+
const data = response.data;
|
|
44
|
+
let audioUrl;
|
|
45
|
+
let contentType;
|
|
46
|
+
if (data.audio && typeof data.audio === "object" && "url" in data.audio) {
|
|
47
|
+
const audioObj = data.audio;
|
|
48
|
+
audioUrl = audioObj.url;
|
|
49
|
+
contentType = audioObj.content_type;
|
|
50
|
+
} else if (typeof data.audio_url === "string") {
|
|
51
|
+
audioUrl = data.audio_url;
|
|
52
|
+
}
|
|
53
|
+
if (!audioUrl) {
|
|
54
|
+
throw new Error("Audio URL not found in fal TTS response");
|
|
55
|
+
}
|
|
56
|
+
const audioResponse = await fetch(audioUrl);
|
|
57
|
+
if (!audioResponse.ok) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`Failed to fetch audio from ${audioUrl}: ${audioResponse.status} ${audioResponse.statusText}`
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
const arrayBuffer = await audioResponse.arrayBuffer();
|
|
63
|
+
const base64 = arrayBufferToBase64(arrayBuffer);
|
|
64
|
+
const contentTypeMime = contentType?.split(";")[0]?.trim();
|
|
65
|
+
const safeUrlExtension = extractUrlExtension(audioUrl);
|
|
66
|
+
const rawFormat = safeUrlExtension || contentTypeMime?.split("/")[1] || "wav";
|
|
67
|
+
const format = rawFormat === "mpeg" ? "mp3" : rawFormat;
|
|
68
|
+
return {
|
|
69
|
+
id: response.requestId || this.generateId(),
|
|
70
|
+
model: this.model,
|
|
71
|
+
audio: base64,
|
|
72
|
+
format,
|
|
73
|
+
contentType: contentTypeMime || `audio/${format}`
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function falSpeech(model, config) {
|
|
78
|
+
return new FalSpeechAdapter(model, config);
|
|
79
|
+
}
|
|
80
|
+
export {
|
|
81
|
+
FalSpeechAdapter,
|
|
82
|
+
falSpeech
|
|
83
|
+
};
|
|
84
|
+
//# sourceMappingURL=speech.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"speech.js","sources":["../../../src/adapters/speech.ts"],"sourcesContent":["import { fal } from '@fal-ai/client'\nimport { BaseTTSAdapter } from '@tanstack/ai/adapters'\nimport {\n arrayBufferToBase64,\n configureFalClient,\n extractUrlExtension,\n generateId as utilGenerateId,\n} from '../utils'\nimport type { OutputType, Result } from '@fal-ai/client'\nimport type { TTSOptions, TTSResult } from '@tanstack/ai'\nimport type { FalClientConfig } from '../utils'\nimport type { FalModel, FalModelInput } from '../model-meta'\n\n/**\n * Provider options for TTS, excluding fields TanStack AI handles.\n */\nexport type FalSpeechProviderOptions<TModel extends string> = Omit<\n FalModelInput<TModel>,\n 'prompt' | 'text'\n>\n\n/**\n * fal.ai text-to-speech adapter.\n *\n * Supports fal.ai TTS models like kokoro, elevenlabs, etc.\n *\n * @example\n * ```typescript\n * const adapter = falSpeech('fal-ai/kokoro/american-english')\n * const result = await generateSpeech({\n * adapter,\n * text: 'Hello, world!',\n * voice: 'af_heart',\n * })\n * ```\n */\nexport class FalSpeechAdapter<TModel extends FalModel> extends BaseTTSAdapter<\n TModel,\n FalSpeechProviderOptions<TModel>\n> {\n readonly name = 'fal' as const\n\n constructor(model: TModel, config?: FalClientConfig) {\n super(model, {})\n configureFalClient(config)\n }\n\n async generateSpeech(\n options: TTSOptions<FalSpeechProviderOptions<TModel>>,\n ): Promise<TTSResult> {\n const { logger } = options\n logger.request(`activity=generateSpeech provider=fal model=${this.model}`, {\n provider: 'fal',\n model: this.model,\n })\n try {\n const input = this.buildInput(options)\n const result = await fal.subscribe(this.model, { input })\n return await this.transformResponse(result)\n } catch (error) {\n logger.errors('fal.generateSpeech fatal', {\n error,\n source: 'fal.generateSpeech',\n })\n throw error\n }\n }\n\n private buildInput(\n options: TTSOptions<FalSpeechProviderOptions<TModel>>,\n ): FalModelInput<TModel> {\n const input = {\n ...options.modelOptions,\n // Map text to both prompt and text fields (different models use different fields)\n prompt: options.text,\n text: options.text,\n ...(options.voice ? { voice: options.voice } : {}),\n ...(options.speed ? { speed: options.speed } : {}),\n } as FalModelInput<TModel>\n return input\n }\n\n protected override generateId(): string {\n return utilGenerateId(this.name)\n }\n\n private async transformResponse(\n response: Result<OutputType<TModel>>,\n ): Promise<TTSResult> {\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 (data.audio && typeof data.audio === 'object' && 'url' in data.audio) {\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 TTS response')\n }\n\n // Fetch the audio and convert to base64 to match TTSResult contract.\n // Using a chunked helper here — spreading Uint8Array into btoa exceeds\n // V8's argument limit (~65k) for any realistic TTS clip.\n const audioResponse = await fetch(audioUrl)\n if (!audioResponse.ok) {\n throw new Error(\n `Failed to fetch audio from ${audioUrl}: ${audioResponse.status} ${audioResponse.statusText}`,\n )\n }\n const arrayBuffer = await audioResponse.arrayBuffer()\n const base64 = arrayBufferToBase64(arrayBuffer)\n\n // Strip parameters like `; charset=...` from contentType, and only use\n // the URL extension as a fallback when it looks like a real extension.\n const contentTypeMime = contentType?.split(';')[0]?.trim()\n const safeUrlExtension = extractUrlExtension(audioUrl)\n // Prefer URL-derived extension when available (more canonical for file\n // consumers), otherwise derive from the content-type mime subtype, then\n // fall back to `wav`. Normalize `mpeg` → `mp3` so the format field is a\n // usable file extension rather than the IANA subtype.\n const rawFormat =\n safeUrlExtension || contentTypeMime?.split('/')[1] || 'wav'\n const format = rawFormat === 'mpeg' ? 'mp3' : rawFormat\n\n return {\n id: response.requestId || this.generateId(),\n model: this.model,\n audio: base64,\n format,\n contentType: contentTypeMime || `audio/${format}`,\n }\n }\n}\n\nexport function falSpeech<TModel extends FalModel>(\n model: TModel,\n config?: FalClientConfig,\n): FalSpeechAdapter<TModel> {\n return new FalSpeechAdapter(model, config)\n}\n"],"names":["utilGenerateId"],"mappings":";;;AAoCO,MAAM,yBAAkD,eAG7D;AAAA,EAGA,YAAY,OAAe,QAA0B;AACnD,UAAM,OAAO,EAAE;AAHjB,SAAS,OAAO;AAId,uBAAmB,MAAM;AAAA,EAC3B;AAAA,EAEA,MAAM,eACJ,SACoB;AACpB,UAAM,EAAE,WAAW;AACnB,WAAO,QAAQ,8CAA8C,KAAK,KAAK,IAAI;AAAA,MACzE,UAAU;AAAA,MACV,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A;AACxC,UAAM,SAAS,oBAAoB,WAAW;AAI9C,UAAM,kBAAkB,aAAa,MAAM,GAAG,EAAE,CAAC,GAAG,KAAA;AACpD,UAAM,mBAAmB,oBAAoB,QAAQ;AAKrD,UAAM,YACJ,oBAAoB,iBAAiB,MAAM,GAAG,EAAE,CAAC,KAAK;AACxD,UAAM,SAAS,cAAc,SAAS,QAAQ;AAE9C,WAAO;AAAA,MACL,IAAI,SAAS,aAAa,KAAK,WAAA;AAAA,MAC/B,OAAO,KAAK;AAAA,MACZ,OAAO;AAAA,MACP;AAAA,MACA,aAAa,mBAAmB,SAAS,MAAM;AAAA,IAAA;AAAA,EAEnD;AACF;AAEO,SAAS,UACd,OACA,QAC0B;AAC1B,SAAO,IAAI,iBAAiB,OAAO,MAAM;AAC3C;"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BaseTranscriptionAdapter } from '@tanstack/ai/adapters';
|
|
2
|
+
import { TranscriptionOptions, TranscriptionResult } from '@tanstack/ai';
|
|
3
|
+
import { FalClientConfig } from '../utils.js';
|
|
4
|
+
import { FalModel, FalModelInput } from '../model-meta.js';
|
|
5
|
+
/**
|
|
6
|
+
* Provider options for transcription, excluding fields TanStack AI handles.
|
|
7
|
+
*/
|
|
8
|
+
export type FalTranscriptionProviderOptions<TModel extends string> = Omit<FalModelInput<TModel>, 'audio_url'>;
|
|
9
|
+
/**
|
|
10
|
+
* fal.ai transcription (speech-to-text) adapter.
|
|
11
|
+
*
|
|
12
|
+
* Supports fal.ai STT models like whisper, wizper, etc.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const adapter = falTranscription('fal-ai/whisper')
|
|
17
|
+
* const result = await generateTranscription({
|
|
18
|
+
* adapter,
|
|
19
|
+
* audio: 'https://example.com/audio.mp3',
|
|
20
|
+
* language: 'en',
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare class FalTranscriptionAdapter<TModel extends FalModel> extends BaseTranscriptionAdapter<TModel, FalTranscriptionProviderOptions<TModel>> {
|
|
25
|
+
readonly name: "fal";
|
|
26
|
+
constructor(model: TModel, config?: FalClientConfig);
|
|
27
|
+
transcribe(options: TranscriptionOptions<FalTranscriptionProviderOptions<TModel>>): Promise<TranscriptionResult>;
|
|
28
|
+
private buildInput;
|
|
29
|
+
protected generateId(): string;
|
|
30
|
+
private transformResponse;
|
|
31
|
+
}
|
|
32
|
+
export declare function falTranscription<TModel extends FalModel>(model: TModel, config?: FalClientConfig): FalTranscriptionAdapter<TModel>;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { fal } from "@fal-ai/client";
|
|
2
|
+
import { BaseTranscriptionAdapter } from "@tanstack/ai/adapters";
|
|
3
|
+
import { configureFalClient, dataUrlToBlob, generateId } from "../utils/client.js";
|
|
4
|
+
class FalTranscriptionAdapter extends BaseTranscriptionAdapter {
|
|
5
|
+
constructor(model, config) {
|
|
6
|
+
super(model, {});
|
|
7
|
+
this.name = "fal";
|
|
8
|
+
configureFalClient(config);
|
|
9
|
+
}
|
|
10
|
+
async transcribe(options) {
|
|
11
|
+
const { logger } = options;
|
|
12
|
+
logger.request(
|
|
13
|
+
`activity=generateTranscription provider=fal model=${this.model}`,
|
|
14
|
+
{
|
|
15
|
+
provider: "fal",
|
|
16
|
+
model: this.model
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
try {
|
|
20
|
+
const input = this.buildInput(options);
|
|
21
|
+
const result = await fal.subscribe(this.model, { input });
|
|
22
|
+
return this.transformResponse(result);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
logger.errors("fal.generateTranscription fatal", {
|
|
25
|
+
error,
|
|
26
|
+
source: "fal.generateTranscription"
|
|
27
|
+
});
|
|
28
|
+
throw error;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
buildInput(options) {
|
|
32
|
+
let audioInput;
|
|
33
|
+
if (options.audio instanceof ArrayBuffer) {
|
|
34
|
+
audioInput = new Blob([options.audio]);
|
|
35
|
+
} else if (typeof options.audio === "string") {
|
|
36
|
+
audioInput = dataUrlToBlob(options.audio) ?? options.audio;
|
|
37
|
+
} else {
|
|
38
|
+
audioInput = options.audio;
|
|
39
|
+
}
|
|
40
|
+
const input = {
|
|
41
|
+
...options.modelOptions,
|
|
42
|
+
audio_url: audioInput,
|
|
43
|
+
...options.language ? { language: options.language } : {},
|
|
44
|
+
...options.prompt ? { prompt: options.prompt } : {}
|
|
45
|
+
};
|
|
46
|
+
return input;
|
|
47
|
+
}
|
|
48
|
+
generateId() {
|
|
49
|
+
return generateId(this.name);
|
|
50
|
+
}
|
|
51
|
+
transformResponse(response) {
|
|
52
|
+
const data = response.data;
|
|
53
|
+
const text = data.text || "";
|
|
54
|
+
let segments;
|
|
55
|
+
const chunks = data.chunks;
|
|
56
|
+
if (chunks && Array.isArray(chunks)) {
|
|
57
|
+
segments = chunks.flatMap((chunk, index) => {
|
|
58
|
+
const ts = chunk.timestamp;
|
|
59
|
+
if (!Array.isArray(ts) || ts.length < 2 || typeof ts[0] !== "number" || typeof ts[1] !== "number") {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
return [
|
|
63
|
+
{
|
|
64
|
+
id: index,
|
|
65
|
+
start: ts[0],
|
|
66
|
+
end: ts[1],
|
|
67
|
+
text: chunk.text,
|
|
68
|
+
...chunk.speaker ? { speaker: chunk.speaker } : {}
|
|
69
|
+
}
|
|
70
|
+
];
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const language = data.language || data.inferred_languages?.[0] || data.languages?.[0];
|
|
74
|
+
return {
|
|
75
|
+
id: response.requestId || this.generateId(),
|
|
76
|
+
model: this.model,
|
|
77
|
+
text,
|
|
78
|
+
language,
|
|
79
|
+
segments
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function falTranscription(model, config) {
|
|
84
|
+
return new FalTranscriptionAdapter(model, config);
|
|
85
|
+
}
|
|
86
|
+
export {
|
|
87
|
+
FalTranscriptionAdapter,
|
|
88
|
+
falTranscription
|
|
89
|
+
};
|
|
90
|
+
//# sourceMappingURL=transcription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transcription.js","sources":["../../../src/adapters/transcription.ts"],"sourcesContent":["import { fal } from '@fal-ai/client'\nimport { BaseTranscriptionAdapter } from '@tanstack/ai/adapters'\nimport {\n configureFalClient,\n dataUrlToBlob,\n generateId as utilGenerateId,\n} from '../utils'\nimport type { OutputType, Result } from '@fal-ai/client'\nimport type {\n TranscriptionOptions,\n TranscriptionResult,\n TranscriptionSegment,\n} from '@tanstack/ai'\nimport type { FalClientConfig } from '../utils'\nimport type { FalModel, FalModelInput } from '../model-meta'\n\n/**\n * Provider options for transcription, excluding fields TanStack AI handles.\n */\nexport type FalTranscriptionProviderOptions<TModel extends string> = Omit<\n FalModelInput<TModel>,\n 'audio_url'\n>\n\ninterface FalChunk {\n text: string\n timestamp?: [number, number] | null\n speaker?: string\n}\n\n/**\n * fal.ai transcription (speech-to-text) adapter.\n *\n * Supports fal.ai STT models like whisper, wizper, etc.\n *\n * @example\n * ```typescript\n * const adapter = falTranscription('fal-ai/whisper')\n * const result = await generateTranscription({\n * adapter,\n * audio: 'https://example.com/audio.mp3',\n * language: 'en',\n * })\n * ```\n */\nexport class FalTranscriptionAdapter<\n TModel extends FalModel,\n> extends BaseTranscriptionAdapter<\n TModel,\n FalTranscriptionProviderOptions<TModel>\n> {\n readonly name = 'fal' as const\n\n constructor(model: TModel, config?: FalClientConfig) {\n super(model, {})\n configureFalClient(config)\n }\n\n async transcribe(\n options: TranscriptionOptions<FalTranscriptionProviderOptions<TModel>>,\n ): Promise<TranscriptionResult> {\n const { logger } = options\n logger.request(\n `activity=generateTranscription provider=fal model=${this.model}`,\n {\n provider: 'fal',\n model: this.model,\n },\n )\n try {\n const input = this.buildInput(options)\n const result = await fal.subscribe(this.model, { input })\n return this.transformResponse(result)\n } catch (error) {\n logger.errors('fal.generateTranscription fatal', {\n error,\n source: 'fal.generateTranscription',\n })\n throw error\n }\n }\n\n private buildInput(\n options: TranscriptionOptions<FalTranscriptionProviderOptions<TModel>>,\n ): FalModelInput<TModel> {\n // fal-client auto-uploads Blob/File inputs via fal.storage.upload, but\n // passes strings through unchanged — so a `data:` URL would reach fal's\n // API and get rejected with a 422 \"Unsupported data URL\". Decode data\n // URLs to a Blob up front so the auto-upload path handles them.\n let audioInput: string | Blob | File\n if (options.audio instanceof ArrayBuffer) {\n audioInput = new Blob([options.audio])\n } else if (typeof options.audio === 'string') {\n audioInput = dataUrlToBlob(options.audio) ?? options.audio\n } else {\n audioInput = options.audio\n }\n\n const input = {\n ...options.modelOptions,\n audio_url: audioInput,\n ...(options.language ? { language: options.language } : {}),\n ...(options.prompt ? { prompt: options.prompt } : {}),\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 ): TranscriptionResult {\n const data = response.data as Record<string, unknown>\n\n const text = (data.text as string) || ''\n\n // Map fal chunks to TanStack segments. fal whisper can return\n // `timestamp: null` on some chunks (e.g. when word-level timing is\n // disabled), and the runtime payload is not actually constrained to a\n // 2-tuple — treat it as unknown and validate before indexing.\n let segments: Array<TranscriptionSegment> | undefined\n const chunks = data.chunks as Array<FalChunk> | undefined\n if (chunks && Array.isArray(chunks)) {\n segments = chunks.flatMap((chunk, index) => {\n const ts = chunk.timestamp as unknown\n if (\n !Array.isArray(ts) ||\n ts.length < 2 ||\n typeof ts[0] !== 'number' ||\n typeof ts[1] !== 'number'\n ) {\n return []\n }\n return [\n {\n id: index,\n start: ts[0],\n end: ts[1],\n text: chunk.text,\n ...(chunk.speaker ? { speaker: chunk.speaker } : {}),\n },\n ]\n })\n }\n\n // Extract language from response\n const language =\n (data.language as string) ||\n (data.inferred_languages as Array<string> | undefined)?.[0] ||\n (data.languages as Array<string> | undefined)?.[0]\n\n return {\n id: response.requestId || this.generateId(),\n model: this.model,\n text,\n language,\n segments,\n }\n }\n}\n\nexport function falTranscription<TModel extends FalModel>(\n model: TModel,\n config?: FalClientConfig,\n): FalTranscriptionAdapter<TModel> {\n return new FalTranscriptionAdapter(model, config)\n}\n"],"names":["utilGenerateId"],"mappings":";;;AA6CO,MAAM,gCAEH,yBAGR;AAAA,EAGA,YAAY,OAAe,QAA0B;AACnD,UAAM,OAAO,EAAE;AAHjB,SAAS,OAAO;AAId,uBAAmB,MAAM;AAAA,EAC3B;AAAA,EAEA,MAAM,WACJ,SAC8B;AAC9B,UAAM,EAAE,WAAW;AACnB,WAAO;AAAA,MACL,qDAAqD,KAAK,KAAK;AAAA,MAC/D;AAAA,QACE,UAAU;AAAA,QACV,OAAO,KAAK;AAAA,MAAA;AAAA,IACd;AAEF,QAAI;AACF,YAAM,QAAQ,KAAK,WAAW,OAAO;AACrC,YAAM,SAAS,MAAM,IAAI,UAAU,KAAK,OAAO,EAAE,OAAO;AACxD,aAAO,KAAK,kBAAkB,MAAM;AAAA,IACtC,SAAS,OAAO;AACd,aAAO,OAAO,mCAAmC;AAAA,QAC/C;AAAA,QACA,QAAQ;AAAA,MAAA,CACT;AACD,YAAM;AAAA,IACR;AAAA,EACF;AAAA,EAEQ,WACN,SACuB;AAKvB,QAAI;AACJ,QAAI,QAAQ,iBAAiB,aAAa;AACxC,mBAAa,IAAI,KAAK,CAAC,QAAQ,KAAK,CAAC;AAAA,IACvC,WAAW,OAAO,QAAQ,UAAU,UAAU;AAC5C,mBAAa,cAAc,QAAQ,KAAK,KAAK,QAAQ;AAAA,IACvD,OAAO;AACL,mBAAa,QAAQ;AAAA,IACvB;AAEA,UAAM,QAAQ;AAAA,MACZ,GAAG,QAAQ;AAAA,MACX,WAAW;AAAA,MACX,GAAI,QAAQ,WAAW,EAAE,UAAU,QAAQ,SAAA,IAAa,CAAA;AAAA,MACxD,GAAI,QAAQ,SAAS,EAAE,QAAQ,QAAQ,OAAA,IAAW,CAA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+)(?:_(.+))?$/);
|
|
5
|
+
if (match) {
|
|
6
|
+
return {
|
|
7
|
+
aspect_ratio: match[1],
|
|
8
|
+
...match[2] ? { resolution: match[2] } : {}
|
|
9
|
+
};
|
|
9
10
|
}
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
image_size: size
|
|
10
14
|
};
|
|
11
15
|
}
|
|
12
16
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image-provider-options.js","sources":["../../../src/image/image-provider-options.ts"],"sourcesContent":["import type { FalModelImageSize, FalModelImageSizeInput } from '../model-meta'\n\nexport function mapSizeToFalFormat<TModel extends string>(\n size: FalModelImageSize<TModel> | undefined,\n): FalModelImageSizeInput<TModel> | undefined {\n if (!size) return undefined\n\n // \"16:9_4K\" → { aspect_ratio, resolution }\n // \"16:9\" → { aspect_ratio }\n // no match → { image_size }\n const match = size.match(/^(\\d+:\\d+)(?:_(.+))?$/)\n\n
|
|
1
|
+
{"version":3,"file":"image-provider-options.js","sources":["../../../src/image/image-provider-options.ts"],"sourcesContent":["import type { FalModelImageSize, FalModelImageSizeInput } from '../model-meta'\n\nexport function mapSizeToFalFormat<TModel extends string>(\n size: FalModelImageSize<TModel> | undefined,\n): FalModelImageSizeInput<TModel> | undefined {\n if (!size) return undefined\n\n // \"16:9_4K\" → { aspect_ratio, resolution }\n // \"16:9\" → { aspect_ratio }\n // no match → { image_size }\n if (typeof size === 'string') {\n const match = size.match(/^(\\d+:\\d+)(?:_(.+))?$/)\n if (match) {\n return {\n aspect_ratio: match[1],\n ...(match[2] ? { resolution: match[2] } : {}),\n } as FalModelImageSizeInput<TModel>\n }\n }\n\n return {\n image_size: size,\n } as FalModelImageSizeInput<TModel>\n}\n"],"names":[],"mappings":"AAEO,SAAS,mBACd,MAC4C;AAC5C,MAAI,CAAC,KAAM,QAAO;AAKlB,MAAI,OAAO,SAAS,UAAU;AAC5B,UAAM,QAAQ,KAAK,MAAM,uBAAuB;AAChD,QAAI,OAAO;AACT,aAAO;AAAA,QACL,cAAc,MAAM,CAAC;AAAA,QACrB,GAAI,MAAM,CAAC,IAAI,EAAE,YAAY,MAAM,CAAC,MAAM,CAAA;AAAA,MAAC;AAAA,IAE/C;AAAA,EACF;AAEA,SAAO;AAAA,IACL,YAAY;AAAA,EAAA;AAEhB;"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export { FalImageAdapter, falImage } from './adapters/image.js';
|
|
2
2
|
export { FalVideoAdapter, falVideo } from './adapters/video.js';
|
|
3
|
-
export {
|
|
3
|
+
export { FalSpeechAdapter, falSpeech } from './adapters/speech.js';
|
|
4
|
+
export { FalTranscriptionAdapter, falTranscription, } from './adapters/transcription.js';
|
|
5
|
+
export { FalAudioAdapter, falAudio } from './adapters/audio.js';
|
|
6
|
+
export { type FalImageProviderOptions, type FalVideoProviderOptions, type FalSpeechProviderOptions, type FalTranscriptionProviderOptions, type FalAudioProviderOptions, type FalModel, type FalModelInput, type FalModelOutput, type FalModelImageSize, type FalModelVideoSize, } from './model-meta.js';
|
|
4
7
|
export { getFalApiKeyFromEnv, configureFalClient, generateId, type FalClientConfig, } from './utils.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
import { FalImageAdapter, falImage } from "./adapters/image.js";
|
|
2
2
|
import { FalVideoAdapter, falVideo } from "./adapters/video.js";
|
|
3
|
+
import { FalSpeechAdapter, falSpeech } from "./adapters/speech.js";
|
|
4
|
+
import { FalTranscriptionAdapter, falTranscription } from "./adapters/transcription.js";
|
|
5
|
+
import { FalAudioAdapter, falAudio } from "./adapters/audio.js";
|
|
3
6
|
import { configureFalClient, generateId, getFalApiKeyFromEnv } from "./utils/client.js";
|
|
4
7
|
export {
|
|
8
|
+
FalAudioAdapter,
|
|
5
9
|
FalImageAdapter,
|
|
10
|
+
FalSpeechAdapter,
|
|
11
|
+
FalTranscriptionAdapter,
|
|
6
12
|
FalVideoAdapter,
|
|
7
13
|
configureFalClient,
|
|
14
|
+
falAudio,
|
|
8
15
|
falImage,
|
|
16
|
+
falSpeech,
|
|
17
|
+
falTranscription,
|
|
9
18
|
falVideo,
|
|
10
19
|
generateId,
|
|
11
20
|
getFalApiKeyFromEnv
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;"}
|
package/dist/esm/model-meta.d.ts
CHANGED
|
@@ -74,3 +74,18 @@ export type FalModelVideoSizeInput<TModel extends string> = TModel extends keyof
|
|
|
74
74
|
* Use this for the `modelOptions` parameter in video generation.
|
|
75
75
|
*/
|
|
76
76
|
export type FalVideoProviderOptions<TModel extends string> = TModel extends keyof EndpointTypeMap ? Omit<FalModelInput<TModel>, 'prompt'> : Record<string, any>;
|
|
77
|
+
/**
|
|
78
|
+
* Provider options for TTS, excluding fields TanStack AI handles.
|
|
79
|
+
* Use this for the `modelOptions` parameter in speech generation.
|
|
80
|
+
*/
|
|
81
|
+
export type FalSpeechProviderOptions<TModel extends string> = Omit<FalModelInput<TModel>, 'prompt' | 'text'>;
|
|
82
|
+
/**
|
|
83
|
+
* Provider options for transcription, excluding fields TanStack AI handles.
|
|
84
|
+
* Use this for the `modelOptions` parameter in transcription.
|
|
85
|
+
*/
|
|
86
|
+
export type FalTranscriptionProviderOptions<TModel extends string> = Omit<FalModelInput<TModel>, 'audio_url'>;
|
|
87
|
+
/**
|
|
88
|
+
* Provider options for audio generation, excluding fields TanStack AI handles.
|
|
89
|
+
* Use this for the `modelOptions` parameter in audio generation.
|
|
90
|
+
*/
|
|
91
|
+
export type FalAudioProviderOptions<TModel extends string> = Omit<FalModelInput<TModel>, 'prompt'>;
|
|
@@ -5,3 +5,34 @@ export interface FalClientConfig {
|
|
|
5
5
|
export declare function getFalApiKeyFromEnv(): string;
|
|
6
6
|
export declare function configureFalClient(config?: FalClientConfig): void;
|
|
7
7
|
export declare function generateId(prefix: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Extract a safe file extension from a URL. Strips query strings, URL
|
|
10
|
+
* fragments, and any trailing slashes, and only returns the extension when
|
|
11
|
+
* it looks like a real one (2-5 alphanumeric chars). Returns undefined
|
|
12
|
+
* otherwise so callers can fall back to a default.
|
|
13
|
+
*/
|
|
14
|
+
export declare function extractUrlExtension(url: string): string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Derive a reasonable audio content-type. Prefers the explicit MIME (stripped
|
|
17
|
+
* of parameters), then an extension-based lookup for common audio formats,
|
|
18
|
+
* otherwise falls back to audio/mpeg — fal URLs virtually always serve mp3.
|
|
19
|
+
*/
|
|
20
|
+
export declare function deriveAudioContentType(explicitContentType: string | undefined, url: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Decode a `data:` URL into a Blob so fal-client can auto-upload it via
|
|
23
|
+
* `fal.storage.upload`. fal's inference API rejects data URLs with a 422
|
|
24
|
+
* "Unsupported data URL", so we convert them before handing them off.
|
|
25
|
+
*
|
|
26
|
+
* Supports both base64 and URL-encoded data URLs. Returns `undefined` for
|
|
27
|
+
* anything that isn't a data URL, so callers can fall through to other
|
|
28
|
+
* handling (http URLs are passed to fal as-is).
|
|
29
|
+
*/
|
|
30
|
+
export declare function dataUrlToBlob(value: string): Blob | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Convert an ArrayBuffer to base64 in a cross-runtime way.
|
|
33
|
+
*
|
|
34
|
+
* The naive `btoa(String.fromCharCode(...bytes))` form blows up V8's argument
|
|
35
|
+
* limit (~65k) on realistic audio payloads, so we either use `Buffer` (Node /
|
|
36
|
+
* Bun) or walk the byte array in a single loop (browser).
|
|
37
|
+
*/
|
|
38
|
+
export declare function arrayBufferToBase64(bytes: ArrayBuffer): string;
|