@startsimpli/integrations 0.2.160 → 0.2.163
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 +6 -6
- package/src/__tests__/converse-brain.test.ts +84 -0
- package/src/__tests__/converse-client.test.ts +39 -0
- package/src/__tests__/converse-config.test.ts +9 -0
- package/src/__tests__/converse-extract.test.ts +19 -0
- package/src/__tests__/converse-page.test.tsx +104 -0
- package/src/__tests__/converse.test.ts +212 -0
- package/src/__tests__/corpus-planning.test.tsx +31 -0
- package/src/__tests__/measure/notes.ts +28 -0
- package/src/__tests__/measure/voice-notes.measure.test.ts +51 -0
- package/src/__tests__/question-evidence.test.ts +82 -0
- package/src/__tests__/question-voice.test.ts +18 -0
- package/src/components/index.ts +2 -0
- package/src/components/pages/ConversePage.tsx +184 -0
- package/src/components/pages/PlanningPage.tsx +14 -4
- package/src/components/pages/converse/silence.ts +20 -0
- package/src/components/pages/converse/useRecorder.ts +55 -0
- package/src/corpus/converse/brain.ts +84 -0
- package/src/corpus/converse/client.ts +64 -0
- package/src/corpus/converse/config.ts +21 -0
- package/src/corpus/converse/extract.ts +41 -0
- package/src/corpus/converse/handle.ts +147 -0
- package/src/corpus/converse/index.ts +16 -0
- package/src/corpus/converse/respond.ts +88 -0
- package/src/corpus/converse/store.ts +74 -0
- package/src/corpus/converse/transcript.ts +20 -0
- package/src/corpus/converse/types.ts +133 -0
- package/src/corpus/index.ts +1 -0
- package/src/corpus/llm.ts +3 -2
- package/src/corpus/questions/evidence.ts +65 -0
- package/src/corpus/questions/index.ts +4 -1
- package/src/corpus/questions/items.ts +21 -0
- package/src/corpus/questions/learn.ts +16 -0
- package/src/corpus/questions/sync.ts +7 -3
- package/src/corpus/questions/types.ts +3 -1
- package/src/corpus/questions/voice.ts +23 -4
- package/src/corpus/voice.ts +5 -2
- package/src/native/app/FoundryApp.tsx +2 -2
- package/src/native/app/__tests__/launch.test.ts +1 -1
- package/src/native/app/config/defaults.ts +3 -2
- package/src/native/app/config/types.ts +3 -1
- package/src/native/app/converseServices.ts +46 -0
- package/src/native/app/launch.ts +4 -0
- package/src/native/app/registry.tsx +8 -0
- package/src/native/app/services.ts +5 -2
- package/src/native/converse/ConverseScreen.tsx +70 -0
- package/src/native/converse/__tests__/controller.test.ts +182 -0
- package/src/native/converse/api.ts +81 -0
- package/src/native/converse/controller.ts +252 -0
- package/src/native/converse/fake.ts +34 -0
- package/src/native/converse/format.ts +25 -0
- package/src/native/converse/index.ts +8 -0
- package/src/native/converse/tab.tsx +14 -0
- package/src/native/index.ts +1 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { PlaybackSource } from '../briefing/audioPlayer';
|
|
2
|
+
|
|
3
|
+
type AuthorizedFetch = (url: string, init?: RequestInit) => Promise<Response>;
|
|
4
|
+
|
|
5
|
+
/** One turn the phone sends: a recording, or typed text (the fakes and the web page use text). */
|
|
6
|
+
export interface ConverseSend {
|
|
7
|
+
conversationId: string | null;
|
|
8
|
+
/** Fresh per press (8-64 of A-Za-z0-9_-); the same id again replays the turn instead of duplicating it (a retry after a dropped answer). */
|
|
9
|
+
turnId?: string;
|
|
10
|
+
recording?: { uri: string; durationMs: number };
|
|
11
|
+
text?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** What POST /corpus/converse answers. */
|
|
15
|
+
export interface ConverseReply {
|
|
16
|
+
conversationId: string;
|
|
17
|
+
/** What was heard, as words. */
|
|
18
|
+
transcript: string;
|
|
19
|
+
replyText: string;
|
|
20
|
+
/** Where the spoken reply is (relative to the tenant or absolute); null when the reply is text only. */
|
|
21
|
+
audioUrl: string | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ConverseApi {
|
|
25
|
+
send(request: ConverseSend): Promise<ConverseReply>;
|
|
26
|
+
/** Save the conversation. */
|
|
27
|
+
end(conversationId: string): Promise<void>;
|
|
28
|
+
/** The spoken reply, owner-checked (needs the bearer), or null when there is only text. */
|
|
29
|
+
replySource(reply: ConverseReply): Promise<PlaybackSource | null>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface Wire { conversation_id: string; transcript?: string; reply_text?: string; audio_url?: string | null }
|
|
33
|
+
|
|
34
|
+
async function refusal(res: Response, what: string): Promise<Error> {
|
|
35
|
+
const detail = await res.json().catch(() => ({})) as { detail?: string; code?: string };
|
|
36
|
+
return Object.assign(new Error(detail.detail || `${what} failed (${res.status})`), { status: res.status, ...(detail.code ? { code: detail.code } : {}) });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* POST /corpus/converse (multipart `audio` or `text`, `conversation_id` after the first turn, `timeZone`) and
|
|
41
|
+
* POST /corpus/converse/end. A refusal throws with the route's `status` and `code` (`transcription_failed` is retryable).
|
|
42
|
+
*/
|
|
43
|
+
export function createConverseApi(deps: {
|
|
44
|
+
tandemUrl: string;
|
|
45
|
+
authorizedFetch: AuthorizedFetch;
|
|
46
|
+
bearer: () => Promise<string | null>;
|
|
47
|
+
filePart?: (uri: string) => Blob;
|
|
48
|
+
createForm?: () => FormData;
|
|
49
|
+
timeZone?: string;
|
|
50
|
+
}): ConverseApi {
|
|
51
|
+
const base = deps.tandemUrl.replace(/\/+$/, '');
|
|
52
|
+
const timeZone = deps.timeZone ?? (() => { try { return Intl.DateTimeFormat().resolvedOptions().timeZone; } catch { return undefined; } })();
|
|
53
|
+
return {
|
|
54
|
+
async send(request) {
|
|
55
|
+
const body = (deps.createForm ?? (() => new FormData()))();
|
|
56
|
+
if (request.recording) {
|
|
57
|
+
if (deps.filePart) body.append('audio', deps.filePart(request.recording.uri), 'converse.m4a');
|
|
58
|
+
else body.append('audio', { uri: request.recording.uri, name: 'converse.m4a', type: 'audio/mp4' } as unknown as Blob);
|
|
59
|
+
}
|
|
60
|
+
if (request.text) body.append('text', request.text);
|
|
61
|
+
if (request.conversationId) body.append('conversation_id', request.conversationId);
|
|
62
|
+
if (request.turnId) body.append('turn_id', request.turnId);
|
|
63
|
+
if (timeZone) body.append('timeZone', timeZone);
|
|
64
|
+
const res = await deps.authorizedFetch(`${base}/corpus/converse`, { method: 'POST', body });
|
|
65
|
+
if (!res.ok) throw await refusal(res, 'POST /corpus/converse');
|
|
66
|
+
const w = (await res.json()) as Wire;
|
|
67
|
+
return { conversationId: w.conversation_id, transcript: w.transcript ?? '', replyText: w.reply_text ?? '', audioUrl: w.audio_url ?? null };
|
|
68
|
+
},
|
|
69
|
+
async end(conversationId) {
|
|
70
|
+
const res = await deps.authorizedFetch(`${base}/corpus/converse/end`, {
|
|
71
|
+
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ conversation_id: conversationId }),
|
|
72
|
+
});
|
|
73
|
+
if (!res.ok) throw await refusal(res, 'POST /corpus/converse/end');
|
|
74
|
+
},
|
|
75
|
+
async replySource(reply) {
|
|
76
|
+
if (!reply.audioUrl) return null;
|
|
77
|
+
const token = await deps.bearer();
|
|
78
|
+
return { uri: new URL(reply.audioUrl, `${base}/`).toString(), ...(token ? { headers: { authorization: `Bearer ${token}` } } : {}) };
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE CONVERSE SCREEN'S BRAIN (bd startsim-fh9xw): tap once to talk, tap again to send, the reply plays by itself.
|
|
3
|
+
*
|
|
4
|
+
* tap (idle) -> release the audio session (playback ends) -> mic on
|
|
5
|
+
* tap (recording) -> mic off -> POST the turn -> the transcript and reply appear -> the reply is spoken
|
|
6
|
+
* tap (speaking) -> the reply is cut off and the mic goes on (talking over it is the point)
|
|
7
|
+
* repeat -> say the last reply again
|
|
8
|
+
* end -> save the conversation and start fresh
|
|
9
|
+
*
|
|
10
|
+
* The recording is never a voice note: it does not enter the upload queue, and the file is deleted once the server has it
|
|
11
|
+
* (kept only while a retry is possible).
|
|
12
|
+
*/
|
|
13
|
+
import { isSilent } from '../recording/levels';
|
|
14
|
+
import type { Permission } from '../recording/permissions';
|
|
15
|
+
import type { RecorderAudio } from '../recording/recorder';
|
|
16
|
+
import type { PlaybackSource, PlayerState } from '../briefing/audioPlayer';
|
|
17
|
+
import type { ConverseApi, ConverseReply, ConverseSend } from './api';
|
|
18
|
+
|
|
19
|
+
export type ConversePhase = 'idle' | 'starting' | 'recording' | 'sending' | 'speaking' | 'saving' | 'error';
|
|
20
|
+
|
|
21
|
+
export interface ConverseTurn {
|
|
22
|
+
id: string;
|
|
23
|
+
role: 'you' | 'assistant';
|
|
24
|
+
text: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ConverseState {
|
|
28
|
+
phase: ConversePhase;
|
|
29
|
+
conversationId: string | null;
|
|
30
|
+
turns: ConverseTurn[];
|
|
31
|
+
/** The conversation was saved by End; cleared by the next turn. */
|
|
32
|
+
saved: boolean;
|
|
33
|
+
error: string | null;
|
|
34
|
+
/** A retry would send the same turn again. */
|
|
35
|
+
retryable: boolean;
|
|
36
|
+
/** A reply with audio exists to repeat. */
|
|
37
|
+
canRepeat: boolean;
|
|
38
|
+
/** Seconds the mic has been on. */
|
|
39
|
+
elapsedSec: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ConverseMessages {
|
|
43
|
+
micDenied: string;
|
|
44
|
+
tooShort: string;
|
|
45
|
+
notHeard: string;
|
|
46
|
+
failed: string;
|
|
47
|
+
noAudio: string;
|
|
48
|
+
saveFailed: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const DEFAULT_CONVERSE_MESSAGES: ConverseMessages = {
|
|
52
|
+
micDenied: 'Microphone access is off. Turn it on in Settings to talk.',
|
|
53
|
+
tooShort: 'That was too short. Tap, say your piece, tap again.',
|
|
54
|
+
notHeard: 'I could not make out any words. Try again.',
|
|
55
|
+
failed: 'Something went wrong sending that. Try again.',
|
|
56
|
+
noAudio: 'The microphone heard nothing. Check it is not covered.',
|
|
57
|
+
saveFailed: 'Could not save the conversation. Try End again.',
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/** Shorter than this cannot hold a sentence: a tap, not a turn. */
|
|
61
|
+
const MIN_TURN_MS = 500;
|
|
62
|
+
|
|
63
|
+
/** The slice of the audio player the conversation needs (the briefing player satisfies it). */
|
|
64
|
+
export interface ConverseSpeaker {
|
|
65
|
+
getState(): PlayerState;
|
|
66
|
+
subscribe(cb: (s: PlayerState) => void): () => void;
|
|
67
|
+
play(source: PlaybackSource): Promise<void>;
|
|
68
|
+
stop(): Promise<void>;
|
|
69
|
+
handoffToRecording(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const BLANK: ConverseState = { phase: 'idle', conversationId: null, turns: [], saved: false, error: null, retryable: false, canRepeat: false, elapsedSec: 0 };
|
|
73
|
+
|
|
74
|
+
export function createConverseController(deps: {
|
|
75
|
+
api: ConverseApi;
|
|
76
|
+
audio: RecorderAudio;
|
|
77
|
+
speaker: ConverseSpeaker;
|
|
78
|
+
removeFile(uri: string): Promise<void>;
|
|
79
|
+
ensurePermission(): Promise<Permission>;
|
|
80
|
+
newId(): string;
|
|
81
|
+
messages?: Partial<ConverseMessages>;
|
|
82
|
+
diag?: { log(event: string, fields?: Record<string, unknown>): void };
|
|
83
|
+
minTurnMs?: number;
|
|
84
|
+
/** Send a recording even when the microphone heard nothing (a simulator has no voice to hear). */
|
|
85
|
+
allowSilence?: boolean;
|
|
86
|
+
/** Counts the seconds the mic is on; default setInterval. */
|
|
87
|
+
tick?: (fn: () => void, ms: number) => () => void;
|
|
88
|
+
}) {
|
|
89
|
+
const messages = { ...DEFAULT_CONVERSE_MESSAGES, ...deps.messages };
|
|
90
|
+
let state: ConverseState = BLANK;
|
|
91
|
+
const listeners = new Set<() => void>();
|
|
92
|
+
const set = (patch: Partial<ConverseState>) => { state = { ...state, ...patch }; listeners.forEach((l) => l()); };
|
|
93
|
+
const log = (event: string, fields?: Record<string, unknown>) => deps.diag?.log(`converse.${event}`, fields);
|
|
94
|
+
|
|
95
|
+
let held = false;
|
|
96
|
+
/** The turn that failed in a retryable way: resent as it stands. */
|
|
97
|
+
let attempt: ConverseSend | null = null;
|
|
98
|
+
let lastReply: ConverseReply | null = null;
|
|
99
|
+
let stopTick: (() => void) | null = null;
|
|
100
|
+
/** Bumps on every new turn / end so a slow speaker from an older turn cannot flip the phase. */
|
|
101
|
+
let epoch = 0;
|
|
102
|
+
|
|
103
|
+
const fail = (message: string, retryable: boolean) => set({ phase: 'error', error: message, retryable });
|
|
104
|
+
const describe = (e: unknown) => {
|
|
105
|
+
const err = e as { status?: number; code?: string; message?: string };
|
|
106
|
+
if (err.code === 'transcription_failed') return { message: messages.notHeard, retryable: true };
|
|
107
|
+
if (err.status === 409 || err.status === 404 || err.status === 400) return { message: err.message || messages.failed, retryable: false };
|
|
108
|
+
return { message: messages.failed, retryable: true };
|
|
109
|
+
};
|
|
110
|
+
const discard = (uri: string) => deps.removeFile(uri).catch(() => undefined);
|
|
111
|
+
const endTick = () => { stopTick?.(); stopTick = null; };
|
|
112
|
+
|
|
113
|
+
async function speak(reply: ConverseReply, mine: number) {
|
|
114
|
+
let source: PlaybackSource | null = null;
|
|
115
|
+
try { source = await deps.api.replySource(reply); } catch (e) { log('source-failed', { error: e instanceof Error ? e.message : String(e) }); }
|
|
116
|
+
if (!source || mine !== epoch) { if (mine === epoch) set({ phase: 'idle' }); return; }
|
|
117
|
+
set({ phase: 'speaking' });
|
|
118
|
+
try {
|
|
119
|
+
await deps.speaker.play(source);
|
|
120
|
+
} catch (e) {
|
|
121
|
+
log('speak-failed', { error: e instanceof Error ? e.message : String(e) });
|
|
122
|
+
if (mine === epoch) set({ phase: 'idle' });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function dispatch(request: ConverseSend) {
|
|
127
|
+
const mine = ++epoch;
|
|
128
|
+
set({ phase: 'sending', error: null, retryable: false, saved: false });
|
|
129
|
+
let reply: ConverseReply;
|
|
130
|
+
try {
|
|
131
|
+
reply = await deps.api.send(request);
|
|
132
|
+
} catch (e) {
|
|
133
|
+
const { message, retryable } = describe(e);
|
|
134
|
+
log('failed', { status: (e as { status?: number }).status, code: (e as { code?: string }).code, error: (e as Error).message });
|
|
135
|
+
if (!retryable && request.recording) await discard(request.recording.uri);
|
|
136
|
+
attempt = retryable ? request : null;
|
|
137
|
+
return fail(message, retryable);
|
|
138
|
+
}
|
|
139
|
+
if (request.recording) await discard(request.recording.uri);
|
|
140
|
+
attempt = null;
|
|
141
|
+
lastReply = reply;
|
|
142
|
+
const id = deps.newId();
|
|
143
|
+
const turns: ConverseTurn[] = [...state.turns];
|
|
144
|
+
if (reply.transcript) turns.push({ id: `${id}-you`, role: 'you', text: reply.transcript });
|
|
145
|
+
if (reply.replyText) turns.push({ id: `${id}-assistant`, role: 'assistant', text: reply.replyText });
|
|
146
|
+
set({ conversationId: reply.conversationId, turns, canRepeat: Boolean(reply.audioUrl) });
|
|
147
|
+
await speak(reply, mine);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function finishRecording() {
|
|
151
|
+
endTick();
|
|
152
|
+
set({ phase: 'sending' });
|
|
153
|
+
let file;
|
|
154
|
+
try {
|
|
155
|
+
file = await deps.audio.stop();
|
|
156
|
+
} catch (e) {
|
|
157
|
+
log('stop-failed', { error: e instanceof Error ? e.message : String(e) });
|
|
158
|
+
return fail(messages.failed, false);
|
|
159
|
+
}
|
|
160
|
+
if (file.durationMs < (deps.minTurnMs ?? MIN_TURN_MS)) { await discard(file.uri); return fail(messages.tooShort, false); }
|
|
161
|
+
if (!deps.allowSilence && file.peakDb !== undefined && isSilent({ peakDb: file.peakDb, samples: 1 })) { await discard(file.uri); return fail(messages.noAudio, false); }
|
|
162
|
+
await dispatch({ conversationId: state.conversationId, turnId: deps.newId(), recording: { uri: file.uri, durationMs: file.durationMs } });
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async function startRecording() {
|
|
166
|
+
held = true;
|
|
167
|
+
epoch++;
|
|
168
|
+
set({ phase: 'starting', error: null, retryable: false, saved: false, elapsedSec: 0 });
|
|
169
|
+
try {
|
|
170
|
+
await deps.speaker.handoffToRecording();
|
|
171
|
+
if ((await deps.ensurePermission()) === 'denied') { held = false; return fail(messages.micDenied, false); }
|
|
172
|
+
await deps.audio.start({ onLevel: () => undefined });
|
|
173
|
+
} catch (e) {
|
|
174
|
+
held = false;
|
|
175
|
+
log('start-failed', { error: e instanceof Error ? e.message : String(e) });
|
|
176
|
+
return fail(messages.failed, false);
|
|
177
|
+
}
|
|
178
|
+
set({ phase: 'recording' });
|
|
179
|
+
const tick = deps.tick ?? ((fn: () => void, ms: number) => { const h = setInterval(fn, ms); return () => clearInterval(h); });
|
|
180
|
+
stopTick = tick(() => set({ elapsedSec: state.elapsedSec + 1 }), 1000);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// The reply ending (or being cut off) is not 'speaking' any more.
|
|
184
|
+
deps.speaker.subscribe((p) => {
|
|
185
|
+
if (state.phase === 'speaking' && (p.phase === 'ended' || p.phase === 'idle' || p.phase === 'failed')) set({ phase: 'idle' });
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
getState: () => state,
|
|
190
|
+
subscribe(listener: () => void) { listeners.add(listener); return () => { listeners.delete(listener); }; },
|
|
191
|
+
|
|
192
|
+
/** The one big button. */
|
|
193
|
+
async tap(): Promise<void> {
|
|
194
|
+
const p = state.phase;
|
|
195
|
+
if (p === 'starting' || p === 'sending' || p === 'saving') return;
|
|
196
|
+
if (p === 'recording') { held = false; await finishRecording(); return; }
|
|
197
|
+
// A new take replaces a failed one that was waiting for a retry.
|
|
198
|
+
if (attempt?.recording) await discard(attempt.recording.uri);
|
|
199
|
+
attempt = null;
|
|
200
|
+
await startRecording();
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
/** Say the last reply again. */
|
|
204
|
+
async repeat(): Promise<void> {
|
|
205
|
+
if (!lastReply || state.phase === 'recording' || state.phase === 'starting' || state.phase === 'sending' || state.phase === 'saving') return;
|
|
206
|
+
await speak(lastReply, ++epoch);
|
|
207
|
+
},
|
|
208
|
+
|
|
209
|
+
/** Send the same turn again after a failure. */
|
|
210
|
+
async retry(): Promise<void> {
|
|
211
|
+
if (state.phase !== 'error' || !state.retryable || !attempt) return;
|
|
212
|
+
await dispatch(attempt);
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
/** Drop a failed turn (and its recording). */
|
|
216
|
+
async dismissError(): Promise<void> {
|
|
217
|
+
if (attempt?.recording) await discard(attempt.recording.uri);
|
|
218
|
+
attempt = null;
|
|
219
|
+
set({ phase: 'idle', error: null, retryable: false });
|
|
220
|
+
},
|
|
221
|
+
|
|
222
|
+
/** Stop talking or listening without sending. */
|
|
223
|
+
async stop(): Promise<void> {
|
|
224
|
+
endTick();
|
|
225
|
+
epoch++;
|
|
226
|
+
if (state.phase === 'recording') { held = false; try { const f = await deps.audio.stop(); await discard(f.uri); } catch { /* nothing to drop */ } }
|
|
227
|
+
await deps.speaker.stop().catch(() => undefined);
|
|
228
|
+
set({ phase: 'idle' });
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
/** End saves the conversation, then the next tap starts a new one. */
|
|
232
|
+
async end(): Promise<void> {
|
|
233
|
+
if (state.phase === 'sending' || state.phase === 'saving' || state.phase === 'starting') return;
|
|
234
|
+
const id = state.conversationId;
|
|
235
|
+
if (state.phase === 'recording') await this.stop();
|
|
236
|
+
await deps.speaker.stop().catch(() => undefined);
|
|
237
|
+
epoch++;
|
|
238
|
+
if (!id) { set({ ...BLANK }); return; }
|
|
239
|
+
set({ phase: 'saving', error: null });
|
|
240
|
+
try {
|
|
241
|
+
await deps.api.end(id);
|
|
242
|
+
} catch (e) {
|
|
243
|
+
log('end-failed', { status: (e as { status?: number }).status, error: (e as Error).message });
|
|
244
|
+
return fail(messages.saveFailed, false);
|
|
245
|
+
}
|
|
246
|
+
lastReply = null; attempt = null;
|
|
247
|
+
set({ ...BLANK, saved: true });
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export type ConverseController = ReturnType<typeof createConverseController>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { PlaybackSource } from '../briefing/audioPlayer';
|
|
2
|
+
import type { ConverseApi, ConverseReply } from './api';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `-tgFakeConverse`: a scripted assistant so the screen can be driven on a simulator with no server, model or voice.
|
|
6
|
+
* Each turn "hears" the next line of `heard` and answers with the next of `replies` (both cycle). When the device has a
|
|
7
|
+
* bundled clip (`fixture`) the reply is spoken with it, so the play / repeat / session handoff is exercised for real.
|
|
8
|
+
*/
|
|
9
|
+
export function createFakeConverseApi(deps: { heard?: string[]; replies?: string[]; fixture?: PlaybackSource; delayMs?: number } = {}): ConverseApi & { ended: string[] } {
|
|
10
|
+
const heard = deps.heard?.length ? deps.heard : ['What is on my plate today?', 'Move the car service to Friday.', 'Thanks, that is all.'];
|
|
11
|
+
const replies = deps.replies?.length ? deps.replies : [
|
|
12
|
+
'You have three things today: send Dana the assignment, review the launch checklist, and book the car service. Want to take them in that order?',
|
|
13
|
+
'Done. The car service is now due Friday. Anything else for today?',
|
|
14
|
+
'Good. I will keep the plan as it stands.',
|
|
15
|
+
];
|
|
16
|
+
let turn = 0;
|
|
17
|
+
const ended: string[] = [];
|
|
18
|
+
return {
|
|
19
|
+
ended,
|
|
20
|
+
async send(request) {
|
|
21
|
+
if (deps.delayMs) await new Promise((r) => setTimeout(r, deps.delayMs));
|
|
22
|
+
const i = turn++;
|
|
23
|
+
const reply: ConverseReply = {
|
|
24
|
+
conversationId: request.conversationId ?? 'fake-conversation',
|
|
25
|
+
transcript: request.text ?? heard[i % heard.length],
|
|
26
|
+
replyText: replies[i % replies.length],
|
|
27
|
+
audioUrl: deps.fixture ? deps.fixture.uri : null,
|
|
28
|
+
};
|
|
29
|
+
return reply;
|
|
30
|
+
},
|
|
31
|
+
async end(conversationId) { ended.push(conversationId); },
|
|
32
|
+
async replySource() { return deps.fixture ?? null; },
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ConversePhase } from './controller';
|
|
2
|
+
|
|
3
|
+
/** The one line under the big button: what a tap does now. */
|
|
4
|
+
export function statusLine(phase: ConversePhase, elapsedSec: number): string {
|
|
5
|
+
switch (phase) {
|
|
6
|
+
case 'starting': return 'Getting the mic ready...';
|
|
7
|
+
case 'recording': return `Listening ${clock(elapsedSec)}. Tap to send`;
|
|
8
|
+
case 'sending': return 'Thinking...';
|
|
9
|
+
case 'speaking': return 'Speaking. Tap to talk over it';
|
|
10
|
+
case 'saving': return 'Saving the conversation...';
|
|
11
|
+
default: return 'Tap to talk';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const clock = (sec: number) => `${Math.floor(sec / 60)}:${String(sec % 60).padStart(2, '0')}`;
|
|
16
|
+
|
|
17
|
+
/** The big button's label. */
|
|
18
|
+
export function buttonLabel(phase: ConversePhase): string {
|
|
19
|
+
switch (phase) {
|
|
20
|
+
case 'recording': return 'Send';
|
|
21
|
+
case 'sending': case 'saving': case 'starting': return '...';
|
|
22
|
+
case 'speaking': return 'Talk';
|
|
23
|
+
default: return 'Talk';
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { createConverseApi } from './api';
|
|
2
|
+
export type { ConverseApi, ConverseReply, ConverseSend } from './api';
|
|
3
|
+
export { createFakeConverseApi } from './fake';
|
|
4
|
+
export { createConverseController, DEFAULT_CONVERSE_MESSAGES } from './controller';
|
|
5
|
+
export type { ConverseController, ConverseMessages, ConversePhase, ConverseSpeaker, ConverseState, ConverseTurn } from './controller';
|
|
6
|
+
export { ConverseScreen } from './ConverseScreen';
|
|
7
|
+
export { converseTab } from './tab';
|
|
8
|
+
export { statusLine } from './format';
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import type { FoundryTab } from '../components/FoundryTabsShell';
|
|
3
|
+
import { ConverseScreen } from './ConverseScreen';
|
|
4
|
+
import type { ConverseController } from './controller';
|
|
5
|
+
|
|
6
|
+
function ConverseTabBody({ controller }: { controller: ConverseController }) {
|
|
7
|
+
// Leaving the tab stops the mic and the voice; the conversation stays open until End.
|
|
8
|
+
useEffect(() => () => { void controller.stop(); }, [controller]);
|
|
9
|
+
return <ConverseScreen controller={controller} />;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function converseTab(controller: ConverseController, over: Partial<Pick<FoundryTab, 'key' | 'label'>> = {}): FoundryTab {
|
|
13
|
+
return { key: 'converse', label: 'Converse', ...over, render: () => <ConverseTabBody controller={controller} /> };
|
|
14
|
+
}
|
package/src/native/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ export { NotesShell, type NotesShellProps } from './components/NotesShell';
|
|
|
16
16
|
export { FoundryTabsShell, type FoundryTab, type FoundryTabsShellProps, type TabContext } from './components/FoundryTabsShell';
|
|
17
17
|
export * from './recording';
|
|
18
18
|
export * from './briefing';
|
|
19
|
+
export * from './converse';
|
|
19
20
|
export * from './app';
|
|
20
21
|
export * from './shell/navigator';
|
|
21
22
|
export * from './shell/overflow';
|