@arhen/pi-senja 1.5.2 → 1.5.3
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/package.json +1 -1
- package/src/diff-counts.ts +69 -0
- package/src/index.ts +4 -1
- package/src/tool-groups.ts +22 -4
package/package.json
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { constants } from "node:fs";
|
|
2
|
+
import { open } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { type ExtensionAPI, generateDiffString } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
|
|
8
|
+
export function countDiff(diff: string) {
|
|
9
|
+
return { added: diff.match(/^\+/gm)?.length ?? 0, removed: diff.match(/^-/gm)?.length ?? 0 };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const lines = (s: string) => (s.match(/\n/g)?.length ?? 0) + Number(!!s && !s.endsWith("\n"));
|
|
13
|
+
|
|
14
|
+
export function trackWrites(pi: ExtensionAPI, enabled = () => true) {
|
|
15
|
+
type Entry = { path: string; overlap?: boolean; counts?: ReturnType<typeof countDiff> };
|
|
16
|
+
const pending = new Map<string, Entry>();
|
|
17
|
+
pi.on("tool_call", async (event, ctx) => {
|
|
18
|
+
if (!enabled() || event.toolName !== "write" || event.parentToolCallId) return;
|
|
19
|
+
const { path, content } = event.input;
|
|
20
|
+
if (typeof path !== "string" || typeof content !== "string" || content.includes("\0")) return;
|
|
21
|
+
if (!pi.getAllTools().some((tool) => tool.name === "write" && tool.sourceInfo?.path === "builtin:write")) return;
|
|
22
|
+
try {
|
|
23
|
+
const raw = path.replace(/^@/, "").replace(/[\u00a0\u2000-\u200a\u202f\u205f\u3000]/g, " ");
|
|
24
|
+
const target = resolve(
|
|
25
|
+
ctx.cwd,
|
|
26
|
+
raw.startsWith("file://") ? fileURLToPath(raw) : raw.replace(/^~(?=\/|$)/, homedir()),
|
|
27
|
+
);
|
|
28
|
+
const entry: Entry = { path: target };
|
|
29
|
+
for (const other of pending.values()) {
|
|
30
|
+
if (other.path === target) other.overlap = entry.overlap = true;
|
|
31
|
+
}
|
|
32
|
+
pending.set(event.toolCallId, entry);
|
|
33
|
+
if (entry.overlap) return;
|
|
34
|
+
let before = "";
|
|
35
|
+
try {
|
|
36
|
+
const file = await open(target, constants.O_RDONLY | constants.O_NONBLOCK);
|
|
37
|
+
try {
|
|
38
|
+
const stat = await file.stat();
|
|
39
|
+
if (!stat.isFile() || stat.size > 512 * 1024) return;
|
|
40
|
+
const bytes = Buffer.alloc(stat.size + 1);
|
|
41
|
+
const { bytesRead } = await file.read(bytes);
|
|
42
|
+
if (bytesRead !== stat.size) return;
|
|
43
|
+
before = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(bytes.subarray(0, bytesRead));
|
|
44
|
+
} finally {
|
|
45
|
+
await file.close();
|
|
46
|
+
}
|
|
47
|
+
} catch (err) {
|
|
48
|
+
if ((err as NodeJS.ErrnoException).code !== "ENOENT") return;
|
|
49
|
+
}
|
|
50
|
+
if (before.includes("\0")) return;
|
|
51
|
+
if (before === content) entry.counts = { added: 0, removed: 0 };
|
|
52
|
+
else if (!before || !content) entry.counts = { added: lines(content), removed: lines(before) };
|
|
53
|
+
else {
|
|
54
|
+
// ponytail: skip expensive whole-file rewrites; move larger diffs to a worker if needed.
|
|
55
|
+
if (content.length > 512 * 1024 || (before + content).split("\n", 2049).length > 2048) return;
|
|
56
|
+
entry.counts = countDiff(generateDiffString(before, content).diff);
|
|
57
|
+
}
|
|
58
|
+
} catch {
|
|
59
|
+
// Optional display metadata must never block the write.
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
pi.on("tool_result", (event) => {
|
|
63
|
+
const entry = pending.get(event.toolCallId);
|
|
64
|
+
pending.delete(event.toolCallId);
|
|
65
|
+
if (event.toolName === "write" && !event.isError && entry?.counts && !entry.overlap && event.details == null)
|
|
66
|
+
return { details: { senjaCounts: entry.counts } };
|
|
67
|
+
});
|
|
68
|
+
pi.on("agent_end", () => pending.clear());
|
|
69
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -21,6 +21,7 @@ import type { ExtensionAPI, ExtensionContext, Theme, ThemeColor } from "@earendi
|
|
|
21
21
|
import { VERSION } from "@earendil-works/pi-coding-agent";
|
|
22
22
|
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
23
23
|
|
|
24
|
+
import { trackWrites } from "./diff-counts.ts";
|
|
24
25
|
import { createTranscriptView, formatDuration, installToolGroups } from "./tool-groups.ts";
|
|
25
26
|
|
|
26
27
|
// ── Visual primitives ────────────────────────────────────────────────────────
|
|
@@ -382,6 +383,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
382
383
|
let renderHandle: RenderHandle | undefined;
|
|
383
384
|
let disposeFooter: (() => void) | undefined;
|
|
384
385
|
let enabled = true;
|
|
386
|
+
trackWrites(pi, () => enabled);
|
|
385
387
|
const starts = new Map<string, number>();
|
|
386
388
|
const cancelled = new Map<string, number | undefined>();
|
|
387
389
|
function startCall(id: string) {
|
|
@@ -418,7 +420,8 @@ export default function (pi: ExtensionAPI) {
|
|
|
418
420
|
const guide =
|
|
419
421
|
"Start each codemode script with a // @title: comment describing its planned action in a short verb phrase (e.g. // @title: Find the goal notice renderer). If using // @options:, put the title on the next line.";
|
|
420
422
|
const unsubscribeTranscript = pi.events.on("pi:transcript-renderer", (accept) => {
|
|
421
|
-
if (enabled && typeof accept === "function")
|
|
423
|
+
if (enabled && typeof accept === "function")
|
|
424
|
+
accept({ mount: createTranscriptView, codemodeGuide: guide, trackWrites });
|
|
422
425
|
});
|
|
423
426
|
pi.on("before_agent_start", (event, ctx) => {
|
|
424
427
|
if (!enabled || ctx.mode !== "tui" || !event.systemPromptOptions?.selectedTools.includes("codemode")) return;
|
package/src/tool-groups.ts
CHANGED
|
@@ -23,6 +23,8 @@ import {
|
|
|
23
23
|
wrapTextWithAnsi,
|
|
24
24
|
} from "@earendil-works/pi-tui";
|
|
25
25
|
|
|
26
|
+
import { countDiff } from "./diff-counts.ts";
|
|
27
|
+
|
|
26
28
|
type ToolRow = Component & {
|
|
27
29
|
toolName: string;
|
|
28
30
|
toolCallId: string;
|
|
@@ -34,7 +36,7 @@ type ToolRow = Component & {
|
|
|
34
36
|
result?: {
|
|
35
37
|
isError: boolean;
|
|
36
38
|
durationMs?: number;
|
|
37
|
-
details?: { diff?: string };
|
|
39
|
+
details?: { diff?: string; senjaCounts?: ReturnType<typeof countDiff> };
|
|
38
40
|
content?: { type: string; text?: string }[];
|
|
39
41
|
};
|
|
40
42
|
setExpanded(value: boolean): void;
|
|
@@ -498,6 +500,7 @@ export function createTranscriptView(
|
|
|
498
500
|
let animation: ReturnType<typeof setInterval> | undefined;
|
|
499
501
|
let pressed: Component | undefined;
|
|
500
502
|
let settle: ReturnType<typeof setTimeout> | undefined;
|
|
503
|
+
const editCounts = new WeakMap<object, ReturnType<typeof countDiff>>();
|
|
501
504
|
const calls = new WeakMap<
|
|
502
505
|
Component,
|
|
503
506
|
{
|
|
@@ -643,7 +646,13 @@ export function createTranscriptView(
|
|
|
643
646
|
}
|
|
644
647
|
const active = motion && tool.isPartial && !error && width > 1;
|
|
645
648
|
animating ||= active || !!elapsed;
|
|
646
|
-
const
|
|
649
|
+
const result = !tool.isPartial && !error ? tool.result : undefined;
|
|
650
|
+
let counts = name === "write" ? result?.details?.senjaCounts : undefined;
|
|
651
|
+
if (name === "edit" && result && typeof result.details?.diff === "string") {
|
|
652
|
+
counts = editCounts.get(result) ?? countDiff(result.details.diff);
|
|
653
|
+
editCounts.set(result, counts);
|
|
654
|
+
}
|
|
655
|
+
const labelKey = `${counts?.added}:${counts?.removed}:${revision}:${width}:${grouped}:${last}:${open}:${state}:${elapsed}:${duration ?? ""}:${active ? frame : 0}:${subject}`;
|
|
647
656
|
if (labelKey !== call.labelKey) {
|
|
648
657
|
call.labelKey = labelKey;
|
|
649
658
|
const available = rowWidth - branch.length - 2;
|
|
@@ -661,8 +670,17 @@ export function createTranscriptView(
|
|
|
661
670
|
const title = () =>
|
|
662
671
|
state === "preparing…" ? ` ${theme.fg("warning", "•")} ${theme.fg("toolTitle", summary(name))}` : heading();
|
|
663
672
|
const caption = grouped ? label : !name ? " •" : subject ? `${title()} ${label}` : title();
|
|
664
|
-
const
|
|
665
|
-
|
|
673
|
+
const stats =
|
|
674
|
+
counts &&
|
|
675
|
+
Number.isSafeInteger(counts.added) &&
|
|
676
|
+
counts.added >= 0 &&
|
|
677
|
+
Number.isSafeInteger(counts.removed) &&
|
|
678
|
+
counts.removed >= 0
|
|
679
|
+
? ` ${theme.fg("toolDiffAdded", `+${counts.added}`)} ${theme.fg("toolDiffRemoved", `-${counts.removed}`)}`
|
|
680
|
+
: "";
|
|
681
|
+
const tail = stats && visibleWidth(stats) + (grouped ? 1 : 9) <= available ? stats : status;
|
|
682
|
+
const script = truncateToWidth(caption, Math.max(0, available - visibleWidth(tail)), "…");
|
|
683
|
+
const line = `${script}${tail} ${icon}`;
|
|
666
684
|
call.label.setText(rowWidth <= 1 ? icon : theme.fg("dim", branch) + (active ? shimmer(line, frame) : line));
|
|
667
685
|
}
|
|
668
686
|
rows.push(call.row);
|