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,
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();
}
};

View File

@ -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);