149 lines
5.4 KiB
JavaScript
149 lines
5.4 KiB
JavaScript
// 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);
|
|
}
|