From bbd530c052c4c2c85b419382fc0120c44306878a Mon Sep 17 00:00:00 2001 From: Sylvie Crowe <107814465+oneirocosm@users.noreply.github.com> Date: Sun, 27 Oct 2024 11:49:34 -0700 Subject: [PATCH] AI Sliding Window (#1151) Only send the 30 most recent ai questions and responses to the model when making requests. This prevents the amount of data being sent from getting too big. --- frontend/app/view/waveai/waveai.tsx | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/frontend/app/view/waveai/waveai.tsx b/frontend/app/view/waveai/waveai.tsx index 7c100542d..bb4741795 100644 --- a/frontend/app/view/waveai/waveai.tsx +++ b/frontend/app/view/waveai/waveai.tsx @@ -24,6 +24,7 @@ interface ChatMessageType { } const outline = "2px solid var(--accent-color)"; +const slidingWindowSize = 30; interface ChatItemProps { chatItem: ChatMessageType; @@ -233,7 +234,7 @@ export class WaveAiModel implements ViewModel { return []; } const history: Array = JSON.parse(new TextDecoder().decode(data)); - return history; + return history.slice(Math.max(history.length - slidingWindowSize, 0)); } giveFocus(): boolean { @@ -294,19 +295,23 @@ export class WaveAiModel implements ViewModel { fullMsg += msg.text ?? ""; globalStore.set(this.updateLastMessageAtom, msg.text ?? "", true); if (this.cancel) { - if (fullMsg == "") { - globalStore.set(this.removeLastMessageAtom); - } break; } + } + if (fullMsg == "") { + // remove a message if empty + globalStore.set(this.removeLastMessageAtom); + // only save the author's prompt + await BlockService.SaveWaveAiData(blockId, [...history, newPrompt]); + } else { + const responsePrompt: OpenAIPromptMessageType = { + role: "assistant", + content: fullMsg, + }; + //mark message as complete globalStore.set(this.updateLastMessageAtom, "", false); - if (fullMsg != "") { - const responsePrompt: OpenAIPromptMessageType = { - role: "assistant", - content: fullMsg, - }; - await BlockService.SaveWaveAiData(blockId, [...history, newPrompt, responsePrompt]); - } + // save a complete message prompt and response + await BlockService.SaveWaveAiData(blockId, [...history, newPrompt, responsePrompt]); } } catch (error) { const updatedHist = [...history, newPrompt];