Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: normalize AI content before detecting edits

Richie McIlroybac8704ee746
diff --git a/apps/web/__tests__/unit/edit-ai-content.test.ts b/apps/web/__tests__/unit/edit-ai-content.test.ts
index 4e95b19cd..3de9414d0 100644
--- a/apps/web/__tests__/unit/edit-ai-content.test.ts
+++ b/apps/web/__tests__/unit/edit-ai-content.test.ts
@@ -72,6 +72,22 @@ beforeEach(() => {
});
describe("editing AI content", () => {
+ it("does not treat existing surrounding whitespace as a manual edit", async () => {
+ const original = {
+ summary: " Original ",
+ chapters: [{ title: " Intro ", start: 0 }],
+ };
+ metadata = { ...original, aiGenerationStatus: "COMPLETE" };
+ expect(
+ await editAiContent(videoId, { expected: original, value: original }),
+ ).toEqual({ success: true, data: original });
+ expect(mocks.write).not.toHaveBeenCalled();
+ await editAiContent(videoId, {
+ expected: original,
+ value: { ...original, summary: "Changed" },
+ });
+ expect(writeSql).not.toContain("chaptersManuallyEdited");
+ });
it("compares chapters independently of MySQL JSON key ordering", async () => {
metadata.chapters = [{ start: 0, title: "Intro" }];
expect(
diff --git a/apps/web/__tests__/unit/summary-editor.test.ts b/apps/web/__tests__/unit/summary-editor.test.ts
index c395f6e40..6adc1819c 100644
--- a/apps/web/__tests__/unit/summary-editor.test.ts
+++ b/apps/web/__tests__/unit/summary-editor.test.ts
@@ -92,6 +92,24 @@ const summary = () => {
};
describe("summary and chapter editor", () => {
+ it("opens whitespace-padded AI output without a dirty draft", async () => {
+ await render(
+ createElement(SummaryEditor, {
+ videoId,
+ initialContent: {
+ summary: " Original ",
+ chapters: [{ title: " Intro ", start: 0 }],
+ },
+ duration: 120,
+ onClose,
+ }),
+ );
+ expect(button("Save changes").disabled).toBe(true);
+ await change(summary(), " Original ");
+ expect(button("Save changes").disabled).toBe(true);
+ await change(summary(), "Changed");
+ expect(button("Save changes").disabled).toBe(false);
+ });
it("starts clean with MySQL chapter key ordering", async () => {
await render(
createElement(SummaryEditor, {
diff --git a/apps/web/actions/videos/edit-ai-content.ts b/apps/web/actions/videos/edit-ai-content.ts
index f7c1c32c7..712b3f4a0 100644
--- a/apps/web/actions/videos/edit-ai-content.ts
+++ b/apps/web/actions/videos/edit-ai-content.ts
@@ -13,6 +13,7 @@ import {
MAX_CHAPTER_TITLE_LENGTH,
MAX_CHAPTERS,
MAX_SUMMARY_LENGTH,
+ normalizeAiContent,
validateAiContent,
} from "@/lib/ai-content";
import { isAiGenerationEnabledForUser } from "@/lib/ai-generation-entitlement";
@@ -52,14 +53,14 @@ export async function editAiContent(
if (typeof videoId !== "string" || !videoId || !parsed.success) {
return { success: false, message: "Invalid summary or chapter data." };
}
- const { value, expected } = parsed.data;
- value.summary = value.summary.trim();
- value.chapters = value.chapters.map((chapter) => ({
- ...chapter,
- title: chapter.title.trim(),
- }));
- const summaryChanged = value.summary !== expected.summary;
- const chaptersChanged = !chaptersEqual(value.chapters, expected.chapters);
+ const { expected } = parsed.data;
+ const value = normalizeAiContent(parsed.data.value);
+ const normalizedExpected = normalizeAiContent(expected);
+ const summaryChanged = value.summary !== normalizedExpected.summary;
+ const chaptersChanged = !chaptersEqual(
+ value.chapters,
+ normalizedExpected.chapters,
+ );
try {
const result = await db().transaction(async (tx): Promise<EditResult> => {
diff --git a/apps/web/app/s/[videoId]/_components/tabs/SummaryEditor.tsx b/apps/web/app/s/[videoId]/_components/tabs/SummaryEditor.tsx
index c6fd7752d..c4c90de7a 100644
--- a/apps/web/app/s/[videoId]/_components/tabs/SummaryEditor.tsx
+++ b/apps/web/app/s/[videoId]/_components/tabs/SummaryEditor.tsx
@@ -14,6 +14,7 @@ import {
MAX_CHAPTER_TITLE_LENGTH,
MAX_CHAPTERS,
MAX_SUMMARY_LENGTH,
+ normalizeAiContent,
parseChapterTime,
validateAiContent,
} from "@/lib/ai-content";
@@ -36,6 +37,9 @@ export function SummaryEditor({
const queryClient = useQueryClient();
const id = useId();
const [expected] = useState(initialContent);
+ const [normalizedExpected] = useState(() =>
+ normalizeAiContent(initialContent),
+ );
const [summary, setSummary] = useState(initialContent.summary);
const nextId = useRef(initialContent.chapters.length);
const [chapters, setChapters] = useState<
@@ -60,8 +64,8 @@ export function SummaryEditor({
})),
};
const dirty =
- value.summary !== expected.summary ||
- !chaptersEqual(value.chapters, expected.chapters);
+ value.summary !== normalizedExpected.summary ||
+ !chaptersEqual(value.chapters, normalizedExpected.chapters);
const validationError = validateAiContent(value, duration);
useEffect(() => {
diff --git a/apps/web/lib/ai-content-metadata.ts b/apps/web/lib/ai-content-metadata.ts
index dcc63ff1a..78eb39eaf 100644
--- a/apps/web/lib/ai-content-metadata.ts
+++ b/apps/web/lib/ai-content-metadata.ts
@@ -8,6 +8,8 @@ export function setGeneratedAiContent(
) {
const path = `$.${field}`;
const editedPath = `$.${field}ManuallyEdited`;
+ // Empty values are deliberate removals. clearAiMetadata in edit-video.ts
+ // deletes the keys on media replacement, allowing regeneration despite old flags.
return sql`IF(
JSON_UNQUOTE(JSON_EXTRACT(${videos.metadata}, ${editedPath})) = 'true'
AND JSON_CONTAINS_PATH(${videos.metadata}, 'one', ${path}),
diff --git a/apps/web/lib/ai-content.ts b/apps/web/lib/ai-content.ts
index 08815f3c9..b58a2ddef 100644
--- a/apps/web/lib/ai-content.ts
+++ b/apps/web/lib/ai-content.ts
@@ -68,3 +68,13 @@ export function chaptersEqual(
)
);
}
+
+export function normalizeAiContent(content: AiContent): AiContent {
+ return {
+ summary: content.summary.trim(),
+ chapters: content.chapters.map((chapter) => ({
+ ...chapter,
+ title: chapter.title.trim(),
+ })),
+ };
+}