@escapeaihq/client-core 0.4.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 (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +122 -0
  3. package/dist/api.d.ts +836 -0
  4. package/dist/api.d.ts.map +1 -0
  5. package/dist/api.js +781 -0
  6. package/dist/api.js.map +1 -0
  7. package/dist/audio.d.ts +278 -0
  8. package/dist/audio.d.ts.map +1 -0
  9. package/dist/audio.js +758 -0
  10. package/dist/audio.js.map +1 -0
  11. package/dist/audioProtocol.d.ts +101 -0
  12. package/dist/audioProtocol.d.ts.map +1 -0
  13. package/dist/audioProtocol.js +62 -0
  14. package/dist/audioProtocol.js.map +1 -0
  15. package/dist/index.d.ts +28 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +28 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/visemeGeometry.d.ts +198 -0
  20. package/dist/visemeGeometry.d.ts.map +1 -0
  21. package/dist/visemeGeometry.js +297 -0
  22. package/dist/visemeGeometry.js.map +1 -0
  23. package/dist/visemeScheduler.d.ts +28 -0
  24. package/dist/visemeScheduler.d.ts.map +1 -0
  25. package/dist/visemeScheduler.js +94 -0
  26. package/dist/visemeScheduler.js.map +1 -0
  27. package/dist/websocket.d.ts +196 -0
  28. package/dist/websocket.d.ts.map +1 -0
  29. package/dist/websocket.js +250 -0
  30. package/dist/websocket.js.map +1 -0
  31. package/package.json +40 -0
  32. package/src/__tests__/api-pollers.test.ts +524 -0
  33. package/src/__tests__/audio.test.ts +714 -0
  34. package/src/__tests__/visemeGeometry.test.ts +465 -0
  35. package/src/__tests__/visemeScheduler.test.ts +256 -0
  36. package/src/api.ts +1577 -0
  37. package/src/audio.ts +934 -0
  38. package/src/audioProtocol.ts +163 -0
  39. package/src/index.ts +28 -0
  40. package/src/visemeGeometry.ts +353 -0
  41. package/src/visemeScheduler.ts +133 -0
  42. package/src/websocket.ts +385 -0
@@ -0,0 +1,256 @@
1
+ import { VisemeScheduler } from '../visemeScheduler';
2
+ import type { WordPhonemeInfo } from '../audioProtocol';
3
+
4
+ // Minimal AudioContext mock — only needs currentTime and to exist
5
+ function createMockAudioContext(initialTime = 0): AudioContext {
6
+ let time = initialTime;
7
+ return {
8
+ get currentTime() {
9
+ return time;
10
+ },
11
+ _setTime(t: number) {
12
+ time = t;
13
+ },
14
+ } as unknown as AudioContext & { _setTime: (t: number) => void };
15
+ }
16
+
17
+ // Suppress requestAnimationFrame in tests — call tick synchronously
18
+ let rafCallback: FrameRequestCallback | null = null;
19
+ beforeEach(() => {
20
+ jest.spyOn(globalThis, 'requestAnimationFrame').mockImplementation((cb) => {
21
+ rafCallback = cb;
22
+ return 1;
23
+ });
24
+ jest.spyOn(globalThis, 'cancelAnimationFrame').mockImplementation(() => {
25
+ rafCallback = null;
26
+ });
27
+ });
28
+ afterEach(() => {
29
+ jest.restoreAllMocks();
30
+ rafCallback = null;
31
+ });
32
+
33
+ function flushRaf() {
34
+ if (rafCallback) {
35
+ const cb = rafCallback;
36
+ rafCallback = null;
37
+ cb(performance.now());
38
+ }
39
+ }
40
+
41
+ const SAMPLE_PHONEMES: WordPhonemeInfo[] = [
42
+ {
43
+ wordIndex: 0,
44
+ phones: [
45
+ { phoneSymbol: 'h', startTime: 0.0, duration: 0.06, visemeSymbol: 'cdgknstxyz' },
46
+ { phoneSymbol: 'ʌ', startTime: 0.06, duration: 0.09, visemeSymbol: 'aei' },
47
+ { phoneSymbol: 'l', startTime: 0.15, duration: 0.06, visemeSymbol: 'l' },
48
+ { phoneSymbol: 'oʊ', startTime: 0.21, duration: 0.1, visemeSymbol: 'o' },
49
+ ],
50
+ },
51
+ ];
52
+
53
+ describe('VisemeScheduler', () => {
54
+ it('starts at rest viseme', () => {
55
+ const ctx = createMockAudioContext();
56
+ const onChange = jest.fn();
57
+ const scheduler = new VisemeScheduler(ctx, onChange);
58
+ expect(scheduler.getCurrentViseme()).toBe('rest');
59
+ expect(onChange).not.toHaveBeenCalled();
60
+ scheduler.destroy();
61
+ });
62
+
63
+ it('schedules phonemes and fires viseme changes on tick', () => {
64
+ const ctx = createMockAudioContext(10.0) as AudioContext & { _setTime: (t: number) => void };
65
+ const changes: string[] = [];
66
+ const scheduler = new VisemeScheduler(ctx, (v) => changes.push(v));
67
+
68
+ // Schedule chunk starting at AudioContext time 10.0
69
+ scheduler.scheduleChunk(SAMPLE_PHONEMES, 10.0);
70
+
71
+ // At t=10.0, first phoneme (cdgknstxyz, 0.0–0.06) should be active
72
+ flushRaf();
73
+ expect(changes).toEqual(['cdgknstxyz']);
74
+
75
+ // Advance to t=10.06 — second phoneme (aei, 0.06–0.15)
76
+ ctx._setTime(10.07);
77
+ flushRaf();
78
+ expect(changes).toEqual(['cdgknstxyz', 'aei']);
79
+
80
+ // Advance to t=10.15 — third phoneme (l, 0.15–0.21)
81
+ ctx._setTime(10.16);
82
+ flushRaf();
83
+ expect(changes).toEqual(['cdgknstxyz', 'aei', 'l']);
84
+
85
+ // Advance to t=10.21 — fourth phoneme (o, 0.21–0.31)
86
+ ctx._setTime(10.22);
87
+ flushRaf();
88
+ expect(changes).toEqual(['cdgknstxyz', 'aei', 'l', 'o']);
89
+
90
+ // Advance past all phonemes — back to rest
91
+ ctx._setTime(10.5);
92
+ flushRaf();
93
+ expect(changes).toEqual(['cdgknstxyz', 'aei', 'l', 'o', 'rest']);
94
+
95
+ scheduler.destroy();
96
+ });
97
+
98
+ it('does not fire onChange when viseme has not changed', () => {
99
+ const ctx = createMockAudioContext(10.0) as AudioContext & { _setTime: (t: number) => void };
100
+ const onChange = jest.fn();
101
+ const scheduler = new VisemeScheduler(ctx, onChange);
102
+
103
+ scheduler.scheduleChunk(SAMPLE_PHONEMES, 10.0);
104
+ flushRaf(); // cdgknstxyz
105
+ expect(onChange).toHaveBeenCalledTimes(1);
106
+
107
+ // Same time — no change
108
+ flushRaf();
109
+ expect(onChange).toHaveBeenCalledTimes(1);
110
+
111
+ scheduler.destroy();
112
+ });
113
+
114
+ it('falls back to rest for unknown viseme symbols', () => {
115
+ const ctx = createMockAudioContext(5.0);
116
+ const changes: string[] = [];
117
+ const scheduler = new VisemeScheduler(ctx, (v) => changes.push(v));
118
+
119
+ const unknownPhonemes: WordPhonemeInfo[] = [
120
+ {
121
+ wordIndex: 0,
122
+ phones: [
123
+ { phoneSymbol: 'x', startTime: 0.0, duration: 0.1, visemeSymbol: 'UNKNOWN_SYMBOL' },
124
+ ],
125
+ },
126
+ ];
127
+
128
+ scheduler.scheduleChunk(unknownPhonemes, 5.0);
129
+ flushRaf();
130
+ // Unknown symbol should be mapped to 'rest', but since we're already at rest,
131
+ // no change fires
132
+ expect(changes).toEqual([]);
133
+ expect(scheduler.getCurrentViseme()).toBe('rest');
134
+
135
+ scheduler.destroy();
136
+ });
137
+
138
+ it('clear() resets to rest and cancels animation', () => {
139
+ const ctx = createMockAudioContext(10.0);
140
+ const changes: string[] = [];
141
+ const scheduler = new VisemeScheduler(ctx, (v) => changes.push(v));
142
+
143
+ scheduler.scheduleChunk(SAMPLE_PHONEMES, 10.0);
144
+ flushRaf(); // cdgknstxyz
145
+
146
+ scheduler.clear();
147
+ expect(scheduler.getCurrentViseme()).toBe('rest');
148
+ expect(changes[changes.length - 1]).toBe('rest');
149
+ expect(cancelAnimationFrame).toHaveBeenCalled();
150
+
151
+ scheduler.destroy();
152
+ });
153
+
154
+ it('clear() does not fire onChange if already at rest', () => {
155
+ const ctx = createMockAudioContext(0);
156
+ const onChange = jest.fn();
157
+ const scheduler = new VisemeScheduler(ctx, onChange);
158
+
159
+ scheduler.clear();
160
+ expect(onChange).not.toHaveBeenCalled();
161
+
162
+ scheduler.destroy();
163
+ });
164
+
165
+ it('handles multiple consecutive chunks', () => {
166
+ const ctx = createMockAudioContext(10.0) as AudioContext & { _setTime: (t: number) => void };
167
+ const changes: string[] = [];
168
+ const scheduler = new VisemeScheduler(ctx, (v) => changes.push(v));
169
+
170
+ // First chunk at 10.0
171
+ scheduler.scheduleChunk(SAMPLE_PHONEMES, 10.0);
172
+ flushRaf();
173
+ expect(changes).toEqual(['cdgknstxyz']);
174
+
175
+ // Second chunk at 10.5 (after first chunk ends)
176
+ const chunk2: WordPhonemeInfo[] = [
177
+ {
178
+ wordIndex: 0,
179
+ phones: [{ phoneSymbol: 'w', startTime: 0.0, duration: 0.08, visemeSymbol: 'qw' }],
180
+ },
181
+ ];
182
+ scheduler.scheduleChunk(chunk2, 10.5);
183
+
184
+ // Advance past first chunk into second
185
+ ctx._setTime(10.51);
186
+ flushRaf();
187
+ expect(changes[changes.length - 1]).toBe('qw');
188
+
189
+ scheduler.destroy();
190
+ });
191
+
192
+ it('destroy() cleans up', () => {
193
+ const ctx = createMockAudioContext(10.0);
194
+ const scheduler = new VisemeScheduler(ctx, jest.fn());
195
+ scheduler.scheduleChunk(SAMPLE_PHONEMES, 10.0);
196
+
197
+ scheduler.destroy();
198
+ expect(scheduler.getCurrentViseme()).toBe('rest');
199
+ expect(cancelAnimationFrame).toHaveBeenCalled();
200
+ });
201
+
202
+ it('restarts the rAF loop when scheduleChunk is called after idle-stop', () => {
203
+ const ctx = createMockAudioContext(10.0) as AudioContext & { _setTime: (t: number) => void };
204
+ const scheduler = new VisemeScheduler(ctx, jest.fn());
205
+ const rafSpy = globalThis.requestAnimationFrame as jest.Mock;
206
+
207
+ // Drain the queue so the loop idle-stops.
208
+ scheduler.scheduleChunk(SAMPLE_PHONEMES, 10.0);
209
+ flushRaf();
210
+ ctx._setTime(10.5);
211
+ flushRaf(); // queue empty + rest fires → loop stops, animationFrameId = null
212
+
213
+ const rafCallsAtIdle = rafSpy.mock.calls.length;
214
+
215
+ // Schedule a fresh chunk after the prior one ended. The
216
+ // documented invariant in tick()'s idle-stop comment says
217
+ // scheduleChunk re-arms requestAnimationFrame when
218
+ // animationFrameId === null — this test pins that contract.
219
+ const chunk2: WordPhonemeInfo[] = [
220
+ {
221
+ wordIndex: 0,
222
+ phones: [{ phoneSymbol: 'w', startTime: 0.0, duration: 0.08, visemeSymbol: 'qw' }],
223
+ },
224
+ ];
225
+ scheduler.scheduleChunk(chunk2, 11.0);
226
+ expect(rafSpy.mock.calls.length).toBeGreaterThan(rafCallsAtIdle);
227
+
228
+ // And the next tick observes the freshly-scheduled phoneme.
229
+ ctx._setTime(11.0);
230
+ flushRaf();
231
+ expect(scheduler.getCurrentViseme()).toBe('qw');
232
+
233
+ scheduler.destroy();
234
+ });
235
+
236
+ it('stops the rAF loop when idle to avoid CPU leak', () => {
237
+ const ctx = createMockAudioContext(10.0) as AudioContext & { _setTime: (t: number) => void };
238
+ const scheduler = new VisemeScheduler(ctx, jest.fn());
239
+ const rafSpy = globalThis.requestAnimationFrame as jest.Mock;
240
+
241
+ scheduler.scheduleChunk(SAMPLE_PHONEMES, 10.0);
242
+ flushRaf(); // cdgknstxyz; queue still has more phonemes → loop reschedules
243
+ const rafCountWhileActive = rafSpy.mock.calls.length;
244
+ expect(rafCountWhileActive).toBeGreaterThan(0);
245
+
246
+ // Advance past the entire chunk; rest fires; queue empty → loop must stop
247
+ ctx._setTime(10.5);
248
+ flushRaf();
249
+
250
+ const rafCountAfterDrain = rafSpy.mock.calls.length;
251
+ flushRaf(); // would call tick again only if cb was scheduled — should be a no-op
252
+ expect(rafSpy.mock.calls.length).toBe(rafCountAfterDrain);
253
+
254
+ scheduler.destroy();
255
+ });
256
+ });