@cad0p/pi-napkin 0.6.1 → 0.6.2-20260809.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.
@@ -45,12 +45,135 @@ function formatKbResult(
45
45
  return text;
46
46
  }
47
47
 
48
+ // ── TUI render helpers (call line + timing) ───────────────────────
49
+ //
50
+ // renderCall mirrors the built-in read/grep tools (bold tool name +
51
+ // accent arg), so the user sees which note was queried/read. The
52
+ // timing line mirrors the built-in bash tool's TUI timing
53
+ // (pi dist/core/tools/bash.js): renderCall records a start timestamp
54
+ // when execution begins, and renderResult renders a live "Elapsed X"
55
+ // counter while the result streams and a final "Took X" once it
56
+ // lands. Like bash's, the timing line is TUI-only — never included in
57
+ // the model-visible content or details, so the agent doesn't see it.
58
+ // It is wired up only for kb_search (potentially heavy); the read /
59
+ // outline tools are trivial lookups and don't need it.
60
+
61
+ interface KbRenderState {
62
+ startedAt?: number;
63
+ endedAt?: number;
64
+ interval?: ReturnType<typeof setInterval>;
65
+ }
66
+
67
+ interface KbRenderContext {
68
+ state: KbRenderState;
69
+ executionStarted: boolean;
70
+ isPartial: boolean;
71
+ isError: boolean;
72
+ invalidate: () => void;
73
+ lastComponent?: unknown;
74
+ }
75
+
76
+ function kbFormatDuration(ms: number): string {
77
+ return `${(ms / 1000).toFixed(1)}s`;
78
+ }
79
+
80
+ function kbRecordStart(state: KbRenderState, executionStarted: boolean): void {
81
+ if (executionStarted && state.startedAt === undefined) {
82
+ state.startedAt = Date.now();
83
+ state.endedAt = undefined;
84
+ }
85
+ }
86
+
87
+ /** Renders the tool call line (`kb_read <file>`), like native read's `read <path>`. */
88
+ function kbRenderCall(
89
+ context: Pick<
90
+ KbRenderContext,
91
+ "state" | "executionStarted" | "lastComponent"
92
+ >,
93
+ label: string,
94
+ arg: string | undefined,
95
+ theme: Theme,
96
+ ): Text {
97
+ kbRecordStart(context.state, context.executionStarted);
98
+ const text =
99
+ (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
100
+ const argDisplay = arg
101
+ ? theme.fg("accent", arg)
102
+ : theme.fg("toolOutput", "...");
103
+ text.setText(`${theme.fg("toolTitle", theme.bold(label))} ${argDisplay}`);
104
+ return text;
105
+ }
106
+
107
+ /**
108
+ * The trailing TUI-only timing line. While the result is still
109
+ * streaming it re-invalidates once a second so the counter ticks.
110
+ */
111
+ function kbTimingLine(
112
+ context: Pick<
113
+ KbRenderContext,
114
+ "state" | "isPartial" | "isError" | "invalidate"
115
+ >,
116
+ theme: Theme,
117
+ ): string {
118
+ const state = context.state;
119
+ if (state.startedAt === undefined) return "";
120
+ if (context.isPartial && !state.interval) {
121
+ state.interval = setInterval(() => context.invalidate(), 1000);
122
+ }
123
+ if (!context.isPartial || context.isError) {
124
+ state.endedAt ??= Date.now();
125
+ if (state.interval) {
126
+ clearInterval(state.interval);
127
+ state.interval = undefined;
128
+ }
129
+ }
130
+ const label = context.isPartial ? "Elapsed" : "Took";
131
+ const endTime = state.endedAt ?? Date.now();
132
+ return `\n${theme.fg("muted", `${label} ${kbFormatDuration(endTime - state.startedAt)}`)}`;
133
+ }
134
+
135
+ /**
136
+ * Composes the result body (collapsed/expanded) with an optional
137
+ * header line (e.g. kb_read's resolved path). No timing — used by
138
+ * the trivial read/outline tools.
139
+ */
140
+ function kbRenderResult(
141
+ result: AgentToolResult<unknown>,
142
+ options: ToolRenderResultOptions,
143
+ theme: Theme,
144
+ maxCollapsedLines: number,
145
+ header?: string,
146
+ ): string {
147
+ const body = formatKbResult(result, options, theme, maxCollapsedLines);
148
+ let text = header ?? "";
149
+ if (body) text = text ? `${text}${body}` : body;
150
+ return text;
151
+ }
152
+
153
+ /** kbRenderResult + the TUI-only timing line (kb_search only). */
154
+ function kbRenderTimedResult(
155
+ result: AgentToolResult<unknown>,
156
+ options: ToolRenderResultOptions,
157
+ theme: Theme,
158
+ maxCollapsedLines: number,
159
+ context: KbRenderContext,
160
+ header?: string,
161
+ ): string {
162
+ let text = kbRenderResult(result, options, theme, maxCollapsedLines, header);
163
+ const timing = kbTimingLine(context, theme);
164
+ if (timing) text = text ? `${text}${timing}` : timing.replace(/^\n/, "");
165
+ return text;
166
+ }
167
+
48
168
  function getNapkin(cwd: string): Napkin {
49
169
  return new Napkin(cwd);
50
170
  }
51
171
 
52
172
  function getOverview(n: Napkin): string | null {
53
173
  try {
174
+ // napkin >= 0.12.3 ships the fork's defaults (collapseDepth 2, maxRows
175
+ // 100) in DEFAULT_CONFIG, so the extension must not diverge from napkin —
176
+ // it relies on napkin's defaults, overridable via the vault config.
54
177
  const overview = n.overview();
55
178
  if (!overview) return null;
56
179
 
@@ -268,10 +391,13 @@ export default function (pi: ExtensionAPI) {
268
391
  details: { results: res.results, page, totalPages: res.totalPages },
269
392
  };
270
393
  },
394
+ renderCall(args, theme, context) {
395
+ return kbRenderCall(context, "kb_search", args.query, theme);
396
+ },
271
397
  renderResult(result, options, theme, context) {
272
398
  const t =
273
399
  (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
274
- t.setText(formatKbResult(result, options, theme, 15));
400
+ t.setText(kbRenderTimedResult(result, options, theme, 15, context));
275
401
  return t;
276
402
  },
277
403
  });
@@ -304,10 +430,20 @@ export default function (pi: ExtensionAPI) {
304
430
  details: { path: result.path },
305
431
  };
306
432
  },
433
+ renderCall(args, theme, context) {
434
+ return kbRenderCall(context, "kb_read", args.file, theme);
435
+ },
307
436
  renderResult(result, options, theme, context) {
308
437
  const t =
309
438
  (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
310
- t.setText(formatKbResult(result, options, theme, 10));
439
+ // Napkin resolves bare note names into subfolders, so surface the
440
+ // resolved path in the TUI (agent already sees it via details) —
441
+ // mirrors kb_outline's `File: <path>` line.
442
+ const path = (result.details as { path?: string } | undefined)?.path;
443
+ const header = path
444
+ ? `${theme.fg("muted", `File: ${path}`)}\n`
445
+ : undefined;
446
+ t.setText(kbRenderResult(result, options, theme, 10, header));
311
447
  return t;
312
448
  },
313
449
  });
@@ -353,10 +489,13 @@ export default function (pi: ExtensionAPI) {
353
489
  details: { path: absPath, headings },
354
490
  };
355
491
  },
492
+ renderCall(args, theme, context) {
493
+ return kbRenderCall(context, "kb_outline", args.file, theme);
494
+ },
356
495
  renderResult(result, options, theme, context) {
357
496
  const t =
358
497
  (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
359
- t.setText(formatKbResult(result, options, theme, 15));
498
+ t.setText(kbRenderResult(result, options, theme, 15));
360
499
  return t;
361
500
  },
362
501
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cad0p/pi-napkin",
3
- "version": "0.6.1",
3
+ "version": "0.6.2-20260809.0",
4
4
  "description": "📜 Napkin integration for pi — vault context, knowledge tools, and automatic distillation with git-worktree concurrency safety",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -64,7 +64,7 @@
64
64
  "vitest": "^2.1.0"
65
65
  },
66
66
  "dependencies": {
67
- "@cad0p/napkin": "^0.12.1"
67
+ "@cad0p/napkin": "^0.12.3"
68
68
  },
69
69
  "peerDependencies": {
70
70
  "@earendil-works/pi-coding-agent": ">=0.74.0",