@aeondave/pi-theme-1337 0.5.1 → 0.6.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.
@@ -1,192 +1,192 @@
1
- /**
2
- * Pure registry that folds runs of consecutive exploration calls.
3
- *
4
- * The host fixes one `ToolExecutionComponent` per call into a transcript container
5
- * no extension can see, but a `renderShell: "self"` renderer can render zero lines
6
- * (the row disappears) or one summary line instead of its view. This module owns
7
- * the ONLY state that decides which, derived from the same documented
8
- * `tool_execution_*` events the host builds rows from and closed by any message
9
- * that paints a transcript row. No host prototypes are patched; the adapter asks
10
- * `decisionFor` at render time and the host's per-frame re-render applies
11
- * transitions for free.
12
- *
13
- * Folding is presentation of the transcript itself — it suppresses neither
14
- * `notify()`, status indicators, widgets, nor any other Pi channel. A run folds
15
- * as soon as it holds MIN_FOLD settled calls, and keeps folding while it is
16
- * open: hidden rows take no lines, so the tally stays where it is and counts up
17
- * as each later call settles into it. A row is never hidden unless another row
18
- * of its run renders the tally. Calls still running, failed calls and
19
- * image-bearing calls keep their own rows (the host paints images outside our
20
- * shell), and Ctrl+O still expands every row.
21
- */
22
- import { isFoldableTool, tallyDetails, tallyName, tallySentence, tallySubject } from "./tool-lexicon.ts";
23
-
24
- export type GroupDecision =
25
- | { readonly kind: "normal" }
26
- | { readonly kind: "hidden" }
27
- /** `sentence` counts ("Read 3 files"); `detailed` names the files when the row has room for it. */
28
- | { readonly kind: "summary"; readonly sentence: string; readonly detailed: string };
29
-
30
- /** What a row adapter consumes. Row-local state (expanded, partial) is the adapter's business, not the registry's. */
31
- export interface GroupControl {
32
- decisionFor(callId: string): GroupDecision;
33
- /** A click on the summary or on any member row: the run's rows return for good (one-way; Ctrl+O still expands everything). */
34
- expand(callId: string): void;
35
- /** The host paints a result's images outside our shell, so a call carrying one never folds. */
36
- markImages(callId: string): void;
37
- /**
38
- * The call's row has its final result. Rows settle their own calls: Pi tells
39
- * extensions a tool ended before it updates the row, so an event-fed registry
40
- * would hide a run's rows while the row meant to carry the tally still
41
- * rendered as running.
42
- */
43
- settle(callId: string, failed: boolean): void;
44
- }
45
-
46
- export interface ToolStartDetails {
47
- /** With `cwd`, name what the call is about, so the tally counts distinct files and patterns. */
48
- readonly args?: unknown;
49
- readonly cwd?: string;
50
- /** This package's adapter renders that tool's rows (default true). */
51
- readonly rendersHere?: boolean;
52
- }
53
-
54
- export interface ToolRunRegistry extends GroupControl {
55
- onToolStart(callId: string, toolName: string, details?: ToolStartDetails): void;
56
- /** Closes the open run when the message paints transcript rows. */
57
- onMessage(message: unknown): void;
58
- closeAll(): void;
59
- reset(): void;
60
- }
61
-
62
- /** Fewer calls than this read fine as rows; a tally only pays for itself on longer runs. */
63
- export const MIN_FOLD = 3;
64
-
65
- // Singletons: decisionFor runs for every row on every frame and must not allocate.
66
- const NORMAL: GroupDecision = Object.freeze({ kind: "normal" });
67
- const HIDDEN: GroupDecision = Object.freeze({ kind: "hidden" });
68
-
69
- /**
70
- * Whether the host paints a transcript row for this message — and so whether it
71
- * sits between two tool rows and must break their run. Tool results belong to
72
- * the rows themselves. An assistant message paints text, a "∴ thinking" row (this
73
- * extension always sets a hidden-thinking label) and, when the turn ended badly,
74
- * an error line; a toolCall-only message paints nothing. Other roles paint unless
75
- * they opt out with `display: false`.
76
- */
77
- export function messagePaintsRows(message: unknown): boolean {
78
- if (typeof message !== "object" || message === null) return false;
79
- const m = message as { role?: unknown; content?: unknown; stopReason?: unknown; display?: unknown };
80
- if (m.role === "toolResult") return false;
81
- if (m.role !== "assistant") return m.display !== false;
82
- if (m.stopReason === "length" || m.stopReason === "error" || m.stopReason === "aborted") return true;
83
- if (typeof m.content === "string") return m.content.trim().length > 0;
84
- if (!Array.isArray(m.content)) return false;
85
- return m.content.some(block => {
86
- if (typeof block !== "object" || block === null) return false;
87
- const b = block as { type?: unknown; text?: unknown; thinking?: unknown };
88
- const text = b.type === "text" ? b.text : b.type === "thinking" ? b.thinking : undefined;
89
- return typeof text === "string" && text.trim().length > 0;
90
- });
91
- }
92
-
93
- interface RunCall {
94
- readonly id: string;
95
- readonly name: string;
96
- readonly subject: string;
97
- readonly display: string;
98
- ended: boolean;
99
- failed: boolean;
100
- images: boolean;
101
- }
102
-
103
- interface Run {
104
- readonly calls: RunCall[];
105
- expanded: boolean;
106
- /** The fold, computed once per mutation; undefined = stale. */
107
- fold?: Map<RunCall, GroupDecision>;
108
- }
109
-
110
- const foldable = (call: RunCall): boolean => call.ended && !call.failed && !call.images;
111
-
112
- function computeFold(run: Run): Map<RunCall, GroupDecision> {
113
- const fold = new Map<RunCall, GroupDecision>();
114
- if (run.expanded) return fold;
115
- const members = run.calls.filter(foldable);
116
- if (members.length < MIN_FOLD) return fold;
117
- const sentence = tallySentence(members);
118
- const detailed = tallyDetails(members);
119
- // The carrier is the last foldable call: the rows before it take no lines, so
120
- // as an open run grows the tally stays put and the newest settled call takes
121
- // it over in place — and it sits where the run ended even when a failed call
122
- // trails it.
123
- for (const call of members) fold.set(call, HIDDEN);
124
- fold.set(members[members.length - 1]!, { kind: "summary", sentence, detailed });
125
- return fold;
126
- }
127
-
128
- export function createToolRunRegistry(): ToolRunRegistry {
129
- let openRun: Run | undefined;
130
- const byCall = new Map<string, { run: Run; call: RunCall }>();
131
-
132
- const close = (): void => {
133
- const run = openRun;
134
- if (!run) return;
135
- openRun = undefined;
136
- // A run that can never fold needs no memory: keep only runs that matter.
137
- if (run.calls.length < MIN_FOLD) for (const call of run.calls) byCall.delete(call.id);
138
- };
139
-
140
- return {
141
- onToolStart(callId, toolName, { args, cwd, rendersHere = true } = {}) {
142
- if (!callId || byCall.has(callId)) return;
143
- // bash (side effects), edit, write, delegate, custom tools and rows another
144
- // renderer owns all break the run and stay untracked themselves.
145
- if (!rendersHere || !isFoldableTool(toolName)) {
146
- close();
147
- return;
148
- }
149
- openRun ??= { calls: [], expanded: false };
150
- const call: RunCall = { id: callId, name: toolName, subject: tallySubject(toolName, args, cwd),
151
- display: tallyName(toolName, args, cwd), ended: false, failed: false, images: false };
152
- openRun.calls.push(call);
153
- openRun.fold = undefined;
154
- byCall.set(callId, { run: openRun, call });
155
- },
156
- settle(callId, failed) {
157
- const entry = byCall.get(callId);
158
- if (!entry || entry.call.ended) return;
159
- entry.call.ended = true;
160
- entry.call.failed = failed;
161
- entry.run.fold = undefined;
162
- },
163
- onMessage(message) {
164
- // Called on every streaming delta: bail before inspecting the message.
165
- if (openRun && messagePaintsRows(message)) close();
166
- },
167
- closeAll: close,
168
- reset() {
169
- openRun = undefined;
170
- byCall.clear();
171
- },
172
- expand(callId) {
173
- const entry = byCall.get(callId);
174
- if (!entry || entry.run.expanded) return;
175
- entry.run.expanded = true;
176
- entry.run.fold = undefined;
177
- },
178
- markImages(callId) {
179
- const entry = byCall.get(callId);
180
- if (!entry || entry.call.images) return;
181
- entry.call.images = true;
182
- entry.run.fold = undefined;
183
- },
184
- decisionFor(callId) {
185
- const entry = byCall.get(callId);
186
- if (!entry) return NORMAL;
187
- const run = entry.run;
188
- run.fold ??= computeFold(run);
189
- return run.fold.get(entry.call) ?? NORMAL;
190
- },
191
- };
192
- }
1
+ /**
2
+ * Pure registry that folds runs of consecutive exploration calls.
3
+ *
4
+ * The host fixes one `ToolExecutionComponent` per call into a transcript container
5
+ * no extension can see, but a `renderShell: "self"` renderer can render zero lines
6
+ * (the row disappears) or one summary line instead of its view. This module owns
7
+ * the ONLY state that decides which, derived from the same documented
8
+ * `tool_execution_*` events the host builds rows from and closed by any message
9
+ * that paints a transcript row. No host prototypes are patched; the adapter asks
10
+ * `decisionFor` at render time and the host's per-frame re-render applies
11
+ * transitions for free.
12
+ *
13
+ * Folding is presentation of the transcript itself — it suppresses neither
14
+ * `notify()`, status indicators, widgets, nor any other Pi channel. A run folds
15
+ * as soon as it holds MIN_FOLD settled calls, and keeps folding while it is
16
+ * open: hidden rows take no lines, so the tally stays where it is and counts up
17
+ * as each later call settles into it. A row is never hidden unless another row
18
+ * of its run renders the tally. Calls still running, failed calls and
19
+ * image-bearing calls keep their own rows (the host paints images outside our
20
+ * shell), and Ctrl+O still expands every row.
21
+ */
22
+ import { isFoldableTool, tallyDetails, tallyName, tallySentence, tallySubject } from "./tool-lexicon.ts";
23
+
24
+ export type GroupDecision =
25
+ | { readonly kind: "normal" }
26
+ | { readonly kind: "hidden" }
27
+ /** `sentence` counts ("Read 3 files"); `detailed` names the files when the row has room for it. */
28
+ | { readonly kind: "summary"; readonly sentence: string; readonly detailed: string };
29
+
30
+ /** What a row adapter consumes. Row-local state (expanded, partial) is the adapter's business, not the registry's. */
31
+ export interface GroupControl {
32
+ decisionFor(callId: string): GroupDecision;
33
+ /** A click on the summary or on any member row: the run's rows return for good (one-way; Ctrl+O still expands everything). */
34
+ expand(callId: string): void;
35
+ /** The host paints a result's images outside our shell, so a call carrying one never folds. */
36
+ markImages(callId: string): void;
37
+ /**
38
+ * The call's row has its final result. Rows settle their own calls: Pi tells
39
+ * extensions a tool ended before it updates the row, so an event-fed registry
40
+ * would hide a run's rows while the row meant to carry the tally still
41
+ * rendered as running.
42
+ */
43
+ settle(callId: string, failed: boolean): void;
44
+ }
45
+
46
+ export interface ToolStartDetails {
47
+ /** With `cwd`, name what the call is about, so the tally counts distinct files and patterns. */
48
+ readonly args?: unknown;
49
+ readonly cwd?: string;
50
+ /** This package's adapter renders that tool's rows (default true). */
51
+ readonly rendersHere?: boolean;
52
+ }
53
+
54
+ export interface ToolRunRegistry extends GroupControl {
55
+ onToolStart(callId: string, toolName: string, details?: ToolStartDetails): void;
56
+ /** Closes the open run when the message paints transcript rows. */
57
+ onMessage(message: unknown): void;
58
+ closeAll(): void;
59
+ reset(): void;
60
+ }
61
+
62
+ /** Fewer calls than this read fine as rows; a tally only pays for itself on longer runs. */
63
+ export const MIN_FOLD = 3;
64
+
65
+ // Singletons: decisionFor runs for every row on every frame and must not allocate.
66
+ const NORMAL: GroupDecision = Object.freeze({ kind: "normal" });
67
+ const HIDDEN: GroupDecision = Object.freeze({ kind: "hidden" });
68
+
69
+ /**
70
+ * Whether the host paints a transcript row for this message — and so whether it
71
+ * sits between two tool rows and must break their run. Tool results belong to
72
+ * the rows themselves. An assistant message paints text, a "∴ thinking" row (this
73
+ * extension always sets a hidden-thinking label) and, when the turn ended badly,
74
+ * an error line; a toolCall-only message paints nothing. Other roles paint unless
75
+ * they opt out with `display: false`.
76
+ */
77
+ export function messagePaintsRows(message: unknown): boolean {
78
+ if (typeof message !== "object" || message === null) return false;
79
+ const m = message as { role?: unknown; content?: unknown; stopReason?: unknown; display?: unknown };
80
+ if (m.role === "toolResult") return false;
81
+ if (m.role !== "assistant") return m.display !== false;
82
+ if (m.stopReason === "length" || m.stopReason === "error" || m.stopReason === "aborted") return true;
83
+ if (typeof m.content === "string") return m.content.trim().length > 0;
84
+ if (!Array.isArray(m.content)) return false;
85
+ return m.content.some(block => {
86
+ if (typeof block !== "object" || block === null) return false;
87
+ const b = block as { type?: unknown; text?: unknown; thinking?: unknown };
88
+ const text = b.type === "text" ? b.text : b.type === "thinking" ? b.thinking : undefined;
89
+ return typeof text === "string" && text.trim().length > 0;
90
+ });
91
+ }
92
+
93
+ interface RunCall {
94
+ readonly id: string;
95
+ readonly name: string;
96
+ readonly subject: string;
97
+ readonly display: string;
98
+ ended: boolean;
99
+ failed: boolean;
100
+ images: boolean;
101
+ }
102
+
103
+ interface Run {
104
+ readonly calls: RunCall[];
105
+ expanded: boolean;
106
+ /** The fold, computed once per mutation; undefined = stale. */
107
+ fold?: Map<RunCall, GroupDecision>;
108
+ }
109
+
110
+ const foldable = (call: RunCall): boolean => call.ended && !call.failed && !call.images;
111
+
112
+ function computeFold(run: Run): Map<RunCall, GroupDecision> {
113
+ const fold = new Map<RunCall, GroupDecision>();
114
+ if (run.expanded) return fold;
115
+ const members = run.calls.filter(foldable);
116
+ if (members.length < MIN_FOLD) return fold;
117
+ const sentence = tallySentence(members);
118
+ const detailed = tallyDetails(members);
119
+ // The carrier is the last foldable call: the rows before it take no lines, so
120
+ // as an open run grows the tally stays put and the newest settled call takes
121
+ // it over in place — and it sits where the run ended even when a failed call
122
+ // trails it.
123
+ for (const call of members) fold.set(call, HIDDEN);
124
+ fold.set(members[members.length - 1]!, { kind: "summary", sentence, detailed });
125
+ return fold;
126
+ }
127
+
128
+ export function createToolRunRegistry(): ToolRunRegistry {
129
+ let openRun: Run | undefined;
130
+ const byCall = new Map<string, { run: Run; call: RunCall }>();
131
+
132
+ const close = (): void => {
133
+ const run = openRun;
134
+ if (!run) return;
135
+ openRun = undefined;
136
+ // A run that can never fold needs no memory: keep only runs that matter.
137
+ if (run.calls.length < MIN_FOLD) for (const call of run.calls) byCall.delete(call.id);
138
+ };
139
+
140
+ return {
141
+ onToolStart(callId, toolName, { args, cwd, rendersHere = true } = {}) {
142
+ if (!callId || byCall.has(callId)) return;
143
+ // bash (side effects), edit, write, delegate, custom tools and rows another
144
+ // renderer owns all break the run and stay untracked themselves.
145
+ if (!rendersHere || !isFoldableTool(toolName)) {
146
+ close();
147
+ return;
148
+ }
149
+ openRun ??= { calls: [], expanded: false };
150
+ const call: RunCall = { id: callId, name: toolName, subject: tallySubject(toolName, args, cwd),
151
+ display: tallyName(toolName, args, cwd), ended: false, failed: false, images: false };
152
+ openRun.calls.push(call);
153
+ openRun.fold = undefined;
154
+ byCall.set(callId, { run: openRun, call });
155
+ },
156
+ settle(callId, failed) {
157
+ const entry = byCall.get(callId);
158
+ if (!entry || entry.call.ended) return;
159
+ entry.call.ended = true;
160
+ entry.call.failed = failed;
161
+ entry.run.fold = undefined;
162
+ },
163
+ onMessage(message) {
164
+ // Called on every streaming delta: bail before inspecting the message.
165
+ if (openRun && messagePaintsRows(message)) close();
166
+ },
167
+ closeAll: close,
168
+ reset() {
169
+ openRun = undefined;
170
+ byCall.clear();
171
+ },
172
+ expand(callId) {
173
+ const entry = byCall.get(callId);
174
+ if (!entry || entry.run.expanded) return;
175
+ entry.run.expanded = true;
176
+ entry.run.fold = undefined;
177
+ },
178
+ markImages(callId) {
179
+ const entry = byCall.get(callId);
180
+ if (!entry || entry.call.images) return;
181
+ entry.call.images = true;
182
+ entry.run.fold = undefined;
183
+ },
184
+ decisionFor(callId) {
185
+ const entry = byCall.get(callId);
186
+ if (!entry) return NORMAL;
187
+ const run = entry.run;
188
+ run.fold ??= computeFold(run);
189
+ return run.fold.get(entry.call) ?? NORMAL;
190
+ },
191
+ };
192
+ }