@cad0p/pi-napkin 0.6.1 → 0.6.2

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,13 +45,152 @@ 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 {
54
- const overview = n.overview();
174
+ // napkin >= 0.12.2 keeps upstream-identical defaults (maxRows 0 =
175
+ // unlimited, collapseDepth 1) so its equivalence oracle holds. The fork's
176
+ // historical context-protection defaults are restored here, still
177
+ // overridable via the vault config's `overview` section.
178
+ const configPath = path.join(n.vault.configPath, "config.json");
179
+ let overviewCfg: Record<string, unknown> = {};
180
+ try {
181
+ if (fs.existsSync(configPath)) {
182
+ overviewCfg = (JSON.parse(fs.readFileSync(configPath, "utf-8"))
183
+ .overview ?? {}) as Record<string, unknown>;
184
+ }
185
+ } catch {
186
+ // Corrupt/unreadable config — fall back to the defaults below.
187
+ }
188
+ const num = (v: unknown, fallback: number) =>
189
+ typeof v === "number" ? v : fallback;
190
+ const overview = n.overview({
191
+ maxRows: num(overviewCfg.maxRows, 100),
192
+ collapseDepth: num(overviewCfg.collapseDepth, 2),
193
+ });
55
194
  if (!overview) return null;
56
195
 
57
196
  let text = overview.context || "";
@@ -268,10 +407,13 @@ export default function (pi: ExtensionAPI) {
268
407
  details: { results: res.results, page, totalPages: res.totalPages },
269
408
  };
270
409
  },
410
+ renderCall(args, theme, context) {
411
+ return kbRenderCall(context, "kb_search", args.query, theme);
412
+ },
271
413
  renderResult(result, options, theme, context) {
272
414
  const t =
273
415
  (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
274
- t.setText(formatKbResult(result, options, theme, 15));
416
+ t.setText(kbRenderTimedResult(result, options, theme, 15, context));
275
417
  return t;
276
418
  },
277
419
  });
@@ -304,10 +446,20 @@ export default function (pi: ExtensionAPI) {
304
446
  details: { path: result.path },
305
447
  };
306
448
  },
449
+ renderCall(args, theme, context) {
450
+ return kbRenderCall(context, "kb_read", args.file, theme);
451
+ },
307
452
  renderResult(result, options, theme, context) {
308
453
  const t =
309
454
  (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
310
- t.setText(formatKbResult(result, options, theme, 10));
455
+ // Napkin resolves bare note names into subfolders, so surface the
456
+ // resolved path in the TUI (agent already sees it via details) —
457
+ // mirrors kb_outline's `File: <path>` line.
458
+ const path = (result.details as { path?: string } | undefined)?.path;
459
+ const header = path
460
+ ? `${theme.fg("muted", `File: ${path}`)}\n`
461
+ : undefined;
462
+ t.setText(kbRenderResult(result, options, theme, 10, header));
311
463
  return t;
312
464
  },
313
465
  });
@@ -353,10 +505,13 @@ export default function (pi: ExtensionAPI) {
353
505
  details: { path: absPath, headings },
354
506
  };
355
507
  },
508
+ renderCall(args, theme, context) {
509
+ return kbRenderCall(context, "kb_outline", args.file, theme);
510
+ },
356
511
  renderResult(result, options, theme, context) {
357
512
  const t =
358
513
  (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
359
- t.setText(formatKbResult(result, options, theme, 15));
514
+ t.setText(kbRenderResult(result, options, theme, 15));
360
515
  return t;
361
516
  },
362
517
  });
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",
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.2"
68
68
  },
69
69
  "peerDependencies": {
70
70
  "@earendil-works/pi-coding-agent": ">=0.74.0",