fix(opencode): restore stable v1 runtime

This commit is contained in:
2026-08-05 17:59:35 +08:00
parent 764a1f4e82
commit 1407dd3bbe
33 changed files with 1106 additions and 1668 deletions
+39 -82
View File
@@ -1,57 +1,34 @@
import { describe, expect, it } from "bun:test";
import { type OpenCodeClient } from "@opencode-ai/client";
import { type OpencodeClient } from "@opencode-ai/sdk/v2";
import {
getEmbeddedServicePaths,
OpencodeRuntimeAdapter,
} from "../../src/runtime/opencode.js";
describe("getEmbeddedServicePaths", () => {
it("isolates service registrations by workspace and Agent port", () => {
const internal = getEmbeddedServicePaths("/srv/tjwater-agent", 8787);
const customer = getEmbeddedServicePaths("/srv/tjwater-agent-customer", 8787);
const secondPort = getEmbeddedServicePaths("/srv/tjwater-agent", 8788);
expect(internal.registrationFile).not.toBe(customer.registrationFile);
expect(internal.registrationFile).not.toBe(secondPort.registrationFile);
expect(internal.registrationFile).toEndWith(
"/data/opencode-service/8787/opencode/service.json",
);
});
});
import { config } from "../../src/config.js";
import { OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
const createRuntimeAdapter = (
messages: unknown[],
calls: { staged: string[]; committed: string[] } = {
staged: [],
committed: [],
},
calls: {
reverted: string[];
removed: string[];
} = { reverted: [], removed: [] },
) =>
Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
messages: async () => messages,
ensureClient: async () =>
({
session: {
revert: {
stage: async ({ messageID }: { messageID: string }) => {
calls.staged.push(messageID);
},
commit: async ({ sessionID }: { sessionID: string }) => {
calls.committed.push(sessionID);
},
},
},
}) as unknown as OpenCodeClient,
revertMessage: async (_sessionId: string, messageId: string) => {
calls.reverted.push(messageId);
},
removeMessage: async (_sessionId: string, messageId: string) => {
calls.removed.push(messageId);
},
}) as OpencodeRuntimeAdapter;
describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
it("skips reverting the first user message when the runtime session is empty", async () => {
const calls = { staged: [] as string[], committed: [] as string[] };
const calls = { reverted: [] as string[], removed: [] as string[] };
const runtime = createRuntimeAdapter([], calls);
await runtime.revertToUserMessage("session-1", { userOrdinal: 1 });
expect(calls).toEqual({ staged: [], committed: [] });
expect(calls).toEqual({ reverted: [], removed: [] });
});
it("keeps ordinal mismatches visible when runtime messages exist", async () => {
@@ -65,8 +42,8 @@ describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
).rejects.toThrow("target user message not found to revert");
});
it("stages and commits the V2 revert at the target user message", async () => {
const calls = { staged: [] as string[], committed: [] as string[] };
it("reverts and removes messages from the target user message onward", async () => {
const calls = { reverted: [] as string[], removed: [] as string[] };
const runtime = createRuntimeAdapter(
[
{ info: { id: "user-1", role: "user" } },
@@ -80,8 +57,8 @@ describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
await runtime.revertToUserMessage("session-1", { userOrdinal: 2 });
expect(calls).toEqual({
staged: ["user-2"],
committed: ["session-1"],
reverted: ["user-2"],
removed: ["assistant-2", "user-2"],
});
});
});
@@ -90,10 +67,11 @@ describe("OpencodeRuntimeAdapter.ensureClient", () => {
it("retries bootstrap after a failed startup attempt", async () => {
let attempts = 0;
const client = {
health: { get: async () => ({ healthy: true }) },
} as unknown as OpenCodeClient;
global: { health: async () => ({ data: { healthy: true } }) },
} as unknown as OpencodeClient;
const runtime = Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
clientPromise: null,
closeServer: null,
bootstrapClient: async () => {
attempts += 1;
if (attempts === 1) {
@@ -110,66 +88,45 @@ describe("OpencodeRuntimeAdapter.ensureClient", () => {
});
describe("OpencodeRuntimeAdapter.warmup", () => {
it("rejects a service that is not the pinned V2 release", async () => {
const client = {
health: {
get: async () => ({ healthy: true, version: "1.18.12" }),
},
} as unknown as OpenCodeClient;
const runtime = Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
clientPromise: null,
ensureClient: async () => client,
}) as OpencodeRuntimeAdapter;
await expect(runtime.warmup()).rejects.toThrow(
"incompatible OpenCode service version",
);
});
it("checks V2 health, catalogs and plugins, then removes the probe session", async () => {
it("initializes the project session and model tools before reporting ready", async () => {
const calls: string[] = [];
const client = {
health: {
get: async () => {
calls.push("health.get");
return { healthy: true, version: "0.0.0-next-16741" };
global: {
health: async () => {
calls.push("health");
return { data: { healthy: true, version: "test" } };
},
},
session: {
create: async () => {
calls.push("session.create");
return { id: "warmup-session" };
return { data: { id: "warmup-session" } };
},
remove: async ({ sessionID }: { sessionID: string }) => {
calls.push(`session.remove:${sessionID}`);
delete: async ({ sessionID }: { sessionID: string }) => {
calls.push(`session.delete:${sessionID}`);
return { data: true };
},
},
model: {
list: async () => {
calls.push("model.list");
tool: {
list: async (model: { provider: string; model: string }) => {
calls.push(`tool.list:${model.provider}/${model.model}`);
return { data: [] };
},
},
plugin: {
list: async () => {
calls.push("plugin.list");
return [];
},
},
} as unknown as OpenCodeClient;
} as unknown as OpencodeClient;
const runtime = Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
clientPromise: null,
closeServer: null,
ensureClient: async () => client,
}) as OpencodeRuntimeAdapter;
await runtime.warmup();
expect(calls).toEqual([
"health.get",
"health",
"session.create",
"model.list",
"plugin.list",
"session.remove:warmup-session",
`tool.list:${config.OPENCODE_MODEL}`,
"session.delete:warmup-session",
]);
});
});
-193
View File
@@ -1,193 +0,0 @@
import { describe, expect, it } from "bun:test";
import { type OpenCodeClient } from "@opencode-ai/client";
import { OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
const createRuntimeAdapter = (client: unknown) =>
Object.assign(new OpencodeRuntimeAdapter(), {
clientPromise: Promise.resolve(client as OpenCodeClient),
}) as OpencodeRuntimeAdapter;
const createEventStream = (events: unknown[]) => ({
async *[Symbol.asyncIterator]() {
for (const event of events) {
yield event;
}
},
});
describe("OpencodeRuntimeAdapter V2 interaction responses", () => {
it("replies through the top-level V2 permission API", async () => {
const runtime = createRuntimeAdapter({
permission: {
reply: async (parameters: Record<string, unknown>) => {
expect(parameters).toEqual({
sessionID: "session-1",
requestID: "request-1",
reply: "once",
message: "approved",
});
},
},
});
await expect(
runtime.replyPermission({
requestId: "request-1",
sessionId: "session-1",
reply: "once",
message: "approved",
}),
).resolves.toBeUndefined();
});
it("replies through the top-level V2 question API", async () => {
const runtime = createRuntimeAdapter({
question: {
reply: async (parameters: Record<string, unknown>) => {
expect(parameters).toEqual({
sessionID: "session-1",
requestID: "request-1",
answers: [["A"]],
});
},
},
});
await expect(
runtime.replyQuestion({
requestId: "request-1",
sessionId: "session-1",
answers: [["A"]],
}),
).resolves.toBeUndefined();
});
it("rejects through the top-level V2 question API", async () => {
const runtime = createRuntimeAdapter({
question: {
reject: async (parameters: Record<string, unknown>) => {
expect(parameters).toEqual({
sessionID: "session-1",
requestID: "request-1",
});
},
},
});
await expect(
runtime.rejectQuestion({
requestId: "request-1",
sessionId: "session-1",
}),
).resolves.toBeUndefined();
});
it("maps frontend question answers to the V2 Form API", async () => {
const runtime = createRuntimeAdapter({
form: {
get: async (parameters: Record<string, unknown>) => {
expect(parameters).toEqual({ sessionID: "session-1", formID: "frm_1" });
return {
fields: [
{
key: "scope",
type: "string",
title: "范围",
options: [{ value: "urban", label: "城区" }],
},
{ key: "confirmed", type: "boolean", title: "确认" },
],
};
},
reply: async (parameters: Record<string, unknown>) => {
expect(parameters).toEqual({
sessionID: "session-1",
formID: "frm_1",
answer: { scope: "urban", confirmed: true },
});
},
},
});
await expect(
runtime.replyQuestion({
requestId: "frm_1",
sessionId: "session-1",
answers: [["城区"], ["是"]],
}),
).resolves.toBeUndefined();
});
it("cancels V2 Form requests by form ID", async () => {
const runtime = createRuntimeAdapter({
form: {
cancel: async (parameters: Record<string, unknown>) => {
expect(parameters).toEqual({ sessionID: "session-1", formID: "frm_1" });
},
},
});
await expect(
runtime.rejectQuestion({ requestId: "frm_1", sessionId: "session-1" }),
).resolves.toBeUndefined();
});
it("normalizes V2 Form and execution events for the chat stream", async () => {
const runtime = createRuntimeAdapter({
event: {
subscribe: () =>
createEventStream([
{
type: "form.created",
data: {
form: {
id: "frm_1",
sessionID: "session-1",
title: "分析参数",
fields: [
{
key: "scope",
type: "string",
title: "范围",
description: "选择分析范围",
options: [{ value: "urban", label: "城区" }],
},
],
},
},
},
{
type: "session.execution.succeeded",
data: { sessionID: "session-1" },
},
]),
},
});
const events = [];
for await (const event of await runtime.subscribeEvents()) {
events.push(event);
}
expect(events).toEqual([
{
type: "question.asked",
sessionId: "session-1",
request: {
id: "frm_1",
questions: [
{
header: "范围",
question: "选择分析范围",
options: [{ label: "城区", description: "" }],
multiple: false,
custom: false,
},
],
},
},
{ type: "session.execution.succeeded", sessionId: "session-1" },
]);
});
});