@modelform/react-native 0.13.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
@@ -148,6 +148,34 @@ const speech: SpeechRecognizer = {
148
148
  forms by other means. The native recognizer is shared, so a real adapter should also wait for the previous session's
149
149
  `end` before starting the next one; otherwise that late event ends the new session too.
150
150
 
151
+ ### Filling the whole form by voice
152
+
153
+ With a recognizer, the form also gets a microphone beside Save. Say several values in one go ("weight seventy two
154
+ point five, slept seven hours, mood four, went for a swim"), tap again to stop, and the form fills what it understood.
155
+ Filled inputs are outlined and marked "From voice" until edited; nothing is recorded until Save, and Undo puts the form
156
+ back as it was before the fill. Listening is `continuous`, so pauses don't cut it off: an adapter should pass that on
157
+ (expo-speech-recognition takes `continuous: true`) and deliver the whole transcript as one final `onText` when it
158
+ stops.
159
+
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
167
+ `fillSchema` for guided generation, reads the reply with `readFill` (so a model can only enter what typing could), and
168
+ with a fallback fills what the model left out:
169
+
170
+ ```tsx
171
+ import { modelFiller, ruleFiller } from '@modelform/react-native';
172
+
173
+ const filler = modelFiller((instructions, prompt, schema) => myModel.generate({ instructions, prompt, schema }), ruleFiller);
174
+ <InputForm dashboard={form} store={store} speech={speech} filler={filler} />;
175
+ ```
176
+
177
+ `filler={null}` turns whole-form filling off and keeps per-field dictation. Groups' fields and counters aren't filled.
178
+
151
179
  ## Bundled dashboards
152
180
 
153
181
  `modelform dashboards [dir] [--watch]` writes an `index.ts` that imports every `.json` file in a folder, so
@@ -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;
@@ -1807,6 +1810,533 @@ function dictationHints(panel, choices2) {
1807
1810
  return out;
1808
1811
  }
1809
1812
 
1813
+ // src/core/formFill.ts
1814
+ var choicesOf = (p, runtime) => (p.options.choicesFrom ? runtime[p.options.choicesFrom] : p.options.choices) ?? [];
1815
+ function fillForm(dashboard, choices2 = {}) {
1816
+ const fields = [];
1817
+ const panels = {};
1818
+ for (const p of plainInputPanels(dashboard)) {
1819
+ const o = p.options;
1820
+ if (p.type === "input-counter" || panels[o.metric]) continue;
1821
+ const base = { key: o.metric, label: p.title || o.metric };
1822
+ let field2;
1823
+ switch (p.type) {
1824
+ case "input-number":
1825
+ field2 = { ...base, type: "number", unit: o.unit, min: o.min, max: o.max };
1826
+ break;
1827
+ case "input-rating":
1828
+ field2 = { ...base, type: "number", min: Math.round(o.min ?? 1), max: Math.round(o.max ?? 5) };
1829
+ break;
1830
+ case "input-slider":
1831
+ field2 = { ...base, type: "number", unit: o.unit, min: o.min ?? 0, max: o.max ?? 10, choices: o.labels?.length ? o.labels : void 0 };
1832
+ break;
1833
+ case "input-duration":
1834
+ field2 = { ...base, type: "duration" };
1835
+ break;
1836
+ case "input-select": {
1837
+ const labels = choicesOf(p, choices2).map((c) => c.label);
1838
+ if (!labels.length) continue;
1839
+ field2 = { ...base, type: o.multi ? "choices" : "choice", choices: labels };
1840
+ break;
1841
+ }
1842
+ case "input-toggle":
1843
+ field2 = { ...base, type: "boolean" };
1844
+ break;
1845
+ case "input-text":
1846
+ field2 = { ...base, type: "text" };
1847
+ break;
1848
+ default:
1849
+ continue;
1850
+ }
1851
+ fields.push(Object.fromEntries(Object.entries(field2).filter(([, v]) => v !== void 0)));
1852
+ panels[o.metric] = p;
1853
+ }
1854
+ return { fields, panels, choices: choices2 };
1855
+ }
1856
+ function describe(f) {
1857
+ const range = f.min != null && f.max != null ? `, from ${f.min} to ${f.max}` : f.min != null ? `, ${f.min} or more` : f.max != null ? `, at most ${f.max}` : "";
1858
+ switch (f.type) {
1859
+ case "number":
1860
+ return `a number${f.unit ? ` in ${f.unit}` : ""}${range}${f.choices ? ` (${f.choices.join(" \u2026 ")} along the scale)` : ""}`;
1861
+ case "duration":
1862
+ return "a duration in seconds";
1863
+ case "choice":
1864
+ return `one of: ${f.choices.map((c) => JSON.stringify(c)).join(", ")}`;
1865
+ case "choices":
1866
+ return `a list of any of: ${f.choices.map((c) => JSON.stringify(c)).join(", ")}`;
1867
+ case "boolean":
1868
+ return "true or false";
1869
+ case "text":
1870
+ return "text, in the user\u2019s words";
1871
+ }
1872
+ }
1873
+ function fillSchema(form) {
1874
+ const properties = {};
1875
+ for (const f of form.fields) {
1876
+ const description = `${f.label}: ${describe(f)}`;
1877
+ switch (f.type) {
1878
+ case "number":
1879
+ case "duration":
1880
+ properties[f.key] = { type: "number", description, ...f.min != null && { minimum: f.min }, ...f.max != null && { maximum: f.max } };
1881
+ break;
1882
+ case "choice":
1883
+ properties[f.key] = { type: "string", enum: f.choices, description };
1884
+ break;
1885
+ case "choices":
1886
+ properties[f.key] = { type: "array", items: { type: "string", enum: f.choices }, description };
1887
+ break;
1888
+ case "boolean":
1889
+ properties[f.key] = { type: "boolean", description };
1890
+ break;
1891
+ case "text":
1892
+ properties[f.key] = { type: "string", description };
1893
+ break;
1894
+ }
1895
+ }
1896
+ return { type: "object", properties, required: [], additionalProperties: false };
1897
+ }
1898
+ function fillPrompt(form, said) {
1899
+ const instructions = [
1900
+ "You fill in a form from what the user said.",
1901
+ "Reply with one JSON object and nothing else. Its keys are field keys from the list below.",
1902
+ "Only include fields the user gave a value for; leave the others out. Never guess or make up values.",
1903
+ "Numbers are plain numbers without units. Choices use the exact text listed."
1904
+ ].join(" ");
1905
+ const list = form.fields.map((f) => `- ${f.key}: ${f.label}, ${describe(f)}`).join("\n");
1906
+ const example = form.fields.find((f) => f.type === "number");
1907
+ const prompt = [
1908
+ `Fields:
1909
+ ${list}`,
1910
+ example ? `Example: if the user said "${example.label.toLowerCase()} 3" the answer is {"${example.key}": 3}` : "",
1911
+ `The user said: "${said.replace(/"/g, "'")}"`,
1912
+ "JSON:"
1913
+ ].filter(Boolean).join("\n\n");
1914
+ return { instructions, prompt };
1915
+ }
1916
+ function parseFillAnswer(answer) {
1917
+ if (answer && typeof answer === "object" && !Array.isArray(answer)) return answer;
1918
+ if (typeof answer !== "string") return null;
1919
+ const start = answer.indexOf("{");
1920
+ const end = answer.lastIndexOf("}");
1921
+ if (start < 0 || end <= start) return null;
1922
+ try {
1923
+ const v = JSON.parse(answer.slice(start, end + 1));
1924
+ return v && typeof v === "object" && !Array.isArray(v) ? v : null;
1925
+ } catch {
1926
+ return null;
1927
+ }
1928
+ }
1929
+ function asSaid(v) {
1930
+ if (v == null) return null;
1931
+ if (typeof v === "boolean") return v ? "yes" : "no";
1932
+ if (typeof v === "number") return Number.isFinite(v) ? String(v) : null;
1933
+ if (typeof v === "string") return v.trim() || null;
1934
+ if (Array.isArray(v)) return v.map(asSaid).filter(Boolean).join(", ") || null;
1935
+ return null;
1936
+ }
1937
+ function readFill(form, answer) {
1938
+ const result = { values: {}, problems: [] };
1939
+ const obj = parseFillAnswer(answer);
1940
+ if (!obj) return { values: {}, problems: [{ key: "", problem: "The answer wasn\u2019t a JSON object" }] };
1941
+ for (const f of form.fields) {
1942
+ const raw = obj[f.key];
1943
+ const panel = form.panels[f.key];
1944
+ const choices2 = choicesOf(panel, form.choices);
1945
+ if (panel.type === "input-select" && !panel.options.multi) {
1946
+ const byValue = choices2.find((c) => c.value === raw);
1947
+ if (byValue) {
1948
+ result.values[f.key] = byValue.value;
1949
+ continue;
1950
+ }
1951
+ }
1952
+ const said = asSaid(raw);
1953
+ if (said == null) continue;
1954
+ const r = spokenValue(panel, said, null, choices2);
1955
+ if ("value" in r) result.values[f.key] = r.value;
1956
+ else result.problems.push({ key: f.key, problem: r.problem });
1957
+ }
1958
+ return result;
1959
+ }
1960
+ function modelFiller(generate, fallback) {
1961
+ return async (form, said) => {
1962
+ let result = null;
1963
+ try {
1964
+ const { instructions, prompt } = fillPrompt(form, said);
1965
+ result = readFill(form, await generate(instructions, prompt, fillSchema(form)));
1966
+ if (result.problems.some((p) => p.key === "")) result = null;
1967
+ } catch {
1968
+ result = null;
1969
+ }
1970
+ if (!fallback) return result ?? { values: {}, problems: [{ key: "", problem: "The model couldn\u2019t fill the form" }] };
1971
+ const rules = await fallback(form, said);
1972
+ if (!result) return rules;
1973
+ const values = { ...rules.values, ...result.values };
1974
+ const problems = [...result.problems, ...rules.problems.filter((p) => !(p.key in values))];
1975
+ return { values, problems };
1976
+ };
1977
+ }
1978
+ function fillHints(form) {
1979
+ const out = /* @__PURE__ */ new Set();
1980
+ for (const f of form.fields) {
1981
+ out.add(f.label);
1982
+ if (f.unit) out.add(f.unit);
1983
+ for (const c of f.choices ?? []) out.add(c);
1984
+ }
1985
+ return [...out];
1986
+ }
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
+
1810
2340
  // src/core/local.ts
1811
2341
  var LOCAL_DATASOURCE_TYPE = "modelform-local";
1812
2342
  var LEGACY_LOCAL_DATASOURCE_TYPE = "dashkit-local";
@@ -8350,7 +8880,7 @@ function cleanDashboard(json) {
8350
8880
  const value = structuredClone(json);
8351
8881
  const removed = [];
8352
8882
  const deletedKeys = /* @__PURE__ */ new Set();
8353
- for (let round = 0; round < 10 && !validateDashboard(value); round++) {
8883
+ for (let round2 = 0; round2 < 10 && !validateDashboard(value); round2++) {
8354
8884
  const errors = collapseUnions((validateDashboard.errors ?? []).filter((e) => !noise(e)));
8355
8885
  const targets = /* @__PURE__ */ new Map();
8356
8886
  for (const e of errors) {
@@ -12244,6 +12774,16 @@ export {
12244
12774
  DICTATION_TYPES,
12245
12775
  takesDictation,
12246
12776
  dictationHints,
12777
+ choicesOf,
12778
+ fillForm,
12779
+ fillSchema,
12780
+ fillPrompt,
12781
+ parseFillAnswer,
12782
+ readFill,
12783
+ modelFiller,
12784
+ fillHints,
12785
+ ruleFill,
12786
+ ruleFiller,
12247
12787
  LOCAL_DATASOURCE_TYPE,
12248
12788
  isLocalDatasource,
12249
12789
  LocalDataSource,