4 Message Protocol
akib-cdda edited this page 2026-11-11 13:23:37 +06:00

Message Protocol

Every runtime message that travels between the editor, the background worker, content scripts and the offscreen document, plus the non-message channels (storage, executeScript calls into page globals) that carry the rest. Read this when you add a message or need to know who answers what.

Source: messages.ts, and the senders/receivers: background.ts, picking.ts, htmlParse.ts, offscreen/main.ts, picker.content.ts, recorder.content.ts, Editor.tsx, ConfigPanel.tsx, PromptBanner.tsx

Design in one paragraph

Messages are commands and one-shot replies only. Nothing the user needs to see later travels as a message: run progress, logs, prompts and recordings go through storage.local (the editor subscribes with storage.onChanged), and starting/stopping workflows is done by saving bf:workflows. That keeps the editor working even when the worker was asleep or restarted. All messages use browser.runtime.sendMessage (a broadcast to every extension page and the worker; content scripts send this way too). There are no tabs.sendMessage, ports or window.postMessage channels between our own contexts; the one port is the native messaging port to the BlockFlow bridge (below).

Typed messages (ExtensionMessage)

isExtensionMessage(value) accepts an object whose type is in the TYPES set; it does not validate the rest. Every listener calls it first.

type Interface Sender Receiver Payload Response
bf:start-pick StartPickMessage Editor (ConfigPanel.startPick) Background → startPicking(sender.tab.id, url, mode, urlFrom) url?: string page to pick on; urlFrom?: 'block' | 'upstream' (the block's own URL, or the nearest Open Tab's); mode?: 'watch' | 'action' | 'form' none; later bf:pick-result or bf:pick-error
bf:picked PickedMessage Picker content script (onPick) Background → finishPicking(msg, sender.tab.id) selector, candidates?: string[], url, sample none
bf:pick-cancelled PickCancelledMessage Picker content script (onCancel, Esc) Background → cancelPicking() none none (the editor is not told)
bf:pick-result PickResultMessage Background (finishPicking) Editor (ConfigPanel listener) selector, candidates? (best first), url, sample, fields?: FormFieldRow[] (form mode) n/a
bf:pick-error PickErrorMessage Background (startPicking, finishPicking) Editor (ConfigPanel) message n/a
bf:run-workflow RunWorkflowMessage Editor RunControls (Run, Run with trace, Debug), ConfigPanel "Run from here" Background → newRunner(workflow).runOnce(options) workflow: Workflow (the canvas as is, saved or not), options?: RunOptions (trace, debug: {pauseOnError?}, startAt: {nodeId, input}) none; progress via bf:runs
bf:stop-run StopRunMessage Editor Stop button Background → cancelRun: cancelRequests.add, answerPrompt(runId, false), answerDebug(runId, 'stop') runId none
bf:answer-prompt AnswerPromptMessage Editor PromptBanner Background → answerPrompt(runId, ok) runId, ok (true = Continue) none
bf:debug-answer DebugAnswerMessage Editor DebugBanner Background → answerDebug(runId, action); stop → cancelRun(runId) runId, action: 'continue' | 'step' | 'stop' none; the pause leaves storage.session bf:debug-pause
bf:accept-baseline AcceptBaselineMessage Editor VisualComparePanel (Accept new baseline) Background → acceptBaseline(comparisonId) (src/browser/visual.ts) comparisonId broadcast bf:baseline-accepted
bf:baseline-accepted BaselineAcceptedMessage Background (broadcast) Editor VisualComparePanel comparisonId, error? n/a
bf:run-suite RunSuiteMessage Editor SuitesPage (▶ Run suite, ↻ Re-run failed) Background → runSuite({suite, only: workflowIds}) suiteId, workflowIds?: string[] (M5.5: only these workflows of the suite) none; bf:suite-progress per workflow, then bf:suite-result
bf:suite-progress / bf:suite-result SuiteProgressMessage / SuiteResultMessage Background (broadcast) Editor SuitesPage suiteId + index, total, workflowName, status / result? or error? n/a
bf:start-recording StartRecordingMessage Editor Record button Background → startRecording(workflowId, url) workflowId, url none; state via bf:recording
bf:stop-recording StopRecordingMessage Editor (Stop in the recording bar) or recorder toolbar in the page (onStop) Background → stopRecording() none none
bf:recorded RecordedMessage Recorder content script (onStep, any frame; locators already frame=…-prefixed) Background → recordStep(step, sender.tab.id) step: RecordedStep none
bf:picker-sync PickerSyncMessage Picker content script, any frame Background → relayPickerSync: tabs.sendMessage to every frame of the sender's tab sync: PickerSync (locked / state / command) none
bf:recorder-sync RecorderSyncMessage Recorder content script, any frame Background → relayRecorderSync: to every frame of the sender's tab mode: 'record' | 'assert-text' | 'assert-visible' none
bf:snapshot-page SnapshotPageMessage Editor (SnapshotButton, 📸 Snapshot page on Open Tab) Background → snapshotForEditor(sender.tab.id, url) in picking.ts: opens or reuses the editor's picker tab like bf:start-pick (or the last-used web tab without a URL), runs the snapshot page op, focuses the editor again url?: string (the block's URL, when it has no {{…}}) none; later bf:snapshot-result
bf:snapshot-result SnapshotResultMessage Background (snapshotForEditor) Editor (SnapshotButton listener; acts only while it is waiting) text? (snapshotDocument(result): title, URL, outline), lines?, truncated?, or error? n/a

Notes:

  • The background's onMessage listener never returns true or calls sendResponse; every typed message is fire-and-forget. Senders .catch the promise only to detect "worker unreachable" (the editor shows "Could not reach the background worker").
  • sender.tab?.id matters: it identifies the editor tab for bf:start-pick (to return to it later), the picked tab for bf:picked (form detection runs there), and filters bf:recorded to the recording tab and its popups.
  • bf:pick-result / bf:pick-error are broadcasts: every open editor tab's ConfigPanel receives them, but only one with a pending pick (pending.current) acts.

Ad-hoc message (not in messages.ts)

type Sender Receiver Payload Response
bf:parse-html Background (actOnHtml, Chrome only) Offscreen document (offscreen/main.ts) html: string, baseUrl: string, request: AgentRequest {ok: true, result: Record<string, unknown>} or {ok: false, error: string}; listener returns true (async)

It is deliberately outside ExtensionMessage: the background and editor ignore it because isExtensionMessage returns false, and the offscreen document ignores everything else.

Flows

Pick:    editor ──bf:start-pick──▶ bg ──executeScript(picker.js)──▶ page
         page ──bf:picked / bf:pick-cancelled──▶ bg ──bf:pick-result / bf:pick-error──▶ editor
Snapshot: editor ──bf:snapshot-page──▶ bg ──agent snapshot op──▶ page; bg ──bf:snapshot-result──▶ editor (clipboard)
Run:     editor ──bf:run-workflow──▶ bg          bg ──storage bf:runs, bf:logs──▶ editor
Stop:    editor ──bf:stop-run──▶ bg
Prompt:  bg ──storage bf:prompts + notification──▶ user/editor ──bf:answer-prompt / notification click──▶ bg
Debug:   bg ──storage.session bf:debug-pause──▶ editor (DebugBanner) ──bf:debug-answer / bf:stop-run──▶ bg
Visual:  editor (VisualComparePanel) ──bf:accept-baseline──▶ bg (IndexedDB blockflow-visual) ──bf:baseline-accepted──▶ editor
Record:  editor ──bf:start-recording──▶ bg ──executeScript(recorder.js)──▶ page
         page ──bf:recorded / bf:stop-recording──▶ bg ──storage bf:recording──▶ editor
Parse:   bg ──bf:parse-html──▶ offscreen ──sendResponse──▶ bg
Bridge:  program ──socket──▶ native host ──native port──▶ bg ──JSON-RPC response + run.* notifications──▶ host ──▶ program
         Settings page ──storage bf:settings──▶ bg (connect / bridge.disabled + disconnect)

Non-message channels

Channel Between Used for
storage.local + storage.onChanged worker ↔ editor workflows (editor → worker), runs, logs, prompts, recording, secrets. See Storage Keys.
scripting.executeScript({func}) calling globalThis.__blockflowAgent.run(call) worker → page agent every page block (Page Protocol, Page Agent)
executeScript({files}) worker → page injecting agent.js, picker.js, recorder.js (content scripts with registration: 'runtime')
executeScript calling __blockflowRecorder.stop() worker → page ending a recording
executeScript({world: 'MAIN'}) on window.__blockflowDialogs worker → page main world Dialogs block
userScripts.execute worker → page Run Script
chrome.debugger.sendCommand worker → tab real input, background screenshots (Browser Host)
native messaging port (runtime.connectNative('dev.akibh.blockflow')) worker ↔ native host M4 bridge, JSON-RPC 2.0 (below)
storage.session bf:bridge-status worker → editor the bridge connection state on the Settings page
notifications (bf-prompt:<runId>) worker ↔ user Manual Step buttons/clicks
context menu items (bf-menu::<workflowId>::<nodeId>) user → worker Context Menu trigger

Native port (the bridge, M4)

While "Allow outside control" (bf:settings) is on, the worker holds one native messaging port to the BlockFlow native host (Bridge). Messages are the JSON-RPC 2.0 objects of src/core/bridgeProtocol.ts, framed by the browser (4-byte length + JSON, at most MAX_MESSAGE_BYTES):

Direction Message Notes
host → worker request {jsonrpc, id, method, params} for every method in BridgeParams except auth id is rewritten by the host, so concurrent clients never collide. Anything without method + id is ignored.
worker → host response {jsonrpc, id, result} or {jsonrpc, id, error: {code, message, data?}} Codes: RPC_ERRORS (disabled while outside control is off, invalidWorkflow with data.issues, notFound, invalidParams, version, internal with data.code).
worker → host notifications run.started, run.step, suite.progress {requestId, …} Sent before the response; the host routes them by requestId to the client that asked.
worker → host bridge.disabled {} Just before the worker disconnects because the setting was switched off; the host fails pending requests and closes its clients.

The methods are described in Browser Host: bridge.ts. No editor ↔ worker message was added for the bridge: the Settings page writes bf:settings and reads bf:bridge-status and bf:bridge-log from storage.

Adding a message

  1. Add an interface to messages.ts, add it to ExtensionMessage, and add its type to TYPES.
  2. Handle it in the receiver's switch / listener (background: runtime.onMessage in background.ts).
  3. Prefer storage for anything long-lived or that the editor must still see after a reload.
  4. Keep payloads JSON-serialisable (structured clone).

See also