pi-studio 0.11.8 → 0.11.10
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/CHANGELOG.md +14 -0
- package/README.md +1 -1
- package/ROADMAP.md +12 -2
- package/client/studio-client.js +164 -21
- package/client/studio.css +147 -1
- package/index.ts +189 -36
- package/package.json +1 -1
- package/shared/studio-repl-activity.js +80 -0
- package/shared/studio-repl-files.js +273 -0
- package/shared/studio-repl-selection.js +10 -3
- package/shared/studio-repl-tmux.js +19 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { createHash, createHmac } from "node:crypto";
|
|
2
|
+
import { realpathSync, statSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { extname, isAbsolute, resolve } from "node:path";
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Image and PDF files a REPL record entry's code saved, such as a plot, so the REPL pane can show
|
|
8
|
+
* them, as pi-markdown-preview's REPL page does (its repl-files.js). A file counts only if the
|
|
9
|
+
* entry's code or output names it and it was written while the entry ran: reading names from
|
|
10
|
+
* text can't be made exact across languages and prose, so the time it was written decides
|
|
11
|
+
* whether a name found there is shown (Sol, Astra, candidate 116). The page gets each file
|
|
12
|
+
* through an opaque route that only this list makes; nothing else is served.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export const STUDIO_REPL_FILE_CONTENT_TYPES = new Map([
|
|
16
|
+
[".png", "image/png"], [".jpg", "image/jpeg"], [".jpeg", "image/jpeg"], [".gif", "image/gif"],
|
|
17
|
+
[".webp", "image/webp"], [".svg", "image/svg+xml"], [".pdf", "application/pdf"],
|
|
18
|
+
]);
|
|
19
|
+
const EXTENSIONS = "png|jpe?g|gif|webp|svg|pdf";
|
|
20
|
+
// A quoted path, as code writes one: savefig("out/fit.png"), png('plot.png'), `fit 2.png`.
|
|
21
|
+
const QUOTES = new Set(['"', "'", "`"]);
|
|
22
|
+
const QUOTED_PATH = new RegExp(`^[^\\r\\n\\0]{1,1024}\\.(?:${EXTENSIONS})$`, "i");
|
|
23
|
+
// A bare path, as output prints one: "Saved to /tmp/fit.png." (a sentence's full stop isn't part of it).
|
|
24
|
+
const BARE = new RegExp(`(?:^|[\\s(=:,\\[])((?:~|\\.{1,2})?\\/?(?:[\\w@%+.,~-]+\\/)*[\\w@%+.,~-]+\\.(?:${EXTENSIONS}))(?=$|[\\s)\\]:;,'"]|\\.(?:\\s|$))`, "gim");
|
|
25
|
+
const CANDIDATES_PER_ENTRY = 20;
|
|
26
|
+
// How long after an entry finished a file it names may still have been written by its code,
|
|
27
|
+
// such as by a plotting process that flushes after the prompt returns.
|
|
28
|
+
export const STUDIO_REPL_FILE_WRITE_GRACE_MS = 2_000;
|
|
29
|
+
export const STUDIO_REPL_FILES_PER_ENTRY = 6;
|
|
30
|
+
// The pane shows a session's latest 12 entries; only they are looked at.
|
|
31
|
+
export const STUDIO_REPL_FILE_ENTRIES = 12;
|
|
32
|
+
const STUDIO_REPL_FILE_ROUTES = 500;
|
|
33
|
+
// Versions of one file a route serves: those the page has been shown, the latest few.
|
|
34
|
+
const STUDIO_REPL_FILE_ROUTE_VERSIONS = 8;
|
|
35
|
+
export const STUDIO_REPL_FILE_MAX_BYTES = 64 * 1024 * 1024;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A text's quoted spans, each closed by its own delimiter on the same line, and the text with
|
|
39
|
+
* them blanked. Inside a span a backslash escapes the next character and a doubled delimiter
|
|
40
|
+
* stands for one (MATLAB's 'O''Brien'), so neither ends it; a span holding either isn't a name
|
|
41
|
+
* as written, so it names nothing (`plain: false`). A quote with no close on its line, such as
|
|
42
|
+
* an apostrophe in prose, starts no span, and nor do later copies of it on that line, so the
|
|
43
|
+
* scan stays linear however many there are (agent-markdown-preview's rule).
|
|
44
|
+
*/
|
|
45
|
+
function quotedSpans(text) {
|
|
46
|
+
const spans = [];
|
|
47
|
+
let unquoted = "";
|
|
48
|
+
// Where each quote last found no close: the end of that line.
|
|
49
|
+
const unclosedUntil = new Map();
|
|
50
|
+
let index = 0;
|
|
51
|
+
while (index < text.length) {
|
|
52
|
+
const quote = text[index];
|
|
53
|
+
let end = index + 1;
|
|
54
|
+
let closed = false;
|
|
55
|
+
let plain = true;
|
|
56
|
+
if (QUOTES.has(quote) && !(unclosedUntil.get(quote) > index)) {
|
|
57
|
+
while (end < text.length && text[end] !== "\n" && text[end] !== "\r") {
|
|
58
|
+
if (text[end] === "\\" && end + 1 < text.length && text[end + 1] !== "\n" && text[end + 1] !== "\r") { plain = false; end += 2; continue; }
|
|
59
|
+
if (text[end] === quote) {
|
|
60
|
+
if (text[end + 1] === quote) { plain = false; end += 2; continue; }
|
|
61
|
+
closed = true;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
end += 1;
|
|
65
|
+
}
|
|
66
|
+
if (!closed) unclosedUntil.set(quote, end);
|
|
67
|
+
}
|
|
68
|
+
if (closed) {
|
|
69
|
+
spans.push({ text: text.slice(index + 1, end), plain });
|
|
70
|
+
unquoted += " ".repeat(end + 1 - index);
|
|
71
|
+
index = end + 1;
|
|
72
|
+
} else {
|
|
73
|
+
unquoted += quote;
|
|
74
|
+
index += 1;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return { spans, unquoted };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The image and PDF paths an entry names, output first, as written, in order and without repeats.
|
|
82
|
+
* A quoted path is taken whole, and nothing inside quotes is searched again for bare paths, so
|
|
83
|
+
* "fit 2.png" and "O'Brien fit.png" can't name 2.png or fit.png (Sol, Astra, candidate 116).
|
|
84
|
+
* A quoted name that isn't a supported path as written names nothing. Prose and other
|
|
85
|
+
* languages' quoting can still mislead this; resolveStudioReplFiles's time check decides.
|
|
86
|
+
*/
|
|
87
|
+
export function studioReplFilePaths(entry) {
|
|
88
|
+
const found = new Set();
|
|
89
|
+
for (const text of [entry?.output, entry?.code]) {
|
|
90
|
+
if (typeof text !== "string") continue;
|
|
91
|
+
const { spans, unquoted } = quotedSpans(text);
|
|
92
|
+
for (const span of spans) if (span.plain && QUOTED_PATH.test(span.text)) found.add(span.text);
|
|
93
|
+
for (const match of unquoted.matchAll(BARE)) found.add(match[1]);
|
|
94
|
+
}
|
|
95
|
+
return [...found].slice(0, CANDIDATES_PER_ENTRY);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** A written path made absolute: against home for `~/`, else against the session's directory, if it has one. */
|
|
99
|
+
function resolveWritten(written, cwd) {
|
|
100
|
+
if (written.startsWith("~/")) return resolve(homedir(), written.slice(2));
|
|
101
|
+
if (isAbsolute(written)) return resolve(written);
|
|
102
|
+
const directory = cwd();
|
|
103
|
+
return typeof directory === "string" && isAbsolute(directory) ? resolve(directory, written) : undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A file's version: it changes when the file is written or replaced, even by a copy that keeps
|
|
108
|
+
* its modification time (cp -p), which changes its inode or change time (Sol, candidate 116).
|
|
109
|
+
*/
|
|
110
|
+
function studioReplFileVersion(info) {
|
|
111
|
+
return createHash("sha256").update([info.dev, info.ino, info.size, info.mtimeMs, info.ctimeMs].join(":")).digest("hex").slice(0, 16);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* When an entry's code could have written a file: from when the entry was recorded, before its
|
|
116
|
+
* code was sent, to a little after it finished, or after now while it runs (a file's time is
|
|
117
|
+
* finer than Date.now()'s). Undefined for an entry without a usable start time, which then
|
|
118
|
+
* shows no files.
|
|
119
|
+
*/
|
|
120
|
+
function studioReplEntryWriteWindow(entry, now) {
|
|
121
|
+
const start = Number(entry?.createdAt);
|
|
122
|
+
if (!Number.isFinite(start) || start <= 0) return undefined;
|
|
123
|
+
if (entry.status === "sending" || entry.status === "sent") return { start, end: now + STUDIO_REPL_FILE_WRITE_GRACE_MS };
|
|
124
|
+
const finished = Number.isFinite(Number(entry.completedAt)) && Number(entry.completedAt) > 0 ? Number(entry.completedAt) : Number(entry.updatedAt);
|
|
125
|
+
return { start, end: (Number.isFinite(finished) && finished >= start ? finished : start) + STUDIO_REPL_FILE_WRITE_GRACE_MS };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* When an entry finished: its completion, or for a finished status without one its last update.
|
|
130
|
+
* Undefined while it's sending or sent, or without a usable time.
|
|
131
|
+
*/
|
|
132
|
+
function studioReplEntryFinish(entry) {
|
|
133
|
+
const completed = Number(entry.completedAt);
|
|
134
|
+
if (Number.isFinite(completed) && completed > 0) return completed;
|
|
135
|
+
if (entry.status === "sending" || entry.status === "sent") return undefined;
|
|
136
|
+
const updated = Number(entry.updatedAt);
|
|
137
|
+
return Number.isFinite(updated) && updated > 0 ? updated : undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* When each entry with code had its session to itself. Sends to a session take turns under its
|
|
142
|
+
* lease, but not in the order they were recorded: a page's send is recorded before it waits for
|
|
143
|
+
* the lease, an agent's once it has it (Sol, candidate 116 round 5). So the order is the order
|
|
144
|
+
* they finished in. An entry's turn starts when it was recorded or when the one before it
|
|
145
|
+
* finished, whichever is later, and a file written once the next one started is the next one's.
|
|
146
|
+
* `start` and `before` bound the time a file was written.
|
|
147
|
+
* - While entries are still running, the last to finish keeps no grace after it: one of them
|
|
148
|
+
* may have started then. Each running entry starts at the last finish or its recording.
|
|
149
|
+
* - Notes have no code and don't count; an entry without a usable time has no bounds.
|
|
150
|
+
*
|
|
151
|
+
* @returns {Map<string, { start: number, before: number }>} by entry ID
|
|
152
|
+
*/
|
|
153
|
+
export function studioReplSessionRunBounds(entries) {
|
|
154
|
+
const sessions = new Map();
|
|
155
|
+
for (const entry of entries ?? []) {
|
|
156
|
+
if (!entry || entry.status === "note" || typeof entry.code !== "string" || !entry.code.trim()) continue;
|
|
157
|
+
if (!Number.isFinite(Number(entry.createdAt)) || Number(entry.createdAt) <= 0) continue;
|
|
158
|
+
const list = sessions.get(entry.sessionName) ?? [];
|
|
159
|
+
list.push(entry);
|
|
160
|
+
sessions.set(entry.sessionName, list);
|
|
161
|
+
}
|
|
162
|
+
const bounds = new Map();
|
|
163
|
+
for (const list of sessions.values()) {
|
|
164
|
+
const finished = [];
|
|
165
|
+
const running = [];
|
|
166
|
+
for (const entry of list) {
|
|
167
|
+
const finish = studioReplEntryFinish(entry);
|
|
168
|
+
if (finish !== undefined) finished.push({ entry, finish });
|
|
169
|
+
else if (entry.status === "sending" || entry.status === "sent") running.push(entry);
|
|
170
|
+
}
|
|
171
|
+
finished.sort((a, b) => a.finish - b.finish);
|
|
172
|
+
let lastFinish = -Infinity;
|
|
173
|
+
const starts = finished.map(({ entry, finish }) => {
|
|
174
|
+
const start = Math.max(Number(entry.createdAt), lastFinish);
|
|
175
|
+
lastFinish = finish;
|
|
176
|
+
return start;
|
|
177
|
+
});
|
|
178
|
+
finished.forEach(({ entry, finish }, index) => {
|
|
179
|
+
const before = index + 1 < finished.length ? starts[index + 1] : running.length ? finish : Infinity;
|
|
180
|
+
bounds.set(entry.id, { start: starts[index], before });
|
|
181
|
+
});
|
|
182
|
+
for (const entry of running) bounds.set(entry.id, { start: Math.max(Number(entry.createdAt), lastFinish), before: Infinity });
|
|
183
|
+
}
|
|
184
|
+
return bounds;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* The files an entry names that its code wrote: up to STUDIO_REPL_FILES_PER_ENTRY of
|
|
189
|
+
* `{ written, path, contentType, version }`, each a regular file last modified while the entry
|
|
190
|
+
* ran (studioReplEntryWriteWindow). A file written before it, such as one whose name merely
|
|
191
|
+
* shares a tail with the name the code wrote, or rewritten since by a later entry, isn't
|
|
192
|
+
* shown. `bounds` (studioReplSessionRunBounds) narrows that to the entry's own turn in its
|
|
193
|
+
* session. `cwd` gives the session's working directory, for relative paths: a string, or a
|
|
194
|
+
* function asked at most once, and only for a relative path. Without it only absolute and
|
|
195
|
+
* `~/` paths resolve.
|
|
196
|
+
*
|
|
197
|
+
* @param {any} entry
|
|
198
|
+
* @param {string | (() => string | undefined) | undefined} cwd
|
|
199
|
+
* @param {{ statFile?: (path: string) => import("node:fs").Stats, now?: number, bounds?: { start: number, before: number } }} [options]
|
|
200
|
+
*/
|
|
201
|
+
export function resolveStudioReplFiles(entry, cwd, { statFile = statSync, now = Date.now(), bounds } = {}) {
|
|
202
|
+
const window = studioReplEntryWriteWindow(entry, now);
|
|
203
|
+
if (!window) return [];
|
|
204
|
+
const start = Math.max(window.start, Number.isFinite(bounds?.start) ? bounds.start : -Infinity);
|
|
205
|
+
const before = typeof bounds?.before === "number" ? bounds.before : Infinity;
|
|
206
|
+
let directory;
|
|
207
|
+
let asked = false;
|
|
208
|
+
const directoryOnce = () => {
|
|
209
|
+
if (!asked) { asked = true; directory = typeof cwd === "function" ? cwd() : cwd; }
|
|
210
|
+
return directory;
|
|
211
|
+
};
|
|
212
|
+
const files = [];
|
|
213
|
+
for (const written of studioReplFilePaths(entry)) {
|
|
214
|
+
if (files.length >= STUDIO_REPL_FILES_PER_ENTRY) break;
|
|
215
|
+
const contentType = STUDIO_REPL_FILE_CONTENT_TYPES.get(extname(written).toLowerCase());
|
|
216
|
+
if (!contentType) continue;
|
|
217
|
+
const path = resolveWritten(written, directoryOnce);
|
|
218
|
+
if (!path || files.some((file) => file.path === path)) continue;
|
|
219
|
+
let info;
|
|
220
|
+
try { info = statFile(path); } catch { continue; }
|
|
221
|
+
if (!info?.isFile() || !(info.mtimeMs >= start && info.mtimeMs <= window.end && info.mtimeMs < before)) continue;
|
|
222
|
+
files.push({ written, path, contentType, version: studioReplFileVersion(info) });
|
|
223
|
+
}
|
|
224
|
+
return files;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Routes for the files the page has been shown: an opaque ID per path, keyed to this Studio
|
|
229
|
+
* server's token, kept for the most recent STUDIO_REPL_FILE_ROUTES files. A route serves only
|
|
230
|
+
* the versions of its file the page was shown (Sol, candidate 116 round 4), so a file replaced
|
|
231
|
+
* since, by anything, isn't served until an entry shows that version.
|
|
232
|
+
*/
|
|
233
|
+
export function createStudioReplFileRoutes(secret) {
|
|
234
|
+
const routes = new Map();
|
|
235
|
+
return {
|
|
236
|
+
add(file) {
|
|
237
|
+
const id = createHmac("sha256", secret).update("repl-file\0").update(file.path).digest("hex");
|
|
238
|
+
const known = routes.get(id);
|
|
239
|
+
const versions = known?.contentType === file.contentType ? known.versions.filter((version) => version !== file.version) : [];
|
|
240
|
+
versions.push(file.version);
|
|
241
|
+
routes.delete(id);
|
|
242
|
+
routes.set(id, { path: file.path, contentType: file.contentType, versions: versions.slice(-STUDIO_REPL_FILE_ROUTE_VERSIONS) });
|
|
243
|
+
while (routes.size > STUDIO_REPL_FILE_ROUTES) routes.delete(routes.keys().next().value);
|
|
244
|
+
return id;
|
|
245
|
+
},
|
|
246
|
+
get(id) {
|
|
247
|
+
return typeof id === "string" && /^[0-9a-f]{64}$/.test(id) ? routes.get(id) : undefined;
|
|
248
|
+
},
|
|
249
|
+
get size() { return routes.size; },
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* The file a route serves now, or why not: it must still be a regular file, within the size
|
|
255
|
+
* limit, whose real path has an extension of the same type as when it was shown, in a version
|
|
256
|
+
* the page was shown.
|
|
257
|
+
*
|
|
258
|
+
* @returns {{ ok: true, filePath: string, contentType: string } | { ok: false, status: number, message: string }}
|
|
259
|
+
*/
|
|
260
|
+
export function openStudioReplFileRoute(route, { realpath = realpathSync, statFile = statSync } = {}) {
|
|
261
|
+
if (!route) return { ok: false, status: 404, message: "This file is no longer shown in the REPL pane." };
|
|
262
|
+
let filePath;
|
|
263
|
+
try { filePath = realpath(route.path); } catch { return { ok: false, status: 404, message: "This file no longer exists." }; }
|
|
264
|
+
if (STUDIO_REPL_FILE_CONTENT_TYPES.get(extname(filePath).toLowerCase()) !== route.contentType) {
|
|
265
|
+
return { ok: false, status: 404, message: "This file is no longer the file that was shown." };
|
|
266
|
+
}
|
|
267
|
+
let info;
|
|
268
|
+
try { info = statFile(filePath); } catch { return { ok: false, status: 404, message: "This file no longer exists." }; }
|
|
269
|
+
if (!info.isFile()) return { ok: false, status: 404, message: "This file is no longer the file that was shown." };
|
|
270
|
+
if (info.size > STUDIO_REPL_FILE_MAX_BYTES) return { ok: false, status: 413, message: "This file is too large to show here." };
|
|
271
|
+
if (!route.versions?.includes(studioReplFileVersion(info))) return { ok: false, status: 404, message: "This file is no longer the file that was shown." };
|
|
272
|
+
return { ok: true, filePath, contentType: route.contentType };
|
|
273
|
+
}
|
|
@@ -32,8 +32,13 @@ const sameSelection = (left, right) => (left.session || right.session
|
|
|
32
32
|
? sameReplSessionLifetime(left.session, right.session)
|
|
33
33
|
: left.runtime === right.runtime);
|
|
34
34
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* `onChange`, when given, is called with the selected session (or null) whenever the selection
|
|
37
|
+
* changes, such as to tell pi-repl what Studio's pane shows (candidate 118).
|
|
38
|
+
* @template {ReplSessionLifetime} S
|
|
39
|
+
* @param {{ onChange?: (session: S | null) => void }} [options]
|
|
40
|
+
*/
|
|
41
|
+
export function createReplSelection({ onChange } = {}) {
|
|
37
42
|
/** @type {{ session?: S, runtime?: string | null }} */
|
|
38
43
|
let selected = { runtime: null };
|
|
39
44
|
let shown = 0;
|
|
@@ -44,8 +49,10 @@ export function createReplSelection() {
|
|
|
44
49
|
/** @param {{ session?: S | null, runtime?: string | null }} target */
|
|
45
50
|
const show = (target) => {
|
|
46
51
|
const next = toSelection(target);
|
|
47
|
-
|
|
52
|
+
const changed = !sameSelection(selected, next);
|
|
53
|
+
if (changed) shown += 1;
|
|
48
54
|
selected = next;
|
|
55
|
+
if (changed) onChange?.(selected.session ?? null);
|
|
49
56
|
};
|
|
50
57
|
|
|
51
58
|
return {
|
|
@@ -127,6 +127,25 @@ export function firstStudioReplPanes(stdout) {
|
|
|
127
127
|
return [...first.values()];
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
/**
|
|
131
|
+
* A pane's current directory, read on its own by pane ID with display-message, for the relative
|
|
132
|
+
* paths of files a record entry names. It isn't in STUDIO_REPL_PANE_FORMAT: a directory's name
|
|
133
|
+
* can hold a newline, which there could forge another pane's row (Sol, candidate 116).
|
|
134
|
+
*/
|
|
135
|
+
export const STUDIO_REPL_PANE_CWD_FORMAT = "#{pane_current_path}";
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The directory from display-message's output, or "" unless it's one absolute path of at most
|
|
139
|
+
* 4,096 characters with no line break or NUL.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} stdout
|
|
142
|
+
* @returns {string}
|
|
143
|
+
*/
|
|
144
|
+
export function parseStudioReplPaneCwd(stdout) {
|
|
145
|
+
const path = String(stdout ?? "").replace(/\n$/, "");
|
|
146
|
+
return path.startsWith("/") && path.length <= 4096 && !/[\r\n\0]/.test(path) ? path : "";
|
|
147
|
+
}
|
|
148
|
+
|
|
130
149
|
const STUDIO_REPL_READY_PROMPTS = {
|
|
131
150
|
python: /^>>>[ \t]*$/,
|
|
132
151
|
ipython: /^In \[\d+\]:[ \t]*$/,
|