@escapeaihq/client-core 0.6.1 → 0.7.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 (103) hide show
  1. package/README.md +27 -2
  2. package/dist/api.d.ts +102 -2
  3. package/dist/api.d.ts.map +1 -1
  4. package/dist/api.js +92 -0
  5. package/dist/api.js.map +1 -1
  6. package/dist/audio.d.ts +1 -1
  7. package/dist/index.d.ts +2 -0
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +2 -0
  10. package/dist/index.js.map +1 -1
  11. package/dist/reactor/entityContext.d.ts +34 -0
  12. package/dist/reactor/entityContext.d.ts.map +1 -0
  13. package/dist/reactor/entityContext.js +119 -0
  14. package/dist/reactor/entityContext.js.map +1 -0
  15. package/dist/reactor/entitySearch.d.ts +71 -0
  16. package/dist/reactor/entitySearch.d.ts.map +1 -0
  17. package/dist/reactor/entitySearch.js +93 -0
  18. package/dist/reactor/entitySearch.js.map +1 -0
  19. package/dist/reactor/heliosCommandDispatcher.d.ts +98 -0
  20. package/dist/reactor/heliosCommandDispatcher.d.ts.map +1 -0
  21. package/dist/reactor/heliosCommandDispatcher.js +181 -0
  22. package/dist/reactor/heliosCommandDispatcher.js.map +1 -0
  23. package/dist/reactor/heliosTools.d.ts +156 -0
  24. package/dist/reactor/heliosTools.d.ts.map +1 -0
  25. package/dist/reactor/heliosTools.js +241 -0
  26. package/dist/reactor/heliosTools.js.map +1 -0
  27. package/dist/reactor/index.d.ts +17 -0
  28. package/dist/reactor/index.d.ts.map +1 -0
  29. package/dist/reactor/index.js +17 -0
  30. package/dist/reactor/index.js.map +1 -0
  31. package/dist/reactor/promptAddonRegistry.d.ts +37 -0
  32. package/dist/reactor/promptAddonRegistry.d.ts.map +1 -0
  33. package/dist/reactor/promptAddonRegistry.js +38 -0
  34. package/dist/reactor/promptAddonRegistry.js.map +1 -0
  35. package/dist/reactor/regenTemplate.d.ts +23 -0
  36. package/dist/reactor/regenTemplate.d.ts.map +1 -0
  37. package/dist/reactor/regenTemplate.js +103 -0
  38. package/dist/reactor/regenTemplate.js.map +1 -0
  39. package/dist/reactor/sceneEntry.d.ts +79 -0
  40. package/dist/reactor/sceneEntry.d.ts.map +1 -0
  41. package/dist/reactor/sceneEntry.js +177 -0
  42. package/dist/reactor/sceneEntry.js.map +1 -0
  43. package/dist/reactor/scenesFromEnrichment.d.ts +67 -0
  44. package/dist/reactor/scenesFromEnrichment.d.ts.map +1 -0
  45. package/dist/reactor/scenesFromEnrichment.js +344 -0
  46. package/dist/reactor/scenesFromEnrichment.js.map +1 -0
  47. package/dist/reactor/seedImageOrchestrator.d.ts +81 -0
  48. package/dist/reactor/seedImageOrchestrator.d.ts.map +1 -0
  49. package/dist/reactor/seedImageOrchestrator.js +79 -0
  50. package/dist/reactor/seedImageOrchestrator.js.map +1 -0
  51. package/dist/reactor/toolPromptAddon.d.ts +44 -0
  52. package/dist/reactor/toolPromptAddon.d.ts.map +1 -0
  53. package/dist/reactor/toolPromptAddon.js +129 -0
  54. package/dist/reactor/toolPromptAddon.js.map +1 -0
  55. package/dist/reactor/toolPromptAddonId.d.ts +10 -0
  56. package/dist/reactor/toolPromptAddonId.d.ts.map +1 -0
  57. package/dist/reactor/toolPromptAddonId.js +10 -0
  58. package/dist/reactor/toolPromptAddonId.js.map +1 -0
  59. package/dist/reactor/types.d.ts +149 -0
  60. package/dist/reactor/types.d.ts.map +1 -0
  61. package/dist/reactor/types.js +2 -0
  62. package/dist/reactor/types.js.map +1 -0
  63. package/dist/reactor/upsamplePrompt.d.ts +30 -0
  64. package/dist/reactor/upsamplePrompt.d.ts.map +1 -0
  65. package/dist/reactor/upsamplePrompt.js +118 -0
  66. package/dist/reactor/upsamplePrompt.js.map +1 -0
  67. package/dist/timeFormat.d.ts +28 -0
  68. package/dist/timeFormat.d.ts.map +1 -0
  69. package/dist/timeFormat.js +48 -0
  70. package/dist/timeFormat.js.map +1 -0
  71. package/package.json +1 -1
  72. package/src/__tests__/reactor/__snapshots__/regenTemplate.test.ts.snap +5 -0
  73. package/src/__tests__/reactor/__snapshots__/scenesFromEnrichment.test.ts.snap +34 -0
  74. package/src/__tests__/reactor/api-reactor.test.ts +253 -0
  75. package/src/__tests__/reactor/entityContext.test.ts +237 -0
  76. package/src/__tests__/reactor/entitySearch.test.ts +223 -0
  77. package/src/__tests__/reactor/heliosCommandDispatcher.test.ts +438 -0
  78. package/src/__tests__/reactor/heliosTools.test.ts +164 -0
  79. package/src/__tests__/reactor/promptAddonRegistry.test.ts +40 -0
  80. package/src/__tests__/reactor/regenTemplate.test.ts +156 -0
  81. package/src/__tests__/reactor/sceneEntry.test.ts +342 -0
  82. package/src/__tests__/reactor/scenesFromEnrichment.test.ts +760 -0
  83. package/src/__tests__/reactor/seedImageOrchestrator.test.ts +139 -0
  84. package/src/__tests__/reactor/toolPromptAddon.test.ts +104 -0
  85. package/src/__tests__/reactor/upsamplePrompt.test.ts +245 -0
  86. package/src/__tests__/timeFormat.test.ts +80 -0
  87. package/src/api.ts +170 -2
  88. package/src/index.ts +2 -0
  89. package/src/reactor/entityContext.ts +147 -0
  90. package/src/reactor/entitySearch.ts +139 -0
  91. package/src/reactor/heliosCommandDispatcher.ts +293 -0
  92. package/src/reactor/heliosTools.ts +260 -0
  93. package/src/reactor/index.ts +17 -0
  94. package/src/reactor/promptAddonRegistry.ts +52 -0
  95. package/src/reactor/regenTemplate.ts +142 -0
  96. package/src/reactor/sceneEntry.ts +267 -0
  97. package/src/reactor/scenesFromEnrichment.ts +393 -0
  98. package/src/reactor/seedImageOrchestrator.ts +132 -0
  99. package/src/reactor/toolPromptAddon.ts +135 -0
  100. package/src/reactor/toolPromptAddonId.ts +9 -0
  101. package/src/reactor/types.ts +158 -0
  102. package/src/reactor/upsamplePrompt.ts +154 -0
  103. package/src/timeFormat.ts +57 -0
@@ -0,0 +1,342 @@
1
+ /**
2
+ * Tests for the Step-Into-Scene orchestrator — `prepareSceneEntry` and
3
+ * `bootstrapSceneInReactor` from `../../reactor/sceneEntry`.
4
+ *
5
+ * Coverage:
6
+ * - prepareSceneEntry calls extractImage with timestamp + sentinel entity
7
+ * - prepareSceneEntry feeds the upsampler the shot caption as the prompt
8
+ * - prepareSceneEntry falls back to the generic caption when none is given
9
+ * - prepareSceneEntry fetches the frame via getSeedImageBlob (proxy)
10
+ * rather than a direct presigned S3 fetch (browser CORS)
11
+ * - prepareSceneEntry omits personaContext when persona is null
12
+ * - prepareSceneEntry threads the signal into all three calls
13
+ * - prepareSceneEntry rejects with a clear error when extractImage
14
+ * returns no imageId (best-effort persistence failed)
15
+ * - prepareSceneEntry returns imageId in the payload
16
+ * - bootstrapSceneInReactor runs uploadFile → set_image → set_prompt → start
17
+ * - bootstrapSceneInReactor short-circuits when aborted between phases
18
+ */
19
+
20
+ import type { ApiClient, ExtractImageResponse, PersonaConfig } from '../../api';
21
+ import {
22
+ bootstrapSceneInReactor,
23
+ prepareSceneEntry,
24
+ } from '../../reactor/sceneEntry';
25
+
26
+ type AnyMock = jest.Mock;
27
+
28
+ const SAMPLE_EXTRACT: ExtractImageResponse = {
29
+ traceId: 'trace-extract',
30
+ imageId: 'img-scene-1',
31
+ imageUrl: 'https://s3.example/scene.png?sig=abc',
32
+ imageS3Key: 'extracted/scene.png',
33
+ sourceTimestamp: 42.0,
34
+ entity: 'scene',
35
+ citation: { videoId: 'vid-1' } as ExtractImageResponse['citation'],
36
+ source: { system: 'test' },
37
+ };
38
+
39
+ function makeApiClient(overrides?: {
40
+ extract?: ExtractImageResponse;
41
+ upsampledPrompt?: string;
42
+ seedBlob?: Blob;
43
+ }): ApiClient {
44
+ return {
45
+ extractImage: jest
46
+ .fn()
47
+ .mockResolvedValue(overrides?.extract ?? SAMPLE_EXTRACT),
48
+ upsampleReactorPrompt: jest.fn().mockResolvedValue({
49
+ traceId: 'trace-upsample',
50
+ upsampledPrompt: overrides?.upsampledPrompt ?? 'A long Helios paragraph…',
51
+ }),
52
+ getSeedImageBlob: jest
53
+ .fn()
54
+ .mockResolvedValue(
55
+ overrides?.seedBlob ?? new Blob(['frame-bytes'], { type: 'image/png' }),
56
+ ),
57
+ } as unknown as ApiClient;
58
+ }
59
+
60
+ function makePersona(overrides?: Partial<PersonaConfig>): PersonaConfig {
61
+ return {
62
+ persona_id: 'p1',
63
+ user_id: 'u1',
64
+ name: 'Test Persona',
65
+ voice_id: 'v1',
66
+ tts_model_id: 'm1',
67
+ temperature: 0.5,
68
+ speaking_rate: 1.0,
69
+ audio_format: 'pcm',
70
+ enable_markups: false,
71
+ chat_model_id: 'c1',
72
+ custom_system_prompt: 'Short backstory for grounding.',
73
+ system_prompt_override: false,
74
+ heartbeat_interval: 30,
75
+ idle_timeout: 300,
76
+ ...overrides,
77
+ } as PersonaConfig;
78
+ }
79
+
80
+ describe('prepareSceneEntry', () => {
81
+ it('calls extractImage with timestamp + sentinel entity', async () => {
82
+ const apiClient = makeApiClient();
83
+ await prepareSceneEntry({
84
+ videoId: 'vid-1',
85
+ timestamp: 42,
86
+ shotCaption: 'Two travelers cross a frozen bridge.',
87
+ enrichment: null,
88
+ persona: null,
89
+ apiClient,
90
+ });
91
+ const call = (apiClient as unknown as Record<string, AnyMock>).extractImage
92
+ .mock.calls[0];
93
+ expect(call?.[0]).toEqual({
94
+ videoId: 'vid-1',
95
+ entity: 'scene',
96
+ timestamp: 42,
97
+ });
98
+ });
99
+
100
+ it('feeds the upsampler the shot caption as the prompt', async () => {
101
+ const apiClient = makeApiClient();
102
+ await prepareSceneEntry({
103
+ videoId: 'vid-1',
104
+ timestamp: 10,
105
+ shotCaption: 'A specific caption.',
106
+ enrichment: null,
107
+ persona: null,
108
+ apiClient,
109
+ });
110
+ const call = (apiClient as unknown as Record<string, AnyMock>)
111
+ .upsampleReactorPrompt.mock.calls[0];
112
+ expect(call?.[0].prompt).toBe('A specific caption.');
113
+ });
114
+
115
+ it('falls back to the generic caption when none is given', async () => {
116
+ const apiClient = makeApiClient();
117
+ await prepareSceneEntry({
118
+ videoId: 'vid-1',
119
+ timestamp: 10,
120
+ shotCaption: null,
121
+ enrichment: null,
122
+ persona: null,
123
+ apiClient,
124
+ });
125
+ const call = (apiClient as unknown as Record<string, AnyMock>)
126
+ .upsampleReactorPrompt.mock.calls[0];
127
+ expect(call?.[0].prompt).toBe('Step into this moment');
128
+ });
129
+
130
+ it('omits personaContext when persona is null', async () => {
131
+ const apiClient = makeApiClient();
132
+ await prepareSceneEntry({
133
+ videoId: 'vid-1',
134
+ timestamp: 10,
135
+ shotCaption: 'caption',
136
+ enrichment: null,
137
+ persona: null,
138
+ apiClient,
139
+ });
140
+ const call = (apiClient as unknown as Record<string, AnyMock>)
141
+ .upsampleReactorPrompt.mock.calls[0];
142
+ expect(call?.[0].personaContext).toBeUndefined();
143
+ });
144
+
145
+ it('includes personaContext when persona has a backstory', async () => {
146
+ const apiClient = makeApiClient();
147
+ await prepareSceneEntry({
148
+ videoId: 'vid-1',
149
+ timestamp: 10,
150
+ shotCaption: 'caption',
151
+ enrichment: null,
152
+ persona: makePersona(),
153
+ apiClient,
154
+ });
155
+ const call = (apiClient as unknown as Record<string, AnyMock>)
156
+ .upsampleReactorPrompt.mock.calls[0];
157
+ expect(call?.[0].personaContext?.name).toBe('Test Persona');
158
+ });
159
+
160
+ it('fetches frame bytes via the seed-image proxy (not direct S3)', async () => {
161
+ const apiClient = makeApiClient();
162
+ await prepareSceneEntry({
163
+ videoId: 'vid-1',
164
+ timestamp: 42,
165
+ shotCaption: null,
166
+ enrichment: null,
167
+ persona: null,
168
+ apiClient,
169
+ });
170
+ const call = (apiClient as unknown as Record<string, AnyMock>)
171
+ .getSeedImageBlob.mock.calls[0];
172
+ expect(call?.[0]).toBe('img-scene-1');
173
+ });
174
+
175
+ it('returns the upsampled prompt, source timestamp, imageId, and a frame File', async () => {
176
+ const apiClient = makeApiClient({
177
+ upsampledPrompt: 'The expanded paragraph.',
178
+ seedBlob: new Blob(['frame-bytes'], { type: 'image/png' }),
179
+ });
180
+ const payload = await prepareSceneEntry({
181
+ videoId: 'vid-1',
182
+ timestamp: 42,
183
+ shotCaption: null,
184
+ enrichment: null,
185
+ persona: null,
186
+ apiClient,
187
+ });
188
+ expect(payload.upsampledPrompt).toBe('The expanded paragraph.');
189
+ expect(payload.sourceTimestamp).toBe(42.0);
190
+ expect(payload.imageId).toBe('img-scene-1');
191
+ expect(payload.frameFile).toBeInstanceOf(File);
192
+ expect(payload.frameFile.type).toBe('image/png');
193
+ expect(payload.frameFile.name).toBe('scene.png');
194
+ });
195
+
196
+ it('threads the signal into extractImage, upsample, and getSeedImageBlob', async () => {
197
+ const apiClient = makeApiClient();
198
+ const controller = new AbortController();
199
+ await prepareSceneEntry({
200
+ videoId: 'vid-1',
201
+ timestamp: 10,
202
+ shotCaption: null,
203
+ enrichment: null,
204
+ persona: null,
205
+ apiClient,
206
+ signal: controller.signal,
207
+ });
208
+ const extractCall = (apiClient as unknown as Record<string, AnyMock>)
209
+ .extractImage.mock.calls[0];
210
+ const upsampleCall = (apiClient as unknown as Record<string, AnyMock>)
211
+ .upsampleReactorPrompt.mock.calls[0];
212
+ const seedCall = (apiClient as unknown as Record<string, AnyMock>)
213
+ .getSeedImageBlob.mock.calls[0];
214
+ expect(extractCall?.[1]).toEqual({ signal: controller.signal });
215
+ expect(upsampleCall?.[1]).toEqual({ signal: controller.signal });
216
+ expect(seedCall?.[1]).toMatchObject({ signal: controller.signal });
217
+ });
218
+
219
+ it('rejects with a clear error when extractImage returns no imageId', async () => {
220
+ const apiClient = makeApiClient({
221
+ extract: { ...SAMPLE_EXTRACT, imageId: null },
222
+ });
223
+ await expect(
224
+ prepareSceneEntry({
225
+ videoId: 'vid-1',
226
+ timestamp: 10,
227
+ shotCaption: null,
228
+ enrichment: null,
229
+ persona: null,
230
+ apiClient,
231
+ }),
232
+ ).rejects.toThrow(/did not return an imageId/);
233
+ // Upsample + seed-blob calls should NOT have run after the early reject.
234
+ expect(
235
+ (apiClient as unknown as Record<string, AnyMock>).upsampleReactorPrompt,
236
+ ).not.toHaveBeenCalled();
237
+ expect(
238
+ (apiClient as unknown as Record<string, AnyMock>).getSeedImageBlob,
239
+ ).not.toHaveBeenCalled();
240
+ });
241
+ });
242
+
243
+ describe('bootstrapSceneInReactor', () => {
244
+ function makeBridge() {
245
+ return {
246
+ sendCommand: jest.fn().mockResolvedValue(undefined) as AnyMock,
247
+ uploadFile: jest.fn().mockResolvedValue({ uploadId: 'u-frame' }) as AnyMock,
248
+ };
249
+ }
250
+
251
+ function makePayload(frameFile?: File) {
252
+ return {
253
+ frameFile:
254
+ frameFile ?? new File(['x'], 'scene-1.png', { type: 'image/png' }),
255
+ upsampledPrompt: 'Helios paragraph.',
256
+ sourceTimestamp: 42,
257
+ imageId: 'img-scene-1',
258
+ };
259
+ }
260
+
261
+ it('runs uploadFile → set_image → set_prompt → start in order', async () => {
262
+ const bridge = makeBridge();
263
+ const payload = makePayload();
264
+ await bootstrapSceneInReactor(payload, bridge);
265
+ expect(bridge.uploadFile).toHaveBeenCalledWith(payload.frameFile);
266
+ expect(bridge.sendCommand.mock.calls[0][0]).toBe('set_image');
267
+ expect(bridge.sendCommand.mock.calls[0][1]).toEqual({
268
+ image: { uploadId: 'u-frame' },
269
+ transition: 'cut',
270
+ });
271
+ expect(bridge.sendCommand.mock.calls[1][0]).toBe('set_prompt');
272
+ expect(bridge.sendCommand.mock.calls[1][1]).toEqual({
273
+ prompt: 'Helios paragraph.',
274
+ });
275
+ expect(bridge.sendCommand.mock.calls[2][0]).toBe('start');
276
+ expect(bridge.sendCommand.mock.calls[2][1]).toEqual({});
277
+ });
278
+
279
+ it('short-circuits when the signal aborts between set_image and set_prompt', async () => {
280
+ const bridge = makeBridge();
281
+ const controller = new AbortController();
282
+ // Abort after the seed apply resolves but before set_prompt fires.
283
+ bridge.sendCommand.mockImplementationOnce(async () => {
284
+ controller.abort();
285
+ });
286
+ await bootstrapSceneInReactor(makePayload(), bridge, {
287
+ signal: controller.signal,
288
+ });
289
+ // Only set_image fired; set_prompt and start were skipped.
290
+ expect(bridge.sendCommand).toHaveBeenCalledTimes(1);
291
+ expect(bridge.sendCommand.mock.calls[0][0]).toBe('set_image');
292
+ });
293
+
294
+ it('rejects immediately if the signal is already aborted on entry', async () => {
295
+ const bridge = makeBridge();
296
+ const controller = new AbortController();
297
+ controller.abort();
298
+ await bootstrapSceneInReactor(makePayload(), bridge, {
299
+ signal: controller.signal,
300
+ });
301
+ // No commands or uploads issued.
302
+ expect(bridge.uploadFile).not.toHaveBeenCalled();
303
+ expect(bridge.sendCommand).not.toHaveBeenCalled();
304
+ });
305
+
306
+ it('returns immediately (no throw) when the signal fires during a slow upload', async () => {
307
+ const bridge = makeBridge();
308
+ const controller = new AbortController();
309
+ // Hold uploadFile open so abort fires while bootstrap is still
310
+ // awaiting it. The Reactor SDK doesn't take an AbortSignal, so the
311
+ // best we can do is race-then-abandon: bootstrap returns now even
312
+ // though the orphaned upload runs to completion in the background.
313
+ let resolveUpload: (v: unknown) => void = () => {};
314
+ bridge.uploadFile.mockImplementationOnce(
315
+ () =>
316
+ new Promise(resolve => {
317
+ resolveUpload = resolve;
318
+ }),
319
+ );
320
+ const bootstrap = bootstrapSceneInReactor(makePayload(), bridge, {
321
+ signal: controller.signal,
322
+ });
323
+ controller.abort();
324
+ // Abort is an expected terminal state, not an error. Callers
325
+ // already check `signal.aborted` to decide what to render — they
326
+ // shouldn't have to distinguish AbortError from real failures.
327
+ await expect(bootstrap).resolves.toBeUndefined();
328
+ // Critically: no follow-up set_prompt or start commands fire,
329
+ // even after the background upload completes. The orphaned
330
+ // set_image inside sendSeedFileToReactor is a no-op on a
331
+ // disconnected session.
332
+ resolveUpload({ uploadId: 'late' });
333
+ // Flush a couple microtask queues so the abandoned chain settles.
334
+ await Promise.resolve();
335
+ await Promise.resolve();
336
+ const commandsIssued = bridge.sendCommand.mock.calls.map(
337
+ (c: unknown[]) => c[0],
338
+ );
339
+ expect(commandsIssued).not.toContain('set_prompt');
340
+ expect(commandsIssued).not.toContain('start');
341
+ });
342
+ });