@sion10032/pi-sessions 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +250 -2
- package/THIRD_PARTY_NOTICES.md +51 -0
- package/package.json +21 -5
- package/src/archive.ts +356 -0
- package/src/commands.ts +401 -0
- package/src/delete.ts +166 -0
- package/src/index.ts +96 -0
- package/src/paths.ts +235 -0
- package/src/picker-state.ts +377 -0
- package/src/picker.ts +526 -0
- package/src/rename.ts +23 -0
- package/src/search.ts +258 -0
- package/src/sessions.ts +492 -0
- package/src/state.ts +115 -0
- package/src/types.ts +47 -0
package/src/paths.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path, name and session-header helpers shared by every part of the extension.
|
|
3
|
+
*
|
|
4
|
+
* Archive status is derived from directory location alone: a session file is
|
|
5
|
+
* archived iff it sits under `<agent>/sessions-archive/`. Nothing here rewrites
|
|
6
|
+
* a session header or name.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
closeSync,
|
|
10
|
+
openSync,
|
|
11
|
+
readSync,
|
|
12
|
+
statSync,
|
|
13
|
+
} from "node:fs";
|
|
14
|
+
import {
|
|
15
|
+
basename,
|
|
16
|
+
isAbsolute,
|
|
17
|
+
join,
|
|
18
|
+
relative,
|
|
19
|
+
resolve,
|
|
20
|
+
sep,
|
|
21
|
+
} from "node:path";
|
|
22
|
+
|
|
23
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
24
|
+
|
|
25
|
+
/** Legacy marker some session names carry; stripped for display and on restore. */
|
|
26
|
+
export const ARCHIVE_TAG = "[ARCHIVE] ";
|
|
27
|
+
|
|
28
|
+
/** One or more leading archive markers. The old plugin wrote a bare `[ARCHIVE]` for unnamed sessions. */
|
|
29
|
+
const LEADING_ARCHIVE_TAG = /^(?:\[ARCHIVE\]\s*)+/;
|
|
30
|
+
|
|
31
|
+
/** C0 controls, DEL and C1 controls would break the one-line picker layout. */
|
|
32
|
+
const CONTROL_CHARS = /[\u0000-\u001f\u007f-\u009f]/g;
|
|
33
|
+
|
|
34
|
+
/** First line of a session JSONL file holds its header; 4KB is plenty. */
|
|
35
|
+
const HEADER_READ_BYTES = 4096;
|
|
36
|
+
|
|
37
|
+
/** Where pi keeps live sessions for every cwd. */
|
|
38
|
+
export function sessionsRoot(): string {
|
|
39
|
+
return join(getAgentDir(), "sessions");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Where this extension moves archived sessions. */
|
|
43
|
+
export function archiveRoot(): string {
|
|
44
|
+
return join(getAgentDir(), "sessions-archive");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Same encoding pi uses for per-cwd session subdirectories. */
|
|
48
|
+
export function cwdDirName(cwd: string): string {
|
|
49
|
+
return `--${cwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function sessionIdFromFile(file: string): string {
|
|
53
|
+
return basename(file, ".jsonl").split("_").pop() ?? "";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Path of `file` relative to `root`, with separators normalized to `/`.
|
|
58
|
+
* Returns undefined when `file` is not strictly inside `root`.
|
|
59
|
+
*/
|
|
60
|
+
export function underRoot(root: string, file: string): string | undefined {
|
|
61
|
+
const rel = relative(resolve(root), resolve(file));
|
|
62
|
+
if (!rel || rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) return undefined;
|
|
63
|
+
return rel.split(sep).join("/");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function isArchived(file: string): boolean {
|
|
67
|
+
return underRoot(archiveRoot(), file) !== undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The file itself plus its counterpart in the other tree: a live session maps
|
|
72
|
+
* to where it would be archived and vice versa. Files outside both trees have
|
|
73
|
+
* no twin.
|
|
74
|
+
*/
|
|
75
|
+
export function twinPaths(file: string): string[] {
|
|
76
|
+
const abs = resolve(file);
|
|
77
|
+
const live = underRoot(sessionsRoot(), abs);
|
|
78
|
+
if (live !== undefined) return [abs, join(archiveRoot(), live)];
|
|
79
|
+
const archived = underRoot(archiveRoot(), abs);
|
|
80
|
+
if (archived !== undefined) return [abs, join(sessionsRoot(), archived)];
|
|
81
|
+
return [abs];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Remove leading legacy archive markers and surrounding whitespace. */
|
|
85
|
+
export function stripArchiveTag(name: string | undefined): string {
|
|
86
|
+
return (name ?? "").replace(LEADING_ARCHIVE_TAG, "").trim();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** True when a name still starts with the legacy archive marker. */
|
|
90
|
+
export function hasArchiveTag(name: string | undefined): boolean {
|
|
91
|
+
return LEADING_ARCHIVE_TAG.test(name ?? "");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Strip legacy archive tags; an empty result becomes `(unnamed)`. */
|
|
95
|
+
export function cleanName(name: string | undefined): string {
|
|
96
|
+
const cleaned = stripArchiveTag(name);
|
|
97
|
+
return cleaned || "(unnamed)";
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Collapse a title to a single line: control characters become spaces and runs
|
|
102
|
+
* of whitespace collapse, so a message containing a newline or an escape
|
|
103
|
+
* sequence cannot break the picker layout.
|
|
104
|
+
*/
|
|
105
|
+
function singleLine(text: string): string {
|
|
106
|
+
return text.replace(CONTROL_CHARS, " ").replace(/\s+/g, " ").trim();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** User-facing session title: the cleaned name, else the first message. */
|
|
110
|
+
export function displayTitle(info: { name?: string; firstMessage: string }): string {
|
|
111
|
+
const named = singleLine(cleanName(info.name));
|
|
112
|
+
if (named !== "" && named !== "(unnamed)") return named;
|
|
113
|
+
return singleLine(info.firstMessage) || "(unnamed)";
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Compact age label with a fixed two-digit number column: `now`, ` 5m`, `23h`,
|
|
118
|
+
* ` 3d`, ` 4w`, ` 1y`. No "ago" suffix.
|
|
119
|
+
*/
|
|
120
|
+
export function relativeTime(date: Date, now: number = Date.now()): string {
|
|
121
|
+
const ms = Math.max(0, now - date.getTime());
|
|
122
|
+
const minutes = Math.floor(ms / 60_000);
|
|
123
|
+
if (minutes < 1) return "now";
|
|
124
|
+
if (minutes < 60) return `${twoDigits(minutes)}m`;
|
|
125
|
+
const hours = Math.floor(ms / 3_600_000);
|
|
126
|
+
if (hours < 72) return `${twoDigits(hours)}h`;
|
|
127
|
+
const days = Math.floor(ms / 86_400_000);
|
|
128
|
+
if (days < 21) return `${twoDigits(days)}d`;
|
|
129
|
+
const weeks = Math.floor(days / 7);
|
|
130
|
+
if (weeks < 52) return `${twoDigits(weeks)}w`;
|
|
131
|
+
return `${twoDigits(Math.floor(weeks / 52))}y`;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Right-align the number in a two-character column. */
|
|
135
|
+
function twoDigits(value: number): string {
|
|
136
|
+
return String(value).padStart(2, " ");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface HeaderLite {
|
|
140
|
+
id?: string;
|
|
141
|
+
cwd?: string;
|
|
142
|
+
parentSession?: string;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Read the session header (the first line of the JSONL file). Only the fields
|
|
147
|
+
* callers need are returned; a missing or malformed file yields undefined.
|
|
148
|
+
*/
|
|
149
|
+
export function readHeader(file: string): HeaderLite | undefined {
|
|
150
|
+
let fd: number | undefined;
|
|
151
|
+
try {
|
|
152
|
+
fd = openSync(file, "r");
|
|
153
|
+
const buffer = Buffer.alloc(HEADER_READ_BYTES);
|
|
154
|
+
const read = readSync(fd, buffer, 0, HEADER_READ_BYTES, 0);
|
|
155
|
+
const firstLine = buffer.subarray(0, read).toString("utf8").split("\n")[0];
|
|
156
|
+
if (!firstLine) return undefined;
|
|
157
|
+
const parsed = JSON.parse(firstLine) as Record<string, unknown>;
|
|
158
|
+
if (typeof parsed !== "object" || parsed === null) return undefined;
|
|
159
|
+
const header: HeaderLite = {};
|
|
160
|
+
if (typeof parsed.id === "string") header.id = parsed.id;
|
|
161
|
+
if (typeof parsed.cwd === "string") header.cwd = parsed.cwd;
|
|
162
|
+
if (typeof parsed.parentSession === "string") header.parentSession = parsed.parentSession;
|
|
163
|
+
return header;
|
|
164
|
+
} catch {
|
|
165
|
+
return undefined;
|
|
166
|
+
} finally {
|
|
167
|
+
if (fd !== undefined) closeSync(fd);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Buffers tried when reading the first entry; a huge first entry falls back to a larger one. */
|
|
172
|
+
const FIRST_ENTRY_READ_SIZES = [16 * 1024, 256 * 1024, 4 * 1024 * 1024] as const;
|
|
173
|
+
|
|
174
|
+
interface FirstEntryCache {
|
|
175
|
+
mtimeMs: number;
|
|
176
|
+
id: string | undefined;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const firstEntryCache = new Map<string, FirstEntryCache>();
|
|
180
|
+
|
|
181
|
+
function readBytes(file: string, length: number): string {
|
|
182
|
+
const fd = openSync(file, "r");
|
|
183
|
+
try {
|
|
184
|
+
const buffer = Buffer.alloc(length);
|
|
185
|
+
const read = readSync(fd, buffer, 0, length, 0);
|
|
186
|
+
return buffer.subarray(0, read).toString("utf8");
|
|
187
|
+
} finally {
|
|
188
|
+
closeSync(fd);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The `id` of a session's first non-header entry, or undefined when the file
|
|
194
|
+
* has none. Forks and clones copy that entry (and its id) from their parent;
|
|
195
|
+
* subagents start with a fresh one, which is how the picker tells them apart.
|
|
196
|
+
*/
|
|
197
|
+
export function firstEntryId(file: string): string | undefined {
|
|
198
|
+
let mtimeMs: number;
|
|
199
|
+
try {
|
|
200
|
+
mtimeMs = statSync(file).mtimeMs;
|
|
201
|
+
} catch {
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
const key = resolve(file);
|
|
205
|
+
const cached = firstEntryCache.get(key);
|
|
206
|
+
if (cached?.mtimeMs === mtimeMs) return cached.id;
|
|
207
|
+
|
|
208
|
+
let id: string | undefined;
|
|
209
|
+
for (const length of FIRST_ENTRY_READ_SIZES) {
|
|
210
|
+
let text: string;
|
|
211
|
+
try {
|
|
212
|
+
text = readBytes(file, length);
|
|
213
|
+
} catch {
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
let readMore = false;
|
|
217
|
+
const lines = text.split("\n");
|
|
218
|
+
for (let index = 1; index < lines.length; index++) {
|
|
219
|
+
const line = lines[index]!.trim();
|
|
220
|
+
if (!line) continue;
|
|
221
|
+
try {
|
|
222
|
+
const entry = JSON.parse(line) as { type?: unknown; id?: unknown };
|
|
223
|
+
if (entry.type !== "session" && typeof entry.id === "string") id = entry.id;
|
|
224
|
+
} catch {
|
|
225
|
+
// A partial last line is retried with a larger buffer; anything else
|
|
226
|
+
// means the file is malformed, so leave the id undefined.
|
|
227
|
+
readMore = text.length === length && index === lines.length - 1;
|
|
228
|
+
}
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
if (id !== undefined || !readMore) break;
|
|
232
|
+
}
|
|
233
|
+
firstEntryCache.set(key, { mtimeMs, id });
|
|
234
|
+
return id;
|
|
235
|
+
}
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The UI-agnostic state machine behind the `/sessions` picker.
|
|
3
|
+
*
|
|
4
|
+
* It owns what the picker shows - filter, sort, scope, expanded subtrees, the
|
|
5
|
+
* query and the selected row - and answers the one question the picker asks
|
|
6
|
+
* before archiving: whether the selected session may be moved right now.
|
|
7
|
+
*
|
|
8
|
+
* Selection is tracked by path rather than by index: every keystroke re-filters
|
|
9
|
+
* and re-sorts the rows, and the session the user picked must stay picked for
|
|
10
|
+
* as long as it is still visible. Nothing here touches the filesystem;
|
|
11
|
+
* archiving itself belongs to `archive.ts`.
|
|
12
|
+
*/
|
|
13
|
+
import { resolve } from "node:path";
|
|
14
|
+
|
|
15
|
+
import { twinPaths } from "./paths.ts";
|
|
16
|
+
import { parseQuery } from "./search.ts";
|
|
17
|
+
import {
|
|
18
|
+
buildRows,
|
|
19
|
+
descendantCounts,
|
|
20
|
+
descendantRecords,
|
|
21
|
+
filterRecords,
|
|
22
|
+
parentPaths,
|
|
23
|
+
type TreeRow,
|
|
24
|
+
} from "./sessions.ts";
|
|
25
|
+
import type {
|
|
26
|
+
ArchiveFilter,
|
|
27
|
+
ExpansionMode,
|
|
28
|
+
Scope,
|
|
29
|
+
SessionRecord,
|
|
30
|
+
SortMode,
|
|
31
|
+
} from "./types.ts";
|
|
32
|
+
|
|
33
|
+
/** What `ctrl+a` would do to the selected session. */
|
|
34
|
+
export type ArchiveAction = "archive" | "deferred-archive" | "unarchive" | "none";
|
|
35
|
+
|
|
36
|
+
/** What `ctrl+d` would do to the selected session. */
|
|
37
|
+
export type DeleteAction = "delete" | "none" | "protected";
|
|
38
|
+
|
|
39
|
+
/** `cycleSort` order, matching the native session picker. */
|
|
40
|
+
const SORT_ORDER: readonly SortMode[] = ["threaded", "recent", "relevance"];
|
|
41
|
+
|
|
42
|
+
/** `cycleArchiveFilter` order: live sessions, archived ones, both. */
|
|
43
|
+
const ARCHIVE_FILTER_ORDER: readonly ArchiveFilter[] = ["active", "archived", "all"];
|
|
44
|
+
|
|
45
|
+
/** The value after `current`, wrapping at the end. */
|
|
46
|
+
function nextIn<T>(values: readonly T[], current: T): T {
|
|
47
|
+
return values[(values.indexOf(current) + 1) % values.length];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export class PickerState {
|
|
51
|
+
/** Every loaded record; the archive filter narrows the view, not this list. */
|
|
52
|
+
private records: SessionRecord[];
|
|
53
|
+
/** Resolved path of the session the session manager is writing. */
|
|
54
|
+
private readonly currentPath: string | undefined;
|
|
55
|
+
/** Working directory the picker was opened for; reloads are the owner's job. */
|
|
56
|
+
private readonly currentCwd: string;
|
|
57
|
+
/** Resolved path of the selected row; undefined when nothing is selected. */
|
|
58
|
+
private selectedKey: string | undefined;
|
|
59
|
+
/** Last valid row index, used to clamp when the selected path disappears. */
|
|
60
|
+
private selectedIndex = 0;
|
|
61
|
+
private queryText = "";
|
|
62
|
+
private scopeValue: Scope = "current";
|
|
63
|
+
private sortMode: SortMode = "threaded";
|
|
64
|
+
private archiveFilterValue: ArchiveFilter = "active";
|
|
65
|
+
private namedOnlyValue = false;
|
|
66
|
+
private showPathValue = false;
|
|
67
|
+
/** Global expansion preset; arrows step it, `ctrl+e` overrides one row. */
|
|
68
|
+
private expansionModeValue: ExpansionMode = "collapsed";
|
|
69
|
+
/** Per-row overrides of the preset, keyed by session path. */
|
|
70
|
+
private readonly expandOverrides = new Map<string, boolean>();
|
|
71
|
+
/** True while the owner is streaming a freshly loaded scope. */
|
|
72
|
+
private loadingValue = false;
|
|
73
|
+
private loadingPhaseValue: "live" | "archive" = "live";
|
|
74
|
+
private loadingLoadedValue = 0;
|
|
75
|
+
private loadingTotalValue = 0;
|
|
76
|
+
|
|
77
|
+
constructor(records: SessionRecord[], currentFile: string | undefined, currentCwd: string) {
|
|
78
|
+
this.records = records;
|
|
79
|
+
this.currentPath = currentFile === undefined ? undefined : resolve(currentFile);
|
|
80
|
+
this.currentCwd = currentCwd;
|
|
81
|
+
this.selectedKey = this.currentPath;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Replace the loaded records, e.g. after archiving refreshed the trees. */
|
|
85
|
+
setRecords(records: SessionRecord[]): void {
|
|
86
|
+
this.records = records;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
setQuery(query: string): void {
|
|
90
|
+
this.queryText = query;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
cycleScope(): void {
|
|
94
|
+
this.scopeValue = this.scopeValue === "current" ? "all" : "current";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
cycleSort(): void {
|
|
98
|
+
this.sortMode = nextIn(SORT_ORDER, this.sortMode);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
cycleArchiveFilter(): void {
|
|
102
|
+
this.archiveFilterValue = nextIn(ARCHIVE_FILTER_ORDER, this.archiveFilterValue);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Start on a given archive filter, e.g. from `/sessions archived`. */
|
|
106
|
+
setArchiveFilter(filter: ArchiveFilter): void {
|
|
107
|
+
this.archiveFilterValue = filter;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
toggleNamed(): void {
|
|
111
|
+
this.namedOnlyValue = !this.namedOnlyValue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
togglePath(): void {
|
|
115
|
+
this.showPathValue = !this.showPathValue;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Cycle the global expansion preset: collapsed -> children -> all ->
|
|
120
|
+
* collapsed. This is the single-key `ctrl+e` behavior; per-row overrides are
|
|
121
|
+
* cleared so the preset applies uniformly.
|
|
122
|
+
*/
|
|
123
|
+
cycleExpansion(): void {
|
|
124
|
+
const modes: readonly ExpansionMode[] = ["collapsed", "children", "all"];
|
|
125
|
+
this.expansionModeValue = modes[(modes.indexOf(this.expansionModeValue) + 1) % modes.length]!;
|
|
126
|
+
this.expandOverrides.clear();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Directional preset step, retained for a future two-key preset control.
|
|
131
|
+
* The picker currently binds only `cycleExpansion` to `ctrl+e`.
|
|
132
|
+
*/
|
|
133
|
+
stepExpansion(delta: number): void {
|
|
134
|
+
const modes: readonly ExpansionMode[] = ["collapsed", "children", "all"];
|
|
135
|
+
const current = modes.indexOf(this.expansionModeValue);
|
|
136
|
+
const next = modes[Math.max(0, Math.min(modes.length - 1, current + delta))];
|
|
137
|
+
if (next === this.expansionModeValue) return;
|
|
138
|
+
this.expansionModeValue = next;
|
|
139
|
+
this.expandOverrides.clear();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Set the selected row's expansion directly. The picker has no key bound to
|
|
144
|
+
* this while dedicated row keys are undecided; the method and overrides stay
|
|
145
|
+
* so re-enabling is a small change.
|
|
146
|
+
*/
|
|
147
|
+
setSelectedExpanded(expanded: boolean): void {
|
|
148
|
+
const row = this.selectedRow();
|
|
149
|
+
if (row === undefined || row.visibleDescendantCount === 0) return;
|
|
150
|
+
const path = row.record.info.path;
|
|
151
|
+
if (this.isExpanded(path) === expanded) return;
|
|
152
|
+
this.expandOverrides.set(path, expanded);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Toggle the selected row; retained for a future row key binding. */
|
|
156
|
+
toggleExpand(): void {
|
|
157
|
+
const row = this.selectedRow();
|
|
158
|
+
if (row === undefined || row.visibleDescendantCount === 0) return;
|
|
159
|
+
this.setSelectedExpanded(!this.isExpanded(row.record.info.path));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Clear the list and show the loading line while the owner reloads. */
|
|
163
|
+
beginLoading(): void {
|
|
164
|
+
this.records = [];
|
|
165
|
+
this.loadingValue = true;
|
|
166
|
+
this.loadingPhaseValue = "live";
|
|
167
|
+
this.loadingLoadedValue = 0;
|
|
168
|
+
this.loadingTotalValue = 0;
|
|
169
|
+
this.selectedIndex = 0;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
setLoadingProgress(loaded: number, total: number, phase: "live" | "archive"): void {
|
|
173
|
+
this.loadingPhaseValue = phase;
|
|
174
|
+
this.loadingLoadedValue = loaded;
|
|
175
|
+
this.loadingTotalValue = total;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
finishLoading(records: SessionRecord[]): void {
|
|
179
|
+
this.records = records;
|
|
180
|
+
this.loadingValue = false;
|
|
181
|
+
this.loadingPhaseValue = "live";
|
|
182
|
+
this.loadingLoadedValue = 0;
|
|
183
|
+
this.loadingTotalValue = 0;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
failLoading(): void {
|
|
187
|
+
this.loadingValue = false;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Records the current view can show: subagents only in the `all` preset. */
|
|
191
|
+
private visibleRecords(): SessionRecord[] {
|
|
192
|
+
if (this.expansionModeValue === "all") return this.records;
|
|
193
|
+
return this.records.filter((record) => record.relation !== "subagent");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** The preset's base expansion plus the user's per-row overrides. */
|
|
197
|
+
private effectiveExpanded(visible: SessionRecord[]): Set<string> {
|
|
198
|
+
const expanded = new Set<string>();
|
|
199
|
+
if (this.expansionModeValue === "all") {
|
|
200
|
+
for (const path of parentPaths(this.records)) expanded.add(path);
|
|
201
|
+
} else if (this.expansionModeValue === "children") {
|
|
202
|
+
for (const path of parentPaths(visible)) expanded.add(path);
|
|
203
|
+
}
|
|
204
|
+
for (const [path, value] of this.expandOverrides) {
|
|
205
|
+
if (value) expanded.add(path);
|
|
206
|
+
else expanded.delete(path);
|
|
207
|
+
}
|
|
208
|
+
return expanded;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Move the selection by `delta` rows, stopping at the list boundaries. */
|
|
212
|
+
move(delta: number): void {
|
|
213
|
+
const rows = this.rows();
|
|
214
|
+
if (rows.length === 0) return;
|
|
215
|
+
const current = rows.findIndex((row) => resolve(row.record.info.path) === this.selectedKey);
|
|
216
|
+
const next = Math.max(0, Math.min(rows.length - 1, current + delta));
|
|
217
|
+
this.select(rows, next);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The rows to render, with the selection re-anchored to the current filter.
|
|
222
|
+
* While a reload is streaming records, a selected path that has not arrived
|
|
223
|
+
* yet is kept instead of being clobbered by the first partial batch.
|
|
224
|
+
*/
|
|
225
|
+
rows(): TreeRow[] {
|
|
226
|
+
const query = parseQuery(this.queryText);
|
|
227
|
+
const visible = this.visibleRecords();
|
|
228
|
+
const rows = buildRows(
|
|
229
|
+
filterRecords(visible, {
|
|
230
|
+
archiveFilter: this.archiveFilterValue,
|
|
231
|
+
namedOnly: this.namedOnlyValue,
|
|
232
|
+
query,
|
|
233
|
+
}),
|
|
234
|
+
{
|
|
235
|
+
query,
|
|
236
|
+
sort: this.sortMode,
|
|
237
|
+
expand: this.effectiveExpanded(visible),
|
|
238
|
+
descendantCounts: descendantCounts(this.records),
|
|
239
|
+
},
|
|
240
|
+
);
|
|
241
|
+
if (rows.length === 0) {
|
|
242
|
+
if (!this.loadingValue) {
|
|
243
|
+
this.selectedKey = undefined;
|
|
244
|
+
this.selectedIndex = 0;
|
|
245
|
+
}
|
|
246
|
+
return rows;
|
|
247
|
+
}
|
|
248
|
+
const found = rows.findIndex((row) => resolve(row.record.info.path) === this.selectedKey);
|
|
249
|
+
if (found >= 0) this.select(rows, found);
|
|
250
|
+
else if (!this.loadingValue) this.select(rows, Math.min(this.selectedIndex, rows.length - 1));
|
|
251
|
+
return rows;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
selectedRow(): TreeRow | undefined {
|
|
255
|
+
const rows = this.rows();
|
|
256
|
+
return rows.length === 0 ? undefined : rows[this.selectedIndex];
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** True for the session file the session manager is still writing. */
|
|
260
|
+
isCurrent(record: SessionRecord): boolean {
|
|
261
|
+
return this.currentPath !== undefined && resolve(record.info.path) === this.currentPath;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* True when `path` names the running session, in either tree: the current
|
|
266
|
+
* session's archived twin cannot be deleted either, since the delete also
|
|
267
|
+
* takes the live twin with it.
|
|
268
|
+
*/
|
|
269
|
+
private isCurrentPath(path: string): boolean {
|
|
270
|
+
if (this.currentPath === undefined) return false;
|
|
271
|
+
return twinPaths(path).some((twin) => resolve(twin) === this.currentPath);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** True when the running session hangs somewhere below `record`. */
|
|
275
|
+
subtreeContainsCurrent(record: SessionRecord): boolean {
|
|
276
|
+
return descendantRecords(this.records, record).some((child) => this.isCurrentPath(child.info.path));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* What deleting the selected session would do. The running session is never
|
|
281
|
+
* deleted - pi is still writing it - and a subtree that contains it is
|
|
282
|
+
* refused whole, so a delete can never leave the live session without its
|
|
283
|
+
* ancestors halfway through.
|
|
284
|
+
*/
|
|
285
|
+
deleteAction(): DeleteAction {
|
|
286
|
+
const row = this.selectedRow();
|
|
287
|
+
if (row === undefined) return "none";
|
|
288
|
+
if (this.isCurrentPath(row.record.info.path)) return "none";
|
|
289
|
+
if (this.subtreeContainsCurrent(row.record)) return "protected";
|
|
290
|
+
return "delete";
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Sessions below `record` across every loaded record - not just the rows on
|
|
295
|
+
* screen. A query, the named filter, the recent sort and a hidden subagent
|
|
296
|
+
* subtree can all leave rows off screen, so the row's own count cannot
|
|
297
|
+
* answer how much an archive would move.
|
|
298
|
+
*/
|
|
299
|
+
descendantCount(record: SessionRecord): number {
|
|
300
|
+
return descendantCounts(this.records).get(record) ?? 0;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* What archiving the selected session would do. The current session wins
|
|
305
|
+
* over the archived flag: moving a file pi is writing is unsafe, so it can
|
|
306
|
+
* only be deferred to shutdown, or not touched at all once archived.
|
|
307
|
+
*/
|
|
308
|
+
archiveAction(): ArchiveAction {
|
|
309
|
+
const row = this.selectedRow();
|
|
310
|
+
if (row === undefined) return "none";
|
|
311
|
+
if (this.isCurrent(row.record)) return row.record.archived ? "none" : "deferred-archive";
|
|
312
|
+
return row.record.archived ? "unarchive" : "archive";
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** True when the row at `path` has its children rendered. */
|
|
316
|
+
isExpanded(path: string): boolean {
|
|
317
|
+
return this.effectiveExpanded(this.visibleRecords()).has(path);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
get expansionMode(): ExpansionMode {
|
|
321
|
+
return this.expansionModeValue;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
get loading(): boolean {
|
|
325
|
+
return this.loadingValue;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
get loadingPhase(): "live" | "archive" {
|
|
329
|
+
return this.loadingPhaseValue;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
get loadingLoaded(): number {
|
|
333
|
+
return this.loadingLoadedValue;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
get loadingTotal(): number {
|
|
337
|
+
return this.loadingTotalValue;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
get scope(): Scope {
|
|
341
|
+
return this.scopeValue;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
get sort(): SortMode {
|
|
345
|
+
return this.sortMode;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
get archiveFilter(): ArchiveFilter {
|
|
349
|
+
return this.archiveFilterValue;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
get namedOnly(): boolean {
|
|
353
|
+
return this.namedOnlyValue;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
get showPath(): boolean {
|
|
357
|
+
return this.showPathValue;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
get query(): string {
|
|
361
|
+
return this.queryText;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Live and archived totals over everything loaded, not just visible rows. */
|
|
365
|
+
get counts(): { active: number; archived: number } {
|
|
366
|
+
let archived = 0;
|
|
367
|
+
for (const record of this.records) if (record.archived) archived++;
|
|
368
|
+
return { active: this.records.length - archived, archived };
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** Make row `index` of `rows` the selection, remembering path and index. */
|
|
372
|
+
private select(rows: TreeRow[], index: number): void {
|
|
373
|
+
const row = rows[index];
|
|
374
|
+
this.selectedKey = resolve(row.record.info.path);
|
|
375
|
+
this.selectedIndex = index;
|
|
376
|
+
}
|
|
377
|
+
}
|