1ac1ba5442
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The `opencode-local` adapter runs the OpenCode harness; its model/provider routing assumes built-in providers (anthropic/openai/...) and their default models > - Deployments increasingly put an OpenAI/Anthropic-compatible LLM gateway between the harness and the model for cost, governance, or data-residency reasons: LiteLLM, OpenRouter, Portkey, Kong, a corporate proxy, self-hosted models (vLLM/Ollama), or region-pinned/sovereign endpoints. But OpenCode only resolves `--model provider/model` when the model is registered in a provider's `models` map, and `OPENCODE_ALLOW_ALL_MODELS` does NOT bypass its internal `getModel()` > - Several lanes also fall back to built-in default models the gateway may not serve: the auxiliary/title model (e.g. `claude-haiku-*`) and the budget/recovery "cheap" lane (`openai/gpt-5.1-codex-mini`); these abort runs with "no keys found that support model" > - This pull request makes the adapter's provider/model wiring declarative via env, so any such deployment can register gateway models + pin the auxiliary/budget lanes without code changes; nothing here is specific to one hosting setup > - The benefit is OpenCode works behind any compatible gateway with config only; with no env set, behavior is unchanged ## Linked Issues or Issue Description No existing issue; describing in-PR (feature / adapter enhancement). - **Gap:** there is no supported way to register custom/gateway providers + models for `opencode-local`, nor to pin the auxiliary (title-gen) and budget (recovery) model lanes, so routing OpenCode through a gateway fails at `getModel()` or on the default helper models. - Related: #5737 (exe.dev sandbox installs for gemini/opencode local), #5823 (unblock claude_local on remote sandbox providers). > Note on ROADMAP: this is adapter-level, opt-in config (defaults unchanged) that *enables* gateway routing for one harness; it is not the core "Cloud / Sandbox agents" platform work itself. Happy to redirect/discuss in #dev if preferred. ## What Changed - `PAPERCLIP_OPENCODE_PROVIDERS`: merge custom/extended providers (OpenCode `provider` shape) into the runtime `opencode.json`, so gateway models are registered and `--model provider/model` resolves. `{env:VAR}` placeholders are expanded server-side (so a key need not depend on the sandbox run env). - A malformed `PAPERCLIP_OPENCODE_PROVIDERS` is no longer silently ignored: invalid JSON, a non-object value, and individual provider entries with non-object values (which are skipped by name) each append a visible note to the run notes so the misconfiguration is diagnosable (addresses both review P1s). - `PAPERCLIP_OPENCODE_SMALL_MODEL` / `PAPERCLIP_OPENCODE_CHEAP_MODEL`: pin the auxiliary (title-generation) and budget (recovery-retry) lanes to gateway-served models; defaults unchanged. - Honour `OPENCODE_ALLOW_ALL_MODELS` on the **remote** execution path too (was local-only, a parity gap). - `PAPERCLIP_OPENCODE_PRINT_LOGS`: optional toggle adding `--print-logs` so OpenCode logs surface on stderr for diagnosing remote/sandbox runs. - `buildOpenCodeModelProfiles()` guards its `process.env` default with `typeof process` so the shared client/server module stays browser-safe (a bare `process.env` at module load threw ReferenceError in the browser under Vite dev middleware and broke UI rendering in the e2e lane). ## Verification - `pnpm --filter @paperclipai/adapter-opencode-local build` and `typecheck` (tsc clean) - `pnpm exec vitest run packages/adapters/opencode-local/src` shows 33 passing (incl. new tests for the provider merge, `{env:}` expansion, the malformed/non-object/skipped-entry provider notes, small/cheap-model resolution, and the remote allow-all bypass) - Manually verified end-to-end against a real OpenAI-/Anthropic-compatible gateway: with the providers + small/cheap model set, both the title-gen and main task route to the configured gateway model and the agent completes (a real completion is returned and billed). That deployment supplies the verification evidence; the mechanism is gateway-agnostic. ## Risks Low. Everything is env-driven and opt-in; with no env set the generated config output is unchanged, and the cheap model profile keeps its model (the only difference is its updated human-readable description). Defaults preserved: built-in providers, Codex-mini cheap lane with `variant: low`, no `--print-logs`. No migration/UI impact. ## Model Used Claude Opus 4.8 (`claude-opus-4-8`, 1M context), extended thinking + tool use, via Claude Code. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md (adapter-level opt-in config enabling gateway routing; not the core sandbox-platform work, noted above) - [x] I have searched GitHub for duplicate or related PRs and linked them above (#5737, #5823) - [x] I have either (a) linked existing issues OR (b) described the issue in-PR following the relevant issue template - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [ ] If this change affects the UI, I have included before/after screenshots (n/a, no UI) - [ ] I have updated relevant documentation to reflect my changes (env vars documented inline via comments; no central doc references the adapter env yet) - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups (the P1 about silently dropped malformed providers JSON is addressed in 6eeb803, the follow-up P1 about silently skipped non-object entries in 2f4045a; the latest review has no further findings, and a re-review is requested for the final note-copy/test-fixture polish at head) - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
264 lines
10 KiB
TypeScript
264 lines
10 KiB
TypeScript
import fs from "node:fs/promises";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import { prepareOpenCodeRuntimeConfig } from "./runtime-config.js";
|
|
|
|
const cleanupPaths = new Set<string>();
|
|
|
|
afterEach(async () => {
|
|
await Promise.all(
|
|
[...cleanupPaths].map(async (filepath) => {
|
|
await fs.rm(filepath, { recursive: true, force: true });
|
|
cleanupPaths.delete(filepath);
|
|
}),
|
|
);
|
|
});
|
|
|
|
async function makeConfigHome(initialConfig?: Record<string, unknown>) {
|
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), "paperclip-opencode-test-"));
|
|
cleanupPaths.add(root);
|
|
const configDir = path.join(root, "opencode");
|
|
await fs.mkdir(configDir, { recursive: true });
|
|
if (initialConfig) {
|
|
await fs.writeFile(
|
|
path.join(configDir, "opencode.json"),
|
|
`${JSON.stringify(initialConfig, null, 2)}\n`,
|
|
"utf8",
|
|
);
|
|
}
|
|
return root;
|
|
}
|
|
|
|
describe("prepareOpenCodeRuntimeConfig", () => {
|
|
it("injects an external_directory allow rule by default", async () => {
|
|
const configHome = await makeConfigHome({
|
|
permission: {
|
|
read: "allow",
|
|
},
|
|
theme: "system",
|
|
});
|
|
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: { XDG_CONFIG_HOME: configHome },
|
|
config: {},
|
|
});
|
|
cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);
|
|
|
|
expect(prepared.env.XDG_CONFIG_HOME).not.toBe(configHome);
|
|
const runtimeConfig = JSON.parse(
|
|
await fs.readFile(
|
|
path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"),
|
|
"utf8",
|
|
),
|
|
) as Record<string, unknown>;
|
|
expect(runtimeConfig).toMatchObject({
|
|
theme: "system",
|
|
permission: {
|
|
read: "allow",
|
|
external_directory: "allow",
|
|
},
|
|
});
|
|
|
|
await prepared.cleanup();
|
|
cleanupPaths.delete(prepared.env.XDG_CONFIG_HOME);
|
|
await expect(fs.access(prepared.env.XDG_CONFIG_HOME)).rejects.toThrow();
|
|
});
|
|
|
|
it("merges custom providers from PAPERCLIP_OPENCODE_PROVIDERS into the config", async () => {
|
|
const configHome = await makeConfigHome({ permission: { read: "allow" } });
|
|
const providers = {
|
|
bifrost: {
|
|
npm: "@ai-sdk/openai-compatible",
|
|
name: "Bifrost EU",
|
|
options: {
|
|
baseURL: "http://gateway.example.svc.cluster.local:8080/v1",
|
|
apiKey: "{env:ANTHROPIC_API_KEY}",
|
|
},
|
|
models: { "example/model-a": { name: "Model A" } },
|
|
},
|
|
};
|
|
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: {
|
|
XDG_CONFIG_HOME: configHome,
|
|
PAPERCLIP_OPENCODE_PROVIDERS: JSON.stringify(providers),
|
|
},
|
|
config: {},
|
|
});
|
|
cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);
|
|
|
|
const runtimeConfig = JSON.parse(
|
|
await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),
|
|
) as Record<string, unknown>;
|
|
expect(runtimeConfig).toMatchObject({
|
|
permission: { read: "allow", external_directory: "allow" },
|
|
provider: providers,
|
|
});
|
|
expect(prepared.notes.some((n) => n.includes("bifrost"))).toBe(true);
|
|
await prepared.cleanup();
|
|
});
|
|
|
|
it("reads PAPERCLIP_OPENCODE_PROVIDERS from process.env when absent from the run env", async () => {
|
|
const configHome = await makeConfigHome({ permission: { read: "allow" } });
|
|
const providers = { bifrost: { npm: "@ai-sdk/openai-compatible", models: { "example/model-a": {} } } };
|
|
process.env.PAPERCLIP_OPENCODE_PROVIDERS = JSON.stringify(providers);
|
|
try {
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: { XDG_CONFIG_HOME: configHome },
|
|
config: {},
|
|
});
|
|
cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);
|
|
const runtimeConfig = JSON.parse(
|
|
await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),
|
|
) as Record<string, unknown>;
|
|
expect(runtimeConfig).toMatchObject({ provider: providers });
|
|
await prepared.cleanup();
|
|
} finally {
|
|
delete process.env.PAPERCLIP_OPENCODE_PROVIDERS;
|
|
}
|
|
});
|
|
|
|
it("expands {env:VAR} placeholders in custom providers using the run/process env (bakes the literal vk)", async () => {
|
|
const configHome = await makeConfigHome({ permission: { read: "allow" } });
|
|
const providers = {
|
|
bifrost: {
|
|
npm: "@ai-sdk/openai-compatible",
|
|
options: { baseURL: "http://bifrost/v1", apiKey: "{env:ANTHROPIC_API_KEY}" },
|
|
models: { "example/model-a": {} },
|
|
},
|
|
};
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: { XDG_CONFIG_HOME: configHome, PAPERCLIP_OPENCODE_PROVIDERS: JSON.stringify(providers), ANTHROPIC_API_KEY: "sk-bf-REALVK" },
|
|
config: {},
|
|
});
|
|
cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);
|
|
const runtimeConfig = JSON.parse(
|
|
await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),
|
|
) as { provider: { bifrost: { options: { apiKey: string } } } };
|
|
// The {env:...} placeholder must be replaced with the literal value, so OpenCode
|
|
// does not depend on its sandboxed process env carrying the key.
|
|
expect(runtimeConfig.provider.bifrost.options.apiKey).toBe("sk-bf-REALVK");
|
|
await prepared.cleanup();
|
|
});
|
|
|
|
it("leaves an unresolvable {env:VAR} placeholder intact", async () => {
|
|
const configHome = await makeConfigHome({ permission: { read: "allow" } });
|
|
const providers = { bifrost: { options: { apiKey: "{env:DEFINITELY_UNSET_VAR_XYZ}" }, models: { "x/y": {} } } };
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: { XDG_CONFIG_HOME: configHome, PAPERCLIP_OPENCODE_PROVIDERS: JSON.stringify(providers) },
|
|
config: {},
|
|
});
|
|
cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);
|
|
const runtimeConfig = JSON.parse(
|
|
await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),
|
|
) as { provider: { bifrost: { options: { apiKey: string } } } };
|
|
expect(runtimeConfig.provider.bifrost.options.apiKey).toBe("{env:DEFINITELY_UNSET_VAR_XYZ}");
|
|
await prepared.cleanup();
|
|
});
|
|
|
|
it("pins small_model from PAPERCLIP_OPENCODE_SMALL_MODEL", async () => {
|
|
const configHome = await makeConfigHome({ permission: { read: "allow" } });
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: { XDG_CONFIG_HOME: configHome, PAPERCLIP_OPENCODE_SMALL_MODEL: "example/model-a" },
|
|
config: {},
|
|
});
|
|
cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);
|
|
const runtimeConfig = JSON.parse(
|
|
await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),
|
|
) as { small_model?: string };
|
|
expect(runtimeConfig.small_model).toBe("example/model-a");
|
|
await prepared.cleanup();
|
|
});
|
|
|
|
it("ignores malformed PAPERCLIP_OPENCODE_PROVIDERS without writing a provider block and surfaces a note", async () => {
|
|
const configHome = await makeConfigHome({ permission: { read: "allow" } });
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: { XDG_CONFIG_HOME: configHome, PAPERCLIP_OPENCODE_PROVIDERS: "not json" },
|
|
config: {},
|
|
});
|
|
cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);
|
|
const runtimeConfig = JSON.parse(
|
|
await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),
|
|
) as Record<string, unknown>;
|
|
expect(runtimeConfig.provider).toBeUndefined();
|
|
expect(prepared.notes).toContain(
|
|
"PAPERCLIP_OPENCODE_PROVIDERS contains invalid JSON; custom providers ignored.",
|
|
);
|
|
await prepared.cleanup();
|
|
});
|
|
|
|
it("surfaces a note when PAPERCLIP_OPENCODE_PROVIDERS is valid JSON but not an object", async () => {
|
|
const configHome = await makeConfigHome({ permission: { read: "allow" } });
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: { XDG_CONFIG_HOME: configHome, PAPERCLIP_OPENCODE_PROVIDERS: "[1,2,3]" },
|
|
config: {},
|
|
});
|
|
cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);
|
|
const runtimeConfig = JSON.parse(
|
|
await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),
|
|
) as Record<string, unknown>;
|
|
expect(runtimeConfig.provider).toBeUndefined();
|
|
expect(prepared.notes).toContain(
|
|
"PAPERCLIP_OPENCODE_PROVIDERS is set but is not a JSON object; custom providers ignored.",
|
|
);
|
|
await prepared.cleanup();
|
|
});
|
|
|
|
it("surfaces skipped provider entries with non-object values and keeps the usable ones", async () => {
|
|
const configHome = await makeConfigHome({ permission: { read: "allow" } });
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: {
|
|
XDG_CONFIG_HOME: configHome,
|
|
PAPERCLIP_OPENCODE_PROVIDERS: JSON.stringify({
|
|
bifrost: "http://gateway.example/v1",
|
|
usable: { options: { baseURL: "http://gateway.example/v1" } },
|
|
}),
|
|
},
|
|
config: {},
|
|
});
|
|
cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);
|
|
const runtimeConfig = JSON.parse(
|
|
await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),
|
|
) as { provider?: Record<string, unknown> };
|
|
expect(runtimeConfig.provider?.usable).toBeDefined();
|
|
expect(runtimeConfig.provider?.bifrost).toBeUndefined();
|
|
expect(prepared.notes).toContain(
|
|
"PAPERCLIP_OPENCODE_PROVIDERS: skipped provider(s) with non-object values: bifrost.",
|
|
);
|
|
await prepared.cleanup();
|
|
});
|
|
|
|
it("surfaces skipped provider entries when no usable entries remain", async () => {
|
|
const configHome = await makeConfigHome({ permission: { read: "allow" } });
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: {
|
|
XDG_CONFIG_HOME: configHome,
|
|
PAPERCLIP_OPENCODE_PROVIDERS: JSON.stringify({ bifrost: "http://gateway.example/v1" }),
|
|
},
|
|
config: {},
|
|
});
|
|
cleanupPaths.add(prepared.env.XDG_CONFIG_HOME);
|
|
const runtimeConfig = JSON.parse(
|
|
await fs.readFile(path.join(prepared.env.XDG_CONFIG_HOME, "opencode", "opencode.json"), "utf8"),
|
|
) as Record<string, unknown>;
|
|
expect(runtimeConfig.provider).toBeUndefined();
|
|
expect(prepared.notes).toContain(
|
|
"PAPERCLIP_OPENCODE_PROVIDERS: skipped provider(s) with non-object values: bifrost.",
|
|
);
|
|
await prepared.cleanup();
|
|
});
|
|
|
|
it("respects explicit opt-out", async () => {
|
|
const configHome = await makeConfigHome();
|
|
const prepared = await prepareOpenCodeRuntimeConfig({
|
|
env: { XDG_CONFIG_HOME: configHome },
|
|
config: { dangerouslySkipPermissions: false },
|
|
});
|
|
|
|
expect(prepared.env).toEqual({ XDG_CONFIG_HOME: configHome });
|
|
expect(prepared.notes).toEqual([]);
|
|
await prepared.cleanup();
|
|
});
|
|
});
|