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
feat(bridge): dial the daemon over an authenticated socket
Shahriar7cf423e14663
diff --git a/lib/bridge/socket.ts b/lib/bridge/socket.tsnew file mode 100644index 0000000..aa786c9--- /dev/null+++ b/lib/bridge/socket.ts@@ -0,0 +1,280 @@+import { browser } from 'wxt/browser';+import { hashManifest } from '@/lib/actions/manifest';+import {+ DAEMON_PORTS,+ SOCKET_PROTOCOL_VERSION,+ failure,+ parseFrame,+ type ActionResult,+ type RunEvent,+ type SocketAuth,+ type SocketFrame,+} from '@/lib/actions/protocol';+import { describeActions } from '@/lib/actions/registry';+import { invokeForHarness } from './invoke';++export const DAEMON_STATE_KEY = 'voicelink/daemon';+/** Wakes the service worker so it can redial after Chrome has torn it down. */+export const RECONNECT_ALARM = 'voicelink/reconnect';+/** The long-lived credential. `storage.local` so a pairing outlives browser restarts. */+const SESSION_KEY_STORE = 'voicelink/sessionKey';++const BASE_DELAY_MS = 1_000;+const MAX_DELAY_MS = 30_000;++export interface DaemonState {+ connected: boolean;+ /** False until the user pairs — the extension connects to nothing by default. */+ paired: boolean;+ port?: number;+ daemonVersion?: string;+ manifestInSync?: boolean;+ error?: string;+ lastChangeAt: number;+}++let socket: WebSocket | null = null;+let attempts = 0;+let reconnectTimer: ReturnType<typeof setTimeout> | undefined;+let runListener: ((runId: string, event: RunEvent) => void) | null = null;++/** Register the single sink for agent run events. The background worker owns it. */+export function onRunEvent(listener: (runId: string, event: RunEvent) => void): void {+ runListener = listener;+}++/**+ * Ask the daemon to act on a free-text instruction. Returns the run id to correlate the+ * events that follow, or null when no daemon is attached to send it to.+ */+export function sendInstruction(text: string): string | null {+ const id = crypto.randomUUID();+ return post({ t: 'instruct', id, text }) ? id : null;+}++/** Reply must arrive before this, or the caller gets a TIMEOUT. Longer than the daemon's own. */+const ANALYZE_TIMEOUT_MS = 90_000;++const pendingAnalyses = new Map<string, (result: ActionResult<{ summary: string }>) => void>();++/**+ * Send a file to the daemon for one-shot summarization and resolve with its summary. The+ * `fileSummary` reply is correlated back by id (the extension's only request/reply path); a+ * missing daemon or a slow one resolves to a failure envelope rather than hanging forever.+ */+export function analyzeFile(file: {+ name: string;+ mime: string;+ size: number;+ content: string;+}): Promise<ActionResult<{ summary: string }>> {+ const id = crypto.randomUUID();+ if (!post({ t: 'analyzeFile', id, ...file })) {+ return Promise.resolve(failure('EXTENSION_OFFLINE', 'No VoiceLink daemon is attached — pair the browser first.'));+ }+ return new Promise((resolve) => {+ const timer = setTimeout(() => {+ pendingAnalyses.delete(id);+ resolve(failure('TIMEOUT', 'The daemon did not return a summary in time.'));+ }, ANALYZE_TIMEOUT_MS);+ pendingAnalyses.set(id, (result) => {+ clearTimeout(timer);+ resolve(result);+ });+ });+}++export function cancelRun(id: string): void {+ post({ t: 'cancel', id });+}++export function sendDecision(id: string, toolId: string, allow: boolean): void {+ post({ t: 'decision', id, toolId, allow });+}++/** Forget the agent's conversation for this browser, so the next instruction starts clean. */+export function resetConversation(): void {+ post({ t: 'reset' });+}++function post(frame: SocketFrame): boolean {+ if (!socket || socket.readyState !== WebSocket.OPEN) return false;+ send(socket, frame);+ return true;+}++/**+ * Reconnect using a stored session key. Does nothing when the browser has never been+ * paired: the connection is opt-in, so an unpaired extension never dials out at all.+ */+export async function connectDaemon(): Promise<void> {+ if (isLive()) return;+ const sessionKey = await storedSessionKey();+ if (!sessionKey) {+ await setState({ connected: false, paired: false, lastChangeAt: Date.now() });+ return;+ }+ clearTimeout(reconnectTimer);+ dial({ sessionKey }, 0);+}++/** Redeem a one-time code from `voicelink-mcp pair`. Resolves once the daemon answers. */+export async function pairDaemon(pairingToken: string): Promise<{ ok: boolean; error?: string }> {+ const code = pairingToken.replace(/[\s-]/g, '').toUpperCase();+ if (!code) return { ok: false, error: 'Enter the pairing code from "voicelink-mcp pair".' };++ disconnectSocket();+ clearTimeout(reconnectTimer);+ attempts = 0;+ return new Promise((resolve) => {+ pairingResult = resolve;+ dial({ pairingToken: code }, 0);+ // Don't leave the popup spinning if no daemon is listening on any port.+ setTimeout(() => settlePairing({ ok: false, error: 'No VoiceLink daemon is running. Start one with "voicelink-mcp pair".' }), 8_000);+ });+}++/** Forget the pairing locally and stop reconnecting. Revoking on the daemon is a CLI action. */+export async function disconnectDaemon(): Promise<void> {+ clearTimeout(reconnectTimer);+ disconnectSocket();+ await browser.storage.local.remove(SESSION_KEY_STORE);+ await setState({ connected: false, paired: false, lastChangeAt: Date.now() });+}++let pairingResult: ((result: { ok: boolean; error?: string }) => void) | null = null;++function settlePairing(result: { ok: boolean; error?: string }): void {+ pairingResult?.(result);+ pairingResult = null;+}++function isLive(): boolean {+ return !!socket && (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING);+}++function disconnectSocket(): void {+ const closing = socket;+ socket = null;+ try {+ closing?.close(1000, 'client disconnecting');+ } catch {+ // Already gone.+ }+}++function dial(auth: SocketAuth, portIndex: number): void {+ if (portIndex >= DAEMON_PORTS.length) {+ settlePairing({ ok: false, error: 'No VoiceLink daemon is running.' });+ // Only a session-key reconnect should keep retrying; a failed pairing needs a new code.+ if ('sessionKey' in auth) scheduleRetry(auth);+ return;+ }++ const port = DAEMON_PORTS[portIndex];+ const ws = new WebSocket(`ws://127.0.0.1:${port}/extension`);+ socket = ws;+ let opened = false;++ ws.onopen = () => {+ opened = true;+ attempts = 0;+ send(ws, {+ t: 'hello',+ protocolVersion: SOCKET_PROTOCOL_VERSION,+ extensionVersion: browser.runtime.getManifest().version,+ manifestHash: hashManifest(describeActions()),+ auth,+ });+ };++ ws.onmessage = (event) => {+ void handle(ws, String(event.data), port, auth);+ };++ // An error is always followed by a close, so all teardown lives in one place.+ ws.onclose = () => {+ if (socket === ws) socket = null;+ if (!opened) return dial(auth, portIndex + 1);+ void setState({ connected: false, paired: true, lastChangeAt: Date.now() });+ if ('sessionKey' in auth) scheduleRetry(auth);+ };+}++async function handle(ws: WebSocket, raw: string, port: number, auth: SocketAuth): Promise<void> {+ const frame = parseFrame(raw);+ if (!frame) return;+ switch (frame.t) {+ case 'ping':+ return send(ws, { t: 'pong', id: frame.id });++ case 'unauthorized': {+ // `retryable` means the credential was wrong but the browser may still be paired+ // (a mistyped code); otherwise the pairing is gone and the stored key is dead weight.+ if (!frame.retryable) await browser.storage.local.remove(SESSION_KEY_STORE);+ clearTimeout(reconnectTimer);+ settlePairing({ ok: false, error: frame.reason });+ await setState({+ connected: false,+ paired: frame.retryable && !('pairingToken' in auth),+ error: frame.reason,+ lastChangeAt: Date.now(),+ });+ return;+ }++ case 'welcome':+ // Present exactly once, on the connect that redeemed the pairing code.+ if (frame.sessionKey) await browser.storage.local.set({ [SESSION_KEY_STORE]: frame.sessionKey });+ settlePairing({ ok: true });+ await setState({+ connected: true,+ paired: true,+ port,+ daemonVersion: frame.daemonVersion,+ manifestInSync: frame.manifestInSync,+ lastChangeAt: Date.now(),+ });+ return;++ case 'describe':+ return send(ws, { t: 'manifest', id: frame.id, tools: describeActions() });++ case 'invoke':+ return send(ws, { t: 'result', id: frame.id, result: await invokeForHarness(frame.action, frame.input) });++ case 'run':+ return runListener?.(frame.id, frame.event);++ case 'fileSummary': {+ pendingAnalyses.get(frame.id)?.(frame.result);+ pendingAnalyses.delete(frame.id);+ return;+ }++ default:+ return;+ }+}++function send(ws: WebSocket, frame: SocketFrame): void {+ if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(frame));+}++function scheduleRetry(auth: SocketAuth): void {+ const backoff = Math.min(MAX_DELAY_MS, BASE_DELAY_MS * 2 ** attempts++);+ // Jitter so several browser windows don't retry in lockstep.+ const delay = backoff * (0.5 + Math.random() / 2);+ clearTimeout(reconnectTimer);+ reconnectTimer = setTimeout(() => dial(auth, 0), delay);+}++function setState(state: DaemonState): Promise<void> {+ return browser.storage.session.set({ [DAEMON_STATE_KEY]: state });+}++async function storedSessionKey(): Promise<string | null> {+ const stored = await browser.storage.local.get(SESSION_KEY_STORE);+ const key = stored[SESSION_KEY_STORE];+ return typeof key === 'string' && key ? key : null;+}diff --git a/lib/bridge/use-daemon-state.ts b/lib/bridge/use-daemon-state.tsnew file mode 100644index 0000000..0ea5fed--- /dev/null+++ b/lib/bridge/use-daemon-state.ts@@ -0,0 +1,26 @@+import { useEffect, useState } from 'react';+import { browser } from 'wxt/browser';+import { DAEMON_STATE_KEY, type DaemonState } from './socket';++/** Live view of the background worker's link to the MCP daemon, for extension pages. */+export function useDaemonState(): DaemonState | null {+ const [state, setState] = useState<DaemonState | null>(null);++ useEffect(() => {+ let current = true;+ void browser.storage.session+ .get(DAEMON_STATE_KEY)+ .then((stored) => current && setState((stored[DAEMON_STATE_KEY] as DaemonState) ?? null));++ const listener = (changes: Record<string, { newValue?: unknown }>) => {+ if (DAEMON_STATE_KEY in changes) setState((changes[DAEMON_STATE_KEY].newValue as DaemonState) ?? null);+ };+ browser.storage.session.onChanged.addListener(listener);+ return () => {+ current = false;+ browser.storage.session.onChanged.removeListener(listener);+ };+ }, []);++ return state;+}