visualproof 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/assets/tips/phone-fingers.light.mp4 +0 -0
- package/dist/assets/tips/phone-fingers.light.webp +0 -0
- package/dist/assets/tips/tour-agent.webp +0 -0
- package/dist/assets/tips/tour-arrange.webp +0 -0
- package/dist/assets/tips/tour-artwork.webp +0 -0
- package/dist/assets/tips/tour-checks.webp +0 -0
- package/dist/assets/tips/tour-keys.webp +0 -0
- package/dist/assets/tips/tour-library.webp +0 -0
- package/dist/assets/tips/tour-project.webp +0 -0
- package/dist/assets/tips/tour-review.webp +0 -0
- package/dist/assets/tips/tour-share.webp +0 -0
- package/dist/assets/tips/tour-source.webp +0 -0
- package/dist/assets/tips/tour-target.webp +0 -0
- package/dist/assets/tips/tour-view.webp +0 -0
- package/dist/assets/tips/tour.json +125 -115
- package/dist/assets/tips/tour.webp +0 -0
- package/dist/extractor/Extract.lean +13 -3
- package/dist/mcp/visualproof-mcp.mjs +20 -9
- package/dist/server/access.js +28 -0
- package/dist/server/agent.js +130 -2819
- package/dist/server/agentactivity.js +72 -0
- package/dist/server/agentadvice.js +115 -0
- package/dist/server/agentdocs.js +1 -1
- package/dist/server/agententry.js +180 -0
- package/dist/server/agentexport.js +469 -0
- package/dist/server/agentfollow.js +384 -0
- package/dist/server/agentgraph.js +128 -0
- package/dist/server/agentlean.js +144 -0
- package/dist/server/agentlibrary.js +1 -1
- package/dist/server/agentplaces.js +242 -0
- package/dist/server/agentread.js +569 -0
- package/dist/server/agenttalk.js +282 -0
- package/dist/server/agenttcb.js +378 -0
- package/dist/server/agentviews.js +2 -2
- package/dist/server/app.js +377 -0
- package/dist/server/appaccess.js +127 -0
- package/dist/server/appagent.js +110 -0
- package/dist/server/appgraph.js +176 -0
- package/dist/server/appshare.js +179 -0
- package/dist/server/appsite.js +120 -0
- package/dist/server/apptalk.js +178 -0
- package/dist/server/artcmd.js +1 -1
- package/dist/server/askfollow.js +1 -1
- package/dist/server/deploy.js +204 -53
- package/dist/server/events.js +1 -1
- package/dist/server/history.js +51 -3
- package/dist/server/historyapi.js +34 -2
- package/dist/server/hover.js +11 -7
- package/dist/server/identity.js +1 -1
- package/dist/server/invites.js +113 -20
- package/dist/server/libraryroute.js +3 -3
- package/dist/server/live.js +186 -30
- package/dist/server/main.js +31 -1613
- package/dist/server/mcptools.js +33 -20
- package/dist/server/options.js +350 -0
- package/dist/server/outside.js +311 -0
- package/dist/server/pastdoc.js +22 -3
- package/dist/server/presence.js +3 -3
- package/dist/server/project.js +28 -4
- package/dist/server/prompt.js +8 -3
- package/dist/server/readonlygate.js +4 -4
- package/dist/server/reviewapi.js +51 -17
- package/dist/server/send.js +259 -0
- package/dist/server/talk.js +238 -116
- package/dist/server/tokens.js +13 -5
- package/dist/server/view.js +3 -1
- package/dist/server/views.js +1 -1
- package/dist/shared/artrecipe.js +3 -3
- package/dist/shared/build.js +35 -1
- package/dist/shared/collab.js +9 -1
- package/dist/shared/comment.js +1 -1
- package/dist/shared/doc/build.js +30 -14
- package/dist/shared/doc/latex.js +7 -2
- package/dist/shared/doc/model.js +4 -2
- package/dist/shared/doc/sections.js +21 -4
- package/dist/shared/keys.js +66 -37
- package/dist/shared/lostfound.js +1 -0
- package/dist/shared/mentions.js +83 -0
- package/dist/shared/outside.js +16 -0
- package/dist/shared/pastgraph.js +11 -2
- package/dist/shared/query.js +13 -4
- package/dist/shared/review.js +91 -21
- package/dist/shared/reviewqueue.js +6 -1
- package/dist/shared/scope.js +1 -1
- package/dist/web/bundle/app.js +51289 -49853
- package/dist/web/index.html +45 -9
- package/dist/web/src/reviewqueue.js +52 -19
- package/dist/web/src/selection.js +1 -1
- package/dist/web/src/theme.js +1 -1
- package/dist/web/src/trust.js +271 -0
- package/dist/web/src/visitors.js +1 -1
- package/dist/web/style.css +181 -70
- package/docs/agent-api/README.md +13 -11
- package/docs/agent-api/advice.md +8 -2
- package/docs/agent-api/export.md +9 -6
- package/docs/agent-api/following.md +11 -12
- package/docs/agent-api/history.md +6 -3
- package/docs/agent-api/mcp.md +15 -15
- package/docs/agent-api/notes.md +1 -1
- package/docs/agent-api/reading.md +10 -4
- package/docs/agent-api/requests.md +12 -15
- package/docs/agent-api/talking.md +60 -55
- package/docs/agent-api/tcb.md +21 -21
- package/docs/agent-api/views.md +3 -4
- package/docs/agent-api/words.md +8 -8
- package/package.json +2 -1
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// How the server sends what it answers (server/app.ts): JSON, the page's files, the graph's parts,
|
|
2
|
+
// the whole of a revision, texts and downloads; and a body refused as too large while it comes.
|
|
3
|
+
//
|
|
4
|
+
// Pages are often far from the server (CollabSpec, "Far pages"): every byte counts, and what a
|
|
5
|
+
// browser has it should not fetch again. The page's files and the graph are sent compressed, with
|
|
6
|
+
// a tag naming their content; a browser that has that content already is told so (304) and sends
|
|
7
|
+
// nothing more. Everything else in JSON is compressed when it is large.
|
|
8
|
+
import { createHash } from "node:crypto";
|
|
9
|
+
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
10
|
+
import { extname } from "node:path";
|
|
11
|
+
import { gunzip, gunzipSync, gzip, gzipSync } from "node:zlib";
|
|
12
|
+
import { packEdges } from "../shared/edgecodec.js";
|
|
13
|
+
const TYPES = {
|
|
14
|
+
".woff2": "font/woff2",
|
|
15
|
+
".html": "text/html; charset=utf-8",
|
|
16
|
+
".css": "text/css; charset=utf-8",
|
|
17
|
+
".js": "text/javascript; charset=utf-8",
|
|
18
|
+
".map": "application/json",
|
|
19
|
+
};
|
|
20
|
+
/** The tag naming a content. */
|
|
21
|
+
function tagOf(body) {
|
|
22
|
+
return `"${createHash("sha256").update(body).digest("base64url").slice(0, 27)}"`;
|
|
23
|
+
}
|
|
24
|
+
/** Whether the request already has the content `etag` names. */
|
|
25
|
+
function fresh(req, etag) {
|
|
26
|
+
const have = req.headers["if-none-match"];
|
|
27
|
+
return !!have && have.split(",").some((t) => t.trim().replace(/^W\//, "") === etag);
|
|
28
|
+
}
|
|
29
|
+
function acceptsGzip(req) {
|
|
30
|
+
return /\bgzip\b/.test(String(req?.headers["accept-encoding"] ?? ""));
|
|
31
|
+
}
|
|
32
|
+
/** the page's files as last read: compressed and tagged once per change on disk */
|
|
33
|
+
const files = new Map();
|
|
34
|
+
export function sendFile(req, res, file) {
|
|
35
|
+
if (refused.has(res))
|
|
36
|
+
return;
|
|
37
|
+
if (!existsSync(file))
|
|
38
|
+
return sendJson(res, { error: "not found" }, 404);
|
|
39
|
+
const st = statSync(file);
|
|
40
|
+
let f = files.get(file);
|
|
41
|
+
if (!f || f.mtime !== st.mtimeMs || f.size !== st.size) {
|
|
42
|
+
const body = readFileSync(file);
|
|
43
|
+
f = { mtime: st.mtimeMs, size: st.size, body, gz: null, etag: tagOf(body) };
|
|
44
|
+
files.set(file, f);
|
|
45
|
+
}
|
|
46
|
+
const head = { "content-type": TYPES[extname(file)] ?? "application/octet-stream", "cache-control": "no-cache", etag: f.etag, vary: "accept-encoding" };
|
|
47
|
+
if (fresh(req, f.etag)) {
|
|
48
|
+
res.writeHead(304, head);
|
|
49
|
+
return void res.end();
|
|
50
|
+
}
|
|
51
|
+
if (!acceptsGzip(req)) {
|
|
52
|
+
res.writeHead(200, head);
|
|
53
|
+
return void res.end(f.body);
|
|
54
|
+
}
|
|
55
|
+
f.gz ??= gzipSync(f.body, { level: 9 });
|
|
56
|
+
res.writeHead(200, { ...head, "content-encoding": "gzip" });
|
|
57
|
+
res.end(f.gz);
|
|
58
|
+
}
|
|
59
|
+
/** how long the rest of a body refused as too large is read before it is answered all the same,
|
|
60
|
+
* and the connection cut: long enough for a client to finish sending what it began, not for an
|
|
61
|
+
* endless body */
|
|
62
|
+
const TOO_LARGE_GRACE_MS = 2000;
|
|
63
|
+
export class TooLarge extends Error {
|
|
64
|
+
}
|
|
65
|
+
/** A request's body, refused (TooLarge) once it passes `limit` bytes: what comes after is not kept. */
|
|
66
|
+
export function readBody(req, limit) {
|
|
67
|
+
return new Promise((ok, fail) => {
|
|
68
|
+
const parts = [];
|
|
69
|
+
let size = 0;
|
|
70
|
+
if (Number(req.headers["content-length"]) > limit)
|
|
71
|
+
return fail(new TooLarge());
|
|
72
|
+
req.on("data", (b) => {
|
|
73
|
+
if (size > limit)
|
|
74
|
+
return;
|
|
75
|
+
size += b.length;
|
|
76
|
+
if (size > limit) {
|
|
77
|
+
parts.length = 0;
|
|
78
|
+
fail(new TooLarge());
|
|
79
|
+
}
|
|
80
|
+
else
|
|
81
|
+
parts.push(b);
|
|
82
|
+
});
|
|
83
|
+
req.on("end", () => ok(Buffer.concat(parts).toString("utf8")));
|
|
84
|
+
req.on("error", fail);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/** Responses refused as too large and not answered yet (tooLarge): what the handler that asked for
|
|
88
|
+
* the body answers meanwhile is not sent. */
|
|
89
|
+
export const refused = new WeakSet();
|
|
90
|
+
/** Answer 413 to a body too large and close the connection. The rest of the body is read and thrown
|
|
91
|
+
* away first, and the answer goes once it has all come: a connection closed while the client is
|
|
92
|
+
* still sending is reset on some systems (macOS), and the client never reads the answer. A client
|
|
93
|
+
* still sending after a short grace is answered all the same, and cut off. */
|
|
94
|
+
export function tooLarge(req, res, body) {
|
|
95
|
+
refused.add(res);
|
|
96
|
+
let done = false;
|
|
97
|
+
const answer = () => {
|
|
98
|
+
if (done)
|
|
99
|
+
return;
|
|
100
|
+
done = true;
|
|
101
|
+
refused.delete(res);
|
|
102
|
+
sendJson(res, body, 413, { connection: "close" });
|
|
103
|
+
};
|
|
104
|
+
req.on("data", () => { });
|
|
105
|
+
req.on("end", answer);
|
|
106
|
+
req.resume();
|
|
107
|
+
if (req.readableEnded)
|
|
108
|
+
answer();
|
|
109
|
+
setTimeout(() => {
|
|
110
|
+
answer();
|
|
111
|
+
setTimeout(() => req.socket.destroy(), 500).unref();
|
|
112
|
+
}, TOO_LARGE_GRACE_MS).unref();
|
|
113
|
+
}
|
|
114
|
+
/** Send a JSON answer, once: a handler that answers after something else already did (a body too
|
|
115
|
+
* large, answered as it was read) is not heard. */
|
|
116
|
+
export function sendJson(res, body, code = 200, headers = {}) {
|
|
117
|
+
if (res.headersSent || res.writableEnded || refused.has(res))
|
|
118
|
+
return;
|
|
119
|
+
sendJsonText(res, JSON.stringify(body), code, headers);
|
|
120
|
+
}
|
|
121
|
+
/** The same, of an answer written as JSON already. */
|
|
122
|
+
function sendJsonText(res, text, code = 200, headers = {}) {
|
|
123
|
+
if (res.headersSent || res.writableEnded || refused.has(res))
|
|
124
|
+
return;
|
|
125
|
+
// a small answer is not worth compressing; a status with its build log, a source file, are; a
|
|
126
|
+
// large one is compressed off the server's thread, which meanwhile answers others
|
|
127
|
+
if (text.length > JSON_GZIP_FROM && acceptsGzip(res.req)) {
|
|
128
|
+
res.writeHead(code, { "content-type": "application/json", "cache-control": "no-store", "content-encoding": "gzip", vary: "accept-encoding", ...headers });
|
|
129
|
+
if (text.length < JSON_GZIP_AWAY_FROM)
|
|
130
|
+
return void res.end(gzipSync(text));
|
|
131
|
+
return void gzip(text, (e, gz) => (e ? res.destroy(e) : res.end(gz)));
|
|
132
|
+
}
|
|
133
|
+
res.writeHead(code, { "content-type": "application/json", "cache-control": "no-store", ...headers });
|
|
134
|
+
res.end(text);
|
|
135
|
+
}
|
|
136
|
+
/** what is sent whole under the tag of its revision, compressed, per what it is of (the review, the
|
|
137
|
+
* build status) */
|
|
138
|
+
const revisionGz = new WeakMap();
|
|
139
|
+
/** A whole sent under the tag of its revision: the review (GET /api/review), tagged with its run and
|
|
140
|
+
* its revision (server/review.ts), and the build status (GET /api/build, server/live.ts). A page that
|
|
141
|
+
* has it is answered 304, which a reload is, and one that fell behind is sent it compressed — once a
|
|
142
|
+
* revision, off the server's thread when it is large, the same bytes for every page (a review of
|
|
143
|
+
* 5,000 opinions on Sphere-Packing-Lean: 8.5 MB of JSON, 1.1 MB sent; the build status of
|
|
144
|
+
* NavierStokesAndEuler's 2,657 files: 362 KB, 42 KB sent). `of` is what it is of; `whole` writes it,
|
|
145
|
+
* asked only where it is sent. */
|
|
146
|
+
export function sendRevision(req, res, of, tag, whole) {
|
|
147
|
+
if (res.headersSent || res.writableEnded || refused.has(res))
|
|
148
|
+
return;
|
|
149
|
+
const head = { "content-type": "application/json", "cache-control": "private, no-cache", etag: tag, vary: "accept-encoding" };
|
|
150
|
+
if (fresh(req, tag)) {
|
|
151
|
+
res.writeHead(304, head);
|
|
152
|
+
return void res.end();
|
|
153
|
+
}
|
|
154
|
+
const text = whole();
|
|
155
|
+
if (text.length <= JSON_GZIP_FROM || !acceptsGzip(req)) {
|
|
156
|
+
res.writeHead(200, head);
|
|
157
|
+
return void res.end(text);
|
|
158
|
+
}
|
|
159
|
+
let c = revisionGz.get(of);
|
|
160
|
+
if (c?.tag !== tag) {
|
|
161
|
+
const gz = text.length < JSON_GZIP_AWAY_FROM ? Promise.resolve(gzipSync(text)) : new Promise((ok, fail) => gzip(text, (e, b) => (e ? fail(e) : ok(b))));
|
|
162
|
+
revisionGz.set(of, (c = { tag, gz }));
|
|
163
|
+
// a compression that failed is tried again by the next page that asks
|
|
164
|
+
gz.catch(() => revisionGz.get(of)?.gz === gz && revisionGz.delete(of));
|
|
165
|
+
}
|
|
166
|
+
c.gz.then((gz) => {
|
|
167
|
+
if (res.headersSent || res.destroyed)
|
|
168
|
+
return;
|
|
169
|
+
res.writeHead(200, { ...head, "content-encoding": "gzip" });
|
|
170
|
+
res.end(gz);
|
|
171
|
+
}, (e) => res.destroy(e));
|
|
172
|
+
}
|
|
173
|
+
/** Send a text that is not JSON (the agents' documentation, the MCP server's file), compressed
|
|
174
|
+
* where the client takes it. */
|
|
175
|
+
export function sendText(req, res, text, type, code = 200) {
|
|
176
|
+
if (res.headersSent || res.writableEnded || refused.has(res))
|
|
177
|
+
return;
|
|
178
|
+
const head = { "content-type": type, "cache-control": "no-store", vary: "accept-encoding" };
|
|
179
|
+
if (text.length > JSON_GZIP_FROM && acceptsGzip(req)) {
|
|
180
|
+
res.writeHead(code, { ...head, "content-encoding": "gzip" });
|
|
181
|
+
return void res.end(gzipSync(text));
|
|
182
|
+
}
|
|
183
|
+
res.writeHead(code, head);
|
|
184
|
+
res.end(text);
|
|
185
|
+
}
|
|
186
|
+
/** Send a file to download (a typeset document): as it is, never cached, named for the disk. */
|
|
187
|
+
export function sendDownload(res, f) {
|
|
188
|
+
if (res.headersSent || res.writableEnded || refused.has(res))
|
|
189
|
+
return;
|
|
190
|
+
res.writeHead(200, { "content-type": f.type, "content-length": String(f.body.length), "content-disposition": `attachment; filename="${f.name.replace(/[^A-Za-z0-9._-]/g, "_")}"`, "cache-control": "no-store" });
|
|
191
|
+
res.end(f.body);
|
|
192
|
+
}
|
|
193
|
+
/** The Host a request was sent to, when it looks like one. */
|
|
194
|
+
export function hostOf(req) {
|
|
195
|
+
return /^[A-Za-z0-9.:[\]-]{1,300}$/.test(req.headers.host ?? "") ? req.headers.host : "127.0.0.1";
|
|
196
|
+
}
|
|
197
|
+
/** The server's address as the request's browser reached it: its Origin where it names the host
|
|
198
|
+
* the request was sent to (a page's POST carries it, with the scheme), else http:// and the Host. */
|
|
199
|
+
export function reachedAt(req) {
|
|
200
|
+
const host = hostOf(req);
|
|
201
|
+
try {
|
|
202
|
+
const o = new URL(String(req.headers.origin ?? ""));
|
|
203
|
+
if (o.host === host && (o.protocol === "http:" || o.protocol === "https:"))
|
|
204
|
+
return o.origin;
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
// no Origin, or not an address: the Host
|
|
208
|
+
}
|
|
209
|
+
return `http://${host}`;
|
|
210
|
+
}
|
|
211
|
+
/** characters from which a JSON answer is sent compressed */
|
|
212
|
+
const JSON_GZIP_FROM = 2048;
|
|
213
|
+
/** characters from which a JSON answer is compressed off the server's thread: a snapshot's whole
|
|
214
|
+
* graph on NavierStokesAndEuler (GET /api/agent/history/graph, 105 MB of JSON) was 2 to 3 s of
|
|
215
|
+
* compressing, every page and agent waiting meanwhile */
|
|
216
|
+
export const JSON_GZIP_AWAY_FROM = 1 << 20;
|
|
217
|
+
/** the graph's parts as sent, each tagged once (a graph's buffers live as long as it does) */
|
|
218
|
+
const gzTags = new WeakMap();
|
|
219
|
+
/** A part of the graph, gzipped already: tagged, so that a page that has it gets 304; a client that
|
|
220
|
+
* does not take gzip (an agent's) is sent it as it is, made so off the server's thread. */
|
|
221
|
+
export function sendGz(req, res, gz, type) {
|
|
222
|
+
if (refused.has(res))
|
|
223
|
+
return;
|
|
224
|
+
let etag = gzTags.get(gz);
|
|
225
|
+
if (!etag)
|
|
226
|
+
gzTags.set(gz, (etag = tagOf(gz)));
|
|
227
|
+
const head = { "content-type": type, "cache-control": "private, no-cache", etag, vary: "accept-encoding" };
|
|
228
|
+
if (fresh(req, etag)) {
|
|
229
|
+
res.writeHead(304, head);
|
|
230
|
+
return void res.end();
|
|
231
|
+
}
|
|
232
|
+
if (!acceptsGzip(req))
|
|
233
|
+
return void gunzip(gz, (e, plain) => {
|
|
234
|
+
if (e)
|
|
235
|
+
return res.destroy(e);
|
|
236
|
+
res.writeHead(200, head);
|
|
237
|
+
res.end(plain);
|
|
238
|
+
});
|
|
239
|
+
res.writeHead(200, { ...head, "content-encoding": "gzip" });
|
|
240
|
+
res.end(gz);
|
|
241
|
+
}
|
|
242
|
+
/** the edges packed for pages (shared/edgecodec.ts), once per graph and format */
|
|
243
|
+
const packedEdges = new WeakMap();
|
|
244
|
+
export function packedGz(edgesGz, format) {
|
|
245
|
+
let byFormat = packedEdges.get(edgesGz);
|
|
246
|
+
if (!byFormat)
|
|
247
|
+
packedEdges.set(edgesGz, (byFormat = new Map()));
|
|
248
|
+
let p = byFormat.get(format);
|
|
249
|
+
if (!p) {
|
|
250
|
+
const raw = gunzipSync(edgesGz);
|
|
251
|
+
const edges = new Int32Array(raw.buffer, raw.byteOffset, raw.byteLength / 4);
|
|
252
|
+
let n = 0;
|
|
253
|
+
for (let k = 0; k < edges.length; k += 3)
|
|
254
|
+
n = Math.max(n, edges[k] + 1, edges[k + 1] + 1);
|
|
255
|
+
p = gzipSync(packEdges(edges, n, format), { level: 9 });
|
|
256
|
+
byFormat.set(format, p);
|
|
257
|
+
}
|
|
258
|
+
return p;
|
|
259
|
+
}
|