update to extract history tree with parent indices

This commit is contained in:
Abi Raja 2023-12-07 10:09:35 -05:00
parent 52adb4602b
commit a559cd3120
2 changed files with 86 additions and 16 deletions

View File

@ -2,7 +2,7 @@ import { expect, test } from "vitest";
import { extractHistoryTree } from "./utils"; import { extractHistoryTree } from "./utils";
import type { History } from "./history_types"; import type { History } from "./history_types";
const data: History = [ const basicLinearHistory: History = [
{ {
type: "ai_create", type: "ai_create",
parentIndex: null, parentIndex: null,
@ -29,8 +29,32 @@ 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", () => { test("should only include history from this point onward", () => {
expect(extractHistoryTree(data, 2)).toEqual([ expect(extractHistoryTree(basicLinearHistory, 2)).toEqual([
"<html>1. create</html>", "<html>1. create</html>",
"use better icons", "use better icons",
"<html>2. edit with better icons</html>", "<html>2. edit with better icons</html>",
@ -38,5 +62,42 @@ test("should only include history from this point onward", () => {
"<html>3. edit with better icons and red text</html>", "<html>3. edit with better icons and red text</html>",
]); ]);
expect(extractHistoryTree(data, 0)).toEqual(["<html>1. create</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>",
"make text red",
"<html>3. edit with better icons and red text</html>",
]);
// Errors - TODO: Handle these
// Bad index
// TODO: Throw an exception instead?
expect(extractHistoryTree(basicLinearHistory, 100)).toEqual([]);
expect(extractHistoryTree(basicLinearHistory, -2)).toEqual([]);
// Bad tree
}); });

View File

@ -1,24 +1,33 @@
import { History } from "./history_types"; import { History, HistoryItem } from "./history_types";
export function extractHistoryTree( export function extractHistoryTree(
history: History, history: History,
version: number version: number
): string[] { ): 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[] = []; const flatHistory: string[] = [];
extractedHistory.forEach((item) => {
if (item.type === "ai_create") { let currentIndex: number | null = version;
// Don't include the image for ai_create since the server while (currentIndex !== null) {
// gets it passed and will include it directly // TODO: Handle currentIndex being out of bounds
flatHistory.push(item.code); 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 { } else {
flatHistory.push(item.inputs.prompt); // Break the loop if the item is not found (should not happen in a well-formed history)
flatHistory.push(item.code); break;
} }
}); }
return flatHistory; return flatHistory;
} }