pi-cc-extensions 1.0.1 → 1.0.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.
Files changed (36) hide show
  1. package/README.en.md +35 -35
  2. package/README.md +3 -3
  3. package/extensions/config/config.ts +5 -0
  4. package/extensions/config/panel.ts +22 -0
  5. package/extensions/feature/compact-thinking.ts +1 -1
  6. package/extensions/feature/context.ts +131 -28
  7. package/extensions/feature/reference/index.ts +2 -0
  8. package/extensions/feature/reference/subagent.ts +4 -1
  9. package/extensions/feature/shell/footer.ts +10 -6
  10. package/extensions/feature/shell/token-rate.ts +15 -3
  11. package/extensions/feature/shell/working-message.ts +2 -21
  12. package/extensions/renderer/compact-mode.ts +105 -55
  13. package/extensions/renderer/default-mode.ts +23 -463
  14. package/extensions/renderer/index.ts +6 -3
  15. package/extensions/renderer/markdown-enhance.ts +63 -22
  16. package/extensions/renderer/mouse/hover.ts +1 -1
  17. package/extensions/renderer/mouse/interaction.ts +2 -2
  18. package/extensions/renderer/mouse/packets.ts +1 -1
  19. package/extensions/renderer/mouse/sticky-prompt.ts +61 -9
  20. package/extensions/renderer/tool/ask-user-question.ts +1 -1
  21. package/extensions/renderer/tool/ccstyle-tool.ts +467 -0
  22. package/extensions/renderer/tool/codemode.ts +1 -1
  23. package/extensions/renderer/tool/diff/diff-edit-render.ts +1 -1
  24. package/extensions/renderer/tool/diff/diff-limits.ts +1 -1
  25. package/extensions/renderer/tool/diff/diff-write-render.ts +1 -1
  26. package/extensions/renderer/tool/diff/line-width-safety.ts +1 -1
  27. package/extensions/renderer/tool/diff/write-execution.ts +2 -2
  28. package/extensions/renderer/tool/grouping.ts +53 -34
  29. package/extensions/renderer/tool/message-display.ts +1 -1
  30. package/extensions/renderer/tool/names.ts +41 -1
  31. package/extensions/renderer/tool/result.ts +2 -862
  32. package/extensions/renderer/tool/tool-io.ts +735 -0
  33. package/extensions/renderer/tool/tool-state.ts +138 -0
  34. package/package.json +1 -1
  35. package/themes/cc-light.json +1 -1
  36. package/extensions/renderer/tool/show-more-hint.ts +0 -48
package/README.en.md CHANGED
@@ -45,52 +45,53 @@ Renderer snapshots for `on` / `compact` modes: [default](./docs/tool-render-exam
45
45
  ```jsonc
46
46
  {
47
47
  // style
48
- "mode": "on", // on / compact / off
49
- "excludeRenderers": [], // tools keeping the native renderer; Agent always keeps its dedicated renderer
48
+ "mode": "on", // on / compact / off
49
+ "excludeRenderers": [], // tools keeping the native renderer; Agent always keeps its dedicated renderer
50
50
 
51
51
  // features
52
- "enableSessionReference": true, // @ session references
53
- "enableSubagentAutocomplete": true, // @subagent:[name] completion and delegation hints
54
- "enableContextCommand": true, // /context usage check
55
- "enableAgentSummary": true, // per-turn tool summary
56
- "enableWorkingMessage": true, // Working... bottom token/elapsed
57
- "enableAliases": true, // /clear, /exit aliases
52
+ "enableSessionReference": true, // @ session references
53
+ "enableSubagentAutocomplete": true, // @subagent:[name] completion and delegation hints
54
+ "enableContextCommand": true, // /context usage check
55
+ "enableAgentSummary": true, // per-turn tool summary
56
+ "enableWorkingMessage": true, // Working... bottom token/elapsed
57
+ "enableAliases": true, // /clear, /exit aliases
58
58
 
59
59
  // ui
60
- "expandedInputMaxLines": 5, // expanded tool Input lines; overflow shows a footer hint
61
- "expandedOutputMaxLines": 10, // expanded tool Output lines; overflow shows a footer hint
62
- "expandedPreviewMaxLines": 40, // max lines for expanded TaskList bodies (expanded diffs always show all)
60
+ "expandedInputMaxLines": 5, // expanded tool Input lines; overflow shows a footer hint
61
+ "expandedOutputMaxLines": 10, // expanded tool Output lines; overflow shows a footer hint
62
+ "expandedPreviewMaxLines": 40, // max lines for expanded TaskList bodies (expanded diffs always show all)
63
63
  "expandedCardBackground": "userMessageBg", // expanded card background slot: userMessageBg (same as user prompts) / toolPendingBg (neutral) etc.
64
- "toolLabelClip": 0, // tool summary path/command clip length; 0 = fit width
65
- "showStartupHeader": true, // startup header (logo + tips) toggle
66
- "stickyUserMessage": true, // fullscreen: pin a user message's first line once it scrolls off the top edge, until the next one takes over
64
+ "toolLabelClip": 0, // tool summary path/command clip length; 0 = fit width
65
+ "showStartupHeader": true, // startup header (logo + tips) toggle
66
+ "stickyUserMessage": true, // fullscreen: pin a user message's first line once it scrolls off the top edge, until the next one takes over
67
67
 
68
68
  // diff
69
- "diffViewMode": "auto", // layout: auto / split / unified
70
- "diffIndicatorMode": "bars", // change indicators: bars / classic / none
71
- "diffSplitMinWidth": 120, // min terminal width for side-by-side columns
72
- "editDiffCollapsedLines": 24, // edit collapse lines; 0 = change-count summary only
73
- "writeDiffCollapsedLines": 0, // write collapse lines; 0 = creation summary only
74
- "diffWordWrap": true, // wrap long diff lines
69
+ "diffViewMode": "auto", // layout: auto / split / unified
70
+ "diffIndicatorMode": "bars", // change indicators: bars / classic / none
71
+ "diffSplitMinWidth": 120, // min terminal width for side-by-side columns
72
+ "editDiffCollapsedLines": 24, // edit collapse lines; 0 = change-count summary only
73
+ "writeDiffCollapsedLines": 0, // write collapse lines; 0 = creation summary only
74
+ "diffWordWrap": true, // wrap long diff lines
75
75
 
76
76
  // thinking
77
77
  "useSummaryTitlesAsThinkingTitle": true, // use latest summary as thinking title
78
- "previewLines": 3, // preview lines; 0 hides
79
- "animationIntervalMs": 90, // title animation interval (ms)
80
- "dimThinkingText": false, // dim thinking body text
78
+ "previewLines": 3, // preview lines; 0 hides
79
+ "animationIntervalMs": 90, // title animation interval (ms)
80
+ "dimThinkingText": false, // dim thinking body text
81
81
 
82
82
  // footer
83
- "enableCustomFooter": true, // custom status bar
84
- "footerTokenRate": true, // output rate chip; false hides it
85
- "footerTokenRateMode": "average", // average: whole-prompt avg; live: trailing 1s rate
86
- "footerCurrency": "USD", // display currency for the footer cost
87
- "footerCurrencyRate": null, // display units per 1 cost unit; null changes the symbol only
88
- "footerNerdIcons": true, // Nerd Font glyphs for git/cache; false = plain text
89
- "footerGitStatsMode": "working", // working (default): uncommitted; branch: cumulative net changes
90
- "footerHiddenKeys": [], // hidden plugin chip keys
91
- "footerLine1Keys": ["pi-usage"], // line1 plugin chip order; pi-usage is shown by default, data from @narumitw/pi-usage
92
- "footerLine2Keys": [], // line2 plugin chip order (after cwd/git)
93
- "footerLine3Keys": [], // line3 overflow slot; painted only when a chip is visible
83
+ "enableCustomFooter": true, // custom status bar
84
+ "footerTokenRate": true, // output rate chip; false hides it
85
+ "footerTokenRateMode": "average", // average: whole-prompt avg; live: trailing 1s rate
86
+ "footerTokenRateTtft": true, // append TTFT to the rate chip; false shows tok/s only
87
+ "footerCurrency": "USD", // display currency for the footer cost
88
+ "footerCurrencyRate": null, // display units per 1 cost unit; null changes the symbol only
89
+ "footerNerdIcons": true, // Nerd Font glyphs for git/cache; false = plain text
90
+ "footerGitStatsMode": "working", // working (default): uncommitted; branch: cumulative net changes
91
+ "footerHiddenKeys": [], // hidden plugin chip keys
92
+ "footerLine1Keys": ["pi-usage"], // line1 plugin chip order; pi-usage is shown by default, data from @narumitw/pi-usage
93
+ "footerLine2Keys": [], // line2 plugin chip order (after cwd/git)
94
+ "footerLine3Keys": [], // line3 overflow slot; painted only when a chip is visible
94
95
  }
95
96
  ```
96
97
 
@@ -116,7 +117,6 @@ pi -e . # one run, does not edit settings.json
116
117
 
117
118
  | Extension | Purpose |
118
119
  | ------------------------ | ----------------------------------------------------------- |
119
- | `npm:@ff-labs/pi-fff` | FFF-powered fuzzy file and content search (fffind / ffgrep) |
120
120
  | `npm:pi-web-access` | Web search, URL fetching, GitHub cloning, PDF/video parsing |
121
121
  | `npm:@narumitw/pi-usage` | Current-account usage for Codex / Copilot / OpenRouter |
122
122
 
package/README.md CHANGED
@@ -81,8 +81,9 @@ pi install git:github.com/minuque/pi-cc-extensions
81
81
 
82
82
  // footer
83
83
  "enableCustomFooter": true, // 自定义状态栏
84
- "footerTokenRate": true, // 输出速率芯片;false 隐藏
85
- "footerTokenRateMode": "average", // average:整段指令平均(avg);live:最近 1 秒瞬时(live)
84
+ "footerTokenRate": true, // 输出速率芯片;false 隐藏
85
+ "footerTokenRateMode": "average", // average:整段指令平均(avg);live:最近 1 秒瞬时(live)
86
+ "footerTokenRateTtft": true, // 速率芯片是否带 TTFT;false 只显示 tok/s
86
87
  "footerCurrency": "USD", // 底栏费用的显示币种
87
88
  "footerCurrencyRate": null, // 1 单位费用换成多少显示币种;null 只换符号、不乘倍率
88
89
  "footerNerdIcons": true, // git/缓存用 Nerd Font 图标;false 为纯文本
@@ -116,7 +117,6 @@ pi -e . # 只跑一次,不改 settings.json
116
117
 
117
118
  | 扩展 | 用途 |
118
119
  | ---------------------------------------- | ------------------------------------------------ |
119
- | `npm:@ff-labs/pi-fff` | 模糊文件与内容检索(fffind / ffgrep) |
120
120
  | `npm:pi-web-access` | 网页搜索、URL 抓取、GitHub 克隆、PDF/视频解析 |
121
121
  | `npm:@narumitw/pi-usage` | 查看当前账号用量(Codex / Copilot / OpenRouter) |
122
122
 
@@ -79,6 +79,8 @@ export type Config = {
79
79
  footerTokenRate: boolean;
80
80
  /** average:整段指令墙钟平均;live:最近 1 秒瞬时速率,空档回退平均。 */
81
81
  footerTokenRateMode: FooterTokenRateMode;
82
+ /** 速率芯片是否带 TTFT。关则只显示速率。 */
83
+ footerTokenRateTtft: boolean;
82
84
  /** ISO 4217 显示币种。USD 时不换算。 */
83
85
  footerCurrency: string;
84
86
  /** 1 单位费用对应的显示币种数量。null 只换符号、不乘倍率。 */
@@ -171,6 +173,7 @@ export const DEFAULT_CONFIG: Config = {
171
173
  enableCustomFooter: true,
172
174
  footerTokenRate: true,
173
175
  footerTokenRateMode: "average",
176
+ footerTokenRateTtft: true,
174
177
  footerCurrency: "USD",
175
178
  footerCurrencyRate: null,
176
179
  footerNerdIcons: true,
@@ -307,6 +310,7 @@ export function normalizeConfig(input: unknown): Config {
307
310
  FOOTER_TOKEN_RATE_MODES,
308
311
  DEFAULT_CONFIG.footerTokenRateMode,
309
312
  ),
313
+ footerTokenRateTtft: source.footerTokenRateTtft !== false,
310
314
  footerCurrency: normalizeCurrency(source.footerCurrency),
311
315
  footerCurrencyRate: normalizeFooterCurrencyRate(source.footerCurrencyRate),
312
316
  footerNerdIcons: source.footerNerdIcons !== false,
@@ -372,6 +376,7 @@ export function formatConfigStatus(source: Config = config): string {
372
376
  `footer=${source.enableCustomFooter ? "on" : "off"}`,
373
377
  `footerTokenRate=${source.footerTokenRate ? "on" : "off"}`,
374
378
  `footerTokenRateMode=${source.footerTokenRateMode}`,
379
+ `footerTokenRateTtft=${source.footerTokenRateTtft ? "on" : "off"}`,
375
380
  `footerCurrency=${source.footerCurrency}`,
376
381
  `footerRate=${source.footerCurrencyRate ?? "-"}`,
377
382
  `footerIcons=${source.footerNerdIcons ? "nerd" : "plain"}`,
@@ -101,6 +101,12 @@ function footerTokenRateModeDescription(mode: FooterTokenRateMode): string {
101
101
  : "Average over the whole user prompt (avg), including tool waits. Stays until the next prompt.";
102
102
  }
103
103
 
104
+ function footerTokenRateTtftDescription(enabled: boolean): string {
105
+ return enabled
106
+ ? "Append time to first token, e.g. (TTFT: 812ms) or (TTFT: 1.2s). Turn off to show the rate only."
107
+ : "Time to first token hidden. The rate chip shows tok/s only.";
108
+ }
109
+
104
110
  function footerGitStatsDescription(mode: FooterGitStatsMode): string {
105
111
  return mode === "branch"
106
112
  ? "Net changes since the merge base, including uncommitted edits. Base: origin/HEAD, then local main/master. Untracked files excluded; unavailable stats hidden."
@@ -758,6 +764,13 @@ export async function showCcstylePanel(
758
764
  currentValue: config.footerTokenRateMode,
759
765
  values: FOOTER_TOKEN_RATE_MODES,
760
766
  };
767
+ const footerTokenRateTtftSetting = {
768
+ id: "footerTokenRateTtft",
769
+ label: "Token rate TTFT",
770
+ description: footerTokenRateTtftDescription(config.footerTokenRateTtft),
771
+ currentValue: config.footerTokenRateTtft ? "on" : "off",
772
+ values: ["on", "off"],
773
+ };
761
774
  const pluginChipsSetting = {
762
775
  id: "footerPluginChips",
763
776
  label: "Plugin chips",
@@ -847,6 +860,14 @@ export async function showCcstylePanel(
847
860
  ctx.ui.notify(`Updated ${id}: ${value}`, "info");
848
861
  return;
849
862
  }
863
+ if (id === "footerTokenRateTtft") {
864
+ const enabled = value === "on";
865
+ updateConfig({ footerTokenRateTtft: enabled });
866
+ footerTokenRateTtftSetting.currentValue = enabled ? "on" : "off";
867
+ footerTokenRateTtftSetting.description = footerTokenRateTtftDescription(enabled);
868
+ ctx.ui.notify(`Updated ${id}: ${value}`, "info");
869
+ return;
870
+ }
850
871
  if (id === "footerNerdIcons") {
851
872
  const enabled = value === "on";
852
873
  updateConfig({ footerNerdIcons: enabled });
@@ -1067,6 +1088,7 @@ export async function showCcstylePanel(
1067
1088
  customFooterSetting,
1068
1089
  footerTokenRateSetting,
1069
1090
  footerTokenRateModeSetting,
1091
+ footerTokenRateTtftSetting,
1070
1092
  footerCurrencySetting,
1071
1093
  footerCurrencyRateSetting,
1072
1094
  footerNerdIconsSetting,
@@ -20,7 +20,7 @@ import {
20
20
  import { Box, Markdown, Spacer, Text, type Component } from "@earendil-works/pi-tui";
21
21
  // 展开卡背景是 ccstyle 的渲染约定,不随上游 CompactThinkingConfig 走,直接读全局配置。
22
22
  import { config as styleConfig } from "../config/config.ts";
23
- import { isToolTuiFullscreen } from "../renderer/tool/show-more-hint.ts";
23
+ import { isToolTuiFullscreen } from "../renderer/tool/names.ts";
24
24
  import {
25
25
  animateCompactThinkingText,
26
26
  formatThoughtDuration,
@@ -3,6 +3,7 @@ import {
3
3
  type ExtensionAPI,
4
4
  type ExtensionCommandContext,
5
5
  type ToolInfo,
6
+ copyToClipboard,
6
7
  estimateTokens,
7
8
  formatSkillsForPrompt,
8
9
  getMarkdownTheme,
@@ -54,6 +55,30 @@ export function escCloseHitbox(bounds: DialogBounds): {
54
55
  };
55
56
  }
56
57
 
58
+ const COPY_LABEL = "[copy]";
59
+ const ESC_LABEL = "[esc]";
60
+
61
+ /** 测试可替换;生产使用 pi 的剪贴板实现。 */
62
+ let copyTextImpl: (text: string) => Promise<void> = (text) => copyToClipboard(text);
63
+
64
+ export function setPreviewCopyImpl(impl: ((text: string) => Promise<void>) | null): void {
65
+ copyTextImpl = impl ?? ((text) => copyToClipboard(text));
66
+ }
67
+
68
+ /** 1-based terminal hitbox of the [copy] button, one space left of [esc] on the same title row. */
69
+ export function copyButtonHitbox(bounds: DialogBounds): {
70
+ row: number;
71
+ startCol: number;
72
+ endCol: number;
73
+ } {
74
+ const esc = escCloseHitbox(bounds);
75
+ return {
76
+ row: esc.row,
77
+ startCol: esc.startCol - visibleWidth(COPY_LABEL) - 1,
78
+ endCol: esc.startCol - 2,
79
+ };
80
+ }
81
+
57
82
  let activeContextOverlays = 0;
58
83
 
59
84
  /** fullscreen 输入包装用于把鼠标事件继续传给当前 context 主弹框或文本预览 overlay。 */
@@ -81,7 +106,9 @@ export async function showTextPreview(
81
106
  let pageSize = 1;
82
107
  let totalLines = 1;
83
108
  let escHovered = false;
109
+ let copyHovered = false;
84
110
  let escHitbox: { row: number; startCol: number; endCol: number } | undefined;
111
+ let copyHitbox: { row: number; startCol: number; endCol: number } | undefined;
85
112
  let scrollbarHitbox:
86
113
  | {
87
114
  col: number;
@@ -108,6 +135,24 @@ export async function showTextPreview(
108
135
  tui.requestRender();
109
136
  };
110
137
 
138
+ const setCopyHovered = (hovered: boolean): void => {
139
+ if (hovered === copyHovered) return;
140
+ copyHovered = hovered;
141
+ tui.requestRender();
142
+ };
143
+
144
+ const copyPreview = (): void => {
145
+ void copyTextImpl(content)
146
+ .then(() => {
147
+ const flash = (tui as { flash?: (message: string) => void }).flash;
148
+ if (typeof flash === "function") flash("Copied!");
149
+ else ctx.ui.notify("Copied preview to clipboard", "info");
150
+ })
151
+ .catch((error: unknown) => {
152
+ ctx.ui.notify(error instanceof Error ? error.message : "Copy failed", "error");
153
+ });
154
+ };
155
+
111
156
  const dragScrollbarTo = (mouseRow: number): void => {
112
157
  if (!scrollbarHitbox || scrollbarDragOffset === null) return;
113
158
  const trackSize = scrollbarHitbox.endRow - scrollbarHitbox.startRow + 1;
@@ -151,7 +196,14 @@ export async function showTextPreview(
151
196
  mouse.col >= escHitbox.startCol &&
152
197
  mouse.col <= escHitbox.endCol,
153
198
  );
199
+ const overCopy = Boolean(
200
+ copyHitbox &&
201
+ mouse.row === copyHitbox.row &&
202
+ mouse.col >= copyHitbox.startCol &&
203
+ mouse.col <= copyHitbox.endCol,
204
+ );
154
205
  setEscHovered(overEsc);
206
+ setCopyHovered(overCopy);
155
207
  const button = mouseBaseButton(mouse.code);
156
208
  const motion = (mouse.code & 32) !== 0;
157
209
  if (button === 0 && !motion) {
@@ -159,6 +211,10 @@ export async function showTextPreview(
159
211
  done(undefined);
160
212
  return;
161
213
  }
214
+ if (overCopy) {
215
+ copyPreview();
216
+ return;
217
+ }
162
218
  if (
163
219
  scrollbarHitbox &&
164
220
  mouse.col === scrollbarHitbox.col &&
@@ -187,7 +243,7 @@ export async function showTextPreview(
187
243
 
188
244
  render(width: number) {
189
245
  const inner = Math.max(1, width - 2);
190
- const escWidth = visibleWidth("[esc]");
246
+ const actionsWidth = visibleWidth(`${COPY_LABEL} ${ESC_LABEL}`);
191
247
  const bodyInner = Math.max(1, inner - 1);
192
248
  const bodyWidth = Math.max(1, bodyInner - 1);
193
249
  const terminalHeight = Math.max(1, tui.terminal.rows);
@@ -205,6 +261,7 @@ export async function showTextPreview(
205
261
  const overlayTop = 2 + Math.floor((availableHeight - viewportHeight) / 2);
206
262
  const overlayLeft = Math.floor((Math.max(1, tui.terminal.columns) - width) / 2);
207
263
  escHitbox = escCloseHitbox({ left: overlayLeft, top: overlayTop, width });
264
+ copyHitbox = copyButtonHitbox({ left: overlayLeft, top: overlayTop, width });
208
265
  const visible = wrapped.slice(scrollOffset, scrollOffset + pageSize);
209
266
  const border = (text: string) => theme.fg("border", text);
210
267
  const scrollable = totalLines > pageSize;
@@ -237,11 +294,11 @@ export async function showTextPreview(
237
294
  });
238
295
  const start = totalLines === 0 ? 0 : scrollOffset + 1;
239
296
  const end = Math.min(totalLines, scrollOffset + pageSize);
240
- const status = `${start}-${end} / ${totalLines} lines · ↑↓ PgUp/PgDn Home/End · [esc] close`;
297
+ const status = `${start}-${end} / ${totalLines} lines · ↑↓ PgUp/PgDn Home/End · [copy] · [esc] close`;
241
298
 
242
299
  return [
243
300
  border(`╭${"─".repeat(inner)}╮`),
244
- `${border("│")}${padLine(` ${theme.bold(theme.fg("accent", title))}`, inner - escWidth)}${theme.fg(escHovered ? "text" : "muted", "[esc]")}${border("│")}`,
301
+ `${border("│")}${padLine(` ${theme.bold(theme.fg("accent", title))}`, inner - actionsWidth)}${theme.fg(copyHovered ? "text" : "muted", COPY_LABEL)} ${theme.fg(escHovered ? "text" : "muted", ESC_LABEL)}${border("│")}`,
245
302
  `${border("├")}${border("─".repeat(inner))}${border("┤")}`,
246
303
  ...bodyRows,
247
304
  `${border("├")}${border("─".repeat(inner))}${border("┤")}`,
@@ -273,6 +330,27 @@ const tokenEstimate = (value: unknown): number => {
273
330
  return Math.max(0, Math.ceil(text.length / 4));
274
331
  };
275
332
 
333
+ /** system 消息对请求的贡献:正文 + 各 section 文本 + 声明的工具 schema,与 pi 的 estimateTokens 同口径。 */
334
+ function systemMessageTokens(message: {
335
+ content?: unknown;
336
+ sections?: Record<string, string | null> | null;
337
+ toolsAdded?: unknown;
338
+ toolsRemoved?: unknown;
339
+ }): number {
340
+ let chars = typeof message.content === "string" ? message.content.length : 0;
341
+ for (const section of Object.values(message.sections ?? {})) {
342
+ if (typeof section === "string") chars += section.length;
343
+ }
344
+ if (message.toolsAdded) chars += JSON.stringify(message.toolsAdded).length;
345
+ if (message.toolsRemoved) chars += JSON.stringify(message.toolsRemoved).length;
346
+ return Math.ceil(chars / 4);
347
+ }
348
+
349
+ /** prompt 组装会把 CRLF 归一成 LF,匹配前对齐行尾。 */
350
+ function normalizeLineEndings(text: string): string {
351
+ return text.replace(/\r\n?/g, "\n");
352
+ }
353
+
276
354
  /** 只统计确实嵌进 system prompt 的片段,避免源文件预览把占用加两遍。 */
277
355
  function embeddedTokens(prompt: string, chunk: string): number {
278
356
  if (!chunk || !prompt.includes(chunk)) return 0;
@@ -348,24 +426,24 @@ export function collectContextBreakdown(
348
426
  allTools: ToolInfo[],
349
427
  ): ContextBreakdown {
350
428
  const options = (ctx.getSystemPromptOptions?.() ?? {}) as BuildSystemPromptOptions;
351
- const systemPrompt = typeof ctx.getSystemPrompt === "function" ? ctx.getSystemPrompt() : "";
352
429
  const selectedTools = new Set(options.selectedTools ?? ["read", "bash", "edit", "write"]);
353
430
  const toolDefinitionPreview: string[] = [];
354
431
  const toolResultPreview: string[] = [];
355
432
  const contextPreview: string[] = [];
433
+ const systemPromptPreview: string[] = [];
356
434
  let toolDefinitionTokens = 0;
357
435
  let toolResultTokens = 0;
358
436
  let contextTokens = 0;
437
+ let systemTokens = 0;
359
438
 
360
439
  const memoryPreview: string[] = [];
361
- let memoryTokens = 0;
362
- for (const file of options.contextFiles ?? []) {
363
- memoryTokens += embeddedTokens(systemPrompt, file.content);
440
+ const contextFiles = options.contextFiles ?? [];
441
+ for (const file of contextFiles) {
364
442
  memoryPreview.push(`## ${file.path}\n\n${previewValue(file.content)}`);
365
443
  }
366
444
 
367
- const skillsText = formatSkillsForPrompt(options.skills ?? []).trim();
368
- const skillsTokens = embeddedTokens(systemPrompt, skillsText);
445
+ const skills = options.skills ?? [];
446
+ const skillsText = formatSkillsForPrompt(skills).trim();
369
447
 
370
448
  for (const tool of allTools) {
371
449
  if (!selectedTools.has(tool.name)) continue;
@@ -378,21 +456,51 @@ export function collectContextBreakdown(
378
456
  toolDefinitionPreview.push(`## Definition: ${tool.name}\n\n${previewValue(definition)}`);
379
457
  }
380
458
 
381
- for (const entry of ctx.sessionManager.buildContextEntries()) {
382
- if (entry.type === "message") {
383
- const message = entry.message;
384
- if (message.role === "assistant") {
459
+ let memoryTokens = 0;
460
+ let skillsTokens = 0;
461
+ for (const projected of ctx.sessionManager.buildSessionProjection().entries) {
462
+ const source = projected.sourceEntry;
463
+ for (const message of projected.messages) {
464
+ if (message.role === "system") {
465
+ systemTokens += systemMessageTokens(message);
466
+ // Memory / skills 按投影中的实际 section 文本切分,与 system prompt 增量互补、不重不漏。
467
+ const sections = (message.sections ?? {}) as Record<string, string | null>;
468
+ const projectContext =
469
+ typeof sections.project_context === "string"
470
+ ? normalizeLineEndings(sections.project_context)
471
+ : "";
472
+ for (const file of contextFiles) {
473
+ const chunk = `<project_instructions path="${file.path}">\n${normalizeLineEndings(file.content)}\n</project_instructions>`;
474
+ memoryTokens += embeddedTokens(projectContext, chunk);
475
+ systemTokens -= embeddedTokens(projectContext, chunk);
476
+ }
477
+ const skillsSection =
478
+ typeof sections.skills === "string" ? normalizeLineEndings(sections.skills) : "";
479
+ skillsTokens += embeddedTokens(skillsSection, skillsText);
480
+ systemTokens -= embeddedTokens(skillsSection, skillsText);
481
+ systemPromptPreview.push(
482
+ (source.type === "compaction"
483
+ ? "## System prompt snapshot at compaction\n\n"
484
+ : "## System prompt update\n\n") +
485
+ previewValue({
486
+ content: message.content,
487
+ sections: message.sections,
488
+ toolsAdded: (message.toolsAdded ?? []).map((tool: { name?: string }) => tool?.name),
489
+ toolsRemoved: (message.toolsRemoved ?? []).map(
490
+ (tool: { name?: string }) => tool?.name,
491
+ ),
492
+ }),
493
+ );
494
+ } else if (message.role === "assistant") {
495
+ contextTokens += estimateTokens(message);
385
496
  for (const block of message.content) {
386
497
  if (block.type === "toolCall") {
387
- contextTokens += tokenEstimate(block.name) + tokenEstimate(block.arguments);
388
498
  contextPreview.push(
389
499
  `## Assistant tool call: ${block.name}\n\n${previewValue(block.arguments)}`,
390
500
  );
391
501
  } else if (block.type === "text") {
392
- contextTokens += tokenEstimate(block.text);
393
502
  contextPreview.push(`## Assistant\n\n${block.text}`);
394
503
  } else if (block.type === "thinking") {
395
- contextTokens += tokenEstimate(block.thinking);
396
504
  contextPreview.push(`## Assistant thinking\n\n${block.thinking}`);
397
505
  }
398
506
  }
@@ -402,8 +510,8 @@ export function collectContextBreakdown(
402
510
  `## Result: ${message.toolName}\n\n${previewValue(message.content)}`,
403
511
  );
404
512
  } else if (message.role === "bashExecution") {
405
- toolResultTokens += estimateTokens(message);
406
- toolResultPreview.push(
513
+ contextTokens += estimateTokens(message);
514
+ contextPreview.push(
407
515
  `## Bash\n\nCommand:\n\n${previewValue(message.command)}\n\nOutput:\n\n${previewValue(message.output)}`,
408
516
  );
409
517
  } else if (message.role === "branchSummary" || message.role === "compactionSummary") {
@@ -413,18 +521,10 @@ export function collectContextBreakdown(
413
521
  contextTokens += estimateTokens(message);
414
522
  contextPreview.push(`## ${message.role}\n\n${previewValue(message.content)}`);
415
523
  }
416
- } else if (entry.type === "compaction" || entry.type === "branch_summary") {
417
- contextTokens += tokenEstimate(entry.summary);
418
- contextPreview.push(
419
- `## ${entry.type === "compaction" ? "Compaction" : "Branch summary"}\n\n${entry.summary}`,
420
- );
421
- } else if (entry.type === "custom_message") {
422
- contextTokens += tokenEstimate(entry.content);
423
- contextPreview.push(`## Custom: ${entry.customType}\n\n${previewValue(entry.content)}`);
424
524
  }
425
525
  }
426
526
 
427
- const systemTokens = Math.max(0, tokenEstimate(systemPrompt) - memoryTokens - skillsTokens);
527
+ systemTokens = Math.max(0, systemTokens);
428
528
  return {
429
529
  parts: [
430
530
  { label: "System prompt", tokens: systemTokens, color: "accent" },
@@ -435,7 +535,10 @@ export function collectContextBreakdown(
435
535
  { label: "Context", tokens: contextTokens, color: "warning" },
436
536
  ] satisfies ContextPart[],
437
537
  previews: {
438
- systemPrompt: systemPrompt || "No system prompt.",
538
+ systemPrompt:
539
+ systemPromptPreview.join("\n\n") ||
540
+ (typeof ctx.getSystemPrompt === "function" ? ctx.getSystemPrompt() : "") ||
541
+ "No system prompt.",
439
542
  memoryFiles: memoryPreview.join("\n\n") || "No memory files in context.",
440
543
  skills: skillsText || "No skills in context.",
441
544
  tools: toolDefinitionPreview.join("\n\n") || "No active tool definitions.",
@@ -432,6 +432,8 @@ export default function sessionReferenceExtension(pi: ExtensionAPI): void {
432
432
  });
433
433
 
434
434
  pi.on("before_agent_start", async (event, ctx) => {
435
+ // Pi 1.2.0 custom events have no prompt. Older hosts omit messageType.
436
+ if (!("prompt" in event)) return;
435
437
  const referenceIds = extractSessionReferenceIds(event.prompt);
436
438
  if (referenceIds.length === 0) return;
437
439
 
@@ -26,8 +26,9 @@ const MENTION_PATTERN = /(?:^|[\t ])@([^\s@]*)$/;
26
26
  const SUBAGENT_REFERENCE_PATTERN = /(?:^|\s)@subagent:\[([^\]]+)](?![\w-])/g;
27
27
 
28
28
  function getAgentDir(): string {
29
+ // PI_CODING_AGENT_DIR 本身就是 agent 目录(默认 ~/.pi/agent),不是家目录。
29
30
  const override = process.env.PI_CODING_AGENT_DIR;
30
- return join(override ? override : homedir(), ".pi", "agent", "agents");
31
+ return join(override ? override : join(homedir(), ".pi", "agent"), "agents");
31
32
  }
32
33
 
33
34
  function parseFrontmatter(content: string): Record<string, string> {
@@ -207,6 +208,8 @@ export default function agentAutocompleteExtension(pi: ExtensionAPI): void {
207
208
 
208
209
  // Inject as a request message, not systemPrompt, so the cached prefix stays stable.
209
210
  pi.on("before_agent_start", async (event, _ctx) => {
211
+ // Pi 1.2.0 custom events have no prompt. Older hosts omit messageType.
212
+ if (!("prompt" in event)) return;
210
213
  const agents = getAgents();
211
214
  if (agents.length === 0) return;
212
215
 
@@ -53,13 +53,16 @@ export function footerGlyphs(nerdIcons: boolean): { git: string; cache: string;
53
53
  : { git: "", cache: "", mcp: "" };
54
54
  }
55
55
 
56
- /** 思考等级的填充圆标:等级越高填得越满。 */
56
+ /**
57
+ * 思考等级的填充圆标:等级越高填得越满。
58
+ * 全部取自同一圆径的几何圆,避开占位虚线圆和偏小的半圆。
59
+ */
57
60
  const EFFORT_SYMBOL: Record<string, string> = {
58
- minimal: "\u25cc",
59
- low: "\u25cb",
61
+ minimal: "\u25cb",
62
+ low: "\u25ce",
60
63
  medium: "\u25d4",
61
- high: "\u25d1",
62
- xhigh: "\u25d5",
64
+ high: "\u25d5",
65
+ xhigh: "\u25c9",
63
66
  max: "\u25cf",
64
67
  };
65
68
 
@@ -72,7 +75,7 @@ export function modelChip(
72
75
  ): string {
73
76
  const symbol = EFFORT_SYMBOL[level];
74
77
  if (!symbol) return theme.fg("accent", modelLabel);
75
- return `${theme.fg("accent", modelLabel)} ${thinkingColor(symbol)} ${theme.fg("dim", level)}`;
78
+ return `${theme.fg("accent", modelLabel)} ${thinkingColor(`${symbol} ${level}`)}`;
76
79
  }
77
80
 
78
81
  /** git 状态符号按职能分色:冲突/落后 error,修改 warning,未跟踪 muted,领先 success。 */
@@ -441,6 +444,7 @@ const createCustomFooterFactory =
441
444
  displayTokenRate(config.footerTokenRateMode),
442
445
  currentTtftMs(),
443
446
  config.footerTokenRateMode,
447
+ config.footerTokenRateTtft,
444
448
  )
445
449
  : "";
446
450
  const rateChip = rateLabel ? theme.fg("dim", rateLabel) : "";
@@ -182,11 +182,23 @@ export function currentTtftMs(): number {
182
182
  return Math.max(0, firstTokenAt - runStartTime);
183
183
  }
184
184
 
185
- /** `avg 1.2k tok/s (TTFT: 812ms)`;无数据显示空串,未出首 token 时无括号段。 */
186
- export function formatTokenRate(rate: number, ttftMs = 0, mode: TokenRateMode = "average"): string {
185
+ /** 不足 1 秒用毫秒,否则用一位小数的秒。 */
186
+ export function formatTtft(ttftMs: number): string {
187
+ if (ttftMs < 1000) return `${Math.round(ttftMs)}ms`;
188
+ return `${(ttftMs / 1000).toFixed(1)}s`;
189
+ }
190
+
191
+ /** `avg 1.2k tok/s (TTFT: 812ms)`;无数据显示空串,未出首 token 或 showTtft 为 false 时无括号段。 */
192
+ export function formatTokenRate(
193
+ rate: number,
194
+ ttftMs = 0,
195
+ mode: TokenRateMode = "average",
196
+ showTtft = true,
197
+ ): string {
187
198
  if (!Number.isFinite(rate) || rate <= 0) return "";
188
199
  const value = rate >= 1000 ? `${(rate / 1000).toFixed(1)}k` : `${Math.round(rate)}`;
189
- const ttft = Number.isFinite(ttftMs) && ttftMs > 0 ? ` (TTFT: ${Math.round(ttftMs)}ms)` : "";
200
+ const ttft =
201
+ showTtft && Number.isFinite(ttftMs) && ttftMs > 0 ? ` (TTFT: ${formatTtft(ttftMs)})` : "";
190
202
  const label = mode === "live" ? "live" : "avg";
191
203
  return `${label} ${value} tok/s${ttft}`;
192
204
  }
@@ -1,10 +1,4 @@
1
1
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
- import { getCompactRunStatusText } from "../../renderer/compact-mode.ts";
3
- import {
4
- fullscreenLazyTui,
5
- getToolMouseTui,
6
- isFullscreenAtBottom,
7
- } from "../../renderer/mouse/scroll.ts";
8
2
  import { formatDuration, formatTokenCount } from "../../utils/format.ts";
9
3
  import {
10
4
  resetTokenRate,
@@ -60,23 +54,10 @@ export default function (pi: ExtensionAPI): void {
60
54
  if (output > 0) recordProviderUsage(output);
61
55
  }
62
56
 
63
- /**
64
- * 摘要行滚出视口才镜像:仅 fullscreen 且已离开 transcript 底部。
65
- * regular 没有“离开底部”信号(transcript 在终端回滚区),保持 Pi 默认文案。
66
- */
67
- function compactMirrorText(): string | undefined {
68
- const tui = getToolMouseTui();
69
- if (!tui || !fullscreenLazyTui(tui) || isFullscreenAtBottom(tui)) return undefined;
70
- return getCompactRunStatusText();
71
- }
72
-
73
57
  function buildWorkingMessage(): string {
74
58
  const parts: string[] = [];
75
59
  const tokens = tokenCount();
76
60
  if (tokens > 0) parts.push(`↓ ${formatTokenCount(tokens)} tokens`);
77
- // compact 活动回合且已滚出摘要行:直接用摘要行文案(自带回合时长,不叠 agent 计时)。
78
- const compactStatus = compactMirrorText();
79
- if (compactStatus) return [compactStatus, ...parts].join(" · ");
80
61
  const elapsed = Date.now() - (agentStartTime || turnStartTime);
81
62
  if (elapsed >= SHOW_TIMER_AFTER_MS || tokens > 0) {
82
63
  // formatDuration 低于 1 秒返回 "",此处回退 "0s" 保持计时器连续跳动。
@@ -186,9 +167,9 @@ export default function (pi: ExtensionAPI): void {
186
167
  evt.type === "text_start" ||
187
168
  evt.type === "toolcall_start"
188
169
  ) {
189
- // TTFT 的首块含 toolcall_start:模型直接发起 tool call(thinking/text 省略)时也算首 token
170
+ // TTFT 的首块含 toolcall_start:模型直接发起 tool call(thinking/text 省略)时也算首 token。
171
+ // 分段锚点只在 turn_start 重置。这里再重置会把累计 usage.output 当成新的首报。
190
172
  markFirstToken();
191
- startMessageSegment();
192
173
  updateProviderUsage(evt.partial);
193
174
  } else if (evt.type === "thinking_delta" || evt.type === "text_delta") {
194
175
  if (typeof evt.delta === "string") sampleTokenDelta(evt.delta);