@escapeaihq/client-core 0.4.2 → 0.6.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 +5 -3
- package/dist/api.d.ts +95 -1
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +127 -2
- package/dist/api.js.map +1 -1
- package/dist/websocket.d.ts +105 -1
- package/dist/websocket.d.ts.map +1 -1
- package/dist/websocket.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/chat-auth.test.ts +261 -0
- package/src/__tests__/loadBundleManifest.test.ts +331 -0
- package/src/api.ts +204 -2
- package/src/websocket.ts +107 -1
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
// Tests for ApiClient.loadBundleManifest — closes specs/technical-debt.md#103.
|
|
2
|
+
|
|
3
|
+
import { ApiClient, type VisemeBundleRecord } from '../api';
|
|
4
|
+
|
|
5
|
+
const TEST_BASE_URL = 'http://test.local';
|
|
6
|
+
const TEST_CHAT_BASE_URL = 'http://test.local/chat';
|
|
7
|
+
|
|
8
|
+
const buildCompleteBundle = (
|
|
9
|
+
overrides: Partial<VisemeBundleRecord> = {},
|
|
10
|
+
): VisemeBundleRecord => ({
|
|
11
|
+
bundle_id: 'bundle-1',
|
|
12
|
+
persona_id: 'persona-1',
|
|
13
|
+
user_id: 'user-1',
|
|
14
|
+
status: 'complete',
|
|
15
|
+
viewbox_width: 800,
|
|
16
|
+
viewbox_height: 1200,
|
|
17
|
+
lip_fill: '#cc4444',
|
|
18
|
+
body_svg: '<svg>inline</svg>',
|
|
19
|
+
body_svg_s3_key: 'viseme-bundles/bundle-1/body.svg',
|
|
20
|
+
mouth_paths: { rest: 'M0 0', aei: 'M1 1' },
|
|
21
|
+
viseme_png_s3_keys: {
|
|
22
|
+
rest: 'viseme-bundles/bundle-1/visemes/rest.png',
|
|
23
|
+
aei: 'viseme-bundles/bundle-1/visemes/aei.png',
|
|
24
|
+
},
|
|
25
|
+
created_at: '2026-05-12T00:00:00Z',
|
|
26
|
+
updated_at: '2026-05-12T00:00:00Z',
|
|
27
|
+
...overrides,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const makeClient = () =>
|
|
31
|
+
new ApiClient(TEST_BASE_URL, 'test-token', TEST_CHAT_BASE_URL);
|
|
32
|
+
|
|
33
|
+
afterEach(() => {
|
|
34
|
+
jest.restoreAllMocks();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
describe('loadBundleManifest', () => {
|
|
38
|
+
it('returns null when persona has no viseme_bundle_id', async () => {
|
|
39
|
+
const client = makeClient();
|
|
40
|
+
const getSpy = jest.spyOn(client, 'getVisemeBundle');
|
|
41
|
+
|
|
42
|
+
const result = await client.loadBundleManifest({
|
|
43
|
+
persona_id: 'persona-1',
|
|
44
|
+
viseme_bundle_id: null,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
expect(result).toBeNull();
|
|
48
|
+
expect(getSpy).not.toHaveBeenCalled();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('returns inline manifest when body_svg is present on the row', async () => {
|
|
52
|
+
const client = makeClient();
|
|
53
|
+
jest.spyOn(client, 'getVisemeBundle').mockResolvedValue(buildCompleteBundle());
|
|
54
|
+
const bodySvgSpy = jest.spyOn(client, 'getVisemeBundleBodySvg');
|
|
55
|
+
|
|
56
|
+
const result = await client.loadBundleManifest({
|
|
57
|
+
persona_id: 'persona-1',
|
|
58
|
+
viseme_bundle_id: 'bundle-1',
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
expect(result).toEqual({
|
|
62
|
+
viewbox_width: 800,
|
|
63
|
+
viewbox_height: 1200,
|
|
64
|
+
body_svg: '<svg>inline</svg>',
|
|
65
|
+
lip_fill: '#cc4444',
|
|
66
|
+
mouth_paths: { rest: 'M0 0', aei: 'M1 1' },
|
|
67
|
+
});
|
|
68
|
+
expect(bodySvgSpy).not.toHaveBeenCalled();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('falls back to the body.svg proxy when body_svg is null but s3 key is set', async () => {
|
|
72
|
+
const client = makeClient();
|
|
73
|
+
jest.spyOn(client, 'getVisemeBundle').mockResolvedValue(
|
|
74
|
+
buildCompleteBundle({ body_svg: null }),
|
|
75
|
+
);
|
|
76
|
+
const bodySvgSpy = jest
|
|
77
|
+
.spyOn(client, 'getVisemeBundleBodySvg')
|
|
78
|
+
.mockResolvedValue('<svg>from-s3</svg>');
|
|
79
|
+
|
|
80
|
+
const result = await client.loadBundleManifest({
|
|
81
|
+
persona_id: 'persona-1',
|
|
82
|
+
viseme_bundle_id: 'bundle-1',
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
expect(bodySvgSpy).toHaveBeenCalledWith('persona-1', 'bundle-1', {
|
|
86
|
+
signal: undefined,
|
|
87
|
+
});
|
|
88
|
+
expect(result?.body_svg).toBe('<svg>from-s3</svg>');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('returns null when neither body_svg nor body_svg_s3_key is set', async () => {
|
|
92
|
+
const client = makeClient();
|
|
93
|
+
jest.spyOn(client, 'getVisemeBundle').mockResolvedValue(
|
|
94
|
+
buildCompleteBundle({ body_svg: null, body_svg_s3_key: null }),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const result = await client.loadBundleManifest({
|
|
98
|
+
persona_id: 'persona-1',
|
|
99
|
+
viseme_bundle_id: 'bundle-1',
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
expect(result).toBeNull();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('returns null when the S3 fallback fetch throws', async () => {
|
|
106
|
+
const client = makeClient();
|
|
107
|
+
jest.spyOn(client, 'getVisemeBundle').mockResolvedValue(
|
|
108
|
+
buildCompleteBundle({ body_svg: null }),
|
|
109
|
+
);
|
|
110
|
+
jest
|
|
111
|
+
.spyOn(client, 'getVisemeBundleBodySvg')
|
|
112
|
+
.mockRejectedValue(new Error('502 from upstream'));
|
|
113
|
+
// Non-abort failure path warns; mute so it doesn't pollute test output.
|
|
114
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
115
|
+
|
|
116
|
+
const result = await client.loadBundleManifest({
|
|
117
|
+
persona_id: 'persona-1',
|
|
118
|
+
viseme_bundle_id: 'bundle-1',
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
expect(result).toBeNull();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it.each([
|
|
125
|
+
['status pending', { status: 'pending' as const }],
|
|
126
|
+
['status failed', { status: 'failed' as const }],
|
|
127
|
+
['missing mouth_paths', { mouth_paths: null }],
|
|
128
|
+
['missing viewbox_width', { viewbox_width: null }],
|
|
129
|
+
['missing viewbox_height', { viewbox_height: null }],
|
|
130
|
+
['missing lip_fill', { lip_fill: null }],
|
|
131
|
+
])('returns null when bundle is %s', async (_name, overrides) => {
|
|
132
|
+
const client = makeClient();
|
|
133
|
+
jest
|
|
134
|
+
.spyOn(client, 'getVisemeBundle')
|
|
135
|
+
.mockResolvedValue(buildCompleteBundle(overrides));
|
|
136
|
+
|
|
137
|
+
const result = await client.loadBundleManifest({
|
|
138
|
+
persona_id: 'persona-1',
|
|
139
|
+
viseme_bundle_id: 'bundle-1',
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
expect(result).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('returns null when the bundle GET itself throws', async () => {
|
|
146
|
+
const client = makeClient();
|
|
147
|
+
jest
|
|
148
|
+
.spyOn(client, 'getVisemeBundle')
|
|
149
|
+
.mockRejectedValue(new Error('network'));
|
|
150
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
151
|
+
|
|
152
|
+
const result = await client.loadBundleManifest({
|
|
153
|
+
persona_id: 'persona-1',
|
|
154
|
+
viseme_bundle_id: 'bundle-1',
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
expect(result).toBeNull();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('populates viseme_png_urls against chatApiBaseUrl when opted in', async () => {
|
|
161
|
+
const client = makeClient();
|
|
162
|
+
jest.spyOn(client, 'getVisemeBundle').mockResolvedValue(buildCompleteBundle());
|
|
163
|
+
|
|
164
|
+
const result = await client.loadBundleManifest(
|
|
165
|
+
{ persona_id: 'persona-1', viseme_bundle_id: 'bundle-1' },
|
|
166
|
+
{ includeVisemePngUrls: true },
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
expect(result?.viseme_png_urls).toEqual({
|
|
170
|
+
rest: `${TEST_CHAT_BASE_URL}/api/v1/personas/persona-1/viseme-bundle/bundle-1/visemes/rest.png`,
|
|
171
|
+
aei: `${TEST_CHAT_BASE_URL}/api/v1/personas/persona-1/viseme-bundle/bundle-1/visemes/aei.png`,
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('encodes path segments in viseme_png_urls', async () => {
|
|
176
|
+
const client = makeClient();
|
|
177
|
+
jest.spyOn(client, 'getVisemeBundle').mockResolvedValue(
|
|
178
|
+
buildCompleteBundle({
|
|
179
|
+
viseme_png_s3_keys: { 'a/b': 'k1', 'c d': 'k2' },
|
|
180
|
+
}),
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const result = await client.loadBundleManifest(
|
|
184
|
+
{ persona_id: 'p 1', viseme_bundle_id: 'b/1' },
|
|
185
|
+
{ includeVisemePngUrls: true },
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
expect(result?.viseme_png_urls).toEqual({
|
|
189
|
+
'a/b': `${TEST_CHAT_BASE_URL}/api/v1/personas/p%201/viseme-bundle/b%2F1/visemes/a%2Fb.png`,
|
|
190
|
+
'c d': `${TEST_CHAT_BASE_URL}/api/v1/personas/p%201/viseme-bundle/b%2F1/visemes/c%20d.png`,
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('omits viseme_png_urls when includeVisemePngUrls=true but viseme_png_s3_keys is null', async () => {
|
|
195
|
+
const client = makeClient();
|
|
196
|
+
jest.spyOn(client, 'getVisemeBundle').mockResolvedValue(
|
|
197
|
+
buildCompleteBundle({ viseme_png_s3_keys: null }),
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
const result = await client.loadBundleManifest(
|
|
201
|
+
{ persona_id: 'persona-1', viseme_bundle_id: 'bundle-1' },
|
|
202
|
+
{ includeVisemePngUrls: true },
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
expect(result).not.toBeNull();
|
|
206
|
+
expect(result).not.toHaveProperty('viseme_png_urls');
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('omits viseme_png_urls by default', async () => {
|
|
210
|
+
const client = makeClient();
|
|
211
|
+
jest.spyOn(client, 'getVisemeBundle').mockResolvedValue(buildCompleteBundle());
|
|
212
|
+
|
|
213
|
+
const result = await client.loadBundleManifest({
|
|
214
|
+
persona_id: 'persona-1',
|
|
215
|
+
viseme_bundle_id: 'bundle-1',
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
expect(result).not.toHaveProperty('viseme_png_urls');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('forwards AbortSignal to getVisemeBundle', async () => {
|
|
222
|
+
const client = makeClient();
|
|
223
|
+
const getSpy = jest
|
|
224
|
+
.spyOn(client, 'getVisemeBundle')
|
|
225
|
+
.mockResolvedValue(buildCompleteBundle());
|
|
226
|
+
const controller = new AbortController();
|
|
227
|
+
|
|
228
|
+
await client.loadBundleManifest(
|
|
229
|
+
{ persona_id: 'persona-1', viseme_bundle_id: 'bundle-1' },
|
|
230
|
+
{ signal: controller.signal },
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
expect(getSpy).toHaveBeenCalledWith('persona-1', 'bundle-1', {
|
|
234
|
+
signal: controller.signal,
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it('returns null silently (no console.warn) when bundle GET aborts', async () => {
|
|
239
|
+
const client = makeClient();
|
|
240
|
+
jest
|
|
241
|
+
.spyOn(client, 'getVisemeBundle')
|
|
242
|
+
.mockRejectedValue(new DOMException('aborted', 'AbortError'));
|
|
243
|
+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
244
|
+
|
|
245
|
+
const result = await client.loadBundleManifest({
|
|
246
|
+
persona_id: 'persona-1',
|
|
247
|
+
viseme_bundle_id: 'bundle-1',
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
expect(result).toBeNull();
|
|
251
|
+
expect(warnSpy).not.toHaveBeenCalled();
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('treats plain Error with name="AbortError" as an abort (polyfill shape)', async () => {
|
|
255
|
+
// Some fetch polyfills throw a plain Error rather than a DOMException
|
|
256
|
+
// for aborts. The isAbortError check must accept both so warn
|
|
257
|
+
// suppression survives a future runtime swap.
|
|
258
|
+
const client = makeClient();
|
|
259
|
+
const polyfillAbort = Object.assign(new Error('aborted'), {
|
|
260
|
+
name: 'AbortError',
|
|
261
|
+
});
|
|
262
|
+
jest.spyOn(client, 'getVisemeBundle').mockRejectedValue(polyfillAbort);
|
|
263
|
+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
264
|
+
|
|
265
|
+
const result = await client.loadBundleManifest({
|
|
266
|
+
persona_id: 'persona-1',
|
|
267
|
+
viseme_bundle_id: 'bundle-1',
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
expect(result).toBeNull();
|
|
271
|
+
expect(warnSpy).not.toHaveBeenCalled();
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('returns null silently (no console.warn) when body.svg fallback aborts', async () => {
|
|
275
|
+
const client = makeClient();
|
|
276
|
+
jest.spyOn(client, 'getVisemeBundle').mockResolvedValue(
|
|
277
|
+
buildCompleteBundle({ body_svg: null }),
|
|
278
|
+
);
|
|
279
|
+
jest
|
|
280
|
+
.spyOn(client, 'getVisemeBundleBodySvg')
|
|
281
|
+
.mockRejectedValue(new DOMException('aborted', 'AbortError'));
|
|
282
|
+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
283
|
+
|
|
284
|
+
const result = await client.loadBundleManifest({
|
|
285
|
+
persona_id: 'persona-1',
|
|
286
|
+
viseme_bundle_id: 'bundle-1',
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
expect(result).toBeNull();
|
|
290
|
+
expect(warnSpy).not.toHaveBeenCalled();
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it('warns (but still returns null) when bundle GET fails for non-abort reasons', async () => {
|
|
294
|
+
const client = makeClient();
|
|
295
|
+
jest
|
|
296
|
+
.spyOn(client, 'getVisemeBundle')
|
|
297
|
+
.mockRejectedValue(new Error('500 Internal Server Error'));
|
|
298
|
+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
299
|
+
|
|
300
|
+
const result = await client.loadBundleManifest({
|
|
301
|
+
persona_id: 'persona-1',
|
|
302
|
+
viseme_bundle_id: 'bundle-1',
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
expect(result).toBeNull();
|
|
306
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
307
|
+
'[client-core] loadBundleManifest: bundle GET failed',
|
|
308
|
+
expect.any(Error),
|
|
309
|
+
);
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it('forwards AbortSignal to getVisemeBundleBodySvg on the S3 fallback path', async () => {
|
|
313
|
+
const client = makeClient();
|
|
314
|
+
jest.spyOn(client, 'getVisemeBundle').mockResolvedValue(
|
|
315
|
+
buildCompleteBundle({ body_svg: null }),
|
|
316
|
+
);
|
|
317
|
+
const bodySvgSpy = jest
|
|
318
|
+
.spyOn(client, 'getVisemeBundleBodySvg')
|
|
319
|
+
.mockResolvedValue('<svg>from-s3</svg>');
|
|
320
|
+
const controller = new AbortController();
|
|
321
|
+
|
|
322
|
+
await client.loadBundleManifest(
|
|
323
|
+
{ persona_id: 'persona-1', viseme_bundle_id: 'bundle-1' },
|
|
324
|
+
{ signal: controller.signal },
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
expect(bodySvgSpy).toHaveBeenCalledWith('persona-1', 'bundle-1', {
|
|
328
|
+
signal: controller.signal,
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
});
|
package/src/api.ts
CHANGED
|
@@ -210,6 +210,23 @@ export interface VisemeBundleRecord {
|
|
|
210
210
|
updated_at: string;
|
|
211
211
|
}
|
|
212
212
|
|
|
213
|
+
/**
|
|
214
|
+
* Render-ready bundle shape consumed by the persona face renderer in both
|
|
215
|
+
* `<seer-character-face>` (Vue) and `<CharacterFace>` (React). Built from
|
|
216
|
+
* a `VisemeBundleRecord` once `body_svg` has been resolved — inline when
|
|
217
|
+
* small, fetched from the body.svg S3 proxy when the SVG exceeded the
|
|
218
|
+
* DynamoDB inline cap.
|
|
219
|
+
*/
|
|
220
|
+
export interface VisemeBundleManifest {
|
|
221
|
+
viewbox_width: number;
|
|
222
|
+
viewbox_height: number;
|
|
223
|
+
body_svg: string;
|
|
224
|
+
lip_fill: string;
|
|
225
|
+
mouth_paths: Record<string, string>;
|
|
226
|
+
/** viseme symbol → URL. Absent until populated; never `null`. */
|
|
227
|
+
viseme_png_urls?: Record<string, string>;
|
|
228
|
+
}
|
|
229
|
+
|
|
213
230
|
export interface RegenerateVisemeAccept {
|
|
214
231
|
bundle_id: string;
|
|
215
232
|
viseme: string;
|
|
@@ -425,11 +442,46 @@ export interface VoiceInfo {
|
|
|
425
442
|
voiceId: string;
|
|
426
443
|
displayName: string;
|
|
427
444
|
description: string;
|
|
445
|
+
/** Inworld language code (e.g. `'EN_US'`, `'ZH_CN'`). Always present
|
|
446
|
+
* on current API responses — the backend pydantic model defaults to
|
|
447
|
+
* `""` rather than omitting the field — but typed optional so older
|
|
448
|
+
* cached payloads (pre-field) don't break consumers. Treat `""` and
|
|
449
|
+
* `undefined` equivalently as "no language code". */
|
|
450
|
+
langCode?: string;
|
|
428
451
|
languages: string[];
|
|
429
452
|
tags: string[];
|
|
453
|
+
/** Voice origin per `schemas.VoiceInfo.source`:
|
|
454
|
+
* - `'SYSTEM'` — built-in Inworld voice
|
|
455
|
+
* - `'IVC'` — Instant Voice Clone
|
|
456
|
+
* - `'PVC'` — Professional Voice Clone
|
|
457
|
+
* Backend default is `'SYSTEM'`. */
|
|
458
|
+
source?: string;
|
|
430
459
|
isCustom: boolean;
|
|
431
460
|
}
|
|
432
461
|
|
|
462
|
+
export interface VoicesResponse {
|
|
463
|
+
voices: VoiceInfo[];
|
|
464
|
+
/** Server reports whether this response came from the 5-min in-memory
|
|
465
|
+
* cache or a fresh Inworld fetch. */
|
|
466
|
+
cached: boolean;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/** Response shape for `POST /api/v1/chat/ws-token` — see
|
|
470
|
+
* `api/chat/app.py:create_websocket_token` + `schemas.WebSocketTokenResponse`.
|
|
471
|
+
* Ephemeral token (10-min TTL via `WS_TOKEN_TTL_SECONDS`) consumed by the
|
|
472
|
+
* WebSocket handler's auth cache. Prefer over passing the raw JWT in the
|
|
473
|
+
* `?token=` query param — JWTs leak into CloudFront / ALB / CloudWatch
|
|
474
|
+
* Logs which the ephemeral exchange avoids. */
|
|
475
|
+
export interface ChatWsTokenResponse {
|
|
476
|
+
wsToken: string;
|
|
477
|
+
/** Unix epoch seconds. Backend declares `float`, so the value may
|
|
478
|
+
* carry fractional seconds — don't `parseInt`/floor unless you need
|
|
479
|
+
* whole-second precision. */
|
|
480
|
+
expiresAt: number;
|
|
481
|
+
/** Seconds until expiry. Mirrors `WS_TOKEN_TTL_SECONDS` server-side. */
|
|
482
|
+
expiresIn: number;
|
|
483
|
+
}
|
|
484
|
+
|
|
433
485
|
// Session types matching backend shared/persona_models.py
|
|
434
486
|
export interface SessionMessage {
|
|
435
487
|
role: string;
|
|
@@ -585,6 +637,20 @@ export interface DeleteRelationshipResponse {
|
|
|
585
637
|
deleted: number;
|
|
586
638
|
}
|
|
587
639
|
|
|
640
|
+
/**
|
|
641
|
+
* Detect an abort (controller.abort(), unmount, signal expiry) so callers
|
|
642
|
+
* can stay silent on expected cancellations while still surfacing real
|
|
643
|
+
* fetch errors (5xx / timeout / CORS). Native `fetch` rejects with a
|
|
644
|
+
* `DOMException { name: 'AbortError' }`; the project's own pollers throw
|
|
645
|
+
* the same shape; a polyfilled fetch may throw a plain `Error` with the
|
|
646
|
+
* same name. Accept both so the warn-suppression survives a future
|
|
647
|
+
* polyfill or SSR runtime swap.
|
|
648
|
+
*/
|
|
649
|
+
function isAbortError(err: unknown): boolean {
|
|
650
|
+
if (err instanceof DOMException && err.name === 'AbortError') return true;
|
|
651
|
+
return err instanceof Error && err.name === 'AbortError';
|
|
652
|
+
}
|
|
653
|
+
|
|
588
654
|
export class ApiClient {
|
|
589
655
|
private baseURL: string;
|
|
590
656
|
private sessionToken?: string;
|
|
@@ -1172,6 +1238,41 @@ export class ApiClient {
|
|
|
1172
1238
|
}
|
|
1173
1239
|
}
|
|
1174
1240
|
|
|
1241
|
+
// ==================== Chat Auth + Voices ====================
|
|
1242
|
+
// Note: Both endpoints live on the Chat API (chatBaseUrl).
|
|
1243
|
+
|
|
1244
|
+
/**
|
|
1245
|
+
* Exchange a valid JWT for a short-lived (10-min TTL) ephemeral
|
|
1246
|
+
* WebSocket token. Use the returned `wsToken` as the `?token=` query
|
|
1247
|
+
* param when opening the chat WS. This avoids leaking the long-lived
|
|
1248
|
+
* JWT into query-string logs (CloudFront / ALB / CloudWatch). Backed
|
|
1249
|
+
* by `POST /api/v1/chat/ws-token` in `api/chat/app.py`.
|
|
1250
|
+
*/
|
|
1251
|
+
async getChatWsToken(): Promise<ChatWsTokenResponse> {
|
|
1252
|
+
return this.chatRequest<ChatWsTokenResponse>('/api/v1/chat/ws-token', {
|
|
1253
|
+
method: 'POST',
|
|
1254
|
+
});
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
/**
|
|
1258
|
+
* List available TTS voices. Backed by `GET /api/voices` — server
|
|
1259
|
+
* caches Inworld results for 5 min; pass `refresh: true` to bypass.
|
|
1260
|
+
*/
|
|
1261
|
+
async listVoices(params?: {
|
|
1262
|
+
/** ISO 639-1 (e.g. 'en'). */
|
|
1263
|
+
language?: string;
|
|
1264
|
+
/** Force a fresh fetch from Inworld, bypassing the 5-min cache. */
|
|
1265
|
+
refresh?: boolean;
|
|
1266
|
+
}): Promise<VoicesResponse> {
|
|
1267
|
+
const search = new URLSearchParams();
|
|
1268
|
+
if (params?.language) search.append('language', params.language);
|
|
1269
|
+
if (params?.refresh) search.append('refresh', 'true');
|
|
1270
|
+
const qs = search.toString();
|
|
1271
|
+
return this.chatRequest<VoicesResponse>(
|
|
1272
|
+
`/api/voices${qs ? `?${qs}` : ''}`
|
|
1273
|
+
);
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1175
1276
|
// ==================== Session API Methods ====================
|
|
1176
1277
|
// Note: Sessions are served by the Chat API, not the Tools API
|
|
1177
1278
|
|
|
@@ -1316,14 +1417,23 @@ export class ApiClient {
|
|
|
1316
1417
|
* Fetch a bundle's body SVG bytes from the backend's S3 proxy. Used as a
|
|
1317
1418
|
* fallback when the bundle row's inline body_svg is null because the SVG
|
|
1318
1419
|
* exceeded the DynamoDB inline cap.
|
|
1420
|
+
*
|
|
1421
|
+
* Pass `signal` to abort the in-flight fetch — `loadBundleManifest`
|
|
1422
|
+
* threads the caller's signal through so a rapid persona switch
|
|
1423
|
+
* cancels the (potentially MB-sized) SVG download rather than letting
|
|
1424
|
+
* it complete and get discarded by the cancelled-flag guard.
|
|
1319
1425
|
*/
|
|
1320
|
-
async getVisemeBundleBodySvg(
|
|
1426
|
+
async getVisemeBundleBodySvg(
|
|
1427
|
+
personaId: string,
|
|
1428
|
+
bundleId: string,
|
|
1429
|
+
options: { signal?: AbortSignal } = {},
|
|
1430
|
+
): Promise<string> {
|
|
1321
1431
|
const url = `${this.chatApiBaseUrl}/api/v1/personas/${personaId}/viseme-bundle/${bundleId}/body.svg`;
|
|
1322
1432
|
const headers: Record<string, string> = {};
|
|
1323
1433
|
if (this.sessionToken) {
|
|
1324
1434
|
headers['Authorization'] = `Bearer ${this.sessionToken}`;
|
|
1325
1435
|
}
|
|
1326
|
-
const response = await fetch(url, { headers });
|
|
1436
|
+
const response = await fetch(url, { headers, signal: options.signal });
|
|
1327
1437
|
if (!response.ok) {
|
|
1328
1438
|
throw new Error(`Failed to fetch body SVG: HTTP ${response.status}`);
|
|
1329
1439
|
}
|
|
@@ -1357,6 +1467,98 @@ export class ApiClient {
|
|
|
1357
1467
|
return URL.createObjectURL(blob);
|
|
1358
1468
|
}
|
|
1359
1469
|
|
|
1470
|
+
/**
|
|
1471
|
+
* Resolve a persona's viseme bundle to a render-ready manifest. Wraps
|
|
1472
|
+
* `getVisemeBundle` with the body_svg → S3 fallback so the inline-vs-S3
|
|
1473
|
+
* split is invisible to callers. Returns null when the persona has no
|
|
1474
|
+
* bundle, the bundle isn't `complete`, or required fields are missing
|
|
1475
|
+
* (renderers treat null as "fall back to the default face").
|
|
1476
|
+
*
|
|
1477
|
+
* `viseme_png_urls` is omitted by default. Pass `includeVisemePngUrls:
|
|
1478
|
+
* true` to populate it with proxy URLs built against `chatApiBaseUrl` —
|
|
1479
|
+
* works when a server-side proxy attaches auth (Vue/Web-App). React
|
|
1480
|
+
* renderers that need `<img>`-friendly blob URLs leave it unset and
|
|
1481
|
+
* populate the map themselves after the manifest loads.
|
|
1482
|
+
*/
|
|
1483
|
+
async loadBundleManifest(
|
|
1484
|
+
persona: { persona_id: string; viseme_bundle_id?: string | null },
|
|
1485
|
+
options: { signal?: AbortSignal; includeVisemePngUrls?: boolean } = {},
|
|
1486
|
+
): Promise<VisemeBundleManifest | null> {
|
|
1487
|
+
const bundleId = persona.viseme_bundle_id;
|
|
1488
|
+
if (!bundleId) return null;
|
|
1489
|
+
|
|
1490
|
+
let record: VisemeBundleRecord;
|
|
1491
|
+
try {
|
|
1492
|
+
record = await this.getVisemeBundle(persona.persona_id, bundleId, {
|
|
1493
|
+
signal: options.signal,
|
|
1494
|
+
});
|
|
1495
|
+
} catch (err) {
|
|
1496
|
+
// AbortError is an expected outcome (persona switch mid-flight,
|
|
1497
|
+
// unmount). Stay silent so the console isn't spammed on every UX
|
|
1498
|
+
// navigation. Everything else (5xx / timeout / CORS) is unexpected
|
|
1499
|
+
// — log so production debugging can distinguish "bundle pending"
|
|
1500
|
+
// from "fetch failed". Renderers still receive null and fall back.
|
|
1501
|
+
if (!isAbortError(err)) {
|
|
1502
|
+
console.warn('[client-core] loadBundleManifest: bundle GET failed', err);
|
|
1503
|
+
}
|
|
1504
|
+
return null;
|
|
1505
|
+
}
|
|
1506
|
+
|
|
1507
|
+
if (record.status !== 'complete') return null;
|
|
1508
|
+
if (
|
|
1509
|
+
!record.mouth_paths ||
|
|
1510
|
+
record.viewbox_width == null ||
|
|
1511
|
+
record.viewbox_height == null ||
|
|
1512
|
+
!record.lip_fill
|
|
1513
|
+
) {
|
|
1514
|
+
return null;
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
let bodySvg = record.body_svg;
|
|
1518
|
+
if (!bodySvg) {
|
|
1519
|
+
if (!record.body_svg_s3_key) return null;
|
|
1520
|
+
try {
|
|
1521
|
+
bodySvg = await this.getVisemeBundleBodySvg(
|
|
1522
|
+
persona.persona_id,
|
|
1523
|
+
bundleId,
|
|
1524
|
+
{ signal: options.signal },
|
|
1525
|
+
);
|
|
1526
|
+
} catch (err) {
|
|
1527
|
+
// Same visibility as the bundle-GET catch above — silent on
|
|
1528
|
+
// AbortError (expected on persona switch), warn on anything
|
|
1529
|
+
// else.
|
|
1530
|
+
if (!isAbortError(err)) {
|
|
1531
|
+
console.warn(
|
|
1532
|
+
'[client-core] loadBundleManifest: body.svg S3 fallback failed',
|
|
1533
|
+
err,
|
|
1534
|
+
);
|
|
1535
|
+
}
|
|
1536
|
+
return null;
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
let visemePngUrls: Record<string, string> | undefined;
|
|
1541
|
+
if (options.includeVisemePngUrls && record.viseme_png_s3_keys) {
|
|
1542
|
+
visemePngUrls = {};
|
|
1543
|
+
for (const viseme of Object.keys(record.viseme_png_s3_keys)) {
|
|
1544
|
+
visemePngUrls[viseme] =
|
|
1545
|
+
`${this.chatApiBaseUrl}/api/v1/personas/` +
|
|
1546
|
+
`${encodeURIComponent(persona.persona_id)}/viseme-bundle/` +
|
|
1547
|
+
`${encodeURIComponent(bundleId)}/visemes/` +
|
|
1548
|
+
`${encodeURIComponent(viseme)}.png`;
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
return {
|
|
1553
|
+
viewbox_width: record.viewbox_width,
|
|
1554
|
+
viewbox_height: record.viewbox_height,
|
|
1555
|
+
body_svg: bodySvg,
|
|
1556
|
+
lip_fill: record.lip_fill,
|
|
1557
|
+
mouth_paths: record.mouth_paths,
|
|
1558
|
+
...(visemePngUrls ? { viseme_png_urls: visemePngUrls } : {}),
|
|
1559
|
+
};
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1360
1562
|
/**
|
|
1361
1563
|
* Persona-tuner: GET defaults for the 11 viseme prompt descriptions.
|
|
1362
1564
|
*/
|