feat: add opencode local adapter support
This commit is contained in:
@@ -33,6 +33,7 @@
|
||||
"@aws-sdk/client-s3": "^3.888.0",
|
||||
"@paperclipai/adapter-claude-local": "workspace:*",
|
||||
"@paperclipai/adapter-codex-local": "workspace:*",
|
||||
"@paperclipai/adapter-opencode-local": "workspace:*",
|
||||
"@paperclipai/adapter-openclaw": "workspace:*",
|
||||
"@paperclipai/adapter-utils": "workspace:*",
|
||||
"@paperclipai/db": "workspace:*",
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sessionCodec as claudeSessionCodec } from "@paperclipai/adapter-claude-local/server";
|
||||
import { sessionCodec as codexSessionCodec, isCodexUnknownSessionError } from "@paperclipai/adapter-codex-local/server";
|
||||
import {
|
||||
sessionCodec as opencodeSessionCodec,
|
||||
isOpenCodeUnknownSessionError,
|
||||
} from "@paperclipai/adapter-opencode-local/server";
|
||||
|
||||
describe("adapter session codecs", () => {
|
||||
it("normalizes claude session params with cwd", () => {
|
||||
@@ -38,6 +42,24 @@ describe("adapter session codecs", () => {
|
||||
});
|
||||
expect(codexSessionCodec.getDisplayId?.(serialized ?? null)).toBe("codex-session-1");
|
||||
});
|
||||
|
||||
it("normalizes opencode session params with cwd", () => {
|
||||
const parsed = opencodeSessionCodec.deserialize({
|
||||
sessionID: "opencode-session-1",
|
||||
cwd: "/tmp/opencode",
|
||||
});
|
||||
expect(parsed).toEqual({
|
||||
sessionId: "opencode-session-1",
|
||||
cwd: "/tmp/opencode",
|
||||
});
|
||||
|
||||
const serialized = opencodeSessionCodec.serialize(parsed);
|
||||
expect(serialized).toEqual({
|
||||
sessionId: "opencode-session-1",
|
||||
cwd: "/tmp/opencode",
|
||||
});
|
||||
expect(opencodeSessionCodec.getDisplayId?.(serialized ?? null)).toBe("opencode-session-1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("codex resume recovery detection", () => {
|
||||
@@ -62,3 +84,20 @@ describe("codex resume recovery detection", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("opencode resume recovery detection", () => {
|
||||
it("detects unknown session errors from opencode output", () => {
|
||||
expect(
|
||||
isOpenCodeUnknownSessionError(
|
||||
"",
|
||||
"NotFoundError: Resource not found: /Users/test/.local/share/opencode/storage/session/proj/ses_missing.json",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
isOpenCodeUnknownSessionError(
|
||||
"{\"type\":\"step_finish\",\"part\":{\"reason\":\"stop\"}}",
|
||||
"",
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { testEnvironment } from "@paperclipai/adapter-opencode-local/server";
|
||||
|
||||
describe("opencode_local environment diagnostics", () => {
|
||||
it("creates a missing working directory when cwd is absolute", async () => {
|
||||
const cwd = path.join(
|
||||
os.tmpdir(),
|
||||
`paperclip-opencode-local-cwd-${Date.now()}-${Math.random().toString(16).slice(2)}`,
|
||||
"workspace",
|
||||
);
|
||||
|
||||
await fs.rm(path.dirname(cwd), { recursive: true, force: true });
|
||||
|
||||
const result = await testEnvironment({
|
||||
companyId: "company-1",
|
||||
adapterType: "opencode_local",
|
||||
config: {
|
||||
command: process.execPath,
|
||||
cwd,
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.checks.some((check) => check.code === "opencode_cwd_valid")).toBe(true);
|
||||
expect(result.checks.some((check) => check.level === "error")).toBe(false);
|
||||
const stats = await fs.stat(cwd);
|
||||
expect(stats.isDirectory()).toBe(true);
|
||||
await fs.rm(path.dirname(cwd), { recursive: true, force: true });
|
||||
});
|
||||
});
|
||||
224
server/src/__tests__/opencode-local-adapter.test.ts
Normal file
224
server/src/__tests__/opencode-local-adapter.test.ts
Normal file
@@ -0,0 +1,224 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { isOpenCodeUnknownSessionError, parseOpenCodeJsonl } from "@paperclipai/adapter-opencode-local/server";
|
||||
import { parseOpenCodeStdoutLine } from "@paperclipai/adapter-opencode-local/ui";
|
||||
import { printOpenCodeStreamEvent } from "@paperclipai/adapter-opencode-local/cli";
|
||||
|
||||
describe("opencode_local parser", () => {
|
||||
it("extracts session, summary, usage, cost, and terminal error message", () => {
|
||||
const stdout = [
|
||||
JSON.stringify({ type: "step_start", sessionID: "ses_123" }),
|
||||
JSON.stringify({ type: "text", part: { type: "text", text: "hello" } }),
|
||||
JSON.stringify({
|
||||
type: "step_finish",
|
||||
part: {
|
||||
reason: "tool-calls",
|
||||
cost: 0.001,
|
||||
tokens: {
|
||||
input: 100,
|
||||
output: 40,
|
||||
cache: { read: 20, write: 0 },
|
||||
},
|
||||
},
|
||||
}),
|
||||
JSON.stringify({
|
||||
type: "step_finish",
|
||||
part: {
|
||||
reason: "stop",
|
||||
cost: 0.002,
|
||||
tokens: {
|
||||
input: 50,
|
||||
output: 25,
|
||||
cache: { read: 10, write: 0 },
|
||||
},
|
||||
},
|
||||
}),
|
||||
JSON.stringify({ type: "error", message: "model access denied" }),
|
||||
].join("\n");
|
||||
|
||||
const parsed = parseOpenCodeJsonl(stdout);
|
||||
expect(parsed.sessionId).toBe("ses_123");
|
||||
expect(parsed.summary).toBe("hello");
|
||||
expect(parsed.usage).toEqual({
|
||||
inputTokens: 150,
|
||||
cachedInputTokens: 30,
|
||||
outputTokens: 65,
|
||||
});
|
||||
expect(parsed.costUsd).toBeCloseTo(0.003, 6);
|
||||
expect(parsed.errorMessage).toBe("model access denied");
|
||||
});
|
||||
});
|
||||
|
||||
describe("opencode_local stale session detection", () => {
|
||||
it("treats missing persisted session file as an unknown session error", () => {
|
||||
const stderr =
|
||||
"NotFoundError: Resource not found: /Users/test/.local/share/opencode/storage/session/project/ses_missing.json";
|
||||
|
||||
expect(isOpenCodeUnknownSessionError("", stderr)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("opencode_local ui stdout parser", () => {
|
||||
it("parses assistant and tool lifecycle events", () => {
|
||||
const ts = "2026-03-04T00:00:00.000Z";
|
||||
|
||||
expect(
|
||||
parseOpenCodeStdoutLine(
|
||||
JSON.stringify({
|
||||
type: "text",
|
||||
part: {
|
||||
type: "text",
|
||||
text: "I will run a command.",
|
||||
},
|
||||
}),
|
||||
ts,
|
||||
),
|
||||
).toEqual([
|
||||
{
|
||||
kind: "assistant",
|
||||
ts,
|
||||
text: "I will run a command.",
|
||||
},
|
||||
]);
|
||||
|
||||
expect(
|
||||
parseOpenCodeStdoutLine(
|
||||
JSON.stringify({
|
||||
type: "tool_use",
|
||||
part: {
|
||||
id: "prt_tool_1",
|
||||
callID: "call_1",
|
||||
tool: "bash",
|
||||
state: {
|
||||
status: "completed",
|
||||
input: { command: "ls -1" },
|
||||
output: "AGENTS.md\nDockerfile\n",
|
||||
metadata: { exit: 0 },
|
||||
},
|
||||
},
|
||||
}),
|
||||
ts,
|
||||
),
|
||||
).toEqual([
|
||||
{
|
||||
kind: "tool_call",
|
||||
ts,
|
||||
name: "bash",
|
||||
input: { command: "ls -1" },
|
||||
},
|
||||
{
|
||||
kind: "tool_result",
|
||||
ts,
|
||||
toolUseId: "call_1",
|
||||
content: "status: completed\nexit: 0\n\nAGENTS.md\nDockerfile",
|
||||
isError: false,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("parses finished steps into usage-aware results", () => {
|
||||
const ts = "2026-03-04T00:00:00.000Z";
|
||||
expect(
|
||||
parseOpenCodeStdoutLine(
|
||||
JSON.stringify({
|
||||
type: "step_finish",
|
||||
part: {
|
||||
reason: "stop",
|
||||
cost: 0.00042,
|
||||
tokens: {
|
||||
input: 10,
|
||||
output: 5,
|
||||
cache: { read: 2, write: 0 },
|
||||
},
|
||||
},
|
||||
}),
|
||||
ts,
|
||||
),
|
||||
).toEqual([
|
||||
{
|
||||
kind: "result",
|
||||
ts,
|
||||
text: "stop",
|
||||
inputTokens: 10,
|
||||
outputTokens: 5,
|
||||
cachedTokens: 2,
|
||||
costUsd: 0.00042,
|
||||
subtype: "stop",
|
||||
isError: false,
|
||||
errors: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
function stripAnsi(value: string): string {
|
||||
return value.replace(/\x1b\[[0-9;]*m/g, "");
|
||||
}
|
||||
|
||||
describe("opencode_local cli formatter", () => {
|
||||
it("prints step, assistant, tool, and result events", () => {
|
||||
const spy = vi.spyOn(console, "log").mockImplementation(() => {});
|
||||
|
||||
try {
|
||||
printOpenCodeStreamEvent(
|
||||
JSON.stringify({ type: "step_start", sessionID: "ses_abc" }),
|
||||
false,
|
||||
);
|
||||
printOpenCodeStreamEvent(
|
||||
JSON.stringify({
|
||||
type: "text",
|
||||
part: { type: "text", text: "hello" },
|
||||
}),
|
||||
false,
|
||||
);
|
||||
printOpenCodeStreamEvent(
|
||||
JSON.stringify({
|
||||
type: "tool_use",
|
||||
part: {
|
||||
callID: "call_1",
|
||||
tool: "bash",
|
||||
state: {
|
||||
status: "completed",
|
||||
input: { command: "ls -1" },
|
||||
output: "AGENTS.md\n",
|
||||
metadata: { exit: 0 },
|
||||
},
|
||||
},
|
||||
}),
|
||||
false,
|
||||
);
|
||||
printOpenCodeStreamEvent(
|
||||
JSON.stringify({
|
||||
type: "step_finish",
|
||||
part: {
|
||||
reason: "stop",
|
||||
cost: 0.00042,
|
||||
tokens: {
|
||||
input: 10,
|
||||
output: 5,
|
||||
cache: { read: 2, write: 0 },
|
||||
},
|
||||
},
|
||||
}),
|
||||
false,
|
||||
);
|
||||
|
||||
const lines = spy.mock.calls
|
||||
.map((call) => call.map((v) => String(v)).join(" "))
|
||||
.map(stripAnsi);
|
||||
|
||||
expect(lines).toEqual(
|
||||
expect.arrayContaining([
|
||||
"step started (session: ses_abc)",
|
||||
"assistant: hello",
|
||||
"tool_call: bash (call_1)",
|
||||
"tool_result status=completed exit=0",
|
||||
"AGENTS.md",
|
||||
"step finished: reason=stop",
|
||||
"tokens: in=10 out=5 cached=2 cost=$0.000420",
|
||||
]),
|
||||
);
|
||||
} finally {
|
||||
spy.mockRestore();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,12 @@ import {
|
||||
sessionCodec as codexSessionCodec,
|
||||
} from "@paperclipai/adapter-codex-local/server";
|
||||
import { agentConfigurationDoc as codexAgentConfigurationDoc, models as codexModels } from "@paperclipai/adapter-codex-local";
|
||||
import {
|
||||
execute as opencodeExecute,
|
||||
testEnvironment as opencodeTestEnvironment,
|
||||
sessionCodec as opencodeSessionCodec,
|
||||
} from "@paperclipai/adapter-opencode-local/server";
|
||||
import { agentConfigurationDoc as opencodeAgentConfigurationDoc, models as opencodeModels } from "@paperclipai/adapter-opencode-local";
|
||||
import {
|
||||
execute as openclawExecute,
|
||||
testEnvironment as openclawTestEnvironment,
|
||||
@@ -44,6 +50,16 @@ const codexLocalAdapter: ServerAdapterModule = {
|
||||
agentConfigurationDoc: codexAgentConfigurationDoc,
|
||||
};
|
||||
|
||||
const opencodeLocalAdapter: ServerAdapterModule = {
|
||||
type: "opencode_local",
|
||||
execute: opencodeExecute,
|
||||
testEnvironment: opencodeTestEnvironment,
|
||||
sessionCodec: opencodeSessionCodec,
|
||||
models: opencodeModels,
|
||||
supportsLocalAgentJwt: true,
|
||||
agentConfigurationDoc: opencodeAgentConfigurationDoc,
|
||||
};
|
||||
|
||||
const openclawAdapter: ServerAdapterModule = {
|
||||
type: "openclaw",
|
||||
execute: openclawExecute,
|
||||
@@ -54,7 +70,7 @@ const openclawAdapter: ServerAdapterModule = {
|
||||
};
|
||||
|
||||
const adaptersByType = new Map<string, ServerAdapterModule>(
|
||||
[claudeLocalAdapter, codexLocalAdapter, openclawAdapter, processAdapter, httpAdapter].map((a) => [a.type, a]),
|
||||
[claudeLocalAdapter, codexLocalAdapter, opencodeLocalAdapter, openclawAdapter, processAdapter, httpAdapter].map((a) => [a.type, a]),
|
||||
);
|
||||
|
||||
export function getServerAdapter(type: string): ServerAdapterModule {
|
||||
|
||||
@@ -36,11 +36,13 @@ import {
|
||||
DEFAULT_CODEX_LOCAL_BYPASS_APPROVALS_AND_SANDBOX,
|
||||
DEFAULT_CODEX_LOCAL_MODEL,
|
||||
} from "@paperclipai/adapter-codex-local";
|
||||
import { DEFAULT_OPENCODE_LOCAL_MODEL } from "@paperclipai/adapter-opencode-local";
|
||||
|
||||
export function agentRoutes(db: Db) {
|
||||
const DEFAULT_INSTRUCTIONS_PATH_KEYS: Record<string, string> = {
|
||||
claude_local: "instructionsFilePath",
|
||||
codex_local: "instructionsFilePath",
|
||||
opencode_local: "instructionsFilePath",
|
||||
};
|
||||
const KNOWN_INSTRUCTIONS_PATH_KEYS = new Set(["instructionsFilePath", "agentsMdPath"]);
|
||||
|
||||
@@ -178,17 +180,21 @@ export function agentRoutes(db: Db) {
|
||||
adapterType: string | null | undefined,
|
||||
adapterConfig: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
if (adapterType !== "codex_local") return adapterConfig;
|
||||
|
||||
const next = { ...adapterConfig };
|
||||
if (!asNonEmptyString(next.model)) {
|
||||
next.model = DEFAULT_CODEX_LOCAL_MODEL;
|
||||
if (adapterType === "codex_local") {
|
||||
if (!asNonEmptyString(next.model)) {
|
||||
next.model = DEFAULT_CODEX_LOCAL_MODEL;
|
||||
}
|
||||
const hasBypassFlag =
|
||||
typeof next.dangerouslyBypassApprovalsAndSandbox === "boolean" ||
|
||||
typeof next.dangerouslyBypassSandbox === "boolean";
|
||||
if (!hasBypassFlag) {
|
||||
next.dangerouslyBypassApprovalsAndSandbox = DEFAULT_CODEX_LOCAL_BYPASS_APPROVALS_AND_SANDBOX;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
const hasBypassFlag =
|
||||
typeof next.dangerouslyBypassApprovalsAndSandbox === "boolean" ||
|
||||
typeof next.dangerouslyBypassSandbox === "boolean";
|
||||
if (!hasBypassFlag) {
|
||||
next.dangerouslyBypassApprovalsAndSandbox = DEFAULT_CODEX_LOCAL_BYPASS_APPROVALS_AND_SANDBOX;
|
||||
if (adapterType === "opencode_local" && !asNonEmptyString(next.model)) {
|
||||
next.model = DEFAULT_OPENCODE_LOCAL_MODEL;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
@@ -70,6 +70,10 @@ const ADAPTER_DEFAULT_RULES_BY_TYPE: Record<string, Array<{ path: string[]; valu
|
||||
{ path: ["timeoutSec"], value: 0 },
|
||||
{ path: ["graceSec"], value: 15 },
|
||||
],
|
||||
opencode_local: [
|
||||
{ path: ["timeoutSec"], value: 0 },
|
||||
{ path: ["graceSec"], value: 15 },
|
||||
],
|
||||
claude_local: [
|
||||
{ path: ["timeoutSec"], value: 0 },
|
||||
{ path: ["graceSec"], value: 15 },
|
||||
|
||||
Reference in New Issue
Block a user