proper-base 0.6.0 → 0.7.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 +36 -13
- package/UPDATES.md +129 -0
- package/index.ts +76 -111
- 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/base-keybindings.ts +114 -0
- package/src/clipboard-selection.ts +310 -0
- package/src/fast-mode.ts +73 -12
- package/src/footer-colors.ts +55 -3
- package/src/host-interop.ts +49 -0
- package/src/image-preview.ts +75 -66
- package/src/proactive-delegation.ts +33 -14
- package/src/prompt-display-host.ts +136 -0
- package/src/prompt-display.ts +44 -82
- package/src/prompt-sections.ts +16 -0
- package/src/session-list.ts +132 -21
- package/src/skill-context.ts +52 -14
- package/src/startup-defaults.ts +4 -122
- package/src/sticky-defaults.ts +196 -0
- package/src/wheel-scroll.ts +24 -27
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,5 +1,7 @@
|
|
|
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
|
|
@@ -40,12 +42,12 @@ If at any point you can parallelize work by delegating tasks to another agent (n
|
|
|
40
42
|
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
43
|
|
|
42
44
|
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:";
|
|
45
|
+
"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
46
|
|
|
45
47
|
/** A scoped model as the delegation text names it. */
|
|
46
48
|
export type ScopedModelRef = { provider: string; id: string };
|
|
47
49
|
|
|
48
|
-
/**
|
|
50
|
+
/** The placeholder is a routing entry point, not a concrete model choice. */
|
|
49
51
|
const PLACEHOLDER_PROVIDER = "llm-router";
|
|
50
52
|
|
|
51
53
|
function modelChoiceText(models: readonly ScopedModelRef[]): string {
|
|
@@ -72,20 +74,37 @@ export function readProactiveDelegationEnabled(agentDir: string): boolean {
|
|
|
72
74
|
}
|
|
73
75
|
}
|
|
74
76
|
|
|
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
|
-
*/
|
|
77
|
+
/** Change policy in its structured fields, preserving later contributions. */
|
|
79
78
|
export function applyProactiveDelegation(
|
|
80
|
-
|
|
79
|
+
options: NormalizedBuildSystemPromptOptions,
|
|
81
80
|
hasSubagentTool: boolean,
|
|
82
81
|
scopedModels: readonly ScopedModelRef[] = [],
|
|
83
|
-
):
|
|
84
|
-
if (
|
|
85
|
-
|
|
82
|
+
): void {
|
|
83
|
+
if (
|
|
84
|
+
!hasSubagentTool ||
|
|
85
|
+
options.sections.proper_base_delegation ||
|
|
86
|
+
options.forceSystemPrompt?.includes(PROACTIVE_DELEGATION_TEXT)
|
|
87
|
+
)
|
|
88
|
+
return;
|
|
89
|
+
const rewrite = (text: string) =>
|
|
90
|
+
text
|
|
91
|
+
.replace(CATALOG_SENTENCE, PROACTIVE_CATALOG_SENTENCE)
|
|
92
|
+
.replace(GUIDELINE, PROACTIVE_GUIDELINE);
|
|
93
|
+
options.promptGuidelines = options.promptGuidelines.map(rewrite);
|
|
94
|
+
for (const [name, rules] of Object.entries(options.toolGuidelines)) {
|
|
95
|
+
options.toolGuidelines[name] = rules.map(rewrite);
|
|
96
|
+
}
|
|
97
|
+
for (const [name, text] of Object.entries(options.sections)) {
|
|
98
|
+
options.sections[name] = rewrite(text);
|
|
86
99
|
}
|
|
87
|
-
|
|
88
|
-
.
|
|
89
|
-
|
|
90
|
-
|
|
100
|
+
if (options.customPrompt !== undefined)
|
|
101
|
+
options.customPrompt = rewrite(options.customPrompt);
|
|
102
|
+
options.appendSystemPrompt = rewrite(options.appendSystemPrompt);
|
|
103
|
+
if (options.forceSystemPrompt !== undefined)
|
|
104
|
+
options.forceSystemPrompt = rewrite(options.forceSystemPrompt);
|
|
105
|
+
appendPromptSection(
|
|
106
|
+
options,
|
|
107
|
+
"proper_base_delegation",
|
|
108
|
+
`${PROACTIVE_DELEGATION_TEXT}${modelChoiceText(scopedModels)}`,
|
|
109
|
+
);
|
|
91
110
|
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import {
|
|
3
|
+
AgentSession,
|
|
4
|
+
InteractiveMode,
|
|
5
|
+
type MarkdownTransformer,
|
|
6
|
+
type SessionManager,
|
|
7
|
+
} from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { installWrapper, originalInput } from "./host-interop.ts";
|
|
9
|
+
import type { PromptDisplayController } from "./prompt-display.ts";
|
|
10
|
+
|
|
11
|
+
type Message = Parameters<SessionManager["appendMessage"]>[0];
|
|
12
|
+
type Manager = Pick<SessionManager, "getBranch">;
|
|
13
|
+
type Controller = { activate(manager: Manager): void; dispose(): void };
|
|
14
|
+
const INSTALLED = Symbol.for("pi-proper-base.prompt-display");
|
|
15
|
+
const host = globalThis as typeof globalThis & { [INSTALLED]?: Controller };
|
|
16
|
+
|
|
17
|
+
/** Pi 0.86 has neither input correlation nor message identity in Markdown hooks.
|
|
18
|
+
* Keep this adapter removable when those APIs exist. Never emulate expansion.
|
|
19
|
+
* @lat: [[lat.md/proper-base/lifecycle#Prompt history lifecycle#Prompt display]]
|
|
20
|
+
*/
|
|
21
|
+
export function installPromptDisplayHost(
|
|
22
|
+
display: PromptDisplayController,
|
|
23
|
+
commands: () => ReadonlyArray<{ name: string; source: string }>,
|
|
24
|
+
marker: MarkdownTransformer,
|
|
25
|
+
): Controller {
|
|
26
|
+
host[INSTALLED]?.dispose();
|
|
27
|
+
let active = true;
|
|
28
|
+
let manager: Manager | undefined;
|
|
29
|
+
const dispatch = new AsyncLocalStorage<{
|
|
30
|
+
raw: string | undefined;
|
|
31
|
+
bound: boolean;
|
|
32
|
+
}>();
|
|
33
|
+
const restorers: Array<() => void> = [];
|
|
34
|
+
const agents = new WeakSet<AgentSession["agent"]>();
|
|
35
|
+
const managers = new WeakSet<SessionManager>();
|
|
36
|
+
|
|
37
|
+
function bindSession(session: AgentSession): void {
|
|
38
|
+
const agent = session.agent;
|
|
39
|
+
if (!agents.has(agent)) {
|
|
40
|
+
agents.add(agent);
|
|
41
|
+
for (const name of ["prompt", "steer", "followUp"] as const) {
|
|
42
|
+
const original = agent[name];
|
|
43
|
+
const wrapped = function (this: typeof agent, ...args: unknown[]) {
|
|
44
|
+
const scope = dispatch.getStore();
|
|
45
|
+
if (active && scope?.raw !== undefined && !scope.bound) {
|
|
46
|
+
const values = (
|
|
47
|
+
Array.isArray(args[0]) ? args[0] : [args[0]]
|
|
48
|
+
) as Message[];
|
|
49
|
+
const message = values.find((value) => value?.role === "user");
|
|
50
|
+
if (message) {
|
|
51
|
+
display.bind(message, scope.raw);
|
|
52
|
+
scope.bound = true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return Reflect.apply(original, this, args);
|
|
56
|
+
};
|
|
57
|
+
restorers.push(installWrapper(agent, name, wrapped));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const store = session.sessionManager;
|
|
61
|
+
if (!managers.has(store)) {
|
|
62
|
+
managers.add(store);
|
|
63
|
+
const append = store.appendMessage;
|
|
64
|
+
const wrapped: typeof append = function (this: SessionManager, message) {
|
|
65
|
+
const id = append.call(this, message);
|
|
66
|
+
if (active) display.persist(message, id);
|
|
67
|
+
return id;
|
|
68
|
+
};
|
|
69
|
+
restorers.push(installWrapper(store, "appendMessage", wrapped));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
for (const name of ["prompt", "steer", "followUp"] as const) {
|
|
74
|
+
const original = AgentSession.prototype[name];
|
|
75
|
+
const wrapped = function (this: AgentSession, ...args: unknown[]) {
|
|
76
|
+
if (!active || this.sessionManager !== manager)
|
|
77
|
+
return Reflect.apply(original, this, args);
|
|
78
|
+
bindSession(this);
|
|
79
|
+
const options = args[name === "prompt" ? 1 : 2] as
|
|
80
|
+
| { source?: string; expandPromptTemplates?: boolean }
|
|
81
|
+
| undefined;
|
|
82
|
+
const text = originalInput(args[0] as string, options);
|
|
83
|
+
const command = /^\/([^\s]+)/.exec(text)?.[1];
|
|
84
|
+
const raw =
|
|
85
|
+
(options?.source ?? "interactive") === "interactive" &&
|
|
86
|
+
options?.expandPromptTemplates !== false &&
|
|
87
|
+
command &&
|
|
88
|
+
commands().some(
|
|
89
|
+
(item) => item.name === command && item.source === "prompt",
|
|
90
|
+
)
|
|
91
|
+
? text
|
|
92
|
+
: undefined;
|
|
93
|
+
return dispatch.run({ raw, bound: false }, () =>
|
|
94
|
+
Reflect.apply(original, this, args),
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
restorers.push(installWrapper(AgentSession.prototype, name, wrapped));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Swap only the text Pi gives its native user component, preserving images,
|
|
101
|
+
// layout and other transformers. Persisted and model-facing text is untouched.
|
|
102
|
+
type InteractiveHost = {
|
|
103
|
+
session: AgentSession;
|
|
104
|
+
getUserMessageText(message: Message): string;
|
|
105
|
+
getMarkdownTransformers(): MarkdownTransformer[];
|
|
106
|
+
};
|
|
107
|
+
const prototype = InteractiveMode.prototype as unknown as InteractiveHost;
|
|
108
|
+
const original = prototype.getUserMessageText;
|
|
109
|
+
const wrapped: typeof original = function (this: InteractiveHost, message) {
|
|
110
|
+
if (!active || !this.getMarkdownTransformers().includes(marker))
|
|
111
|
+
return original.call(this, message);
|
|
112
|
+
if (manager !== this.session.sessionManager) {
|
|
113
|
+
// Reload rebuilds chat before session_start. Resolve entry identities now.
|
|
114
|
+
manager = this.session.sessionManager;
|
|
115
|
+
display.restore(manager.getBranch());
|
|
116
|
+
}
|
|
117
|
+
return display.rawFor(message) ?? original.call(this, message);
|
|
118
|
+
};
|
|
119
|
+
restorers.push(installWrapper(prototype, "getUserMessageText", wrapped));
|
|
120
|
+
|
|
121
|
+
const controller: Controller = {
|
|
122
|
+
activate(next) {
|
|
123
|
+
manager = next;
|
|
124
|
+
display.restore(next.getBranch());
|
|
125
|
+
},
|
|
126
|
+
dispose() {
|
|
127
|
+
active = false;
|
|
128
|
+
for (const restore of restorers.reverse()) restore();
|
|
129
|
+
dispatch.disable();
|
|
130
|
+
display.clear();
|
|
131
|
+
if (host[INSTALLED] === controller) delete host[INSTALLED];
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
host[INSTALLED] = controller;
|
|
135
|
+
return controller;
|
|
136
|
+
}
|
package/src/prompt-display.ts
CHANGED
|
@@ -1,101 +1,63 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { SessionManager } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
|
|
3
3
|
export const PROMPT_DISPLAY_ENTRY = "proper-prompt-display";
|
|
4
|
-
|
|
5
|
-
type
|
|
6
|
-
type
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
export type PromptDisplayRecord = { hash: string; raw: string };
|
|
13
|
-
|
|
14
|
-
export type PromptDisplayController = {
|
|
15
|
-
captureInput(text: string, commands: Command[]): void;
|
|
16
|
-
captureUser(message: Message): void;
|
|
17
|
-
transform(markdown: string): string;
|
|
18
|
-
drain(): PromptDisplayRecord[];
|
|
19
|
-
restore(entries: Entry[]): void;
|
|
20
|
-
clear(): void;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export function createPromptDisplay(): PromptDisplayController {
|
|
24
|
-
const display = new Map<string, string>();
|
|
25
|
-
const pending = new Map<string, string>();
|
|
26
|
-
const inputs: Array<string | undefined> = [];
|
|
27
|
-
|
|
4
|
+
type Message = Parameters<SessionManager["appendMessage"]>[0];
|
|
5
|
+
type Entry = ReturnType<SessionManager["getBranch"]>[number];
|
|
6
|
+
export type PromptDisplayRecord = { messageEntryId: string; raw: string };
|
|
7
|
+
|
|
8
|
+
/** Display metadata belongs to a message, never to its text or submission order. */
|
|
9
|
+
export function createPromptDisplay() {
|
|
10
|
+
let display = new WeakMap<Message, string>();
|
|
11
|
+
const pending: PromptDisplayRecord[] = [];
|
|
28
12
|
return {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const prompt = name
|
|
32
|
-
? commands.some(
|
|
33
|
-
(command) => command.name === name && command.source === "prompt",
|
|
34
|
-
)
|
|
35
|
-
: false;
|
|
36
|
-
inputs.push(prompt ? text : undefined);
|
|
13
|
+
bind(message: Message, raw: string) {
|
|
14
|
+
display.set(message, raw);
|
|
37
15
|
},
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (!raw) return;
|
|
41
|
-
const expanded = messageText(message);
|
|
42
|
-
if (!expanded) return;
|
|
43
|
-
const key = hash(expanded);
|
|
44
|
-
display.set(key, raw);
|
|
45
|
-
pending.set(key, raw);
|
|
16
|
+
rawFor(message: Message): string | undefined {
|
|
17
|
+
return display.get(message);
|
|
46
18
|
},
|
|
47
|
-
|
|
48
|
-
|
|
19
|
+
persist(message: Message, messageEntryId: string) {
|
|
20
|
+
const raw = display.get(message);
|
|
21
|
+
if (raw !== undefined) pending.push({ messageEntryId, raw });
|
|
49
22
|
},
|
|
50
|
-
drain() {
|
|
51
|
-
|
|
52
|
-
pending.clear();
|
|
53
|
-
return records;
|
|
23
|
+
drain(): PromptDisplayRecord[] {
|
|
24
|
+
return pending.splice(0);
|
|
54
25
|
},
|
|
55
|
-
restore(entries) {
|
|
56
|
-
display
|
|
57
|
-
pending.
|
|
58
|
-
|
|
26
|
+
restore(entries: Entry[]) {
|
|
27
|
+
display = new WeakMap();
|
|
28
|
+
pending.length = 0;
|
|
29
|
+
const messages = new Map(
|
|
30
|
+
entries.flatMap((entry) =>
|
|
31
|
+
entry.type === "message" && entry.message.role === "user"
|
|
32
|
+
? [[entry.id, entry.message] as const]
|
|
33
|
+
: [],
|
|
34
|
+
),
|
|
35
|
+
);
|
|
59
36
|
for (const entry of entries) {
|
|
60
37
|
if (
|
|
61
38
|
entry.type !== "custom" ||
|
|
62
39
|
entry.customType !== PROMPT_DISPLAY_ENTRY
|
|
63
|
-
)
|
|
40
|
+
)
|
|
64
41
|
continue;
|
|
42
|
+
const data = entry.data as { prompts?: unknown } | undefined;
|
|
43
|
+
if (!data || !Array.isArray(data.prompts)) continue;
|
|
44
|
+
for (const record of data.prompts) {
|
|
45
|
+
if (
|
|
46
|
+
!record ||
|
|
47
|
+
typeof record !== "object" ||
|
|
48
|
+
typeof record.messageEntryId !== "string" ||
|
|
49
|
+
typeof record.raw !== "string"
|
|
50
|
+
)
|
|
51
|
+
continue;
|
|
52
|
+
const message = messages.get(record.messageEntryId);
|
|
53
|
+
if (message) display.set(message, record.raw);
|
|
65
54
|
}
|
|
66
|
-
const records = asRecords(entry.data);
|
|
67
|
-
for (const record of records) display.set(record.hash, record.raw);
|
|
68
55
|
}
|
|
69
56
|
},
|
|
70
57
|
clear() {
|
|
71
|
-
display
|
|
72
|
-
pending.
|
|
73
|
-
inputs.length = 0;
|
|
58
|
+
display = new WeakMap();
|
|
59
|
+
pending.length = 0;
|
|
74
60
|
},
|
|
75
61
|
};
|
|
76
62
|
}
|
|
77
|
-
|
|
78
|
-
function messageText(message: Message): string {
|
|
79
|
-
if (typeof message.content === "string") return message.content;
|
|
80
|
-
return message.content
|
|
81
|
-
.filter((part) => part.type === "text" && typeof part.text === "string")
|
|
82
|
-
.map((part) => part.text ?? "")
|
|
83
|
-
.join("");
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function hash(text: string): string {
|
|
87
|
-
return createHash("sha256").update(text.trim()).digest("hex");
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function asRecords(data: unknown): PromptDisplayRecord[] {
|
|
91
|
-
if (typeof data !== "object" || data === null) return [];
|
|
92
|
-
const prompts = (data as { prompts?: unknown }).prompts;
|
|
93
|
-
if (!Array.isArray(prompts)) return [];
|
|
94
|
-
return prompts.flatMap((record) => {
|
|
95
|
-
if (typeof record !== "object" || record === null) return [];
|
|
96
|
-
const { hash, raw } = record as { hash?: unknown; raw?: unknown };
|
|
97
|
-
return typeof hash === "string" && typeof raw === "string"
|
|
98
|
-
? [{ hash, raw }]
|
|
99
|
-
: [];
|
|
100
|
-
});
|
|
101
|
-
}
|
|
63
|
+
export type PromptDisplayController = ReturnType<typeof createPromptDisplay>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { NormalizedBuildSystemPromptOptions } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
/** Compose normally; never turn structured input into an opaque replacement. */
|
|
4
|
+
export function appendPromptSection(
|
|
5
|
+
options: NormalizedBuildSystemPromptOptions,
|
|
6
|
+
name: string,
|
|
7
|
+
text: string,
|
|
8
|
+
): void {
|
|
9
|
+
if (options.forceSystemPrompt !== undefined) {
|
|
10
|
+
// A prior extension explicitly replaced the prompt. Preserve that choice;
|
|
11
|
+
// Pi ignores structured sections in this mode, so append to its text.
|
|
12
|
+
options.forceSystemPrompt += `\n\n<${name}>\n${text}\n</${name}>`;
|
|
13
|
+
} else {
|
|
14
|
+
options.sections[name] = text;
|
|
15
|
+
}
|
|
16
|
+
}
|