pi-tui-prompt-box 0.1.0 → 0.2.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/CHANGELOG.md +15 -0
- package/README.md +33 -10
- package/package.json +2 -2
- package/src/config.ts +111 -0
- package/src/index.ts +46 -8
- package/src/prompt-thinking-level.ts +26 -0
- package/src/routed-model.ts +57 -0
- package/src/ui/chat-messages.ts +221 -0
- package/src/ui/padded-containers.ts +12 -6
- package/src/ui/padded-footer.ts +16 -5
- package/src/ui/prompt-box-editor.ts +41 -6
- package/src/ui/status-line.ts +47 -6
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.2.0] - 18.11.2026
|
|
4
|
+
|
|
5
|
+
### New
|
|
6
|
+
* `[ui]` Draw user messages in the chat as prompt boxes with the box background.
|
|
7
|
+
* `[ui]` Color the accent bar of each user message with its prompt's thinking level.
|
|
8
|
+
* `[ui]` Hide Pi's `Thinking level` chat status -> the boxes show the level by color.
|
|
9
|
+
* `[config]` Add the `userMessageBoxes` option to keep Pi's own user messages.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## [0.1.1] - 12.11.2026
|
|
13
|
+
|
|
14
|
+
### New
|
|
15
|
+
* `[ui]` Show the routed model of a virtual model in the status line, not in the footer.
|
|
16
|
+
* `[ui]` Color the routed thinking level and the busy indicator with that level's color.
|
|
17
|
+
|
|
3
18
|
## [0.1.0] - 11.11.2026
|
|
4
19
|
|
|
5
20
|
### New
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# pi-tui-prompt-box
|
|
2
2
|
|
|
3
3
|
<p align="center">
|
|
4
|
-
<img width="456" src="https://media.githubusercontent.com/media/dimk90/pi-tui-prompt-box/
|
|
4
|
+
<img width="456" src="https://media.githubusercontent.com/media/dimk90/pi-tui-prompt-box/e0ae300ca7aea2ed2d873e6c7d544951dc6b0564/doc/images/pi-tui-prompt-box.png" alt="pi-tui-prompt-box preview">
|
|
5
5
|
<br>
|
|
6
6
|
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square" alt="License: MIT"></a>
|
|
7
7
|
<a href="https://pi.dev/packages/pi-tui-prompt-box"><img src="https://img.shields.io/badge/Pi-Package-6366F1?style=flat-square" alt="Pi Package"></a>
|
|
@@ -10,17 +10,27 @@
|
|
|
10
10
|
|
|
11
11
|
<br>
|
|
12
12
|
|
|
13
|
-
An alternative style for the prompt input in [Pi](https://pi.dev)
|
|
13
|
+
An alternative style for the prompt input in [Pi](https://pi.dev): a filled
|
|
14
|
+
prompt box with the model, thinking level, and busy indicator inside.
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
Your messages in the chat are drawn as the same boxes, which is
|
|
17
|
+
[optional](#configuration). Other TUI components are only moved to line up
|
|
18
|
+
with the prompt box. Their content and styling stay unchanged.
|
|
17
19
|
|
|
18
20
|
It is a building block, not a full theme: combine it with your favorite footer,
|
|
19
21
|
completion menu, or any other styling extension.
|
|
20
22
|
|
|
21
|
-
## Demo
|
|
23
|
+
## Demo: `Mode Colors`
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
The accent bar and spinner colors show the selected thinking level:
|
|
26
|
+
|
|
27
|
+

|
|
28
|
+
|
|
29
|
+
## Demo: `Message Colors`
|
|
30
|
+
|
|
31
|
+
Each message in the chat keeps the color of the thinking level it was sent with:
|
|
32
|
+
|
|
33
|
+

|
|
24
34
|
|
|
25
35
|
## Install
|
|
26
36
|
|
|
@@ -28,10 +38,10 @@ completion menu, or any other styling extension.
|
|
|
28
38
|
pi install npm:pi-tui-prompt-box
|
|
29
39
|
```
|
|
30
40
|
|
|
31
|
-
##
|
|
41
|
+
## What It Changes
|
|
32
42
|
|
|
33
43
|
- Moves the chat, widgets, dialogs, and footer right so they line up with the
|
|
34
|
-
prompt box.
|
|
44
|
+
prompt box.
|
|
35
45
|
|
|
36
46
|
- Removes the model, provider, and thinking level from the default footer, as the
|
|
37
47
|
prompt box already shows them. Custom footers stay unchanged.
|
|
@@ -39,8 +49,21 @@ pi install npm:pi-tui-prompt-box
|
|
|
39
49
|
- Moves Pi's busy indicator (spinner and message) into the prompt box. Custom
|
|
40
50
|
spinner widgets stay unchanged.
|
|
41
51
|
|
|
42
|
-
-
|
|
43
|
-
|
|
52
|
+
- Draws your messages in the chat as prompt boxes, colored by the thinking level
|
|
53
|
+
they were sent with, and hides Pi's `Thinking level` chat status. This is
|
|
54
|
+
[optional](#configuration).
|
|
55
|
+
|
|
56
|
+
## Configuration
|
|
57
|
+
|
|
58
|
+
User message boxes are on by default. To keep Pi's own look for your messages
|
|
59
|
+
in the chat, create
|
|
60
|
+
`~/.pi/agent/extensions/pi-tui-prompt-box.json` and run `/reload`:
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"userMessageBoxes": false
|
|
65
|
+
}
|
|
66
|
+
```
|
|
44
67
|
|
|
45
68
|
## Context Footprint
|
|
46
69
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-tui-prompt-box",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A prompt box style for Pi that combines with other styling extensions",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"extensions": [
|
|
33
33
|
"./src/index.ts"
|
|
34
34
|
],
|
|
35
|
-
"image": "https://media.githubusercontent.com/media/dimk90/pi-tui-prompt-box/
|
|
35
|
+
"image": "https://media.githubusercontent.com/media/dimk90/pi-tui-prompt-box/e0ae300ca7aea2ed2d873e6c7d544951dc6b0564/doc/images/pi-tui-prompt-box.png"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"@earendil-works/pi-coding-agent": "*",
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Override-only user configuration.
|
|
3
|
+
*
|
|
4
|
+
* Defaults live in this module; the global `<agent dir>/extensions/pi-tui-prompt-box.json` carries overrides
|
|
5
|
+
* only and is never created by the extension. An absent file or omitted key is silent; an unreadable,
|
|
6
|
+
* unparseable, or invalid file warns and falls back to the defaults.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { readFileSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
|
|
12
|
+
import { getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
13
|
+
|
|
14
|
+
/** Global override file; project-local configuration is not supported. */
|
|
15
|
+
export const CONFIG_FILE_NAME = "pi-tui-prompt-box.json";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Options of the extension.
|
|
19
|
+
*/
|
|
20
|
+
export interface PromptBoxConfig {
|
|
21
|
+
/** Draw user messages in the chat as prompt boxes and hide Pi's thinking level status; see doc/ui.md. */
|
|
22
|
+
userMessageBoxes: boolean;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A loaded configuration with the problems found in its file.
|
|
27
|
+
*/
|
|
28
|
+
export interface ConfigLoadResult {
|
|
29
|
+
/** Defaults with every valid override applied. */
|
|
30
|
+
config: PromptBoxConfig;
|
|
31
|
+
/** One message per problem, ready to show to the user. */
|
|
32
|
+
warnings: string[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Configuration used when no override file exists. */
|
|
36
|
+
export const DEFAULT_CONFIG: Readonly<PromptBoxConfig> = { userMessageBoxes: true };
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Returns the absolute path of the global override file.
|
|
40
|
+
*/
|
|
41
|
+
export function getConfigFilePath(): string {
|
|
42
|
+
return join(getAgentDir(), "extensions", CONFIG_FILE_NAME);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Reads and validates the override file at `filePath`. A missing file yields the defaults without warnings;
|
|
47
|
+
* an invalid or unknown key warns and keeps the other keys.
|
|
48
|
+
*/
|
|
49
|
+
export function loadConfigFile(filePath: string): ConfigLoadResult {
|
|
50
|
+
let text: string;
|
|
51
|
+
try {
|
|
52
|
+
text = readFileSync(filePath, "utf8");
|
|
53
|
+
} catch (error) {
|
|
54
|
+
// An absent file is the expected state for users who never configured anything
|
|
55
|
+
if (isMissingFileError(error))
|
|
56
|
+
return { config: { ...DEFAULT_CONFIG }, warnings: [] };
|
|
57
|
+
|
|
58
|
+
return useDefaults(`Cannot read ${filePath}: ${describeError(error)}.`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let overrides: unknown;
|
|
62
|
+
try {
|
|
63
|
+
overrides = JSON.parse(text);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
return useDefaults(`Cannot parse ${filePath}: ${describeError(error)}.`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (typeof overrides !== "object" || overrides === null || Array.isArray(overrides))
|
|
69
|
+
return useDefaults(`${filePath} must contain a JSON object.`);
|
|
70
|
+
|
|
71
|
+
return applyOverrides(overrides, filePath);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Applies the valid entries of `overrides` to the defaults, with a warning for every other entry.
|
|
76
|
+
*/
|
|
77
|
+
function applyOverrides(overrides: object, filePath: string): ConfigLoadResult {
|
|
78
|
+
const config = { ...DEFAULT_CONFIG };
|
|
79
|
+
const warnings: string[] = [];
|
|
80
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
81
|
+
if (key !== "userMessageBoxes")
|
|
82
|
+
warnings.push(`Unknown key "${key}" in ${filePath}.`);
|
|
83
|
+
else if (typeof value !== "boolean")
|
|
84
|
+
warnings.push(`"${key}" in ${filePath} must be true or false.`);
|
|
85
|
+
else
|
|
86
|
+
config.userMessageBoxes = value;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return { config, warnings };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Returns the defaults with `warning` as the only problem.
|
|
94
|
+
*/
|
|
95
|
+
function useDefaults(warning: string): ConfigLoadResult {
|
|
96
|
+
return { config: { ...DEFAULT_CONFIG }, warnings: [warning] };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Tells whether `error` is the file system error for a path that does not exist.
|
|
101
|
+
*/
|
|
102
|
+
function isMissingFileError(error: unknown): boolean {
|
|
103
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Returns a short reason for an unknown thrown value.
|
|
108
|
+
*/
|
|
109
|
+
function describeError(error: unknown): string {
|
|
110
|
+
return error instanceof Error ? error.message : String(error);
|
|
111
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -4,22 +4,29 @@
|
|
|
4
4
|
* Usage: pi -e .
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import type { ExtensionAPI, Theme } from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import type { RgbColor, TUI } from "@earendil-works/pi-tui";
|
|
9
9
|
|
|
10
|
+
import { getConfigFilePath, loadConfigFile } from "./config.ts";
|
|
11
|
+
import { findPromptThinkingLevel } from "./prompt-thinking-level.ts";
|
|
12
|
+
import { findRoutedModel } from "./routed-model.ts";
|
|
10
13
|
import { watchTerminalBackground } from "./terminal-background.ts";
|
|
14
|
+
import { type ChatMessageStyle, styleChatMessages } from "./ui/chat-messages.ts";
|
|
11
15
|
import { computeBoxColors, type ReadBoxColors } from "./ui/colors.ts";
|
|
12
|
-
import { padPiContainers } from "./ui/padded-containers.ts";
|
|
16
|
+
import { findPiContainers, padPiContainers } from "./ui/padded-containers.ts";
|
|
13
17
|
import { findBuiltInFooter, PaddedFooter } from "./ui/padded-footer.ts";
|
|
14
18
|
import { PromptBoxEditor, type RenderStatus } from "./ui/prompt-box-editor.ts";
|
|
15
19
|
import { readScrollbarPadding } from "./ui/scrollbar-padding.ts";
|
|
16
|
-
import { formatStatus } from "./ui/status-line.ts";
|
|
20
|
+
import { formatStatus, readRoutedLevelColor } from "./ui/status-line.ts";
|
|
21
|
+
|
|
22
|
+
type ThinkingLevel = Parameters<Theme["getThinkingBorderColor"]>[0];
|
|
17
23
|
|
|
18
24
|
/**
|
|
19
25
|
* Registers the prompt box editor for every session.
|
|
20
26
|
*/
|
|
21
27
|
export default function promptBox(pi: ExtensionAPI): void {
|
|
22
28
|
let restoreContainers: (() => void) | undefined;
|
|
29
|
+
let restoreChatMessages: (() => void) | undefined;
|
|
23
30
|
let stopWatchingBackground: (() => void) | undefined;
|
|
24
31
|
|
|
25
32
|
pi.on("session_start", (_event, ctx) => {
|
|
@@ -31,6 +38,7 @@ export default function promptBox(pi: ExtensionAPI): void {
|
|
|
31
38
|
model: ctx.model,
|
|
32
39
|
getProviderName: (provider) => ctx.modelRegistry.getProviderDisplayName(provider),
|
|
33
40
|
thinkingLevel: pi.getThinkingLevel(),
|
|
41
|
+
routedModel: findRoutedModel(ctx),
|
|
34
42
|
});
|
|
35
43
|
|
|
36
44
|
let terminalBackground: RgbColor | undefined;
|
|
@@ -42,7 +50,13 @@ export default function promptBox(pi: ExtensionAPI): void {
|
|
|
42
50
|
ctx.ui.setEditorComponent((tui, theme, keybindings) => {
|
|
43
51
|
editorTui = tui;
|
|
44
52
|
const readRightPadding = () => readScrollbarPadding(tui);
|
|
45
|
-
|
|
53
|
+
const readBusyColor = () => readRoutedLevelColor(ctx.ui.theme, findRoutedModel(ctx));
|
|
54
|
+
editor = new PromptBoxEditor(tui, theme, keybindings,
|
|
55
|
+
renderStatus,
|
|
56
|
+
readBoxColors,
|
|
57
|
+
readRightPadding,
|
|
58
|
+
readBusyColor,
|
|
59
|
+
);
|
|
46
60
|
return editor;
|
|
47
61
|
});
|
|
48
62
|
|
|
@@ -50,6 +64,11 @@ export default function promptBox(pi: ExtensionAPI): void {
|
|
|
50
64
|
if (!editorTui || !editor)
|
|
51
65
|
return;
|
|
52
66
|
|
|
67
|
+
// Read on every session start, so a reload applies a changed file
|
|
68
|
+
const { config, warnings } = loadConfigFile(getConfigFilePath());
|
|
69
|
+
for (const warning of warnings)
|
|
70
|
+
ctx.ui.notify(warning, "warning");
|
|
71
|
+
|
|
53
72
|
stopWatchingBackground = watchTerminalBackground(editorTui, (background) => {
|
|
54
73
|
terminalBackground = background;
|
|
55
74
|
editorTui?.requestRender();
|
|
@@ -57,12 +76,28 @@ export default function promptBox(pi: ExtensionAPI): void {
|
|
|
57
76
|
|
|
58
77
|
restoreContainers = padPiContainers(editorTui, editor);
|
|
59
78
|
|
|
79
|
+
// A message sent in this session has no entry id yet and gets the current level
|
|
80
|
+
const resolveBarColor: ChatMessageStyle["resolveBarColor"] = (messageId) => {
|
|
81
|
+
const promptLevel = messageId ? findPromptThinkingLevel(ctx.sessionManager, messageId) : undefined;
|
|
82
|
+
// The session keeps levels as plain strings; Pi's theme draws an unknown level as `off`
|
|
83
|
+
const level = (promptLevel ?? pi.getThinkingLevel()) as ThinkingLevel;
|
|
84
|
+
|
|
85
|
+
return (text) => ctx.ui.theme.getThinkingBorderColor(level)(text);
|
|
86
|
+
};
|
|
87
|
+
const chat = config.userMessageBoxes ? findPiContainers(editorTui, editor)?.chat : undefined;
|
|
88
|
+
if (chat)
|
|
89
|
+
restoreChatMessages = styleChatMessages(chat, { readBoxColors, resolveBarColor });
|
|
90
|
+
|
|
60
91
|
// Search before setFooter(): it removes the current footer from the tree before calling the factory
|
|
61
92
|
const builtInFooter = findBuiltInFooter(editorTui);
|
|
62
93
|
if (!builtInFooter)
|
|
63
94
|
return;
|
|
64
95
|
|
|
65
|
-
const readModelState = () => ({
|
|
96
|
+
const readModelState = () => ({
|
|
97
|
+
model: ctx.model,
|
|
98
|
+
thinkingLevel: pi.getThinkingLevel(),
|
|
99
|
+
routedModel: findRoutedModel(ctx),
|
|
100
|
+
});
|
|
66
101
|
ctx.ui.setFooter((tui) => new PaddedFooter(builtInFooter, readModelState, () => readScrollbarPadding(tui)));
|
|
67
102
|
});
|
|
68
103
|
|
|
@@ -70,11 +105,14 @@ export default function promptBox(pi: ExtensionAPI): void {
|
|
|
70
105
|
stopWatchingBackground?.();
|
|
71
106
|
stopWatchingBackground = undefined;
|
|
72
107
|
|
|
73
|
-
// Pi keeps its containers across extension reloads and session changes, so remove the padding
|
|
74
|
-
// next extension instance; on quit
|
|
75
|
-
if (event.reason !== "quit")
|
|
108
|
+
// Pi keeps its containers across extension reloads and session changes, so remove the padding and the
|
|
109
|
+
// message styling for the next extension instance; on quit they stay, as Pi may still print the transcript
|
|
110
|
+
if (event.reason !== "quit") {
|
|
76
111
|
restoreContainers?.();
|
|
112
|
+
restoreChatMessages?.();
|
|
113
|
+
}
|
|
77
114
|
|
|
78
115
|
restoreContainers = undefined;
|
|
116
|
+
restoreChatMessages = undefined;
|
|
79
117
|
});
|
|
80
118
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thinking level of a past prompt, read from the session.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Finds the thinking level that was set when the session entry `entryId` was added: the level of the nearest
|
|
9
|
+
* thinking level change above it on its branch.
|
|
10
|
+
*
|
|
11
|
+
* Pi records the level at session start and on every change, so only an unknown entry returns `undefined`.
|
|
12
|
+
*/
|
|
13
|
+
export function findPromptThinkingLevel(
|
|
14
|
+
sessionManager: Pick<ExtensionContext["sessionManager"], "getEntry">,
|
|
15
|
+
entryId: string,
|
|
16
|
+
): string | undefined {
|
|
17
|
+
let entry = sessionManager.getEntry(entryId);
|
|
18
|
+
while (entry) {
|
|
19
|
+
if (entry.type === "thinking_level_change")
|
|
20
|
+
return entry.thinkingLevel;
|
|
21
|
+
|
|
22
|
+
entry = entry.parentId ? sessionManager.getEntry(entry.parentId) : undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Routed model: the physical model a virtual model picked for the latest successful response.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
|
|
7
|
+
// API id of Pi's virtual models; Pi does not export its own constant
|
|
8
|
+
const VIRTUAL_MODEL_API = "pi-virtual";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Physical model that answered, with the thinking level the router picked for it.
|
|
12
|
+
*/
|
|
13
|
+
export interface RoutedModel {
|
|
14
|
+
model: NonNullable<ExtensionContext["model"]>;
|
|
15
|
+
thinkingLevel?: Parameters<Theme["getThinkingBorderColor"]>[0];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Parts of the extension context the routed model is read from.
|
|
20
|
+
*/
|
|
21
|
+
export interface RoutedModelSource {
|
|
22
|
+
model: ExtensionContext["model"];
|
|
23
|
+
sessionManager: Pick<ExtensionContext["sessionManager"], "getLeafEntry" | "getEntry">;
|
|
24
|
+
modelRegistry: Pick<ExtensionContext["modelRegistry"], "find">;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Finds the routed model of the latest successful response on the current session branch.
|
|
29
|
+
*
|
|
30
|
+
* Pi gives extensions no access to the `routedModel` of its session, so this follows the same rules:
|
|
31
|
+
* only under a virtual selection, and failed or aborted responses are skipped. Returns `undefined`
|
|
32
|
+
* without such a response, or when its model is no longer in the registry.
|
|
33
|
+
*/
|
|
34
|
+
export function findRoutedModel(source: RoutedModelSource): RoutedModel | undefined {
|
|
35
|
+
const { model, sessionManager, modelRegistry } = source;
|
|
36
|
+
if (model?.api !== VIRTUAL_MODEL_API)
|
|
37
|
+
return undefined;
|
|
38
|
+
|
|
39
|
+
// The answer is usually near the leaf, so walk up from it instead of building the whole branch
|
|
40
|
+
let entry = sessionManager.getLeafEntry();
|
|
41
|
+
while (entry) {
|
|
42
|
+
if (entry.type === "message" && entry.message.role === "assistant") {
|
|
43
|
+
const { stopReason, provider, model: modelId, thinkingLevel } = entry.message;
|
|
44
|
+
if (stopReason !== "error" && stopReason !== "aborted") {
|
|
45
|
+
const routedModel = modelRegistry.find(provider, modelId);
|
|
46
|
+
if (!routedModel || routedModel.api === VIRTUAL_MODEL_API)
|
|
47
|
+
return undefined;
|
|
48
|
+
|
|
49
|
+
return { model: routedModel, thinkingLevel };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
entry = entry.parentId ? sessionManager.getEntry(entry.parentId) : undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pi's chat messages restyled: user messages drawn as prompt boxes, and Pi's thinking level status hidden;
|
|
3
|
+
* see doc/ui.md.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { UserMessageComponent } from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { type Component, Markdown, Spacer, stripTerminalSequences } from "@earendil-works/pi-tui";
|
|
8
|
+
|
|
9
|
+
import type { ReadBoxColors } from "./colors.ts";
|
|
10
|
+
import type { ParentComponent } from "./padded-containers.ts";
|
|
11
|
+
import { BAR_WIDTH, INNER_PADDING, renderBoxRow } from "./prompt-box-editor.ts";
|
|
12
|
+
|
|
13
|
+
// Pi's chat status after a thinking level change; the user message boxes show the level by their color
|
|
14
|
+
const THINKING_LEVEL_STATUS = /^Thinking level: \S+$/;
|
|
15
|
+
// The chat is already moved by the box margins
|
|
16
|
+
const NO_MARGINS = { left: 0, right: 0 };
|
|
17
|
+
// Fields of Pi's Markdown that draw the padding and the background of a user message
|
|
18
|
+
const MARKDOWN_BLOCK_FIELDS = ["paddingX", "paddingY", "defaultTextStyle"] as const;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Look of the restyled chat messages.
|
|
22
|
+
*/
|
|
23
|
+
export interface ChatMessageStyle {
|
|
24
|
+
/** Called on every render, so the user message boxes follow the terminal background like the prompt box. */
|
|
25
|
+
readBoxColors: ReadBoxColors;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the accent bar color of a user message. Called once per message, when it is styled, with the
|
|
28
|
+
* session entry id Pi gave it; a message sent in this session has none. The returned color is called on
|
|
29
|
+
* every render.
|
|
30
|
+
*/
|
|
31
|
+
resolveBarColor: (messageId: string | undefined) => (text: string) => string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Restyles the messages of Pi's `chat` container: the ones it holds now and every one Pi adds later.
|
|
36
|
+
*
|
|
37
|
+
* User messages are drawn as prompt boxes, see {@link boxUserMessage}. Pi's thinking level status is hidden,
|
|
38
|
+
* see {@link hideThinkingLevelStatus}. Only the `chat` instance and the message instances are changed, not
|
|
39
|
+
* their classes.
|
|
40
|
+
*
|
|
41
|
+
* @returns A function that restores `chat` and the messages it still holds, or `undefined` when `chat` is
|
|
42
|
+
* already restyled.
|
|
43
|
+
*/
|
|
44
|
+
export function styleChatMessages(chat: ParentComponent, style: ChatMessageStyle): (() => void) | undefined {
|
|
45
|
+
if (Object.hasOwn(chat, "addChild"))
|
|
46
|
+
return undefined;
|
|
47
|
+
|
|
48
|
+
const restoreFunctions = new WeakMap<Component, () => void>();
|
|
49
|
+
const styleMessage = (message: Component, previous: Component | undefined) => {
|
|
50
|
+
const restore = message instanceof UserMessageComponent
|
|
51
|
+
? boxUserMessage(message, style)
|
|
52
|
+
: hideThinkingLevelStatus(message, previous);
|
|
53
|
+
if (restore)
|
|
54
|
+
restoreFunctions.set(message, restore);
|
|
55
|
+
};
|
|
56
|
+
chat.children.forEach((message, index) => styleMessage(message, chat.children[index - 1]));
|
|
57
|
+
|
|
58
|
+
const addChild = chat.addChild.bind(chat);
|
|
59
|
+
chat.addChild = (message: Component): void => {
|
|
60
|
+
styleMessage(message, chat.children.at(-1));
|
|
61
|
+
addChild(message);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
return () => {
|
|
65
|
+
Reflect.deleteProperty(chat, "addChild");
|
|
66
|
+
for (const message of chat.children)
|
|
67
|
+
restoreFunctions.get(message)?.();
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Makes the user `message` render as a prompt box: accent bar, and the text lines between a top and a bottom
|
|
73
|
+
* padding row on the box background; see doc/ui.md.
|
|
74
|
+
*
|
|
75
|
+
* Pi's Markdown inside the message keeps rendering the text, without its own padding and background. Pi
|
|
76
|
+
* replaces it when the `outputPad` setting changes, so it is looked up on every render. When it is not found,
|
|
77
|
+
* or below the box minimum width, the message renders as Pi draws it.
|
|
78
|
+
*
|
|
79
|
+
* @returns A function that restores Pi's rendering.
|
|
80
|
+
*/
|
|
81
|
+
function boxUserMessage(message: UserMessageComponent, style: ChatMessageStyle): () => void {
|
|
82
|
+
const render = message.render.bind(message);
|
|
83
|
+
const barColor = style.resolveBarColor(readMessageId(message));
|
|
84
|
+
let unpadded: { markdown: Markdown; restore: () => void } | undefined;
|
|
85
|
+
const restoreMarkdown = () => {
|
|
86
|
+
unpadded?.restore();
|
|
87
|
+
unpadded = undefined;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
message.render = (width: number): string[] => {
|
|
91
|
+
const textWidth = width - BAR_WIDTH - 2 * INNER_PADDING;
|
|
92
|
+
const markdown = textWidth < 1 ? undefined : findMarkdown(message);
|
|
93
|
+
if (!markdown) {
|
|
94
|
+
restoreMarkdown();
|
|
95
|
+
return render(width);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (unpadded?.markdown !== markdown)
|
|
99
|
+
unpadded = { markdown, restore: unpadMarkdown(markdown) };
|
|
100
|
+
|
|
101
|
+
const textLines = render(textWidth);
|
|
102
|
+
if (textLines.length === 0)
|
|
103
|
+
return textLines;
|
|
104
|
+
|
|
105
|
+
const boxStyle = { margins: NO_MARGINS, barColor, colors: style.readBoxColors() };
|
|
106
|
+
|
|
107
|
+
return [
|
|
108
|
+
renderBoxRow("", textWidth, boxStyle),
|
|
109
|
+
...textLines.map((line) => renderBoxRow(line, textWidth, boxStyle)),
|
|
110
|
+
renderBoxRow("", textWidth, boxStyle),
|
|
111
|
+
];
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
return () => {
|
|
115
|
+
Reflect.deleteProperty(message, "render");
|
|
116
|
+
restoreMarkdown();
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Returns the session entry id of the user `message`, which Pi's types mark private, or `undefined` for a
|
|
122
|
+
* message sent in this session.
|
|
123
|
+
*/
|
|
124
|
+
function readMessageId(message: UserMessageComponent): string | undefined {
|
|
125
|
+
const messageId: unknown = Reflect.get(message, "messageId");
|
|
126
|
+
|
|
127
|
+
return typeof messageId === "string" ? messageId : undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Finds Pi's Markdown that renders the text of the user `message`, or returns `undefined` when the message
|
|
132
|
+
* has another shape.
|
|
133
|
+
*/
|
|
134
|
+
function findMarkdown(message: UserMessageComponent): Markdown | undefined {
|
|
135
|
+
const markdown = message.children[0];
|
|
136
|
+
if (!(markdown instanceof Markdown) || typeof Reflect.get(markdown, "paddingX") !== "number")
|
|
137
|
+
return undefined;
|
|
138
|
+
|
|
139
|
+
return markdown;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Removes the padding and the background of `markdown`, so it renders only the text lines. Pi's types mark
|
|
144
|
+
* these fields private.
|
|
145
|
+
*
|
|
146
|
+
* @returns A function that sets them back.
|
|
147
|
+
*/
|
|
148
|
+
function unpadMarkdown(markdown: Markdown): () => void {
|
|
149
|
+
const fields = MARKDOWN_BLOCK_FIELDS.map((name) => [name, Reflect.get(markdown, name) as unknown] as const);
|
|
150
|
+
const textStyle = Reflect.get(markdown, "defaultTextStyle") as unknown;
|
|
151
|
+
|
|
152
|
+
Reflect.set(markdown, "paddingX", 0);
|
|
153
|
+
Reflect.set(markdown, "paddingY", 0);
|
|
154
|
+
Reflect.set(markdown, "defaultTextStyle", { ...(isObject(textStyle) ? textStyle : {}), bgColor: undefined });
|
|
155
|
+
markdown.invalidate();
|
|
156
|
+
|
|
157
|
+
return () => {
|
|
158
|
+
for (const [name, value] of fields)
|
|
159
|
+
Reflect.set(markdown, name, value);
|
|
160
|
+
|
|
161
|
+
markdown.invalidate();
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Hides Pi's `Thinking level: <level>` chat status: the `status` text and the `spacer` Pi adds before it.
|
|
167
|
+
*
|
|
168
|
+
* Pi shows later statuses in the same text component, so the text is checked on every render and only the
|
|
169
|
+
* thinking level status is hidden. Pi does not export its status text class, so it is matched by its `build`
|
|
170
|
+
* method; other components are left unchanged.
|
|
171
|
+
*
|
|
172
|
+
* @returns A function that shows the status again, or `undefined` when `status` is not a status text.
|
|
173
|
+
*/
|
|
174
|
+
function hideThinkingLevelStatus(status: Component, spacer: Component | undefined): (() => void) | undefined {
|
|
175
|
+
if (!isStatusText(status))
|
|
176
|
+
return undefined;
|
|
177
|
+
|
|
178
|
+
const isHidden = () => THINKING_LEVEL_STATUS.test(stripTerminalSequences(status.build()).trim());
|
|
179
|
+
const showStatus = hideWhile(status, isHidden);
|
|
180
|
+
const showSpacer = spacer instanceof Spacer ? hideWhile(spacer, isHidden) : undefined;
|
|
181
|
+
|
|
182
|
+
return () => {
|
|
183
|
+
showStatus();
|
|
184
|
+
showSpacer?.();
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Pi's status text, which builds its themed text on demand.
|
|
190
|
+
*/
|
|
191
|
+
interface StatusText extends Component {
|
|
192
|
+
build: () => string;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Checks if `component` has a `build` method, as Pi's status text has.
|
|
197
|
+
*/
|
|
198
|
+
function isStatusText(component: Component): component is StatusText {
|
|
199
|
+
return "build" in component && typeof component.build === "function";
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Makes `component` render no lines while `isHidden()` returns `true`.
|
|
204
|
+
*
|
|
205
|
+
* @returns A function that removes the check.
|
|
206
|
+
*/
|
|
207
|
+
function hideWhile(component: Component, isHidden: () => boolean): () => void {
|
|
208
|
+
const render = component.render.bind(component);
|
|
209
|
+
component.render = (width: number): string[] => (isHidden() ? [] : render(width));
|
|
210
|
+
|
|
211
|
+
return () => {
|
|
212
|
+
Reflect.deleteProperty(component, "render");
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Checks if `value` is a non-null object.
|
|
218
|
+
*/
|
|
219
|
+
function isObject(value: unknown): value is object {
|
|
220
|
+
return typeof value === "object" && value !== null;
|
|
221
|
+
}
|
|
@@ -20,8 +20,9 @@ const CHAT_INDEX = 2;
|
|
|
20
20
|
/**
|
|
21
21
|
* Component with child components, matched by shape like Pi's `Container`.
|
|
22
22
|
*/
|
|
23
|
-
interface ParentComponent extends Component {
|
|
23
|
+
export interface ParentComponent extends Component {
|
|
24
24
|
children: Component[];
|
|
25
|
+
addChild(component: Component): void;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
/**
|
|
@@ -101,7 +102,9 @@ export function padComponent(
|
|
|
101
102
|
/**
|
|
102
103
|
* Pi's containers that get the padding.
|
|
103
104
|
*/
|
|
104
|
-
interface PiContainers {
|
|
105
|
+
export interface PiContainers {
|
|
106
|
+
/** Holds the messages of the session. */
|
|
107
|
+
chat: ParentComponent;
|
|
105
108
|
/** Chat, queued messages, and widgets above and below the editor. */
|
|
106
109
|
alwaysPadded: Component[];
|
|
107
110
|
/** Holds the editor, or a selector or extension dialog shown in its place. */
|
|
@@ -112,7 +115,7 @@ interface PiContainers {
|
|
|
112
115
|
* Finds Pi's chat, queued messages, widget, and editor containers, or returns `undefined` when the tree has
|
|
113
116
|
* another shape.
|
|
114
117
|
*/
|
|
115
|
-
function findPiContainers(root: Component, editor: Component): PiContainers | undefined {
|
|
118
|
+
export function findPiContainers(root: Component, editor: Component): PiContainers | undefined {
|
|
116
119
|
if (!isParent(root) || root.children.length !== ROOT_CHILD_COUNT) return undefined;
|
|
117
120
|
if (root.children.some((child) => !isParent(child))) return undefined;
|
|
118
121
|
const rootChildren = root.children as ParentComponent[];
|
|
@@ -125,7 +128,7 @@ function findPiContainers(root: Component, editor: Component): PiContainers | un
|
|
|
125
128
|
if (!chat || !isParent(chat)) return undefined;
|
|
126
129
|
|
|
127
130
|
const alwaysPadded = [chat, ...PADDED_ROOT_INDEXES.map((index) => rootChildren[index] as Component)];
|
|
128
|
-
return { alwaysPadded, editorContainer };
|
|
131
|
+
return { chat, alwaysPadded, editorContainer };
|
|
129
132
|
}
|
|
130
133
|
|
|
131
134
|
/**
|
|
@@ -136,8 +139,11 @@ function isPadded(component: Component): boolean {
|
|
|
136
139
|
}
|
|
137
140
|
|
|
138
141
|
/**
|
|
139
|
-
* Checks if `component` has a `children` array, as Pi's `Container` has.
|
|
142
|
+
* Checks if `component` has a `children` array and an `addChild` method, as Pi's `Container` has.
|
|
140
143
|
*/
|
|
141
144
|
function isParent(component: Component): component is ParentComponent {
|
|
142
|
-
|
|
145
|
+
if (!("children" in component) || !Array.isArray(component.children))
|
|
146
|
+
return false;
|
|
147
|
+
|
|
148
|
+
return "addChild" in component && typeof component.addChild === "function";
|
|
143
149
|
}
|
package/src/ui/padded-footer.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
6
6
|
import { type Component, stripTerminalSequences, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
|
|
7
7
|
|
|
8
|
+
import type { RoutedModel } from "../routed-model.ts";
|
|
8
9
|
import { LEFT_MARGIN, RIGHT_MARGIN } from "./prompt-box-editor.ts";
|
|
9
10
|
import type { ReadRightPadding } from "./scrollbar-padding.ts";
|
|
10
11
|
|
|
@@ -20,6 +21,8 @@ const MIN_MODEL_GAP = 2;
|
|
|
20
21
|
export interface FooterModelState {
|
|
21
22
|
model: ExtensionContext["model"];
|
|
22
23
|
thinkingLevel: string;
|
|
24
|
+
/** Model a virtual `model` routed the latest response to; Pi's footer adds it after `→`. */
|
|
25
|
+
routedModel?: RoutedModel;
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
/**
|
|
@@ -45,14 +48,14 @@ export function findBuiltInFooter(root: Component): Component | undefined {
|
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
/**
|
|
48
|
-
* Removes the model part that Pi's built-in footer right-aligns on its stats line: `(provider) model • level
|
|
51
|
+
* Removes the model part that Pi's built-in footer right-aligns on its stats line: `(provider) model • level`,
|
|
52
|
+
* with ` → model • level` after it for a routed model.
|
|
49
53
|
*
|
|
50
54
|
* The prompt box status line already shows it. The line is changed only when it ends with exactly the text
|
|
51
|
-
* Pi builds from `state`; otherwise, for example when Pi truncated it
|
|
52
|
-
* returned unchanged.
|
|
55
|
+
* Pi builds from `state`; otherwise, for example when Pi truncated it, it is returned unchanged.
|
|
53
56
|
*/
|
|
54
57
|
export function removeModelPart(line: string, state: FooterModelState): string {
|
|
55
|
-
const { model, thinkingLevel } = state;
|
|
58
|
+
const { model, thinkingLevel, routedModel } = state;
|
|
56
59
|
if (!model)
|
|
57
60
|
return line;
|
|
58
61
|
|
|
@@ -60,9 +63,17 @@ export function removeModelPart(line: string, state: FooterModelState): string {
|
|
|
60
63
|
if (model.reasoning)
|
|
61
64
|
modelPart += thinkingLevel === "off" ? " • thinking off" : ` • ${thinkingLevel}`;
|
|
62
65
|
|
|
66
|
+
// Pi may show no routed model where the status line still does, for example after compaction
|
|
67
|
+
const modelParts = [modelPart];
|
|
68
|
+
if (routedModel) {
|
|
69
|
+
const routedLevel = routedModel.thinkingLevel ? ` • ${routedModel.thinkingLevel}` : "";
|
|
70
|
+
modelParts.unshift(`${modelPart} → ${routedModel.model.id}${routedLevel}`);
|
|
71
|
+
}
|
|
72
|
+
|
|
63
73
|
const plainLine = stripTerminalSequences(line);
|
|
64
74
|
const gap = " ".repeat(MIN_MODEL_GAP);
|
|
65
|
-
|
|
75
|
+
const candidates = modelParts.flatMap((part) => [`(${model.provider}) ${part}`, part]);
|
|
76
|
+
for (const candidate of candidates) {
|
|
66
77
|
if (!plainLine.endsWith(gap + candidate))
|
|
67
78
|
continue;
|
|
68
79
|
|
|
@@ -23,8 +23,8 @@ import type { BashMode } from "./status-line.ts";
|
|
|
23
23
|
*/
|
|
24
24
|
export const LEFT_MARGIN = 1;
|
|
25
25
|
export const RIGHT_MARGIN = 0;
|
|
26
|
-
const INNER_PADDING = 2;
|
|
27
|
-
const BAR_WIDTH = 1;
|
|
26
|
+
export const INNER_PADDING = 2;
|
|
27
|
+
export const BAR_WIDTH = 1;
|
|
28
28
|
// Pi's select list starts every item with a 2-column prefix (`→ ` or spaces); the list is moved left by it,
|
|
29
29
|
// so the item text starts in the same column as the input text
|
|
30
30
|
const SELECT_PREFIX_WIDTH = 2;
|
|
@@ -57,6 +57,12 @@ const BOTTOM_BORDER_MARKER = "\x00prompt-box-bottom-border\x00";
|
|
|
57
57
|
*/
|
|
58
58
|
export type RenderStatus = (barColor: (text: string) => string, bashMode: BashMode) => string;
|
|
59
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Returns the color of Pi's busy indicator when it differs from the accent bar: the routed thinking level
|
|
62
|
+
* color. Returns `undefined` to use the accent bar color.
|
|
63
|
+
*/
|
|
64
|
+
export type ReadBusyColor = () => ((text: string) => string) | undefined;
|
|
65
|
+
|
|
60
66
|
// Pi does not export the StatusIndicator type
|
|
61
67
|
type StatusIndicator = NonNullable<Parameters<CustomEditor["setWorkingStatusIndicator"]>[0]>;
|
|
62
68
|
|
|
@@ -80,7 +86,7 @@ interface BoxMargins {
|
|
|
80
86
|
/**
|
|
81
87
|
* Shared look of the box rows of one render.
|
|
82
88
|
*/
|
|
83
|
-
interface BoxStyle {
|
|
89
|
+
export interface BoxStyle {
|
|
84
90
|
margins: BoxMargins;
|
|
85
91
|
barColor: (text: string) => string;
|
|
86
92
|
colors: BoxColors;
|
|
@@ -92,7 +98,9 @@ interface BoxStyle {
|
|
|
92
98
|
* The base editor renders the text lines; this class replaces its borders and
|
|
93
99
|
* wraps every line in the box background. `renderStatus` is called on every
|
|
94
100
|
* render, so the status line always shows the current state. The accent bar
|
|
95
|
-
* uses `borderColor
|
|
101
|
+
* uses the `borderColor` Pi sets from the thinking level and bash mode.
|
|
102
|
+
* Pi colors its working and compaction indicators with the `borderColor` it reads back, so reading it
|
|
103
|
+
* returns `readBusyColor()` when set, except in bash mode; the bar keeps the color Pi set.
|
|
96
104
|
* With `embedWorkingStatus`, Pi passes its busy indicators (working, retry,
|
|
97
105
|
* compaction, branch summary) to the editor, which shows them in the status line.
|
|
98
106
|
* `readBoxColors` is called on every render, so the box follows the terminal background.
|
|
@@ -109,6 +117,9 @@ export class PromptBoxEditor extends CustomEditor {
|
|
|
109
117
|
private readonly editorTheme: EditorTheme;
|
|
110
118
|
private readonly readBoxColors: ReadBoxColors;
|
|
111
119
|
private readonly readRightPadding: ReadRightPadding;
|
|
120
|
+
private readonly readBusyColor: ReadBusyColor;
|
|
121
|
+
// The borderColor Pi set; the `borderColor` property itself returns the busy indicator color
|
|
122
|
+
private barColor: (text: string) => string;
|
|
112
123
|
private statusIndicator: StatusIndicator | undefined;
|
|
113
124
|
// Pi's editor paddingX setting; the base editor keeps 0, so its text indent inside the box stays the same
|
|
114
125
|
private boxPaddingX = 0;
|
|
@@ -127,12 +138,25 @@ export class PromptBoxEditor extends CustomEditor {
|
|
|
127
138
|
renderStatus: RenderStatus,
|
|
128
139
|
readBoxColors: ReadBoxColors,
|
|
129
140
|
readRightPadding: ReadRightPadding = () => 0,
|
|
141
|
+
readBusyColor: ReadBusyColor = () => undefined,
|
|
130
142
|
) {
|
|
131
143
|
super(tui, theme, keybindings, { paddingX: 0, embedWorkingStatus: true });
|
|
132
144
|
this.renderStatus = renderStatus;
|
|
133
145
|
this.editorTheme = theme;
|
|
134
146
|
this.readBoxColors = readBoxColors;
|
|
135
147
|
this.readRightPadding = readRightPadding;
|
|
148
|
+
this.readBusyColor = readBusyColor;
|
|
149
|
+
|
|
150
|
+
// The base class declares borderColor as a field, so the accessor is defined on the instance
|
|
151
|
+
this.barColor = this.borderColor;
|
|
152
|
+
Object.defineProperty(this, "borderColor", {
|
|
153
|
+
configurable: true,
|
|
154
|
+
enumerable: true,
|
|
155
|
+
get: () => this.readIndicatorColor(),
|
|
156
|
+
set: (color: (text: string) => string) => {
|
|
157
|
+
this.barColor = color;
|
|
158
|
+
},
|
|
159
|
+
});
|
|
136
160
|
}
|
|
137
161
|
|
|
138
162
|
// The base class keeps the indicator private, so keep a copy for the status line
|
|
@@ -191,7 +215,7 @@ export class PromptBoxEditor extends CustomEditor {
|
|
|
191
215
|
this.boxLayout = { leftMargin: margins.left, textWidth, textLineCount: textLines.length };
|
|
192
216
|
|
|
193
217
|
const boxWidth = width - margins.left - margins.right - BAR_WIDTH;
|
|
194
|
-
const barColor = this.
|
|
218
|
+
const barColor = this.barColor;
|
|
195
219
|
const colors = this.readBoxColors();
|
|
196
220
|
const status = this.renderStatusLine(textWidth, barColor);
|
|
197
221
|
|
|
@@ -236,6 +260,17 @@ export class PromptBoxEditor extends CustomEditor {
|
|
|
236
260
|
return super.handleMouse({ ...baseEvent, x: event.x - textIndent, y });
|
|
237
261
|
}
|
|
238
262
|
|
|
263
|
+
/**
|
|
264
|
+
* Returns the color Pi's busy indicator gets when it reads `borderColor`. In bash mode, and for the base
|
|
265
|
+
* editor borders of the fallback layout, it is the accent bar color.
|
|
266
|
+
*/
|
|
267
|
+
private readIndicatorColor(): (text: string) => string {
|
|
268
|
+
if (this.renderingFallback || detectBashMode(this.getText()) !== "off")
|
|
269
|
+
return this.barColor;
|
|
270
|
+
|
|
271
|
+
return this.readBusyColor() ?? this.barColor;
|
|
272
|
+
}
|
|
273
|
+
|
|
239
274
|
/**
|
|
240
275
|
* Renders the base editor layout with Pi's borders, for widths below the box minimum.
|
|
241
276
|
*/
|
|
@@ -343,7 +378,7 @@ function renderHalfRow(barChar: string, fillChar: string, boxWidth: number, styl
|
|
|
343
378
|
* Renders one full-height box row: accent bar, padding and content on the box background.
|
|
344
379
|
* Content wider than `textWidth` extends into the right inner padding; only scroll indicators do that.
|
|
345
380
|
*/
|
|
346
|
-
function renderBoxRow(content: string, textWidth: number, style: BoxStyle): string {
|
|
381
|
+
export function renderBoxRow(content: string, textWidth: number, style: BoxStyle): string {
|
|
347
382
|
const { margins, barColor, colors } = style;
|
|
348
383
|
const leftMargin = " ".repeat(margins.left);
|
|
349
384
|
const rightMargin = " ".repeat(margins.right);
|
package/src/ui/status-line.ts
CHANGED
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
|
|
6
6
|
|
|
7
|
+
import type { RoutedModel } from "../routed-model.ts";
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
10
|
* Bash mode of the input:
|
|
9
11
|
* off a normal prompt
|
|
@@ -24,25 +26,64 @@ export interface StatusLineOptions {
|
|
|
24
26
|
/** Returns the display name of a provider id, like Pi's model registry: `opencode` → `OpenCode Zen`. */
|
|
25
27
|
getProviderName: (provider: string) => string;
|
|
26
28
|
thinkingLevel: string;
|
|
29
|
+
/** Model a virtual `model` routed the latest response to; shown after `→`. */
|
|
30
|
+
routedModel?: RoutedModel;
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
/**
|
|
30
|
-
*
|
|
34
|
+
* Returns the theme color of the routed thinking level, or `undefined` when the status line shows no routed
|
|
35
|
+
* level: without a routed model, without its level, or for a routed model without reasoning support.
|
|
36
|
+
*/
|
|
37
|
+
export function readRoutedLevelColor(
|
|
38
|
+
theme: Theme,
|
|
39
|
+
routedModel: RoutedModel | undefined,
|
|
40
|
+
): ((text: string) => string) | undefined {
|
|
41
|
+
if (!routedModel?.model.reasoning || !routedModel.thinkingLevel)
|
|
42
|
+
return undefined;
|
|
43
|
+
|
|
44
|
+
return theme.getThinkingBorderColor(routedModel.thinkingLevel);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Builds the status line text: model, provider and thinking level, then the routed model in the same format.
|
|
31
49
|
* Like Pi's footer, the level shows only for models with reasoning support, and level `off` shows as `thinking off`.
|
|
32
50
|
* In bash mode, `Bash` replaces them. Pi's busy indicator is added by the editor.
|
|
33
51
|
*/
|
|
34
52
|
export function formatStatus(options: StatusLineOptions): string {
|
|
35
|
-
const { theme, levelColor, bashMode, model,
|
|
53
|
+
const { theme, levelColor, bashMode, model, thinkingLevel, routedModel } = options;
|
|
36
54
|
if (bashMode === "context")
|
|
37
55
|
return levelColor("Bash");
|
|
38
56
|
|
|
39
57
|
if (bashMode === "no-context")
|
|
40
58
|
return `${levelColor("Bash")}${theme.fg("dim", " (no context)")}`;
|
|
41
59
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
60
|
+
if (!model)
|
|
61
|
+
return theme.fg("dim", "no model");
|
|
62
|
+
|
|
63
|
+
const selectedText = formatModel(options, model, thinkingLevel, levelColor);
|
|
64
|
+
if (!routedModel)
|
|
65
|
+
return selectedText;
|
|
66
|
+
|
|
67
|
+
// The routed level has the color of its own level, which can differ from the accent bar
|
|
68
|
+
const routedLevelColor = readRoutedLevelColor(theme, routedModel) ?? levelColor;
|
|
69
|
+
const routedText = formatModel(options, routedModel.model, routedModel.thinkingLevel, routedLevelColor);
|
|
70
|
+
|
|
71
|
+
return `${selectedText}${theme.fg("dim", " → ")}${routedText}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Formats one model as `name provider · level`; the level is left out for models without reasoning support
|
|
76
|
+
* and when it is unknown.
|
|
77
|
+
*/
|
|
78
|
+
function formatModel(
|
|
79
|
+
options: StatusLineOptions,
|
|
80
|
+
model: NonNullable<ExtensionContext["model"]>,
|
|
81
|
+
thinkingLevel: string | undefined,
|
|
82
|
+
levelColor: (text: string) => string,
|
|
83
|
+
): string {
|
|
84
|
+
const { theme, getProviderName } = options;
|
|
85
|
+
const modelText = `${theme.fg("text", model.name)} ${theme.fg("dim", getProviderName(model.provider))}`;
|
|
86
|
+
if (!model.reasoning || !thinkingLevel)
|
|
46
87
|
return modelText;
|
|
47
88
|
|
|
48
89
|
const separator = theme.fg("dim", " · ");
|