@startsimpli/integrations 0.2.124 → 0.2.126
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__/briefing-actions.test.ts +316 -0
- package/src/__tests__/briefing-compose.test.ts +6 -0
- package/src/__tests__/briefing-intent-table.ts +121 -0
- package/src/__tests__/briefing-intent.test.ts +281 -0
- package/src/corpus/briefing/actions/confirm.ts +65 -0
- package/src/corpus/briefing/actions/execute.ts +153 -0
- package/src/corpus/briefing/actions/index.ts +9 -0
- package/src/corpus/briefing/actions/log.ts +63 -0
- package/src/corpus/briefing/actions/seam.ts +50 -0
- package/src/corpus/briefing/actions/types.ts +90 -0
- package/src/corpus/briefing/compose.ts +3 -0
- package/src/corpus/briefing/index.ts +2 -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
- package/src/corpus/client.ts +1 -0
- package/src/corpus/types.ts +4 -1
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ITEM EDIT SEAM: the one interface every voice edit goes through.
|
|
3
|
+
*
|
|
4
|
+
* Today it is backed by the tenant's commitment upsert (`commitmentEditSeam`).
|
|
5
|
+
* Epic A's sync engine wraps it so an edit is also stamped for, and pushed to,
|
|
6
|
+
* Linear: it replaces or decorates `update`/`create` and nothing else in the
|
|
7
|
+
* executor changes. Keep this the ONLY way an action touches an item.
|
|
8
|
+
*/
|
|
9
|
+
import type { Commitment } from '../../types';
|
|
10
|
+
|
|
11
|
+
/** What an edit needs to know about an item; a `Commitment` satisfies it. */
|
|
12
|
+
export interface EditableItem {
|
|
13
|
+
id: string;
|
|
14
|
+
title: string;
|
|
15
|
+
status: string;
|
|
16
|
+
dueAt?: string | null;
|
|
17
|
+
priority?: number;
|
|
18
|
+
resolvedAt?: string | null;
|
|
19
|
+
statusReason?: string;
|
|
20
|
+
externalId?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The fields a voice reply changes. `null` clears (due date, priority, resolved time). */
|
|
24
|
+
export interface ItemPatch {
|
|
25
|
+
status?: string;
|
|
26
|
+
statusReason?: string;
|
|
27
|
+
dueAt?: string | null;
|
|
28
|
+
priority?: number | null;
|
|
29
|
+
resolvedAt?: string | null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ItemDraft {
|
|
33
|
+
title: string;
|
|
34
|
+
dueAt?: string | null;
|
|
35
|
+
ownerSub: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ItemEditSeam {
|
|
39
|
+
load(id: string): Promise<EditableItem | null>;
|
|
40
|
+
update(item: EditableItem, patch: ItemPatch): Promise<EditableItem>;
|
|
41
|
+
create(draft: ItemDraft): Promise<EditableItem>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type ActionStatus = 'applied' | 'failed' | 'asked';
|
|
45
|
+
|
|
46
|
+
export interface ActionResult {
|
|
47
|
+
status: ActionStatus;
|
|
48
|
+
/** What was said back for this action. */
|
|
49
|
+
spoken: string;
|
|
50
|
+
itemId?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One undoable change: the fields before and after, to restore and to check nothing moved since. */
|
|
54
|
+
export interface UndoEntry {
|
|
55
|
+
actionId: string;
|
|
56
|
+
kind: 'edit' | 'create';
|
|
57
|
+
itemId: string;
|
|
58
|
+
title: string;
|
|
59
|
+
before: ItemPatch;
|
|
60
|
+
after: ItemPatch;
|
|
61
|
+
at: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Everything remembered per person: what was applied (idempotency) and what can be undone. */
|
|
65
|
+
export interface ActionLog {
|
|
66
|
+
applied: Record<string, ActionResult>;
|
|
67
|
+
/** Insertion order of `applied`, so the oldest ids can be pruned. */
|
|
68
|
+
order: string[];
|
|
69
|
+
/** Newest last. */
|
|
70
|
+
stack: UndoEntry[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface ActionLogStore {
|
|
74
|
+
load(): Promise<ActionLog>;
|
|
75
|
+
save(log: ActionLog): Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ExecuteOutcome {
|
|
79
|
+
results: ActionResult[];
|
|
80
|
+
/** The sentences to say back, in order. */
|
|
81
|
+
text: string;
|
|
82
|
+
applied: number;
|
|
83
|
+
failed: number;
|
|
84
|
+
/** How many changes can still be undone. */
|
|
85
|
+
undoDepth: number;
|
|
86
|
+
/** True when every action had already been applied under this client action id. */
|
|
87
|
+
replayed: boolean;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export type ItemOf = Pick<Commitment, 'id'>;
|
|
@@ -28,6 +28,9 @@ export function localDate(instant: number, timeZone = 'UTC'): string {
|
|
|
28
28
|
function dueDate(dueAt: string | null | undefined, timeZone: string): string | null {
|
|
29
29
|
if (!dueAt) return null;
|
|
30
30
|
if (/^\d{4}-\d{2}-\d{2}$/.test(dueAt)) return dueAt;
|
|
31
|
+
// The tenant reader turns a stored bare date into an instant at UTC midnight. That is still a DATE (a
|
|
32
|
+
// voice reply writes "Friday" as 2026-11-06): localising it would put it on the evening before in New York.
|
|
33
|
+
if (/^\d{4}-\d{2}-\d{2}T00:00:00(?:\.0+)?Z$/.test(dueAt)) return dueAt.slice(0, 10);
|
|
31
34
|
const t = Date.parse(dueAt);
|
|
32
35
|
return Number.isNaN(t) ? null : localDate(t, timeZone);
|
|
33
36
|
}
|
|
@@ -11,3 +11,5 @@ export { briefingResponse, briefingAudioResponse } from './respond';
|
|
|
11
11
|
export { createEntityBriefingStore, briefingExternalId, BRIEFING_SOURCE } from './store';
|
|
12
12
|
export type { BriefingContextRecord, BriefingRecord, BriefingStore } from './store';
|
|
13
13
|
export { briefingContentHash, hashString, SCRIPT_VERSION } from './cache-key';
|
|
14
|
+
export * from './intent';
|
|
15
|
+
export * from './actions';
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spoken dates for voice replies. PURE: `now` and the listener's `timeZone` come
|
|
3
|
+
* in, a calendar date (`YYYY-MM-DD`) or "later" comes out. "Today" is the
|
|
4
|
+
* listener's day, never UTC's.
|
|
5
|
+
*
|
|
6
|
+
* Conventions (written down because a spoken weekday is ambiguous):
|
|
7
|
+
* - a bare weekday is the NEXT one strictly after today (on a Wednesday, "Wednesday" is in a week);
|
|
8
|
+
* - "this Friday" is the coming one, today included;
|
|
9
|
+
* - "next Friday" is that weekday of NEXT calendar week (Monday to Sunday);
|
|
10
|
+
* - "next week" is Monday of next week;
|
|
11
|
+
* - "later" (and "someday") means no date: the item's due date is cleared.
|
|
12
|
+
*/
|
|
13
|
+
import { localDate } from '../compose';
|
|
14
|
+
import { speakDate } from '../speak';
|
|
15
|
+
|
|
16
|
+
export interface SpokenWhen {
|
|
17
|
+
/** `YYYY-MM-DD`; null when `later`. */
|
|
18
|
+
date: string | null;
|
|
19
|
+
later: boolean;
|
|
20
|
+
/** How it is said back: `tomorrow`, `next week`, `Friday`. */
|
|
21
|
+
label: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const DAY_MS = 86_400_000;
|
|
25
|
+
const WEEKDAYS = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
|
|
26
|
+
const MONTHS = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october',
|
|
27
|
+
'november', 'december'];
|
|
28
|
+
const WORD_NUMBERS: Record<string, number> = {
|
|
29
|
+
a: 1, an: 1, one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9, ten: 10,
|
|
30
|
+
eleven: 11, twelve: 12, fourteen: 14,
|
|
31
|
+
};
|
|
32
|
+
const ORDINALS: Record<string, number> = {
|
|
33
|
+
first: 1, second: 2, third: 3, fourth: 4, fifth: 5, sixth: 6, seventh: 7, eighth: 8, ninth: 9, tenth: 10,
|
|
34
|
+
eleventh: 11, twelfth: 12, thirteenth: 13, fourteenth: 14, fifteenth: 15, sixteenth: 16, seventeenth: 17,
|
|
35
|
+
eighteenth: 18, nineteenth: 19, twentieth: 20, 'twenty first': 21, 'twenty second': 22, 'twenty third': 23,
|
|
36
|
+
'twenty fourth': 24, 'twenty fifth': 25, 'twenty sixth': 26, 'twenty seventh': 27, 'twenty eighth': 28,
|
|
37
|
+
'twenty ninth': 29, thirtieth: 30, 'thirty first': 31,
|
|
38
|
+
'twenty-first': 21, 'twenty-second': 22, 'twenty-third': 23, 'twenty-fourth': 24, 'twenty-fifth': 25,
|
|
39
|
+
'twenty-sixth': 26, 'twenty-seventh': 27, 'twenty-eighth': 28, 'twenty-ninth': 29, 'thirty-first': 31,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// -- calendar arithmetic on `YYYY-MM-DD` (UTC math: a date string has no zone) ---
|
|
43
|
+
|
|
44
|
+
const toUtc = (iso: string) => { const [y, m, d] = iso.split('-').map(Number); return Date.UTC(y, m - 1, d); };
|
|
45
|
+
const fromUtc = (ms: number) => new Date(ms).toISOString().slice(0, 10);
|
|
46
|
+
export const addDays = (iso: string, n: number) => fromUtc(toUtc(iso) + n * DAY_MS);
|
|
47
|
+
const dow = (iso: string) => new Date(toUtc(iso)).getUTCDay();
|
|
48
|
+
const mondayOf = (iso: string) => addDays(iso, -((dow(iso) + 6) % 7));
|
|
49
|
+
const isRealDate = (y: number, m: number, d: number) => {
|
|
50
|
+
const t = new Date(Date.UTC(y, m - 1, d));
|
|
51
|
+
return t.getUTCFullYear() === y && t.getUTCMonth() === m - 1 && t.getUTCDate() === d;
|
|
52
|
+
};
|
|
53
|
+
const pad = (n: number) => String(n).padStart(2, '0');
|
|
54
|
+
|
|
55
|
+
/** `YYYY-MM-DD` that is a real calendar day. */
|
|
56
|
+
export const isIsoDate = (value: unknown): value is string => {
|
|
57
|
+
if (typeof value !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
|
|
58
|
+
const [y, m, d] = value.split('-').map(Number);
|
|
59
|
+
return isRealDate(y, m, d);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export function weekdayName(iso: string): string {
|
|
63
|
+
const name = WEEKDAYS[dow(iso)];
|
|
64
|
+
return name[0].toUpperCase() + name.slice(1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** How a resolved date is said back: `today`, `tomorrow`, `Friday`, or `Monday the ninth`. */
|
|
68
|
+
export function spokenWhen(date: string, today: string): string {
|
|
69
|
+
if (date === today) return 'today';
|
|
70
|
+
if (date === addDays(today, 1)) return 'tomorrow';
|
|
71
|
+
const gap = (toUtc(date) - toUtc(today)) / DAY_MS;
|
|
72
|
+
if (gap > 1 && gap < 7) return weekdayName(date);
|
|
73
|
+
return speakDate(date, today);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// -- the phrases --------------------------------------------------------------
|
|
77
|
+
|
|
78
|
+
const WD = '(sunday|monday|tuesday|wednesday|thursday|friday|saturday)';
|
|
79
|
+
const MON = `(${MONTHS.join('|')})`;
|
|
80
|
+
const NUM = '(\\d{1,2}|a|an|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|fourteen)';
|
|
81
|
+
const ORD_WORDS = Object.keys(ORDINALS).filter((k) => !k.includes('-')).sort((a, b) => b.length - a.length).join('|');
|
|
82
|
+
const DAYNUM = `(\\d{1,2}(?:st|nd|rd|th)?|${ORD_WORDS})`;
|
|
83
|
+
|
|
84
|
+
interface Rule { re: RegExp; resolve(m: RegExpExecArray, today: string): SpokenWhen | null }
|
|
85
|
+
const on = (date: string, label: string): SpokenWhen => ({ date, later: false, label });
|
|
86
|
+
const LATER: SpokenWhen = { date: null, later: true, label: 'later' };
|
|
87
|
+
const dayNumber = (word: string) => ORDINALS[word.replace(/\s+/g, ' ')] ?? (parseInt(word, 10) || 0);
|
|
88
|
+
|
|
89
|
+
const RULES: Rule[] = [
|
|
90
|
+
{ re: /\bthe day after tomorrow\b/, resolve: (_m, t) => on(addDays(t, 2), spokenWhen(addDays(t, 2), t)) },
|
|
91
|
+
{ re: /\b(?:tomorrow|tmrw)\b/, resolve: (_m, t) => on(addDays(t, 1), 'tomorrow') },
|
|
92
|
+
{ re: /\blater (?:today|tonight|this (?:evening|afternoon))\b/, resolve: (_m, t) => on(t, 'today') },
|
|
93
|
+
{ re: /\blater (?:this|in the) week\b/, resolve: (_m, t) => fridayOf(t) },
|
|
94
|
+
{ re: /\b(?:tonight|today|this (?:evening|afternoon|morning))\b/, resolve: (_m, t) => on(t, 'today') },
|
|
95
|
+
{ re: /\b(?:end of (?:the |this )?week)\b/, resolve: (_m, t) => fridayOf(t) },
|
|
96
|
+
{ re: /\b(?:this weekend)\b/, resolve: (_m, t) => {
|
|
97
|
+
const delta = dow(t) === 0 ? 0 : (6 - dow(t) + 7) % 7;
|
|
98
|
+
return on(addDays(t, delta), 'this weekend');
|
|
99
|
+
} },
|
|
100
|
+
{ re: /\bnext weekend\b/, resolve: (_m, t) => on(addDays(mondayOf(t), 12), 'next weekend') },
|
|
101
|
+
{ re: /\b(?:next week|the week after(?: next)?)\b/, resolve: (_m, t) => on(addDays(mondayOf(t), 7), 'next week') },
|
|
102
|
+
{ re: /\bnext month\b/, resolve: (_m, t) => {
|
|
103
|
+
const [y, m] = t.split('-').map(Number);
|
|
104
|
+
const ny = m === 12 ? y + 1 : y;
|
|
105
|
+
return on(`${ny}-${pad(m === 12 ? 1 : m + 1)}-01`, 'next month');
|
|
106
|
+
} },
|
|
107
|
+
{ re: new RegExp(`\\bin ${NUM} (day|week)s?\\b`), resolve: (m, t) => {
|
|
108
|
+
const n = WORD_NUMBERS[m[1]] ?? parseInt(m[1], 10);
|
|
109
|
+
if (!n) return null;
|
|
110
|
+
const date = addDays(t, m[2] === 'week' ? n * 7 : n);
|
|
111
|
+
return on(date, spokenWhen(date, t));
|
|
112
|
+
} },
|
|
113
|
+
{ re: /\ba week from (?:today|now)\b/, resolve: (_m, t) => on(addDays(t, 7), spokenWhen(addDays(t, 7), t)) },
|
|
114
|
+
{ re: new RegExp(`\\b(?:(next|this|on) )?${WD}\\b`), resolve: (m, t) => {
|
|
115
|
+
const target = WEEKDAYS.indexOf(m[2]);
|
|
116
|
+
let date: string;
|
|
117
|
+
if (m[1] === 'next') date = addDays(mondayOf(t), 7 + ((target + 6) % 7));
|
|
118
|
+
else {
|
|
119
|
+
const delta = (target - dow(t) + 7) % 7;
|
|
120
|
+
date = addDays(t, delta === 0 && m[1] !== 'this' ? 7 : delta);
|
|
121
|
+
}
|
|
122
|
+
return on(date, m[1] === 'next' ? `next ${weekdayName(date)}` : spokenWhen(date, t));
|
|
123
|
+
} },
|
|
124
|
+
{ re: new RegExp(`\\b${MON} (?:the )?${DAYNUM}(?: of ${MON})?\\b`), resolve: (m, t) => monthDay(MONTHS.indexOf(m[1]) + 1, dayNumber(m[2]), t) },
|
|
125
|
+
{ re: new RegExp(`\\b(?:the )?${DAYNUM} of ${MON}\\b`), resolve: (m, t) => monthDay(MONTHS.indexOf(m[2]) + 1, dayNumber(m[1]), t) },
|
|
126
|
+
// "the 12th", "the third": a bare day of the month, but never "the third one".
|
|
127
|
+
{ re: new RegExp(`\\bthe ${DAYNUM}(?!\\s*(?:one|thing|item|task|to ?do|on)\\b)(?=\\s*$)`), resolve: (m, t) => {
|
|
128
|
+
const day = dayNumber(m[1]);
|
|
129
|
+
const [y, mo, d] = t.split('-').map(Number);
|
|
130
|
+
let year = y; let month = mo;
|
|
131
|
+
if (day <= d) { month += 1; if (month > 12) { month = 1; year += 1; } }
|
|
132
|
+
return isRealDate(year, month, day) ? on(`${year}-${pad(month)}-${pad(day)}`, spokenWhen(`${year}-${pad(month)}-${pad(day)}`, t)) : null;
|
|
133
|
+
} },
|
|
134
|
+
{ re: /\b(?:some ?time|some ?day|some other time|another time|next time|not now|not today|whenever|eventually|at some point|later)\b/, resolve: () => LATER },
|
|
135
|
+
];
|
|
136
|
+
|
|
137
|
+
function fridayOf(today: string): SpokenWhen {
|
|
138
|
+
const d = dow(today);
|
|
139
|
+
const date = d >= 1 && d <= 5 ? addDays(today, 5 - d) : addDays(today, d === 6 ? 6 : 5);
|
|
140
|
+
return on(date, d === 5 ? 'today' : spokenWhen(date, today));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function monthDay(month: number, day: number, today: string): SpokenWhen | null {
|
|
144
|
+
const [y, , ] = today.split('-').map(Number);
|
|
145
|
+
if (!month || !day) return null;
|
|
146
|
+
let year = y;
|
|
147
|
+
if (!isRealDate(year, month, day)) return null;
|
|
148
|
+
let date = `${year}-${pad(month)}-${pad(day)}`;
|
|
149
|
+
if (date < today) { year += 1; if (!isRealDate(year, month, day)) return null; date = `${year}-${pad(month)}-${pad(day)}`; }
|
|
150
|
+
return on(date, spokenWhen(date, today));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Lowercase, hyphens and stray punctuation to spaces: how the rules above expect text. */
|
|
154
|
+
export function normalizeSpeech(text: string): string {
|
|
155
|
+
return text.toLowerCase().replace(/[‘’]/g, "'").replace(/[^a-z0-9'\s-]/g, ' ').replace(/\s+/g, ' ').trim();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export interface FoundWhen { when: SpokenWhen; start: number; end: number }
|
|
159
|
+
|
|
160
|
+
/** The leftmost (then longest) date phrase in `text`, with where it sits. */
|
|
161
|
+
export function findWhen(text: string, now: number, timeZone: string): FoundWhen | null {
|
|
162
|
+
const today = localDate(now, timeZone);
|
|
163
|
+
// Callers pass text already through `normalizeSpeech`, so indexes here are indexes into it.
|
|
164
|
+
const lower = text.toLowerCase().replace(/-/g, ' ').replace(/\s+/g, ' ').trim();
|
|
165
|
+
let best: { m: RegExpExecArray; rule: Rule } | null = null;
|
|
166
|
+
for (const rule of RULES) {
|
|
167
|
+
const m = rule.re.exec(lower);
|
|
168
|
+
if (!m) continue;
|
|
169
|
+
if (!best || m.index < best.m.index || (m.index === best.m.index && m[0].length > best.m[0].length)) best = { m, rule };
|
|
170
|
+
}
|
|
171
|
+
if (!best) return null;
|
|
172
|
+
const when = best.rule.resolve(best.m, today);
|
|
173
|
+
return when ? { when, start: best.m.index, end: best.m.index + best.m[0].length } : null;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Resolve a phrase that should be ONLY a date ("next friday"); null when it is not one. */
|
|
177
|
+
export function parseWhen(phrase: string, now: number, timeZone: string): SpokenWhen | null {
|
|
178
|
+
const text = normalizeSpeech(phrase).replace(/^(?:to|until|till|for|on|by|out to|up to|back to)\s+/, '');
|
|
179
|
+
if (isIsoDate(text as unknown)) return on(text, spokenWhen(text, localDate(now, timeZone)));
|
|
180
|
+
const found = findWhen(text, now, timeZone);
|
|
181
|
+
if (!found) return null;
|
|
182
|
+
const spans = text.replace(/-/g, ' ');
|
|
183
|
+
const rest = (spans.slice(0, found.start) + spans.slice(found.end)).replace(/\b(?:the|a|an|to|on|at|by|in|of|this)\b/g, ' ').trim();
|
|
184
|
+
return rest ? null : found.when;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** `text` with the date phrase cut out, plus the phrase's meaning. */
|
|
188
|
+
export function splitWhen(text: string, now: number, timeZone: string): { rest: string; when: SpokenWhen; after: string } | null {
|
|
189
|
+
const clean = normalizeSpeech(text).replace(/-/g, ' ');
|
|
190
|
+
const found = findWhen(clean, now, timeZone);
|
|
191
|
+
if (!found) return null;
|
|
192
|
+
const before = clean.slice(0, found.start).replace(/\s*(?:to|until|till|for|on|by|due|before|at|out to|up to|back to|into|in|from)\s*$/, '').trim();
|
|
193
|
+
const after = clean.slice(found.end).trim();
|
|
194
|
+
return { rest: `${before} ${after}`.replace(/\s+/g, ' ').trim(), when: found.when, after };
|
|
195
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** One line saying what was understood, shown BEFORE anything is applied. */
|
|
2
|
+
import { localDate } from '../compose';
|
|
3
|
+
import { speakable } from '../speak';
|
|
4
|
+
import { spokenWhen } from './dates';
|
|
5
|
+
import type { IntentContext, VoiceAction } from './types';
|
|
6
|
+
|
|
7
|
+
export const PRIORITY_NAMES = ['critical', 'urgent', 'medium', 'low', 'lowest'];
|
|
8
|
+
|
|
9
|
+
export function describeActions(actions: VoiceAction[], ctx: IntentContext): string {
|
|
10
|
+
const today = localDate(ctx.now, ctx.timeZone);
|
|
11
|
+
const titles = new Map([...ctx.briefed, ...ctx.open].map((i) => [i.id, speakable(i.title, 8)] as const));
|
|
12
|
+
const name = (id: string) => titles.get(id) ?? 'that item';
|
|
13
|
+
return actions.map((a) => {
|
|
14
|
+
switch (a.type) {
|
|
15
|
+
case 'defer': return `Move ${name(a.itemId)} to ${a.to === 'later' ? 'later' : a.label ?? spokenWhen(a.to, today)}`;
|
|
16
|
+
case 'pull_today': return `Pull ${name(a.itemId)} into today`;
|
|
17
|
+
case 'done': return `Mark ${name(a.itemId)} done`;
|
|
18
|
+
case 'drop': return `Drop ${name(a.itemId)}`;
|
|
19
|
+
case 'set_priority': return `Make ${name(a.itemId)} ${PRIORITY_NAMES[a.priority] ?? 'priority ' + a.priority}`;
|
|
20
|
+
case 'add_item': return `Add ${a.title}${a.dueDate ? ` for ${spokenWhen(a.dueDate, today)}` : ''}`;
|
|
21
|
+
case 'undo': return 'Undo the last change';
|
|
22
|
+
case 'clarify': return a.question;
|
|
23
|
+
default: return '';
|
|
24
|
+
}
|
|
25
|
+
}).filter(Boolean).join('. ');
|
|
26
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE DETERMINISTIC PARSER: the reply grammar that needs no model.
|
|
3
|
+
*
|
|
4
|
+
* It is the fallback when the model is down, slow or wrong, and it is also a
|
|
5
|
+
* check on the model's output (the same actions must be reachable from plain
|
|
6
|
+
* rules). It covers the verbs people actually say: done, later, tomorrow, today,
|
|
7
|
+
* drop, undo, move/push to a date, pull up to today, make it urgent, add X. A
|
|
8
|
+
* reply it cannot place gets a spoken "say that again", never a guess.
|
|
9
|
+
*
|
|
10
|
+
* ORDER MATTERS and is the file's contract: undo, add, priority, pull/defer,
|
|
11
|
+
* "do it today", done, drop, then a bare date ("next week") for the item being
|
|
12
|
+
* read. Several actions in one reply ("done with this, and move the next one to
|
|
13
|
+
* tomorrow") split at "and" ONLY where both halves are actions in their own right.
|
|
14
|
+
*/
|
|
15
|
+
import { localDate } from '../compose';
|
|
16
|
+
import { normalizeSpeech, splitWhen, type SpokenWhen } from './dates';
|
|
17
|
+
import { describeActions } from './describe';
|
|
18
|
+
import { contentWords } from './match';
|
|
19
|
+
import { resolveTarget } from './target';
|
|
20
|
+
import type { IntentContext, IntentResult, VoiceAction } from './types';
|
|
21
|
+
|
|
22
|
+
export const REPEAT_QUESTION = 'Sorry, I did not catch that. Can you say that again?';
|
|
23
|
+
|
|
24
|
+
const FILLER = /^(?:(?:hey|ok|okay|um|uh|so|well|actually|please|yeah|yes|alright|no wait|wait|oh|hmm|like|can you|could you|would you|will you|i want to|i would like to|i'd like to|let's|lets|i need you to|go ahead and|just|and|then|what about|how about)\b[,\s]*)+/;
|
|
25
|
+
/** ", I am not doing that today": a reason, not part of the instruction. */
|
|
26
|
+
const REASON = /(?<=\S)\s*[,;]\s*(?:because|since|cause|i(?:'m| am| can't| cannot| won't| will not| don't| do not| have no| already| just)\b|it's|it is|that's|that is|there's)\b.*$/;
|
|
27
|
+
const SPLIT = /\s*(?:,\s*)?\b(?:and then|and also|and|then|also|after that)\b\s+/;
|
|
28
|
+
|
|
29
|
+
const UNDO = /^(?:undo(?: that| it| the last(?: one| thing| change)?)?|never ?mind(?: that| it)?|cancel that|revert(?: that| it)?|take (?:that|it) back|scratch that|go back|put (?:it|that|them|this) back|reverse (?:that|it)|oops)$/;
|
|
30
|
+
const ADD = /^(?:add|create|new task|new to ?do|remind me to|remember to|note to self|don't let me forget to|dont let me forget to)\s+(.+)$/;
|
|
31
|
+
const PRIORITY_VERB = /\b(?:make|mark|set|flag|bump|change|label|put|rate|prioriti[sz]e)\b/;
|
|
32
|
+
const PULL_VERB = /\b(?:pull|bring)\b/;
|
|
33
|
+
const DEFER_VERB = /\b(?:move|push(?:ed)?|bump|postpone|delay|reschedule|snooze|defer|punt|shift|kick|slide|put off|put out|remind me (?:about|of))\b/;
|
|
34
|
+
const DO_TODAY = /^(?:i(?:'ll| will| am going to| gonna)? )?(?:do|work on|tackle|start|get to)\s+(.*?)\s*\b(?:today|tonight|now|this afternoon|this evening)$/;
|
|
35
|
+
const DONE_VERB = /\b(?:done|finished|finish|completed?|did|handled|sorted|wrapped up|knocked out|crossed off|cross off|checked off|check off)\b/;
|
|
36
|
+
const DROP_VERB = /\b(?:drop|delete|remove|cancel|scrap|ditch|kill|forget(?: about)?|get rid of|toss|trash|skip)\b/;
|
|
37
|
+
|
|
38
|
+
const LEVELS: Array<[RegExp, number]> = [
|
|
39
|
+
[/\b(?:lowest(?: priority)?|backlog|back burner)\b/, 4],
|
|
40
|
+
[/\b(?:critical|highest(?: priority)?|drop everything|p0)\b/, 0],
|
|
41
|
+
[/\b(?:urgent|high priority|top priority|important|asap|p1)\b/, 1],
|
|
42
|
+
[/\b(?:medium(?: priority)?|normal(?: priority)?|regular)\b/, 2],
|
|
43
|
+
[/\b(?:low priority|low|minor)\b/, 3],
|
|
44
|
+
];
|
|
45
|
+
const PRIORITY_NUMBER: Record<string, number> = { zero: 0, one: 1, two: 2, three: 3, four: 4, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4 };
|
|
46
|
+
const PRIORITY_NUM_RE = /\bpriority (?:to |of |level )?(zero|one|two|three|four|[0-4])\b/;
|
|
47
|
+
|
|
48
|
+
type Pending = { actions: VoiceAction[] };
|
|
49
|
+
const ask = (question: string): VoiceAction => ({ type: 'clarify', question });
|
|
50
|
+
|
|
51
|
+
/** Does this clause hold an action of its own? (Decides where "and" splits.) */
|
|
52
|
+
function isAction(clause: string): boolean {
|
|
53
|
+
const n = normalizeSpeech(clause).replace(FILLER, '');
|
|
54
|
+
if (!n) return false;
|
|
55
|
+
return UNDO.test(n) || ADD.test(n) || DONE_VERB.test(n) || DROP_VERB.test(n) || PULL_VERB.test(n) || DEFER_VERB.test(n)
|
|
56
|
+
|| DO_TODAY.test(n) || (PRIORITY_VERB.test(n) && (LEVELS.some(([re]) => re.test(n)) || PRIORITY_NUM_RE.test(n)));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function clauses(raw: string): string[] {
|
|
60
|
+
const lead = normalizeSpeech(raw).replace(FILLER, '');
|
|
61
|
+
if (ADD.test(lead)) return [raw]; // "add buy salt and pepper": the "and" is part of the title
|
|
62
|
+
const parts = raw.split(SPLIT).filter((p) => p.trim());
|
|
63
|
+
const out: string[] = [];
|
|
64
|
+
for (const part of parts) {
|
|
65
|
+
const last = out[out.length - 1];
|
|
66
|
+
if (last !== undefined && isAction(last) && isAction(part)) out.push(part);
|
|
67
|
+
else if (last !== undefined) out[out.length - 1] = `${last} and ${part}`;
|
|
68
|
+
else out.push(part);
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const title = (s: string) => { const t = s.trim().replace(/[.,;:!?]+$/, ''); return t ? t[0].toUpperCase() + t.slice(1) : t; };
|
|
74
|
+
|
|
75
|
+
function parseClause(clause: string, ctx: IntentContext, reading: string | null): Pending | null {
|
|
76
|
+
const n = normalizeSpeech(clause.replace(REASON, '')).replace(FILLER, '').trim();
|
|
77
|
+
if (!n) return null;
|
|
78
|
+
const today = localDate(ctx.now, ctx.timeZone);
|
|
79
|
+
|
|
80
|
+
if (UNDO.test(n)) {
|
|
81
|
+
return { actions: [ctx.hasUndo ? { type: 'undo' } : ask('There is nothing to undo.')] };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const add = ADD.exec(n);
|
|
85
|
+
if (add) {
|
|
86
|
+
const body = add[1].replace(/^(?:a |an |the )?(?:new )?(?:task|to ?do|item|reminder)(?: to| called| named| for|:)?\s+/, '')
|
|
87
|
+
.replace(/\s+(?:to|on|in) (?:my|the) (?:list|to ?dos?|tasks?)$/, '');
|
|
88
|
+
const split = splitWhen(body, ctx.now, ctx.timeZone);
|
|
89
|
+
// A date only counts as the due date when it ends the sentence ("... for tomorrow"), not mid-title.
|
|
90
|
+
const dated = split !== null && split.after === '';
|
|
91
|
+
const text = title(dated && split ? split.rest : body);
|
|
92
|
+
if (!text) return { actions: [ask('What should I add?')] };
|
|
93
|
+
return { actions: [{ type: 'add_item', title: text, dueDate: dated && split && split.when.date ? split.when.date : null }] };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// PRIORITY
|
|
97
|
+
const level = LEVELS.find(([re]) => re.test(n));
|
|
98
|
+
const numbered = PRIORITY_NUM_RE.exec(n);
|
|
99
|
+
if ((PRIORITY_VERB.test(n) || numbered) && (level || numbered)) {
|
|
100
|
+
const priority = numbered ? PRIORITY_NUMBER[numbered[1]] : (level as [RegExp, number])[1];
|
|
101
|
+
const phrase = n.replace(numbered ? PRIORITY_NUM_RE : (level as [RegExp, number])[0], ' ')
|
|
102
|
+
.replace(/\b(?:make|mark|set|flag|bump|change|label|put|rate|prioriti[sz]e|as|to|priority|a|an|level|of|is|be|its|it's)\b/g, ' ');
|
|
103
|
+
const target = resolveTarget(phrase, ctx, reading);
|
|
104
|
+
if (!target.ok) return { actions: [ask(target.question)] };
|
|
105
|
+
return { actions: [{ type: 'set_priority', itemId: target.item.id, priority }] };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// PULL / DEFER with a date
|
|
109
|
+
const pull = PULL_VERB.test(n);
|
|
110
|
+
const defer = DEFER_VERB.test(n) || (/\bput\b/.test(n) && / (?:off|out)\b/.test(n));
|
|
111
|
+
if (pull || defer || (/\bput\b/.test(n) && splitWhen(n, ctx.now, ctx.timeZone))) {
|
|
112
|
+
const split = splitWhen(n, ctx.now, ctx.timeZone);
|
|
113
|
+
const phrase = (split ? split.rest : n)
|
|
114
|
+
.replace(/\b(?:move|pushed|push|bump|postpone|delay|reschedule|snooze|defer|punt|shift|kick|slide|put off|put out|put|pull|bring|remind me about|remind me of|out|up|off|back|into|to|for|until|till|it|them)\b/g, ' ');
|
|
115
|
+
const target = resolveTarget(phrase, ctx, reading);
|
|
116
|
+
if (!target.ok) return { actions: [ask(target.question)] };
|
|
117
|
+
let when: SpokenWhen | null = split ? split.when : null;
|
|
118
|
+
if (!when && /\bsnooze\b|\b(?:push|put) (?:it |this |that )?(?:out|off)\b/.test(n)) when = { date: null, later: true, label: 'later' };
|
|
119
|
+
if (!when && pull) when = { date: today, later: false, label: 'today' };
|
|
120
|
+
if (!when) return { actions: [ask('Move it to when?')] };
|
|
121
|
+
return { actions: [deferTo(target.item.id, when, today)] };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const doToday = DO_TODAY.exec(n);
|
|
125
|
+
if (doToday) {
|
|
126
|
+
const target = resolveTarget(doToday[1], ctx, reading);
|
|
127
|
+
if (!target.ok) return { actions: [ask(target.question)] };
|
|
128
|
+
return { actions: [{ type: 'pull_today', itemId: target.item.id, label: 'today' }] };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (DONE_VERB.test(n)) {
|
|
132
|
+
const phrase = n.replace(/\b(?:mark|as|is|are|was|been|has|have|i|ive|i've|already|just|with|done|finished|finish|completed?|did|handled|sorted|wrapped up|knocked out|crossed off|cross off|checked off|check off|off|its|it's|that's|thats)\b/g, ' ');
|
|
133
|
+
const target = resolveTarget(phrase, ctx, reading);
|
|
134
|
+
if (!target.ok) return { actions: [ask(target.question)] };
|
|
135
|
+
return { actions: [{ type: 'done', itemId: target.item.id }] };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (DROP_VERB.test(n)) {
|
|
139
|
+
const phrase = n.replace(/\b(?:drop|delete|remove|cancel|scrap|ditch|kill|forget about|forget|get rid of|toss|trash|skip|i|am|never|doing|just)\b/g, ' ');
|
|
140
|
+
const target = resolveTarget(phrase, ctx, reading);
|
|
141
|
+
if (!target.ok) return { actions: [ask(target.question)] };
|
|
142
|
+
return { actions: [{ type: 'drop', itemId: target.item.id }] };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// A bare date ("next week", "tomorrow", "not today") means: put the item being read there.
|
|
146
|
+
const bare = splitWhen(n, ctx.now, ctx.timeZone);
|
|
147
|
+
if (bare && !contentWords(bare.rest.replace(/\b(?:until|till|make|do|can|wait|will|should|be)\b/g, ' ')).length) {
|
|
148
|
+
const target = resolveTarget('', ctx, reading);
|
|
149
|
+
if (!target.ok) return { actions: [ask(target.question)] };
|
|
150
|
+
return { actions: [deferTo(target.item.id, bare.when, today)] };
|
|
151
|
+
}
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function deferTo(itemId: string, when: SpokenWhen, today: string): VoiceAction {
|
|
156
|
+
if (!when.later && when.date === today) return { type: 'pull_today', itemId, label: 'today' };
|
|
157
|
+
return { type: 'defer', itemId, to: when.later ? 'later' : (when.date as string), label: when.label };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function parseWithGrammar(ctx: IntentContext): IntentResult {
|
|
161
|
+
const raw = ctx.utterance.toLowerCase().replace(/[‘’]/g, "'").trim();
|
|
162
|
+
const actions: VoiceAction[] = [];
|
|
163
|
+
let reading = ctx.readingId;
|
|
164
|
+
for (const clause of clauses(raw)) {
|
|
165
|
+
const parsed = parseClause(clause, ctx, reading);
|
|
166
|
+
if (!parsed) {
|
|
167
|
+
if (!actions.length) actions.push(ask(REPEAT_QUESTION));
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
actions.push(...parsed.actions);
|
|
171
|
+
if (parsed.actions.some((a) => a.type === 'clarify')) break; // act on what was clear, ask about the rest
|
|
172
|
+
}
|
|
173
|
+
const final = actions.length ? actions : [ask(REPEAT_QUESTION)];
|
|
174
|
+
return { actions: final, source: 'grammar', understood: describeActions(final, ctx), notes: [] };
|
|
175
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GROUNDING: is each item the model picked one the person actually pointed at?
|
|
3
|
+
*
|
|
4
|
+
* A small model acts when it should ask: with nothing being read it defers the
|
|
5
|
+
* first item on "later", or every item; it takes "pull" in "pull it up" for
|
|
6
|
+
* "pull request". So an action is kept only when its item is
|
|
7
|
+
* - the item being read, and the reply names no other item; or
|
|
8
|
+
* - the one the words describe (title, project or ticket code), uniquely; or
|
|
9
|
+
* - what "the next / previous / third / other one" structurally means.
|
|
10
|
+
* Anything else is a guess, and a guess becomes the grammar's spoken question.
|
|
11
|
+
*/
|
|
12
|
+
import { localDate } from '../compose';
|
|
13
|
+
import { splitWhen } from './dates';
|
|
14
|
+
import { contentWords, words } from './match';
|
|
15
|
+
import type { IntentContext, IntentItem, VoiceAction } from './types';
|
|
16
|
+
|
|
17
|
+
/** Words that are the instruction, not the description of an item. */
|
|
18
|
+
const COMMAND = new Set(['move', 'push', 'pull', 'bring', 'mark', 'make', 'set', 'flag', 'done', 'drop', 'delete', 'remove',
|
|
19
|
+
'cancel', 'scrap', 'finish', 'finished', 'complete', 'completed', 'snooze', 'delay', 'postpone', 'defer', 'reschedule',
|
|
20
|
+
'later', 'today', 'tomorrow', 'tonight', 'week', 'weekend', 'month', 'day', 'days', 'weeks', 'next', 'monday', 'tuesday',
|
|
21
|
+
'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'priority', 'urgent', 'critical', 'low', 'lowest', 'high',
|
|
22
|
+
'medium', 'important', 'undo', 'add', 'remind', 'forget', 'kill', 'hold', 'wait', 'yeah', 'actually', 'okay', 'ok',
|
|
23
|
+
'complete', 'finish', 'bump', 'shift', 'put', 'get', 'rid', 'doing', 'never', 'want', 'dont', 'first', 'second', 'third',
|
|
24
|
+
'fourth', 'fifth', 'previous', 'last', 'january', 'february', 'march', 'april', 'june', 'july', 'august', 'september',
|
|
25
|
+
'october', 'november', 'december', 'sometime', 'time', 'tmrw', 'asap']);
|
|
26
|
+
|
|
27
|
+
const same = (a: string, b: string) => a === b || (a.length >= 4 && b.length >= 4 && a.slice(0, 4) === b.slice(0, 4));
|
|
28
|
+
const ORDINALS: Record<string, number> = { first: 1, second: 2, third: 3, fourth: 4, fifth: 5, sixth: 6, seventh: 7, eighth: 8 };
|
|
29
|
+
|
|
30
|
+
/** How many of the person's descriptive words fit this item (title 1, project 0.6, ticket code 1). */
|
|
31
|
+
function fit(spoken: string[], item: IntentItem): number {
|
|
32
|
+
const title = contentWords(item.title);
|
|
33
|
+
const focus = contentWords(item.focusName ?? '');
|
|
34
|
+
const code = words(item.linearIdentifier ?? '');
|
|
35
|
+
let sum = 0;
|
|
36
|
+
for (const w of spoken) {
|
|
37
|
+
if (title.some((t) => same(t, w)) || code.includes(w)) sum += 1;
|
|
38
|
+
else if (focus.some((f) => same(f, w))) sum += 0.6;
|
|
39
|
+
}
|
|
40
|
+
return sum;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** null when every item is grounded; otherwise why not. */
|
|
44
|
+
export function ungrounded(actions: VoiceAction[], ctx: IntentContext): string | null {
|
|
45
|
+
// A move needs a date in the reply (or a word that means "later"); a model that supplies one is guessing.
|
|
46
|
+
const said = splitWhen(ctx.utterance, ctx.now, ctx.timeZone);
|
|
47
|
+
const dated = said !== null;
|
|
48
|
+
const laterWord = /\b(?:snooze|postpone|delay|defer|can wait|could wait|let it wait|put off|hold off|push (?:it |this |that )?out|punt|kick)\b/i.test(ctx.utterance);
|
|
49
|
+
for (const a of actions) {
|
|
50
|
+
if (a.type === 'defer' && !dated && !(a.to === 'later' && laterWord)) return 'no date was said';
|
|
51
|
+
// The date the model chose must be the date the words say.
|
|
52
|
+
if (a.type === 'defer' && said && (said.when.later ? 'later' : said.when.date) !== a.to) return 'the date disagrees with the words';
|
|
53
|
+
if (a.type === 'pull_today' && said && !said.when.later && said.when.date !== localDate(ctx.now, ctx.timeZone)) return 'the date disagrees with the words';
|
|
54
|
+
}
|
|
55
|
+
const targeted = actions.filter((a): a is Extract<VoiceAction, { itemId: string }> => 'itemId' in a);
|
|
56
|
+
if (!targeted.length) return null;
|
|
57
|
+
const all = [...ctx.briefed, ...ctx.open.filter((o) => !ctx.briefed.some((b) => b.id === o.id))];
|
|
58
|
+
const text = words(ctx.utterance);
|
|
59
|
+
const spoken = text.filter((w) => contentWords(w).length && !COMMAND.has(w));
|
|
60
|
+
const idx = ctx.readingId ? ctx.briefed.findIndex((i) => i.id === ctx.readingId) : -1;
|
|
61
|
+
const briefedIds = new Set(ctx.briefed.map((i) => i.id));
|
|
62
|
+
|
|
63
|
+
const structural = new Set<string>();
|
|
64
|
+
const asks = (w: string) => new RegExp(`\\b${w} (?:one|item|thing|task)\\b`).test(ctx.utterance.toLowerCase());
|
|
65
|
+
if (asks('next') && idx >= 0 && ctx.briefed[idx + 1]) structural.add(ctx.briefed[idx + 1].id);
|
|
66
|
+
if ((asks('previous') || asks('one before')) && idx > 0) structural.add(ctx.briefed[idx - 1].id);
|
|
67
|
+
if (asks('last')) structural.add(ctx.briefed[ctx.briefed.length - 1]?.id ?? '');
|
|
68
|
+
for (const w of text) if (ORDINALS[w] && /\b(?:one|item|thing)\b/.test(ctx.utterance.toLowerCase()) && ctx.briefed[ORDINALS[w] - 1]) structural.add(ctx.briefed[ORDINALS[w] - 1].id);
|
|
69
|
+
const other = asks('other');
|
|
70
|
+
const deictic = /\b(?:this|that|it|these|those)\b/.test(ctx.utterance.toLowerCase());
|
|
71
|
+
|
|
72
|
+
const scored = all.map((item) => ({ item, fit: fit(spoken, item) })).filter((s) => s.fit > 0);
|
|
73
|
+
const top = scored.length ? Math.max(...scored.map((s) => s.fit)) : 0;
|
|
74
|
+
const best = scored.filter((s) => s.fit >= top - 1e-9).map((s) => s.item.id);
|
|
75
|
+
const targetedIds = new Set(targeted.map((a) => a.itemId));
|
|
76
|
+
|
|
77
|
+
for (const a of targeted) {
|
|
78
|
+
const id = a.itemId;
|
|
79
|
+
if (structural.has(id)) continue;
|
|
80
|
+
if (other && !briefedIds.has(id)) {
|
|
81
|
+
if (ctx.open.filter((o) => !briefedIds.has(o.id)).length > 1 && !scored.some((s) => s.item.id === id)) return `"${id}" is a guess among several other items`;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (scored.length) {
|
|
85
|
+
if (!scored.some((s) => s.item.id === id)) return `"${id}" is not what the words describe`;
|
|
86
|
+
if (best.length > 1 && best.includes(id) && !best.every((b) => targetedIds.has(b))) return 'the words fit several items equally';
|
|
87
|
+
if (targeted.length === 1 && !best.includes(id)) return `"${id}" fits worse than another item`;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (id === ctx.readingId && (deictic || !(structural.size || other))) continue;
|
|
91
|
+
if (structural.size || other) return `"${id}" is not the next/previous/other item`;
|
|
92
|
+
if (id !== ctx.readingId) return ctx.readingId ? `"${id}" is not the item being read` : 'nothing is being read';
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { resolveVoiceIntent } from './resolve';
|
|
2
|
+
export { parseWithGrammar, REPEAT_QUESTION } from './grammar';
|
|
3
|
+
export { parseWhen, splitWhen, spokenWhen, isIsoDate, addDays } from './dates';
|
|
4
|
+
export type { SpokenWhen } from './dates';
|
|
5
|
+
export { readingAt, charPositionOf, CHARS_PER_SECOND } from './position';
|
|
6
|
+
export type { PlaybackPosition } from './position';
|
|
7
|
+
export { describeActions, PRIORITY_NAMES } from './describe';
|
|
8
|
+
export { intentPrompt, intentSystemPrompt, checkModelReply } from './model';
|
|
9
|
+
export { matchItems, scoreItems } from './match';
|
|
10
|
+
export { resolveTarget, whichOne } from './target';
|
|
11
|
+
export type { IntentContext, IntentItem, IntentOptions, IntentResult, VoiceAction } from './types';
|