An agentic browser harness: an AI side panel & orb in your own logged-in Chrome, Edge, Arc, Brave or even Firefox run by the agent/subscription you already use (Claude Code, Codex, Antigravity). Free, open source, no API key. Also an MCP server.
TypeScript agentic-browseragentic-browsingagy-cliaiai-agentai-browser-extensionai-toolsbrave-extensionbrowser-agentbrowser-automationbrowser-extensionbrowser-usebrowserskillchrome-extensionclaude-codecodexfirefox-addonmcp-servernanobrowserweb-automation
refactor(bridge): move the run timeline reducer into its own module
Shahriard02e6146cacc
diff --git a/components/run-timeline.tsx b/components/run-timeline.tsxindex 1d1dffe..b528813 100644--- a/components/run-timeline.tsx+++ b/components/run-timeline.tsx@@ -28,7 +28,7 @@ import {import { Markdown } from '@/components/markdown';import { Button } from '@/components/ui/button';import { openScreenshot } from '@/lib/bridge/screenshot-preview';-import type { RunItem } from '@/lib/bridge/use-run';+import type { RunItem } from '@/lib/bridge/run-items';import { cn } from '@/lib/utils';interface RunTimelineProps {diff --git a/lib/bridge/run-items.ts b/lib/bridge/run-items.tsnew file mode 100644index 0000000..dd30ad2--- /dev/null+++ b/lib/bridge/run-items.ts@@ -0,0 +1,119 @@+import { screenshot } from '@/lib/actions/page/screenshot';+import type { RunEvent } from '@/lib/actions/protocol';+import type { MonitorState } from '@/lib/monitor/events';+import { redactInput } from './redact';+import type { ScreenshotPreview } from './screenshot-preview';++export type RunItem =+ | { kind: 'user'; id: string; text: string }+ | { kind: 'assistant'; id: string; text: string }+ | { kind: 'notice'; id: string; tone: 'info' | 'error'; text: string }+ | {+ kind: 'tool';+ id: string;+ action: string;+ input: unknown;+ summary?: string;+ ok?: boolean;+ awaiting?: boolean;+ site?: string;+ source?: 'local' | 'external';+ preview?: ScreenshotPreview;+ };++export const nextId = () => crypto.randomUUID();++export function reduce(items: RunItem[], event: RunEvent): RunItem[] {+ switch (event.kind) {+ case 'started': {+ const overlays = event.overlays?.length ? ` + site notes: ${event.overlays.join(', ')}` : '';+ return [+ ...items,+ { kind: 'notice', id: nextId(), tone: 'info', text: `Loaded skill: ${event.skill}${overlays}` },+ ];+ }++ case 'text': {+ const last = items.at(-1);+ if (last?.kind === 'assistant') {+ return [...items.slice(0, -1), { ...last, text: last.text + event.delta }];+ }+ return [...items, { kind: 'assistant', id: nextId(), text: event.delta }];+ }++ case 'tool':+ return [+ ...items,+ {+ kind: 'tool',+ id: event.toolId,+ action: event.action,+ input: redactInput(event.action, event.input),+ source: event.source,+ },+ ];++ case 'approval':+ return patchTool(items, event.toolId, { awaiting: true, site: event.site });++ case 'toolResult':+ return patchTool(items, event.toolId, { ok: event.ok, summary: event.summary, awaiting: false });++ case 'session':+ return items;++ case 'done':+ return event.stopReason === 'end_turn'+ ? items+ : [...items, { kind: 'notice', id: nextId(), tone: 'info', text: `Stopped: ${event.stopReason}` }];++ case 'error':+ return [...items, { kind: 'notice', id: nextId(), tone: 'error', text: `${event.code}: ${event.message}` }];+ }+}++export function patchTool(+ items: RunItem[],+ toolId: string,+ patch: Partial<Extract<RunItem, { kind: 'tool' }>>,+): RunItem[] {+ return items.map((item) => (item.kind === 'tool' && item.id === toolId ? { ...item, ...patch } : item));+}++export function attachPreview(items: RunItem[], preview: ScreenshotPreview): RunItem[] {+ for (let i = items.length - 1; i >= 0; i--) {+ const item = items[i];+ if (item.kind === 'tool' && item.action === screenshot.name && !item.preview) {+ const next = [...items];+ next[i] = { ...item, preview };+ return next;+ }+ }+ return items;+}++export function notice(tone: 'info' | 'error', text: string): RunItem {+ return { kind: 'notice', id: nextId(), tone, text };+}++export function monitorNotice(state: MonitorState): { tone: 'info' | 'error'; text: string } {+ const name = state.label ?? state.host;+ const elapsed = clock(state.elapsedMs);+ switch (state.phase) {+ case 'done':+ return { tone: 'info', text: `Monitor “${name}” finished after ${elapsed}.` };+ case 'stopped':+ return { tone: 'info', text: `Monitor “${name}” stopped.` };+ case 'timeout':+ return { tone: 'error', text: `Monitor “${name}” gave up after ${elapsed} without completing.` };+ case 'tab-closed':+ return { tone: 'error', text: `Monitor “${name}” ended — the tab was closed.` };+ default:+ return { tone: 'error', text: `Monitor “${name}” ended — ${state.message ?? 'the tab left the watched site.'}` };+ }+}++function clock(ms: number): string {+ const total = Math.floor(ms / 1000);+ return `${Math.floor(total / 60)}:${String(total % 60).padStart(2, '0')}`;+}diff --git a/lib/bridge/session-store.ts b/lib/bridge/session-store.tsindex b87b3b8..5db5f4c 100644--- a/lib/bridge/session-store.ts+++ b/lib/bridge/session-store.ts@@ -1,6 +1,6 @@import { browser } from 'wxt/browser';import type { AgentKind } from '@/lib/agents/catalog';-import type { RunItem } from './use-run';+import type { RunItem } from './run-items';import { nameSession } from './socket';export const SESSIONS_INDEX_KEY = 'browsentic:sessions';