Add persistent OpenCode permission actions (#1168)
This commit is contained in:
@@ -0,0 +1,90 @@
|
|||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
import { createTestLogger } from "../../../test-utils/test-logger.js";
|
||||||
|
import { OpenCodeAgentClient } from "./opencode-agent.js";
|
||||||
|
import {
|
||||||
|
idleEvent,
|
||||||
|
TestOpenCodeClient,
|
||||||
|
TestOpenCodeRuntime,
|
||||||
|
} from "./opencode/test-utils/test-opencode-runtime.js";
|
||||||
|
|
||||||
|
function mockOpenCodeClient(events: unknown[]) {
|
||||||
|
const runtime = new TestOpenCodeRuntime();
|
||||||
|
const openCodeClient = new TestOpenCodeClient();
|
||||||
|
openCodeClient.sessionPromptAsyncEvents = events;
|
||||||
|
runtime.enqueueClient(openCodeClient);
|
||||||
|
|
||||||
|
return { openCodeClient, runtime };
|
||||||
|
}
|
||||||
|
|
||||||
|
function toolPermissionEvent(): unknown {
|
||||||
|
return {
|
||||||
|
type: "permission.asked",
|
||||||
|
properties: {
|
||||||
|
id: "permission-1",
|
||||||
|
sessionID: "session-1",
|
||||||
|
permission: "external_directory",
|
||||||
|
patterns: ["/tmp/outside/*"],
|
||||||
|
metadata: {
|
||||||
|
reason: "Inspect files outside the project",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("OpenCode permission actions", () => {
|
||||||
|
test("allow always sends OpenCode's always reply", async () => {
|
||||||
|
const { openCodeClient, runtime } = mockOpenCodeClient([toolPermissionEvent(), idleEvent()]);
|
||||||
|
const client = new OpenCodeAgentClient(createTestLogger(), undefined, { runtime });
|
||||||
|
const session = await client.createSession({
|
||||||
|
provider: "opencode",
|
||||||
|
cwd: "/tmp/project",
|
||||||
|
modeId: "build",
|
||||||
|
});
|
||||||
|
|
||||||
|
await session.run("Inspect outside files");
|
||||||
|
const permission = session.getPendingPermissions()[0]!;
|
||||||
|
await session.respondToPermission(permission.id, {
|
||||||
|
behavior: "allow",
|
||||||
|
selectedActionId: "allow_always",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(openCodeClient.calls.permissionReply).toEqual([
|
||||||
|
{
|
||||||
|
requestID: "permission-1",
|
||||||
|
directory: "/tmp/project",
|
||||||
|
reply: "always",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
expect(session.getPendingPermissions()).toEqual([]);
|
||||||
|
|
||||||
|
await session.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("plain allow keeps the backward-compatible once reply", async () => {
|
||||||
|
const { openCodeClient, runtime } = mockOpenCodeClient([toolPermissionEvent(), idleEvent()]);
|
||||||
|
const client = new OpenCodeAgentClient(createTestLogger(), undefined, { runtime });
|
||||||
|
const session = await client.createSession({
|
||||||
|
provider: "opencode",
|
||||||
|
cwd: "/tmp/project",
|
||||||
|
modeId: "build",
|
||||||
|
});
|
||||||
|
|
||||||
|
await session.run("Inspect outside files");
|
||||||
|
const permission = session.getPendingPermissions()[0]!;
|
||||||
|
await session.respondToPermission(permission.id, {
|
||||||
|
behavior: "allow",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(openCodeClient.calls.permissionReply).toEqual([
|
||||||
|
{
|
||||||
|
requestID: "permission-1",
|
||||||
|
directory: "/tmp/project",
|
||||||
|
reply: "once",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
expect(session.getPendingPermissions()).toEqual([]);
|
||||||
|
|
||||||
|
await session.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -24,6 +24,7 @@ import {
|
|||||||
type AgentLaunchContext,
|
type AgentLaunchContext,
|
||||||
type AgentMode,
|
type AgentMode,
|
||||||
type AgentModelDefinition,
|
type AgentModelDefinition,
|
||||||
|
type AgentPermissionAction,
|
||||||
type AgentPermissionRequest,
|
type AgentPermissionRequest,
|
||||||
type AgentPermissionResponse,
|
type AgentPermissionResponse,
|
||||||
type AgentPersistenceHandle,
|
type AgentPersistenceHandle,
|
||||||
@@ -85,6 +86,8 @@ const OPENCODE_BUILD_MODE_ID = "build";
|
|||||||
const OPENCODE_FULL_ACCESS_MODE_ID = "full-access";
|
const OPENCODE_FULL_ACCESS_MODE_ID = "full-access";
|
||||||
const OPENCODE_PERSISTED_SESSION_LIMIT = 200;
|
const OPENCODE_PERSISTED_SESSION_LIMIT = 200;
|
||||||
const OPENCODE_PENDING_ABORT_START_TIMEOUT_MS = 10_000;
|
const OPENCODE_PENDING_ABORT_START_TIMEOUT_MS = 10_000;
|
||||||
|
const OPENCODE_PERMISSION_ACTION_ALLOW_ONCE = "allow_once";
|
||||||
|
const OPENCODE_PERMISSION_ACTION_ALLOW_ALWAYS = "allow_always";
|
||||||
|
|
||||||
const DEFAULT_MODES: AgentMode[] = [
|
const DEFAULT_MODES: AgentMode[] = [
|
||||||
{
|
{
|
||||||
@@ -104,6 +107,44 @@ const DEFAULT_MODES: AgentMode[] = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
function buildOpenCodePermissionActions(): AgentPermissionAction[] {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
id: "deny",
|
||||||
|
label: "Deny",
|
||||||
|
behavior: "deny",
|
||||||
|
variant: "danger",
|
||||||
|
intent: "dismiss",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: OPENCODE_PERMISSION_ACTION_ALLOW_ALWAYS,
|
||||||
|
label: "Allow always",
|
||||||
|
behavior: "allow",
|
||||||
|
variant: "secondary",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: OPENCODE_PERMISSION_ACTION_ALLOW_ONCE,
|
||||||
|
label: "Allow once",
|
||||||
|
behavior: "allow",
|
||||||
|
variant: "primary",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveOpenCodePermissionReply(
|
||||||
|
response: AgentPermissionResponse,
|
||||||
|
): "once" | "always" | "reject" {
|
||||||
|
if (response.behavior === "deny") {
|
||||||
|
return "reject";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.selectedActionId === OPENCODE_PERMISSION_ACTION_ALLOW_ALWAYS) {
|
||||||
|
return "always";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "once";
|
||||||
|
}
|
||||||
|
|
||||||
type OpenCodeAgentConfig = AgentSessionConfig & { provider: "opencode" };
|
type OpenCodeAgentConfig = AgentSessionConfig & { provider: "opencode" };
|
||||||
type OpenCodeMessageRole = "user" | "assistant";
|
type OpenCodeMessageRole = "user" | "assistant";
|
||||||
type OpenCodePersistedSession = OpenCodeSession | OpenCodeGlobalSession;
|
type OpenCodePersistedSession = OpenCodeSession | OpenCodeGlobalSession;
|
||||||
@@ -2234,6 +2275,7 @@ function appendOpenCodePermissionAsked(
|
|||||||
...(description ? { description } : {}),
|
...(description ? { description } : {}),
|
||||||
input,
|
input,
|
||||||
detail,
|
detail,
|
||||||
|
actions: buildOpenCodePermissionActions(),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -3130,7 +3172,7 @@ class OpenCodeAgentSession implements AgentSession {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const reply = response.behavior === "allow" ? "once" : "reject";
|
const reply = resolveOpenCodePermissionReply(response);
|
||||||
await this.client.permission.reply({
|
await this.client.permission.reply({
|
||||||
requestID: requestId,
|
requestID: requestId,
|
||||||
directory: this.config.cwd,
|
directory: this.config.cwd,
|
||||||
|
|||||||
@@ -2,6 +2,28 @@ import { describe, expect, it } from "vitest";
|
|||||||
|
|
||||||
import { translateOpenCodeEvent, type OpenCodeEventTranslationState } from "../opencode-agent.js";
|
import { translateOpenCodeEvent, type OpenCodeEventTranslationState } from "../opencode-agent.js";
|
||||||
|
|
||||||
|
const openCodePermissionActions = [
|
||||||
|
{
|
||||||
|
id: "deny",
|
||||||
|
label: "Deny",
|
||||||
|
behavior: "deny",
|
||||||
|
variant: "danger",
|
||||||
|
intent: "dismiss",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "allow_always",
|
||||||
|
label: "Allow always",
|
||||||
|
behavior: "allow",
|
||||||
|
variant: "secondary",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "allow_once",
|
||||||
|
label: "Allow once",
|
||||||
|
behavior: "allow",
|
||||||
|
variant: "primary",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
function createState(sessionId = "session-1"): OpenCodeEventTranslationState {
|
function createState(sessionId = "session-1"): OpenCodeEventTranslationState {
|
||||||
return {
|
return {
|
||||||
sessionId,
|
sessionId,
|
||||||
@@ -306,6 +328,7 @@ describe("translateOpenCodeEvent", () => {
|
|||||||
type: "shell",
|
type: "shell",
|
||||||
command: "ls /home/user/secrets",
|
command: "ls /home/user/secrets",
|
||||||
},
|
},
|
||||||
|
actions: openCodePermissionActions,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
@@ -358,6 +381,7 @@ describe("translateOpenCodeEvent", () => {
|
|||||||
},
|
},
|
||||||
output: null,
|
output: null,
|
||||||
},
|
},
|
||||||
|
actions: openCodePermissionActions,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user