Open History to see the last commit
1import type { Reactor, ReactorMessage } from '@reactor-team/js-sdk'
2import type { ReactorWorldModel } from '@tanstack/ai-reactor'
3
4export function worldNeedsSeedImage(model: ReactorWorldModel): boolean {
5 return model === 'lingbot' || model === 'lingbot-world-2'
6}
7
8/**
9 * LingBot drifts the camera unless every prompt pins it. The Reactor prompt
10 * guide calls this the camera layer. Keep it verbatim across prompts.
11 */
12const LINGBOT_CAMERA =
13 'First-person view at standing eye height, horizon level across the middle of the frame, camera height constant. The camera does not move on its own; movement input is the only source of motion.'
14
15/** Compose a LingBot prompt: scene base, optional steer detail, camera layer. */
16export function lingbotPrompt(base: string, detail = ''): string {
17 return [base.trim(), detail.trim(), LINGBOT_CAMERA]
18 .filter((part) => part.length > 0)
19 .join(' ')
20}
21
22/** Default is 5 deg per latent frame, which spins past the target on a tap. */
23export const LINGBOT_ROTATION_SPEED_DEG = 1.5
24
25export function liveAcceptsSeedImage(model: string): boolean {
26 return model === 'helios'
27}
28
29export function isRecorderUnavailable(error: unknown): boolean {
30 if (typeof error === 'object' && error !== null && 'code' in error) {
31 return error.code === 'RECORDER_DISABLED'
32 }
33 return /recorder.*(disabled|not enabled)|RECORDER_DISABLED/i.test(
34 error instanceof Error ? error.message : String(error),
35 )
36}
37
38export function commandErrorMessage(data: unknown): string | null {
39 if (typeof data === 'string' && data.length > 0) return data
40 if (typeof data !== 'object' || data === null || !('reason' in data)) {
41 return null
42 }
43 const reason = data.reason
44 return typeof reason === 'string' && reason.length > 0 ? reason : null
45}
46
47export async function setReactorImage(
48 reactor: Reactor,
49 file: File,
50): Promise<void> {
51 const image = await reactor.uploadFile(file)
52 await reactor.sendCommand('set_image', { image })
53}
54
55export function watchReactorFailure(
56 reactor: Reactor,
57 onFailure: (message: string) => void,
58): () => void {
59 const onError = (err: Error) => {
60 onFailure(err.message)
61 }
62 const onMessage = (msg: ReactorMessage) => {
63 if (msg.type !== 'command_error') return
64 onFailure(commandErrorMessage(msg.data) ?? 'Command failed')
65 }
66 const onStatus = (status: string) => {
67 if (status === 'disconnected') onFailure('Session disconnected')
68 }
69 reactor.on('error', onError)
70 reactor.on('message', onMessage)
71 reactor.on('statusChanged', onStatus)
72 return () => {
73 reactor.off('error', onError)
74 reactor.off('message', onMessage)
75 reactor.off('statusChanged', onStatus)
76 }
77}
78
79export type LingbotAxis =
80 | 'move_longitudinal'
81 | 'move_lateral'
82 | 'look_horizontal'
83 | 'look_vertical'
84
85/**
86 * Set one held camera axis. Values persist until you send `idle`.
87 * LingBot has one movement axis (`set_movement`). LingBot World 2 splits it.
88 */
89export async function setLingbotAxis(
90 reactor: Reactor,
91 model: ReactorWorldModel,
92 axis: LingbotAxis,
93 value: string,
94): Promise<void> {
95 if (model === 'lingbot' && axis.startsWith('move_')) {
96 await reactor.sendCommand('set_movement', { movement: value })
97 return
98 }
99 await reactor.sendCommand(`set_${axis}`, { [axis]: value })
100}