proper-base 0.2.3 → 0.2.4

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.
package/README.md CHANGED
@@ -137,18 +137,14 @@ pi install npm:proper-base
137
137
  From a local checkout:
138
138
 
139
139
  ```bash
140
- npm install
141
140
  pi install /path/to/proper-pi-extensions/proper-base
142
141
  ```
143
142
 
144
143
  Pi supplies the core `@earendil-works/pi-coding-agent` and
145
- `@earendil-works/pi-tui` peer packages. Local `npm install` prepares development
146
- tooling and runs the install-time settings seed; it is not a runtime build.
147
-
148
- When an existing `settings.json` is readable, the postinstall hook sets
149
- `subagents.agentOverrides.worker.defaultContext` to `fresh` if no worker
150
- override exists. It leaves existing overrides, missing files, and unreadable
151
- settings untouched. Installing with `--ignore-scripts` skips that default.
144
+ `@earendil-works/pi-tui` peer packages. The package has no runtime
145
+ dependencies, no build step, and no install-time npm scripts, and it does
146
+ not modify Pi settings during installation or runtime. `npm install` only
147
+ prepares the development checks below.
152
148
 
153
149
  This package replaces the former local `proper-customs` identity. Keep only one
154
150
  registration. Existing data under the legacy `proper-history` path remains
@@ -171,6 +167,7 @@ compatible.
171
167
  ## Development
172
168
 
173
169
  ```bash
170
+ npm install
174
171
  npm test
175
172
  npm run typecheck
176
173
  npm run test:coverage
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "proper-base",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Baseline Pi behavior for session naming, history, prompt editing, fullscreen navigation, and footer styling",
5
5
  "keywords": [
6
6
  "pi-package",
@@ -35,13 +35,11 @@
35
35
  "type": "module",
36
36
  "files": [
37
37
  "index.ts",
38
- "install.mjs",
39
38
  "src",
40
39
  "README.md",
41
40
  "LICENSE"
42
41
  ],
43
42
  "scripts": {
44
- "postinstall": "node install.mjs",
45
43
  "prepack": "npm test && npm run typecheck",
46
44
  "test": "node --test test/*.test.ts",
47
45
  "test:coverage": "node --experimental-test-coverage --test --test-coverage-lines=93 --test-coverage-branches=78 --test-coverage-functions=90 test/*.test.ts",
@@ -75,6 +75,7 @@ export function installFooterColors(
75
75
 
76
76
  let state = { ctx, tui };
77
77
  let timer: ReturnType<typeof setInterval> | undefined;
78
+ const ownsRender = Object.hasOwn(footer, "render");
78
79
  const render = footer.render;
79
80
  const dispose = footer.dispose;
80
81
  const stopAnimation = () => {
@@ -101,7 +102,8 @@ export function installFooterColors(
101
102
  uninstall() {
102
103
  if (footer[INSTALLED] !== controller) return;
103
104
  stopAnimation();
104
- footer.render = render;
105
+ if (ownsRender) footer.render = render;
106
+ else Reflect.deleteProperty(footer, "render");
105
107
  if (dispose) footer.dispose = dispose;
106
108
  else delete footer.dispose;
107
109
  delete footer[INSTALLED];
@@ -43,6 +43,12 @@ type State = {
43
43
  ids: WeakMap<object, string>;
44
44
  nextId: number;
45
45
  globalExpanded: boolean;
46
+ assistantCache: WeakMap<AssistantComponent, AssistantRenderCache>;
47
+ };
48
+ type AssistantRenderCache = {
49
+ message: AssistantMessage;
50
+ width: number;
51
+ lines: Map<string, string[]>;
46
52
  };
47
53
  type DetailKind = "tool" | "error";
48
54
  type Detail = {
@@ -55,6 +61,7 @@ type ToolComponent = Component & {
55
61
  toolName?: string;
56
62
  args?: unknown;
57
63
  result?: { isError?: boolean };
64
+ expanded?: boolean;
58
65
  setExpanded?(expanded: boolean): void;
59
66
  };
60
67
  type MouseTui = TUI & {
@@ -96,8 +103,10 @@ export function installTranscriptCleanup(
96
103
  ids: new WeakMap(),
97
104
  nextId: 0,
98
105
  globalExpanded: ctx.ui.getToolsExpanded(),
106
+ assistantCache: new WeakMap(),
99
107
  };
100
108
  const render = chat.render;
109
+ const invalidate = chat.invalidate;
101
110
  const listener: TuiInputListener = (data) => handleClick(data, state);
102
111
  const unsubscribe = tui.addInputListener(listener);
103
112
  prioritize(tui, listener);
@@ -166,9 +175,16 @@ export function installTranscriptCleanup(
166
175
  if (chat[INSTALLED] !== controller) return;
167
176
  unsubscribe();
168
177
  chat.render = render;
178
+ chat.invalidate = invalidate;
169
179
  delete chat[INSTALLED];
170
180
  },
171
181
  };
182
+ // Theme or terminal changes rebuild component content, so drop memoized
183
+ // assistant lines along with the native caches.
184
+ chat.invalidate = () => {
185
+ state = { ...state, assistantCache: new WeakMap() };
186
+ invalidate.call(chat);
187
+ };
172
188
  chat.render = (width: number) => {
173
189
  const globalExpanded = state.ctx.ui.getToolsExpanded();
174
190
  if (globalExpanded !== state.globalExpanded) {
@@ -278,13 +294,13 @@ function renderGroup(
278
294
  message.content.forEach((part) => {
279
295
  if (part.type === "thinking" && part.thinking?.trim()) {
280
296
  lines.push(
281
- ...renderAssistantContent(assistant, message, [part], width),
297
+ ...renderAssistantContent(assistant, message, [part], width, state),
282
298
  );
283
299
  }
284
300
  if (hasToolCall && part.type === "text" && part.text?.trim()) {
285
301
  lines.push(
286
302
  ...withVerticalSpacing(
287
- renderAssistantContent(assistant, message, [part], width),
303
+ renderAssistantContent(assistant, message, [part], width, state),
288
304
  ),
289
305
  );
290
306
  }
@@ -314,7 +330,7 @@ function renderGroup(
314
330
  );
315
331
  if (direct.length) {
316
332
  lines.push(
317
- ...renderAssistantContent(assistant, message, direct, width),
333
+ ...renderAssistantContent(assistant, message, direct, width, state),
318
334
  );
319
335
  }
320
336
  }
@@ -351,12 +367,26 @@ function renderAssistantContent(
351
367
  message: AssistantMessage,
352
368
  content: AssistantMessage["content"],
353
369
  width: number,
370
+ state: State,
354
371
  ): string[] {
355
372
  if (!assistant.updateContent) return assistant.render(width);
373
+ // The updateContent swap below rebuilds the component's children and
374
+ // discards their internal render caches, so re-running it every frame
375
+ // re-parses the whole transcript's markdown. Memoize per content subset.
376
+ let cache = state.assistantCache.get(assistant);
377
+ if (!cache || cache.message !== message || cache.width !== width) {
378
+ cache = { message, width, lines: new Map() };
379
+ state.assistantCache.set(assistant, cache);
380
+ }
381
+ const key = content.map((part) => message.content.indexOf(part)).join(",");
382
+ const cached = cache.lines.get(key);
383
+ if (cached) return cached;
356
384
  const { errorMessage: _errorMessage, ...rest } = message;
357
385
  assistant.updateContent({ ...rest, content, stopReason: "stop" }, false);
358
386
  try {
359
- return assistant.render(width);
387
+ const lines = assistant.render(width);
388
+ cache.lines.set(key, lines);
389
+ return lines;
360
390
  } finally {
361
391
  assistant.updateContent(message, false);
362
392
  }
@@ -400,7 +430,9 @@ function renderDetail(detail: Detail, width: number, state: State): string[] {
400
430
  `${header}${" ".repeat(Math.max(0, width - visibleWidth(header)))}`,
401
431
  ];
402
432
  const expandable = detail.component as ToolComponent;
403
- expandable.setExpanded?.(expanded);
433
+ // setExpanded rebuilds the tool's result renderer even when the state is
434
+ // unchanged, which re-sanitizes full outputs on every frame. Skip no-ops.
435
+ if (expandable.expanded !== expanded) expandable.setExpanded?.(expanded);
404
436
  if (expanded) {
405
437
  lines.push(...detail.component.render(width));
406
438
  const button = `\x1b[7m ${state.ctx.ui.theme.fg(detailColor(detail.kind), "collapse")} \x1b[27m`;
package/install.mjs DELETED
@@ -1,84 +0,0 @@
1
- /**
2
- * npm `postinstall` hook.
3
- *
4
- * Pi has no package install hook of its own, so a default that another
5
- * extension reads only from `settings.json` is written here: once, when the
6
- * package is installed, never from a running session. Plain JavaScript because
7
- * npm runs this with bare `node`.
8
- *
9
- * Runs for `pi install npm:proper-base`, for git sources, and for `npm
10
- * install` in a local checkout. A local path registered with `pi install
11
- * /path/to/proper-base` is only recorded in settings, so it seeds when that
12
- * checkout's own `npm install` runs.
13
- */
14
-
15
- import { readFileSync, renameSync, writeFileSync } from "node:fs";
16
- import { homedir } from "node:os";
17
- import { join } from "node:path";
18
- import { fileURLToPath } from "node:url";
19
-
20
- /** Pi's user agent directory: `PI_CODING_AGENT_DIR`, else `~/.pi/agent`. */
21
- export function agentDir() {
22
- const configured = process.env.PI_CODING_AGENT_DIR;
23
- if (!configured) return join(homedir(), ".pi", "agent");
24
- return configured.startsWith("~/")
25
- ? join(homedir(), configured.slice(2))
26
- : configured;
27
- }
28
-
29
- function asObject(value) {
30
- return typeof value === "object" && value !== null && !Array.isArray(value)
31
- ? value
32
- : undefined;
33
- }
34
-
35
- /**
36
- * Give pi-subagents' packaged `worker` agent a fresh context by default.
37
- *
38
- * `worker` ships `defaultContext: fork`, which replays the parent transcript
39
- * into every child. When parent and child resolve to the same model, Pi keeps
40
- * the inherited signed thinking blocks and Anthropic rejects the request with
41
- * "`thinking` blocks in the latest assistant message cannot be modified".
42
- *
43
- * Returns true only when the file was rewritten.
44
- */
45
- export function seedWorkerContext(dir) {
46
- const file = join(dir, "settings.json");
47
- let settings;
48
- try {
49
- settings = asObject(JSON.parse(readFileSync(file, "utf-8")));
50
- } catch {
51
- // No settings file yet, or one Pi itself cannot read. Writing here would
52
- // destroy content we failed to parse, so leave it untouched.
53
- return false;
54
- }
55
- if (!settings) return false;
56
-
57
- const subagents = asObject(settings.subagents) ?? {};
58
- const overrides = asObject(subagents.agentOverrides) ?? {};
59
- if ("worker" in overrides) return false;
60
-
61
- settings.subagents = {
62
- ...subagents,
63
- agentOverrides: { ...overrides, worker: { defaultContext: "fresh" } },
64
- };
65
-
66
- // Rename onto the target so a crash mid-write cannot truncate settings.
67
- const staged = `${file}.proper-base`;
68
- writeFileSync(staged, `${JSON.stringify(settings, null, 2)}\n`, "utf-8");
69
- renameSync(staged, file);
70
- return true;
71
- }
72
-
73
- if (process.argv[1] === fileURLToPath(import.meta.url)) {
74
- try {
75
- if (seedWorkerContext(agentDir())) {
76
- console.log(
77
- "proper-base: set subagents.agentOverrides.worker.defaultContext to fresh",
78
- );
79
- }
80
- } catch (error) {
81
- // A failed convenience default must never fail the install.
82
- console.warn(`proper-base: could not seed settings.json: ${error}`);
83
- }
84
- }