-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsessions.ts
More file actions
295 lines (274 loc) · 11 KB
/
Copy pathsessions.ts
File metadata and controls
295 lines (274 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import * as nodeFs from "node:fs";
import * as path from "node:path";
export interface SessionListEntry {
id: string;
cwd: string;
displayName: string;
rawSummary: string;
customName?: string;
updatedAt: number;
createdAt: number;
numMessages: number;
modelId?: string;
}
export interface SessionMetaOverride {
customName?: string;
pinnedAt?: number;
/** Last verdict the user gave to an exit_plan_mode card in this session, for the restore-card label. */
lastPlanVerdict?: "approved" | "rejected" | "abandoned";
/** Every plan the user resolved in this session, in chronological order. grok's plan.md only
* retains the latest plan content on disk; saving each one here lets the resume view replay
* rejected/cancelled plans that grok overwrote later in the conversation. `afterUserMessage`
* is the count of user messages that had been sent at the moment the plan was resolved, so
* the resume view can render each card right after that message instead of dumping all the
* plan cards at the bottom of the restored conversation. */
plans?: { text: string; verdict: "approved" | "rejected" | "abandoned"; afterUserMessage?: number }[];
/** Dashboard "unread" badge: a turn finished while this session wasn't focused and
* hasn't been opened since. Drives the green/red dot; cleared on open. Persisted
* (not tied to the live process) so the badge survives reaping and a reload. */
unread?: boolean;
/** The unread turn ended in an error (red dot instead of green). */
unreadError?: boolean;
}
export type SessionMetaOverrides = Record<string, SessionMetaOverride>;
/** Move a renamed session's `customName` from one id to another and drop the source entry. Used when
* a primer-only session is discarded and restarted under a new grok id (a model/effort switch on an
* empty session): the user's rename should follow to the new session, and the abandoned id's
* override must not linger. Only `customName` carries — a fresh session has no plans/unread/etc.
* worth keeping. Pure: removing the on-disk dir is the caller's job. Returns a new map; the input is
* left untouched. No-op carry when the source has no `customName` or `toId` is undefined. */
export function carrySessionName(
overrides: SessionMetaOverrides,
fromId: string,
toId: string | undefined,
): SessionMetaOverrides {
const next: SessionMetaOverrides = { ...overrides };
const carried = next[fromId]?.customName?.trim();
delete next[fromId];
if (carried && toId) next[toId] = { ...(next[toId] ?? {}), customName: carried };
return next;
}
export interface FsLike {
existsSync(p: string): boolean;
readdirSync(p: string): string[];
readFileSync(p: string, encoding: "utf8"): string;
statSync(p: string): { isDirectory(): boolean; mtimeMs: number };
rmSync?(p: string, opts?: { recursive?: boolean; force?: boolean }): void;
rmdirSync(p: string, opts?: { recursive?: boolean }): void;
}
export interface ListDeps {
fs: FsLike;
grokHome: string;
cwd: string;
overrides: SessionMetaOverrides;
now?: () => number;
log?: (msg: string) => void;
}
export interface DeleteDeps {
fs: FsLike;
grokHome: string;
cwd: string;
id: string;
}
/** Build the directory grok uses for sessions rooted at `cwd`. Mirrors grok's URL-encoded layout. */
export function sessionsDirFor(grokHome: string, cwd: string): string {
return path.join(grokHome, "sessions", encodeURIComponent(cwd));
}
/** Default friendly name when no `customName` or `session_summary` is available. */
export function fallbackName(summary: string, updatedAt: number): string {
const s = (summary || "").trim();
if (s) return s.length > 60 ? s.slice(0, 57) + "…" : s;
const d = new Date(updatedAt || Date.now());
if (isNaN(d.getTime())) return "Untitled";
const yyyy = d.getFullYear();
const mm = String(d.getMonth() + 1).padStart(2, "0");
const dd = String(d.getDate()).padStart(2, "0");
const hh = String(d.getHours()).padStart(2, "0");
const mi = String(d.getMinutes()).padStart(2, "0");
return `Untitled (${yyyy}-${mm}-${dd} ${hh}:${mi})`;
}
function parseTimestamp(s: unknown, fallback: number): number {
if (typeof s !== "string") return fallback;
const t = Date.parse(s);
return isNaN(t) ? fallback : t;
}
/** Parse one already-read summary.json into a list entry, applying any customName override. */
function buildEntry(
dirName: string,
raw: any,
cwd: string,
overrides: SessionMetaOverrides,
fallbackNow: number,
): SessionListEntry {
const id = (raw?.info?.id as string) ?? dirName;
const sessCwd = (raw?.info?.cwd as string) ?? cwd;
const rawSummary = typeof raw?.session_summary === "string" ? raw.session_summary : "";
const updatedAt = parseTimestamp(raw?.updated_at, fallbackNow);
const createdAt = parseTimestamp(raw?.created_at, updatedAt);
const numMessages = typeof raw?.num_messages === "number" ? raw.num_messages : 0;
const modelId = typeof raw?.current_model_id === "string" ? raw.current_model_id : undefined;
const override = overrides[id];
const customName = override?.customName?.trim() || undefined;
const displayName = customName || fallbackName(rawSummary, updatedAt);
return { id, cwd: sessCwd, displayName, rawSummary, customName, updatedAt, createdAt, numMessages, modelId };
}
export interface SessionIndexEntry {
/** Directory name = grok session id. */
id: string;
/** Modification time of the session's `summary.json` (ms). A cheap proxy for last activity —
* grok rewrites that file (which also holds `updated_at`) on every turn. */
mtimeMs: number;
}
export interface IndexDeps {
fs: FsLike;
grokHome: string;
cwd: string;
log?: (msg: string) => void;
}
/** Cheap ordering pass: every session id newest-first by `summary.json` mtime, WITHOUT reading or
* parsing any summary content. One `stat` per dir instead of a `stat` + `read` + `JSON.parse`, so
* it stays fast even with thousands of sessions. The caller reads (via `readSessionEntries`) only
* the window it actually shows. mtime is an approximate sort key; the exact `updated_at` order is
* re-applied within the loaded page after reading. */
export function indexSessions(deps: IndexDeps): SessionIndexEntry[] {
const { fs, grokHome, cwd, log } = deps;
const dir = sessionsDirFor(grokHome, cwd);
if (!fs.existsSync(dir)) return [];
let names: string[];
try {
names = fs.readdirSync(dir);
} catch (e) {
log?.(`[sessions] failed to read ${dir}: ${(e as Error).message}`);
return [];
}
const out: SessionIndexEntry[] = [];
for (const name of names) {
const summaryPath = path.join(dir, name, "summary.json");
let st: { mtimeMs: number };
try {
// A stat on summary.json doubles as the "is this a real session dir?" check: a stray file
// entry (or a dir without summary.json) makes the join non-existent and statSync throws.
st = fs.statSync(summaryPath);
} catch {
continue;
}
out.push({ id: name, mtimeMs: st.mtimeMs });
}
out.sort((a, b) => b.mtimeMs - a.mtimeMs);
return out;
}
export interface ReadEntriesDeps {
fs: FsLike;
grokHome: string;
cwd: string;
ids: string[];
overrides: SessionMetaOverrides;
now?: () => number;
log?: (msg: string) => void;
}
/** Read + parse summary.json for exactly the given ids (a page), returning full list entries in the
* same order. Malformed or vanished entries are skipped. This is the only path that touches file
* content, so callers keep it to the visible window. */
export function readSessionEntries(deps: ReadEntriesDeps): SessionListEntry[] {
const { fs, grokHome, cwd, ids, overrides, log } = deps;
const now = deps.now ? deps.now() : Date.now();
const dir = sessionsDirFor(grokHome, cwd);
const out: SessionListEntry[] = [];
for (const id of ids) {
const summaryPath = path.join(dir, id, "summary.json");
let raw: any;
try {
raw = JSON.parse(fs.readFileSync(summaryPath, "utf8"));
} catch (e) {
log?.(`[sessions] could not read summary.json for ${id}: ${(e as Error).message}`);
continue;
}
out.push(buildEntry(id, raw, cwd, overrides, now));
}
return out;
}
/** Full session list sorted by last activity. Equivalent to `indexSessions` + `readSessionEntries`
* over every id; reads every summary.json, so prefer the paginated index/read primitives on hot
* paths. Kept for callers that genuinely need the whole list at once. */
export function listSessions(deps: ListDeps): SessionListEntry[] {
const { fs, grokHome, cwd, overrides, log } = deps;
const now = deps.now ? deps.now() : Date.now();
const index = indexSessions({ fs, grokHome, cwd, log });
const out = readSessionEntries({
fs,
grokHome,
cwd,
ids: index.map((e) => e.id),
overrides,
now: () => now,
log,
});
out.sort((a, b) => b.updatedAt - a.updatedAt);
return out;
}
/** Remove the on-disk session directory. No-op if missing. */
export function deleteSessionDir(deps: DeleteDeps): void {
const { fs, grokHome, cwd, id } = deps;
const dir = path.join(sessionsDirFor(grokHome, cwd), id);
if (!fs.existsSync(dir)) return;
if (fs.rmSync) {
fs.rmSync(dir, { recursive: true, force: true });
} else {
fs.rmdirSync(dir, { recursive: true });
}
}
export interface ClearDeps {
fs: FsLike;
grokHome: string;
cwd: string;
/** Session id to keep (the live/focused one — grok re-persists it, so deleting it wouldn't stick). */
exceptId?: string;
}
/** Remove every session directory under `cwd`, optionally keeping one. Returns the ids it removed.
* Best-effort: a directory that fails to remove is skipped, not thrown, so one locked dir doesn't
* abort the sweep. The directory name is the session id (mirrors `deleteSessionDir`). */
export function clearSessions(deps: ClearDeps): string[] {
const { fs, grokHome, cwd, exceptId } = deps;
const dir = sessionsDirFor(grokHome, cwd);
if (!fs.existsSync(dir)) return [];
let entries: string[];
try {
entries = fs.readdirSync(dir);
} catch {
return [];
}
const removed: string[] = [];
for (const name of entries) {
if (exceptId && name === exceptId) continue;
const full = path.join(dir, name);
try {
if (!fs.statSync(full).isDirectory()) continue;
} catch {
continue;
}
try {
if (fs.rmSync) fs.rmSync(full, { recursive: true, force: true });
else fs.rmdirSync(full, { recursive: true });
removed.push(name);
} catch {
continue;
}
}
return removed;
}
/** Default node fs adapter for production use. */
export const defaultFs: FsLike = {
existsSync: nodeFs.existsSync,
readdirSync: (p) => nodeFs.readdirSync(p) as string[],
readFileSync: (p, enc) => nodeFs.readFileSync(p, enc),
statSync: (p) => nodeFs.statSync(p),
rmSync: (nodeFs as any).rmSync
? (p, opts) => (nodeFs as any).rmSync(p, opts)
: undefined,
rmdirSync: (p, opts) => nodeFs.rmdirSync(p, opts as any),
};
/** Resolve the grok home directory honoring HOME/USERPROFILE (matching cli-locator semantics). */
export function resolveGrokHome(env: NodeJS.ProcessEnv = process.env): string {
const home = env.HOME || env.USERPROFILE || "";
return path.join(home, ".grok");
}