proper-base 0.6.0 → 0.8.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.
- package/README.md +270 -46
- package/UPDATES.md +129 -0
- package/index.ts +265 -136
- package/package.json +4 -3
- package/src/auto-update/availability.ts +132 -0
- package/src/auto-update/index.ts +403 -0
- package/src/auto-update/inventory.mjs +96 -0
- package/src/auto-update/readiness.ts +94 -0
- package/src/auto-update/runtime.ts +268 -0
- package/src/auto-update/settings.ts +146 -0
- package/src/autocomplete-details.ts +1 -1
- package/src/base-keybindings.ts +114 -0
- package/src/clipboard-selection.ts +310 -0
- package/src/context-tokens.ts +340 -0
- package/src/fast-mode.ts +74 -13
- package/src/footer-colors.ts +56 -3
- package/src/host-interop.ts +49 -0
- package/src/image-preview.ts +75 -66
- package/src/proactive-delegation.ts +47 -17
- package/src/prompt-display-host.ts +166 -0
- package/src/prompt-display.ts +44 -82
- package/src/prompt-sections.ts +16 -0
- package/src/session-list.ts +132 -21
- package/src/settings.ts +94 -30
- package/src/skill-context.ts +238 -159
- package/src/skills.ts +587 -0
- package/src/startup-defaults.ts +4 -122
- package/src/sticky-defaults.ts +199 -0
- package/src/transcript-cleanup.ts +8 -2
- package/src/wheel-scroll.ts +24 -27
package/src/fast-mode.ts
CHANGED
|
@@ -61,6 +61,11 @@ export function isFastToggle(text: string): boolean {
|
|
|
61
61
|
export class FastOverlay {
|
|
62
62
|
private sessionEnabled = false;
|
|
63
63
|
private modelsCache: { mtimeMs: number; ids: Set<string> } | undefined;
|
|
64
|
+
private display:
|
|
65
|
+
| { provider: string; global: boolean; ids: Set<string> }
|
|
66
|
+
| undefined;
|
|
67
|
+
private displayTimer: ReturnType<typeof setInterval> | undefined;
|
|
68
|
+
private displayChanged: (() => void) | undefined;
|
|
64
69
|
private readonly agentDir: string;
|
|
65
70
|
private readonly env: Record<string, string | undefined>;
|
|
66
71
|
|
|
@@ -76,7 +81,7 @@ export class FastOverlay {
|
|
|
76
81
|
return join(this.agentDir, CONFIG_FILE_NAME);
|
|
77
82
|
}
|
|
78
83
|
|
|
79
|
-
/** Session Fast
|
|
84
|
+
/** Session Fast starts off; /clear restores its captured flag after startup. */
|
|
80
85
|
resetSession(): void {
|
|
81
86
|
this.sessionEnabled = false;
|
|
82
87
|
}
|
|
@@ -90,17 +95,18 @@ export class FastOverlay {
|
|
|
90
95
|
return this.sessionEnabled;
|
|
91
96
|
}
|
|
92
97
|
|
|
93
|
-
/**
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
98
|
+
/** A valid environment override, which makes the JSON flag read-only. */
|
|
99
|
+
globalEnvironmentOverride(): boolean | undefined {
|
|
100
|
+
const value = this.env.CLIPROXYAPI_FAST;
|
|
101
|
+
return value === undefined ? undefined : parseBooleanSetting(value);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The effective provider default, re-read for every request. */
|
|
97
105
|
isGlobalEnabled(): boolean {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
return readJsonObject(this.configPath())?.fast === true;
|
|
106
|
+
return (
|
|
107
|
+
this.globalEnvironmentOverride() ??
|
|
108
|
+
readJsonObject(this.configPath())?.fast === true
|
|
109
|
+
);
|
|
104
110
|
}
|
|
105
111
|
|
|
106
112
|
/**
|
|
@@ -108,6 +114,9 @@ export class FastOverlay {
|
|
|
108
114
|
* the provider's own file format. Write failures propagate to the caller.
|
|
109
115
|
*/
|
|
110
116
|
toggleGlobal(): boolean {
|
|
117
|
+
if (this.globalEnvironmentOverride() !== undefined) {
|
|
118
|
+
throw new Error("CLIPROXYAPI_FAST is set in the environment");
|
|
119
|
+
}
|
|
111
120
|
const existing = readJsonObject(this.configPath()) ?? {};
|
|
112
121
|
const next = !this.isGlobalEnabled();
|
|
113
122
|
writeFileSync(
|
|
@@ -115,6 +124,7 @@ export class FastOverlay {
|
|
|
115
124
|
`${JSON.stringify({ ...existing, fast: next }, null, 2)}\n`,
|
|
116
125
|
"utf8",
|
|
117
126
|
);
|
|
127
|
+
this.refreshDisplay();
|
|
118
128
|
return next;
|
|
119
129
|
}
|
|
120
130
|
|
|
@@ -131,13 +141,17 @@ export class FastOverlay {
|
|
|
131
141
|
/** Whether the provider's cached catalog marks this model Fast-capable. */
|
|
132
142
|
supportsModel(model: FastModel | undefined): boolean {
|
|
133
143
|
if (!model || model.provider !== this.providerId()) return false;
|
|
144
|
+
return this.fastModelIds().has(model.id);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
private fastModelIds(): Set<string> {
|
|
134
148
|
const path = join(this.agentDir, MODELS_CACHE_FILE_NAME);
|
|
135
149
|
let mtimeMs: number;
|
|
136
150
|
try {
|
|
137
151
|
mtimeMs = statSync(path).mtimeMs;
|
|
138
152
|
} catch {
|
|
139
153
|
this.modelsCache = undefined;
|
|
140
|
-
return
|
|
154
|
+
return new Set();
|
|
141
155
|
}
|
|
142
156
|
if (this.modelsCache?.mtimeMs !== mtimeMs) {
|
|
143
157
|
const ids = readJsonObject(path)?.fastModelIds;
|
|
@@ -150,7 +164,54 @@ export class FastOverlay {
|
|
|
150
164
|
),
|
|
151
165
|
};
|
|
152
166
|
}
|
|
153
|
-
return this.modelsCache.ids
|
|
167
|
+
return this.modelsCache.ids;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Refresh outside rendering; request-time reads remain uncached. */
|
|
171
|
+
startDisplayRefresh(onChange: () => void): void {
|
|
172
|
+
this.stopDisplayRefresh();
|
|
173
|
+
this.displayChanged = onChange;
|
|
174
|
+
this.refreshDisplay();
|
|
175
|
+
this.displayTimer = setInterval(() => this.refreshDisplay(), 1000);
|
|
176
|
+
this.displayTimer.unref?.();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
stopDisplayRefresh(): void {
|
|
180
|
+
clearInterval(this.displayTimer);
|
|
181
|
+
this.displayTimer = undefined;
|
|
182
|
+
this.displayChanged = undefined;
|
|
183
|
+
this.display = undefined;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private refreshDisplay(): void {
|
|
187
|
+
if (!this.displayChanged) return;
|
|
188
|
+
const next = {
|
|
189
|
+
provider: this.providerId(),
|
|
190
|
+
global: this.isGlobalEnabled(),
|
|
191
|
+
ids: this.fastModelIds(),
|
|
192
|
+
};
|
|
193
|
+
const previous = this.display;
|
|
194
|
+
this.display = next;
|
|
195
|
+
if (
|
|
196
|
+
!previous ||
|
|
197
|
+
previous.provider !== next.provider ||
|
|
198
|
+
previous.global !== next.global ||
|
|
199
|
+
previous.ids.size !== next.ids.size ||
|
|
200
|
+
[...next.ids].some((id) => !previous.ids.has(id))
|
|
201
|
+
) {
|
|
202
|
+
this.displayChanged();
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** In-memory only: safe on every animation frame and keypress. */
|
|
207
|
+
isEffectiveForDisplay(model: FastModel | undefined): boolean {
|
|
208
|
+
return !!(
|
|
209
|
+
model &&
|
|
210
|
+
this.display &&
|
|
211
|
+
model.provider === this.display.provider &&
|
|
212
|
+
(this.sessionEnabled || this.display.global) &&
|
|
213
|
+
this.display.ids.has(model.id)
|
|
214
|
+
);
|
|
154
215
|
}
|
|
155
216
|
|
|
156
217
|
/** Whether requests for this model currently carry the priority tier. */
|
package/src/footer-colors.ts
CHANGED
|
@@ -32,6 +32,9 @@ const RAINBOW = [
|
|
|
32
32
|
] as const;
|
|
33
33
|
const ANIMATION_INTERVAL_MS = 120;
|
|
34
34
|
const HIGHLIGHT_CYCLE_MS = 4000;
|
|
35
|
+
// Upper bound on how long cached footer lines may survive an input we cannot
|
|
36
|
+
// observe (git branch switches, ctx.ui.setStatus from another extension).
|
|
37
|
+
const BASE_CACHE_TTL_MS = 1000;
|
|
35
38
|
const USAGE_THROUGH_COST =
|
|
36
39
|
/^((?:(?:↑|↓|R|W|CH)\S+\s+)*\$\S+(?:\s+\(sub\))?)(?:\s+|$)/;
|
|
37
40
|
const USAGE_FIELD =
|
|
@@ -104,6 +107,9 @@ export function installFooterColors(
|
|
|
104
107
|
uninstall() {
|
|
105
108
|
if (footer[INSTALLED] !== controller) return;
|
|
106
109
|
stopAnimation();
|
|
110
|
+
baseCache.clear();
|
|
111
|
+
baseKey = undefined;
|
|
112
|
+
baseTheme = undefined;
|
|
107
113
|
if (ownsRender) footer.render = render;
|
|
108
114
|
else Reflect.deleteProperty(footer, "render");
|
|
109
115
|
if (dispose) footer.dispose = dispose;
|
|
@@ -111,20 +117,67 @@ export function installFooterColors(
|
|
|
111
117
|
delete footer[INSTALLED];
|
|
112
118
|
},
|
|
113
119
|
};
|
|
120
|
+
// Pi's FooterComponent.render() sums usage over every session entry, and
|
|
121
|
+
// SessionManager.getEntries() allocates a fresh array per call, so a single
|
|
122
|
+
// frame costs O(session entries). layoutFooter needs a second render at a
|
|
123
|
+
// wider width, doubling that. Pi's footer output has no time-dependent part,
|
|
124
|
+
// so cache the raw lines per width and drop them whenever a visible input
|
|
125
|
+
// changes. getLeafId() is O(1) and moves on every appended entry, which is
|
|
126
|
+
// what the usage totals are derived from.
|
|
127
|
+
const baseCache = new Map<number, string[]>();
|
|
128
|
+
let baseKey: string | undefined;
|
|
129
|
+
let baseTheme: FooterTheme | undefined;
|
|
130
|
+
let baseAt = 0;
|
|
131
|
+
const baseLines = (
|
|
132
|
+
at: number,
|
|
133
|
+
key: string,
|
|
134
|
+
theme: FooterTheme,
|
|
135
|
+
now: number,
|
|
136
|
+
): string[] => {
|
|
137
|
+
if (
|
|
138
|
+
key !== baseKey ||
|
|
139
|
+
theme !== baseTheme ||
|
|
140
|
+
now - baseAt > BASE_CACHE_TTL_MS
|
|
141
|
+
) {
|
|
142
|
+
baseCache.clear();
|
|
143
|
+
baseKey = key;
|
|
144
|
+
baseTheme = theme;
|
|
145
|
+
baseAt = now;
|
|
146
|
+
}
|
|
147
|
+
let lines = baseCache.get(at);
|
|
148
|
+
if (!lines) {
|
|
149
|
+
lines = render.call(footer, at);
|
|
150
|
+
baseCache.set(at, lines);
|
|
151
|
+
}
|
|
152
|
+
return lines;
|
|
153
|
+
};
|
|
154
|
+
|
|
114
155
|
footer.render = (width: number) => {
|
|
115
156
|
const level = state.ctx.thinkingLevel as FooterThinkingLevel;
|
|
116
157
|
if (level === "max" || level === "ultra") startAnimation();
|
|
117
158
|
else stopAnimation();
|
|
118
159
|
const theme = state.ctx.ui.theme;
|
|
119
160
|
const model = state.ctx.model?.id;
|
|
161
|
+
const isFast = model ? state.fast?.() === true : false;
|
|
162
|
+
const now = Date.now();
|
|
163
|
+
// A host without getLeafId() only loses entry-driven invalidation; the
|
|
164
|
+
// TTL still bounds staleness. Never let a footer cache key crash render.
|
|
165
|
+
const leafId =
|
|
166
|
+
typeof state.ctx.sessionManager?.getLeafId === "function"
|
|
167
|
+
? (state.ctx.sessionManager.getLeafId() ?? "")
|
|
168
|
+
: "";
|
|
169
|
+
// /tokens swaps the window without a new entry or model id.
|
|
170
|
+
const key = `${leafId}\u0000${model ?? ""}\u0000${
|
|
171
|
+
state.ctx.model?.contextWindow ?? ""
|
|
172
|
+
}\u0000${level}\u0000${isFast ? "1" : "0"}`;
|
|
120
173
|
const base = (at: number) => {
|
|
121
|
-
const lines =
|
|
122
|
-
return model &&
|
|
174
|
+
const lines = baseLines(at, key, theme, now);
|
|
175
|
+
return model && isFast ? tagFast(lines, at, model, theme) : lines;
|
|
123
176
|
};
|
|
124
177
|
return colorFooter(layoutFooter(base(width), width, theme, base), {
|
|
125
178
|
level,
|
|
126
179
|
model,
|
|
127
|
-
now
|
|
180
|
+
now,
|
|
128
181
|
theme,
|
|
129
182
|
});
|
|
130
183
|
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// This small protocol ships in independent packages. The root integration
|
|
2
|
+
// test requires identical copies; packages do not depend on one another.
|
|
3
|
+
export const ORIGINAL_INPUT = Symbol.for("proper-pi.original-input.v1");
|
|
4
|
+
const WRAPPER = Symbol.for("proper-pi.method-wrapper.v1");
|
|
5
|
+
type WrapperState = {
|
|
6
|
+
original: unknown;
|
|
7
|
+
descriptor: PropertyDescriptor | undefined;
|
|
8
|
+
active: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function originalInput(
|
|
12
|
+
text: string,
|
|
13
|
+
options: object | undefined,
|
|
14
|
+
): string {
|
|
15
|
+
const value = options && Reflect.get(options, ORIGINAL_INPUT);
|
|
16
|
+
return typeof value === "string" ? value : text;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Owners deactivate their callbacks before disposal. Unlink inactive peers
|
|
20
|
+
* even when shutdown runs in installation order rather than stack order.
|
|
21
|
+
*/
|
|
22
|
+
export function installWrapper(
|
|
23
|
+
target: object,
|
|
24
|
+
key: PropertyKey,
|
|
25
|
+
wrapped: object,
|
|
26
|
+
): () => void {
|
|
27
|
+
const state: WrapperState = {
|
|
28
|
+
original: Reflect.get(target, key),
|
|
29
|
+
descriptor: Object.getOwnPropertyDescriptor(target, key),
|
|
30
|
+
active: true,
|
|
31
|
+
};
|
|
32
|
+
Reflect.set(wrapped, WRAPPER, state);
|
|
33
|
+
Reflect.set(target, key, wrapped);
|
|
34
|
+
return () => {
|
|
35
|
+
state.active = false;
|
|
36
|
+
if (Reflect.get(target, key) !== wrapped) return;
|
|
37
|
+
let previous = state;
|
|
38
|
+
while (typeof previous.original === "function") {
|
|
39
|
+
const prior = Reflect.get(previous.original, WRAPPER) as
|
|
40
|
+
| WrapperState
|
|
41
|
+
| undefined;
|
|
42
|
+
if (!prior || prior.active) break;
|
|
43
|
+
previous = prior;
|
|
44
|
+
}
|
|
45
|
+
if (previous.descriptor)
|
|
46
|
+
Object.defineProperty(target, key, previous.descriptor);
|
|
47
|
+
else Reflect.deleteProperty(target, key);
|
|
48
|
+
};
|
|
49
|
+
}
|
package/src/image-preview.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
1
|
import { basename, extname, isAbsolute } from "node:path";
|
|
3
2
|
import {
|
|
4
3
|
type Component,
|
|
5
4
|
getCapabilities,
|
|
6
5
|
getCellDimensions,
|
|
7
|
-
getImageDimensions,
|
|
8
6
|
Image,
|
|
9
7
|
type ImageTheme,
|
|
10
8
|
Loader,
|
|
@@ -75,11 +73,6 @@ type Preview = {
|
|
|
75
73
|
cancelThumbnail: (() => void) | undefined;
|
|
76
74
|
};
|
|
77
75
|
|
|
78
|
-
type PreviewImagePlan = {
|
|
79
|
-
image: PreviewImage | undefined;
|
|
80
|
-
thumbnail: { widthPx: number; heightPx: number } | undefined;
|
|
81
|
-
};
|
|
82
|
-
|
|
83
76
|
export type ImagePreviewController = {
|
|
84
77
|
prepare(text: string): string;
|
|
85
78
|
clear(): void;
|
|
@@ -142,49 +135,63 @@ function sharpThumbnail(
|
|
|
142
135
|
done: (image: PreviewImage | undefined) => void,
|
|
143
136
|
): () => void {
|
|
144
137
|
let cancelled = false;
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
138
|
+
const metadataPipeline = sharp(path, { pages: 1 }).timeout({
|
|
139
|
+
seconds: THUMBNAIL_TIMEOUT_SECONDS,
|
|
140
|
+
});
|
|
141
|
+
let outputPipeline: ReturnType<typeof sharp> | undefined;
|
|
142
|
+
void metadataPipeline
|
|
143
|
+
.metadata()
|
|
144
|
+
.then((metadata) => {
|
|
145
|
+
if (cancelled) return undefined;
|
|
146
|
+
const orientation = metadata.orientation ?? 1;
|
|
147
|
+
const width = orientation >= 5 ? metadata.height : metadata.width;
|
|
148
|
+
const height = orientation >= 5 ? metadata.width : metadata.height;
|
|
149
|
+
outputPipeline = sharp(path, { pages: 1 }).timeout({
|
|
150
|
+
seconds: THUMBNAIL_TIMEOUT_SECONDS,
|
|
151
|
+
});
|
|
152
|
+
if (width && height && width <= widthPx && height <= heightPx) {
|
|
153
|
+
return outputPipeline
|
|
154
|
+
.rotate()
|
|
155
|
+
.toBuffer()
|
|
156
|
+
.then((output) => ({
|
|
157
|
+
base64: output.toString("base64"),
|
|
158
|
+
mimeType: MIME_TYPES[extname(path).toLowerCase()] ?? "image/png",
|
|
159
|
+
}));
|
|
160
160
|
}
|
|
161
|
+
return outputPipeline
|
|
162
|
+
.rotate()
|
|
163
|
+
.resize({
|
|
164
|
+
width: widthPx,
|
|
165
|
+
height: heightPx,
|
|
166
|
+
fit: "inside",
|
|
167
|
+
withoutEnlargement: true,
|
|
168
|
+
})
|
|
169
|
+
.png()
|
|
170
|
+
.toBuffer()
|
|
171
|
+
.then((output) => ({
|
|
172
|
+
base64: output.toString("base64"),
|
|
173
|
+
mimeType: "image/png",
|
|
174
|
+
}));
|
|
175
|
+
})
|
|
176
|
+
.then((image) => {
|
|
177
|
+
if (!cancelled && image) done(image);
|
|
161
178
|
})
|
|
162
179
|
.catch(() => {
|
|
163
180
|
if (!cancelled) done(undefined);
|
|
164
181
|
});
|
|
165
182
|
return () => {
|
|
166
183
|
cancelled = true;
|
|
167
|
-
|
|
184
|
+
metadataPipeline.destroy();
|
|
185
|
+
outputPipeline?.destroy();
|
|
168
186
|
};
|
|
169
187
|
}
|
|
170
188
|
|
|
171
|
-
export function
|
|
172
|
-
mimeType: string,
|
|
173
|
-
source: Buffer,
|
|
174
|
-
): PreviewImagePlan {
|
|
175
|
-
const base64 = source.toString("base64");
|
|
176
|
-
const dimensions = getImageDimensions(base64, mimeType);
|
|
189
|
+
export function previewPixelBounds(): { widthPx: number; heightPx: number } {
|
|
177
190
|
const cells = getCellDimensions();
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
dimensions.widthPx <= widthPx &&
|
|
183
|
-
dimensions.heightPx <= heightPx
|
|
184
|
-
) {
|
|
185
|
-
return { image: { base64, mimeType }, thumbnail: undefined };
|
|
186
|
-
}
|
|
187
|
-
return { image: undefined, thumbnail: { widthPx, heightPx } };
|
|
191
|
+
return {
|
|
192
|
+
widthPx: Math.max(1, PREVIEW_WIDTH * cells.widthPx),
|
|
193
|
+
heightPx: Math.max(1, PREVIEW_HEIGHT * cells.heightPx),
|
|
194
|
+
};
|
|
188
195
|
}
|
|
189
196
|
|
|
190
197
|
function singleDeletionIndex(
|
|
@@ -360,38 +367,40 @@ export function installImagePreview(
|
|
|
360
367
|
const known = markersByPath.get(path);
|
|
361
368
|
if (known) return known;
|
|
362
369
|
if (!isAbsolute(path) || !CLIPBOARD_IMAGE.test(basename(path))) return;
|
|
363
|
-
|
|
364
|
-
|
|
370
|
+
if (!MIME_TYPES[extname(path).toLowerCase()]) return;
|
|
371
|
+
const marker = `[image ${++counter}]`;
|
|
372
|
+
const preview: Preview = {
|
|
373
|
+
marker,
|
|
374
|
+
path,
|
|
375
|
+
image: undefined,
|
|
376
|
+
cancelThumbnail: undefined,
|
|
377
|
+
};
|
|
378
|
+
previews.set(marker, preview);
|
|
379
|
+
markersByPath.set(path, marker);
|
|
380
|
+
const bounds = previewPixelBounds();
|
|
365
381
|
try {
|
|
366
|
-
|
|
367
|
-
const
|
|
368
|
-
const preview: Preview = {
|
|
369
|
-
marker,
|
|
382
|
+
let completed = false;
|
|
383
|
+
const cancel = thumbnailer(
|
|
370
384
|
path,
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
preview.image = image;
|
|
385
|
-
visibleMarkers = "";
|
|
386
|
-
sync(target.getText?.() ?? "");
|
|
387
|
-
tui.requestRender();
|
|
388
|
-
},
|
|
389
|
-
);
|
|
390
|
-
}
|
|
391
|
-
return marker;
|
|
385
|
+
bounds.widthPx,
|
|
386
|
+
bounds.heightPx,
|
|
387
|
+
(image) => {
|
|
388
|
+
completed = true;
|
|
389
|
+
preview.cancelThumbnail = undefined;
|
|
390
|
+
if (!image || previews.get(marker) !== preview) return;
|
|
391
|
+
preview.image = image;
|
|
392
|
+
visibleMarkers = "";
|
|
393
|
+
sync(target.getText?.() ?? "");
|
|
394
|
+
tui.requestRender();
|
|
395
|
+
},
|
|
396
|
+
);
|
|
397
|
+
if (!completed) preview.cancelThumbnail = cancel;
|
|
392
398
|
} catch {
|
|
399
|
+
previews.delete(marker);
|
|
400
|
+
markersByPath.delete(path);
|
|
393
401
|
return;
|
|
394
402
|
}
|
|
403
|
+
return marker;
|
|
395
404
|
};
|
|
396
405
|
const ingest = (text: string, cursor: number | undefined) => {
|
|
397
406
|
let mappedCursor = cursor;
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import type { NormalizedBuildSystemPromptOptions } from "@earendil-works/pi-coding-agent";
|
|
4
|
+
import { appendPromptSection } from "./prompt-sections.ts";
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* Codex switches GPT-6 Astra into `MultiAgentMode::Proactive` at the Ultra
|
|
6
8
|
* effort: a developer message telling the model to delegate whenever work
|
|
7
9
|
* parallelizes. Pi's subagent policy is the opposite, set by two sentences
|
|
8
10
|
* pi-subagents puts in the system prompt: a tool guideline and a line in
|
|
9
|
-
* the `<advertised_subagents>` catalog.
|
|
10
|
-
*
|
|
11
|
-
*
|
|
11
|
+
* the `<advertised_subagents>` catalog. Since 0.71 a fresh session starts
|
|
12
|
+
* with only the `subagents_enable` loader active, whose tool-list line
|
|
13
|
+
* repeats the ask-first rule. This rewrites exactly those sentences and
|
|
14
|
+
* appends the Codex paragraph; the preflight instructions around them and
|
|
15
|
+
* the tool descriptions' safety guidance stay as they are.
|
|
12
16
|
*
|
|
13
17
|
* Codex's Ultra also picks each subagent's model. pi-subagents' `models`
|
|
14
18
|
* action lists the whole registry and the tool description says to copy
|
|
@@ -20,10 +24,15 @@ const GUIDELINE = "Use subagent only when delegation is needed.";
|
|
|
20
24
|
const CATALOG_SENTENCE =
|
|
21
25
|
"Their descriptions indicate available specializations, not instructions to delegate. Use subagent only when delegation is needed.";
|
|
22
26
|
|
|
27
|
+
const LOADER_SENTENCES =
|
|
28
|
+
"For authorized specialist, independent-review, or parallel work, call subagents_enable, then subagent. Authorization must come from the current request or applicable instructions; complexity alone is not authorization.";
|
|
29
|
+
|
|
23
30
|
const PROACTIVE_GUIDELINE =
|
|
24
31
|
"Delegate to subagents proactively whenever work parallelizes; see Multi-agent mode below.";
|
|
25
32
|
const PROACTIVE_CATALOG_SENTENCE =
|
|
26
33
|
"Their descriptions indicate available specializations; delegate to them proactively whenever work parallelizes.";
|
|
34
|
+
const PROACTIVE_LOADER_SENTENCES =
|
|
35
|
+
"To delegate, call subagents_enable, then subagent; see Multi-agent mode below.";
|
|
27
36
|
|
|
28
37
|
/**
|
|
29
38
|
* Codex's `PROACTIVE_MULTI_AGENT_MODE_TEXT` retargeted at pi's tool, plus
|
|
@@ -40,12 +49,12 @@ If at any point you can parallelize work by delegating tasks to another agent (n
|
|
|
40
49
|
Delegate work that is independent and large enough to justify a fresh context; keep tightly sequential steps, small tasks where coordination costs more than it saves, and edits to the same area in this session. You own synthesis and the final answer: read every result and decide yourself, never hand a child "based on the findings, do X". Messages you send to other agents may be read by a human, so ensure they are legible and always put proper spaces between words and numbers.`;
|
|
41
50
|
|
|
42
51
|
const MODEL_CHOICE_HEADER =
|
|
43
|
-
"For every delegation choose the model best suited to that task's difficulty and cost, passing it as the subagent tool's exact provider/id model argument. Bounded, well-specified subtasks suit cheaper or faster models; reserve the strongest models for decomposition, review, and hard reasoning. Only these models are enabled in this session; a model the subagent models action lists that is missing here must not be used:";
|
|
52
|
+
"For every delegation choose the model best suited to that task's difficulty and cost, passing it as the subagent tool's exact provider/id model argument. If an active routing extension advertises a task-text override, include that override as well; model= alone does not pin a routed child. Bounded, well-specified subtasks suit cheaper or faster models; reserve the strongest models for decomposition, review, and hard reasoning. Only these models are enabled in this session; a model the subagent models action lists that is missing here must not be used:";
|
|
44
53
|
|
|
45
54
|
/** A scoped model as the delegation text names it. */
|
|
46
55
|
export type ScopedModelRef = { provider: string; id: string };
|
|
47
56
|
|
|
48
|
-
/**
|
|
57
|
+
/** The placeholder is a routing entry point, not a concrete model choice. */
|
|
49
58
|
const PLACEHOLDER_PROVIDER = "llm-router";
|
|
50
59
|
|
|
51
60
|
function modelChoiceText(models: readonly ScopedModelRef[]): string {
|
|
@@ -72,20 +81,41 @@ export function readProactiveDelegationEnabled(agentDir: string): boolean {
|
|
|
72
81
|
}
|
|
73
82
|
}
|
|
74
83
|
|
|
75
|
-
/**
|
|
76
|
-
* The rewritten system prompt, or `undefined` when nothing changes: the
|
|
77
|
-
* subagent tool is absent, the mode is off, or the paragraph is already in.
|
|
78
|
-
*/
|
|
84
|
+
/** Change policy in its structured fields, preserving later contributions. */
|
|
79
85
|
export function applyProactiveDelegation(
|
|
80
|
-
|
|
86
|
+
options: NormalizedBuildSystemPromptOptions,
|
|
81
87
|
hasSubagentTool: boolean,
|
|
82
88
|
scopedModels: readonly ScopedModelRef[] = [],
|
|
83
|
-
):
|
|
84
|
-
if (
|
|
85
|
-
|
|
89
|
+
): void {
|
|
90
|
+
if (
|
|
91
|
+
!hasSubagentTool ||
|
|
92
|
+
options.sections.proper_base_delegation ||
|
|
93
|
+
options.forceSystemPrompt?.includes(PROACTIVE_DELEGATION_TEXT)
|
|
94
|
+
)
|
|
95
|
+
return;
|
|
96
|
+
const rewrite = (text: string) =>
|
|
97
|
+
text
|
|
98
|
+
.replace(CATALOG_SENTENCE, PROACTIVE_CATALOG_SENTENCE)
|
|
99
|
+
.replace(GUIDELINE, PROACTIVE_GUIDELINE)
|
|
100
|
+
.replace(LOADER_SENTENCES, PROACTIVE_LOADER_SENTENCES);
|
|
101
|
+
options.promptGuidelines = options.promptGuidelines.map(rewrite);
|
|
102
|
+
for (const [name, text] of Object.entries(options.toolSnippets)) {
|
|
103
|
+
options.toolSnippets[name] = rewrite(text);
|
|
104
|
+
}
|
|
105
|
+
for (const [name, rules] of Object.entries(options.toolGuidelines)) {
|
|
106
|
+
options.toolGuidelines[name] = rules.map(rewrite);
|
|
107
|
+
}
|
|
108
|
+
for (const [name, text] of Object.entries(options.sections)) {
|
|
109
|
+
options.sections[name] = rewrite(text);
|
|
86
110
|
}
|
|
87
|
-
|
|
88
|
-
.
|
|
89
|
-
|
|
90
|
-
|
|
111
|
+
if (options.customPrompt !== undefined)
|
|
112
|
+
options.customPrompt = rewrite(options.customPrompt);
|
|
113
|
+
options.appendSystemPrompt = rewrite(options.appendSystemPrompt);
|
|
114
|
+
if (options.forceSystemPrompt !== undefined)
|
|
115
|
+
options.forceSystemPrompt = rewrite(options.forceSystemPrompt);
|
|
116
|
+
appendPromptSection(
|
|
117
|
+
options,
|
|
118
|
+
"proper_base_delegation",
|
|
119
|
+
`${PROACTIVE_DELEGATION_TEXT}${modelChoiceText(scopedModels)}`,
|
|
120
|
+
);
|
|
91
121
|
}
|