Files
backnotprop__plannotator/apps/pi-extension/index.ts
T
Michael Ramos b780739291 feat(annotate): support HTML files and URL annotation (#545)
* fix(annotate): sanitize dangerous link protocols in markdown renderer

Block javascript:, data:, and vbscript: URLs in InlineMarkdown link
rendering. Links with dangerous protocols render as plain text instead
of clickable anchors. Uses a blocklist approach so existing links with
custom protocols (obsidian://, vscode://, Windows C:\ paths) continue
to work.

For provenance purposes, this commit was AI assisted.

* feat(annotate): add HTML-to-markdown and URL-to-markdown utilities

- html-to-markdown.ts: Turndown wrapper with GFM table rule, strips
  script/style/noscript tags
- url-to-markdown.ts: Jina Reader (free, returns markdown) with
  fetch+Turndown fallback. Warns on Jina failure, auto-skips Jina for
  local/private URLs (localhost, 192.168.*, 10.*, etc.)
- config.ts: add jina setting and resolveUseJina() with priority chain
  --no-jina flag > PLANNOTATOR_JINA env > config.json > default true

For provenance purposes, this commit was AI assisted.

* feat(annotate): support HTML files and URLs in annotate command

Extend the annotate subcommand to accept .html/.htm local files
(converted via Turndown) and https:// URLs (fetched via Jina Reader
with fetch+Turndown fallback). URL content is fetched terminal-side
before opening the browser.

Add --no-jina global flag to disable Jina Reader per-invocation.
Add 10MB file size guard for local HTML files.

For provenance purposes, this commit was AI assisted.

* feat(annotate): HTML files in folder browser and on-demand conversion

- Widen file browser glob to include .html/.htm alongside markdown
- handleDoc converts HTML files via Turndown on demand when selected
- hasMarkdownFiles accepts optional extensions param for folder validation
- Add sourceInfo field to annotate server API response
- Add _site/, public/, out/, .docusaurus/, .jekyll-cache/,
  storybook-static/ to FILE_BROWSER_EXCLUDED

For provenance purposes, this commit was AI assisted.

* feat(annotate): source attribution badge for HTML/URL annotations

Show a subtle badge in DocBadges displaying the URL hostname or HTML
filename for converted content. Thread sourceInfo from API response
through App → Viewer → DocBadges.

Also update Pi extension to accept HTML-only folders in annotate mode.

For provenance purposes, this commit was AI assisted.

* test: update CLI help text assertion for HTML/URL annotate support

For provenance purposes, this commit was AI assisted.

* fix(annotate): address PR review findings

Security:
- Add project-root containment check for HTML files in /api/doc handler
  using exported isWithinProjectRoot() from resolve-file.ts
- Blocks path traversal via absolute paths or ../ escapes

isLocalUrl fixes:
- Add bracketed IPv6 loopback [::1] detection
- Replace hostname.startsWith('10.') with proper IPv4 regex to avoid
  matching public hostnames like 10.example.com

Revert Pi extension change:
- Pi server doesn't implement HTML file browsing or conversion yet
- Keep Pi folder validation markdown-only until both implementations
  are updated per CLAUDE.md guidelines

Cleanup:
- Remove dead el.children || el.childNodes fallback in table rule
- Extract hostnameOrFallback() helper to @plannotator/shared/project
  replacing duplicated try/catch IIFEs in DocBadges and index.ts

For provenance purposes, this commit was AI assisted.

* feat(annotate): Pi extension HTML annotation parity

Bring the Pi extension to full parity with the Bun server for HTML
annotation support:

- Vendor html-to-markdown and url-to-markdown via vendor.sh
- walkMarkdownFiles now scans .html/.htm alongside markdown
- handleDocRequest converts HTML files on-demand via Turndown with
  isWithinProjectRoot containment check
- serverAnnotate includes sourceInfo in /api/plan response
- index.ts supports URL detection (Jina Reader + fallback), HTML file
  detection with Turndown conversion, folder HTML validation, and 10MB
  file size guard
- openMarkdownAnnotation accepts and threads sourceInfo
- Add turndown as a Pi extension dependency

For provenance purposes, this commit was AI assisted.

* fix(pi): Obsidian vault walks stay markdown-only, add try/catch for HTML

- Add extensions param to walkMarkdownFiles (default: HTML-inclusive)
- Obsidian callers pass /\.mdx?$/i to match Bun server behavior
- Add try/catch around HTML file reads in handleDocRequest

For provenance purposes, this commit was AI assisted.

* fix(annotate): address second review — base-block traversal, metadata IP, dead code

Security:
- Add isWithinProjectRoot check to the base-relative block for HTML
  files in both Bun and Pi /api/doc handlers. Previously HTML files
  served via the base query param bypassed the containment guard.
- Add 169.254.0.0/16 (link-local / cloud metadata) to isLocalUrl
  private IP ranges

Cleanup:
- Remove dead hostname === "[::1]" check (WHATWG URL parser strips
  brackets; hostname === "::1" already handles it)
- Remove dead parent?.childNodes fallback in table cell() function

For provenance purposes, this commit was AI assisted.

* refactor(annotate): replace custom table rules with turndown-plugin-gfm

Drop ~60 lines of hand-rolled GFM table conversion that had a bug
(tables without explicit <thead> produced invalid GFM). Use the
official turndown-plugin-gfm plugin (24KB) which correctly handles
all table patterns plus adds strikethrough and task list support.

For provenance purposes, this commit was AI assisted.

* fix(annotate): handle all CommonMark backslash escapes in InlineMarkdown

Expand the backslash escape regex to cover all CommonMark-defined
escapable characters (. ) - # > + | { } &), not just the subset
the parser uses for formatting. Fixes literal backslashes appearing
in rendered output for Turndown-escaped content like "1\." → "1.".

For provenance purposes, this commit was AI assisted.

* fix(annotate): prevent SSRF via redirect to private/local URLs

Replace redirect: "follow" with redirect: "manual" in fetchViaTurndown
and validate each redirect hop against isLocalUrl. Blocks attacks where
an external URL redirects to cloud metadata endpoints (169.254.169.254)
or other private IPs. Limits redirect chain to 10 hops.

For provenance purposes, this commit was AI assisted.

* chore: update lockfile for turndown-plugin-gfm in Pi extension

bun install needed to resolve turndown-plugin-gfm in the Pi extension
workspace after adding it to apps/pi-extension/package.json.

For provenance purposes, this commit was AI assisted.

* fix(annotate): switch to @joplin/turndown-plugin-gfm, fix TS errors

Replace unmaintained turndown-plugin-gfm (2017, v1.0.2) with the
actively maintained Joplin fork (2025, v1.0.64, 16KB).

Fix TypeScript errors that broke CI:
- Add @ts-expect-error for untyped @joplin/turndown-plugin-gfm import
- Restructure fetchViaTurndown redirect loop to avoid uninitialized
  variable — first fetch before loop, loop only for redirects

For provenance purposes, this commit was AI assisted.

* fix(annotate): use proper declarations.d.ts instead of ts-expect-error

Add declarations.d.ts for @joplin/turndown-plugin-gfm with typed
function signatures, remove the ts-expect-error suppression.

For provenance purposes, this commit was AI assisted.

* fix: explicitly include declarations.d.ts in shared tsconfig

CI's tsc wasn't finding the ambient module declaration with implicit
include. Add explicit include to ensure declarations.d.ts is always
picked up regardless of environment.

For provenance purposes, this commit was AI assisted.

* fix: use ts-expect-error for @joplin/turndown-plugin-gfm types

CI's tsc does not pick up ambient declarations.d.ts files despite
local tsc finding them — likely a module resolution discrepancy
between environments. Revert to @ts-expect-error which passes in
both CI and local typecheck.

For provenance purposes, this commit was AI assisted.

* fix(annotate): body size limit for URL fetches, redirect error, file: protocol

- Add 10MB body size limit to both Jina and fetch+Turndown URL paths,
  matching the local HTML file guard. Streams response body and aborts
  if limit exceeded.
- Distinguish "Too many redirects" from a genuine 3xx response after
  redirect loop exhaustion.
- Add file: to the dangerous protocol blocklist in sanitizeLinkUrl.

For provenance purposes, this commit was AI assisted.

* fix(annotate): HTML folder outside cwd, HTML linked doc navigation

- Remove containment check from base-relative block for HTML files in
  both Bun and Pi /api/doc handlers. Matches markdown behavior so HTML
  files in annotated folders outside cwd are served correctly.
  Standalone block (no base) retains its cwd check as fallback.
- Widen isLocalMd → isLocalDoc to treat .html/.htm links as linked
  documents. Clicking [Next](next.html) in a converted page now opens
  it via /api/doc with Turndown conversion instead of a new browser tab.

For provenance purposes, this commit was AI assisted.

* fix(annotate): full loopback range, drain redirect bodies, document env vars

- Expand loopback check from just 127.0.0.1 to the full 127.0.0.0/8
  range so all loopback addresses skip Jina Reader
- Cancel redirect response body before re-fetching to avoid leaking
  TCP connections back to the pool
- Document PLANNOTATOR_JINA and JINA_API_KEY in CLAUDE.md env var table

For provenance purposes, this commit was AI assisted.

* fix(annotate): IPv6 loopback, readBodyWithLimit fallback, env var docs, comments

- Add [::1] back to isLocalUrl — WHATWG URL hostname getter preserves
  brackets for IPv6 (verified: Bun and Node both return "[::1]").
  Add comment explaining the empirical verification so future reviewers
  don't re-flag.
- Fix readBodyWithLimit null-body fallback to still enforce the 10MB
  limit via text length check instead of silently falling through.
- Document PLANNOTATOR_JINA and JINA_API_KEY in AGENTS.md env var table
  (CLAUDE.md is a symlink to AGENTS.md).
- Add comments to base-relative blocks in both Bun and Pi handleDoc
  explaining the intentional lack of containment check (matches
  pre-existing markdown behavior, base is set server-side).

For provenance purposes, this commit was AI assisted.

* fix(annotate): block IPv4-mapped IPv6 and private IPv6 ranges in isLocalUrl

Add PRIVATE_IPV6 regex matching bracketed IPv6 private/reserved ranges:
- ::ffff: (IPv4-mapped — embeds private IPv4 as hex, e.g. [::ffff:c0a8:1])
- fe80: (link-local)
- fc00::/7 (unique-local, covers fc00:: through fdff::)

Closes the redirect-SSRF bypass where a public URL redirects to a
private address expressed as IPv4-mapped IPv6, e.g.
http://[::ffff:169.254.169.254]/latest/meta-data/

For provenance purposes, this commit was AI assisted.

* fix(annotate): document IPv6 hostname verification, sourceInfo type, annotate flow

- Expand isLocalUrl comment with full empirical verification table
  showing actual hostname getter output for every IPv6 format in both
  Bun and Node — prevents false-positive review findings about brackets
- Add sourceInfo to /api/plan response type in App.tsx for type safety
- Update CLAUDE.md annotate flow diagram to reflect HTML/URL/folder
  input types

For provenance purposes, this commit was AI assisted.

* fix(annotate): escape \(, cancel response bodies on error, doc sourceInfo

- Add ( to backslash escape regex alongside existing ) — Turndown
  emits \( in link-adjacent contexts
- Cancel response body before throwing on !res.ok in both fetchViaJina
  and fetchViaTurndown error paths (redirect loop already did this)
- Document sourceInfo field in AGENTS.md annotate server API table

For provenance purposes, this commit was AI assisted.

* fix(annotate): skip base injection for URL annotations, body cleanup

- Skip dirname(filePath) base injection when filePath is a URL in both
  Bun and Pi annotate servers. dirname on a URL string produces a
  nonsensical filesystem path, causing linked doc clicks to 404.
  URL annotations now let links open normally instead.
- Cancel response body before throwing on content-type mismatch and
  content-length overflow in fetchViaTurndown/readBodyWithLimit.
- Fix double parseInt in readBodyWithLimit content-length check.
- Correct AGENTS.md flow diagram: OpenCode not yet implemented for
  HTML/URL annotation.

For provenance purposes, this commit was AI assisted.

* feat(annotate): OpenCode HTML file and URL annotation support

Add URL detection (Jina Reader + fallback), HTML file detection with
Turndown conversion, 10MB file size guard, and sourceInfo threading
to OpenCode's handleAnnotateCommand. Uses the same shared utilities
as the Bun CLI and Pi extension.

OpenCode uses the Bun server directly (startAnnotateServer from
@plannotator/server/annotate), so no server-side changes needed —
only the command handler routing was missing.

Note: folder annotation mode is not added (OpenCode didn't have it
before this PR for markdown either — separate scope).

For provenance purposes, this commit was AI assisted.

* chore(annotate): update slash command description, align fetch log messages

- OpenCode plannotator-annotate.md description now mentions HTML/URL
- Align fetch progress messages across all three clients: all now show
  "(via Jina Reader)" or "(via fetch+Turndown)" consistently

For provenance purposes, this commit was AI assisted.

* fix(annotate): skip conversion for .md URLs, wikilink HTML targets, cleanup

- URLs ending in .md/.mdx are fetched raw — no Jina, no Turndown.
  Content is already markdown. Removes text/plain from fetchViaTurndown
  content-type whitelist since .md URLs are now short-circuited.
- Wikilink regex widened to preserve .html/.htm targets instead of
  appending .md (e.g. [[page.html]] no longer becomes page.html.md)
- Remove redundant existsSync before statSync in OpenCode handler

For provenance purposes, this commit was AI assisted.

* test(annotate): add htmlToMarkdown conversion tests

Tests cover the core conversion utility that all three clients depend on:
- Basic HTML → markdown (headings, paragraphs, links, code blocks)
- Tables with and without <thead> (the GFM plugin bug that was caught)
- Script/style/noscript stripping
- Strikethrough (GFM)
- Empty HTML handling
- Dangerous links preserved (sanitization is in the renderer, not here)

For provenance purposes, this commit was AI assisted.

* fix(annotate): check content-type before treating .md URLs as raw markdown

URLs ending in .md/.mdx (e.g. GitHub's viewer page for README.md)
may return HTML instead of raw markdown. fetchRawText now checks the
response content-type — if the server returns HTML, returns null so
the caller falls through to Jina/Turndown for proper conversion.

For provenance purposes, this commit was AI assisted.

* fix(annotate): add SSRF redirect protection to fetchRawText

fetchRawText (for .md/.mdx URLs) was using default redirect: "follow"
with no isLocalUrl validation on redirect hops — a .md URL redirecting
to 169.254.169.254 would be followed and credentials returned as
"markdown". Now uses redirect: "manual" with per-hop isLocalUrl checks,
matching fetchViaTurndown's SSRF protection.

For provenance purposes, this commit was AI assisted.
2026-04-12 18:56:28 -07:00

1038 lines
33 KiB
TypeScript

/**
* Plannotator Pi Extension — File-based plan mode with visual browser review.
*
* Plans are written to PLAN.md on disk (git-trackable, editor-visible).
* The agent calls plannotator_submit_plan to request approval; the user
* reviews the plan in the Plannotator browser UI and can approve, deny
* with annotations, or request changes.
*
* Features:
* - /plannotator command or Ctrl+Alt+P to toggle
* - --plan flag to start in planning mode
* - --plan-file flag to customize the plan file path
* - Bash unrestricted during planning (prompt-guided)
* - Write restricted to plan file only during planning
* - plannotator_submit_plan tool with browser-based visual approval
* - [DONE:n] markers for execution progress tracking
* - /plannotator-review command for code review
* - /plannotator-annotate command for markdown annotation
*/
import { existsSync, readFileSync, statSync } from "node:fs";
import { resolve, basename } from "node:path";
import type { ThinkingLevel } from "@mariozechner/pi-agent-core";
import { Type } from "@mariozechner/pi-ai";
import type {
ExtensionAPI,
ExtensionContext,
} from "@mariozechner/pi-coding-agent";
import { Key } from "@mariozechner/pi-tui";
import { buildPromptVariables, formatTodoList, loadPlannotatorConfig, renderTemplate, resolvePhaseProfile } from "./config.js";
import {
type ChecklistItem,
markCompletedSteps,
parseChecklist,
} from "./generated/checklist.js";
import { planDenyFeedback } from "./generated/feedback-templates.js";
import { hasMarkdownFiles } from "./generated/resolve-file.js";
import { FILE_BROWSER_EXCLUDED } from "./generated/reference-common.js";
import { htmlToMarkdown } from "./generated/html-to-markdown.js";
import { urlToMarkdown } from "./generated/url-to-markdown.js";
import { loadConfig, resolveUseJina } from "./generated/config.js";
import {
getLastAssistantMessageText,
hasPlanBrowserHtml,
hasReviewBrowserHtml,
getStartupErrorMessage,
openArchiveBrowserAction,
openCodeReview,
openLastMessageAnnotation,
openMarkdownAnnotation,
openPlanReviewBrowser,
registerPlannotatorEventListeners,
} from "./plannotator-events.js";
import {
getToolsForPhase,
PLAN_SUBMIT_TOOL,
type Phase,
stripPlanningOnlyTools,
} from "./tool-scope.ts";
// ── Types ──────────────────────────────────────────────────────────────
type SavedPhaseState = {
activeTools: string[];
model?: { provider: string; id: string };
thinkingLevel: ThinkingLevel;
};
type PersistedPlannotatorState = {
phase: Phase;
planFilePath?: string;
savedState?: SavedPhaseState;
};
type AssistantTextBlock = { type?: string; text?: string };
type AssistantMessageLike = {
role?: unknown;
content?: unknown;
};
function isAssistantMessage(m: AssistantMessageLike): m is { role: "assistant"; content: AssistantTextBlock[] } {
return m.role === "assistant" && Array.isArray(m.content);
}
function getTextContent(message: { content: AssistantTextBlock[] }): string {
return message.content
.filter((block): block is { type: "text"; text: string } => block.type === "text")
.map((block) => block.text)
.join("\n");
}
function getPlanReviewAvailabilityWarning(options: { hasUI: boolean; hasPlanHtml: boolean }): string | null {
const { hasUI, hasPlanHtml } = options;
if (hasUI && hasPlanHtml) return null;
if (!hasUI && !hasPlanHtml) {
return "Plannotator: interactive plan review is unavailable in this session (no UI support and missing built assets). Plans will auto-approve on exit_plan_mode.";
}
if (!hasUI) {
return "Plannotator: interactive plan review is unavailable in this session (no UI support). Plans will auto-approve on exit_plan_mode.";
}
return "Plannotator: interactive plan review assets are missing. Rebuild the extension to restore the browser UI. Plans will auto-approve on exit_plan_mode.";
}
export default function plannotator(pi: ExtensionAPI): void {
let phase: Phase = "idle";
void registerPlannotatorEventListeners(pi);
let planFilePath = "PLAN.md";
let checklistItems: ChecklistItem[] = [];
let savedState: SavedPhaseState | null = null;
let plannotatorConfig = {};
// ── Flags ────────────────────────────────────────────────────────────
pi.registerFlag("plan", {
description: "Start in plan mode (restricted exploration and planning)",
type: "boolean",
default: false,
});
pi.registerFlag("plan-file", {
description: "Plan file path (default: PLAN.md)",
type: "string",
default: "PLAN.md",
});
// ── Helpers ──────────────────────────────────────────────────────────
function resolvePlanPath(cwd: string): string {
return resolve(cwd, planFilePath);
}
function getPhaseProfile(): ReturnType<typeof resolvePhaseProfile> | undefined {
if (phase === "planning" || phase === "executing") {
return resolvePhaseProfile(plannotatorConfig, phase);
}
return undefined;
}
function updateStatus(ctx: ExtensionContext): void {
const profile = getPhaseProfile();
if (phase === "executing" && checklistItems.length > 0) {
const completed = checklistItems.filter((t) => t.completed).length;
ctx.ui.setStatus(
"plannotator",
ctx.ui.theme.fg("accent", `📋 ${completed}/${checklistItems.length}`),
);
} else if (phase === "planning" && profile?.statusLabel) {
ctx.ui.setStatus("plannotator", ctx.ui.theme.fg("warning", profile.statusLabel));
} else if (phase === "executing" && profile?.statusLabel) {
ctx.ui.setStatus("plannotator", ctx.ui.theme.fg("accent", profile.statusLabel));
} else {
ctx.ui.setStatus("plannotator", undefined);
}
}
function updateWidget(ctx: ExtensionContext): void {
if (phase === "executing" && checklistItems.length > 0) {
const lines = checklistItems.map((item) => {
if (item.completed) {
return (
ctx.ui.theme.fg("success", "☑ ") +
ctx.ui.theme.fg("muted", ctx.ui.theme.strikethrough(item.text))
);
}
return `${ctx.ui.theme.fg("muted", "☐ ")}${item.text}`;
});
ctx.ui.setWidget("plannotator-progress", lines);
} else {
ctx.ui.setWidget("plannotator-progress", undefined);
}
}
function captureSavedState(ctx: ExtensionContext): void {
savedState = {
activeTools: pi.getActiveTools(),
model: ctx.model ? { provider: ctx.model.provider, id: ctx.model.id } : undefined,
thinkingLevel: pi.getThinkingLevel(),
};
}
function persistState(): void {
pi.appendEntry("plannotator", { phase, planFilePath, savedState });
}
async function applyModelRef(
ref: { provider: string; id: string },
ctx: ExtensionContext,
reason: string,
): Promise<void> {
const model = ctx.modelRegistry.find(ref.provider, ref.id);
if (!model) {
ctx.ui.notify(`Plannotator: ${reason} model ${ref.provider}/${ref.id} not found.`, "warning");
return;
}
const success = await pi.setModel(model);
if (!success) {
ctx.ui.notify(`Plannotator: no API key for ${ref.provider}/${ref.id}.`, "warning");
}
}
async function restoreSavedState(ctx: ExtensionContext): Promise<void> {
if (!savedState) return;
pi.setActiveTools(savedState.activeTools);
if (savedState.model) {
await applyModelRef(savedState.model, ctx, "restore");
}
pi.setThinkingLevel(savedState.thinkingLevel);
}
async function applyPhaseConfig(ctx: ExtensionContext, opts: { restoreSavedState?: boolean } = {}): Promise<void> {
const profile = getPhaseProfile();
if (opts.restoreSavedState !== false && savedState) {
await restoreSavedState(ctx);
}
if (phase === "planning" || phase === "executing") {
const baseTools = stripPlanningOnlyTools(savedState?.activeTools ?? pi.getActiveTools());
const toolSet = new Set(baseTools);
for (const tool of profile?.activeTools ?? []) toolSet.add(tool);
if (phase === "planning") {
pi.setActiveTools(getToolsForPhase([...toolSet], phase));
} else {
pi.setActiveTools([...toolSet]);
}
}
if (profile?.model) {
await applyModelRef(profile.model, ctx, phase);
}
if (profile?.thinking) {
pi.setThinkingLevel(profile.thinking);
}
updateStatus(ctx);
updateWidget(ctx);
}
async function enterPlanning(ctx: ExtensionContext): Promise<void> {
phase = "planning";
checklistItems = [];
captureSavedState(ctx);
await applyPhaseConfig(ctx, { restoreSavedState: false });
persistState();
ctx.ui.notify(
`Plannotator: planning mode enabled. Write your plan to ${planFilePath}.`,
);
const warning = getPlanReviewAvailabilityWarning({ hasUI: ctx.hasUI, hasPlanHtml: hasPlanBrowserHtml() });
if (warning) {
ctx.ui.notify(warning, "warning");
}
}
async function exitToIdle(ctx: ExtensionContext): Promise<void> {
phase = "idle";
checklistItems = [];
await restoreSavedState(ctx);
savedState = null;
updateStatus(ctx);
updateWidget(ctx);
persistState();
ctx.ui.notify("Plannotator: disabled. Full access restored.");
}
async function togglePlanMode(ctx: ExtensionContext): Promise<void> {
if (phase === "idle") {
await enterPlanning(ctx);
} else {
await exitToIdle(ctx);
}
}
// ── Commands & Shortcuts ─────────────────────────────────────────────
pi.registerCommand("plannotator", {
description: "Toggle plannotator (file-based plan mode)",
handler: async (args, ctx) => {
if (phase !== "idle") {
await exitToIdle(ctx);
return;
}
// Accept path as argument: /plannotator plans/auth.md
let targetPath = args?.trim() || undefined;
// No arg — prompt for file path interactively
if (!targetPath && ctx.hasUI) {
targetPath = await ctx.ui.input("Plan file path", planFilePath);
if (targetPath === undefined) return; // cancelled
}
if (targetPath) planFilePath = targetPath;
await enterPlanning(ctx);
},
});
pi.registerCommand("plannotator-status", {
description: "Show plannotator status",
handler: async (_args, ctx) => {
const parts = [`Phase: ${phase}`, `Plan file: ${planFilePath}`];
if (checklistItems.length > 0) {
const done = checklistItems.filter((t) => t.completed).length;
parts.push(`Progress: ${done}/${checklistItems.length}`);
}
ctx.ui.notify(parts.join("\n"), "info");
},
});
pi.registerCommand("plannotator-set-file", {
description: "Change the plan file path",
handler: async (args, ctx) => {
let targetPath = args?.trim() || undefined;
if (!targetPath && ctx.hasUI) {
targetPath = await ctx.ui.input("Plan file path", planFilePath);
if (targetPath === undefined) return; // cancelled
}
if (!targetPath) {
ctx.ui.notify(`Current plan file: ${planFilePath}`, "info");
return;
}
planFilePath = targetPath;
persistState();
ctx.ui.notify(`Plan file changed to: ${planFilePath}`);
},
});
pi.registerCommand("plannotator-review", {
description: "Open interactive code review for current changes or a PR URL",
handler: async (args, ctx) => {
if (!hasReviewBrowserHtml()) {
ctx.ui.notify(
"Code review UI not available. Run 'bun run build' in the pi-extension directory.",
"error",
);
return;
}
try {
const prUrl = args?.trim() || undefined;
const isPRReview = prUrl?.startsWith("http://") || prUrl?.startsWith("https://");
const result = await openCodeReview(ctx, { prUrl });
if (result.exit) {
ctx.ui.notify("Code review session closed.", "info");
} else if (result.feedback) {
if (result.approved) {
pi.sendUserMessage(
`# Code Review\n\nCode review completed — no changes requested.`,
);
} else if (isPRReview) {
// Platform PR actions (approve/comment) return approved:false with a
// status message — don't tell the agent to "address" a platform action.
pi.sendUserMessage(result.feedback);
} else {
pi.sendUserMessage(
`${result.feedback}\n\nPlease address this feedback.`,
);
}
} else {
ctx.ui.notify("Code review closed (no feedback).", "info");
}
} catch (err) {
ctx.ui.notify(
`Failed to start code review UI: ${getStartupErrorMessage(err)}`,
"error",
);
}
},
});
pi.registerCommand("plannotator-annotate", {
description: "Open markdown file or folder in annotation UI",
handler: async (args, ctx) => {
const filePath = args?.trim();
if (!filePath) {
ctx.ui.notify("Usage: /plannotator-annotate <file.md | file.html | https://... | folder/>", "error");
return;
}
if (!hasPlanBrowserHtml()) {
ctx.ui.notify(
"Annotation UI not available. Run 'bun run build' in the pi-extension directory.",
"error",
);
return;
}
let markdown: string;
let absolutePath: string;
let folderPath: string | undefined;
let mode: "annotate" | "annotate-folder" | undefined;
let sourceInfo: string | undefined;
let isFolder = false;
// --- URL annotation ---
const isUrl = /^https?:\/\//i.test(filePath);
if (isUrl) {
const useJina = resolveUseJina(false, loadConfig());
ctx.ui.notify(`Fetching: ${filePath}${useJina ? " (via Jina Reader)" : " (via fetch+Turndown)"}...`, "info");
try {
const result = await urlToMarkdown(filePath, { useJina });
markdown = result.markdown;
} catch (err) {
ctx.ui.notify(`Failed to fetch URL: ${err instanceof Error ? err.message : String(err)}`, "error");
return;
}
absolutePath = filePath;
sourceInfo = filePath;
} else {
absolutePath = resolve(ctx.cwd, filePath);
if (!existsSync(absolutePath)) {
ctx.ui.notify(`File not found: ${absolutePath}`, "error");
return;
}
try {
isFolder = statSync(absolutePath).isDirectory();
} catch {
ctx.ui.notify(`Cannot access: ${absolutePath}`, "error");
return;
}
if (isFolder) {
if (!hasMarkdownFiles(absolutePath, FILE_BROWSER_EXCLUDED, /\.(mdx?|html?)$/i)) {
ctx.ui.notify(`No markdown or HTML files found in ${absolutePath}`, "error");
return;
}
markdown = "";
folderPath = absolutePath;
mode = "annotate-folder";
ctx.ui.notify(`Opening annotation UI for folder ${filePath}...`, "info");
} else if (/\.html?$/i.test(absolutePath)) {
// HTML file annotation — convert to markdown via Turndown
const fileSize = statSync(absolutePath).size;
if (fileSize > 10 * 1024 * 1024) {
ctx.ui.notify(`File too large (${Math.round(fileSize / 1024 / 1024)}MB, max 10MB)`, "error");
return;
}
const html = readFileSync(absolutePath, "utf-8");
markdown = htmlToMarkdown(html);
sourceInfo = basename(absolutePath);
ctx.ui.notify(`Opening annotation UI for ${filePath}...`, "info");
} else {
markdown = readFileSync(absolutePath, "utf-8");
ctx.ui.notify(`Opening annotation UI for ${filePath}...`, "info");
}
}
try {
const result = await openMarkdownAnnotation(ctx, absolutePath, markdown, mode ?? "annotate", folderPath, sourceInfo);
if (result.exit) {
ctx.ui.notify("Annotation session closed.", "info");
} else if (result.feedback) {
const header = isFolder
? `# Markdown Annotations\n\nFolder: ${absolutePath}\n\n`
: `# Markdown Annotations\n\nFile: ${absolutePath}\n\n`;
pi.sendUserMessage(
`${header}${result.feedback}\n\nPlease address the annotation feedback above.`,
);
} else {
ctx.ui.notify("Annotation closed (no feedback).", "info");
}
} catch (err) {
ctx.ui.notify(
`Failed to start annotation UI: ${getStartupErrorMessage(err)}`,
"error",
);
}
},
});
pi.registerCommand("plannotator-last", {
description: "Annotate the last assistant message",
handler: async (_args, ctx) => {
if (!hasPlanBrowserHtml()) {
ctx.ui.notify(
"Annotation UI not available. Run 'bun run build' in the pi-extension directory.",
"error",
);
return;
}
const lastText = await getLastAssistantMessageText(ctx);
if (!lastText) {
ctx.ui.notify("No assistant message found in session.", "error");
return;
}
ctx.ui.notify("Opening annotation UI for last message...", "info");
try {
const result = await openLastMessageAnnotation(ctx, lastText);
if (result.exit) {
ctx.ui.notify("Annotation session closed.", "info");
} else if (result.feedback) {
pi.sendUserMessage(
`# Message Annotations\n\n${result.feedback}\n\nPlease address the annotation feedback above.`,
);
} else {
ctx.ui.notify("Annotation closed (no feedback).", "info");
}
} catch (err) {
ctx.ui.notify(
`Failed to start annotation UI: ${getStartupErrorMessage(err)}`,
"error",
);
}
},
});
pi.registerCommand("plannotator-archive", {
description: "Browse saved plan decisions",
handler: async (_args, ctx) => {
if (!hasPlanBrowserHtml()) {
ctx.ui.notify(
"Archive UI not available. Run 'bun run build' in the pi-extension directory.",
"error",
);
return;
}
ctx.ui.notify("Opening plan archive...", "info");
try {
await openArchiveBrowserAction(ctx);
ctx.ui.notify("Archive browser closed.", "info");
} catch (err) {
ctx.ui.notify(
`Failed to start archive: ${getStartupErrorMessage(err)}`,
"error",
);
}
},
});
pi.registerShortcut(Key.ctrlAlt("p"), {
description: "Toggle plannotator",
handler: async (ctx) => {
await togglePlanMode(ctx);
},
});
// ── plannotator_submit_plan Tool ────────────────────────────────────
pi.registerTool({
name: PLAN_SUBMIT_TOOL,
label: "Submit Plan",
description:
"Submit your Plannotator plan for user review. " +
"Call this only while Plannotator planning mode is active, after drafting or revising your plan file. " +
"The user will review the plan in a visual browser UI and can approve, deny with feedback, or annotate it. " +
"If denied, use the edit tool to make targeted revisions (not write), then call this again.",
parameters: Type.Object({
summary: Type.Optional(
Type.String({
description: "Brief summary of the plan for the user's review",
}),
),
}) as any,
async execute(_toolCallId, _params, _signal, _onUpdate, ctx) {
// Guard: must be in planning phase
if (phase !== "planning") {
return {
content: [
{
type: "text",
text: "Error: Not in plan mode. Use /plannotator to enter planning mode first.",
},
],
details: { approved: false },
};
}
// Read plan file
const fullPath = resolvePlanPath(ctx.cwd);
let planContent: string;
try {
planContent = readFileSync(fullPath, "utf-8");
} catch {
return {
content: [
{
type: "text",
text: `Error: ${planFilePath} does not exist. Write your plan using the write tool first, then call ${PLAN_SUBMIT_TOOL} again.`,
},
],
details: { approved: false },
};
}
if (planContent.trim().length === 0) {
return {
content: [
{
type: "text",
text: `Error: ${planFilePath} is empty. Write your plan first, then call ${PLAN_SUBMIT_TOOL} again.`,
},
],
details: { approved: false },
};
}
// Parse checklist items
checklistItems = parseChecklist(planContent);
// Non-interactive or no HTML: auto-approve
if (!ctx.hasUI || !hasPlanBrowserHtml()) {
phase = "executing";
await applyPhaseConfig(ctx, { restoreSavedState: true });
pi.appendEntry("plannotator-execute", { planFilePath });
persistState();
return {
content: [
{
type: "text",
text: "Plan auto-approved (non-interactive mode). Execute the plan now.",
},
],
details: { approved: true },
};
}
let result: Awaited<ReturnType<typeof openPlanReviewBrowser>>;
try {
result = await openPlanReviewBrowser(ctx, planContent);
} catch (err) {
const message = `Failed to start plan review UI: ${getStartupErrorMessage(err)}`;
ctx.ui.notify(message, "error");
return {
content: [{ type: "text", text: message }],
details: { approved: false },
};
}
if (result.approved) {
phase = "executing";
await applyPhaseConfig(ctx, { restoreSavedState: true });
pi.appendEntry("plannotator-execute", { planFilePath });
persistState();
const doneMsg =
checklistItems.length > 0
? `After completing each step, include [DONE:n] in your response where n is the step number.`
: "";
if (result.feedback) {
return {
content: [
{
type: "text",
text: `Plan approved with notes! You now have full tool access (read, bash, edit, write). Execute the plan in ${planFilePath}. ${doneMsg}\n\n## Implementation Notes\n\nThe user approved your plan but added the following notes to consider during implementation:\n\n${result.feedback}\n\nProceed with implementation, incorporating these notes where applicable.`,
},
],
details: { approved: true, feedback: result.feedback },
};
}
return {
content: [
{
type: "text",
text: `Plan approved. You now have full tool access (read, bash, edit, write). Execute the plan in ${planFilePath}. ${doneMsg}`,
},
],
details: { approved: true },
};
}
// Denied
const feedbackText = result.feedback || "Plan rejected. Please revise.";
return {
content: [
{
type: "text",
text: planDenyFeedback(feedbackText, PLAN_SUBMIT_TOOL, {
planFilePath,
}),
},
],
details: { approved: false, feedback: feedbackText },
};
},
});
// ── Event Handlers ───────────────────────────────────────────────────
// Gate writes during planning
pi.on("tool_call", async (event, ctx) => {
if (phase !== "planning") return;
if (event.toolName === "write") {
const targetPath = resolve(ctx.cwd, event.input.path as string);
const allowedPath = resolvePlanPath(ctx.cwd);
if (targetPath !== allowedPath) {
return {
block: true,
reason: `Plannotator: writes are restricted to ${planFilePath} during planning. Blocked: ${event.input.path}`,
};
}
}
if (event.toolName === "edit") {
const targetPath = resolve(ctx.cwd, event.input.path as string);
const allowedPath = resolvePlanPath(ctx.cwd);
if (targetPath !== allowedPath) {
return {
block: true,
reason: `Plannotator: edits are restricted to ${planFilePath} during planning. Blocked: ${event.input.path}`,
};
}
}
});
// Inject phase-specific context
pi.on("before_agent_start", async (_event, ctx) => {
const profile = getPhaseProfile();
if (phase === "executing") {
// Re-read from disk each turn to stay current
const fullPath = resolvePlanPath(ctx.cwd);
try {
const planContent = readFileSync(fullPath, "utf-8");
checklistItems = parseChecklist(planContent);
} catch {
// File deleted during execution — degrade gracefully
}
}
const todoStats = phase === "executing" ? formatTodoList(checklistItems) : formatTodoList([]);
if (profile?.systemPrompt) {
const rendered = renderTemplate(
profile.systemPrompt,
buildPromptVariables({
planFilePath,
phase,
todoList: todoStats.todoList,
completedCount: todoStats.completedCount,
totalCount: todoStats.totalCount,
remainingCount: todoStats.remainingCount,
}),
);
if (rendered.unknownVariables.length > 0) {
ctx.ui.notify(
"Plannotator: unknown template variables in " + phase + " prompt: " + rendered.unknownVariables.join(", "),
"warning",
);
}
return { systemPrompt: rendered.text };
}
if (phase === "planning") {
return {
message: {
customType: "plannotator-context",
content: `[PLANNOTATOR - PLANNING PHASE]
You are in plan mode. You MUST NOT make any changes to the codebase — no edits, no commits, no installs, no destructive commands. The ONLY file you may write to or edit is the plan file: ${planFilePath}.
Available tools: read, bash, grep, find, ls, write (${planFilePath} only), edit (${planFilePath} only), ${PLAN_SUBMIT_TOOL}
Do not run destructive bash commands (rm, git push, npm install, etc.) — focus on reading and exploring the codebase. Web fetching (curl, wget) is fine.
## Iterative Planning Workflow
You are pair-planning with the user. Explore the code to build context, then write your findings into ${planFilePath} as you go. The plan starts as a rough skeleton and gradually becomes the final plan.
### The Loop
Repeat this cycle until the plan is complete:
1. **Explore** — Use read, grep, find, ls, and bash to understand the codebase. Actively search for existing functions, utilities, and patterns that can be reused — avoid proposing new code when suitable implementations already exist.
2. **Update the plan file** — After each discovery, immediately capture what you learned in ${planFilePath}. Don't wait until the end. Use write for the initial draft, then edit for all subsequent updates.
3. **Ask the user** — When you hit an ambiguity or decision you can't resolve from code alone, ask. Then go back to step 1.
### First Turn
Start by quickly scanning key files to form an initial understanding of the task scope. Then write a skeleton plan (headers and rough notes) and ask the user your first round of questions. Don't explore exhaustively before engaging the user.
### Asking Good Questions
- Never ask what you could find out by reading the code.
- Batch related questions together.
- Focus on things only the user can answer: requirements, preferences, tradeoffs, edge-case priorities.
- Scale depth to the task — a vague feature request needs many rounds; a focused bug fix may need one or none.
### Plan File Structure
Your plan file should use markdown with clear sections:
- **Context** — Why this change is being made: the problem, what prompted it, the intended outcome.
- **Approach** — Your recommended approach only, not all alternatives considered.
- **Files to modify** — List the critical file paths that will be changed.
- **Reuse** — Reference existing functions and utilities you found, with their file paths.
- **Steps** — Implementation checklist:
- [ ] Step 1 description
- [ ] Step 2 description
- **Verification** — How to test the changes end-to-end (run the code, run tests, manual checks).
Keep the plan concise enough to scan quickly, but detailed enough to execute effectively.
### When to Submit
Your plan is ready when you've addressed all ambiguities and it covers: what to change, which files to modify, what existing code to reuse, and how to verify. Call ${PLAN_SUBMIT_TOOL} to submit for review.
### Revising After Feedback
When the user denies a plan with feedback:
1. Read ${planFilePath} to see the current plan.
2. Use the edit tool to make targeted changes addressing the feedback — do NOT rewrite the entire file.
3. Call ${PLAN_SUBMIT_TOOL} again to resubmit.
### Ending Your Turn
Your turn should only end by either:
- Asking the user a question to gather more information.
- Calling ${PLAN_SUBMIT_TOOL} when the plan is ready for review.
Do not end your turn without doing one of these two things.`,
display: false,
},
};
}
if (phase === "executing" && checklistItems.length > 0) {
// Re-read from disk each turn to stay current
const fullPath = resolvePlanPath(ctx.cwd);
let planContent = "";
try {
planContent = readFileSync(fullPath, "utf-8");
checklistItems = parseChecklist(planContent);
} catch {
// File deleted during execution — degrade gracefully
}
const remaining = checklistItems.filter((t) => !t.completed);
if (remaining.length > 0) {
const todoList = remaining
.map((t) => `- [ ] ${t.step}. ${t.text}`)
.join("\n");
return {
message: {
customType: "plannotator-context",
content: `[PLANNOTATOR - EXECUTING PLAN]
Full tool access is enabled. Execute the plan from ${planFilePath}.
Remaining steps:
${todoList}
Execute each step in order. After completing a step, include [DONE:n] in your response where n is the step number.`,
display: false,
},
};
}
}
});
// Filter stale context when idle
pi.on("context", async (event) => {
if (phase !== "idle") return;
return {
messages: event.messages.filter((m) => {
const msg = m as { customType?: string; role?: string; content?: unknown };
if (msg.customType === "plannotator-context") return false;
if (msg.role !== "user") return true;
const content = msg.content;
if (typeof content === "string") {
return !content.includes("[PLANNOTATOR -");
}
if (Array.isArray(content)) {
return !content.some(
(c) =>
c.type === "text" &&
(c as { text?: string }).text?.includes("[PLANNOTATOR -"),
);
}
return true;
}),
};
});
// Track execution progress
pi.on("turn_end", async (event, ctx) => {
if (phase !== "executing" || checklistItems.length === 0) return;
if (!isAssistantMessage(event.message as AssistantMessageLike)) return;
const text = getTextContent(event.message as { content: AssistantTextBlock[] });
if (markCompletedSteps(text, checklistItems) > 0) {
updateStatus(ctx);
updateWidget(ctx);
}
persistState();
});
// Detect execution completion
pi.on("agent_end", async (_event, ctx) => {
if (phase !== "executing" || checklistItems.length === 0) return;
if (checklistItems.every((t) => t.completed)) {
const completedList = checklistItems
.map((t) => `- [x] ~~${t.text}~~`)
.join("\n");
pi.sendMessage(
{
customType: "plannotator-complete",
content: `**Plan Complete!** ✓\n\n${completedList}`,
display: true,
},
{ triggerTurn: false },
);
phase = "idle";
checklistItems = [];
await restoreSavedState(ctx);
savedState = null;
updateStatus(ctx);
updateWidget(ctx);
persistState();
}
});
// Restore state on session start/resume
pi.on("session_start", async (_event, ctx) => {
// Resolve plan file path from flag
const flagPlanFile = pi.getFlag("plan-file") as string;
if (flagPlanFile) {
planFilePath = flagPlanFile;
}
const loadedConfig = loadPlannotatorConfig(ctx.cwd);
plannotatorConfig = loadedConfig.config;
for (const warning of loadedConfig.warnings) {
ctx.ui.notify(`Plannotator config: ${warning}`, "warning");
}
// Check --plan flag
if (pi.getFlag("plan") === true) {
phase = "planning";
}
// Restore persisted state
const entries = ctx.sessionManager.getEntries();
const stateEntry = entries
.filter(
(e: { type: string; customType?: string }) =>
e.type === "custom" && e.customType === "plannotator",
)
.pop() as { data?: PersistedPlannotatorState } | undefined;
if (stateEntry?.data) {
phase = stateEntry.data.phase ?? phase;
planFilePath = stateEntry.data.planFilePath ?? planFilePath;
savedState = stateEntry.data.savedState ?? savedState;
}
// Rebuild execution state from disk + session messages
if (phase === "executing") {
const fullPath = resolvePlanPath(ctx.cwd);
if (existsSync(fullPath)) {
const content = readFileSync(fullPath, "utf-8");
checklistItems = parseChecklist(content);
// Find last execution marker and scan messages after it for [DONE:n]
let executeIndex = -1;
for (let i = entries.length - 1; i >= 0; i--) {
const entry = entries[i] as { type: string; customType?: string };
if (entry.customType === "plannotator-execute") {
executeIndex = i;
break;
}
}
for (let i = executeIndex + 1; i < entries.length; i++) {
const entry = entries[i];
if (
entry.type === "message" &&
"message" in entry &&
isAssistantMessage(entry.message as AssistantMessageLike)
) {
const text = getTextContent(entry.message as { content: AssistantTextBlock[] });
markCompletedSteps(text, checklistItems);
}
}
} else {
// Plan file gone — fall back to idle
phase = "idle";
}
}
if (phase === "planning") {
checklistItems = [];
const warning = getPlanReviewAvailabilityWarning({ hasUI: ctx.hasUI, hasPlanHtml: hasPlanBrowserHtml() });
if (warning) {
ctx.ui.notify(warning, "warning");
}
}
if (phase === "idle") {
if (savedState) {
await restoreSavedState(ctx);
savedState = null;
} else {
// Strip planning-only tools on fresh sessions where savedState is null.
// Without this, plannotator_submit_plan stays in the active tool set
// even though plan mode hasn't been activated. See #387.
pi.setActiveTools(stripPlanningOnlyTools(pi.getActiveTools()));
}
} else if (phase === "planning" || phase === "executing") {
await applyPhaseConfig(ctx, { restoreSavedState: true });
}
updateStatus(ctx);
updateWidget(ctx);
persistState();
});
}