Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
/**
* OpenCode's config is a file people write by hand.
*
* The installer adds a plugin and an MCP server to it without reserializing it,
* because reserializing would eat every comment and every formatting choice the
* user made. That means walking JSONC the way editors do — over comments, past
* trailing commas, through single-quoted strings — and splicing the new value
* into the original text. Each of those is a thing `JSON.parse` refuses and a
* naive scanner mis-reads, so each gets a case here.
*/
import assert from "node:assert/strict";
import { execFile, execFileSync } from "node:child_process";
import { copyFileSync, existsSync, mkdirSync } from "node:fs";
import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
import { stripJsonc, updateOpencodeConfig } from "./lib/install/jsonc-edit.mjs";
import { expectExit, runHookScript } from "./testing/support.mjs";
const PROXY = "/home/u/.openviking/opencode-mcp-proxy/openviking/servers/mcp-proxy.mjs";
const SPEC = "@openviking/opencode-plugin";
/** What the config parses to once the comments and trailing commas are gone. */
function parse(raw) {
return JSON.parse(stripJsonc(raw));
}
test("comments survive the edit, wherever they sit", () => {
const next = updateOpencodeConfig([
"{",
" // keep this user note",
' "theme": "system",',
" /* keep this block note */",
' "mcp": {',
" // keep this mcp note",
' "other": { "type": "local", "command": ["node", "other.js"] }',
" }",
"}",
"",
].join("\n"), { pluginSpec: SPEC, mcpProxy: PROXY });
assert.match(next, /\/\/ keep this user note/);
assert.match(next, /\/\* keep this block note \*\//);
assert.match(next, /\/\/ keep this mcp note/);
assert.match(next, /"theme": "system"/);
assert.match(next, /"other": \{ "type": "local"/);
assert.deepEqual(parse(next).plugin, [SPEC]);
assert.equal(parse(next).mcp.openviking.command[1], PROXY);
});
test("a trailing comma is not doubled", () => {
const next = updateOpencodeConfig([
"{",
' "plugin": [',
' "some-other-plugin",',
" ],",
' "mcp": {',
' "other": { "type": "local", "command": ["node", "other.js"] },',
" },",
"}",
"",
].join("\n"), { pluginSpec: SPEC, mcpProxy: PROXY });
assert.doesNotMatch(next, /,\s*,/);
assert.deepEqual(parse(next).plugin, ["some-other-plugin", SPEC]);
assert.deepEqual(Object.keys(parse(next).mcp), ["other", "openviking"]);
});
// A single quote makes the whole file unparseable, so this is the one case the
// editor gets through on the text walk alone: nothing tells it where "mcp" is
// except the scan, and the brace and the `//` inside the string would end the
// object and start a comment for a scanner that does not know it is in a quote.
test("a single-quoted value is stepped over, not scanned into", () => {
const next = updateOpencodeConfig([
"{",
" 'theme': 'dark } // not a comment',",
' "mcp": {',
' "other": { "type": "local", "command": ["node", "other.js"] }',
" }",
"}",
"",
].join("\n"), { pluginSpec: SPEC, mcpProxy: PROXY });
assert.match(next, /'dark \} \/\/ not a comment'/);
assert.match(next, /"openviking": \{/);
assert.ok(
next.indexOf('"openviking"') > next.indexOf('"other"'),
"the server belongs inside the mcp object the scan had to find",
);
assert.match(next, /"plugin": \[\n\s+"@openviking\/opencode-plugin"\n\s+\]/);
});
test("the MCP server lands inside an existing mcp object, beside its siblings", () => {
const next = updateOpencodeConfig([
"{",
' "mcp": {',
' "other": {',
' "type": "local",',
' "command": ["node", "other.js"],',
' "environment": { "NESTED": { "deeper": true } }',
" }",
" }",
"}",
"",
].join("\n"), { pluginSpec: "", mcpProxy: PROXY });
const data = parse(next);
assert.deepEqual(data.mcp.other.environment, { NESTED: { deeper: true } });
assert.deepEqual(data.mcp.openviking, {
type: "local",
command: ["node", PROXY],
enabled: true,
timeout: 15000,
});
assert.equal(data.openviking, undefined, "the server must not be added at the top level");
});
test("an empty config is filled in, and a second run changes nothing", () => {
const first = updateOpencodeConfig("", { pluginSpec: SPEC, mcpProxy: PROXY });
assert.deepEqual(parse(first).plugin, [SPEC]);
assert.equal(updateOpencodeConfig(first, { pluginSpec: SPEC, mcpProxy: PROXY }), first);
});
test("an npm registration of the plugin is dropped when the file plugin is written", () => {
const next = updateOpencodeConfig([
"{",
" // keep this user note",
' "plugin": ["@openviking/[email protected]", "some-other-plugin"]',
"}",
"",
].join("\n"), { pluginSpec: "", mcpProxy: PROXY });
assert.match(next, /\/\/ keep this user note/);
assert.deepEqual(parse(next).plugin, ["some-other-plugin"]);
assert.equal(parse(next).mcp.openviking.command[1], PROXY);
});
test("a server the user disabled stays disabled", () => {
const raw = [
"{",
' "mcp": {',
' "openviking": { "type": "local", "command": ["node", "old.js"], "enabled": false }',
" }",
"}",
"",
].join("\n");
assert.equal(updateOpencodeConfig(raw, { pluginSpec: "", mcpProxy: PROXY }), raw);
});
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..");
const installer = join(repoRoot, "examples", "memory-plugin-shared", "install.sh");
function runInstaller(args, options, script = installer) {
return new Promise((resolvePromise, reject) => {
execFile("bash", [script, ...args], options, (error, stdout, stderr) => {
if (error) {
error.stdout = stdout;
error.stderr = stderr;
reject(error);
} else {
resolvePromise({ stdout, stderr });
}
});
});
}
// The cases above are the editor; this is the wiring. It is the only thing that
// proves install.sh still finds the module and hands it the right three
// arguments now that the editor no longer lives inside the script.
for (const sourceMode of ["dev", "archive"]) {
test(`the OpenCode ${sourceMode} install loads its entries from clean sources`, async () => {
const dir = await mkdtemp(join(tmpdir(), "ov-opencode-jsonc-"));
try {
const home = join(dir, "home");
const bin = join(dir, "bin");
const configDir = join(home, ".config", "opencode");
await mkdir(bin, { recursive: true });
await mkdir(configDir, { recursive: true });
const opencode = join(bin, "opencode");
await writeFile(opencode, "#!/usr/bin/env sh\nprintf 'opencode 0.0.0-test\\n'\n");
await chmod(opencode, 0o755);
// An earlier installer registered the npm package; the file plugin
// replaces it rather than loading beside it.
const configPath = join(configDir, "opencode.jsonc");
await writeFile(configPath, `{\n // keep this user note\n "theme": "system",\n "plugin": ["${SPEC}"]\n}\n`);
// Copy only tracked paths from the working tree: no ignored generated
// runtime or node_modules may hide a missing installation step.
const source = join(dir, "source");
const files = execFileSync("git", ["ls-files", "-z", "--", "examples", "agent-plugins", ".github/scripts"], { cwd: repoRoot, encoding: "utf8" }).split("\0").filter(Boolean);
for (const file of files) {
// A working-tree deletion remains in the index until it is staged.
if (!existsSync(join(repoRoot, file))) continue;
mkdirSync(dirname(join(source, file)), { recursive: true });
copyFileSync(join(repoRoot, file), join(source, file));
}
await mkdir(join(source, ".git"));
if (sourceMode === "archive") {
execFileSync("bash", [join(source, ".github/scripts/stage-memory-plugin-marketplace.sh"), join(dir, "memory-plugin-marketplace")]);
execFileSync("zip", ["-rq", join(dir, "memory-plugin-marketplace.zip"), "memory-plugin-marketplace"], { cwd: dir });
}
await runInstaller([
"--harness", "opencode",
"--source", sourceMode,
"--lang", "en",
"--url", "http://127.0.0.1:9",
"--api-key", "",
"--yes",
], {
cwd: source,
env: {
...process.env,
HOME: home,
OPENVIKING_HOME: join(home, ".openviking"),
OPENVIKING_MARKETPLACE_ARCHIVE_URL: `file://${join(dir, "memory-plugin-marketplace.zip")}`,
OPENVIKING_DOWNLOAD_BASE: "https://downloads.example.invalid",
OPENVIKING_SKIP_VERSION_CHECK: "1",
PATH: `${bin}:${process.env.PATH}`,
},
}, join(source, "examples/memory-plugin-shared/install.sh"));
const raw = await readFile(configPath, "utf8");
assert.match(raw, /keep this user note/);
assert.match(raw, /"theme": "system"/);
assert.match(parse(raw).mcp.openviking.command[1], /servers\/mcp-proxy\.mjs$/);
const installedRoot = join(configDir, "plugins/openviking");
const env = { HOME: home, OPENVIKING_HOME: join(home, ".openviking") };
assert.deepEqual(parse(raw).plugin, []);
expectExit(await runHookScript(parse(raw).mcp.openviking.command[1], { cwd: home, env }));
expectExit(await runHookScript(join(installedRoot, "index.mjs"), { cwd: home, env }));
} finally {
await rm(dir, { recursive: true, force: true });
}
});
}