@aaroncql/pim-agent 0.1.0 → 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.
@@ -2,8 +2,12 @@ import type { AgentSessionEvent } from "@earendil-works/pi-coding-agent";
2
2
  import { GrammyError, type Api } from "grammy";
3
3
  import { basename } from "node:path";
4
4
 
5
+ import type { ApplyEntry } from "../extensions/apply-patch/executor";
5
6
  import type { SubagentDetails } from "../extensions/subagent/subagent";
6
7
  import type { TodoInput } from "../extensions/todo/schema";
8
+ import type { ToolDiff } from "../shared/DiffLines";
9
+ import { DiffView, type DiffStats } from "../shared/DiffView";
10
+ import { type PatchOp, PatchSummary } from "../shared/PatchSummary";
7
11
  import type { LogsMode } from "./Config";
8
12
  import { Markdown } from "./Markdown";
9
13
  import type { Session, SessionId } from "./Session";
@@ -15,15 +19,30 @@ type TurnState = TurnEndState | "running";
15
19
  type TrackerEntry = {
16
20
  readonly key: string;
17
21
  readonly kind: "tool" | "todo" | "thinking" | "narration";
18
- readonly emoji: string;
22
+ emoji: string;
19
23
  label: string;
20
24
  state: "running" | "ok" | "error";
25
+ // Plaintext "+4/-3" appended after the label once the tool finishes.
26
+ stats?: string;
21
27
  };
22
28
 
29
+ type ApplyOp = {
30
+ readonly emoji: string;
31
+ readonly text: string;
32
+ };
33
+
34
+ const EDIT_EMOJI = "✏️";
35
+ const DELETE_EMOJI = "🗑️";
36
+ const ARROW = "➝";
37
+
38
+ // Keys an apply_patch tool call may carry its patch text under (canonical first).
39
+ const PATCH_TEXT_KEYS = ["input", "patch", "patchText", "patch_text"] as const;
40
+
23
41
  const TOOL_EMOJI: Record<string, string> = {
24
42
  read: "📄",
25
- edit: "✏️",
26
- write: "✏️",
43
+ edit: EDIT_EMOJI,
44
+ write: EDIT_EMOJI,
45
+ apply_patch: EDIT_EMOJI,
27
46
  bash: "⚡️",
28
47
  grep: "🔎",
29
48
  glob: "🔎",
@@ -130,6 +149,9 @@ export class Renderer {
130
149
  return;
131
150
  }
132
151
  this.updateSubagentLabel(event.toolCallId, event.toolName, event.result);
152
+ if (!event.isError) {
153
+ this.applyDiffStats(event.toolCallId, event.toolName, event.result);
154
+ }
133
155
  const idx = this.toolIndex.get(event.toolCallId);
134
156
  if (idx !== undefined) {
135
157
  this.entries[idx]!.state = event.isError ? "error" : "ok";
@@ -179,6 +201,21 @@ export class Renderer {
179
201
  this.scheduleEdit();
180
202
  return;
181
203
  }
204
+ if (name === "apply_patch") {
205
+ const { emoji, label } = Renderer.buildApplyEntry(
206
+ Renderer.applyOpsFromArgs(args)
207
+ );
208
+ this.entries.push({
209
+ key: toolCallId,
210
+ kind: "tool",
211
+ emoji,
212
+ label,
213
+ state: "running",
214
+ });
215
+ this.toolIndex.set(toolCallId, this.entries.length - 1);
216
+ this.scheduleEdit();
217
+ return;
218
+ }
182
219
  const emoji = TOOL_EMOJI[name] ?? "⚙️";
183
220
  const label = Renderer.toolLabel(toolName, args);
184
221
  if (name === "subagent") {
@@ -234,6 +271,42 @@ export class Renderer {
234
271
  this.scheduleEdit();
235
272
  }
236
273
 
274
+ private applyDiffStats(
275
+ toolCallId: string,
276
+ toolName: string,
277
+ result: unknown
278
+ ): void {
279
+ const idx = this.toolIndex.get(toolCallId);
280
+ if (idx === undefined) {
281
+ return;
282
+ }
283
+ const name = toolName.toLowerCase();
284
+ const details = (result as { readonly details?: unknown } | null)?.details;
285
+ if (name === "edit" || name === "write") {
286
+ const diff = (details as { readonly diff?: ToolDiff } | undefined)?.diff;
287
+ const stats = Renderer.formatPlainStats(DiffView.countStats(diff));
288
+ if (stats) {
289
+ this.entries[idx]!.stats = stats;
290
+ this.scheduleEdit();
291
+ }
292
+ return;
293
+ }
294
+ if (name === "apply_patch") {
295
+ const entries = (
296
+ details as { readonly entries?: readonly ApplyEntry[] } | undefined
297
+ )?.entries;
298
+ if (!entries) {
299
+ return;
300
+ }
301
+ const built = Renderer.buildApplyEntry(
302
+ Renderer.applyOpsFromEntries(entries)
303
+ );
304
+ this.entries[idx]!.emoji = built.emoji;
305
+ this.entries[idx]!.label = built.label;
306
+ this.scheduleEdit();
307
+ }
308
+ }
309
+
237
310
  private flushThinking(): void {
238
311
  if (this.logsMode !== "verbose") {
239
312
  this.thinking = "";
@@ -370,7 +443,8 @@ export class Renderer {
370
443
  } else if (state === "running" && isLastEntry) {
371
444
  suffix = " 🟡";
372
445
  }
373
- pieces.push(`${entry.emoji} ${entry.label}${suffix}`);
446
+ const stats = entry.stats ? ` ${entry.stats}` : "";
447
+ pieces.push(`${entry.emoji} ${entry.label}${stats}${suffix}`);
374
448
  }
375
449
  const next = visible[i + 1];
376
450
  if (next) {
@@ -490,6 +564,118 @@ export class Renderer {
490
564
  }
491
565
  }
492
566
 
567
+ private static buildApplyEntry(ops: readonly ApplyOp[]): {
568
+ readonly emoji: string;
569
+ readonly label: string;
570
+ } {
571
+ const [first, ...rest] = ops;
572
+ if (!first) {
573
+ return { emoji: EDIT_EMOJI, label: "" };
574
+ }
575
+ const label = [
576
+ first.text,
577
+ ...rest.map((op) => `${op.emoji} ${op.text}`),
578
+ ].join("\n");
579
+ return { emoji: first.emoji, label };
580
+ }
581
+
582
+ private static applyOpsFromArgs(args: unknown): readonly ApplyOp[] {
583
+ const text = Renderer.patchTextFromArgs(args);
584
+ if (!text) {
585
+ return [];
586
+ }
587
+ return PatchSummary.fromText(text).map((op) => Renderer.opFromSummary(op));
588
+ }
589
+
590
+ private static applyOpsFromEntries(
591
+ entries: readonly ApplyEntry[]
592
+ ): readonly ApplyOp[] {
593
+ return entries
594
+ .filter(
595
+ (entry) => !(entry.action.kind === "update" && entry.diff === undefined)
596
+ )
597
+ .map((entry) => Renderer.opFromEntry(entry));
598
+ }
599
+
600
+ private static opFromSummary(op: PatchOp): ApplyOp {
601
+ const isMove = op.movePath !== undefined && op.movePath !== op.path;
602
+ return Renderer.applyOp({
603
+ kind: isMove ? "move" : op.kind,
604
+ path: op.path,
605
+ movePath: op.movePath,
606
+ });
607
+ }
608
+
609
+ private static opFromEntry(entry: ApplyEntry): ApplyOp {
610
+ return Renderer.applyOp({
611
+ kind: entry.action.kind,
612
+ path: entry.action.path,
613
+ movePath: entry.action.movePath,
614
+ stats: Renderer.formatPlainStats(DiffView.countStats(entry.diff)),
615
+ });
616
+ }
617
+
618
+ private static applyOp(params: {
619
+ readonly kind: "add" | "delete" | "move" | "update";
620
+ readonly path: string;
621
+ readonly movePath?: string;
622
+ readonly stats?: string;
623
+ }): ApplyOp {
624
+ const suffix = params.stats ? ` ${params.stats}` : "";
625
+ if (params.kind === "delete") {
626
+ return {
627
+ emoji: DELETE_EMOJI,
628
+ text: `${Renderer.codeName(params.path)}${suffix}`,
629
+ };
630
+ }
631
+ if (params.kind === "move") {
632
+ return {
633
+ emoji: EDIT_EMOJI,
634
+ text: `${Renderer.moveText(params.path, params.movePath ?? params.path)}${suffix}`,
635
+ };
636
+ }
637
+ return {
638
+ emoji: EDIT_EMOJI,
639
+ text: `${Renderer.codeName(params.path)}${suffix}`,
640
+ };
641
+ }
642
+
643
+ private static moveText(from: string, to: string): string {
644
+ return `${Renderer.codeName(from)} ${ARROW} ${Renderer.codeName(to)}`;
645
+ }
646
+
647
+ private static codeName(path: string): string {
648
+ return `<code>${Markdown.escape(basename(path))}</code>`;
649
+ }
650
+
651
+ private static patchTextFromArgs(args: unknown): string | undefined {
652
+ if (typeof args === "string") {
653
+ return args;
654
+ }
655
+ if (!args || typeof args !== "object") {
656
+ return undefined;
657
+ }
658
+ const record = args as Record<string, unknown>;
659
+ for (const key of PATCH_TEXT_KEYS) {
660
+ const value = record[key];
661
+ if (typeof value === "string" && value) {
662
+ return value;
663
+ }
664
+ }
665
+ return undefined;
666
+ }
667
+
668
+ private static formatPlainStats(stats: DiffStats): string {
669
+ const parts: string[] = [];
670
+ if (stats.added > 0) {
671
+ parts.push(`+${stats.added}`);
672
+ }
673
+ if (stats.removed > 0) {
674
+ parts.push(`-${stats.removed}`);
675
+ }
676
+ return parts.join("/");
677
+ }
678
+
493
679
  private static toolLabel(toolName: string, args: unknown): string {
494
680
  const obj =
495
681
  args && typeof args === "object" ? (args as Record<string, unknown>) : {};