diff --git a/app/components/chat/Chat.client.tsx b/app/components/chat/Chat.client.tsx index 20adba4c..c88964a2 100644 --- a/app/components/chat/Chat.client.tsx +++ b/app/components/chat/Chat.client.tsx @@ -23,6 +23,7 @@ import { simulationRepositoryUpdated, sendChatMessage, type ChatReference, + simulationReset, } from '~/lib/replay/SimulationPrompt'; import { getIFrameSimulationData } from '~/lib/replay/Recording'; import { getCurrentIFrame } from '~/components/workbench/Preview'; @@ -216,6 +217,7 @@ export const ChatImpl = memo( if (gActiveChatMessageTelemetry) { gActiveChatMessageTelemetry.abort('StopButtonClicked'); clearActiveChat(); + simulationReset(); } }; @@ -381,6 +383,8 @@ export const ChatImpl = memo( const lastMessage = newMessages[newMessages.length - 1]; setApproveChangesMessageId(lastMessage.id); + } else { + simulationReset(); } }; diff --git a/app/lib/replay/SimulationPrompt.ts b/app/lib/replay/SimulationPrompt.ts index cfa06c08..3bf109d8 100644 --- a/app/lib/replay/SimulationPrompt.ts +++ b/app/lib/replay/SimulationPrompt.ts @@ -110,15 +110,11 @@ class ChatManager { }); } - finishSimulationData(): SimulationData { + finishSimulationData() { assert(this.client, 'Chat has been destroyed'); assert(!this.simulationFinished, 'Simulation has been finished'); - assert(this.repositoryId, 'Expected repository ID'); - const allData = [createRepositoryIdPacket(this.repositoryId), ...this.pageData]; this.simulationFinished = true; - - return allData; } async sendChatMessage(messages: Message[], references: ChatReference[], onResponsePart: ChatResponsePartCallback) { @@ -152,14 +148,16 @@ class ChatManager { // There is only one chat active at a time. let gChatManager: ChatManager | undefined; -function startChat(repositoryId: string, pageData: SimulationData) { +function startChat(repositoryId: string | undefined, pageData: SimulationData) { if (gChatManager) { gChatManager.destroy(); } gChatManager = new ChatManager(); - gChatManager.setRepositoryId(repositoryId); + if (repositoryId) { + gChatManager.setRepositoryId(repositoryId); + } if (pageData.length) { gChatManager.addPageData(pageData); @@ -179,7 +177,7 @@ export function simulationRepositoryUpdated(repositoryId: string) { * Called when the page gathering interaction data has been reloaded. We'll * start a new chat with the same repository contents as any existing chat. */ -export async function simulationReloaded() { +export function simulationReloaded() { assert(gChatManager, 'Expected to have an active chat'); const repositoryId = gChatManager.repositoryId; @@ -188,6 +186,15 @@ export async function simulationReloaded() { startChat(repositoryId, []); } +/* + * Called when the current message has finished with no repository change. + * We'll start a new chat with the same simulation data as the previous chat. + */ +export function simulationReset() { + assert(gChatManager, 'Expected to have an active chat'); + startChat(gChatManager.repositoryId, gChatManager.pageData); +} + export function simulationAddData(data: SimulationData) { assert(gChatManager, 'Expected to have an active chat'); gChatManager.addPageData(data);