-
-
Notifications
You must be signed in to change notification settings - Fork 19
BL-16523 AI Image Editor: whole-book AI image editing + per-user OpenRouter key #8033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hatton
wants to merge
20
commits into
master
Choose a base branch
from
AddAIImages
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8c585c7
AI Image Editor: whole-book image enumeration + book-wide replacement
hatton 3dd5687
Document AiImageEditorApi and remove the vestigial C# frame-message path
hatton ddf7be0
Merge remote-tracking branch 'origin/master' into AddAIImages
hatton da81d80
AI Image Editor: per-user OpenRouter key storage + serve editor from …
hatton 3046752
go.sh --with: develop local library checkouts alongside Bloom
hatton 1407bb6
go/dev: stage jQuery global in dev; make --with iframe-app startup wa…
hatton 4be4735
AI Image Editor: document public RegisterWithApiHandler (preflight)
hatton 87f4b36
Merge origin/master into AddAIImages (D5)
hatton c22c67f
AI Image Editor: apply preflight review decisions (D2-D4, F2-F5)
hatton 118aa43
AI Image Editor: robustness, image-replacement matching, and dev-laun…
hatton 3682c87
Merge remote-tracking branch 'origin/master' into AddAIImages
hatton 87637f8
AI Image Editor: clean up orphaned ai-image files; localize menu item…
hatton 5b5c881
AI Image Editor: fix CORS preflight, document OAuth reflection, test …
hatton 0ad16c6
AI Image Editor: add host-side tests; extract testable helpers (BL-16…
hatton aa5fb65
AI Image Editor: restrict to editable image formats (BL-16523)
hatton 3b59570
Merge remote-tracking branch 'origin/master' into AddAIImages
hatton a96190e
AI Image Editor: address review feedback (BL-16523)
hatton 5e4e97b
AI Image Editor: remove the OpenRouter OAuth sign-in path (BL-16523)
hatton 162ad6d
Merge remote-tracking branch 'origin/master' into AddAIImages
hatton dbecb8f
Correct two AI Image Editor code comments per review (BL-16523)
hatton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/BloomBrowserUI/bookEdit/toolbox/canvas/aiEditorSlotMatching.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| import { matchReplacementsToElements } from "./aiEditorSlotMatching"; | ||
|
|
||
| // Unit tests for the current-page slot matcher used by the AI editor's commit (see | ||
| // aiEditorSlotMatching.ts and canvasControlRegistry.ts editWithAi). The tricky case is a page | ||
| // with several image slots that share a filename: distinct replacements must land on distinct | ||
| // elements, in slot (ordinal) order, not all collapse onto the first same-filename element. | ||
|
|
||
| // A minimal stand-in for a replacement and a live page element, so the test needs no DOM. | ||
| interface Repl { | ||
| incomingId: string; // "{pageId}:{ordinal}" | ||
| oldSrc: string; // filename the replacement wants to land on | ||
| newSrc: string; // the replacement image (only used to identify the pair here) | ||
| } | ||
| interface El { | ||
| filename: string; // filename the live element currently shows | ||
| tag: string; // just a label so assertions can name the element | ||
| } | ||
|
|
||
| const ordinalOf = (r: Repl) => | ||
| parseInt(r.incomingId.split(":").pop() ?? "", 10) || 0; | ||
|
|
||
| // Run the matcher with the Repl/El accessors wired up the way the real caller does. | ||
| function match(replacements: Repl[], candidates: El[]) { | ||
| return matchReplacementsToElements( | ||
| replacements, | ||
| ordinalOf, | ||
| (r) => r.oldSrc, | ||
| candidates, | ||
| (e) => e.filename, | ||
| ); | ||
| } | ||
|
|
||
| describe("matchReplacementsToElements", () => { | ||
| test("matches a single replacement to the element with the same filename", () => { | ||
| const els: El[] = [ | ||
| { filename: "a.png", tag: "A" }, | ||
| { filename: "b.png", tag: "B" }, | ||
| ]; | ||
| const result = match( | ||
| [{ incomingId: "p:1", oldSrc: "b.png", newSrc: "new-b.png" }], | ||
| els, | ||
| ); | ||
|
|
||
| expect(result).toHaveLength(1); | ||
| expect(result[0].element.tag).toBe("B"); | ||
| expect(result[0].replacement.newSrc).toBe("new-b.png"); | ||
| }); | ||
|
|
||
| test("two same-filename slots get distinct elements, paired by ordinal order", () => { | ||
| // Two placeholder slots that both currently show placeholder.png. The ordinal-0 | ||
| // replacement must take the first placeholder element and ordinal-1 the second — | ||
| // neither collapsing onto the same element. | ||
| const els: El[] = [ | ||
| { filename: "placeholder.png", tag: "first" }, | ||
| { filename: "placeholder.png", tag: "second" }, | ||
| ]; | ||
| // Deliberately pass them out of ordinal order to prove the matcher sorts. | ||
| const result = match( | ||
| [ | ||
| { | ||
| incomingId: "p:1", | ||
| oldSrc: "placeholder.png", | ||
| newSrc: "gen-1.png", | ||
| }, | ||
| { | ||
| incomingId: "p:0", | ||
| oldSrc: "placeholder.png", | ||
| newSrc: "gen-0.png", | ||
| }, | ||
| ], | ||
| els, | ||
| ); | ||
|
|
||
| expect(result).toHaveLength(2); | ||
| // Applied in ascending ordinal order: ordinal 0 first, ordinal 1 second. | ||
| expect(result[0].replacement.newSrc).toBe("gen-0.png"); | ||
| expect(result[0].element.tag).toBe("first"); | ||
| expect(result[1].replacement.newSrc).toBe("gen-1.png"); | ||
| expect(result[1].element.tag).toBe("second"); | ||
| // Sanity: the two replacements did NOT land on the same element. | ||
| expect(result[0].element).not.toBe(result[1].element); | ||
| }); | ||
|
|
||
| test("a replacement with no filename match is skipped, others still match", () => { | ||
| const els: El[] = [{ filename: "a.png", tag: "A" }]; | ||
| const result = match( | ||
| [ | ||
| { | ||
| incomingId: "p:0", | ||
| oldSrc: "missing.png", | ||
| newSrc: "gen-miss.png", | ||
| }, | ||
| { incomingId: "p:1", oldSrc: "a.png", newSrc: "gen-a.png" }, | ||
| ], | ||
| els, | ||
| ); | ||
|
|
||
| expect(result).toHaveLength(1); | ||
| expect(result[0].replacement.newSrc).toBe("gen-a.png"); | ||
| expect(result[0].element.tag).toBe("A"); | ||
| }); | ||
|
|
||
| test("more same-filename replacements than elements: extras drop out, no reuse", () => { | ||
| const els: El[] = [{ filename: "dup.png", tag: "only" }]; | ||
| const result = match( | ||
| [ | ||
| { incomingId: "p:0", oldSrc: "dup.png", newSrc: "gen-0.png" }, | ||
| { incomingId: "p:1", oldSrc: "dup.png", newSrc: "gen-1.png" }, | ||
| ], | ||
| els, | ||
| ); | ||
|
|
||
| // Only one element exists, so only the first (ordinal 0) replacement lands; the | ||
| // second finds no unused same-filename element and is omitted rather than reusing. | ||
| expect(result).toHaveLength(1); | ||
| expect(result[0].replacement.newSrc).toBe("gen-0.png"); | ||
| }); | ||
|
|
||
| test("does not mutate the caller's replacements array order", () => { | ||
| const replacements: Repl[] = [ | ||
| { incomingId: "p:2", oldSrc: "a.png", newSrc: "n2.png" }, | ||
| { incomingId: "p:0", oldSrc: "a.png", newSrc: "n0.png" }, | ||
| ]; | ||
| const els: El[] = [ | ||
| { filename: "a.png", tag: "A0" }, | ||
| { filename: "a.png", tag: "A2" }, | ||
| ]; | ||
| match(replacements, els); | ||
|
|
||
| // The matcher sorts a copy; the original array keeps its input order. | ||
| expect(replacements[0].incomingId).toBe("p:2"); | ||
| expect(replacements[1].incomingId).toBe("p:0"); | ||
| }); | ||
| }); |
60 changes: 60 additions & 0 deletions
60
src/BloomBrowserUI/bookEdit/toolbox/canvas/aiEditorSlotMatching.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Pure slot-matching helper for applying AI-editor replacements to the currently-open page. | ||
| // | ||
| // When the AI commit returns replacements for the page the user is looking at, the front-end | ||
| // has to pair each replacement with the right live image element. Matching is by filename | ||
| // (a cache-busting query string or path prefix on the live element would defeat a full-src | ||
| // compare), but a single page can have several image slots that share the same filename — | ||
| // e.g. two empty placeholders, or the same photo used twice. Filename alone can't tell those | ||
| // apart, so we (a) apply in slot order — the ordinal in each replacement's "{pageId}:{n}" id | ||
| // counts the saved page's image holders in document order, which is the live candidates' | ||
| // order too — and (b) consume each element at most once, so distinct replacements land on | ||
| // distinct elements instead of all collapsing onto the first same-filename match. | ||
| // | ||
| // This is factored out of canvasControlRegistry.ts's editWithAi command so the pairing logic | ||
| // can be unit-tested without a DOM or the changeImage side effects; the caller supplies the | ||
| // ordinal/filename accessors and performs the actual image swap on the returned pairs. | ||
|
|
||
| export interface IReplacementMatch<TReplacement, TElement> { | ||
| replacement: TReplacement; | ||
| element: TElement; | ||
| } | ||
|
|
||
| /** | ||
| * Pairs replacements to candidate elements by filename, in ascending ordinal order, using each | ||
| * candidate at most once. A replacement with no filename match (given the still-unused | ||
| * candidates) is skipped and simply omitted from the result. | ||
| * | ||
| * @param replacements the current-page replacements to place | ||
| * @param ordinalOf extracts a replacement's slot ordinal (used only to order placement) | ||
| * @param wantedFilenameOf the filename a replacement wants to land on (from its oldSrc) | ||
| * @param candidates the live page's image-bearing elements, in document order | ||
| * @param candidateFilenameOf the filename currently shown by a candidate element | ||
| * @returns one {replacement, element} pair per successfully matched replacement, in the order | ||
| * they were applied (ascending ordinal) | ||
| */ | ||
| export function matchReplacementsToElements<TReplacement, TElement>( | ||
| replacements: TReplacement[], | ||
| ordinalOf: (replacement: TReplacement) => number, | ||
| wantedFilenameOf: (replacement: TReplacement) => string, | ||
| candidates: TElement[], | ||
| candidateFilenameOf: (element: TElement) => string, | ||
| ): Array<IReplacementMatch<TReplacement, TElement>> { | ||
| const used = new Set<TElement>(); | ||
| const matches: Array<IReplacementMatch<TReplacement, TElement>> = []; | ||
| [...replacements] | ||
|
hatton marked this conversation as resolved.
|
||
| .sort((a, b) => ordinalOf(a) - ordinalOf(b)) | ||
| .forEach((replacement) => { | ||
| const wanted = wantedFilenameOf(replacement); | ||
| const element = candidates.find( | ||
| (candidate) => | ||
| !used.has(candidate) && | ||
| candidateFilenameOf(candidate) === wanted, | ||
| ); | ||
| if (element === undefined) { | ||
| return; | ||
| } | ||
| used.add(element); | ||
| matches.push({ replacement, element }); | ||
| }); | ||
| return matches; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.