Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)
Python
update to extract history tree with parent indices
Abi Rajaa559cd3120ba
diff --git a/frontend/src/components/history/utils.test.ts b/frontend/src/components/history/utils.test.tsindex a0dfd878..0dc26bd9 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,63 @@ const data: History = [},];+const basicBranchingHistory: History = [+ ...basicLinearHistory,+ {+ type: "ai_edit",+ parentIndex: 1,+ code: "<html>4. edit with better icons and green text</html>",+ inputs: {+ prompt: "make text green",+ },+ },+];++const longerBranchingHistory: History = [+ ...basicBranchingHistory,+ {+ type: "ai_edit",+ parentIndex: 3,+ code: "<html>5. edit with better icons and green, bold text</html>",+ inputs: {+ prompt: "make text bold",+ },+ },+];+test("should only include history from this point onward", () => {- expect(extractHistoryTree(data, 2)).toEqual([+ expect(extractHistoryTree(basicLinearHistory, 2)).toEqual([+ "<html>1. create</html>",+ "use better icons",+ "<html>2. edit with better icons</html>",+ "make text red",+ "<html>3. edit with better icons and red text</html>",+ ]);++ expect(extractHistoryTree(basicLinearHistory, 0)).toEqual([+ "<html>1. create</html>",+ ]);++ // Test branching+ expect(extractHistoryTree(basicBranchingHistory, 3)).toEqual([+ "<html>1. create</html>",+ "use better icons",+ "<html>2. edit with better icons</html>",+ "make text green",+ "<html>4. edit with better icons and green text</html>",+ ]);++ expect(extractHistoryTree(longerBranchingHistory, 4)).toEqual([+ "<html>1. create</html>",+ "use better icons",+ "<html>2. edit with better icons</html>",+ "make text green",+ "<html>4. edit with better icons and green text</html>",+ "make text bold",+ "<html>5. edit with better icons and green, bold text</html>",+ ]);++ expect(extractHistoryTree(longerBranchingHistory, 2)).toEqual(["<html>1. create</html>","use better icons","<html>2. edit with better icons</html>",@@ -38,5 +93,11 @@ test("should only include history from this point onward", () => {"<html>3. edit with better icons and red text</html>",]);- expect(extractHistoryTree(data, 0)).toEqual(["<html>1. create</html>"]);+ // 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.tsindex 7356ee10..cfa61712 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 expectsconst 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;}