diff --git a/app/src/layout/dock/AgentChat.ts b/app/src/layout/dock/AgentChat.ts index b0b93b234..2e3f9f643 100644 --- a/app/src/layout/dock/AgentChat.ts +++ b/app/src/layout/dock/AgentChat.ts @@ -188,6 +188,7 @@ export class AgentChat extends Model { this.sessionTotalDuration = session.totalDuration || 0; if (this.composer) { this.composer.restoreHistory(session.messageHistory || []); + console.log("[AgentChat] loaded history:", (session.messageHistory || []).length, "items, composer has:", this.composer.getHistory().length); } this.titleElement.textContent = session.title; this.updateTokenDisplay(); @@ -504,6 +505,7 @@ export class AgentChat extends Model { this.messages.push({role: "user", content: text}); this.appendUserMessage(text); if (this.composer) { this.composer.pushHistory(text); } + this.saveSession().catch(function (e) { console.error(e); }); this.requestStartTime = Date.now(); diff --git a/app/src/layout/dock/AgentComposer.ts b/app/src/layout/dock/AgentComposer.ts index db1542709..4563d7690 100644 --- a/app/src/layout/dock/AgentComposer.ts +++ b/app/src/layout/dock/AgentComposer.ts @@ -52,6 +52,7 @@ export function mountComposer(host: HTMLElement, onSend: () => void): ComposerHa const history: string[] = []; let historyIdx = -1; + let savedDraft = ""; const updateHighlight = () => { if (!suggestionMenu) { return; } @@ -212,12 +213,15 @@ export function mountComposer(host: HTMLElement, onSend: () => void): ComposerHa return true; } if (event.key === "ArrowUp" && !suggestionMenu) { + const navigating = historyIdx >= 0; const isEmpty = _view.state.doc.childCount === 1 && _view.state.doc.firstChild?.childCount === 0; - if (isEmpty && history.length > 0) { + if ((navigating || isEmpty) && history.length > 0) { event.preventDefault(); - if (historyIdx === -1) { historyIdx = history.length - 1; } - else if (historyIdx > 0) { historyIdx--; } + if (historyIdx === -1) { + savedDraft = editor.state.doc.textContent; + historyIdx = history.length - 1; + } else if (historyIdx > 0) { historyIdx--; } if (historyIdx >= 0) { editor.commands.setContent(history[historyIdx]); } @@ -229,12 +233,17 @@ export function mountComposer(host: HTMLElement, onSend: () => void): ComposerHa historyIdx++; if (historyIdx >= history.length) { historyIdx = -1; - editor.commands.clearContent(); + editor.commands.setContent(savedDraft || ""); + savedDraft = ""; } else { editor.commands.setContent(history[historyIdx]); } return true; } + if (historyIdx >= 0 && event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) { + historyIdx = -1; + savedDraft = ""; + } return false; }, },