From a559cd3120ba14c17e9f647ff9a410b2ef5679e2 Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Thu, 7 Dec 2023 10:09:35 -0500 Subject: [PATCH] update to extract history tree with parent indices --- frontend/src/components/history/utils.test.ts | 67 ++++++++++++++++++- frontend/src/components/history/utils.ts | 35 ++++++---- 2 files changed, 86 insertions(+), 16 deletions(-) diff --git a/frontend/src/components/history/utils.test.ts b/frontend/src/components/history/utils.test.ts index a0dfd87..0dc26bd 100644 --- a/frontend/src/components/history/utils.test.ts +++ b/frontend/src/components/history/utils.test.ts @@ -2,7 +2,7 @@ import { expect, test } from "vitest"; import { extractHistoryTree } from "./utils"; import type { History } from "./history_types"; -const data: History = [ +const basicLinearHistory: History = [ { type: "ai_create", parentIndex: null, @@ -29,8 +29,32 @@ const data: History = [ }, ]; +const basicBranchingHistory: History = [ + ...basicLinearHistory, + { + type: "ai_edit", + parentIndex: 1, + code: "4. edit with better icons and green text", + inputs: { + prompt: "make text green", + }, + }, +]; + +const longerBranchingHistory: History = [ + ...basicBranchingHistory, + { + type: "ai_edit", + parentIndex: 3, + code: "5. edit with better icons and green, bold text", + inputs: { + prompt: "make text bold", + }, + }, +]; + test("should only include history from this point onward", () => { - expect(extractHistoryTree(data, 2)).toEqual([ + expect(extractHistoryTree(basicLinearHistory, 2)).toEqual([ "1. create", "use better icons", "2. edit with better icons", @@ -38,5 +62,42 @@ test("should only include history from this point onward", () => { "3. edit with better icons and red text", ]); - expect(extractHistoryTree(data, 0)).toEqual(["1. create"]); + expect(extractHistoryTree(basicLinearHistory, 0)).toEqual([ + "1. create", + ]); + + // Test branching + expect(extractHistoryTree(basicBranchingHistory, 3)).toEqual([ + "1. create", + "use better icons", + "2. edit with better icons", + "make text green", + "4. edit with better icons and green text", + ]); + + expect(extractHistoryTree(longerBranchingHistory, 4)).toEqual([ + "1. create", + "use better icons", + "2. edit with better icons", + "make text green", + "4. edit with better icons and green text", + "make text bold", + "5. edit with better icons and green, bold text", + ]); + + expect(extractHistoryTree(longerBranchingHistory, 2)).toEqual([ + "1. create", + "use better icons", + "2. edit with better icons", + "make text red", + "3. edit with better icons and red text", + ]); + + // Errors - TODO: Handle these + // Bad index + // TODO: Throw an exception instead? + expect(extractHistoryTree(basicLinearHistory, 100)).toEqual([]); + expect(extractHistoryTree(basicLinearHistory, -2)).toEqual([]); + + // Bad tree }); diff --git a/frontend/src/components/history/utils.ts b/frontend/src/components/history/utils.ts index 7356ee1..cfa6171 100644 --- a/frontend/src/components/history/utils.ts +++ b/frontend/src/components/history/utils.ts @@ -1,24 +1,33 @@ -import { History } from "./history_types"; +import { History, HistoryItem } from "./history_types"; export function extractHistoryTree( history: History, version: number ): string[] { - // Get all history items up to the current version - const extractedHistory = history.slice(0, version + 1); - - // Convert the history into a flat array of strings that the backend expects const flatHistory: string[] = []; - extractedHistory.forEach((item) => { - if (item.type === "ai_create") { - // Don't include the image for ai_create since the server - // gets it passed and will include it directly - flatHistory.push(item.code); + + let currentIndex: number | null = version; + while (currentIndex !== null) { + // TODO: Handle currentIndex being out of bounds + const item: HistoryItem = history[currentIndex]; + console.log(item); + + if (item) { + if (item.type === "ai_create") { + // Don't include the image for ai_create + flatHistory.unshift(item.code); + } else { + flatHistory.unshift(item.code); + flatHistory.unshift(item.inputs.prompt); + } + + // Move to the parent of the current item + currentIndex = item.parentIndex; } else { - flatHistory.push(item.inputs.prompt); - flatHistory.push(item.code); + // Break the loop if the item is not found (should not happen in a well-formed history) + break; } - }); + } return flatHistory; }