Open History to see the last commit
1import { BaseLiveVideoAdapter } from '@tanstack/ai/adapters'
2import { generateId } from '@tanstack/ai-utils'
3import {
4 mintReactorSessionToken,
5 resolveReactorApiKey,
6 resolveReactorApiUrl,
7} from '../utils/client'
8import { REACTOR_VIDEO_SLUGS } from '../model-meta'
9import type { ReactorClientConfig } from '../utils/client'
10import type {
11 LiveVideoGenerationOptions,
12 LiveVideoGenerationResult,
13} from '@tanstack/ai'
14import type {
15 ReactorVideoModel,
16 ReactorVideoProviderOptions,
17} from '../model-meta'
18
19/**
20 * Reactor live-video adapter. Mints a session-scoped JWT so a browser can
21 * connect with `@reactor-team/js-sdk`, set the prompt, and start the stream.
22 *
23 * @example
24 * ```ts
25 * import { generateLiveVideo } from '@tanstack/ai'
26 * import { reactorVideo } from '@tanstack/ai-reactor'
27 *
28 * const live = await generateLiveVideo({
29 * adapter: reactorVideo('helios'),
30 * prompt: 'A neon cyberpunk city at night',
31 * })
32 * ```
33 */
34export class ReactorVideoAdapter<
35 TModel extends ReactorVideoModel,
36> extends BaseLiveVideoAdapter<TModel, ReactorVideoProviderOptions> {
37 readonly name = 'reactor' as const
38 private readonly clientConfig: ReactorClientConfig
39 private readonly apiKey: string
40
41 constructor(model: TModel, config: ReactorClientConfig = {}) {
42 super(model, {})
43 this.apiKey = resolveReactorApiKey(config)
44 this.clientConfig = config
45 }
46
47 async createLiveVideo(
48 options: LiveVideoGenerationOptions<ReactorVideoProviderOptions>,
49 ): Promise<LiveVideoGenerationResult> {
50 const { logger, prompt, abortSignal } = options
51 const modelSlug = REACTOR_VIDEO_SLUGS[this.model]
52 const apiUrl = resolveReactorApiUrl(this.clientConfig)
53
54 logger.request(
55 `activity=generateLiveVideo provider=reactor model=${modelSlug}`,
56 { provider: 'reactor', model: modelSlug },
57 )
58
59 try {
60 const token = await mintReactorSessionToken({
61 apiKey: this.apiKey,
62 apiUrl,
63 modelSlug,
64 fetchImpl: this.clientConfig.fetch,
65 abortSignal,
66 })
67
68 const result: LiveVideoGenerationResult = {
69 id: generateId('liveVideo'),
70 model: modelSlug,
71 token: token.jwt,
72 expiresAt: token.expires_at * 1000,
73 prompt,
74 status: 'ready',
75 }
76
77 logger.output(
78 `activity=generateLiveVideo provider=reactor model=${modelSlug}`,
79 { model: modelSlug, status: result.status },
80 )
81
82 return result
83 } catch (error) {
84 logger.errors('reactor.createLiveVideo failed', {
85 error,
86 source: 'reactor.createLiveVideo',
87 })
88 throw error
89 }
90 }
91}
92
93/**
94 * Create a Reactor live-video adapter. Reads `REACTOR_API_KEY` when `apiKey` is
95 * omitted.
96 */
97export function reactorVideo<TModel extends ReactorVideoModel>(
98 model: TModel,
99 config?: ReactorClientConfig,
100): ReactorVideoAdapter<TModel> {
101 return new ReactorVideoAdapter(model, config ?? {})
102}
103
104/**
105 * Create a Reactor live-video adapter with an explicit API key.
106 */
107export function createReactorVideo<TModel extends ReactorVideoModel>(
108 model: TModel,
109 apiKey: string,
110 config?: Omit<ReactorClientConfig, 'apiKey'>,
111): ReactorVideoAdapter<TModel> {
112 return new ReactorVideoAdapter(model, { ...config, apiKey })
113}