@trim21/personal-pi-extensions 0.1.528 → 0.1.530

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/spawn-agent.ts +30 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trim21/personal-pi-extensions",
3
- "version": "0.1.528",
3
+ "version": "0.1.530",
4
4
  "type": "module",
5
5
  "description": "Custom pi coding-agent extensions: bwrap sandbox, workspace guard, opencode edit, and more",
6
6
  "keywords": [
@@ -15,9 +15,14 @@
15
15
  * `text: <content>` lines for completed text blocks, keeping the last
16
16
  * `MAX_PROGRESS_LINES` lines. Consecutive tool calls are merged into a
17
17
  * single `tool:` line (`read x 2, glob`) and over-long line content is
18
- * folded to the first/last 7 chars joined by `…`, so a burst of tool calls
18
+ * folded to the first/last 9 chars joined by `…`, so a burst of tool calls
19
19
  * or a long text block does not flood the window; any text block starts a
20
- * new line.
20
+ * new line. Line content is sanitized first: markdown marker characters are
21
+ * stripped and whitespace (including newlines) is collapsed to single spaces,
22
+ * so one log entry is always exactly one rendered line. The final line is
23
+ * always the subagent name as a code span (`` `scout` ``), followed by the
24
+ * live usage stats when there are any; it rides outside the rolling window so
25
+ * it is never trimmed.
21
26
  *
22
27
  * Security default: without an explicit `tools:` in the frontmatter, the
23
28
  * subagent only gets read-only tools (read/grep/find/ls) — no bash/write/edit.
@@ -65,6 +70,11 @@ const DEFAULT_TOOLS = ["read", "grep", "find", "ls"];
65
70
  const MAX_PROGRESS_LINES = 5;
66
71
  /** Progress line content (without the `tool:` / `text:` prefix) is capped at 21 chars; longer text is folded to the first/last 9 chars joined by ` … `. */
67
72
  const MAX_PROGRESS_CHARS_PER_LINE = 21;
73
+ /**
74
+ * 进度内容会被 pi 按 markdown 渲染,这些标记字符会改变显示效果(代码块、粗体、
75
+ * 链接、标题等),因此在进日志前统一删掉。
76
+ */
77
+ const PROGRESS_MARKDOWN_MARKERS_RE = /[`*_~[\]<>#|]/g;
68
78
  /** 错误消息里 stderr 的展示上限。 */
69
79
  const MAX_STDERR_ERROR_BYTES = 4 * 1024;
70
80
  /** 全局默认配置:~/.pi/agent/spawn-agent.json,字段可被 frontmatter 覆盖。 */
@@ -182,6 +192,15 @@ function foldProgressLine(text: string): string {
182
192
  return `${text.slice(0, keep)} … ${text.slice(-keep)}`;
183
193
  }
184
194
 
195
+ /**
196
+ * 进度行是「单行内容 + markdown 渲染」:内容里的换行会打乱按行滚动的窗口,
197
+ * markdown 标记会改变渲染效果。先删掉标记字符,再把换行/制表符/连续空格折成
198
+ * 单个空格并去掉首尾空白,保证一条日志恒为一行。
199
+ */
200
+ function sanitizeProgressLine(text: string): string {
201
+ return text.replaceAll(PROGRESS_MARKDOWN_MARKERS_RE, "").replaceAll(/\s+/g, " ").trim();
202
+ }
203
+
185
204
  function formatTokens(count: number): string {
186
205
  if (count < 1000) return count.toString();
187
206
  if (count < 10_000) return `${(count / 1000).toFixed(1)}k`;
@@ -371,7 +390,8 @@ export async function runAgent(
371
390
  toolLine = undefined;
372
391
  };
373
392
 
374
- const appendToolLine = (name: string) => {
393
+ const appendToolLine = (rawName: string) => {
394
+ const name = sanitizeProgressLine(rawName);
375
395
  const firstInBatch = toolLine === undefined;
376
396
  if (toolLine === undefined) {
377
397
  toolLineSegments = [];
@@ -396,12 +416,14 @@ export async function runAgent(
396
416
  };
397
417
 
398
418
  const emitUpdate = () => {
399
- // Usage line rides on the last row so the TUI always shows live token
400
- // cost; it lives outside the rolling window so it is never trimmed.
419
+ // 最后一行固定是「子代理名 + 运行中统计」:名字用 code span 标出,进度流里
420
+ // 一眼能看出属于哪个 subagent;usage 与它同行,TUI 始终能看到实时 token 开销。
421
+ // 这行位于滚动窗口之外,因此永远不会被挤掉。
401
422
  const usageLine = formatUsageStats(result.usage, result.model);
402
- const lines = usageLine ? [...logLines, usageLine] : logLines;
423
+ const name = sanitizeProgressLine(result.agent);
424
+ const footer = usageLine ? `\`${name}\` ${usageLine}` : `\`${name}\``;
403
425
  onUpdate?.({
404
- content: [{ type: "text", text: lines.join("\n") || "(running...)" }],
426
+ content: [{ type: "text", text: [...logLines, footer].join("\n") }],
405
427
  details: { ...result },
406
428
  });
407
429
  };
@@ -413,7 +435,7 @@ export async function runAgent(
413
435
  // `text:` log line. Deltas/thinking are intentionally not logged.
414
436
  const delta = event.assistantMessageEvent;
415
437
  if (delta.type === "text_end") {
416
- pushLogLine(`text: ${foldProgressLine(delta.content)}`);
438
+ pushLogLine(`text: ${foldProgressLine(sanitizeProgressLine(delta.content))}`);
417
439
  emitUpdate();
418
440
  }
419
441