@escapeaihq/client-core 0.42.0 → 0.49.1
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 -5
- package/dist/adapters/worldAdapter.js +1 -1
- package/dist/admin.d.ts +123 -0
- package/dist/admin.d.ts.map +1 -1
- package/dist/admin.js +74 -0
- package/dist/admin.js.map +1 -1
- package/dist/api.d.ts +328 -2
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +198 -3
- package/dist/api.js.map +1 -1
- package/dist/conversation/binding.d.ts +18 -3
- package/dist/conversation/binding.d.ts.map +1 -1
- package/dist/conversation/binding.js +8 -2
- package/dist/conversation/binding.js.map +1 -1
- package/dist/conversation/controller.d.ts.map +1 -1
- package/dist/conversation/controller.js +8 -0
- package/dist/conversation/controller.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -4
- package/dist/index.js.map +1 -1
- package/dist/websocket.d.ts +19 -0
- package/dist/websocket.d.ts.map +1 -1
- package/dist/websocket.js.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/api.test.ts +83 -0
- package/src/__tests__/conversation/binding.test.ts +62 -0
- package/src/__tests__/conversation/controller.test.ts +35 -0
- package/src/__tests__/monitoring.test.ts +162 -0
- package/src/__tests__/persona-generation.test.ts +352 -0
- package/src/__tests__/websocket.test.ts +50 -0
- package/src/admin.ts +207 -0
- package/src/api.ts +507 -2
- package/src/conversation/binding.ts +22 -3
- package/src/conversation/controller.ts +8 -0
- package/src/index.ts +10 -4
- package/src/websocket.ts +24 -0
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for the C3a persona-generation client-core additions (spec Step 11) —
|
|
3
|
+
* the API methods + types web needs to trigger auto-generation and track job
|
|
4
|
+
* progress against the landed backend endpoints (spec Step 9):
|
|
5
|
+
* - `getPersonaGenSettings()` GET /api/v1/personas/settings/generation
|
|
6
|
+
* - `updatePersonaGenSettings(body)` PUT /api/v1/personas/settings/generation
|
|
7
|
+
* - `generatePersonasForVideo(videoId)` POST /api/v1/videos/{videoId}/personas/generate
|
|
8
|
+
* - `getPersonaJob(jobId)` GET /api/v1/persona-jobs/{jobId}
|
|
9
|
+
* - `listPersonasByVideo(videoId)` GET /api/v1/personas?source_video_id=
|
|
10
|
+
*
|
|
11
|
+
* Most route through `chatRequest` → `makeRequest` → fetch on the CHAT base URL
|
|
12
|
+
* (the personas API is mounted on the chat app). Same global-fetch-mock +
|
|
13
|
+
* save/restore discipline as cast.test.ts.
|
|
14
|
+
*
|
|
15
|
+
* Step 13 (review page) additions — the audition + reclone backend routes landed
|
|
16
|
+
* with tech-debt #477 (PR #356), so this file now also covers:
|
|
17
|
+
* - `auditionPersona(personaId, body?)` POST /api/v1/personas/{id}/audition → Blob
|
|
18
|
+
* - `reclonePersona(personaId)` POST /api/v1/personas/{id}/reclone
|
|
19
|
+
* Both bypass the JSON `chatRequest` wrapper (audition returns binary audio;
|
|
20
|
+
* reclone needs to surface the structured 409 as a typed RecloneConflictError).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { ApiClient, RecloneConflictError } from '../api';
|
|
24
|
+
import type {
|
|
25
|
+
AutoGenerationJob,
|
|
26
|
+
GeneratePersonasJobResponse,
|
|
27
|
+
PersonaGenerationSettings,
|
|
28
|
+
ReclonedPersonaResponse,
|
|
29
|
+
UpdatePersonaGenSettingsRequest,
|
|
30
|
+
} from '../api';
|
|
31
|
+
|
|
32
|
+
const TEST_BASE_URL = 'http://test.local';
|
|
33
|
+
const TEST_CHAT_BASE_URL = 'http://test.local/chat';
|
|
34
|
+
|
|
35
|
+
const makeClient = (sessionToken: string = 'test-token') =>
|
|
36
|
+
new ApiClient(TEST_BASE_URL, sessionToken, TEST_CHAT_BASE_URL);
|
|
37
|
+
|
|
38
|
+
const originalFetch = (globalThis as { fetch?: typeof fetch }).fetch;
|
|
39
|
+
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
if (originalFetch === undefined) {
|
|
42
|
+
delete (globalThis as { fetch?: typeof fetch }).fetch;
|
|
43
|
+
} else {
|
|
44
|
+
(globalThis as { fetch?: typeof fetch }).fetch = originalFetch;
|
|
45
|
+
}
|
|
46
|
+
jest.restoreAllMocks();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const mockJson = (body: unknown, init?: { ok?: boolean; status?: number }) => {
|
|
50
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
51
|
+
ok: init?.ok ?? true,
|
|
52
|
+
status: init?.status ?? 200,
|
|
53
|
+
json: async () => body,
|
|
54
|
+
});
|
|
55
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
56
|
+
return fetchMock;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const buildSettings = (): PersonaGenerationSettings => ({
|
|
60
|
+
user_id: 'u-1',
|
|
61
|
+
default_voice_id: 'voice-default',
|
|
62
|
+
default_tts_model_id: 'inworld-tts-1',
|
|
63
|
+
default_temperature: 1.1,
|
|
64
|
+
default_speaking_rate: 1.0,
|
|
65
|
+
default_audio_format: 'PCM16',
|
|
66
|
+
default_enable_markups: false,
|
|
67
|
+
default_chat_model_id: 'amazon.nova-pro-v1:0',
|
|
68
|
+
default_system_prompt_template: null,
|
|
69
|
+
default_system_prompt_override: false,
|
|
70
|
+
entity_type_filter: ['person'],
|
|
71
|
+
min_dialog_lines: 1,
|
|
72
|
+
min_graph_speakers: 1,
|
|
73
|
+
max_personas: 20,
|
|
74
|
+
auto_generate_enabled: true,
|
|
75
|
+
voice_clone_max_retries: 3,
|
|
76
|
+
voice_clone_min_clip_seconds: 10.0,
|
|
77
|
+
updated_at: '2026-06-14T00:00:00Z',
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('getPersonaGenSettings', () => {
|
|
81
|
+
it('GETs /api/v1/personas/settings/generation on the CHAT base URL', async () => {
|
|
82
|
+
const fetchMock = mockJson(buildSettings());
|
|
83
|
+
|
|
84
|
+
const result = await makeClient().getPersonaGenSettings();
|
|
85
|
+
|
|
86
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
87
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
88
|
+
expect(url).toBe(`${TEST_CHAT_BASE_URL}/api/v1/personas/settings/generation`);
|
|
89
|
+
// Default RequestInit → GET (no method override).
|
|
90
|
+
expect(init?.method).toBeUndefined();
|
|
91
|
+
expect(result?.max_personas).toBe(20);
|
|
92
|
+
expect(result?.entity_type_filter).toEqual(['person']);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('returns null when the user has never saved settings', async () => {
|
|
96
|
+
mockJson(null);
|
|
97
|
+
const result = await makeClient().getPersonaGenSettings();
|
|
98
|
+
expect(result).toBeNull();
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
describe('updatePersonaGenSettings', () => {
|
|
103
|
+
it('PUTs the partial body to settings/generation and returns the merged settings', async () => {
|
|
104
|
+
const merged = { ...buildSettings(), max_personas: 5, min_dialog_lines: 2 };
|
|
105
|
+
const fetchMock = mockJson(merged);
|
|
106
|
+
|
|
107
|
+
const body: UpdatePersonaGenSettingsRequest = {
|
|
108
|
+
max_personas: 5,
|
|
109
|
+
min_dialog_lines: 2,
|
|
110
|
+
};
|
|
111
|
+
const result = await makeClient().updatePersonaGenSettings(body);
|
|
112
|
+
|
|
113
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
114
|
+
expect(url).toBe(`${TEST_CHAT_BASE_URL}/api/v1/personas/settings/generation`);
|
|
115
|
+
expect(init?.method).toBe('PUT');
|
|
116
|
+
expect(JSON.parse(init?.body as string)).toEqual(body);
|
|
117
|
+
expect(result.max_personas).toBe(5);
|
|
118
|
+
expect(result.min_dialog_lines).toBe(2);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('generatePersonasForVideo', () => {
|
|
123
|
+
it('POSTs to /api/v1/videos/{videoId}/personas/generate and returns the job envelope', async () => {
|
|
124
|
+
const envelope: GeneratePersonasJobResponse = {
|
|
125
|
+
jobId: 'job-abc',
|
|
126
|
+
status: 'pending',
|
|
127
|
+
traceId: 'trace-xyz',
|
|
128
|
+
};
|
|
129
|
+
const fetchMock = mockJson(envelope, { status: 202 });
|
|
130
|
+
|
|
131
|
+
const result = await makeClient().generatePersonasForVideo('v-123');
|
|
132
|
+
|
|
133
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
134
|
+
// camelCase wire keys (jobId/traceId) match the backend alias serialization.
|
|
135
|
+
expect(url).toBe(`${TEST_CHAT_BASE_URL}/api/v1/videos/v-123/personas/generate`);
|
|
136
|
+
expect(init?.method).toBe('POST');
|
|
137
|
+
// C3a-f1: with no entityIds the call is byte-identical to before — NO body
|
|
138
|
+
// (the whole-video wire shape; the backend defaults to all characters).
|
|
139
|
+
expect(init?.body).toBeUndefined();
|
|
140
|
+
expect(result.jobId).toBe('job-abc');
|
|
141
|
+
expect(result.status).toBe('pending');
|
|
142
|
+
expect(result.traceId).toBe('trace-xyz');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('URI-encodes the video id', async () => {
|
|
146
|
+
const fetchMock = mockJson({ jobId: 'j', status: 'pending' });
|
|
147
|
+
await makeClient().generatePersonasForVideo('v/with space');
|
|
148
|
+
const [url] = fetchMock.mock.calls[0];
|
|
149
|
+
expect(url).toBe(
|
|
150
|
+
`${TEST_CHAT_BASE_URL}/api/v1/videos/${encodeURIComponent('v/with space')}/personas/generate`,
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('C3a-f1: sends a snake_case entity_ids body when an entityIds subset is given', async () => {
|
|
155
|
+
const fetchMock = mockJson({ jobId: 'j', status: 'pending' });
|
|
156
|
+
await makeClient().generatePersonasForVideo('v-1', {
|
|
157
|
+
entityIds: ['ent-A', 'ent-B'],
|
|
158
|
+
});
|
|
159
|
+
const [, init] = fetchMock.mock.calls[0];
|
|
160
|
+
expect(init?.method).toBe('POST');
|
|
161
|
+
// Body mirrors GeneratePersonasRequest (snake_case), NOT the camelCase
|
|
162
|
+
// response model.
|
|
163
|
+
expect(JSON.parse(init?.body as string)).toEqual({
|
|
164
|
+
entity_ids: ['ent-A', 'ent-B'],
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('C3a-f1: an empty entityIds subset sends no body (whole-video default)', async () => {
|
|
169
|
+
const fetchMock = mockJson({ jobId: 'j', status: 'pending' });
|
|
170
|
+
await makeClient().generatePersonasForVideo('v-1', { entityIds: [] });
|
|
171
|
+
const [, init] = fetchMock.mock.calls[0];
|
|
172
|
+
expect(init?.body).toBeUndefined();
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe('getPersonaJob', () => {
|
|
177
|
+
it('GETs /api/v1/persona-jobs/{jobId} and returns the snake_case job record', async () => {
|
|
178
|
+
const job: AutoGenerationJob = {
|
|
179
|
+
job_id: 'job-abc',
|
|
180
|
+
user_id: 'u-1',
|
|
181
|
+
video_id: 'v-123',
|
|
182
|
+
status: 'cloning',
|
|
183
|
+
created_at: '2026-06-14T18:01:02Z',
|
|
184
|
+
updated_at: '2026-06-14T18:03:14Z',
|
|
185
|
+
progress: { total_characters: 4, synthesized: 4, cloned: 2, failed: 0 },
|
|
186
|
+
persona_ids: ['p-1', 'p-2'],
|
|
187
|
+
errors: [],
|
|
188
|
+
clips_used: { 'p-1': [3, 7] },
|
|
189
|
+
};
|
|
190
|
+
const fetchMock = mockJson(job);
|
|
191
|
+
|
|
192
|
+
const result = await makeClient().getPersonaJob('job-abc');
|
|
193
|
+
|
|
194
|
+
const [url] = fetchMock.mock.calls[0];
|
|
195
|
+
expect(url).toBe(`${TEST_CHAT_BASE_URL}/api/v1/persona-jobs/job-abc`);
|
|
196
|
+
expect(result.status).toBe('cloning');
|
|
197
|
+
expect(result.progress.cloned).toBe(2);
|
|
198
|
+
expect(result.persona_ids).toEqual(['p-1', 'p-2']);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('URI-encodes the job id', async () => {
|
|
202
|
+
const fetchMock = mockJson({});
|
|
203
|
+
await makeClient().getPersonaJob('job/weird');
|
|
204
|
+
const [url] = fetchMock.mock.calls[0];
|
|
205
|
+
expect(url).toBe(
|
|
206
|
+
`${TEST_CHAT_BASE_URL}/api/v1/persona-jobs/${encodeURIComponent('job/weird')}`,
|
|
207
|
+
);
|
|
208
|
+
});
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
describe('listPersonasByVideo', () => {
|
|
212
|
+
it('GETs /api/v1/personas?source_video_id= and returns the list response', async () => {
|
|
213
|
+
const fetchMock = mockJson({
|
|
214
|
+
personas: [],
|
|
215
|
+
total: 0,
|
|
216
|
+
next_token: null,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const result = await makeClient().listPersonasByVideo('v-123');
|
|
220
|
+
|
|
221
|
+
const [url] = fetchMock.mock.calls[0];
|
|
222
|
+
expect(url).toBe(
|
|
223
|
+
`${TEST_CHAT_BASE_URL}/api/v1/personas?source_video_id=v-123`,
|
|
224
|
+
);
|
|
225
|
+
expect(result.personas).toEqual([]);
|
|
226
|
+
expect(result.total).toBe(0);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('is also reachable via listPersonas({ sourceVideoId })', async () => {
|
|
230
|
+
const fetchMock = mockJson({ personas: [], total: 0, next_token: null });
|
|
231
|
+
await makeClient().listPersonas({ sourceVideoId: 'v-9' });
|
|
232
|
+
const [url] = fetchMock.mock.calls[0];
|
|
233
|
+
expect(url).toBe(`${TEST_CHAT_BASE_URL}/api/v1/personas?source_video_id=v-9`);
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// A fetch mock returning a binary body via `.blob()` (the audio/wav response).
|
|
238
|
+
const mockBlob = (
|
|
239
|
+
body: BlobPart,
|
|
240
|
+
init?: { ok?: boolean; status?: number; json?: unknown },
|
|
241
|
+
) => {
|
|
242
|
+
const blob = new Blob([body], { type: 'audio/wav' });
|
|
243
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
244
|
+
ok: init?.ok ?? true,
|
|
245
|
+
status: init?.status ?? 200,
|
|
246
|
+
blob: async () => blob,
|
|
247
|
+
json: async () => init?.json ?? {},
|
|
248
|
+
});
|
|
249
|
+
(globalThis as { fetch?: typeof fetch }).fetch = fetchMock as unknown as typeof fetch;
|
|
250
|
+
return fetchMock;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
describe('auditionPersona', () => {
|
|
254
|
+
it('POSTs to /personas/{id}/audition with a JSON body and returns the audio blob', async () => {
|
|
255
|
+
const fetchMock = mockBlob('RIFFwav');
|
|
256
|
+
|
|
257
|
+
const blob = await makeClient().auditionPersona('p-1', { text: 'Hi there' });
|
|
258
|
+
|
|
259
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
260
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
261
|
+
expect(url).toBe(`${TEST_CHAT_BASE_URL}/api/v1/personas/p-1/audition`);
|
|
262
|
+
expect(init?.method).toBe('POST');
|
|
263
|
+
expect(JSON.parse(init?.body as string)).toEqual({ text: 'Hi there' });
|
|
264
|
+
expect((init?.headers as Record<string, string>)['Authorization']).toBe('Bearer test-token');
|
|
265
|
+
expect(blob.type).toBe('audio/wav');
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('sends an empty JSON object when no body is given (default sample text)', async () => {
|
|
269
|
+
const fetchMock = mockBlob('x');
|
|
270
|
+
await makeClient().auditionPersona('p-2');
|
|
271
|
+
const [, init] = fetchMock.mock.calls[0];
|
|
272
|
+
expect(JSON.parse(init?.body as string)).toEqual({});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('URI-encodes the persona id', async () => {
|
|
276
|
+
const fetchMock = mockBlob('x');
|
|
277
|
+
await makeClient().auditionPersona('p/weird');
|
|
278
|
+
const [url] = fetchMock.mock.calls[0];
|
|
279
|
+
expect(url).toBe(
|
|
280
|
+
`${TEST_CHAT_BASE_URL}/api/v1/personas/${encodeURIComponent('p/weird')}/audition`,
|
|
281
|
+
);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it('throws the FastAPI detail when the render fails', async () => {
|
|
285
|
+
mockBlob('x', {
|
|
286
|
+
ok: false,
|
|
287
|
+
status: 502,
|
|
288
|
+
json: { detail: 'Voice audition render failed' },
|
|
289
|
+
});
|
|
290
|
+
await expect(makeClient().auditionPersona('p-1')).rejects.toThrow(
|
|
291
|
+
'Voice audition render failed',
|
|
292
|
+
);
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
describe('reclonePersona', () => {
|
|
297
|
+
it('POSTs to /personas/{id}/reclone and returns the camelCase response', async () => {
|
|
298
|
+
const body: ReclonedPersonaResponse = {
|
|
299
|
+
personaId: 'p-1',
|
|
300
|
+
voiceId: 'voice-new',
|
|
301
|
+
clipsUsed: [0, 1, 2, 3],
|
|
302
|
+
};
|
|
303
|
+
const fetchMock = mockJson(body);
|
|
304
|
+
|
|
305
|
+
const result = await makeClient().reclonePersona('p-1');
|
|
306
|
+
|
|
307
|
+
const [url, init] = fetchMock.mock.calls[0];
|
|
308
|
+
expect(url).toBe(`${TEST_CHAT_BASE_URL}/api/v1/personas/p-1/reclone`);
|
|
309
|
+
expect(init?.method).toBe('POST');
|
|
310
|
+
expect(result.voiceId).toBe('voice-new');
|
|
311
|
+
expect(result.clipsUsed).toEqual([0, 1, 2, 3]);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('raises a typed RecloneConflictError on a 409 retries-exhausted', async () => {
|
|
315
|
+
mockJson(
|
|
316
|
+
{ detail: { error: 'voice_clone_max_retries_exhausted', attempts: 3 } },
|
|
317
|
+
{ ok: false, status: 409 },
|
|
318
|
+
);
|
|
319
|
+
const err = await makeClient()
|
|
320
|
+
.reclonePersona('p-1')
|
|
321
|
+
.catch((e) => e);
|
|
322
|
+
expect(err).toBeInstanceOf(RecloneConflictError);
|
|
323
|
+
expect(err.code).toBe('voice_clone_max_retries_exhausted');
|
|
324
|
+
expect(err.attempts).toBe(3);
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it('raises a typed RecloneConflictError on a 409 no-clips-remaining', async () => {
|
|
328
|
+
mockJson(
|
|
329
|
+
{ detail: { error: 'no_candidate_clips_remaining' } },
|
|
330
|
+
{ ok: false, status: 409 },
|
|
331
|
+
);
|
|
332
|
+
const err = await makeClient()
|
|
333
|
+
.reclonePersona('p-1')
|
|
334
|
+
.catch((e) => e);
|
|
335
|
+
expect(err).toBeInstanceOf(RecloneConflictError);
|
|
336
|
+
expect(err.code).toBe('no_candidate_clips_remaining');
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it('throws the FastAPI detail string on a non-conflict failure', async () => {
|
|
340
|
+
mockJson({ detail: 'Voice cloning failed' }, { ok: false, status: 502 });
|
|
341
|
+
await expect(makeClient().reclonePersona('p-1')).rejects.toThrow('Voice cloning failed');
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it('URI-encodes the persona id', async () => {
|
|
345
|
+
const fetchMock = mockJson({ personaId: 'x', voiceId: 'v', clipsUsed: [] });
|
|
346
|
+
await makeClient().reclonePersona('p/weird');
|
|
347
|
+
const [url] = fetchMock.mock.calls[0];
|
|
348
|
+
expect(url).toBe(
|
|
349
|
+
`${TEST_CHAT_BASE_URL}/api/v1/personas/${encodeURIComponent('p/weird')}/reclone`,
|
|
350
|
+
);
|
|
351
|
+
});
|
|
352
|
+
});
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
|
|
13
13
|
import { ChatWebSocket } from '../websocket';
|
|
14
14
|
import type { ChatWsEvent } from '../websocket';
|
|
15
|
+
import type { DmDirectiveEvent, DmDirectiveKind } from '../api';
|
|
16
|
+
import { ConversationRuntime } from '../conversation/runtime';
|
|
17
|
+
import { PresenceKind } from '../conversation/participant';
|
|
15
18
|
|
|
16
19
|
interface FakeWebSocket {
|
|
17
20
|
url: string;
|
|
@@ -564,3 +567,50 @@ describe('ChatWsEvent — M10 9a multi-persona protocol additions', () => {
|
|
|
564
567
|
}
|
|
565
568
|
});
|
|
566
569
|
});
|
|
570
|
+
|
|
571
|
+
describe('ChatWsEvent — Dashboard Spec A dm_directive (A-DM0, land-dark)', () => {
|
|
572
|
+
it('narrows a dm_directive event to its directive / params fields', () => {
|
|
573
|
+
const event: ChatWsEvent = {
|
|
574
|
+
type: 'dm_directive',
|
|
575
|
+
timestamp: 2.0,
|
|
576
|
+
directive: 'enter_world',
|
|
577
|
+
params: { prompt: 'Walk into the fog' },
|
|
578
|
+
};
|
|
579
|
+
expect(event.type).toBe('dm_directive');
|
|
580
|
+
if (event.type === 'dm_directive') {
|
|
581
|
+
expect(event.directive).toBe('enter_world');
|
|
582
|
+
expect(event.params.prompt).toBe('Walk into the fog');
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
|
|
586
|
+
it('accepts each allowlisted directive kind (single-sourced with the backend)', () => {
|
|
587
|
+
const kinds: DmDirectiveKind[] = [
|
|
588
|
+
'enter_world',
|
|
589
|
+
'exit_world',
|
|
590
|
+
'seek_video',
|
|
591
|
+
'set_experience_source',
|
|
592
|
+
];
|
|
593
|
+
for (const directive of kinds) {
|
|
594
|
+
const event: DmDirectiveEvent = { type: 'dm_directive', directive, params: {} };
|
|
595
|
+
expect(event.directive).toBe(directive);
|
|
596
|
+
}
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
it('is dropped by ConversationRuntime.applyFrame (default arm, no state change)', () => {
|
|
600
|
+
const rt = new ConversationRuntime({
|
|
601
|
+
boundPersonaId: 'p-dm',
|
|
602
|
+
participants: [
|
|
603
|
+
{
|
|
604
|
+
personaId: 'p-dm',
|
|
605
|
+
isDm: true,
|
|
606
|
+
displayName: 'The Narrator',
|
|
607
|
+
presence: PresenceKind.PRESENCE_FACE_2D,
|
|
608
|
+
},
|
|
609
|
+
],
|
|
610
|
+
});
|
|
611
|
+
const before = rt.getState();
|
|
612
|
+
rt.applyFrame({ type: 'dm_directive', directive: 'exit_world', params: {} });
|
|
613
|
+
// Behavior-preserving: the runtime state is untouched (no consumer wired).
|
|
614
|
+
expect(rt.getState()).toEqual(before);
|
|
615
|
+
});
|
|
616
|
+
});
|
package/src/admin.ts
CHANGED
|
@@ -135,9 +135,104 @@ export interface AllEvalRunsResult {
|
|
|
135
135
|
truncated: boolean;
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
+
// --------------------------------------------------------------------------- //
|
|
139
|
+
// C4 Phase 3 — admin monitoring tools. Read-only proxies over CloudWatch
|
|
140
|
+
// (GetMetricData) + X-Ray (GetTraceSummaries / BatchGetTraces), behind the same
|
|
141
|
+
// require_admin gate. Mirror the backend schemas in `api/admin/schemas.py`
|
|
142
|
+
// EXACTLY (camelCase + nullability).
|
|
143
|
+
// --------------------------------------------------------------------------- //
|
|
144
|
+
|
|
145
|
+
/** One allowlisted metric the admin tools may chart (catalog view). */
|
|
146
|
+
export interface MonitoringMetricCatalogEntry {
|
|
147
|
+
metricId: string;
|
|
148
|
+
namespace: string;
|
|
149
|
+
metricName: string;
|
|
150
|
+
stat: string;
|
|
151
|
+
unit: string;
|
|
152
|
+
label: string;
|
|
153
|
+
dimensionKeys: string[];
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Response for `GET /api/v1/admin/monitoring/metrics` (catalog list). */
|
|
157
|
+
export interface MonitoringMetricCatalogResponse {
|
|
158
|
+
metrics: MonitoringMetricCatalogEntry[];
|
|
159
|
+
traceId: string;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** One (timestamp, value) point on a metric series. */
|
|
163
|
+
export interface MonitoringMetricDatapoint {
|
|
164
|
+
timestamp: string;
|
|
165
|
+
value: number;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** A resolved CloudWatch series for one allowlisted metric. */
|
|
169
|
+
export interface MonitoringMetricSeriesResponse {
|
|
170
|
+
metricId: string;
|
|
171
|
+
label: string;
|
|
172
|
+
unit: string;
|
|
173
|
+
periodSeconds: number;
|
|
174
|
+
startTime: string;
|
|
175
|
+
endTime: string;
|
|
176
|
+
/** May be empty (no data in window / seam not yet instrumented). */
|
|
177
|
+
datapoints: MonitoringMetricDatapoint[];
|
|
178
|
+
traceId: string;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** One X-Ray trace in the explorer list view. */
|
|
182
|
+
export interface MonitoringTraceSummary {
|
|
183
|
+
traceId: string;
|
|
184
|
+
durationSeconds: number | null;
|
|
185
|
+
responseTimeSeconds: number | null;
|
|
186
|
+
hasError: boolean;
|
|
187
|
+
hasFault: boolean;
|
|
188
|
+
hasThrottle: boolean;
|
|
189
|
+
httpStatus: number | null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Response for `GET /api/v1/admin/monitoring/traces` (X-Ray summaries). */
|
|
193
|
+
export interface MonitoringTraceSummariesResponse {
|
|
194
|
+
startTime: string;
|
|
195
|
+
endTime: string;
|
|
196
|
+
/** Empty until more seams are instrumented (only bedrock.converse so far). */
|
|
197
|
+
traces: MonitoringTraceSummary[];
|
|
198
|
+
traceId: string;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Response for `GET /api/v1/admin/monitoring/traces/{xrayTraceId}`.
|
|
203
|
+
*
|
|
204
|
+
* `found=false` (200, empty `segments`) when X-Ray has no such trace. `segments`
|
|
205
|
+
* is the raw X-Ray segment-document list for the client-side waterfall renderer.
|
|
206
|
+
*/
|
|
207
|
+
export interface MonitoringTraceDetailResponse {
|
|
208
|
+
xrayTraceId: string;
|
|
209
|
+
found: boolean;
|
|
210
|
+
segments: Array<Record<string, unknown>>;
|
|
211
|
+
traceId: string;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Query params for `getMetricSeries`. */
|
|
215
|
+
export interface MetricSeriesParams {
|
|
216
|
+
/** Look-back window in seconds (server clamps to <= 14 days). */
|
|
217
|
+
rangeSeconds?: number;
|
|
218
|
+
/** Aggregation bucket in seconds (server clamps to [60, 86400]). */
|
|
219
|
+
periodSeconds?: number;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Query params for `listTraces`. */
|
|
223
|
+
export interface ListTracesParams {
|
|
224
|
+
/** Filter by the seer.job_id span annotation. */
|
|
225
|
+
jobId?: string;
|
|
226
|
+
/** Filter by the seer.video_id span annotation. */
|
|
227
|
+
videoId?: string;
|
|
228
|
+
/** Look-back window in seconds (server clamps to <= 6 hours). */
|
|
229
|
+
rangeSeconds?: number;
|
|
230
|
+
}
|
|
231
|
+
|
|
138
232
|
const ADMIN_PROMPTS_PATH = '/api/v1/admin/prompts';
|
|
139
233
|
const ADMIN_EVAL_RUNS_PATH = '/api/v1/admin/eval-runs';
|
|
140
234
|
const ADMIN_REGEN_PROMPTS_PATH = '/api/v1/admin/regen-prompts';
|
|
235
|
+
const ADMIN_MONITORING_PATH = '/api/v1/admin/monitoring';
|
|
141
236
|
|
|
142
237
|
/**
|
|
143
238
|
* Safety cap on `listAllEvalRuns` pagination. A misbehaving server that always
|
|
@@ -308,3 +403,115 @@ export class AdminClient {
|
|
|
308
403
|
return { runs, truncated: true };
|
|
309
404
|
}
|
|
310
405
|
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Client for the C4 Phase-3 admin monitoring tools (read-only).
|
|
409
|
+
*
|
|
410
|
+
* Wraps the four `require_admin`-gated `/api/v1/admin/monitoring/*` read proxies
|
|
411
|
+
* over CloudWatch (GetMetricData) + X-Ray (GetTraceSummaries / BatchGetTraces).
|
|
412
|
+
* Like {@link AdminClient}, it targets the CHAT API base (the router is mounted
|
|
413
|
+
* in `api/chat/app.py`) and carries the Descope session token. Additive — it
|
|
414
|
+
* does NOT touch the public `ApiClient` surface. A metric is selected by its
|
|
415
|
+
* allowlist `metricId` (from {@link listMetrics}); there is no raw
|
|
416
|
+
* namespace/metric-name pass-through.
|
|
417
|
+
*/
|
|
418
|
+
export class MonitoringClient {
|
|
419
|
+
private chatBaseUrl: string;
|
|
420
|
+
private sessionToken?: string;
|
|
421
|
+
|
|
422
|
+
constructor(chatBaseUrl: string, sessionToken?: string) {
|
|
423
|
+
this.chatBaseUrl = chatBaseUrl;
|
|
424
|
+
this.sessionToken = sessionToken;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** Update the bearer token (e.g. after a Descope session refresh). */
|
|
428
|
+
setSessionToken(token: string | undefined): void {
|
|
429
|
+
this.sessionToken = token;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** List the allowlisted metric catalog (what can be charted; no AWS call). */
|
|
433
|
+
async listMetrics(
|
|
434
|
+
options: { signal?: AbortSignal } = {},
|
|
435
|
+
): Promise<MonitoringMetricCatalogResponse> {
|
|
436
|
+
return requestJson<MonitoringMetricCatalogResponse>(
|
|
437
|
+
this.chatBaseUrl,
|
|
438
|
+
`${ADMIN_MONITORING_PATH}/metrics`,
|
|
439
|
+
this.sessionToken,
|
|
440
|
+
{ signal: options.signal },
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Fetch a CloudWatch series for ONE allowlisted metric over a clamped window.
|
|
446
|
+
* An unknown `metricId` yields a 404 (thrown `Error`); an empty `datapoints`
|
|
447
|
+
* is normal (no data / seam not yet wired).
|
|
448
|
+
*/
|
|
449
|
+
async getMetricSeries(
|
|
450
|
+
metricId: string,
|
|
451
|
+
params: MetricSeriesParams = {},
|
|
452
|
+
options: { signal?: AbortSignal } = {},
|
|
453
|
+
): Promise<MonitoringMetricSeriesResponse> {
|
|
454
|
+
const search = new URLSearchParams();
|
|
455
|
+
if (params.rangeSeconds !== undefined) {
|
|
456
|
+
search.set('rangeSeconds', String(params.rangeSeconds));
|
|
457
|
+
}
|
|
458
|
+
if (params.periodSeconds !== undefined) {
|
|
459
|
+
search.set('periodSeconds', String(params.periodSeconds));
|
|
460
|
+
}
|
|
461
|
+
const qs = search.toString();
|
|
462
|
+
const base = `${ADMIN_MONITORING_PATH}/metrics/${encodeURIComponent(metricId)}`;
|
|
463
|
+
const path = qs ? `${base}?${qs}` : base;
|
|
464
|
+
return requestJson<MonitoringMetricSeriesResponse>(
|
|
465
|
+
this.chatBaseUrl,
|
|
466
|
+
path,
|
|
467
|
+
this.sessionToken,
|
|
468
|
+
{ signal: options.signal },
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* List recent X-Ray traces, optionally filtered by `jobId` / `videoId` (the
|
|
474
|
+
* `seer.*` span annotations) + window. An empty list is normal until more
|
|
475
|
+
* seams are instrumented. A malformed id (quotes/whitespace) yields a 422.
|
|
476
|
+
*/
|
|
477
|
+
async listTraces(
|
|
478
|
+
params: ListTracesParams = {},
|
|
479
|
+
options: { signal?: AbortSignal } = {},
|
|
480
|
+
): Promise<MonitoringTraceSummariesResponse> {
|
|
481
|
+
const search = new URLSearchParams();
|
|
482
|
+
if (params.jobId !== undefined) {
|
|
483
|
+
search.set('jobId', params.jobId);
|
|
484
|
+
}
|
|
485
|
+
if (params.videoId !== undefined) {
|
|
486
|
+
search.set('videoId', params.videoId);
|
|
487
|
+
}
|
|
488
|
+
if (params.rangeSeconds !== undefined) {
|
|
489
|
+
search.set('rangeSeconds', String(params.rangeSeconds));
|
|
490
|
+
}
|
|
491
|
+
const qs = search.toString();
|
|
492
|
+
const base = `${ADMIN_MONITORING_PATH}/traces`;
|
|
493
|
+
const path = qs ? `${base}?${qs}` : base;
|
|
494
|
+
return requestJson<MonitoringTraceSummariesResponse>(
|
|
495
|
+
this.chatBaseUrl,
|
|
496
|
+
path,
|
|
497
|
+
this.sessionToken,
|
|
498
|
+
{ signal: options.signal },
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Fetch the full segment documents for ONE X-Ray trace (for the waterfall
|
|
504
|
+
* view). `found=false` (with empty `segments`) when X-Ray has no such trace.
|
|
505
|
+
*/
|
|
506
|
+
async getTraceDetail(
|
|
507
|
+
xrayTraceId: string,
|
|
508
|
+
options: { signal?: AbortSignal } = {},
|
|
509
|
+
): Promise<MonitoringTraceDetailResponse> {
|
|
510
|
+
return requestJson<MonitoringTraceDetailResponse>(
|
|
511
|
+
this.chatBaseUrl,
|
|
512
|
+
`${ADMIN_MONITORING_PATH}/traces/${encodeURIComponent(xrayTraceId)}`,
|
|
513
|
+
this.sessionToken,
|
|
514
|
+
{ signal: options.signal },
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
}
|