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: 18 additions & 0 deletions packages/opencode/src/cli/cmd/tui/component/dialog-command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export type CommandOption = DialogSelectOption<string> & {
enabled?: boolean
}

function matchesSlash(option: CommandOption, name: string) {
const slash = option.slash
if (!slash) return false
return slash.name === name || !!slash.aliases?.includes(name)
}

function init() {
const root = getOwner()
const [registrations, setRegistrations] = createSignal<Accessor<CommandOption[]>[]>([])
Expand Down Expand Up @@ -84,6 +90,18 @@ function init() {
}
}
},
hasSlash(name: string) {
return entries().some((option) => isVisible(option) && matchesSlash(option, name))
},
executeSlash(name: string) {
for (const option of entries()) {
if (!isVisible(option)) continue
if (!matchesSlash(option, name)) continue
option.onSelect?.(dialog)
return true
}
return false
},
slashes() {
return visibleOptions().flatMap((option) => {
const slash = option.slash
Expand Down
12 changes: 12 additions & 0 deletions packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useKeybind } from "@tui/context/keybind"
import { usePromptHistory, type PromptInfo } from "./history"
import { computePromptTraits } from "./traits"
import { assign } from "./part"
import { getLocalSlashCommand } from "./slash"
import { usePromptStash } from "./stash"
import { DialogStash } from "../dialog-stash"
import { type AutocompleteRef, Autocomplete } from "./autocomplete"
Expand Down Expand Up @@ -827,6 +828,17 @@ export function Prompt(props: PromptProps) {
void exit()
return true
}
const localSlash = getLocalSlashCommand(store.prompt.input, command.hasSlash)
if (localSlash && command.executeSlash(localSlash)) {
input.extmarks.clear()
input.clear()
setStore("prompt", {
input: "",
parts: [],
})
setStore("extmarkToPartIndex", new Map())
return true
}
const selectedModel = local.model.current()
if (!selectedModel) {
void promptModelWarning()
Expand Down
10 changes: 10 additions & 0 deletions packages/opencode/src/cli/cmd/tui/component/prompt/slash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function getLocalSlashCommand(input: string, hasSlash: (name: string) => boolean) {
const trimmed = input.trim()
if (!trimmed.startsWith("/")) return
if (trimmed.includes(" ") || trimmed.includes("\n")) return

const name = trimmed.slice(1)
if (!name) return

return hasSlash(name) ? name : undefined
}
26 changes: 26 additions & 0 deletions packages/opencode/test/cli/cmd/tui/prompt-slash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, test } from "bun:test"
import { getLocalSlashCommand } from "../../../../src/cli/cmd/tui/component/prompt/slash"

describe("getLocalSlashCommand", () => {
test("returns a standalone local slash command name", () => {
expect(getLocalSlashCommand(" /spec ", (name) => name === "spec")).toBe("spec")
})

test("does not intercept unknown standalone slash commands", () => {
expect(getLocalSlashCommand("/review", () => false)).toBeUndefined()
})

test("does not intercept slash commands with arguments or multiline content", () => {
const hasSlash = () => true

expect(getLocalSlashCommand("/spec draft", hasSlash)).toBeUndefined()
expect(getLocalSlashCommand("/spec\ndraft", hasSlash)).toBeUndefined()
})

test("does not intercept bare slashes or regular text", () => {
const hasSlash = () => true

expect(getLocalSlashCommand("/", hasSlash)).toBeUndefined()
expect(getLocalSlashCommand("spec", hasSlash)).toBeUndefined()
})
})
Loading