@modelform/react-native 0.14.0 → 0.15.0

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/README.md CHANGED
@@ -157,9 +157,13 @@ back as it was before the fill. Listening is `continuous`, so pauses don't cut i
157
157
  (expo-speech-recognition takes `continuous: true`) and deliver the whole transcript as one final `onText` when it
158
158
  stops.
159
159
 
160
- What was said goes to a `FormFiller`, `(form, said) => Promise<FillResult>`. The default, `ruleFiller`, needs no model:
161
- a value is what follows a field's name, and fields not named are found by unit ("72.5 kilos") or by a choice only one
162
- field has. To use a model, wrap whatever generates text in `modelFiller`; it prompts with `fillPrompt`, passes
160
+ What was said goes to a `FormFiller`, `(form, said) => Promise<FillResult>`. The default, `ruleFiller`, needs no model.
161
+ A named field takes the value said next to it, and it finds names in the forms people say them in ("slept", "weighed",
162
+ "meditation") plus a few everyday synonyms ("drink" for Alcohol, "jog" for Run, "feeling" for Mood). That covers
163
+ "slept badly, maybe five hours", "didn't drink", "no meditation", "mood pretty low" and "in a good mood". If a field
164
+ is named twice, the last value wins ("mood 3, no wait, 4"). Fields not named are found by what only they could take: a
165
+ number with their unit ("72.5 kilos", "5k"), a time when only one field takes times ("for an hour and a half" fills a
166
+ minutes field with 90), or a choice only one field has ("went for a jog"). It leaves a field empty rather than guess. To use a model, wrap whatever generates text in `modelFiller`; it prompts with `fillPrompt`, passes
163
167
  `fillSchema` for guided generation, reads the reply with `readFill` (so a model can only enter what typing could), and
164
168
  with a fallback fills what the model left out:
165
169
 
@@ -382,8 +382,8 @@ function interpolate(text, ctx) {
382
382
  const builtins = builtinValues(ctx);
383
383
  return text.replace(
384
384
  /\$(\w+)|\$\{(\w+)(?::(\w+))?\}|\[\[(\w+)(?::(\w+))?\]\]/g,
385
- (match, plain, braced, bracedFmt, old, oldFmt) => {
386
- const name = plain ?? braced ?? old;
385
+ (match, plain2, braced, bracedFmt, old, oldFmt) => {
386
+ const name = plain2 ?? braced ?? old;
387
387
  const format3 = bracedFmt ?? oldFmt;
388
388
  const scoped = ctx.scoped?.[name];
389
389
  const value = scoped ? scoped.value : ctx.values[name];
@@ -1694,7 +1694,10 @@ function parseSpokenDuration(text) {
1694
1694
  return clock[1] != null ? h * 3600 + m * 60 + s + Number(`0.${f}`) : m * 60 + s + Number(`0.${f}`);
1695
1695
  }
1696
1696
  }
1697
- const lower = text.toLowerCase().replace(/\ban hour\b/g, "one hour").replace(/\ba minute\b/g, "one minute").replace(/(\S+) (hours?|minutes?) and a half/g, "$1 and a half $2");
1697
+ const lower = text.toLowerCase().replace(
1698
+ /\b(?:(?:a|one|three) )?(half|quarters?)(?: of)? an? (hour|minute)\b/g,
1699
+ (m, part, unit) => `${part === "half" ? 0.5 : m.startsWith("three") ? 0.75 : 0.25} ${unit}`
1700
+ ).replace(/\ban hour\b/g, "one hour").replace(/\ba minute\b/g, "one minute").replace(/(\S+) (hours?|minutes?) and a half/g, "$1 and a half $2");
1698
1701
  const parts = lower.split(/\b(hours?|hrs?|minutes?|mins?|seconds?|secs?)\b/);
1699
1702
  const scale = (unit) => unit.startsWith("h") ? 3600 : unit.startsWith("m") ? 60 : 1;
1700
1703
  let total = 0;
@@ -1954,66 +1957,6 @@ function readFill(form, answer) {
1954
1957
  }
1955
1958
  return result;
1956
1959
  }
1957
- var UNIT_WORDS = {
1958
- kg: ["kg", "kgs", "kilo", "kilos", "kilogram", "kilograms"],
1959
- lb: ["lb", "lbs", "pound", "pounds"],
1960
- h: ["h", "hr", "hrs", "hour", "hours"],
1961
- min: ["min", "mins", "minute", "minutes"],
1962
- s: ["s", "sec", "secs", "second", "seconds"],
1963
- km: ["km", "kms", "kilometer", "kilometers", "kilometre", "kilometres", "k"],
1964
- mi: ["mi", "mile", "miles"],
1965
- m: ["m", "meter", "meters", "metre", "metres"]
1966
- };
1967
- var NUMBER_WORD = "(?:-?\\d+(?:[.,]\\d+)?|zero|oh|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred|thousand|point|and|a|half|quarter|minus)";
1968
- var escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1969
- function namesOf(f) {
1970
- const names = /* @__PURE__ */ new Set([f.label.toLowerCase(), f.key.toLowerCase().replace(/[-_]+/g, " ")]);
1971
- return [...names].filter((n) => n.trim().length > 1);
1972
- }
1973
- var LEAD = /^[\s,:;.-]*(?:(?:is|was|were|of|at|about|around|to|=)\b[\s,:;.-]*)*/i;
1974
- function ruleFill(form, said) {
1975
- const lower = said.toLowerCase();
1976
- const hits = [];
1977
- for (const f of form.fields)
1978
- for (const name of namesOf(f)) {
1979
- const re = new RegExp(`(?<![\\p{L}\\p{N}])${escape(name)}(?![\\p{L}\\p{N}])`, "gu");
1980
- for (const m of lower.matchAll(re)) {
1981
- const at = m.index;
1982
- const end = at + m[0].length;
1983
- if (!hits.some((h) => at < h.end && end > h.at)) hits.push({ key: f.key, at, end });
1984
- }
1985
- }
1986
- hits.sort((a, b) => a.at - b.at);
1987
- const segments = {};
1988
- const used = [];
1989
- hits.forEach((h, i) => {
1990
- const stop = i + 1 < hits.length ? hits[i + 1].at : said.length;
1991
- if (h.key in segments) return;
1992
- segments[h.key] = said.slice(h.end, stop).replace(LEAD, "").replace(/[\s,;.]+$/, "");
1993
- used.push([h.at, stop]);
1994
- });
1995
- const answer = {};
1996
- for (const f of form.fields) if (segments[f.key]) answer[f.key] = segments[f.key];
1997
- const rest = used.reduceRight((text, [a, b]) => text.slice(0, a) + " ".repeat(b - a) + text.slice(b), said);
1998
- for (const f of form.fields) {
1999
- if (f.key in answer) continue;
2000
- if (f.type === "number" && f.unit) {
2001
- const words2 = UNIT_WORDS[f.unit.toLowerCase()] ?? [f.unit.toLowerCase()];
2002
- const unitRe = new RegExp(`(?<![\\p{L}\\p{N}])((?:${NUMBER_WORD}[\\s-]+)+)(?:${words2.map(escape).join("|")})(?![\\p{L}\\p{N}])`, "iu");
2003
- const m = unitRe.exec(rest);
2004
- if (m && m[1].trim()) answer[f.key] = m[1];
2005
- } else if ((f.type === "choice" || f.type === "choices") && f.choices) {
2006
- const options = f.choices.map((label) => ({ label, value: label }));
2007
- const hits2 = matchChoices(rest, options);
2008
- const shared = form.fields.some(
2009
- (g) => g !== f && !(g.key in answer) && (g.type === "choice" || g.type === "choices") && matchChoices(rest, (g.choices ?? []).map((l) => ({ label: l, value: l }))).length
2010
- );
2011
- if (hits2.length && !shared) answer[f.key] = f.type === "choices" ? hits2 : hits2[0];
2012
- }
2013
- }
2014
- return readFill(form, answer);
2015
- }
2016
- var ruleFiller = async (form, said) => ruleFill(form, said);
2017
1960
  function modelFiller(generate, fallback) {
2018
1961
  return async (form, said) => {
2019
1962
  let result = null;
@@ -2042,6 +1985,358 @@ function fillHints(form) {
2042
1985
  return [...out];
2043
1986
  }
2044
1987
 
1988
+ // src/core/ruleFill.ts
1989
+ var IRREGULAR = {
1990
+ slept: "sleep",
1991
+ felt: "feel",
1992
+ ran: "run",
1993
+ swam: "swim",
1994
+ rode: "ride",
1995
+ drank: "drink",
1996
+ drunk: "drink",
1997
+ ate: "eat",
1998
+ weigh: "weight",
1999
+ weighs: "weight",
2000
+ weighed: "weight",
2001
+ weighing: "weight"
2002
+ };
2003
+ var SYNONYMS = [
2004
+ ["alcohol", "drink", "drinks", "booze"],
2005
+ ["mood", "feel", "feeling"],
2006
+ ["run", "jog"],
2007
+ ["ride", "bike", "cycle"],
2008
+ ["strength", "gym", "lift"]
2009
+ ];
2010
+ var STOP = /* @__PURE__ */ new Set(["a", "an", "the", "and", "or", "no", "not", "i", "it", "is", "was", "at", "in", "of", "to", "for", "my", "me", "on", "yes"]);
2011
+ function stem(word) {
2012
+ let w = word.toLowerCase().replace(/['’]/g, "");
2013
+ w = IRREGULAR[w] ?? w;
2014
+ const cut = (suffix, add = "") => {
2015
+ if (!w.endsWith(suffix) || w.length - suffix.length < 3) return false;
2016
+ w = w.slice(0, -suffix.length) + add;
2017
+ return true;
2018
+ };
2019
+ const cutDone = cut("ations", "at") || cut("ation", "at") || cut("ing") || cut("ed");
2020
+ if (!cutDone && !w.endsWith("ss")) cut("s");
2021
+ if (cutDone && /([b-df-hj-np-tv-z])\1$/.test(w) && !/(ll|ss|ff|zz)$/.test(w)) w = w.slice(0, -1);
2022
+ if (w.length > 3 && w.endsWith("e")) w = w.slice(0, -1);
2023
+ return w;
2024
+ }
2025
+ var GLUED_UNIT = /^(\d+(?:[.,]\d+)?)(k|km|kms|kg|kgs|lb|lbs|h|hr|hrs|min|mins|s|sec|secs|mi|m)$/;
2026
+ function tokenize(said) {
2027
+ const out = [];
2028
+ const add = (text, at, end) => out.push({ text, stem: stem(text), at, end });
2029
+ for (const m of said.matchAll(/[\p{L}\p{N}]+(?:['’.,][\p{L}\p{N}]+)*/gu)) {
2030
+ const at = m.index;
2031
+ const raw = m[0].toLowerCase();
2032
+ const glued = GLUED_UNIT.exec(raw);
2033
+ if (glued) {
2034
+ add(glued[1], at, at + glued[1].length);
2035
+ add(glued[2], at + glued[1].length, at + raw.length);
2036
+ } else add(raw, at, at + raw.length);
2037
+ }
2038
+ return out;
2039
+ }
2040
+ var plain = (t) => t.text.replace(/['’]/g, "");
2041
+ var phrase = (text, label, synonym = false) => {
2042
+ const words2 = tokenize(text).map((t) => t.text);
2043
+ if (!words2.length || !label && words2.length === 1 && STOP.has(words2[0])) return null;
2044
+ return { words: words2, stems: words2.map(stem), synonym, label };
2045
+ };
2046
+ function phrasesFor(names, label = false) {
2047
+ const out = [];
2048
+ for (const name of names) {
2049
+ const p = phrase(name, label);
2050
+ if (!p) continue;
2051
+ out.push(p);
2052
+ if (p.stems.length === 1) {
2053
+ for (const group of SYNONYMS)
2054
+ if (group.some((g) => stem(g) === p.stems[0])) {
2055
+ for (const g of group) if (stem(g) !== p.stems[0]) out.push(phrase(g, label, true));
2056
+ }
2057
+ }
2058
+ }
2059
+ return out;
2060
+ }
2061
+ function findPhrases(tokens, targets) {
2062
+ const all = targets.flatMap((t) => t.phrases.map((p) => ({ ...t, p }))).sort((a, b) => b.p.stems.length - a.p.stems.length);
2063
+ const hits = [];
2064
+ for (const { key, label, p } of all)
2065
+ for (let i = 0; i + p.stems.length <= tokens.length; i++) {
2066
+ const span = tokens.slice(i, i + p.stems.length);
2067
+ if (span.some((t, n) => !t.stem || t.stem !== p.stems[n] || !p.label && STOP.has(t.text))) continue;
2068
+ const end = i + p.stems.length;
2069
+ if (hits.some((h) => i < h.end && end > h.start)) continue;
2070
+ const exact = span.every((t, n) => t.text === p.words[n]);
2071
+ hits.push({ key, label, start: i, end, kind: p.synonym ? "synonym" : exact ? "exact" : "form" });
2072
+ }
2073
+ return hits.sort((a, b) => a.start - b.start);
2074
+ }
2075
+ var namesOf = (f) => [.../* @__PURE__ */ new Set([f.label, f.key.replace(/[-_]+/g, " ")])];
2076
+ var NUMBER_WORDS = new Set(
2077
+ "zero oh nought one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty thirty forty fifty sixty seventy eighty ninety hundred thousand".split(" ")
2078
+ );
2079
+ var TIME_UNITS = {
2080
+ s: 1,
2081
+ sec: 1,
2082
+ secs: 1,
2083
+ second: 1,
2084
+ seconds: 1,
2085
+ min: 60,
2086
+ mins: 60,
2087
+ minute: 60,
2088
+ minutes: 60,
2089
+ h: 3600,
2090
+ hr: 3600,
2091
+ hrs: 3600,
2092
+ hour: 3600,
2093
+ hours: 3600
2094
+ };
2095
+ var UNIT_WORDS = {
2096
+ kg: ["kg", "kgs", "kilo", "kilos", "kilogram", "kilograms"],
2097
+ lb: ["lb", "lbs", "pound", "pounds"],
2098
+ km: ["km", "kms", "kilometer", "kilometers", "kilometre", "kilometres", "k"],
2099
+ mi: ["mi", "mile", "miles"],
2100
+ m: ["m", "meter", "meters", "metre", "metres"]
2101
+ };
2102
+ var isDigits = (t) => !!t && /^\d/.test(t.text);
2103
+ var isNumberWord2 = (t) => !!t && NUMBER_WORDS.has(t.text);
2104
+ var isTimeUnit = (t) => !!t && t.text in TIME_UNITS;
2105
+ var is = (t, ...words2) => !!t && words2.includes(t.text);
2106
+ var timeScale = (unit) => unit ? TIME_UNITS[unit.toLowerCase()] : void 0;
2107
+ var unitWords = (unit) => UNIT_WORDS[unit.toLowerCase()] ?? [unit.toLowerCase()];
2108
+ function numberStarts(tokens, i) {
2109
+ const t = tokens[i];
2110
+ const next = tokens[i + 1];
2111
+ if (isDigits(t) || isNumberWord2(t)) return true;
2112
+ if (is(t, "a", "an")) return is(next, "half", "quarter", "hundred", "thousand", "hour", "minute");
2113
+ if (is(t, "half", "quarter")) return is(next, "a", "an", "of", "hour", "minute");
2114
+ return false;
2115
+ }
2116
+ function numberAt(said, tokens, i, stop) {
2117
+ let k = i;
2118
+ let time = false;
2119
+ const numberish = (j) => {
2120
+ const t = tokens[j];
2121
+ const next = tokens[j + 1];
2122
+ if (isDigits(t) || isNumberWord2(t) || is(t, "point", "half", "quarter")) return true;
2123
+ if (is(t, "and")) return j + 1 < stop && (isDigits(next) || isNumberWord2(next) || is(next, "a"));
2124
+ if (is(t, "a", "an")) return is(next, "half", "quarter", "hundred", "thousand", "hour", "minute");
2125
+ if (is(t, "of")) return is(tokens[j - 1], "half", "quarter") && is(next, "a", "an");
2126
+ return false;
2127
+ };
2128
+ while (k < stop) {
2129
+ if (numberish(k)) k++;
2130
+ else if (isTimeUnit(tokens[k])) {
2131
+ time = true;
2132
+ k++;
2133
+ if (!(k < stop && (numberStarts(tokens, k) || is(tokens[k], "and") && numberish(k)))) break;
2134
+ } else break;
2135
+ }
2136
+ while (k > i && is(tokens[k - 1], "and", "a", "an", "of")) k--;
2137
+ if (k <= i) return null;
2138
+ const text = said.slice(tokens[i].at, tokens[k - 1].end);
2139
+ if (time) {
2140
+ const seconds = parseSpokenDuration(text);
2141
+ return seconds == null ? null : { end: k, text, seconds };
2142
+ }
2143
+ if (parseSpokenNumber(text) == null) return null;
2144
+ const unit = k < stop && !STOP.has(tokens[k].text) ? tokens[k].text : void 0;
2145
+ return { end: unit ? k + 1 : k, text, unit };
2146
+ }
2147
+ var round = (n) => Math.round(n * 1e3) / 1e3;
2148
+ function spanValue(f, span) {
2149
+ const scale = timeScale(f.unit);
2150
+ if (span.seconds != null) {
2151
+ if (f.type === "duration") return span.seconds;
2152
+ return scale ? round(span.seconds / scale) : null;
2153
+ }
2154
+ if (span.unit && f.unit && !scale && !unitWords(f.unit).includes(span.unit) && Object.values(UNIT_WORDS).some((ws) => ws.includes(span.unit))) return null;
2155
+ return f.type === "duration" ? span.text : parseSpokenNumber(span.text);
2156
+ }
2157
+ var SCALE_WORDS = {
2158
+ terrible: 0,
2159
+ awful: 0,
2160
+ horrible: 0,
2161
+ dreadful: 0,
2162
+ miserable: 0,
2163
+ worst: 0,
2164
+ bad: 0.25,
2165
+ low: 0.25,
2166
+ poor: 0.25,
2167
+ rough: 0.25,
2168
+ down: 0.25,
2169
+ rubbish: 0.25,
2170
+ easy: 0,
2171
+ light: 0.25,
2172
+ ok: 0.5,
2173
+ okay: 0.5,
2174
+ fine: 0.5,
2175
+ alright: 0.5,
2176
+ average: 0.5,
2177
+ meh: 0.5,
2178
+ moderate: 0.5,
2179
+ normal: 0.5,
2180
+ good: 0.75,
2181
+ decent: 0.75,
2182
+ nice: 0.75,
2183
+ happy: 0.75,
2184
+ high: 0.75,
2185
+ hard: 0.75,
2186
+ tough: 0.75,
2187
+ great: 1,
2188
+ amazing: 1,
2189
+ excellent: 1,
2190
+ fantastic: 1,
2191
+ brilliant: 1,
2192
+ awesome: 1,
2193
+ wonderful: 1,
2194
+ perfect: 1,
2195
+ best: 1,
2196
+ brutal: 1
2197
+ };
2198
+ var INTENSIFIERS = /* @__PURE__ */ new Set(["very", "really", "super", "so", "extremely", "totally", "incredibly"]);
2199
+ function scaleValue(f, tokens, from, to) {
2200
+ if (f.min == null || f.max == null || f.unit) return null;
2201
+ for (let j = Math.max(0, from); j < to; j++) {
2202
+ const w = plain(tokens[j]);
2203
+ if (!(w in SCALE_WORDS)) continue;
2204
+ if (is(tokens[j - 1], "not") || is(tokens[j - 2], "not")) return null;
2205
+ let x = SCALE_WORDS[w];
2206
+ if (INTENSIFIERS.has(tokens[j - 1]?.text ?? "") && x !== 0.5) x = x < 0.5 ? Math.max(0, x - 0.25) : Math.min(1, x + 0.25);
2207
+ return { value: Math.round(f.min + x * (f.max - f.min)), at: j };
2208
+ }
2209
+ return null;
2210
+ }
2211
+ var NEGATIONS = /* @__PURE__ */ new Set(["no", "not", "didnt", "dont", "never", "skipped", "skip", "without", "zero", "none", "havent", "hasnt", "wasnt", "nope", "cant", "couldnt"]);
2212
+ function negatedBefore(tokens, start, free) {
2213
+ for (let j = start - 1; j >= Math.max(0, start - 3); j--) {
2214
+ if (!free[j]) return null;
2215
+ if (NEGATIONS.has(plain(tokens[j])) && !is(tokens[j + 1], "wait")) return j;
2216
+ }
2217
+ return null;
2218
+ }
2219
+ var CORRECTIONS = [["no", "wait"], ["wait"], ["sorry"], ["actually"], ["i", "mean"], ["make", "that"], ["scratch", "that"], ["or", "rather"], ["correction"]];
2220
+ function lastCorrection(tokens, from, to) {
2221
+ let last = null;
2222
+ for (let j = from; j < to; j++)
2223
+ for (const c of CORRECTIONS) if (c.every((w, n) => tokens[j + n]?.text === w) && j + c.length <= to) last = j + c.length;
2224
+ return last;
2225
+ }
2226
+ var LEAD = /^[\s,:;.-]*(?:(?:is|was|were|of|at|about|around|to|=)\b[\s,:;.-]*)*/i;
2227
+ var MAX_GAP = 4;
2228
+ function valueAfter(said, f, h, tokens, stop, free, nextIsToggle) {
2229
+ const from = h.end;
2230
+ if (f.type === "text") {
2231
+ if (from >= stop) return null;
2232
+ const text = said.slice(tokens[from].at, tokens[stop - 1].end).replace(LEAD, "").replace(/[\s,;.]+$/, "");
2233
+ return text ? { value: text, end: stop } : null;
2234
+ }
2235
+ if (f.type === "boolean") {
2236
+ const after = tokens.slice(from, Math.min(stop, from + 2));
2237
+ const last = after[after.length - 1];
2238
+ const lastIsNo = !!last && from + after.length === stop && nextIsToggle && NEGATIONS.has(plain(last));
2239
+ const yes = lastIsNo ? null : parseSpokenYesNo(after.map((t) => t.text).join(" "));
2240
+ if (yes != null) return { value: yes, end: from + after.length };
2241
+ const neg = negatedBefore(tokens, h.start, free);
2242
+ if (neg != null) return { value: false, end: h.end, start: neg };
2243
+ return h.kind === "synonym" ? null : { value: true, end: h.end };
2244
+ }
2245
+ if (f.type === "choice" || f.type === "choices") {
2246
+ if (from >= stop) return null;
2247
+ const labels = choiceLabels(f, tokens.slice(from, stop));
2248
+ if (!labels.length || f.type === "choice" && labels.length > 1) return null;
2249
+ return { value: f.type === "choice" ? labels[0] : labels, end: stop };
2250
+ }
2251
+ const fix = lastCorrection(tokens, from, stop);
2252
+ for (const start of fix != null ? [fix, from] : [from])
2253
+ for (let j = start; j < Math.min(stop, start + MAX_GAP + 1); j++) {
2254
+ if (!numberStarts(tokens, j)) continue;
2255
+ const span = numberAt(said, tokens, j, stop);
2256
+ const value = span && spanValue(f, span);
2257
+ if (span && value != null) return { value, end: span.end };
2258
+ break;
2259
+ }
2260
+ const near = Math.min(stop, from + 3);
2261
+ if (f.choices && from < near) {
2262
+ const [hit] = matchChoices(said.slice(tokens[from].at, tokens[near - 1].end), f.choices.map((label) => ({ label, value: label })));
2263
+ if (hit != null) return { value: hit, end: near };
2264
+ }
2265
+ const scale = scaleValue(f, tokens, from, near);
2266
+ if (scale) return { value: scale.value, end: scale.at + 1 };
2267
+ const before = scaleValue(f, tokens, h.start - 2, h.start);
2268
+ if (before && tokens.slice(before.at, h.start).every((_, n) => free[before.at + n]) && !/[,.;]/.test(said.slice(tokens[before.at].end, tokens[h.start].at)))
2269
+ return { value: before.value, end: h.end, start: before.at };
2270
+ return null;
2271
+ }
2272
+ function choiceLabels(f, tokens) {
2273
+ const hits = findPhrases(tokens, (f.choices ?? []).map((label) => ({ key: f.key, label, phrases: phrasesFor([label], true) })));
2274
+ return [...new Set(hits.map((h) => h.label))];
2275
+ }
2276
+ function ruleFill(form, said) {
2277
+ const tokens = tokenize(said);
2278
+ const free = tokens.map(() => true);
2279
+ const take = (from, to) => {
2280
+ for (let j = from; j < to; j++) free[j] = false;
2281
+ };
2282
+ const fieldOf = (key) => form.fields.find((f) => f.key === key);
2283
+ const named = [];
2284
+ let inText = false;
2285
+ for (const h of findPhrases(tokens, form.fields.map((f) => ({ key: f.key, phrases: phrasesFor(namesOf(f)) })))) {
2286
+ if (inText && h.kind !== "exact") continue;
2287
+ named.push(h);
2288
+ inText = fieldOf(h.key).type === "text";
2289
+ }
2290
+ for (const h of named) take(h.start, h.end);
2291
+ const answer = {};
2292
+ for (const f of form.fields) {
2293
+ const mine = named.map((h, i) => ({ h, i })).filter(({ h }) => h.key === f.key);
2294
+ for (const { h, i } of mine.reverse()) {
2295
+ const stop = named[i + 1]?.start ?? tokens.length;
2296
+ const nextIsToggle = !!named[i + 1] && fieldOf(named[i + 1].key).type === "boolean";
2297
+ const found = valueAfter(said, f, h, tokens, stop, free, nextIsToggle);
2298
+ if (!found) continue;
2299
+ answer[f.key] = found.value;
2300
+ take(found.start ?? h.start, found.end);
2301
+ break;
2302
+ }
2303
+ }
2304
+ const open = (f) => !(f.key in answer);
2305
+ const timeFields = () => form.fields.filter((f) => open(f) && (f.type === "duration" || f.type === "number" && timeScale(f.unit)));
2306
+ for (let j = 0; j < tokens.length; j++) {
2307
+ if (!free[j] || !numberStarts(tokens, j)) continue;
2308
+ let stop = j;
2309
+ while (stop < tokens.length && free[stop]) stop++;
2310
+ const span = numberAt(said, tokens, j, stop);
2311
+ if (!span) continue;
2312
+ let field2;
2313
+ if (span.seconds != null) {
2314
+ const unitSaid = tokens.slice(j, span.end).find(isTimeUnit).text;
2315
+ const candidates = timeFields();
2316
+ field2 = candidates.find((f) => timeScale(f.unit) === TIME_UNITS[unitSaid]) ?? (candidates.length === 1 ? candidates[0] : void 0);
2317
+ } else if (span.unit) {
2318
+ const matching = form.fields.filter((f) => open(f) && f.type === "number" && f.unit && !timeScale(f.unit) && unitWords(f.unit).includes(span.unit));
2319
+ if (matching.length === 1) field2 = matching[0];
2320
+ }
2321
+ const value = field2 && spanValue(field2, span);
2322
+ if (field2 && value != null) {
2323
+ answer[field2.key] = value;
2324
+ take(j, span.end);
2325
+ j = span.end - 1;
2326
+ }
2327
+ }
2328
+ const unclaimed = tokens.map((t, j) => free[j] ? t : { ...t, stem: "", text: "" });
2329
+ const choiceFields = form.fields.filter((f) => open(f) && (f.type === "choice" || f.type === "choices"));
2330
+ const mentioned = choiceFields.map((f) => ({ f, labels: choiceLabels(f, unclaimed) })).filter((c) => c.labels.length);
2331
+ if (mentioned.length === 1) {
2332
+ const [{ f, labels }] = mentioned;
2333
+ if (f.type === "choices") answer[f.key] = labels;
2334
+ else if (labels.length === 1) answer[f.key] = labels[0];
2335
+ }
2336
+ return readFill(form, answer);
2337
+ }
2338
+ var ruleFiller = async (form, said) => ruleFill(form, said);
2339
+
2045
2340
  // src/core/local.ts
2046
2341
  var LOCAL_DATASOURCE_TYPE = "modelform-local";
2047
2342
  var LEGACY_LOCAL_DATASOURCE_TYPE = "dashkit-local";
@@ -8585,7 +8880,7 @@ function cleanDashboard(json) {
8585
8880
  const value = structuredClone(json);
8586
8881
  const removed = [];
8587
8882
  const deletedKeys = /* @__PURE__ */ new Set();
8588
- for (let round = 0; round < 10 && !validateDashboard(value); round++) {
8883
+ for (let round2 = 0; round2 < 10 && !validateDashboard(value); round2++) {
8589
8884
  const errors = collapseUnions((validateDashboard.errors ?? []).filter((e) => !noise(e)));
8590
8885
  const targets = /* @__PURE__ */ new Map();
8591
8886
  for (const e of errors) {
@@ -12479,15 +12774,16 @@ export {
12479
12774
  DICTATION_TYPES,
12480
12775
  takesDictation,
12481
12776
  dictationHints,
12777
+ choicesOf,
12482
12778
  fillForm,
12483
12779
  fillSchema,
12484
12780
  fillPrompt,
12485
12781
  parseFillAnswer,
12486
12782
  readFill,
12487
- ruleFill,
12488
- ruleFiller,
12489
12783
  modelFiller,
12490
12784
  fillHints,
12785
+ ruleFill,
12786
+ ruleFiller,
12491
12787
  LOCAL_DATASOURCE_TYPE,
12492
12788
  isLocalDatasource,
12493
12789
  LocalDataSource,