Initial import: opencode Chrome extension + native host (browser MCP bridge)

This commit is contained in:
ner committed 2026-11-25 18:46:48 +02:00
commit ee5b79570c
41 files changed
+6845

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
#!/bin/bash
# Builds a self-contained macOS installer: dist/opencode-browser-<version>.pkg
#
# packaging/build-pkg.sh [--arch universal|arm64|x64]
#
# The package carries its own Node.js (no Node needed on the target Mac), the
# MCP server and native host bundled into single files, and the extension.
set -euo pipefail
ARCH="universal"
while [ $# -gt 0 ]; do
case "$1" in
--arch) shift; ARCH="$1" ;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
shift
done
NODE_VERSION="22.13.1"
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
PKG="$ROOT/packaging"
VERSION="$(node -p "require('$ROOT/extension/manifest.json').version")"
BUILD="$ROOT/build/pkg"
CACHE="$ROOT/build/cache"
APP="$BUILD/root/Library/Application Support/opencode-browser"
rm -rf "$BUILD"
mkdir -p "$APP/bin" "$APP/host" "$CACHE" "$ROOT/dist"
# --- Node.js runtime (official binaries, checksum-verified) ---
fetch_node() { # arch -> path of the node binary
local a="$1" name="node-v$NODE_VERSION-darwin-$1"
if [ ! -x "$CACHE/$name/bin/node" ]; then
curl -fsSL -o "$CACHE/$name.tar.gz" "https://nodejs.org/dist/v$NODE_VERSION/$name.tar.gz"
curl -fsSL -o "$CACHE/SHASUMS256.txt" "https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt"
(cd "$CACHE" && grep " $name.tar.gz\$" SHASUMS256.txt | shasum -a 256 -c - >/dev/null)
tar -xzf "$CACHE/$name.tar.gz" -C "$CACHE" "$name/bin/node"
fi
echo "$CACHE/$name/bin/node"
}
echo "Node.js $NODE_VERSION ($ARCH)"
if [ "$ARCH" = "universal" ]; then
lipo -create "$(fetch_node arm64)" "$(fetch_node x64)" -output "$APP/bin/node"
# lipo invalidates the vendor signatures; Apple silicon needs a valid one.
codesign --force --sign - "$APP/bin/node"
else
cp "$(fetch_node "$ARCH")" "$APP/bin/node"
fi
# --- host: bundle the MCP server and native host into single files ---
echo "Bundling the host"
(cd "$ROOT/host" && npm install --silent --no-audit --no-fund)
ESBUILD="$ROOT/host/node_modules/.bin/esbuild"
common=(--bundle --platform=node --target=node22 --format=esm --log-level=warning
"--banner:js=import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);")
"$ESBUILD" "$ROOT/host/mcp-server.js" "${common[@]}" --outfile="$APP/host/mcp-server.mjs"
"$ESBUILD" "$ROOT/host/native-host.js" "${common[@]}" --outfile="$APP/host/native-host.mjs"
cp "$ROOT/host/instructions.md" "$APP/host/"
# --- extension, scripts, presets ---
cp -R "$ROOT/extension" "$APP/extension"
cp "$PKG/native-host" "$PKG/setup" "$PKG/uninstall" "$PKG/user-setup.mjs" "$PKG/llama-provider.json" "$APP/"
cp "$ROOT/LICENSE" "$APP/"
find "$BUILD/root" -name .DS_Store -delete
xattr -cr "$BUILD/root"
# --- packages ---
echo "Packaging"
export COPYFILE_DISABLE=1 # no ._AppleDouble files for extended attributes
pkgbuild --quiet --root "$BUILD/root" --scripts "$PKG/scripts-main" \
--identifier com.opencode.browser --version "$VERSION" --install-location / \
"$BUILD/main.pkg"
pkgbuild --quiet --nopayload --scripts "$PKG/scripts-llama" \
--identifier com.opencode.browser.llama --version "$VERSION" \
"$BUILD/llama.pkg"
HOST_ARCHS="arm64,x86_64"
[ "$ARCH" = "arm64" ] && HOST_ARCHS="arm64"
[ "$ARCH" = "x64" ] && HOST_ARCHS="x86_64"
sed -e "s/@VERSION@/$VERSION/g" -e "s/@ARCHS@/$HOST_ARCHS/g" "$PKG/distribution.xml" > "$BUILD/distribution.xml"
SUFFIX=""
[ "$ARCH" != "universal" ] && SUFFIX="-$ARCH"
OUT="$ROOT/dist/opencode-browser-$VERSION$SUFFIX.pkg"
productbuild --quiet --distribution "$BUILD/distribution.xml" --package-path "$BUILD" \
--resources "$PKG/resources" "$OUT"
echo "Built $OUT ($(du -h "$OUT" | cut -f1))"
+24
View File
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<installer-gui-script minSpecVersion="2">
<title>opencode browser tools</title>
<welcome file="welcome.html" mime-type="text/html"/>
<conclusion file="conclusion.html" mime-type="text/html"/>
<options customize="allow" require-scripts="false" rootVolumeOnly="true" hostArchitectures="@ARCHS@"/>
<volume-check>
<allowed-os-versions><os-version min="11.0"/></allowed-os-versions>
</volume-check>
<choices-outline>
<line choice="main"/>
<line choice="llama"/>
</choices-outline>
<choice id="main" title="Browser tools for opencode" enabled="false" selected="true"
description="Extension, native messaging host, MCP server (with Node.js) and the opencode MCP config.">
<pkg-ref id="com.opencode.browser"/>
</choice>
<choice id="llama" title="llama.cpp model (192.168.0.130)" start_selected="true"
description="Adds the llama.cpp provider (Qwen3.8 27B, http://192.168.0.130:8080/v1) to opencode, and makes it the default model if none is set.">
<pkg-ref id="com.opencode.browser.llama"/>
</choice>
<pkg-ref id="com.opencode.browser" version="@VERSION@">main.pkg</pkg-ref>
<pkg-ref id="com.opencode.browser.llama" version="@VERSION@">llama.pkg</pkg-ref>
</installer-gui-script>
+86
View File
@@ -0,0 +1,86 @@
{
"model": "llama.cpp/qwen3.8-27b-nvfp4",
"provider": {
"llama.cpp": {
"npm": "@ai-sdk/openai-compatible",
"name": "llama.cpp",
"options": {
"baseURL": "http://192.168.0.130:8080/v1"
},
"models": {
"qwen3.8-27b-nvfp4": {
"name": "Qwen3.8 27B NVFP4",
"attachment": true,
"reasoning": true,
"tool_call": true,
"modalities": {
"input": [
"text",
"image"
],
"output": [
"text"
]
},
"limit": {
"context": 252928,
"output": 65536
},
"options": {
"reasoningEffort": "medium"
},
"variants": {
"none": {
"reasoningEffort": "none"
},
"low": {
"reasoningEffort": "low"
},
"medium": {
"reasoningEffort": "medium"
},
"xhigh": {
"reasoningEffort": "xhigh"
}
}
},
"qwen3.8-27b-nvfp4-uncensored": {
"name": "Qwen3.8 27B NVFP4 Uncensored",
"attachment": true,
"reasoning": true,
"tool_call": true,
"modalities": {
"input": [
"text",
"image"
],
"output": [
"text"
]
},
"limit": {
"context": 252928,
"output": 65536
},
"options": {
"reasoningEffort": "medium"
},
"variants": {
"none": {
"reasoningEffort": "none"
},
"low": {
"reasoningEffort": "low"
},
"medium": {
"reasoningEffort": "medium"
},
"xhigh": {
"reasoningEffort": "xhigh"
}
}
}
}
}
}
}
+5
View File
@@ -0,0 +1,5 @@
#!/bin/sh
# Started by the browser (native messaging) with a minimal environment.
APP="/Library/Application Support/opencode-browser"
cd "$APP/host" || exit 1
exec "$APP/bin/node" "$APP/host/native-host.mjs" "$@"
+13
View File
@@ -0,0 +1,13 @@
<html><body style="font-family:-apple-system,sans-serif;font-size:13px">
<h3>One step left: add the extension to Chrome</h3>
<ol>
<li>Open <code>chrome://extensions</code> in Chrome.</li>
<li>Turn on <b>Developer mode</b> (top right).</li>
<li>Drag the <b>extension</b> folder from the Finder window that just opened onto that page.<br>
(Or click <b>Load unpacked</b>, press ⌘⇧G and enter <code>/Library/Application Support/opencode-browser/extension</code>.)</li>
<li>Click the extension's toolbar icon: it should say <b>Connected</b>.</li>
</ol>
<p>Then start a new opencode session. The browser tools appear as <code>chrome_*</code>.</p>
<p>Other user account on this Mac: run <code>"/Library/Application Support/opencode-browser/setup"</code> as that user.<br>
Uninstall: <code>sudo "/Library/Application Support/opencode-browser/uninstall"</code></p>
</body></html>
+11
View File
@@ -0,0 +1,11 @@
<html><body style="font-family:-apple-system,sans-serif;font-size:13px">
<h3>opencode browser tools</h3>
<p>Gives <b>opencode</b> the Claude-in-Chrome browser tools (<code>chrome_*</code>), driving your Chrome through the <b>opencode</b> extension.</p>
<p>This installs:</p>
<ul>
<li>the extension, the native messaging host and the MCP server, with their own Node.js runtime, in <code>/Library/Application Support/opencode-browser</code></li>
<li>the <code>chrome</code> MCP server in <code>~/.config/opencode/opencode.json</code> (a backup is kept)</li>
<li>optionally, the <b>llama.cpp</b> model provider (192.168.0.130:8080). Choose it under <i>Customize</i>.</li>
</ul>
<p>Personal, non-commercial use only (PolyForm Noncommercial 1.0.0).</p>
</body></html>
+10
View File
@@ -0,0 +1,10 @@
#!/bin/bash
# Optional component: adds the llama.cpp provider to the user's opencode config.
APP="/Library/Application Support/opencode-browser"
USER_NAME="$(stat -f%Su /dev/console)"
if [ -z "$USER_NAME" ] || [ "$USER_NAME" = "root" ] || [ "$USER_NAME" = "loginwindow" ]; then
exit 0
fi
USER_HOME="$(dscl . -read "/Users/$USER_NAME" NFSHomeDirectory | awk '{print $2}')"
sudo -u "$USER_NAME" HOME="$USER_HOME" "$APP/bin/node" "$APP/user-setup.mjs" llama "$APP" || true
exit 0
+32
View File
@@ -0,0 +1,32 @@
#!/bin/bash
# Runs as root after the payload lands in /Library/Application Support/opencode-browser.
APP="/Library/Application Support/opencode-browser"
HOST_NAME="com.opencode.browser"
# System-wide registration for Google Chrome covers every user on this Mac,
# even one who has never opened Chrome yet.
SYS_DIR="/Library/Google/Chrome/NativeMessagingHosts"
mkdir -p "$SYS_DIR"
cat > "$SYS_DIR/$HOST_NAME.json" <<JSON
{
"name": "$HOST_NAME",
"description": "opencode Browser native messaging host",
"path": "$APP/native-host",
"type": "stdio",
"allowed_origins": ["chrome-extension://ilgnnaeidkbcbnklkmjllflfkmendiib/"]
}
JSON
# Everything else is per user: the logged-in (console) user.
USER_NAME="$(stat -f%Su /dev/console)"
if [ -z "$USER_NAME" ] || [ "$USER_NAME" = "root" ] || [ "$USER_NAME" = "loginwindow" ]; then
echo "opencode-browser: no logged-in user; run '$APP/setup' later as your user."
exit 0
fi
USER_HOME="$(dscl . -read "/Users/$USER_NAME" NFSHomeDirectory | awk '{print $2}')"
sudo -u "$USER_NAME" HOME="$USER_HOME" "$APP/bin/node" "$APP/user-setup.mjs" install "$APP" || true
# Show the extension folder so it can be dragged onto chrome://extensions.
launchctl asuser "$(id -u "$USER_NAME")" sudo -u "$USER_NAME" open -R "$APP/extension" || true
exit 0
+9
View File
@@ -0,0 +1,9 @@
#!/bin/bash
# Re-run the per-user setup (native host registration + opencode config) for
# the current user, e.g. for a second account on this Mac.
# setup browser tools only
# setup --llama ...and the llama.cpp provider
APP="/Library/Application Support/opencode-browser"
"$APP/bin/node" "$APP/user-setup.mjs" install "$APP"
[ "$1" = "--llama" ] && "$APP/bin/node" "$APP/user-setup.mjs" llama "$APP"
exit 0
+17
View File
@@ -0,0 +1,17 @@
#!/bin/bash
# Removes opencode Browser from this Mac:
# sudo "/Library/Application Support/opencode-browser/uninstall"
# The extension itself is removed from chrome://extensions.
APP="/Library/Application Support/opencode-browser"
if [ "$(id -u)" != 0 ]; then
echo "Run with sudo."
exit 1
fi
USER_NAME="${SUDO_USER:-$(stat -f%Su /dev/console)}"
USER_HOME="$(dscl . -read "/Users/$USER_NAME" NFSHomeDirectory | awk '{print $2}')"
sudo -u "$USER_NAME" HOME="$USER_HOME" "$APP/bin/node" "$APP/user-setup.mjs" uninstall "$APP"
rm -f /Library/Google/Chrome/NativeMessagingHosts/com.opencode.browser.json
rm -rf "$APP"
pkgutil --forget com.opencode.browser >/dev/null 2>&1
pkgutil --forget com.opencode.browser.llama >/dev/null 2>&1
echo "Removed. Remove the extension in chrome://extensions. The llama.cpp provider (if added) stays in opencode.json."
+148
View File
@@ -0,0 +1,148 @@
// Per-user part of the macOS package. The installer's postinstall scripts run
// it as the logged-in user, with the bundled node.
//
// user-setup.mjs install <app-dir> register the native host in every
// browser profile dir + add the MCP
// server to opencode's global config
// user-setup.mjs llama <app-dir> add the llama.cpp provider (and make it
// the default model if none is set)
// user-setup.mjs uninstall <app-dir> undo both
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const HOST_NAME = "com.opencode.browser";
const EXTENSION_ID = "ilgnnaeidkbcbnklkmjllflfkmendiib";
const [cmd, appDir] = process.argv.slice(2);
if (!cmd || !appDir) {
console.error("usage: user-setup.mjs install|llama|uninstall <app-dir>");
process.exit(1);
}
const home = os.homedir();
const support = path.join(home, "Library", "Application Support");
// [label, NativeMessagingHosts dir, register even if the browser was never run]
const BROWSERS = [
["Google Chrome", "Google/Chrome", true],
["Google Chrome Beta", "Google/Chrome Beta"],
["Google Chrome Canary", "Google/Chrome Canary"],
["Chrome for Testing", "Google/Chrome for Testing"],
["Chromium", "Chromium"],
["Microsoft Edge", "Microsoft Edge"],
["Brave", "BraveSoftware/Brave-Browser"],
["Vivaldi", "Vivaldi"],
["Arc", "Arc/User Data"]
].map(([name, rel, always]) => ({ name, dir: path.join(support, rel, "NativeMessagingHosts"), always }));
const configDir = process.env.OPENCODE_CONFIG_DIR || path.join(home, ".config", "opencode");
const configFile = path.join(configDir, "opencode.json");
const mcpServer = path.join(appDir, "host", "mcp-server.mjs");
const instructions = path.join(appDir, "host", "instructions.md");
const nodeBin = path.join(appDir, "bin", "node");
function registerHosts() {
const manifest = {
name: HOST_NAME,
description: "opencode Browser native messaging host",
path: path.join(appDir, "native-host"),
type: "stdio",
allowed_origins: [`chrome-extension://${EXTENSION_ID}/`]
};
for (const b of BROWSERS) {
if (!b.always && !fs.existsSync(path.dirname(b.dir))) continue;
fs.mkdirSync(b.dir, { recursive: true });
fs.writeFileSync(path.join(b.dir, `${HOST_NAME}.json`), JSON.stringify(manifest, null, 2) + "\n");
console.log(`Registered the native host for ${b.name}`);
}
}
function unregisterHosts() {
for (const b of BROWSERS) {
const f = path.join(b.dir, `${HOST_NAME}.json`);
if (fs.existsSync(f)) {
fs.rmSync(f);
console.log(`Removed the native host for ${b.name}`);
}
}
}
// Read-modify-write opencode.json, keeping a backup. A config with comments
// can't be edited safely, so then the change is printed for a manual edit.
function editConfig(describe, change) {
fs.mkdirSync(configDir, { recursive: true });
let config = { $schema: "https://opencode.ai/config.json" };
if (fs.existsSync(configFile)) {
const raw = fs.readFileSync(configFile, "utf-8");
try {
config = JSON.parse(raw);
} catch {
console.log(`${configFile} is not plain JSON (comments?). Add this by hand:`);
console.log(JSON.stringify(change({}), null, 2));
return;
}
const backup = `${configFile}.bak-${new Date().toISOString().replace(/[:.]/g, "-")}`;
fs.writeFileSync(backup, raw);
console.log(`Backed up ${configFile} -> ${backup}`);
} else if (fs.existsSync(path.join(configDir, "opencode.jsonc"))) {
console.log(`${configDir}/opencode.jsonc exists; add this to it by hand:`);
console.log(JSON.stringify(change({}), null, 2));
return;
}
fs.writeFileSync(configFile, JSON.stringify(change(config), null, 2) + "\n");
console.log(`${describe} in ${configFile}`);
}
// Instructions from an earlier install (this package or a source checkout).
const isOurInstructions = (p) =>
typeof p === "string" && /opencode-(browser|chrome)\/host\/instructions\.md$/.test(p);
function addMcp(config) {
config.mcp ??= {};
config.mcp.chrome = {
type: "local",
command: [nodeBin, mcpServer],
enabled: true,
// switch_browser waits up to 2 minutes for the user to click Connect.
timeout: 150000
};
config.instructions = [...(config.instructions || []).filter((p) => !isOurInstructions(p)), instructions];
return config;
}
function removeMcp(config) {
if (config.mcp?.chrome) delete config.mcp.chrome;
if (config.mcp && Object.keys(config.mcp).length === 0) delete config.mcp;
if (config.instructions) {
config.instructions = config.instructions.filter((p) => !isOurInstructions(p));
if (config.instructions.length === 0) delete config.instructions;
}
return config;
}
function addLlama(config) {
const preset = JSON.parse(fs.readFileSync(path.join(appDir, "llama-provider.json"), "utf-8"));
config.provider ??= {};
Object.assign(config.provider, preset.provider);
config.model ??= preset.model;
return config;
}
switch (cmd) {
case "install":
registerHosts();
editConfig('Added the "chrome" MCP server and browser instructions', addMcp);
break;
case "llama":
editConfig('Added the "llama.cpp" provider', addLlama);
break;
case "uninstall":
unregisterHosts();
if (fs.existsSync(configFile)) editConfig('Removed the "chrome" MCP server', removeMcp);
break;
default:
console.error(`unknown command: ${cmd}`);
process.exit(1);
}