longtable 0.1.35 → 0.1.38
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/bin/longtable.mjs +11 -3
- package/package.json +1 -1
- package/src/approve-server.mjs +109 -13
- package/src/claude.mjs +139 -31
- package/src/extras.mjs +2 -1
- package/src/inbox-hook.mjs +54 -0
- package/src/pointed.mjs +3 -2
- package/src/procs.mjs +152 -0
- package/src/repo.mjs +130 -12
- package/src/run.mjs +344 -40
- package/src/sessions.mjs +93 -0
package/src/sessions.mjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// One Claude Code session per chat for a person's own requests in their own folder, as at home: each request picks up
|
|
2
|
+
// where the last one left off (what it read, ran and found is still there), and Claude Code compacts it by itself when
|
|
3
|
+
// it fills up. Each person's sessions are their own, on their own computer: one person's filling up never touches
|
|
4
|
+
// another's. Kept here: which session belongs to which chat, and what of the chat that session has already seen, so a
|
|
5
|
+
// request sends only what is new.
|
|
6
|
+
|
|
7
|
+
import { randomUUID } from "node:crypto";
|
|
8
|
+
import { mkdirSync, readFileSync, readdirSync, rmSync } from "node:fs";
|
|
9
|
+
import { join, resolve } from "node:path";
|
|
10
|
+
import { home, writeWhole } from "./store.mjs";
|
|
11
|
+
|
|
12
|
+
const dir = () => join(home, "sessions");
|
|
13
|
+
const STARTED = "started.list";
|
|
14
|
+
const fileFor = (table) => join(dir(), `${String(table).replace(/[^A-Za-z0-9_-]/g, "")}.json`);
|
|
15
|
+
const read = (table) => { try { const v = JSON.parse(readFileSync(fileFor(table), "utf8")); return v && typeof v === "object" ? v : {}; } catch { return {}; } };
|
|
16
|
+
const sameFolder = (a, b) => !!a && !!b && resolve(a).toLowerCase() === resolve(b).toLowerCase();
|
|
17
|
+
|
|
18
|
+
// A chat's session at this table, when it was started in this same folder (Claude Code keeps a session under the
|
|
19
|
+
// folder it ran in, so it can only be picked up from there).
|
|
20
|
+
export function sessionFor(table, chat, folder) {
|
|
21
|
+
const s = read(table)[chat];
|
|
22
|
+
return s && typeof s.id === "string" && sameFolder(s.folder, folder) ? s : null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// A new session, written down before it runs: every session it ever started is kept, even after a disconnect or a
|
|
26
|
+
// failed first run, so none is read later as the person's own conversation.
|
|
27
|
+
export function newSessionId() {
|
|
28
|
+
const id = randomUUID();
|
|
29
|
+
let started = [];
|
|
30
|
+
try { started = JSON.parse(readFileSync(join(dir(), STARTED), "utf8")); } catch {}
|
|
31
|
+
mkdirSync(dir(), { recursive: true });
|
|
32
|
+
writeWhole(join(dir(), STARTED), JSON.stringify([...(Array.isArray(started) ? started : []), id].slice(-2000)));
|
|
33
|
+
return id;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// What of the chat this session has not seen: messages after the last one it was given, without the requests it was
|
|
37
|
+
// given and the answers it wrote (both already in the session), told apart by their ids, never by their words: a
|
|
38
|
+
// teammate's "yes" is not the person's earlier "yes".
|
|
39
|
+
export function unseen(history, s) {
|
|
40
|
+
const asked = new Set(s?.asked || []);
|
|
41
|
+
const jobs = new Set(s?.jobs || []);
|
|
42
|
+
return (history || []).filter((m) => (!s?.lastAt || String(m.created_at) > String(s.lastAt))
|
|
43
|
+
&& !(m.id && asked.has(m.id)) && !(m.job_id && jobs.has(m.job_id)));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// After a request: the session, the newest message it has now seen, and what it was asked and answered.
|
|
47
|
+
export function remember(table, chat, { id, folder, history, questionId, jobId }) {
|
|
48
|
+
const all = read(table);
|
|
49
|
+
const was = all[chat]?.id === id ? all[chat] : {};
|
|
50
|
+
const last = (history || []).reduce((at, m) => (String(m.created_at) > at ? String(m.created_at) : at), String(was.lastAt || ""));
|
|
51
|
+
all[chat] = {
|
|
52
|
+
id, folder, lastAt: last || null, at: Date.now(),
|
|
53
|
+
asked: [...(was.asked || []), questionId].filter(Boolean).slice(-200),
|
|
54
|
+
jobs: [...(was.jobs || []), jobId].filter(Boolean).slice(-200),
|
|
55
|
+
};
|
|
56
|
+
mkdirSync(dir(), { recursive: true });
|
|
57
|
+
writeWhole(fileFor(table), JSON.stringify(all));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function forgetSession(table, chat) {
|
|
61
|
+
const all = read(table);
|
|
62
|
+
if (!all[chat]) return;
|
|
63
|
+
delete all[chat];
|
|
64
|
+
writeWhole(fileFor(table), JSON.stringify(all));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Every session id Longtable started on this computer, so reading the person's own recent conversation never mistakes
|
|
68
|
+
// a table's session for one of theirs.
|
|
69
|
+
export function tableSessionIds() {
|
|
70
|
+
const ids = new Set();
|
|
71
|
+
let files = [];
|
|
72
|
+
try { for (const id of JSON.parse(readFileSync(join(dir(), STARTED), "utf8"))) ids.add(id); } catch {}
|
|
73
|
+
try { files = readdirSync(dir()).filter((f) => f.endsWith(".json")); } catch { return ids; }
|
|
74
|
+
for (const f of files) {
|
|
75
|
+
try { for (const s of Object.values(JSON.parse(readFileSync(join(dir(), f), "utf8")))) if (s?.id) ids.add(s.id); } catch {}
|
|
76
|
+
}
|
|
77
|
+
return ids;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function forgetTableSessions(tables) {
|
|
81
|
+
for (const t of tables || []) rmSync(fileFor(t), { force: true });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// One request at a time in a session: a second one in the same chat while the first runs starts fresh instead.
|
|
85
|
+
const busy = new Set();
|
|
86
|
+
export function takeSession(table, chat) {
|
|
87
|
+
const k = `${table}:${chat}`;
|
|
88
|
+
if (busy.has(k)) return false;
|
|
89
|
+
busy.add(k);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
export function giveBackSession(table, chat) { busy.delete(`${table}:${chat}`); }
|
|
93
|
+
export const sessionInUse = (key) => busy.has(key);
|