diff --git a/apps/docs/src/renderer/ai/AiPanel.tsx b/apps/docs/src/renderer/ai/AiPanel.tsx
index 72c794e..896df13 100644
--- a/apps/docs/src/renderer/ai/AiPanel.tsx
+++ b/apps/docs/src/renderer/ai/AiPanel.tsx
@@ -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')}
- {chat.length > 0 && (
+ {(chat.length > 0 || historicChat.length > 0) && (
[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 {
+ 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('.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()
+ }
+ })
+})
diff --git a/apps/slides/src/renderer/ai/AiPanel.tsx b/apps/slides/src/renderer/ai/AiPanel.tsx
index da380a7..3033a5f 100644
--- a/apps/slides/src/renderer/ai/AiPanel.tsx
+++ b/apps/slides/src/renderer/ai/AiPanel.tsx
@@ -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')}
- {chat.length > 0 && (
+ {(chat.length > 0 || historicChat.length > 0) && (