tryharness 0.7.32 → 0.7.34
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 +48 -0
- package/lib/actorRoster.mjs +106 -0
- package/lib/boardAccess.mjs +328 -0
- package/lib/boardCommands.mjs +28 -0
- package/lib/boardFileCommands.mjs +228 -0
- package/lib/boardFiles.mjs +584 -0
- package/lib/boardProtocol.json +470 -0
- package/lib/boardProtocol.mjs +27 -0
- package/lib/boardThreads.mjs +153 -0
- package/lib/boardTools.mjs +202 -0
- package/lib/boardWatches.mjs +92 -0
- package/lib/bridge.mjs +272 -0
- package/lib/cards.mjs +5 -83
- package/lib/coordinator.mjs +13 -3
- package/lib/daemon.mjs +809 -5459
- package/lib/goalCommands.mjs +459 -0
- package/lib/harnessModel.mjs +166 -0
- package/lib/mcp-stdio.mjs +8 -2
- package/lib/models.mjs +107 -11
- package/lib/phones.mjs +8 -121
- package/lib/roomPage.mjs +44 -0
- package/lib/runAuthority.mjs +109 -0
- package/lib/runWorker.mjs +503 -0
- package/lib/sessionControl.mjs +788 -0
- package/lib/sessionIndex.mjs +245 -0
- package/lib/store.mjs +28 -9
- package/lib/swarm.mjs +28 -9
- package/lib/taskCommands.mjs +737 -0
- package/lib/taskRuns.mjs +196 -0
- package/lib/threadRuns.mjs +487 -0
- package/package.json +1 -1
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { documentText, ensureDir, isDocument, mimeFor } from "./files.mjs";
|
|
4
|
+
import { paths } from "./paths.mjs";
|
|
5
|
+
|
|
6
|
+
export function createBoardFileCommands({
|
|
7
|
+
FILE_LIMITS,
|
|
8
|
+
FILE_PART_BYTES,
|
|
9
|
+
MAX_FILE_BYTES,
|
|
10
|
+
MB,
|
|
11
|
+
blobPath,
|
|
12
|
+
fileLimit,
|
|
13
|
+
fileLine,
|
|
14
|
+
fileName,
|
|
15
|
+
fileView,
|
|
16
|
+
keepBlob,
|
|
17
|
+
namedPath,
|
|
18
|
+
notifyBoard,
|
|
19
|
+
readFilesSoon,
|
|
20
|
+
roomOf,
|
|
21
|
+
store,
|
|
22
|
+
textLike,
|
|
23
|
+
tooBig,
|
|
24
|
+
visibleFile,
|
|
25
|
+
}) {
|
|
26
|
+
return (request, { agent, id }) => {
|
|
27
|
+
if (request.command === "board.file.add") {
|
|
28
|
+
const task = store.getTask(id);
|
|
29
|
+
if (!task) return { ok: false, error: "task not found" };
|
|
30
|
+
let bytes;
|
|
31
|
+
if (request.path != null) {
|
|
32
|
+
// Only an agent on this computer names a path on it.
|
|
33
|
+
if (request.hostedCaller)
|
|
34
|
+
return {
|
|
35
|
+
ok: false,
|
|
36
|
+
error:
|
|
37
|
+
"an agent reaching this computer through Harness attaches a file's content (data), not a path",
|
|
38
|
+
};
|
|
39
|
+
const file = String(request.path);
|
|
40
|
+
const stat = path.isAbsolute(file)
|
|
41
|
+
? fs.statSync(file, { throwIfNoEntry: false })
|
|
42
|
+
: null;
|
|
43
|
+
if (!stat?.isFile())
|
|
44
|
+
return {
|
|
45
|
+
ok: false,
|
|
46
|
+
error: `${file} is not an absolute path to a file`,
|
|
47
|
+
};
|
|
48
|
+
if (stat.size > MAX_FILE_BYTES)
|
|
49
|
+
return tooBig(
|
|
50
|
+
stat.size,
|
|
51
|
+
FILE_LIMITS.find((limit) => limit.bytes === MAX_FILE_BYTES)
|
|
52
|
+
);
|
|
53
|
+
bytes = fs.readFileSync(file);
|
|
54
|
+
} else if (typeof request.data === "string" && request.data)
|
|
55
|
+
bytes = Buffer.from(request.data, "base64");
|
|
56
|
+
else
|
|
57
|
+
return {
|
|
58
|
+
ok: false,
|
|
59
|
+
error: "give the file's path or its content as base64 data",
|
|
60
|
+
};
|
|
61
|
+
// A file sent in parts, in order (a sealed message carries about 1 MB): kept aside until its last part arrives.
|
|
62
|
+
if (request.upload !== undefined) {
|
|
63
|
+
const upload = String(request.upload),
|
|
64
|
+
offset = Number(request.offset),
|
|
65
|
+
total = Number(request.total);
|
|
66
|
+
const partial = path.join(paths.files, "uploads", `${agent}-${upload}`);
|
|
67
|
+
if (
|
|
68
|
+
!/^[\w-]{8,64}$/.test(upload) ||
|
|
69
|
+
!Number.isSafeInteger(total) ||
|
|
70
|
+
total > MAX_FILE_BYTES ||
|
|
71
|
+
offset !==
|
|
72
|
+
(offset ? fs.statSync(partial, { throwIfNoEntry: false })?.size : 0)
|
|
73
|
+
)
|
|
74
|
+
return {
|
|
75
|
+
ok: false,
|
|
76
|
+
error:
|
|
77
|
+
"a part must follow the one before it, within the file's size",
|
|
78
|
+
};
|
|
79
|
+
ensureDir(path.dirname(partial));
|
|
80
|
+
(offset ? fs.appendFileSync : fs.writeFileSync)(partial, bytes, {
|
|
81
|
+
mode: 0o600,
|
|
82
|
+
});
|
|
83
|
+
if (offset + bytes.length < total)
|
|
84
|
+
return { ok: true, received: offset + bytes.length };
|
|
85
|
+
bytes = fs.readFileSync(partial);
|
|
86
|
+
fs.rmSync(partial, { force: true });
|
|
87
|
+
if (bytes.length !== total)
|
|
88
|
+
return {
|
|
89
|
+
ok: false,
|
|
90
|
+
error: `the parts came to ${bytes.length} of the file's ${total} bytes`,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
if (!bytes.length) return { ok: false, error: "the file is empty" };
|
|
94
|
+
const name = fileName(
|
|
95
|
+
request.name ||
|
|
96
|
+
(request.path != null ? path.basename(String(request.path)) : "")
|
|
97
|
+
);
|
|
98
|
+
const mime =
|
|
99
|
+
typeof request.mime === "string" &&
|
|
100
|
+
/^[a-z0-9.+-]+\/[a-z0-9.+-]+$/i.test(request.mime)
|
|
101
|
+
? request.mime.toLowerCase()
|
|
102
|
+
: mimeFor(name);
|
|
103
|
+
const limit = fileLimit(mime, bytes);
|
|
104
|
+
if (bytes.length > limit.bytes) return tooBig(bytes.length, limit);
|
|
105
|
+
// Shared with the agents on Harness's side unless whoever attached it keeps it on the computer (owner, October 14:
|
|
106
|
+
// "shared should be default ... the user should select if something shouldnt be shared"); bytes that came through
|
|
107
|
+
// Harness were readable there already. To another person's computer a file still goes only sealed.
|
|
108
|
+
const file = store.addFile({
|
|
109
|
+
taskId: id,
|
|
110
|
+
name,
|
|
111
|
+
mime,
|
|
112
|
+
bytes: bytes.length,
|
|
113
|
+
sha256: keepBlob(bytes),
|
|
114
|
+
author: agent,
|
|
115
|
+
cloud: request.hostedCaller === true || request.cloud !== false,
|
|
116
|
+
});
|
|
117
|
+
// Said in the thread the task is in, with its id, so every agent there can open it.
|
|
118
|
+
const place = roomOf(task)?.id ?? id;
|
|
119
|
+
store.addContribution({
|
|
120
|
+
taskId: place,
|
|
121
|
+
kind: "file",
|
|
122
|
+
author: agent,
|
|
123
|
+
text: `${agent} attached ${fileLine(file)}`,
|
|
124
|
+
source: `file:${file.id}`,
|
|
125
|
+
});
|
|
126
|
+
notifyBoard(store.getTask(place));
|
|
127
|
+
readFilesSoon();
|
|
128
|
+
return { ok: true, file: fileView(file, request.hostedCaller) };
|
|
129
|
+
} else if (request.command === "board.file.share") {
|
|
130
|
+
// Whether the agents on Harness's side may read a file: the person's call, either way, on any file. A call through
|
|
131
|
+
// Harness in the clear (an unpaired browser) may only keep a file here: Harness must not be able to open one up.
|
|
132
|
+
if (agent !== "user")
|
|
133
|
+
return {
|
|
134
|
+
ok: false,
|
|
135
|
+
error: "only the person shares a file or keeps it on this computer",
|
|
136
|
+
};
|
|
137
|
+
if (request.hostedCaller && request.cloud === true)
|
|
138
|
+
return {
|
|
139
|
+
ok: false,
|
|
140
|
+
code: "sealed_only",
|
|
141
|
+
error:
|
|
142
|
+
"share a file from the buddy or a browser linked to this computer: npx tryharness link",
|
|
143
|
+
};
|
|
144
|
+
const file = store.file(String(request.fileId || ""));
|
|
145
|
+
if (!file) return { ok: false, error: "file not found" };
|
|
146
|
+
store.setFileCloud(file.id, request.cloud === true);
|
|
147
|
+
store.meta(
|
|
148
|
+
`file-share:${file.id}`,
|
|
149
|
+
JSON.stringify({ by: "user", at: new Date().toISOString() })
|
|
150
|
+
);
|
|
151
|
+
notifyBoard(
|
|
152
|
+
store.getTask(roomOf(store.getTask(file.task_id))?.id ?? file.task_id)
|
|
153
|
+
);
|
|
154
|
+
return { ok: true, file: fileView(store.file(file.id), false) };
|
|
155
|
+
} else if (request.command === "board.file.get") {
|
|
156
|
+
const file = store.file(String(request.fileId || ""));
|
|
157
|
+
if (!file || !visibleFile(file, agent, request.hostedCaller))
|
|
158
|
+
return { ok: false, error: "file not found" };
|
|
159
|
+
if (request.hostedCaller && !file.cloud)
|
|
160
|
+
return {
|
|
161
|
+
ok: false,
|
|
162
|
+
code: "sealed",
|
|
163
|
+
error:
|
|
164
|
+
"this file stays on the person's computers: the agent that attached it did not share it with cloud agents",
|
|
165
|
+
};
|
|
166
|
+
if (!file.present)
|
|
167
|
+
return {
|
|
168
|
+
ok: false,
|
|
169
|
+
error: "this file is still on its way from the other computer",
|
|
170
|
+
};
|
|
171
|
+
// A part of the file for the person's own phone or linked browser: a sealed answer carries about 1 MB
|
|
172
|
+
// (src/shared/bridge/sealedWire.ts), so a larger file is read in parts. A caller through Harness gets the whole
|
|
173
|
+
// file, which the checks in plainBoard read as a whole.
|
|
174
|
+
if (request.offset !== undefined && !request.hostedCaller) {
|
|
175
|
+
const offset = Number(request.offset),
|
|
176
|
+
length = Number(request.length ?? FILE_PART_BYTES);
|
|
177
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || offset >= file.bytes)
|
|
178
|
+
return {
|
|
179
|
+
ok: false,
|
|
180
|
+
error: "offset must be a byte within the file",
|
|
181
|
+
};
|
|
182
|
+
if (
|
|
183
|
+
!Number.isSafeInteger(length) ||
|
|
184
|
+
length < 1 ||
|
|
185
|
+
length > FILE_PART_BYTES
|
|
186
|
+
)
|
|
187
|
+
return {
|
|
188
|
+
ok: false,
|
|
189
|
+
error: `a part is 1 to ${FILE_PART_BYTES} bytes`,
|
|
190
|
+
};
|
|
191
|
+
const part = Buffer.alloc(Math.min(length, file.bytes - offset));
|
|
192
|
+
const fd = fs.openSync(blobPath(file.sha256), "r");
|
|
193
|
+
try {
|
|
194
|
+
fs.readSync(fd, part, 0, part.length, offset);
|
|
195
|
+
} finally {
|
|
196
|
+
fs.closeSync(fd);
|
|
197
|
+
}
|
|
198
|
+
return {
|
|
199
|
+
ok: true,
|
|
200
|
+
file: fileView(file, false),
|
|
201
|
+
offset,
|
|
202
|
+
data: part.toString("base64"),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
// An agent here that can take only an image or text inline gets anything else (a video) by its path alone.
|
|
206
|
+
const inline =
|
|
207
|
+
request.inlineOnly !== true ||
|
|
208
|
+
(/^image\/(png|jpeg|gif|webp)$/.test(file.mime) &&
|
|
209
|
+
file.bytes <= 3.75 * MB) ||
|
|
210
|
+
(textLike(file.mime) && file.bytes <= 200_000);
|
|
211
|
+
// A document's words too, for an agent here whose tools do not read it.
|
|
212
|
+
const words =
|
|
213
|
+
request.inlineOnly === true && isDocument(file.mime)
|
|
214
|
+
? documentText(namedPath(file), file.mime)
|
|
215
|
+
: null;
|
|
216
|
+
return {
|
|
217
|
+
ok: true,
|
|
218
|
+
file: fileView(file, request.hostedCaller),
|
|
219
|
+
...(inline
|
|
220
|
+
? {
|
|
221
|
+
data: fs.readFileSync(blobPath(file.sha256)).toString("base64"),
|
|
222
|
+
}
|
|
223
|
+
: {}),
|
|
224
|
+
...(words !== null ? { text: words } : {}),
|
|
225
|
+
};
|
|
226
|
+
} else return { ok: false, error: "unknown command" };
|
|
227
|
+
};
|
|
228
|
+
}
|