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): normalize workbench widget manifests

joshistoast4dcf1bc4da32
diff --git a/invokeai/frontend/webv2/src/workbench/WorkbenchWidgetRegistryContext.tsx b/invokeai/frontend/webv2/src/workbench/WorkbenchWidgetRegistryContext.tsx
index eee6fbb577..bd61387b53 100644
--- a/invokeai/frontend/webv2/src/workbench/WorkbenchWidgetRegistryContext.tsx
+++ b/invokeai/frontend/webv2/src/workbench/WorkbenchWidgetRegistryContext.tsx
@@ -1,21 +1,27 @@
import { createContext, use, useMemo, type ReactNode } from 'react';
-import type { RegisteredWidget, WorkbenchRegion } from './types';
+import type { RegisteredWidget, WidgetRegion, WidgetTypeId } from './types';
interface WorkbenchWidgetRegistryContextValue {
- getWidgetsForRegion: (region: WorkbenchRegion) => RegisteredWidget[];
+ getWidgetById: (widgetId: WidgetTypeId) => RegisteredWidget | undefined;
+ getWidgetsForRegion: (region: WidgetRegion) => RegisteredWidget[];
}
const WorkbenchWidgetRegistryContext = createContext<WorkbenchWidgetRegistryContextValue | null>(null);
export const WorkbenchWidgetRegistryProvider = ({
children,
+ getWidgetById,
getWidgetsForRegion,
}: {
children: ReactNode;
- getWidgetsForRegion: (region: WorkbenchRegion) => RegisteredWidget[];
+ getWidgetById: (widgetId: WidgetTypeId) => RegisteredWidget | undefined;
+ getWidgetsForRegion: (region: WidgetRegion) => RegisteredWidget[];
}) => {
- const value = useMemo<WorkbenchWidgetRegistryContextValue>(() => ({ getWidgetsForRegion }), [getWidgetsForRegion]);
+ const value = useMemo<WorkbenchWidgetRegistryContextValue>(
+ () => ({ getWidgetById, getWidgetsForRegion }),
+ [getWidgetById, getWidgetsForRegion]
+ );
return <WorkbenchWidgetRegistryContext value={value}>{children}</WorkbenchWidgetRegistryContext>;
};
diff --git a/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts b/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts
new file mode 100644
index 0000000000..fa6de53f12
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/widgetRegistry.test.ts
@@ -0,0 +1,85 @@
+import { describe, expect, it } from 'vitest';
+
+import type { WidgetManifest } from './types';
+
+import { registerFirstPartyWidgets, registerWidgets } from './widgetRegistry';
+
+const TestIcon = () => null;
+const TestView = () => null;
+
+const createManifest = (overrides: Partial<WidgetManifest> = {}): WidgetManifest => ({
+ allowMultiple: false,
+ allowedRegions: ['right'],
+ failurePolicy: { isolateRenderFailure: true, onRegistrationFailure: 'disable' },
+ icon: TestIcon,
+ id: 'vendor.widget',
+ label: 'Test Widget',
+ labelText: 'Test Widget',
+ version: 1,
+ view: TestView,
+ ...overrides,
+});
+
+describe('widget registry', () => {
+ it('registers first-party widget manifests without icon validation failures', () => {
+ const widgets = registerFirstPartyWidgets();
+
+ expect(widgets).toHaveLength(15);
+ expect(widgets.flatMap((widget) => widget.failure ?? [])).toEqual([]);
+ expect(widgets.every((widget) => widget.status === 'enabled')).toBe(true);
+ });
+
+ it('normalizes default state and api version', () => {
+ const [widget] = registerWidgets([createManifest({ state: undefined })]);
+
+ expect(widget.status).toBe('enabled');
+ expect(widget.manifest.apiVersion).toBe(1);
+ expect(widget.manifest.state).toMatchObject({ persistence: 'project', version: 1 });
+ expect(widget.manifest.state.createInitial()).toEqual({});
+ });
+
+ it('disables invalid icon manifests', () => {
+ const [widget] = registerWidgets([createManifest({ icon: 'invalid' as unknown as WidgetManifest['icon'] })]);
+
+ expect(widget.status).toBe('disabled');
+ expect(widget.failure?.message).toContain('must provide an icon component');
+ });
+
+ it('fails closed for empty regions', () => {
+ const [widget] = registerWidgets([createManifest({ allowedRegions: [] })]);
+
+ expect(widget.status).toBe('disabled');
+ expect(widget.failure?.message).toContain('must declare at least one allowed region');
+ });
+
+ it('fails closed for renderable regions without a view', () => {
+ const [widget] = registerWidgets([createManifest({ view: undefined })]);
+
+ expect(widget.status).toBe('disabled');
+ expect(widget.failure?.message).toContain('does not include manifest.view');
+ });
+
+ it('honors hidden failure policy for invalid manifests', () => {
+ const [widget] = registerWidgets([
+ createManifest({
+ failurePolicy: { isolateRenderFailure: true, onRegistrationFailure: 'hide' },
+ view: undefined,
+ }),
+ ]);
+
+ expect(widget.status).toBe('hidden');
+ expect(widget.failure).toBeDefined();
+ });
+
+ it('rejects unstable ids and unsupported api versions', () => {
+ const [badId, badApiVersion] = registerWidgets([
+ createManifest({ id: 'bad id' }),
+ createManifest({ apiVersion: 2 as unknown as WidgetManifest['apiVersion'], id: 'vendor.bad-api' }),
+ ]);
+
+ expect(badId.status).toBe('disabled');
+ expect(badId.failure?.message).toContain('stable non-empty string id');
+ expect(badApiVersion.status).toBe('disabled');
+ expect(badApiVersion.failure?.message).toContain('unsupported apiVersion');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts b/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
index e7df8320ff..6357f918e4 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetRegistry.ts
@@ -1,13 +1,18 @@
-import type { RegisteredWidget, WidgetFailure, WidgetId, WidgetManifest, WorkbenchRegion } from './types';
+import type {
+ NormalizedWidgetManifest,
+ RegisteredWidget,
+ WidgetFailure,
+ WidgetManifest,
+ WidgetRegion,
+ WidgetTypeId,
+} from './types';
import { getAuthSession } from './auth/session';
-import { isSupportedIconId } from './iconResolver';
import { autosaveStatusWidgetManifest } from './widgets/autosave-status';
import { canvasWidgetManifest } from './widgets/canvas';
import { diagnosticsWidgetManifest } from './widgets/diagnostics';
import { galleryWidgetManifest } from './widgets/gallery';
import { generateWidgetManifest } from './widgets/generate';
-import { historyControlsWidgetManifest } from './widgets/history-controls';
import { layersWidgetManifest } from './widgets/layers';
import { layoutActionsWidgetManifest } from './widgets/layout-actions';
import { modelsWidgetManifest } from './widgets/models';
@@ -19,7 +24,7 @@ import { serverStatusWidgetManifest } from './widgets/server-status';
import { versionStatusWidgetManifest } from './widgets/version-status';
import { workflowWidgetManifest } from './widgets/workflow';
-const firstPartyWidgetManifests: WidgetManifest[] = [
+export const firstPartyWidgetManifests: WidgetManifest[] = [
generateWidgetManifest,
workflowWidgetManifest,
canvasWidgetManifest,
@@ -33,36 +38,60 @@ const firstPartyWidgetManifests: WidgetManifest[] = [
notificationsWidgetManifest,
serverStatusWidgetManifest,
autosaveStatusWidgetManifest,
- historyControlsWidgetManifest,
layoutActionsWidgetManifest,
versionStatusWidgetManifest,
];
-const createFailure = (widgetId: WidgetId, error: unknown): WidgetFailure => ({
+const createFailure = (widgetId: WidgetTypeId, error: unknown): WidgetFailure => ({
details: error instanceof Error ? (error.stack ?? error.message) : String(error),
message: error instanceof Error ? error.message : `Failed to register ${widgetId}.`,
occurredAt: new Date().toISOString(),
widgetId,
});
-const renderableRegions = new Set<WorkbenchRegion>(['bottom', 'center', 'dialog', 'left', 'popover', 'right']);
+const renderableRegions = new Set<WidgetRegion>(['bottom', 'center', 'left', 'right']);
-const validateManifest = (manifest: WidgetManifest): void => {
- if (manifest.regions.length === 0) {
+const isWidgetIconComponent = (value: WidgetManifest['icon']): boolean =>
+ typeof value === 'function' || (typeof value === 'object' && value !== null && '$$typeof' in value);
+
+const validateManifest = (manifest: NormalizedWidgetManifest): void => {
+ if (typeof manifest.id !== 'string' || manifest.id.trim().length === 0 || /\s/.test(manifest.id)) {
+ throw new Error('Widget manifest must provide a stable non-empty string id without whitespace.');
+ }
+
+ if (manifest.apiVersion !== 1) {
+ throw new Error(`Widget ${manifest.id} declares unsupported apiVersion ${String(manifest.apiVersion)}.`);
+ }
+
+ if (manifest.allowedRegions.length === 0) {
throw new Error(`Widget ${manifest.id} must declare at least one allowed region.`);
}
- if (!isSupportedIconId(manifest.icon)) {
- throw new Error(`Widget ${manifest.id} references unsupported icon id ${manifest.icon}.`);
+ for (const region of manifest.allowedRegions) {
+ if (!renderableRegions.has(region)) {
+ throw new Error(`Widget ${manifest.id} declares unsupported region ${String(region)}.`);
+ }
+ }
+
+ if (!isWidgetIconComponent(manifest.icon)) {
+ throw new TypeError(`Widget ${manifest.id} must provide an icon component.`);
}
- if (manifest.regions.some((region) => renderableRegions.has(region)) && !manifest.view) {
+ if (manifest.allowedRegions.some((region) => renderableRegions.has(region)) && !manifest.view) {
throw new Error(`Widget ${manifest.id} declares a renderable region but does not include manifest.view.`);
}
};
-export const registerFirstPartyWidgets = (): RegisteredWidget[] =>
- firstPartyWidgetManifests.map((manifest) => {
+export const normalizeWidgetManifest = (manifest: WidgetManifest): NormalizedWidgetManifest => ({
+ ...manifest,
+ apiVersion: manifest.apiVersion ?? 1,
+ state: manifest.state ?? { createInitial: () => ({}), persistence: 'project', version: 1 },
+});
+
+export const registerWidgets = (manifests: WidgetManifest[]): RegisteredWidget[] =>
+ manifests.map((rawManifest) => {
+ const manifest = normalizeWidgetManifest(rawManifest);
+
try {
validateManifest(manifest);
@@ -75,6 +104,8 @@ export const registerFirstPartyWidgets = (): RegisteredWidget[] =>
}
});
+export const registerFirstPartyWidgets = (): RegisteredWidget[] => registerWidgets(firstPartyWidgetManifests);
+
export const registeredWidgets = registerFirstPartyWidgets();
/**
@@ -93,12 +124,13 @@ const isWidgetAvailable = (widget: RegisteredWidget): boolean => {
return session.multiuserEnabled && session.user?.is_admin === true;
};
-export const getWidgetsForRegion = (region: WorkbenchRegion): RegisteredWidget[] =>
+export const getWidgetsForRegion = (region: WidgetRegion): RegisteredWidget[] =>
registeredWidgets.filter(
- (widget) => widget.status !== 'hidden' && widget.manifest.regions.includes(region) && isWidgetAvailable(widget)
+ (widget) =>
+ widget.status !== 'hidden' && widget.manifest.allowedRegions.includes(region) && isWidgetAvailable(widget)
);
-export const getWidgetById = (widgetId: WidgetId): RegisteredWidget | undefined =>
+export const getWidgetById = (widgetId: WidgetTypeId): RegisteredWidget | undefined =>
registeredWidgets.find((widget) => widget.manifest.id === widgetId);
export const widgetRegistrationFailures = registeredWidgets.flatMap((widget) =>