proper-base 0.2.4 → 0.2.6
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/index.ts +16 -2
- package/package.json +1 -1
- package/src/commit-guard.ts +673 -0
- package/src/footer-colors.ts +37 -8
- package/src/transcript-cleanup.ts +123 -88
package/index.ts
CHANGED
|
@@ -23,6 +23,7 @@ import {
|
|
|
23
23
|
installModelAutocompleteSubmit,
|
|
24
24
|
sortModelAutocompleteDescending,
|
|
25
25
|
} from "./src/autocomplete-details.ts";
|
|
26
|
+
import { commitGuardReason } from "./src/commit-guard.ts";
|
|
26
27
|
import {
|
|
27
28
|
installEditorNavigation,
|
|
28
29
|
installPromptClear,
|
|
@@ -146,7 +147,9 @@ function installKeybindings(keybindings: EditorKeybindings): void {
|
|
|
146
147
|
controller.apply();
|
|
147
148
|
}
|
|
148
149
|
|
|
149
|
-
type EditorTui = Parameters<EditorFactory>[0]
|
|
150
|
+
type EditorTui = Parameters<EditorFactory>[0] & {
|
|
151
|
+
scrollToBottom?(): void;
|
|
152
|
+
};
|
|
150
153
|
type PromptEditor = ReturnType<EditorFactory> & {
|
|
151
154
|
isShowingAutocomplete?(): boolean;
|
|
152
155
|
};
|
|
@@ -306,6 +309,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
306
309
|
|
|
307
310
|
pi.on("input", (event) => {
|
|
308
311
|
if (event.source === "interactive") {
|
|
312
|
+
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Pinned transcript scrolling]]
|
|
313
|
+
if (event.text.trim()) activeTui?.scrollToBottom?.();
|
|
309
314
|
promptDisplay.captureInput(event.text, pi.getCommands());
|
|
310
315
|
}
|
|
311
316
|
if (
|
|
@@ -405,6 +410,15 @@ export default function (pi: ExtensionAPI) {
|
|
|
405
410
|
transcriptCleanup?.start();
|
|
406
411
|
});
|
|
407
412
|
|
|
413
|
+
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Commit message guard]]
|
|
414
|
+
pi.on("tool_call", (event) => {
|
|
415
|
+
if (event.toolName !== "bash" && event.toolName !== "quill_execute") return;
|
|
416
|
+
const command = (event.input as { command?: unknown } | undefined)?.command;
|
|
417
|
+
if (typeof command !== "string") return;
|
|
418
|
+
const reason = commitGuardReason(command);
|
|
419
|
+
if (reason) return { block: true, reason };
|
|
420
|
+
});
|
|
421
|
+
|
|
408
422
|
pi.on("tool_execution_start", () => {
|
|
409
423
|
if (pendingPrompt) pendingPrompt.processed = true;
|
|
410
424
|
});
|
|
@@ -558,7 +572,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
558
572
|
activeTui = tui;
|
|
559
573
|
// @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Settled transcript]]
|
|
560
574
|
transcriptCleanup?.uninstall();
|
|
561
|
-
transcriptCleanup = installTranscriptCleanup(tui, ctx
|
|
575
|
+
transcriptCleanup = installTranscriptCleanup(tui, ctx);
|
|
562
576
|
removeSmartSelection?.();
|
|
563
577
|
removeSmartSelection = installSmartSelection(tui);
|
|
564
578
|
historyGuard = installHistoryGuard(editor);
|
package/package.json
CHANGED
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Commit-command guard: validates `git commit` invocations before the bash
|
|
3
|
+
* tool executes them. Port of the Claude Code PreToolUse hook
|
|
4
|
+
* `commit_message_validator.py` (command mode; the amend guard is
|
|
5
|
+
* intentionally not ported).
|
|
6
|
+
*
|
|
7
|
+
* The command must be one direct `git … commit …` — no wrappers, env
|
|
8
|
+
* prefixes, compound shell, or dynamic tokens — with literal `-m`/`--message`
|
|
9
|
+
* text only. Message text must keep the subject and non-trailer body lines
|
|
10
|
+
* within 72 characters, a blank second line, and no attribution lines.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const MAX_LINE_LENGTH = 72;
|
|
14
|
+
|
|
15
|
+
const TRAILER_RE = /^[A-Za-z0-9][A-Za-z0-9-]*:\s+\S/;
|
|
16
|
+
const CONTINUATION_RE = /^[ \t]/;
|
|
17
|
+
const ASSIGNMENT_RE = /^[A-Za-z_][A-Za-z0-9_]*=/;
|
|
18
|
+
const FORBIDDEN_PATTERNS: readonly (readonly [RegExp, string])[] = [
|
|
19
|
+
[/\bco[-_ ]*authored[-_ ]*by\b/i, "co-authored-by"],
|
|
20
|
+
[/\bco[-_ ]*author\b/i, "co-author"],
|
|
21
|
+
[/\bauthored[-_ ]*by\b.*\bclaude\b/i, "authored-by Claude"],
|
|
22
|
+
[/\bnoreply@anthropic\b/i, "noreply@anthropic"],
|
|
23
|
+
[/\bclaude\b.*<[^>]+@[^>]+>/i, "Claude email"],
|
|
24
|
+
[/^\s*generated with claude(?: code)?\s*$/i, "Claude attribution"],
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
interface ShellToken {
|
|
28
|
+
text: string;
|
|
29
|
+
dynamic: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const CONTROL_OPERATORS = new Set(["&&", "||", ";", "|", "&", "\n"]);
|
|
33
|
+
const COMMAND_START_KEYWORDS = new Set([
|
|
34
|
+
"(",
|
|
35
|
+
"{",
|
|
36
|
+
"then",
|
|
37
|
+
"do",
|
|
38
|
+
"else",
|
|
39
|
+
"elif",
|
|
40
|
+
"if",
|
|
41
|
+
"while",
|
|
42
|
+
"until",
|
|
43
|
+
"!",
|
|
44
|
+
]);
|
|
45
|
+
const WRAPPER_COMMANDS = new Set([
|
|
46
|
+
"env",
|
|
47
|
+
"command",
|
|
48
|
+
"sudo",
|
|
49
|
+
"time",
|
|
50
|
+
"builtin",
|
|
51
|
+
"nohup",
|
|
52
|
+
"sh",
|
|
53
|
+
"bash",
|
|
54
|
+
"zsh",
|
|
55
|
+
"fish",
|
|
56
|
+
"timeout",
|
|
57
|
+
"nice",
|
|
58
|
+
"setsid",
|
|
59
|
+
"stdbuf",
|
|
60
|
+
"ionice",
|
|
61
|
+
]);
|
|
62
|
+
const SHELL_WRAPPER_COMMANDS = new Set(["sh", "bash", "zsh", "fish"]);
|
|
63
|
+
|
|
64
|
+
const GIT_GLOBAL_OPTS_WITH_VALUES = new Set([
|
|
65
|
+
"-C",
|
|
66
|
+
"-c",
|
|
67
|
+
"--git-dir",
|
|
68
|
+
"--work-tree",
|
|
69
|
+
"--namespace",
|
|
70
|
+
"--super-prefix",
|
|
71
|
+
"--config-env",
|
|
72
|
+
]);
|
|
73
|
+
const GIT_GLOBAL_OPTS_WITH_EQUALS = [
|
|
74
|
+
"--git-dir=",
|
|
75
|
+
"--work-tree=",
|
|
76
|
+
"--namespace=",
|
|
77
|
+
"--super-prefix=",
|
|
78
|
+
"--config-env=",
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
const UNSUPPORTED_MESSAGE_FLAGS: Record<string, string> = {
|
|
82
|
+
"-F": "Use literal -m/--message text instead of -F/--file.",
|
|
83
|
+
"--file": "Use literal -m/--message text instead of -F/--file.",
|
|
84
|
+
"-c": "Do not reuse commit messages with -c/--reedit-message.",
|
|
85
|
+
"-C": "Do not reuse commit messages with -C/--reuse-message.",
|
|
86
|
+
"--reuse-message": "Do not reuse commit messages with -C/--reuse-message.",
|
|
87
|
+
"--reedit-message": "Do not reuse commit messages with -c/--reedit-message.",
|
|
88
|
+
"-t": "Do not use commit templates with -t/--template.",
|
|
89
|
+
"--template": "Do not use commit templates with -t/--template.",
|
|
90
|
+
"-e": "Do not open the editor with -e/--edit; use literal -m/--message text.",
|
|
91
|
+
"--edit":
|
|
92
|
+
"Do not open the editor with -e/--edit; use literal -m/--message text.",
|
|
93
|
+
"-s": "Do not use -s/--signoff because it mutates the final commit message.",
|
|
94
|
+
"--fixup": "Do not use --fixup because it rewrites the commit message.",
|
|
95
|
+
"--squash": "Do not use --squash because it rewrites the commit message.",
|
|
96
|
+
"--signoff":
|
|
97
|
+
"Do not use --signoff because it mutates the final commit message.",
|
|
98
|
+
"--trailer":
|
|
99
|
+
"Do not use --trailer because it mutates the final commit message.",
|
|
100
|
+
"--cleanup":
|
|
101
|
+
"Do not use --cleanup because it can mutate the final commit message.",
|
|
102
|
+
"--allow-empty-message": "Do not use --allow-empty-message.",
|
|
103
|
+
"--no-verify": "Do not use --no-verify because it bypasses commit hooks.",
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
function basename(token: string): string {
|
|
107
|
+
return token.slice(token.lastIndexOf("/") + 1);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function isGitBinary(token: string): boolean {
|
|
111
|
+
return basename(token) === "git";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const DYNAMIC_FOLLOWERS =
|
|
115
|
+
"({0123456789@*#?$!-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
|
|
116
|
+
|
|
117
|
+
function startsDynamicValue(command: string, index: number): boolean {
|
|
118
|
+
const char = command[index];
|
|
119
|
+
if (char === "`") return true;
|
|
120
|
+
if (char !== "$" || index + 1 >= command.length) return false;
|
|
121
|
+
return DYNAMIC_FOLLOWERS.includes(command[index + 1] as string);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function tokenizeShellCommand(command: string): ShellToken[] {
|
|
125
|
+
const tokens: ShellToken[] = [];
|
|
126
|
+
let chars: string[] = [];
|
|
127
|
+
let tokenStarted = false;
|
|
128
|
+
let dynamic = false;
|
|
129
|
+
let state: "plain" | "single" | "double" = "plain";
|
|
130
|
+
let index = 0;
|
|
131
|
+
|
|
132
|
+
const flush = () => {
|
|
133
|
+
if (tokenStarted) tokens.push({ text: chars.join(""), dynamic });
|
|
134
|
+
chars = [];
|
|
135
|
+
tokenStarted = false;
|
|
136
|
+
dynamic = false;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
while (index < command.length) {
|
|
140
|
+
const char = command[index] as string;
|
|
141
|
+
|
|
142
|
+
if (state === "plain") {
|
|
143
|
+
if (char === "\n") {
|
|
144
|
+
flush();
|
|
145
|
+
tokens.push({ text: "\n", dynamic: false });
|
|
146
|
+
index += 1;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (/\s/.test(char)) {
|
|
150
|
+
flush();
|
|
151
|
+
index += 1;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (";|&(){}!".includes(char)) {
|
|
155
|
+
flush();
|
|
156
|
+
if (
|
|
157
|
+
index + 1 < command.length &&
|
|
158
|
+
command[index + 1] === char &&
|
|
159
|
+
(char === "|" || char === "&")
|
|
160
|
+
) {
|
|
161
|
+
tokens.push({ text: char + char, dynamic: false });
|
|
162
|
+
index += 2;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
tokens.push({ text: char, dynamic: false });
|
|
166
|
+
index += 1;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (char === "'") {
|
|
170
|
+
tokenStarted = true;
|
|
171
|
+
state = "single";
|
|
172
|
+
index += 1;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (char === '"') {
|
|
176
|
+
tokenStarted = true;
|
|
177
|
+
state = "double";
|
|
178
|
+
index += 1;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (char === "\\") {
|
|
182
|
+
tokenStarted = true;
|
|
183
|
+
index += 1;
|
|
184
|
+
if (index >= command.length)
|
|
185
|
+
throw new Error("unterminated escape sequence");
|
|
186
|
+
chars.push(command[index] as string);
|
|
187
|
+
index += 1;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
tokenStarted = true;
|
|
192
|
+
if (startsDynamicValue(command, index)) dynamic = true;
|
|
193
|
+
chars.push(char);
|
|
194
|
+
index += 1;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (state === "single") {
|
|
199
|
+
if (char === "'") {
|
|
200
|
+
state = "plain";
|
|
201
|
+
index += 1;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
chars.push(char);
|
|
205
|
+
index += 1;
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// state === "double"
|
|
210
|
+
if (char === '"') {
|
|
211
|
+
state = "plain";
|
|
212
|
+
index += 1;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (char === "\\") {
|
|
216
|
+
index += 1;
|
|
217
|
+
if (index >= command.length)
|
|
218
|
+
throw new Error("unterminated escape sequence");
|
|
219
|
+
const escaped = command[index] as string;
|
|
220
|
+
if ('"\\$`\n'.includes(escaped)) {
|
|
221
|
+
chars.push(escaped);
|
|
222
|
+
} else {
|
|
223
|
+
chars.push("\\", escaped);
|
|
224
|
+
}
|
|
225
|
+
index += 1;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (startsDynamicValue(command, index)) dynamic = true;
|
|
229
|
+
chars.push(char);
|
|
230
|
+
index += 1;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (state !== "plain") throw new Error("unterminated quote");
|
|
234
|
+
|
|
235
|
+
flush();
|
|
236
|
+
return tokens;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function isTrailerLine(line: string): boolean {
|
|
240
|
+
return TRAILER_RE.test(line);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function isContinuationLine(line: string): boolean {
|
|
244
|
+
return CONTINUATION_RE.test(line);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function finalTrailerBlock(lines: string[]): Set<number> {
|
|
248
|
+
let end = lines.length - 1;
|
|
249
|
+
while (end >= 0 && (lines[end] as string).trim() === "") end -= 1;
|
|
250
|
+
|
|
251
|
+
if (end < 0) return new Set();
|
|
252
|
+
|
|
253
|
+
let start = end;
|
|
254
|
+
while (
|
|
255
|
+
start >= 0 &&
|
|
256
|
+
(isTrailerLine(lines[start] as string) ||
|
|
257
|
+
isContinuationLine(lines[start] as string))
|
|
258
|
+
) {
|
|
259
|
+
start -= 1;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (start >= 0 && (lines[start] as string).trim() !== "") return new Set();
|
|
263
|
+
|
|
264
|
+
const block = lines.slice(start + 1, end + 1);
|
|
265
|
+
if (!block.length || !block.some((line) => isTrailerLine(line)))
|
|
266
|
+
return new Set();
|
|
267
|
+
|
|
268
|
+
let sawTrailer = false;
|
|
269
|
+
for (const line of block) {
|
|
270
|
+
if (isTrailerLine(line)) {
|
|
271
|
+
sawTrailer = true;
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (isContinuationLine(line) && sawTrailer) continue;
|
|
275
|
+
return new Set();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const indexes = new Set<number>();
|
|
279
|
+
for (let i = start + 1; i <= end; i += 1) indexes.add(i);
|
|
280
|
+
return indexes;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Validate final commit message text against the house rules. */
|
|
284
|
+
export function validateMessageText(text: string): string[] {
|
|
285
|
+
const lines = text.split(/\r\n|\r|\n/);
|
|
286
|
+
if (text === "") lines.length = 0;
|
|
287
|
+
const errors: string[] = [];
|
|
288
|
+
const trailerBlock = finalTrailerBlock(lines);
|
|
289
|
+
|
|
290
|
+
if (!lines.length || (lines[0] as string).trim() === "") {
|
|
291
|
+
return ["line 1: subject line is missing or empty"];
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if ((lines[0] as string).length > MAX_LINE_LENGTH) {
|
|
295
|
+
errors.push(`line 1: line exceeds ${MAX_LINE_LENGTH} characters`);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (lines.length > 1 && (lines[1] as string).trim() !== "") {
|
|
299
|
+
errors.push("line 2: body must start with a blank line after the subject");
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
lines.forEach((line, index) => {
|
|
303
|
+
const lineNumber = index + 1;
|
|
304
|
+
if (!trailerBlock.has(index) && line.length > MAX_LINE_LENGTH) {
|
|
305
|
+
errors.push(
|
|
306
|
+
`line ${lineNumber}: line exceeds ${MAX_LINE_LENGTH} characters`,
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
for (const [pattern, label] of FORBIDDEN_PATTERNS) {
|
|
310
|
+
if (pattern.test(line)) {
|
|
311
|
+
errors.push(
|
|
312
|
+
`line ${lineNumber}: forbidden attribution line (${label})`,
|
|
313
|
+
);
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
return errors;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function commandStartIndexes(tokens: ShellToken[]): number[] {
|
|
323
|
+
const starts = tokens.length ? [0] : [];
|
|
324
|
+
tokens.slice(0, -1).forEach((token, index) => {
|
|
325
|
+
if (
|
|
326
|
+
CONTROL_OPERATORS.has(token.text) ||
|
|
327
|
+
COMMAND_START_KEYWORDS.has(token.text)
|
|
328
|
+
) {
|
|
329
|
+
starts.push(index + 1);
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
return starts;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function findCommitArgs(tokens: ShellToken[]): ShellToken[] | null {
|
|
336
|
+
if (!tokens.length || !isGitBinary((tokens[0] as ShellToken).text))
|
|
337
|
+
return null;
|
|
338
|
+
|
|
339
|
+
let idx = 1;
|
|
340
|
+
while (idx < tokens.length) {
|
|
341
|
+
const token = (tokens[idx] as ShellToken).text;
|
|
342
|
+
if (token === "commit") return tokens.slice(idx + 1);
|
|
343
|
+
if (token === "--") return null;
|
|
344
|
+
if (GIT_GLOBAL_OPTS_WITH_VALUES.has(token)) {
|
|
345
|
+
idx += 2;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
if (
|
|
349
|
+
GIT_GLOBAL_OPTS_WITH_EQUALS.some((prefix) => token.startsWith(prefix))
|
|
350
|
+
) {
|
|
351
|
+
idx += 1;
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
if (token.startsWith("-")) {
|
|
355
|
+
idx += 1;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function startsDirectGitCommit(tokens: ShellToken[], start: number): boolean {
|
|
365
|
+
if (
|
|
366
|
+
start >= tokens.length ||
|
|
367
|
+
!isGitBinary((tokens[start] as ShellToken).text)
|
|
368
|
+
)
|
|
369
|
+
return false;
|
|
370
|
+
return findCommitArgs(tokens.slice(start)) !== null;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function startsWrappedGitCommit(tokens: ShellToken[], start: number): boolean {
|
|
374
|
+
if (start >= tokens.length) return false;
|
|
375
|
+
|
|
376
|
+
let index = start;
|
|
377
|
+
const first = tokens[index] as ShellToken;
|
|
378
|
+
if (first.dynamic && first.text.toLowerCase().endsWith("git")) {
|
|
379
|
+
return (
|
|
380
|
+
index + 1 < tokens.length &&
|
|
381
|
+
(tokens[index + 1] as ShellToken).text === "commit"
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const command = basename(first.text);
|
|
386
|
+
if (WRAPPER_COMMANDS.has(command)) {
|
|
387
|
+
if (SHELL_WRAPPER_COMMANDS.has(command)) {
|
|
388
|
+
return tokens
|
|
389
|
+
.slice(index + 1)
|
|
390
|
+
.some(
|
|
391
|
+
(token) =>
|
|
392
|
+
token.text.includes("git") && token.text.includes("commit"),
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
index += 1;
|
|
397
|
+
while (
|
|
398
|
+
index < tokens.length &&
|
|
399
|
+
((tokens[index] as ShellToken).text.startsWith("-") ||
|
|
400
|
+
ASSIGNMENT_RE.test((tokens[index] as ShellToken).text))
|
|
401
|
+
) {
|
|
402
|
+
if (
|
|
403
|
+
(tokens[index] as ShellToken).text.startsWith("-") &&
|
|
404
|
+
index + 1 < tokens.length &&
|
|
405
|
+
!(tokens[index + 1] as ShellToken).text.startsWith("-") &&
|
|
406
|
+
!ASSIGNMENT_RE.test((tokens[index + 1] as ShellToken).text)
|
|
407
|
+
) {
|
|
408
|
+
index += 2;
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
index += 1;
|
|
412
|
+
}
|
|
413
|
+
if (
|
|
414
|
+
startsDirectGitCommit(tokens, index) ||
|
|
415
|
+
startsWrappedGitCommit(tokens, index)
|
|
416
|
+
) {
|
|
417
|
+
return true;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
let scan = index + 1;
|
|
421
|
+
while (scan < tokens.length) {
|
|
422
|
+
const text = (tokens[scan] as ShellToken).text;
|
|
423
|
+
if (CONTROL_OPERATORS.has(text) || COMMAND_START_KEYWORDS.has(text))
|
|
424
|
+
break;
|
|
425
|
+
if (
|
|
426
|
+
startsDirectGitCommit(tokens, scan) ||
|
|
427
|
+
startsWrappedGitCommit(tokens, scan)
|
|
428
|
+
) {
|
|
429
|
+
return true;
|
|
430
|
+
}
|
|
431
|
+
scan += 1;
|
|
432
|
+
}
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
if (ASSIGNMENT_RE.test(first.text)) {
|
|
437
|
+
while (
|
|
438
|
+
index < tokens.length &&
|
|
439
|
+
ASSIGNMENT_RE.test((tokens[index] as ShellToken).text)
|
|
440
|
+
) {
|
|
441
|
+
index += 1;
|
|
442
|
+
}
|
|
443
|
+
return startsDirectGitCommit(tokens, index);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function unsupportedFlagMessage(flag: string): string | undefined {
|
|
450
|
+
if (flag in UNSUPPORTED_MESSAGE_FLAGS) return UNSUPPORTED_MESSAGE_FLAGS[flag];
|
|
451
|
+
const prefixed: readonly (readonly [string, string])[] = [
|
|
452
|
+
["--file=", UNSUPPORTED_MESSAGE_FLAGS["--file"] as string],
|
|
453
|
+
[
|
|
454
|
+
"--reuse-message=",
|
|
455
|
+
UNSUPPORTED_MESSAGE_FLAGS["--reuse-message"] as string,
|
|
456
|
+
],
|
|
457
|
+
[
|
|
458
|
+
"--reedit-message=",
|
|
459
|
+
UNSUPPORTED_MESSAGE_FLAGS["--reedit-message"] as string,
|
|
460
|
+
],
|
|
461
|
+
["--template=", UNSUPPORTED_MESSAGE_FLAGS["--template"] as string],
|
|
462
|
+
["--fixup=", UNSUPPORTED_MESSAGE_FLAGS["--fixup"] as string],
|
|
463
|
+
["--squash=", UNSUPPORTED_MESSAGE_FLAGS["--squash"] as string],
|
|
464
|
+
["--trailer=", UNSUPPORTED_MESSAGE_FLAGS["--trailer"] as string],
|
|
465
|
+
["--cleanup=", UNSUPPORTED_MESSAGE_FLAGS["--cleanup"] as string],
|
|
466
|
+
];
|
|
467
|
+
for (const [prefix, message] of prefixed) {
|
|
468
|
+
if (flag.startsWith(prefix)) return message;
|
|
469
|
+
}
|
|
470
|
+
return undefined;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function parseShortOptions(
|
|
474
|
+
token: ShellToken,
|
|
475
|
+
args: ShellToken[],
|
|
476
|
+
index: number,
|
|
477
|
+
): { messages: ShellToken[]; index: number; errors: string[] } {
|
|
478
|
+
const messages: ShellToken[] = [];
|
|
479
|
+
const errors: string[] = [];
|
|
480
|
+
const shortFlags = token.text.slice(1);
|
|
481
|
+
let cursor = 0;
|
|
482
|
+
|
|
483
|
+
while (cursor < shortFlags.length) {
|
|
484
|
+
const flag = shortFlags[cursor] as string;
|
|
485
|
+
if (flag === "m") {
|
|
486
|
+
const attachedValue = shortFlags.slice(cursor + 1);
|
|
487
|
+
if (attachedValue) {
|
|
488
|
+
messages.push({ text: attachedValue, dynamic: token.dynamic });
|
|
489
|
+
return { messages, index, errors };
|
|
490
|
+
}
|
|
491
|
+
if (index + 1 >= args.length) {
|
|
492
|
+
errors.push("Commit message is missing after -m.");
|
|
493
|
+
return { messages, index, errors };
|
|
494
|
+
}
|
|
495
|
+
messages.push(args[index + 1] as ShellToken);
|
|
496
|
+
return { messages, index: index + 1, errors };
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if ("FcCtes".includes(flag)) {
|
|
500
|
+
errors.push(
|
|
501
|
+
unsupportedFlagMessage(`-${flag}`) ?? `Unsupported flag: -${flag}`,
|
|
502
|
+
);
|
|
503
|
+
if (
|
|
504
|
+
cursor === shortFlags.length - 1 &&
|
|
505
|
+
flag !== "e" &&
|
|
506
|
+
index + 1 < args.length
|
|
507
|
+
) {
|
|
508
|
+
return { messages, index: index + 1, errors };
|
|
509
|
+
}
|
|
510
|
+
return { messages, index, errors };
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
cursor += 1;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
return { messages, index, errors };
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Extract the literal commit message from a shell command. Returns the
|
|
521
|
+
* joined message when the command is a valid direct commit, `null` with no
|
|
522
|
+
* errors when the command contains no commit, and `null` with errors when
|
|
523
|
+
* the commit invocation itself is not allowed.
|
|
524
|
+
*/
|
|
525
|
+
export function extractMessageFromCommand(command: string): {
|
|
526
|
+
message: string | null;
|
|
527
|
+
errors: string[];
|
|
528
|
+
} {
|
|
529
|
+
let tokens: ShellToken[];
|
|
530
|
+
try {
|
|
531
|
+
tokens = tokenizeShellCommand(command);
|
|
532
|
+
} catch (error) {
|
|
533
|
+
return {
|
|
534
|
+
message: null,
|
|
535
|
+
errors: [
|
|
536
|
+
`Unable to parse git commit command: ${error instanceof Error ? error.message : String(error)}`,
|
|
537
|
+
],
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const starts = commandStartIndexes(tokens);
|
|
542
|
+
const directStart = starts.find((start) =>
|
|
543
|
+
startsDirectGitCommit(tokens, start),
|
|
544
|
+
);
|
|
545
|
+
|
|
546
|
+
if (directStart === undefined) {
|
|
547
|
+
if (starts.some((start) => startsWrappedGitCommit(tokens, start))) {
|
|
548
|
+
return {
|
|
549
|
+
message: null,
|
|
550
|
+
errors: [
|
|
551
|
+
"Commit command must be a direct `git ... commit ...` invocation. Wrappers, env prefixes, and compound shell commands are not allowed.",
|
|
552
|
+
],
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
return { message: null, errors: [] };
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
if (directStart !== 0 || starts.length > 1) {
|
|
559
|
+
return {
|
|
560
|
+
message: null,
|
|
561
|
+
errors: [
|
|
562
|
+
"Commit command must be a direct `git ... commit ...` invocation. Wrappers, env prefixes, and compound shell commands are not allowed.",
|
|
563
|
+
],
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const commitArgs = findCommitArgs(tokens);
|
|
568
|
+
if (commitArgs === null) return { message: null, errors: [] };
|
|
569
|
+
|
|
570
|
+
if (commitArgs.some((token) => token.dynamic)) {
|
|
571
|
+
return {
|
|
572
|
+
message: null,
|
|
573
|
+
errors: [
|
|
574
|
+
"Commit command must not contain shell expansion, command substitution, or variables in commit arguments. Use literal flags and literal message text only.",
|
|
575
|
+
],
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
const messages: ShellToken[] = [];
|
|
580
|
+
const errors: string[] = [];
|
|
581
|
+
let idx = 0;
|
|
582
|
+
|
|
583
|
+
while (idx < commitArgs.length) {
|
|
584
|
+
const token = commitArgs[idx] as ShellToken;
|
|
585
|
+
const tokenText = token.text;
|
|
586
|
+
|
|
587
|
+
const unsupported = unsupportedFlagMessage(tokenText);
|
|
588
|
+
if (unsupported !== undefined) {
|
|
589
|
+
errors.push(unsupported);
|
|
590
|
+
idx += 1;
|
|
591
|
+
continue;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if (tokenText === "--message") {
|
|
595
|
+
if (idx + 1 >= commitArgs.length) {
|
|
596
|
+
errors.push("Commit message is missing after --message.");
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
messages.push(commitArgs[idx + 1] as ShellToken);
|
|
600
|
+
idx += 2;
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
if (tokenText.startsWith("--message=")) {
|
|
605
|
+
messages.push({
|
|
606
|
+
text: tokenText.split(/=(.*)/s)[1] as string,
|
|
607
|
+
dynamic: token.dynamic,
|
|
608
|
+
});
|
|
609
|
+
idx += 1;
|
|
610
|
+
continue;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
if (tokenText.startsWith("--")) {
|
|
614
|
+
idx += 1;
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
if (tokenText.startsWith("-") && tokenText !== "-") {
|
|
619
|
+
const parsed = parseShortOptions(token, commitArgs, idx);
|
|
620
|
+
messages.push(...parsed.messages);
|
|
621
|
+
errors.push(...parsed.errors);
|
|
622
|
+
idx = parsed.index + 1;
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
idx += 1;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
if (errors.length) return { message: null, errors };
|
|
630
|
+
|
|
631
|
+
if (!messages.length) {
|
|
632
|
+
return {
|
|
633
|
+
message: null,
|
|
634
|
+
errors: [
|
|
635
|
+
"Commit has no literal -m/--message content. Use -m or --message with full commit text.",
|
|
636
|
+
],
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
if (messages.some((message) => message.dynamic)) {
|
|
641
|
+
return {
|
|
642
|
+
message: null,
|
|
643
|
+
errors: [
|
|
644
|
+
"Commit messages must be literal text. Shell expansion, command substitution, and variables are not allowed.",
|
|
645
|
+
],
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
return {
|
|
650
|
+
message: messages.map((message) => message.text).join("\n\n"),
|
|
651
|
+
errors: [],
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Return a block reason for a shell command containing an invalid `git
|
|
657
|
+
* commit`, or `undefined` when the command is fine (valid commit or no
|
|
658
|
+
* commit at all). Commands that never mention `git` and `commit` are
|
|
659
|
+
* skipped without tokenizing, so unrelated commands with heredocs or odd
|
|
660
|
+
* quoting are never blocked.
|
|
661
|
+
*/
|
|
662
|
+
export function commitGuardReason(command: string): string | undefined {
|
|
663
|
+
if (!/\bgit\b/.test(command) || !/\bcommit\b/.test(command)) return undefined;
|
|
664
|
+
|
|
665
|
+
const { message, errors } = extractMessageFromCommand(command);
|
|
666
|
+
const allErrors =
|
|
667
|
+
errors.length || message === null ? errors : validateMessageText(message);
|
|
668
|
+
if (!allErrors.length) return undefined;
|
|
669
|
+
return [
|
|
670
|
+
"Commit message validation failed:",
|
|
671
|
+
...allErrors.map((e) => `- ${e}`),
|
|
672
|
+
].join("\n");
|
|
673
|
+
}
|
package/src/footer-colors.ts
CHANGED
|
@@ -114,12 +114,17 @@ export function installFooterColors(
|
|
|
114
114
|
if (level === "max" || level === "ultra") startAnimation();
|
|
115
115
|
else stopAnimation();
|
|
116
116
|
const theme = state.ctx.ui.theme;
|
|
117
|
-
return colorFooter(
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
117
|
+
return colorFooter(
|
|
118
|
+
layoutFooter(render.call(footer, width), width, theme, (wide) =>
|
|
119
|
+
render.call(footer, wide),
|
|
120
|
+
),
|
|
121
|
+
{
|
|
122
|
+
level,
|
|
123
|
+
model: state.ctx.model?.id,
|
|
124
|
+
now: Date.now(),
|
|
125
|
+
theme,
|
|
126
|
+
},
|
|
127
|
+
);
|
|
123
128
|
};
|
|
124
129
|
footer.dispose = () => {
|
|
125
130
|
controller.uninstall();
|
|
@@ -133,12 +138,13 @@ function layoutFooter(
|
|
|
133
138
|
lines: string[],
|
|
134
139
|
width: number,
|
|
135
140
|
theme: FooterTheme,
|
|
141
|
+
reRender?: (width: number) => string[],
|
|
136
142
|
): string[] {
|
|
137
143
|
if (lines.length < 2 || !Number.isFinite(width) || width < 20) return lines;
|
|
138
144
|
|
|
139
|
-
|
|
145
|
+
let statsLine = lines[1];
|
|
140
146
|
if (!statsLine) return lines;
|
|
141
|
-
|
|
147
|
+
let match = stripTerminalSequences(statsLine).match(USAGE_THROUGH_COST);
|
|
142
148
|
const usage = match?.[1];
|
|
143
149
|
if (!match || !usage) return lines;
|
|
144
150
|
|
|
@@ -147,6 +153,22 @@ function layoutFooter(
|
|
|
147
153
|
const availablePathWidth = contentWidth - usageWidth - 1;
|
|
148
154
|
if (availablePathWidth < 8) return lines;
|
|
149
155
|
|
|
156
|
+
// Pi right-aligns the model segment against the original width and cuts
|
|
157
|
+
// its tail (thinking level, fast, paused) with no ellipsis when the
|
|
158
|
+
// one-line stats row overflows. This layout frees exactly the usage
|
|
159
|
+
// block's columns, so re-render at that width to recover the cut tags.
|
|
160
|
+
if (reRender) {
|
|
161
|
+
const wide = reRender(width + usageWidth);
|
|
162
|
+
const wideStats = wide[1] ?? "";
|
|
163
|
+
const wideMatch =
|
|
164
|
+
stripTerminalSequences(wideStats).match(USAGE_THROUGH_COST);
|
|
165
|
+
if (wideMatch?.[1] === usage) {
|
|
166
|
+
lines = wide;
|
|
167
|
+
statsLine = wideStats;
|
|
168
|
+
match = wideMatch;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
150
172
|
const result = [...lines];
|
|
151
173
|
const path = truncateToWidth(
|
|
152
174
|
result[0] ?? "",
|
|
@@ -166,6 +188,13 @@ function layoutFooter(
|
|
|
166
188
|
true,
|
|
167
189
|
);
|
|
168
190
|
result[1] = realignFooterRemainder(remainder, contentWidth, theme);
|
|
191
|
+
for (let index = 2; index < result.length; index++) {
|
|
192
|
+
result[index] = truncateToWidth(
|
|
193
|
+
result[index] ?? "",
|
|
194
|
+
width,
|
|
195
|
+
theme.fg("dim", "..."),
|
|
196
|
+
);
|
|
197
|
+
}
|
|
169
198
|
return result;
|
|
170
199
|
}
|
|
171
200
|
|
|
@@ -10,13 +10,9 @@ import {
|
|
|
10
10
|
} from "@earendil-works/pi-tui";
|
|
11
11
|
|
|
12
12
|
const INSTALLED = Symbol.for("pi-proper-base.transcript-cleanup");
|
|
13
|
-
const PROCESS_LINK_PREFIX = "pi-proper-base://process/";
|
|
14
13
|
const SGR_MOUSE_TAIL = /^\[<(\d+);(\d+);(\d+)([Mm])$/;
|
|
15
14
|
const LEFT_BUTTON = "0";
|
|
16
15
|
|
|
17
|
-
type Keybindings = {
|
|
18
|
-
getKeys(action: "app.tools.expand"): readonly string[];
|
|
19
|
-
};
|
|
20
16
|
type ComponentContainer = Component & { children: Component[] };
|
|
21
17
|
type AssistantMessage = {
|
|
22
18
|
content: Array<{ type: string; text?: string; thinking?: string }>;
|
|
@@ -31,17 +27,20 @@ type AssistantComponent = Component & {
|
|
|
31
27
|
type InstalledContainer = ComponentContainer & {
|
|
32
28
|
[INSTALLED]?: TranscriptCleanupController;
|
|
33
29
|
};
|
|
30
|
+
type DetailHit = {
|
|
31
|
+
owner: object;
|
|
32
|
+
line: string;
|
|
33
|
+
end: number;
|
|
34
|
+
};
|
|
34
35
|
type State = {
|
|
35
36
|
ctx: ExtensionContext;
|
|
36
37
|
tui: TUI;
|
|
37
|
-
keybindings: Keybindings;
|
|
38
38
|
activeStart: number | undefined;
|
|
39
39
|
completed: Set<Component>;
|
|
40
40
|
owned: Set<Component>;
|
|
41
41
|
expanded: Map<object, boolean>;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
nextId: number;
|
|
42
|
+
hits: Map<number, DetailHit>;
|
|
43
|
+
pendingHits: DetailHit[];
|
|
45
44
|
globalExpanded: boolean;
|
|
46
45
|
assistantCache: WeakMap<AssistantComponent, AssistantRenderCache>;
|
|
47
46
|
};
|
|
@@ -68,6 +67,16 @@ type MouseTui = TUI & {
|
|
|
68
67
|
inputListeners?: Set<TuiInputListener>;
|
|
69
68
|
previousLines?: string[];
|
|
70
69
|
previousScreen?: string[];
|
|
70
|
+
getPrimaryScrollView?(): unknown;
|
|
71
|
+
getScrollSelectionPoint?(
|
|
72
|
+
scrollView: unknown,
|
|
73
|
+
x: number,
|
|
74
|
+
y: number,
|
|
75
|
+
): { row: number; col: number } | undefined;
|
|
76
|
+
};
|
|
77
|
+
type TranscriptContainers = {
|
|
78
|
+
document: ComponentContainer;
|
|
79
|
+
chat: InstalledContainer;
|
|
71
80
|
};
|
|
72
81
|
|
|
73
82
|
export type TranscriptCleanupController = {
|
|
@@ -75,33 +84,31 @@ export type TranscriptCleanupController = {
|
|
|
75
84
|
completeAssistant(message: object): void;
|
|
76
85
|
completeTool(toolCallId: string): void;
|
|
77
86
|
settle(): void;
|
|
78
|
-
update(ctx: ExtensionContext, tui: TUI
|
|
87
|
+
update(ctx: ExtensionContext, tui: TUI): void;
|
|
79
88
|
uninstall(): void;
|
|
80
89
|
};
|
|
81
90
|
|
|
82
91
|
export function installTranscriptCleanup(
|
|
83
92
|
tui: TUI,
|
|
84
93
|
ctx: ExtensionContext,
|
|
85
|
-
keybindings: Keybindings,
|
|
86
94
|
): TranscriptCleanupController | undefined {
|
|
87
|
-
const
|
|
88
|
-
if (!
|
|
95
|
+
const containers = findTranscriptContainers(tui);
|
|
96
|
+
if (!containers) return undefined;
|
|
97
|
+
const { document, chat } = containers;
|
|
89
98
|
if (chat[INSTALLED]) {
|
|
90
|
-
chat[INSTALLED].update(ctx, tui
|
|
99
|
+
chat[INSTALLED].update(ctx, tui);
|
|
91
100
|
return chat[INSTALLED];
|
|
92
101
|
}
|
|
93
102
|
|
|
94
103
|
let state: State = {
|
|
95
104
|
ctx,
|
|
96
105
|
tui,
|
|
97
|
-
keybindings,
|
|
98
106
|
activeStart: ctx.isIdle() ? undefined : chat.children.length,
|
|
99
107
|
completed: new Set(),
|
|
100
108
|
owned: new Set(chat.children),
|
|
101
109
|
expanded: new Map(),
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
nextId: 0,
|
|
110
|
+
hits: new Map(),
|
|
111
|
+
pendingHits: [],
|
|
105
112
|
globalExpanded: ctx.ui.getToolsExpanded(),
|
|
106
113
|
assistantCache: new WeakMap(),
|
|
107
114
|
};
|
|
@@ -163,12 +170,11 @@ export function installTranscriptCleanup(
|
|
|
163
170
|
};
|
|
164
171
|
state.tui.requestRender();
|
|
165
172
|
},
|
|
166
|
-
update(nextCtx, nextTui
|
|
173
|
+
update(nextCtx, nextTui) {
|
|
167
174
|
state = {
|
|
168
175
|
...state,
|
|
169
176
|
ctx: nextCtx,
|
|
170
177
|
tui: nextTui,
|
|
171
|
-
keybindings: nextKeybindings,
|
|
172
178
|
};
|
|
173
179
|
},
|
|
174
180
|
uninstall() {
|
|
@@ -190,15 +196,19 @@ export function installTranscriptCleanup(
|
|
|
190
196
|
if (globalExpanded !== state.globalExpanded) {
|
|
191
197
|
state = { ...state, globalExpanded, expanded: new Map() };
|
|
192
198
|
}
|
|
193
|
-
state.
|
|
199
|
+
state.pendingHits.length = 0;
|
|
200
|
+
let lines: string[];
|
|
194
201
|
if (state.activeStart === undefined && state.ctx.isIdle()) {
|
|
195
|
-
|
|
202
|
+
lines = renderCollapsed(chat.children, width, state);
|
|
203
|
+
} else {
|
|
204
|
+
const start = state.activeStart ?? chat.children.length;
|
|
205
|
+
lines = [
|
|
206
|
+
...renderCollapsed(chat.children.slice(0, start), width, state),
|
|
207
|
+
...renderActive(chat.children.slice(start), width, state),
|
|
208
|
+
];
|
|
196
209
|
}
|
|
197
|
-
|
|
198
|
-
return
|
|
199
|
-
...renderCollapsed(chat.children.slice(0, start), width, state),
|
|
200
|
-
...renderActive(chat.children.slice(start), width, state),
|
|
201
|
-
];
|
|
210
|
+
indexDetailHits(lines, documentRowOffset(document, chat, width), state);
|
|
211
|
+
return lines;
|
|
202
212
|
};
|
|
203
213
|
chat[INSTALLED] = controller;
|
|
204
214
|
return controller;
|
|
@@ -407,28 +417,20 @@ function hasSectionText(lines: string[]): boolean {
|
|
|
407
417
|
}
|
|
408
418
|
|
|
409
419
|
function renderDetail(detail: Detail, width: number, state: State): string[] {
|
|
410
|
-
let id = state.ids.get(detail.owner);
|
|
411
|
-
if (!id) {
|
|
412
|
-
id = String(++state.nextId);
|
|
413
|
-
state.ids.set(detail.owner, id);
|
|
414
|
-
}
|
|
415
|
-
state.groups.set(id, detail.owner);
|
|
416
420
|
const expanded = state.expanded.get(detail.owner) ?? state.globalExpanded;
|
|
417
|
-
const
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
? ` (click or ${keys} to ${action})`
|
|
421
|
-
: ` (click to ${action})`;
|
|
422
|
-
const label = `${expanded ? "▾" : "▸"} ${detail.label}${hint}`;
|
|
423
|
-
const styled = state.ctx.ui.theme.fg(detailColor(detail.kind), label);
|
|
424
|
-
const header = truncateToWidth(
|
|
425
|
-
`\x1b]8;;${PROCESS_LINK_PREFIX}${id}\x07${styled}\x1b]8;;\x07`,
|
|
426
|
-
width,
|
|
427
|
-
"",
|
|
421
|
+
const marker = state.ctx.ui.theme.fg(
|
|
422
|
+
"borderAccent",
|
|
423
|
+
state.ctx.ui.theme.bold(expanded ? "⌄" : "›"),
|
|
428
424
|
);
|
|
429
|
-
const
|
|
430
|
-
|
|
431
|
-
|
|
425
|
+
const label = state.ctx.ui.theme.fg(detailColor(detail.kind), detail.label);
|
|
426
|
+
const header = truncateToWidth(`${marker} ${label}`, width, "");
|
|
427
|
+
const headerLine = `${header}${" ".repeat(Math.max(0, width - visibleWidth(header)))}`;
|
|
428
|
+
state.pendingHits.push({
|
|
429
|
+
owner: detail.owner,
|
|
430
|
+
line: headerLine,
|
|
431
|
+
end: visibleWidth(header),
|
|
432
|
+
});
|
|
433
|
+
const lines = [headerLine];
|
|
432
434
|
const expandable = detail.component as ToolComponent;
|
|
433
435
|
// setExpanded rebuilds the tool's result renderer even when the state is
|
|
434
436
|
// unchanged, which re-sanitizes full outputs on every frame. Skip no-ops.
|
|
@@ -436,11 +438,14 @@ function renderDetail(detail: Detail, width: number, state: State): string[] {
|
|
|
436
438
|
if (expanded) {
|
|
437
439
|
lines.push(...detail.component.render(width));
|
|
438
440
|
const button = `\x1b[7m ${state.ctx.ui.theme.fg(detailColor(detail.kind), "collapse")} \x1b[27m`;
|
|
439
|
-
const
|
|
440
|
-
const
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
441
|
+
const rendered = truncateToWidth(button, width, "");
|
|
442
|
+
const buttonLine = `${rendered}${" ".repeat(Math.max(0, width - visibleWidth(rendered)))}`;
|
|
443
|
+
state.pendingHits.push({
|
|
444
|
+
owner: detail.owner,
|
|
445
|
+
line: buttonLine,
|
|
446
|
+
end: visibleWidth(rendered),
|
|
447
|
+
});
|
|
448
|
+
lines.push(buttonLine);
|
|
444
449
|
}
|
|
445
450
|
return lines;
|
|
446
451
|
}
|
|
@@ -459,7 +464,16 @@ function detailColor(
|
|
|
459
464
|
function describeTool(tool: ToolComponent, error: boolean): string {
|
|
460
465
|
const name = tool.toolName ?? "tool";
|
|
461
466
|
const args = asRecord(tool.args);
|
|
462
|
-
const value = [
|
|
467
|
+
const value = [
|
|
468
|
+
"tool",
|
|
469
|
+
"path",
|
|
470
|
+
"command",
|
|
471
|
+
"pattern",
|
|
472
|
+
"query",
|
|
473
|
+
"task",
|
|
474
|
+
"url",
|
|
475
|
+
"action",
|
|
476
|
+
]
|
|
463
477
|
.map((key) => args?.[key])
|
|
464
478
|
.find((candidate): candidate is string => typeof candidate === "string");
|
|
465
479
|
const target = value ? ` · ${oneLine(value)}` : "";
|
|
@@ -476,6 +490,29 @@ function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
|
476
490
|
: undefined;
|
|
477
491
|
}
|
|
478
492
|
|
|
493
|
+
function documentRowOffset(
|
|
494
|
+
document: ComponentContainer,
|
|
495
|
+
chat: ComponentContainer,
|
|
496
|
+
width: number,
|
|
497
|
+
): number {
|
|
498
|
+
const chatIndex = document.children.indexOf(chat);
|
|
499
|
+
if (chatIndex <= 0) return 0;
|
|
500
|
+
return document.children
|
|
501
|
+
.slice(0, chatIndex)
|
|
502
|
+
.reduce((rows, child) => rows + child.render(width).length, 0);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function indexDetailHits(lines: string[], offset: number, state: State): void {
|
|
506
|
+
state.hits.clear();
|
|
507
|
+
let nextRow = 0;
|
|
508
|
+
for (const hit of state.pendingHits) {
|
|
509
|
+
const relativeRow = lines.indexOf(hit.line, nextRow);
|
|
510
|
+
if (relativeRow < 0) continue;
|
|
511
|
+
state.hits.set(offset + relativeRow, hit);
|
|
512
|
+
nextRow = relativeRow + 1;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
479
516
|
function handleClick(
|
|
480
517
|
data: string,
|
|
481
518
|
state: State,
|
|
@@ -485,51 +522,44 @@ function handleClick(
|
|
|
485
522
|
if (!match || match[1] !== LEFT_BUTTON) return undefined;
|
|
486
523
|
const column = Number.parseInt(match[2] ?? "", 10) - 1;
|
|
487
524
|
const row = Number.parseInt(match[3] ?? "", 10) - 1;
|
|
488
|
-
const
|
|
489
|
-
if (!
|
|
490
|
-
const
|
|
491
|
-
if (!
|
|
492
|
-
const
|
|
493
|
-
if (!
|
|
525
|
+
const point = contentPoint(state.tui, column, row);
|
|
526
|
+
if (!point) return undefined;
|
|
527
|
+
const hit = state.hits.get(point.row);
|
|
528
|
+
if (!hit || point.col < 0 || point.col >= hit.end) return undefined;
|
|
529
|
+
const screenLine = screenLines(state.tui)[row];
|
|
530
|
+
if (!screenLine || !sameHitText(screenLine, hit)) return undefined;
|
|
494
531
|
if (match[4] === "M") {
|
|
495
532
|
state.expanded.set(
|
|
496
|
-
owner,
|
|
497
|
-
!(state.expanded.get(owner) ?? state.globalExpanded),
|
|
533
|
+
hit.owner,
|
|
534
|
+
!(state.expanded.get(hit.owner) ?? state.globalExpanded),
|
|
498
535
|
);
|
|
499
536
|
state.tui.requestRender();
|
|
500
537
|
}
|
|
501
538
|
return { consume: true };
|
|
502
539
|
}
|
|
503
540
|
|
|
504
|
-
function
|
|
541
|
+
function contentPoint(
|
|
542
|
+
tui: TUI,
|
|
543
|
+
column: number,
|
|
544
|
+
row: number,
|
|
545
|
+
): { row: number; col: number } | undefined {
|
|
505
546
|
const host = tui as MouseTui;
|
|
506
|
-
|
|
547
|
+
const scrollView = host.getPrimaryScrollView?.();
|
|
548
|
+
return scrollView !== undefined && host.getScrollSelectionPoint
|
|
549
|
+
? host.getScrollSelectionPoint(scrollView, column, row)
|
|
550
|
+
: { row, col: column };
|
|
507
551
|
}
|
|
508
552
|
|
|
509
|
-
function
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
}
|
|
520
|
-
if (line[index] === "\x1b") {
|
|
521
|
-
const end = line.indexOf("m", index + 1);
|
|
522
|
-
if (end < 0) break;
|
|
523
|
-
index = end + 1;
|
|
524
|
-
continue;
|
|
525
|
-
}
|
|
526
|
-
const character = line[index] ?? "";
|
|
527
|
-
const width = visibleWidth(character);
|
|
528
|
-
if (column >= visible && column < visible + width) return active;
|
|
529
|
-
visible += width;
|
|
530
|
-
index += character.length;
|
|
531
|
-
}
|
|
532
|
-
return undefined;
|
|
553
|
+
function sameHitText(line: string, hit: DetailHit): boolean {
|
|
554
|
+
return (
|
|
555
|
+
truncateToWidth(stripTerminalSequences(line), hit.end, "") ===
|
|
556
|
+
truncateToWidth(stripTerminalSequences(hit.line), hit.end, "")
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
function screenLines(tui: TUI): string[] {
|
|
561
|
+
const host = tui as MouseTui;
|
|
562
|
+
return host.previousScreen ?? host.previousLines ?? [];
|
|
533
563
|
}
|
|
534
564
|
|
|
535
565
|
function prioritize(tui: TUI, listener: TuiInputListener): void {
|
|
@@ -541,7 +571,7 @@ function prioritize(tui: TUI, listener: TuiInputListener): void {
|
|
|
541
571
|
for (const entry of rest) listeners.add(entry);
|
|
542
572
|
}
|
|
543
573
|
|
|
544
|
-
function
|
|
574
|
+
function findTranscriptContainers(tui: TUI): TranscriptContainers | undefined {
|
|
545
575
|
for (const root of roots(tui)) {
|
|
546
576
|
const document = find(root, (component) => {
|
|
547
577
|
const children = childrenOf(component);
|
|
@@ -553,7 +583,12 @@ function findChatContainer(tui: TUI): ComponentContainer | undefined {
|
|
|
553
583
|
);
|
|
554
584
|
});
|
|
555
585
|
const chat = document ? childrenOf(document)[2] : undefined;
|
|
556
|
-
if (
|
|
586
|
+
if (document && chat) {
|
|
587
|
+
return {
|
|
588
|
+
document: document as ComponentContainer,
|
|
589
|
+
chat: chat as InstalledContainer,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
557
592
|
}
|
|
558
593
|
return undefined;
|
|
559
594
|
}
|