@bismawy/pi-arnative 0.16.1 → 0.18.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 +25 -16
- package/extensions/index.ts +41 -0
- package/lib/active-clock.ts +91 -0
- package/lib/ansi.ts +40 -78
- package/lib/box.ts +49 -68
- package/lib/chatbox.ts +371 -0
- package/lib/color.ts +3 -31
- package/lib/compat.ts +0 -60
- package/lib/edit-diff.ts +199 -0
- package/{extensions → lib/features}/arnative.ts +96 -423
- package/{extensions/autocompact.ts → lib/features/auto-compact.ts} +30 -327
- package/{extensions → lib/features}/budget.ts +17 -242
- package/lib/features/footer.ts +2232 -0
- package/{extensions/section-headers.ts → lib/features/headers.ts} +38 -49
- package/{extensions → lib/features}/model-heal.ts +5 -28
- package/lib/features/progress-group.ts +1557 -0
- package/{extensions → lib/features}/render-cache.ts +113 -161
- package/{extensions → lib/features}/timestamps.ts +18 -94
- package/{extensions → lib/features}/todos.ts +44 -158
- package/lib/features/tools.ts +1202 -0
- package/lib/features/ui-render-tweaks.ts +606 -0
- package/lib/features/usage.ts +786 -0
- package/lib/format.ts +22 -4
- package/lib/git-status.ts +164 -0
- package/lib/list-picker.ts +1 -127
- package/lib/model-heal.ts +70 -162
- package/lib/patch.ts +0 -63
- package/lib/preset-store.ts +17 -133
- package/lib/provider-store.ts +573 -0
- package/lib/provider.ts +52 -851
- package/lib/runtime.ts +209 -0
- package/lib/shortcuts.ts +8 -58
- package/lib/spinner.ts +88 -162
- package/lib/status-widget.ts +4 -30
- package/lib/todos.ts +20 -143
- package/lib/usage-pricing.ts +143 -0
- package/lib/usage-store.ts +44 -378
- package/package.json +5 -6
- package/themes/arnative-blossom.json +0 -0
- package/themes/arnative-crimson.json +0 -0
- package/themes/arnative-cyberpunk.json +0 -0
- package/themes/arnative-dracula.json +0 -0
- package/themes/arnative-earthy.json +0 -0
- package/themes/arnative-garden.json +0 -0
- package/themes/arnative-gruvbox.json +0 -0
- package/themes/arnative-lemonade.json +0 -0
- package/themes/arnative-magical.json +0 -0
- package/themes/arnative-matrix.json +0 -0
- package/themes/arnative-neutral.json +0 -0
- package/themes/arnative-nord.json +0 -0
- package/themes/arnative-zinc.json +0 -0
- package/themes/arnative.json +0 -0
- package/CHANGELOG.md +0 -683
- package/extensions/animations.ts +0 -242
- package/extensions/footer.ts +0 -2617
- package/extensions/tools.ts +0 -2231
- package/extensions/ui-render-tweaks.ts +0 -1005
- package/extensions/usage-export.ts +0 -283
- package/extensions/usage.ts +0 -1406
- package/lib/check.ts +0 -21
- package/themes/gen-themes.mjs +0 -715
package/lib/chatbox.ts
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chatbox editor border, prompt styling, scroll border, and working-status dialog placement.
|
|
3
|
+
*/
|
|
4
|
+
import { InteractiveMode } from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
6
|
+
import { stripAnsi } from "./ansi.ts";
|
|
7
|
+
import { patchOnce } from "./patch.ts";
|
|
8
|
+
import {
|
|
9
|
+
CHATBOX_DIALOG_KEY as CHATBOX_DIALOG_CONFIG_KEY,
|
|
10
|
+
CHATBOX_PRESET_KEY as CHATBOX_PRESET_CONFIG_KEY,
|
|
11
|
+
CHATBOX_PROMPT_KEY as CHATBOX_PROMPT_CONFIG_KEY,
|
|
12
|
+
choiceSetting,
|
|
13
|
+
} from "./preset-store.ts";
|
|
14
|
+
import { getRuntime } from "./runtime.ts";
|
|
15
|
+
|
|
16
|
+
export const CHATBOX_PRESETS = ["Pi (system)", "Arnative"] as const;
|
|
17
|
+
export type ChatboxPreset = (typeof CHATBOX_PRESETS)[number];
|
|
18
|
+
export const DEFAULT_CHATBOX_PRESET: ChatboxPreset = "Arnative";
|
|
19
|
+
const chatboxPreset = choiceSetting(CHATBOX_PRESET_CONFIG_KEY, CHATBOX_PRESETS, DEFAULT_CHATBOX_PRESET);
|
|
20
|
+
export const CHATBOX_PRESET_KEY = chatboxPreset.slot;
|
|
21
|
+
|
|
22
|
+
export const CHATBOX_PROMPTS = ["❯", ">", "›", "▸", "→", "\uf44f", "\udb82\udf79", "\uf51b", "\udb80\udf7d", "\uf120", "\uf489", "\udb80\udd7a"] as const;
|
|
23
|
+
export type ChatboxPrompt = (typeof CHATBOX_PROMPTS)[number];
|
|
24
|
+
export const DEFAULT_CHATBOX_PROMPT: ChatboxPrompt = "❯";
|
|
25
|
+
const chatboxPrompt = choiceSetting(CHATBOX_PROMPT_CONFIG_KEY, CHATBOX_PROMPTS, DEFAULT_CHATBOX_PROMPT);
|
|
26
|
+
export const CHATBOX_PROMPT_KEY = chatboxPrompt.slot;
|
|
27
|
+
|
|
28
|
+
export const CHATBOX_DIALOGS = ["Left", "Center", "Right", "Inside", "Outside"] as const;
|
|
29
|
+
export type ChatboxDialog = (typeof CHATBOX_DIALOGS)[number];
|
|
30
|
+
export const DEFAULT_CHATBOX_DIALOG: ChatboxDialog = "Left";
|
|
31
|
+
const chatboxDialog = choiceSetting(CHATBOX_DIALOG_CONFIG_KEY, CHATBOX_DIALOGS, DEFAULT_CHATBOX_DIALOG);
|
|
32
|
+
export const CHATBOX_DIALOG_KEY = chatboxDialog.slot;
|
|
33
|
+
|
|
34
|
+
export const PROMPT_IDLE_MS = 3_000;
|
|
35
|
+
|
|
36
|
+
let chatboxPoke: () => void = () => {};
|
|
37
|
+
|
|
38
|
+
/** Register the repaint callback invoked when chatbox presets or prompt idle state change. */
|
|
39
|
+
export function bindChatboxPoke(fn: () => void): void {
|
|
40
|
+
chatboxPoke = fn;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const activeChatboxPreset = (): ChatboxPreset => chatboxPreset.get();
|
|
44
|
+
export function setChatboxPreset(name: ChatboxPreset): void {
|
|
45
|
+
chatboxPreset.set(name);
|
|
46
|
+
chatboxPoke();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const activeChatboxPrompt = (): ChatboxPrompt => chatboxPrompt.get();
|
|
50
|
+
export function setChatboxPrompt(prompt: ChatboxPrompt): void {
|
|
51
|
+
chatboxPrompt.set(prompt);
|
|
52
|
+
chatboxPoke();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function nextChatboxPrompt(current: ChatboxPrompt = activeChatboxPrompt()): ChatboxPrompt {
|
|
56
|
+
const idx = CHATBOX_PROMPTS.indexOf(current);
|
|
57
|
+
return CHATBOX_PROMPTS[(idx + 1) % CHATBOX_PROMPTS.length]!;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export const activeChatboxDialog = (): ChatboxDialog => chatboxDialog.get();
|
|
61
|
+
export function setChatboxDialog(dialog: ChatboxDialog): void {
|
|
62
|
+
chatboxDialog.set(dialog);
|
|
63
|
+
chatboxPoke();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function nextChatboxDialog(current: ChatboxDialog = activeChatboxDialog()): ChatboxDialog {
|
|
67
|
+
const idx = CHATBOX_DIALOGS.indexOf(current);
|
|
68
|
+
return CHATBOX_DIALOGS[(idx + 1) % CHATBOX_DIALOGS.length]!;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function isPromptActive(
|
|
72
|
+
now: number = Date.now(),
|
|
73
|
+
lastTyped: number = getRuntime().promptTypedAt,
|
|
74
|
+
): boolean {
|
|
75
|
+
return lastTyped > 0 && now - lastTyped <= PROMPT_IDLE_MS;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function stopPromptIdle(): void {
|
|
79
|
+
const rt = getRuntime();
|
|
80
|
+
if (rt.promptIdleTimer) clearTimeout(rt.promptIdleTimer);
|
|
81
|
+
rt.promptIdleTimer = null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function markPromptTyped(now: number = Date.now()): void {
|
|
85
|
+
const rt = getRuntime();
|
|
86
|
+
rt.promptTypedAt = now;
|
|
87
|
+
stopPromptIdle();
|
|
88
|
+
const timer = setTimeout(() => {
|
|
89
|
+
getRuntime().promptIdleTimer = null;
|
|
90
|
+
chatboxPoke();
|
|
91
|
+
}, PROMPT_IDLE_MS + 25);
|
|
92
|
+
timer.unref?.();
|
|
93
|
+
rt.promptIdleTimer = timer;
|
|
94
|
+
chatboxPoke();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function chatboxPrimaryPaint(
|
|
98
|
+
th: { fg?(color: string, text: string): string } | null | undefined,
|
|
99
|
+
): (text: string) => string {
|
|
100
|
+
if (!th?.fg) return (t) => t;
|
|
101
|
+
for (const token of ["mdHeading", "accent"]) {
|
|
102
|
+
try {
|
|
103
|
+
const probe = th.fg(token, "x");
|
|
104
|
+
if (typeof probe === "string" && probe.includes("x")) return (t) => th.fg!(token, t);
|
|
105
|
+
} catch {
|
|
106
|
+
// try next
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return (t) => t;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function isWideNerdPrompt(glyph: string): boolean {
|
|
113
|
+
const cp = glyph.codePointAt(0) ?? 0;
|
|
114
|
+
return (cp >= 0xe000 && cp <= 0xf8ff) || cp >= 0xf0000;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function paintChatboxPrompt(
|
|
118
|
+
promptGlyph: string,
|
|
119
|
+
th: { fg?(color: string, text: string): string } | null | undefined,
|
|
120
|
+
active: boolean,
|
|
121
|
+
): string {
|
|
122
|
+
const text = isWideNerdPrompt(promptGlyph) ? ` ${promptGlyph} ` : ` ${promptGlyph} `;
|
|
123
|
+
if (!th?.fg) return text;
|
|
124
|
+
try {
|
|
125
|
+
if (!active) return th.fg("dim", text);
|
|
126
|
+
return chatboxPrimaryPaint(th)(text);
|
|
127
|
+
} catch {
|
|
128
|
+
return text;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function renderChatboxScrollBorder(
|
|
133
|
+
direction: "\u2191" | "\u2193",
|
|
134
|
+
hiddenLineCount: number,
|
|
135
|
+
width: number,
|
|
136
|
+
borderColor: (text: string) => string,
|
|
137
|
+
primaryColor: (text: string) => string,
|
|
138
|
+
): string {
|
|
139
|
+
const availableWidth = Math.max(0, width);
|
|
140
|
+
if (hiddenLineCount <= 0) return borderColor("\u2500".repeat(availableWidth));
|
|
141
|
+
const core = `${direction} ${hiddenLineCount} more`;
|
|
142
|
+
const labelWidth = visibleWidth(core) + 2;
|
|
143
|
+
if (labelWidth + 2 <= availableWidth) {
|
|
144
|
+
const leftWidth = Math.floor((availableWidth - labelWidth) / 2);
|
|
145
|
+
const rightWidth = availableWidth - leftWidth - labelWidth;
|
|
146
|
+
return `${borderColor(`${"\u2500".repeat(leftWidth)} `)}${primaryColor(core)}${borderColor(` ${"\u2500".repeat(rightWidth)}`)}`;
|
|
147
|
+
}
|
|
148
|
+
const prefix = "\u2500\u2500\u2500 ";
|
|
149
|
+
const remaining = availableWidth - visibleWidth(prefix) - visibleWidth(core) - 1;
|
|
150
|
+
if (remaining >= 0) {
|
|
151
|
+
return `${borderColor(prefix)}${primaryColor(core)}${borderColor(` ${"\u2500".repeat(remaining)}`)}`;
|
|
152
|
+
}
|
|
153
|
+
return borderColor(".".repeat(Math.min(3, availableWidth)) + "\u2500".repeat(Math.max(0, availableWidth - 3)));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function renderChatboxLines(
|
|
157
|
+
preset: ChatboxPreset,
|
|
158
|
+
renderInner: (w: number) => string[],
|
|
159
|
+
width: number,
|
|
160
|
+
visible: number | undefined,
|
|
161
|
+
color: (text: string) => string,
|
|
162
|
+
prompt = " > ",
|
|
163
|
+
): string[] {
|
|
164
|
+
const promptW = visibleWidth(prompt);
|
|
165
|
+
if (preset === "Pi (system)" || typeof visible !== "number" || width < promptW + 8) return renderInner(width);
|
|
166
|
+
return boxEditorLines(renderInner(width - (2 + promptW)), visible, color, prompt);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function boxEditorLines(
|
|
170
|
+
lines: readonly string[],
|
|
171
|
+
visible: number,
|
|
172
|
+
color: (text: string) => string,
|
|
173
|
+
prompt = " > ",
|
|
174
|
+
): string[] {
|
|
175
|
+
const inner = lines.length > 0 ? visibleWidth(lines[0]!) : 0;
|
|
176
|
+
const promptWidth = visibleWidth(prompt);
|
|
177
|
+
if (inner < promptWidth + 4 || visible < 1 || lines.length < visible + 2) return [...lines];
|
|
178
|
+
const out = [...lines];
|
|
179
|
+
const side = color("\u2502");
|
|
180
|
+
const indent = " ".repeat(promptWidth);
|
|
181
|
+
for (let i = 1; i <= visible; i++) out[i] = side + (i === 1 ? prompt : indent) + lines[i]! + side;
|
|
182
|
+
const patch = color("\u2500".repeat(promptWidth));
|
|
183
|
+
out[0] = color("\u256d") + lines[0]! + patch + color("\u256e");
|
|
184
|
+
out[visible + 1] = color("\u2570") + lines[visible + 1]! + patch + color("\u256f");
|
|
185
|
+
return out;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function formatTopBorderDialog(
|
|
189
|
+
innerW: number,
|
|
190
|
+
hiddenLineCount: number,
|
|
191
|
+
dialog: "Left" | "Center" | "Right",
|
|
192
|
+
status: string,
|
|
193
|
+
spinnerOnly: string,
|
|
194
|
+
color: (text: string) => string,
|
|
195
|
+
primaryColor: (text: string) => string = (t) => t,
|
|
196
|
+
): string {
|
|
197
|
+
const scrollCore = hiddenLineCount > 0 ? `\u2191 ${hiddenLineCount} more` : "";
|
|
198
|
+
const scrollW = scrollCore ? visibleWidth(scrollCore) + 2 : 0;
|
|
199
|
+
const scrollPainted = scrollCore ? `${color(" ")}${primaryColor(scrollCore)}${color(" ")}` : "";
|
|
200
|
+
const tryPlace = (label: string): string | null => {
|
|
201
|
+
const lw = visibleWidth(label);
|
|
202
|
+
if (lw === 0 || innerW < lw + 4) return null;
|
|
203
|
+
if (dialog === "Left") {
|
|
204
|
+
if (scrollW > 0) {
|
|
205
|
+
const centerStart = Math.floor((innerW - scrollW) / 2);
|
|
206
|
+
if (centerStart >= 4 + lw + 1) {
|
|
207
|
+
return `${color("\u2500\u2500 ")}${label}${color(` ${"\u2500".repeat(centerStart - (4 + lw))}`)}${scrollPainted}${color("\u2500".repeat(innerW - centerStart - scrollW))}`;
|
|
208
|
+
}
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
return color("\u2500\u2500 ") + label + color(` ${"\u2500".repeat(innerW - lw - 4)}`);
|
|
212
|
+
}
|
|
213
|
+
if (dialog === "Center") {
|
|
214
|
+
const leftD = Math.floor((innerW - lw - 2) / 2);
|
|
215
|
+
const rightD = innerW - lw - 2 - leftD;
|
|
216
|
+
if (scrollW > 0) {
|
|
217
|
+
const leftEnd = 2 + scrollW;
|
|
218
|
+
if (leftD >= leftEnd + 1) {
|
|
219
|
+
return `${color("\u2500\u2500")}${scrollPainted}${color(`${"\u2500".repeat(leftD - leftEnd)} `)}${label}${color(` ${"\u2500".repeat(rightD)}`)}`;
|
|
220
|
+
}
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
return color(`${"\u2500".repeat(leftD)} `) + label + color(` ${"\u2500".repeat(rightD)}`);
|
|
224
|
+
}
|
|
225
|
+
if (scrollW > 0) {
|
|
226
|
+
const centerStart = Math.floor((innerW - scrollW) / 2);
|
|
227
|
+
if (centerStart + scrollW + 1 <= innerW - lw - 4) {
|
|
228
|
+
return `${color("\u2500".repeat(centerStart))}${scrollPainted}${color(`${"\u2500".repeat(innerW - lw - 4 - centerStart - scrollW)} `)}${label}${color(" \u2500\u2500")}`;
|
|
229
|
+
}
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
return color(`${"\u2500".repeat(innerW - lw - 4)} `) + label + color(" \u2500\u2500");
|
|
233
|
+
};
|
|
234
|
+
const placed = tryPlace(status) ?? (spinnerOnly ? tryPlace(spinnerOnly) : null);
|
|
235
|
+
if (placed) return placed;
|
|
236
|
+
const fallback = spinnerOnly || status;
|
|
237
|
+
const fw = visibleWidth(fallback);
|
|
238
|
+
if (innerW >= fw + 4) {
|
|
239
|
+
if (dialog === "Right") return color(`${"\u2500".repeat(innerW - fw - 4)} `) + fallback + color(" \u2500\u2500");
|
|
240
|
+
if (dialog === "Center") {
|
|
241
|
+
const leftD = Math.floor((innerW - fw - 2) / 2);
|
|
242
|
+
return color(`${"\u2500".repeat(leftD)} `) + fallback + color(` ${"\u2500".repeat(innerW - fw - 2 - leftD)}`);
|
|
243
|
+
}
|
|
244
|
+
return color("\u2500\u2500 ") + fallback + color(` ${"\u2500".repeat(innerW - fw - 4)}`);
|
|
245
|
+
}
|
|
246
|
+
const sw = Math.min(fw, innerW);
|
|
247
|
+
const prefixW = dialog === "Right" ? Math.max(0, innerW - sw) : dialog === "Center" ? Math.max(0, Math.floor((innerW - sw) / 2)) : Math.min(2, Math.max(0, innerW - sw));
|
|
248
|
+
return color("\u2500".repeat(prefixW)) + truncateToWidth(fallback, sw, "") + color("\u2500".repeat(Math.max(0, innerW - prefixW - sw)));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Places the active working/status dialog relative to the chatbox per the user's Dialog preset. */
|
|
252
|
+
export function applyChatboxDialog(
|
|
253
|
+
lines: readonly string[],
|
|
254
|
+
boxed: boolean,
|
|
255
|
+
width: number,
|
|
256
|
+
hiddenLineCount: number,
|
|
257
|
+
dialog: ChatboxDialog,
|
|
258
|
+
status: string,
|
|
259
|
+
spinnerOnly: string,
|
|
260
|
+
color: (text: string) => string,
|
|
261
|
+
primaryColor: (text: string) => string = (t) => t,
|
|
262
|
+
): string[] {
|
|
263
|
+
const sw = visibleWidth(status);
|
|
264
|
+
if (sw === 0 || lines.length === 0) return [...lines];
|
|
265
|
+
const out = [...lines];
|
|
266
|
+
if (dialog === "Outside") return out;
|
|
267
|
+
if (dialog === "Inside") {
|
|
268
|
+
if (out.length < 2) return out;
|
|
269
|
+
const side = boxed ? color("\u2502") : "";
|
|
270
|
+
if (boxed && (!out[1]!.startsWith(side) || !out[1]!.endsWith(side))) return out;
|
|
271
|
+
const inner = boxed ? out[1]!.slice(side.length, out[1]!.length - side.length) : out[1]!;
|
|
272
|
+
const innerW = boxed ? width - 2 : width;
|
|
273
|
+
const trimmed = inner.replace(/ +$/, "");
|
|
274
|
+
const gap = innerW - visibleWidth(trimmed);
|
|
275
|
+
if (gap >= sw + 2) {
|
|
276
|
+
out[1] = side + trimmed + " ".repeat(gap - sw - 1) + status + " " + side;
|
|
277
|
+
} else if (innerW >= sw + 6) {
|
|
278
|
+
const kept = truncateToWidth(trimmed, innerW - sw - 2, "");
|
|
279
|
+
const pad = Math.max(1, innerW - visibleWidth(kept) - sw - 1);
|
|
280
|
+
out[1] = side + kept + " ".repeat(pad) + status + " " + side;
|
|
281
|
+
}
|
|
282
|
+
return out;
|
|
283
|
+
}
|
|
284
|
+
const innerW = boxed ? Math.max(1, width - 2) : width;
|
|
285
|
+
const topInner = formatTopBorderDialog(innerW, hiddenLineCount, dialog, status, spinnerOnly, color, primaryColor);
|
|
286
|
+
out[0] = boxed ? color("\u256d") + topInner + color("\u256e") : topInner;
|
|
287
|
+
return out;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Left-aligns the Outside dialog status in the above-editor slot with 1 blank line
|
|
291
|
+
* above and below so it never crowds adjacent messages, Todos, or the chatbox. */
|
|
292
|
+
export function overlayOutsideDialogOnWidgetLines(lines: readonly string[], width: number, status: string): string[] {
|
|
293
|
+
const sw = visibleWidth(status);
|
|
294
|
+
if (sw === 0) return [...lines];
|
|
295
|
+
const leftRow = truncateToWidth(` ${status}`, width, "");
|
|
296
|
+
if (lines.length === 0) return ["", leftRow, ""];
|
|
297
|
+
const out = [...lines];
|
|
298
|
+
const lastIdx = out.length - 1;
|
|
299
|
+
const last = out[lastIdx]!;
|
|
300
|
+
const plainLast = stripAnsi(last).trim();
|
|
301
|
+
if (plainLast.length === 0) {
|
|
302
|
+
return ["", leftRow, ""];
|
|
303
|
+
}
|
|
304
|
+
if (!/^[╭╰│]/.test(plainLast)) {
|
|
305
|
+
const trimmed = last.replace(/ +$/, "");
|
|
306
|
+
const rest = trimmed.startsWith(" ") ? trimmed : ` ${trimmed}`;
|
|
307
|
+
out[lastIdx] = truncateToWidth(` ${status} ${rest}`, width, "");
|
|
308
|
+
if (stripAnsi(out[0]!).trim().length > 0) out.unshift("");
|
|
309
|
+
out.push("");
|
|
310
|
+
return out;
|
|
311
|
+
}
|
|
312
|
+
const boxStart = out.findIndex((line) => /^[╭╰│]/.test(stripAnsi(line).trim()));
|
|
313
|
+
const prefix = out.slice(0, boxStart).filter((line) => stripAnsi(line).trim().length > 0);
|
|
314
|
+
const boxed = out.slice(boxStart);
|
|
315
|
+
return prefix.length > 0 ? ["", leftRow, "", ...prefix, "", ...boxed] : ["", leftRow, "", ...boxed];
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const ABOVE_WRAPPED_KEY = Symbol.for("pi-arnative.aboveWidgetWrapped");
|
|
319
|
+
const ABOVE_MODE_KEY = Symbol.for("pi-arnative.aboveWidgetMode");
|
|
320
|
+
|
|
321
|
+
function installAboveEditorHook(widgetContainer: { render?: (w: number) => string[] } | undefined, mode: unknown): void {
|
|
322
|
+
if (!widgetContainer || typeof widgetContainer.render !== "function") return;
|
|
323
|
+
const box = widgetContainer as Record<symbol, unknown> & { render: (w: number) => string[] };
|
|
324
|
+
box[ABOVE_MODE_KEY] = mode;
|
|
325
|
+
if (box[ABOVE_WRAPPED_KEY]) return;
|
|
326
|
+
box[ABOVE_WRAPPED_KEY] = true;
|
|
327
|
+
const ownOrig = Object.hasOwn(box, "render") ? box.render : null;
|
|
328
|
+
box.render = function (this: Record<symbol, unknown>, width: number): string[] {
|
|
329
|
+
const baseRender = ownOrig ?? (Object.getPrototypeOf(this) as { render: (w: number) => string[] }).render;
|
|
330
|
+
const lines = baseRender.call(this, width);
|
|
331
|
+
try {
|
|
332
|
+
const hook = getRuntime().outsideDialogHook;
|
|
333
|
+
return hook ? hook(lines, width, this[ABOVE_MODE_KEY]) : lines;
|
|
334
|
+
} catch {
|
|
335
|
+
return lines;
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
interface InteractiveModeWithWidgets {
|
|
341
|
+
widgetContainerAbove?: { render?: (w: number) => string[] };
|
|
342
|
+
renderWidgetContainer?: (container: unknown, widgets: unknown, spacerWhenEmpty: boolean, leadingSpacer: boolean) => void;
|
|
343
|
+
showStatusIndicator?: (indicator: unknown) => void;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (InteractiveMode?.prototype) {
|
|
347
|
+
const modeProto = InteractiveMode.prototype as unknown as InteractiveModeWithWidgets;
|
|
348
|
+
if (typeof modeProto.renderWidgetContainer === "function") {
|
|
349
|
+
patchOnce(modeProto, "renderWidgetContainer", (orig) => function (
|
|
350
|
+
this: InteractiveModeWithWidgets,
|
|
351
|
+
container: unknown,
|
|
352
|
+
widgets: unknown,
|
|
353
|
+
spacerWhenEmpty: boolean,
|
|
354
|
+
leadingSpacer: boolean,
|
|
355
|
+
): void {
|
|
356
|
+
if (container && container === this.widgetContainerAbove) {
|
|
357
|
+
installAboveEditorHook(container as { render?: (w: number) => string[] }, this);
|
|
358
|
+
}
|
|
359
|
+
orig.call(this, container, widgets, spacerWhenEmpty, leadingSpacer);
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
if (typeof modeProto.showStatusIndicator === "function") {
|
|
363
|
+
patchOnce(modeProto, "showStatusIndicator", (orig) => function (
|
|
364
|
+
this: InteractiveModeWithWidgets,
|
|
365
|
+
indicator: unknown,
|
|
366
|
+
): void {
|
|
367
|
+
installAboveEditorHook(this.widgetContainerAbove, this);
|
|
368
|
+
orig.call(this, indicator);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
}
|
package/lib/color.ts
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
* (`themes/gen-themes.mjs`) and the theme self-check
|
|
4
4
|
* (`extensions/ui-render-tweaks.ts`). No dependencies.
|
|
5
5
|
*/
|
|
6
|
-
import { assert, isMain } from "./check.ts";
|
|
7
6
|
|
|
8
7
|
interface Rgb {
|
|
9
8
|
r: number;
|
|
@@ -128,33 +127,6 @@ export function hexToOklch(hex: string): Oklch {
|
|
|
128
127
|
return { L, C: Math.hypot(A, B), H: ((Math.atan2(B, A) * 180) / Math.PI + 360) % 360 };
|
|
129
128
|
}
|
|
130
129
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
white: "#ffffff",
|
|
135
|
-
black: "#000000",
|
|
136
|
-
short: "#0af",
|
|
137
|
-
shortExpanded: "#00aaff",
|
|
138
|
-
cyan: "#00d7ff",
|
|
139
|
-
};
|
|
140
|
-
const o = parseOklch("oklch(62% 0.1 200)");
|
|
141
|
-
assert(o !== null && Math.abs(o.L - 0.62) < 1e-9 && o.C === 0.1 && o.H === 200, "parseOklch reads percent L");
|
|
142
|
-
assert(parseOklch("oklch(0.62 0.1 200)")?.L === 0.62, "parseOklch reads fractional L");
|
|
143
|
-
assert(parseOklch(FIXTURE.short) === null, "parseOklch rejects hex");
|
|
144
|
-
assert(toHex(FIXTURE.short) === FIXTURE.shortExpanded, "toHex expands #rgb");
|
|
145
|
-
assert(toHex("oklch(100% 0 0)") === FIXTURE.white, "toHex converts oklch to hex");
|
|
146
|
-
assert(oklchToHex(1, 0, 0) === FIXTURE.white, "oklch L=1 is white");
|
|
147
|
-
assert(oklchToHex(0, 0, 0) === FIXTURE.black, "oklch L=0 is black");
|
|
148
|
-
assert(contrast(FIXTURE.white, FIXTURE.black) === 21, "white on black = 21:1");
|
|
149
|
-
assert(Math.abs(contrast(FIXTURE.black, FIXTURE.white) - 21) < 1e-9, "contrast is order-independent");
|
|
150
|
-
const back = hexToOklch(FIXTURE.cyan);
|
|
151
|
-
assert(Math.abs(back.H - 218) < 6, `hexToOklch hue of ${FIXTURE.cyan} ~218 (got ${back.H.toFixed(1)})`);
|
|
152
|
-
assert(oklchToRgb(0.7, 0.4, 145).chroma < 0.4, "out-of-gamut chroma is reduced");
|
|
153
|
-
// Quantizer vectors taken from pi-tui's own table: cube black, the teal panel pi's
|
|
154
|
-
// dark theme uses, and a near-neutral that must fall on the grey ramp.
|
|
155
|
-
assert(rgbToAnsi256({ r: 0, g: 24, b: 23 }) === 16, "near-black quantizes to cube black (index 16)");
|
|
156
|
-
assert(rgbToAnsi256({ r: 33, g: 59, b: 73 }) === 23, "pi dark panel quantizes to 23");
|
|
157
|
-
assert(rgbToAnsi256({ r: 200, g: 200, b: 200 }) >= 232, "neutral grey uses the grey ramp");
|
|
158
|
-
assert(hexToRgb("#1c3f3d").g === 0x3f, "hexToRgb reads channels");
|
|
159
|
-
console.log("lib/color.ts OK");
|
|
160
|
-
}
|
|
130
|
+
export const __test = {
|
|
131
|
+
oklchToHex,
|
|
132
|
+
};
|
package/lib/compat.ts
CHANGED
|
@@ -8,7 +8,6 @@
|
|
|
8
8
|
* (on demand) which `reasoning_effort` names the channel accepts.
|
|
9
9
|
*/
|
|
10
10
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
11
|
-
import { assert, isMain } from "./check.ts";
|
|
12
11
|
|
|
13
12
|
export type Compat = Record<string, unknown>;
|
|
14
13
|
|
|
@@ -429,62 +428,3 @@ export async function probeEffort(model: CompatModel, options: EffortProbeOption
|
|
|
429
428
|
}
|
|
430
429
|
|
|
431
430
|
// ----------------------------------------------------------- self-check
|
|
432
|
-
|
|
433
|
-
if (isMain(import.meta.url)) {
|
|
434
|
-
const base = { provider: "r", id: "m", api: "openai-completions", baseUrl: "https://relay.x/v1" };
|
|
435
|
-
assert(suggestCompat(base).supportsLongCacheRetention === false, "a third-party relay declares retention false");
|
|
436
|
-
assert(suggestCompat(base).sendSessionAffinityHeaders === true, "an openai-compatible relay gets affinity headers");
|
|
437
|
-
assert(suggestCompat({ ...base, baseUrl: "https://api.openai.com/v1" }).supportsLongCacheRetention === true, "official OpenAI keeps long retention");
|
|
438
|
-
assert(suggestCompat({ ...base, reasoning: true }).supportsDeveloperRole === false, "a reasoning model on a relay opts out of the developer role");
|
|
439
|
-
assert(suggestCompat({ ...base, reasoning: true, compat: { supportsDeveloperRole: true } }).supportsDeveloperRole === undefined, "an explicit value is never overwritten");
|
|
440
|
-
assert(suggestCompat({ ...base, id: "deepseek-v4" }).thinkingFormat === "deepseek", "a deepseek id gets the deepseek thinking format");
|
|
441
|
-
assert(suggestCompat({ ...base, api: "anthropic-messages", id: "claude-opus-4-6" }).forceAdaptiveThinking === true, "Opus 4.6 needs adaptive thinking");
|
|
442
|
-
assert(suggestCompat({ ...base, id: "claude-sonnet-4-5" }).cacheControlFormat === "anthropic", "Claude on a proxy gets anthropic cache control");
|
|
443
|
-
assert(suggestCompat({ ...base, provider: "llama.cpp", compat: { supportsStore: false, supportsDeveloperRole: false, supportsReasoningEffort: false, supportsUsageInStreaming: false, supportsStrictMode: false, maxTokensField: "max_tokens" } }).sendSessionAffinityHeaders === undefined, "pi's built-in llama.cpp is left alone");
|
|
444
|
-
|
|
445
|
-
const meta = parseModelMeta({ data: [{ id: "a", context_length: 1_000_000, max_output_tokens: 64_000 }, { id: "b", max_model_len: 262_144, max_tokens: 262_144 }] });
|
|
446
|
-
assert(meta.get("a")?.contextWindow === 1_000_000 && meta.get("a")?.maxTokens === 64_000, "a catalog entry yields window and cap");
|
|
447
|
-
assert(meta.get("b")?.maxTokens === undefined, "a max_tokens equal to the window is not an output cap");
|
|
448
|
-
assert(parseModelMeta({ models: [{ name: "models/gemini-x", context_window: 128_000 }] }).get("gemini-x")?.contextWindow === 128_000, "a gemini-shaped list parses too");
|
|
449
|
-
assert(contextWindowAccepted(1_048_576, 131_072), "growth is accepted");
|
|
450
|
-
assert(!contextWindowAccepted(8192, 1_000_000), "a per-request number is rejected");
|
|
451
|
-
assert(mergeMeta({ maxTokens: 4096 }, { maxTokens: 8192 }).maxTokens === 8192, "an output cap only grows");
|
|
452
|
-
assert(mergeMeta({ contextWindow: 1024 }, { maxTokens: 8192 }).maxTokens === 1024, "a cap above the window clamps to the window");
|
|
453
|
-
assert(mergeMeta({ contextWindow: 200_000 }, { contextWindow: 4096, maxTokens: 32_000 }).maxTokens === 32_000, "a rejected anomaly window does not clamp maxTokens");
|
|
454
|
-
|
|
455
|
-
// Nested shapes (Gemini inputTokenLimit, OpenRouter top_provider, models.dev limit) and fallback index.
|
|
456
|
-
assert(parseMeta({ inputTokenLimit: 1_048_576, outputTokenLimit: 65_536 }).contextWindow === 1_048_576, "gemini inputTokenLimit parses");
|
|
457
|
-
assert(parseMeta({ top_provider: { context_length: 200_000, max_completion_tokens: 16_384 } }).contextWindow === 200_000, "nested top_provider parses");
|
|
458
|
-
assert(parseMeta({ limit: { context: 262_144, output: 32_768 } }).maxTokens === 32_768, "nested limit.context/output parses");
|
|
459
|
-
const idx = buildCatalogIndex([
|
|
460
|
-
["z-ai/glm-5.3-flash", { contextWindow: 1_000_000, maxTokens: 131_072 }],
|
|
461
|
-
["qwen/qwen3.8-27b", { contextWindow: 262_144, maxTokens: 32_768 }],
|
|
462
|
-
]);
|
|
463
|
-
assert(findMetaForId("zai-org/GLM-5.3-Flash", idx)?.contextWindow === 1_000_000, "vendor prefix difference still matches bare name");
|
|
464
|
-
assert(findMetaForId("Qwen/Qwen3.8-27B-FP8", idx)?.contextWindow === 262_144, "quantization suffix strips for fallback lookup");
|
|
465
|
-
|
|
466
|
-
assert(buildEffortMap([]) === undefined, "nothing answered writes nothing");
|
|
467
|
-
const exact = buildEffortMap(["none", "low", "high", "max"])!;
|
|
468
|
-
assert(exact.low === "low" && exact.high === "high" && exact.max === "max", "an accepted effort maps to itself");
|
|
469
|
-
assert(exact.minimal === null && exact.medium === null && exact.xhigh === null, "a rejected effort is unsupported, never remapped");
|
|
470
|
-
assert(exact.off === undefined, "off stays pi's call");
|
|
471
|
-
assert(carriesRemap({ medium: "high" }) && carriesRemap({ low: null }) && !carriesRemap({ low: "low" }), "only a shaped map counts as deliberate");
|
|
472
|
-
assert(thinkingPayload({ thinkingFormat: "zai" }, true)?.thinking !== undefined, "a channel that needs a thinking block gets one");
|
|
473
|
-
assert(isEffortProbeable({ ...base, reasoning: true }), "a reasoning model on a relay is probeable");
|
|
474
|
-
assert(!isEffortProbeable({ ...base, reasoning: true, baseUrl: "https://api.openai.com/v1" }), "a first-party API is not probed");
|
|
475
|
-
assert(!isEffortProbeable({ ...base, reasoning: true, compat: { supportsReasoningEffort: false } }), "a channel that sends no effort is not probed");
|
|
476
|
-
|
|
477
|
-
// The probe loop itself: a channel that takes none/minimal/low/high and 400s medium.
|
|
478
|
-
const fakeFetch = (async (_url: string, init?: { body?: string }) => {
|
|
479
|
-
const level = JSON.parse(init?.body ?? "{}").reasoning_effort as string;
|
|
480
|
-
const ok = ["none", "minimal", "low", "high"].includes(level);
|
|
481
|
-
return { status: ok ? 200 : 400, text: async () => (ok ? "" : "invalid reasoning_effort") };
|
|
482
|
-
}) as unknown as typeof fetch;
|
|
483
|
-
const supported = await probeEffort({ ...base, id: "relay-model", reasoning: true }, { baseUrl: base.baseUrl, fetchImpl: fakeFetch });
|
|
484
|
-
assert(supported.join() === "none,minimal,low,high", "the probe reports exactly the accepted levels");
|
|
485
|
-
assert(buildEffortMap(supported)?.medium === null, "a rejected level is not offered");
|
|
486
|
-
const deadFetch = (async () => ({ status: 401, text: async () => "unauthorized" })) as unknown as typeof fetch;
|
|
487
|
-
assert((await probeEffort({ ...base, reasoning: true }, { baseUrl: base.baseUrl, fetchImpl: deadFetch })).length === 0, "a credential failure answers nothing, so nothing is written");
|
|
488
|
-
|
|
489
|
-
console.log("compat.ts self-check OK");
|
|
490
|
-
}
|