@digital-gravy/etch-connector

中文文档
0.5.3发布于 2026/11/10SEE LICENSE IN LICENSEES Module近 30 天下载 2328

Local bridge that lets an AI harness drive a live Etch tab by evaluating scripts against the etch-public-api over a WebSocket.

原始使用文档
文档目录14 节
  1. Status
  2. Install & run
  3. Use
  4. 1. Start the daemon (once)
  5. 2. Connect a tab
  6. 3. Drive it
  7. Output contract (for agents)
  8. For AI agents
  9. CLI
  10. Assets
  11. HTTP control API
  12. Architecture
  13. Going to MCP later
  14. Security

etch-connector

AGENT: read this first. Before interacting with Etch in any way, run etch-connector --help (or npx @digital-gravy/etch-connector --help) and read the entire output. It documents the backends, commands, flags, the stdout/stderr/exit-code contract, and how to pull the current Etch docs — everything you need to drive a tab correctly. Do not call any other command until you have read it.

A tiny local bridge that lets an AI harness — Claude Code, Cowork, or any tool that can run a shell command — drive a live Etch builder tab by evaluating scripts against the etch-public-api surface (window.etch.*).

Etch's scriptable API only exists inside the browser. There is no server you can call directly. So this connector inverts the usual direction: the browser dials out to the connector over a WebSocket (the same way Chrome remote debugging works, but in reverse), and the connector relays scripts into the tab and streams the result back.

Claude Code / Cowork
      │  shell:  etch-connector eval -t homepage "return ..."
      ▼
  etch-connector  (the `serve` daemon)
      │  ▲   WebSocket  (tab dials in via etch.connectAs)
      ▼  │
  Etch builder tab  ──►  window.etch.*

Status

This is an eval relay: the harness sends a JavaScript snippet, the page runs it as an async function, and the return value + console output come back. It is not an MCP server yet — see Going to MCP.

Scripts run in a page-enforced safe mode: the script sees only etch and the standard JS built-ins. window, document, fetch, storage, eval, … are withheld.

Install & run

No install needed — run it on demand with npx (npm) or bunx (Bun):

npx @digital-gravy/etch-connector serve
# or
bunx @digital-gravy/etch-connector serve

To get the short etch-connector command on your PATH, install it globally:

npm install -g @digital-gravy/etch-connector   # or: bun install -g …
etch-connector serve

Using the connector requires a valid, active Etch license — see LICENSE.

The examples below use the bare etch-connector command; if you're running via npx/bunx instead, prefix them with npx @digital-gravy/etch-connector (or bunx …).

Use

1. Start the daemon (once)

etch-connector serve
# bridge   ws://127.0.0.1:7331
# control  http://127.0.0.1:7332

2. Connect a tab

Primary way — the AI button. Enable the AI Connector in the Etch Settings, then open an Etch builder page and click the AI button in the left sidebar. It connects this tab for you, registering it under your site name (it just calls connectAs under the hood). Click it again to disconnect.

Secondary way — the console. You can also connect manually from the browser console:

etch.connectAs('homepage');

(connectAs is part of the public API; it opens the WebSocket back to the daemon and registers this tab under the given name. Pass { port: <n> } if you started the daemon on a non-default WebSocket port.)

3. Drive it

# list connected tabs
etch-connector tabs

# run a script (value → stdout, console output → stderr).
# reference the API as `etch.*`.
etch-connector eval -t homepage "return etch.apiVersion"

# multi-line / from a file / from stdin
etch-connector eval -t homepage -f ./script.js
echo "return await etch.blocks.getTree()" | etch-connector eval -t homepage

The script body runs as an async function, so it can return a value and use top-level await. Write scripts against the etch-public-api surface via the etch binding (window is not available). Examples:

return etch.apiVersion;
return await etch.blocks.getTree();
etch.blocks.select('block-id-123');
const groups = await etch.fields.listGroupsAsync();
return groups.length;

Output contract (for agents)

eval keeps stdout clean so a harness can capture it as the result:

  • stdout — the script's return value, as pretty JSON.
  • stderr — forwarded console.* output, prefixed with the level, plus connector notes that carry no such prefix (the runtime banner, warnings).
  • exit code — 0 success · 2 the script threw · 1 operational failure (no such tab, ambiguous target, timeout, daemon unreachable).

For AI agents

etch-connector tabs reports, per tab, which Etch runtime it is and which surfaces that runtime backs (read off etch.environment), then prints the authoring rules that follow from it: the dynamic-data model (data sources vs loops), the route tree, the content model, the media lookup, the namespaces that throw NOT_AVAILABLE, and the block types the runtime can construct. Not every runtime backs every surface, so read that before writing scripts — and branch on a capability, never on the product name. eval names the runtime it targets on stderr as a reminder, and tabs --quiet drops the prose when you are only checking that a tab is still connected.

The authoritative agent instructions live in etch-connector --help — read the entire output before interacting with Etch. It covers the backends, commands and flags, the stdout/stderr/exit-code contract, how to pull the current Etch docs (Context7 MCP or https://docs.etchwp.com/public-api), and the conventions for structuring pages (e.g. the section/container layout pattern). The public API may also expose ready-made skills for common tasks — check the docs before improvising. Project conventions for working on this repo live in AGENTS.md.

CLI

etch-connector — drive a live Etch tab from the command line

Usage:
  etch-connector serve [--ws-port 7331] [--control-port 7332] [--ws-host 127.0.0.1]
  etch-connector tabs  [--json] [--quiet] [--control-port 7332] [--cdp]
  etch-connector eval  [code] [-t|--tab name] [-f|--file path] [--timeout ms] [--cdp]
  etch-connector upload   <file…> [-t name] [--alt text] [--collection id] [--compress webp:80[:maxWidth]] [--cdp]
  etch-connector download <id…>   [-t name] [-o|--out dir|file] [--force] [--cdp]
  etch-connector shot      [-t name] [-s|--selector css] [--full] [--jpeg] [-o|--out file] [--freeze=false]   (CDP)
  etch-connector html      <selector> [-t name]                                              (CDP)
  etch-connector computed  <selector> [-t name] [--props a,b,c]                               (CDP)

Two backends:
  bridge  (default) the tab dials in via etch.connectAs(); page-sandboxed.
          Scripts see only "etch" + the standard JS built-ins.
  cdp     connect OUT to a Chrome started with --remote-debugging-port=9222.
          No daemon. Runs at DevTools privilege: full "window", plus screenshots,
          rendered HTML, and computed styles. Opt in with --cdp (or --cdp-port /
          --remote-debugging-port / --cdp-host). Target a tab with -t <url|title substring>.

Examples:
  etch-connector serve
  etch-connector tabs
  etch-connector eval -t homepage "return etch.apiVersion"
  echo "return await etch.blocks.getTree()" | etch-connector eval -t homepage
  etch-connector eval -t homepage -f ./script.js
  # CDP backend (Chrome launched with --remote-debugging-port=9222):
  etch-connector tabs --cdp
  etch-connector eval --cdp -t etch "return document.title"
  etch-connector shot --cdp -t etch -s .orbit -o orbit.png
  etch-connector computed --cdp -t etch -s .orbit__chip --props transform,animation-duration

When exactly one tab is connected, -t is optional. With several tabs you must name one.

Assets

The assets capability denotes the full API (as in Etch Studio). Runtimes may instead report individual capabilities, including alongside wpMedia:

  • assetsRead: listAsync and getAsync.
  • assetsUpload: uploadAsync, without compression on upload.
  • assetsDownload: downloadAsync.
  • assetsUpdate: updateAsync for alt and collectionIds, not filename.
  • assetsDelete: deleteAsync.
  • assetsCollections: list, create, update and delete collections.
  • assetsCompressInPlace: compressAsync with keepOriginal absent or false.

Absent flags are false. Check tabs before choosing operations; upload needs assetsUpload and download needs assetsDownload, unless full assets is backed. The bridge carries JSON only, so the connector base64-encodes bytes.

# Upload a photo with alt text; prints the stored asset
etch-connector upload ./hero.jpg --alt "Team on the roof"

# Where the full assets capability is backed, optionally compress before upload
etch-connector upload ./hero.jpg --alt "Team on the roof" --compress webp:80:2400

# Write an asset's bytes to a file (or into a directory, under its own name)
etch-connector download 12 --out /tmp/hero.webp

upload prints the stored asset (its id is what an etch/dynamic-image mediaId takes) and download prints { id, path, bytes }. The connector rejects ordinary uploads over 24.9 MiB locally; this Studio-derived limit also applies to WordPress, which can have its own file size and MIME restrictions. With --compress, sources up to 60 MiB may go to a runtime that supports upload-time compression. --alt takes one file per command. download refuses to overwrite an existing file, or to write a name starting with ., unless --force is given (the name comes from the tab). Both default to a 4-minute timeout, since compressing a large photo in the tab is slow; --timeout overrides it. A timed-out upload may still land, so check etch.assets.listAsync() before retrying. A runtime without etch.assets fails the script with "this runtime has no etch.assets" (exit 2).

HTTP control API

The daemon also exposes a loopback HTTP API (this is the seam the CLI uses, and the same one a future MCP server would call):

MethodPathBodyReturns
GET/health—{ ok }
GET/tabs—{ tabs }
POST/eval{ tab?, code, timeoutMs?, mode? }EvalResult

Architecture

  • src/protocol.ts — the wire format, mirrored from the Etch-side bridge.ts. Keep the two in sync.
  • src/bridge-server.ts — BridgeServer, the transport-agnostic core: owns the sockets, tracks tabs by name, correlates requests, handles timeouts and disconnects. This is the reusable piece.
  • src/control-server.ts — the HTTP control API over the core.
  • src/guidance.ts — turns a tab's etch.environment descriptor into the authoring instructions for that runtime.
  • src/cli.ts — the serve / tabs / eval commands.

Going to MCP later

Today this is a CLI eval relay. A future version will expose the same bridge as a proper MCP server, so an AI harness can call first-class tools (list tabs, run a script) directly instead of shelling out — without changing how you connect a tab.

Security

Two layers, neither a substitute for the other:

Transport. Everything binds to 127.0.0.1, so any local process can reach the daemon. Run it only on a trusted machine and do not expose the ports beyond loopback. The control API refuses any request carrying an Origin header — browsers always send one, local tools do not — and /eval only takes application/json, so a web page you visit cannot drive a tab through it.

Execution. Bridge mode runs the script inside a with-bound Proxy that exposes only etch plus the standard JS built-ins and throws on window, document, fetch, storage, eval, Function, … This is a guardrail, not a hardened sandbox: because the script shares the page's JS realm, a determined author can still reach globals through prototype / constructor chains (e.g. ({}).constructor.constructor). It reliably keeps casual and AI-authored scripts on the etch surface and closes the obvious escape hatches; it does not contain hostile code. For a true boundary against adversarial scripts, run the daemon only on a trusted machine.

版本与发布记录

@digital-gravy/etch-connector 共发布 15 个版本,首次发布于 2026/06/18;过去 12 个月发布了 15 个版本。

版本发布日期距今
0.5.3latest2026/11/103 天前
0.5.22026/11/103 天前
0.5.12026/10/2915 天前
0.5.02026/10/2816 天前
0.4.02026/10/2321 天前
0.3.02026/10/1430 天前

关于 @digital-gravy/etch-connector 的常见问题

@digital-gravy/etch-connector 是什么?

Local bridge that lets an AI harness drive a live Etch tab by evaluating scripts against the etch-public-api over a WebSocket.

如何安装 @digital-gravy/etch-connector?

在项目目录运行 npm install @digital-gravy/etch-connector,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 etch-connector,可以用 npx etch-connector 直接运行。

@digital-gravy/etch-connector 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/digital-gravy__etch-connector 是否存在。

@digital-gravy/etch-connector 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@digital-gravy/etch-connector 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@digital-gravy/etch-connector 可以商用吗?

@digital-gravy/etch-connector 使用 SEE LICENSE IN LICENSE 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@digital-gravy/etch-connector 的下载量有多少?

根据 npm 官方下载统计,@digital-gravy/etch-connector 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,328 次,日均约 78 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@digital-gravy/etch-connector 还在维护吗?

最新版本 0.5.3 发布于 2026/11/10(3 天前);至今共发布 15 个版本,其中过去 12 个月发布了 15 个;首次发布于 2026/06/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。