@startsimpli/integrations 0.2.123 → 0.2.125

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.
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Which item does "this one" / "the bench thing" / "the next one" mean?
3
+ * Resolved against the spoken list and the open items; never invents one.
4
+ */
5
+ import { speakable } from '../speak';
6
+ import { contentWords, matchItems, words } from './match';
7
+ import type { IntentContext, IntentItem } from './types';
8
+
9
+ export type Target =
10
+ | { ok: true; item: IntentItem }
11
+ | { ok: false; question: string };
12
+
13
+ const DEICTIC = /^(?:the )?(?:this|that|it|them|these|those|current|same)(?: one| thing| item| task| one i am reading)?$/;
14
+ const NEXT = /^(?:the )?next(?: one| item| thing| task)?$/;
15
+ const PREVIOUS = /^(?:the )?(?:previous|last one|one before)(?: one| item| thing| task)?$/;
16
+ const OTHER = /^(?:that |the |those )?other(?: one| thing| item| task| ones| things)?$/;
17
+ const ORDINAL_WORDS: Record<string, number> = {
18
+ first: 1, second: 2, third: 3, fourth: 4, fifth: 5, sixth: 6, seventh: 7, eighth: 8, ninth: 9, tenth: 10,
19
+ '1st': 1, '2nd': 2, '3rd': 3, '4th': 4, '5th': 5, '6th': 6, '7th': 7, '8th': 8,
20
+ one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8,
21
+ };
22
+ const ORDINAL = /^(?:the )?(?:(first|second|third|fourth|fifth|sixth|seventh|eighth|ninth|tenth|1st|2nd|3rd|4th|5th|6th|7th|8th)|(?:number|item) (\d|one|two|three|four|five|six|seven|eight))(?: one| item| thing| task)?$/;
23
+
24
+ const join = (titles: string[]) => (titles.length <= 1 ? titles.join('') : `${titles.slice(0, -1).join(', ')}, or ${titles[titles.length - 1]}`);
25
+ const say = (item: IntentItem) => speakable(item.title, 8);
26
+
27
+ /** "Which one: A, B, or C?" - three at most, then "or another". */
28
+ export function whichOne(items: IntentItem[]): string {
29
+ if (!items.length) return 'Which item do you mean?';
30
+ const shown = items.slice(0, 3).map(say);
31
+ if (items.length > 3) shown.push('another');
32
+ return `Which one: ${join(shown)}?`;
33
+ }
34
+
35
+ export function resolveTarget(phrase: string, ctx: IntentContext, readingId: string | null): Target {
36
+ const p = words(phrase).join(' ');
37
+ const byId = new Map([...ctx.briefed, ...ctx.open].map((i) => [i.id, i] as const));
38
+ const reading = readingId ? byId.get(readingId) ?? null : null;
39
+ const index = reading ? ctx.briefed.findIndex((i) => i.id === reading.id) : -1;
40
+
41
+ const structural = ORDINAL.test(p) || NEXT.test(p) || PREVIOUS.test(p) || OTHER.test(p);
42
+ if (!p || DEICTIC.test(p) || (!structural && !contentWords(p).length)) {
43
+ if (reading) return { ok: true, item: reading };
44
+ if (ctx.briefed.length === 1) return { ok: true, item: ctx.briefed[0] };
45
+ return { ok: false, question: whichOne(ctx.briefed) };
46
+ }
47
+ if (NEXT.test(p)) {
48
+ const next = index >= 0 ? ctx.briefed[index + 1] : undefined;
49
+ if (next) return { ok: true, item: next };
50
+ return { ok: false, question: index >= 0 ? 'There is no next item.' : whichOne(ctx.briefed) };
51
+ }
52
+ if (PREVIOUS.test(p)) {
53
+ const prev = index > 0 ? ctx.briefed[index - 1] : p.includes('last') && index < 0 ? ctx.briefed[ctx.briefed.length - 1] : undefined;
54
+ return prev ? { ok: true, item: prev } : { ok: false, question: whichOne(ctx.briefed) };
55
+ }
56
+ const ord = ORDINAL.exec(p);
57
+ if (ord) {
58
+ const n = ORDINAL_WORDS[ord[1] ?? ''] ?? (ord[2] ? ORDINAL_WORDS[ord[2]] ?? parseInt(ord[2], 10) : 0);
59
+ const item = ctx.briefed[n - 1];
60
+ return item ? { ok: true, item } : { ok: false, question: `There are only ${ctx.briefed.length} items in the briefing.` };
61
+ }
62
+ if (OTHER.test(p)) {
63
+ const briefedIds = new Set(ctx.briefed.map((i) => i.id));
64
+ const pool = ctx.open.filter((i) => !briefedIds.has(i.id));
65
+ if (pool.length === 1) return { ok: true, item: pool[0] };
66
+ return { ok: false, question: pool.length ? whichOne(pool) : 'There is nothing else open.' };
67
+ }
68
+ const found = matchItems(p, [...ctx.briefed, ...ctx.open.filter((o) => !ctx.briefed.some((b) => b.id === o.id))]);
69
+ if (found.kind === 'one') return { ok: true, item: found.item };
70
+ if (found.kind === 'many') return { ok: false, question: whichOne(found.items) };
71
+ return { ok: false, question: `I could not find a ${contentWords(p).join(' ')} item. Say add it to create one.` };
72
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * What a spoken reply becomes. The parser NEVER executes: it returns these, the
3
+ * executor (`../actions`) applies them, and a person sees what was understood first.
4
+ */
5
+ import type { BriefingScriptLlm } from '../script';
6
+
7
+ /** An item a reply may refer to: only the fields the matcher and the prompt need. */
8
+ export interface IntentItem {
9
+ id: string;
10
+ title: string;
11
+ focusName?: string;
12
+ linearIdentifier?: string;
13
+ }
14
+
15
+ export interface IntentContext {
16
+ /** What the person said (the transcript). */
17
+ utterance: string;
18
+ now: number;
19
+ /** The listener's IANA zone: "tomorrow" and "Friday" are resolved in it. */
20
+ timeZone: string;
21
+ /** The items just spoken, in the order they were spoken. */
22
+ briefed: IntentItem[];
23
+ /** Other open items a reply may name ("that other thing"). */
24
+ open: IntentItem[];
25
+ /** The item being read when the person pressed talk (see `readingAt`), or null. */
26
+ readingId: string | null;
27
+ /** Whether the undo stack has anything on it. */
28
+ hasUndo: boolean;
29
+ }
30
+
31
+ export type VoiceAction =
32
+ /** `to` is `later` (clear the due date) or a `YYYY-MM-DD` date after today. */
33
+ | { type: 'defer'; itemId: string; to: string; label?: string }
34
+ | { type: 'pull_today'; itemId: string; label?: string }
35
+ | { type: 'done'; itemId: string }
36
+ | { type: 'drop'; itemId: string }
37
+ /** 0 critical, 1 urgent, 2 medium, 3 low, 4 lowest. */
38
+ | { type: 'set_priority'; itemId: string; priority: number }
39
+ | { type: 'add_item'; title: string; dueDate: string | null }
40
+ | { type: 'undo' }
41
+ /** A short thing to say back when the reply was not clear enough to act on. */
42
+ | { type: 'clarify'; question: string }
43
+ | { type: 'none' };
44
+
45
+ export interface IntentResult {
46
+ actions: VoiceAction[];
47
+ source: 'llm' | 'grammar';
48
+ /** One line to show before applying: "Move Pick up printer paper to Friday". */
49
+ understood: string;
50
+ /** Why the model's answer was not used, for logs and the accuracy probe. */
51
+ notes: string[];
52
+ }
53
+
54
+ export interface IntentOptions {
55
+ /** The model. Omit to use the grammar alone. */
56
+ llm?: BriefingScriptLlm | null;
57
+ /** Model deadline; past it the grammar answers. Default 8 s. */
58
+ timeoutMs?: number;
59
+ /** Below this the reply asks instead of acting. Default 0.6. */
60
+ minConfidence?: number;
61
+ }
@@ -22,12 +22,13 @@ function setup(over: { playing?: boolean } = {}) {
22
22
  onInterruption: (cb) => { listeners.interruption = cb; return () => { listeners.interruption = undefined; }; },
23
23
  onRouteChange: (cb) => { listeners.route = cb; return () => { listeners.route = undefined; }; },
24
24
  };
25
+ const sleep = vi.fn(async (_ms: number) => undefined);
25
26
  let tick: (() => void) | undefined;
26
27
  const diag = { log: vi.fn() };
27
28
  const player = createAudioPlayer({
28
29
  driver, session, diag,
29
30
  poll: (fn) => { tick = fn; return () => { tick = undefined; }; },
30
- sleep: async () => undefined,
31
+ sleep,
31
32
  stepTimeoutMs: 30,
32
33
  });
33
34
  const recorderDriver: AudioDriver = {
@@ -43,7 +44,7 @@ function setup(over: { playing?: boolean } = {}) {
43
44
  sleep: async () => undefined, diag, onInterrupted: vi.fn(),
44
45
  });
45
46
  return {
46
- player, driver, order, diag, listeners, recorder, tick: () => tick?.(),
47
+ player, driver, order, diag, sleep, listeners, recorder, tick: () => tick?.(),
47
48
  setStatus: (s: Partial<PlaybackStatus>) => { status = { ...status, ...s }; },
48
49
  category: () => category,
49
50
  };
@@ -71,6 +72,15 @@ describe('briefing playback and the recorder share one audio session', () => {
71
72
  ]);
72
73
  });
73
74
 
75
+ it('waits for the OS to settle after releasing the player, before the recorder may start (a start 200 ms after playback failed on the simulator)', async () => {
76
+ const s = setup();
77
+ await s.player.play(SOURCE);
78
+ s.sleep.mockClear();
79
+ await s.player.handoffToRecording();
80
+ expect(s.sleep).toHaveBeenCalledWith(expect.any(Number));
81
+ expect(Math.max(...s.sleep.mock.calls.map((c) => c[0]))).toBeGreaterThanOrEqual(500);
82
+ });
83
+
74
84
  it('logs the session before and after the handoff', async () => {
75
85
  const s = setup();
76
86
  await s.player.play(SOURCE);
@@ -43,6 +43,8 @@ export interface PlayerState {
43
43
  const POLL_MS = 250;
44
44
  const START_TRIES = 80;
45
45
  const START_STEP_MS = 100;
46
+ /** After releasing the player, the OS needs a moment before the recorder can take the session (a start 200 ms after playback failed with '!rec'). */
47
+ const HANDOFF_SETTLE_MS = 600;
46
48
 
47
49
  export function createAudioPlayer(deps: {
48
50
  driver: AudioPlayerDriver;
@@ -172,6 +174,7 @@ export function createAudioPlayer(deps: {
172
174
  async handoffToRecording(): Promise<void> {
173
175
  const before = await facts();
174
176
  await release();
177
+ await deps.sleep(HANDOFF_SETTLE_MS);
175
178
  set({ phase: 'idle', positionMillis: 0 });
176
179
  deps.diag.log('audio.handoff', { before, after: await facts() });
177
180
  },