import type { SnapshotDiffResult } from "../../diff/snapshots.ts"; import { isProgress, type PiToolShell, type ToolContext } from "../../types.ts"; import { toolErrorLabel, toolExpandHint, toolFreshnessLabel } from "../tool-labels.ts"; import { toolProgressView } from "../tool-progress.ts"; import { toolStatus } from "../tool-status.ts"; import { muted as toolMuted, renderText as toolText } from "../tui.ts"; import type { RenderComponent, RenderTheme } from "../types.ts"; export function renderWebDiffResult( result: PiToolShell, expanded = false, theme?: RenderTheme, ): RenderComponent { const envelope = result.details as Partial>>; if (isProgress(envelope)) return toolProgressView("web_scrape diff", envelope, theme, { allowIcons: false }); const diff = envelope.data; const diffState = !diff?.previous ? "saved baseline" : envelope.summary?.includes("No meaningful") || envelope.summary?.includes("No content") ? "no content changes" : `changed: ${diff.diff?.changedCount ?? 0} changed, ${diff.diff?.addedCount ?? 0} added, ${diff.diff?.removedCount ?? 0} removed`; const title = envelope.error ? toolErrorLabel("web_scrape", envelope.error, { allowIcons: false }) : toolStatus([diffState, toolFreshnessLabel(envelope)], theme); if (!expanded) return toolText(toolStatus([title, toolMuted(toolExpandHint.text, theme)], theme), { padToWidth: true, }); const lines = [title]; if (diff) lines.push( "fetched current page", "loaded previous snapshot", "compared normalized content", "saved snapshot", ); const preview = envelope.answerContext ?? result.content[0]?.text; if (preview) lines.push("", preview.slice(0, 500)); if (envelope.responseId) lines.push("", toolMuted(`responseId: ${envelope.responseId}`, theme)); return toolText(lines.join("\n"), { padToWidth: true }); }