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): sync uploaded skills from extension storage to the daemon
Shahriar0dbc4cf6ffd9
diff --git a/entrypoints/background.ts b/entrypoints/background.tsindex bb6c682..1a4292c 100644--- a/entrypoints/background.ts+++ b/entrypoints/background.ts@@ -3,8 +3,9 @@ import { describeActions } from '@/lib/actions/registry';import { injectContentScript } from '@/lib/actions/client';import { invokeForHarness } from '@/lib/bridge/invoke';import { analyzeStoredFile } from '@/lib/bridge/file-store';+import { pushSkill, removeSkill, resyncSkills } from '@/lib/bridge/skill-store';import { serveRunPorts } from '@/lib/bridge/run-port';-import { RECONNECT_ALARM, connectDaemon, disconnectDaemon, pairDaemon } from '@/lib/bridge/socket';+import { RECONNECT_ALARM, connectDaemon, disconnectDaemon, onWelcome, pairDaemon } from '@/lib/bridge/socket';export default defineBackground(() => {// Action bridge for extension pages (the side panel).@@ -30,6 +31,19 @@ export default defineBackground(() => {sendResponse(success(true));return;}+ // Push an uploaded skill to the daemon's skills directory, or take it back off. Same+ // fire-and-forget shape as analyzeFile — the worker reads the body from storage itself and+ // writes the sync status back to the index, so the panel need not stay open for it.+ if (message.op === 'saveSkill' && typeof message.skillId === 'string') {+ void pushSkill(message.skillId);+ sendResponse(success(true));+ return;+ }+ if (message.op === 'removeSkill' && typeof message.skillId === 'string') {+ void removeSkill(message.skillId);+ sendResponse(success(true));+ return;+ }// Pairing is driven from the popup: the worker owns the socket, the UI just asks.if (message.op === 'pair' && typeof message.token === 'string') {pairDaemon(message.token)@@ -45,7 +59,12 @@ export default defineBackground(() => {.catch((error) => sendResponse(failure('BRIDGE_ERROR', String(error))));return true;}- sendResponse(failure('INVALID_REQUEST', 'Expected {op:"describe"|"invoke"|"pair"|"disconnect"}'));+ sendResponse(+ failure(+ 'INVALID_REQUEST',+ 'Expected {op:"describe"|"invoke"|"analyzeFile"|"saveSkill"|"removeSkill"|"pair"|"disconnect"}',+ ),+ );return;});@@ -65,6 +84,10 @@ export default defineBackground(() => {// A port rather than sendMessage, because assistant text streams in token by token.serveRunPorts();+ // A skill uploaded while the daemon was down never reached disk. Push those, and only those,+ // once it is back — re-pushing everything would rewrite every file on every reconnect.+ onWelcome(() => void resyncSkills());+// The same actions, reached from outside the browser by the local MCP daemon — but only// once the user has paired this browser. An unpaired extension never dials out.void connectDaemon();diff --git a/lib/bridge/skill-store.ts b/lib/bridge/skill-store.tsnew file mode 100644index 0000000..b167bd3--- /dev/null+++ b/lib/bridge/skill-store.ts@@ -0,0 +1,168 @@+import { browser } from 'wxt/browser';+import type { SkillCategory, SkillDraft } from '@/lib/skills/format';+import { deleteSiteMap, deleteSkill, saveSkill } from './socket';++/**+ * The extension's library of uploaded skills, kept in `browser.storage.local`. This is the+ * durable copy: the daemon's `~/voicelink/skills/*.md` is a projection of it, rewritten+ * whenever a record has not reached disk yet, so an upload made while the daemon was down+ * still lands, and a wiped state directory can be refilled.+ *+ * Split into two shapes, like the file store, so patching a sync status never rewrites the+ * body — the index is what fans out to every open panel through `storage.onChanged`.+ *+ * 'voicelink:skills' -> StoredSkillMeta[] the small, reactive index the UI reads+ * 'voicelink:skill:<id>' -> StoredSkillBody the markdown, read only when pushing+ */++/** The reactive metadata index — never holds skill bodies. */+export const SKILLS_INDEX_KEY = 'voicelink:skills';++const skillBodyKey = (id: string) => `voicelink:skill:${id}`;++/** Whether this record has reached the daemon's disk yet. */+export type SkillSyncStatus = 'pending' | 'saved' | 'error';++export interface StoredSkillMeta {+ id: string;+ name: string;+ description: string;+ category: SkillCategory;+ domains: string[];+ status: SkillSyncStatus;+ /** Where the daemon wrote it — the only ground truth about which HOME it landed in. */+ path?: string;+ /** Present when the daemon refused it. */+ error?: string;+ addedAt: number;+ /**+ * `generated` marks a site map: the daemon wrote it, its body lives on disk rather than here,+ * and it is removed by its own op. Absent on records written before mapping existed, which are+ * all uploads.+ */+ origin?: 'upload' | 'generated';+ /** Pages a map covers. Display only. */+ pages?: number;+}++export interface StoredSkillBody {+ id: string;+ body: string;+ triggers: string[];+}++export async function listSkillMeta(): Promise<StoredSkillMeta[]> {+ const stored = await browser.storage.local.get(SKILLS_INDEX_KEY);+ const list = stored[SKILLS_INDEX_KEY];+ return Array.isArray(list) ? (list as StoredSkillMeta[]) : [];+}++async function writeIndex(list: StoredSkillMeta[]): Promise<void> {+ await browser.storage.local.set({ [SKILLS_INDEX_KEY]: list });+}++/** Store a skill: its body under its own key, its metadata at the head of the index. */+export async function putSkill(meta: StoredSkillMeta, body: StoredSkillBody): Promise<void> {+ await browser.storage.local.set({ [skillBodyKey(meta.id)]: body });+ const list = await listSkillMeta();+ // Replace by name as well as id: two uploads of the same name are one file on disk, so the+ // older record has to go — along with its body, which nothing would reach again.+ const superseded = list.filter((skill) => skill.id !== meta.id && skill.name === meta.name);+ if (superseded.length) await browser.storage.local.remove(superseded.map((skill) => skillBodyKey(skill.id)));+ await writeIndex([meta, ...list.filter((skill) => skill.id !== meta.id && skill.name !== meta.name)]);+}++export async function readSkillBody(id: string): Promise<StoredSkillBody | null> {+ const key = skillBodyKey(id);+ const stored = await browser.storage.local.get(key);+ const body = stored[key] as StoredSkillBody | undefined;+ return body && typeof body.body === 'string' ? body : null;+}++export async function updateSkillMeta(id: string, patch: Partial<StoredSkillMeta>): Promise<void> {+ const list = await listSkillMeta();+ await writeIndex(list.map((skill) => (skill.id === id ? { ...skill, ...patch } : skill)));+}++/**+ * Push one stored skill to the daemon and fold the answer back into the index. Called from the+ * background worker (it owns the socket), so the result lands even if the panel that asked has+ * closed — the same fire-and-forget shape as analyzing a file.+ */+export async function pushSkill(id: string): Promise<void> {+ const meta = (await listSkillMeta()).find((skill) => skill.id === id);+ // A generated map has no body here — the daemon wrote it, and this index is only a pointer.+ if (meta?.origin === 'generated') return;+ const stored = await readSkillBody(id);+ if (!meta || !stored) {+ await updateSkillMeta(id, { status: 'error', error: 'The skill is missing from storage.' });+ return;+ }+ const draft: SkillDraft = {+ name: meta.name,+ description: meta.description,+ category: meta.category,+ domains: meta.domains,+ triggers: stored.triggers,+ body: stored.body,+ };+ const result = await saveSkill(draft);+ if (result.ok) {+ await updateSkillMeta(id, { status: 'saved', path: result.data.path, error: undefined });+ } else {+ await updateSkillMeta(id, { status: 'error', error: `${result.error.code}: ${result.error.message}` });+ }+}++/**+ * Remove a skill here and on the daemon. The local record goes either way: a user who removes+ * a skill while the daemon is down should not find it back in the list, and a file left behind+ * is visible in `voicelink-mcp skills`.+ */+export async function removeSkill(id: string): Promise<void> {+ const meta = (await listSkillMeta()).find((skill) => skill.id === id);+ // Two different objects on disk, so two different ops: `deleteSkill` removes a flat `.md` and+ // a map's directory survives it, which is what stops removing an upload from taking a+ // same-named map's screenshots down with it.+ if (meta) await (meta.origin === 'generated' ? deleteSiteMap(meta.name) : deleteSkill(meta.name));+ await browser.storage.local.remove(skillBodyKey(id));+ const list = await listSkillMeta();+ await writeIndex(list.filter((skill) => skill.id !== id));+}++/**+ * Note a map the daemon just activated, so it shows in the panel beside uploads. Metadata only —+ * the body is on disk and is never mirrored here, so nothing can re-push and overwrite it.+ */+export async function recordGeneratedSkill(draft: {+ name: string;+ domain: string;+ directory: string;+ pages: number;+ generatedAt: string;+}): Promise<void> {+ const list = await listSkillMeta();+ const meta: StoredSkillMeta = {+ id: crypto.randomUUID(),+ name: draft.name,+ description: `Mapped ${draft.domain} — ${draft.pages} pages.`,+ category: 'site-exploration',+ domains: [draft.domain],+ status: 'saved',+ path: draft.directory,+ addedAt: Date.parse(draft.generatedAt) || Date.now(),+ origin: 'generated',+ pages: draft.pages,+ };+ await writeIndex([meta, ...list.filter((skill) => skill.name !== draft.name)]);+}++/**+ * On reconnect, push only what has not reached disk. Re-pushing everything would rewrite every+ * file on every reconnect — and the worker redials on a one-minute alarm.+ */+export async function resyncSkills(): Promise<void> {+ for (const skill of await listSkillMeta()) {+ if (skill.status !== 'saved' && skill.origin !== 'generated') await pushSkill(skill.id);+ }+}diff --git a/lib/bridge/socket.ts b/lib/bridge/socket.tsindex aa786c9..e526a75 100644--- a/lib/bridge/socket.ts+++ b/lib/bridge/socket.ts@@ -6,11 +6,15 @@ import {failure,parseFrame,type ActionResult,+ type RunContext,type RunEvent,+ type SavedSkill,type SocketAuth,type SocketFrame,} from '@/lib/actions/protocol';import { describeActions } from '@/lib/actions/registry';+import type { SkillDraft } from '@/lib/skills/format';+import type { SiteMapDraft } from '@/lib/skills/site-map';import { invokeForHarness } from './invoke';export const DAEMON_STATE_KEY = 'voicelink/daemon';@@ -37,6 +41,13 @@ let socket: WebSocket | null = null;let attempts = 0;let reconnectTimer: ReturnType<typeof setTimeout> | undefined;let runListener: ((runId: string, event: RunEvent) => void) | null = null;+let welcomeListener: (() => void) | null = null;+let draftListener: ((runId: string, draft: SiteMapDraft) => void) | null = null;++/** Register the sink for a staged site map awaiting review. The background worker owns it. */+export function onSiteMapDraft(listener: (runId: string, draft: SiteMapDraft) => void): void {+ draftListener = listener;+}/** Register the single sink for agent run events. The background worker owns it. */export function onRunEvent(listener: (runId: string, event: RunEvent) => void): void {@@ -44,12 +55,22 @@ export function onRunEvent(listener: (runId: string, event: RunEvent) => void):}/**- * 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.+ * Called on every accepted connection, so the worker can push anything that queued up while+ * the daemon was away. A listener rather than a direct call because the things that need to+ * re-sync already import from this module, and the arrow has to stay one-way.*/-export function sendInstruction(text: string): string | null {+export function onWelcome(listener: () => void): void {+ welcomeListener = listener;+}++/**+ * Ask the daemon to act on a free-text instruction. `context` names the tab it was typed+ * against, which is what decides whether a site-exploration skill applies. 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, context?: RunContext): string | null {const id = crypto.randomUUID();- return post({ t: 'instruct', id, text }) ? id : null;+ return post({ t: 'instruct', id, text, context }) ? id : null;}/** Reply must arrive before this, or the caller gets a TIMEOUT. Longer than the daemon's own. */@@ -84,6 +105,56 @@ export function analyzeFile(file: {});}+/** The daemon answers these synchronously, so a slow reply means something is wrong. */+const SKILL_TIMEOUT_MS = 15_000;++const pendingSkillOps = new Map<string, (result: ActionResult<SavedSkill>) => void>();++/** Write an uploaded skill into the daemon's skills directory, replacing one of the same name. */+export function saveSkill(skill: SkillDraft): Promise<ActionResult<SavedSkill>> {+ return skillOp({ t: 'saveSkill', id: crypto.randomUUID(), skill });+}++/** Take an uploaded skill back off disk. Unknown names succeed — the file is gone either way. */+export function deleteSkill(name: string): Promise<ActionResult<SavedSkill>> {+ return skillOp({ t: 'deleteSkill', id: crypto.randomUUID(), name });+}++/** Remove a generated map's whole directory. Deliberately not `deleteSkill`: different objects. */+export function deleteSiteMap(name: string): Promise<ActionResult<SavedSkill>> {+ return skillOp({ t: 'deleteSiteMap', id: crypto.randomUUID(), name });+}++/** Arm a staged map. `exactHost` narrows it from the domain to the precise host that was mapped. */+export function activateSiteMap(stagingId: string, exactHost = false): Promise<ActionResult<SavedSkill>> {+ return skillOp({ t: 'activateSiteMap', id: crypto.randomUUID(), stagingId, exactHost });+}++export function discardSiteMap(stagingId: string): Promise<ActionResult<SavedSkill>> {+ return skillOp({ t: 'discardSiteMap', id: crypto.randomUUID(), stagingId });+}++function skillOp(+ frame: Extract<+ SocketFrame,+ { t: 'saveSkill' | 'deleteSkill' | 'deleteSiteMap' | 'activateSiteMap' | 'discardSiteMap' }+ >,+): Promise<ActionResult<SavedSkill>> {+ if (!post(frame)) {+ return Promise.resolve(failure('EXTENSION_OFFLINE', 'No VoiceLink daemon is attached — pair the browser first.'));+ }+ return new Promise((resolve) => {+ const timer = setTimeout(() => {+ pendingSkillOps.delete(frame.id);+ resolve(failure('TIMEOUT', 'The daemon did not answer in time.'));+ }, SKILL_TIMEOUT_MS);+ pendingSkillOps.set(frame.id, (result) => {+ clearTimeout(timer);+ resolve(result);+ });+ });+}+export function cancelRun(id: string): void {post({ t: 'cancel', id });}@@ -235,13 +306,19 @@ async function handle(ws: WebSocket, raw: string, port: number, auth: SocketAuthmanifestInSync: frame.manifestInSync,lastChangeAt: Date.now(),});+ // The daemon is reachable again; anything that failed to reach it can go now.+ welcomeListener?.();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) });+ return send(ws, {+ t: 'result',+ id: frame.id,+ result: await invokeForHarness(frame.action, frame.input, frame.tabId),+ });case 'run':return runListener?.(frame.id, frame.event);@@ -252,6 +329,15 @@ async function handle(ws: WebSocket, raw: string, port: number, auth: SocketAuthreturn;}+ case 'skillResult': {+ pendingSkillOps.get(frame.id)?.(frame.result);+ pendingSkillOps.delete(frame.id);+ return;+ }++ case 'siteMapDraft':+ return draftListener?.(frame.id, frame.draft);+default:return;}diff --git a/lib/bridge/use-stored-skills.ts b/lib/bridge/use-stored-skills.tsnew file mode 100644index 0000000..53d3868--- /dev/null+++ b/lib/bridge/use-stored-skills.ts@@ -0,0 +1,33 @@+import { useEffect, useState } from 'react';+import { browser } from 'wxt/browser';+import { SKILLS_INDEX_KEY, type StoredSkillMeta } from './skill-store';++/**+ * The uploaded-skill index, kept live through `storage.local.onChanged` so it stays in sync+ * across extension pages and re-renders pending→saved as the background pushes each one to+ * the daemon.+ */+export function useStoredSkills(): StoredSkillMeta[] {+ const [skills, setSkills] = useState<StoredSkillMeta[]>([]);++ useEffect(() => {+ let live = true;+ void browser.storage.local.get(SKILLS_INDEX_KEY).then((stored) => {+ const list = stored[SKILLS_INDEX_KEY];+ if (live && Array.isArray(list)) setSkills(list as StoredSkillMeta[]);+ });+ const listener = (changes: Record<string, { newValue?: unknown }>) => {+ if (SKILLS_INDEX_KEY in changes) {+ const next = changes[SKILLS_INDEX_KEY].newValue;+ setSkills(Array.isArray(next) ? (next as StoredSkillMeta[]) : []);+ }+ };+ browser.storage.local.onChanged.addListener(listener);+ return () => {+ live = false;+ browser.storage.local.onChanged.removeListener(listener);+ };+ }, []);++ return skills;+}