Fix bugs when using stop button

This commit is contained in:
Brian Hackett 2025-03-18 20:40:39 -07:00
parent ede4993ab5
commit 3271171646
2 changed files with 19 additions and 8 deletions

View File

@ -23,6 +23,7 @@ import {
simulationRepositoryUpdated, simulationRepositoryUpdated,
sendChatMessage, sendChatMessage,
type ChatReference, type ChatReference,
simulationReset,
} from '~/lib/replay/SimulationPrompt'; } from '~/lib/replay/SimulationPrompt';
import { getIFrameSimulationData } from '~/lib/replay/Recording'; import { getIFrameSimulationData } from '~/lib/replay/Recording';
import { getCurrentIFrame } from '~/components/workbench/Preview'; import { getCurrentIFrame } from '~/components/workbench/Preview';
@ -216,6 +217,7 @@ export const ChatImpl = memo(
if (gActiveChatMessageTelemetry) { if (gActiveChatMessageTelemetry) {
gActiveChatMessageTelemetry.abort('StopButtonClicked'); gActiveChatMessageTelemetry.abort('StopButtonClicked');
clearActiveChat(); clearActiveChat();
simulationReset();
} }
}; };
@ -381,6 +383,8 @@ export const ChatImpl = memo(
const lastMessage = newMessages[newMessages.length - 1]; const lastMessage = newMessages[newMessages.length - 1];
setApproveChangesMessageId(lastMessage.id); setApproveChangesMessageId(lastMessage.id);
} else {
simulationReset();
} }
}; };

View File

@ -110,15 +110,11 @@ class ChatManager {
}); });
} }
finishSimulationData(): SimulationData { finishSimulationData() {
assert(this.client, 'Chat has been destroyed'); assert(this.client, 'Chat has been destroyed');
assert(!this.simulationFinished, 'Simulation has been finished'); assert(!this.simulationFinished, 'Simulation has been finished');
assert(this.repositoryId, 'Expected repository ID');
const allData = [createRepositoryIdPacket(this.repositoryId), ...this.pageData];
this.simulationFinished = true; this.simulationFinished = true;
return allData;
} }
async sendChatMessage(messages: Message[], references: ChatReference[], onResponsePart: ChatResponsePartCallback) { async sendChatMessage(messages: Message[], references: ChatReference[], onResponsePart: ChatResponsePartCallback) {
@ -152,14 +148,16 @@ class ChatManager {
// There is only one chat active at a time. // There is only one chat active at a time.
let gChatManager: ChatManager | undefined; let gChatManager: ChatManager | undefined;
function startChat(repositoryId: string, pageData: SimulationData) { function startChat(repositoryId: string | undefined, pageData: SimulationData) {
if (gChatManager) { if (gChatManager) {
gChatManager.destroy(); gChatManager.destroy();
} }
gChatManager = new ChatManager(); gChatManager = new ChatManager();
gChatManager.setRepositoryId(repositoryId); if (repositoryId) {
gChatManager.setRepositoryId(repositoryId);
}
if (pageData.length) { if (pageData.length) {
gChatManager.addPageData(pageData); gChatManager.addPageData(pageData);
@ -179,7 +177,7 @@ export function simulationRepositoryUpdated(repositoryId: string) {
* Called when the page gathering interaction data has been reloaded. We'll * 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. * 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'); assert(gChatManager, 'Expected to have an active chat');
const repositoryId = gChatManager.repositoryId; const repositoryId = gChatManager.repositoryId;
@ -188,6 +186,15 @@ export async function simulationReloaded() {
startChat(repositoryId, []); 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) { export function simulationAddData(data: SimulationData) {
assert(gChatManager, 'Expected to have an active chat'); assert(gChatManager, 'Expected to have an active chat');
gChatManager.addPageData(data); gChatManager.addPageData(data);