@hackbuild/ai-views 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,122 @@
1
+ /**
2
+ * ModelSelect in the DOM: the closed line (or the bar chip) with its one
3
+ * pink "load, <size>", and the sheet in a drawer with search, the three
4
+ * filters, grouped rows, options and the sheet's own "your own" forms. Rows come from
5
+ * useModelPick; hackbuild-ui modelSelect, modelSheet and drawer draw them.
6
+ */
7
+ import { canLoad, drawer, lineWords, loadLabel, modelSelect, modelSheet } from "@virgilvox/hackbuild-ui/vanilla";
8
+ import { esc, listeners } from "./dom.js";
9
+ /** The sheet of one pick, in a drawer on the body. Opens on `open()`. */
10
+ export function pickSheet(pick) {
11
+ const doc = document;
12
+ const root = doc.createElement("div");
13
+ root.className = "hb-drawer hb";
14
+ const s0 = pick.snapshot();
15
+ root.innerHTML = `<div class="hb-drawer-panel" role="dialog" aria-modal="true"><div class="hb-drawer-hd"><span>${esc(s0.title)}</span><button type="button" class="hb-iconbtn" aria-label="close" data-hb-close><svg class="hb-icon is-stroke" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M6 6l12 12M18 6 6 18"/></svg></button></div><div class="hb-msheet-host"></div></div>`;
16
+ doc.body.append(root);
17
+ const body = root.querySelector(".hb-msheet-host");
18
+ const sheet = modelSheet(body, {
19
+ rows: s0.rows,
20
+ chosen: s0.chosen,
21
+ title: s0.title,
22
+ onChoose: (id) => pick.choose(id),
23
+ onLoad: (id) => void pick.load(id).catch(() => undefined),
24
+ onCancel: (id) => pick.cancel(id),
25
+ onRemove: (id) => void pick.remove(id),
26
+ onOption: (id, k, v) => pick.option(id, k, v),
27
+ onCustom: (kind, values) => void pick.custom(kind, values),
28
+ });
29
+ const l = listeners();
30
+ const handle = drawer(root, { side: "auto", breakpoint: 641, label: s0.title });
31
+ l.add(pick.subscribe((s) => sheet.update(s.rows, s.chosen)));
32
+ return {
33
+ open: () => handle.open(),
34
+ dispose() {
35
+ l.off();
36
+ sheet.dispose();
37
+ handle.dispose();
38
+ root.remove();
39
+ },
40
+ };
41
+ }
42
+ function chipWords(s) {
43
+ const w = lineWords(s.row, s.empty);
44
+ return w.state ? `${w.name}, ${w.state}` : w.name;
45
+ }
46
+ /** The closed picker over a host; pressing it opens the sheet. */
47
+ export function mountModelSelect(host, pick, o = {}) {
48
+ const sheet = pickSheet(pick);
49
+ host.classList.add("hb-msel-host");
50
+ const l = listeners();
51
+ if (o.variant === "chip") {
52
+ const draw = (s) => {
53
+ const words = chipWords(s);
54
+ host.innerHTML = `<button type="button" class="hb-msel-chip${s.row?.state ? ` is-${s.row.state}` : ""}" aria-haspopup="dialog" aria-label="model: ${esc(words)}"><span>${esc(words)}</span></button>`;
55
+ };
56
+ l.on(host, "click", (e) => {
57
+ if (e.target.closest(".hb-msel-chip"))
58
+ sheet.open();
59
+ });
60
+ l.add(pick.subscribe(draw));
61
+ }
62
+ else {
63
+ const inner = document.createElement("div");
64
+ host.replaceChildren(inner);
65
+ const s0 = pick.snapshot();
66
+ const sel = modelSelect(inner, { rows: s0.rows, chosen: s0.chosen, go: o.go ?? true, empty: s0.empty, onOpen: () => sheet.open(), onLoad: (id) => void pick.load(id).catch(() => undefined) });
67
+ l.add(pick.subscribe((s) => sel.update(s.rows, s.chosen)));
68
+ l.add(() => sel.dispose());
69
+ }
70
+ return {
71
+ dispose() {
72
+ l.off();
73
+ sheet.dispose();
74
+ },
75
+ };
76
+ }
77
+ /** The words of a gate's one pink button for a pick: "load, 1.7 gb", then "ready". */
78
+ export function gateWords(row) {
79
+ return row && canLoad(row) ? loadLabel(row) : row?.state === "loaded" ? "ready" : loadLabel(row);
80
+ }
81
+ /** Several jobs: one line per job, presets with totals, and one pink "load all, <total>". */
82
+ export function mountModelJobs(host, jobs, o = {}) {
83
+ const sheets = new Map([...jobs.picks].map(([job, p]) => [job, pickSheet(p)]));
84
+ host.classList.add("hb-msel-host");
85
+ const draw = () => {
86
+ const presets = jobs.presets();
87
+ const all = jobs.all();
88
+ host.innerHTML =
89
+ `<div class="hb-msel-jobs">` +
90
+ (presets.length ? `<div class="hb-msel-presets" role="group" aria-label="presets">${presets.map((p) => `<button type="button" class="hb-chip" data-preset="${esc(p.id)}">${esc(p.label)}, ${esc(p.total)}</button>`).join("")}</div>` : "") +
91
+ jobs
92
+ .lines()
93
+ .map((j) => {
94
+ const w = lineWords(j.row, "no model");
95
+ return `<div class="hb-msel-job"><span class="hb-msel-joblabel">${esc(j.label)}${j.required ? "<b> required</b>" : ""}</span><button type="button" class="hb-msel-line" data-job="${j.job}" aria-haspopup="dialog" aria-label="${esc(j.label)}: ${esc(w.name)}"><span class="hb-msel-name">${esc(w.name)}</span>${w.meta ? `<span class="hb-msel-meta">${esc(w.meta)}</span>` : ""}${w.state ? `<span class="hb-msel-state${j.row?.state === "loaded" ? " is-loaded" : ""}">${esc(w.state)}</span>` : ""}</button></div>`;
96
+ })
97
+ .join("") +
98
+ ((o.go ?? true) && all.ids.length ? `<div class="hb-msel"><button type="button" class="hb-btn hb-btn-go" data-all>${esc(all.label)}</button></div>` : "") +
99
+ `</div>`;
100
+ };
101
+ const l = listeners();
102
+ l.on(host, "click", (e) => {
103
+ const t = e.target;
104
+ const job = t.closest("[data-job]")?.dataset.job;
105
+ if (job)
106
+ return sheets.get(job)?.open();
107
+ const pre = t.closest("[data-preset]")?.dataset.preset;
108
+ if (pre)
109
+ return jobs.applyPreset(pre);
110
+ if (t.closest("[data-all]"))
111
+ void jobs.loadAll();
112
+ });
113
+ l.add(jobs.subscribe(draw));
114
+ draw();
115
+ return {
116
+ dispose() {
117
+ l.off();
118
+ for (const s of sheets.values())
119
+ s.dispose();
120
+ },
121
+ };
122
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * TranscribeDrop, TranscriptView and their rack parts in the DOM: the drop zone is the page until a file is in
3
+ * it, then the transcript is. Lines carry their time, edit in place (one
4
+ * undo step each), and export as txt, srt, vtt and json once there is text.
5
+ */
6
+ import { type Mounted } from "./dom.js";
7
+ import type { Channel, TranscribeQueue } from "./transcribe.js";
8
+ /**
9
+ * Options for a transcript: speaker fields, a seek callback for the times, and whether dropped
10
+ * lines show in place.
11
+ */
12
+ export interface TranscriptViewOptions {
13
+ /** Speaker fields beside each line. Off by default: they are inputs, and a subject keeps few controls. */
14
+ speakers?: boolean;
15
+ /** Times as buttons that seek a player. */
16
+ onSeek?: (seconds: number) => void;
17
+ /** Show dropped lines in place. */
18
+ showDropped?: boolean;
19
+ }
20
+ /** The HTML of one channel's transcript, or an empty note when there is none. */
21
+ export declare function transcriptHtml(c: Channel | undefined, o?: TranscriptViewOptions): string;
22
+ /** The selected channel's transcript over a host. */
23
+ export declare function mountTranscriptView(host: HTMLElement, queue: TranscribeQueue, o?: TranscriptViewOptions): Mounted;
24
+ /**
25
+ * Options for `mountTranscribeDrop`: accepted types, empty state words and a sample button, plus
26
+ * the transcript view options.
27
+ */
28
+ export interface TranscribeDropOptions extends TranscriptViewOptions {
29
+ accept?: string;
30
+ title?: string;
31
+ note?: string;
32
+ pickLabel?: string;
33
+ /** A sample a person can try with no file; the page queues it. */
34
+ sampleLabel?: string;
35
+ onSample?: () => void;
36
+ }
37
+ /** A mounted drop zone whose `pick` opens the file picker. */
38
+ export interface TranscribeDropHandle extends Mounted {
39
+ /** Open the file picker: the add files verb. */
40
+ pick(): void;
41
+ }
42
+ /** The drop zone that becomes the transcript. */
43
+ export declare function mountTranscribeDrop(host: HTMLElement, queue: TranscribeQueue, o?: TranscribeDropOptions): TranscribeDropHandle;
44
+ /** The queue as hackbuild-ui strips: name, state, figures, progress, remove; run and stop. */
45
+ export declare function mountTranscribeQueue(host: HTMLElement, queue: TranscribeQueue): Mounted;
46
+ /** Exports and undo for the selected transcript. Nothing shows until there is text; undo shows once there is a change. */
47
+ /** `go` makes the first export the one pink action: the next thing to do once there is text. */
48
+ export declare function mountTranscriptExports(host: HTMLElement, queue: TranscribeQueue, o?: {
49
+ save?: (name: string, text: string, type: string) => void;
50
+ go?: boolean;
51
+ }): Mounted;
@@ -0,0 +1,203 @@
1
+ /**
2
+ * TranscribeDrop, TranscriptView and their rack parts in the DOM: the drop zone is the page until a file is in
3
+ * it, then the transcript is. Lines carry their time, edit in place (one
4
+ * undo step each), and export as txt, srt, vtt and json once there is text.
5
+ */
6
+ import { clockTime, droppedLine, stripStatus } from "@virgilvox/hackbuild-ui/vanilla";
7
+ import { ICON, download, esc, listeners, show } from "./dom.js";
8
+ /** The HTML of one channel's transcript, or an empty note when there is none. */
9
+ export function transcriptHtml(c, o = {}) {
10
+ if (!c)
11
+ return `<p class="hb-lay-empty-note">nothing yet. drop a file.</p>`;
12
+ const st = stripStatus(c.status);
13
+ const head = `<div class="hb-panel-hd-inst" role="status">${esc(c.name)}, ${esc(st.word)}${c.status === "working" ? `, ${c.pct}%` : ""}</div>${c.error ? `<p class="hb-lay-empty-note">${esc(c.error)}</p>` : ""}`;
14
+ if (!c.segments.length)
15
+ return `<section class="hb-panel">${head}</section>`;
16
+ const drops = c.dropped.length ? `<li class="hb-tseg-note">${esc(droppedLine(c.dropped))}</li>` : "";
17
+ const rows = c.segments
18
+ .map((s, i) => {
19
+ const t = clockTime(s.start);
20
+ const time = o.onSeek ? `<button type="button" class="hb-tseg-t" data-seek="${s.start}" aria-label="play from ${t}">${t}</button>` : `<span class="hb-tseg-t">${t}</span>`;
21
+ const who = o.speakers ? `<input class="hb-tseg-who" data-who="${i}" placeholder="speaker" aria-label="speaker at ${t}" value="${esc(s.speaker ?? "")}">` : s.speaker ? `<b>${esc(s.speaker)}: </b>` : "";
22
+ return `<li><div class="hb-tseg"><span class="hb-tseg-body">${time}${who}<span class="hb-tseg-text" contenteditable="true" role="textbox" aria-label="text at ${t}" data-line="${i}">${esc(s.text)}</span>${s.edited ? `<span class="hb-tseg-why">edited</span>` : ""}</span></div></li>`;
23
+ })
24
+ .join("");
25
+ const dropped = o.showDropped ? c.dropped.map((d) => `<li><div class="hb-tseg is-dropped"><span class="hb-tseg-body"><span class="hb-tseg-t">${clockTime(d.start)}</span><span class="hb-tseg-text">${esc(d.text)}</span><span class="hb-tseg-why">[${esc(d.why)}]</span></span></div></li>`).join("") : "";
26
+ return `<section class="hb-panel">${head}<ol class="hb-transcript is-edit" aria-label="transcript">${drops}${rows}${dropped}</ol></section>`;
27
+ }
28
+ const pickedOf = (s) => s.channels.find((c) => c.id === s.selected);
29
+ /** The selected channel's transcript over a host. */
30
+ export function mountTranscriptView(host, queue, o = {}) {
31
+ const l = listeners();
32
+ let drawn = "";
33
+ const draw = (s) => {
34
+ // a line being typed in is not redrawn under the caret
35
+ if (host.contains(document.activeElement) && document.activeElement.isContentEditable)
36
+ return;
37
+ const html = transcriptHtml(pickedOf(s), o);
38
+ if (html !== drawn)
39
+ host.innerHTML = drawn = html;
40
+ };
41
+ const commit = (el) => {
42
+ const s = queue.snapshot();
43
+ const c = pickedOf(s);
44
+ if (!c)
45
+ return;
46
+ if (el.dataset.line !== undefined)
47
+ queue.edit(c.id, Number(el.dataset.line), { text: (el.textContent ?? "").trim() });
48
+ else if (el.dataset.who !== undefined)
49
+ queue.edit(c.id, Number(el.dataset.who), { speaker: el.value.trim() });
50
+ };
51
+ l.on(host, "focusout", (e) => {
52
+ commit(e.target);
53
+ draw(queue.snapshot());
54
+ });
55
+ l.on(host, "keydown", (e) => {
56
+ const ke = e;
57
+ const el = ke.target;
58
+ if (ke.key === "Enter" && el.isContentEditable) {
59
+ ke.preventDefault();
60
+ el.blur();
61
+ }
62
+ });
63
+ l.on(host, "click", (e) => {
64
+ const seek = e.target.closest("[data-seek]");
65
+ if (seek)
66
+ o.onSeek?.(Number(seek.dataset.seek));
67
+ });
68
+ l.add(queue.subscribe(draw));
69
+ return { dispose: () => l.off() };
70
+ }
71
+ /** The drop zone that becomes the transcript. */
72
+ export function mountTranscribeDrop(host, queue, o = {}) {
73
+ const l = listeners();
74
+ const input = document.createElement("input");
75
+ input.type = "file";
76
+ input.hidden = true;
77
+ input.multiple = true;
78
+ input.accept = o.accept ?? "audio/*,video/*";
79
+ const zone = document.createElement("div");
80
+ const result = document.createElement("div");
81
+ host.replaceChildren(zone, result, input);
82
+ zone.className = "hb-lay-drop";
83
+ // the result scrolls inside the subject, as the drop first layout's transcript does
84
+ result.className = "hb-lay-scroll";
85
+ zone.innerHTML =
86
+ `<div class="hb-lay-empty"><span class="hb-lay-empty-name">${esc(o.title ?? "drop audio or video")}</span>` +
87
+ `<p class="hb-lay-empty-note">${esc(o.note ?? "any length. the words are worked out on this machine and the file never leaves it.")}</p>` +
88
+ `<div class="hb-lay-empty-acts"><button type="button" class="hb-btn hb-btn-go" data-pick>${esc(o.pickLabel ?? "choose a file")}</button>${o.sampleLabel ? `<button type="button" class="hb-btn" data-sample>${esc(o.sampleLabel)}</button>` : ""}</div></div>`;
89
+ const view = mountTranscriptView(result, queue, o);
90
+ const take = (list) => {
91
+ const files = list ? [...list] : [];
92
+ if (files.length)
93
+ queue.add(files);
94
+ };
95
+ l.on(host, "dragover", (e) => {
96
+ e.preventDefault();
97
+ zone.classList.add("is-hot");
98
+ });
99
+ l.on(host, "dragleave", () => zone.classList.remove("is-hot"));
100
+ l.on(host, "drop", (e) => {
101
+ e.preventDefault();
102
+ zone.classList.remove("is-hot");
103
+ take(e.dataTransfer?.files);
104
+ });
105
+ l.on(input, "change", () => {
106
+ take(input.files);
107
+ input.value = "";
108
+ });
109
+ l.on(zone, "click", (e) => {
110
+ const t = e.target;
111
+ if (t.closest("[data-pick]"))
112
+ input.click();
113
+ else if (t.closest("[data-sample]"))
114
+ o.onSample?.();
115
+ });
116
+ l.add(queue.subscribe((s) => {
117
+ const empty = s.channels.length === 0;
118
+ show(zone, empty);
119
+ show(result, !empty);
120
+ }));
121
+ return {
122
+ pick: () => input.click(),
123
+ dispose() {
124
+ l.off();
125
+ view.dispose();
126
+ },
127
+ };
128
+ }
129
+ /** The queue as hackbuild-ui strips: name, state, figures, progress, remove; run and stop. */
130
+ export function mountTranscribeQueue(host, queue) {
131
+ const l = listeners();
132
+ const draw = (s) => {
133
+ const strips = s.channels
134
+ .map((c, i) => {
135
+ const st = stripStatus(c.status);
136
+ return (`<li class="hb-qstrip is-${st.tone || "idle"}${c.id === s.selected ? " is-sel" : ""}${c.status === "done" ? " is-done" : ""}">` +
137
+ `<button type="button" class="hb-qstrip-main" data-sel="${c.id}" aria-pressed="${c.id === s.selected}"><span class="hb-qstrip-n">${String(i + 1).padStart(2, "0")}</span><span class="hb-qstrip-body"><span class="hb-qstrip-hd"><span class="hb-qstrip-name">${esc(c.name)}</span><span class="hb-qstrip-stat">${esc(st.word)}</span></span>` +
138
+ (c.meta.length || c.error ? `<span class="hb-qstrip-meta">${c.meta.map((m) => `<span>${m.label ? `${esc(m.label)} ` : ""}<b${m.tone ? ` class="is-${m.tone}"` : ""}>${esc(m.value)}</b></span>`).join("")}${c.error ? `<span class="is-lo">${esc(c.error)}</span>` : ""}</span>` : "") +
139
+ (c.status === "working" ? `<span class="hb-qstrip-bar" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="${c.pct}" aria-label="${esc(c.name)} progress"><i style="--hb-v:${c.pct / 100}"></i></span>` : "") +
140
+ `</span></button>` +
141
+ (s.running ? "" : `<button type="button" class="hb-qstrip-x" data-rm="${c.id}" aria-label="remove ${esc(c.name)}" title="remove">${ICON.x}</button>`) +
142
+ `</li>`);
143
+ })
144
+ .join("");
145
+ const queued = s.channels.some((c) => c.status === "queued");
146
+ host.innerHTML =
147
+ `<ol class="hb-qstrips" aria-label="files">${strips || `<li class="hb-qstrip-empty">no files yet. drop audio or video.</li>`}</ol>` +
148
+ (s.running || queued || s.channels.length ? `<div class="hb-toolbar">${s.running ? `<button type="button" class="hb-btn" data-stop>stop</button>` : queued ? `<button type="button" class="hb-btn" data-run>transcribe</button>` : ""}${s.channels.length && !s.running ? `<button type="button" class="hb-btn" data-clear>clear</button>` : ""}</div>` : "");
149
+ };
150
+ l.on(host, "click", (e) => {
151
+ const t = e.target;
152
+ const sel = t.closest("[data-sel]");
153
+ if (sel)
154
+ return queue.select(Number(sel.dataset.sel));
155
+ const rm = t.closest("[data-rm]");
156
+ if (rm)
157
+ return queue.remove(Number(rm.dataset.rm));
158
+ if (t.closest("[data-run]"))
159
+ void queue.start();
160
+ else if (t.closest("[data-stop]"))
161
+ queue.stop();
162
+ else if (t.closest("[data-clear]"))
163
+ queue.clear();
164
+ });
165
+ l.add(queue.subscribe(draw));
166
+ return { dispose: () => l.off() };
167
+ }
168
+ /** Exports and undo for the selected transcript. Nothing shows until there is text; undo shows once there is a change. */
169
+ /** `go` makes the first export the one pink action: the next thing to do once there is text. */
170
+ export function mountTranscriptExports(host, queue, o = {}) {
171
+ const l = listeners();
172
+ const kinds = ["txt", "srt", "vtt", "json"];
173
+ const draw = (s) => {
174
+ const c = pickedOf(s);
175
+ const has = !!c?.segments.length;
176
+ host.innerHTML = has
177
+ ? `<div class="hb-toolbar" role="group" aria-label="export">${kinds.map((k, i) => `<button type="button" class="hb-btn${o.go && i === 0 ? " hb-btn-go" : ""}" data-ex="${k}">${o.go && i === 0 ? `save ${k}` : k}</button>`).join("")}<button type="button" class="hb-btn" data-copy>${ICON.copy}copy</button>${s.canUndo ? `<button type="button" class="hb-iconbtn" data-undo aria-label="undo the last edit" title="undo">${ICON.undo}</button>` : ""}${s.canRedo ? `<button type="button" class="hb-iconbtn" data-redo aria-label="redo" title="redo">${ICON.redo}</button>` : ""}</div>`
178
+ : "";
179
+ };
180
+ l.on(host, "click", (e) => {
181
+ const t = e.target;
182
+ const ex = t.closest("[data-ex]");
183
+ if (ex) {
184
+ const f = queue.exportAs(ex.dataset.ex);
185
+ if (f)
186
+ (o.save ?? download)(f.name, f.text, f.type);
187
+ return;
188
+ }
189
+ const cp = t.closest("[data-copy]");
190
+ if (cp) {
191
+ const f = queue.exportAs("txt");
192
+ if (f)
193
+ void navigator.clipboard?.writeText(f.text).then(() => (cp.lastChild.textContent = "copied")).catch(() => (cp.lastChild.textContent = "copy blocked"));
194
+ return;
195
+ }
196
+ if (t.closest("[data-undo]"))
197
+ queue.undo();
198
+ else if (t.closest("[data-redo]"))
199
+ queue.redo();
200
+ });
201
+ l.add(queue.subscribe(draw));
202
+ return { dispose: () => l.off() };
203
+ }
package/dist/pick.d.ts ADDED
@@ -0,0 +1,149 @@
1
+ /**
2
+ * useModelPick(job): the state behind a model picker. It turns the job's
3
+ * records into rows fitted to this machine, keeps the choice, the options
4
+ * and the load state of each model, and loads through @hackbuild/weights
5
+ * with progress. Framework free: a view subscribes and redraws; the Vue
6
+ * component and the custom element both sit on it.
7
+ */
8
+ import type { Env } from "@hackbuild/ai-env";
9
+ import type { ModelRecord, StorageEstimate } from "@hackbuild/models";
10
+ import { type WeightStore } from "@hackbuild/weights";
11
+ import { type ModelOption, type ModelRow, type OwnKind } from "@virgilvox/hackbuild-ui/vanilla";
12
+ import { type Job } from "./jobs.js";
13
+ /** What a loader gets: an abort signal, a progress callback and the row's options. */
14
+ export interface LoadContext {
15
+ signal: AbortSignal;
16
+ /** 0 to 1, or null while the total is unknown. */
17
+ onProgress: (fraction: number | null) => void;
18
+ options: Record<string, string>;
19
+ }
20
+ /** Fetches a model's weights (and may start its runtime). Throws to fail the row. */
21
+ export type Loader = (rec: ModelRecord, ctx: LoadContext) => Promise<unknown>;
22
+ /**
23
+ * A loader over a weight store: a record with a url fetches that file, a
24
+ * record with a repo and files fetches each, resuming and checking the
25
+ * SHA-256 when the record has one. Records a runtime fetches itself (WebLLM,
26
+ * transformers.js model ids) need the app's loader.
27
+ */
28
+ /** The cache keys of a record's files in a weight store, as weightsLoader writes them. */
29
+ export declare function weightKeys(rec: ModelRecord): string[];
30
+ /**
31
+ * A loader that fetches a record's weight files through a weight store; records that load through
32
+ * their runtime throw.
33
+ */
34
+ export declare function weightsLoader(store: WeightStore): Loader;
35
+ /** Deletes a stored model's files from a weight store: the remove of a picker. */
36
+ export declare function weightsRemover(store: WeightStore): (rec: ModelRecord) => Promise<void>;
37
+ /**
38
+ * Options for `useModelPick`: records, env, storage estimate, loader, remover, stored ids, the
39
+ * chosen id and extra row options.
40
+ */
41
+ export interface PickOptions {
42
+ /** Records to pick from; default every record the views know. */
43
+ records?: readonly ModelRecord[];
44
+ env?: Env;
45
+ estimate?: StorageEstimate | null;
46
+ /** Loads a model; required to load. weightsLoader(store) fetches files; a page with a runtime passes its own. */
47
+ loader?: Loader;
48
+ /** Deletes a stored model's weights; weightsRemover(store) does it through @hackbuild/weights. */
49
+ onRemove?: (rec: ModelRecord) => Promise<void> | void;
50
+ /** Ids already on this machine. */
51
+ stored?: Iterable<string>;
52
+ chosen?: string;
53
+ /** Options a job adds to every row, like a language select for speech. */
54
+ extra?: (rec: ModelRecord) => readonly ModelOption[];
55
+ }
56
+ /** What a picker shows: its job, title, rows, the chosen id and row, and what the loader returned for it. */
57
+ export interface PickSnapshot {
58
+ job: Job;
59
+ title: string;
60
+ empty: string;
61
+ rows: ModelRow[];
62
+ chosen: string;
63
+ row: ModelRow | undefined;
64
+ /** What the loader returned for the chosen model, once it is loaded (an engine, a session). */
65
+ loaded: unknown;
66
+ }
67
+ /**
68
+ * A model picker for one job: choose, load, cancel, remove, set row options, add your own model,
69
+ * and subscribe to its snapshot.
70
+ */
71
+ export interface ModelPick {
72
+ readonly job: Job;
73
+ snapshot(): PickSnapshot;
74
+ subscribe(fn: (s: PickSnapshot) => void): () => void;
75
+ choose(id: string): void;
76
+ /** Load the chosen model, or `id`, through the loader. Rejects when there is no loader or the load fails; the row shows it. */
77
+ load(id?: string): Promise<void>;
78
+ cancel(id?: string): void;
79
+ /** Delete a stored model through onRemove, then forget it. */
80
+ remove(id: string): Promise<void>;
81
+ option(id: string, key: string, value: string): void;
82
+ /** Options set on a row. */
83
+ optionsOf(id: string): Record<string, string>;
84
+ /** Add a model from a "your own" form; returns its id. */
85
+ custom(kind: OwnKind, values: Record<string, string>): string;
86
+ /** New machine facts (env, storage) or records. */
87
+ update(o: {
88
+ env?: Env | undefined;
89
+ estimate?: StorageEstimate | null | undefined;
90
+ records?: readonly ModelRecord[] | undefined;
91
+ loader?: Loader | undefined;
92
+ onRemove?: PickOptions["onRemove"];
93
+ }): void;
94
+ /** What the loader returned for a model. */
95
+ result(id: string): unknown;
96
+ record(id: string): ModelRecord | undefined;
97
+ /** The error of the last failed load of a row. */
98
+ error(id: string): string | undefined;
99
+ }
100
+ /** A record for a "your own" form. Its licence is unknown until the person reads the card. */
101
+ export declare function ownRecord(job: Job, kind: OwnKind, v: Record<string, string>): ModelRecord;
102
+ /**
103
+ * The row a picker starts on: the first "start here" record that fits this
104
+ * machine, else the first that fits and is fast or steady, else the first
105
+ * that fits, else the first.
106
+ */
107
+ export declare function defaultPick(records: readonly ModelRecord[], env?: Env, estimate?: StorageEstimate | null): string;
108
+ /** Creates a model picker for a job, with rows that say whether each model fits this machine. */
109
+ export declare function useModelPick(job: Job, o?: PickOptions): ModelPick;
110
+ /** A preset for a tool with several jobs: one pick per job. */
111
+ export interface PickPreset {
112
+ id: string;
113
+ label: string;
114
+ picks: Partial<Record<Job, string>>;
115
+ }
116
+ /** One line of a jobs rack: the job, its label, whether it is required, and the chosen row. */
117
+ export interface JobLine {
118
+ job: Job;
119
+ label: string;
120
+ required: boolean;
121
+ row: ModelRow | undefined;
122
+ }
123
+ /** Several pickers as one rack: a line per job, presets with their totals, and load all. */
124
+ export interface JobsPick {
125
+ readonly picks: ReadonlyMap<Job, ModelPick>;
126
+ lines(): JobLine[];
127
+ presets(): {
128
+ id: string;
129
+ label: string;
130
+ total: string;
131
+ }[];
132
+ applyPreset(id: string): void;
133
+ /** "load all, 1.2 gb" and the ids it would fetch. */
134
+ all(): {
135
+ ids: string[];
136
+ label: string;
137
+ };
138
+ loadAll(): Promise<void>;
139
+ subscribe(fn: () => void): () => void;
140
+ /** New machine facts, records or loader for every job. */
141
+ update(o: Parameters<ModelPick["update"]>[0]): void;
142
+ setPresets(p: readonly PickPreset[]): void;
143
+ }
144
+ /** Creates one picker per job and joins them as a rack with presets and load all. */
145
+ export declare function useJobsPick(jobs: readonly {
146
+ job: Job;
147
+ required?: boolean;
148
+ options?: PickOptions;
149
+ }[], initialPresets?: readonly PickPreset[], shared?: PickOptions): JobsPick;