@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,251 @@
1
+ /**
2
+ * Tests for `startVisionPublisher`.
3
+ *
4
+ * Strategy: mock the `VisionPublisher` module so we can assert on
5
+ * construction arguments and lifecycle calls without standing up real
6
+ * RTCPeerConnections. The `toVisionRtcCredentials` helper is also mocked
7
+ * so we can confirm the helper threads the raw response through it and
8
+ * passes the converted credentials to VisionPublisher.
9
+ */
10
+
11
+ import { startVisionPublisher } from '../startVisionPublisher';
12
+ import type {
13
+ StartVisionPublisherOptions,
14
+ VisionPublisherHandle,
15
+ } from '../startVisionPublisher';
16
+ import type { VisionStatus } from '../types';
17
+ import type { ChatVisionRtcCredentialsResponse } from '../../api';
18
+
19
+ // ── Mocks ────────────────────────────────────────────────────────────────────
20
+
21
+ jest.mock('../VisionPublisher');
22
+ jest.mock('../credentials');
23
+
24
+ import { VisionPublisher } from '../VisionPublisher';
25
+ import { toVisionRtcCredentials } from '../credentials';
26
+
27
+ const MockVisionPublisher = VisionPublisher as jest.MockedClass<typeof VisionPublisher>;
28
+ const mockToVisionRtcCredentials = toVisionRtcCredentials as jest.MockedFunction<
29
+ typeof toVisionRtcCredentials
30
+ >;
31
+
32
+ // ── Helpers ──────────────────────────────────────────────────────────────────
33
+
34
+ /** Retrieve the most-recently created mock publisher instance. */
35
+ const lastInstance = (): jest.Mocked<VisionPublisher> =>
36
+ MockVisionPublisher.mock.instances[
37
+ MockVisionPublisher.mock.instances.length - 1
38
+ ] as jest.Mocked<VisionPublisher>;
39
+
40
+ const makeMockWs = () =>
41
+ ({
42
+ on: jest.fn(),
43
+ off: jest.fn(),
44
+ sendVisionOffer: jest.fn(),
45
+ sendVisionIce: jest.fn(),
46
+ sendVisionClose: jest.fn(),
47
+ }) as unknown as import('../../websocket').ChatWebSocket;
48
+
49
+ const baseCredentialsResponse: ChatVisionRtcCredentialsResponse = {
50
+ mode: 'local',
51
+ iceServers: [{ urls: ['stun:stun.l.google.com:19302'], username: null, credential: null }],
52
+ signedSignalingUrl: null,
53
+ channelArn: null,
54
+ clientId: null,
55
+ region: null,
56
+ expiresIn: 3600,
57
+ traceId: 'trace-test',
58
+ };
59
+
60
+ const fakeConvertedCredentials = {
61
+ mode: 'local' as const,
62
+ iceServers: [{ urls: ['stun:stun.l.google.com:19302'] }],
63
+ signedSignalingUrl: null,
64
+ channelArn: null,
65
+ clientId: null,
66
+ region: null,
67
+ expiresIn: 3600,
68
+ };
69
+
70
+ // ── Suite ────────────────────────────────────────────────────────────────────
71
+
72
+ describe('startVisionPublisher', () => {
73
+ beforeEach(() => {
74
+ jest.clearAllMocks();
75
+
76
+ // Mock toVisionRtcCredentials to return a known object.
77
+ mockToVisionRtcCredentials.mockReturnValue(fakeConvertedCredentials);
78
+
79
+ // Default mock: start() resolves immediately; onStatus/onError return
80
+ // unsubscribe functions; stop() is a no-op.
81
+ MockVisionPublisher.prototype.start = jest.fn().mockResolvedValue(undefined);
82
+ MockVisionPublisher.prototype.stop = jest.fn();
83
+ MockVisionPublisher.prototype.onStatus = jest.fn().mockReturnValue(jest.fn());
84
+ MockVisionPublisher.prototype.onError = jest.fn().mockReturnValue(jest.fn());
85
+ });
86
+
87
+ it('passes credentials through toVisionRtcCredentials and constructs VisionPublisher', () => {
88
+ const ws = makeMockWs();
89
+ const track = {} as MediaStreamTrack;
90
+ const opts: StartVisionPublisherOptions = {
91
+ ws,
92
+ track,
93
+ credentialsResponse: baseCredentialsResponse,
94
+ fps: 2.0,
95
+ };
96
+
97
+ startVisionPublisher(opts);
98
+
99
+ // toVisionRtcCredentials was called with the raw response.
100
+ expect(mockToVisionRtcCredentials).toHaveBeenCalledWith(baseCredentialsResponse);
101
+
102
+ // VisionPublisher was constructed with the converted credentials.
103
+ expect(MockVisionPublisher).toHaveBeenCalledWith({
104
+ ws,
105
+ track,
106
+ config: { fps: 2.0 },
107
+ credentials: fakeConvertedCredentials,
108
+ });
109
+ });
110
+
111
+ it('returns a handle whose publisher is the constructed VisionPublisher', () => {
112
+ const ws = makeMockWs();
113
+ const track = {} as MediaStreamTrack;
114
+ const handle: VisionPublisherHandle = startVisionPublisher({
115
+ ws,
116
+ track,
117
+ credentialsResponse: baseCredentialsResponse,
118
+ fps: 1.0,
119
+ });
120
+
121
+ expect(handle.publisher).toBe(lastInstance());
122
+ });
123
+
124
+ it('registers onStatus callback when provided', () => {
125
+ const ws = makeMockWs();
126
+ const track = {} as MediaStreamTrack;
127
+ const onStatus = jest.fn<void, [VisionStatus]>();
128
+
129
+ startVisionPublisher({
130
+ ws,
131
+ track,
132
+ credentialsResponse: baseCredentialsResponse,
133
+ fps: 1.0,
134
+ onStatus,
135
+ });
136
+
137
+ expect(lastInstance().onStatus).toHaveBeenCalledWith(onStatus);
138
+ });
139
+
140
+ it('registers onError callback when provided', () => {
141
+ const ws = makeMockWs();
142
+ const track = {} as MediaStreamTrack;
143
+ const onError = jest.fn<void, [Error]>();
144
+
145
+ startVisionPublisher({
146
+ ws,
147
+ track,
148
+ credentialsResponse: baseCredentialsResponse,
149
+ fps: 1.0,
150
+ onError,
151
+ });
152
+
153
+ expect(lastInstance().onError).toHaveBeenCalledWith(onError);
154
+ });
155
+
156
+ it('does not call onStatus/onError on the publisher when callbacks are omitted', () => {
157
+ const ws = makeMockWs();
158
+ const track = {} as MediaStreamTrack;
159
+
160
+ startVisionPublisher({
161
+ ws,
162
+ track,
163
+ credentialsResponse: baseCredentialsResponse,
164
+ fps: 1.0,
165
+ });
166
+
167
+ expect(lastInstance().onStatus).not.toHaveBeenCalled();
168
+ expect(lastInstance().onError).not.toHaveBeenCalled();
169
+ });
170
+
171
+ it('calls start() on the publisher', () => {
172
+ const ws = makeMockWs();
173
+ const track = {} as MediaStreamTrack;
174
+
175
+ startVisionPublisher({
176
+ ws,
177
+ track,
178
+ credentialsResponse: baseCredentialsResponse,
179
+ fps: 1.0,
180
+ });
181
+
182
+ expect(lastInstance().start).toHaveBeenCalled();
183
+ });
184
+
185
+ it('stop() calls publisher.stop() and both unsubscribers', () => {
186
+ const ws = makeMockWs();
187
+ const track = {} as MediaStreamTrack;
188
+ const offStatus = jest.fn();
189
+ const offError = jest.fn();
190
+ MockVisionPublisher.prototype.onStatus = jest.fn().mockReturnValue(offStatus);
191
+ MockVisionPublisher.prototype.onError = jest.fn().mockReturnValue(offError);
192
+ const onStatus = jest.fn<void, [VisionStatus]>();
193
+ const onError = jest.fn<void, [Error]>();
194
+
195
+ const handle = startVisionPublisher({
196
+ ws,
197
+ track,
198
+ credentialsResponse: baseCredentialsResponse,
199
+ fps: 1.0,
200
+ onStatus,
201
+ onError,
202
+ });
203
+
204
+ handle.stop();
205
+
206
+ expect(lastInstance().stop).toHaveBeenCalled();
207
+ expect(offStatus).toHaveBeenCalled();
208
+ expect(offError).toHaveBeenCalled();
209
+ });
210
+
211
+ it('forwards start() rejection to onError', async () => {
212
+ const ws = makeMockWs();
213
+ const track = {} as MediaStreamTrack;
214
+ const boom = new Error('peer connection exploded');
215
+ MockVisionPublisher.prototype.start = jest.fn().mockRejectedValue(boom);
216
+ const onError = jest.fn<void, [Error]>();
217
+
218
+ startVisionPublisher({
219
+ ws,
220
+ track,
221
+ credentialsResponse: baseCredentialsResponse,
222
+ fps: 1.0,
223
+ onError,
224
+ });
225
+
226
+ // Allow the promise microtask to settle.
227
+ await Promise.resolve();
228
+
229
+ expect(onError).toHaveBeenCalledWith(boom);
230
+ });
231
+
232
+ it('wraps non-Error start() rejections in an Error before forwarding', async () => {
233
+ const ws = makeMockWs();
234
+ const track = {} as MediaStreamTrack;
235
+ MockVisionPublisher.prototype.start = jest.fn().mockRejectedValue('string-error');
236
+ const onError = jest.fn<void, [Error]>();
237
+
238
+ startVisionPublisher({
239
+ ws,
240
+ track,
241
+ credentialsResponse: baseCredentialsResponse,
242
+ fps: 1.0,
243
+ onError,
244
+ });
245
+
246
+ await Promise.resolve();
247
+
248
+ expect(onError).toHaveBeenCalledWith(expect.any(Error));
249
+ expect(onError.mock.calls[0][0].message).toBe('string-error');
250
+ });
251
+ });
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Unit tests for `toVisionRtcCredentials`.
3
+ *
4
+ * Verifies:
5
+ * (a) null ICE `username` / `credential` are coerced to `undefined`
6
+ * (b) `traceId` (on the response type, NOT on VisionRtcCredentials) is dropped
7
+ * (c) all scalar fields pass through unchanged
8
+ */
9
+
10
+ import { toVisionRtcCredentials } from '../credentials';
11
+ import type { ChatVisionRtcCredentialsResponse } from '../../api';
12
+
13
+ const baseResponse: ChatVisionRtcCredentialsResponse = {
14
+ mode: 'kvs',
15
+ iceServers: [
16
+ {
17
+ urls: ['stun:stun.kinesisvideo.us-east-1.amazonaws.com:443'],
18
+ username: 'someuser',
19
+ credential: 'somepass',
20
+ },
21
+ ],
22
+ signedSignalingUrl: 'wss://example.com/signed',
23
+ channelArn: 'arn:aws:kinesisvideo:us-east-1:123:channel/test',
24
+ clientId: 'viewer-abc123',
25
+ region: 'us-east-1',
26
+ expiresIn: 300,
27
+ traceId: 'trace-xyz',
28
+ };
29
+
30
+ describe('toVisionRtcCredentials', () => {
31
+ it('(c) passes through scalar fields unchanged', () => {
32
+ const result = toVisionRtcCredentials(baseResponse);
33
+
34
+ expect(result.mode).toBe('kvs');
35
+ expect(result.signedSignalingUrl).toBe('wss://example.com/signed');
36
+ expect(result.channelArn).toBe(
37
+ 'arn:aws:kinesisvideo:us-east-1:123:channel/test',
38
+ );
39
+ expect(result.clientId).toBe('viewer-abc123');
40
+ expect(result.region).toBe('us-east-1');
41
+ expect(result.expiresIn).toBe(300);
42
+ });
43
+
44
+ it('(c) passes through present ICE username and credential unchanged', () => {
45
+ const result = toVisionRtcCredentials(baseResponse);
46
+
47
+ expect(result.iceServers).toHaveLength(1);
48
+ expect(result.iceServers[0].urls).toEqual([
49
+ 'stun:stun.kinesisvideo.us-east-1.amazonaws.com:443',
50
+ ]);
51
+ expect(result.iceServers[0].username).toBe('someuser');
52
+ expect(result.iceServers[0].credential).toBe('somepass');
53
+ });
54
+
55
+ it('(a) coerces null ICE username to undefined', () => {
56
+ const response: ChatVisionRtcCredentialsResponse = {
57
+ ...baseResponse,
58
+ iceServers: [
59
+ { urls: ['stun:stun.l.google.com:19302'], username: null, credential: null },
60
+ ],
61
+ };
62
+
63
+ const result = toVisionRtcCredentials(response);
64
+
65
+ expect(result.iceServers[0].username).toBeUndefined();
66
+ expect(result.iceServers[0].credential).toBeUndefined();
67
+ });
68
+
69
+ it('(a) coerces null ICE credential to undefined while keeping a present username', () => {
70
+ const response: ChatVisionRtcCredentialsResponse = {
71
+ ...baseResponse,
72
+ iceServers: [
73
+ { urls: ['stun:stun.l.google.com:19302'], username: 'u', credential: null },
74
+ ],
75
+ };
76
+
77
+ const result = toVisionRtcCredentials(response);
78
+
79
+ expect(result.iceServers[0].username).toBe('u');
80
+ expect(result.iceServers[0].credential).toBeUndefined();
81
+ });
82
+
83
+ it('(b) does NOT carry traceId over to VisionRtcCredentials', () => {
84
+ const result = toVisionRtcCredentials(baseResponse);
85
+
86
+ // TypeScript types would normally prevent accessing .traceId, but we cast
87
+ // to verify at runtime that the field is absent.
88
+ expect((result as unknown as Record<string, unknown>)['traceId']).toBeUndefined();
89
+ });
90
+
91
+ it('handles local mode with no kvs-specific fields', () => {
92
+ const localResponse: ChatVisionRtcCredentialsResponse = {
93
+ mode: 'local',
94
+ iceServers: [{ urls: ['stun:stun.l.google.com:19302'], username: null, credential: null }],
95
+ signedSignalingUrl: null,
96
+ channelArn: null,
97
+ clientId: null,
98
+ region: null,
99
+ expiresIn: 3600,
100
+ traceId: 'trace-local',
101
+ };
102
+
103
+ const result = toVisionRtcCredentials(localResponse);
104
+
105
+ expect(result.mode).toBe('local');
106
+ expect(result.signedSignalingUrl).toBeNull();
107
+ expect(result.channelArn).toBeNull();
108
+ expect(result.clientId).toBeNull();
109
+ expect(result.region).toBeNull();
110
+ expect(result.expiresIn).toBe(3600);
111
+ expect((result as unknown as Record<string, unknown>)['traceId']).toBeUndefined();
112
+ });
113
+ });
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Tests for the pure visionScheduler helper — a stateless backpressure
3
+ * loop that adjusts publisher fps in response to backend `vision_status`
4
+ * events.
5
+ *
6
+ * The scheduler is intentionally pure: no timers, no sockets, no
7
+ * RTCRtpSender plumbing. Hooks/publishers feed it state, get a new
8
+ * target fps back, and decide what to do with it. That keeps the
9
+ * heuristic testable without spinning up an RTCPeerConnection.
10
+ */
11
+
12
+ import {
13
+ computeNextFps,
14
+ createVisionScheduler,
15
+ } from '../visionScheduler';
16
+ import type { VisionStatus } from '../types';
17
+
18
+ describe('computeNextFps', () => {
19
+ it('halves fps when the backend reports processingPaused', () => {
20
+ const status: VisionStatus = {
21
+ bufferedFrames: 3,
22
+
23
+ processingPaused: true,
24
+ };
25
+ expect(
26
+ computeNextFps({ currentFps: 2.0, status, minFps: 0.5, maxFps: 3.0 }),
27
+ ).toBe(1.0);
28
+ });
29
+
30
+ it('clamps to minFps when halving would underflow', () => {
31
+ const status: VisionStatus = {
32
+ bufferedFrames: 3,
33
+
34
+ processingPaused: true,
35
+ };
36
+ expect(
37
+ computeNextFps({ currentFps: 0.6, status, minFps: 0.5, maxFps: 3.0 }),
38
+ ).toBe(0.5);
39
+ });
40
+
41
+ it('nudges fps upward when the buffer is empty and inference is fast', () => {
42
+ const status: VisionStatus = {
43
+ bufferedFrames: 0,
44
+
45
+ processingPaused: false,
46
+ };
47
+ const next = computeNextFps({
48
+ currentFps: 1.0,
49
+ status,
50
+ minFps: 0.5,
51
+ maxFps: 3.0,
52
+ });
53
+ expect(next).toBeGreaterThan(1.0);
54
+ expect(next).toBeLessThanOrEqual(3.0);
55
+ });
56
+
57
+ it('clamps the upward nudge at maxFps', () => {
58
+ const status: VisionStatus = {
59
+ bufferedFrames: 0,
60
+
61
+ processingPaused: false,
62
+ };
63
+ expect(
64
+ computeNextFps({ currentFps: 2.9, status, minFps: 0.5, maxFps: 3.0 }),
65
+ ).toBe(3.0);
66
+ });
67
+
68
+ it('leaves fps unchanged when the system is in steady-state', () => {
69
+ const status: VisionStatus = {
70
+ bufferedFrames: 1,
71
+
72
+ processingPaused: false,
73
+ };
74
+ expect(
75
+ computeNextFps({ currentFps: 1.0, status, minFps: 0.5, maxFps: 3.0 }),
76
+ ).toBe(1.0);
77
+ });
78
+ });
79
+
80
+ describe('createVisionScheduler', () => {
81
+ it('invokes the onFpsChange callback only when fps actually changes', () => {
82
+ const onFpsChange = jest.fn();
83
+ const scheduler = createVisionScheduler({
84
+ initialFps: 1.0,
85
+ minFps: 0.5,
86
+ maxFps: 3.0,
87
+ onFpsChange,
88
+ });
89
+
90
+ // Steady state — no callback.
91
+ scheduler.handleStatus({
92
+ bufferedFrames: 1,
93
+
94
+ processingPaused: false,
95
+ });
96
+ expect(onFpsChange).not.toHaveBeenCalled();
97
+
98
+ // Pause → halve to 0.5; callback fires once.
99
+ scheduler.handleStatus({
100
+ bufferedFrames: 3,
101
+
102
+ processingPaused: true,
103
+ });
104
+ expect(onFpsChange).toHaveBeenCalledTimes(1);
105
+ expect(onFpsChange).toHaveBeenCalledWith(0.5);
106
+ expect(scheduler.getFps()).toBe(0.5);
107
+
108
+ // Already at floor under pressure → no further callback.
109
+ scheduler.handleStatus({
110
+ bufferedFrames: 3,
111
+
112
+ processingPaused: true,
113
+ });
114
+ expect(onFpsChange).toHaveBeenCalledTimes(1);
115
+ });
116
+
117
+ it('reports the current fps via getFps()', () => {
118
+ const scheduler = createVisionScheduler({
119
+ initialFps: 1.5,
120
+ minFps: 0.5,
121
+ maxFps: 3.0,
122
+ onFpsChange: () => undefined,
123
+ });
124
+ expect(scheduler.getFps()).toBe(1.5);
125
+ scheduler.handleStatus({
126
+ bufferedFrames: 0,
127
+
128
+ processingPaused: false,
129
+ });
130
+ expect(scheduler.getFps()).toBeGreaterThan(1.5);
131
+ });
132
+
133
+ it('clamps an out-of-range initialFps into [minFps, maxFps]', () => {
134
+ const tooHigh = createVisionScheduler({
135
+ initialFps: 10,
136
+ minFps: 0.5,
137
+ maxFps: 3.0,
138
+ onFpsChange: () => undefined,
139
+ });
140
+ expect(tooHigh.getFps()).toBe(3.0);
141
+ const tooLow = createVisionScheduler({
142
+ initialFps: 0.1,
143
+ minFps: 0.5,
144
+ maxFps: 3.0,
145
+ onFpsChange: () => undefined,
146
+ });
147
+ expect(tooLow.getFps()).toBe(0.5);
148
+ });
149
+ });
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Pure mapping helper: API response → VisionPublisher credentials.
3
+ *
4
+ * The chat-API returns `ChatVisionRtcCredentialsResponse` whose ICE-server
5
+ * entries use `string | null` for optional fields (JSON can't carry
6
+ * `undefined`). `RTCIceServer` / `VisionRtcCredentials` want `string |
7
+ * undefined` instead, so we normalise here.
8
+ *
9
+ * `traceId` is a log-correlation field on the response; it is NOT part of
10
+ * `VisionRtcCredentials` and is intentionally dropped.
11
+ */
12
+
13
+ import type { ChatVisionRtcCredentialsResponse } from '../api';
14
+ import type { VisionRtcCredentials } from './types';
15
+
16
+ /**
17
+ * Convert a `ChatVisionRtcCredentialsResponse` (API wire shape) into the
18
+ * `VisionRtcCredentials` expected by `VisionPublisher`.
19
+ *
20
+ * - `null` ICE `username` / `credential` fields are coerced to `undefined`.
21
+ * - `traceId` (present on the response for log correlation) is dropped.
22
+ */
23
+ export function toVisionRtcCredentials(
24
+ response: ChatVisionRtcCredentialsResponse,
25
+ ): VisionRtcCredentials {
26
+ return {
27
+ mode: response.mode,
28
+ iceServers: response.iceServers.map((entry) => ({
29
+ urls: entry.urls,
30
+ username: entry.username ?? undefined,
31
+ credential: entry.credential ?? undefined,
32
+ })),
33
+ signedSignalingUrl: response.signedSignalingUrl,
34
+ channelArn: response.channelArn,
35
+ clientId: response.clientId,
36
+ region: response.region,
37
+ expiresIn: response.expiresIn,
38
+ };
39
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Public surface for the framework-agnostic vision publisher.
3
+ *
4
+ * Re-exported from `packages/client-core/src/index.ts` so consumers can
5
+ * `import { VisionPublisher } from '@escapeaihq/client-core'`.
6
+ */
7
+
8
+ export { VisionPublisher } from './VisionPublisher';
9
+ export {
10
+ computeNextFps,
11
+ createVisionScheduler,
12
+ } from './visionScheduler';
13
+ export type {
14
+ VisionPublisherConfig,
15
+ VisionPublisherOptions,
16
+ VisionRtcCredentials,
17
+ VisionStatus,
18
+ VisionStatusListener,
19
+ VisionErrorListener,
20
+ } from './types';
21
+ export type {
22
+ ComputeNextFpsInput,
23
+ VisionScheduler,
24
+ VisionSchedulerOptions,
25
+ } from './visionScheduler';
26
+ export { toVisionRtcCredentials } from './credentials';
27
+ export { startVisionPublisher } from './startVisionPublisher';
28
+ export type {
29
+ StartVisionPublisherOptions,
30
+ VisionPublisherHandle,
31
+ } from './startVisionPublisher';
32
+ export { VISION_SOURCE_KINDS } from './sourceKinds';
33
+ export type {
34
+ ElementSourceDescriptor,
35
+ VisionCaptureElement,
36
+ VisionSourceDescriptor,
37
+ VisionSourceKind,
38
+ WebcamSourceDescriptor,
39
+ } from './sourceKinds';
40
+ export { VisionSourceController } from './VisionSourceController';
41
+ export type {
42
+ VisionCredentialsClient,
43
+ VisionSourceControllerOptions,
44
+ } from './VisionSourceController';
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Vision source kinds + descriptors — the single source of truth for the
3
+ * three local vision sources a surface can point the VLM at.
4
+ *
5
+ * R3a of the Platform Experience Restructure
6
+ * (`specs/platform-experience-restructure-phase-R3-vision-source-picker.md`).
7
+ * The `VisionSourceController` consumes these; the React adapter (R3b) and
8
+ * the `/chat-ws` + `/watch` surfaces (R3c/R3d) pick among them. Constants
9
+ * over bare literals, per CLAUDE.md — consumers compare against
10
+ * `VISION_SOURCE_KINDS.*`, never inline strings.
11
+ *
12
+ * Cross-device vision relay is explicitly deferred: every kind here is a
13
+ * LOCAL source, published over the user's own chat WebSocket exactly as
14
+ * today.
15
+ */
16
+
17
+ /** The three local vision source kinds, as a frozen constant object. */
18
+ export const VISION_SOURCE_KINDS = {
19
+ /** The user's camera via `navigator.mediaDevices.getUserMedia`. */
20
+ WEBCAM: 'webcam',
21
+ /** The surface's playing `<video>` element via `captureStream()`. */
22
+ PLAYING_VIDEO: 'playing-video',
23
+ /** A server-rendered world surface (e.g. the Step-Into-Video / Helios
24
+ * canvas) via `captureStream()`. */
25
+ SERVER_RENDER: 'server-render',
26
+ } as const;
27
+
28
+ export type VisionSourceKind =
29
+ (typeof VISION_SOURCE_KINDS)[keyof typeof VISION_SOURCE_KINDS];
30
+
31
+ /**
32
+ * An element whose `captureStream()` output can feed the publisher.
33
+ * `HTMLVideoElement.captureStream` / `HTMLCanvasElement.captureStream`
34
+ * aren't in every lib.dom version, so the controller duck-checks at
35
+ * runtime rather than widening global types (same pattern as the watch
36
+ * hook this lifts).
37
+ */
38
+ export type VisionCaptureElement = HTMLVideoElement | HTMLCanvasElement;
39
+
40
+ /** Webcam source: the controller calls `getUserMedia(constraints ?? { video: true })`. */
41
+ export interface WebcamSourceDescriptor {
42
+ kind: typeof VISION_SOURCE_KINDS.WEBCAM;
43
+ /** Optional getUserMedia constraints; defaults to `{ video: true }`
44
+ * (byte-parity with today's chat-ws webcam path). */
45
+ constraints?: MediaStreamConstraints;
46
+ }
47
+
48
+ /**
49
+ * Element-backed source (`playing-video` / `server-render`): the
50
+ * controller calls `element.captureStream()`. The DOM element stays a
51
+ * call-site concern — the descriptor carries it so the controller never
52
+ * queries the document.
53
+ */
54
+ export interface ElementSourceDescriptor {
55
+ kind:
56
+ | typeof VISION_SOURCE_KINDS.PLAYING_VIDEO
57
+ | typeof VISION_SOURCE_KINDS.SERVER_RENDER;
58
+ element: VisionCaptureElement;
59
+ /**
60
+ * When true and the element is a `<video>` that hasn't decoded its
61
+ * first frame yet (`readyState < HAVE_CURRENT_DATA`), the controller
62
+ * waits for `loadeddata` before capturing — without this gate a fast
63
+ * toggle-on publishes a black frame (the watch-page guard, lifted
64
+ * verbatim). Ignored for canvas elements (no readyState).
65
+ */
66
+ waitForFrame?: boolean;
67
+ }
68
+
69
+ /**
70
+ * Tagged union describing where the published track comes from, without
71
+ * the controller knowing about the page. Discriminate on `kind`.
72
+ */
73
+ export type VisionSourceDescriptor =
74
+ | WebcamSourceDescriptor
75
+ | ElementSourceDescriptor;