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): workflow library api gains last_run_at, thumbnail writes, invalidation listeners

joshistoast4d1c7a27c159
diff --git a/invokeai/frontend/webv2/src/features/workflow/data/api.test.ts b/invokeai/frontend/webv2/src/features/workflow/data/api.test.ts
new file mode 100644
index 0000000000..bc8be86384
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/workflow/data/api.test.ts
@@ -0,0 +1,93 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const mocks = vi.hoisted(() => ({ apiFetch: vi.fn(), apiFetchJson: vi.fn() }));
+
+vi.mock('@platform/transport/http', () => ({ apiFetch: mocks.apiFetch, apiFetchJson: mocks.apiFetchJson }));
+
+import {
+ deleteLibraryWorkflowThumbnail,
+ getAllWorkflowTags,
+ getWorkflowTagCounts,
+ listLibraryWorkflows,
+ setLibraryWorkflowThumbnail,
+ touchLibraryWorkflowLastRunAt,
+} from './api';
+
+describe('workflow library api', () => {
+ beforeEach(() => {
+ mocks.apiFetch.mockReset().mockResolvedValue(new Response());
+ mocks.apiFetchJson.mockReset().mockResolvedValue({});
+ });
+
+ it('touches last_run_at with a PUT to the record-scoped endpoint', async () => {
+ await touchLibraryWorkflowLastRunAt('abc');
+
+ expect(mocks.apiFetch).toHaveBeenCalledWith('/api/v1/workflows/i/abc/last_run_at', {
+ method: 'PUT',
+ signal: undefined,
+ });
+ });
+
+ it('writes a thumbnail as multipart form data under the image field', async () => {
+ const blob = new Blob(['fake-image-bytes'], { type: 'image/png' });
+
+ await setLibraryWorkflowThumbnail('abc', blob);
+
+ expect(mocks.apiFetch).toHaveBeenCalledTimes(1);
+ const [path, init] = mocks.apiFetch.mock.calls[0] as [string, RequestInit];
+
+ expect(path).toBe('/api/v1/workflows/i/abc/thumbnail');
+ expect(init.method).toBe('PUT');
+ expect(init.body).toBeInstanceOf(FormData);
+
+ const uploaded = (init.body as FormData).get('image');
+
+ expect(uploaded).toBeInstanceOf(Blob);
+ expect((uploaded as Blob).size).toBe(blob.size);
+ expect((uploaded as Blob).type).toBe(blob.type);
+ });
+
+ it('deletes a thumbnail with a DELETE to the record-scoped endpoint', async () => {
+ await deleteLibraryWorkflowThumbnail('abc');
+
+ expect(mocks.apiFetch).toHaveBeenCalledWith('/api/v1/workflows/i/abc/thumbnail', {
+ method: 'DELETE',
+ signal: undefined,
+ });
+ });
+
+ it('forwards repeated tags query params when listing workflows', async () => {
+ mocks.apiFetchJson.mockResolvedValue({ items: [], page: 1, pages: 1, total: 0 });
+
+ await listLibraryWorkflows({ category: 'user', page: 1, tags: ['upscaling', 'lora'] });
+
+ expect(mocks.apiFetchJson).toHaveBeenCalledTimes(1);
+ const [path] = mocks.apiFetchJson.mock.calls[0] as [string];
+ const params = new URL(path, 'http://localhost').searchParams;
+
+ expect(params.getAll('tags')).toEqual(['upscaling', 'lora']);
+ });
+
+ it('gets tag counts for the given tags', async () => {
+ mocks.apiFetchJson.mockResolvedValue({ lora: 2, upscaling: 1 });
+
+ const result = await getWorkflowTagCounts({ tags: ['lora', 'upscaling'] });
+
+ expect(mocks.apiFetchJson).toHaveBeenCalledTimes(1);
+ const [path] = mocks.apiFetchJson.mock.calls[0] as [string];
+ const params = new URL(path, 'http://localhost').searchParams;
+
+ expect(path.startsWith('/api/v1/workflows/counts_by_tag?')).toBe(true);
+ expect(params.getAll('tags')).toEqual(['lora', 'upscaling']);
+ expect(result).toEqual({ lora: 2, upscaling: 1 });
+ });
+
+ it('gets all workflow tags', async () => {
+ mocks.apiFetchJson.mockResolvedValue(['lora', 'upscaling']);
+
+ const result = await getAllWorkflowTags();
+
+ expect(mocks.apiFetchJson).toHaveBeenCalledWith('/api/v1/workflows/tags', { signal: undefined });
+ expect(result).toEqual(['lora', 'upscaling']);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/workflow/data/api.ts b/invokeai/frontend/webv2/src/features/workflow/data/api.ts
index 12a2c7dd98..12ec989262 100644
--- a/invokeai/frontend/webv2/src/features/workflow/data/api.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/data/api.ts
@@ -17,6 +17,7 @@ export interface WorkflowLibraryListItem {
created_at?: string;
updated_at?: string;
opened_at?: string | null;
+ last_run_at?: string | null;
thumbnail_url?: string | null;
}
@@ -32,6 +33,7 @@ export interface ListWorkflowsParams {
page: number;
perPage?: number;
query?: string;
+ tags?: string[];
signal?: AbortSignal;
}
@@ -40,6 +42,7 @@ export const listLibraryWorkflows = ({
page,
perPage = 20,
query,
+ tags,
signal,
}: ListWorkflowsParams): Promise<WorkflowLibraryPage> => {
const params = new URLSearchParams({
@@ -55,6 +58,10 @@ export const listLibraryWorkflows = ({
params.set('query', query.trim());
}
+ for (const tag of tags ?? []) {
+ params.append('tags', tag);
+ }
+
return apiFetchJson<WorkflowLibraryPage>(`/api/v1/workflows/?${params.toString()}`, { signal });
};
@@ -109,3 +116,84 @@ export const deleteLibraryWorkflow = async (workflowId: string, signal?: AbortSi
export const touchLibraryWorkflowOpenedAt = async (workflowId: string, signal?: AbortSignal): Promise<void> => {
await apiFetch(`/api/v1/workflows/i/${encodeURIComponent(workflowId)}/opened_at`, { method: 'PUT', signal });
};
+
+export const touchLibraryWorkflowLastRunAt = async (workflowId: string, signal?: AbortSignal): Promise<void> => {
+ await apiFetch(`/api/v1/workflows/i/${encodeURIComponent(workflowId)}/last_run_at`, { method: 'PUT', signal });
+};
+
+export const setLibraryWorkflowThumbnail = async (
+ workflowId: string,
+ image: Blob,
+ signal?: AbortSignal
+): Promise<void> => {
+ const body = new FormData();
+ body.append('image', image);
+
+ await apiFetch(`/api/v1/workflows/i/${encodeURIComponent(workflowId)}/thumbnail`, { body, method: 'PUT', signal });
+};
+
+export const deleteLibraryWorkflowThumbnail = async (workflowId: string, signal?: AbortSignal): Promise<void> => {
+ await apiFetch(`/api/v1/workflows/i/${encodeURIComponent(workflowId)}/thumbnail`, { method: 'DELETE', signal });
+};
+
+export interface GetWorkflowTagCountsParams {
+ categories?: WorkflowLibraryCategory[];
+ hasBeenOpened?: boolean;
+ isPublic?: boolean;
+ tags: string[];
+ signal?: AbortSignal;
+}
+
+/** Thin wrapper over `GET /api/v1/workflows/counts_by_tag`; returns the route's JSON verbatim. */
+export const getWorkflowTagCounts = ({
+ categories,
+ hasBeenOpened,
+ isPublic,
+ tags,
+ signal,
+}: GetWorkflowTagCountsParams): Promise<Record<string, number>> => {
+ const params = new URLSearchParams();
+
+ for (const tag of tags) {
+ params.append('tags', tag);
+ }
+
+ for (const category of categories ?? []) {
+ params.append('categories', category);
+ }
+
+ if (hasBeenOpened !== undefined) {
+ params.set('has_been_opened', String(hasBeenOpened));
+ }
+
+ if (isPublic !== undefined) {
+ params.set('is_public', String(isPublic));
+ }
+
+ return apiFetchJson<Record<string, number>>(`/api/v1/workflows/counts_by_tag?${params.toString()}`, { signal });
+};
+
+export interface GetAllWorkflowTagsParams {
+ categories?: WorkflowLibraryCategory[];
+ isPublic?: boolean;
+ signal?: AbortSignal;
+}
+
+/** Thin wrapper over `GET /api/v1/workflows/tags`; returns the route's JSON verbatim. */
+export const getAllWorkflowTags = ({ categories, isPublic, signal }: GetAllWorkflowTagsParams = {}): Promise<
+ string[]
+> => {
+ const params = new URLSearchParams();
+
+ for (const category of categories ?? []) {
+ params.append('categories', category);
+ }
+
+ if (isPublic !== undefined) {
+ params.set('is_public', String(isPublic));
+ }
+
+ const query = params.toString();
+
+ return apiFetchJson<string[]>(`/api/v1/workflows/tags${query ? `?${query}` : ''}`, { signal });
+};
diff --git a/invokeai/frontend/webv2/src/features/workflow/data/libraryCache.test.ts b/invokeai/frontend/webv2/src/features/workflow/data/libraryCache.test.ts
index c3acb313d1..52f9af3bc7 100644
--- a/invokeai/frontend/webv2/src/features/workflow/data/libraryCache.test.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/data/libraryCache.test.ts
@@ -80,3 +80,17 @@ describe('workflow library account ownership', () => {
await expect(cache.getLibraryWorkflowCached('shared-id')).resolves.toEqual({ owner: 'b' });
});
});
+
+describe('workflow library cache invalidation listeners', () => {
+ it('notifies registered listeners when the cache is invalidated', () => {
+ const listener = vi.fn();
+ const unsubscribe = cache.onWorkflowLibraryCacheInvalidated(listener);
+
+ cache.invalidateWorkflowLibraryCache();
+ expect(listener).toHaveBeenCalledTimes(1);
+
+ unsubscribe();
+ cache.invalidateWorkflowLibraryCache();
+ expect(listener).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/workflow/data/libraryCache.ts b/invokeai/frontend/webv2/src/features/workflow/data/libraryCache.ts
index 8c977bd9ae..653c7a68e1 100644
--- a/invokeai/frontend/webv2/src/features/workflow/data/libraryCache.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/data/libraryCache.ts
@@ -60,9 +60,21 @@ export const getLibraryWorkflowCached = async (
return result;
};
+const invalidationListeners = new Set<() => void>();
+
+/** Registers a listener fired at the end of every `invalidateWorkflowLibraryCache()` call. */
+export const onWorkflowLibraryCacheInvalidated = (listener: () => void): (() => void) => {
+ invalidationListeners.add(listener);
+ return () => invalidationListeners.delete(listener);
+};
+
export const invalidateWorkflowLibraryCache = (): void => {
pageCache.clear();
workflowCache.clear();
+
+ for (const listener of invalidationListeners) {
+ listener();
+ }
};
registerAccountOwnedResource({