@juanhuttemann/pi-deep-research 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,7 +6,7 @@ runs [deep-research](https://github.com/juanhuttemann/deep-research)
6
6
  evidence, and checks every claim against the text of the pages it fetched.
7
7
  Pi's own model answers the model calls, through Pi's sign-in.
8
8
 
9
- ![A deep_research run in Pi: sub-agents searching, with the pages and sources so far](https://raw.githubusercontent.com/juanhuttemann/deep-research/main/docs/images/pi-research.png)
9
+ ![A research run in Pi: the planned sub-topics searched, two follow-up searches running, and the searches, pages and sources so far](https://raw.githubusercontent.com/juanhuttemann/deep-research/main/docs/images/pi-research.png)
10
10
 
11
11
  ## Install
12
12
 
@@ -29,56 +29,73 @@ drsh's search and scraper settings still apply. SearXNG works with no
29
29
  setup, while Exa, Tavily and Firecrawl use their keys from `drsh setup`.
30
30
  `/research-doctor` checks them.
31
31
 
32
- ## What it adds
33
-
34
- - **`deep_research` tool.** Ask Pi to research something.
35
- - While the run goes, the call shows the phase with a spinner and how
36
- long it has taken, what the model is doing, each sub-agent (follow-ups
37
- marked), and the searches, pages and sources so far. Once the search
38
- is over, its sub-agents fold into one line, and the fact-check's tally
39
- shows when it has one.
40
- - When it ends, the call shows the confidence, the fact-check's verdicts,
41
- the claims it did not confirm, and the folder the run's files are in;
42
- Ctrl+O expands it to every claim and each file's full path.
43
- - Esc stops a run. It keeps a checkpoint, and resuming from it skips the
44
- searches already done.
45
- - When the question leaves something open, the clarifying questions open
46
- as Pi dialogs. In print or JSON mode they go back to the model, which
47
- asks you.
48
- - Every run setting drsh has is a tool parameter: depth (instant, quick,
49
- standard, deep), sub-topics, pages per topic, follow-ups, format,
50
- words, tables and clarify. `domains`, `trace` and `review_plan`
51
- (review and edit the plan before any search) are parameters too. The
52
- settings come from `drsh settings`, so a new setting in drsh appears
53
- here with no update.
54
- - **`research_ask` tool.** Answers a follow-up question from the pages a
55
- run already fetched, without searching again. The answer is not
56
- fact-checked.
57
- - **`/research [--topics 3 --follow-ups off --domains a,b …] question`**
58
- runs a research yourself.
59
- - With no flags it opens the settings screen first: ←/→ steps the
60
- setting under the cursor through its values, Enter lists them all, and
61
- the line under the list says what the setting does.
62
- - You answer the clarifying questions and review the plan: its depth,
63
- pages per topic, follow-ups, and the search each sub-topic starts
64
- from. An edit past drsh's sub-topic limit is refused there. Then the
65
- run starts, ends in a card like the tool's (with the tokens it used),
66
- and the agent reports what it found. An instant run has no plan
67
- review: it searches the question as asked.
68
- - `/research-stop` ends the run, keeping a checkpoint.
69
- - `/research-resume` continues the last stopped, failed or incomplete
70
- run from its checkpoint without repeating finished searches;
71
- `/research-resume <checkpoint>` continues another. A run stopped
72
- before drsh's first checkpoint has nothing searched: it starts again
73
- with the same question, settings and answers, and with the plan you
74
- approved, edits included, when you had approved one.
75
- - **`/research-model`** picks the model research runs use:
76
- - Pi's current model, the default.
77
- - Another of Pi's models, for example a cheaper one for the many calls a
78
- run makes.
79
- - drsh's own configured model.
80
- - **`/research-doctor`** checks the model (through Pi), search and the
81
- scraper.
32
+ ## Ask Pi
33
+
34
+ Ask Pi to research something, and it calls the `deep_research` tool. A run
35
+ takes minutes and makes many model calls, so Pi uses it for questions that
36
+ need sources, not quick lookups.
37
+
38
+ - **While it runs**, the call shows the phase with a spinner and its clock,
39
+ what the model is doing, each sub-agent and its search (follow-ups
40
+ marked), and the searches, pages and sources so far. Once the search is
41
+ over, the sub-agents fold into one line, and the fact-check shows its
42
+ tally when it has one.
43
+ - **When it ends**, the call shows the result: the confidence, the claims
44
+ as the run concluded them, the ones the fact-check did not confirm, and
45
+ the folder the run's files are in. Ctrl+O expands it to every claim and
46
+ each file's full path. Pi then answers from the report, keeping its
47
+ citations.
48
+
49
+ ![The result of a run: complete, confidence high, 7 claims supported and 1 partial, with the partial claim named and the run's files](https://raw.githubusercontent.com/juanhuttemann/deep-research/main/docs/images/pi-research-result.png)
50
+
51
+ - **Esc stops a run.** It keeps a checkpoint, and resuming from it skips
52
+ the searches already done.
53
+ - **Clarifying questions**, when the question leaves something open, open
54
+ as Pi dialogs. In print or JSON mode they go back to the model, which
55
+ asks you.
56
+
57
+ Every run setting drsh has is a tool parameter: depth (instant, quick,
58
+ standard, deep), sub-topics, pages per topic, follow-ups, format, words,
59
+ tables and clarify. `domains`, `trace` and `review_plan` (review and edit
60
+ the plan before any search) are parameters too. The settings come from
61
+ `drsh settings`, so a new setting in drsh appears here with no update.
62
+
63
+ For a follow-up, Pi uses the `research_ask` tool: it answers from the pages
64
+ a run already fetched, without searching again. Its answers are not
65
+ fact-checked.
66
+
67
+ ## Run it yourself
68
+
69
+ `/research question` runs a research from the prompt.
70
+
71
+ 1. **Settings.** With no flags, the settings screen opens first. ←/→ steps
72
+ the setting under the cursor through its values, Enter lists them all,
73
+ and the line under the list says what the setting does. Flags skip it:
74
+ `/research --mode quick --follow-ups off --domains a.com,b.org question`.
75
+
76
+ ![The settings screen: Start, then depth, sub-topics, pages per topic, follow-ups, format, words, tables and clarify, with the help for depth under the list](https://raw.githubusercontent.com/juanhuttemann/deep-research/main/docs/images/pi-research-settings.png)
77
+
78
+ 2. **Questions.** You answer the clarifying questions, if any.
79
+ 3. **Plan.** You review the plan: its depth, pages per topic, follow-ups,
80
+ and the search each sub-topic starts from. Run it, edit the sub-topics
81
+ (one per line), or cancel. An edit past drsh's sub-topic limit is
82
+ refused here. An instant run has no plan review: it searches the
83
+ question as asked.
84
+
85
+ ![The plan review: three sub-topics, each with its search, the depth and pages per topic above them, and Run this plan, Edit sub-topics and Cancel below](https://raw.githubusercontent.com/juanhuttemann/deep-research/main/docs/images/pi-research-plan.png)
86
+
87
+ 4. **Run.** The run shows above the prompt as the tool's does, and ends in
88
+ the same result, with the tokens it used. Pi then reports what it found.
89
+
90
+ | Command | What it does |
91
+ | --- | --- |
92
+ | `/research [flags] question` | Runs a research, as above. |
93
+ | `/research-stop` | Ends the `/research` run, keeping a checkpoint. |
94
+ | `/research-resume [checkpoint]` | Continues the last stopped, failed or incomplete run, or the checkpoint given, without repeating finished searches. A run stopped before drsh's first checkpoint starts again with the same question, settings and answers, and the plan you approved, edits included. |
95
+ | `/research-model` | Picks the model runs use: Pi's current model (the default), another of Pi's models (a cheaper one, say, for the many calls a run makes), or drsh's own configured model. Type to filter the list. |
96
+ | `/research-doctor` | Checks the model (through Pi), search and the scraper. |
97
+
98
+ ## Files
82
99
 
83
100
  Each run's files (`.md`, `.pdf`, `.json`, checkpoint) go in
84
101
  `~/.cache/deep-research-pi/runs/` (`$DEEP_RESEARCH_PI_RUNS` to change it),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juanhuttemann/pi-deep-research",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Fact-checked, cited web research inside Pi: runs the deep-research CLI (drsh) on Pi's own model, with live progress, clarifying questions and plan review",
5
5
  "keywords": [
6
6
  "pi-package",
package/src/index.ts CHANGED
@@ -10,7 +10,7 @@ import { join } from "node:path";
10
10
  import type { Api, Model } from "@earendil-works/pi-ai";
11
11
  import { StringEnum } from "@earendil-works/pi-ai";
12
12
  import type { ExtensionAPI, ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
13
- import { Box, Text } from "@earendil-works/pi-tui";
13
+ import { Box, Container, Text, TruncatedText } from "@earendil-works/pi-tui";
14
14
  import { Type, type TSchema } from "typebox";
15
15
  import { checkAnswers, clip, maxAnswerChars, maxAnswers, type Answer } from "./args.ts";
16
16
  import { startBridge, type Bridge } from "./bridge.ts";
@@ -32,7 +32,7 @@ import {
32
32
  valueLabel,
33
33
  type Setting,
34
34
  } from "./settings.ts";
35
- import { type MenuResult, pick as pickWithNotice, SettingsMenu, startLabel } from "./menu.ts";
35
+ import { type MenuResult, pick as pickWithNotice, pickFiltered, SettingsMenu, startLabel } from "./menu.ts";
36
36
 
37
37
  const modelEntry = "deep-research-model";
38
38
  const runEntry = "deep-research-run";
@@ -120,14 +120,25 @@ function untilAborted<T>(p: Promise<T>, signal?: AbortSignal): Promise<T | undef
120
120
  // as lines at ten, and a run's view (the sub-agents of a deep plan and its
121
121
  // follow-ups, the counts, the fact-check's notes) runs past that, losing
122
122
  // its last lines; the plan of a review the same. In the terminal they go in
123
- // as a component, which is not cut; elsewhere as lines.
124
- function showLines(ctx: ExtensionContext, key: string, lines: string[] | undefined): void {
123
+ // as a component, which is not cut; elsewhere as lines. A run's view is
124
+ // one line per row, cut to the terminal's width: wrapped, a sub-agent's
125
+ // search ran onto a line of its own, mid-word. The plan wraps, its names
126
+ // and searches whole.
127
+ function showLines(ctx: ExtensionContext, key: string, lines: string[] | undefined, o: { cut?: boolean } = {}): void {
125
128
  if (lines === undefined || ctx.mode !== "tui") {
126
129
  ctx.ui.setWidget(key, lines);
127
130
  return;
128
131
  }
129
- const text = lines.join("\n");
130
- ctx.ui.setWidget(key, () => new Text(text, 1, 0));
132
+ if (!o.cut) {
133
+ const text = lines.join("\n");
134
+ ctx.ui.setWidget(key, () => new Text(text, 1, 0));
135
+ return;
136
+ }
137
+ ctx.ui.setWidget(key, () => {
138
+ const c = new Container();
139
+ for (const l of lines) c.addChild(new TruncatedText(l, 1, 0));
140
+ return c;
141
+ });
131
142
  }
132
143
 
133
144
  export default function deepResearch(pi: ExtensionAPI) {
@@ -700,7 +711,7 @@ export default function deepResearch(pi: ExtensionAPI) {
700
711
  async function backgroundRun(ctx: ExtensionContext, abort: AbortController, req: Request): Promise<void> {
701
712
  const status = ticker(() => {
702
713
  if (!latest) return;
703
- showLines(ctx, "deep-research", progressLines(latest, ctx.ui.theme));
714
+ showLines(ctx, "deep-research", progressLines(latest, ctx.ui.theme), { cut: true });
704
715
  ctx.ui.setStatus("deep-research", `research: ${statusLabel(latest)}`);
705
716
  }, tickMs, () => latest?.started !== undefined);
706
717
  let latest: ResearchDetails | undefined;
@@ -748,7 +759,7 @@ export default function deepResearch(pi: ExtensionAPI) {
748
759
  const current = "Pi's current model (follows /model)";
749
760
  const own = "drsh's own configured model (drsh setup)";
750
761
  const models = ctx.modelRegistry.getAvailable().map((m) => `${m.provider}/${m.id}`);
751
- const pick = await ctx.ui.select("Model for research runs", [current, own, ...models]);
762
+ const pick = await pickFiltered(ctx, "Model for research runs", [current, own, ...models]);
752
763
  if (pick === undefined) return;
753
764
  if (pick === current) choice = { kind: "pi" };
754
765
  else if (pick === own) choice = { kind: "drsh" };
package/src/menu.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  // place; Enter still hands the row back for the full option list.
7
7
 
8
8
  import type { ExtensionContext, Theme } from "@earendil-works/pi-coding-agent";
9
- import { Container, type KeybindingsManager, matchesKey, Spacer, Text } from "@earendil-works/pi-tui";
9
+ import { Container, decodeKittyPrintable, type KeybindingsManager, matchesKey, Spacer, Text } from "@earendil-works/pi-tui";
10
10
 
11
11
  // Menu is a list of lines with a cursor and an optional warning under them.
12
12
  // Enter picks the line under the cursor, the cancel key closes it.
@@ -215,6 +215,153 @@ export class SettingsMenu extends Menu {
215
215
  }
216
216
  }
217
217
 
218
+ // matching is the indices of the options holding every word of filter, in
219
+ // their order, case aside: drsh's own picker's rule (matching in
220
+ // internal/ui/pick.go), so "fable 5" finds "anthropic/claude-fable-5.2".
221
+ export function matching(options: string[], filter: string): number[] {
222
+ const words = filter.toLowerCase().split(/\s+/).filter(Boolean);
223
+ const out: number[] = [];
224
+ options.forEach((o, i) => {
225
+ const l = o.toLowerCase();
226
+ if (words.every((w) => l.includes(w))) out.push(i);
227
+ });
228
+ return out;
229
+ }
230
+
231
+ const control = (ch: string) => {
232
+ const c = ch.charCodeAt(0);
233
+ return c < 32 || c === 0x7f || (c >= 0x80 && c <= 0x9f);
234
+ };
235
+
236
+ // pasteStart and pasteEnd wrap a bracketed paste, which Pi hands on as it
237
+ // came: rejected for its escape bytes, a pasted model name was dropped.
238
+ const pasteStart = "\x1b[200~";
239
+ const pasteEnd = "\x1b[201~";
240
+
241
+ // typed is the text a key types, or undefined for a key that types none:
242
+ // a Kitty CSI-u sequence decoded, a bracketed paste (its line breaks and
243
+ // other control characters as spaces), or plain text with no control
244
+ // character.
245
+ function typed(data: string): string | undefined {
246
+ const kitty = decodeKittyPrintable(data);
247
+ if (kitty !== undefined) return kitty;
248
+ if (data.startsWith(pasteStart)) {
249
+ const body = data.slice(pasteStart.length).replace(pasteEnd, "");
250
+ return [...body].map((ch) => (control(ch) ? " " : ch)).join("");
251
+ }
252
+ return [...data].some(control) ? undefined : data;
253
+ }
254
+
255
+ // maxShown is how many rows the filtered list shows at once, as Pi's own
256
+ // select does.
257
+ const maxShown = 10;
258
+
259
+ // FilterMenu is a list under a filter box, drawn like Pi's select: typing
260
+ // keeps the rows holding every word typed, the arrows move, Enter picks,
261
+ // the cancel key closes. Pi's select has no filter, and the model list it
262
+ // showed ran to hundreds of rows, read ten at a time with the arrows.
263
+ export class FilterMenu extends Container {
264
+ private filter = "";
265
+ private shown: number[];
266
+ private selected = 0;
267
+ private readonly box = new Text("", 1, 0);
268
+ private readonly list = new Container();
269
+ private readonly theme: Theme;
270
+ private readonly kb: KeybindingsManager;
271
+ private readonly options: string[];
272
+ private readonly close: (index: number | undefined) => void;
273
+
274
+ constructor(theme: Theme, kb: KeybindingsManager, o: { title: string; options: string[] }, close: (index: number | undefined) => void) {
275
+ super();
276
+ this.theme = theme;
277
+ this.kb = kb;
278
+ this.options = o.options;
279
+ this.shown = o.options.map((_, i) => i);
280
+ this.close = close;
281
+ this.addChild(new Border(theme));
282
+ this.addChild(new Spacer(1));
283
+ this.addChild(new Text(theme.fg("accent", theme.bold(o.title)), 1, 0));
284
+ this.addChild(new Spacer(1));
285
+ this.addChild(this.box);
286
+ this.addChild(new Spacer(1));
287
+ this.addChild(this.list);
288
+ this.addChild(new Spacer(1));
289
+ const hint = (k: string, d: string) => theme.fg("dim", k) + theme.fg("muted", ` ${d}`);
290
+ const keys = (k: "tui.select.confirm" | "tui.select.cancel") => kb.getKeys(k).join("/");
291
+ this.addChild(new Text([hint("type", "filter"), hint("↑↓", "navigate"), hint(keys("tui.select.confirm"), "select"), hint(keys("tui.select.cancel"), "cancel")].join(" "), 1, 0));
292
+ this.addChild(new Spacer(1));
293
+ this.addChild(new Border(theme));
294
+ this.draw();
295
+ }
296
+
297
+ private draw(): void {
298
+ const t = this.theme;
299
+ this.box.setText(`${t.fg("accent", "> ")}${this.filter ? t.fg("text", this.filter) : t.fg("dim", "type to filter")}`);
300
+ this.list.clear();
301
+ if (!this.shown.length) {
302
+ this.list.addChild(new Text(t.fg("dim", " nothing matches"), 1, 0));
303
+ return;
304
+ }
305
+ // The window keeps the cursor in view, centred where it can be.
306
+ const start = Math.max(0, Math.min(this.selected - Math.floor(maxShown / 2), this.shown.length - maxShown));
307
+ this.shown.slice(start, start + maxShown).forEach((i, n) => {
308
+ const l = this.options[i];
309
+ const text = start + n === this.selected ? t.fg("accent", `→ ${l}`) : ` ${t.fg("text", l)}`;
310
+ this.list.addChild(new Text(text, 1, 0));
311
+ });
312
+ if (this.shown.length > maxShown) {
313
+ this.list.addChild(new Text(t.fg("dim", ` (${this.selected + 1}/${this.shown.length})`), 1, 0));
314
+ }
315
+ }
316
+
317
+ private refilter(text: string): void {
318
+ this.filter = text;
319
+ this.shown = matching(this.options, text);
320
+ this.selected = 0;
321
+ }
322
+
323
+ handleInput(data: string): void {
324
+ const kb = this.kb;
325
+ // Text typed goes in the filter before any binding is read: a letter
326
+ // bound to confirm picked the highlighted row at the first letter
327
+ // of a name.
328
+ const s = typed(data);
329
+ if (s !== undefined) {
330
+ this.refilter(this.filter + s);
331
+ this.draw();
332
+ return;
333
+ }
334
+ if (kb.matches(data, "tui.select.confirm") || data === "\n") {
335
+ // Enter on a filter that matches nothing does nothing.
336
+ if (this.shown.length) this.close(this.shown[this.selected]);
337
+ return;
338
+ }
339
+ if (kb.matches(data, "tui.select.cancel")) {
340
+ this.close(undefined);
341
+ return;
342
+ }
343
+ if (kb.matches(data, "tui.select.up")) {
344
+ this.selected = Math.max(0, this.selected - 1);
345
+ } else if (kb.matches(data, "tui.select.down")) {
346
+ this.selected = Math.min(this.shown.length - 1, this.selected + 1);
347
+ } else if (matchesKey(data, "backspace") || kb.matches(data, "tui.editor.deleteCharBackward")) {
348
+ if (!this.filter) return;
349
+ this.refilter([...this.filter].slice(0, -1).join(""));
350
+ } else {
351
+ return;
352
+ }
353
+ this.draw();
354
+ }
355
+ }
356
+
357
+ // pickFiltered is ctx.ui.select with a filter box, for a long list. Outside
358
+ // the terminal it is the plain select.
359
+ export async function pickFiltered(ctx: ExtensionContext, title: string, options: string[]): Promise<string | undefined> {
360
+ if (ctx.mode !== "tui") return ctx.ui.select(title, options);
361
+ const i = await ctx.ui.custom<number | undefined>((_tui, theme, kb, done) => new FilterMenu(theme, kb, { title, options }, done));
362
+ return i === undefined ? undefined : options[i];
363
+ }
364
+
218
365
  // Border is DynamicBorder drawn with the theme Pi hands the component: Pi's
219
366
  // own reads a global theme that an extension's module copy may not have set.
220
367
  class Border {