tryharness 0.7.59 → 0.7.61
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/lib/boardProtocol.json +24 -2
- package/lib/cards.mjs +2 -1
- package/lib/commands.mjs +33 -1
- package/lib/daemon.mjs +32 -0
- package/lib/environments.mjs +8 -1
- package/lib/inference.mjs +115 -205
- package/lib/landed.mjs +36 -1
- package/lib/library.mjs +3 -0
- package/lib/listVersions.mjs +61 -0
- package/lib/localmodels.mjs +2 -1
- package/lib/ocr.mjs +54 -22
- package/lib/onboard.mjs +9 -6
- package/lib/onnxWorker.mjs +191 -0
- package/lib/ownMachines.mjs +164 -9
- package/lib/phones.mjs +30 -0
- package/lib/readers.mjs +15 -8
- package/lib/recentRead.mjs +42 -0
- package/lib/sessionControl.mjs +2 -2
- package/lib/sessionIndex.mjs +2 -2
- package/lib/speech.mjs +190 -175
- package/lib/swarm.mjs +42 -12
- package/lib/threadActions.mjs +6 -2
- package/lib/threadRuns.mjs +53 -62
- package/npm-shrinkwrap.json +14 -14
- package/package.json +4 -4
- package/release.json +27 -24
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
// A list a reader keeps a copy of (the buddy's threads, board.swarm.list), sent as what changed since the reader's copy
|
|
4
|
+
// instead of whole: on pop-os, the buddy read all 267 of the person's threads, 435 KB from the computer at home, on
|
|
5
|
+
// every move of the board. Each item is numbered by the version it last changed at; an item gone is remembered with the
|
|
6
|
+
// version it went at. Versions start again with each run of the daemon (its epoch), and a reader from another run, or
|
|
7
|
+
// from before the removals still remembered, gets the whole list.
|
|
8
|
+
|
|
9
|
+
// Items gone that a reader may still hold; one further behind than these gets the whole list.
|
|
10
|
+
const GONE_KEPT = 1_000;
|
|
11
|
+
|
|
12
|
+
export function createListVersions() {
|
|
13
|
+
const epoch = randomUUID();
|
|
14
|
+
let version = 0;
|
|
15
|
+
const current = new Map();
|
|
16
|
+
const gone = new Map();
|
|
17
|
+
// A reader behind this version may miss a removal: gone ones before it are forgotten.
|
|
18
|
+
let forgotten = 0;
|
|
19
|
+
const digestOf = (item) =>
|
|
20
|
+
createHash("sha256").update(JSON.stringify(item)).digest("base64url");
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The list for a reader that holds it up to `after` (from `epoch`): { items, gone, whole, version: { epoch, at, count } }.
|
|
24
|
+
* `items` are those changed since, or all of them (`whole`) when the reader holds none from this run; `gone` the ids
|
|
25
|
+
* removed since; `count` how many there are, so a reader whose copy differs asks for all of it.
|
|
26
|
+
*/
|
|
27
|
+
return function since(items, { epoch: theirs, after } = {}) {
|
|
28
|
+
const present = new Set();
|
|
29
|
+
for (const item of items) {
|
|
30
|
+
present.add(item.id);
|
|
31
|
+
const digest = digestOf(item);
|
|
32
|
+
if (current.get(item.id)?.digest === digest) continue;
|
|
33
|
+
current.set(item.id, { digest, at: ++version });
|
|
34
|
+
gone.delete(item.id);
|
|
35
|
+
}
|
|
36
|
+
for (const id of [...current.keys()]) {
|
|
37
|
+
if (present.has(id)) continue;
|
|
38
|
+
current.delete(id);
|
|
39
|
+
gone.set(id, ++version);
|
|
40
|
+
}
|
|
41
|
+
for (const [id, at] of [...gone].slice(
|
|
42
|
+
0,
|
|
43
|
+
Math.max(0, gone.size - GONE_KEPT)
|
|
44
|
+
)) {
|
|
45
|
+
gone.delete(id);
|
|
46
|
+
forgotten = at;
|
|
47
|
+
}
|
|
48
|
+
const whole =
|
|
49
|
+
theirs !== epoch || !Number.isInteger(after) || after < forgotten;
|
|
50
|
+
return {
|
|
51
|
+
items: whole
|
|
52
|
+
? items
|
|
53
|
+
: items.filter((item) => current.get(item.id).at > after),
|
|
54
|
+
gone: whole
|
|
55
|
+
? []
|
|
56
|
+
: [...gone].filter(([, at]) => at > after).map(([id]) => id),
|
|
57
|
+
whole,
|
|
58
|
+
version: { epoch, at: version, count: items.length },
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
}
|
package/lib/localmodels.mjs
CHANGED
|
@@ -162,7 +162,8 @@ export const MODELS = [
|
|
|
162
162
|
size: 2031,
|
|
163
163
|
},
|
|
164
164
|
],
|
|
165
|
-
|
|
165
|
+
// By name: the model list goes to the inference worker (onnxWorker.mjs), which cannot be handed a function.
|
|
166
|
+
tokenizer: { from: "guard", prepare: "unhide" },
|
|
166
167
|
},
|
|
167
168
|
{
|
|
168
169
|
id: "credentials",
|
package/lib/ocr.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import pngjs from "pngjs";
|
|
4
5
|
import jpeg from "jpeg-js";
|
|
@@ -335,43 +336,74 @@ const readingOrder = (lines) =>
|
|
|
335
336
|
: a.box[1] - b.box[1]
|
|
336
337
|
);
|
|
337
338
|
|
|
339
|
+
// Pictures being read, by their read's token, on the model thread (onnxWorker.mjs) that runs the jobs below: decoded
|
|
340
|
+
// once by the detector's job and read by the recogniser's, then dropped. A read that failed partway leaves its picture
|
|
341
|
+
// here until newer reads push it out.
|
|
342
|
+
const OPEN_PICTURES = 4;
|
|
343
|
+
const pictures = new Map();
|
|
344
|
+
let characters = null;
|
|
345
|
+
|
|
346
|
+
/** The detector's job, on the model thread: decodes the picture, keeps it for the recogniser and finds its text boxes. */
|
|
347
|
+
export async function detectText(model, token, input) {
|
|
348
|
+
const image = input?.data && input.width ? input : decodeImage(input);
|
|
349
|
+
if (!image) throw new Error("only PNG and JPEG images can be read");
|
|
350
|
+
const boxes = await detect(image, model);
|
|
351
|
+
if (boxes.length) {
|
|
352
|
+
pictures.set(token, image);
|
|
353
|
+
for (const old of [...pictures.keys()].slice(0, -OPEN_PICTURES))
|
|
354
|
+
pictures.delete(old);
|
|
355
|
+
}
|
|
356
|
+
return { width: image.width, height: image.height, boxes };
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** A recogniser job, on the model thread: some of the picture's boxes read; the last one lets the picture go. */
|
|
360
|
+
export async function readBoxes(model, token, boxes, last) {
|
|
361
|
+
const image = pictures.get(token);
|
|
362
|
+
if (!image) throw new Error("the picture being read was let go");
|
|
363
|
+
characters ||= characterList(
|
|
364
|
+
fs.readFileSync(path.join(model.dir, "rec", "inference.yml"), "utf8")
|
|
365
|
+
);
|
|
366
|
+
const read = [];
|
|
367
|
+
for (const box of boxes)
|
|
368
|
+
read.push(await recognise(image, box, characters, model));
|
|
369
|
+
if (last) pictures.delete(token);
|
|
370
|
+
return read;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const DETECT = { module: import.meta.url, name: "detectText" };
|
|
374
|
+
const READ_BOXES = { module: import.meta.url, name: "readBoxes" };
|
|
375
|
+
|
|
338
376
|
export function createOcr({ inference }) {
|
|
339
|
-
let characters = null;
|
|
340
377
|
/**
|
|
341
378
|
* The text in an image: { width, height, lines: [{ text, box: [x0, y0, x1, y1], score, chars: [[x0, x1]] }], ms }, boxes
|
|
342
379
|
* in the image's pixels. Takes PNG or JPEG bytes or a decoded { width, height, data: RGBA }.
|
|
343
380
|
*/
|
|
344
381
|
const read = async (input, { lane = "handoff" } = {}) => {
|
|
345
382
|
const started = performance.now();
|
|
346
|
-
const
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
383
|
+
const token = randomUUID();
|
|
384
|
+
const { width, height, boxes } = await inference.onnx(
|
|
385
|
+
"ocr",
|
|
386
|
+
DETECT,
|
|
387
|
+
[token, input],
|
|
388
|
+
{ lane }
|
|
389
|
+
);
|
|
351
390
|
const lines = [];
|
|
352
|
-
for (let i = 0; i < boxes.length; i += REC_PER_JOB)
|
|
391
|
+
for (let i = 0; i < boxes.length; i += REC_PER_JOB)
|
|
353
392
|
lines.push(
|
|
354
393
|
...(await inference.onnx(
|
|
355
394
|
"ocr",
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
);
|
|
363
|
-
const read = [];
|
|
364
|
-
for (const box of boxes.slice(i, i + REC_PER_JOB))
|
|
365
|
-
read.push(await recognise(image, box, characters, model));
|
|
366
|
-
return read;
|
|
367
|
-
},
|
|
395
|
+
READ_BOXES,
|
|
396
|
+
[
|
|
397
|
+
token,
|
|
398
|
+
boxes.slice(i, i + REC_PER_JOB),
|
|
399
|
+
i + REC_PER_JOB >= boxes.length,
|
|
400
|
+
],
|
|
368
401
|
{ lane }
|
|
369
402
|
))
|
|
370
403
|
);
|
|
371
|
-
}
|
|
372
404
|
return {
|
|
373
|
-
width
|
|
374
|
-
height
|
|
405
|
+
width,
|
|
406
|
+
height,
|
|
375
407
|
lines: readingOrder(lines.filter((line) => line.text)),
|
|
376
408
|
ms: Math.round(performance.now() - started),
|
|
377
409
|
};
|
package/lib/onboard.mjs
CHANGED
|
@@ -70,17 +70,17 @@ const copy = (text) => {
|
|
|
70
70
|
return result.status === 0;
|
|
71
71
|
};
|
|
72
72
|
|
|
73
|
-
// How long setup waits for
|
|
74
|
-
//
|
|
75
|
-
|
|
76
|
-
const
|
|
73
|
+
// How long setup waits for any agent's answer to its check before moving on: a minute at most (owner, October 27: "10
|
|
74
|
+
// minutes is entirely too long"). Grok Bot's email wakes have taken up to 59 s, so a slow one can miss it; the person
|
|
75
|
+
// skips the wait with Enter and tests again with `agents test`.
|
|
76
|
+
const ANSWER_WAIT_MS = 60_000;
|
|
77
77
|
|
|
78
78
|
/** Post a setup check for the agent and wait for its report: the same path real work takes. Enter skips the wait. */
|
|
79
79
|
export async function testAgent({
|
|
80
80
|
agent,
|
|
81
81
|
daemonRequest,
|
|
82
82
|
ui = createUi(),
|
|
83
|
-
waitMs = ANSWER_WAIT_MS
|
|
83
|
+
waitMs = ANSWER_WAIT_MS,
|
|
84
84
|
sleep = (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
85
85
|
}) {
|
|
86
86
|
const name = LABEL[agent] || agent;
|
|
@@ -675,7 +675,10 @@ export async function setupElsewhere({
|
|
|
675
675
|
: [];
|
|
676
676
|
const joined = [];
|
|
677
677
|
for (const id of joining) {
|
|
678
|
-
const added = await daemonRequest({
|
|
678
|
+
const added = await daemonRequest({
|
|
679
|
+
command: "swarm.add",
|
|
680
|
+
target: id,
|
|
681
|
+
}).catch(() => null);
|
|
679
682
|
if (added?.ok) joined.push({ id, name: added.name });
|
|
680
683
|
}
|
|
681
684
|
if (joined.length)
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { parentPort, workerData } from "node:worker_threads";
|
|
4
|
+
import { modelById, modelDir, modelFile, unhide } from "./localmodels.mjs";
|
|
5
|
+
import { loadCompactTokenizer } from "./tokenizer.mjs";
|
|
6
|
+
|
|
7
|
+
// The ONNX half of the inference host (inference.mjs), on a thread of its own. ONNX Runtime runs a model synchronously
|
|
8
|
+
// on the thread that calls it (onnxruntime-node's run is the native call inside a setImmediate), so on the daemon's
|
|
9
|
+
// thread every OCR band, guard window and speech step held up every request: up to 0.8 s at a time while a fresh install
|
|
10
|
+
// read the pictures in the person's folders. Here the sessions, their tokenizers and each job's own work around them
|
|
11
|
+
// (decoding a picture, preparing a tensor, reading the outputs) run beside the models, and the daemon only waits for
|
|
12
|
+
// the answer. The daemon's scheduler still decides which job runs next and when a model is unloaded; this thread runs
|
|
13
|
+
// one job at a time, as it is sent them.
|
|
14
|
+
|
|
15
|
+
const { root, list } = workerData;
|
|
16
|
+
// A tokenizer step named in the model list, which cannot carry a function to this thread.
|
|
17
|
+
const PREPARE = { unhide };
|
|
18
|
+
const SESSION_OPTIONS = {
|
|
19
|
+
executionProviders: ["cpu"],
|
|
20
|
+
interOpNumThreads: 1,
|
|
21
|
+
logSeverityLevel: 3,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
let runtime = null;
|
|
25
|
+
const ort = () => (runtime ??= import("onnxruntime-node"));
|
|
26
|
+
|
|
27
|
+
// Models on one vocabulary share its tokenizer while any of them is open; it goes with the last of them.
|
|
28
|
+
const tokenizers = new Map();
|
|
29
|
+
const tokenizerUsers = new Map();
|
|
30
|
+
async function tokenizerOf(source, user) {
|
|
31
|
+
const dir = modelDir(root, source);
|
|
32
|
+
if (!tokenizerUsers.has(dir)) tokenizerUsers.set(dir, new Set());
|
|
33
|
+
tokenizerUsers.get(dir).add(user);
|
|
34
|
+
if (!tokenizers.has(dir)) {
|
|
35
|
+
const tokenizerFile = source.files.find((file) =>
|
|
36
|
+
file.path.endsWith("tokenizer.json")
|
|
37
|
+
);
|
|
38
|
+
const configPath = source.files.find((file) =>
|
|
39
|
+
file.path.endsWith("tokenizer_config.json")
|
|
40
|
+
)?.path;
|
|
41
|
+
const tokenizerPath = path.join(dir, tokenizerFile.path);
|
|
42
|
+
// Saved under the file's pinned hash, so a new tokenizer.json never meets old tables.
|
|
43
|
+
const tablesPath = path.join(
|
|
44
|
+
root,
|
|
45
|
+
"tokenizers",
|
|
46
|
+
`${tokenizerFile.sha256}.bin`
|
|
47
|
+
);
|
|
48
|
+
const library = async () => {
|
|
49
|
+
const { Tokenizer } = await import("@huggingface/tokenizers");
|
|
50
|
+
return new Tokenizer(
|
|
51
|
+
JSON.parse(fs.readFileSync(tokenizerPath, "utf8")),
|
|
52
|
+
configPath
|
|
53
|
+
? JSON.parse(fs.readFileSync(path.join(dir, configPath), "utf8"))
|
|
54
|
+
: {}
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
tokenizers.set(
|
|
58
|
+
dir,
|
|
59
|
+
loadCompactTokenizer(tokenizerPath, tablesPath) ?? (await library())
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
return tokenizers.get(dir);
|
|
63
|
+
}
|
|
64
|
+
function releaseTokenizer(dir, user) {
|
|
65
|
+
const users = tokenizerUsers.get(dir);
|
|
66
|
+
users?.delete(user);
|
|
67
|
+
if (users?.size) return;
|
|
68
|
+
tokenizerUsers.delete(dir);
|
|
69
|
+
tokenizers.delete(dir);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Each open model: its graphs by file (a classifier has one, OCR and speech several), opened on first use, and the
|
|
73
|
+
// tokenizer a classifier reads with.
|
|
74
|
+
const models = new Map();
|
|
75
|
+
function modelOf(id) {
|
|
76
|
+
const entry = modelById(id, list);
|
|
77
|
+
if (!entry || entry.runtime !== "onnx")
|
|
78
|
+
throw new Error(`no ONNX model ${id}`);
|
|
79
|
+
if (!models.has(id)) models.set(id, { entry, graphs: new Map() });
|
|
80
|
+
return models.get(id);
|
|
81
|
+
}
|
|
82
|
+
async function graph(model, file) {
|
|
83
|
+
if (!model.graphs.has(file)) {
|
|
84
|
+
const opened = ort().then((runtime) =>
|
|
85
|
+
runtime.InferenceSession.create(file, {
|
|
86
|
+
...SESSION_OPTIONS,
|
|
87
|
+
intraOpNumThreads: model.entry.threads ?? 1,
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
model.graphs.set(file, opened);
|
|
91
|
+
opened.catch(() => model.graphs.delete(file));
|
|
92
|
+
}
|
|
93
|
+
return model.graphs.get(file);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** What a job runs against: ONNX Runtime, the model's graphs opened by their path in its folder, and the folder. */
|
|
97
|
+
async function graphsOf(id) {
|
|
98
|
+
const model = modelOf(id);
|
|
99
|
+
const dir = modelDir(root, model.entry);
|
|
100
|
+
return {
|
|
101
|
+
ort: await ort(),
|
|
102
|
+
graph: (file) => graph(model, path.join(dir, ...file.split("/"))),
|
|
103
|
+
dir,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Runs a sequence classifier on each encoded input; returns the first output's leading row (its logits). */
|
|
108
|
+
async function logits(id, pairs, maxLength = 512) {
|
|
109
|
+
const model = modelOf(id);
|
|
110
|
+
const { entry } = model;
|
|
111
|
+
const source = entry.tokenizer
|
|
112
|
+
? modelById(entry.tokenizer.from, list)
|
|
113
|
+
: entry;
|
|
114
|
+
if (!model.tokenizer) {
|
|
115
|
+
model.tokenizer = await tokenizerOf(source, id);
|
|
116
|
+
model.tokenizerDir = modelDir(root, source);
|
|
117
|
+
}
|
|
118
|
+
const prepare = PREPARE[entry.tokenizer?.prepare] ?? ((text) => text);
|
|
119
|
+
const runtime = await ort();
|
|
120
|
+
const onnx = await graph(model, modelFile(root, entry));
|
|
121
|
+
const rows = [];
|
|
122
|
+
for (const [text, pair] of pairs) {
|
|
123
|
+
const encoded = model.tokenizer.encode(
|
|
124
|
+
prepare(text),
|
|
125
|
+
pair === undefined
|
|
126
|
+
? { add_special_tokens: true }
|
|
127
|
+
: { text_pair: prepare(pair), add_special_tokens: true }
|
|
128
|
+
);
|
|
129
|
+
const ids = encoded.ids.slice(0, maxLength);
|
|
130
|
+
const feeds = {};
|
|
131
|
+
for (const name of onnx.inputNames) {
|
|
132
|
+
if (name === "input_ids")
|
|
133
|
+
feeds[name] = new runtime.Tensor(
|
|
134
|
+
"int64",
|
|
135
|
+
BigInt64Array.from(ids, BigInt),
|
|
136
|
+
[1, ids.length]
|
|
137
|
+
);
|
|
138
|
+
else if (name === "attention_mask")
|
|
139
|
+
feeds[name] = new runtime.Tensor(
|
|
140
|
+
"int64",
|
|
141
|
+
new BigInt64Array(ids.length).fill(1n),
|
|
142
|
+
[1, ids.length]
|
|
143
|
+
);
|
|
144
|
+
else if (name === "token_type_ids")
|
|
145
|
+
feeds[name] = new runtime.Tensor(
|
|
146
|
+
"int64",
|
|
147
|
+
BigInt64Array.from(
|
|
148
|
+
(encoded.type_ids ?? ids.map(() => 0)).slice(0, ids.length),
|
|
149
|
+
BigInt
|
|
150
|
+
),
|
|
151
|
+
[1, ids.length]
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
const out = await onnx.run(feeds);
|
|
155
|
+
rows.push(Array.from(out[onnx.outputNames[0]].data, Number));
|
|
156
|
+
}
|
|
157
|
+
return rows;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async function close(id) {
|
|
161
|
+
const model = models.get(id);
|
|
162
|
+
models.delete(id);
|
|
163
|
+
if (!model) return;
|
|
164
|
+
if (model.tokenizerDir) releaseTokenizer(model.tokenizerDir, id);
|
|
165
|
+
for (const opened of model.graphs.values())
|
|
166
|
+
await opened.then((session) => session.release?.()).catch(() => {});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const OPS = {
|
|
170
|
+
logits: ({ id, pairs, maxLength }) => logits(id, pairs, maxLength),
|
|
171
|
+
// A job's own function, by the module that exports it: run({ ort, graph(file), dir }, ...args).
|
|
172
|
+
task: async ({ id, module, name, args }) =>
|
|
173
|
+
(await import(module))[name](await graphsOf(id), ...args),
|
|
174
|
+
open: async ({ id, files }) => {
|
|
175
|
+
const { graph: open } = await graphsOf(id);
|
|
176
|
+
await Promise.all(files.map((file) => open(file)));
|
|
177
|
+
},
|
|
178
|
+
close: ({ id }) => close(id),
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
parentPort.on("message", async ({ job, op, ...request }) => {
|
|
182
|
+
try {
|
|
183
|
+
parentPort.postMessage({ job, ok: true, value: await OPS[op](request) });
|
|
184
|
+
} catch (error) {
|
|
185
|
+
parentPort.postMessage({
|
|
186
|
+
job,
|
|
187
|
+
ok: false,
|
|
188
|
+
error: error instanceof Error ? error.message : String(error),
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
});
|
package/lib/ownMachines.mjs
CHANGED
|
@@ -45,6 +45,19 @@ const DIRECT_COMMAND = "machine.direct";
|
|
|
45
45
|
// The person's model and effort for an agent live on the computer that runs it (models.mjs reads its own store), so
|
|
46
46
|
// the buddy's Swarm tab reads and sets them on each computer: a worker asks home, home asks the worker that has it.
|
|
47
47
|
const MODELS_COMMAND = "machine.models";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* How a computer is written after # in a message ("Work Mac" is #Work-Mac): its name with spaces as dashes and anything
|
|
51
|
+
* but letters, digits, dots, dashes and underscores left out. Every menu writes it this way and computerNamed reads it.
|
|
52
|
+
*/
|
|
53
|
+
export const computerHandle = (label) =>
|
|
54
|
+
String(label ?? "")
|
|
55
|
+
.trim()
|
|
56
|
+
.replace(/\s+/g, "-")
|
|
57
|
+
.replace(/[^\w.-]/g, "");
|
|
58
|
+
// Every computer's running agent sessions, and one session's latest messages read on the computer it runs on, for the
|
|
59
|
+
// buddy's Agents tab, the phone and PiP: a worker asks home, home asks the worker that has it.
|
|
60
|
+
const LIVE_COMMAND = "machine.live";
|
|
48
61
|
// A worker passing on the person's choice of another computer as Home (makeHome).
|
|
49
62
|
const HOME_COMMAND = "machine.home";
|
|
50
63
|
// Since when this worker has found home unreachable: { home, since, checked } (watchHome).
|
|
@@ -147,6 +160,12 @@ export function createOwnMachines({
|
|
|
147
160
|
ok: false,
|
|
148
161
|
error: "this computer cannot list its agents' models",
|
|
149
162
|
}),
|
|
163
|
+
// This computer's own running sessions ({ list: true }) or one session's latest messages ({ read }), as the person
|
|
164
|
+
// (sessionControl.mjs board.live and board.live.read).
|
|
165
|
+
ownLive = async () => ({
|
|
166
|
+
ok: false,
|
|
167
|
+
error: "this computer cannot list its sessions",
|
|
168
|
+
}),
|
|
150
169
|
// A proxy link's model call from one of the person's cloud computers, on the computer holding the link
|
|
151
170
|
// (proxyLinks.mjs answer); null where none is held.
|
|
152
171
|
answerLink = null,
|
|
@@ -1455,6 +1474,15 @@ export function createOwnMachines({
|
|
|
1455
1474
|
: { ok: false, error: "this computer holds no proxy link" };
|
|
1456
1475
|
if (command === MODELS_COMMAND || command === MODEL_COMMAND)
|
|
1457
1476
|
return answerModelsCall(call, from, !pinned.hosted);
|
|
1477
|
+
// What runs in the person's sessions is theirs: a cloud computer, which runs agents only, never reads them.
|
|
1478
|
+
if (command === LIVE_COMMAND)
|
|
1479
|
+
return pinned.hosted
|
|
1480
|
+
? {
|
|
1481
|
+
ok: false,
|
|
1482
|
+
error:
|
|
1483
|
+
"That computer runs agents only; it does not read your sessions.",
|
|
1484
|
+
}
|
|
1485
|
+
: answerLiveCall(call, from);
|
|
1458
1486
|
if (command === SSH_COMMAND)
|
|
1459
1487
|
return answerSshCall(call, from, !pinned.hosted);
|
|
1460
1488
|
// Only the person, from a computer they drive, moves Home (a hosted computer is Harness's to run).
|
|
@@ -1785,14 +1813,16 @@ export function createOwnMachines({
|
|
|
1785
1813
|
? await answerLink(pinned.machine_id, call.link)
|
|
1786
1814
|
: call.models
|
|
1787
1815
|
? await ownModels(call.models)
|
|
1788
|
-
: call.
|
|
1789
|
-
?
|
|
1790
|
-
:
|
|
1791
|
-
?
|
|
1792
|
-
:
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1816
|
+
: call.live
|
|
1817
|
+
? await ownLive(call.live)
|
|
1818
|
+
: call.ssh
|
|
1819
|
+
? ownSshAllow(call.ssh)
|
|
1820
|
+
: WINDOW_TOOLS.has(call.tool)
|
|
1821
|
+
? await askOwnWindows(call)
|
|
1822
|
+
: {
|
|
1823
|
+
ok: false,
|
|
1824
|
+
error: `home cannot ask this computer's windows for ${call.tool}`,
|
|
1825
|
+
};
|
|
1796
1826
|
return {
|
|
1797
1827
|
envelope: await sealAskAnswer(
|
|
1798
1828
|
key,
|
|
@@ -2010,6 +2040,129 @@ export function createOwnMachines({
|
|
|
2010
2040
|
};
|
|
2011
2041
|
};
|
|
2012
2042
|
|
|
2043
|
+
// ---- every computer's running sessions, for the Agents tab, the phone and PiP ----
|
|
2044
|
+
|
|
2045
|
+
/** One computer's sessions as it answered, or why there are none: offline, or it did not answer. */
|
|
2046
|
+
const liveEntry = (computer, answer) => ({
|
|
2047
|
+
id: computer.id,
|
|
2048
|
+
label: computer.label,
|
|
2049
|
+
role: computer.role ?? "home",
|
|
2050
|
+
online: computer.online,
|
|
2051
|
+
sessions: answer?.ok ? (answer.sessions ?? []) : [],
|
|
2052
|
+
...(answer?.ok
|
|
2053
|
+
? {}
|
|
2054
|
+
: {
|
|
2055
|
+
error: computer.online
|
|
2056
|
+
? (answer?.error ?? `${computer.label} did not answer`)
|
|
2057
|
+
: `${computer.label} is offline`,
|
|
2058
|
+
}),
|
|
2059
|
+
});
|
|
2060
|
+
|
|
2061
|
+
/** On home: home and each confirmed worker but `except`, each listing its own sessions. */
|
|
2062
|
+
const everyComputersLive = async (except = null) => {
|
|
2063
|
+
const workers = siblings.filter(
|
|
2064
|
+
(sibling) => sibling.id !== except && confirmed(sibling)
|
|
2065
|
+
);
|
|
2066
|
+
const [own, ...answers] = await Promise.all([
|
|
2067
|
+
except === machine.id ? null : ownLive({ list: true }),
|
|
2068
|
+
...workers.map((worker) =>
|
|
2069
|
+
worker.connected ? askWorker(worker.id, { live: { list: true } }) : null
|
|
2070
|
+
),
|
|
2071
|
+
]);
|
|
2072
|
+
return [
|
|
2073
|
+
liveEntry(
|
|
2074
|
+
{
|
|
2075
|
+
id: machine.id,
|
|
2076
|
+
label: machine.label,
|
|
2077
|
+
role: machine.role ?? "home",
|
|
2078
|
+
online: true,
|
|
2079
|
+
},
|
|
2080
|
+
own
|
|
2081
|
+
),
|
|
2082
|
+
...workers.map((worker, index) =>
|
|
2083
|
+
liveEntry({ ...worker, online: worker.connected }, answers[index])
|
|
2084
|
+
),
|
|
2085
|
+
];
|
|
2086
|
+
};
|
|
2087
|
+
|
|
2088
|
+
/** The person's computers, this one first, each with the agent sessions running on it. */
|
|
2089
|
+
const sessions = async () => {
|
|
2090
|
+
if (isHome())
|
|
2091
|
+
return {
|
|
2092
|
+
ok: true,
|
|
2093
|
+
here: machine.id,
|
|
2094
|
+
computers: await everyComputersLive(),
|
|
2095
|
+
};
|
|
2096
|
+
const own = liveEntry(
|
|
2097
|
+
{ id: machine.id, label: machine.label, role: "worker", online: true },
|
|
2098
|
+
await ownLive({ list: true })
|
|
2099
|
+
);
|
|
2100
|
+
const pinned = pinnedHome();
|
|
2101
|
+
const fromHome = pinned
|
|
2102
|
+
? await callOnce(pinned, {
|
|
2103
|
+
command: LIVE_COMMAND,
|
|
2104
|
+
agent: "user",
|
|
2105
|
+
list: true,
|
|
2106
|
+
})
|
|
2107
|
+
: null;
|
|
2108
|
+
const others = fromHome?.ok
|
|
2109
|
+
? fromHome.computers.filter((computer) => computer.id !== machine.id)
|
|
2110
|
+
: siblings.map((sibling) =>
|
|
2111
|
+
liveEntry({ ...sibling, online: false }, null)
|
|
2112
|
+
);
|
|
2113
|
+
return {
|
|
2114
|
+
ok: true,
|
|
2115
|
+
here: machine.id,
|
|
2116
|
+
computers: [own, ...others],
|
|
2117
|
+
...(fromHome?.ok ? {} : { note: unreachableNote(fromHome) }),
|
|
2118
|
+
};
|
|
2119
|
+
};
|
|
2120
|
+
|
|
2121
|
+
/**
|
|
2122
|
+
* One session's latest messages and steps, read on the computer it runs on (`machine`, this one by default): through
|
|
2123
|
+
* home, which asks the worker that has it.
|
|
2124
|
+
*/
|
|
2125
|
+
const readSession = async (request) => {
|
|
2126
|
+
const target = String(request.machine || machine.id);
|
|
2127
|
+
const read = {
|
|
2128
|
+
to: request.to,
|
|
2129
|
+
sessionId: request.sessionId,
|
|
2130
|
+
folder: request.folder,
|
|
2131
|
+
limit: request.limit,
|
|
2132
|
+
};
|
|
2133
|
+
if (target === machine.id) return ownLive({ read });
|
|
2134
|
+
if (isHome()) {
|
|
2135
|
+
if (
|
|
2136
|
+
!siblings.some((sibling) => sibling.id === target && confirmed(sibling))
|
|
2137
|
+
)
|
|
2138
|
+
return { ok: false, error: "that computer is not in your swarm" };
|
|
2139
|
+
return (
|
|
2140
|
+
(await askWorker(target, { live: { read } })) ?? {
|
|
2141
|
+
ok: false,
|
|
2142
|
+
error: `${labelOf(target) ?? "That computer"} did not answer`,
|
|
2143
|
+
}
|
|
2144
|
+
);
|
|
2145
|
+
}
|
|
2146
|
+
const pinned = pinnedHome();
|
|
2147
|
+
if (!pinned) return { ok: false, error: unreachableNote(null) };
|
|
2148
|
+
return (
|
|
2149
|
+
(await callOnce(pinned, {
|
|
2150
|
+
command: LIVE_COMMAND,
|
|
2151
|
+
agent: "user",
|
|
2152
|
+
read,
|
|
2153
|
+
machine: target,
|
|
2154
|
+
})) ?? offline()
|
|
2155
|
+
);
|
|
2156
|
+
};
|
|
2157
|
+
|
|
2158
|
+
/** On home: a worker's call for every computer's sessions, or for one session read where it runs. */
|
|
2159
|
+
const answerLiveCall = async (call, from) => {
|
|
2160
|
+
if (call.agent !== "user")
|
|
2161
|
+
return { ok: false, error: "only the person reads their sessions" };
|
|
2162
|
+
if (call.read) return readSession({ ...call.read, machine: call.machine });
|
|
2163
|
+
return { ok: true, computers: await everyComputersLive(from) };
|
|
2164
|
+
};
|
|
2165
|
+
|
|
2013
2166
|
/** The person's model and effort for one agent on one computer, set on that computer. */
|
|
2014
2167
|
const setModel = async (request) => {
|
|
2015
2168
|
const pick = {
|
|
@@ -2266,7 +2419,7 @@ export function createOwnMachines({
|
|
|
2266
2419
|
)) {
|
|
2267
2420
|
const name = match[1].replace(/[.,;:!?]+$/, "").toLowerCase();
|
|
2268
2421
|
const found = computers.find(
|
|
2269
|
-
(computer) => computer.label.toLowerCase() === name
|
|
2422
|
+
(computer) => computerHandle(computer.label).toLowerCase() === name
|
|
2270
2423
|
);
|
|
2271
2424
|
if (found) return { id: found.id, label: found.label };
|
|
2272
2425
|
}
|
|
@@ -2341,6 +2494,8 @@ export function createOwnMachines({
|
|
|
2341
2494
|
directStatus: () => directLink?.status() ?? [],
|
|
2342
2495
|
list,
|
|
2343
2496
|
models,
|
|
2497
|
+
sessions,
|
|
2498
|
+
readSession,
|
|
2344
2499
|
setModel,
|
|
2345
2500
|
allowSsh,
|
|
2346
2501
|
status,
|
package/lib/phones.mjs
CHANGED
|
@@ -133,6 +133,10 @@ const BROWSER_READS = boardCommandsFor("browserReads");
|
|
|
133
133
|
// The person's linked window adds a computer to the swarm: how a cloud worker joins, with home pinning its key exactly
|
|
134
134
|
// as for a code the person typed here. Only as the person, never in a Harness chat call.
|
|
135
135
|
const MACHINE_COMMANDS = boardCommandsFor("browserMachines");
|
|
136
|
+
// The phone, as the person: their computers and every one's sessions, the model each agent runs on, read and set on
|
|
137
|
+
// the computer that runs it, and which computer is Home or taken off the swarm (the Swarm tab, # in a message,
|
|
138
|
+
// sessions by computer). Adding a computer stays on the computers themselves, which pair by a code typed there.
|
|
139
|
+
const PHONE_MACHINE_COMMANDS = boardCommandsFor("phoneMachines");
|
|
136
140
|
// The person's window signs a cloud computer's agents in (agentSignIn.mjs) and picks the model each runs on
|
|
137
141
|
// (models.mjs): only there, and only as the person.
|
|
138
142
|
const SIGN_IN_COMMANDS = boardCommandsFor("browserSignIn");
|
|
@@ -719,6 +723,9 @@ export async function createPhoneDesk({
|
|
|
719
723
|
if (!own || callInput.as === "harness")
|
|
720
724
|
return { ok: false, error: `a browser cannot call ${command} here` };
|
|
721
725
|
if (command === "machines.list") return own.list();
|
|
726
|
+
// Every computer's running sessions, and one read where it runs: the person's, so the same on every device.
|
|
727
|
+
if (command === "machines.live") return own.sessions();
|
|
728
|
+
if (command === "machines.live.read") return own.readSession(callInput);
|
|
722
729
|
// Only a join code, which confirms a computer already connected to the account and waiting to join; setup's
|
|
723
730
|
// 16-character code would also release a new computer's connection, which stays the buddy's and the CLI's.
|
|
724
731
|
const code = normalizeJoinCode(callInput.code);
|
|
@@ -811,6 +818,29 @@ export async function createPhoneDesk({
|
|
|
811
818
|
})
|
|
812
819
|
);
|
|
813
820
|
}
|
|
821
|
+
if (PHONE_MACHINE_COMMANDS.has(command)) {
|
|
822
|
+
const own = machines();
|
|
823
|
+
if (!own) return { ok: false, error: "this computer has no swarm list" };
|
|
824
|
+
if (command === "machines.list") return own.list();
|
|
825
|
+
if (command === "machines.live") return own.sessions();
|
|
826
|
+
if (command === "machines.live.read") return own.readSession(callInput);
|
|
827
|
+
if (command === "machines.models")
|
|
828
|
+
return own.models({ refresh: callInput.refresh === true });
|
|
829
|
+
if (command === "machines.home" || command === "machines.remove") {
|
|
830
|
+
// The phone names the computer it means; it never moves Home or removes one by leaving it out.
|
|
831
|
+
const computer = String(callInput.computer || "");
|
|
832
|
+
if (!computer) return { ok: false, error: "name the computer" };
|
|
833
|
+
return command === "machines.home"
|
|
834
|
+
? own.makeHome(computer)
|
|
835
|
+
: own.remove(computer);
|
|
836
|
+
}
|
|
837
|
+
return own.setModel({
|
|
838
|
+
computer: callInput.computer,
|
|
839
|
+
target: callInput.target,
|
|
840
|
+
model: callInput.model,
|
|
841
|
+
effort: callInput.effort ?? null,
|
|
842
|
+
});
|
|
843
|
+
}
|
|
814
844
|
// Lid-Closed Mode's wake: no arguments, never queued, and it can only keep the Mac awake and say how it is.
|
|
815
845
|
if (command === "machine.wake") return { ok: true, machine: machine() };
|
|
816
846
|
if (!PHONE_COMMANDS.has(command))
|