From b5c6763d115fceb880dbae17f11ba518f7db9894 Mon Sep 17 00:00:00 2001 From: Shoubhit Dash Date: Thu, 21 May 2026 16:33:59 +0530 Subject: [PATCH] refactor(tui): simplify compact slash handling --- .../cmd/tui/component/prompt/autocomplete.tsx | 24 ++++++++----------- .../cli/cmd/tui/component/prompt/index.tsx | 18 +++++--------- packages/opencode/src/session/compaction.ts | 4 ++-- 3 files changed, 18 insertions(+), 28 deletions(-) diff --git a/packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx b/packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx index 88d3e58938..fad7f84984 100644 --- a/packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx +++ b/packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx @@ -543,19 +543,21 @@ export function Autocomplete(props: { ), ) + function insertSlashCommand(name: string) { + const newText = `/${name} ` + const cursor = props.input().logicalCursor + props.input().deleteRange(0, 0, cursor.row, cursor.col) + props.input().insertText(newText) + props.input().cursorOffset = Bun.stringWidth(newText) + } + const commands = createMemo((): AutocompleteOption[] => { const results: AutocompleteOption[] = slashes().map((command) => { const name = command.display.trim().slice(1) if (name !== "compact") return command return { ...command, - onSelect: () => { - const newText = `/${name} ` - const cursor = props.input().logicalCursor - props.input().deleteRange(0, 0, cursor.row, cursor.col) - props.input().insertText(newText) - props.input().cursorOffset = Bun.stringWidth(newText) - }, + onSelect: () => insertSlashCommand(name), } }) @@ -565,13 +567,7 @@ export function Autocomplete(props: { results.push({ display: "/" + serverCommand.name + label, description: serverCommand.description, - onSelect: () => { - const newText = "/" + serverCommand.name + " " - const cursor = props.input().logicalCursor - props.input().deleteRange(0, 0, cursor.row, cursor.col) - props.input().insertText(newText) - props.input().cursorOffset = Bun.stringWidth(newText) - }, + onSelect: () => insertSlashCommand(serverCommand.name), }) } diff --git a/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx b/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx index b991c15db2..9764439ec0 100644 --- a/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx +++ b/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx @@ -1145,18 +1145,12 @@ export function Prompt(props: PromptProps) { setStore("mode", "normal") } else if (isCompactSlash(inputText)) { const instructions = slashArguments(inputText).trim() - const payload: Parameters[0] & { $body_instructions?: string } = instructions - ? { - sessionID, - modelID: selectedModel.modelID, - providerID: selectedModel.providerID, - $body_instructions: instructions, - } - : { - sessionID, - modelID: selectedModel.modelID, - providerID: selectedModel.providerID, - } + const payload: Parameters[0] & { $body_instructions?: string } = { + sessionID, + modelID: selectedModel.modelID, + providerID: selectedModel.providerID, + ...(instructions ? { $body_instructions: instructions } : {}), + } void sdk.client.session.summarize(payload) } else if ( inputText.startsWith("/") && diff --git a/packages/opencode/src/session/compaction.ts b/packages/opencode/src/session/compaction.ts index ae5382e7b9..b6028e8770 100644 --- a/packages/opencode/src/session/compaction.ts +++ b/packages/opencode/src/session/compaction.ts @@ -133,7 +133,7 @@ function buildPrompt(input: { previousSummary?: string; context: string[] }) { return [anchor, SUMMARY_TEMPLATE, ...input.context].join("\n\n") } -function withInstructions(prompt: string, instructions: string | undefined) { +function appendInstructions(prompt: string, instructions: string | undefined) { const trimmed = instructions?.trim() if (!trimmed) return prompt return [prompt, "Additional user instructions for this compaction:", trimmed].join("\n\n") @@ -407,7 +407,7 @@ export const layer = Layer.effect( { sessionID: input.sessionID }, { context: [], prompt: undefined }, ) - const nextPrompt = withInstructions( + const nextPrompt = appendInstructions( compacting.prompt ?? buildPrompt({ previousSummary, context: compacting.context }), compactionPart?.instructions, )