@startsimpli/integrations 0.2.124 → 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.
- package/package.json +4 -4
- package/src/__tests__/briefing-intent-table.ts +121 -0
- package/src/__tests__/briefing-intent.test.ts +281 -0
- package/src/corpus/briefing/index.ts +1 -0
- package/src/corpus/briefing/intent/dates.ts +195 -0
- package/src/corpus/briefing/intent/describe.ts +26 -0
- package/src/corpus/briefing/intent/grammar.ts +175 -0
- package/src/corpus/briefing/intent/ground.ts +95 -0
- package/src/corpus/briefing/intent/index.ts +11 -0
- package/src/corpus/briefing/intent/match.ts +56 -0
- package/src/corpus/briefing/intent/model.ts +135 -0
- package/src/corpus/briefing/intent/position.ts +49 -0
- package/src/corpus/briefing/intent/resolve.ts +49 -0
- package/src/corpus/briefing/intent/target.ts +72 -0
- package/src/corpus/briefing/intent/types.ts +61 -0
|
@@ -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
|
+
}
|