@startsimpli/integrations 0.2.53 → 0.2.55
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__/fixtures/voice-golden.ts +103 -0
- package/src/__tests__/helpers/voice-eval.ts +45 -0
- package/src/__tests__/project-surfaces.test.tsx +563 -0
- package/src/__tests__/voice-eval.test.ts +54 -0
- package/src/__tests__/voice-manage.test.ts +140 -0
- package/src/__tests__/voice-notes-page.test.tsx +53 -0
- package/src/__tests__/voice-quality.test.ts +201 -0
- package/src/__tests__/voice-record-button.test.tsx +129 -0
- package/src/__tests__/voice-server-client.test.ts +58 -0
- package/src/__tests__/voice.test.ts +1 -1
- package/src/components/DocumentDetail.tsx +144 -0
- package/src/components/FiledUnder.tsx +95 -0
- package/src/components/FocusDocuments.tsx +97 -0
- package/src/components/documentText.ts +31 -0
- package/src/components/index.ts +2 -0
- package/src/components/pages/AskPage.tsx +40 -1
- package/src/components/pages/LibraryPage.tsx +4 -162
- package/src/components/pages/PlanningPage.tsx +12 -0
- package/src/components/pages/VoiceNotesPage.tsx +48 -75
- package/src/components/pages/VoiceRecordButton.tsx +162 -0
- package/src/corpus/ask.ts +21 -1
- package/src/corpus/client.ts +12 -0
- package/src/corpus/filing.ts +122 -0
- package/src/corpus/index.ts +26 -0
- package/src/corpus/llm.ts +88 -16
- package/src/corpus/types.ts +22 -1
- package/src/corpus/voice-manage.ts +241 -0
- package/src/corpus/voice.ts +102 -41
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
import * as React from 'react';
|
|
33
33
|
|
|
34
34
|
import { displayProgress } from '../../corpus/planning';
|
|
35
|
+
import { FocusDocuments } from '../FocusDocuments';
|
|
35
36
|
import {
|
|
36
37
|
UnfilteredNotice,
|
|
37
38
|
WorkItemRow,
|
|
@@ -384,6 +385,17 @@ export function PlanningPage({
|
|
|
384
385
|
/>
|
|
385
386
|
))}
|
|
386
387
|
</section>
|
|
388
|
+
|
|
389
|
+
{selectedFocus && api.listDocuments ? (
|
|
390
|
+
<FocusDocuments
|
|
391
|
+
listDocuments={(query) => apiRef.current.listDocuments!(query)}
|
|
392
|
+
focusId={selectedFocus.id}
|
|
393
|
+
focusTitle={selectedFocus.title}
|
|
394
|
+
epoch={epoch}
|
|
395
|
+
onOpenDocument={onOpenDocument}
|
|
396
|
+
classNames={cn}
|
|
397
|
+
/>
|
|
398
|
+
) : null}
|
|
387
399
|
</div>
|
|
388
400
|
);
|
|
389
401
|
}
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import * as React from 'react';
|
|
18
18
|
|
|
19
19
|
import type { VoiceNote, VoiceNotesApi, VoiceFocus } from '../../corpus/voice';
|
|
20
|
+
import { VoiceRecordButton, formatDuration } from './VoiceRecordButton';
|
|
20
21
|
|
|
21
22
|
type Slot = 'root' | 'section' | 'card' | 'button' | 'recording' | 'muted' | 'error' | 'select';
|
|
22
23
|
export type VoiceNotesClassNames = Partial<Record<Slot, string>>;
|
|
@@ -32,17 +33,7 @@ const FALLBACK: Record<Slot, string> = {
|
|
|
32
33
|
select: 'rounded-md border px-2 py-1 text-sm',
|
|
33
34
|
};
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
const CANDIDATE_TYPES = ['audio/webm;codecs=opus', 'audio/webm', 'audio/mp4', 'audio/ogg;codecs=opus'];
|
|
37
|
-
|
|
38
|
-
export function pickMimeType(isSupported: (type: string) => boolean): string {
|
|
39
|
-
return CANDIDATE_TYPES.find((type) => isSupported(type)) ?? '';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function formatDuration(ms: number): string {
|
|
43
|
-
const total = Math.max(0, Math.round(ms / 1000));
|
|
44
|
-
return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')}`;
|
|
45
|
-
}
|
|
36
|
+
export { formatDuration, pickMimeType } from './VoiceRecordButton';
|
|
46
37
|
|
|
47
38
|
export interface VoiceNotesPageProps {
|
|
48
39
|
api: VoiceNotesApi;
|
|
@@ -54,8 +45,6 @@ export interface VoiceNotesPageProps {
|
|
|
54
45
|
recorderFactory?: (stream: MediaStream, mimeType: string) => MediaRecorder;
|
|
55
46
|
}
|
|
56
47
|
|
|
57
|
-
type Phase = 'idle' | 'recording' | 'saving';
|
|
58
|
-
|
|
59
48
|
export function VoiceNotesPage({
|
|
60
49
|
api, classNames, pollMs = 3000, mediaDevices, recorderFactory,
|
|
61
50
|
}: VoiceNotesPageProps) {
|
|
@@ -63,15 +52,13 @@ export function VoiceNotesPage({
|
|
|
63
52
|
|
|
64
53
|
const [notes, setNotes] = React.useState<VoiceNote[] | null>(null);
|
|
65
54
|
const [focuses, setFocuses] = React.useState<VoiceFocus[]>([]);
|
|
66
|
-
const [phase, setPhase] = React.useState<Phase>('idle');
|
|
67
|
-
const [elapsed, setElapsed] = React.useState(0);
|
|
68
55
|
const [error, setError] = React.useState<string | null>(null);
|
|
56
|
+
const [busy, setBusy] = React.useState<Set<string>>(new Set());
|
|
57
|
+
const [audio, setAudio] = React.useState<Record<string, string>>({});
|
|
69
58
|
|
|
70
59
|
// The api may be rebuilt by the parent; the effects below must not re-fire.
|
|
71
60
|
const apiRef = React.useRef(api);
|
|
72
61
|
apiRef.current = api;
|
|
73
|
-
const recorderRef = React.useRef<MediaRecorder | null>(null);
|
|
74
|
-
const startedAt = React.useRef(0);
|
|
75
62
|
|
|
76
63
|
const refresh = React.useCallback(async () => {
|
|
77
64
|
try {
|
|
@@ -93,61 +80,31 @@ export function VoiceNotesPage({
|
|
|
93
80
|
return () => clearTimeout(timer);
|
|
94
81
|
}, [processing, notes, pollMs, refresh]);
|
|
95
82
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const tick = setInterval(() => setElapsed(Date.now() - startedAt.current), 250);
|
|
99
|
-
return () => clearInterval(tick);
|
|
100
|
-
}, [phase]);
|
|
101
|
-
|
|
102
|
-
async function start() {
|
|
103
|
-
setError(null);
|
|
83
|
+
async function act(note: VoiceNote, run: () => Promise<void>, failure: string) {
|
|
84
|
+
setBusy((current) => new Set(current).add(note.id));
|
|
104
85
|
try {
|
|
105
|
-
|
|
106
|
-
if (!devices?.getUserMedia) throw new Error('This browser cannot record audio.');
|
|
107
|
-
const stream = await devices.getUserMedia({ audio: true });
|
|
108
|
-
const mimeType = pickMimeType((t) => typeof MediaRecorder !== 'undefined' && MediaRecorder.isTypeSupported(t));
|
|
109
|
-
const recorder = recorderFactory
|
|
110
|
-
? recorderFactory(stream, mimeType)
|
|
111
|
-
: new MediaRecorder(stream, mimeType ? { mimeType } : undefined);
|
|
112
|
-
const chunks: Blob[] = [];
|
|
113
|
-
recorder.ondataavailable = (event) => { if (event.data.size) chunks.push(event.data); };
|
|
114
|
-
recorder.onstop = () => {
|
|
115
|
-
stream.getTracks().forEach((track) => track.stop());
|
|
116
|
-
const durationMs = Date.now() - startedAt.current;
|
|
117
|
-
const blob = new Blob(chunks, { type: recorder.mimeType || mimeType || 'audio/webm' });
|
|
118
|
-
void save(blob, durationMs);
|
|
119
|
-
};
|
|
120
|
-
recorderRef.current = recorder;
|
|
121
|
-
startedAt.current = Date.now();
|
|
122
|
-
setElapsed(0);
|
|
123
|
-
recorder.start();
|
|
124
|
-
setPhase('recording');
|
|
86
|
+
await run();
|
|
125
87
|
} catch (e) {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
? 'Microphone access was blocked. Allow it in the browser and try again.'
|
|
130
|
-
: e instanceof Error ? e.message : 'Could not start recording.',
|
|
131
|
-
);
|
|
88
|
+
setError(e instanceof Error ? e.message : failure);
|
|
89
|
+
} finally {
|
|
90
|
+
setBusy((current) => { const next = new Set(current); next.delete(note.id); return next; });
|
|
132
91
|
}
|
|
133
92
|
}
|
|
134
93
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
94
|
+
const retry = (note: VoiceNote) => act(note, async () => {
|
|
95
|
+
await apiRef.current.retry?.(note.id);
|
|
96
|
+
await refresh();
|
|
97
|
+
}, 'Could not retry that note.');
|
|
139
98
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
}
|
|
99
|
+
const remove = (note: VoiceNote) => act(note, async () => {
|
|
100
|
+
await apiRef.current.remove?.(note.id);
|
|
101
|
+
setNotes((current) => current?.filter((n) => n.id !== note.id) ?? current);
|
|
102
|
+
}, 'Could not delete that note.');
|
|
103
|
+
|
|
104
|
+
const play = (note: VoiceNote) => act(note, async () => {
|
|
105
|
+
const url = await apiRef.current.audioUrl?.(note.id);
|
|
106
|
+
if (url) setAudio((current) => ({ ...current, [note.id]: url }));
|
|
107
|
+
}, 'Could not load the recording.');
|
|
151
108
|
|
|
152
109
|
async function move(note: VoiceNote, focusId: string) {
|
|
153
110
|
const next = focusId || null;
|
|
@@ -164,15 +121,14 @@ export function VoiceNotesPage({
|
|
|
164
121
|
return (
|
|
165
122
|
<div className={cx('root')}>
|
|
166
123
|
<section className={cx('section')}>
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
)}
|
|
124
|
+
<VoiceRecordButton
|
|
125
|
+
api={api}
|
|
126
|
+
onSaved={refresh}
|
|
127
|
+
mediaDevices={mediaDevices}
|
|
128
|
+
recorderFactory={recorderFactory}
|
|
129
|
+
classNames={{ button: cx('button'), recording: cx('recording'), muted: cx('muted'), error: cx('error') }}
|
|
130
|
+
hint={null}
|
|
131
|
+
/>
|
|
176
132
|
<p className={cx('muted')}>
|
|
177
133
|
Say what needs doing and for which project. It is transcribed, summarised and filed under
|
|
178
134
|
the project it sounds like — change it below if that guess is wrong.
|
|
@@ -197,6 +153,23 @@ export function VoiceNotesPage({
|
|
|
197
153
|
{note.status === 'failed' ? (
|
|
198
154
|
<p role="alert" className={cx('error')}>Could not process this note{note.error ? `: ${note.error}` : '.'}</p>
|
|
199
155
|
) : null}
|
|
156
|
+
{note.status === 'failed' && (api.retry || api.remove) ? (
|
|
157
|
+
<p>
|
|
158
|
+
{api.retry ? (
|
|
159
|
+
<button type="button" className={cx('button')} disabled={busy.has(note.id)} onClick={() => void retry(note)}>Retry</button>
|
|
160
|
+
) : null}{' '}
|
|
161
|
+
{api.remove ? (
|
|
162
|
+
<button type="button" className={cx('button')} disabled={busy.has(note.id)} onClick={() => void remove(note)}>Delete</button>
|
|
163
|
+
) : null}
|
|
164
|
+
</p>
|
|
165
|
+
) : null}
|
|
166
|
+
{note.status === 'ready' && note.audioRef && api.audioUrl ? (
|
|
167
|
+
audio[note.id] ? (
|
|
168
|
+
<audio controls autoPlay src={audio[note.id]} aria-label={`Recording of ${note.title}`} />
|
|
169
|
+
) : (
|
|
170
|
+
<button type="button" className={cx('button')} disabled={busy.has(note.id)} onClick={() => void play(note)}>Play recording</button>
|
|
171
|
+
)
|
|
172
|
+
) : null}
|
|
200
173
|
{note.summary ? <p style={{ whiteSpace: 'pre-line' }}>{note.summary}</p> : null}
|
|
201
174
|
{note.transcript ? (
|
|
202
175
|
<details>
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE RECORD BUTTON (bd startsim-ikxdz): one composer, mounted on Tandem Home and
|
|
3
|
+
* on the Voice page, so a quick note and a note from the full page are the same
|
|
4
|
+
* code path — `api.submit`, nothing else.
|
|
5
|
+
*
|
|
6
|
+
* THE RECORDER WATCHES ITS OWN SIZE. The server refuses a recording over 24 MB
|
|
7
|
+
* (nginx allows 25). A long note must not be lost at the last second to a 413,
|
|
8
|
+
* so the recorder asks for timesliced chunks, counts them, and stops itself a
|
|
9
|
+
* safety margin short of the cap — then SAYS it did, and still saves what it has.
|
|
10
|
+
*
|
|
11
|
+
* MIME TYPE: Chrome/Firefox make webm/ogg, Safari makes mp4. The first type the
|
|
12
|
+
* browser can make wins; with none supported the browser's own default is used.
|
|
13
|
+
*/
|
|
14
|
+
import * as React from 'react';
|
|
15
|
+
|
|
16
|
+
import { MAX_VOICE_BYTES } from '../../corpus/voice-manage';
|
|
17
|
+
import type { VoiceNotesApi } from '../../corpus/voice';
|
|
18
|
+
|
|
19
|
+
export type VoiceRecordSlot = 'root' | 'button' | 'recording' | 'muted' | 'error';
|
|
20
|
+
export type VoiceRecordClassNames = Partial<Record<VoiceRecordSlot, string>>;
|
|
21
|
+
|
|
22
|
+
const FALLBACK: Record<VoiceRecordSlot, string> = {
|
|
23
|
+
root: 'space-y-2',
|
|
24
|
+
button: 'rounded-md border px-4 py-2 text-sm font-medium',
|
|
25
|
+
recording: 'rounded-md border px-4 py-2 text-sm font-medium',
|
|
26
|
+
muted: 'text-sm opacity-70',
|
|
27
|
+
error: 'rounded-md border p-3 text-sm',
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const VOICE_MIME_CANDIDATES = ['audio/webm;codecs=opus', 'audio/webm', 'audio/mp4', 'audio/ogg;codecs=opus'];
|
|
31
|
+
|
|
32
|
+
export function pickMimeType(isSupported: (type: string) => boolean): string {
|
|
33
|
+
return VOICE_MIME_CANDIDATES.find((type) => isSupported(type)) ?? '';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function formatDuration(ms: number): string {
|
|
37
|
+
const total = Math.max(0, Math.round(ms / 1000));
|
|
38
|
+
return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Stop this far short of the server cap: chunks arrive on a timer, and the last one lands after stop(). */
|
|
42
|
+
const SIZE_MARGIN_BYTES = 512 * 1024;
|
|
43
|
+
const TIMESLICE_MS = 1000;
|
|
44
|
+
|
|
45
|
+
type Phase = 'idle' | 'recording' | 'saving';
|
|
46
|
+
|
|
47
|
+
export interface VoiceRecordButtonProps {
|
|
48
|
+
api: Pick<VoiceNotesApi, 'submit'>;
|
|
49
|
+
/** Called after the server has the recording (processing continues there). */
|
|
50
|
+
onSaved?: (saved: { id: string }) => void | Promise<void>;
|
|
51
|
+
classNames?: VoiceRecordClassNames;
|
|
52
|
+
/** Injectable for tests; defaults to the browser's. */
|
|
53
|
+
mediaDevices?: Pick<MediaDevices, 'getUserMedia'>;
|
|
54
|
+
recorderFactory?: (stream: MediaStream, mimeType: string) => MediaRecorder;
|
|
55
|
+
/** Shown under the button; pass `null` to hide. */
|
|
56
|
+
hint?: React.ReactNode;
|
|
57
|
+
children?: React.ReactNode;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function VoiceRecordButton({
|
|
61
|
+
api, onSaved, classNames, mediaDevices, recorderFactory, hint, children,
|
|
62
|
+
}: VoiceRecordButtonProps) {
|
|
63
|
+
const cx = (slot: VoiceRecordSlot) => classNames?.[slot] ?? FALLBACK[slot];
|
|
64
|
+
const [phase, setPhase] = React.useState<Phase>('idle');
|
|
65
|
+
const [elapsed, setElapsed] = React.useState(0);
|
|
66
|
+
const [error, setError] = React.useState<string | null>(null);
|
|
67
|
+
const [notice, setNotice] = React.useState<string | null>(null);
|
|
68
|
+
|
|
69
|
+
const apiRef = React.useRef(api);
|
|
70
|
+
apiRef.current = api;
|
|
71
|
+
const onSavedRef = React.useRef(onSaved);
|
|
72
|
+
onSavedRef.current = onSaved;
|
|
73
|
+
const recorderRef = React.useRef<MediaRecorder | null>(null);
|
|
74
|
+
const startedAt = React.useRef(0);
|
|
75
|
+
|
|
76
|
+
React.useEffect(() => {
|
|
77
|
+
if (phase !== 'recording') return undefined;
|
|
78
|
+
const tick = setInterval(() => setElapsed(Date.now() - startedAt.current), 250);
|
|
79
|
+
return () => clearInterval(tick);
|
|
80
|
+
}, [phase]);
|
|
81
|
+
|
|
82
|
+
async function save(blob: Blob, durationMs: number) {
|
|
83
|
+
try {
|
|
84
|
+
if (blob.size === 0) throw new Error('Nothing was recorded.');
|
|
85
|
+
const saved = await apiRef.current.submit(blob, { durationMs });
|
|
86
|
+
await onSavedRef.current?.(saved);
|
|
87
|
+
} catch (e) {
|
|
88
|
+
setError(e instanceof Error ? e.message : 'Saving the recording failed.');
|
|
89
|
+
} finally {
|
|
90
|
+
setPhase('idle');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function start() {
|
|
95
|
+
setError(null);
|
|
96
|
+
setNotice(null);
|
|
97
|
+
try {
|
|
98
|
+
const devices = mediaDevices ?? navigator.mediaDevices;
|
|
99
|
+
if (!devices?.getUserMedia) throw new Error('This browser cannot record audio.');
|
|
100
|
+
const stream = await devices.getUserMedia({ audio: true });
|
|
101
|
+
const mimeType = pickMimeType((t) => typeof MediaRecorder !== 'undefined' && MediaRecorder.isTypeSupported(t));
|
|
102
|
+
const recorder = recorderFactory
|
|
103
|
+
? recorderFactory(stream, mimeType)
|
|
104
|
+
: new MediaRecorder(stream, mimeType ? { mimeType } : undefined);
|
|
105
|
+
const chunks: Blob[] = [];
|
|
106
|
+
let total = 0;
|
|
107
|
+
let capped = false;
|
|
108
|
+
recorder.ondataavailable = (event) => {
|
|
109
|
+
if (!event.data.size) return;
|
|
110
|
+
chunks.push(event.data);
|
|
111
|
+
total += event.data.size;
|
|
112
|
+
if (!capped && total >= MAX_VOICE_BYTES - SIZE_MARGIN_BYTES) {
|
|
113
|
+
capped = true;
|
|
114
|
+
setNotice('Stopped at the 24 MB size limit — saving what was recorded. Start a new note to keep going.');
|
|
115
|
+
setPhase('saving');
|
|
116
|
+
recorder.stop();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
recorder.onstop = () => {
|
|
120
|
+
stream.getTracks().forEach((track) => track.stop());
|
|
121
|
+
const durationMs = Date.now() - startedAt.current;
|
|
122
|
+
const blob = new Blob(chunks, { type: recorder.mimeType || mimeType || 'audio/webm' });
|
|
123
|
+
void save(blob, durationMs);
|
|
124
|
+
};
|
|
125
|
+
recorderRef.current = recorder;
|
|
126
|
+
startedAt.current = Date.now();
|
|
127
|
+
setElapsed(0);
|
|
128
|
+
recorder.start(TIMESLICE_MS);
|
|
129
|
+
setPhase('recording');
|
|
130
|
+
} catch (e) {
|
|
131
|
+
setPhase('idle');
|
|
132
|
+
setError(
|
|
133
|
+
e instanceof Error && e.name === 'NotAllowedError'
|
|
134
|
+
? 'Microphone access was blocked. Allow it in the browser and try again.'
|
|
135
|
+
: e instanceof Error ? e.message : 'Could not start recording.',
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function stop() {
|
|
141
|
+
setPhase('saving');
|
|
142
|
+
recorderRef.current?.stop();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<div className={cx('root')}>
|
|
147
|
+
{phase === 'recording' ? (
|
|
148
|
+
<button type="button" className={cx('recording')} onClick={stop}>
|
|
149
|
+
Stop · {formatDuration(elapsed)}
|
|
150
|
+
</button>
|
|
151
|
+
) : (
|
|
152
|
+
<button type="button" className={cx('button')} onClick={() => void start()} disabled={phase === 'saving'}>
|
|
153
|
+
{phase === 'saving' ? 'Saving…' : 'Record a voice note'}
|
|
154
|
+
</button>
|
|
155
|
+
)}
|
|
156
|
+
{hint !== undefined ? hint : null}
|
|
157
|
+
{notice ? <p role="status" className={cx('muted')}>{notice}</p> : null}
|
|
158
|
+
{error ? <p role="alert" className={cx('error')}>{error}</p> : null}
|
|
159
|
+
{children}
|
|
160
|
+
</div>
|
|
161
|
+
);
|
|
162
|
+
}
|
package/src/corpus/ask.ts
CHANGED
|
@@ -241,7 +241,10 @@ async function loadCandidates(
|
|
|
241
241
|
|
|
242
242
|
const [documents, money] = await Promise.all([
|
|
243
243
|
corpus.listDocuments(documentQuery),
|
|
244
|
-
|
|
244
|
+
// A money event cannot be filed under a project (`about_focus` is
|
|
245
|
+
// source_document -> focus), so a project-scoped question has none to offer.
|
|
246
|
+
// Asking anyway would put workspace-wide payments into a "this project" answer.
|
|
247
|
+
corpus.listMoneyEvents && !request.filters?.focus
|
|
245
248
|
? corpus
|
|
246
249
|
.listMoneyEvents({
|
|
247
250
|
...(searchTerm ? { search: searchTerm } : {}),
|
|
@@ -262,6 +265,23 @@ async function loadCandidates(
|
|
|
262
265
|
};
|
|
263
266
|
}
|
|
264
267
|
|
|
268
|
+
/**
|
|
269
|
+
* The filters an Ask route may accept from a request body: only the ones Ask
|
|
270
|
+
* understands, as non-empty strings. A route passes `body.filters` through this
|
|
271
|
+
* so a crafted POST cannot smuggle an arbitrary query parameter into retrieval.
|
|
272
|
+
* `undefined` when nothing usable remains.
|
|
273
|
+
*/
|
|
274
|
+
export function sanitizeAskFilters(raw: unknown): AskRequest['filters'] | undefined {
|
|
275
|
+
if (!raw || typeof raw !== 'object') return undefined;
|
|
276
|
+
const source = raw as Record<string, unknown>;
|
|
277
|
+
const out: Record<string, string> = {};
|
|
278
|
+
for (const key of ['source', 'partyId', 'focus', 'occurredAfter', 'occurredBefore']) {
|
|
279
|
+
const value = source[key];
|
|
280
|
+
if (typeof value === 'string' && value.trim()) out[key] = value.trim();
|
|
281
|
+
}
|
|
282
|
+
return Object.keys(out).length ? (out as AskRequest['filters']) : undefined;
|
|
283
|
+
}
|
|
284
|
+
|
|
265
285
|
/**
|
|
266
286
|
* How many records this workspace holds AT ALL — asked only when the answer is
|
|
267
287
|
* going to be "nothing matched", and only to decide WHICH "nothing".
|
package/src/corpus/client.ts
CHANGED
|
@@ -49,6 +49,7 @@ import {
|
|
|
49
49
|
import type {
|
|
50
50
|
CorpusTypeKeys, TenantEntity, TenantFacetRow, TenantRelationship,
|
|
51
51
|
} from './entities';
|
|
52
|
+
import { ABOUT_FOCUS, createFilingApi } from './filing';
|
|
52
53
|
import {
|
|
53
54
|
FOCUS_TYPE,
|
|
54
55
|
IN_FOCUS,
|
|
@@ -285,6 +286,7 @@ const DOCUMENT_FILTER_PARAMS: Array<[keyof DocumentQuery, string[]]> = [
|
|
|
285
286
|
['search', ['search']],
|
|
286
287
|
['source', ['source', 'attr.source']],
|
|
287
288
|
['partyId', ['party', 'related_to']],
|
|
289
|
+
['focus', ['related_to']],
|
|
288
290
|
['occurredAfter', ['occurred_after', 'attr.occurred_at__gte']],
|
|
289
291
|
['occurredBefore', ['occurred_before', 'attr.occurred_at__lte']],
|
|
290
292
|
['folder', ['attr.path']],
|
|
@@ -368,6 +370,14 @@ function params(query: DocumentQuery, type: string) {
|
|
|
368
370
|
if (query.order && (query.order !== 'relevance' || query.search)) out.order = query.order;
|
|
369
371
|
if (query.source) out.source = query.source;
|
|
370
372
|
if (query.partyId) out.party = query.partyId;
|
|
373
|
+
if (query.focus) {
|
|
374
|
+
// Filing is an edge, so "the documents in this project" is an edge walk. A
|
|
375
|
+
// document is the SOURCE of its own `about_focus` edge: `out` returns
|
|
376
|
+
// documents, `in` returns an empty page (see `commitmentParams`).
|
|
377
|
+
out.related_to = query.focus;
|
|
378
|
+
out.related_via = ABOUT_FOCUS;
|
|
379
|
+
out.related_direction = 'out';
|
|
380
|
+
}
|
|
371
381
|
if (query.occurredAfter) out.occurred_after = query.occurredAfter;
|
|
372
382
|
if (query.occurredBefore) out.occurred_before = query.occurredBefore;
|
|
373
383
|
// THE FOLDER BOUNDS, and only ever one of them. `attr.path` is the shelf and
|
|
@@ -684,6 +694,8 @@ export function createCorpusApi(
|
|
|
684
694
|
}
|
|
685
695
|
|
|
686
696
|
return {
|
|
697
|
+
...createFilingApi(http, { entities, relationships }),
|
|
698
|
+
|
|
687
699
|
async listDocuments(query) {
|
|
688
700
|
const documents = await page<TenantEntity>(types.document, query, (entity) => entity);
|
|
689
701
|
// Skip the edge read entirely for an empty page: an empty corpus should
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FILING A DOCUMENT UNDER A PROJECT (bd startsim-vkymg).
|
|
3
|
+
*
|
|
4
|
+
* Any `source_document` — a voice note, a Fathom meeting, a PDF — is filed under
|
|
5
|
+
* a project (a `focus`) by ONE `about_focus` edge, source_document -> focus. This
|
|
6
|
+
* is the single implementation of that edge's lifecycle; the voice page, the
|
|
7
|
+
* Library and anything later call it rather than re-writing replace-the-edge.
|
|
8
|
+
*
|
|
9
|
+
* `about_focus` is not `in_focus`. `in_focus` is commitment -> focus only and is
|
|
10
|
+
* what Planning's 'N of M done' counts; a document must never enter that number,
|
|
11
|
+
* which is why the two edges have different names and nothing here touches
|
|
12
|
+
* `in_focus`.
|
|
13
|
+
*/
|
|
14
|
+
import type { CorpusHttp } from './client';
|
|
15
|
+
import type { TenantEntity, TenantRelationship } from './entities';
|
|
16
|
+
import { FOCUS_TYPE } from './planning';
|
|
17
|
+
|
|
18
|
+
/** A document is ABOUT a focus — its own edge, never `in_focus`. */
|
|
19
|
+
export const ABOUT_FOCUS = 'about_focus';
|
|
20
|
+
|
|
21
|
+
export interface FocusChoice {
|
|
22
|
+
id: string;
|
|
23
|
+
title: string;
|
|
24
|
+
detail?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** What a document is filed under. */
|
|
28
|
+
export interface DocumentFiling {
|
|
29
|
+
id: string;
|
|
30
|
+
title: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type Page<T> = { results?: T[] };
|
|
34
|
+
|
|
35
|
+
function text(value: unknown): string {
|
|
36
|
+
return typeof value === 'string' ? value : '';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface FilingPaths {
|
|
40
|
+
entities?: string;
|
|
41
|
+
relationships?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Replace, never add: a document lives in one project. Existing `about_focus`
|
|
46
|
+
* edges from the document are deleted, then the new one is written (none for
|
|
47
|
+
* `null`, which unfiles).
|
|
48
|
+
*/
|
|
49
|
+
export async function fileDocument(
|
|
50
|
+
http: CorpusHttp,
|
|
51
|
+
documentId: string,
|
|
52
|
+
focusId: string | null,
|
|
53
|
+
paths: FilingPaths = {},
|
|
54
|
+
): Promise<void> {
|
|
55
|
+
const relationships = paths.relationships ?? 'api/v1/relationships';
|
|
56
|
+
if (!http.post) throw new Error('This client is read-only.');
|
|
57
|
+
const existing = await http.get<Page<TenantRelationship>>(relationships, {
|
|
58
|
+
params: { rel_type: ABOUT_FOCUS, source: documentId },
|
|
59
|
+
});
|
|
60
|
+
if (existing.results?.length) {
|
|
61
|
+
if (!http.delete) throw new Error('This client cannot move a note — no delete method was supplied.');
|
|
62
|
+
for (const edge of existing.results) await http.delete(`${relationships}/${edge.id}`);
|
|
63
|
+
}
|
|
64
|
+
if (focusId) {
|
|
65
|
+
await http.post(relationships, { rel_type: ABOUT_FOCUS, source: documentId, target: focusId });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The projects a document can be filed under, by title. */
|
|
70
|
+
export async function listFocusChoices(
|
|
71
|
+
http: CorpusHttp,
|
|
72
|
+
paths: FilingPaths = {},
|
|
73
|
+
): Promise<FocusChoice[]> {
|
|
74
|
+
const entities = paths.entities ?? 'api/v1/entities';
|
|
75
|
+
const page = await http.get<Page<TenantEntity>>(entities, {
|
|
76
|
+
params: { type: FOCUS_TYPE, order: 'attr.title', page_size: 200 },
|
|
77
|
+
});
|
|
78
|
+
return (page.results ?? []).map((row) => ({
|
|
79
|
+
id: String(row.id),
|
|
80
|
+
title: text(((row.data ?? {}) as Record<string, unknown>).title) || row.name || 'Untitled',
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The project a document is filed under, or null when it is not filed. */
|
|
85
|
+
export async function getDocumentFiling(
|
|
86
|
+
http: CorpusHttp,
|
|
87
|
+
documentId: string,
|
|
88
|
+
paths: FilingPaths = {},
|
|
89
|
+
): Promise<DocumentFiling | null> {
|
|
90
|
+
const entities = paths.entities ?? 'api/v1/entities';
|
|
91
|
+
const relationships = paths.relationships ?? 'api/v1/relationships';
|
|
92
|
+
const edges = await http.get<Page<TenantRelationship>>(relationships, {
|
|
93
|
+
params: { rel_type: ABOUT_FOCUS, source: documentId },
|
|
94
|
+
});
|
|
95
|
+
const edge = edges.results?.[0];
|
|
96
|
+
if (!edge) return null;
|
|
97
|
+
const id = String(edge.target);
|
|
98
|
+
try {
|
|
99
|
+
const row = await http.get<TenantEntity>(`${entities}/${encodeURIComponent(id)}`);
|
|
100
|
+
const title = text(((row.data ?? {}) as Record<string, unknown>).title) || row.name;
|
|
101
|
+
return { id, title: title || 'Untitled' };
|
|
102
|
+
} catch {
|
|
103
|
+
// The edge is real even when the project row is not readable by this
|
|
104
|
+
// principal; say it is filed rather than that it is not.
|
|
105
|
+
return { id, title: 'A project you cannot open' };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The filing operations bound to one client — what `createCorpusApi` exposes.
|
|
111
|
+
* `fileDocument` is present only when the client can write: a control that
|
|
112
|
+
* appears to work and throws "read-only" is worse than none.
|
|
113
|
+
*/
|
|
114
|
+
export function createFilingApi(http: CorpusHttp, paths: FilingPaths = {}) {
|
|
115
|
+
return {
|
|
116
|
+
...(http.post
|
|
117
|
+
? { fileDocument: (documentId: string, focusId: string | null) => fileDocument(http, documentId, focusId, paths) }
|
|
118
|
+
: {}),
|
|
119
|
+
getDocumentFiling: (documentId: string) => getDocumentFiling(http, documentId, paths),
|
|
120
|
+
listFocusChoices: () => listFocusChoices(http, paths),
|
|
121
|
+
};
|
|
122
|
+
}
|
package/src/corpus/index.ts
CHANGED
|
@@ -101,6 +101,7 @@ export type { QuestionTerms, RetrieveOptions, ScopeFilterResult } from './retrie
|
|
|
101
101
|
export {
|
|
102
102
|
answerSystemPrompt,
|
|
103
103
|
askCorpus,
|
|
104
|
+
sanitizeAskFilters,
|
|
104
105
|
buildAnswerRequest,
|
|
105
106
|
documentText,
|
|
106
107
|
renderAnswerPrompt,
|
|
@@ -197,6 +198,15 @@ export {
|
|
|
197
198
|
} from './fixtures';
|
|
198
199
|
export type { FixtureCorpusOptions } from './fixtures';
|
|
199
200
|
|
|
201
|
+
export {
|
|
202
|
+
ABOUT_FOCUS,
|
|
203
|
+
createFilingApi,
|
|
204
|
+
fileDocument,
|
|
205
|
+
getDocumentFiling,
|
|
206
|
+
listFocusChoices,
|
|
207
|
+
} from './filing';
|
|
208
|
+
export type { DocumentFiling } from './filing';
|
|
209
|
+
|
|
200
210
|
export {
|
|
201
211
|
VOICE_SOURCE,
|
|
202
212
|
VOICE_SOURCE_LABEL,
|
|
@@ -208,6 +218,22 @@ export {
|
|
|
208
218
|
voiceExternalId,
|
|
209
219
|
voiceToEntity,
|
|
210
220
|
} from './voice';
|
|
221
|
+
export {
|
|
222
|
+
MAX_VOICE_BYTES,
|
|
223
|
+
VoiceNoteConflict,
|
|
224
|
+
VoiceNoteNotFound,
|
|
225
|
+
createVoiceServerClient,
|
|
226
|
+
deleteVoiceNote,
|
|
227
|
+
retryVoiceNote,
|
|
228
|
+
voiceAudioResponse,
|
|
229
|
+
voiceErrorResponse,
|
|
230
|
+
} from './voice-manage';
|
|
231
|
+
export type {
|
|
232
|
+
ManagedHttp,
|
|
233
|
+
ManagedVoiceAudioStore,
|
|
234
|
+
RetriedVoiceNote,
|
|
235
|
+
VoiceServerClient,
|
|
236
|
+
} from './voice-manage';
|
|
211
237
|
export type {
|
|
212
238
|
FinishedVoiceNote,
|
|
213
239
|
FocusChoice,
|