@pratikgajjar/pi-recall 0.1.0 → 0.1.2

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 (3) hide show
  1. package/README.md +22 -0
  2. package/package.json +1 -1
  3. package/src/index.ts +77 -19
package/README.md CHANGED
@@ -41,6 +41,28 @@ pi -e ./packages/pi-recall/src/index.ts
41
41
 
42
42
  All tools accept `repo` (pass `"."` for the current project), `source` (`cursor` \| `claude` \| `codex` \| `pi`), and `since` (e.g. `7d`).
43
43
 
44
+ ### Navigating large sessions
45
+
46
+ Some sessions are huge (thousands of messages). `recall_transcript` takes
47
+ three extra params to slice instead of dumping everything:
48
+
49
+ - `range` — Python-style slice over the message list: `":100"` first 100,
50
+ `"-50:"` last 50, `"305:315"` window. Negative indices count from the end.
51
+ - `outline` — one line per message: `[N] role: first-line`. A cheap table of
52
+ contents you can scan before slicing in.
53
+ - `role` — comma-separated allowlist: `"user,assistant"` (skip tool noise),
54
+ `"user"` (just the prompts), `"tool"`. Tool-related labels (`toolResult`,
55
+ `toolCall`, `function_call`, …) all collapse to `tool`. In long agent loops,
56
+ ~50% of messages are tool noise; in some, 98% — a 30k-msg session shrinks to
57
+ ~600 with `role="user"`.
58
+
59
+ Every rendered message carries its own `## msg N/TOTAL role` header so any
60
+ slice is self-locating. **Default-cap:** if a session has more than 200
61
+ messages and you ask for neither `range` nor `outline`, the tool returns the
62
+ outline (plus a one-line note explaining how to drill in) instead of a
63
+ context-blowing wall of text. After a `recall_search` hit at `msg_idx=N`,
64
+ prefer `range: "N-5:N+5"` over reading the whole session.
65
+
44
66
  ### Recommended agent prompt
45
67
 
46
68
  Drop into your project's `AGENTS.md` / `CLAUDE.md`:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pratikgajjar/pi-recall",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "pi extension: search your past AI chat history (Cursor, Claude Code, Codex, pi) via the recall CLI",
5
5
  "type": "module",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -338,37 +338,96 @@ export default function recallExtension(pi: ExtensionAPI) {
338
338
  repo: Type.Optional(Type.String({ description: REPO_HELP })),
339
339
  source: Type.Optional(Type.String({ description: SOURCE_HELP })),
340
340
  since: Type.Optional(Type.String({ description: SINCE_HELP })),
341
+ range: Type.Optional(
342
+ Type.String({
343
+ description:
344
+ "Python-style slice over the message list. ':100' = first 100, '-50:' = last 50, '305:315' = window around a recall_search hit (msg_idx). Negative indices count from the end.",
345
+ }),
346
+ ),
347
+ outline: Type.Optional(
348
+ Type.Boolean({
349
+ description:
350
+ "One line per message ([N] role: first-line). Use to navigate a large session before slicing in with 'range'.",
351
+ }),
352
+ ),
353
+ role: Type.Optional(
354
+ Type.String({
355
+ description:
356
+ "Comma-separated roles to keep: 'user', 'assistant', 'tool'. Tool-related roles (toolResult, toolCall, function_call, etc.) collapse to 'tool'. Use 'user,assistant' to skip tool noise (~50% of long agent loops are tool messages).",
357
+ }),
358
+ ),
341
359
  });
342
360
 
361
+ // Above this many messages, recall_transcript called without 'range' or
362
+ // 'outline' returns the outline instead of the full body — protects the
363
+ // agent from accidentally pulling a 25 MB transcript into context.
364
+ const BIG_SESSION_THRESHOLD = 200;
365
+
366
+ function parseMsgCount(outlineStdout: string): number {
367
+ const m = outlineStdout.match(/msgs=(\d+)\s+\(outline\)/);
368
+ return m ? Number.parseInt(m[1], 10) : 0;
369
+ }
370
+
343
371
  pi.registerTool({
344
372
  name: "recall_transcript",
345
373
  label: "recall transcript",
346
374
  description:
347
- "Read a past AI session as a full transcript. Pass a session_id from recall_search, or omit it to get the most recent session (optionally filtered by repo/source/since).",
375
+ "Read a past AI session as a transcript. Pass a session_id from recall_search, or omit it to get the most recent session (optionally filtered by repo/source/since). Large sessions: use 'outline' to navigate, then 'range' to slice. After a recall_search hit at msg_idx=N, prefer range='N-5:N+5' over the full session.",
348
376
  promptSnippet: "Read a past AI session transcript",
349
377
  promptGuidelines: [
350
378
  "Call recall_transcript after recall_search to read a specific session before reusing its decisions.",
351
379
  "Omit session_id with repo: '.' to pull the most recent conversation in this project.",
380
+ "After a recall_search hit at msg_idx=N, call recall_transcript with range='N-5:N+5' to read around it instead of dumping the whole session.",
381
+ "For an unfamiliar large session, call with outline=true first — it returns one line per message so you can pick a range to drill into.",
352
382
  ],
353
383
  parameters: transcriptSchema,
354
384
 
355
385
  async execute(_id, params, signal) {
356
- const args = params.session_id
357
- ? ["show", params.session_id]
358
- : ["last", ...buildFilterArgs({
359
- repo: params.repo,
360
- source: params.source,
361
- since: params.since,
362
- })];
363
- const res = await runRecall(args, signal);
364
- if (!res.ok) {
365
- const msg = res.stderr || res.stdout.trim() || `recall exited with code ${res.code}`;
386
+ const filterArgs = buildFilterArgs({
387
+ repo: params.repo,
388
+ source: params.source,
389
+ since: params.since,
390
+ });
391
+ const cmd = params.session_id ? ["show", params.session_id] : ["last", ...filterArgs];
392
+
393
+ // Honor an explicit slice as-is (range / outline / role all count).
394
+ if (params.range || params.outline || params.role) {
395
+ if (params.range) cmd.push("--range", params.range);
396
+ if (params.outline) cmd.push("--outline");
397
+ if (params.role) cmd.push("--role", params.role);
398
+ const res = await runRecall(cmd, signal);
399
+ if (!res.ok) {
400
+ const msg = res.stderr || res.stdout.trim() || `recall exited with code ${res.code}`;
401
+ return { content: [{ type: "text", text: msg }], details: { found: false }, isError: true };
402
+ }
403
+ return {
404
+ content: [{ type: "text", text: capTranscript(res.stdout.trim()) }],
405
+ details: { found: true },
406
+ };
407
+ }
408
+
409
+ // No slice asked for: peek at the outline first to size the session.
410
+ // ~ms; lets us auto-protect the agent's context on huge transcripts.
411
+ const peek = await runRecall([...cmd, "--outline"], signal);
412
+ if (!peek.ok) {
413
+ const msg = peek.stderr || peek.stdout.trim() || `recall exited with code ${peek.code}`;
414
+ return { content: [{ type: "text", text: msg }], details: { found: false }, isError: true };
415
+ }
416
+ const msgCount = parseMsgCount(peek.stdout);
417
+ if (msgCount > BIG_SESSION_THRESHOLD) {
418
+ const note =
419
+ `// session has ${msgCount} messages; defaulting to outline. Call recall_transcript again with range='FROM:TO' (e.g. range='-50:') to read a slice.\n\n`;
366
420
  return {
367
- content: [{ type: "text", text: msg }],
368
- details: { found: false },
369
- isError: true,
421
+ content: [{ type: "text", text: capTranscript(note + peek.stdout.trim()) }],
422
+ details: { found: true },
370
423
  };
371
424
  }
425
+ // Small session — the full transcript is safe.
426
+ const res = await runRecall(cmd, signal);
427
+ if (!res.ok) {
428
+ const msg = res.stderr || res.stdout.trim() || `recall exited with code ${res.code}`;
429
+ return { content: [{ type: "text", text: msg }], details: { found: false }, isError: true };
430
+ }
372
431
  return {
373
432
  content: [{ type: "text", text: capTranscript(res.stdout.trim()) }],
374
433
  details: { found: true },
@@ -378,11 +437,10 @@ export default function recallExtension(pi: ExtensionAPI) {
378
437
  renderCall(args, theme, context) {
379
438
  const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
380
439
  const target = args?.session_id ?? (args?.repo ? `last in ${args.repo}` : "last");
381
- text.setText(
382
- theme.fg("toolTitle", theme.bold("recall transcript")) +
383
- " " +
384
- theme.fg("accent", target),
385
- );
440
+ let c = theme.fg("toolTitle", theme.bold("recall transcript")) + " " + theme.fg("accent", target);
441
+ if (args?.outline) c += theme.fg("muted", " [outline]");
442
+ else if (args?.range) c += theme.fg("muted", ` [${args.range}]`);
443
+ text.setText(c);
386
444
  return text;
387
445
  },
388
446
  renderResult(result, options, theme, context) {