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.

refactor(workbench): construct the extension registry per mount

joshistoast867d40a8dbaa
diff --git a/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx b/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx
index 6f0a766b66..697bf63eed 100644
--- a/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx
+++ b/invokeai/frontend/webv2/src/workbench/WorkbenchContext.tsx
@@ -8,6 +8,7 @@ import { shallowEqual as selectorShallowEqual, useExternalStoreSelector } from '
import { createContext, use, useEffect, useSyncExternalStore, useState, type ReactNode } from 'react';
import { WorkbenchSplashScreen } from './components/WorkbenchSplashScreen';
+import { createExtensionRegistry, type ExtensionRegistry } from './extensions/extensionRegistry';
import { createWorkbenchPersistenceRuntime } from './persistenceRuntime';
import {
createSyncedWorkbenchPersistence,
@@ -32,6 +33,7 @@ type WorkbenchSelector<T> = (snapshot: WorkbenchSnapshot) => T;
const WorkbenchStoreContext = createContext<WorkbenchInternalStore | null>(null);
const WorkbenchPersistenceContext = createContext<SyncedWorkbenchPersistence | null>(null);
+const WorkbenchExtensionsContext = createContext<ExtensionRegistry | null>(null);
const subscribeToNothing = (): (() => void) => () => {};
const getNullSnapshot = (): null => null;
@@ -47,6 +49,7 @@ export const WorkbenchProvider = ({
}) => {
const [store] = useState(() => createWorkbenchStore());
const [persistence] = useState(createSyncedWorkbenchPersistence);
+ const [extensions] = useState(createExtensionRegistry);
const hasHydrated = useSyncExternalStore(store.subscribe, store.getSnapshot, store.getSnapshot).hasHydrated;
// The runtime is created inside the effect: disposal is terminal, so each
@@ -76,9 +79,11 @@ export const WorkbenchProvider = ({
return (
<WorkbenchPersistenceContext value={persistence}>
- <WorkbenchStoreContext value={store}>
- {hasHydrated ? children : <WorkbenchSplashScreen messageKey="splash.openingProject" />}
- </WorkbenchStoreContext>
+ <WorkbenchExtensionsContext value={extensions}>
+ <WorkbenchStoreContext value={store}>
+ {hasHydrated ? children : <WorkbenchSplashScreen messageKey="splash.openingProject" />}
+ </WorkbenchStoreContext>
+ </WorkbenchExtensionsContext>
</WorkbenchPersistenceContext>
);
};
@@ -204,6 +209,14 @@ export const useWorkbenchPersistenceService = (): SyncedWorkbenchPersistence =>
export const useOptionalWorkbenchPersistenceService = (): SyncedWorkbenchPersistence | null =>
use(WorkbenchPersistenceContext);
+export const useWorkbenchExtensions = (): ExtensionRegistry => {
+ const extensions = use(WorkbenchExtensionsContext);
+ if (!extensions) {
+ throw new Error('useWorkbenchExtensions must be used within a WorkbenchProvider.');
+ }
+ return extensions;
+};
+
export const useOptionalWorkbenchCommands = () => useOptionalWorkbenchStore()?.commands ?? null;
export const useOptionalWorkbenchQueries = () => useOptionalWorkbenchStore()?.queries ?? null;
diff --git a/invokeai/frontend/webv2/src/workbench/extensions/README.md b/invokeai/frontend/webv2/src/workbench/extensions/README.md
index 3864f04f13..a6c379a30c 100644
--- a/invokeai/frontend/webv2/src/workbench/extensions/README.md
+++ b/invokeai/frontend/webv2/src/workbench/extensions/README.md
@@ -49,7 +49,7 @@ This avoids symlink assumptions and lets multi-user/admin rules follow the exist
## Missing Core Systems
-The extension API stubs exist in `extensionApi.ts`, but these systems still need real shell features:
+The contribution registries exist in `extensionRegistry.ts` (one registry per Workbench mount, constructed by `WorkbenchProvider`), but these systems still need real shell features:
- Command registry UI and lifecycle ownership.
- Command palette UI.
diff --git a/invokeai/frontend/webv2/src/workbench/extensions/extensionApi.ts b/invokeai/frontend/webv2/src/workbench/extensions/extensionApi.ts
deleted file mode 100644
index 94565dbf2a..0000000000
--- a/invokeai/frontend/webv2/src/workbench/extensions/extensionApi.ts
+++ /dev/null
@@ -1,95 +0,0 @@
-import type {
- WidgetCommandApi,
- WidgetCommandContribution,
- WidgetCommandPaletteApi,
- WidgetCommandPaletteContribution,
- WidgetContributionSource,
- WidgetHotkeyApi,
- WidgetHotkeyContribution,
- WidgetMenuApi,
- WidgetMenuContribution,
- WidgetSearchApi,
- WidgetSearchProvider,
- WidgetToolbarApi,
- WidgetToolbarContribution,
-} from '@workbench/widgetContracts';
-
-import { createCollectionStore } from '@platform/state/externalStore';
-
-import { areWidgetContributionSourcesEqual, getWidgetContributionRegistrationKey } from './contributionSource';
-
-const noopDispose = (): void => {};
-
-const commandStore = createCollectionStore<WidgetCommandContribution>();
-const hotkeyStore = createCollectionStore<WidgetHotkeyContribution>();
-const menuStore = createCollectionStore<WidgetMenuContribution>();
-const paletteStore = createCollectionStore<WidgetCommandPaletteContribution>();
-const searchStore = createCollectionStore<WidgetSearchProvider>();
-const toolbarStore = createCollectionStore<WidgetToolbarContribution>();
-
-const findCommand = (
- commandId: string,
- source: WidgetContributionSource | null = null
-): WidgetCommandContribution | undefined =>
- source
- ? (commandStore.findLatest(
- (candidate) => candidate.id === commandId && areWidgetContributionSourcesEqual(candidate.source, source)
- ) ?? commandStore.findLatest((candidate) => candidate.id === commandId && !candidate.source))
- : commandStore.findLatest((candidate) => candidate.id === commandId && !candidate.source);
-
-export const commandApi: WidgetCommandApi = {
- execute(commandId, ...args) {
- const command = findCommand(commandId);
-
- return Promise.resolve(command ? command.handler(...args) : undefined);
- },
- executeForSource(commandId, source, ...args) {
- const command = findCommand(commandId, source);
-
- return Promise.resolve(command ? command.handler(...args) : undefined);
- },
- register(command) {
- return commandStore.register(command, getWidgetContributionRegistrationKey(command.id, command.source));
- },
-};
-
-export const hotkeyApi: WidgetHotkeyApi = {
- register(hotkey) {
- return hotkeyStore.register(hotkey, getWidgetContributionRegistrationKey(hotkey.id, hotkey.source));
- },
-};
-
-export const menuApi: WidgetMenuApi = {
- register(menu) {
- return menuStore.register(menu, getWidgetContributionRegistrationKey(menu.id, menu.source));
- },
-};
-
-export const commandPaletteApi: WidgetCommandPaletteApi = {
- register(entry) {
- return paletteStore.register(entry, entry.commandId);
- },
-};
-
-export const searchApi: WidgetSearchApi = {
- registerProvider(provider) {
- return searchStore.register(provider, provider.id);
- },
-};
-
-export const toolbarApi: WidgetToolbarApi = {
- register(toolbar) {
- return toolbarStore.register(toolbar, toolbar.id);
- },
-};
-
-export const extensionContributionStores = {
- commands: commandStore,
- hotkeys: hotkeyStore,
- menus: menuStore,
- palette: paletteStore,
- search: searchStore,
- toolbars: toolbarStore,
-};
-
-export const disposeExtensionContribution = noopDispose;
diff --git a/invokeai/frontend/webv2/src/workbench/extensions/extensionApi.test.ts b/invokeai/frontend/webv2/src/workbench/extensions/extensionRegistry.test.ts
similarity index 50%
rename from invokeai/frontend/webv2/src/workbench/extensions/extensionApi.test.ts
rename to invokeai/frontend/webv2/src/workbench/extensions/extensionRegistry.test.ts
index fa47c45b2f..b4381521c4 100644
--- a/invokeai/frontend/webv2/src/workbench/extensions/extensionApi.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/extensions/extensionRegistry.test.ts
@@ -1,72 +1,76 @@
import { describe, expect, it, vi } from 'vitest';
-import { commandApi } from './extensionApi';
+import { createExtensionRegistry } from './extensionRegistry';
-describe('commandApi', () => {
+describe('createExtensionRegistry', () => {
it('registers, executes, and disposes commands by id', async () => {
+ const { commands } = createExtensionRegistry();
const handler = vi.fn(() => 'ok');
- const dispose = commandApi.register({ handler, id: 'test.command', title: 'Test command' });
+ const dispose = commands.register({ handler, id: 'test.command', title: 'Test command' });
- await expect(commandApi.execute('test.command', 'arg')).resolves.toBe('ok');
+ await expect(commands.execute('test.command', 'arg')).resolves.toBe('ok');
expect(handler).toHaveBeenCalledWith('arg');
dispose();
- await expect(commandApi.execute('test.command')).resolves.toBeUndefined();
+ await expect(commands.execute('test.command')).resolves.toBeUndefined();
});
it('replaces duplicate global command registrations', async () => {
+ const { commands } = createExtensionRegistry();
const firstHandler = vi.fn(() => 'first');
const secondHandler = vi.fn(() => 'second');
- const disposeFirst = commandApi.register({ handler: firstHandler, id: 'test.duplicate-command', title: 'First' });
- const disposeSecond = commandApi.register({
+ const disposeFirst = commands.register({ handler: firstHandler, id: 'test.duplicate-command', title: 'First' });
+ const disposeSecond = commands.register({
handler: secondHandler,
id: 'test.duplicate-command',
title: 'Second',
});
- await expect(commandApi.execute('test.duplicate-command')).resolves.toBe('second');
+ await expect(commands.execute('test.duplicate-command')).resolves.toBe('second');
disposeFirst();
- await expect(commandApi.execute('test.duplicate-command')).resolves.toBe('second');
+ await expect(commands.execute('test.duplicate-command')).resolves.toBe('second');
disposeSecond();
- await expect(commandApi.execute('test.duplicate-command')).resolves.toBeUndefined();
+ await expect(commands.execute('test.duplicate-command')).resolves.toBeUndefined();
});
it('replaces duplicate command registrations from the same source', async () => {
+ const { commands } = createExtensionRegistry();
const firstHandler = vi.fn(() => 'first');
const secondHandler = vi.fn(() => 'second');
const source = { instanceId: 'alpha', projectId: 'project-1', region: 'right', typeId: 'test-widget' } as const;
- const disposeFirst = commandApi.register({
+ const disposeFirst = commands.register({
handler: firstHandler,
id: 'test.replace-command',
source,
title: 'First',
});
- const disposeSecond = commandApi.register({
+ const disposeSecond = commands.register({
handler: secondHandler,
id: 'test.replace-command',
source,
title: 'Second',
});
- await expect(commandApi.executeForSource('test.replace-command', source)).resolves.toBe('second');
+ await expect(commands.executeForSource('test.replace-command', source)).resolves.toBe('second');
disposeFirst();
- await expect(commandApi.executeForSource('test.replace-command', source)).resolves.toBe('second');
+ await expect(commands.executeForSource('test.replace-command', source)).resolves.toBe('second');
disposeSecond();
- await expect(commandApi.executeForSource('test.replace-command', source)).resolves.toBeUndefined();
+ await expect(commands.executeForSource('test.replace-command', source)).resolves.toBeUndefined();
});
it('does not execute a command registered by the same widget source in another project', async () => {
+ const { commands } = createExtensionRegistry();
const handler = vi.fn(() => 'project-1');
- const dispose = commandApi.register({
+ const dispose = commands.register({
handler,
id: 'test.project-command',
source: { instanceId: 'alpha', projectId: 'project-1', region: 'right', typeId: 'test-widget' },
@@ -74,7 +78,7 @@ describe('commandApi', () => {
});
await expect(
- commandApi.executeForSource('test.project-command', {
+ commands.executeForSource('test.project-command', {
instanceId: 'alpha',
projectId: 'project-2',
region: 'right',
@@ -87,8 +91,9 @@ describe('commandApi', () => {
});
it('does not fall back to another widget instance when executing for a source', async () => {
+ const { commands } = createExtensionRegistry();
const handler = vi.fn(() => 'beta');
- const dispose = commandApi.register({
+ const dispose = commands.register({
handler,
id: 'test.scoped-command',
source: { instanceId: 'beta', projectId: 'project-1', region: 'right', typeId: 'test-widget' },
@@ -96,7 +101,7 @@ describe('commandApi', () => {
});
await expect(
- commandApi.executeForSource('test.scoped-command', {
+ commands.executeForSource('test.scoped-command', {
instanceId: 'alpha',
projectId: 'project-1',
region: 'right',
@@ -107,4 +112,17 @@ describe('commandApi', () => {
dispose();
});
+
+ it('keeps contributions isolated between registries', async () => {
+ const first = createExtensionRegistry();
+ const second = createExtensionRegistry();
+ const handler = vi.fn(() => 'first-registry');
+
+ first.commands.register({ handler, id: 'test.isolated-command', title: 'Isolated' });
+ first.palette.register({ commandId: 'test.isolated-command', title: 'Isolated' });
+
+ await expect(second.commands.execute('test.isolated-command')).resolves.toBeUndefined();
+ expect(second.stores.palette.list()).toHaveLength(0);
+ expect(first.stores.palette.list()).toHaveLength(1);
+ });
});
diff --git a/invokeai/frontend/webv2/src/workbench/extensions/extensionRegistry.ts b/invokeai/frontend/webv2/src/workbench/extensions/extensionRegistry.ts
new file mode 100644
index 0000000000..24fda27f46
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/extensions/extensionRegistry.ts
@@ -0,0 +1,109 @@
+import type {
+ WidgetCommandApi,
+ WidgetCommandContribution,
+ WidgetCommandPaletteApi,
+ WidgetCommandPaletteContribution,
+ WidgetContributionSource,
+ WidgetHotkeyApi,
+ WidgetHotkeyContribution,
+ WidgetMenuApi,
+ WidgetMenuContribution,
+ WidgetSearchApi,
+ WidgetSearchProvider,
+ WidgetToolbarApi,
+ WidgetToolbarContribution,
+} from '@workbench/widgetContracts';
+
+import { createCollectionStore, type CollectionStore } from '@platform/state/externalStore';
+
+import { areWidgetContributionSourcesEqual, getWidgetContributionRegistrationKey } from './contributionSource';
+
+export interface ExtensionContributionStores {
+ commands: CollectionStore<WidgetCommandContribution>;
+ hotkeys: CollectionStore<WidgetHotkeyContribution>;
+ menus: CollectionStore<WidgetMenuContribution>;
+ palette: CollectionStore<WidgetCommandPaletteContribution>;
+ search: CollectionStore<WidgetSearchProvider>;
+ toolbars: CollectionStore<WidgetToolbarContribution>;
+}
+
+export interface ExtensionRegistry {
+ commands: WidgetCommandApi;
+ hotkeys: WidgetHotkeyApi;
+ menus: WidgetMenuApi;
+ palette: WidgetCommandPaletteApi;
+ search: WidgetSearchApi;
+ /** Read side for shell surfaces (hotkey runtime, palette/search/toolbar UI). */
+ stores: ExtensionContributionStores;
+ toolbars: WidgetToolbarApi;
+}
+
+/**
+ * One extension registry is constructed per Workbench mount (see
+ * WorkbenchProvider) so contribution state lives and dies with the mount —
+ * never in process-wide singletons.
+ */
+export const createExtensionRegistry = (): ExtensionRegistry => {
+ const stores: ExtensionContributionStores = {
+ commands: createCollectionStore<WidgetCommandContribution>(),
+ hotkeys: createCollectionStore<WidgetHotkeyContribution>(),
+ menus: createCollectionStore<WidgetMenuContribution>(),
+ palette: createCollectionStore<WidgetCommandPaletteContribution>(),
+ search: createCollectionStore<WidgetSearchProvider>(),
+ toolbars: createCollectionStore<WidgetToolbarContribution>(),
+ };
+
+ const findCommand = (
+ commandId: string,
+ source: WidgetContributionSource | null = null
+ ): WidgetCommandContribution | undefined =>
+ source
+ ? (stores.commands.findLatest(
+ (candidate) => candidate.id === commandId && areWidgetContributionSourcesEqual(candidate.source, source)
+ ) ?? stores.commands.findLatest((candidate) => candidate.id === commandId && !candidate.source))
+ : stores.commands.findLatest((candidate) => candidate.id === commandId && !candidate.source);
+
+ return {
+ commands: {
+ execute(commandId, ...args) {
+ const command = findCommand(commandId);
+
+ return Promise.resolve(command ? command.handler(...args) : undefined);
+ },
+ executeForSource(commandId, source, ...args) {
+ const command = findCommand(commandId, source);
+
+ return Promise.resolve(command ? command.handler(...args) : undefined);
+ },
+ register(command) {
+ return stores.commands.register(command, getWidgetContributionRegistrationKey(command.id, command.source));
+ },
+ },
+ hotkeys: {
+ register(hotkey) {
+ return stores.hotkeys.register(hotkey, getWidgetContributionRegistrationKey(hotkey.id, hotkey.source));
+ },
+ },
+ menus: {
+ register(menu) {
+ return stores.menus.register(menu, getWidgetContributionRegistrationKey(menu.id, menu.source));
+ },
+ },
+ palette: {
+ register(entry) {
+ return stores.palette.register(entry, getWidgetContributionRegistrationKey(entry.commandId, entry.source));
+ },
+ },
+ search: {
+ registerProvider(provider) {
+ return stores.search.register(provider, getWidgetContributionRegistrationKey(provider.id, provider.source));
+ },
+ },
+ stores,
+ toolbars: {
+ register(toolbar) {
+ return stores.toolbars.register(toolbar, getWidgetContributionRegistrationKey(toolbar.id, toolbar.source));
+ },
+ },
+ };
+};
diff --git a/invokeai/frontend/webv2/src/workbench/hotkeys/WorkbenchHotkeyRuntime.tsx b/invokeai/frontend/webv2/src/workbench/hotkeys/WorkbenchHotkeyRuntime.tsx
index 1f80c9262b..0128e07854 100644
--- a/invokeai/frontend/webv2/src/workbench/hotkeys/WorkbenchHotkeyRuntime.tsx
+++ b/invokeai/frontend/webv2/src/workbench/hotkeys/WorkbenchHotkeyRuntime.tsx
@@ -1,10 +1,9 @@
import type { WidgetContributionSource } from '@workbench/widgetContracts';
-import { commandApi } from '@workbench/extensions/extensionApi';
import { getFocusedRegionSnapshot } from '@workbench/focusRegions';
import { useWorkbenchPreferenceSelector } from '@workbench/settings/store';
import { areWidgetPlacementProjectsEqual, getWidgetPlacementProject } from '@workbench/widgetPlacementMeta';
-import { useActiveProjectSelector } from '@workbench/WorkbenchContext';
+import { useActiveProjectSelector, useWorkbenchExtensions } from '@workbench/WorkbenchContext';
import { useEffect, useEffectEvent, useMemo } from 'react';
import { tinykeys } from 'tinykeys';
@@ -39,6 +38,7 @@ export const shouldPreventHotkeyDefault = (hotkey: RegisteredHotkey | null): boo
export const WorkbenchHotkeyRuntime = () => {
useRegisterFirstPartyCommands();
+ const { commands: commandApi } = useWorkbenchExtensions();
const customHotkeys = useWorkbenchPreferenceSelector((preferences) => preferences.customHotkeys);
const project = useActiveProjectSelector(getWidgetPlacementProject, areWidgetPlacementProjectsEqual);
const extensionHotkeys = useExtensionHotkeyDefinitions();
diff --git a/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.ts b/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.ts
index e528cc0a06..6a23e57bd1 100644
--- a/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.ts
+++ b/invokeai/frontend/webv2/src/workbench/hotkeys/extensionHotkeys.ts
@@ -1,8 +1,8 @@
import type { WidgetRegion } from '@workbench/layoutContracts';
import type { WidgetHotkeyContribution, WorkbenchRegion } from '@workbench/widgetContracts';
-import { extensionContributionStores } from '@workbench/extensions/extensionApi';
-import { useMemo } from 'react';
+import { useWorkbenchExtensions } from '@workbench/WorkbenchContext';
+import { useMemo, useSyncExternalStore } from 'react';
import type { HotkeyDefinition } from './types';
@@ -40,7 +40,8 @@ export const toExtensionHotkeyDefinition = (hotkey: WidgetHotkeyContribution): H
};
export const useExtensionHotkeyDefinitions = (): HotkeyDefinition[] => {
- const hotkeys = extensionContributionStores.hotkeys.useList();
+ const store = useWorkbenchExtensions().stores.hotkeys;
+ const hotkeys = useSyncExternalStore(store.subscribe, store.list, store.list);
return useMemo(
() => hotkeys.map(toExtensionHotkeyDefinition).filter((hotkey): hotkey is HotkeyDefinition => hotkey !== null),
diff --git a/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.ts b/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.ts
index a5ca69da54..de6adf247d 100644
--- a/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.ts
+++ b/invokeai/frontend/webv2/src/workbench/hotkeys/firstPartyCommands.ts
@@ -11,13 +11,12 @@ import { queueCommands } from '@features/queue';
import { useInvocationTemplatesSelector } from '@features/workflow/react';
import { useMountEffect } from '@platform/react/useMountEffect';
import { getConnectionStatus } from '@platform/transport/connectionStore';
-import { commandApi } from '@workbench/extensions/extensionApi';
import { isInvocationRouteValid, resolveInvocationRoute } from '@workbench/invocation';
import { submitResolvedInvocation } from '@workbench/invocationSubmit';
import { openWidgetPlacement } from '@workbench/widgetPlacementCommands';
import { getWidgetsForRegion } from '@workbench/widgetRegistry';
import { getProjectWidgetValues } from '@workbench/widgetState';
-import { useWorkbenchCommands, useWorkbenchQueries } from '@workbench/WorkbenchContext';
+import { useWorkbenchCommands, useWorkbenchExtensions, useWorkbenchQueries } from '@workbench/WorkbenchContext';
import { useEffect, useEffectEvent } from 'react';
const imageRecallCommands: Record<string, ImageRecallKind> = {
@@ -61,6 +60,7 @@ const getAvailableModels = () => {
export const useRegisterFirstPartyCommands = () => {
const commands = useWorkbenchCommands();
+ const { commands: commandApi } = useWorkbenchExtensions();
const queries = useWorkbenchQueries();
const { layout, notifications, queue, widgets } = commands;
useInvocationTemplatesSelector((snapshot) => snapshot.status);
@@ -306,5 +306,5 @@ export const useRegisterFirstPartyCommands = () => {
return () => {
disposers.forEach((dispose) => dispose());
};
- }, [commands, layout, notifications, queries, queue, widgets]);
+ }, [commandApi, commands, layout, notifications, queries, queue, widgets]);
};
diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.test.ts b/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.test.ts
index 817972a950..028df9c212 100644
--- a/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.test.ts
@@ -10,6 +10,7 @@ import type { WorkbenchAction } from '@workbench/workbenchState.testing';
import type { WorkbenchWidgetCommands } from '@workbench/workbenchStore';
import { clearProjectDiagnostics, configureDiagnostics, getProjectDiagnostics } from '@workbench/diagnostics/logger';
+import { createExtensionRegistry } from '@workbench/extensions/extensionRegistry';
import { createWidgetImplementationResource } from '@workbench/widgetImplementationResource';
import { beforeEach, describe, expect, it, vi } from 'vitest';
@@ -126,6 +127,7 @@ describe('createWidgetRuntime', () => {
const { actions, widgets } = createDispatch();
const state = createStateApi();
const runtime = createWidgetRuntime({
+ extensions: createExtensionRegistry(),
widgets,
getWidgetById: () => undefined,
getWidgetsForRegion: createRegistry({}),
@@ -147,6 +149,7 @@ describe('createWidgetRuntime', () => {
it('binds diagnostics to the current widget source', () => {
const { widgets } = createDispatch();
const runtime = createWidgetRuntime({
+ extensions: createExtensionRegistry(),
widgets,
getWidgetById: () => undefined,
getWidgetsForRegion: createRegistry({}),
@@ -178,6 +181,7 @@ describe('createWidgetRuntime', () => {
const { actions, widgets } = createDispatch();
const createInitial = vi.fn(() => ({ seeded: true }));
const runtime = createWidgetRuntime({
+ extensions: createExtensionRegistry(),
widgets,
getWidgetById: () => undefined,
getWidgetsForRegion: createRegistry({
@@ -230,6 +234,7 @@ describe('createWidgetRuntime', () => {
it('dispatches own-region close and reveal actions', () => {
const { actions, widgets } = createDispatch();
const runtime = createWidgetRuntime({
+ extensions: createExtensionRegistry(),
widgets,
getWidgetById: (typeId) => createWidget({ id: typeId, label: typeId }),
getWidgetsForRegion: createRegistry({}),
@@ -257,6 +262,7 @@ describe('createWidgetRuntime', () => {
it('returns unsupported for cross-region close and reveal', () => {
const { actions, widgets } = createDispatch();
const runtime = createWidgetRuntime({
+ extensions: createExtensionRegistry(),
widgets,
getWidgetById: () => undefined,
getWidgetsForRegion: createRegistry({}),
@@ -273,7 +279,9 @@ describe('createWidgetRuntime', () => {
it('executes commands registered by the current widget source', async () => {
const { widgets } = createDispatch();
+ const extensions = createExtensionRegistry();
const alphaRuntime = createWidgetRuntime({
+ extensions,
widgets,
getWidgetById: () => undefined,
getWidgetsForRegion: createRegistry({}),
@@ -283,6 +291,7 @@ describe('createWidgetRuntime', () => {
state: createStateApi(),
});
const betaRuntime = createWidgetRuntime({
+ extensions,
widgets,
getWidgetById: () => undefined,
getWidgetsForRegion: createRegistry({}),
@@ -311,7 +320,9 @@ describe('createWidgetRuntime', () => {
it('does not execute commands registered by the same widget instance in another project', async () => {
const { widgets } = createDispatch();
+ const extensions = createExtensionRegistry();
const projectOneRuntime = createWidgetRuntime({
+ extensions,
widgets,
getWidgetById: () => undefined,
getWidgetsForRegion: createRegistry({}),
@@ -321,6 +332,7 @@ describe('createWidgetRuntime', () => {
state: createStateApi(),
});
const projectTwoRuntime = createWidgetRuntime({
+ extensions,
widgets,
getWidgetById: () => undefined,
getWidgetsForRegion: createRegistry({}),
@@ -346,4 +358,28 @@ describe('createWidgetRuntime', () => {
disposeOne();
disposeTwo();
});
+
+ it('scopes palette, search, and toolbar registrations to the widget source', () => {
+ const { widgets } = createDispatch();
+ const extensions = createExtensionRegistry();
+ const runtime = createWidgetRuntime({
+ extensions,
+ widgets,
+ getWidgetById: () => undefined,
+ getWidgetsForRegion: createRegistry({}),
+ instance: createInstance('alpha', 'test-widget'),
+ project: createPlacementProject(),
+ region: 'right',
+ state: createStateApi(),
+ });
+
+ runtime.palette.register({ commandId: 'test-widget.palette-command', title: 'Palette entry' });
+ runtime.search.registerProvider({ id: 'test-widget.search', label: 'Search', search: () => [] });
+ runtime.toolbars.register({ id: 'test-widget.toolbar', items: [], location: 'status.left' });
+
+ const expectedSource = expect.objectContaining({ instanceId: 'alpha', typeId: 'test-widget' });
+ expect(extensions.stores.palette.list()).toEqual([expect.objectContaining({ source: expectedSource })]);
+ expect(extensions.stores.search.list()).toEqual([expect.objectContaining({ source: expectedSource })]);
+ expect(extensions.stores.toolbars.list()).toEqual([expect.objectContaining({ source: expectedSource })]);
+ });
});
diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts b/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts
index 44985f6d10..b16262029f 100644
--- a/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts
+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/createWidgetRuntime.ts
@@ -1,3 +1,4 @@
+import type { ExtensionRegistry } from '@workbench/extensions/extensionRegistry';
import type { WidgetRegion } from '@workbench/layoutContracts';
import type { Project } from '@workbench/projectContracts';
import type {
@@ -15,16 +16,8 @@ import type { WidgetPlacementProject } from '@workbench/widgetPlacementCommands'
import type { WorkbenchWidgetCommands } from '@workbench/workbenchStore';
import { createProjectLogger } from '@workbench/diagnostics/logger';
-import {
- commandApi as sharedCommandApi,
- commandPaletteApi,
- hotkeyApi,
- menuApi,
- searchApi,
- toolbarApi,
-} from '@workbench/extensions/extensionApi';
import { closeWidgetPlacement, openWidgetPlacement, revealWidgetPlacement } from '@workbench/widgetPlacementCommands';
-import { useWorkbenchInternalStore } from '@workbench/WorkbenchContext';
+import { useWorkbenchExtensions, useWorkbenchInternalStore } from '@workbench/WorkbenchContext';
import { useMemo } from 'react';
const WIDGET_REGIONS = new Set<WidgetRegion>(['bottom', 'center', 'left', 'right']);
@@ -71,6 +64,7 @@ export const getProjectWidgetRuntimeState = <State extends Record<string, unknow
);
export const createWidgetRuntime = ({
+ extensions,
getWidgetsForRegion,
getWidgetById,
instance,
@@ -79,6 +73,7 @@ export const createWidgetRuntime = ({
state,
widgets,
}: {
+ extensions: ExtensionRegistry;
getWidgetById: (typeId: WidgetTypeId) => RegisteredWidget | undefined;
getWidgetsForRegion: (region: WidgetRegion) => RegisteredWidget[];
instance: WidgetInstanceRuntimeMeta;
@@ -95,20 +90,20 @@ export const createWidgetRuntime = ({
typeId: instance.typeId,
};
const commands = {
- execute: (commandId, ...args) => sharedCommandApi.executeForSource(commandId, source, ...args),
- executeForSource: sharedCommandApi.executeForSource,
- register: (command) => sharedCommandApi.register({ ...command, source }),
+ execute: (commandId, ...args) => extensions.commands.executeForSource(commandId, source, ...args),
+ executeForSource: extensions.commands.executeForSource,
+ register: (command) => extensions.commands.register({ ...command, source }),
} satisfies WidgetRuntimeApi['commands'];
const hotkeys: WidgetHotkeyApi = {
register: (hotkey) =>
- hotkeyApi.register({
+ extensions.hotkeys.register({
...hotkey,
scope: hotkey.scope ?? 'widget',
source,
}),
};
const menus = {
- register: (menu) => menuApi.register({ ...menu, source }),
+ register: (menu) => extensions.menus.register({ ...menu, source }),
} satisfies WidgetRuntimeApi['menus'];
const diagnostics = {
logger: (namespace) => createProjectLogger(namespace, { ...source, kind: 'widget' }),
@@ -120,11 +115,17 @@ export const createWidgetRuntime = ({
hotkeys,
instanceId: instance.id,
menus,
- palette: commandPaletteApi,
+ palette: {
+ register: (entry) => extensions.palette.register({ ...entry, source }),
+ },
region,
- search: searchApi,
+ search: {
+ registerProvider: (provider) => extensions.search.registerProvider({ ...provider, source }),
+ },
state,
- toolbars: toolbarApi,
+ toolbars: {
+ register: (toolbar) => extensions.toolbars.register({ ...toolbar, source }),
+ },
typeId: instance.typeId,
workbench: {
closeWidgetInstance: (instanceId) => {
@@ -161,6 +162,7 @@ export const useWidgetRuntime = ({
region: WorkbenchRegion;
}): WidgetRuntimeApi => {
const store = useWorkbenchInternalStore();
+ const extensions = useWorkbenchExtensions();
const { widgets } = store.commands;
const projectId = project.projectId ?? store.getSnapshot().activeProject.id;
const state = useMemo<WidgetRuntimeStateApi>(
@@ -173,7 +175,17 @@ export const useWidgetRuntime = ({
);
return useMemo(
- () => createWidgetRuntime({ getWidgetById, getWidgetsForRegion, instance, project, region, state, widgets }),
- [getWidgetById, getWidgetsForRegion, instance, project, region, state, widgets]
+ () =>
+ createWidgetRuntime({
+ extensions,
+ getWidgetById,
+ getWidgetsForRegion,
+ instance,
+ project,
+ region,
+ state,
+ widgets,
+ }),
+ [extensions, getWidgetById, getWidgetsForRegion, instance, project, region, state, widgets]
);
};
diff --git a/invokeai/frontend/webv2/src/workbench/widgetContracts.ts b/invokeai/frontend/webv2/src/workbench/widgetContracts.ts
index 26b52a0cde..349879ac21 100644
--- a/invokeai/frontend/webv2/src/workbench/widgetContracts.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgetContracts.ts
@@ -217,6 +217,7 @@ export interface WidgetCommandPaletteContribution {
commandId: string;
title: string;
keywords?: string[];
+ source?: WidgetContributionSource;
}
export interface WidgetSearchApi {
@@ -227,6 +228,7 @@ export interface WidgetSearchProvider {
id: string;
label: string;
search: (query: string) => Promise<WidgetSearchResult[]> | WidgetSearchResult[];
+ source?: WidgetContributionSource;
}
export interface WidgetSearchResult {
@@ -248,6 +250,7 @@ export interface WidgetToolbarContribution {
icon?: WidgetIconComponent;
label?: string;
}>;
+ source?: WidgetContributionSource;
}
export interface OpenWorkbenchWidgetOptions {