Commit 8fd6337
authored
feat: add fal.ai as a first-class provider (record + replay) (#153)
## Summary
- Adds a general fal.ai handler (`src/fal.ts`) supporting arbitrary JSON
request/response payloads — image, video, motion, music, etc. —
alongside the existing audio-only `src/fal-audio.ts`.
- Routes by `x-fal-target-host` header to mirror the `@fal-ai/client`
server-side `requestMiddleware` convention (since `proxyUrl` is
browser-only). Falls through to the legacy `/fal/queue/...` and
`/fal/run/...` paths for back-compat.
- Adds `mock.onFalQueue(/model/, payload)` (primary) and
`mock.onFalRun(/model/, payload)` (sync alias) to `LLMock`. Queue submit
auto-mints a request_id and returns the standard envelope; status/result
lookups go through a per-testId TTL+bounded `FalQueueStateMap` (mirrors
the `FalJobMap` pattern from `fal-audio.ts` and the `VideoStateMap` from
`video.ts`).
- Adds a `RawJSONResponse` (`{ json: unknown }`) variant to
`FixtureResponse` so the recorder can save fal payloads verbatim instead
of mis-classifying them as `ImageResponse` / `VideoResponse`.
Closes #152.
## Surface
| Method | Path | x-fal-target-host | Behaviour |
| --- | --- | --- | --- |
| POST | `/fal/{owner}/{model}` | `queue.fal.run` | queue submit
(auto-mints request_id, returns envelope) |
| GET | `/fal/{owner}/{model}/requests/{id}/status` | `queue.fal.run` |
queue status (`COMPLETED`) |
| GET | `/fal/{owner}/{model}/requests/{id}` | `queue.fal.run` | queue
result (the matched JSON payload) |
| PUT | `/fal/{owner}/{model}/requests/{id}/cancel` | `queue.fal.run` |
`ALREADY_COMPLETED` |
| POST | `/fal/{owner}/{model}` | `fal.run` | sync run (returns JSON
directly, no envelope) |
| POST | `/fal/storage/upload/initiate` | `rest.fal.ai` /
`rest.alpha.fal.ai` | synthesised upload envelope stub |
| _legacy_ | `/fal/queue/submit/{model}`,
`/fal/queue/requests/{id}/...`, `/fal/run/{model}` | _(none)_ |
unchanged — `fal-audio.ts` |
## Usage
```ts
const mock = await LLMock.create({ port: 4010 });
mock.onFalQueue(/flux/, { images: [{ url: 'https://example.com/cat.png' }] });
mock.onFalQueue(/kling/, { video: { url: 'https://example.com/v.mp4' } });
// In your app:
fal.config({
requestMiddleware: async (req) => {
const original = new URL(req.url);
if (!FAL_HOSTS.has(original.hostname)) return req;
const rewritten = new URL('http://localhost:4010/fal');
rewritten.pathname += original.pathname;
rewritten.search = original.search;
return {
...req,
url: rewritten.toString(),
headers: { ...req.headers, 'x-fal-target-host': original.hostname },
};
},
});
await fal.queue.submit('fal-ai/flux/dev', { input: { prompt: 'a cat' } });
// → returns { request_id, status_url, response_url, cancel_url, queue_position: 0 }
```
In record mode (`record.providers.fal: 'https://queue.fal.run'`, or omit
and let the header drive it), unmatched requests proxy through to fal.ai
and are saved as `endpoint: "fal"` fixtures with the verbatim JSON
payload.
## Test plan
- [x] `pnpm test` — 2751 passed, 36 skipped, 78 test files
- [x] `pnpm run lint`, `pnpm run format:check` (the only formatting
warning is in pre-existing `.claude/settings.local.json`)
- [x] New `src/__tests__/fal.test.ts` (12 tests): queue lifecycle,
host-mirror routing, sync run, cancel, error fixtures, storage stub,
testId isolation, legacy back-compat, record + replay
- [x] Existing `src/__tests__/fal-audio.test.ts` still green
- [x] Existing `src/__tests__/recorder.test.ts` still green
- [ ] Smoke test against real `@fal-ai/client` in a downstream app
(TanStack Start)
## Notes
- `recorder.ts` already tees SSE streams progressively (added in a prior
commit), so the streaming-relay concern raised in the issue's first
comment is no longer a blocker for fal.
- The new helpers expose `onFalQueue` as the primary entry point and
`onFalRun` as a thin alias, since `fal.queue.*` is how clients actually
call fal in modern code.
🤖 Generated with [Claude Code](https://claude.com/claude-code)10 files changed
Lines changed: 967 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
77 | | - | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| |||
0 commit comments