@lune_99/pi-lune-extensions-core 0.2.0 → 0.2.1
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 +77 -40
- package/README.md +23 -8
- package/dist/core/panel.d.ts +6 -1
- package/dist/core/panel.js +26 -1
- package/dist/core/settings.d.ts +9 -1
- package/dist/core/settings.js +36 -2
- package/dist/extension.js +5 -1
- package/dist/mention/autocomplete.d.ts +4 -2
- package/dist/mention/autocomplete.js +7 -2
- package/dist/mention/budget.d.ts +8 -0
- package/dist/mention/budget.js +10 -0
- package/dist/mention/catalog.d.ts +23 -6
- package/dist/mention/catalog.js +81 -40
- package/dist/mention/external.d.ts +4 -4
- package/dist/mention/external.js +33 -39
- package/dist/mention/index.d.ts +11 -2
- package/dist/mention/index.js +3 -2
- package/dist/mention/pi.d.ts +11 -0
- package/dist/mention/pi.js +172 -0
- package/dist/mention/session-index.d.ts +66 -0
- package/dist/mention/session-index.js +303 -0
- package/dist/panel/panel.js +9 -5
- package/package.json +4 -4
- package/src/core/panel.ts +29 -1
- package/src/core/settings.ts +42 -2
- package/src/extension.ts +6 -1
- package/src/mention/autocomplete.ts +6 -2
- package/src/mention/budget.ts +11 -0
- package/src/mention/catalog.ts +90 -41
- package/src/mention/external.ts +51 -52
- package/src/mention/index.ts +22 -2
- package/src/mention/pi.ts +192 -0
- package/src/mention/session-index.ts +356 -0
- package/src/panel/panel.ts +10 -6
package/dist/mention/external.js
CHANGED
|
@@ -4,54 +4,33 @@ import { basename, join, resolve } from "node:path";
|
|
|
4
4
|
import { parseClaudeSession } from "./claude.js";
|
|
5
5
|
import { parseCodexSession } from "./codex.js";
|
|
6
6
|
import { jsonl, string } from "./jsonl.js";
|
|
7
|
-
import { SOURCE_NAMES
|
|
8
|
-
/**
|
|
7
|
+
import { SOURCE_NAMES } from "./session.js";
|
|
8
|
+
/** Codex and Claude Code session files; the Codex title index is cached per Pi session. */
|
|
9
9
|
export class ExternalSessions {
|
|
10
10
|
codexHome = resolve(process.env["CODEX_HOME"] ?? join(homedir(), ".codex"));
|
|
11
11
|
claudeHome = resolve(process.env["CLAUDE_CONFIG_DIR"] ?? join(homedir(), ".claude"));
|
|
12
|
-
cached = new Map();
|
|
13
12
|
titleIndex;
|
|
14
13
|
async list(signal, report) {
|
|
15
14
|
const errors = [];
|
|
16
|
-
const seen = new Set();
|
|
17
15
|
const sources = ["codex", "claude"];
|
|
18
16
|
const results = await Promise.all(sources.map(async (source) => {
|
|
19
17
|
try {
|
|
20
18
|
const paths = await this.paths(source);
|
|
21
|
-
const names = source === "codex" ? await this.codexNames(signal) :
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
for (const path of paths) {
|
|
25
|
-
signal.throwIfAborted();
|
|
26
|
-
seen.add(path);
|
|
19
|
+
const names = source === "codex" ? await this.codexNames(signal) : undefined;
|
|
20
|
+
const currentVersion = source === "codex" ? this.titleIndex?.version : undefined;
|
|
21
|
+
const files = await Promise.all(paths.map(async (path) => {
|
|
27
22
|
try {
|
|
28
|
-
|
|
29
|
-
const version = `${stats.ino}:${stats.size}:${stats.mtimeMs}`;
|
|
30
|
-
let cached = this.cached.get(path);
|
|
31
|
-
if (cached?.version !== version) {
|
|
32
|
-
cached = { version };
|
|
33
|
-
const content = await readFile(path, { encoding: "utf8", signal });
|
|
34
|
-
cached.session = parseExternalSession(source, content, path)?.session;
|
|
35
|
-
if (!signal.aborted)
|
|
36
|
-
this.cached.set(path, cached);
|
|
37
|
-
}
|
|
38
|
-
if (cached.session) {
|
|
39
|
-
const name = source === "codex"
|
|
40
|
-
? names.get(cached.session.id)
|
|
41
|
-
: cached.session.name;
|
|
42
|
-
if (name !== cached.session.name) {
|
|
43
|
-
cached.session = { ...cached.session, name };
|
|
44
|
-
}
|
|
45
|
-
// Stable objects preserve autocomplete's existing WeakMap search cache.
|
|
46
|
-
sessions.push(cached.session);
|
|
47
|
-
}
|
|
23
|
+
return externalFile(source, path, await stat(path), names, currentVersion);
|
|
48
24
|
}
|
|
49
25
|
catch (error) {
|
|
50
|
-
|
|
51
|
-
|
|
26
|
+
// Removed since the directory was listed.
|
|
27
|
+
if (error.code !== "ENOENT") {
|
|
28
|
+
errors.push(fileError(source, path, error));
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
52
31
|
}
|
|
53
|
-
}
|
|
54
|
-
return
|
|
32
|
+
}));
|
|
33
|
+
return files.filter((file) => file !== undefined);
|
|
55
34
|
}
|
|
56
35
|
catch (error) {
|
|
57
36
|
signal.throwIfAborted();
|
|
@@ -60,17 +39,12 @@ export class ExternalSessions {
|
|
|
60
39
|
}
|
|
61
40
|
}));
|
|
62
41
|
signal.throwIfAborted();
|
|
63
|
-
for (const path of this.cached.keys()) {
|
|
64
|
-
if (!seen.has(path))
|
|
65
|
-
this.cached.delete(path);
|
|
66
|
-
}
|
|
67
42
|
if (errors.length > 0) {
|
|
68
43
|
report(new AggregateError(errors, errors.map((error) => error.message).join("\n")));
|
|
69
44
|
}
|
|
70
45
|
return results.flat();
|
|
71
46
|
}
|
|
72
47
|
clear() {
|
|
73
|
-
this.cached.clear();
|
|
74
48
|
this.titleIndex = undefined;
|
|
75
49
|
}
|
|
76
50
|
async paths(source) {
|
|
@@ -134,6 +108,26 @@ export class ExternalSessions {
|
|
|
134
108
|
return names;
|
|
135
109
|
}
|
|
136
110
|
}
|
|
111
|
+
function externalFile(source, path, stats, codexNames, currentVersion) {
|
|
112
|
+
return {
|
|
113
|
+
path,
|
|
114
|
+
stats,
|
|
115
|
+
label: SOURCE_NAMES[source],
|
|
116
|
+
currentVersion,
|
|
117
|
+
async read(signal) {
|
|
118
|
+
const content = await readFile(path, { encoding: "utf8", signal });
|
|
119
|
+
return parseExternalSession(source, content, path)?.session;
|
|
120
|
+
},
|
|
121
|
+
// Codex keeps titles in its own index, so a rename does not change the rollout.
|
|
122
|
+
current: codexNames
|
|
123
|
+
? (session) => {
|
|
124
|
+
const name = codexNames.get(session.id);
|
|
125
|
+
// The same object keeps autocomplete's search cache for the session.
|
|
126
|
+
return name === session.name ? session : { ...session, name };
|
|
127
|
+
}
|
|
128
|
+
: undefined,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
137
131
|
export function parseExternalSession(source, content, path) {
|
|
138
132
|
try {
|
|
139
133
|
return source === "codex"
|
package/dist/mention/index.d.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
1
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import type { ResourceScope } from "../resources/resources.ts";
|
|
3
|
+
export interface MentionOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Bytes the session index may keep, asked again on every load of the session list.
|
|
6
|
+
* Without it, or when it rejects, the index keeps INDEX_BUDGET_BYTES.
|
|
7
|
+
*/
|
|
8
|
+
indexBudget?(ctx: ExtensionContext): Promise<number>;
|
|
9
|
+
/** Characters kept per text field, asked again on every session-list load. */
|
|
10
|
+
indexTextLimit?(ctx: ExtensionContext): Promise<number>;
|
|
11
|
+
}
|
|
3
12
|
/**
|
|
4
13
|
* `@` lists local sessions with pi's file matches, and `@@` lists sessions only. A picked
|
|
5
14
|
* session becomes a source-specific token. On submit, each such token becomes an `@` reference to
|
|
6
15
|
* a filtered copy of that session, which the model reads when it needs the background.
|
|
7
16
|
*/
|
|
8
|
-
export declare function installMentions(pi: ExtensionAPI, resources: ResourceScope): void;
|
|
17
|
+
export declare function installMentions(pi: ExtensionAPI, resources: ResourceScope, options?: MentionOptions): void;
|
package/dist/mention/index.js
CHANGED
|
@@ -12,7 +12,7 @@ import { buildTranscript } from "./transcript.js";
|
|
|
12
12
|
* session becomes a source-specific token. On submit, each such token becomes an `@` reference to
|
|
13
13
|
* a filtered copy of that session, which the model reads when it needs the background.
|
|
14
14
|
*/
|
|
15
|
-
export function installMentions(pi, resources) {
|
|
15
|
+
export function installMentions(pi, resources, options = {}) {
|
|
16
16
|
let catalog;
|
|
17
17
|
const transcripts = new TranscriptCache();
|
|
18
18
|
const retire = () => {
|
|
@@ -28,7 +28,8 @@ export function installMentions(pi, resources) {
|
|
|
28
28
|
return;
|
|
29
29
|
ctx.ui.notify(`Session list failed: ${errorMessage(error)}`, "error");
|
|
30
30
|
};
|
|
31
|
-
const
|
|
31
|
+
const { indexBudget, indexTextLimit } = options;
|
|
32
|
+
const sessions = new SessionCatalog(ctx.sessionManager.getSessionDir(), report, indexBudget && (() => indexBudget(ctx)), indexTextLimit && (() => indexTextLimit(ctx)));
|
|
32
33
|
catalog = sessions;
|
|
33
34
|
if (ctx.mode !== "tui")
|
|
34
35
|
return;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { SessionFile } from "./session-index.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Pi session files whose sessions have the fields `SessionManager.listAll` builds.
|
|
4
|
+
* listAll keeps no cache and reads every file again on each call; these are read through
|
|
5
|
+
* the index.
|
|
6
|
+
*/
|
|
7
|
+
export declare class PiSessions {
|
|
8
|
+
private readonly sessionDir;
|
|
9
|
+
constructor(sessionDir: string);
|
|
10
|
+
list(signal: AbortSignal): Promise<SessionFile[]>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { createReadStream } from "node:fs";
|
|
2
|
+
import { readdir, stat } from "node:fs/promises";
|
|
3
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
4
|
+
import { createInterface } from "node:readline";
|
|
5
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
/**
|
|
7
|
+
* Pi session files whose sessions have the fields `SessionManager.listAll` builds.
|
|
8
|
+
* listAll keeps no cache and reads every file again on each call; these are read through
|
|
9
|
+
* the index.
|
|
10
|
+
*/
|
|
11
|
+
export class PiSessions {
|
|
12
|
+
sessionDir;
|
|
13
|
+
constructor(sessionDir) {
|
|
14
|
+
this.sessionDir = sessionDir;
|
|
15
|
+
}
|
|
16
|
+
async list(signal) {
|
|
17
|
+
return (await sessionFiles(this.sessionDir, signal)).map(({ path, stats }) => ({
|
|
18
|
+
path,
|
|
19
|
+
stats,
|
|
20
|
+
label: "Pi",
|
|
21
|
+
read: (readSignal) => readSession(path, stats, readSignal),
|
|
22
|
+
}));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Matches the "all" scope of /resume: the default store keeps one folder per project under
|
|
27
|
+
* the agent's sessions root, and a custom session directory holds every session itself.
|
|
28
|
+
* The order is listAll's, so sessions with equal activity times keep its order.
|
|
29
|
+
*/
|
|
30
|
+
async function sessionFiles(sessionDir, signal) {
|
|
31
|
+
const root = join(getAgentDir(), "sessions");
|
|
32
|
+
if (sessionDir !== "" && resolve(dirname(sessionDir)) !== resolve(root)) {
|
|
33
|
+
const names = (await jsonlFiles(sessionDir)).sort((a, b) => b.localeCompare(a));
|
|
34
|
+
return withStats(names.map((name) => join(sessionDir, name)), signal);
|
|
35
|
+
}
|
|
36
|
+
let projects;
|
|
37
|
+
try {
|
|
38
|
+
projects = (await readdir(root, { withFileTypes: true }))
|
|
39
|
+
.filter((entry) => entry.isDirectory() || entry.isSymbolicLink())
|
|
40
|
+
.map((entry) => join(root, entry.name));
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (error.code === "ENOENT")
|
|
44
|
+
return [];
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
const paths = await Promise.all(projects.map(async (project) => (await jsonlFiles(project)).map((name) => join(project, name))));
|
|
48
|
+
const files = await withStats(paths.flat(), signal);
|
|
49
|
+
return files.sort((a, b) => b.stats.mtimeMs - a.stats.mtimeMs || basename(b.path).localeCompare(basename(a.path)));
|
|
50
|
+
}
|
|
51
|
+
async function jsonlFiles(directory) {
|
|
52
|
+
try {
|
|
53
|
+
return (await readdir(directory)).filter((name) => name.endsWith(".jsonl"));
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
// A project entry may be a link to a file, and a custom store may not exist yet.
|
|
57
|
+
const code = error.code;
|
|
58
|
+
if (code === "ENOENT" || code === "ENOTDIR")
|
|
59
|
+
return [];
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function withStats(paths, signal) {
|
|
64
|
+
const files = await Promise.all(paths.map(async (path) => {
|
|
65
|
+
try {
|
|
66
|
+
return { path, stats: await stat(path) };
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error.code === "ENOENT")
|
|
70
|
+
return undefined;
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}));
|
|
74
|
+
signal.throwIfAborted();
|
|
75
|
+
return files.filter((file) => file !== undefined);
|
|
76
|
+
}
|
|
77
|
+
/** Pi's buildSessionInfo, field for field; undefined where it returns null. */
|
|
78
|
+
async function readSession(path, stats, signal) {
|
|
79
|
+
let header;
|
|
80
|
+
let name;
|
|
81
|
+
let messageCount = 0;
|
|
82
|
+
let firstMessage = "";
|
|
83
|
+
let lastActivity;
|
|
84
|
+
const texts = [];
|
|
85
|
+
const lines = createInterface({
|
|
86
|
+
input: createReadStream(path, { encoding: "utf8", signal }),
|
|
87
|
+
crlfDelay: Infinity,
|
|
88
|
+
});
|
|
89
|
+
for await (const line of lines) {
|
|
90
|
+
const entry = parseLine(line);
|
|
91
|
+
if (!entry)
|
|
92
|
+
continue;
|
|
93
|
+
if (!header) {
|
|
94
|
+
if (entry["type"] !== "session" || typeof entry["id"] !== "string")
|
|
95
|
+
return undefined;
|
|
96
|
+
header = entry;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (entry["type"] === "session_info") {
|
|
100
|
+
const value = entry["name"];
|
|
101
|
+
name = (typeof value === "string" ? value.trim() : "") || undefined;
|
|
102
|
+
}
|
|
103
|
+
if (entry["type"] !== "message")
|
|
104
|
+
continue;
|
|
105
|
+
messageCount++;
|
|
106
|
+
const message = entry["message"];
|
|
107
|
+
if (!isRecord(message) || !("content" in message))
|
|
108
|
+
continue;
|
|
109
|
+
if (message["role"] !== "user" && message["role"] !== "assistant")
|
|
110
|
+
continue;
|
|
111
|
+
const timestamp = message["timestamp"];
|
|
112
|
+
const activity = typeof timestamp === "number" ? timestamp : timeOf(entry["timestamp"]);
|
|
113
|
+
if (activity !== undefined)
|
|
114
|
+
lastActivity = Math.max(lastActivity ?? 0, activity);
|
|
115
|
+
const text = textOf(message["content"]);
|
|
116
|
+
if (!text)
|
|
117
|
+
continue;
|
|
118
|
+
texts.push(text);
|
|
119
|
+
if (!firstMessage && message["role"] === "user")
|
|
120
|
+
firstMessage = text;
|
|
121
|
+
}
|
|
122
|
+
if (!header)
|
|
123
|
+
return undefined;
|
|
124
|
+
const headerTime = typeof header["timestamp"] === "string" ? timeOf(header["timestamp"]) : undefined;
|
|
125
|
+
const parent = header["parentSession"];
|
|
126
|
+
return {
|
|
127
|
+
source: "pi",
|
|
128
|
+
path,
|
|
129
|
+
id: header["id"],
|
|
130
|
+
cwd: typeof header["cwd"] === "string" ? header["cwd"] : "",
|
|
131
|
+
name,
|
|
132
|
+
parentSessionPath: typeof parent === "string" ? parent : undefined,
|
|
133
|
+
created: new Date(header["timestamp"]),
|
|
134
|
+
modified: lastActivity !== undefined && lastActivity > 0
|
|
135
|
+
? new Date(lastActivity)
|
|
136
|
+
: headerTime !== undefined
|
|
137
|
+
? new Date(headerTime)
|
|
138
|
+
: stats.mtime,
|
|
139
|
+
messageCount,
|
|
140
|
+
firstMessage: firstMessage || "(no messages)",
|
|
141
|
+
allMessagesText: texts.join(" "),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
// Pi skips malformed lines too; the last line can be cut off while Pi appends to the file.
|
|
145
|
+
function parseLine(line) {
|
|
146
|
+
if (!line.trim())
|
|
147
|
+
return undefined;
|
|
148
|
+
try {
|
|
149
|
+
const entry = JSON.parse(line);
|
|
150
|
+
return isRecord(entry) ? entry : undefined;
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function textOf(content) {
|
|
157
|
+
if (typeof content === "string")
|
|
158
|
+
return content;
|
|
159
|
+
if (!Array.isArray(content))
|
|
160
|
+
return "";
|
|
161
|
+
return content
|
|
162
|
+
.filter((block) => isRecord(block) && block["type"] === "text")
|
|
163
|
+
.map((block) => block["text"])
|
|
164
|
+
.join(" ");
|
|
165
|
+
}
|
|
166
|
+
function timeOf(value) {
|
|
167
|
+
const time = new Date(value).getTime();
|
|
168
|
+
return Number.isNaN(time) ? undefined : time;
|
|
169
|
+
}
|
|
170
|
+
function isRecord(value) {
|
|
171
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
172
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Stats } from "node:fs";
|
|
2
|
+
import { type LocalSession } from "./session.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The default budget; Core settings let the user change it. The newest sessions whose entries
|
|
5
|
+
* fit are indexed and listed; older ones are neither read nor listed.
|
|
6
|
+
*/
|
|
7
|
+
export declare const INDEX_BUDGET_BYTES: number;
|
|
8
|
+
export declare function sessionIndexFile(agentDir?: string): string;
|
|
9
|
+
/** A file with the same inode, size and modification time reads the same. */
|
|
10
|
+
export declare function fileVersion(stats: Stats): string;
|
|
11
|
+
/** A session file a source found, read only when the index has no entry for its version. */
|
|
12
|
+
export interface SessionFile {
|
|
13
|
+
readonly path: string;
|
|
14
|
+
readonly stats: Stats;
|
|
15
|
+
/** Names the source in read errors. */
|
|
16
|
+
readonly label: string;
|
|
17
|
+
/** The session in the file; undefined when the file holds none to list. */
|
|
18
|
+
read(signal: AbortSignal): Promise<LocalSession | undefined>;
|
|
19
|
+
/** The session with state kept outside its file, such as a Codex title, applied. */
|
|
20
|
+
current?(session: LocalSession): LocalSession;
|
|
21
|
+
/** Invalidates an omitted entry's size when state outside its file changes. */
|
|
22
|
+
readonly currentVersion?: string;
|
|
23
|
+
}
|
|
24
|
+
export interface SessionScan {
|
|
25
|
+
/** The listed sessions, in the order of the files. */
|
|
26
|
+
sessions: LocalSession[];
|
|
27
|
+
errors: Error[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* What the catalog read from each session file, saved so that a new Pi session or process
|
|
31
|
+
* reads only the files that changed since. Listing reads every file in full, and the session
|
|
32
|
+
* stores reach gigabytes because of tool output the list never uses.
|
|
33
|
+
*
|
|
34
|
+
* Every load and every changed scan reads or writes the whole index, so it holds only the
|
|
35
|
+
* newest sessions that fit its budget, with their text clipped to the configured limit.
|
|
36
|
+
*
|
|
37
|
+
* Several Pi processes share the file. Each writes a complete index and renames it into
|
|
38
|
+
* place, so readers always see a whole index and the last writer wins.
|
|
39
|
+
*/
|
|
40
|
+
export declare class SessionIndex {
|
|
41
|
+
private readonly file;
|
|
42
|
+
/**
|
|
43
|
+
* Bytes the entries of a scan may take. A smaller budget drops the oldest entries at the
|
|
44
|
+
* next scan; a larger one reads the files the last scan left out.
|
|
45
|
+
*/
|
|
46
|
+
budget: number;
|
|
47
|
+
textLimit: number;
|
|
48
|
+
private entries;
|
|
49
|
+
private indexedTextLimit;
|
|
50
|
+
private loaded;
|
|
51
|
+
private changed;
|
|
52
|
+
constructor(file: string, budget?: number, textLimit?: number);
|
|
53
|
+
/**
|
|
54
|
+
* Reads the saved index once. A missing file or another format starts empty; an invalid
|
|
55
|
+
* one is reported and replaced by the next save.
|
|
56
|
+
*/
|
|
57
|
+
load(signal: AbortSignal): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Lists the sessions of the newest files, by modification time, until the next one does
|
|
60
|
+
* not fit the budget. Files the index lacks are read one at a time, which bounds memory
|
|
61
|
+
* for large rollouts and keeps the order. Entries for every other file are dropped.
|
|
62
|
+
*/
|
|
63
|
+
scan(files: readonly SessionFile[], signal: AbortSignal): Promise<SessionScan>;
|
|
64
|
+
/** Writes the index if the last scan changed it. */
|
|
65
|
+
save(): Promise<void>;
|
|
66
|
+
}
|