@startsimpli/integrations 0.2.164 → 0.2.166
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/package.json +4 -4
- package/src/__tests__/converse-browser-io.test.ts +105 -0
- package/src/__tests__/converse-meeting-vad.test.ts +61 -0
- package/src/__tests__/converse-meeting.test.ts +196 -0
- package/src/__tests__/converse-page.test.tsx +87 -72
- package/src/__tests__/converse.test.ts +23 -1
- package/src/components/pages/ConversePage.tsx +73 -118
- package/src/components/pages/converse/browserIO.ts +167 -0
- package/src/corpus/converse/handle.ts +1 -0
- package/src/corpus/converse/index.ts +6 -0
- package/src/corpus/converse/meeting-config.ts +35 -0
- package/src/corpus/converse/meeting.ts +183 -0
- package/src/corpus/converse/respond.ts +25 -1
- package/src/corpus/converse/types.ts +2 -0
- package/src/corpus/converse/vad.ts +48 -0
- package/src/native/app/converseServices.ts +28 -14
- package/src/native/app/registry.tsx +2 -2
- package/src/native/converse/ConverseScreen.tsx +38 -29
- package/src/native/converse/__tests__/meetingIO.test.ts +177 -0
- package/src/native/converse/api.ts +27 -47
- package/src/native/converse/fake.ts +41 -23
- package/src/native/converse/format.ts +17 -15
- package/src/native/converse/index.ts +5 -5
- package/src/native/converse/io.ts +122 -0
- package/src/native/converse/tab.tsx +4 -10
- package/src/components/pages/converse/useRecorder.ts +0 -55
- package/src/native/converse/__tests__/controller.test.ts +0 -189
- package/src/native/converse/controller.ts +0 -252
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@startsimpli/integrations",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.166",
|
|
4
4
|
"description": "Provider-agnostic types, API clients, hooks, and UI primitives for email + calendar integrations across StartSimpli apps, plus foundry customer email (subpath ./comms).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://startsimpli.com",
|
|
@@ -50,9 +50,9 @@
|
|
|
50
50
|
"react-native": ">=0.74.0",
|
|
51
51
|
"@startsimpli/ui-native": ">=0.1.12",
|
|
52
52
|
"react-native-safe-area-context": ">=4.0.0",
|
|
53
|
-
"@startsimpli/
|
|
53
|
+
"@startsimpli/ui": "^0.4.182",
|
|
54
54
|
"@startsimpli/auth": "^0.4.52",
|
|
55
|
-
"@startsimpli/
|
|
55
|
+
"@startsimpli/utils": "^0.4.18"
|
|
56
56
|
},
|
|
57
57
|
"peerDependenciesMeta": {
|
|
58
58
|
"react": {
|
|
@@ -99,8 +99,8 @@
|
|
|
99
99
|
"react-native": "0.85.3",
|
|
100
100
|
"react-native-safe-area-context": "~5.7.0",
|
|
101
101
|
"@startsimpli/llm": "0.4.31",
|
|
102
|
-
"@startsimpli/ui": "0.4.182",
|
|
103
102
|
"@startsimpli/ui-native": "0.1.14",
|
|
103
|
+
"@startsimpli/ui": "0.4.182",
|
|
104
104
|
"@startsimpli/auth": "0.4.52",
|
|
105
105
|
"@startsimpli/utils": "0.4.18"
|
|
106
106
|
},
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { createBrowserMeetingIO } from '../components/pages/converse/browserIO';
|
|
4
|
+
import { resolveMeetingConfig } from '../corpus/converse';
|
|
5
|
+
|
|
6
|
+
const config = resolveMeetingConfig({ silenceMs: 500, minUtteranceMs: 200, speechLevel: 0.1, speechStartMs: 100, idleRotateMs: 2000 });
|
|
7
|
+
|
|
8
|
+
function rig() {
|
|
9
|
+
let level = 0;
|
|
10
|
+
let now = 0;
|
|
11
|
+
const tick: { fn: (() => void) | null } = { fn: null };
|
|
12
|
+
const recorders: any[] = [];
|
|
13
|
+
const audio: any = { src: '', play: vi.fn(async () => undefined), pause: vi.fn(), onended: null, onerror: null };
|
|
14
|
+
const io = createBrowserMeetingIO({
|
|
15
|
+
mediaDevices: { getUserMedia: vi.fn(async () => ({ getTracks: () => [{ stop: vi.fn() }] }) as unknown as MediaStream) },
|
|
16
|
+
recorderFactory: () => {
|
|
17
|
+
const r: any = { mimeType: 'audio/mp4', state: 'inactive', start: vi.fn(() => { r.state = 'recording'; }),
|
|
18
|
+
stop: vi.fn(() => { r.ondataavailable?.({ data: new Blob([new Uint8Array(4)], { type: 'audio/mp4' }) }); r.state = 'inactive'; r.onstop?.(); }) };
|
|
19
|
+
recorders.push(r);
|
|
20
|
+
return r;
|
|
21
|
+
},
|
|
22
|
+
levelMeter: () => ({ read: () => level, close: vi.fn() }),
|
|
23
|
+
createAudio: () => audio,
|
|
24
|
+
now: () => now,
|
|
25
|
+
setTimer: (fn) => { tick.fn = fn; return 1 as any; },
|
|
26
|
+
clearTimer: () => { tick.fn = null; },
|
|
27
|
+
});
|
|
28
|
+
const advance = (ms: number, lvl: number) => { for (let t = 0; t < ms; t += 50) { now += 50; level = lvl; tick.fn?.(); } };
|
|
29
|
+
return { io, advance, recorders, audio };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
describe('browser meeting io', () => {
|
|
33
|
+
it('records an utterance and resolves when the speaker goes quiet', async () => {
|
|
34
|
+
const { io, advance, recorders } = rig();
|
|
35
|
+
await io.open();
|
|
36
|
+
const p = io.listen(new AbortController().signal, config);
|
|
37
|
+
advance(1000, 0.5);
|
|
38
|
+
advance(1000, 0);
|
|
39
|
+
const out = await p;
|
|
40
|
+
expect(out.audio?.size).toBeGreaterThan(0);
|
|
41
|
+
expect(recorders[0].start).toHaveBeenCalled();
|
|
42
|
+
});
|
|
43
|
+
it('drops a noise burst and keeps listening', async () => {
|
|
44
|
+
const { io, advance } = rig();
|
|
45
|
+
await io.open();
|
|
46
|
+
const ac = new AbortController();
|
|
47
|
+
const p = io.listen(ac.signal, config);
|
|
48
|
+
advance(150, 0.5);
|
|
49
|
+
advance(1500, 0);
|
|
50
|
+
ac.abort();
|
|
51
|
+
expect((await p).audio).toBeNull();
|
|
52
|
+
});
|
|
53
|
+
it('abort stops the recorder and resolves cancelled', async () => {
|
|
54
|
+
const { io, recorders } = rig();
|
|
55
|
+
await io.open();
|
|
56
|
+
const ac = new AbortController();
|
|
57
|
+
const p = io.listen(ac.signal, config);
|
|
58
|
+
ac.abort();
|
|
59
|
+
expect(await p).toEqual({ audio: null, cancelled: true });
|
|
60
|
+
expect(recorders[0].stop).toHaveBeenCalled();
|
|
61
|
+
});
|
|
62
|
+
it('speak plays inside the unlocked element and resolves when it ends; abort stops it', async () => {
|
|
63
|
+
const { io, audio } = rig();
|
|
64
|
+
await io.open();
|
|
65
|
+
const p = io.speak({ conversationId: 'c', turn: 1, replyText: 'r', audioUrl: 'blob:x' }, new AbortController().signal, config);
|
|
66
|
+
await Promise.resolve();
|
|
67
|
+
expect(audio.src).toBe('blob:x');
|
|
68
|
+
audio.onended();
|
|
69
|
+
await p;
|
|
70
|
+
const ac = new AbortController();
|
|
71
|
+
const q = io.speak({ conversationId: 'c', turn: 1, replyText: 'r', audioUrl: 'blob:y' }, ac.signal, config);
|
|
72
|
+
await Promise.resolve();
|
|
73
|
+
ac.abort();
|
|
74
|
+
await q;
|
|
75
|
+
expect(audio.pause).toHaveBeenCalled();
|
|
76
|
+
});
|
|
77
|
+
it('asks the browser to keep the screen on while the meeting is open, and lets go on close', async () => {
|
|
78
|
+
const release = vi.fn(async () => undefined);
|
|
79
|
+
const request = vi.fn(async () => ({ release }));
|
|
80
|
+
const io = createBrowserMeetingIO({
|
|
81
|
+
mediaDevices: { getUserMedia: vi.fn(async () => ({ getTracks: () => [] }) as unknown as MediaStream) },
|
|
82
|
+
levelMeter: () => ({ read: () => 0, close: vi.fn() }), createAudio: () => ({ play: vi.fn(async () => undefined), pause: vi.fn() }) as any,
|
|
83
|
+
wakeLock: { request },
|
|
84
|
+
});
|
|
85
|
+
await io.open();
|
|
86
|
+
expect(request).toHaveBeenCalledWith('screen');
|
|
87
|
+
io.close();
|
|
88
|
+
expect(release).toHaveBeenCalled();
|
|
89
|
+
});
|
|
90
|
+
it('a browser without wake lock, or one that refuses it, still opens', async () => {
|
|
91
|
+
const io = createBrowserMeetingIO({
|
|
92
|
+
mediaDevices: { getUserMedia: vi.fn(async () => ({ getTracks: () => [] }) as unknown as MediaStream) },
|
|
93
|
+
levelMeter: () => ({ read: () => 0, close: vi.fn() }), createAudio: () => ({ play: vi.fn(async () => undefined), pause: vi.fn() }) as any,
|
|
94
|
+
wakeLock: { request: vi.fn(async () => { throw new Error('denied'); }) },
|
|
95
|
+
});
|
|
96
|
+
await expect(io.open()).resolves.toBeUndefined();
|
|
97
|
+
});
|
|
98
|
+
it('open fails with a person-readable message when the microphone is blocked', async () => {
|
|
99
|
+
const io = createBrowserMeetingIO({
|
|
100
|
+
mediaDevices: { getUserMedia: vi.fn(async () => { throw Object.assign(new Error('x'), { name: 'NotAllowedError' }); }) },
|
|
101
|
+
levelMeter: () => ({ read: () => 0, close: vi.fn() }), createAudio: () => ({ play: vi.fn(async () => undefined) }) as any,
|
|
102
|
+
});
|
|
103
|
+
await expect(io.open()).rejects.toThrow(/blocked/);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createUtteranceDetector, resolveMeetingConfig } from '../corpus/converse';
|
|
3
|
+
|
|
4
|
+
const cfg = resolveMeetingConfig({ silenceMs: 1000, minUtteranceMs: 400, maxUtteranceMs: 5000, speechLevel: 0.1, speechStartMs: 100 });
|
|
5
|
+
/** Feed a level every 50 ms from t0; returns the events with the time they fired. */
|
|
6
|
+
function run(levels: Array<[level: number, ms: number]>, config = cfg) {
|
|
7
|
+
const d = createUtteranceDetector(config);
|
|
8
|
+
const events: Array<{ at: number; type: string; reason?: string }> = [];
|
|
9
|
+
let t = 0;
|
|
10
|
+
for (const [level, ms] of levels) for (let i = 0; i < ms / 50; i += 1, t += 50) {
|
|
11
|
+
const e = d.feed(level, t);
|
|
12
|
+
if (e) events.push({ at: t, ...e });
|
|
13
|
+
}
|
|
14
|
+
return events;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('resolveMeetingConfig', () => {
|
|
18
|
+
it('defaults, partial overrides, and ignores nonsense', () => {
|
|
19
|
+
const d = resolveMeetingConfig();
|
|
20
|
+
expect(d).toMatchObject({ silenceMs: 1200, minUtteranceMs: 400, maxUtteranceMs: 60000, interruptOnSpeech: false });
|
|
21
|
+
expect(resolveMeetingConfig({ silenceMs: 800, interruptOnSpeech: true })).toMatchObject({ silenceMs: 800, interruptOnSpeech: true, minUtteranceMs: 400 });
|
|
22
|
+
expect(resolveMeetingConfig({ silenceMs: -5, speechLevel: Number.NaN }).silenceMs).toBe(1200);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('end-of-speech detector', () => {
|
|
27
|
+
it('quiet room: nothing happens', () => {
|
|
28
|
+
expect(run([[0.01, 5000]])).toEqual([]);
|
|
29
|
+
});
|
|
30
|
+
it('speech then silence: starts after the start window, ends after silenceMs', () => {
|
|
31
|
+
const ev = run([[0.01, 500], [0.5, 1500], [0.01, 2000]]);
|
|
32
|
+
expect(ev.map((e) => e.type)).toEqual(['start', 'end']);
|
|
33
|
+
expect(ev[0].at).toBeGreaterThanOrEqual(500);
|
|
34
|
+
expect(ev[0].at).toBeLessThanOrEqual(700);
|
|
35
|
+
// last voice at ~1950, silence for 1000 ms -> end at ~2950
|
|
36
|
+
expect(ev[1].at).toBeGreaterThanOrEqual(2900);
|
|
37
|
+
expect(ev[1].at).toBeLessThanOrEqual(3050);
|
|
38
|
+
});
|
|
39
|
+
it('a short pause inside a sentence does not end it', () => {
|
|
40
|
+
const ev = run([[0.5, 1000], [0.01, 600], [0.5, 1000], [0.01, 2000]]);
|
|
41
|
+
expect(ev.map((e) => e.type)).toEqual(['start', 'end']);
|
|
42
|
+
});
|
|
43
|
+
it('a cough shorter than the start window is not speech', () => {
|
|
44
|
+
expect(run([[0.01, 500], [0.5, 50], [0.01, 3000]])).toEqual([]);
|
|
45
|
+
});
|
|
46
|
+
it('speech shorter than minUtteranceMs is discarded, not sent', () => {
|
|
47
|
+
const ev = run([[0.5, 250], [0.01, 2000]]);
|
|
48
|
+
expect(ev.map((e) => e.type)).toEqual(['start', 'discard']);
|
|
49
|
+
});
|
|
50
|
+
it('very long speech is cut at maxUtteranceMs and says why', () => {
|
|
51
|
+
const ev = run([[0.5, 8000]]);
|
|
52
|
+
// continued speech after the cut starts the next utterance
|
|
53
|
+
expect(ev.map((e) => e.type).slice(0, 2)).toEqual(['start', 'end']);
|
|
54
|
+
expect(ev[1].reason).toBe('max');
|
|
55
|
+
expect(ev[1].at).toBeLessThanOrEqual(5300);
|
|
56
|
+
});
|
|
57
|
+
it('listens again after an utterance', () => {
|
|
58
|
+
const ev = run([[0.5, 1000], [0.01, 1500], [0.5, 1000], [0.01, 1500]]);
|
|
59
|
+
expect(ev.map((e) => e.type)).toEqual(['start', 'end', 'start', 'end']);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { ConverseClientError, createMeetingController, type MeetingApi, type MeetingIO } from '../corpus/converse';
|
|
4
|
+
type ConverseClient = MeetingApi<Blob>;
|
|
5
|
+
|
|
6
|
+
const blob = () => new Blob([new Uint8Array([1, 2, 3])], { type: 'audio/mp4' });
|
|
7
|
+
const turnResult = (n: number, over: object = {}) => ({
|
|
8
|
+
conversation_id: 'conversation-1', turn_id: `t${n}`, turn: n, transcript: `said ${n}`, reply_text: `reply ${n}`,
|
|
9
|
+
audio_url: `/corpus/converse/conversation-1/turn/${n}/audio`, audio_reason: null, ...over,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
/** listen() hands out the queued utterances, then waits until cancelled. */
|
|
13
|
+
function rig(opts: { utterances?: Array<Blob | null>; turn?: ConverseClient['turn']; speak?: MeetingIO['speak'] } = {}) {
|
|
14
|
+
const queue = [...(opts.utterances ?? [blob(), blob()])];
|
|
15
|
+
const phases: string[] = [];
|
|
16
|
+
let n = 0;
|
|
17
|
+
const client: ConverseClient = {
|
|
18
|
+
turn: opts.turn ?? vi.fn(async () => turnResult(++n)),
|
|
19
|
+
end: vi.fn(async () => ({ saved: true as const, conversation_id: 'conversation-1', record_id: '9', turns: n, asks: [], decisions: [] })),
|
|
20
|
+
};
|
|
21
|
+
const speak = opts.speak ?? vi.fn(async () => undefined);
|
|
22
|
+
const io: MeetingIO = {
|
|
23
|
+
listen: vi.fn((signal: AbortSignal) => new Promise((resolve) => {
|
|
24
|
+
const next = queue.shift();
|
|
25
|
+
if (next !== undefined) { resolve(next ? { audio: next } : { audio: null }); return; }
|
|
26
|
+
signal.addEventListener('abort', () => resolve({ audio: null, cancelled: true }));
|
|
27
|
+
})),
|
|
28
|
+
speak,
|
|
29
|
+
};
|
|
30
|
+
const c = createMeetingController({ client, io, config: {}, onChange: (s) => phases.push(s.phase) });
|
|
31
|
+
return { c, client, io, speak, phases };
|
|
32
|
+
}
|
|
33
|
+
const settle = async (check: () => boolean) => { for (let i = 0; i < 200 && !check(); i += 1) await new Promise((r) => setTimeout(r, 5)); };
|
|
34
|
+
|
|
35
|
+
describe('meeting controller without a microphone', () => {
|
|
36
|
+
it('a listen that is cancelled by itself does not spin the loop', async () => {
|
|
37
|
+
const listen = vi.fn(async () => ({ audio: null, cancelled: true }));
|
|
38
|
+
const client: ConverseClient = { turn: vi.fn(async () => turnResult(1)) as never, end: vi.fn() as never };
|
|
39
|
+
const c = createMeetingController({ client, io: { listen, speak: vi.fn(async () => undefined) } });
|
|
40
|
+
c.start();
|
|
41
|
+
await new Promise((r) => setTimeout(r, 60));
|
|
42
|
+
expect(listen.mock.calls.length).toBeLessThan(3);
|
|
43
|
+
await c.sendText('hello there');
|
|
44
|
+
expect(c.state().lines.length).toBe(2);
|
|
45
|
+
await c.end();
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('starting again after End or a stop', () => {
|
|
50
|
+
it('a second start() is a NEW conversation: no old id, lines, saved or noAudio', async () => {
|
|
51
|
+
const { c, client } = rig({ utterances: [blob()] });
|
|
52
|
+
c.start();
|
|
53
|
+
await settle(() => c.state().lines.length >= 2);
|
|
54
|
+
await c.end();
|
|
55
|
+
expect(c.state().phase).toBe('ended');
|
|
56
|
+
expect(c.state().saved).not.toBeNull();
|
|
57
|
+
c.start();
|
|
58
|
+
expect(c.state()).toMatchObject({ lines: [], conversationId: null, saved: null, noAudio: null, error: null });
|
|
59
|
+
await c.sendText('fresh start');
|
|
60
|
+
expect((client.turn as any).mock.calls.at(-1)[0].conversationId).toBeUndefined();
|
|
61
|
+
expect(c.state().lines.map((l) => l.text).length).toBe(2);
|
|
62
|
+
await c.end();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('also after the meeting stopped on a rejected session', async () => {
|
|
66
|
+
const turn = vi.fn().mockRejectedValueOnce(new ConverseClientError('Your session is not valid', 401)).mockResolvedValue(turnResult(1));
|
|
67
|
+
const { c } = rig({ turn: turn as any, utterances: [blob()] });
|
|
68
|
+
c.start();
|
|
69
|
+
await settle(() => c.state().phase === 'stopped');
|
|
70
|
+
c.start();
|
|
71
|
+
expect(c.state()).toMatchObject({ lines: [], conversationId: null, error: null });
|
|
72
|
+
await c.end();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('start() while already running changes nothing', async () => {
|
|
76
|
+
const { c } = rig({ utterances: [blob()] });
|
|
77
|
+
c.start();
|
|
78
|
+
await settle(() => c.state().lines.length >= 2);
|
|
79
|
+
c.start();
|
|
80
|
+
expect(c.state().lines.length).toBe(2);
|
|
81
|
+
await c.end();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe('meeting controller', () => {
|
|
86
|
+
it('loops: listen -> thinking -> speaking -> listening, turn after turn, keeping the conversation id', async () => {
|
|
87
|
+
const { c, client, phases } = rig();
|
|
88
|
+
c.start();
|
|
89
|
+
await settle(() => c.state().lines.length >= 4);
|
|
90
|
+
expect(c.state().lines.map((l) => l.text)).toEqual(['said 1', 'reply 1', 'said 2', 'reply 2']);
|
|
91
|
+
expect((client.turn as any).mock.calls[0][0].conversationId).toBeUndefined();
|
|
92
|
+
expect((client.turn as any).mock.calls[1][0].conversationId).toBe('conversation-1');
|
|
93
|
+
expect(phases).toContain('thinking');
|
|
94
|
+
expect(phases).toContain('speaking');
|
|
95
|
+
expect(c.state().phase).toBe('listening');
|
|
96
|
+
await c.end();
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('End cancels listening, stops speech, saves, and reports the result', async () => {
|
|
100
|
+
const { c, client } = rig({ utterances: [blob()] });
|
|
101
|
+
c.start();
|
|
102
|
+
await settle(() => c.state().lines.length >= 2);
|
|
103
|
+
const saved = await c.end();
|
|
104
|
+
expect(saved?.saved).toBe(true);
|
|
105
|
+
expect(client.end).toHaveBeenCalledWith('conversation-1');
|
|
106
|
+
expect(c.state().phase).toBe('ended');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('End before anything was said saves nothing and does not call the server', async () => {
|
|
110
|
+
const { c, client } = rig({ utterances: [] });
|
|
111
|
+
c.start();
|
|
112
|
+
expect(await c.end()).toBeNull();
|
|
113
|
+
expect(client.end).not.toHaveBeenCalled();
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('a failed transcription does not end the meeting: it says so and listens again, same turn id on retry', async () => {
|
|
117
|
+
const turn = vi.fn()
|
|
118
|
+
.mockRejectedValueOnce(new ConverseClientError('could not make out', 502, 'transcription_failed'))
|
|
119
|
+
.mockResolvedValue(turnResult(1));
|
|
120
|
+
const { c } = rig({ turn: turn as any, utterances: [blob()] });
|
|
121
|
+
c.start();
|
|
122
|
+
await settle(() => c.state().lines.length >= 2);
|
|
123
|
+
expect(c.state().lines.map((l) => l.text)).toEqual(['said 1', 'reply 1']);
|
|
124
|
+
expect(turn.mock.calls[1][0].turnId).toBe(turn.mock.calls[0][0].turnId);
|
|
125
|
+
await c.end();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('a rejected session stops the meeting with the error instead of looping', async () => {
|
|
129
|
+
const turn = vi.fn().mockRejectedValue(new ConverseClientError('Your session is not valid', 401));
|
|
130
|
+
const { c } = rig({ turn: turn as any, utterances: [blob(), blob()] });
|
|
131
|
+
c.start();
|
|
132
|
+
await settle(() => c.state().phase === 'stopped');
|
|
133
|
+
expect(c.state().phase).toBe('stopped');
|
|
134
|
+
expect(c.state().error).toMatch(/session/i);
|
|
135
|
+
expect(turn).toHaveBeenCalledTimes(1);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('a reply with no audio says why and keeps listening', async () => {
|
|
139
|
+
const { c, speak } = rig({ utterances: [blob()], turn: vi.fn(async () => turnResult(1, { audio_url: null, audio_reason: 'speech is not configured' })) as any });
|
|
140
|
+
c.start();
|
|
141
|
+
await settle(() => c.state().lines.length >= 2);
|
|
142
|
+
expect(c.state().noAudio).toBe('speech is not configured');
|
|
143
|
+
expect(speak).not.toHaveBeenCalled();
|
|
144
|
+
expect(c.state().phase).toBe('listening');
|
|
145
|
+
await c.end();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('a playback failure does not end the meeting', async () => {
|
|
149
|
+
const { c } = rig({ utterances: [blob(), blob()], speak: vi.fn(async () => { throw new Error('blocked'); }) });
|
|
150
|
+
c.start();
|
|
151
|
+
await settle(() => c.state().lines.length >= 4);
|
|
152
|
+
expect(c.state().lines).toHaveLength(4);
|
|
153
|
+
expect(c.state().noAudio).toMatch(/blocked/);
|
|
154
|
+
await c.end();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('speak receives the whole reply object', async () => {
|
|
158
|
+
const { c, speak } = rig({ utterances: [blob()] });
|
|
159
|
+
c.start();
|
|
160
|
+
await settle(() => c.state().lines.length >= 2);
|
|
161
|
+
expect((speak as any).mock.calls[0][0]).toEqual({ conversationId: 'conversation-1', turn: 1, replyText: 'reply 1', audioUrl: '/corpus/converse/conversation-1/turn/1/audio' });
|
|
162
|
+
await c.end();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('subscribe/getState and retryable', async () => {
|
|
166
|
+
const turn = vi.fn().mockRejectedValue(new ConverseClientError('could not make out', 502, 'transcription_failed'));
|
|
167
|
+
const { c } = rig({ turn: turn as any, utterances: [blob()] });
|
|
168
|
+
const seen: string[] = [];
|
|
169
|
+
const off = c.subscribe((st) => seen.push(st.phase));
|
|
170
|
+
c.start();
|
|
171
|
+
await settle(() => !!c.getState().error);
|
|
172
|
+
expect(c.getState().retryable).toBe(true);
|
|
173
|
+
expect(seen).toContain('thinking');
|
|
174
|
+
off();
|
|
175
|
+
await c.end();
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('replay plays the last reply again', async () => {
|
|
179
|
+
const { c, speak } = rig({ utterances: [blob()] });
|
|
180
|
+
c.start();
|
|
181
|
+
await settle(() => c.state().lines.length >= 2);
|
|
182
|
+
const before = (speak as any).mock.calls.length;
|
|
183
|
+
await c.replay();
|
|
184
|
+
expect((speak as any).mock.calls.length).toBe(before + 1);
|
|
185
|
+
await c.end();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('typed text is a turn too', async () => {
|
|
189
|
+
const { c, client } = rig({ utterances: [] });
|
|
190
|
+
c.start();
|
|
191
|
+
await c.sendText('hello there');
|
|
192
|
+
expect((client.turn as any).mock.calls[0][0].text).toBe('hello there');
|
|
193
|
+
expect(c.state().lines.map((l) => l.text)).toEqual(['said 1', 'reply 1']);
|
|
194
|
+
await c.end();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
@@ -1,104 +1,119 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
/**
|
|
3
|
-
* The /converse page (bd startsim-
|
|
4
|
-
* and
|
|
3
|
+
* The /converse page as a meeting (bd startsim-1jqce): Start once, it listens by itself, answers, listens again;
|
|
4
|
+
* state and reply text are always visible; a missing reply sound is explained; End saves.
|
|
5
5
|
*/
|
|
6
6
|
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
|
7
7
|
import * as React from 'react';
|
|
8
8
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
9
9
|
|
|
10
10
|
import { ConversePage } from '../components/pages/ConversePage';
|
|
11
|
-
import {
|
|
11
|
+
import type { BrowserMeetingIO } from '../components/pages/converse/browserIO';
|
|
12
|
+
import type { ConverseClient } from '../corpus/converse';
|
|
12
13
|
|
|
13
14
|
afterEach(cleanup);
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
return rec;
|
|
21
|
-
}
|
|
22
|
-
const stream = { getTracks: () => [{ stop: vi.fn() }] } as unknown as MediaStream;
|
|
16
|
+
const turnResult = (n: number, over: object = {}) => ({
|
|
17
|
+
conversation_id: 'conversation-1', turn_id: `t${n}`, turn: n, transcript: `said ${n}`, reply_text: `reply ${n}`,
|
|
18
|
+
audio_url: `/corpus/converse/conversation-1/turn/${n}/audio`, audio_reason: null, ...over,
|
|
19
|
+
});
|
|
23
20
|
|
|
24
|
-
function setup(over:
|
|
21
|
+
function setup(over: { turn?: ConverseClient['turn']; openError?: Error; utterances?: number } = {}) {
|
|
22
|
+
let n = 0;
|
|
25
23
|
const client: ConverseClient = {
|
|
26
|
-
turn: vi.fn(async (
|
|
27
|
-
end: vi.fn(async () => ({ saved: true as const, conversation_id: 'conversation-1', record_id: '7', turns:
|
|
28
|
-
audioUrl: vi.fn(async () =>
|
|
29
|
-
|
|
24
|
+
turn: over.turn ?? vi.fn(async () => turnResult(++n)),
|
|
25
|
+
end: vi.fn(async () => ({ saved: true as const, conversation_id: 'conversation-1', record_id: '7', turns: n, asks: [], decisions: [] })),
|
|
26
|
+
audioUrl: vi.fn(async (p: string) => `blob:${p}`),
|
|
27
|
+
};
|
|
28
|
+
let left = over.utterances ?? 1;
|
|
29
|
+
const speak = vi.fn(async () => undefined);
|
|
30
|
+
const io: BrowserMeetingIO = {
|
|
31
|
+
open: vi.fn(async () => { if (over.openError) throw over.openError; }),
|
|
32
|
+
close: vi.fn(),
|
|
33
|
+
listen: vi.fn((signal: AbortSignal) => new Promise<{ audio: Blob | null; cancelled?: boolean }>((resolve) => {
|
|
34
|
+
if (left > 0) { left -= 1; resolve({ audio: new Blob([new Uint8Array([1])], { type: 'audio/mp4' }) }); return; }
|
|
35
|
+
signal.addEventListener('abort', () => resolve({ audio: null, cancelled: true }));
|
|
36
|
+
})),
|
|
37
|
+
speak,
|
|
30
38
|
};
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
render(<ConversePage client={client} mediaDevices={{ getUserMedia: vi.fn(async () => stream) }} recorderFactory={() => rec} createAudio={() => audio} />);
|
|
34
|
-
return { client, audio, rec };
|
|
39
|
+
render(<ConversePage client={client} createIO={() => io} />);
|
|
40
|
+
return { client, io, speak };
|
|
35
41
|
}
|
|
42
|
+
const status = () => screen.getByTestId('converse-status').textContent ?? '';
|
|
36
43
|
|
|
37
|
-
describe('ConversePage', () => {
|
|
38
|
-
it('
|
|
39
|
-
const { client,
|
|
40
|
-
fireEvent.click(screen.getByTestId('converse-
|
|
41
|
-
await waitFor(() => expect(
|
|
42
|
-
expect(
|
|
43
|
-
expect(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
await waitFor(() => expect(
|
|
47
|
-
expect(screen.getByText('what is due today')).toBeTruthy();
|
|
48
|
-
expect((client.turn as any).mock.calls[0][0].audio).toBeInstanceOf(Blob);
|
|
49
|
-
expect(audio.src).toBe('blob:reply');
|
|
50
|
-
expect(audio.play).toHaveBeenCalledTimes(2);
|
|
44
|
+
describe('ConversePage (meeting)', () => {
|
|
45
|
+
it('one Start tap, then it listens, answers, speaks and listens again by itself', async () => {
|
|
46
|
+
const { client, io, speak } = setup({ utterances: 2 });
|
|
47
|
+
fireEvent.click(screen.getByTestId('converse-start'));
|
|
48
|
+
await waitFor(() => expect(screen.getByText('reply 2')).toBeTruthy());
|
|
49
|
+
expect(io.open).toHaveBeenCalledTimes(1);
|
|
50
|
+
expect(client.turn).toHaveBeenCalledTimes(2);
|
|
51
|
+
expect(speak).toHaveBeenCalledTimes(2);
|
|
52
|
+
expect(screen.getByText('said 1')).toBeTruthy();
|
|
53
|
+
await waitFor(() => expect(status()).toMatch(/listening/i));
|
|
51
54
|
});
|
|
52
55
|
|
|
53
|
-
it('
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
fireEvent.click(screen.
|
|
57
|
-
await waitFor(() => expect(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
await waitFor(() => expect(audio.play.mock.calls.length).toBe(before + 1));
|
|
56
|
+
it('shows the state while it works', async () => {
|
|
57
|
+
let release: (v: unknown) => void = () => undefined;
|
|
58
|
+
setup({ turn: vi.fn(() => new Promise((r) => { release = r; })) as never });
|
|
59
|
+
fireEvent.click(screen.getByTestId('converse-start'));
|
|
60
|
+
await waitFor(() => expect(status()).toMatch(/thinking/i));
|
|
61
|
+
release(turnResult(1));
|
|
62
|
+
await waitFor(() => expect(screen.getByText('reply 1')).toBeTruthy());
|
|
61
63
|
});
|
|
62
64
|
|
|
63
|
-
it('
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
fireEvent.click(screen.getByText('Send'));
|
|
68
|
-
await waitFor(() => expect(screen.getByText(text)).toBeTruthy());
|
|
69
|
-
}
|
|
70
|
-
expect((client.turn as any).mock.calls[0][0].conversationId).toBeUndefined();
|
|
71
|
-
expect((client.turn as any).mock.calls[1][0].conversationId).toBe('conversation-1');
|
|
65
|
+
it('a reply with no sound says so and why', async () => {
|
|
66
|
+
setup({ turn: vi.fn(async () => turnResult(1, { audio_url: null, audio_reason: 'speech is not configured' })) as never });
|
|
67
|
+
fireEvent.click(screen.getByTestId('converse-start'));
|
|
68
|
+
await waitFor(() => expect(status()).toMatch(/no spoken reply.*speech is not configured/i));
|
|
72
69
|
});
|
|
73
70
|
|
|
74
|
-
it('
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
71
|
+
it('End and save saves, clears the screen and says so', async () => {
|
|
72
|
+
const { client, io } = setup();
|
|
73
|
+
fireEvent.click(screen.getByTestId('converse-start'));
|
|
74
|
+
await waitFor(() => expect(screen.getByText('reply 1')).toBeTruthy());
|
|
75
|
+
fireEvent.click(screen.getByTestId('converse-end'));
|
|
76
|
+
await waitFor(() => expect(screen.getByTestId('converse-saved').textContent).toMatch(/Saved 1 turn/));
|
|
77
|
+
expect(client.end).toHaveBeenCalledWith('conversation-1');
|
|
78
|
+
expect(io.close).toHaveBeenCalled();
|
|
79
|
+
expect(screen.getByTestId('converse-start')).toBeTruthy();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('Play reply plays the last reply again', async () => {
|
|
83
|
+
const { speak } = setup();
|
|
84
|
+
fireEvent.click(screen.getByTestId('converse-start'));
|
|
85
|
+
await waitFor(() => expect(screen.getByText('reply 1')).toBeTruthy());
|
|
86
|
+
const before = speak.mock.calls.length;
|
|
87
|
+
fireEvent.click(screen.getByText('Play reply'));
|
|
88
|
+
await waitFor(() => expect(speak.mock.calls.length).toBe(before + 1));
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('typing works without the microphone path', async () => {
|
|
92
|
+
const { client } = setup({ utterances: 0 });
|
|
93
|
+
fireEvent.click(screen.getByTestId('converse-start'));
|
|
94
|
+
await waitFor(() => expect(status()).toMatch(/listening/i));
|
|
79
95
|
fireEvent.change(screen.getByLabelText('Type a message'), { target: { value: 'hello there' } });
|
|
80
96
|
fireEvent.click(screen.getByText('Send'));
|
|
81
|
-
await waitFor(() => expect(screen.
|
|
82
|
-
|
|
83
|
-
await waitFor(() => expect(screen.getByText('Fine.')).toBeTruthy());
|
|
84
|
-
expect(turn.mock.calls[1][0].turnId).toBe(turn.mock.calls[0][0].turnId);
|
|
97
|
+
await waitFor(() => expect(screen.getByText('reply 1')).toBeTruthy());
|
|
98
|
+
expect((client.turn as any).mock.calls[0][0].text).toBe('hello there');
|
|
85
99
|
});
|
|
86
100
|
|
|
87
|
-
it('
|
|
88
|
-
const { client } = setup();
|
|
89
|
-
fireEvent.change(screen.getByLabelText('Type a message'), { target: { value: '
|
|
101
|
+
it('typing still works when the microphone is blocked, and says why there is no listening', async () => {
|
|
102
|
+
const { client } = setup({ openError: new Error('Microphone access was blocked. Allow it in the browser and try again.'), utterances: 0 });
|
|
103
|
+
fireEvent.change(screen.getByLabelText('Type a message'), { target: { value: 'hello there' } });
|
|
90
104
|
fireEvent.click(screen.getByText('Send'));
|
|
91
|
-
await waitFor(() => expect(screen.getByText('
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
expect(
|
|
95
|
-
expect(
|
|
105
|
+
await waitFor(() => expect(screen.getByText('reply 1')).toBeTruthy());
|
|
106
|
+
expect((client.turn as any).mock.calls[0][0].text).toBe('hello there');
|
|
107
|
+
expect(screen.getByRole('alert').textContent).toMatch(/blocked/);
|
|
108
|
+
expect(status()).not.toMatch(/speak whenever/i);
|
|
109
|
+
expect(status()).toMatch(/type/i);
|
|
96
110
|
});
|
|
97
111
|
|
|
98
|
-
it('a blocked microphone says so', async () => {
|
|
99
|
-
const client = {
|
|
100
|
-
|
|
101
|
-
fireEvent.click(screen.getByTestId('converse-talk'));
|
|
112
|
+
it('a blocked microphone says so and nothing starts', async () => {
|
|
113
|
+
const { client } = setup({ openError: new Error('Microphone access was blocked. Allow it in the browser and try again.') });
|
|
114
|
+
fireEvent.click(screen.getByTestId('converse-start'));
|
|
102
115
|
await waitFor(() => expect(screen.getByRole('alert').textContent).toMatch(/blocked/));
|
|
116
|
+
expect(client.turn).not.toHaveBeenCalled();
|
|
117
|
+
expect(screen.getByTestId('converse-start')).toBeTruthy();
|
|
103
118
|
});
|
|
104
119
|
});
|
|
@@ -137,6 +137,7 @@ describe('an audio turn', () => {
|
|
|
137
137
|
const out = await handleConverseTurn(deps, { text: 'hello' });
|
|
138
138
|
expect(out.reply_text).toBe('You said: hello');
|
|
139
139
|
expect(out.audio_url).toBeNull();
|
|
140
|
+
expect(out.audio_reason).toBe('tts down');
|
|
140
141
|
});
|
|
141
142
|
|
|
142
143
|
it('never loses the turn when the brain fails: says so and keeps going', async () => {
|
|
@@ -158,6 +159,27 @@ describe('audio route and End', () => {
|
|
|
158
159
|
expect((await converseAudioResponse(a.conversation_id, 7, 'reply', deps)).status).toBe(404);
|
|
159
160
|
});
|
|
160
161
|
|
|
162
|
+
it('answers a Range request with 206 and the slice, and advertises Accept-Ranges', async () => {
|
|
163
|
+
const { deps } = setup();
|
|
164
|
+
const a = await handleConverseTurn(deps, { text: 'hello' });
|
|
165
|
+
const range = (r: string) => converseAudioResponse(a.conversation_id, 1, 'reply', deps, r);
|
|
166
|
+
const full = await converseAudioResponse(a.conversation_id, 1, 'reply', deps);
|
|
167
|
+
expect(full.headers.get('accept-ranges')).toBe('bytes');
|
|
168
|
+
const part = await range('bytes=1-2');
|
|
169
|
+
expect(part.status).toBe(206);
|
|
170
|
+
expect(part.headers.get('content-range')).toBe('bytes 1-2/3');
|
|
171
|
+
expect([...new Uint8Array(await part.arrayBuffer())]).toEqual([2, 3]);
|
|
172
|
+
const open = await range('bytes=1-');
|
|
173
|
+
expect(open.status).toBe(206);
|
|
174
|
+
expect(open.headers.get('content-range')).toBe('bytes 1-2/3');
|
|
175
|
+
const tail = await range('bytes=-1');
|
|
176
|
+
expect(tail.headers.get('content-range')).toBe('bytes 2-2/3');
|
|
177
|
+
const bad = await range('bytes=9-12');
|
|
178
|
+
expect(bad.status).toBe(416);
|
|
179
|
+
expect(bad.headers.get('content-range')).toBe('bytes */3');
|
|
180
|
+
expect((await range('garbage')).status).toBe(200);
|
|
181
|
+
});
|
|
182
|
+
|
|
161
183
|
it('End marks it finished, writes the transcript as the document body, and is idempotent', async () => {
|
|
162
184
|
const { t, deps } = setup();
|
|
163
185
|
const a = await handleConverseTurn(deps, { text: 'plan friday' });
|
|
@@ -181,7 +203,7 @@ describe('Response wrappers', () => {
|
|
|
181
203
|
form.set('timeZone', 'America/New_York');
|
|
182
204
|
const res = await converseResponse(new Request('http://x/corpus/converse', { method: 'POST', body: form }), deps);
|
|
183
205
|
expect(res.status).toBe(200);
|
|
184
|
-
expect(Object.keys(await res.json()).sort()).toEqual(['audio_url', 'conversation_id', 'reply_text', 'transcript', 'turn', 'turn_id'].sort());
|
|
206
|
+
expect(Object.keys(await res.json()).sort()).toEqual(['audio_reason', 'audio_url', 'conversation_id', 'reply_text', 'transcript', 'turn', 'turn_id'].sort());
|
|
185
207
|
});
|
|
186
208
|
|
|
187
209
|
it('multipart audio turn and JSON body both work; errors carry {detail, code}', async () => {
|