feat(opencode): migrate agent runtime to v2
This commit is contained in:
@@ -1,34 +1,57 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import { type OpencodeClient } from "@opencode-ai/sdk/v2";
|
||||
import { type OpenCodeClient } from "@opencode-ai/client";
|
||||
|
||||
import { config } from "../../src/config.js";
|
||||
import { OpencodeRuntimeAdapter } from "../../src/runtime/opencode.js";
|
||||
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",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
const createRuntimeAdapter = (
|
||||
messages: unknown[],
|
||||
calls: {
|
||||
reverted: string[];
|
||||
removed: string[];
|
||||
} = { reverted: [], removed: [] },
|
||||
calls: { staged: string[]; committed: string[] } = {
|
||||
staged: [],
|
||||
committed: [],
|
||||
},
|
||||
) =>
|
||||
Object.assign(Object.create(OpencodeRuntimeAdapter.prototype), {
|
||||
messages: async () => messages,
|
||||
revertMessage: async (_sessionId: string, messageId: string) => {
|
||||
calls.reverted.push(messageId);
|
||||
},
|
||||
removeMessage: async (_sessionId: string, messageId: string) => {
|
||||
calls.removed.push(messageId);
|
||||
},
|
||||
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,
|
||||
}) as OpencodeRuntimeAdapter;
|
||||
|
||||
describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
|
||||
it("skips reverting the first user message when the runtime session is empty", async () => {
|
||||
const calls = { reverted: [] as string[], removed: [] as string[] };
|
||||
const calls = { staged: [] as string[], committed: [] as string[] };
|
||||
const runtime = createRuntimeAdapter([], calls);
|
||||
|
||||
await runtime.revertToUserMessage("session-1", { userOrdinal: 1 });
|
||||
|
||||
expect(calls).toEqual({ reverted: [], removed: [] });
|
||||
expect(calls).toEqual({ staged: [], committed: [] });
|
||||
});
|
||||
|
||||
it("keeps ordinal mismatches visible when runtime messages exist", async () => {
|
||||
@@ -42,8 +65,8 @@ describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
|
||||
).rejects.toThrow("target user message not found to revert");
|
||||
});
|
||||
|
||||
it("reverts and removes messages from the target user message onward", async () => {
|
||||
const calls = { reverted: [] as string[], removed: [] as string[] };
|
||||
it("stages and commits the V2 revert at the target user message", async () => {
|
||||
const calls = { staged: [] as string[], committed: [] as string[] };
|
||||
const runtime = createRuntimeAdapter(
|
||||
[
|
||||
{ info: { id: "user-1", role: "user" } },
|
||||
@@ -57,8 +80,8 @@ describe("OpencodeRuntimeAdapter.revertToUserMessage", () => {
|
||||
await runtime.revertToUserMessage("session-1", { userOrdinal: 2 });
|
||||
|
||||
expect(calls).toEqual({
|
||||
reverted: ["user-2"],
|
||||
removed: ["assistant-2", "user-2"],
|
||||
staged: ["user-2"],
|
||||
committed: ["session-1"],
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -67,11 +90,10 @@ describe("OpencodeRuntimeAdapter.ensureClient", () => {
|
||||
it("retries bootstrap after a failed startup attempt", async () => {
|
||||
let attempts = 0;
|
||||
const client = {
|
||||
global: { health: async () => ({ data: { healthy: true } }) },
|
||||
} as unknown as OpencodeClient;
|
||||
health: { get: async () => ({ 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) {
|
||||
@@ -88,45 +110,66 @@ describe("OpencodeRuntimeAdapter.ensureClient", () => {
|
||||
});
|
||||
|
||||
describe("OpencodeRuntimeAdapter.warmup", () => {
|
||||
it("initializes the project session and model tools before reporting ready", async () => {
|
||||
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 () => {
|
||||
const calls: string[] = [];
|
||||
const client = {
|
||||
global: {
|
||||
health: async () => {
|
||||
calls.push("health");
|
||||
return { data: { healthy: true, version: "test" } };
|
||||
health: {
|
||||
get: async () => {
|
||||
calls.push("health.get");
|
||||
return { healthy: true, version: "0.0.0-next-16741" };
|
||||
},
|
||||
},
|
||||
session: {
|
||||
create: async () => {
|
||||
calls.push("session.create");
|
||||
return { data: { id: "warmup-session" } };
|
||||
return { id: "warmup-session" };
|
||||
},
|
||||
delete: async ({ sessionID }: { sessionID: string }) => {
|
||||
calls.push(`session.delete:${sessionID}`);
|
||||
return { data: true };
|
||||
remove: async ({ sessionID }: { sessionID: string }) => {
|
||||
calls.push(`session.remove:${sessionID}`);
|
||||
},
|
||||
},
|
||||
tool: {
|
||||
list: async (model: { provider: string; model: string }) => {
|
||||
calls.push(`tool.list:${model.provider}/${model.model}`);
|
||||
model: {
|
||||
list: async () => {
|
||||
calls.push("model.list");
|
||||
return { data: [] };
|
||||
},
|
||||
},
|
||||
} as unknown as OpencodeClient;
|
||||
plugin: {
|
||||
list: async () => {
|
||||
calls.push("plugin.list");
|
||||
return [];
|
||||
},
|
||||
},
|
||||
} 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",
|
||||
"health.get",
|
||||
"session.create",
|
||||
`tool.list:${config.OPENCODE_MODEL}`,
|
||||
"session.delete:warmup-session",
|
||||
"model.list",
|
||||
"plugin.list",
|
||||
"session.remove:warmup-session",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
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" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user