pi-context-kit 0.4.3 → 0.4.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/CHANGELOG.md +6 -0
- package/README.md +100 -87
- package/context-kit.schema.json +58 -77
- package/extensions/index.ts +200 -107
- 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 +513 -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/panel.ts +0 -75
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DynamicBorder,
|
|
3
|
+
getSelectListTheme,
|
|
4
|
+
getSettingsListTheme,
|
|
5
|
+
type Theme,
|
|
6
|
+
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import {
|
|
8
|
+
fuzzyFilter,
|
|
9
|
+
getKeybindings,
|
|
10
|
+
Input,
|
|
11
|
+
SelectList,
|
|
12
|
+
SettingsList,
|
|
13
|
+
wrapTextWithAnsi,
|
|
14
|
+
type Component,
|
|
15
|
+
type SelectItem,
|
|
16
|
+
type SettingItem,
|
|
17
|
+
type TUI,
|
|
18
|
+
type TuiMouseEvent,
|
|
19
|
+
type TuiMouseEventResult,
|
|
20
|
+
} from "@earendil-works/pi-tui";
|
|
21
|
+
|
|
22
|
+
import { errorText } from "./errors.js";
|
|
23
|
+
import type { LayeredSettingScope } from "./layered-settings.js";
|
|
24
|
+
import { SEPARATOR, statusMark } from "./ui.js";
|
|
25
|
+
|
|
26
|
+
/** The theme methods the settings widgets draw with; Pi's `Theme` satisfies it. */
|
|
27
|
+
export type SettingsMenuTheme = Pick<Theme, "fg" | "bold">;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The value after `current` in `values`, wrapping around; `undefined` when `values` is empty.
|
|
31
|
+
* A `current` that is not in `values` yields the first value.
|
|
32
|
+
*/
|
|
33
|
+
export function nextCycleValue<Value extends string>(
|
|
34
|
+
values: readonly Value[],
|
|
35
|
+
current: string,
|
|
36
|
+
): Value | undefined {
|
|
37
|
+
return values[(values.findIndex((value) => value === current) + 1) % values.length];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Single-line value entry with inline validation. `submit` may throw a user-facing message,
|
|
42
|
+
* which is shown in place of the hint until the next keystroke.
|
|
43
|
+
*/
|
|
44
|
+
export class ValueInput implements Component {
|
|
45
|
+
private readonly input: Input;
|
|
46
|
+
private error: string | undefined;
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
private readonly title: string,
|
|
50
|
+
private readonly hint: string,
|
|
51
|
+
private readonly theme: SettingsMenuTheme,
|
|
52
|
+
submit: (text: string) => void,
|
|
53
|
+
cancel: () => void,
|
|
54
|
+
) {
|
|
55
|
+
this.input = new Input({ placeholder: hint });
|
|
56
|
+
this.input.focused = true;
|
|
57
|
+
this.input.onSubmit = (text) => {
|
|
58
|
+
try {
|
|
59
|
+
submit(text);
|
|
60
|
+
} catch (cause) {
|
|
61
|
+
this.error = errorText(cause);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
this.input.onEscape = cancel;
|
|
65
|
+
}
|
|
66
|
+
handleInput(data: string): void {
|
|
67
|
+
this.error = undefined;
|
|
68
|
+
this.input.handleInput(data);
|
|
69
|
+
}
|
|
70
|
+
render(width: number): string[] {
|
|
71
|
+
return [
|
|
72
|
+
this.theme.bold(this.title),
|
|
73
|
+
...this.input.render(width),
|
|
74
|
+
this.error
|
|
75
|
+
? `${statusMark(this.theme, "failed")} ${this.theme.fg("error", this.error)}`
|
|
76
|
+
: this.theme.fg("dim", this.hint),
|
|
77
|
+
];
|
|
78
|
+
}
|
|
79
|
+
invalidate(): void {
|
|
80
|
+
this.input.invalidate();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const listActions = [
|
|
85
|
+
"tui.select.up",
|
|
86
|
+
"tui.select.down",
|
|
87
|
+
"tui.select.confirm",
|
|
88
|
+
"tui.select.cancel",
|
|
89
|
+
] as const;
|
|
90
|
+
|
|
91
|
+
/** Fuzzy-searchable model list whose first choice is `inherit`. */
|
|
92
|
+
export class ModelPicker implements Component {
|
|
93
|
+
private readonly input = new Input({ placeholder: "type to search" });
|
|
94
|
+
private list: SelectList;
|
|
95
|
+
|
|
96
|
+
constructor(
|
|
97
|
+
private readonly models: readonly string[],
|
|
98
|
+
private readonly choose: (value: string) => void,
|
|
99
|
+
private readonly cancel: () => void,
|
|
100
|
+
) {
|
|
101
|
+
this.input.focused = true;
|
|
102
|
+
this.list = this.createList("");
|
|
103
|
+
}
|
|
104
|
+
private createList(query: string): SelectList {
|
|
105
|
+
const items: SelectItem[] = ["inherit", ...this.models].map((value) => ({
|
|
106
|
+
value,
|
|
107
|
+
label: value,
|
|
108
|
+
}));
|
|
109
|
+
const list = new SelectList(
|
|
110
|
+
query ? fuzzyFilter(items, query, (item) => item.value) : items,
|
|
111
|
+
10,
|
|
112
|
+
getSelectListTheme(),
|
|
113
|
+
);
|
|
114
|
+
list.onSelect = (item) => this.choose(item.value);
|
|
115
|
+
list.onCancel = this.cancel;
|
|
116
|
+
return list;
|
|
117
|
+
}
|
|
118
|
+
handleInput(data: string): void {
|
|
119
|
+
const bindings = getKeybindings();
|
|
120
|
+
if (listActions.some((action) => bindings.matches(data, action))) {
|
|
121
|
+
this.list.handleInput(data);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
this.input.handleInput(data);
|
|
125
|
+
this.list = this.createList(this.input.getValue());
|
|
126
|
+
}
|
|
127
|
+
render(width: number): string[] {
|
|
128
|
+
return [...this.input.render(width), ...this.list.render(width)];
|
|
129
|
+
}
|
|
130
|
+
invalidate(): void {
|
|
131
|
+
this.input.invalidate();
|
|
132
|
+
this.list.invalidate();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Native UI collaborators a settings menu needs, as supplied by `ctx.ui.custom`. */
|
|
137
|
+
export interface SettingsMenuUi {
|
|
138
|
+
tui: TUI;
|
|
139
|
+
theme: SettingsMenuTheme;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** `effective · source`, the usual `inEffect` text for `cycleDisplay`. */
|
|
143
|
+
export function effectiveWithSource(effective: string, source: string): string {
|
|
144
|
+
return `${effective}${SEPARATOR}${source}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* A cycled option's display: the selected scope's own value, or what it inherits, noting when a
|
|
149
|
+
* higher-precedence scope overrides it. `inEffect` describes the effective value and its source.
|
|
150
|
+
*/
|
|
151
|
+
export function cycleDisplay(input: {
|
|
152
|
+
own: string | undefined;
|
|
153
|
+
inEffect: string;
|
|
154
|
+
source: string;
|
|
155
|
+
scope: string;
|
|
156
|
+
}): string {
|
|
157
|
+
if (input.own === undefined) return `inherit (${input.inEffect})`;
|
|
158
|
+
return input.source === input.scope ? input.own : `${input.own} (overridden: ${input.inEffect})`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** What the frame shows from an extension's view: its live status lines and writable scopes. */
|
|
162
|
+
export interface SettingsMenuView {
|
|
163
|
+
headline: readonly string[];
|
|
164
|
+
/** Writable scopes, in the order the Scope row cycles them. */
|
|
165
|
+
scopes: readonly LayeredSettingScope[];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The fields every editable row shows. */
|
|
169
|
+
interface SettingRowFields {
|
|
170
|
+
id: string;
|
|
171
|
+
label: string;
|
|
172
|
+
description: string;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* One row of a settings menu, in display order. `scope` is the frame's Scope row. Every change
|
|
177
|
+
* function receives the scope being written. A `cycle` row steps through `values`, starting after
|
|
178
|
+
* the scope's own value `own` (`undefined` when that scope inherits). An `input` row takes typed
|
|
179
|
+
* text and a `pick` row chooses from `choices`; both throw a user-facing message for invalid text.
|
|
180
|
+
* An `action` row runs `run` when chosen.
|
|
181
|
+
*/
|
|
182
|
+
export type SettingRow<Change> =
|
|
183
|
+
| { kind: "scope" }
|
|
184
|
+
| (SettingRowFields & {
|
|
185
|
+
kind: "cycle";
|
|
186
|
+
values: readonly string[];
|
|
187
|
+
own: string | undefined;
|
|
188
|
+
/** The effective value and its source, as `effectiveWithSource` shows them. */
|
|
189
|
+
inEffect: string;
|
|
190
|
+
source: string;
|
|
191
|
+
change: (value: string, scope: LayeredSettingScope) => Change;
|
|
192
|
+
})
|
|
193
|
+
| (SettingRowFields & {
|
|
194
|
+
kind: "input";
|
|
195
|
+
value: string;
|
|
196
|
+
hint: string;
|
|
197
|
+
change: (text: string, scope: LayeredSettingScope) => Change;
|
|
198
|
+
})
|
|
199
|
+
| (SettingRowFields & {
|
|
200
|
+
kind: "pick";
|
|
201
|
+
value: string;
|
|
202
|
+
choices: readonly string[];
|
|
203
|
+
change: (value: string, scope: LayeredSettingScope) => Change;
|
|
204
|
+
})
|
|
205
|
+
| (SettingRowFields & {
|
|
206
|
+
kind: "action";
|
|
207
|
+
value: string;
|
|
208
|
+
run: () => Promise<void>;
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
/** An extension's settings authority behind a menu: it reads the view and writes one change. */
|
|
212
|
+
export interface SettingsMenuSpec<View extends SettingsMenuView, Change> {
|
|
213
|
+
title: string;
|
|
214
|
+
/** Read the host fresh; a read that throws is shown inline and the last good view is kept. */
|
|
215
|
+
read(): View;
|
|
216
|
+
/** The rows for `view` with `scope` selected, in display order. */
|
|
217
|
+
rows(view: View, scope: LayeredSettingScope): readonly SettingRow<Change>[];
|
|
218
|
+
/** Persist one change at `scope`; a rejection is shown inline. */
|
|
219
|
+
apply(scope: LayeredSettingScope, change: Change): Promise<void>;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Pi's settings-screen look (a border, an accent title, live status lines, then a native settings
|
|
224
|
+
* list) plus a serialized edit queue. An extension supplies its rows and host as a
|
|
225
|
+
* {@link SettingsMenuSpec}.
|
|
226
|
+
*/
|
|
227
|
+
export class SettingsMenu<View extends SettingsMenuView, Change> implements Component {
|
|
228
|
+
/** The last failed edit, shown under the list until the next edit starts. */
|
|
229
|
+
private error: string | undefined;
|
|
230
|
+
/** Edits run one at a time so each reads the result of the previous one. */
|
|
231
|
+
private pending: Promise<void> = Promise.resolve();
|
|
232
|
+
private view: View;
|
|
233
|
+
private scope: LayeredSettingScope = "session";
|
|
234
|
+
private rows: readonly SettingRow<Change>[];
|
|
235
|
+
private items = new Map<string, SettingItem>();
|
|
236
|
+
private list: SettingsList | undefined;
|
|
237
|
+
/** The row last changed or opened; a rebuilt list selects it unless a new row appears. */
|
|
238
|
+
private lastRow = "scope";
|
|
239
|
+
private submenuOpen = false;
|
|
240
|
+
private rebuildPending = false;
|
|
241
|
+
private readonly border: DynamicBorder;
|
|
242
|
+
|
|
243
|
+
constructor(
|
|
244
|
+
private readonly spec: SettingsMenuSpec<View, Change>,
|
|
245
|
+
private readonly ui: SettingsMenuUi,
|
|
246
|
+
private readonly done: () => void,
|
|
247
|
+
) {
|
|
248
|
+
this.border = new DynamicBorder((text) => ui.theme.fg("border", text));
|
|
249
|
+
this.view = spec.read();
|
|
250
|
+
this.rows = spec.rows(this.view, this.scope);
|
|
251
|
+
this.rebuild();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Resolves once every edit started so far has been applied or rejected. */
|
|
255
|
+
settled(): Promise<void> {
|
|
256
|
+
return this.pending;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Re-read the host after external state changes, such as a Review starting. */
|
|
260
|
+
refresh(): void {
|
|
261
|
+
try {
|
|
262
|
+
this.view = this.spec.read();
|
|
263
|
+
} catch (cause) {
|
|
264
|
+
this.error = errorText(cause);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if (!this.view.scopes.includes(this.scope)) this.scope = "session";
|
|
268
|
+
this.rows = this.spec.rows(this.view, this.scope);
|
|
269
|
+
this.sync();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Update rows in place; rebuild the list only when the set of rows changes. */
|
|
273
|
+
private sync(): void {
|
|
274
|
+
const ids = this.rows.map(rowId);
|
|
275
|
+
if (ids.length === this.items.size && ids.every((id) => this.items.has(id))) {
|
|
276
|
+
for (const row of this.rows) {
|
|
277
|
+
const item = this.items.get(rowId(row));
|
|
278
|
+
if (item) Object.assign(item, this.toItem(row));
|
|
279
|
+
}
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (this.submenuOpen) this.rebuildPending = true;
|
|
283
|
+
else this.rebuild();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Create a fresh native list; a newly listed row is selected, otherwise the last row. */
|
|
287
|
+
private rebuild(): void {
|
|
288
|
+
const previous = new Set(this.items.keys());
|
|
289
|
+
const added = this.rows.map(rowId).find((id) => !previous.has(id));
|
|
290
|
+
this.rebuildPending = false;
|
|
291
|
+
this.items = new Map(this.rows.map((row) => [rowId(row), this.toItem(row)]));
|
|
292
|
+
this.list = new SettingsList(
|
|
293
|
+
[...this.items.values()],
|
|
294
|
+
this.items.size,
|
|
295
|
+
getSettingsListTheme(),
|
|
296
|
+
(id, value) => this.change(id, value),
|
|
297
|
+
this.done,
|
|
298
|
+
);
|
|
299
|
+
this.list.selectItem(added ?? this.lastRow);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private toItem(row: SettingRow<Change>): SettingItem {
|
|
303
|
+
switch (row.kind) {
|
|
304
|
+
case "scope":
|
|
305
|
+
return {
|
|
306
|
+
id: "scope",
|
|
307
|
+
label: "Scope",
|
|
308
|
+
currentValue: this.scope,
|
|
309
|
+
values: [...this.view.scopes],
|
|
310
|
+
description: "Where edits are written",
|
|
311
|
+
};
|
|
312
|
+
case "cycle":
|
|
313
|
+
return {
|
|
314
|
+
id: row.id,
|
|
315
|
+
label: row.label,
|
|
316
|
+
description: row.description,
|
|
317
|
+
currentValue: this.cycleText(row),
|
|
318
|
+
values: [...row.values],
|
|
319
|
+
};
|
|
320
|
+
case "input":
|
|
321
|
+
return {
|
|
322
|
+
id: row.id,
|
|
323
|
+
label: row.label,
|
|
324
|
+
description: row.description,
|
|
325
|
+
currentValue: row.value,
|
|
326
|
+
submenu: this.submenu(row.id, (close) => {
|
|
327
|
+
return new ValueInput(
|
|
328
|
+
row.id,
|
|
329
|
+
row.hint,
|
|
330
|
+
this.ui.theme,
|
|
331
|
+
(text) => {
|
|
332
|
+
this.apply(row.change(text, this.scope));
|
|
333
|
+
close();
|
|
334
|
+
},
|
|
335
|
+
close,
|
|
336
|
+
);
|
|
337
|
+
}),
|
|
338
|
+
};
|
|
339
|
+
case "pick":
|
|
340
|
+
return {
|
|
341
|
+
id: row.id,
|
|
342
|
+
label: row.label,
|
|
343
|
+
description: row.description,
|
|
344
|
+
currentValue: row.value,
|
|
345
|
+
submenu: this.submenu(row.id, (close) => {
|
|
346
|
+
return new ModelPicker(
|
|
347
|
+
row.choices,
|
|
348
|
+
(value) => {
|
|
349
|
+
this.apply(row.change(value, this.scope));
|
|
350
|
+
close();
|
|
351
|
+
},
|
|
352
|
+
close,
|
|
353
|
+
);
|
|
354
|
+
}),
|
|
355
|
+
};
|
|
356
|
+
case "action":
|
|
357
|
+
return {
|
|
358
|
+
id: row.id,
|
|
359
|
+
label: row.label,
|
|
360
|
+
description: row.description,
|
|
361
|
+
currentValue: row.value,
|
|
362
|
+
values: [row.value],
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** This scope's own value, or what it inherits; notes when another scope overrides it. */
|
|
368
|
+
private cycleText(row: CycleRow<Change>): string {
|
|
369
|
+
return cycleDisplay({
|
|
370
|
+
own: row.own,
|
|
371
|
+
inEffect: row.inEffect,
|
|
372
|
+
source: row.source,
|
|
373
|
+
scope: this.scope,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** Track an open submenu so a rebuild waits for it to close. */
|
|
378
|
+
private submenu(
|
|
379
|
+
id: string,
|
|
380
|
+
open: (close: () => void) => Component,
|
|
381
|
+
): NonNullable<SettingItem["submenu"]> {
|
|
382
|
+
return (_value, done) => {
|
|
383
|
+
this.submenuOpen = true;
|
|
384
|
+
this.lastRow = id;
|
|
385
|
+
return open(() => {
|
|
386
|
+
this.submenuOpen = false;
|
|
387
|
+
done();
|
|
388
|
+
if (this.rebuildPending) this.rebuild();
|
|
389
|
+
});
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private change(id: string, value: string): void {
|
|
394
|
+
if (id === "scope") {
|
|
395
|
+
const scope = this.view.scopes.find((name) => name === value);
|
|
396
|
+
if (scope) this.scope = scope;
|
|
397
|
+
this.lastRow = id;
|
|
398
|
+
this.refresh();
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const row = this.rows.find((item) => rowId(item) === id);
|
|
402
|
+
if (row?.kind === "action") this.run(row.run);
|
|
403
|
+
if (row?.kind !== "cycle") return;
|
|
404
|
+
this.lastRow = id;
|
|
405
|
+
const scope = this.scope;
|
|
406
|
+
this.run(() => this.cycle(id, scope));
|
|
407
|
+
// The list already overwrote the row with its proposal; show the current value until the
|
|
408
|
+
// write settles and refresh() shows the new one.
|
|
409
|
+
const item = this.items.get(id);
|
|
410
|
+
if (item) item.currentValue = this.cycleText(row);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Write the value after the scope's own value, read when the queued edit runs so an earlier
|
|
415
|
+
* edit is counted. The list's proposal is not used: the row's display text is not a value.
|
|
416
|
+
*/
|
|
417
|
+
private async cycle(id: string, scope: LayeredSettingScope): Promise<void> {
|
|
418
|
+
const row = this.spec.rows(this.spec.read(), scope).find((item) => rowId(item) === id);
|
|
419
|
+
if (row?.kind !== "cycle") return;
|
|
420
|
+
const next = nextCycleValue(row.values, row.own ?? "inherit") ?? "inherit";
|
|
421
|
+
await this.spec.apply(scope, row.change(next, scope));
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Apply a change already built for the current scope. */
|
|
425
|
+
private apply(change: Change): void {
|
|
426
|
+
const scope = this.scope;
|
|
427
|
+
this.run(() => this.spec.apply(scope, change));
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** Queue an edit; a failure shows inline, and the menu refreshes once it settles. */
|
|
431
|
+
private run(task: () => Promise<void>): void {
|
|
432
|
+
this.error = undefined;
|
|
433
|
+
this.pending = this.pending
|
|
434
|
+
.then(task)
|
|
435
|
+
.catch((cause: unknown) => {
|
|
436
|
+
this.error = errorText(cause);
|
|
437
|
+
})
|
|
438
|
+
.finally(() => {
|
|
439
|
+
this.refresh();
|
|
440
|
+
this.ui.tui.requestRender();
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
handleInput(data: string): void {
|
|
445
|
+
this.list?.handleInput(data);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
handleMouse(event: TuiMouseEvent): TuiMouseEventResult | undefined {
|
|
449
|
+
return this.list?.handleMouse(event);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
render(width: number): string[] {
|
|
453
|
+
const { theme } = this.ui;
|
|
454
|
+
const indented = (text: string) => wrapTextWithAnsi(` ${text}`, width);
|
|
455
|
+
return [
|
|
456
|
+
...this.border.render(width),
|
|
457
|
+
...indented(theme.fg("accent", theme.bold(this.spec.title))),
|
|
458
|
+
...this.view.headline.flatMap(indented),
|
|
459
|
+
"",
|
|
460
|
+
...(this.list?.render(width) ?? []),
|
|
461
|
+
...(this.error
|
|
462
|
+
? indented(`${statusMark(theme, "failed")} ${theme.fg("error", this.error)}`)
|
|
463
|
+
: []),
|
|
464
|
+
...this.border.render(width),
|
|
465
|
+
];
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
invalidate(): void {
|
|
469
|
+
this.list?.invalidate();
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
type CycleRow<Change> = Extract<SettingRow<Change>, { kind: "cycle" }>;
|
|
474
|
+
|
|
475
|
+
function rowId<Change>(row: SettingRow<Change>): string {
|
|
476
|
+
return row.kind === "scope" ? "scope" : row.id;
|
|
477
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token-estimate calibration. Extensions that size Review Evidence by Pi's chars/4 estimate
|
|
3
|
+
* undercount real tokens (about 1.3× on Claude Haiku and 1.7–1.9× on Claude Opus for JSON-heavy
|
|
4
|
+
* evidence). A model's factor starts conservative and follows the tokens its provider reports
|
|
5
|
+
* against the estimate, in coarse steps with hysteresis so a stable factor keeps the evidence
|
|
6
|
+
* window, and with it the provider's prompt cache, stable from review to review.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const minimumFactor = 1;
|
|
10
|
+
const maximumFactor = 3;
|
|
11
|
+
/** Factors move in steps of this size. */
|
|
12
|
+
const factorStep = 0.25;
|
|
13
|
+
/** A lower measured ratio replaces the factor only when it falls this far below it. */
|
|
14
|
+
const shrinkMargin = 0.35;
|
|
15
|
+
/** A higher measured ratio replaces the factor only when it exceeds it by more than this. */
|
|
16
|
+
const growMargin = 0.05;
|
|
17
|
+
/** Requests smaller than this are dominated by fixed provider overhead and calibrate nothing. */
|
|
18
|
+
const minimumCalibrationTokens = 2_000;
|
|
19
|
+
|
|
20
|
+
/** The next factor after a review whose request Pi estimated at `estimated` tokens (chars/4). */
|
|
21
|
+
export function calibratedFactor(current: number, estimated: number, reported: number): number {
|
|
22
|
+
if (estimated < minimumCalibrationTokens || !(reported > 0)) return current;
|
|
23
|
+
const ratio = reported / estimated;
|
|
24
|
+
const stepped = Math.min(
|
|
25
|
+
maximumFactor,
|
|
26
|
+
Math.max(minimumFactor, Math.ceil(ratio / factorStep) * factorStep),
|
|
27
|
+
);
|
|
28
|
+
if (ratio > current + growMargin || ratio < current - shrinkMargin) return stepped;
|
|
29
|
+
return current;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* One measurement: Pi's chars/4 estimate of a request or its added evidence, and the real tokens
|
|
34
|
+
* reported for it.
|
|
35
|
+
*/
|
|
36
|
+
export interface TokenSample {
|
|
37
|
+
estimated: number;
|
|
38
|
+
reported: number;
|
|
39
|
+
}
|