Skip to content

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.

feat(webv2): add mixed gallery item contracts

joshistoast172a7d91944d
diff --git a/invokeai/frontend/webv2/src/features/gallery/contracts.ts b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
index b0451f9578..6c1d4a5d7e 100644
--- a/invokeai/frontend/webv2/src/features/gallery/contracts.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/contracts.ts
@@ -10,6 +10,25 @@ export type {
GeneratedImageContract,
} from './core/types';
export { normalizeGalleryImage } from './core/image';
+export {
+ assertNeverGalleryItem,
+ formatGalleryVideoDuration,
+ galleryImageItemToGalleryImage,
+ isGalleryImageItem,
+ legacyGeneratedImageToGalleryItem,
+ parseGalleryItemKey,
+ toGalleryItemKey,
+ toGalleryItemRef,
+ type GalleryImageItem,
+ type GalleryItem,
+ type GalleryItemCategory,
+ type GalleryItemKey,
+ type GalleryItemKind,
+ type GalleryItemMutationResult,
+ type GalleryItemRef,
+ type GalleryItemsPage,
+ type GalleryVideoItem,
+} from './core/items';
export { GALLERY_RECENT_IMAGE_LIMIT, getBoundedRecentImages } from './core/recentImages';
export {
DEFAULT_GALLERY_SETTINGS,
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/items.test.ts b/invokeai/frontend/webv2/src/features/gallery/core/items.test.ts
new file mode 100644
index 0000000000..d0ac80ae13
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/gallery/core/items.test.ts
@@ -0,0 +1,136 @@
+import { describe, expect, it } from 'vitest';
+
+import type { GalleryImage, GeneratedImageContract } from './types';
+
+import {
+ assertNeverGalleryItem,
+ formatGalleryVideoDuration,
+ galleryImageItemToGalleryImage,
+ isGalleryImageItem,
+ legacyGeneratedImageToGalleryItem,
+ parseGalleryItemKey,
+ toGalleryItemKey,
+ toGalleryItemRef,
+ type GalleryImageItem,
+} from './items';
+
+const generatedImage: GeneratedImageContract = {
+ height: 512,
+ imageName: 'legacy.png',
+ imageUrl: '/images/legacy.png/full',
+ queuedAt: '2026-07-30T12:00:00.000Z',
+ sourceQueueItemId: 'queue-123',
+ thumbnailUrl: '/images/legacy.png/thumbnail',
+ width: 768,
+};
+
+describe('gallery item keys', () => {
+ it('creates an unambiguous key and reference for every media kind', () => {
+ const item: GalleryImageItem = {
+ boardId: 'none',
+ category: 'general',
+ createdAt: '2026-07-30T12:00:00.000Z',
+ fullUrl: '/images/example.png/full',
+ height: 512,
+ isIntermediate: false,
+ kind: 'image',
+ name: 'folder:example.png',
+ starred: false,
+ thumbnailUrl: '/images/example.png/thumbnail',
+ width: 768,
+ };
+
+ expect(toGalleryItemKey(item)).toBe('image:folder:example.png');
+ expect(toGalleryItemRef(item)).toEqual({ kind: 'image', name: 'folder:example.png' });
+ });
+
+ it('parses only non-empty exact media prefixes while retaining legacy names', () => {
+ expect(parseGalleryItemKey('image:folder:example.png')).toEqual({ kind: 'image', name: 'folder:example.png' });
+ expect(parseGalleryItemKey('video:clip:take-1.mp4')).toEqual({ kind: 'video', name: 'clip:take-1.mp4' });
+ expect(parseGalleryItemKey('legacy:folder:example.png')).toEqual({
+ kind: 'image',
+ name: 'legacy:folder:example.png',
+ });
+ expect(parseGalleryItemKey('image:')).toEqual({ kind: 'image', name: 'image:' });
+ expect(parseGalleryItemKey('video:')).toEqual({ kind: 'image', name: 'video:' });
+ expect(parseGalleryItemKey('bare.png')).toEqual({ kind: 'image', name: 'bare.png' });
+ });
+});
+
+describe('gallery item compatibility', () => {
+ it('normalizes missing legacy gallery context when adapting a generated image', () => {
+ expect(legacyGeneratedImageToGalleryItem(generatedImage)).toEqual({
+ boardId: 'none',
+ category: 'general',
+ createdAt: '2026-07-30T12:00:00.000Z',
+ fullUrl: '/images/legacy.png/full',
+ height: 512,
+ isIntermediate: false,
+ kind: 'image',
+ name: 'legacy.png',
+ sourceQueueItemId: 'queue-123',
+ starred: false,
+ thumbnailUrl: '/images/legacy.png/thumbnail',
+ width: 768,
+ });
+ });
+
+ it('round-trips an image item into the legacy gallery contract with a backend source fallback', () => {
+ const item: GalleryImageItem = {
+ boardId: 'board-1',
+ category: 'user',
+ createdAt: '2026-07-30T12:00:00.000Z',
+ fullUrl: '/images/upload.png/full',
+ height: 512,
+ isIntermediate: false,
+ kind: 'image',
+ name: 'upload.png',
+ starred: true,
+ thumbnailUrl: '/images/upload.png/thumbnail',
+ width: 768,
+ };
+
+ const expected: GalleryImage = {
+ boardId: 'board-1',
+ height: 512,
+ imageCategory: 'user',
+ imageName: 'upload.png',
+ imageUrl: '/images/upload.png/full',
+ queuedAt: '2026-07-30T12:00:00.000Z',
+ sourceQueueItemId: 'backend-gallery',
+ starred: true,
+ thumbnailUrl: '/images/upload.png/thumbnail',
+ width: 768,
+ };
+
+ expect(galleryImageItemToGalleryImage(item)).toEqual(expected);
+ });
+});
+
+describe('gallery item discriminators', () => {
+ it('recognizes images and rejects videos', () => {
+ const image = legacyGeneratedImageToGalleryItem(generatedImage);
+ const video = { ...image, durationSeconds: 12, kind: 'video' as const };
+
+ expect(isGalleryImageItem(image)).toBe(true);
+ expect(isGalleryImageItem(video)).toBe(false);
+ });
+
+ it('throws when an exhaustive gallery-item branch receives an impossible value', () => {
+ expect(() => assertNeverGalleryItem('audio' as never)).toThrow('Unexpected gallery item');
+ });
+});
+
+describe('gallery video duration formatting', () => {
+ it.each([
+ [Number.NaN, '0:00'],
+ [-1, '0:00'],
+ [0, '0:00'],
+ [0.1, '0:01'],
+ [59.01, '1:00'],
+ [65, '1:05'],
+ [3_601, '1:00:01'],
+ ])('formats %s seconds as %s', (duration, expected) => {
+ expect(formatGalleryVideoDuration(duration)).toBe(expected);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/items.ts b/invokeai/frontend/webv2/src/features/gallery/core/items.ts
new file mode 100644
index 0000000000..c49f30625e
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/gallery/core/items.ts
@@ -0,0 +1,118 @@
+import type { GalleryImage, GeneratedImageContract } from './types';
+
+export type GalleryItemKind = 'image' | 'video';
+
+export type GalleryItemCategory = GalleryImage['imageCategory'];
+
+export interface GalleryItemRef {
+ kind: GalleryItemKind;
+ name: string;
+}
+
+export type GalleryItemKey = `${GalleryItemKind}:${string}`;
+
+interface GalleryItemBase {
+ boardId: string;
+ category: GalleryItemCategory;
+ createdAt: string;
+ fullUrl: string;
+ height: number;
+ isIntermediate: boolean;
+ name: string;
+ starred: boolean;
+ thumbnailUrl: string;
+ width: number;
+}
+
+export interface GalleryImageItem extends GalleryItemBase {
+ kind: 'image';
+ sourceQueueItemId?: string;
+}
+
+export interface GalleryVideoItem extends GalleryItemBase {
+ durationSeconds: number;
+ fps?: number;
+ kind: 'video';
+}
+
+export type GalleryItem = GalleryImageItem | GalleryVideoItem;
+
+export interface GalleryItemsPage {
+ items: GalleryItem[];
+ total: number;
+}
+
+export interface GalleryItemMutationResult {
+ failed: GalleryItemRef[];
+ succeeded: GalleryItemRef[];
+}
+
+export const toGalleryItemKey = ({ kind, name }: GalleryItemRef): GalleryItemKey => `${kind}:${name}`;
+
+export const parseGalleryItemKey = (key: string): GalleryItemRef => {
+ const separatorIndex = key.indexOf(':');
+ const kind = key.slice(0, separatorIndex);
+ const name = key.slice(separatorIndex + 1);
+
+ if ((kind === 'image' || kind === 'video') && name.length > 0) {
+ return { kind, name };
+ }
+
+ return { kind: 'image', name: key };
+};
+
+export const toGalleryItemRef = ({ kind, name }: GalleryItem): GalleryItemRef => ({ kind, name });
+
+export const isGalleryImageItem = (item: GalleryItem): item is GalleryImageItem => item.kind === 'image';
+
+export const assertNeverGalleryItem = (item: never): never => {
+ throw new Error(`Unexpected gallery item: ${String(item)}`);
+};
+
+type LegacyGalleryImage = GeneratedImageContract & Partial<Pick<GalleryImage, 'boardId' | 'imageCategory' | 'starred'>>;
+
+export const legacyGeneratedImageToGalleryItem = (image: LegacyGalleryImage): GalleryImageItem => ({
+ boardId: image.boardId ?? 'none',
+ category: image.imageCategory ?? 'general',
+ createdAt: image.queuedAt,
+ fullUrl: image.imageUrl,
+ height: image.height,
+ isIntermediate: false,
+ kind: 'image',
+ name: image.imageName,
+ sourceQueueItemId: image.sourceQueueItemId,
+ starred: image.starred ?? false,
+ thumbnailUrl: image.thumbnailUrl,
+ width: image.width,
+});
+
+export const galleryImageItemToGalleryImage = (item: GalleryImageItem): GalleryImage => ({
+ boardId: item.boardId,
+ height: item.height,
+ imageCategory: item.category,
+ imageName: item.name,
+ imageUrl: item.fullUrl,
+ queuedAt: item.createdAt,
+ sourceQueueItemId: item.sourceQueueItemId ?? 'backend-gallery',
+ starred: item.starred,
+ thumbnailUrl: item.thumbnailUrl,
+ width: item.width,
+});
+
+export const formatGalleryVideoDuration = (durationSeconds: number): string => {
+ if (!Number.isFinite(durationSeconds) || durationSeconds < 0) {
+ return '0:00';
+ }
+
+ const totalSeconds = Math.ceil(durationSeconds);
+ const seconds = totalSeconds % 60;
+ const totalMinutes = Math.floor(totalSeconds / 60);
+
+ if (totalMinutes < 60) {
+ return `${totalMinutes}:${String(seconds).padStart(2, '0')}`;
+ }
+
+ const minutes = totalMinutes % 60;
+ const hours = Math.floor(totalMinutes / 60);
+ return `${hours}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
+};
diff --git a/invokeai/frontend/webv2/src/features/gallery/core/recentImages.test.ts b/invokeai/frontend/webv2/src/features/gallery/core/recentImages.test.ts
new file mode 100644
index 0000000000..7d33ab6786
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/gallery/core/recentImages.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, it } from 'vitest';
+
+import type { GeneratedImageContract } from './types';
+
+import { getBoundedRecentImages } from './recentImages';
+
+describe('getBoundedRecentImages', () => {
+ it('retains the original generated-image object and its gallery context by reference', () => {
+ const image: GeneratedImageContract & { boardId: string; imageCategory: 'general'; starred: boolean } = {
+ boardId: 'board-1',
+ height: 512,
+ imageCategory: 'general',
+ imageName: 'recent.png',
+ imageUrl: '/images/recent.png/full',
+ queuedAt: '2026-07-30T12:00:00.000Z',
+ sourceQueueItemId: 'queue-123',
+ starred: true,
+ thumbnailUrl: '/images/recent.png/thumbnail',
+ width: 768,
+ };
+
+ const [recent] = getBoundedRecentImages([image]);
+
+ expect(recent).toBe(image);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/gallery/index.ts b/invokeai/frontend/webv2/src/features/gallery/index.ts
index dd99256843..e0ec20066d 100644
--- a/invokeai/frontend/webv2/src/features/gallery/index.ts
+++ b/invokeai/frontend/webv2/src/features/gallery/index.ts
@@ -9,6 +9,25 @@ export type {
GalleryView,
GeneratedImageContract,
} from './core/types';
+export {
+ assertNeverGalleryItem,
+ formatGalleryVideoDuration,
+ galleryImageItemToGalleryImage,
+ isGalleryImageItem,
+ legacyGeneratedImageToGalleryItem,
+ parseGalleryItemKey,
+ toGalleryItemKey,
+ toGalleryItemRef,
+ type GalleryImageItem,
+ type GalleryItem,
+ type GalleryItemCategory,
+ type GalleryItemKey,
+ type GalleryItemKind,
+ type GalleryItemMutationResult,
+ type GalleryItemRef,
+ type GalleryItemsPage,
+ type GalleryVideoItem,
+} from './core/items';
import {
addImagesToGalleryBoard,
deleteGalleryImages,