fix(ai-panels): clear restored history transcript on New chat (fixes #195) (#209)

* fix(ai-panels): clear restored history transcript on New chat (fixes #195)

* ci: retrigger checks after prosemirror teardown flake

---------

Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
Co-authored-by: merrick-2002 <merrick@mainfunc.ai>
This commit is contained in:
ANIRUDDHA ADAK
2026-09-06 11:13:43 +08:00
committed by GitHub
co-authored by Aniruddha Adak merrick-2002
parent a5b5903939
commit d9769d1c12
3 changed files with 125 additions and 6 deletions
+4 -1
View File
@@ -874,6 +874,9 @@ export function AiPanel({
loopRef.current?.reset()
setBusy(false)
setChat([])
// Restored history is painted above the live turn: without this the
// previous conversation survives "New chat" on screen (#195).
setHistoricChat([])
sentAttachmentsRef.current = []
inputRef.current?.focus()
}
@@ -1033,7 +1036,7 @@ export function AiPanel({
{t('aiPanelTitle')}
</span>
<div className="ai-panel-header-actions">
{chat.length > 0 && (
{(chat.length > 0 || historicChat.length > 0) && (
<button
className="ai-header-btn"
onClick={newChat}
@@ -0,0 +1,117 @@
// "New chat" must clear the restored history transcript, not just the live
// turn: previously the previous conversation stayed painted on screen with
// no way to dismiss it (#195).
import { beforeAll, describe, expect, it, vi } from 'vitest'
import { act, createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { Editor } from '@tiptap/core'
import { editorExtensions } from '../src/renderer/editor/extensions'
import { AiPanel } from '../src/renderer/ai/AiPanel'
import { AI_PROVIDERS, type AiSettings } from '../src/shared/ipc'
const settings: AiSettings = {
provider: 'anthropic',
providers: Object.fromEntries(
AI_PROVIDERS.map((p) => [p.id, { apiKey: '', model: p.defaultModel }]),
) as AiSettings['providers'],
}
function createEditor(): Editor {
return new Editor({
element: document.createElement('div'),
extensions: editorExtensions,
content: {
type: 'doc',
content: [
{
type: 'docParagraph',
attrs: { docxIndex: 0 },
content: [{ type: 'text', text: 'EVs market research' }],
},
],
},
})
}
function mount(element: React.ReactElement): {
container: HTMLElement
cleanup: () => void
} {
const container = document.createElement('div')
document.body.appendChild(container)
const root: Root = createRoot(container)
act(() => root.render(element))
return {
container,
cleanup: () => {
act(() => root.unmount())
container.remove()
},
}
}
function panelProps(editor: Editor) {
// No onCollapse: the New chat button is then the only .ai-header-btn,
// which keeps the selector independent of the active i18n locale.
return {
editor,
blocks: [],
settings,
open: true,
}
}
const HISTORY = [{ role: 'user', text: 'earlier question' }]
function mockProjectApi() {
const win = window as unknown as { projectApi?: unknown }
const previous = win.projectApi
win.projectApi = {
resolveChat: vi.fn(async () => ({ projectId: 'p', chatId: 'c' })),
loadChat: vi.fn(async () => HISTORY),
}
return () => {
win.projectApi = previous
}
}
async function flush(): Promise<void> {
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 0))
})
}
beforeAll(() => {
// jsdom has no scrollTo; the panel auto-scrolls its chat log
Element.prototype.scrollTo ??= () => {}
})
describe('AiPanel new chat', () => {
it('clears the restored history transcript along with the live turn', async () => {
const restoreApi = mockProjectApi()
try {
const editor = createEditor()
const { container, cleanup } = mount(createElement(AiPanel, panelProps(editor)))
try {
await flush()
await flush()
// the previous conversation is painted above the live turn…
expect(container.querySelectorAll('.ai-msg-historic').length).toBe(1)
// …and New chat is offered even though the live turn is empty
const button = container.querySelector<HTMLButtonElement>('.ai-header-btn')
expect(button).not.toBeNull()
act(() => button!.click())
// both the transcript and the button are gone
expect(container.querySelectorAll('.ai-msg-historic').length).toBe(0)
expect(container.querySelector('.ai-header-btn')).toBeNull()
} finally {
cleanup()
editor.destroy()
}
} finally {
restoreApi()
}
})
})
+4 -5
View File
@@ -1864,10 +1864,9 @@ export function AiPanel({
loopRef.current?.reset()
setBusy(false)
setChat([])
// Answered clarifications are transcript too: they render under their
// original message index, so stale entries would re-attach to unrelated
// new messages after an index collision.
setClarifyAnswers([])
// Same as docs (#195): the restored transcript is painted above the live
// turn, so it must clear too — otherwise the old conversation survives.
setHistoricChat([])
sentAttachmentsRef.current = []
readAttachmentPathsRef.current.clear()
inputRef.current?.focus()
@@ -2005,7 +2004,7 @@ export function AiPanel({
{t('aiPanelTitle')}
</span>
<div className="ai-panel-header-actions">
{chat.length > 0 && (
{(chat.length > 0 || historicChat.length > 0) && (
<button
className="ai-header-btn"
onClick={newChat}