-
Notifications
You must be signed in to change notification settings - Fork 492
Expand file tree
/
Copy pathtitle-generator.ts
More file actions
222 lines (198 loc) · 6.41 KB
/
title-generator.ts
File metadata and controls
222 lines (198 loc) · 6.41 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
/**
* Generate session titles using a smol, fast model.
*/
import * as path from "node:path";
import type { ThinkingLevel } from "@oh-my-pi/pi-agent-core";
import type { Api, Model } from "@oh-my-pi/pi-ai";
import { completeSimple } from "@oh-my-pi/pi-ai";
import { logger, prompt } from "@oh-my-pi/pi-utils";
import type { ModelRegistry } from "../config/model-registry";
import { resolveRoleSelection } from "../config/model-resolver";
import type { Settings } from "../config/settings";
import titleSystemPrompt from "../prompts/system/title-system.md" with { type: "text" };
import { toReasoningEffort } from "../thinking";
const DEFAULT_TERMINAL_TITLE = "\u03C0";
const TERMINAL_TITLE_CONTROL_CHARS = /[\u0000-\u001f\u007f-\u009f]/g;
const MAX_INPUT_CHARS = 2000;
function getTitleModel(
registry: ModelRegistry,
settings: Settings,
currentModel?: Model<Api>,
): { model: Model<Api>; thinkingLevel?: ThinkingLevel } | undefined {
const availableModels = registry.getAvailable();
if (availableModels.length === 0) return undefined;
const titleModel = resolveRoleSelection(["commit", "smol"], settings, availableModels, registry);
if (titleModel) {
return { model: titleModel.model, thinkingLevel: titleModel.thinkingLevel };
}
if (currentModel) {
return { model: currentModel };
}
return undefined;
}
/** Clean up model-generated title text. */
function cleanTitle(raw: string): string | null {
let title = raw.trim();
if (!title) return null;
// Strip leading markdown heading markers and common LLM artifacts
title = title
.replace(/^#+\s*/, "")
.replace(/^["']|["']$/g, "")
.replace(/[.!?]$/, "");
return title || null;
}
/**
* Shared LLM call for title generation.
* Resolves model, API key, calls completeSimple, and cleans the result.
*/
async function callTitleModel(
userMessage: string,
template: { currentTitle?: string; projectName?: string },
registry: ModelRegistry,
settings: Settings,
sessionId?: string,
currentModel?: Model<Api>,
): Promise<string | null> {
const candidate = getTitleModel(registry, settings, currentModel);
if (!candidate) {
logger.debug("title-generator: no title model found");
return null;
}
const apiKey = await registry.getApiKey(candidate.model, sessionId);
if (!apiKey) {
logger.debug("title-generator: no API key", {
provider: candidate.model.provider,
id: candidate.model.id,
});
return null;
}
const systemPrompt = prompt.render(titleSystemPrompt, template);
const model = `${candidate.model.provider}/${candidate.model.id}`;
logger.debug("title-generator: request", { model, userMessage });
try {
const response = await completeSimple(
candidate.model,
{
systemPrompt,
messages: [{ role: "user", content: userMessage, timestamp: Date.now() }],
},
{
apiKey,
maxTokens: 30,
reasoning: toReasoningEffort(candidate.thinkingLevel),
},
);
if (response.stopReason === "error") {
logger.debug("title-generator: response error", { model, errorMessage: response.errorMessage });
return null;
}
let title = "";
for (const content of response.content) {
if (content.type === "text") title += content.text;
}
logger.debug("title-generator: response", {
model,
title,
usage: response.usage,
stopReason: response.stopReason,
});
return cleanTitle(title);
} catch (err) {
logger.debug("title-generator: error", { model, error: err instanceof Error ? err.message : String(err) });
return null;
}
}
/**
* Generate a title for a session based on the first user message.
*/
export async function generateSessionTitle(
firstMessage: string,
registry: ModelRegistry,
settings: Settings,
sessionId?: string,
currentModel?: Model<Api>,
): Promise<string | null> {
const truncated =
firstMessage.length > MAX_INPUT_CHARS ? `${firstMessage.slice(0, MAX_INPUT_CHARS)}\u2026` : firstMessage;
return callTitleModel(
`<user-message>\n${truncated}\n</user-message>`,
{},
registry,
settings,
sessionId,
currentModel,
);
}
/**
* Re-generate a session title from a compact context summary.
* Uses the same prompt but passes currentTitle and projectName as template context
* so the model can decide whether the title still fits.
*/
export async function regenerateSessionTitle(
contextSummary: string,
currentTitle: string | undefined,
registry: ModelRegistry,
settings: Settings,
sessionId?: string,
currentModel?: Model<Api>,
projectName?: string,
): Promise<string | null> {
if (!contextSummary.trim()) return null;
return callTitleModel(
`<session-context>\n${contextSummary}\n</session-context>`,
{ currentTitle, projectName },
registry,
settings,
sessionId,
currentModel,
);
}
/**
* Remove control characters so model-generated titles cannot inject terminal escapes.
*/
function sanitizeTerminalTitlePart(value: string | undefined): string | undefined {
if (!value) return undefined;
const sanitized = value.replace(TERMINAL_TITLE_CONTROL_CHARS, "").trim();
return sanitized || undefined;
}
function getFallbackTerminalTitle(cwd: string | undefined): string | undefined {
if (!cwd) return undefined;
const resolvedCwd = path.resolve(cwd);
const baseName = path.basename(resolvedCwd);
if (!baseName || baseName === path.parse(resolvedCwd).root) return undefined;
return sanitizeTerminalTitlePart(baseName);
}
export function formatSessionTerminalTitle(
sessionName: string | undefined,
cwd?: string,
titleSource?: "auto" | "user" | undefined,
): string {
const label =
sanitizeTerminalTitlePart(titleSource === "auto" ? undefined : sessionName) ?? getFallbackTerminalTitle(cwd);
return label ? `${DEFAULT_TERMINAL_TITLE}: ${label}` : DEFAULT_TERMINAL_TITLE;
}
/**
* Set the terminal title using OSC 0 (sets both tab and window title). Unsupported terminals ignore it.
*/
export function setTerminalTitle(title: string): void {
process.stdout.write(`\x1b]0;${sanitizeTerminalTitlePart(title) ?? DEFAULT_TERMINAL_TITLE}\x07`);
}
export function setSessionTerminalTitle(
sessionName: string | undefined,
cwd?: string,
titleSource?: "auto" | "user" | undefined,
): void {
setTerminalTitle(formatSessionTerminalTitle(sessionName, cwd, titleSource));
}
/**
* Save the current terminal title on terminals that support xterm window ops.
*/
export function pushTerminalTitle(): void {
process.stdout.write("\x1b[22;2t");
}
/**
* Restore the previously saved terminal title on terminals that support xterm window ops.
*/
export function popTerminalTitle(): void {
process.stdout.write("\x1b[23;2t");
}