@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.
- package/LICENSE +21 -0
- package/README.md +241 -2
- package/dist/chat.d.ts +107 -0
- package/dist/chat.js +243 -0
- package/dist/dom.d.ts +38 -0
- package/dist/dom.js +67 -0
- package/dist/elements.d.ts +48 -0
- package/dist/elements.js +378 -0
- package/dist/frame.d.ts +57 -0
- package/dist/frame.js +40 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +10 -0
- package/dist/jobs.d.ts +31 -0
- package/dist/jobs.js +52 -0
- package/dist/memory.d.ts +35 -0
- package/dist/memory.js +39 -0
- package/dist/mount-chat.d.ts +41 -0
- package/dist/mount-chat.js +178 -0
- package/dist/mount-frame.d.ts +57 -0
- package/dist/mount-frame.js +239 -0
- package/dist/mount-mask.d.ts +127 -0
- package/dist/mount-mask.js +450 -0
- package/dist/mount-model.d.ts +28 -0
- package/dist/mount-model.js +122 -0
- package/dist/mount-transcribe.d.ts +51 -0
- package/dist/mount-transcribe.js +203 -0
- package/dist/pick.d.ts +149 -0
- package/dist/pick.js +260 -0
- package/dist/rows.d.ts +42 -0
- package/dist/rows.js +149 -0
- package/dist/single/elements.iife.js +9599 -0
- package/dist/single/elements.js +9573 -0
- package/dist/single/index.iife.js +6843 -0
- package/dist/single/index.js +6807 -0
- package/dist/single/vue.iife.js +11376 -0
- package/dist/single/vue.js +11359 -0
- package/dist/transcribe.d.ts +92 -0
- package/dist/transcribe.js +175 -0
- package/dist/vue.d.ts +281 -0
- package/dist/vue.js +221 -0
- package/how.ts +26 -0
- package/llms.md +1133 -0
- package/package.json +62 -4
package/dist/pick.js
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
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 { getRepo, hfResolve } from "@hackbuild/weights";
|
|
9
|
+
import { bytesText, canLoad, loadAll } from "@virgilvox/hackbuild-ui/vanilla";
|
|
10
|
+
import { ALL_RECORDS, JOBS, jobRecords } from "./jobs.js";
|
|
11
|
+
import { rowOf } from "./rows.js";
|
|
12
|
+
/**
|
|
13
|
+
* A loader over a weight store: a record with a url fetches that file, a
|
|
14
|
+
* record with a repo and files fetches each, resuming and checking the
|
|
15
|
+
* SHA-256 when the record has one. Records a runtime fetches itself (WebLLM,
|
|
16
|
+
* transformers.js model ids) need the app's loader.
|
|
17
|
+
*/
|
|
18
|
+
/** The cache keys of a record's files in a weight store, as weightsLoader writes them. */
|
|
19
|
+
export function weightKeys(rec) {
|
|
20
|
+
const s = rec.source;
|
|
21
|
+
if (s.url && !s.repo)
|
|
22
|
+
return [s.url];
|
|
23
|
+
const base = s.repo ? hfResolve(s.repo, s.revision ?? "main") : "";
|
|
24
|
+
const files = s.files?.length ? s.files.map((f) => f.path) : s.file ? [s.file] : [];
|
|
25
|
+
return base ? files.map((f) => base + f) : [];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A loader that fetches a record's weight files through a weight store; records that load through
|
|
29
|
+
* their runtime throw.
|
|
30
|
+
*/
|
|
31
|
+
export function weightsLoader(store) {
|
|
32
|
+
return async (rec, { signal, onProgress }) => {
|
|
33
|
+
if (rec.noDownload || rec.sizeMB === 0) {
|
|
34
|
+
onProgress(1);
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const s = rec.source;
|
|
38
|
+
if (s.url && !s.repo) {
|
|
39
|
+
return store.get({ url: s.url, licence: rec.licence, ...(s.sha256 ? { sha256: s.sha256 } : {}) }, { signal, onProgress: (p) => onProgress(p.fraction) });
|
|
40
|
+
}
|
|
41
|
+
const files = s.files?.length ? s.files : s.file ? [{ path: s.file, ...(s.sha256 ? { sha256: s.sha256 } : {}) }] : [];
|
|
42
|
+
if (s.repo && files.length) {
|
|
43
|
+
return getRepo(store, { base: hfResolve(s.repo, s.revision ?? "main"), files, licence: rec.licence }, { signal, onProgress: (p) => onProgress(p.total ? p.loaded / p.total : null) });
|
|
44
|
+
}
|
|
45
|
+
throw new Error(`${rec.label ?? rec.id} loads through its runtime. the page passes a loader for it.`);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** Deletes a stored model's files from a weight store: the remove of a picker. */
|
|
49
|
+
export function weightsRemover(store) {
|
|
50
|
+
return async (rec) => {
|
|
51
|
+
for (const k of weightKeys(rec))
|
|
52
|
+
await store.evict(k);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
const OWN_PROVIDES = { chat: ["chat"], transcribe: ["transcribe"], vision: ["vision.ask", "vision.caption"], voice: ["speak"], embed: ["embed"] };
|
|
56
|
+
/** A record for a "your own" form. Its licence is unknown until the person reads the card. */
|
|
57
|
+
export function ownRecord(job, kind, v) {
|
|
58
|
+
const provides = OWN_PROVIDES[job] ?? [job];
|
|
59
|
+
const base = { provides, sizeMB: null, sizeSource: "unknown", licence: "unknown, read the model card", commercial: "unknown", speed: "steady", verdict: "your own" };
|
|
60
|
+
const slug = (s) => s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
61
|
+
switch (kind) {
|
|
62
|
+
case "gguf":
|
|
63
|
+
return { ...base, id: `own-${slug(`${v.repo}-${v.file}`)}`, label: v.file || v.repo || "gguf", provider: "wllama", source: { repo: v.repo, file: v.file }, ladder: [{ device: "wasm" }] };
|
|
64
|
+
case "onnx":
|
|
65
|
+
return { ...base, id: `own-${slug(v.repo ?? "onnx")}`, label: (v.repo ?? "").split("/").pop() || "onnx", provider: "tjs", source: { modelId: v.repo }, ladder: [{ device: "webgpu" }, { device: "wasm" }] };
|
|
66
|
+
case "mlc":
|
|
67
|
+
return { ...base, id: `own-${slug(v.url ?? "mlc")}`, label: (v.url ?? "").replace(/\/+$/, "").split("/").pop() || "mlc", provider: "webllm", source: { url: v.url }, ladder: [{ device: "webgpu" }] };
|
|
68
|
+
case "endpoint":
|
|
69
|
+
return { ...base, id: `own-${slug(v.url ?? "endpoint")}`, label: (v.url ?? "").replace(/^https?:\/\//, "").split("/")[0] || "endpoint", provider: "remote", source: { url: v.url }, ladder: [{ device: "remote" }], speed: "fast", verdict: "your endpoint" };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The row a picker starts on: the first "start here" record that fits this
|
|
74
|
+
* machine, else the first that fits and is fast or steady, else the first
|
|
75
|
+
* that fits, else the first.
|
|
76
|
+
*/
|
|
77
|
+
export function defaultPick(records, env, estimate) {
|
|
78
|
+
const fits = records.filter((r) => rowOf(r, { env, estimate }).unfit === undefined);
|
|
79
|
+
return (fits.find((r) => r.group === "start here") ?? fits.find((r) => r.speed === "fast" || r.speed === "steady") ?? fits[0] ?? records[0])?.id ?? "";
|
|
80
|
+
}
|
|
81
|
+
/** Creates a model picker for a job, with rows that say whether each model fits this machine. */
|
|
82
|
+
export function useModelPick(job, o = {}) {
|
|
83
|
+
const def = JOBS[job];
|
|
84
|
+
let records = jobRecords(job, o.records ?? ALL_RECORDS);
|
|
85
|
+
let env = o.env;
|
|
86
|
+
let estimate = o.estimate ?? null;
|
|
87
|
+
let loader = o.loader;
|
|
88
|
+
let onRemove = o.onRemove;
|
|
89
|
+
const results = new Map();
|
|
90
|
+
const own = [];
|
|
91
|
+
const state = new Map();
|
|
92
|
+
const progress = new Map();
|
|
93
|
+
const errors = new Map();
|
|
94
|
+
const options = new Map();
|
|
95
|
+
const aborts = new Map();
|
|
96
|
+
for (const id of o.stored ?? [])
|
|
97
|
+
state.set(id, "stored");
|
|
98
|
+
const all = () => [...records, ...own];
|
|
99
|
+
let chosen = o.chosen ?? defaultPick(all(), env, estimate);
|
|
100
|
+
const subs = new Set();
|
|
101
|
+
const rows = () => all().map((r) => rowOf(r, { env, estimate, state: state.get(r.id), progress: progress.get(r.id), options: options.get(r.id), extra: o.extra?.(r), own: own.includes(r) }));
|
|
102
|
+
const snapshot = () => {
|
|
103
|
+
const rs = rows();
|
|
104
|
+
return { job, title: def.title, empty: def.empty, rows: rs, chosen, row: rs.find((r) => r.id === chosen), loaded: results.get(chosen) };
|
|
105
|
+
};
|
|
106
|
+
const emit = () => {
|
|
107
|
+
const s = snapshot();
|
|
108
|
+
for (const fn of subs)
|
|
109
|
+
fn(s);
|
|
110
|
+
};
|
|
111
|
+
const pick = {
|
|
112
|
+
job,
|
|
113
|
+
snapshot,
|
|
114
|
+
subscribe(fn) {
|
|
115
|
+
subs.add(fn);
|
|
116
|
+
fn(snapshot());
|
|
117
|
+
return () => subs.delete(fn);
|
|
118
|
+
},
|
|
119
|
+
choose(id) {
|
|
120
|
+
if (!all().some((r) => r.id === id) || id === chosen)
|
|
121
|
+
return;
|
|
122
|
+
chosen = id;
|
|
123
|
+
emit();
|
|
124
|
+
},
|
|
125
|
+
async load(id = chosen) {
|
|
126
|
+
const rec = all().find((r) => r.id === id);
|
|
127
|
+
const row = rec && rowOf(rec, { env, estimate, state: state.get(id) });
|
|
128
|
+
if (!rec || !canLoad(row))
|
|
129
|
+
return;
|
|
130
|
+
if (!loader) {
|
|
131
|
+
state.set(id, "error");
|
|
132
|
+
errors.set(id, "no loader. the page passes one to load a model.");
|
|
133
|
+
emit();
|
|
134
|
+
throw new Error(`no loader for ${rec.id}: pass one to useModelPick, such as weightsLoader(store)`);
|
|
135
|
+
}
|
|
136
|
+
const ac = new AbortController();
|
|
137
|
+
aborts.set(id, ac);
|
|
138
|
+
state.set(id, "loading");
|
|
139
|
+
progress.set(id, 0);
|
|
140
|
+
errors.delete(id);
|
|
141
|
+
emit();
|
|
142
|
+
try {
|
|
143
|
+
const out = await loader(rec, {
|
|
144
|
+
signal: ac.signal,
|
|
145
|
+
options: options.get(id) ?? {},
|
|
146
|
+
onProgress: (f) => {
|
|
147
|
+
progress.set(id, f ?? progress.get(id) ?? 0);
|
|
148
|
+
emit();
|
|
149
|
+
},
|
|
150
|
+
});
|
|
151
|
+
results.set(id, out);
|
|
152
|
+
state.set(id, "loaded");
|
|
153
|
+
}
|
|
154
|
+
catch (e) {
|
|
155
|
+
const aborted = ac.signal.aborted;
|
|
156
|
+
state.set(id, aborted ? "none" : "error");
|
|
157
|
+
if (!aborted)
|
|
158
|
+
errors.set(id, e instanceof Error ? e.message : String(e));
|
|
159
|
+
if (!aborted)
|
|
160
|
+
throw e;
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
aborts.delete(id);
|
|
164
|
+
progress.delete(id);
|
|
165
|
+
emit();
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
cancel(id = chosen) {
|
|
169
|
+
aborts.get(id)?.abort();
|
|
170
|
+
},
|
|
171
|
+
async remove(id) {
|
|
172
|
+
const rec = all().find((r) => r.id === id);
|
|
173
|
+
if (!rec)
|
|
174
|
+
return;
|
|
175
|
+
await onRemove?.(rec);
|
|
176
|
+
state.delete(id);
|
|
177
|
+
results.delete(id);
|
|
178
|
+
emit();
|
|
179
|
+
},
|
|
180
|
+
option(id, key, value) {
|
|
181
|
+
options.set(id, { ...options.get(id), [key]: value });
|
|
182
|
+
emit();
|
|
183
|
+
},
|
|
184
|
+
optionsOf: (id) => ({ ...options.get(id) }),
|
|
185
|
+
custom(kind, values) {
|
|
186
|
+
const rec = ownRecord(job, kind, values);
|
|
187
|
+
const i = own.findIndex((r) => r.id === rec.id);
|
|
188
|
+
if (i >= 0)
|
|
189
|
+
own[i] = rec;
|
|
190
|
+
else
|
|
191
|
+
own.push(rec);
|
|
192
|
+
chosen = rec.id;
|
|
193
|
+
emit();
|
|
194
|
+
return rec.id;
|
|
195
|
+
},
|
|
196
|
+
update(n) {
|
|
197
|
+
if ("env" in n)
|
|
198
|
+
env = n.env;
|
|
199
|
+
if ("estimate" in n)
|
|
200
|
+
estimate = n.estimate ?? null;
|
|
201
|
+
if (n.records)
|
|
202
|
+
records = jobRecords(job, n.records);
|
|
203
|
+
if (n.loader)
|
|
204
|
+
loader = n.loader;
|
|
205
|
+
if (n.onRemove)
|
|
206
|
+
onRemove = n.onRemove;
|
|
207
|
+
emit();
|
|
208
|
+
},
|
|
209
|
+
result: (id) => results.get(id),
|
|
210
|
+
record: (id) => all().find((r) => r.id === id),
|
|
211
|
+
error: (id) => errors.get(id),
|
|
212
|
+
};
|
|
213
|
+
return pick;
|
|
214
|
+
}
|
|
215
|
+
/** Creates one picker per job and joins them as a rack with presets and load all. */
|
|
216
|
+
export function useJobsPick(jobs, initialPresets = [], shared = {}) {
|
|
217
|
+
let presets = initialPresets;
|
|
218
|
+
const extra = new Set();
|
|
219
|
+
const picks = new Map(jobs.map((j) => [j.job, useModelPick(j.job, { ...shared, ...j.options })]));
|
|
220
|
+
const required = new Map(jobs.map((j) => [j.job, !!j.required]));
|
|
221
|
+
const chosenRows = () => [...picks.values()].map((p) => p.snapshot().row);
|
|
222
|
+
return {
|
|
223
|
+
picks,
|
|
224
|
+
lines: () => [...picks].map(([job, p]) => ({ job, label: JOBS[job].label, required: required.get(job) ?? false, row: p.snapshot().row })),
|
|
225
|
+
presets: () => presets.map((pr) => {
|
|
226
|
+
let bytes = 0;
|
|
227
|
+
for (const [job, id] of Object.entries(pr.picks)) {
|
|
228
|
+
const row = picks.get(job)?.snapshot().rows.find((r) => r.id === id);
|
|
229
|
+
if (row && row.state !== "stored" && row.state !== "loaded")
|
|
230
|
+
bytes += row.bytes ?? 0;
|
|
231
|
+
}
|
|
232
|
+
return { id: pr.id, label: pr.label, total: bytes > 0 ? `about ${bytesText(bytes)}` : "nothing to fetch" };
|
|
233
|
+
}),
|
|
234
|
+
applyPreset(id) {
|
|
235
|
+
const pr = presets.find((p) => p.id === id);
|
|
236
|
+
if (!pr)
|
|
237
|
+
return;
|
|
238
|
+
for (const [job, mid] of Object.entries(pr.picks))
|
|
239
|
+
picks.get(job)?.choose(mid);
|
|
240
|
+
},
|
|
241
|
+
all: () => loadAll(chosenRows()),
|
|
242
|
+
async loadAll() {
|
|
243
|
+
await Promise.all([...picks.values()].map((p) => p.load().catch(() => undefined)));
|
|
244
|
+
},
|
|
245
|
+
subscribe(fn) {
|
|
246
|
+
const offs = [...picks.values()].map((p) => p.subscribe(() => fn()));
|
|
247
|
+
extra.add(fn);
|
|
248
|
+
return () => (offs.forEach((off) => off()), extra.delete(fn));
|
|
249
|
+
},
|
|
250
|
+
update(o) {
|
|
251
|
+
for (const p of picks.values())
|
|
252
|
+
p.update(o);
|
|
253
|
+
},
|
|
254
|
+
setPresets(p) {
|
|
255
|
+
presets = p;
|
|
256
|
+
for (const fn of extra)
|
|
257
|
+
fn();
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
}
|
package/dist/rows.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalogue records to hackbuild-ui ModelRow: every word a person reads in
|
|
3
|
+
* the picker is written here, once. The kit draws rows and never reads a
|
|
4
|
+
* record; this module never draws.
|
|
5
|
+
*/
|
|
6
|
+
import type { Env } from "@hackbuild/ai-env";
|
|
7
|
+
import { type ModelRecord, type StorageEstimate } from "@hackbuild/models";
|
|
8
|
+
import { type ModelBackend, type ModelOption, type ModelPickState, type ModelRow, type ModelRowGroup } from "@virgilvox/hackbuild-ui/vanilla";
|
|
9
|
+
/** Whether a record has nothing to download: a built in, system or zero size model. */
|
|
10
|
+
export declare function isNoDownload(r: ModelRecord): boolean;
|
|
11
|
+
/** Whether a record's censorship is "uncensored" or "abliterated". */
|
|
12
|
+
export declare function isUncensored(r: ModelRecord): boolean;
|
|
13
|
+
/** The group a row sits in. A model on this machine leads; else the record's group, else by size. */
|
|
14
|
+
export declare function groupOf(r: ModelRecord, state?: ModelPickState): ModelRowGroup;
|
|
15
|
+
/** Where it runs here: the first rung this machine can take, else the record's first. */
|
|
16
|
+
export declare function backendOf(r: ModelRecord, env?: Env): ModelBackend;
|
|
17
|
+
/** Download words: "1.7 gb". Null for a record with no download or an unknown size. */
|
|
18
|
+
export declare function sizeWords(r: ModelRecord): string | undefined;
|
|
19
|
+
/** The chevron detail: licence, source, context, quant, runtime, and where the size came from. */
|
|
20
|
+
export declare function detailOf(r: ModelRecord): {
|
|
21
|
+
label: string;
|
|
22
|
+
value: string;
|
|
23
|
+
}[];
|
|
24
|
+
/** The options a chosen row offers: backend when the ladder has gpu and cpu, quant when it has several, context for chat. */
|
|
25
|
+
export declare function optionsOf(r: ModelRecord, values?: Record<string, string>, extra?: readonly ModelOption[]): ModelOption[];
|
|
26
|
+
/**
|
|
27
|
+
* What a row needs beyond its record: env, storage estimate, load state and progress, option
|
|
28
|
+
* values, job options, and whether the person added it.
|
|
29
|
+
*/
|
|
30
|
+
export interface RowContext {
|
|
31
|
+
env?: Env | undefined;
|
|
32
|
+
estimate?: StorageEstimate | null | undefined;
|
|
33
|
+
state?: ModelPickState | undefined;
|
|
34
|
+
progress?: number | undefined;
|
|
35
|
+
options?: Record<string, string> | undefined;
|
|
36
|
+
/** Options a job adds, like a language for speech. */
|
|
37
|
+
extra?: readonly ModelOption[] | undefined;
|
|
38
|
+
/** A row the person added by hand. */
|
|
39
|
+
own?: boolean | undefined;
|
|
40
|
+
}
|
|
41
|
+
/** One record as one picker row. */
|
|
42
|
+
export declare function rowOf(r: ModelRecord, ctx?: RowContext): ModelRow;
|
package/dist/rows.js
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Catalogue records to hackbuild-ui ModelRow: every word a person reads in
|
|
3
|
+
* the picker is written here, once. The kit draws rows and never reads a
|
|
4
|
+
* record; this module never draws.
|
|
5
|
+
*/
|
|
6
|
+
import { fitOf, ladderFor } from "@hackbuild/models";
|
|
7
|
+
import { bytesText } from "@virgilvox/hackbuild-ui/vanilla";
|
|
8
|
+
const MIB = 1024 * 1024;
|
|
9
|
+
/** Catalogue group words to the sheet's groups. */
|
|
10
|
+
const GROUPS = {
|
|
11
|
+
"start here": "start",
|
|
12
|
+
tiny: "small",
|
|
13
|
+
cpu: "small",
|
|
14
|
+
good: "better",
|
|
15
|
+
uncensored: "uncensored",
|
|
16
|
+
reasoning: "reasoning",
|
|
17
|
+
"no download": "none",
|
|
18
|
+
own: "own",
|
|
19
|
+
};
|
|
20
|
+
/** Whether a record has nothing to download: a built in, system or zero size model. */
|
|
21
|
+
export function isNoDownload(r) {
|
|
22
|
+
return !!r.noDownload || r.sizeMB === 0 || r.provider === "prompt-api" || r.provider === "system";
|
|
23
|
+
}
|
|
24
|
+
/** Whether a record's censorship is "uncensored" or "abliterated". */
|
|
25
|
+
export function isUncensored(r) {
|
|
26
|
+
return r.censorship === "uncensored" || r.censorship === "abliterated";
|
|
27
|
+
}
|
|
28
|
+
/** The group a row sits in. A model on this machine leads; else the record's group, else by size. */
|
|
29
|
+
export function groupOf(r, state = "none") {
|
|
30
|
+
if (state === "stored" || state === "loaded")
|
|
31
|
+
return "here";
|
|
32
|
+
if (r.group && GROUPS[r.group])
|
|
33
|
+
return GROUPS[r.group];
|
|
34
|
+
if (isNoDownload(r))
|
|
35
|
+
return "none";
|
|
36
|
+
if (isUncensored(r))
|
|
37
|
+
return "uncensored";
|
|
38
|
+
return r.sizeMB !== null && r.sizeMB < 600 ? "small" : "better";
|
|
39
|
+
}
|
|
40
|
+
/** Where it runs here: the first rung this machine can take, else the record's first. */
|
|
41
|
+
export function backendOf(r, env) {
|
|
42
|
+
if (r.provider === "remote")
|
|
43
|
+
return "remote";
|
|
44
|
+
const rung = ladderFor(r, env).rungs[0] ?? r.ladder[0];
|
|
45
|
+
switch (rung?.device) {
|
|
46
|
+
case "webgpu":
|
|
47
|
+
case "webgl":
|
|
48
|
+
return "gpu";
|
|
49
|
+
case "browser":
|
|
50
|
+
return "browser";
|
|
51
|
+
case "remote":
|
|
52
|
+
return "remote";
|
|
53
|
+
default:
|
|
54
|
+
return "cpu";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/** Download words: "1.7 gb". Null for a record with no download or an unknown size. */
|
|
58
|
+
export function sizeWords(r) {
|
|
59
|
+
if (isNoDownload(r) || r.sizeMB === null)
|
|
60
|
+
return undefined;
|
|
61
|
+
return bytesText(r.sizeMB * MIB);
|
|
62
|
+
}
|
|
63
|
+
/** The record's label, else its id made readable: "hermes 3 llama 3.2 3b". */
|
|
64
|
+
const pretty = (r) => r.label ?? r.id.replace(/-(mlc|gguf)$/, "").replace(/-/g, " ");
|
|
65
|
+
/** The chevron detail: licence, source, context, quant, runtime, and where the size came from. */
|
|
66
|
+
export function detailOf(r) {
|
|
67
|
+
const out = [];
|
|
68
|
+
const use = r.commercial === true ? "commercial use allowed" : r.commercial === false ? "not for commercial use" : "commercial use unknown";
|
|
69
|
+
out.push({ label: "licence", value: `${r.licence}. ${use}` });
|
|
70
|
+
const src = r.source.repo ?? r.source.modelId ?? r.source.url;
|
|
71
|
+
if (src)
|
|
72
|
+
out.push({ label: "source", value: r.source.file ? `${src}, ${r.source.file}` : src });
|
|
73
|
+
if (r.sizeMB !== null && !isNoDownload(r))
|
|
74
|
+
out.push({ label: "size", value: `${bytesText(r.sizeMB * MIB)}, ${r.sizeSource}` });
|
|
75
|
+
if (r.vramMB != null)
|
|
76
|
+
out.push({ label: "gpu memory", value: `${bytesText(r.vramMB * MIB)}, ${r.vramSource ?? "unknown"}` });
|
|
77
|
+
if (r.ctx)
|
|
78
|
+
out.push({ label: "context", value: `${r.ctx} tokens` });
|
|
79
|
+
if (r.quant)
|
|
80
|
+
out.push({ label: "quant", value: r.quant });
|
|
81
|
+
if (r.runtime)
|
|
82
|
+
out.push({ label: "runtime", value: r.runtime });
|
|
83
|
+
for (const n of r.notes ?? [])
|
|
84
|
+
out.push({ label: "note", value: n });
|
|
85
|
+
for (const w of r.quirks?.warnings ?? [])
|
|
86
|
+
out.push({ label: "warning", value: w });
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
/** Context sizes a chat record can take, up to its own. */
|
|
90
|
+
const CTX = [2048, 4096, 8192, 16384, 32768];
|
|
91
|
+
/** The options a chosen row offers: backend when the ladder has gpu and cpu, quant when it has several, context for chat. */
|
|
92
|
+
export function optionsOf(r, values = {}, extra = []) {
|
|
93
|
+
const out = [];
|
|
94
|
+
const devices = new Set(r.ladder.map((g) => (g.device === "webgpu" ? "webgpu" : g.device === "wasm" || g.device === "cpu" ? "wasm" : g.device)));
|
|
95
|
+
if (devices.has("webgpu") && devices.has("wasm")) {
|
|
96
|
+
out.push({ key: "backend", label: "backend", value: values.backend ?? "auto", choices: [{ value: "auto", label: "auto" }, { value: "webgpu", label: "webgpu" }, { value: "wasm", label: "wasm, cpu" }] });
|
|
97
|
+
}
|
|
98
|
+
const dtypes = [...new Set(r.ladder.map((g) => (typeof g.dtype === "string" ? g.dtype : null)).filter((d) => !!d))];
|
|
99
|
+
if (dtypes.length > 1)
|
|
100
|
+
out.push({ key: "quant", label: "quant", value: values.quant ?? dtypes[0], choices: dtypes.map((d) => ({ value: d, label: d })) });
|
|
101
|
+
if (r.provides.includes("chat") && r.ctx && r.provider !== "remote") {
|
|
102
|
+
const sizes = CTX.filter((c) => c <= Math.max(r.ctx, 2048));
|
|
103
|
+
if (sizes.length > 1)
|
|
104
|
+
out.push({ key: "context", label: "context", value: values.context ?? String(r.ctx), choices: sizes.map((c) => ({ value: String(c), label: `${c / 1024}k` })) });
|
|
105
|
+
}
|
|
106
|
+
return [...out, ...extra];
|
|
107
|
+
}
|
|
108
|
+
/** One record as one picker row. */
|
|
109
|
+
export function rowOf(r, ctx = {}) {
|
|
110
|
+
const state = ctx.state ?? "none";
|
|
111
|
+
// the system voice runs wherever speechSynthesis does; its ladder's browser rung reads as a prompt api need
|
|
112
|
+
const fit = r.provider === "system" ? { fit: "fits", ok: true, reason: "" } : fitOf(r, ctx.env, ctx.estimate ?? null);
|
|
113
|
+
const size = sizeWords(r);
|
|
114
|
+
const row = {
|
|
115
|
+
id: r.id,
|
|
116
|
+
name: pretty(r),
|
|
117
|
+
group: ctx.own ? "own" : groupOf(r, state),
|
|
118
|
+
backend: backendOf(r, ctx.env),
|
|
119
|
+
state,
|
|
120
|
+
};
|
|
121
|
+
if (size)
|
|
122
|
+
row.size = size;
|
|
123
|
+
if (r.vramMB != null)
|
|
124
|
+
row.memory = `needs ${bytesText(r.vramMB * MIB)}`;
|
|
125
|
+
if (r.sizeMB !== null && !isNoDownload(r))
|
|
126
|
+
row.bytes = r.sizeMB * MIB;
|
|
127
|
+
const speed = fit.fit === "fits on cpu, slow" ? "slow" : r.speed;
|
|
128
|
+
if (speed)
|
|
129
|
+
row.speed = speed;
|
|
130
|
+
if (r.verdict ?? r.badge)
|
|
131
|
+
row.verdict = (r.verdict ?? r.badge);
|
|
132
|
+
if (isUncensored(r))
|
|
133
|
+
row.uncensored = true;
|
|
134
|
+
if (r.commercial === false)
|
|
135
|
+
row.noncommercial = true;
|
|
136
|
+
if (isNoDownload(r))
|
|
137
|
+
row.noDownload = true;
|
|
138
|
+
if (!fit.ok)
|
|
139
|
+
row.unfit = fit.reason;
|
|
140
|
+
if (state === "loading")
|
|
141
|
+
row.progress = ctx.progress ?? 0;
|
|
142
|
+
const detail = detailOf(r);
|
|
143
|
+
if (detail.length)
|
|
144
|
+
row.detail = detail;
|
|
145
|
+
const options = optionsOf(r, ctx.options, ctx.extra);
|
|
146
|
+
if (options.length)
|
|
147
|
+
row.options = options;
|
|
148
|
+
return row;
|
|
149
|
+
}
|