@d3ara1n/pi-session-namer 0.1.3 → 0.2.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/README.md +1 -1
- package/package.json +1 -1
- package/src/config.ts +31 -33
- package/src/index.ts +131 -126
- package/src/namer.ts +61 -58
package/README.md
CHANGED
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -12,33 +12,33 @@ import type { SessionNamerConfig } from "./types.ts";
|
|
|
12
12
|
import { DEFAULT_CONFIG } from "./types.ts";
|
|
13
13
|
|
|
14
14
|
function getAgentDir(): string {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const envDir = process.env.PI_AGENT_DIR;
|
|
16
|
+
if (envDir) return envDir;
|
|
17
|
+
return path.join(os.homedir(), ".pi", "agent");
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
function readSettingsFile(filePath: string): any {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
try {
|
|
22
|
+
if (!fs.existsSync(filePath)) return {};
|
|
23
|
+
const content = fs.readFileSync(filePath, "utf-8");
|
|
24
|
+
return JSON.parse(content);
|
|
25
|
+
} catch {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
function merge(target: any, source: any): any {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
31
|
+
if (!source || typeof source !== "object") return target;
|
|
32
|
+
if (!target || typeof target !== "object") return source;
|
|
33
|
+
const result = { ...target };
|
|
34
|
+
for (const key of Object.keys(source)) {
|
|
35
|
+
if (source[key] && typeof source[key] === "object" && !Array.isArray(source[key])) {
|
|
36
|
+
result[key] = merge(result[key], source[key]);
|
|
37
|
+
} else {
|
|
38
|
+
result[key] = source[key];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/**
|
|
@@ -46,18 +46,16 @@ function merge(target: any, source: any): any {
|
|
|
46
46
|
* @param cwd - Project working directory
|
|
47
47
|
*/
|
|
48
48
|
export function loadNamerConfig(cwd?: string): SessionNamerConfig {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
: {};
|
|
53
|
-
const settings = merge(globalSettings, projectSettings);
|
|
49
|
+
const globalSettings = readSettingsFile(path.join(getAgentDir(), "settings.json"));
|
|
50
|
+
const projectSettings = cwd ? readSettingsFile(path.join(cwd, ".pi", "settings.json")) : {};
|
|
51
|
+
const settings = merge(globalSettings, projectSettings);
|
|
54
52
|
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
const raw = settings?.sessionNamer;
|
|
54
|
+
if (!raw) return DEFAULT_CONFIG;
|
|
57
55
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
return {
|
|
57
|
+
enabled: raw.enabled ?? DEFAULT_CONFIG.enabled,
|
|
58
|
+
sideAgentRole: raw.sideAgentRole ?? DEFAULT_CONFIG.sideAgentRole,
|
|
59
|
+
maxLength: raw.maxLength ?? DEFAULT_CONFIG.maxLength,
|
|
60
|
+
};
|
|
63
61
|
}
|
package/src/index.ts
CHANGED
|
@@ -8,136 +8,141 @@
|
|
|
8
8
|
|
|
9
9
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
10
10
|
import { getModelRolesAPI } from "@d3ara1n/pi-model-roles";
|
|
11
|
+
import type { ModelRolesAPI } from "@d3ara1n/pi-model-roles";
|
|
11
12
|
import { DEFAULT_CONFIG } from "./types.ts";
|
|
12
13
|
import type { SessionNamerConfig } from "./types.ts";
|
|
13
14
|
import { loadNamerConfig } from "./config.ts";
|
|
14
15
|
import { generateSessionName } from "./namer.ts";
|
|
15
16
|
|
|
16
17
|
export default function sessionNamerExtension(pi: ExtensionAPI) {
|
|
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
|
-
|
|
18
|
+
let config: SessionNamerConfig = DEFAULT_CONFIG;
|
|
19
|
+
let hasNamed = false;
|
|
20
|
+
let lastPrompt = "";
|
|
21
|
+
|
|
22
|
+
// ── session_start: load config, reset flag ──────────────────────
|
|
23
|
+
pi.on("session_start", async (_event, _ctx) => {
|
|
24
|
+
if (!_ctx.hasUI) return;
|
|
25
|
+
config = loadNamerConfig(_ctx.cwd);
|
|
26
|
+
hasNamed = false;
|
|
27
|
+
lastPrompt = "";
|
|
28
|
+
|
|
29
|
+
// If the session already has a name (resume/fork/user-set), don't auto-name
|
|
30
|
+
const existingName = pi.getSessionName();
|
|
31
|
+
if (existingName) {
|
|
32
|
+
hasNamed = true;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// ── before_agent_start: auto-name on first prompt ───────────────
|
|
37
|
+
pi.on("before_agent_start", async (event, ctx) => {
|
|
38
|
+
if (!ctx.hasUI) return;
|
|
39
|
+
// Always cache the latest prompt for /namer:rename, even if auto-naming is done
|
|
40
|
+
lastPrompt = event.prompt;
|
|
41
|
+
|
|
42
|
+
if (!config.enabled || hasNamed) return;
|
|
43
|
+
|
|
44
|
+
// Skip empty prompts (e.g. image-only messages)
|
|
45
|
+
if (!event.prompt?.trim()) return;
|
|
46
|
+
|
|
47
|
+
// Mark as handled (no retry regardless of subsequent success/failure)
|
|
48
|
+
hasNamed = true;
|
|
49
|
+
|
|
50
|
+
// Name asynchronously so we don't block the main agent startup
|
|
51
|
+
(async () => {
|
|
52
|
+
let rolesApi: ModelRolesAPI;
|
|
53
|
+
try {
|
|
54
|
+
rolesApi = getModelRolesAPI();
|
|
55
|
+
} catch {
|
|
56
|
+
console.warn("[pi-session-namer] pi-model-roles not initialized — skipping");
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const resolved = await rolesApi.resolveRoleAsync(config.sideAgentRole);
|
|
61
|
+
if (!resolved.model) {
|
|
62
|
+
console.warn(
|
|
63
|
+
`[pi-session-namer] Side agent role "${config.sideAgentRole}" not available — skipping`,
|
|
64
|
+
);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const name = await generateSessionName(
|
|
69
|
+
resolved.model,
|
|
70
|
+
resolved.apiKey,
|
|
71
|
+
resolved.headers,
|
|
72
|
+
config,
|
|
73
|
+
event.prompt,
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
pi.setSessionName(name);
|
|
77
|
+
})().catch((err) => console.warn("[pi-session-namer] naming failed:", err));
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// ── /namer — show status ────────────────────────────────────────
|
|
81
|
+
pi.registerCommand("namer", {
|
|
82
|
+
description: "Show session namer status and config",
|
|
83
|
+
handler: async (args, ctx) => {
|
|
84
|
+
const value = (args ?? "").trim().toLowerCase();
|
|
85
|
+
|
|
86
|
+
// Handle on/off toggles
|
|
87
|
+
if (value === "on") {
|
|
88
|
+
config.enabled = true;
|
|
89
|
+
ctx.ui.notify("Session Namer: enabled", "info");
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (value === "off") {
|
|
93
|
+
config.enabled = false;
|
|
94
|
+
ctx.ui.notify("Session Namer: disabled", "info");
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const currentName = pi.getSessionName();
|
|
99
|
+
const lines = [
|
|
100
|
+
`Session Namer: ${config.enabled ? "enabled" : "disabled"}`,
|
|
101
|
+
`Side agent role: ${config.sideAgentRole}`,
|
|
102
|
+
`Max length: ${config.maxLength}`,
|
|
103
|
+
`Current name: ${currentName ?? "(none)"}`,
|
|
104
|
+
`Has auto-named: ${hasNamed}`,
|
|
105
|
+
];
|
|
106
|
+
ctx.ui.notify(lines.join("\n"), "info");
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// ── /namer:rename — force regenerate ────────────────────────────
|
|
111
|
+
pi.registerCommand("namer:rename", {
|
|
112
|
+
description: "Regenerate session name from the last user prompt",
|
|
113
|
+
handler: async (_args, ctx) => {
|
|
114
|
+
if (!lastPrompt?.trim()) {
|
|
115
|
+
ctx.ui.notify("No user prompt available to generate a name from.", "warning");
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
let rolesApi: ModelRolesAPI;
|
|
120
|
+
try {
|
|
121
|
+
rolesApi = getModelRolesAPI();
|
|
122
|
+
} catch {
|
|
123
|
+
ctx.ui.notify("pi-model-roles not initialized. Cannot rename.", "error");
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const resolved = await rolesApi.resolveRoleAsync(config.sideAgentRole);
|
|
128
|
+
if (!resolved.model) {
|
|
129
|
+
ctx.ui.notify(
|
|
130
|
+
`Side agent role "${config.sideAgentRole}" not available. Cannot rename.`,
|
|
131
|
+
"error",
|
|
132
|
+
);
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const name = await generateSessionName(
|
|
137
|
+
resolved.model,
|
|
138
|
+
resolved.apiKey,
|
|
139
|
+
resolved.headers,
|
|
140
|
+
config,
|
|
141
|
+
lastPrompt,
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
pi.setSessionName(name);
|
|
145
|
+
ctx.ui.notify(`Session renamed: ${name}`, "info");
|
|
146
|
+
},
|
|
147
|
+
});
|
|
143
148
|
}
|
package/src/namer.ts
CHANGED
|
@@ -12,85 +12,88 @@ import type { SessionNamerConfig } from "./types.ts";
|
|
|
12
12
|
* Build the system prompt for the naming side agent.
|
|
13
13
|
*/
|
|
14
14
|
export function buildNamerSystemPrompt(maxLength: number): string {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
return [
|
|
16
|
+
`You are a session naming assistant. Generate a concise title for a coding session based on the user's first message.`,
|
|
17
|
+
``,
|
|
18
|
+
`Rules:`,
|
|
19
|
+
`- Output in the SAME language as the user's message`,
|
|
20
|
+
`- Maximum ${maxLength} characters`,
|
|
21
|
+
`- Output ONLY the title, no quotes, no prefix, no explanation`,
|
|
22
|
+
`- Summarize intent, do not copy the original message verbatim`,
|
|
23
|
+
`- If the message mentions specific files, modules, or functions, keep those names`,
|
|
24
|
+
`- Be specific: "Fix auth token refresh bug" is better than "Fix a bug"`,
|
|
25
|
+
].join("\n");
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* Call the side agent to generate a session name.
|
|
30
30
|
*/
|
|
31
31
|
export async function generateSessionName(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
sideModel: any,
|
|
33
|
+
apiKey: string | undefined,
|
|
34
|
+
headers: Record<string, string> | undefined,
|
|
35
|
+
config: SessionNamerConfig,
|
|
36
|
+
userPrompt: string,
|
|
37
37
|
): Promise<string> {
|
|
38
|
-
|
|
38
|
+
const systemPrompt = buildNamerSystemPrompt(config.maxLength);
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
? userPrompt.slice(0, 2000) + "..."
|
|
43
|
-
: userPrompt;
|
|
40
|
+
// Truncate very long prompts to avoid wasting tokens
|
|
41
|
+
const truncatedPrompt = userPrompt.length > 2000 ? userPrompt.slice(0, 2000) + "..." : userPrompt;
|
|
44
42
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
const options: Record<string, any> = {
|
|
44
|
+
maxTokens: 64,
|
|
45
|
+
};
|
|
48
46
|
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
if (apiKey) options.apiKey = apiKey;
|
|
48
|
+
if (headers) options.headers = headers;
|
|
51
49
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
try {
|
|
51
|
+
const result = await complete(
|
|
52
|
+
sideModel,
|
|
53
|
+
{
|
|
54
|
+
systemPrompt,
|
|
55
|
+
messages: [{ role: "user", content: truncatedPrompt, timestamp: Date.now() }],
|
|
56
|
+
},
|
|
57
|
+
options,
|
|
58
|
+
);
|
|
57
59
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
const raw =
|
|
61
|
+
result.content
|
|
62
|
+
?.filter((block: any) => block.type === "text")
|
|
63
|
+
?.map((block: any) => block.text)
|
|
64
|
+
?.join("")
|
|
65
|
+
?.trim() ?? "";
|
|
63
66
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
return cleanSessionName(raw, config.maxLength);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
console.warn("[pi-session-namer] Side agent call failed:", err);
|
|
70
|
+
// Fallback: truncate user prompt as name
|
|
71
|
+
return userPrompt.slice(0, config.maxLength).replace(/\n/g, " ").trim();
|
|
72
|
+
}
|
|
70
73
|
}
|
|
71
74
|
|
|
72
75
|
/**
|
|
73
76
|
* Clean and truncate the generated name.
|
|
74
77
|
*/
|
|
75
78
|
function cleanSessionName(raw: string, maxLength: number): string {
|
|
76
|
-
|
|
79
|
+
let name = raw.trim();
|
|
77
80
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
81
|
+
// Strip surrounding quotes if present
|
|
82
|
+
if (
|
|
83
|
+
(name.startsWith('"') && name.endsWith('"')) ||
|
|
84
|
+
(name.startsWith("'") && name.endsWith("'")) ||
|
|
85
|
+
(name.startsWith("「") && name.endsWith("」"))
|
|
86
|
+
) {
|
|
87
|
+
name = name.slice(1, -1);
|
|
88
|
+
}
|
|
86
89
|
|
|
87
|
-
|
|
88
|
-
|
|
90
|
+
// Remove newlines
|
|
91
|
+
name = name.replace(/\n/g, " ").trim();
|
|
89
92
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
93
|
+
// Truncate
|
|
94
|
+
if (name.length > maxLength) {
|
|
95
|
+
name = name.slice(0, maxLength - 3) + "...";
|
|
96
|
+
}
|
|
94
97
|
|
|
95
|
-
|
|
98
|
+
return name || "New session";
|
|
96
99
|
}
|