@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.
- package/README.md +48 -4
- package/dist/adapters/capabilities.d.ts +10 -0
- package/dist/adapters/capabilities.d.ts.map +1 -0
- package/dist/adapters/capabilities.js +2 -0
- package/dist/adapters/capabilities.js.map +1 -0
- package/dist/adapters/chatAdapter.d.ts +38 -0
- package/dist/adapters/chatAdapter.d.ts.map +1 -0
- package/dist/adapters/chatAdapter.js +88 -0
- package/dist/adapters/chatAdapter.js.map +1 -0
- package/dist/adapters/directorAdapter.d.ts +62 -0
- package/dist/adapters/directorAdapter.d.ts.map +1 -0
- package/dist/adapters/directorAdapter.js +71 -0
- package/dist/adapters/directorAdapter.js.map +1 -0
- package/dist/adapters/imageAdapter.d.ts +44 -0
- package/dist/adapters/imageAdapter.d.ts.map +1 -0
- package/dist/adapters/imageAdapter.js +73 -0
- package/dist/adapters/imageAdapter.js.map +1 -0
- package/dist/adapters/index.d.ts +47 -0
- package/dist/adapters/index.d.ts.map +1 -0
- package/dist/adapters/index.js +41 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/registry.d.ts +98 -0
- package/dist/adapters/registry.d.ts.map +1 -0
- package/dist/adapters/registry.js +106 -0
- package/dist/adapters/registry.js.map +1 -0
- package/dist/adapters/worldAdapter.d.ts +142 -0
- package/dist/adapters/worldAdapter.d.ts.map +1 -0
- package/dist/adapters/worldAdapter.js +164 -0
- package/dist/adapters/worldAdapter.js.map +1 -0
- package/dist/admin.d.ts +200 -0
- package/dist/admin.d.ts.map +1 -0
- package/dist/admin.js +124 -0
- package/dist/admin.js.map +1 -0
- package/dist/api.d.ts +1299 -3
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +683 -95
- package/dist/api.js.map +1 -1
- package/dist/audio.d.ts +33 -1
- package/dist/audio.d.ts.map +1 -1
- package/dist/audio.js +4 -1
- package/dist/audio.js.map +1 -1
- package/dist/castProjection.d.ts +35 -0
- package/dist/castProjection.d.ts.map +1 -0
- package/dist/castProjection.js +80 -0
- package/dist/castProjection.js.map +1 -0
- package/dist/conversation/binding.d.ts +62 -0
- package/dist/conversation/binding.d.ts.map +1 -0
- package/dist/conversation/binding.js +51 -0
- package/dist/conversation/binding.js.map +1 -0
- package/dist/conversation/controller.d.ts +209 -0
- package/dist/conversation/controller.d.ts.map +1 -0
- package/dist/conversation/controller.js +342 -0
- package/dist/conversation/controller.js.map +1 -0
- package/dist/conversation/effectiveModel.d.ts +62 -0
- package/dist/conversation/effectiveModel.d.ts.map +1 -0
- package/dist/conversation/effectiveModel.js +73 -0
- package/dist/conversation/effectiveModel.js.map +1 -0
- package/dist/conversation/index.d.ts +27 -0
- package/dist/conversation/index.d.ts.map +1 -0
- package/dist/conversation/index.js +27 -0
- package/dist/conversation/index.js.map +1 -0
- package/dist/conversation/participant.d.ts +66 -0
- package/dist/conversation/participant.d.ts.map +1 -0
- package/dist/conversation/participant.js +36 -0
- package/dist/conversation/participant.js.map +1 -0
- package/dist/conversation/presence.d.ts +56 -0
- package/dist/conversation/presence.d.ts.map +1 -0
- package/dist/conversation/presence.js +34 -0
- package/dist/conversation/presence.js.map +1 -0
- package/dist/conversation/presenceBus.d.ts +42 -0
- package/dist/conversation/presenceBus.d.ts.map +1 -0
- package/dist/conversation/presenceBus.js +81 -0
- package/dist/conversation/presenceBus.js.map +1 -0
- package/dist/conversation/presenceRegistry.d.ts +49 -0
- package/dist/conversation/presenceRegistry.d.ts.map +1 -0
- package/dist/conversation/presenceRegistry.js +59 -0
- package/dist/conversation/presenceRegistry.js.map +1 -0
- package/dist/conversation/roster.d.ts +72 -0
- package/dist/conversation/roster.d.ts.map +1 -0
- package/dist/conversation/roster.js +69 -0
- package/dist/conversation/roster.js.map +1 -0
- package/dist/conversation/rosterProjection.d.ts +52 -0
- package/dist/conversation/rosterProjection.d.ts.map +1 -0
- package/dist/conversation/rosterProjection.js +74 -0
- package/dist/conversation/rosterProjection.js.map +1 -0
- package/dist/conversation/router.d.ts +74 -0
- package/dist/conversation/router.d.ts.map +1 -0
- package/dist/conversation/router.js +58 -0
- package/dist/conversation/router.js.map +1 -0
- package/dist/conversation/runtime.d.ts +64 -0
- package/dist/conversation/runtime.d.ts.map +1 -0
- package/dist/conversation/runtime.js +172 -0
- package/dist/conversation/runtime.js.map +1 -0
- package/dist/conversation/types.d.ts +99 -0
- package/dist/conversation/types.d.ts.map +1 -0
- package/dist/conversation/types.js +13 -0
- package/dist/conversation/types.js.map +1 -0
- package/dist/conversation/voice.d.ts +185 -0
- package/dist/conversation/voice.d.ts.map +1 -0
- package/dist/conversation/voice.js +307 -0
- package/dist/conversation/voice.js.map +1 -0
- package/dist/graphSchema.d.ts +260 -0
- package/dist/graphSchema.d.ts.map +1 -0
- package/dist/graphSchema.js +354 -0
- package/dist/graphSchema.js.map +1 -0
- package/dist/http.d.ts +20 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +57 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -1
- package/dist/platformAssistant.d.ts +155 -0
- package/dist/platformAssistant.d.ts.map +1 -0
- package/dist/platformAssistant.js +256 -0
- package/dist/platformAssistant.js.map +1 -0
- package/dist/reactor/abortUtils.d.ts +16 -0
- package/dist/reactor/abortUtils.d.ts.map +1 -0
- package/dist/reactor/abortUtils.js +26 -0
- package/dist/reactor/abortUtils.js.map +1 -0
- package/dist/reactor/applyScenePrompt.d.ts +75 -0
- package/dist/reactor/applyScenePrompt.d.ts.map +1 -0
- package/dist/reactor/applyScenePrompt.js +60 -0
- package/dist/reactor/applyScenePrompt.js.map +1 -0
- package/dist/reactor/chunkDistribution.d.ts +43 -0
- package/dist/reactor/chunkDistribution.d.ts.map +1 -0
- package/dist/reactor/chunkDistribution.js +85 -0
- package/dist/reactor/chunkDistribution.js.map +1 -0
- package/dist/reactor/heliosCommandDispatcher.d.ts.map +1 -1
- package/dist/reactor/heliosCommandDispatcher.js +10 -15
- package/dist/reactor/heliosCommandDispatcher.js.map +1 -1
- package/dist/reactor/index.d.ts +4 -0
- package/dist/reactor/index.d.ts.map +1 -1
- package/dist/reactor/index.js +7 -0
- package/dist/reactor/index.js.map +1 -1
- package/dist/reactor/longLiveCommandDispatcher.d.ts +103 -0
- package/dist/reactor/longLiveCommandDispatcher.d.ts.map +1 -0
- package/dist/reactor/longLiveCommandDispatcher.js +87 -0
- package/dist/reactor/longLiveCommandDispatcher.js.map +1 -0
- package/dist/reactor/sceneEntry.d.ts +40 -6
- package/dist/reactor/sceneEntry.d.ts.map +1 -1
- package/dist/reactor/sceneEntry.js +63 -24
- package/dist/reactor/sceneEntry.js.map +1 -1
- package/dist/reactor/scenesFromEnrichment.d.ts.map +1 -1
- package/dist/reactor/scenesFromEnrichment.js +2 -66
- package/dist/reactor/scenesFromEnrichment.js.map +1 -1
- package/dist/reactor/seedImageOrchestrator.d.ts +42 -2
- package/dist/reactor/seedImageOrchestrator.d.ts.map +1 -1
- package/dist/reactor/seedImageOrchestrator.js +173 -4
- package/dist/reactor/seedImageOrchestrator.js.map +1 -1
- package/dist/reactor/storyTimeline.d.ts +44 -0
- package/dist/reactor/storyTimeline.d.ts.map +1 -0
- package/dist/reactor/storyTimeline.js +95 -0
- package/dist/reactor/storyTimeline.js.map +1 -0
- package/dist/storyGraph.d.ts +70 -0
- package/dist/storyGraph.d.ts.map +1 -0
- package/dist/storyGraph.js +178 -0
- package/dist/storyGraph.js.map +1 -0
- package/dist/timeFormat.d.ts +10 -0
- package/dist/timeFormat.d.ts.map +1 -1
- package/dist/timeFormat.js +12 -0
- package/dist/timeFormat.js.map +1 -1
- package/dist/toolSchemas.d.ts +370 -0
- package/dist/toolSchemas.d.ts.map +1 -0
- package/dist/toolSchemas.js +182 -0
- package/dist/toolSchemas.js.map +1 -0
- package/dist/vision/VisionPublisher.d.ts +115 -0
- package/dist/vision/VisionPublisher.d.ts.map +1 -0
- package/dist/vision/VisionPublisher.js +475 -0
- package/dist/vision/VisionPublisher.js.map +1 -0
- package/dist/vision/VisionSourceController.d.ts +127 -0
- package/dist/vision/VisionSourceController.d.ts.map +1 -0
- package/dist/vision/VisionSourceController.js +284 -0
- package/dist/vision/VisionSourceController.js.map +1 -0
- package/dist/vision/credentials.d.ts +22 -0
- package/dist/vision/credentials.d.ts.map +1 -0
- package/dist/vision/credentials.js +34 -0
- package/dist/vision/credentials.js.map +1 -0
- package/dist/vision/index.d.ts +18 -0
- package/dist/vision/index.d.ts.map +1 -0
- package/dist/vision/index.js +13 -0
- package/dist/vision/index.js.map +1 -0
- package/dist/vision/sourceKinds.d.ts +65 -0
- package/dist/vision/sourceKinds.d.ts.map +1 -0
- package/dist/vision/sourceKinds.js +26 -0
- package/dist/vision/sourceKinds.js.map +1 -0
- package/dist/vision/startVisionPublisher.d.ts +38 -0
- package/dist/vision/startVisionPublisher.d.ts.map +1 -0
- package/dist/vision/startVisionPublisher.js +44 -0
- package/dist/vision/startVisionPublisher.js.map +1 -0
- package/dist/vision/types.d.ts +95 -0
- package/dist/vision/types.d.ts.map +1 -0
- package/dist/vision/types.js +12 -0
- package/dist/vision/types.js.map +1 -0
- package/dist/vision/visionScheduler.d.ts +63 -0
- package/dist/vision/visionScheduler.d.ts.map +1 -0
- package/dist/vision/visionScheduler.js +67 -0
- package/dist/vision/visionScheduler.js.map +1 -0
- package/dist/websocket.d.ts +195 -0
- package/dist/websocket.d.ts.map +1 -1
- package/dist/websocket.js +179 -1
- package/dist/websocket.js.map +1 -1
- package/package.json +11 -1
- package/src/__tests__/adapters-capabilities.test.ts +46 -0
- package/src/__tests__/adapters-chat.test.ts +172 -0
- package/src/__tests__/adapters-director.test.ts +95 -0
- package/src/__tests__/adapters-image.test.ts +133 -0
- package/src/__tests__/adapters-platform.test.ts +95 -0
- package/src/__tests__/adapters-registry.test.ts +189 -0
- package/src/__tests__/adapters-world.test.ts +296 -0
- package/src/__tests__/admin.test.ts +312 -0
- package/src/__tests__/adventures-party.test.ts +219 -0
- package/src/__tests__/adventures.test.ts +282 -0
- package/src/__tests__/api-pollers.test.ts +104 -0
- package/src/__tests__/api.clip-lookup.test.ts +64 -0
- package/src/__tests__/api.list-images-filters.test.ts +103 -0
- package/src/__tests__/api.test.ts +470 -0
- package/src/__tests__/audio.test.ts +65 -0
- package/src/__tests__/cast.test.ts +95 -0
- package/src/__tests__/castProjection.test.ts +167 -0
- package/src/__tests__/chat-auth.test.ts +91 -0
- package/src/__tests__/conversation/controller.test.ts +697 -0
- package/src/__tests__/conversation/effectiveModel.test.ts +169 -0
- package/src/__tests__/conversation/headlessRenderer.ts +46 -0
- package/src/__tests__/conversation/presenceBus.test.ts +236 -0
- package/src/__tests__/conversation/presenceRegistry.test.ts +73 -0
- package/src/__tests__/conversation/roster.test.ts +100 -0
- package/src/__tests__/conversation/rosterProjection.test.ts +142 -0
- package/src/__tests__/conversation/router.test.ts +81 -0
- package/src/__tests__/conversation/runtime.test.ts +206 -0
- package/src/__tests__/conversation/voice.test.ts +440 -0
- package/src/__tests__/documents.test.ts +360 -0
- package/src/__tests__/graphSchema.test.ts +326 -0
- package/src/__tests__/longLiveCommandDispatcher.test.ts +185 -0
- package/src/__tests__/platformAssistant.test.ts +484 -0
- package/src/__tests__/reactor/applyScenePrompt.test.ts +192 -0
- package/src/__tests__/reactor/heliosCommandDispatcher.test.ts +4 -3
- package/src/__tests__/reactor/sceneEntry.test.ts +134 -10
- package/src/__tests__/reactor/seedImageOrchestrator.test.ts +51 -16
- package/src/__tests__/reactor/storyTimeline.test.ts +186 -0
- package/src/__tests__/regeneration.test.ts +486 -0
- package/src/__tests__/stories.test.ts +171 -0
- package/src/__tests__/storyGraph.test.ts +134 -0
- package/src/__tests__/timeFormat.test.ts +22 -1
- package/src/__tests__/toolSchemas.test.ts +57 -0
- package/src/__tests__/tools-wrappers.test.ts +272 -0
- package/src/__tests__/videoGraph.test.ts +94 -0
- package/src/__tests__/websocket-vision.test.ts +265 -0
- package/src/__tests__/websocket.test.ts +213 -0
- package/src/adapters/capabilities.ts +24 -0
- package/src/adapters/chatAdapter.ts +114 -0
- package/src/adapters/directorAdapter.ts +126 -0
- package/src/adapters/imageAdapter.ts +100 -0
- package/src/adapters/index.ts +67 -0
- package/src/adapters/registry.ts +159 -0
- package/src/adapters/worldAdapter.ts +272 -0
- package/src/admin.ts +310 -0
- package/src/api.ts +1890 -162
- package/src/audio.ts +35 -0
- package/src/castProjection.ts +95 -0
- package/src/conversation/binding.ts +81 -0
- package/src/conversation/controller.ts +439 -0
- package/src/conversation/effectiveModel.ts +111 -0
- package/src/conversation/index.ts +27 -0
- package/src/conversation/participant.ts +71 -0
- package/src/conversation/presence.ts +60 -0
- package/src/conversation/presenceBus.ts +109 -0
- package/src/conversation/presenceRegistry.ts +83 -0
- package/src/conversation/roster.ts +111 -0
- package/src/conversation/rosterProjection.ts +98 -0
- package/src/conversation/router.ts +96 -0
- package/src/conversation/runtime.ts +188 -0
- package/src/conversation/types.ts +100 -0
- package/src/conversation/voice.ts +402 -0
- package/src/graphSchema.ts +532 -0
- package/src/http.ts +72 -0
- package/src/index.ts +12 -0
- package/src/platformAssistant.ts +347 -0
- package/src/reactor/abortUtils.ts +22 -0
- package/src/reactor/applyScenePrompt.ts +136 -0
- package/src/reactor/chunkDistribution.ts +98 -0
- package/src/reactor/heliosCommandDispatcher.ts +10 -18
- package/src/reactor/index.ts +7 -0
- package/src/reactor/longLiveCommandDispatcher.ts +156 -0
- package/src/reactor/sceneEntry.ts +104 -33
- package/src/reactor/scenesFromEnrichment.ts +11 -76
- package/src/reactor/seedImageOrchestrator.ts +200 -4
- package/src/reactor/storyTimeline.ts +127 -0
- package/src/storyGraph.ts +298 -0
- package/src/timeFormat.ts +13 -0
- package/src/toolSchemas.ts +225 -0
- package/src/vision/VisionPublisher.ts +553 -0
- package/src/vision/VisionSourceController.ts +354 -0
- package/src/vision/__tests__/VisionPublisher.kvs.test.ts +244 -0
- package/src/vision/__tests__/VisionPublisher.test.ts +301 -0
- package/src/vision/__tests__/VisionSourceController.test.ts +719 -0
- package/src/vision/__tests__/startVisionPublisher.test.ts +251 -0
- package/src/vision/__tests__/toVisionRtcCredentials.test.ts +113 -0
- package/src/vision/__tests__/visionScheduler.test.ts +149 -0
- package/src/vision/credentials.ts +39 -0
- package/src/vision/index.ts +44 -0
- package/src/vision/sourceKinds.ts +75 -0
- package/src/vision/startVisionPublisher.ts +64 -0
- package/src/vision/types.ts +101 -0
- package/src/vision/visionScheduler.ts +102 -0
- package/src/websocket.ts +334 -4
|
@@ -1,17 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Pure async orchestration of the "selected image → Reactor seed" flow.
|
|
3
3
|
*
|
|
4
|
+
* Wire shape: Helios's `set_image` command expects `{ image: <FileRef>,
|
|
5
|
+
* transition? }`. The FileRef comes from `reactor.uploadFile()`, which
|
|
6
|
+
* uploads the bytes via HTTP to Reactor's object store and returns a
|
|
7
|
+
* cheap reference. The command channel only carries the reference, so
|
|
8
|
+
* image size is decoupled from the 64KB-per-message data channel cap.
|
|
9
|
+
*
|
|
10
|
+
* Authoritative docs (anchors are stable; line numbers rot):
|
|
11
|
+
* - `ai_docs/reference/reactor/helios-schema.md` `#set_image` — set_image
|
|
12
|
+
* parameter table and behavior notes
|
|
13
|
+
* - `ai_docs/reference/reactor/file-uploads.md:33-54` — canonical
|
|
14
|
+
* uploadFile + set_image pattern
|
|
15
|
+
* - `ai_docs/reference/reactor/helios-schema.md` `#complete-example-image-to-video`
|
|
16
|
+
* — full image-to-video flow (now uses atomic `set_conditioning` to avoid
|
|
17
|
+
* a first-chunk race; `set_image`-then-`set_prompt` remains valid)
|
|
18
|
+
*
|
|
4
19
|
* Lifted from three React copies (#116):
|
|
5
20
|
* - `web/app/interactive/InteractiveController.tsx` (Phase 1)
|
|
6
21
|
* - `web/app/interactive/director/useSeedImageEffect.ts` (Phase 2)
|
|
7
22
|
* - `web/app/interactive/chat/page.tsx` (Phase 3)
|
|
8
23
|
*
|
|
9
|
-
*
|
|
24
|
+
* Flow:
|
|
10
25
|
* load blob from `apiClient.getSeedImageBlob`
|
|
11
26
|
* → wrap as `File` with extension derived from MIME (round 14 fix)
|
|
12
27
|
* → `reactor.uploadFile(file)` → `FileRef`
|
|
13
28
|
* → `reactor.sendCommand('set_image', { image: ref, transition })`
|
|
14
|
-
* → optionally await `waitForImageReady()` (model echoes `
|
|
29
|
+
* → optionally await `waitForImageReady()` (model echoes `image_set`
|
|
30
|
+
* with width/height)
|
|
15
31
|
*
|
|
16
32
|
* The pending-file queue (selected image arrived before the session was
|
|
17
33
|
* ready) lives in the React hook on top of this — it's UI-lifecycle
|
|
@@ -27,6 +43,13 @@ import type { ApiClient } from '../api';
|
|
|
27
43
|
* Minimal Reactor surface needed by the orchestrator. Same shape used
|
|
28
44
|
* by `HeliosReactorBridge` in `heliosCommandDispatcher.ts` — the
|
|
29
45
|
* dispatcher and orchestrator share the bridge by structural typing.
|
|
46
|
+
*
|
|
47
|
+
* `uploadFile` must return a real `FileRef` instance (or a structural
|
|
48
|
+
* equivalent the SDK recognises). The SDK detects FileRef values in
|
|
49
|
+
* the `data` argument of `sendCommand` and lifts them into a separate
|
|
50
|
+
* `uploads` envelope section before transmitting — that's the only
|
|
51
|
+
* way image bytes reach Helios. Passing a plain object that looks
|
|
52
|
+
* like a FileRef but lacks the class identity will silently no-op.
|
|
30
53
|
*/
|
|
31
54
|
export interface SeedImageReactorBridge {
|
|
32
55
|
sendCommand: (command: string, data: unknown) => Promise<void>;
|
|
@@ -36,6 +59,140 @@ export interface SeedImageReactorBridge {
|
|
|
36
59
|
) => Promise<unknown>;
|
|
37
60
|
}
|
|
38
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Reactor's WebRTC data channel rejects messages over 65,536 bytes
|
|
64
|
+
* (the SDK enforces this client-side before send). A 1080p PNG from
|
|
65
|
+
* the extract-image endpoint is ~1.27 MB raw → ~1.69 MB base64 → the
|
|
66
|
+
* whole `set_image` envelope blows past the limit and the SDK throws
|
|
67
|
+
* "Data channel message too large". We have to keep the base64
|
|
68
|
+
* payload below ~64 KB, which means raw bytes ≤ ~48 KB.
|
|
69
|
+
*
|
|
70
|
+
* Strategy: load the blob into an HTMLImageElement, draw to canvas
|
|
71
|
+
* at a clamped dimension, encode as JPEG with a quality ramp until
|
|
72
|
+
* the result fits the budget. We don't lose meaningful conditioning
|
|
73
|
+
* fidelity — Helios's image encoder downsamples internally to a
|
|
74
|
+
* fixed latent resolution regardless of input size.
|
|
75
|
+
*
|
|
76
|
+
* Falls back to the original blob in environments without a canvas
|
|
77
|
+
* backend (jsdom in tests). Browser callers always exercise the
|
|
78
|
+
* downscale path.
|
|
79
|
+
*/
|
|
80
|
+
const DEFAULT_MAX_RAW_BYTES = 45 * 1024;
|
|
81
|
+
const DEFAULT_MAX_DIMENSION = 512;
|
|
82
|
+
|
|
83
|
+
export interface DownscaleOptions {
|
|
84
|
+
/** Upper bound on the post-encode raw byte size. Default ~45 KB so
|
|
85
|
+
* the resulting base64 string fits within Reactor's 64 KB data
|
|
86
|
+
* channel message cap with envelope overhead to spare. */
|
|
87
|
+
maxRawBytes?: number;
|
|
88
|
+
/** Cap on the longer image dimension. Default 512 (covers 16:9
|
|
89
|
+
* 512×288 etc.). Helios's image encoder downsamples internally so
|
|
90
|
+
* going larger gains nothing for conditioning. */
|
|
91
|
+
maxDimension?: number;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export async function downscaleImageForDataChannel(
|
|
95
|
+
blob: Blob,
|
|
96
|
+
options: DownscaleOptions = {},
|
|
97
|
+
): Promise<Blob> {
|
|
98
|
+
const maxRawBytes = options.maxRawBytes ?? DEFAULT_MAX_RAW_BYTES;
|
|
99
|
+
const maxDimension = options.maxDimension ?? DEFAULT_MAX_DIMENSION;
|
|
100
|
+
|
|
101
|
+
// Already small enough — short-circuit. (The "already small" fast
|
|
102
|
+
// path also lets character-image thumbnails through unchanged.)
|
|
103
|
+
if (blob.size <= maxRawBytes) return blob;
|
|
104
|
+
|
|
105
|
+
// jsdom / SSR — no canvas backend available. Pass through so unit
|
|
106
|
+
// tests don't have to mock canvas; browser code never hits this.
|
|
107
|
+
if (typeof document === 'undefined') return blob;
|
|
108
|
+
const canvas = document.createElement('canvas');
|
|
109
|
+
const ctx = canvas.getContext('2d');
|
|
110
|
+
if (!ctx) return blob;
|
|
111
|
+
|
|
112
|
+
let img: HTMLImageElement;
|
|
113
|
+
try {
|
|
114
|
+
img = await loadBlobAsImage(blob);
|
|
115
|
+
} catch {
|
|
116
|
+
// Image decode failed — return the original. The downstream
|
|
117
|
+
// sendCommand will surface the data-channel-too-large error
|
|
118
|
+
// instead of a misleading decode error.
|
|
119
|
+
return blob;
|
|
120
|
+
}
|
|
121
|
+
const width = img.naturalWidth || img.width;
|
|
122
|
+
const height = img.naturalHeight || img.height;
|
|
123
|
+
if (!width || !height) return blob;
|
|
124
|
+
const scale = Math.min(1, maxDimension / Math.max(width, height));
|
|
125
|
+
const w = Math.max(1, Math.round(width * scale));
|
|
126
|
+
const h = Math.max(1, Math.round(height * scale));
|
|
127
|
+
canvas.width = w;
|
|
128
|
+
canvas.height = h;
|
|
129
|
+
ctx.drawImage(img, 0, 0, w, h);
|
|
130
|
+
|
|
131
|
+
for (const quality of [0.85, 0.75, 0.65, 0.55, 0.45, 0.35]) {
|
|
132
|
+
const encoded = await canvasToBlob(canvas, 'image/jpeg', quality);
|
|
133
|
+
if (!encoded) continue;
|
|
134
|
+
if (encoded.size <= maxRawBytes) return encoded;
|
|
135
|
+
}
|
|
136
|
+
// Last-ditch: take whatever the lowest quality yields. Even if it
|
|
137
|
+
// exceeds the budget, the SDK will surface the same data-channel
|
|
138
|
+
// error and the operator can pick a smaller frame.
|
|
139
|
+
const lastResort = await canvasToBlob(canvas, 'image/jpeg', 0.3);
|
|
140
|
+
return lastResort ?? blob;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function loadBlobAsImage(blob: Blob): Promise<HTMLImageElement> {
|
|
144
|
+
return new Promise((resolve, reject) => {
|
|
145
|
+
const url = URL.createObjectURL(blob);
|
|
146
|
+
const img = new Image();
|
|
147
|
+
img.onload = () => {
|
|
148
|
+
URL.revokeObjectURL(url);
|
|
149
|
+
resolve(img);
|
|
150
|
+
};
|
|
151
|
+
img.onerror = () => {
|
|
152
|
+
URL.revokeObjectURL(url);
|
|
153
|
+
reject(new Error('image decode failed'));
|
|
154
|
+
};
|
|
155
|
+
img.src = url;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function canvasToBlob(
|
|
160
|
+
canvas: HTMLCanvasElement,
|
|
161
|
+
type: string,
|
|
162
|
+
quality: number,
|
|
163
|
+
): Promise<Blob | null> {
|
|
164
|
+
return new Promise(resolve => {
|
|
165
|
+
canvas.toBlob(b => resolve(b), type, quality);
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Read a blob's bytes and base64-encode them. Uses FileReader (available
|
|
171
|
+
* in browsers AND jsdom) and strips the data-URL prefix so the output
|
|
172
|
+
* is the bare base64 payload, which is what Helios expects.
|
|
173
|
+
*/
|
|
174
|
+
export async function fileToBase64(file: Blob): Promise<string> {
|
|
175
|
+
return await new Promise<string>((resolve, reject) => {
|
|
176
|
+
const reader = new FileReader();
|
|
177
|
+
reader.onload = () => {
|
|
178
|
+
const result = reader.result;
|
|
179
|
+
if (typeof result !== 'string') {
|
|
180
|
+
reject(new Error('FileReader yielded non-string result'));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const commaIdx = result.indexOf(',');
|
|
184
|
+
if (commaIdx === -1 || !result.startsWith('data:')) {
|
|
185
|
+
reject(new Error('Unexpected FileReader data-URL shape'));
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
resolve(result.slice(commaIdx + 1));
|
|
189
|
+
};
|
|
190
|
+
reader.onerror = () =>
|
|
191
|
+
reject(reader.error ?? new Error('FileReader error'));
|
|
192
|
+
reader.readAsDataURL(file);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
39
196
|
export interface ApplySeedImageOptions {
|
|
40
197
|
/** `'cut'` (default) for the first seed in a session, `'blend'` for
|
|
41
198
|
* subsequent swaps so the model smooths the visual transition.
|
|
@@ -102,9 +259,48 @@ export async function sendSeedFileToReactor(
|
|
|
102
259
|
reactor: SeedImageReactorBridge,
|
|
103
260
|
options: Omit<ApplySeedImageOptions, 'signal' | 'filenameStem'> = {},
|
|
104
261
|
): Promise<void> {
|
|
105
|
-
|
|
262
|
+
// Canonical Helios image-to-video pattern (file-uploads.md:33,
|
|
263
|
+
// helios-schema.md#set_image): uploadFile → FileRef → set_image
|
|
264
|
+
// with `{ image: ref, transition? }`. Bytes go via HTTP to Reactor's
|
|
265
|
+
// object store; the command channel carries only the ref.
|
|
266
|
+
//
|
|
267
|
+
// **Transition is opt-in.** The JS spike that reliably reproduced
|
|
268
|
+
// image-to-video (`scripts/spikes/helios_set_image_spike.html`) sends
|
|
269
|
+
// `{ image: ref }` with NO `transition` field, matching the docs
|
|
270
|
+
// imperative sample verbatim. We default to omitting it; the
|
|
271
|
+
// InteractiveController seed-swap path passes `'blend'` explicitly
|
|
272
|
+
// when swapping mid-stream.
|
|
273
|
+
//
|
|
274
|
+
// **Do not wait for an ack here.** The standalone Python spike
|
|
275
|
+
// (`scripts/spikes/helios_set_image_spike.py`) confirmed that the
|
|
276
|
+
// success event is `image_accepted` (see `helios-schema.md` Messages
|
|
277
|
+
// table) and it only fires AFTER `start` is issued, during chunk 0
|
|
278
|
+
// generation. (Older docs called this `image_set`; upstream has since
|
|
279
|
+
// corrected this — keep this comment as the historical reason we
|
|
280
|
+
// don't gate on a pre-start ack.)
|
|
281
|
+
// Any wait between set_image and the subsequent set_prompt+start
|
|
282
|
+
// therefore deadlocks for `waitForImageReady`-timeout milliseconds
|
|
283
|
+
// and produces no value. Match the docs sample's back-to-back
|
|
284
|
+
// sequence.
|
|
285
|
+
//
|
|
286
|
+
// `waitForImageReady` is kept on the options surface for back-compat
|
|
287
|
+
// with InteractiveController's pending-seed flush (which DOES want
|
|
288
|
+
// to know when the seed has landed before letting the operator
|
|
289
|
+
// submit a prompt). Callers like bootstrapSceneInReactor that
|
|
290
|
+
// chain `start` right after should NOT pass it.
|
|
291
|
+
const uploadStart = Date.now();
|
|
106
292
|
const ref = await reactor.uploadFile(file);
|
|
107
|
-
|
|
293
|
+
console.info('[seedImage] uploaded', {
|
|
294
|
+
bytes: file.size,
|
|
295
|
+
mime: file.type,
|
|
296
|
+
ref,
|
|
297
|
+
elapsedMs: Date.now() - uploadStart,
|
|
298
|
+
});
|
|
299
|
+
const setImageData: { image: unknown; transition?: 'cut' | 'blend' } = {
|
|
300
|
+
image: ref,
|
|
301
|
+
};
|
|
302
|
+
if (options.transition) setImageData.transition = options.transition;
|
|
303
|
+
await reactor.sendCommand('set_image', setImageData);
|
|
108
304
|
if (options.waitForImageReady) await options.waitForImageReady();
|
|
109
305
|
}
|
|
110
306
|
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* storyTimeline — maps the COLLAPSED story projection (`SceneView[]`, the R5
|
|
3
|
+
* `detail=story` view shaped by `deriveScenes`) into an ordered array of
|
|
4
|
+
* `TimelineBeat`s ready to schedule onto a world model (Platform Restructure
|
|
5
|
+
* R8.1).
|
|
6
|
+
*
|
|
7
|
+
* This is the STORY-GRAPH-driven sibling of `mapEnrichmentToTimeline` (which
|
|
8
|
+
* maps raw Pegasus enrichment shapes). Both share ONE chunk-distribution
|
|
9
|
+
* algorithm (`./chunkDistribution`) so the chunk math can only break in one
|
|
10
|
+
* place. `mapEnrichmentToTimeline` stays the labelled FALLBACK for stories with
|
|
11
|
+
* no graph scenes (degenerate Pegasus-only videos) — R8.1 does not delete it.
|
|
12
|
+
*
|
|
13
|
+
* One beat per `SceneView`, ordered by the scene's natural `deriveScenes`
|
|
14
|
+
* ordering (act.index, then scene.index — already applied by the projection).
|
|
15
|
+
* The beat `prompt` is composed from the scene `summary` + featured-character
|
|
16
|
+
* names + location label; `sourceStart`/`sourceEnd` from `startTime`/`endTime`;
|
|
17
|
+
* `label` from the scene `title`, tagged with the source scene id so a Timeline
|
|
18
|
+
* UI (and a PlayCanvas renderer) can mark story-derived vs operator-added beats.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { SceneView } from '../storyGraph';
|
|
22
|
+
import type { TimelineBeat } from './scenesFromEnrichment';
|
|
23
|
+
import {
|
|
24
|
+
DEFAULT_MAX_CHUNKS,
|
|
25
|
+
DEFAULT_MIN_CHUNK_SPACING,
|
|
26
|
+
computeChunkIndices,
|
|
27
|
+
enforceSpacing,
|
|
28
|
+
type TimedItem,
|
|
29
|
+
} from './chunkDistribution';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Caller-tunable options for `deriveStoryTimeline`. Mirrors
|
|
33
|
+
* `MapEnrichmentOptions` so both mappers share one tunable surface. Defaults
|
|
34
|
+
* pull from the DIRECTOR adapter's chunk budget (R1) at the call site — pass
|
|
35
|
+
* `maxChunks` explicitly from the adapter; the constants here are the shared
|
|
36
|
+
* fallback, not a re-declared page literal.
|
|
37
|
+
*/
|
|
38
|
+
export interface StoryTimelineOptions {
|
|
39
|
+
/** Total chunks to fit. Beats distributed proportionally by scene timing. */
|
|
40
|
+
maxChunks?: number;
|
|
41
|
+
/** Skip beats closer than this many chunks to the previous kept beat. */
|
|
42
|
+
minChunkSpacing?: number;
|
|
43
|
+
/** Free-text prefix injected into every beat prompt (e.g. a style hint). */
|
|
44
|
+
styleHint?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Prefix the scene-id provenance tag onto a beat label so a Timeline UI can
|
|
49
|
+
* distinguish story-derived beats from operator-added ones. Kept as a single
|
|
50
|
+
* helper so the tag format has one source.
|
|
51
|
+
*/
|
|
52
|
+
function labelFor(scene: SceneView): string {
|
|
53
|
+
const title = scene.title.trim() || scene.sceneId;
|
|
54
|
+
return `${title} (${scene.sceneId})`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Compose the beat prompt from the collapsed scene: summary first, then the
|
|
59
|
+
* featured-character names and the location label appended as grounding
|
|
60
|
+
* context. A `styleHint` (when non-empty) is prepended with a single space and
|
|
61
|
+
* no trailing punctuation — the IDENTICAL rule `mapEnrichmentToTimeline` uses.
|
|
62
|
+
*/
|
|
63
|
+
function promptFor(scene: SceneView, styleHint: string): string {
|
|
64
|
+
const parts: string[] = [];
|
|
65
|
+
|
|
66
|
+
const summary = (scene.summary ?? '').trim();
|
|
67
|
+
if (summary) parts.push(summary);
|
|
68
|
+
else if (scene.title.trim()) parts.push(scene.title.trim());
|
|
69
|
+
|
|
70
|
+
const names = scene.featuredCharacters.map((c) => c.name.trim()).filter((n) => n.length > 0);
|
|
71
|
+
if (names.length > 0) parts.push(`Featuring ${names.join(', ')}.`);
|
|
72
|
+
|
|
73
|
+
const location = scene.location?.name.trim();
|
|
74
|
+
if (location) parts.push(`Location: ${location}.`);
|
|
75
|
+
|
|
76
|
+
const body = parts.join(' ');
|
|
77
|
+
return styleHint ? `${styleHint} ${body}` : body;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Map a collapsed story projection (`SceneView[]`) to scheduling beats.
|
|
82
|
+
*
|
|
83
|
+
* Defensive contract (matches `mapEnrichmentToTimeline`): an empty `scenes`
|
|
84
|
+
* array — or a non-positive `maxChunks` budget — returns `[]` (never throws).
|
|
85
|
+
*/
|
|
86
|
+
export function deriveStoryTimeline(
|
|
87
|
+
scenes: readonly SceneView[],
|
|
88
|
+
options: StoryTimelineOptions = {},
|
|
89
|
+
): TimelineBeat[] {
|
|
90
|
+
const maxChunks = options.maxChunks ?? DEFAULT_MAX_CHUNKS;
|
|
91
|
+
const minChunkSpacing = options.minChunkSpacing ?? DEFAULT_MIN_CHUNK_SPACING;
|
|
92
|
+
const styleHint = (options.styleHint ?? '').trim();
|
|
93
|
+
|
|
94
|
+
if (scenes.length === 0) return [];
|
|
95
|
+
// Hard floor — a maxChunks of 0 or negative makes no sense; treat as no budget.
|
|
96
|
+
if (maxChunks <= 0) return [];
|
|
97
|
+
|
|
98
|
+
// Project to the minimal timing shape the distribution rules read. A scene
|
|
99
|
+
// with a null startTime/endTime is left untimed (undefined) so the mapper
|
|
100
|
+
// falls back to even distribution when not every scene is timed — matching
|
|
101
|
+
// `mapEnrichmentToTimeline`'s behaviour.
|
|
102
|
+
const timed: TimedItem[] = scenes.map((s) => {
|
|
103
|
+
const item: TimedItem = {};
|
|
104
|
+
if (s.startTime !== null) item.start = s.startTime;
|
|
105
|
+
if (s.endTime !== null) item.end = s.endTime;
|
|
106
|
+
return item;
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const chunks = computeChunkIndices(timed, maxChunks);
|
|
110
|
+
const kept = enforceSpacing(
|
|
111
|
+
scenes.map((scene, i) => ({ scene, ...timed[i] })),
|
|
112
|
+
chunks,
|
|
113
|
+
minChunkSpacing,
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
return kept.map(({ item, chunk }) => {
|
|
117
|
+
const { scene } = item;
|
|
118
|
+
const beat: TimelineBeat = {
|
|
119
|
+
chunk,
|
|
120
|
+
prompt: promptFor(scene, styleHint),
|
|
121
|
+
label: labelFor(scene),
|
|
122
|
+
};
|
|
123
|
+
if (scene.startTime !== null) beat.sourceStart = scene.startTime;
|
|
124
|
+
if (scene.endTime !== null) beat.sourceEnd = scene.endTime;
|
|
125
|
+
return beat;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* storyGraph — pure, framework-agnostic projections over a get_video_graph
|
|
3
|
+
* payload (VideoGraphResponse), shared by the studio Scene inspector, Cast
|
|
4
|
+
* grid, AND the Director story-timeline mapper (Platform Restructure R8.1) so
|
|
5
|
+
* those read-only surfaces never drift.
|
|
6
|
+
*
|
|
7
|
+
* Composition-only: reads the EXISTING `GET /api/v1/tools/graph/{videoId}`
|
|
8
|
+
* payload and projects it into scene/cast view-models. No new endpoints, no
|
|
9
|
+
* mutations, no new graph machinery — only filtering + edge traversal.
|
|
10
|
+
*
|
|
11
|
+
* Edge direction is treated AGNOSTICALLY: get_video_graph emits real Cypher
|
|
12
|
+
* direction (source = startNode, target = endNode), but the structural
|
|
13
|
+
* relationships (CONTAINS/INCLUDES/FEATURES/SET_IN/RELATES_TO/ON_ARC) are
|
|
14
|
+
* matched by EITHER endpoint, so a future direction change in the writer cannot
|
|
15
|
+
* silently break these read-only views.
|
|
16
|
+
*
|
|
17
|
+
* Lifted verbatim from `web/app/components/studio/storyGraph.ts` (R8.1): the
|
|
18
|
+
* web module is now a thin re-export shim. Do NOT re-add logic to the web file.
|
|
19
|
+
*/
|
|
20
|
+
import type { GraphNode, GraphEdge, VideoGraphResponse } from './api';
|
|
21
|
+
import {
|
|
22
|
+
STUDIO_NODE_LABELS,
|
|
23
|
+
STUDIO_EDGE_TYPES,
|
|
24
|
+
STUDIO_NODE_PROPERTIES,
|
|
25
|
+
STUDIO_CHARACTER_TYPE,
|
|
26
|
+
} from './graphSchema';
|
|
27
|
+
|
|
28
|
+
// Note: character detection reads `Entity.type` directly against the studio
|
|
29
|
+
// read-surface constants (single-sourced in `graphSchema.ts`), so this pure
|
|
30
|
+
// module takes NO dependency on the heavy cytoscape-backed graph editor
|
|
31
|
+
// (`GraphCanvas`) — keeping the helper testable in isolation. The match is
|
|
32
|
+
// identical to the editor's `entityTypeOf(node) === 'character'`.
|
|
33
|
+
|
|
34
|
+
// ---------- view models ----------
|
|
35
|
+
|
|
36
|
+
export interface ShotView {
|
|
37
|
+
shotId: string;
|
|
38
|
+
caption: string;
|
|
39
|
+
index: number;
|
|
40
|
+
startTime: number | null;
|
|
41
|
+
endTime: number | null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface CharacterRef {
|
|
45
|
+
nodeId: string;
|
|
46
|
+
entityId: string;
|
|
47
|
+
name: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface LocationRef {
|
|
51
|
+
nodeId: string;
|
|
52
|
+
entityId: string;
|
|
53
|
+
name: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface SceneView {
|
|
57
|
+
nodeId: string;
|
|
58
|
+
sceneId: string;
|
|
59
|
+
title: string;
|
|
60
|
+
index: number;
|
|
61
|
+
startTime: number | null;
|
|
62
|
+
endTime: number | null;
|
|
63
|
+
summary: string | null;
|
|
64
|
+
actNodeId: string | null;
|
|
65
|
+
actTitle: string | null;
|
|
66
|
+
actIndex: number;
|
|
67
|
+
featuredCharacters: CharacterRef[];
|
|
68
|
+
location: LocationRef | null;
|
|
69
|
+
shots: ShotView[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface ArcView {
|
|
73
|
+
arcId: string;
|
|
74
|
+
title: string;
|
|
75
|
+
description: string | null;
|
|
76
|
+
index: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface CharacterView {
|
|
80
|
+
nodeId: string;
|
|
81
|
+
entityId: string;
|
|
82
|
+
name: string;
|
|
83
|
+
aliases: string[];
|
|
84
|
+
sceneIds: string[];
|
|
85
|
+
arcs: ArcView[];
|
|
86
|
+
relatedCharacterIds: string[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ---------- property readers (constant-driven, no literals) ----------
|
|
90
|
+
|
|
91
|
+
function str(node: GraphNode, key: string): string | null {
|
|
92
|
+
const v = node.properties[key];
|
|
93
|
+
return typeof v === 'string' ? v : null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function num(node: GraphNode, key: string): number | null {
|
|
97
|
+
const v = node.properties[key];
|
|
98
|
+
if (typeof v === 'number') return v;
|
|
99
|
+
if (typeof v === 'string' && v.trim() !== '' && Number.isFinite(Number(v))) {
|
|
100
|
+
return Number(v);
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function strList(node: GraphNode, key: string): string[] {
|
|
106
|
+
const v = node.properties[key];
|
|
107
|
+
return Array.isArray(v) ? v.filter((x): x is string => typeof x === 'string') : [];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** First numeric value across `keys` (in order), or null. */
|
|
111
|
+
function numCoalesce(node: GraphNode, keys: readonly string[]): number | null {
|
|
112
|
+
for (const key of keys) {
|
|
113
|
+
const v = num(node, key);
|
|
114
|
+
if (v !== null) return v;
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function isCharacter(node: GraphNode): boolean {
|
|
120
|
+
return (
|
|
121
|
+
node.label === STUDIO_NODE_LABELS.ENTITY &&
|
|
122
|
+
str(node, STUDIO_NODE_PROPERTIES.ENTITY_TYPE) === STUDIO_CHARACTER_TYPE
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function isLocation(node: GraphNode): boolean {
|
|
127
|
+
// A location entity is any non-character Entity reached by a SET_IN edge; we
|
|
128
|
+
// do not over-constrain on the exact `type` value (location vs place) — the
|
|
129
|
+
// SET_IN edge is the authoritative signal.
|
|
130
|
+
return node.label === STUDIO_NODE_LABELS.ENTITY && !isCharacter(node);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function businessIdOf(node: GraphNode, key: string): string {
|
|
134
|
+
return str(node, key) ?? node.id;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ---------- traversal helpers ----------
|
|
138
|
+
|
|
139
|
+
interface GraphIndex {
|
|
140
|
+
byId: Map<string, GraphNode>;
|
|
141
|
+
edges: GraphEdge[];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function indexGraph(graph: VideoGraphResponse): GraphIndex {
|
|
145
|
+
const byId = new Map<string, GraphNode>();
|
|
146
|
+
for (const node of graph.nodes) byId.set(node.id, node);
|
|
147
|
+
return { byId, edges: graph.edges };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* For a node id and a set of edge types, return the OTHER endpoint nodes of
|
|
152
|
+
* every matching edge, regardless of which side `nodeId` sits on.
|
|
153
|
+
*/
|
|
154
|
+
function neighbours(
|
|
155
|
+
index: GraphIndex,
|
|
156
|
+
nodeId: string,
|
|
157
|
+
edgeTypes: readonly string[],
|
|
158
|
+
): GraphNode[] {
|
|
159
|
+
const types = new Set(edgeTypes);
|
|
160
|
+
const out: GraphNode[] = [];
|
|
161
|
+
const seen = new Set<string>();
|
|
162
|
+
for (const edge of index.edges) {
|
|
163
|
+
if (!types.has(edge.type)) continue;
|
|
164
|
+
let otherId: string | null = null;
|
|
165
|
+
if (edge.source === nodeId) otherId = edge.target;
|
|
166
|
+
else if (edge.target === nodeId) otherId = edge.source;
|
|
167
|
+
if (otherId === null || seen.has(otherId)) continue;
|
|
168
|
+
const other = index.byId.get(otherId);
|
|
169
|
+
if (other) {
|
|
170
|
+
seen.add(otherId);
|
|
171
|
+
out.push(other);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return out;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Act→Scene and Scene→Shot containment is CONTAINS or INCLUDES. */
|
|
178
|
+
const CONTAINMENT_EDGES = [STUDIO_EDGE_TYPES.CONTAINS, STUDIO_EDGE_TYPES.INCLUDES] as const;
|
|
179
|
+
|
|
180
|
+
// ---------- scene derivation ----------
|
|
181
|
+
|
|
182
|
+
export function deriveScenes(graph: VideoGraphResponse): SceneView[] {
|
|
183
|
+
const index = indexGraph(graph);
|
|
184
|
+
const P = STUDIO_NODE_PROPERTIES;
|
|
185
|
+
const sceneNodes = graph.nodes.filter((n) => n.label === STUDIO_NODE_LABELS.SCENE);
|
|
186
|
+
|
|
187
|
+
const scenes: SceneView[] = sceneNodes.map((scene) => {
|
|
188
|
+
// Parent act (containment edge whose other endpoint is an Act node).
|
|
189
|
+
const actNode =
|
|
190
|
+
neighbours(index, scene.id, CONTAINMENT_EDGES).find(
|
|
191
|
+
(n) => n.label === STUDIO_NODE_LABELS.ACT,
|
|
192
|
+
) ?? null;
|
|
193
|
+
|
|
194
|
+
// Shots in this scene (containment edges to Shot nodes), sorted by index.
|
|
195
|
+
const shots: ShotView[] = neighbours(index, scene.id, CONTAINMENT_EDGES)
|
|
196
|
+
.filter((n) => n.label === STUDIO_NODE_LABELS.SHOT)
|
|
197
|
+
.map((sh) => ({
|
|
198
|
+
shotId: businessIdOf(sh, P.SHOT_ID),
|
|
199
|
+
caption: str(sh, P.CAPTION) ?? '',
|
|
200
|
+
index: num(sh, P.INDEX) ?? 0,
|
|
201
|
+
startTime: num(sh, P.START_TIME),
|
|
202
|
+
endTime: num(sh, P.END_TIME),
|
|
203
|
+
}))
|
|
204
|
+
.sort((a, b) => a.index - b.index);
|
|
205
|
+
|
|
206
|
+
// Featured characters (FEATURES edges to character entities).
|
|
207
|
+
const featuredCharacters: CharacterRef[] = neighbours(index, scene.id, [
|
|
208
|
+
STUDIO_EDGE_TYPES.FEATURES,
|
|
209
|
+
])
|
|
210
|
+
.filter(isCharacter)
|
|
211
|
+
.map((c) => ({
|
|
212
|
+
nodeId: c.id,
|
|
213
|
+
entityId: businessIdOf(c, P.ENTITY_ID),
|
|
214
|
+
name: str(c, P.NAME) ?? businessIdOf(c, P.ENTITY_ID),
|
|
215
|
+
}))
|
|
216
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
217
|
+
|
|
218
|
+
// Location (SET_IN edge to a non-character entity).
|
|
219
|
+
const locNode = neighbours(index, scene.id, [STUDIO_EDGE_TYPES.SET_IN]).find(isLocation);
|
|
220
|
+
const location: LocationRef | null = locNode
|
|
221
|
+
? {
|
|
222
|
+
nodeId: locNode.id,
|
|
223
|
+
entityId: businessIdOf(locNode, P.ENTITY_ID),
|
|
224
|
+
name: str(locNode, P.NAME) ?? businessIdOf(locNode, P.ENTITY_ID),
|
|
225
|
+
}
|
|
226
|
+
: null;
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
nodeId: scene.id,
|
|
230
|
+
sceneId: businessIdOf(scene, P.SCENE_ID),
|
|
231
|
+
title: str(scene, P.TITLE) ?? businessIdOf(scene, P.SCENE_ID),
|
|
232
|
+
index: num(scene, P.INDEX) ?? 0,
|
|
233
|
+
// Scene nodes store their time bounds as t0/t1 (MERGE_SCENE — the
|
|
234
|
+
// get_video_graph `detail=story` reader annotates `t0`/`t1`, NOT
|
|
235
|
+
// start/endTime; Shots use start/endTime). Read t0/t1 first, falling back
|
|
236
|
+
// to start/endTime defensively. Without this, every production SceneView
|
|
237
|
+
// had null times and deriveStoryTimeline (R8.1) lost proportional chunk
|
|
238
|
+
// placement (codex R8.1 P2).
|
|
239
|
+
startTime: numCoalesce(scene, [P.T0, P.START_TIME]),
|
|
240
|
+
endTime: numCoalesce(scene, [P.T1, P.END_TIME]),
|
|
241
|
+
summary: str(scene, P.SUMMARY),
|
|
242
|
+
actNodeId: actNode?.id ?? null,
|
|
243
|
+
actTitle: actNode ? str(actNode, P.TITLE) : null,
|
|
244
|
+
actIndex: actNode ? (num(actNode, P.INDEX) ?? 0) : Number.MAX_SAFE_INTEGER,
|
|
245
|
+
featuredCharacters,
|
|
246
|
+
location,
|
|
247
|
+
shots,
|
|
248
|
+
};
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
// Sort by act.index, then scene.index. Scenes with no act sort last.
|
|
252
|
+
scenes.sort((a, b) => a.actIndex - b.actIndex || a.index - b.index);
|
|
253
|
+
return scenes;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// ---------- cast derivation ----------
|
|
257
|
+
|
|
258
|
+
export function deriveCast(graph: VideoGraphResponse): CharacterView[] {
|
|
259
|
+
const index = indexGraph(graph);
|
|
260
|
+
const P = STUDIO_NODE_PROPERTIES;
|
|
261
|
+
const characterNodes = graph.nodes.filter(isCharacter);
|
|
262
|
+
|
|
263
|
+
const cast: CharacterView[] = characterNodes.map((char) => {
|
|
264
|
+
// Scenes this character is featured in (FEATURES edges to Scene nodes).
|
|
265
|
+
const sceneIds = neighbours(index, char.id, [STUDIO_EDGE_TYPES.FEATURES])
|
|
266
|
+
.filter((n) => n.label === STUDIO_NODE_LABELS.SCENE)
|
|
267
|
+
.map((s) => businessIdOf(s, P.SCENE_ID));
|
|
268
|
+
|
|
269
|
+
// Character arcs (ON_ARC edges to CharacterArc nodes), sorted by index.
|
|
270
|
+
const arcs: ArcView[] = neighbours(index, char.id, [STUDIO_EDGE_TYPES.ON_ARC])
|
|
271
|
+
.filter((n) => n.label === STUDIO_NODE_LABELS.CHARACTER_ARC)
|
|
272
|
+
.map((a) => ({
|
|
273
|
+
arcId: businessIdOf(a, P.ARC_ID),
|
|
274
|
+
title: str(a, P.TITLE) ?? businessIdOf(a, P.ARC_ID),
|
|
275
|
+
description: str(a, P.DESCRIPTION) ?? str(a, P.SUMMARY),
|
|
276
|
+
index: num(a, P.INDEX) ?? 0,
|
|
277
|
+
}))
|
|
278
|
+
.sort((x, y) => x.index - y.index);
|
|
279
|
+
|
|
280
|
+
// Related characters (RELATES_TO edges to other character entities).
|
|
281
|
+
const relatedCharacterIds = neighbours(index, char.id, [STUDIO_EDGE_TYPES.RELATES_TO])
|
|
282
|
+
.filter(isCharacter)
|
|
283
|
+
.map((c) => businessIdOf(c, P.ENTITY_ID));
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
nodeId: char.id,
|
|
287
|
+
entityId: businessIdOf(char, P.ENTITY_ID),
|
|
288
|
+
name: str(char, P.NAME) ?? businessIdOf(char, P.ENTITY_ID),
|
|
289
|
+
aliases: strList(char, P.ALIASES),
|
|
290
|
+
sceneIds,
|
|
291
|
+
arcs,
|
|
292
|
+
relatedCharacterIds,
|
|
293
|
+
};
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
cast.sort((a, b) => a.name.localeCompare(b.name));
|
|
297
|
+
return cast;
|
|
298
|
+
}
|
package/src/timeFormat.ts
CHANGED
|
@@ -55,3 +55,16 @@ export function formatTimestamp(
|
|
|
55
55
|
|
|
56
56
|
return base;
|
|
57
57
|
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Preset of {@link formatTimestamp} with `centiseconds: true` — the format
|
|
61
|
+
* used for citation timestamps in chat surfaces where sub-second word/turn
|
|
62
|
+
* boundaries are load-bearing.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* formatCitationTimestamp(83.45) // "1:23.45"
|
|
66
|
+
* formatCitationTimestamp(0) // "0:00.00"
|
|
67
|
+
*/
|
|
68
|
+
export function formatCitationTimestamp(seconds: number): string {
|
|
69
|
+
return formatTimestamp(seconds, { centiseconds: true });
|
|
70
|
+
}
|