Skip to content

Commit 2070847

Browse files
committed
fix: preserve per-session input drafts when switching sessions
Store each session's draft input in a module-level Map so that switching between sessions restores the text the user was typing. Previously, switching sessions either lost the draft or carried it to the new session. Closes anomalyco#14263
1 parent 6b02165 commit 2070847

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎packages/opencode/src/cli/cmd/tui/app.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import { writeHeapSnapshot } from "v8"
4040
import { PromptRefProvider, usePromptRef } from "./context/prompt"
4141
import { TuiConfigProvider } from "./context/tui-config"
4242
import { TuiConfig } from "@/config/tui"
43+
import { drafts } from "./component/prompt"
4344

4445
async function getTerminalBackgroundColor(): Promise<"dark" | "light"> {
4546
// can't set raw mode if not a TTY
@@ -385,11 +386,12 @@ function App() {
385386
},
386387
onSelect: () => {
387388
const current = promptRef.current
388-
// Don't require focus - if there's any text, preserve it
389-
const currentPrompt = current?.current?.input ? current.current : undefined
389+
// Save draft for the current session instead of carrying it to home
390+
if (current?.current?.input && route.data.type === "session") {
391+
drafts.set(route.data.sessionID, { input: current.current.input, parts: [...current.current.parts] })
392+
}
390393
route.navigate({
391394
type: "home",
392-
initialPrompt: currentPrompt,
393395
})
394396
dialog.clear()
395397
},

‎packages/opencode/src/cli/cmd/tui/component/dialog-session-list.tsx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { DialogSessionRename } from "./dialog-session-rename"
1111
import { useKV } from "../context/kv"
1212
import { createDebouncedSignal } from "../util/signal"
1313
import { Spinner } from "./spinner"
14+
import { usePromptRef } from "../context/prompt"
15+
import { drafts } from "./prompt"
1416

1517
export function DialogSessionList() {
1618
const dialog = useDialog()
@@ -20,6 +22,7 @@ export function DialogSessionList() {
2022
const { theme } = useTheme()
2123
const sdk = useSDK()
2224
const kv = useKV()
25+
const promptRef = usePromptRef()
2326

2427
const [toDelete, setToDelete] = createSignal<string>()
2528
const [search, setSearch] = createDebouncedSignal("", 150)
@@ -74,6 +77,15 @@ export function DialogSessionList() {
7477
setToDelete(undefined)
7578
}}
7679
onSelect={(option) => {
80+
// Save current prompt as draft for the current session
81+
if (route.data.type === "session") {
82+
const current = promptRef.current
83+
if (current?.current?.input) {
84+
drafts.set(route.data.sessionID, { input: current.current.input, parts: [...current.current.parts] })
85+
} else {
86+
drafts.delete(route.data.sessionID)
87+
}
88+
}
7789
route.navigate({
7890
type: "session",
7991
sessionID: option.value,

‎packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export type PromptRef = {
5858
const PLACEHOLDERS = ["Fix a TODO in the codebase", "What is the tech stack of this project?", "Fix broken tests"]
5959
const SHELL_PLACEHOLDERS = ["ls -la", "git status", "pwd"]
6060

61+
// Per-session draft input storage
62+
export const drafts = new Map<string, PromptInfo>()
63+
6164
export function Prompt(props: PromptProps) {
6265
let input: TextareaRenderable
6366
let anchor: BoxRenderable
@@ -137,10 +140,33 @@ export function Prompt(props: PromptProps) {
137140
interrupt: 0,
138141
})
139142

143+
let previousSessionID: string | undefined = props.sessionID
140144
createEffect(
141145
on(
142146
() => props.sessionID,
143-
() => {
147+
(nextSessionID) => {
148+
// Save current draft for the previous session
149+
if (previousSessionID && store.prompt.input) {
150+
drafts.set(previousSessionID, { input: store.prompt.input, parts: [...store.prompt.parts] })
151+
} else if (previousSessionID) {
152+
drafts.delete(previousSessionID)
153+
}
154+
155+
// Restore draft for the new session or clear
156+
const draft = nextSessionID ? drafts.get(nextSessionID) : undefined
157+
if (draft) {
158+
input.setText(draft.input)
159+
setStore("prompt", { input: draft.input, parts: draft.parts })
160+
restoreExtmarksFromParts(draft.parts)
161+
input.gotoBufferEnd()
162+
} else {
163+
input.clear()
164+
input.extmarks.clear()
165+
setStore("prompt", { input: "", parts: [] })
166+
setStore("extmarkToPartIndex", new Map())
167+
}
168+
169+
previousSessionID = nextSessionID
144170
setStore("placeholder", Math.floor(Math.random() * PLACEHOLDERS.length))
145171
},
146172
{ defer: true },
@@ -645,6 +671,7 @@ export function Prompt(props: PromptProps) {
645671
parts: [],
646672
})
647673
setStore("extmarkToPartIndex", new Map())
674+
if (props.sessionID) drafts.delete(props.sessionID)
648675
props.onSubmit?.()
649676

650677
// temporary hack to make sure the message is sent

0 commit comments

Comments
 (0)