Skip to content
Closed
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
1 change: 1 addition & 0 deletions packages/tui/src/component/prompt/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ export function Autocomplete(props: {
}

function select() {
if (store.visible === "/" && !search()) return
const selected = options()[store.selected]
if (!selected) return
hide()
Expand Down
19 changes: 6 additions & 13 deletions packages/tui/src/component/prompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { useTuiConfig } from "../../config"
import { usePromptWorkspace } from "./workspace"
import { usePromptMove } from "./move"
import { readLocalAttachment } from "./local-attachment"
import { resolvePromptSlashCommand } from "./slash-command"

export type PromptProps = {
sessionID?: string
Expand Down Expand Up @@ -1050,6 +1051,8 @@ export function Prompt(props: PromptProps) {
]
: []

const command = resolvePromptSlashCommand(inputText, sync.data.command)

if (store.mode === "shell") {
move.startSubmit()
void sdk.client.session.shell({
Expand All @@ -1062,22 +1065,12 @@ export function Prompt(props: PromptProps) {
command: inputText,
})
setStore("mode", "normal")
} else if (
inputText.startsWith("/") &&
sync.data.command.some((x) => x.name === inputText.split("\n")[0].split(" ")[0].slice(1))
) {
} else if (command) {
move.startSubmit()
// Parse command from first line, preserve multi-line content in arguments
const firstLineEnd = inputText.indexOf("\n")
const firstLine = firstLineEnd === -1 ? inputText : inputText.slice(0, firstLineEnd)
const [command, ...firstLineArgs] = firstLine.split(" ")
const restOfInput = firstLineEnd === -1 ? "" : inputText.slice(firstLineEnd + 1)
const args = firstLineArgs.join(" ") + (restOfInput ? "\n" + restOfInput : "")

void sdk.client.session.command({
sessionID,
command: command.slice(1),
arguments: args,
command: command.name,
arguments: command.arguments,
agent: agent.name,
model: `${selectedModel.providerID}/${selectedModel.modelID}`,
variant,
Expand Down
21 changes: 21 additions & 0 deletions packages/tui/src/component/prompt/slash-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export type SlashCommand = {
name: string
}

export function resolvePromptSlashCommand(inputText: string, commands: readonly SlashCommand[]) {
if (!inputText.startsWith("/")) return

const firstLineEnd = inputText.indexOf("\n")
const firstLine = firstLineEnd === -1 ? inputText : inputText.slice(0, firstLineEnd)
const [rawCommand, ...firstLineArgs] = firstLine.split(" ")
const name = rawCommand.slice(1)
if (!name) return
if (!commands.some((command) => command.name === name)) return

const restOfInput = firstLineEnd === -1 ? "" : inputText.slice(firstLineEnd + 1)
const args = firstLineArgs.join(" ") + (restOfInput ? "\n" + restOfInput : "")
return {
name,
arguments: args,
}
}
32 changes: 32 additions & 0 deletions packages/tui/test/prompt/slash-command.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, test } from "bun:test"
import { resolvePromptSlashCommand } from "../../src/component/prompt/slash-command"

describe("prompt slash command parsing", () => {
const commands = [{ name: "review" }, { name: "skills" }]

test("ignores a bare slash", () => {
expect(resolvePromptSlashCommand("/", commands)).toBeUndefined()
})

test("ignores empty command names even if a command snapshot is malformed", () => {
expect(resolvePromptSlashCommand("/", [{ name: "" }])).toBeUndefined()
})

test("parses known slash command arguments", () => {
expect(resolvePromptSlashCommand("/review branch", commands)).toEqual({
name: "review",
arguments: "branch",
})
})

test("preserves multiline command arguments", () => {
expect(resolvePromptSlashCommand("/review branch\ninclude logs", commands)).toEqual({
name: "review",
arguments: "branch\ninclude logs",
})
})

test("ignores unknown slash commands", () => {
expect(resolvePromptSlashCommand("/missing", commands)).toBeUndefined()
})
})
Loading