Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/opencode/src/acp/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,21 @@ import { ShellID } from "@/tool/shell/id"

type ModeOption = { id: string; name: string; description?: string }
type ModelOption = { modelId: string; name: string }
export type CommandOption = { name: string; description: string }
const decodeTodos = Schema.decodeUnknownResult(Schema.fromJsonString(Schema.Array(Todo.Info)))

const DEFAULT_VARIANT_VALUE = "default"

export function addCommandFallbacks(commands: CommandOption[]) {
const names = new Set(commands.map((c) => c.name))
const fallbacks: CommandOption[] = [
{ name: "compact", description: "compact the session" },
{ name: "model", description: "change the session model" },
{ name: "mode", description: "change the session mode" },
]
commands.push(...fallbacks.filter((command) => !names.has(command.name)))
}

const log = Log.create({ service: "acp-agent" })

async function getContextLimit(
Expand Down Expand Up @@ -1137,12 +1148,7 @@ export class Agent implements ACPAgent {
name: command.name,
description: command.description ?? "",
}))
const names = new Set(availableCommands.map((c) => c.name))
if (!names.has("compact"))
availableCommands.push({
name: "compact",
description: "compact the session",
})
addCommandFallbacks(availableCommands)

const mcpServers: Record<string, ConfigMCP.Info> = {}
for (const server of params.mcpServers) {
Expand Down
21 changes: 21 additions & 0 deletions packages/opencode/test/acp/command-fallbacks.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, test } from "bun:test"
import { ACP } from "../../src/acp/agent"

describe("acp command fallbacks", () => {
test("registers built-in session commands when they are missing", () => {
const commands: ACP.CommandOption[] = []

ACP.addCommandFallbacks(commands)

expect(commands.map((command) => command.name)).toEqual(["compact", "model", "mode"])
})

test("does not duplicate commands returned by the server", () => {
const commands: ACP.CommandOption[] = [{ name: "model", description: "custom model command" }]

ACP.addCommandFallbacks(commands)

expect(commands.filter((command) => command.name === "model")).toHaveLength(1)
expect(commands.map((command) => command.name)).toEqual(["model", "compact", "mode"])
})
})
Loading