The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.
TypeScript agentai-chatai-studioall-in-onecoding-agentdesktop-appfluximage-generationlinuxllmlocal-ailocal-llmopen-sourceprivacyself-hostedstable-diffusionuncensoreduncensored-aivideo-generationwindows
feat(chat): the model's answer can be edited in place (D#81)
purple42e28a708f76
diff --git a/src/components/chat/MessageBubble.tsx b/src/components/chat/MessageBubble.tsxindex f69ef93a..f31e4099 100644--- a/src/components/chat/MessageBubble.tsx+++ b/src/components/chat/MessageBubble.tsx@@ -50,6 +50,7 @@ function MessageBubbleImpl({ message, onRegenerate, onEdit, pendingApprovalId, o// agent" banner instead of leaving the user staring at a JSON dump.const activeConversationId = useChatStore((s) => s.activeConversationId)const deleteMessage = useChatStore((s) => s.deleteMessage)+ const updateMessageContent = useChatStore((s) => s.updateMessageContent)const isAgentActive = useAgentModeStore((s) =>activeConversationId ? s.agentModeActive[activeConversationId] ?? false : false)@@ -117,6 +118,15 @@ function MessageBubbleImpl({ message, onRegenerate, onEdit, pendingApprovalId, o[thoughtOnly, isAgentActive, chatToolsEnabled, message.thinking],)+ // A turn whose visible body comes from per-iteration answer blocks has no+ // single editable text, so the pencil hides there instead of editing a+ // field nobody sees. Also used below to pick the content fallback.+ const hasRealAnswerBlocks = useMemo(() => [...cleanBlocks.values()].some(Boolean), [cleanBlocks])+ // No editing while the turn still streams: the flush would overwrite the+ // edit a frame later. Done is marked by usage arriving with the done chunk.+ const canEditAssistant = !isUser && !hasRealAnswerBlocks+ && !!(message.content || '').trim() && (!isLast || !!message.usage)+useEffect(() => {if (isEditing && editRef.current) {editRef.current.focus()@@ -132,14 +142,23 @@ function MessageBubbleImpl({ message, onRegenerate, onEdit, pendingApprovalId, o}const startEdit = () => {- // Edit the short slash command, not its expanded instruction.- setEditContent(message.displayContent || message.content)+ // User: edit the short slash command, not its expanded instruction.+ // Assistant: edit what the model actually said.+ setEditContent(isUser ? (message.displayContent || message.content) : message.content)setIsEditing(true)}const confirmEdit = () => {- if (editContent.trim() && editContent !== message.content && onEdit) {- onEdit(message.id, editContent.trim())+ const next = editContent.trim()+ if (next && next !== message.content) {+ if (isUser) {+ // Editing your own message re-asks the question (resend path).+ onEdit?.(message.id, next)+ } else if (activeConversationId) {+ // Editing the model's answer rewrites history in place (D#81):+ // no resend, and every later turn reads the edited text as context.+ updateMessageContent(activeConversationId, message.id, next)+ }}setIsEditing(false)}@@ -308,7 +327,7 @@ function MessageBubbleImpl({ message, onRegenerate, onEdit, pendingApprovalId, o: 'px-1 py-0.5')}>- {isUser && isEditing ? (+ {isEditing ? (<div className="space-y-1"><textarearef={editRef}@@ -342,8 +361,7 @@ function MessageBubbleImpl({ message, onRegenerate, onEdit, pendingApprovalId, o// Judge "has a real answer block" on the STRIPPED text: a block// holding nothing but a LOOP_DONE line must not suppress the// fallback, or the turn renders blank.- const hasAnswerBlock = [...cleanBlocks.values()].some(Boolean)- if (hasAnswerBlock) return null+ if (hasRealAnswerBlocks) return nullreturn (<div className="text-[0.78rem] leading-relaxed"><MarkdownRenderer content={cleanContent} />@@ -410,6 +428,9 @@ function MessageBubbleImpl({ message, onRegenerate, onEdit, pendingApprovalId, o{isUser && onEdit && (<button onClick={startEdit} className="p-1 rounded-md text-gray-400 dark:text-gray-500 hover:text-gray-700 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-white/10 transition-colors" aria-label="Edit message" title="Edit"><Pencil size={12} /></button>)}+ {canEditAssistant && (+ <button onClick={startEdit} className="p-1 rounded-md text-gray-400 dark:text-gray-500 hover:text-gray-700 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-white/10 transition-colors" aria-label="Edit response" title="Edit"><Pencil size={12} /></button>+ )}{!isUser && onRegenerate && (<button onClick={() => onRegenerate(message.id)} className="p-1 rounded-md text-gray-400 dark:text-gray-500 hover:text-gray-700 dark:hover:text-gray-200 hover:bg-gray-100 dark:hover:bg-white/10 transition-colors" aria-label="Regenerate response" title="Regenerate"><RefreshCw size={12} /></button>)}diff --git a/src/components/chat/__tests__/MessageBubble-edit-assistant.test.ts b/src/components/chat/__tests__/MessageBubble-edit-assistant.test.tsnew file mode 100644index 00000000..3bf79f09--- /dev/null+++ b/src/components/chat/__tests__/MessageBubble-edit-assistant.test.ts@@ -0,0 +1,64 @@+/**+ * D#81 (TheRealNovelist): the model's answer can be edited in place.+ * The store half is a real behaviour test; the component half is a source+ * guard in the house pattern (no render harness in this repo).+ */+import { readFileSync } from 'node:fs'+import { join } from 'node:path'+import { describe, it, expect, beforeEach } from 'vitest'+import { useChatStore } from '../../../stores/chatStore'+import type { Message } from '../../../types/chat'++function msg(overrides: Partial<Message> = {}): Message {+ return {+ id: `msg-${Math.random().toString(36).slice(2)}`,+ role: 'assistant',+ content: 'the original answer',+ timestamp: Date.now(),+ ...overrides,+ }+}++beforeEach(() => {+ useChatStore.setState({ conversations: [], activeConversationId: null })+})++describe('assistant edit, store half', () => {+ it('rewrites the answer in place and later context reads the edit', () => {+ const s = useChatStore.getState()+ const convId = s.createConversation('gemma4:12b', '', 'lu')+ const m = msg()+ s.addMessage(convId, m)+ s.updateMessageContent(convId, m.id, 'the corrected answer')+ const stored = useChatStore.getState().conversations[0].messages.find((x) => x.id === m.id)+ expect(stored?.content).toBe('the corrected answer')+ // No resend semantics: nothing after the message was touched.+ expect(useChatStore.getState().conversations[0].messages).toHaveLength(1)+ })+})++describe('assistant edit, component wiring (source guards)', () => {+ const src = readFileSync(join(__dirname, '../MessageBubble.tsx'), 'utf8')++ it('offers a pencil on assistant messages', () => {+ expect(src).toContain('aria-label="Edit response"')+ })++ it('assistant saves rewrite the store, user saves keep the resend path', () => {+ expect(src).toContain('updateMessageContent(activeConversationId, message.id, next)')+ expect(src).toContain('onEdit?.(message.id, next)')+ })++ it('hides the pencil where there is no single editable text', () => {+ // Block-built turns render per-iteration text, and a still-streaming+ // turn would overwrite the edit a frame later.+ expect(src).toContain('!hasRealAnswerBlocks')+ expect(src).toMatch(/canEditAssistant = !isUser && !hasRealAnswerBlocks/)+ expect(src).toContain('(!isLast || !!message.usage)')+ })++ it('the edit textarea opens for both roles, not only the user', () => {+ expect(src).toContain('{isEditing ? (')+ expect(src).not.toContain('{isUser && isEditing ? (')+ })+})