Open History to see the last commit
1import { fal } from '@fal-ai/client'
2import {
3 BaseFilesAdapter,
4 normalizeFileUploadInput,
5} from '@tanstack/ai/adapters'
6import { configureFalClient } from '../utils/client'
7import type { StorageSettings } from '@fal-ai/client'
8import type { FileHandle, FileUploadInput } from '@tanstack/ai/adapters'
9import type { FalClientConfig } from '../utils/client'
10
11export interface FalFilesConfig extends FalClientConfig {
12 /**
13 * Optional lifecycle for uploaded objects — one of fal's presets
14 * (`'1h' | '1d' | '7d' | '30d' | '1y' | 'never' | 'immediate'`) or a number
15 * of seconds. Applied as the object's `X-Fal-Object-Lifecycle-Preference`.
16 */
17 expiresIn?: StorageSettings['expiresIn']
18}
19
20/**
21 * fal Files adapter — uploads media to fal storage via `fal.storage.upload`.
22 * The handle is a storage URL, so `fileSourceFromHandle(handle)` references it
23 * as a normal URL (fal endpoints accept it directly). Upload-only: fal storage
24 * has no retrieval/deletion API, so `get`/`delete` are unavailable.
25 */
26export class FalFilesAdapter extends BaseFilesAdapter<'fal'> {
27 readonly name = 'fal' as const
28 private readonly expiresIn?: StorageSettings['expiresIn']
29
30 constructor(config?: FalFilesConfig) {
31 super()
32 configureFalClient(config)
33 this.expiresIn = config?.expiresIn
34 }
35
36 async upload(input: FileUploadInput): Promise<FileHandle<'fal'>> {
37 const { blob, mimeType } = normalizeFileUploadInput(input)
38 const url = await fal.storage.upload(
39 blob,
40 this.expiresIn !== undefined
41 ? { lifecycle: { expiresIn: this.expiresIn } }
42 : undefined,
43 )
44 return {
45 id: url,
46 provider: 'fal',
47 uri: url,
48 ...(mimeType ? { mimeType } : {}),
49 }
50 }
51}
52
53/**
54 * Create a fal Files adapter. Reads the API key from `FAL_KEY` unless one is
55 * provided in `config`.
56 */
57export function falFiles(config?: FalFilesConfig): FalFilesAdapter {
58 return new FalFilesAdapter(config)
59}