fix(message-input): persist draft across restarts by keying on conversation id

This commit is contained in:
xintaofei
2026-04-24 12:32:35 +08:00
parent f8d7c8966a
commit e9c3076197
3 changed files with 9 additions and 31 deletions

View File

@@ -407,7 +407,7 @@ export function MessageInput({
[tExperts]
)
const { shortcuts } = useShortcutSettings()
const effectiveDraftStorageKey = draftStorageKey ?? attachmentTabId ?? null
const effectiveDraftStorageKey = draftStorageKey ?? null
const resolvedPlaceholder = placeholder ?? t("askAnything")
const [text, setText] = useState(() => {
if (!effectiveDraftStorageKey) return ""

View File

@@ -66,7 +66,7 @@ import {
import {
buildConversationDraftStorageKey,
buildNewConversationDraftStorageKey,
moveMessageInputDraft,
clearMessageInputDraft,
} from "@/lib/message-input-draft"
import {
ContextMenu,
@@ -268,10 +268,10 @@ const ConversationTabView = memo(function ConversationTabView({
const externalId = detail?.summary.external_id ?? undefined
const draftStorageKey = useMemo(() => {
if (dbConversationId != null) {
return buildConversationDraftStorageKey(selectedAgent, dbConversationId)
return buildConversationDraftStorageKey(dbConversationId)
}
return buildNewConversationDraftStorageKey({ tabId })
}, [dbConversationId, tabId, selectedAgent])
return buildNewConversationDraftStorageKey()
}, [dbConversationId])
// Use the per-tab workingDir (derived from the tab's own folderId by the
// parent) rather than the active folder's path — otherwise switching tabs
// briefly exposes the previous folder's path to the ACP auto-connect
@@ -625,10 +625,7 @@ const ConversationTabView = memo(function ConversationTabView({
title,
effectiveConversationId
)
moveMessageInputDraft(
buildNewConversationDraftStorageKey({ tabId }),
buildConversationDraftStorageKey(selectedAgent, newConversationId)
)
clearMessageInputDraft(buildNewConversationDraftStorageKey())
statusUpdatedRef.current = false
// If the turn already finished while we were creating the
// conversation, apply the deferred status directly instead

View File

@@ -1,7 +1,5 @@
"use client"
import type { AgentType } from "@/lib/types"
interface PersistedDraftState {
text: string
}
@@ -83,16 +81,13 @@ function scheduleDraftPersistence(): void {
}
export function buildConversationDraftStorageKey(
agentType: AgentType,
conversationId: number
): string {
return `conv:${agentType}:${conversationId}`
return `conv:${conversationId}`
}
export function buildNewConversationDraftStorageKey(params: {
tabId: string
}): string {
return `new:${params.tabId}`
export function buildNewConversationDraftStorageKey(): string {
return "new"
}
export function loadMessageInputDraft(draftKey: string): string | null {
@@ -137,17 +132,3 @@ export function clearMessageInputDraft(draftKey: string): void {
/* ignore */
}
}
export function moveMessageInputDraft(
fromDraftKey: string,
toDraftKey: string
): void {
if (fromDraftKey === toDraftKey) return
const sourceText = loadMessageInputDraft(fromDraftKey)
const targetText = loadMessageInputDraft(toDraftKey)
if (sourceText && !targetText) {
saveMessageInputDraft(toDraftKey, sourceText)
}
clearMessageInputDraft(fromDraftKey)
}