Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(webv2): merged tag chips no longer double-count case variants
joshistoast4cd00a662d52
diff --git a/invokeai/frontend/webv2/src/features/workflow/core/libraryTags.test.ts b/invokeai/frontend/webv2/src/features/workflow/core/libraryTags.test.tsindex 0d5ad86f0c..cfad096c18 100644--- a/invokeai/frontend/webv2/src/features/workflow/core/libraryTags.test.ts+++ b/invokeai/frontend/webv2/src/features/workflow/core/libraryTags.test.ts@@ -16,55 +16,80 @@ describe('parseWorkflowTags', () => {});describe('mergeTagCountsByCase', () => {- it('folds rows that differ only in casing and sums their counts', () => {+ it("takes one variant's count, not their sum, because backend counts are already case-insensitive", () => {+ // `counts_by_tag` counts each requested tag with `tags LIKE '%tag%'`, and+ // SQLite's LIKE is case-insensitive — so the row for 'sdxl' and the row for+ // 'SDXL' are both the full total over the *same* two workflows. Summing+ // them showed "sdxl 4" for a library holding two.expect(mergeTagCountsByCase([{ count: 2, tag: 'sdxl' },+ { count: 2, tag: 'SDXL' },+ ])+ ).toEqual([{ count: 2, tag: 'sdxl' }]);+ });++ it('folds rows that differ only in casing, leaving unrelated tags alone', () => {+ expect(+ mergeTagCountsByCase([+ { count: 5, tag: 'sdxl' },{ count: 5, tag: 'SDXL' },{ count: 1, tag: 'lora' },])).toEqual([- { count: 7, tag: 'SDXL' },+ { count: 5, tag: 'sdxl' },{ count: 1, tag: 'lora' },]);});- it('displays the casing of the single biggest contributing row, not the first seen', () => {+ it('takes the largest count if the variants ever disagree', () => {+ // They should not, given the backend's LIKE semantics — but a merged chip+ // that under-reports is worse than one that is merely robust.expect(mergeTagCountsByCase([{ count: 3, tag: 'Upscaling' },{ count: 4, tag: 'upscaling' },{ count: 2, tag: 'UPSCALING' },])- ).toEqual([{ count: 9, tag: 'upscaling' }]);+ ).toEqual([{ count: 4, tag: 'upscaling' }]);+ });++ it('labels the merged chip with the casing of the biggest row', () => {+ expect(+ mergeTagCountsByCase([+ { count: 2, tag: 'Lora' },+ { count: 6, tag: 'LoRA' },+ ])+ ).toEqual([{ count: 6, tag: 'LoRA' }]);});it('breaks a count tie lexicographically so the chip label is stable', () => {+ // The normal case: equal counts, so this tiebreak is what actually decides.expect(mergeTagCountsByCase([{ count: 3, tag: 'SDXL' },{ count: 3, tag: 'sdxl' },])- ).toEqual([{ count: 6, tag: 'sdxl' }]);+ ).toEqual([{ count: 3, tag: 'sdxl' }]);// Same rows, opposite input order — the winner must not depend on it.expect(mergeTagCountsByCase([{ count: 3, tag: 'sdxl' },{ count: 3, tag: 'SDXL' },])- ).toEqual([{ count: 6, tag: 'sdxl' }]);+ ).toEqual([{ count: 3, tag: 'sdxl' }]);});it('leaves the input rows untouched', () => {const counts = [- { count: 1, tag: 'SDXL' },+ { count: 2, tag: 'SDXL' },{ count: 2, tag: 'sdxl' },] as const;mergeTagCountsByCase(counts);expect(counts).toEqual([- { count: 1, tag: 'SDXL' },+ { count: 2, tag: 'SDXL' },{ count: 2, tag: 'sdxl' },]);});@@ -85,7 +110,7 @@ describe('sortTagCounts', () => {]);});- it('merges case-duplicate rows before ordering them', () => {+ it('merges case-duplicate rows before ordering them, without inflating their count', () => {expect(sortTagCounts([{ count: 2, tag: 'sdxl' },@@ -93,8 +118,8 @@ describe('sortTagCounts', () => {{ count: 2, tag: 'SDXL' },])).toEqual([- { count: 4, tag: 'sdxl' },{ count: 3, tag: 'lora' },+ { count: 2, tag: 'sdxl' },]);});diff --git a/invokeai/frontend/webv2/src/features/workflow/core/libraryTags.ts b/invokeai/frontend/webv2/src/features/workflow/core/libraryTags.tsindex 4f65144bef..31a6e5f66b 100644--- a/invokeai/frontend/webv2/src/features/workflow/core/libraryTags.ts+++ b/invokeai/frontend/webv2/src/features/workflow/core/libraryTags.ts@@ -24,39 +24,44 @@ export const parseWorkflowTags = (tags: string | null | undefined): string[] =>};/**- * Folds rows that differ only in casing into one chip. The backend counts tags- * exactly as they were stored, so a library where some workflows say `sdxl` and- * others `SDXL` reports two rows for what is, to the user, one tag — filtering- * is a case-insensitive SQLite LIKE, so both chips return the same workflows.- * Counts are summed and the label is the casing of the single biggest- * contributing row (ties broken lexicographically, so the chip never depends on- * the order the backend happened to return).+ * Folds rows that differ only in casing into one chip. The backend reports one+ * row per *stored* casing, so a library where some workflows say `sdxl` and+ * others `SDXL` gets two rows for what is, to the user, one tag.+ *+ * The merged count is the **maximum** of the variants, never their sum: the+ * backend counts each requested tag with `tags LIKE '%tag%'`, and SQLite's LIKE+ * is case-insensitive, so every variant's count is *already* the full+ * case-insensitive total — the same workflows, counted once per casing. Summing+ * them showed "sdxl 4" over two workflows. Given those semantics the variants+ * are equal anyway; taking the max just refuses to be wrong if they ever drift.+ *+ * The label is the casing of the biggest contributing row, ties broken+ * lexicographically — and since equal counts are the normal case, that tiebreak+ * is what actually decides, which keeps the chip stable whatever order the+ * backend returned.*/export const mergeTagCountsByCase = (counts: readonly WorkflowTagCount[]): WorkflowTagCount[] => {- const merged = new Map<string, { count: number; tag: string; tagCount: number }>();+ const merged = new Map<string, WorkflowTagCount>();for (const entry of counts) {const key = entry.tag.toLowerCase();const existing = merged.get(key);if (!existing) {- merged.set(key, { count: entry.count, tag: entry.tag, tagCount: entry.count });+ merged.set(key, { count: entry.count, tag: entry.tag });continue;}- existing.count += entry.count;-- // Compared against the winning *row's* own count, not the running total.- if (- entry.count > existing.tagCount ||- (entry.count === existing.tagCount && entry.tag.localeCompare(existing.tag) < 0)- ) {+ // Compared before the max is folded in, so `existing.count` is still the+ // count of the row whose casing currently wins.+ if (entry.count > existing.count || (entry.count === existing.count && entry.tag.localeCompare(existing.tag) < 0)) {existing.tag = entry.tag;- existing.tagCount = entry.count;}++ existing.count = Math.max(existing.count, entry.count);}- return [...merged.values()].map(({ count, tag }) => ({ count, tag }));+ return [...merged.values()];};/** Orders tag chips by how many workflows carry them, then alphabetically; case-duplicate and empty tags are merged away. */