@startsimpli/integrations 0.2.50 → 0.2.52
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 +3 -3
- package/src/__tests__/voice-notes-page.test.tsx +103 -0
- package/src/__tests__/voice.test.ts +211 -0
- package/src/comms/__tests__/announcement-composer.test.tsx +56 -0
- package/src/comms/__tests__/customer-emails-page.test.tsx +48 -4
- package/src/comms/__tests__/email-viewer.test.tsx +233 -0
- package/src/comms/components/AnnouncementComposer.tsx +51 -1
- package/src/comms/components/DraftReview.tsx +10 -9
- package/src/comms/components/EmailPreview.tsx +155 -12
- package/src/comms/components/EmailPreviewPage.tsx +161 -0
- package/src/comms/components/SequenceSettings.tsx +3 -7
- package/src/comms/components/index.ts +1 -0
- package/src/comms/components/pages/CustomerEmailsPage.tsx +16 -1
- package/src/comms/hooks/index.ts +27 -0
- package/src/components/index.ts +3 -0
- package/src/components/pages/VoiceNotesPage.tsx +227 -0
- package/src/corpus/index.ts +28 -0
- package/src/corpus/llm.ts +91 -0
- package/src/corpus/voice.ts +354 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* VOICE NOTES (bd startsim-f3wtn) — a recording becomes an ordinary
|
|
3
|
+
* `source_document`, filed under a focus (a project).
|
|
4
|
+
*
|
|
5
|
+
* A VOICE NOTE IS NOT A NEW ENTITY TYPE. It is a source_document whose source is
|
|
6
|
+
* `voice`, with the transcript in `body` and a generated `summary` beside it —
|
|
7
|
+
* the two-field split the canonical schema documents. That is what makes
|
|
8
|
+
* Library, Ask and the Tracker sweep pick it up with no extra code, and why a
|
|
9
|
+
* second voice-only list would have been the wrong design.
|
|
10
|
+
*
|
|
11
|
+
* THE PROJECT IS AN EDGE, NOT A FIELD. The model proposes a focus; the answer is
|
|
12
|
+
* written as an `about_focus` relationship, exactly how a commitment is filed, and
|
|
13
|
+
* re-filing replaces that edge. Storing `focus_id` on the document as well would
|
|
14
|
+
* be a second copy of the same fact that disagrees the first time someone
|
|
15
|
+
* changes it in Planning.
|
|
16
|
+
*
|
|
17
|
+
* THE MODEL ONLY EVER CHOOSES FROM A LIST IT WAS GIVEN. Focus ids are echoed
|
|
18
|
+
* back and validated against that list; an id it invented, or `null`, files
|
|
19
|
+
* nothing and leaves the note unfiled — visibly, rather than under a guess.
|
|
20
|
+
*
|
|
21
|
+
* PURE + INJECTED. Nothing here imports a provider or a bucket client; the
|
|
22
|
+
* transcriber, analyzer and audio store are seams the server route supplies.
|
|
23
|
+
*/
|
|
24
|
+
import type { CorpusHttp } from './client';
|
|
25
|
+
import type { TenantEntity, TenantRelationship } from './entities';
|
|
26
|
+
import type { CorpusScope } from './types';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A document is ABOUT a focus — its own edge, not `in_focus`. `in_focus` is
|
|
30
|
+
* declared commitment -> focus only (a relationship definition has one source
|
|
31
|
+
* type), and the Planning counts walk it expecting items. Reusing it for
|
|
32
|
+
* documents would have been rejected by the tenant (400 "source must be a
|
|
33
|
+
* 'commitment'"); widening it would let documents leak into item counts.
|
|
34
|
+
*/
|
|
35
|
+
export const ABOUT_FOCUS = 'about_focus';
|
|
36
|
+
|
|
37
|
+
export const VOICE_SOURCE = 'voice';
|
|
38
|
+
export const VOICE_SOURCE_LABEL = 'Voice note';
|
|
39
|
+
export type VoiceStatus = 'processing' | 'ready' | 'failed';
|
|
40
|
+
|
|
41
|
+
export interface FocusChoice {
|
|
42
|
+
id: string;
|
|
43
|
+
title: string;
|
|
44
|
+
detail?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface VoiceAnalysis {
|
|
48
|
+
title: string;
|
|
49
|
+
summary: string;
|
|
50
|
+
/** One of the offered focus ids, or null when none fit. */
|
|
51
|
+
focusId: string | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface VoiceAnalyzer {
|
|
55
|
+
analyze(input: { transcript: string; focuses: FocusChoice[] }): Promise<VoiceAnalysis>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface VoiceTranscriber {
|
|
59
|
+
transcribe(input: { audio: Uint8Array; mimeType: string; focusTitles: string[] }): Promise<string>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface VoiceAudioStore {
|
|
63
|
+
/** Persist the recording; returns an opaque reference kept on the document. */
|
|
64
|
+
put(input: { noteId: string; audio: Uint8Array; mimeType: string }): Promise<string>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface VoiceHttp extends CorpusHttp {
|
|
68
|
+
post<T>(path: string, body?: unknown): Promise<T>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
// the record
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
export function voiceExternalId(noteId: string): string {
|
|
76
|
+
return `voice:${noteId}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function defaultVoiceTitle(at: Date): string {
|
|
80
|
+
return `Voice note — ${at.toLocaleString('en-US', { month: 'short', day: 'numeric', hour: 'numeric', minute: '2-digit' })}`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The tenant upsert payload. `patch` keys shallow-merge, so a later call never drops earlier ones. */
|
|
84
|
+
export function voiceToEntity(input: {
|
|
85
|
+
noteId: string;
|
|
86
|
+
title: string;
|
|
87
|
+
status: VoiceStatus;
|
|
88
|
+
occurredAt: string;
|
|
89
|
+
body?: string;
|
|
90
|
+
summary?: string;
|
|
91
|
+
audioRef?: string;
|
|
92
|
+
audioMime?: string;
|
|
93
|
+
durationMs?: number;
|
|
94
|
+
error?: string;
|
|
95
|
+
}): { entity_type: string; external_id: string; name: string; data: Record<string, unknown> } {
|
|
96
|
+
return {
|
|
97
|
+
entity_type: 'source_document',
|
|
98
|
+
external_id: voiceExternalId(input.noteId),
|
|
99
|
+
name: input.title.slice(0, 200),
|
|
100
|
+
data: {
|
|
101
|
+
title: input.title,
|
|
102
|
+
source: VOICE_SOURCE,
|
|
103
|
+
source_label: VOICE_SOURCE_LABEL,
|
|
104
|
+
source_ref: input.noteId,
|
|
105
|
+
occurred_at: input.occurredAt,
|
|
106
|
+
voice_status: input.status,
|
|
107
|
+
...(input.body !== undefined ? { body: input.body } : {}),
|
|
108
|
+
...(input.summary !== undefined ? { summary: input.summary } : {}),
|
|
109
|
+
...(input.audioRef ? { audio_ref: input.audioRef } : {}),
|
|
110
|
+
...(input.audioMime ? { audio_mime: input.audioMime } : {}),
|
|
111
|
+
...(input.durationMs !== undefined ? { duration_ms: input.durationMs } : {}),
|
|
112
|
+
...(input.error !== undefined ? { voice_error: input.error } : {}),
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
// the pipeline
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
|
|
121
|
+
export interface VoiceDeps {
|
|
122
|
+
http: VoiceHttp;
|
|
123
|
+
transcriber: VoiceTranscriber;
|
|
124
|
+
analyzer: VoiceAnalyzer;
|
|
125
|
+
store: VoiceAudioStore;
|
|
126
|
+
listFocuses(): Promise<FocusChoice[]>;
|
|
127
|
+
now?: () => Date;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface VoiceSubmission {
|
|
131
|
+
noteId: string;
|
|
132
|
+
audio: Uint8Array;
|
|
133
|
+
mimeType: string;
|
|
134
|
+
durationMs?: number;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface StartedVoiceNote {
|
|
138
|
+
id: string;
|
|
139
|
+
noteId: string;
|
|
140
|
+
audioRef: string;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Step 1 — fast: keep the audio, create the row as `processing`. Safe to await inside a request. */
|
|
144
|
+
export async function startVoiceNote(
|
|
145
|
+
submission: VoiceSubmission,
|
|
146
|
+
deps: VoiceDeps,
|
|
147
|
+
): Promise<StartedVoiceNote> {
|
|
148
|
+
if (!submission.audio?.byteLength) throw new Error('There is no audio to save.');
|
|
149
|
+
const at = (deps.now ?? (() => new Date()))();
|
|
150
|
+
const audioRef = await deps.store.put({
|
|
151
|
+
noteId: submission.noteId,
|
|
152
|
+
audio: submission.audio,
|
|
153
|
+
mimeType: submission.mimeType,
|
|
154
|
+
});
|
|
155
|
+
const row = await deps.http.post<TenantEntity>(
|
|
156
|
+
'api/v1/entities/upsert',
|
|
157
|
+
voiceToEntity({
|
|
158
|
+
noteId: submission.noteId,
|
|
159
|
+
title: defaultVoiceTitle(at),
|
|
160
|
+
status: 'processing',
|
|
161
|
+
occurredAt: at.toISOString(),
|
|
162
|
+
audioRef,
|
|
163
|
+
audioMime: submission.mimeType,
|
|
164
|
+
durationMs: submission.durationMs,
|
|
165
|
+
}),
|
|
166
|
+
);
|
|
167
|
+
return { id: String(row.id), noteId: submission.noteId, audioRef };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface FinishedVoiceNote {
|
|
171
|
+
id: string;
|
|
172
|
+
focusId: string | null;
|
|
173
|
+
title: string;
|
|
174
|
+
transcript: string;
|
|
175
|
+
/** Set when the project edge was refused; the note itself is still `ready`. */
|
|
176
|
+
filingError?: string;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Step 2 — slow, run after the response has gone: transcribe, then ONE model
|
|
181
|
+
* call for title + summary + project, then file. A failure is written to the row
|
|
182
|
+
* (status `failed` + the reason) and rethrown, so it is never a silent
|
|
183
|
+
* "processing" forever.
|
|
184
|
+
*/
|
|
185
|
+
export async function finishVoiceNote(
|
|
186
|
+
started: StartedVoiceNote,
|
|
187
|
+
submission: VoiceSubmission,
|
|
188
|
+
deps: VoiceDeps,
|
|
189
|
+
): Promise<FinishedVoiceNote> {
|
|
190
|
+
const at = (deps.now ?? (() => new Date()))();
|
|
191
|
+
const fail = async (error: unknown): Promise<never> => {
|
|
192
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
193
|
+
await deps.http
|
|
194
|
+
.post('api/v1/entities/upsert', {
|
|
195
|
+
...voiceToEntity({
|
|
196
|
+
noteId: started.noteId, title: defaultVoiceTitle(at), status: 'failed',
|
|
197
|
+
occurredAt: at.toISOString(), error: message.slice(0, 500),
|
|
198
|
+
}),
|
|
199
|
+
})
|
|
200
|
+
.catch(() => undefined);
|
|
201
|
+
throw error;
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
const focuses = await deps.listFocuses().catch(() => [] as FocusChoice[]);
|
|
206
|
+
const transcript = (await deps.transcriber.transcribe({
|
|
207
|
+
audio: submission.audio,
|
|
208
|
+
mimeType: submission.mimeType,
|
|
209
|
+
focusTitles: focuses.map((f) => f.title),
|
|
210
|
+
})).trim();
|
|
211
|
+
if (!transcript) throw new Error('No speech was detected in the recording.');
|
|
212
|
+
|
|
213
|
+
const analysis = await deps.analyzer.analyze({ transcript, focuses });
|
|
214
|
+
const title = analysis.title || defaultVoiceTitle(at);
|
|
215
|
+
|
|
216
|
+
await deps.http.post('api/v1/entities/upsert', voiceToEntity({
|
|
217
|
+
noteId: started.noteId, title, status: 'ready', occurredAt: at.toISOString(),
|
|
218
|
+
body: transcript, summary: analysis.summary,
|
|
219
|
+
}));
|
|
220
|
+
// Filing is best-effort AFTER the note is already `ready`: a refused edge
|
|
221
|
+
// must leave a readable, unfiled note — not overwrite it as `failed` and
|
|
222
|
+
// throw away the transcript and summary that were just produced.
|
|
223
|
+
let filingError: string | undefined;
|
|
224
|
+
let filed = analysis.focusId;
|
|
225
|
+
if (analysis.focusId) {
|
|
226
|
+
try {
|
|
227
|
+
await deps.http.post('api/v1/relationships', {
|
|
228
|
+
rel_type: ABOUT_FOCUS, source: started.id, target: analysis.focusId,
|
|
229
|
+
});
|
|
230
|
+
} catch (error) {
|
|
231
|
+
filed = null;
|
|
232
|
+
filingError = error instanceof Error ? error.message : String(error);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return { id: started.id, focusId: filed, title, transcript, ...(filingError ? { filingError } : {}) };
|
|
236
|
+
} catch (error) {
|
|
237
|
+
return fail(error);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ---------------------------------------------------------------------------
|
|
242
|
+
// the page's client
|
|
243
|
+
// ---------------------------------------------------------------------------
|
|
244
|
+
|
|
245
|
+
export interface VoiceNote {
|
|
246
|
+
id: string;
|
|
247
|
+
title: string;
|
|
248
|
+
summary: string;
|
|
249
|
+
transcript: string;
|
|
250
|
+
status: VoiceStatus;
|
|
251
|
+
error?: string;
|
|
252
|
+
occurredAt: string | null;
|
|
253
|
+
durationMs?: number;
|
|
254
|
+
focusId: string | null;
|
|
255
|
+
audioRef?: string;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export interface VoiceFocus { id: string; title: string }
|
|
259
|
+
|
|
260
|
+
export interface VoiceNotesApi {
|
|
261
|
+
/** Upload a finished recording. Resolves once it is saved; processing continues server-side. */
|
|
262
|
+
submit(audio: Blob, meta: { durationMs: number }): Promise<{ id: string }>;
|
|
263
|
+
list(): Promise<VoiceNote[]>;
|
|
264
|
+
listFocuses(): Promise<VoiceFocus[]>;
|
|
265
|
+
/** Move a note to another project, or `null` to unfile it. */
|
|
266
|
+
file(noteId: string, focusId: string | null): Promise<void>;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function text(value: unknown): string {
|
|
270
|
+
return typeof value === 'string' ? value : '';
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export function toVoiceNote(entity: TenantEntity, focusId: string | null): VoiceNote {
|
|
274
|
+
const data = (entity.data ?? {}) as Record<string, unknown>;
|
|
275
|
+
// The @startsimpli/api client camel-cases keys inside `data` too, so every
|
|
276
|
+
// snake_case field is read under both spellings.
|
|
277
|
+
const pick = (snake: string, camel: string): unknown => data[snake] ?? data[camel];
|
|
278
|
+
const status = text(pick('voice_status', 'voiceStatus'));
|
|
279
|
+
const error = text(pick('voice_error', 'voiceError'));
|
|
280
|
+
const durationMs = pick('duration_ms', 'durationMs');
|
|
281
|
+
const audioRef = text(pick('audio_ref', 'audioRef'));
|
|
282
|
+
return {
|
|
283
|
+
id: String(entity.id),
|
|
284
|
+
title: text(data.title) || entity.name || 'Voice note',
|
|
285
|
+
summary: text(data.summary),
|
|
286
|
+
transcript: text(data.body),
|
|
287
|
+
status: status === 'ready' || status === 'failed' ? status : 'processing',
|
|
288
|
+
...(error ? { error } : {}),
|
|
289
|
+
occurredAt: text(pick('occurred_at', 'occurredAt')) || null,
|
|
290
|
+
...(typeof durationMs === 'number' ? { durationMs } : {}),
|
|
291
|
+
focusId,
|
|
292
|
+
...(audioRef ? { audioRef } : {}),
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export interface CreateVoiceApiOptions {
|
|
297
|
+
http: CorpusHttp;
|
|
298
|
+
scope: CorpusScope;
|
|
299
|
+
/** Sends the recording to the server route. */
|
|
300
|
+
upload: (audio: Blob, meta: { durationMs: number }) => Promise<{ id: string }>;
|
|
301
|
+
limit?: number;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
type Page<T> = { results?: T[] };
|
|
305
|
+
|
|
306
|
+
export function createVoiceApi(options: CreateVoiceApiOptions): VoiceNotesApi {
|
|
307
|
+
const { http } = options;
|
|
308
|
+
return {
|
|
309
|
+
submit: options.upload,
|
|
310
|
+
|
|
311
|
+
async list() {
|
|
312
|
+
const page = await http.get<Page<TenantEntity>>('api/v1/entities', {
|
|
313
|
+
params: { type: 'source_document', source: VOICE_SOURCE, order: 'recent', page_size: options.limit ?? 25 },
|
|
314
|
+
});
|
|
315
|
+
const rows = page.results ?? [];
|
|
316
|
+
if (!rows.length) return [];
|
|
317
|
+
const edges = await http.get<Page<TenantRelationship>>('api/v1/relationships', {
|
|
318
|
+
params: { rel_type: ABOUT_FOCUS, entity__in: rows.map((r) => r.id).join(','), page_size: 200 },
|
|
319
|
+
});
|
|
320
|
+
const focusOf = new Map<string, string>();
|
|
321
|
+
for (const edge of edges.results ?? []) focusOf.set(String(edge.source), String(edge.target));
|
|
322
|
+
return rows
|
|
323
|
+
// `source=voice` may be ignored by an older backend (it says so in
|
|
324
|
+
// `applied_filters`); never show another source's rows as voice notes.
|
|
325
|
+
.filter((r) => text(((r.data ?? {}) as Record<string, unknown>).source) === VOICE_SOURCE)
|
|
326
|
+
.map((r) => toVoiceNote(r, focusOf.get(String(r.id)) ?? null));
|
|
327
|
+
},
|
|
328
|
+
|
|
329
|
+
async listFocuses() {
|
|
330
|
+
const page = await http.get<Page<TenantEntity>>('api/v1/entities', {
|
|
331
|
+
params: { type: 'focus', order: 'attr.title', page_size: 200 },
|
|
332
|
+
});
|
|
333
|
+
return (page.results ?? []).map((r) => ({
|
|
334
|
+
id: String(r.id),
|
|
335
|
+
title: text(((r.data ?? {}) as Record<string, unknown>).title) || r.name || 'Untitled',
|
|
336
|
+
}));
|
|
337
|
+
},
|
|
338
|
+
|
|
339
|
+
async file(noteId, focusId) {
|
|
340
|
+
if (!http.post) throw new Error('This client is read-only.');
|
|
341
|
+
// Replace, never add: a note lives in one project.
|
|
342
|
+
const existing = await http.get<Page<TenantRelationship>>('api/v1/relationships', {
|
|
343
|
+
params: { rel_type: ABOUT_FOCUS, source: noteId },
|
|
344
|
+
});
|
|
345
|
+
if (existing.results?.length) {
|
|
346
|
+
if (!http.delete) throw new Error('This client cannot move a note — no delete method was supplied.');
|
|
347
|
+
for (const edge of existing.results) await http.delete(`api/v1/relationships/${edge.id}`);
|
|
348
|
+
}
|
|
349
|
+
if (focusId) {
|
|
350
|
+
await http.post('api/v1/relationships', { rel_type: ABOUT_FOCUS, source: noteId, target: focusId });
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
};
|
|
354
|
+
}
|