pi-bro 0.35.1 → 0.36.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 +9 -0
- package/README.md +3 -0
- package/docs/README.md +1 -0
- package/docs/features/copy.md +37 -0
- package/docs/reference/commands.md +1 -0
- package/docs/reference/data-and-permissions.md +2 -0
- package/package.json +5 -2
- package/src/bro.ts +10 -0
- package/src/copy.ts +230 -0
- package/src/explain-modal.ts +5 -16
- package/src/last-assistant.ts +23 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to pi-bro are documented here.
|
|
4
4
|
|
|
5
|
+
## [0.36.0] - 2026-11-09
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- `/bro copy` opens the last completed assistant reply as raw editable Markdown, with Ctrl+D Preview and Ctrl+C clipboard copy, without model calls or saved drafts.
|
|
10
|
+
- Unedited copies preserve source text exactly. Edited copies use Pi Editor's text, with notice for normalized tabs and CRLF.
|
|
11
|
+
- Copy and Simplify now share the same completed-reply extractor, keeping Simplify's trim.
|
|
12
|
+
- Copy source, editor, clipboard, and modal regression tests; live Pi terminal/clipboard qualification remains open.
|
|
13
|
+
|
|
5
14
|
## [0.35.1] - 2026-11-09
|
|
6
15
|
|
|
7
16
|
### Highlights
|
package/README.md
CHANGED
|
@@ -47,6 +47,7 @@ For a first success, type this in Pi after setting up a backend:
|
|
|
47
47
|
| After that | What happens |
|
|
48
48
|
| --- | --- |
|
|
49
49
|
| `/bro` | Explains the agent's latest completed reply (requires one) |
|
|
50
|
+
| `/bro copy` | Edit and preview the raw Markdown of the last assistant reply before copying |
|
|
50
51
|
| `/bro btw What is a race condition?` | Asks a private side question |
|
|
51
52
|
| `/bro show` | Draws the current session's recent conversation, if any |
|
|
52
53
|
| `/bro architecture` | Creates or refreshes `docs/architecture/` for this project |
|
|
@@ -60,6 +61,7 @@ File and webpage examples need your own real path or URL; see [how to explain th
|
|
|
60
61
|
|
|
61
62
|
| Command | What you get |
|
|
62
63
|
| --- | --- |
|
|
64
|
+
| `/bro copy` | **[Copy Markdown](docs/features/copy.md).** Edit, preview and copy the last completed reply as Markdown, without a model call or saved file. |
|
|
63
65
|
| `/bro`, `/bro text`, `/bro file`, `/bro url` | **[Explain](docs/features/explain.md).** A plain-language version of the agent's last reply, pasted text, a PDF/DOCX/Markdown file, or a public webpage. Three modes: `brief`, `balanced`, `faithful`. |
|
|
64
66
|
| `/bro show` | **[Show](docs/features/show.md#bro-show).** The last few turns drawn as a shape (call tree, file tree, diff, pseudocode, a sequence diagram, or an HTML layout) instead of prose. |
|
|
65
67
|
| `/bro architecture [focus]` | **[Architecture Docs](docs/features/architecture.md).** Inspects the project and writes Markdown, diagrams and an offline interactive `architecture.html` under `docs/architecture/`. Uses BTW model settings. |
|
|
@@ -106,6 +108,7 @@ which may keep it under their own policies. Paths below are under
|
|
|
106
108
|
|
|
107
109
|
| Feature | What is sent | What Bro keeps on disk |
|
|
108
110
|
| --- | --- | --- |
|
|
111
|
+
| Copy Markdown | Nothing; it reads the last completed assistant reply already in Pi's session | Nothing; only a temporary draft while the modal is open |
|
|
109
112
|
| Explain | The agent's last reply, your pasted text, or text extracted from your file or webpage, plus your preferences | Last result as a private Pi session entry |
|
|
110
113
|
| Architecture Docs | Repository source, existing architecture docs, focus and preferences; only selected backend can see snapshot files | Generated Markdown, JSON graph and offline HTML in `docs/architecture/`; temporary inspection copy is removed |
|
|
111
114
|
| Show | User and assistant text from recent turns (no tool calls, tool results, reasoning or images), plus your preferences | Last result in the private Pi session, plus temporary diagram pages in `pi-bro-<uid>/` |
|
package/docs/README.md
CHANGED
|
@@ -10,6 +10,7 @@ Start with what you want to do. The [README](../README.md) explains the product,
|
|
|
10
10
|
|
|
11
11
|
## I want to do something
|
|
12
12
|
|
|
13
|
+
- [Edit, preview and copy Markdown](features/copy.md) — a model-free scratch editor for the last reply.
|
|
13
14
|
- [Explain an agent answer or text](features/explain.md#explanation-modes) — plain-language explanations.
|
|
14
15
|
- [Explain a local file](features/explain.md#explain-a-document) or [webpage](features/explain.md#explain-a-webpage).
|
|
15
16
|
- [Visualize a conversation](features/show.md#bro-show) — Show diagrams.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# Copy Markdown — /bro copy
|
|
2
|
+
|
|
3
|
+
When Pi renders a reply, its Markdown tables and formatted text are easy to read but awkward to trim and paste. `/bro copy` opens the **last completed assistant reply** as raw Markdown in a temporary editor. You can edit the text, preview it, and copy your version without leaving Pi.
|
|
4
|
+
|
|
5
|
+
Pi already has **`/copy`**, which copies the last assistant reply immediately. Use **`/bro copy`** when you want to trim or change it before copying.
|
|
6
|
+
|
|
7
|
+
## Controls
|
|
8
|
+
|
|
9
|
+
| Key | Edit view | Preview view |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| **Ctrl+D** | Show rendered Markdown preview | Return to raw Markdown editor |
|
|
12
|
+
| **Ctrl+C** | Copy the current raw Markdown | Copy the same raw Markdown, never rendered text |
|
|
13
|
+
| **F1** | Show Help without taking Pi's Ctrl+G shortcut | Show Help |
|
|
14
|
+
| **?** | Type `?` normally in the draft | Show Help |
|
|
15
|
+
| **↑/↓, PgUp/PgDn** | Move in the editor | Scroll preview |
|
|
16
|
+
| **Esc** | Close; two presses if you edited the draft | Close; two presses if you edited the draft |
|
|
17
|
+
|
|
18
|
+
In Help, Esc returns to the previous view. **Ctrl+D** switches views but replaces Pi Editor's forward-delete key inside Copy; the Delete key still works. **Enter** adds a newline in Edit; it does not submit a prompt.
|
|
19
|
+
|
|
20
|
+
## What is preserved
|
|
21
|
+
|
|
22
|
+
- The source comes from the last normally completed assistant message in the active Pi session, not from the terminal's rendered output. Bro copies **text content**, including Markdown tables, links and code fences; reasoning and nontext parts are excluded. If one reply contains several text blocks, Bro joins them with **one newline between blocks** (same rule as Simplify); that separator is an intentional representation of the reply, not a byte in either source block.
|
|
23
|
+
- **Unedited:** Bro copies the original Markdown verbatim, including tabs and CRLF line endings. **Edited:** Bro copies Pi Editor's expanded text, which uses spaces for tabs and LF line endings. If the original contains tabs or CRLF, the modal shows a note when edited. Undo, paste, deletion and other Pi Editor actions remain copyable; there is no secondary raw-draft store or blocked-copy recovery.
|
|
24
|
+
- Preview uses the **same draft** as Edit. Switching views does not reset the editor or create another copy.
|
|
25
|
+
- Large pastes: copy always reads `Editor.getExpandedText()` rather than its shortened on-screen paste markers.
|
|
26
|
+
|
|
27
|
+
The modal is temporary. Closing it discards the scratch draft; it does not change Pi's conversation, files, saved settings, or agent context. No model request runs. **Ctrl+C writes only to your system clipboard**; other applications with clipboard access may read it.
|
|
28
|
+
|
|
29
|
+
## When there's no reply or the terminal is too small
|
|
30
|
+
|
|
31
|
+
If Pi has no completed assistant reply, Bro displays a notice instead of opening an empty modal.
|
|
32
|
+
|
|
33
|
+
Pi's built-in `Editor` renders at most **about 30% of the terminal height** on Pi 1.3–1.5; a long draft can scroll inside this smaller region even when the surrounding Bro modal has unused space. No supported editor-height setting exists in those Pi versions. Bro keeps this upstream editor rather than adding a custom one solely to fill the modal.
|
|
34
|
+
|
|
35
|
+
On a very small terminal, the editor refuses blind typing and asks for more space. You can still preview, copy, and close. If clipboard access fails, Bro shows an error rather than claiming a successful copy.
|
|
36
|
+
|
|
37
|
+
This is deliberately not a file editor, message-history selector, or AI rewriting workflow.
|
|
@@ -28,6 +28,7 @@ Typing `/bro ` displays ten task-level suggestions: `simplify`, `show`, `btw`, `
|
|
|
28
28
|
| --- | --- |
|
|
29
29
|
| `/bro` | Open Simplify and explain the latest completed assistant reply. |
|
|
30
30
|
| `/bro simplify` | Reopen the last Simplify screen within this Pi session, without a new model call. |
|
|
31
|
+
| `/bro copy` | Edit and preview the latest completed assistant reply as raw Markdown. Ctrl+D switches views; Ctrl+C copies Markdown; F1 opens Help. No backend call or saved draft. Native Pi `/copy` copies immediately. [Guide](../features/copy.md). |
|
|
31
32
|
| `/bro text <text>` | Explain pasted text. |
|
|
32
33
|
| `/bro <input>` | Explain it directly: a lone URL runs the webpage reader, an existing workspace file with a supported extension runs the document reader, and anything else is pasted text. A quoted path with spaces is routed too when the file exists. |
|
|
33
34
|
| `/bro file <path>` | Explain a workspace-local `.md`, `.markdown`, `.txt`, `.pdf`, or `.docx` file. |
|
|
@@ -94,6 +94,8 @@ See [feature guide](../features/architecture.md).
|
|
|
94
94
|
|
|
95
95
|
## Privacy and safety
|
|
96
96
|
|
|
97
|
+
- **Copy Markdown:** `/bro copy` reads the last completed assistant reply from the current Pi session and lets you edit it in a temporary modal. Preview renders locally. Ctrl+C writes to the system clipboard, which other apps may read. No backend request, saved file, session record, or Pi message changes.
|
|
98
|
+
|
|
97
99
|
- **Overthink requests:** after a substantive Pi turn, Bro makes one additional provider call using Pi's active model and current session conversation projection (including exposed tool evidence), appending the current session's questions and any saved `bro-preferences.md` (for tone and language only). Overthink never executes tool calls. It may still send tool descriptions copied from Pi's existing context to support cache reuse; a tool-use response is rejected without executing the tool. The provider may retain input under its policy. Results and questions are stored in private Pi session custom entries, not LLM turns; global questions use `bro-overthink-defaults.json` in the agent home. This cannot promise a cache hit or fixed incremental cost. Features → Overthink has an on/off switch (on by default). The viewer displays provider-reported input/cache-read/cache-write/output/reasoning tokens and USD cost when available; a reported cache-read value of zero may mean either a cache miss or unsupported caching. See [Overthink](../overthink.md).
|
|
98
100
|
|
|
99
101
|
- **External requests**: Bro sends the latest completed assistant response,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-bro",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
4
|
"description": "A sidekick for you and your Pi agent: plain-language explanations, side questions, diagrams, PR review and second opinions, without cluttering the agent's context.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"btw",
|
|
32
32
|
"side-conversation",
|
|
33
33
|
"overthink",
|
|
34
|
+
"markdown-copy",
|
|
34
35
|
"advisor",
|
|
35
36
|
"second-opinion",
|
|
36
37
|
"codex",
|
|
@@ -55,6 +56,8 @@
|
|
|
55
56
|
"src/overthink.ts",
|
|
56
57
|
"src/overthink-state.ts",
|
|
57
58
|
"src/overthink-questions-ui.ts",
|
|
59
|
+
"src/copy.ts",
|
|
60
|
+
"src/last-assistant.ts",
|
|
58
61
|
"src/remember-store.ts",
|
|
59
62
|
"src/remember-engine.ts",
|
|
60
63
|
"src/remember-tool.ts",
|
|
@@ -105,7 +108,7 @@
|
|
|
105
108
|
"scripts": {
|
|
106
109
|
"docs:check": "node scripts/check-docs.mjs && node --test test/docs-quality.test.mjs",
|
|
107
110
|
"typecheck": "tsc --noEmit",
|
|
108
|
-
"test": "npm run typecheck && npm run docs:check && node .github/scripts/release-utils.mjs selftest && node --experimental-transform-types --test --test-concurrency=2 test/architecture.test.mjs test/architecture-review.test.mjs test/architecture-controller.test.mjs test/elevate.test.ts test/overthink.test.ts test/simplify.test.ts test/simplify-modal.test.mjs test/session-state.test.ts test/session-lifecycle.test.mjs test/host-lifecycle.test.mjs test/grill.test.ts test/config-navigation.test.ts test/config-navigation-modal.test.mjs test/remember.test.ts test/remember-modal.test.mjs test/review-ux.test.mjs test/review-doctor.test.mjs test/review-versions.test.mjs test/review-regeneration.test.mjs test/review-modal.test.mjs test/review.test.ts test/review-session.test.mjs test/helpers.test.mjs test/prompt.test.ts test/backend.test.ts test/claude.test.ts test/grok.test.ts test/codex.test.ts test/muse.test.ts test/settings.test.ts test/sources.test.ts test/release.test.ts test/show-html.test.ts test/smoke-rpc.test.ts test/ui-capabilities.test.ts test/modal-contract.test.mjs benchmark/*.test.ts && sh ./test/smoke-test.sh",
|
|
111
|
+
"test": "npm run typecheck && npm run docs:check && node .github/scripts/release-utils.mjs selftest && node --experimental-transform-types --test --test-concurrency=2 test/architecture.test.mjs test/architecture-review.test.mjs test/architecture-controller.test.mjs test/elevate.test.ts test/overthink.test.ts test/copy-modal.test.mjs test/simplify.test.ts test/simplify-modal.test.mjs test/session-state.test.ts test/session-lifecycle.test.mjs test/host-lifecycle.test.mjs test/grill.test.ts test/config-navigation.test.ts test/config-navigation-modal.test.mjs test/remember.test.ts test/remember-modal.test.mjs test/review-ux.test.mjs test/review-doctor.test.mjs test/review-versions.test.mjs test/review-regeneration.test.mjs test/review-modal.test.mjs test/review.test.ts test/review-session.test.mjs test/helpers.test.mjs test/prompt.test.ts test/backend.test.ts test/claude.test.ts test/grok.test.ts test/codex.test.ts test/muse.test.ts test/settings.test.ts test/sources.test.ts test/release.test.ts test/show-html.test.ts test/smoke-rpc.test.ts test/ui-capabilities.test.ts test/modal-contract.test.mjs benchmark/*.test.ts && sh ./test/smoke-test.sh",
|
|
109
112
|
"benchmark:dry-run": "node benchmark/run.ts dry-run",
|
|
110
113
|
"benchmark:run": "node benchmark/run.ts run",
|
|
111
114
|
"benchmark:report": "node benchmark/run.ts report",
|
package/src/bro.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { setPiHostContext, stopActiveBackendExecutions } from "./backend.ts";
|
|
|
12
12
|
import { openElevateModal } from "./elevate.ts";
|
|
13
13
|
import { openArchitectureDocs } from "./architecture.ts";
|
|
14
14
|
import { openGrillModal } from "./grill.ts";
|
|
15
|
+
import { openCopyModal } from "./copy.ts";
|
|
15
16
|
import { registerOverthink } from "./overthink.ts";
|
|
16
17
|
import { openRememberModal } from "./remember.ts";
|
|
17
18
|
import { openSimplifyWorkflow, type SimplifyState } from "./simplify-ui.ts";
|
|
@@ -73,10 +74,12 @@ export * from "./simplify-ui.ts";
|
|
|
73
74
|
export * from "./overthink-state.ts";
|
|
74
75
|
export * from "./overthink.ts";
|
|
75
76
|
export * from "./session-state.ts";
|
|
77
|
+
export * from "./copy.ts";
|
|
76
78
|
|
|
77
79
|
const COMMANDS = [
|
|
78
80
|
{ value: "architecture", label: "architecture", description: "Document this repository in docs/architecture/ with diagrams and an offline map" },
|
|
79
81
|
{ value: "simplify", label: "simplify", description: "Reopen the last Simplify screen without a new model call" },
|
|
82
|
+
{ value: "copy", label: "copy", description: "Edit, preview and copy the last assistant reply as Markdown — no model call" },
|
|
80
83
|
{ value: "text", label: "text", description: "Explain pasted text, or the latest reply when text is omitted" },
|
|
81
84
|
{ value: "file", label: "file", description: "Explain a local document" },
|
|
82
85
|
{ value: "url", label: "url", description: "Explain a public webpage" },
|
|
@@ -231,6 +234,13 @@ export default async function bro(pi: ExtensionAPI) {
|
|
|
231
234
|
return;
|
|
232
235
|
}
|
|
233
236
|
|
|
237
|
+
if (action === "copy") {
|
|
238
|
+
if (value) { ctx.ui.notify("Use /bro copy without arguments to edit the last reply.", "warning"); return; }
|
|
239
|
+
try { await openCopyModal(ctx); }
|
|
240
|
+
catch (error) { ctx.ui.notify("Bro Copy: " + errorMessage(error), "error"); }
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
234
244
|
if (action === "show") {
|
|
235
245
|
const { requested, steering, invalid } = parseShowArguments(value);
|
|
236
246
|
if (invalid) {
|
package/src/copy.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model-free Markdown scratchpad for the last completed Pi reply.
|
|
3
|
+
* Pi Editor is the only editable draft. Preview renders that draft.
|
|
4
|
+
* Unedited copy preserves the original raw message; edited copy uses Pi's
|
|
5
|
+
* expanded editor text, including its normal tab/line-ending conversion.
|
|
6
|
+
*/
|
|
7
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { copyToClipboard, getMarkdownTheme, getSelectListTheme } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { Editor, Markdown, matchesKey, wrapTextWithAnsi, type Component, type EditorTheme, type TUI } from "@earendil-works/pi-tui";
|
|
10
|
+
import { broOverlayArea, delegateMouse, formatNotice, modalHeight, openBroOverlay, renderFrame, Scroller, type Area, type Theme, type TuiLike } from "./modal-kit.ts";
|
|
11
|
+
import { hasBroCustomUi, type BroMouseEvent, type BroMouseResult } from "./ui-capabilities.ts";
|
|
12
|
+
import { errorMessage } from "./util.ts";
|
|
13
|
+
import { latestCompletedAssistantText } from "./last-assistant.ts";
|
|
14
|
+
|
|
15
|
+
type CopyText = (markdown: string) => Promise<void>;
|
|
16
|
+
type CopyView = "edit" | "preview";
|
|
17
|
+
|
|
18
|
+
export class CopyModal implements Component {
|
|
19
|
+
readonly editor: Editor;
|
|
20
|
+
private readonly preview: Markdown;
|
|
21
|
+
private readonly scroller: Scroller;
|
|
22
|
+
private readonly original: string;
|
|
23
|
+
private readonly initialEditorText: string;
|
|
24
|
+
private readonly tui: TUI;
|
|
25
|
+
private readonly theme: Theme;
|
|
26
|
+
private readonly onClose: () => void;
|
|
27
|
+
private readonly copyText: CopyText;
|
|
28
|
+
|
|
29
|
+
private view: CopyView = "edit";
|
|
30
|
+
private showingHelp = false;
|
|
31
|
+
private discardArmed = false;
|
|
32
|
+
private canEdit = false;
|
|
33
|
+
private closed = false;
|
|
34
|
+
private notice = "";
|
|
35
|
+
private editorArea: Area = { top: 0, left: 0, width: 0, height: 0 };
|
|
36
|
+
private modalArea: Area | undefined;
|
|
37
|
+
|
|
38
|
+
constructor(tui: TUI, theme: Theme, markdown: string, onClose: () => void, copyText: CopyText = copyToClipboard) {
|
|
39
|
+
this.tui = tui;
|
|
40
|
+
this.theme = theme;
|
|
41
|
+
this.original = markdown;
|
|
42
|
+
this.onClose = onClose;
|
|
43
|
+
this.copyText = copyText;
|
|
44
|
+
const editorTheme: EditorTheme = {
|
|
45
|
+
borderColor: (value: string) => theme.fg("border", value),
|
|
46
|
+
selectList: getSelectListTheme(),
|
|
47
|
+
};
|
|
48
|
+
this.editor = new Editor(tui, editorTheme);
|
|
49
|
+
this.editor.focused = true;
|
|
50
|
+
this.editor.setText(markdown);
|
|
51
|
+
this.initialEditorText = this.editor.getExpandedText();
|
|
52
|
+
this.editor.onChange = () => {
|
|
53
|
+
this.discardArmed = false;
|
|
54
|
+
this.notice = "";
|
|
55
|
+
this.tui.requestRender();
|
|
56
|
+
};
|
|
57
|
+
this.preview = new Markdown("", 0, 0, getMarkdownTheme());
|
|
58
|
+
this.scroller = new Scroller(() => this.tui.requestRender());
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private edited(): boolean {
|
|
62
|
+
return this.editor.getExpandedText() !== this.initialEditorText;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** No edit: exact original Markdown. Edited: one authoritative Pi Editor draft. */
|
|
66
|
+
private markdown(): string {
|
|
67
|
+
return this.edited() ? this.editor.getExpandedText() : this.original;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
private normalizationNotice(): string | undefined {
|
|
71
|
+
if (!this.edited()) return undefined;
|
|
72
|
+
const changes = [
|
|
73
|
+
this.original.includes("\t") ? "tabs become spaces" : undefined,
|
|
74
|
+
this.original.includes("\r") ? "CRLF becomes LF" : undefined,
|
|
75
|
+
].filter((item): item is string => Boolean(item));
|
|
76
|
+
return changes.length ? "Edited copy: " + changes.join("; ") + "." : undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private noticeOf(message: string, tone: "info" | "success" | "warning" | "error"): void {
|
|
80
|
+
if (this.closed) return;
|
|
81
|
+
this.notice = formatNotice(this.theme, message, tone);
|
|
82
|
+
this.tui.requestRender();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private copy(): void {
|
|
86
|
+
this.discardArmed = false;
|
|
87
|
+
const markdown = this.markdown();
|
|
88
|
+
// Snapshot before the async clipboard operation; never copy on-screen
|
|
89
|
+
// paste markers or Preview's rendered terminal text.
|
|
90
|
+
void Promise.resolve()
|
|
91
|
+
.then(() => this.copyText(markdown))
|
|
92
|
+
.then(() => this.noticeOf("Copied Markdown.", "success"))
|
|
93
|
+
.catch(error => this.noticeOf("Copy failed: " + errorMessage(error), "error"));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private toggleView(): void {
|
|
97
|
+
if (this.closed) return;
|
|
98
|
+
this.showingHelp = false;
|
|
99
|
+
this.discardArmed = false;
|
|
100
|
+
this.notice = "";
|
|
101
|
+
this.view = this.view === "edit" ? "preview" : "edit";
|
|
102
|
+
if (this.view === "preview") {
|
|
103
|
+
this.preview.setText(this.markdown());
|
|
104
|
+
this.scroller.toStart();
|
|
105
|
+
}
|
|
106
|
+
this.tui.requestRender();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
render(width: number): string[] {
|
|
110
|
+
const height = modalHeight(this.tui);
|
|
111
|
+
const innerWidth = Math.max(1, width - 2);
|
|
112
|
+
this.modalArea = broOverlayArea(this.tui as TuiLike, width, height);
|
|
113
|
+
this.canEdit = false;
|
|
114
|
+
this.editorArea = { top: 0, left: 0, width: 0, height: 0 };
|
|
115
|
+
|
|
116
|
+
const hint = this.showingHelp
|
|
117
|
+
? "Esc back · F1 back · Ctrl+C copy"
|
|
118
|
+
: this.discardArmed
|
|
119
|
+
? "Esc again discard · Ctrl+C copy · Ctrl+D preview/edit"
|
|
120
|
+
: this.view === "edit"
|
|
121
|
+
? "Ctrl+D preview · Ctrl+C copy · F1 help · Esc close"
|
|
122
|
+
: "↑/↓ scroll · Ctrl+D edit · Ctrl+C copy · ? / F1 help · Esc close";
|
|
123
|
+
const editorLines = !this.showingHelp && this.view === "edit" ? this.editor.render(innerWidth) : [];
|
|
124
|
+
const help = [
|
|
125
|
+
"Edit the last completed assistant reply as raw Markdown.",
|
|
126
|
+
"The original Pi conversation is never changed.",
|
|
127
|
+
"",
|
|
128
|
+
"Ctrl+D Switch between Edit and Markdown Preview (replaces forward-delete in Copy)",
|
|
129
|
+
"Ctrl+C Copy full raw Markdown; never rendered terminal text",
|
|
130
|
+
"F1 Help in both views",
|
|
131
|
+
"? Help in Preview; ordinary text in Edit",
|
|
132
|
+
"Esc Back from Help; close (confirm if edited)",
|
|
133
|
+
"",
|
|
134
|
+
"Unedited copy preserves the original; edits follow Pi Editor's tab/CRLF rules.",
|
|
135
|
+
"No model calls, saved drafts, or workspace changes.",
|
|
136
|
+
];
|
|
137
|
+
const normalizationNotice = this.normalizationNotice();
|
|
138
|
+
|
|
139
|
+
return renderFrame(this.theme, width, height, {
|
|
140
|
+
header: () => this.theme.fg("accent", this.theme.bold("Bro · Copy · " +
|
|
141
|
+
(this.showingHelp ? "Help" : this.view === "edit" ? "Edit" : "Preview"))) +
|
|
142
|
+
(this.edited() ? this.theme.fg("dim", " · edited") : "") +
|
|
143
|
+
(!this.showingHelp && this.view === "preview" && this.scroller.indicator()
|
|
144
|
+
? this.theme.fg("dim", " · " + this.scroller.indicator()) : ""),
|
|
145
|
+
body: (rows, inner) => {
|
|
146
|
+
if (this.showingHelp) return this.scroller.view(help.flatMap(line => wrapTextWithAnsi(line, inner)), rows);
|
|
147
|
+
if (this.view === "preview") return this.scroller.view(this.preview.render(inner), rows);
|
|
148
|
+
if (rows < editorLines.length) {
|
|
149
|
+
return wrapTextWithAnsi("Enlarge the terminal to edit. Ctrl+D previews; Ctrl+C still copies the draft.", inner).slice(0, rows);
|
|
150
|
+
}
|
|
151
|
+
this.canEdit = true;
|
|
152
|
+
this.editorArea = { top: 3, left: 1, width: inner, height: editorLines.length };
|
|
153
|
+
return editorLines;
|
|
154
|
+
},
|
|
155
|
+
notice: this.notice || (normalizationNotice ? formatNotice(this.theme, normalizationNotice, "warning") : undefined),
|
|
156
|
+
hints: hint,
|
|
157
|
+
minBody: this.showingHelp || this.view === "preview" ? 1 : editorLines.length,
|
|
158
|
+
fill: true,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
invalidate(): void {
|
|
163
|
+
this.editor.invalidate();
|
|
164
|
+
this.preview.invalidate();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
handleMouse(event: BroMouseEvent): BroMouseResult {
|
|
168
|
+
if (this.showingHelp || this.view === "preview") return this.scroller.handleMouse(event);
|
|
169
|
+
return delegateMouse(this.editor, event, this.editorArea);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
handleInput(data: string): void {
|
|
173
|
+
if (this.closed) return;
|
|
174
|
+
if (matchesKey(data, "ctrl+c")) { this.copy(); return; }
|
|
175
|
+
if (matchesKey(data, "escape")) {
|
|
176
|
+
if (this.showingHelp) {
|
|
177
|
+
this.showingHelp = false;
|
|
178
|
+
this.scroller.toStart();
|
|
179
|
+
this.tui.requestRender();
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (this.edited() && !this.discardArmed) {
|
|
183
|
+
this.discardArmed = true;
|
|
184
|
+
this.noticeOf("Edits will be discarded. Esc again to close, or Ctrl+C to copy.", "warning");
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
this.closed = true;
|
|
188
|
+
this.onClose();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (matchesKey(data, "ctrl+d")) { this.toggleView(); return; }
|
|
192
|
+
if (matchesKey(data, "f1") || (this.view === "preview" && matchesKey(data, "?"))) {
|
|
193
|
+
this.showingHelp = !this.showingHelp;
|
|
194
|
+
this.scroller.toStart();
|
|
195
|
+
this.notice = "";
|
|
196
|
+
this.discardArmed = false;
|
|
197
|
+
this.tui.requestRender();
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
if (this.showingHelp || this.view === "preview") {
|
|
201
|
+
if (!this.scroller.handleKey(data, this.modalArea)) this.tui.requestRender();
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (!this.canEdit) return; // Never change text in an invisible editor.
|
|
205
|
+
this.discardArmed = false;
|
|
206
|
+
this.notice = "";
|
|
207
|
+
if (matchesKey(data, "enter")) this.editor.insertTextAtCursor("\n");
|
|
208
|
+
else this.editor.handleInput(data);
|
|
209
|
+
this.tui.requestRender();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
dispose(): void { this.closed = true; }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export async function openCopyModal(ctx: ExtensionCommandContext): Promise<void> {
|
|
216
|
+
if (!hasBroCustomUi(ctx)) {
|
|
217
|
+
ctx.ui.notify("Use /bro copy in Pi's interactive terminal.", "warning");
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const markdown = latestCompletedAssistantText(ctx.sessionManager.getBranch());
|
|
221
|
+
if (!markdown) {
|
|
222
|
+
ctx.ui.notify("No completed assistant reply to copy yet.", "warning");
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
await openBroOverlay<void>(
|
|
226
|
+
ctx,
|
|
227
|
+
(tui, theme, _keys, done) => new CopyModal(tui, theme, markdown, () => done()),
|
|
228
|
+
{ mouse: true },
|
|
229
|
+
);
|
|
230
|
+
}
|
package/src/explain-modal.ts
CHANGED
|
@@ -39,6 +39,7 @@ import type { BroSource } from "./sources.ts";
|
|
|
39
39
|
import { BRO_OVERLAY, broOverlayArea, delegateMouse, formatNotice, modalHeight, openBroOverlay, renderFrame, Scroller, type Area, type NoticeTone, type Theme, type TuiLike } from "./modal-kit.ts";
|
|
40
40
|
import { hasBroCustomUi, type BroMouseEvent, type BroMouseResult } from "./ui-capabilities.ts";
|
|
41
41
|
import { errorMessage } from "./util.ts";
|
|
42
|
+
import { latestCompletedAssistantText } from "./last-assistant.ts";
|
|
42
43
|
|
|
43
44
|
const LOADING_TEXT = "Simplifying for my bro…";
|
|
44
45
|
|
|
@@ -49,22 +50,8 @@ export type BroResult = { source: BroSource; text: string; model?: string; mode?
|
|
|
49
50
|
export type ModalResult = { source?: BroSource; text: string; htmlPath?: string; model?: string; mode?: BroMode; preferences?: boolean };
|
|
50
51
|
|
|
51
52
|
export function latestAssistant(ctx: ExtensionCommandContext): BroSource | undefined {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
for (let i = branch.length - 1; i >= 0; i--) {
|
|
55
|
-
const entry = branch[i];
|
|
56
|
-
if (entry.type !== "message" || entry.message.role !== "assistant" || entry.message.stopReason !== "stop") {
|
|
57
|
-
continue;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const text = entry.message.content
|
|
61
|
-
.filter((part): part is { type: "text"; text: string } => part.type === "text")
|
|
62
|
-
.map((part) => part.text)
|
|
63
|
-
.join("\n")
|
|
64
|
-
.trim();
|
|
65
|
-
|
|
66
|
-
if (text) return { text };
|
|
67
|
-
}
|
|
53
|
+
const text = latestCompletedAssistantText(ctx.sessionManager.getBranch())?.trim();
|
|
54
|
+
return text ? { text } : undefined;
|
|
68
55
|
}
|
|
69
56
|
|
|
70
57
|
export async function simplify(
|
|
@@ -403,6 +390,8 @@ These are needs, not modes. Copied text does not enter Pi until you paste and su
|
|
|
403
390
|
- Inside Simplify: \`/simplify\`, \`/text <text>\`, \`/file <path>\`, \`/url <url>\`, \`/copy\`, \`/clear\`, \`/help\`. **M** changes the mode and re-explains; **R** retries. One source at a time, no chat thread.\n- \`/bro open\` — legacy last-result view (retained for compatibility)
|
|
404
391
|
- \`/bro show [n-turns] [query]\` — draw recent turns' conversation text as shapes; an optional query steers the focus
|
|
405
392
|
|
|
393
|
+
- \`/bro copy\` — edit and preview the last reply before copying raw Markdown.
|
|
394
|
+
|
|
406
395
|
## Architecture documentation
|
|
407
396
|
|
|
408
397
|
- \`/bro architecture [focus]\` — create source-backed docs and an offline map in \`docs/architecture/\` using the BTW model. See docs/features/architecture.md.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One authoritative selection rule for the last completed assistant reply.
|
|
3
|
+
* Simplify intentionally trims its result; Copy keeps the untrimmed text.
|
|
4
|
+
*/
|
|
5
|
+
import type { SessionEntry } from "@earendil-works/pi-coding-agent";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Multiple text content blocks are flattened with one LF, matching Bro's
|
|
9
|
+
* existing Simplify convention (the LF is a separator, not an original block).
|
|
10
|
+
* Ignore reasoning, images, unfinished replies and non-text messages.
|
|
11
|
+
*/
|
|
12
|
+
export function latestCompletedAssistantText(branch: readonly SessionEntry[]): string | undefined {
|
|
13
|
+
for (let i = branch.length - 1; i >= 0; i--) {
|
|
14
|
+
const entry = branch[i]!;
|
|
15
|
+
if (entry.type !== "message" || entry.message.role !== "assistant" || entry.message.stopReason !== "stop") continue;
|
|
16
|
+
const text = entry.message.content
|
|
17
|
+
.filter((part): part is { type: "text"; text: string } => part.type === "text")
|
|
18
|
+
.map(part => part.text)
|
|
19
|
+
.join("\n");
|
|
20
|
+
if (text.trim()) return text;
|
|
21
|
+
}
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|