@escapeaihq/client-core 0.8.0 → 0.42.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.
Files changed (307) hide show
  1. package/README.md +48 -4
  2. package/dist/adapters/capabilities.d.ts +10 -0
  3. package/dist/adapters/capabilities.d.ts.map +1 -0
  4. package/dist/adapters/capabilities.js +2 -0
  5. package/dist/adapters/capabilities.js.map +1 -0
  6. package/dist/adapters/chatAdapter.d.ts +38 -0
  7. package/dist/adapters/chatAdapter.d.ts.map +1 -0
  8. package/dist/adapters/chatAdapter.js +88 -0
  9. package/dist/adapters/chatAdapter.js.map +1 -0
  10. package/dist/adapters/directorAdapter.d.ts +62 -0
  11. package/dist/adapters/directorAdapter.d.ts.map +1 -0
  12. package/dist/adapters/directorAdapter.js +71 -0
  13. package/dist/adapters/directorAdapter.js.map +1 -0
  14. package/dist/adapters/imageAdapter.d.ts +44 -0
  15. package/dist/adapters/imageAdapter.d.ts.map +1 -0
  16. package/dist/adapters/imageAdapter.js +73 -0
  17. package/dist/adapters/imageAdapter.js.map +1 -0
  18. package/dist/adapters/index.d.ts +47 -0
  19. package/dist/adapters/index.d.ts.map +1 -0
  20. package/dist/adapters/index.js +41 -0
  21. package/dist/adapters/index.js.map +1 -0
  22. package/dist/adapters/registry.d.ts +98 -0
  23. package/dist/adapters/registry.d.ts.map +1 -0
  24. package/dist/adapters/registry.js +106 -0
  25. package/dist/adapters/registry.js.map +1 -0
  26. package/dist/adapters/worldAdapter.d.ts +142 -0
  27. package/dist/adapters/worldAdapter.d.ts.map +1 -0
  28. package/dist/adapters/worldAdapter.js +164 -0
  29. package/dist/adapters/worldAdapter.js.map +1 -0
  30. package/dist/admin.d.ts +200 -0
  31. package/dist/admin.d.ts.map +1 -0
  32. package/dist/admin.js +124 -0
  33. package/dist/admin.js.map +1 -0
  34. package/dist/api.d.ts +1299 -3
  35. package/dist/api.d.ts.map +1 -1
  36. package/dist/api.js +683 -95
  37. package/dist/api.js.map +1 -1
  38. package/dist/audio.d.ts +33 -1
  39. package/dist/audio.d.ts.map +1 -1
  40. package/dist/audio.js +4 -1
  41. package/dist/audio.js.map +1 -1
  42. package/dist/castProjection.d.ts +35 -0
  43. package/dist/castProjection.d.ts.map +1 -0
  44. package/dist/castProjection.js +80 -0
  45. package/dist/castProjection.js.map +1 -0
  46. package/dist/conversation/binding.d.ts +62 -0
  47. package/dist/conversation/binding.d.ts.map +1 -0
  48. package/dist/conversation/binding.js +51 -0
  49. package/dist/conversation/binding.js.map +1 -0
  50. package/dist/conversation/controller.d.ts +209 -0
  51. package/dist/conversation/controller.d.ts.map +1 -0
  52. package/dist/conversation/controller.js +342 -0
  53. package/dist/conversation/controller.js.map +1 -0
  54. package/dist/conversation/effectiveModel.d.ts +62 -0
  55. package/dist/conversation/effectiveModel.d.ts.map +1 -0
  56. package/dist/conversation/effectiveModel.js +73 -0
  57. package/dist/conversation/effectiveModel.js.map +1 -0
  58. package/dist/conversation/index.d.ts +27 -0
  59. package/dist/conversation/index.d.ts.map +1 -0
  60. package/dist/conversation/index.js +27 -0
  61. package/dist/conversation/index.js.map +1 -0
  62. package/dist/conversation/participant.d.ts +66 -0
  63. package/dist/conversation/participant.d.ts.map +1 -0
  64. package/dist/conversation/participant.js +36 -0
  65. package/dist/conversation/participant.js.map +1 -0
  66. package/dist/conversation/presence.d.ts +56 -0
  67. package/dist/conversation/presence.d.ts.map +1 -0
  68. package/dist/conversation/presence.js +34 -0
  69. package/dist/conversation/presence.js.map +1 -0
  70. package/dist/conversation/presenceBus.d.ts +42 -0
  71. package/dist/conversation/presenceBus.d.ts.map +1 -0
  72. package/dist/conversation/presenceBus.js +81 -0
  73. package/dist/conversation/presenceBus.js.map +1 -0
  74. package/dist/conversation/presenceRegistry.d.ts +49 -0
  75. package/dist/conversation/presenceRegistry.d.ts.map +1 -0
  76. package/dist/conversation/presenceRegistry.js +59 -0
  77. package/dist/conversation/presenceRegistry.js.map +1 -0
  78. package/dist/conversation/roster.d.ts +72 -0
  79. package/dist/conversation/roster.d.ts.map +1 -0
  80. package/dist/conversation/roster.js +69 -0
  81. package/dist/conversation/roster.js.map +1 -0
  82. package/dist/conversation/rosterProjection.d.ts +52 -0
  83. package/dist/conversation/rosterProjection.d.ts.map +1 -0
  84. package/dist/conversation/rosterProjection.js +74 -0
  85. package/dist/conversation/rosterProjection.js.map +1 -0
  86. package/dist/conversation/router.d.ts +74 -0
  87. package/dist/conversation/router.d.ts.map +1 -0
  88. package/dist/conversation/router.js +58 -0
  89. package/dist/conversation/router.js.map +1 -0
  90. package/dist/conversation/runtime.d.ts +64 -0
  91. package/dist/conversation/runtime.d.ts.map +1 -0
  92. package/dist/conversation/runtime.js +172 -0
  93. package/dist/conversation/runtime.js.map +1 -0
  94. package/dist/conversation/types.d.ts +99 -0
  95. package/dist/conversation/types.d.ts.map +1 -0
  96. package/dist/conversation/types.js +13 -0
  97. package/dist/conversation/types.js.map +1 -0
  98. package/dist/conversation/voice.d.ts +185 -0
  99. package/dist/conversation/voice.d.ts.map +1 -0
  100. package/dist/conversation/voice.js +307 -0
  101. package/dist/conversation/voice.js.map +1 -0
  102. package/dist/graphSchema.d.ts +260 -0
  103. package/dist/graphSchema.d.ts.map +1 -0
  104. package/dist/graphSchema.js +354 -0
  105. package/dist/graphSchema.js.map +1 -0
  106. package/dist/http.d.ts +20 -0
  107. package/dist/http.d.ts.map +1 -0
  108. package/dist/http.js +57 -0
  109. package/dist/http.js.map +1 -0
  110. package/dist/index.d.ts +9 -0
  111. package/dist/index.d.ts.map +1 -1
  112. package/dist/index.js +12 -0
  113. package/dist/index.js.map +1 -1
  114. package/dist/platformAssistant.d.ts +155 -0
  115. package/dist/platformAssistant.d.ts.map +1 -0
  116. package/dist/platformAssistant.js +256 -0
  117. package/dist/platformAssistant.js.map +1 -0
  118. package/dist/reactor/abortUtils.d.ts +16 -0
  119. package/dist/reactor/abortUtils.d.ts.map +1 -0
  120. package/dist/reactor/abortUtils.js +26 -0
  121. package/dist/reactor/abortUtils.js.map +1 -0
  122. package/dist/reactor/applyScenePrompt.d.ts +75 -0
  123. package/dist/reactor/applyScenePrompt.d.ts.map +1 -0
  124. package/dist/reactor/applyScenePrompt.js +60 -0
  125. package/dist/reactor/applyScenePrompt.js.map +1 -0
  126. package/dist/reactor/chunkDistribution.d.ts +43 -0
  127. package/dist/reactor/chunkDistribution.d.ts.map +1 -0
  128. package/dist/reactor/chunkDistribution.js +85 -0
  129. package/dist/reactor/chunkDistribution.js.map +1 -0
  130. package/dist/reactor/heliosCommandDispatcher.d.ts.map +1 -1
  131. package/dist/reactor/heliosCommandDispatcher.js +10 -15
  132. package/dist/reactor/heliosCommandDispatcher.js.map +1 -1
  133. package/dist/reactor/index.d.ts +4 -0
  134. package/dist/reactor/index.d.ts.map +1 -1
  135. package/dist/reactor/index.js +7 -0
  136. package/dist/reactor/index.js.map +1 -1
  137. package/dist/reactor/longLiveCommandDispatcher.d.ts +103 -0
  138. package/dist/reactor/longLiveCommandDispatcher.d.ts.map +1 -0
  139. package/dist/reactor/longLiveCommandDispatcher.js +87 -0
  140. package/dist/reactor/longLiveCommandDispatcher.js.map +1 -0
  141. package/dist/reactor/sceneEntry.d.ts +40 -6
  142. package/dist/reactor/sceneEntry.d.ts.map +1 -1
  143. package/dist/reactor/sceneEntry.js +63 -24
  144. package/dist/reactor/sceneEntry.js.map +1 -1
  145. package/dist/reactor/scenesFromEnrichment.d.ts.map +1 -1
  146. package/dist/reactor/scenesFromEnrichment.js +2 -66
  147. package/dist/reactor/scenesFromEnrichment.js.map +1 -1
  148. package/dist/reactor/seedImageOrchestrator.d.ts +42 -2
  149. package/dist/reactor/seedImageOrchestrator.d.ts.map +1 -1
  150. package/dist/reactor/seedImageOrchestrator.js +173 -4
  151. package/dist/reactor/seedImageOrchestrator.js.map +1 -1
  152. package/dist/reactor/storyTimeline.d.ts +44 -0
  153. package/dist/reactor/storyTimeline.d.ts.map +1 -0
  154. package/dist/reactor/storyTimeline.js +95 -0
  155. package/dist/reactor/storyTimeline.js.map +1 -0
  156. package/dist/storyGraph.d.ts +70 -0
  157. package/dist/storyGraph.d.ts.map +1 -0
  158. package/dist/storyGraph.js +178 -0
  159. package/dist/storyGraph.js.map +1 -0
  160. package/dist/timeFormat.d.ts +10 -0
  161. package/dist/timeFormat.d.ts.map +1 -1
  162. package/dist/timeFormat.js +12 -0
  163. package/dist/timeFormat.js.map +1 -1
  164. package/dist/toolSchemas.d.ts +370 -0
  165. package/dist/toolSchemas.d.ts.map +1 -0
  166. package/dist/toolSchemas.js +182 -0
  167. package/dist/toolSchemas.js.map +1 -0
  168. package/dist/vision/VisionPublisher.d.ts +115 -0
  169. package/dist/vision/VisionPublisher.d.ts.map +1 -0
  170. package/dist/vision/VisionPublisher.js +475 -0
  171. package/dist/vision/VisionPublisher.js.map +1 -0
  172. package/dist/vision/VisionSourceController.d.ts +127 -0
  173. package/dist/vision/VisionSourceController.d.ts.map +1 -0
  174. package/dist/vision/VisionSourceController.js +284 -0
  175. package/dist/vision/VisionSourceController.js.map +1 -0
  176. package/dist/vision/credentials.d.ts +22 -0
  177. package/dist/vision/credentials.d.ts.map +1 -0
  178. package/dist/vision/credentials.js +34 -0
  179. package/dist/vision/credentials.js.map +1 -0
  180. package/dist/vision/index.d.ts +18 -0
  181. package/dist/vision/index.d.ts.map +1 -0
  182. package/dist/vision/index.js +13 -0
  183. package/dist/vision/index.js.map +1 -0
  184. package/dist/vision/sourceKinds.d.ts +65 -0
  185. package/dist/vision/sourceKinds.d.ts.map +1 -0
  186. package/dist/vision/sourceKinds.js +26 -0
  187. package/dist/vision/sourceKinds.js.map +1 -0
  188. package/dist/vision/startVisionPublisher.d.ts +38 -0
  189. package/dist/vision/startVisionPublisher.d.ts.map +1 -0
  190. package/dist/vision/startVisionPublisher.js +44 -0
  191. package/dist/vision/startVisionPublisher.js.map +1 -0
  192. package/dist/vision/types.d.ts +95 -0
  193. package/dist/vision/types.d.ts.map +1 -0
  194. package/dist/vision/types.js +12 -0
  195. package/dist/vision/types.js.map +1 -0
  196. package/dist/vision/visionScheduler.d.ts +63 -0
  197. package/dist/vision/visionScheduler.d.ts.map +1 -0
  198. package/dist/vision/visionScheduler.js +67 -0
  199. package/dist/vision/visionScheduler.js.map +1 -0
  200. package/dist/websocket.d.ts +195 -0
  201. package/dist/websocket.d.ts.map +1 -1
  202. package/dist/websocket.js +179 -1
  203. package/dist/websocket.js.map +1 -1
  204. package/package.json +11 -1
  205. package/src/__tests__/adapters-capabilities.test.ts +46 -0
  206. package/src/__tests__/adapters-chat.test.ts +172 -0
  207. package/src/__tests__/adapters-director.test.ts +95 -0
  208. package/src/__tests__/adapters-image.test.ts +133 -0
  209. package/src/__tests__/adapters-platform.test.ts +95 -0
  210. package/src/__tests__/adapters-registry.test.ts +189 -0
  211. package/src/__tests__/adapters-world.test.ts +296 -0
  212. package/src/__tests__/admin.test.ts +312 -0
  213. package/src/__tests__/adventures-party.test.ts +219 -0
  214. package/src/__tests__/adventures.test.ts +282 -0
  215. package/src/__tests__/api-pollers.test.ts +104 -0
  216. package/src/__tests__/api.clip-lookup.test.ts +64 -0
  217. package/src/__tests__/api.list-images-filters.test.ts +103 -0
  218. package/src/__tests__/api.test.ts +470 -0
  219. package/src/__tests__/audio.test.ts +65 -0
  220. package/src/__tests__/cast.test.ts +95 -0
  221. package/src/__tests__/castProjection.test.ts +167 -0
  222. package/src/__tests__/chat-auth.test.ts +91 -0
  223. package/src/__tests__/conversation/controller.test.ts +697 -0
  224. package/src/__tests__/conversation/effectiveModel.test.ts +169 -0
  225. package/src/__tests__/conversation/headlessRenderer.ts +46 -0
  226. package/src/__tests__/conversation/presenceBus.test.ts +236 -0
  227. package/src/__tests__/conversation/presenceRegistry.test.ts +73 -0
  228. package/src/__tests__/conversation/roster.test.ts +100 -0
  229. package/src/__tests__/conversation/rosterProjection.test.ts +142 -0
  230. package/src/__tests__/conversation/router.test.ts +81 -0
  231. package/src/__tests__/conversation/runtime.test.ts +206 -0
  232. package/src/__tests__/conversation/voice.test.ts +440 -0
  233. package/src/__tests__/documents.test.ts +360 -0
  234. package/src/__tests__/graphSchema.test.ts +326 -0
  235. package/src/__tests__/longLiveCommandDispatcher.test.ts +185 -0
  236. package/src/__tests__/platformAssistant.test.ts +484 -0
  237. package/src/__tests__/reactor/applyScenePrompt.test.ts +192 -0
  238. package/src/__tests__/reactor/heliosCommandDispatcher.test.ts +4 -3
  239. package/src/__tests__/reactor/sceneEntry.test.ts +134 -10
  240. package/src/__tests__/reactor/seedImageOrchestrator.test.ts +51 -16
  241. package/src/__tests__/reactor/storyTimeline.test.ts +186 -0
  242. package/src/__tests__/regeneration.test.ts +486 -0
  243. package/src/__tests__/stories.test.ts +171 -0
  244. package/src/__tests__/storyGraph.test.ts +134 -0
  245. package/src/__tests__/timeFormat.test.ts +22 -1
  246. package/src/__tests__/toolSchemas.test.ts +57 -0
  247. package/src/__tests__/tools-wrappers.test.ts +272 -0
  248. package/src/__tests__/videoGraph.test.ts +94 -0
  249. package/src/__tests__/websocket-vision.test.ts +265 -0
  250. package/src/__tests__/websocket.test.ts +213 -0
  251. package/src/adapters/capabilities.ts +24 -0
  252. package/src/adapters/chatAdapter.ts +114 -0
  253. package/src/adapters/directorAdapter.ts +126 -0
  254. package/src/adapters/imageAdapter.ts +100 -0
  255. package/src/adapters/index.ts +67 -0
  256. package/src/adapters/registry.ts +159 -0
  257. package/src/adapters/worldAdapter.ts +272 -0
  258. package/src/admin.ts +310 -0
  259. package/src/api.ts +1890 -162
  260. package/src/audio.ts +35 -0
  261. package/src/castProjection.ts +95 -0
  262. package/src/conversation/binding.ts +81 -0
  263. package/src/conversation/controller.ts +439 -0
  264. package/src/conversation/effectiveModel.ts +111 -0
  265. package/src/conversation/index.ts +27 -0
  266. package/src/conversation/participant.ts +71 -0
  267. package/src/conversation/presence.ts +60 -0
  268. package/src/conversation/presenceBus.ts +109 -0
  269. package/src/conversation/presenceRegistry.ts +83 -0
  270. package/src/conversation/roster.ts +111 -0
  271. package/src/conversation/rosterProjection.ts +98 -0
  272. package/src/conversation/router.ts +96 -0
  273. package/src/conversation/runtime.ts +188 -0
  274. package/src/conversation/types.ts +100 -0
  275. package/src/conversation/voice.ts +402 -0
  276. package/src/graphSchema.ts +532 -0
  277. package/src/http.ts +72 -0
  278. package/src/index.ts +12 -0
  279. package/src/platformAssistant.ts +347 -0
  280. package/src/reactor/abortUtils.ts +22 -0
  281. package/src/reactor/applyScenePrompt.ts +136 -0
  282. package/src/reactor/chunkDistribution.ts +98 -0
  283. package/src/reactor/heliosCommandDispatcher.ts +10 -18
  284. package/src/reactor/index.ts +7 -0
  285. package/src/reactor/longLiveCommandDispatcher.ts +156 -0
  286. package/src/reactor/sceneEntry.ts +104 -33
  287. package/src/reactor/scenesFromEnrichment.ts +11 -76
  288. package/src/reactor/seedImageOrchestrator.ts +200 -4
  289. package/src/reactor/storyTimeline.ts +127 -0
  290. package/src/storyGraph.ts +298 -0
  291. package/src/timeFormat.ts +13 -0
  292. package/src/toolSchemas.ts +225 -0
  293. package/src/vision/VisionPublisher.ts +553 -0
  294. package/src/vision/VisionSourceController.ts +354 -0
  295. package/src/vision/__tests__/VisionPublisher.kvs.test.ts +244 -0
  296. package/src/vision/__tests__/VisionPublisher.test.ts +301 -0
  297. package/src/vision/__tests__/VisionSourceController.test.ts +719 -0
  298. package/src/vision/__tests__/startVisionPublisher.test.ts +251 -0
  299. package/src/vision/__tests__/toVisionRtcCredentials.test.ts +113 -0
  300. package/src/vision/__tests__/visionScheduler.test.ts +149 -0
  301. package/src/vision/credentials.ts +39 -0
  302. package/src/vision/index.ts +44 -0
  303. package/src/vision/sourceKinds.ts +75 -0
  304. package/src/vision/startVisionPublisher.ts +64 -0
  305. package/src/vision/types.ts +101 -0
  306. package/src/vision/visionScheduler.ts +102 -0
  307. package/src/websocket.ts +334 -4
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Translate parsed LongLive-v2 commands into `reactor.sendCommand` calls.
3
+ *
4
+ * The {@link dispatchHeliosCommand} analogue for the Reactor LongLive-v2
5
+ * world model. SAME Reactor transport (JWT / `main_video` track) as Helios —
6
+ * only the `modelName` string differs on the bridge. The COMMAND VOCABULARY
7
+ * differs: LongLive is text-to-video with a hard `scene_cut` (a
8
+ * memory-purging transition Helios lacks) and NO image conditioning (there is
9
+ * NO `uploadFile` / `set_image` / `swap_seed` equivalent — text-only).
10
+ *
11
+ * Pure framework-agnostic helper: no React imports, no `@reactor-team/js-sdk`
12
+ * dependency. It reuses the structural {@link HeliosReactorBridge}
13
+ * `sendCommand` surface via `Pick` (no rename — additive-only) but takes NO
14
+ * `uploadFile` (LongLive has no image path).
15
+ *
16
+ * Failure policy mirrors the Helios dispatcher: every command is wrapped in a
17
+ * try/catch. A clean abort is silent; any other failure is logged and skipped
18
+ * so the chat session keeps working even when the world stream is unreachable.
19
+ */
20
+
21
+ import { isAbortError } from './abortUtils';
22
+ import type { HeliosReactorBridge } from './heliosCommandDispatcher';
23
+
24
+ /**
25
+ * Discriminated union of every LongLive-v2 world-steering action.
26
+ *
27
+ * Wire vocabulary (Reactor `sendCommand`):
28
+ * - `set_shot` — soft transition (also the OPENING shot before `start`).
29
+ * - `scene_cut` — HARD transition: purges memory + resets the per-scene chunk
30
+ * counter (Helios has no equivalent).
31
+ * - `schedule_shot` / `schedule_scene_cut` — schedule at a CUMULATIVE
32
+ * `session_chunk` index (snake_case `at_session_chunk` on the wire).
33
+ * Presumes a STARTED session (the dispatcher only emits `start` from the
34
+ * opening `set_shot`); Step 3's director schedules only after the opening
35
+ * shot, so a schedule before `start` is a caller error, not handled here.
36
+ * - `pause` / `resume` / `reset` — lifecycle.
37
+ * - `set_seed` — deterministic seed.
38
+ *
39
+ * NOTE: there is deliberately NO `swap_seed` variant — LongLive is
40
+ * text-to-video and has no image-conditioning surface.
41
+ */
42
+ export type LongLiveCommand =
43
+ | { type: 'set_shot'; prompt: string }
44
+ | { type: 'scene_cut'; prompt: string }
45
+ | { type: 'schedule_shot'; prompt: string; atSessionChunk: number }
46
+ | { type: 'schedule_scene_cut'; prompt: string; atSessionChunk: number }
47
+ | { type: 'pause' }
48
+ | { type: 'resume' }
49
+ | { type: 'reset' }
50
+ | { type: 'set_seed'; seed: number };
51
+
52
+ /**
53
+ * Inputs the LongLive dispatcher needs beyond the command itself.
54
+ *
55
+ * Reuses the structural {@link HeliosReactorBridge} `sendCommand` slot via
56
+ * `Pick` so client-core takes NO runtime SDK dependency and the same mockable
57
+ * bridge works for both dispatchers. Deliberately NARROWER than
58
+ * `HeliosToolContext`: no `apiClient` (no upsample/entity lookup here), no
59
+ * `uploadFile` (no image path), no `videoId` / `imageId` / `entityContext`
60
+ * (text-to-video, no `swap_seed`).
61
+ */
62
+ export interface LongLiveToolContext {
63
+ reactor: Pick<HeliosReactorBridge, 'sendCommand'>;
64
+ /** Whether the page has already issued a `start` command in this session.
65
+ * `set_shot` at the opening shot needs to know: if false, fire
66
+ * `set_shot` + `start` (this is the first shot); if true, fire `set_shot`
67
+ * only (re-emitting `start` while running is undocumented Reactor
68
+ * behavior — defensive to skip it). Mirrors the Helios `set_scene`
69
+ * chunk-0 + `hasStarted` guard.
70
+ *
71
+ * CALLER CONTRACT (the dispatcher does NOT mutate `ctx`): the caller MUST
72
+ * set this to `true` after the first `set_shot` dispatches, and clear it to
73
+ * `false` on `reset`. A caller that reuses one context object without
74
+ * updating `hasStarted` will re-fire `start` on every `set_shot`. (Step 3's
75
+ * director tracks this on its session state.) */
76
+ hasStarted?: boolean;
77
+ /** Optional `AbortSignal`: when aborted (before dispatch), the function
78
+ * returns silently without invoking `sendCommand`. Reactor `sendCommand`
79
+ * is NOT itself aborted (the SDK has no signal surface) — but a clean
80
+ * abort thrown from within is routed silently through `isAbortError`. */
81
+ signal?: AbortSignal;
82
+ }
83
+
84
+ /**
85
+ * Dispatch a single {@link LongLiveCommand} to the live Reactor session.
86
+ * Resolves with no value on both success and failure — the caller just awaits
87
+ * and continues to the next command (mirrors {@link dispatchHeliosCommand}).
88
+ *
89
+ * When `ctx.signal` is already aborted, returns silently without invoking
90
+ * `sendCommand`. A clean abort thrown mid-dispatch is also silent.
91
+ */
92
+ export async function dispatchLongLiveCommand(
93
+ cmd: LongLiveCommand,
94
+ ctx: LongLiveToolContext,
95
+ ): Promise<void> {
96
+ if (ctx.signal?.aborted) return;
97
+ try {
98
+ switch (cmd.type) {
99
+ case 'set_shot':
100
+ await ctx.reactor.sendCommand('set_shot', { prompt: cmd.prompt });
101
+ // Opening-shot-then-start: only emit `start` when the page hasn't
102
+ // fired it yet (re-emitting `start` while running is undocumented
103
+ // Reactor behavior). Mirrors the Helios set_scene chunk-0 guard.
104
+ if (!ctx.hasStarted) {
105
+ await ctx.reactor.sendCommand('start', {});
106
+ }
107
+ return;
108
+ case 'scene_cut':
109
+ await ctx.reactor.sendCommand('scene_cut', { prompt: cmd.prompt });
110
+ return;
111
+ case 'schedule_shot':
112
+ await ctx.reactor.sendCommand('schedule_shot', {
113
+ prompt: cmd.prompt,
114
+ at_session_chunk: cmd.atSessionChunk,
115
+ });
116
+ return;
117
+ case 'schedule_scene_cut':
118
+ await ctx.reactor.sendCommand('schedule_scene_cut', {
119
+ prompt: cmd.prompt,
120
+ at_session_chunk: cmd.atSessionChunk,
121
+ });
122
+ return;
123
+ case 'pause':
124
+ await ctx.reactor.sendCommand('pause', {});
125
+ return;
126
+ case 'resume':
127
+ await ctx.reactor.sendCommand('resume', {});
128
+ return;
129
+ case 'reset':
130
+ await ctx.reactor.sendCommand('reset', {});
131
+ return;
132
+ case 'set_seed':
133
+ await ctx.reactor.sendCommand('set_seed', { seed: cmd.seed });
134
+ return;
135
+ default: {
136
+ // Exhaustiveness check — TS complains at compile time if a new
137
+ // LongLiveCommand variant is added without updating this switch. The
138
+ // runtime branch is unreachable but keeps the narrowing honest.
139
+ const _exhaustive: never = cmd;
140
+ console.warn(
141
+ '[longLiveCommandDispatcher] unknown command type',
142
+ _exhaustive,
143
+ );
144
+ return;
145
+ }
146
+ }
147
+ } catch (err) {
148
+ // Abort is a clean signal — silent and stop here.
149
+ if (isAbortError(err)) return;
150
+ console.warn(
151
+ '[longLiveCommandDispatcher] command failed; skipping',
152
+ cmd,
153
+ err,
154
+ );
155
+ }
156
+ }
@@ -21,6 +21,7 @@ import type {
21
21
  ImageRecord,
22
22
  PersonaConfig,
23
23
  } from '../api';
24
+ import { isAbortError } from './abortUtils';
24
25
  import {
25
26
  aggregateEntityContext,
26
27
  aggregatePersonaContext,
@@ -30,22 +31,39 @@ import {
30
31
  sendSeedFileToReactor,
31
32
  type SeedImageReactorBridge,
32
33
  } from './seedImageOrchestrator';
34
+ import type { EntityContext, PersonaContext } from './types';
33
35
 
34
36
  export interface SceneEntryInput {
35
37
  /** The Seer video the user is watching. */
36
38
  videoId: string;
37
39
  /** Playhead position at the moment Step Into Scene was clicked. */
38
40
  timestamp: number;
39
- /** Caption for the bracketing shot, if enrichment is loaded. Feeds the
40
- * upsample as the user-supplied prompt — Pegasus's narrative summary
41
- * of THIS moment is a stronger upsample input than the entity name
42
- * alone. Falls back to a generic seed when absent. */
41
+ /** Caption for the bracketing shot, if enrichment is loaded. Only
42
+ * used when `includeCaption: true` — the default image-first path
43
+ * ignores it because Helios already sees the seed frame via
44
+ * `set_image`. */
43
45
  shotCaption?: string | null;
46
+ /** When `true`, the upsampler runs with `shotCaption` (or a fallback)
47
+ * as the input prompt and the resulting Helios paragraph is sent as
48
+ * `set_prompt`. Defaults to `false`, in which case the upsample step
49
+ * is bypassed entirely and `IMAGE_FIRST_DIRECTIVE` is sent as
50
+ * `set_prompt` instead — instructing Helios to continue the seed
51
+ * image faithfully with only natural motion. Ignored when
52
+ * `customPrompt` is also provided. */
53
+ includeCaption?: boolean;
54
+ /** Operator-authored prompt that overrides BOTH the IMAGE_FIRST_DIRECTIVE
55
+ * default and the caption-upsample path. When provided, this string is
56
+ * sent verbatim as the initial `set_prompt`. Enables the watch-page
57
+ * prompt-editor surface where the user previews and tweaks the prompt
58
+ * before stepping into a scene. Trim externally — empty / whitespace-only
59
+ * strings fall back to the includeCaption-driven defaults. */
60
+ customPrompt?: string | null;
44
61
  /** Most-recent SUCCEEDED Pegasus enrichment for the video, used to
45
- * derive `sceneContext` inside the entity grounding. */
62
+ * derive `sceneContext` inside the entity grounding. Only consulted
63
+ * on the caption-driven path. */
46
64
  enrichment: EnrichmentRecord | null;
47
65
  /** Active persona on the watch page. `null` means "no persona
48
- * grounding" — the upsample runs with entity-only context. */
66
+ * grounding". Only consulted on the caption-driven path. */
49
67
  persona: PersonaConfig | null;
50
68
  apiClient: ApiClient;
51
69
  signal?: AbortSignal;
@@ -63,13 +81,42 @@ export interface SceneEntryPayload {
63
81
  * (or any consumer) deep-link to `/images/{imageId}` for inspection
64
82
  * or wire a "View in gallery" affordance. */
65
83
  imageId: string;
84
+ /** Aggregated entity grounding for the extracted frame. Surfaced so
85
+ * in-modal prompt resubmits (via `applyScenePrompt`) ground on the
86
+ * same scene context that the initial upsample used. */
87
+ entityContext: EntityContext;
88
+ /** Persona grounding, when the operator had a persona selected.
89
+ * `null` means "no persona". Mirrors `prepareSceneEntry`'s
90
+ * internal calculation so consumers don't recompute it. */
91
+ personaContext: PersonaContext | null;
66
92
  }
67
93
 
68
- /** Fallback shot caption when enrichment is absent or the playhead is
69
- * outside any shot. Matches the cadence of the upsample's training
70
- * prompts so the LLM doesn't reject a one-word input. */
94
+ /** Fallback shot caption when `includeCaption: true` is set but
95
+ * enrichment is absent or the playhead is outside any shot. Matches
96
+ * the cadence of the upsample's training prompts so the LLM doesn't
97
+ * reject a one-word input. */
71
98
  const FALLBACK_SHOT_CAPTION = 'Step into this moment';
72
99
 
100
+ /** Default `set_prompt` for the image-first path. The seed frame is
101
+ * already loaded into Helios via `set_image`, so this prompt only
102
+ * needs to (a) tell the model to continue what it sees and (b)
103
+ * preserve the visual specifics. Written as a Helios-quality dense
104
+ * paragraph (~95 words) so we can send it directly without the
105
+ * upsampler round-trip. Follows the same prompt-guide rules
106
+ * encoded in `upsamplePrompt.ts::BASE_SYSTEM_RULES`: camera framing
107
+ * named explicitly, light by its effect, behavior over emotion. */
108
+ export const IMAGE_FIRST_DIRECTIVE =
109
+ 'Static medium shot, matching the seed image exactly. Preserve the ' +
110
+ 'subject\'s identity, position, posture, and wardrobe. Preserve the ' +
111
+ 'environment, depth, palette, and the way light falls across surfaces — ' +
112
+ 'the same shadows, the same highlights, the same atmospheric texture. ' +
113
+ 'Hold the original camera framing and lens character; the operator ' +
114
+ 'chose this exact moment. Apply only subtle natural motion: gentle ' +
115
+ 'breathing, slight weight shifts, ambient air moving fabric or hair, ' +
116
+ 'soft eye movement. No new actions, no new objects, no scene cuts, no ' +
117
+ 'introduced characters. Let the world settle into the frame the way it ' +
118
+ 'was captured and continue from there.';
119
+
73
120
  /** Sentinel entity name for the `ExtractImageRequest`. The backend
74
121
  * endpoint requires this field but uses `timestamp` for the actual
75
122
  * frame extraction when both are provided — `entity` survives only as
@@ -89,11 +136,14 @@ export async function prepareSceneEntry(
89
136
  videoId,
90
137
  timestamp,
91
138
  shotCaption,
139
+ includeCaption = false,
140
+ customPrompt,
92
141
  enrichment,
93
142
  persona,
94
143
  apiClient,
95
144
  signal,
96
145
  } = input;
146
+ const customPromptTrimmed = customPrompt?.trim() || null;
97
147
 
98
148
  // Step 1 — extract the frame server-side. The endpoint uses ffmpeg
99
149
  // against the source video's presigned URL and persists an
@@ -114,28 +164,37 @@ export async function prepareSceneEntry(
114
164
  );
115
165
  }
116
166
 
117
- // Step 2 — fan out the upsample call, blob fetch (through the
118
- // same-origin seed-image proxy), and the entity-grounding build in
119
- // parallel. Upsample uses text-only grounding (Pegasus scene context
120
- // is richer than single-frame visual would be); the blob proxy and
121
- // upsample are independent so running them serially would add ~1.5s
122
- // of round-trip latency for no benefit.
167
+ // Step 2 — build entity/persona context (cheap, always run so the
168
+ // returned payload carries them for follow-up prompts via
169
+ // HeliosWorldControls).
123
170
  const entityContext = aggregateEntityContext({
124
171
  imageRecord: synthesizeImageRecord(videoId, extract),
125
172
  enrichment: enrichment ?? undefined,
126
173
  });
127
174
  const personaContext = persona ? aggregatePersonaContext(persona) : null;
128
- const promptInput = shotCaption?.trim() || FALLBACK_SHOT_CAPTION;
129
175
 
130
- const [upsample, frameFile] = await Promise.all([
131
- apiClient.upsampleReactorPrompt(
132
- {
133
- prompt: promptInput,
134
- entityContext,
135
- ...(personaContext ? { personaContext } : {}),
136
- },
137
- { signal },
138
- ),
176
+ // Step 3 — fan out the frame-blob fetch alongside the prompt
177
+ // resolution. Three paths, in priority order:
178
+ // 1. customPrompt: operator-authored, sent verbatim. No upsample.
179
+ // 2. includeCaption: legacy caption-upsample (Bedrock round-trip).
180
+ // 3. default image-first: send IMAGE_FIRST_DIRECTIVE verbatim.
181
+ const promptPromise: Promise<string> = customPromptTrimmed
182
+ ? Promise.resolve(customPromptTrimmed)
183
+ : includeCaption
184
+ ? apiClient
185
+ .upsampleReactorPrompt(
186
+ {
187
+ prompt: shotCaption?.trim() || FALLBACK_SHOT_CAPTION,
188
+ entityContext,
189
+ ...(personaContext ? { personaContext } : {}),
190
+ },
191
+ { signal },
192
+ )
193
+ .then(res => res.upsampledPrompt)
194
+ : Promise.resolve(IMAGE_FIRST_DIRECTIVE);
195
+
196
+ const [upsampledPrompt, frameFile] = await Promise.all([
197
+ promptPromise,
139
198
  loadSeedImageFile(extract.imageId, apiClient, {
140
199
  signal,
141
200
  filenameStem: 'scene',
@@ -144,9 +203,11 @@ export async function prepareSceneEntry(
144
203
 
145
204
  return {
146
205
  frameFile,
147
- upsampledPrompt: upsample.upsampledPrompt,
206
+ upsampledPrompt,
148
207
  sourceTimestamp: extract.sourceTimestamp,
149
208
  imageId: extract.imageId,
209
+ entityContext,
210
+ personaContext,
150
211
  };
151
212
  }
152
213
 
@@ -171,7 +232,9 @@ export async function prepareSceneEntry(
171
232
  export async function bootstrapSceneInReactor(
172
233
  payload: SceneEntryPayload,
173
234
  bridge: SeedImageReactorBridge,
174
- options: { signal?: AbortSignal } = {},
235
+ options: {
236
+ signal?: AbortSignal;
237
+ } = {},
175
238
  ): Promise<void> {
176
239
  // Abort is an *expected* terminal state for this flow (user clicked
177
240
  // Back to video, modal unmounted, etc.). Internally we use exceptions
@@ -181,8 +244,21 @@ export async function bootstrapSceneInReactor(
181
244
  // shouldn't have to ALSO distinguish AbortError from real errors.
182
245
  try {
183
246
  if (options.signal?.aborted) return;
247
+ // Match the docs sample exactly (file-uploads.md:33,
248
+ // image-to-video-player.md:94): uploadFile → set_image →
249
+ // set_prompt → start back-to-back, no wait between commands.
250
+ // Per `scripts/spikes/helios_set_image_spike.py`, Helios emits
251
+ // `image_accepted` AFTER `start` fires (during chunk 0), so any
252
+ // wait between set_image and start deadlocks. Chunk 0 is
253
+ // text-only briefly; chunks 1+ render with the seed.
254
+ // No transition on the first seed of a session — matches the
255
+ // working JS spike (helios_set_image_spike.html) verbatim. The
256
+ // docs (helios-overview.md) say first-call transition is a no-op,
257
+ // and empirically including `'cut'` here correlated with the
258
+ // image never taking effect in the React app even though the
259
+ // spike (no transition) reliably loads the seed.
184
260
  await raceWithAbort(
185
- sendSeedFileToReactor(payload.frameFile, bridge, { transition: 'cut' }),
261
+ sendSeedFileToReactor(payload.frameFile, bridge, {}),
186
262
  options.signal,
187
263
  );
188
264
  if (options.signal?.aborted) return;
@@ -198,11 +274,6 @@ export async function bootstrapSceneInReactor(
198
274
  }
199
275
  }
200
276
 
201
- function isAbortError(err: unknown): boolean {
202
- if (err instanceof DOMException && err.name === 'AbortError') return true;
203
- return err instanceof Error && err.name === 'AbortError';
204
- }
205
-
206
277
  /**
207
278
  * Race a promise against an `AbortSignal`. When the signal fires, the
208
279
  * returned promise rejects with `AbortError` even if the underlying
@@ -34,6 +34,12 @@
34
34
 
35
35
  import type { EnrichmentRecord } from '../api';
36
36
  import { ENRICHMENT_STATUS_SUCCEEDED } from '../api';
37
+ import {
38
+ DEFAULT_MAX_CHUNKS,
39
+ DEFAULT_MIN_CHUNK_SPACING,
40
+ computeChunkIndices,
41
+ enforceSpacing,
42
+ } from './chunkDistribution';
37
43
 
38
44
  /**
39
45
  * A single beat on the director timeline. One beat = one
@@ -68,8 +74,10 @@ export interface MapEnrichmentOptions {
68
74
  styleHint?: string;
69
75
  }
70
76
 
71
- const DEFAULT_MAX_CHUNKS = 30;
72
- const DEFAULT_MIN_CHUNK_SPACING = 2;
77
+ // DEFAULT_MAX_CHUNKS / DEFAULT_MIN_CHUNK_SPACING and the chunk-distribution
78
+ // helpers (computeChunkIndices / enforceSpacing) now live in
79
+ // `./chunkDistribution` so `deriveStoryTimeline` shares the SAME algorithm (R8.1
80
+ // DRY — one place can break). They are imported above.
73
81
 
74
82
  interface RawScene {
75
83
  start?: number;
@@ -265,79 +273,6 @@ function extractScenes(
265
273
  return undefined;
266
274
  }
267
275
 
268
- /**
269
- * Compute the chunk index for each scene. When all scenes have valid
270
- * `start` timings and the time span is positive, scale proportionally.
271
- * Otherwise distribute evenly across `[0, maxChunks)`.
272
- */
273
- function computeChunkIndices(
274
- scenes: RawScene[],
275
- maxChunks: number,
276
- ): number[] {
277
- if (scenes.length === 0) return [];
278
- if (scenes.length === 1) return [0];
279
-
280
- const allTimed = scenes.every(
281
- (s) => typeof s.start === 'number' && typeof s.end === 'number',
282
- );
283
-
284
- // If timings are present and meaningful (positive span), scale.
285
- if (allTimed) {
286
- const starts = scenes.map((s) => s.start as number);
287
- const ends = scenes.map((s) => s.end as number);
288
- const minStart = Math.min(...starts);
289
- const maxEnd = Math.max(...ends);
290
- const span = maxEnd - minStart;
291
- if (span > 0) {
292
- return scenes.map((s, i) => {
293
- if (i === 0) return 0;
294
- const start = s.start as number;
295
- const frac = (start - minStart) / span;
296
- const chunk = Math.round(frac * (maxChunks - 1));
297
- // Clamp into [0, maxChunks - 1].
298
- return Math.max(0, Math.min(maxChunks - 1, chunk));
299
- });
300
- }
301
- }
302
-
303
- // Fallback — even distribution. First beat at 0, last beat at
304
- // maxChunks - 1, with linear spacing between.
305
- return scenes.map((_, i) => {
306
- if (i === 0) return 0;
307
- const frac = i / (scenes.length - 1);
308
- return Math.round(frac * (maxChunks - 1));
309
- });
310
- }
311
-
312
- /**
313
- * Apply `minChunkSpacing`. Walks in order, always keeps the first beat
314
- * (forced to chunk 0), drops any beat whose chunk is closer than
315
- * `minChunkSpacing` to the previously-kept beat.
316
- */
317
- function enforceSpacing(
318
- scenes: RawScene[],
319
- chunks: number[],
320
- minSpacing: number,
321
- ): { scene: RawScene; chunk: number; index: number }[] {
322
- const kept: { scene: RawScene; chunk: number; index: number }[] = [];
323
- for (let i = 0; i < scenes.length; i++) {
324
- const scene = scenes[i];
325
- let chunk = chunks[i];
326
- if (kept.length === 0) {
327
- // First beat is always pinned to chunk 0.
328
- chunk = 0;
329
- kept.push({ scene, chunk, index: i });
330
- continue;
331
- }
332
- const prev = kept[kept.length - 1].chunk;
333
- if (chunk - prev < minSpacing) {
334
- continue;
335
- }
336
- kept.push({ scene, chunk, index: i });
337
- }
338
- return kept;
339
- }
340
-
341
276
  export function mapEnrichmentToTimeline(
342
277
  enrichment: EnrichmentRecord,
343
278
  opts: MapEnrichmentOptions = {},
@@ -378,7 +313,7 @@ export function mapEnrichmentToTimeline(
378
313
  const chunks = computeChunkIndices(scenes, maxChunks);
379
314
  const kept = enforceSpacing(scenes, chunks, minChunkSpacing);
380
315
 
381
- return kept.map(({ scene, chunk, index }) => {
316
+ return kept.map(({ item: scene, chunk, index }) => {
382
317
  const description = scene.description as string; // coerced non-empty
383
318
  const prompt = styleHint ? `${styleHint} ${description}` : description;
384
319
  const beat: TimelineBeat = {