pi-bro 0.37.0 → 0.38.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.
Files changed (43) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +18 -22
  3. package/docs/README.md +7 -7
  4. package/docs/elevate.md +10 -93
  5. package/docs/examples.md +1 -1
  6. package/docs/feature-map.md +33 -0
  7. package/docs/features/architecture.md +1 -1
  8. package/docs/features/elevate.md +122 -0
  9. package/docs/features/explain.md +16 -20
  10. package/docs/features/grill-me.md +65 -0
  11. package/docs/features/guided-review.md +150 -0
  12. package/docs/features/overthink.md +66 -0
  13. package/docs/features/remember-me.md +92 -0
  14. package/docs/features/show.md +9 -15
  15. package/docs/getting-started.md +5 -5
  16. package/docs/grill-me.md +5 -51
  17. package/docs/guided-review.md +14 -113
  18. package/docs/overthink.md +7 -46
  19. package/docs/piG-agent-shell-qualification.md +3 -3
  20. package/docs/pig-compatibility.md +1 -1
  21. package/docs/reference/commands.md +18 -33
  22. package/docs/reference/data-and-permissions.md +9 -9
  23. package/docs/reference/settings.md +8 -8
  24. package/docs/reference/troubleshooting.md +3 -3
  25. package/docs/remember-me.md +6 -75
  26. package/docs/user-guide.md +4 -4
  27. package/package.json +6 -4
  28. package/src/advisor.ts +1 -1
  29. package/src/architecture.ts +1 -1
  30. package/src/backend.ts +1 -0
  31. package/src/bro.ts +47 -189
  32. package/src/btw.ts +10 -199
  33. package/src/composer-modal.ts +197 -0
  34. package/src/explain-modal.ts +28 -79
  35. package/src/grill.ts +3 -2
  36. package/src/overthink-state.ts +3 -3
  37. package/src/overthink.ts +2 -2
  38. package/src/remember.ts +3 -2
  39. package/src/session-state.ts +46 -28
  40. package/src/settings.ts +11 -3
  41. package/src/simplify-engine.ts +50 -24
  42. package/src/simplify-ui.ts +165 -126
  43. package/src/sources.ts +3 -3
@@ -1,180 +1,219 @@
1
- /** Source-focused Simplify modal. No chat thread, durable draft or new backend boundary. */
2
1
  import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
3
2
  import { stripVTControlCharacters } from "node:util";
4
3
  import { copyToClipboard } from "@earendil-works/pi-coding-agent";
5
4
  import { matchesKey } from "@earendil-works/pi-tui";
6
- import { BtwModal } from "./btw.ts";
5
+ import { ComposerModal } from "./composer-modal.ts";
7
6
  import { latestAssistant, simplify, type BroResult } from "./explain-modal.ts";
8
7
  import { openBroOverlay, startProgressClock } from "./modal-kit.ts";
9
8
  import { nextBroMode, type BroMode } from "./prompt.ts";
10
9
  import { readSettings } from "./settings.ts";
11
10
  import { extractDocumentText, extractWebPage, type BroSource } from "./sources.ts";
11
+ import { captureShowTranscript, openShowHtml, runShowExplanation, showDiagramPath, stripShowDiagramFence } from "./show.ts";
12
12
  import { hasBroCustomUi } from "./ui-capabilities.ts";
13
13
  import { errorMessage, unquote } from "./util.ts";
14
- import { parseSimplifyAction, SIMPLIFY_MODAL_HELP, type SimplifyAction } from "./simplify-engine.ts";
14
+ import { parseSimplifyAction, SIMPLIFY_MODAL_HELP, type SimplifyRequest } from "./simplify-engine.ts";
15
15
 
16
+ // A blank steering string still marks a Show result. Only captured input and text are persisted.
17
+ export type SimplifyResult = BroResult & { showSteering?: string };
16
18
  export type SimplifyState = {
17
- result?: BroResult;
19
+ result?: SimplifyResult;
18
20
  view?: "result" | "help" | "empty";
19
21
  draft?: string;
20
22
  error?: string;
21
23
  };
22
- export type SimplifyStart =
23
- | { kind:"resume" }
24
- | { kind:"latest" }
25
- | { kind:"text"|"file"|"url"; input:string };
26
24
 
27
- async function resolveSource(ctx: ExtensionCommandContext, request: SimplifyStart | SimplifyAction,
28
- signal: AbortSignal, retained?: BroSource): Promise<BroSource> {
29
- if (retained) return retained;
25
+ type Attempt = SimplifyRequest | { kind: "retry"; result: SimplifyResult; mode?: BroMode };
26
+
27
+ async function resolveSource(ctx: ExtensionCommandContext, request: Exclude<SimplifyRequest, { kind: "show" }>, signal: AbortSignal): Promise<BroSource> {
30
28
  switch (request.kind) {
31
29
  case "latest": {
32
30
  await ctx.waitForIdle();
33
- if (signal.aborted) throw new Error("Cancelled.");
34
- const source=latestAssistant(ctx);
31
+ signal.throwIfAborted();
32
+ const source = latestAssistant(ctx);
35
33
  if (!source) throw new Error("No completed assistant reply to simplify.");
36
- return {text:source.text,label:"Latest assistant reply"};
34
+ return { text: source.text, label: "Latest assistant reply" };
37
35
  }
38
- case "text": return {text:request.input,label:"Pasted text"};
39
- case "file": return {text:await extractDocumentText(request.input,ctx.cwd,signal),label:unquote(request.input)};
40
- case "url": return await extractWebPage(request.input,signal);
41
- default: throw new Error("Choose a source with /simplify, /text, /file or /url.");
36
+ case "text": return { text: request.input, label: "Pasted text" };
37
+ case "file": return { text: await extractDocumentText(request.input, ctx.cwd, signal), label: unquote(request.input) };
38
+ case "url": return await extractWebPage(request.input, signal);
39
+ }
40
+ }
41
+
42
+ async function runSimplify(
43
+ ctx: ExtensionCommandContext, request: Attempt, signal: AbortSignal,
44
+ onWaiting?: (show: boolean) => void, onText?: (text: string) => void,
45
+ ): Promise<SimplifyResult> {
46
+ const saved = request.kind === "retry" ? request.result : undefined;
47
+ const steering = request.kind === "show" ? request.steering : saved?.showSteering;
48
+ const source = saved?.source ?? (request.kind === "show" || request.kind === "retry" ? undefined : await resolveSource(ctx, request, signal));
49
+ signal.throwIfAborted();
50
+ const settings = await readSettings();
51
+ signal.throwIfAborted();
52
+ if (steering !== undefined) {
53
+ const captured = source ?? captureShowTranscript(ctx, (request.kind === "show" ? request.turns : undefined) ?? settings.showTurns);
54
+ if (!captured?.text) throw new Error("Nothing to show yet. Start a conversation, then run /show again.");
55
+ onWaiting?.(true);
56
+ return { source: captured, showSteering: steering, ...await runShowExplanation(captured.text, steering, signal, settings, onText) };
42
57
  }
58
+ if (!source) throw new Error("Choose a source first.");
59
+ onWaiting?.(false);
60
+ return { source, ...await simplify(source.text, signal, settings, onText, request.kind === "retry" ? request.mode : undefined) };
43
61
  }
44
62
 
45
- /** The state belongs to the parent /bro extension instance and resets on session_start. */
46
- export async function openSimplifyWorkflow(
47
- ctx: ExtensionCommandContext,
48
- state: SimplifyState,
49
- initial: SimplifyStart,
50
- ): Promise<void> {
63
+ function showResult(state: SimplifyState, result: SimplifyResult): void {
64
+ state.result = result;
65
+ state.view = "result";
66
+ state.error = undefined;
67
+ }
68
+
69
+ /** Explicit sources can run headless; a viewer open never makes a model call. */
70
+ export async function openSimplifyWorkflow(ctx: ExtensionCommandContext, state: SimplifyState, initial?: SimplifyRequest, submitted?: string): Promise<void> {
51
71
  if (!hasBroCustomUi(ctx)) {
52
- if (initial.kind==="resume") { ctx.ui.notify("Simplify's interactive view requires Pi TUI.", "warning"); return; }
53
- const controller=new AbortController();
54
- const source=await resolveSource(ctx,initial,controller.signal);
55
- const result=await simplify(source.text,controller.signal,await readSettings());
56
- state.result={source,...result};
57
- state.view="result"; state.error=undefined;
72
+ if (!initial) {
73
+ ctx.ui.notify("Simplify's interactive view requires Pi TUI. Use /bro simplify latest, text, file, url, or show to run without it.", "warning");
74
+ return;
75
+ }
76
+ showResult(state, await runSimplify(ctx, initial, new AbortController().signal));
58
77
  return;
59
78
  }
60
79
 
61
- if (initial.kind === "resume") {
62
- state.view = state.result ? "result" : "help";
63
- if (!state.draft?.trim()) state.error = undefined;
64
- }
80
+ state.view = state.result ? "result" : "help";
81
+ if (!state.draft?.trim()) state.error = undefined;
82
+ // Rebuild from the saved answer, not a stale path from a previous process.
83
+ const prepareDiagram = (result: SimplifyResult | undefined) => result?.showSteering === undefined
84
+ ? undefined
85
+ : showDiagramPath(result.text, message => ctx.ui.notify(message, "warning"));
86
+ const restoredDiagram = prepareDiagram(state.result);
87
+ let htmlPath = restoredDiagram ? await restoredDiagram : undefined;
65
88
 
66
- await openBroOverlay<void>(ctx,(tui,theme,_keys,done)=>{
67
- let closed=false;
68
- let controller:AbortController|undefined;
69
- let stopProgress:(()=>void)|undefined;
70
- let running=false;
71
- const syncDraft=()=>{const draft=modal.getComposerText();state.draft=draft;if(!draft.trim())state.error=undefined;};
72
- const close=()=>{if(closed)return; closed=true;syncDraft();stopProgress?.();controller?.abort();done(undefined);};
73
- const modal=new BtwModal(tui,theme,close,submit,()=>{
74
- if(!closed){closed=true;syncDraft();stopProgress?.();controller?.abort();}
75
- },{
76
- title:"Bro · simplify",
77
- commands:"",
78
- inputLabel:"Simplify command · /simplify runs again on the latest reply",
79
- idleHint:"Enter run · M change mode · R retry · /copy · /help · Esc back from help / close",
89
+ await openBroOverlay<void>(ctx, (tui, theme, _keys, done) => {
90
+ let closed = false;
91
+ let controller: AbortController | undefined;
92
+ let stopProgress: (() => void) | undefined;
93
+ const dispose = () => {
94
+ if (closed) return;
95
+ closed = true;
96
+ state.draft = modal.getComposerText();
97
+ if (!state.draft.trim()) state.error = undefined;
98
+ stopProgress?.();
99
+ controller?.abort();
100
+ };
101
+ const readingResult = () => state.view === "result" ? state.result : undefined;
102
+ const idleHint = () => {
103
+ const result = readingResult();
104
+ return `Enter run${result?.mode ? " · M change mode" : ""}${result && htmlPath ? " · O open diagram" : ""}${result ? " · R retry · /copy" : ""} · /help · Esc ${state.view === "help" && state.result ? "back" : "close"}`;
105
+ };
106
+ const modal = new ComposerModal(tui, theme, () => { dispose(); done(undefined); }, submit, dispose, {
107
+ title: "Bro · simplify",
108
+ inputLabel: "Simplify command · /simplify runs again on the latest reply",
109
+ idleHint,
80
110
  });
81
- const display=()=>{
82
- modal.setCustomMode(state.result?.mode ? state.result.mode+(state.result.preferences ? " · prefs" : "") : "one source · one explanation");
83
- modal.setModel(state.result?.model ?? "");
84
- modal.setText(
85
- state.view==="result"&&state.result
86
- ? (state.result.source.label ? "**Source:** "+stripVTControlCharacters(state.result.source.label).replace(/[\r\n]/g," ")+"\n\n---\n\n" : "")+state.result.text
87
- : SIMPLIFY_MODAL_HELP,
88
- );
89
- // Notices describe the currently shown state, not an earlier request.
90
- modal.setNotice(state.error ?? "",state.error ? "error" : "info");
111
+ const display = () => {
112
+ const result = readingResult();
113
+ const label = result?.source.label && stripVTControlCharacters(result.source.label).replace(/[\u0000-\u001f\u007f-\u009f]/g, " ");
114
+ const mode = result?.mode ?? (result?.showSteering !== undefined ? "diagram" : "one source");
115
+ modal.setCustomMode(mode + (result?.preferences ? " · prefs" : ""));
116
+ modal.setModel(result?.model ?? "");
117
+ modal.setText(result
118
+ ? (label ? `**Source:** ${label}\n\n---\n\n` : "") + (htmlPath ? stripShowDiagramFence(result.text) : result.text)
119
+ : SIMPLIFY_MODAL_HELP);
120
+ modal.setNotice(state.error ?? "", state.error ? "error" : "info");
91
121
  modal.scrollToStart();
92
122
  };
93
- const run=async (request:SimplifyStart|SimplifyAction, retained?:BroSource, mode?:BroMode, submitted?:string)=>{
94
- if(running||closed)return;
95
- const attempt=new AbortController();
96
- controller=attempt; running=true;
97
- modal.setRunning(true);modal.setNotice("");
98
- let phase=request.kind==="url" ? "Fetching page" : request.kind==="file" ? "Reading file"
99
- : request.kind==="latest" ? "Getting latest reply" : "Preparing source";
100
- const stop=startProgressClock(attempt.signal,seconds=>{
101
- if(!closed&&controller===attempt)modal.setProgress(`${phase} · ${seconds}s`);
123
+ const run = async (request: Attempt, command?: string) => {
124
+ if (controller || closed) return;
125
+ const attempt = new AbortController();
126
+ controller = attempt;
127
+ modal.setRunning(true);
128
+ modal.setNotice("");
129
+ let phase = request.kind === "url" ? "Fetching page" : request.kind === "file" ? "Reading file"
130
+ : request.kind === "latest" ? "Getting latest reply" : "Preparing source";
131
+ const stop = startProgressClock(attempt.signal, seconds => {
132
+ if (!closed) modal.setProgress(`${phase} · ${seconds}s`);
102
133
  });
103
- stopProgress=stop;
104
- const previous=state.result,previousView=state.view;
134
+ stopProgress = stop;
105
135
  try {
106
- const source=await resolveSource(ctx,request,attempt.signal,retained);
107
- if(closed||attempt.signal.aborted)return;
108
- const currentSettings=await readSettings();
109
- phase="Waiting for explanation";
110
- const result=await simplify(source.text,attempt.signal,currentSettings,text=>{
111
- if(!closed&&!attempt.signal.aborted){
112
- phase="Receiving explanation";
136
+ const result = await runSimplify(ctx, request, attempt.signal,
137
+ show => { phase = show ? "Drawing recent turns" : "Waiting for explanation"; },
138
+ text => {
139
+ if (closed) return;
140
+ phase = "Receiving output";
113
141
  modal.setText(text);
114
- }
115
- },mode);
116
- if(closed||attempt.signal.aborted)return;
117
- state.result={source,...result};
118
- state.view="result";state.error=undefined;
119
- display();
120
- if(mode && mode !== currentSettings.mode)modal.setNotice("Mode: "+mode+" (not saved)");
121
- } catch(error){
122
- if(closed||attempt.signal.aborted)return;
123
- state.result=previous;state.view=previousView;
124
- state.error=errorMessage(error);
142
+ });
143
+ if (closed) return;
144
+ const diagram = await prepareDiagram(result);
145
+ if (closed) return;
146
+ htmlPath = diagram;
147
+ showResult(state, result);
125
148
  display();
126
- // Failed replacement is editable and retryable. Do not overwrite text the user
127
- // entered while this request was running. Preserve it on modal close as well.
128
- if(submitted && !modal.getComposerText().trim()) {
129
- modal.setComposerText(submitted);
130
- state.draft=submitted;
149
+ if (request.kind === "retry" && request.mode !== undefined && request.mode !== request.result.mode) {
150
+ modal.setNotice(`Mode: ${request.mode} (not saved)`);
131
151
  }
152
+ } catch (error) {
153
+ if (closed) return;
154
+ state.error = (request.kind === "retry" ? "Retry failed: " : "") + errorMessage(error);
155
+ display();
156
+ // Retain failed input without overwriting a command typed while it ran.
157
+ if (command && !modal.getComposerText().trim()) modal.setComposerText(command);
132
158
  } finally {
133
159
  stop();
134
- if(stopProgress===stop)stopProgress=undefined;
135
- if(controller===attempt)controller=undefined;
136
- running=false;
137
- if(!closed)modal.setRunning(false);
160
+ stopProgress = undefined;
161
+ controller = undefined;
162
+ if (!closed) modal.setRunning(false);
138
163
  }
139
164
  };
140
- const retry=(switchMode=false)=>{
141
- if(running||!state.result)return;
142
- const next=switchMode?nextBroMode(state.result.mode??"balanced"):state.result.mode;
143
- void run({kind:"latest"},state.result.source,next);
165
+ const retry = (switchMode = false) => {
166
+ const result = state.result;
167
+ if (!result || (switchMode && !result.mode)) return;
168
+ const mode = switchMode && result.mode ? nextBroMode(result.mode) : result.mode;
169
+ void run({ kind: "retry", result, mode });
144
170
  };
145
- const copy=()=>{
146
- if(!state.result?.text){modal.setNotice("Nothing to copy yet.","warning");return;}
171
+ const copy = () => {
172
+ if (!state.result?.text) { modal.setNotice("Nothing to copy yet.", "warning"); return; }
147
173
  void copyToClipboard(state.result.text)
148
- .then(()=>{if(!closed)modal.setNotice("Explanation copied.","success");})
149
- .catch(error=>{if(!closed)modal.setNotice("Copy failed: "+errorMessage(error),"error");});
174
+ .then(() => { if (!closed) modal.setNotice("Result copied.", "success"); })
175
+ .catch(error => { if (!closed) modal.setNotice("Copy failed: " + errorMessage(error), "error"); });
150
176
  };
151
- function submit(raw:string):void {
152
- if(running||closed)return;
153
- const action=parseSimplifyAction(raw);
154
- if(action.kind==="invalid"){modal.setNotice(action.message,"warning");return;}
155
- modal.clearComposer();state.draft=undefined;
156
- if(action.kind==="copy"){copy();return;}
157
- if(action.kind==="clear"){
158
- state.result=undefined;state.view="empty";state.error=undefined;display();return;
177
+ function submit(raw: string): void {
178
+ if (controller || closed) return;
179
+ const action = parseSimplifyAction(raw);
180
+ if (action.kind === "invalid") { modal.setNotice(action.message, "warning"); return; }
181
+ modal.clearComposer();
182
+ state.draft = undefined;
183
+ switch (action.kind) {
184
+ case "copy": copy(); return;
185
+ case "clear": state.result = undefined; state.view = "empty"; htmlPath = undefined; break;
186
+ case "help": state.view = "help"; break;
187
+ case "retry": retry(); return;
188
+ default: void run(action, raw); return;
159
189
  }
160
- if(action.kind==="help"){state.view="help";state.error=undefined;display();return;}
161
- if(action.kind==="retry"){retry();return;}
162
- void run(action,undefined,undefined,raw);
190
+ state.error = undefined;
191
+ display();
163
192
  }
164
- modal.onKey=(data:string)=>{
165
- if(!running && state.view==="help" && state.result && matchesKey(data,"escape")){
166
- state.view="result";state.error=undefined;display();return true;
193
+ modal.onKey = data => {
194
+ if (!controller && state.view === "help" && state.result && matchesKey(data, "escape")) {
195
+ state.view = "result";
196
+ state.error = undefined;
197
+ display();
198
+ return true;
199
+ }
200
+ if (controller || modal.getComposerText().trim()) return false;
201
+ const result = readingResult();
202
+ if (!result) return false;
203
+ const key = data.toLowerCase();
204
+ if (key === "m" && result.mode) { retry(true); return true; }
205
+ if (key === "r") { retry(); return true; }
206
+ if (key === "c") { copy(); return true; }
207
+ if (key === "o" && htmlPath) {
208
+ const opened = openShowHtml(htmlPath);
209
+ modal.setNotice(opened ? "Opening diagram" : `Could not open ${htmlPath}. Reopen Simplify to rebuild it.`, opened ? "info" : "error");
210
+ return true;
167
211
  }
168
- if(running||modal.getComposerText().trim())return false;
169
- if(matchesKey(data,"m")||matchesKey(data,"shift+m")){retry(true);return Boolean(state.result);}
170
- if(matchesKey(data,"r")||matchesKey(data,"shift+r")){retry();return Boolean(state.result);}
171
- if(matchesKey(data,"c")||matchesKey(data,"shift+c")){copy();return true;}
172
212
  return false;
173
213
  };
174
214
  display();
175
- if(state.draft)modal.setComposerText(state.draft);
176
- if(initial.kind!=="resume")queueMicrotask(()=>{if(!closed)void run(initial,undefined,undefined,
177
- initial.kind==="latest" ? "/simplify" : "/"+initial.kind+" "+initial.input);});
215
+ if (state.draft) modal.setComposerText(state.draft);
216
+ if (initial) queueMicrotask(() => { void run(initial, submitted); });
178
217
  return modal;
179
- },{mouse:true});
218
+ }, { mouse: true });
180
219
  }
package/src/sources.ts CHANGED
@@ -25,7 +25,7 @@ export const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
25
25
 
26
26
  export async function extractDocumentText(input: string, cwd: string, signal?: AbortSignal): Promise<string> {
27
27
  const requested = unquote(input.trim());
28
- if (!requested) throw new Error("Use /bro file <path>.");
28
+ if (!requested) throw new Error("Use /bro simplify file <path>.");
29
29
  if (signal?.aborted) throw new Error("Canceled.");
30
30
 
31
31
  let path: string;
@@ -143,13 +143,13 @@ export function isPublicWebAddress(address: string): boolean {
143
143
 
144
144
  export function parseWebUrl(input: string): URL {
145
145
  const requested = unquote(input.trim());
146
- if (!requested) throw new Error("Use /bro url <url>.");
146
+ if (!requested) throw new Error("Use /bro simplify url <url>.");
147
147
 
148
148
  let url: URL;
149
149
  try {
150
150
  url = new URL(requested);
151
151
  } catch {
152
- throw new Error("That is not a valid URL. Use /bro url https://example.com/article.");
152
+ throw new Error("That is not a valid URL. Use /bro simplify url https://example.com/article.");
153
153
  }
154
154
  if (url.protocol !== "http:" && url.protocol !== "https:") {
155
155
  throw new Error("Bro can read only public HTTP or HTTPS webpages.");