pi-context-kit 0.4.3 → 0.4.5
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/CHANGELOG.md +13 -0
- package/README.md +100 -87
- package/context-kit.schema.json +58 -77
- package/extensions/index.ts +227 -110
- package/node_modules/pi-hl-utils/LICENSE +21 -0
- package/node_modules/pi-hl-utils/package.json +47 -0
- package/node_modules/pi-hl-utils/src/errors.ts +9 -0
- package/node_modules/pi-hl-utils/src/evidence.ts +363 -0
- package/node_modules/pi-hl-utils/src/expand-on-click.ts +90 -0
- package/node_modules/pi-hl-utils/src/layered-settings.ts +549 -0
- package/node_modules/pi-hl-utils/src/null-optional-arguments.ts +147 -0
- package/node_modules/pi-hl-utils/src/pi-agent-session-discovery.ts +48 -0
- package/node_modules/pi-hl-utils/src/record.ts +3 -0
- package/node_modules/pi-hl-utils/src/settings-command.ts +213 -0
- package/node_modules/pi-hl-utils/src/settings-menu.ts +477 -0
- package/node_modules/pi-hl-utils/src/token-calibration.ts +39 -0
- package/node_modules/pi-hl-utils/src/ui.ts +355 -0
- package/package.json +7 -1
- package/src/command.ts +17 -0
- package/src/config.ts +1 -176
- package/src/context-kit-settings.ts +152 -0
- package/src/menu.ts +159 -0
- package/src/migrate.ts +93 -1
- package/src/settings-file.ts +10 -85
- package/src/shake.ts +87 -7
- package/src/panel.ts +0 -75
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isDeepStrictEqual } from "node:util";
|
|
3
|
+
|
|
4
|
+
import type { ImageContent, Message, TextContent, ToolCall } from "@earendil-works/pi-ai";
|
|
5
|
+
import { convertToLlm, estimateTokens, type AgentSession } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Evidence: the observed model's view of its context, without what Pi stores only for
|
|
9
|
+
* replay or display (signatures, `details`, provider/response metadata, native IDs).
|
|
10
|
+
*/
|
|
11
|
+
export type EvidenceBlock =
|
|
12
|
+
| { type: "text"; text: string }
|
|
13
|
+
| { type: "thinking"; thinking: string }
|
|
14
|
+
/** Reasoning the provider withheld; the observed model saw only an opaque payload. */
|
|
15
|
+
| { type: "thinking"; redacted: true }
|
|
16
|
+
| { type: "image"; attachment: number }
|
|
17
|
+
| { type: "toolCall"; ref: string; name: string; arguments: ToolCall["arguments"] };
|
|
18
|
+
export type EvidenceMessage =
|
|
19
|
+
| { role: "user"; content: string | EvidenceBlock[] }
|
|
20
|
+
| {
|
|
21
|
+
role: "assistant";
|
|
22
|
+
content: EvidenceBlock[];
|
|
23
|
+
/** Present only for responses that did not finish normally. */
|
|
24
|
+
stopReason?: "error" | "aborted" | "length";
|
|
25
|
+
errorMessage?: string;
|
|
26
|
+
}
|
|
27
|
+
| {
|
|
28
|
+
role: "toolResult";
|
|
29
|
+
ref: string;
|
|
30
|
+
toolName: string;
|
|
31
|
+
isError: boolean;
|
|
32
|
+
content: EvidenceBlock[];
|
|
33
|
+
};
|
|
34
|
+
export interface Evidence {
|
|
35
|
+
messages: EvidenceMessage[];
|
|
36
|
+
/** Native image attachments, referenced 1-based by `{ type: "image", attachment }` blocks. */
|
|
37
|
+
images: ImageContent[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Tool-Call Reference: compact, shared by an observed tool call and its result. Derived from
|
|
41
|
+
* the native call ID alone, so it is stable across projections and reviewer sessions and can
|
|
42
|
+
* be recomputed from the observed transcript.
|
|
43
|
+
*/
|
|
44
|
+
export function toolCallRef(toolCallId: unknown): string {
|
|
45
|
+
// Coerce rather than trust the stored shape: a malformed ID must not interrupt the consumer.
|
|
46
|
+
return createHash("sha256").update(String(toolCallId)).digest("base64url").slice(0, 8);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Every Tool-Call Reference in projected evidence with its tool name, from calls and results. */
|
|
50
|
+
export function evidenceTools(messages: readonly EvidenceMessage[]): [ref: string, tool: string][] {
|
|
51
|
+
return messages.flatMap((message): [string, string][] => {
|
|
52
|
+
if (message.role === "toolResult") return [[message.ref, message.toolName]];
|
|
53
|
+
if (message.role !== "assistant") return [];
|
|
54
|
+
return message.content.flatMap((block): [string, string][] =>
|
|
55
|
+
block.type === "toolCall" ? [[block.ref, block.name]] : [],
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Every Tool-Call Reference in projected evidence, from calls and their results. */
|
|
61
|
+
export function evidenceRefs(messages: readonly EvidenceMessage[]): string[] {
|
|
62
|
+
return evidenceTools(messages).map(([ref]) => ref);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The text fields of projected evidence that a finding may quote. */
|
|
66
|
+
export function evidenceText(messages: readonly EvidenceMessage[]): string {
|
|
67
|
+
return messages
|
|
68
|
+
.flatMap((message) =>
|
|
69
|
+
typeof message.content === "string" ? [message.content] : message.content.flatMap(blockText),
|
|
70
|
+
)
|
|
71
|
+
.join("\n");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function blockText(block: EvidenceBlock): string[] {
|
|
75
|
+
if (block.type === "text") return [block.text];
|
|
76
|
+
if (block.type === "thinking") return "thinking" in block ? [block.thinking] : [];
|
|
77
|
+
if (block.type === "toolCall")
|
|
78
|
+
return Object.values(block.arguments).filter((value) => typeof value === "string");
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Each observed message's role before Pi converted it for the model. Compaction and branch
|
|
84
|
+
* summaries, custom messages (including findings delivered back to the observed agent), and `!` command results all
|
|
85
|
+
* reach the model as `user` messages; only `user` origins are requests from the user.
|
|
86
|
+
*/
|
|
87
|
+
export function messageOrigins(
|
|
88
|
+
messages: readonly Message[],
|
|
89
|
+
sources: AgentSession["messages"],
|
|
90
|
+
): string[] {
|
|
91
|
+
// Sources that reach the model as user messages, in order, grouped by timestamp.
|
|
92
|
+
const byTime = Map.groupBy(
|
|
93
|
+
sources.flatMap((source) => {
|
|
94
|
+
const [converted] = convertToLlm([source]);
|
|
95
|
+
return converted?.role === "user" ? [{ source, content: converted.content }] : [];
|
|
96
|
+
}),
|
|
97
|
+
({ source }) => source.timestamp,
|
|
98
|
+
);
|
|
99
|
+
const used = new Set<object>();
|
|
100
|
+
const origins = messages.map((message): string | undefined =>
|
|
101
|
+
message.role === "user" ? undefined : message.role,
|
|
102
|
+
);
|
|
103
|
+
const claim = (index: number, matches: (content: Message["content"]) => boolean) => {
|
|
104
|
+
const message = messages[index];
|
|
105
|
+
if (message?.role !== "user" || origins[index]) return;
|
|
106
|
+
const match = byTime
|
|
107
|
+
.get(message.timestamp)
|
|
108
|
+
?.find(({ source, content }) => !used.has(source) && matches(content));
|
|
109
|
+
if (!match) return;
|
|
110
|
+
used.add(match.source);
|
|
111
|
+
origins[index] = match.source.role;
|
|
112
|
+
};
|
|
113
|
+
// Exact conversions first; then, within a timestamp, the k-th unmatched message pairs with the
|
|
114
|
+
// k-th unused source (a message a context hook rewrote, such as with images removed).
|
|
115
|
+
for (const [index, message] of messages.entries())
|
|
116
|
+
claim(index, (content) => isDeepStrictEqual(content, message.content));
|
|
117
|
+
for (const index of messages.keys()) claim(index, () => true);
|
|
118
|
+
return origins.map((origin) => origin ?? "unknown");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Pi's compaction estimate for one image, plus a token for a longer attachment index.
|
|
122
|
+
const imageTokens =
|
|
123
|
+
estimateTokens({
|
|
124
|
+
role: "user",
|
|
125
|
+
content: [{ type: "image", data: "", mimeType: "image/png" }],
|
|
126
|
+
timestamp: 0,
|
|
127
|
+
}) + 1;
|
|
128
|
+
|
|
129
|
+
/** Pi's chars/4 estimate of projected evidence messages, plus Pi's per-image estimate. */
|
|
130
|
+
export function evidenceTokens({ messages, images }: Evidence): number {
|
|
131
|
+
return Math.ceil(JSON.stringify(messages).length / 4) + images.length * imageTokens;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** One projected message with the native images its attachment blocks reference. */
|
|
135
|
+
export interface EvidenceItem {
|
|
136
|
+
message: EvidenceMessage;
|
|
137
|
+
images: ImageContent[];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Builds the text that replaces what `shortenEvidence` cut, given the number of omitted characters. */
|
|
141
|
+
export type OmissionMarker = (omitted: number) => string;
|
|
142
|
+
|
|
143
|
+
/** Tokens evidence items add to a serialized evidence list, by Pi's chars/4 heuristic. */
|
|
144
|
+
export function evidenceItemsCost(items: readonly EvidenceItem[]): number {
|
|
145
|
+
return items.reduce(
|
|
146
|
+
(total, { message, images }) =>
|
|
147
|
+
total + Math.ceil((JSON.stringify(message).length + 1) / 4) + images.length * imageTokens,
|
|
148
|
+
0,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Shorten every text (and serialized tool-call arguments) longer than `limit` characters,
|
|
154
|
+
* appending `marker(omitted)`. Items that do not change keep their identity.
|
|
155
|
+
*/
|
|
156
|
+
export function shortenEvidence(
|
|
157
|
+
items: readonly EvidenceItem[],
|
|
158
|
+
limit: number,
|
|
159
|
+
marker: OmissionMarker,
|
|
160
|
+
): EvidenceItem[] {
|
|
161
|
+
return items.map((item) => {
|
|
162
|
+
let changed = false;
|
|
163
|
+
const cut = (text: string) => {
|
|
164
|
+
const shortened = `${text.slice(0, limit)}\n${marker(text.length - limit)}`;
|
|
165
|
+
if (shortened.length >= text.length) return text;
|
|
166
|
+
changed = true;
|
|
167
|
+
return shortened;
|
|
168
|
+
};
|
|
169
|
+
const block = (part: EvidenceBlock): EvidenceBlock => {
|
|
170
|
+
if (part.type === "text") return { type: "text", text: cut(part.text) };
|
|
171
|
+
if (part.type === "thinking" && "thinking" in part)
|
|
172
|
+
return { type: "thinking", thinking: cut(part.thinking) };
|
|
173
|
+
if (part.type !== "toolCall") return part;
|
|
174
|
+
const json = JSON.stringify(part.arguments);
|
|
175
|
+
const shortened = cut(json);
|
|
176
|
+
// Oversized arguments become their marked JSON prefix, which no longer parses.
|
|
177
|
+
return shortened === json ? part : { ...part, arguments: { shortenedJson: shortened } };
|
|
178
|
+
};
|
|
179
|
+
const { message } = item;
|
|
180
|
+
const next: EvidenceMessage =
|
|
181
|
+
message.role === "user"
|
|
182
|
+
? {
|
|
183
|
+
role: "user",
|
|
184
|
+
content: Array.isArray(message.content)
|
|
185
|
+
? message.content.map(block)
|
|
186
|
+
: cut(message.content),
|
|
187
|
+
}
|
|
188
|
+
: { ...message, content: message.content.map(block) };
|
|
189
|
+
return changed ? { message: next, images: item.images } : item;
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The items unchanged if they cost at most `allowance` tokens, else shortened with the longest
|
|
195
|
+
* per-string limit that fits; `undefined` when even fully shortened they do not fit.
|
|
196
|
+
*/
|
|
197
|
+
export function fitEvidence(
|
|
198
|
+
items: readonly EvidenceItem[],
|
|
199
|
+
allowance: number,
|
|
200
|
+
marker: OmissionMarker,
|
|
201
|
+
): EvidenceItem[] | undefined {
|
|
202
|
+
if (evidenceItemsCost(items) <= allowance) return [...items];
|
|
203
|
+
let low = 0;
|
|
204
|
+
let high = JSON.stringify(items.map(({ message }) => message)).length;
|
|
205
|
+
if (evidenceItemsCost(shortenEvidence(items, low, marker)) > allowance) return undefined;
|
|
206
|
+
while (low < high) {
|
|
207
|
+
const middle = Math.ceil((low + high) / 2);
|
|
208
|
+
if (evidenceItemsCost(shortenEvidence(items, middle, marker)) <= allowance) low = middle;
|
|
209
|
+
else high = middle - 1;
|
|
210
|
+
}
|
|
211
|
+
return shortenEvidence(items, low, marker);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Join individually projected messages, renumbering their image attachments. */
|
|
215
|
+
export function combineEvidence(items: readonly EvidenceItem[]): Evidence {
|
|
216
|
+
const images: ImageContent[] = [];
|
|
217
|
+
const messages = items.map(({ message, images: own }) => {
|
|
218
|
+
const offset = images.length;
|
|
219
|
+
images.push(...own);
|
|
220
|
+
if (!offset || !own.length || !Array.isArray(message.content)) return message;
|
|
221
|
+
const content = message.content.map((block) =>
|
|
222
|
+
block.type === "image" ? { ...block, attachment: block.attachment + offset } : block,
|
|
223
|
+
);
|
|
224
|
+
return { ...message, content };
|
|
225
|
+
});
|
|
226
|
+
return { messages, images };
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** Opt-in cap on tool-result text; projection without one never shortens any text. */
|
|
230
|
+
export interface ToolResultCap {
|
|
231
|
+
/** Longest tool-result text kept whole, in characters; longer text keeps its head and tail. */
|
|
232
|
+
limit: number;
|
|
233
|
+
/** Builds the text that replaces the omitted middle, given the number of omitted characters. */
|
|
234
|
+
marker: OmissionMarker;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Options for projecting evidence; every option is opt-in. */
|
|
238
|
+
export interface ProjectionOptions {
|
|
239
|
+
/** Cap each tool result's text, so oversized results keep a head, a tail, and a marker. */
|
|
240
|
+
toolResultCap?: ToolResultCap | undefined;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Tool-result texts among `messages` that the tool-result cap shortens. */
|
|
244
|
+
export function cappedResults(
|
|
245
|
+
messages: readonly Message[],
|
|
246
|
+
cap: ToolResultCap | undefined,
|
|
247
|
+
): number {
|
|
248
|
+
if (!cap) return 0;
|
|
249
|
+
return messages
|
|
250
|
+
.flatMap((message) => (message.role === "toolResult" ? message.content : []))
|
|
251
|
+
.filter((block) => block.type === "text" && capText(block.text, cap) !== block.text).length;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* `text` unchanged if it is within `cap.limit` characters or capping would not shorten it, else
|
|
256
|
+
* its head and tail (about half the limit each) around `cap.marker(omitted)`. Pure, so equal
|
|
257
|
+
* input always gives equal output; a cut never splits a surrogate pair.
|
|
258
|
+
*/
|
|
259
|
+
export function capText(text: string, cap: ToolResultCap): string {
|
|
260
|
+
if (text.length <= cap.limit) return text;
|
|
261
|
+
let head = Math.ceil(cap.limit / 2);
|
|
262
|
+
let tail = text.length - (cap.limit - head);
|
|
263
|
+
const high = (index: number) => {
|
|
264
|
+
const code = text.charCodeAt(index);
|
|
265
|
+
return code >= 0xd800 && code <= 0xdbff;
|
|
266
|
+
};
|
|
267
|
+
const low = (index: number) => {
|
|
268
|
+
const code = text.charCodeAt(index);
|
|
269
|
+
return code >= 0xdc00 && code <= 0xdfff;
|
|
270
|
+
};
|
|
271
|
+
if (head > 0 && high(head - 1)) head--;
|
|
272
|
+
if (tail < text.length && low(tail)) tail++;
|
|
273
|
+
const capped = `${text.slice(0, head)}\n${cap.marker(tail - head)}\n${text.slice(tail)}`;
|
|
274
|
+
return capped.length >= text.length ? text : capped;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Project observed messages in order; attachment indexes restart at 1 for each projection.
|
|
279
|
+
* Without `options.toolResultCap`, text is never shortened.
|
|
280
|
+
*/
|
|
281
|
+
export function projectEvidence(
|
|
282
|
+
messages: readonly Message[],
|
|
283
|
+
options: ProjectionOptions = {},
|
|
284
|
+
): Evidence {
|
|
285
|
+
const images: ImageContent[] = [];
|
|
286
|
+
const { toolResultCap } = options;
|
|
287
|
+
/** User and tool-result content; unknown future block types are omitted, not forwarded. */
|
|
288
|
+
const media = (blocks: readonly (TextContent | ImageContent)[]) =>
|
|
289
|
+
blocks.flatMap((block): EvidenceBlock[] => {
|
|
290
|
+
if (block.type === "text") return [{ type: "text", text: block.text }];
|
|
291
|
+
if (block.type === "image") {
|
|
292
|
+
images.push(block);
|
|
293
|
+
return [{ type: "image", attachment: images.length }];
|
|
294
|
+
}
|
|
295
|
+
return [];
|
|
296
|
+
});
|
|
297
|
+
const projected = messages.flatMap((message): EvidenceMessage[] => {
|
|
298
|
+
switch (message.role) {
|
|
299
|
+
case "user":
|
|
300
|
+
return [
|
|
301
|
+
{
|
|
302
|
+
role: "user",
|
|
303
|
+
content: Array.isArray(message.content) ? media(message.content) : message.content,
|
|
304
|
+
},
|
|
305
|
+
];
|
|
306
|
+
case "toolResult":
|
|
307
|
+
return [
|
|
308
|
+
{
|
|
309
|
+
role: "toolResult",
|
|
310
|
+
ref: toolCallRef(message.toolCallId),
|
|
311
|
+
toolName: message.toolName,
|
|
312
|
+
isError: message.isError,
|
|
313
|
+
content: media(message.content).map((block) =>
|
|
314
|
+
toolResultCap && block.type === "text"
|
|
315
|
+
? { type: "text", text: capText(block.text, toolResultCap) }
|
|
316
|
+
: block,
|
|
317
|
+
),
|
|
318
|
+
},
|
|
319
|
+
];
|
|
320
|
+
case "assistant": {
|
|
321
|
+
const content = message.content.flatMap((block): EvidenceBlock[] => {
|
|
322
|
+
if (block.type === "text") return [{ type: "text", text: block.text }];
|
|
323
|
+
if (block.type === "thinking") {
|
|
324
|
+
if (block.redacted) return [{ type: "thinking", redacted: true }];
|
|
325
|
+
return block.thinking.trim() ? [{ type: "thinking", thinking: block.thinking }] : [];
|
|
326
|
+
}
|
|
327
|
+
if (block.type === "toolCall")
|
|
328
|
+
return [
|
|
329
|
+
{
|
|
330
|
+
type: "toolCall",
|
|
331
|
+
ref: toolCallRef(block.id),
|
|
332
|
+
name: block.name,
|
|
333
|
+
arguments: block.arguments,
|
|
334
|
+
},
|
|
335
|
+
];
|
|
336
|
+
return [];
|
|
337
|
+
});
|
|
338
|
+
const stop = message.stopReason;
|
|
339
|
+
if (stop !== "error" && stop !== "aborted" && stop !== "length")
|
|
340
|
+
return [{ role: "assistant", content }];
|
|
341
|
+
const abnormal: EvidenceMessage = { role: "assistant", content, stopReason: stop };
|
|
342
|
+
if (message.errorMessage) abnormal.errorMessage = message.errorMessage;
|
|
343
|
+
return [abnormal];
|
|
344
|
+
}
|
|
345
|
+
default:
|
|
346
|
+
// System messages reach the consumer through its own setup summary; unknown roles are omitted.
|
|
347
|
+
return [];
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
return { messages: projected, images };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** Project one observed message; `undefined` when its role is omitted from evidence. */
|
|
354
|
+
export function projectEvidenceItem(
|
|
355
|
+
message: Message,
|
|
356
|
+
options?: ProjectionOptions,
|
|
357
|
+
): EvidenceItem | undefined {
|
|
358
|
+
const {
|
|
359
|
+
messages: [only],
|
|
360
|
+
images,
|
|
361
|
+
} = projectEvidence([message], options);
|
|
362
|
+
return only ? { message: only, images } : undefined;
|
|
363
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { EntryRenderer, MessageRenderer } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import {
|
|
3
|
+
MouseRegion,
|
|
4
|
+
type Component,
|
|
5
|
+
type TuiMouseEvent,
|
|
6
|
+
type TuiMouseEventResult,
|
|
7
|
+
} from "@earendil-works/pi-tui";
|
|
8
|
+
|
|
9
|
+
/** A custom message or entry renderer: Pi's `MessageRenderer` and `EntryRenderer` have this shape. */
|
|
10
|
+
type ItemRenderer<Item extends WeakKey, Options extends { expanded: boolean }, RenderTheme> = (
|
|
11
|
+
item: Item,
|
|
12
|
+
options: Options,
|
|
13
|
+
theme: RenderTheme,
|
|
14
|
+
) => Component | undefined;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A clicked item's own view, kept per message or entry object because Pi rebuilds their components
|
|
18
|
+
* on invalidation. `global` is Pi's expanded flag when the item was clicked: once ctrl+o changes it,
|
|
19
|
+
* the click is forgotten, as Pi's tool rows forget theirs.
|
|
20
|
+
*/
|
|
21
|
+
const clickedViews = new WeakMap<WeakKey, { global: boolean; expanded: boolean }>();
|
|
22
|
+
|
|
23
|
+
function viewFor(item: WeakKey, global: boolean): boolean {
|
|
24
|
+
const clicked = clickedViews.get(item);
|
|
25
|
+
if (clicked?.global === global) return clicked.expanded;
|
|
26
|
+
clickedViews.delete(item);
|
|
27
|
+
return global;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Renders one item and swaps between its views on a left click its content does not handle. */
|
|
31
|
+
class ExpandOnClick implements Component {
|
|
32
|
+
private region: MouseRegion;
|
|
33
|
+
|
|
34
|
+
constructor(
|
|
35
|
+
child: Component,
|
|
36
|
+
private readonly item: WeakKey,
|
|
37
|
+
private readonly global: boolean,
|
|
38
|
+
private readonly build: (expanded: boolean) => Component | undefined,
|
|
39
|
+
) {
|
|
40
|
+
this.region = this.wrap(child);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private wrap(child: Component): MouseRegion {
|
|
44
|
+
return new MouseRegion(child, (event) => {
|
|
45
|
+
if (event.type !== "click" || event.button !== "left") return undefined;
|
|
46
|
+
const expanded = !viewFor(this.item, this.global);
|
|
47
|
+
const next = this.build(expanded);
|
|
48
|
+
if (!next) return undefined;
|
|
49
|
+
clickedViews.set(this.item, { global: this.global, expanded });
|
|
50
|
+
this.region = this.wrap(next);
|
|
51
|
+
return { handled: true };
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
render(width: number): string[] {
|
|
56
|
+
return this.region.render(width);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
|
|
60
|
+
return this.region.handleMouse(event);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
invalidate(): void {
|
|
64
|
+
this.region.invalidate();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function expandOnClick<Item extends WeakKey, Options extends { expanded: boolean }, RenderTheme>(
|
|
69
|
+
render: ItemRenderer<Item, Options, RenderTheme>,
|
|
70
|
+
): ItemRenderer<Item, Options, RenderTheme> {
|
|
71
|
+
return (item, options, theme) => {
|
|
72
|
+
const build = (expanded: boolean) => render(item, { ...options, expanded }, theme);
|
|
73
|
+
const child = build(viewFor(item, options.expanded));
|
|
74
|
+
return child && new ExpandOnClick(child, item, options.expanded, build);
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Lets a click toggle a custom message between its Collapsed and Expanded View, as Pi's tool rows
|
|
80
|
+
* and built-in messages do. Pi's custom-message host has no click handling of its own, so wrap the
|
|
81
|
+
* renderer passed to `registerMessageRenderer`. ctrl+o still sets every item.
|
|
82
|
+
*/
|
|
83
|
+
export function expandMessageOnClick<T>(render: MessageRenderer<T>): MessageRenderer<T> {
|
|
84
|
+
return expandOnClick(render);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** `expandMessageOnClick` for the renderer passed to `registerEntryRenderer`. */
|
|
88
|
+
export function expandEntryOnClick<T>(render: EntryRenderer<T>): EntryRenderer<T> {
|
|
89
|
+
return expandOnClick(render);
|
|
90
|
+
}
|