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(widget-frame): report graph preview and copy failures as toasts

Josh Corbett1fa2f74dc274
diff --git a/invokeai/frontend/webv2/public/locales/en.json b/invokeai/frontend/webv2/public/locales/en.json
index 7c4bc37cdf..b0f4571d33 100644
--- a/invokeai/frontend/webv2/public/locales/en.json
+++ b/invokeai/frontend/webv2/public/locales/en.json
@@ -1858,7 +1858,9 @@
"centerViewsAdd": "Add To Center",
"failure": {
"compactLabel": "{{label}} failed — select to retry",
+ "copiedError": "Error details copied",
"copyError": "Copy Error",
+ "copyErrorFailed": "Could not copy the error details",
"retry": "Retry",
"title": "{{label}} failed"
},
@@ -2461,6 +2463,7 @@
},
"graph": {
"setSource": "Set Source",
+ "previewFailed": "Could not open the graph preview",
"viewGraph": "View Graph"
},
"project": {
diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetActionsMenu.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetActionsMenu.tsx
index 2c5dd2afba..af44d95aea 100644
--- a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetActionsMenu.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetActionsMenu.tsx
@@ -11,11 +11,12 @@ import { Icon, Menu, Portal, Text } from '@chakra-ui/react';
import { flushWorkbenchDrafts } from '@platform/react/draftRegistry';
import { IconButton } from '@platform/ui/Button';
import { MenuContent } from '@platform/ui/Menu';
+import { toaster } from '@platform/ui/toaster';
import { createGraphBearingSurface } from '@workbench/graphSurfaces';
import { resolveWidgetLabel } from '@workbench/widgetLabels';
import { useActiveProjectSelector, useWorkbenchCommands } from '@workbench/WorkbenchContext';
import { GitBranchIcon, MoreHorizontalIcon, TargetIcon } from 'lucide-react';
-import { lazy, Suspense, useCallback, useMemo, useState } from 'react';
+import { Component, lazy, Suspense, useCallback, useMemo, useState, type ReactNode } from 'react';
import { useTranslation } from 'react-i18next';
/**
@@ -31,6 +32,32 @@ import { useTranslation } from 'react-i18next';
const GraphPreviewHost = lazy(() => import('./GraphPreviewHost'));
+/**
+ * The preview is a dialog over the widget, not part of it: a throw inside it
+ * (a failed chunk load, a compile edge case that escaped the source) surfaces
+ * as a toast and drops the dialog, instead of climbing to the widget's
+ * failure boundary and replacing the whole widget with a failure card. An
+ * error unmounts the host, so the next open starts a fresh boundary.
+ */
+class GraphPreviewBoundary extends Component<
+ { children: ReactNode; onError: (error: Error) => void },
+ { hasFailed: boolean }
+> {
+ state = { hasFailed: false };
+
+ static getDerivedStateFromError(): { hasFailed: boolean } {
+ return { hasFailed: true };
+ }
+
+ componentDidCatch(error: Error) {
+ this.props.onError(error);
+ }
+
+ render() {
+ return this.state.hasFailed ? null : this.props.children;
+ }
+}
+
const MENU_POSITIONING = { placement: 'bottom-end' } as const;
const DISABLED_PROPS = { opacity: 0.4 };
@@ -111,6 +138,14 @@ export const WidgetActionsMenu = ({
setIsPreviewMounted(false);
}
}, [isPreviewOpen]);
+ const handlePreviewError = useCallback(
+ (error: Error) => {
+ toaster.create({ description: error.message, title: t('widgets.graph.previewFailed'), type: 'error' });
+ setIsPreviewOpen(false);
+ setIsPreviewMounted(false);
+ },
+ [t]
+ );
if (!surface && !HeaderMenu) {
return null;
@@ -137,14 +172,16 @@ export const WidgetActionsMenu = ({
</Portal>
</Menu.Root>
{surface && isPreviewMounted ? (
- <Suspense fallback={null}>
- <GraphPreviewHost
- isOpen={isPreviewOpen}
- surface={surface}
- onExitComplete={handlePreviewExitComplete}
- onOpenChange={setIsPreviewOpen}
- />
- </Suspense>
+ <GraphPreviewBoundary onError={handlePreviewError}>
+ <Suspense fallback={null}>
+ <GraphPreviewHost
+ isOpen={isPreviewOpen}
+ surface={surface}
+ onExitComplete={handlePreviewExitComplete}
+ onOpenChange={setIsPreviewOpen}
+ />
+ </Suspense>
+ </GraphPreviewBoundary>
) : null}
</>
);
diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetFailureBoundary.tsx b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetFailureBoundary.tsx
index 8912821e46..1c64d8218b 100644
--- a/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetFailureBoundary.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/WidgetFailureBoundary.tsx
@@ -7,11 +7,12 @@ import type {
import { Box, Code, Flex, HStack, ScrollArea, Stack, Text, useRecipe } from '@chakra-ui/react';
import { Button } from '@platform/ui/Button';
+import { toaster } from '@platform/ui/toaster';
import { useScrollAreaPhantomHeal } from '@platform/ui/useScrollAreaPhantomHeal';
import { chipRecipe } from '@theme/recipes';
import { resolveWidgetInstanceLabel } from '@workbench/widgetLabels';
import { TriangleAlertIcon } from 'lucide-react';
-import { Component, type ErrorInfo, type ReactNode, useRef } from 'react';
+import { Component, type ErrorInfo, type ReactNode, useCallback, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { WidgetPanelFrame, WidgetTooltipFrame } from './WidgetFrames';
@@ -40,7 +41,7 @@ interface WidgetFailureBoundaryState {
interface WidgetFailureFallbackProps extends Omit<WidgetFailureBoundaryProps, 'children' | 'resetKey' | 'onRetry'> {
details: string;
- onCopy: () => void;
+ onCopy: () => Promise<void>;
onRetry: () => void;
}
@@ -53,13 +54,23 @@ const WidgetFailureCard = ({
}: {
details: string;
label: string;
- onCopy: () => void;
+ onCopy: () => Promise<void>;
onRetry: () => void;
}) => {
const { t } = useTranslation();
const viewportRef = useRef<HTMLDivElement | null>(null);
useScrollAreaPhantomHeal(viewportRef);
+ // The card has no other feedback channel: without a toast the click is
+ // indistinguishable from one that silently failed.
+ const copy = useCallback(
+ () =>
+ onCopy().then(
+ () => toaster.create({ duration: 2500, title: t('widgets.failure.copiedError'), type: 'success' }),
+ () => toaster.create({ title: t('widgets.failure.copyErrorFailed'), type: 'error' })
+ ),
+ [onCopy, t]
+ );
return (
<Stack bg="bg.muted" borderColor="border.error" borderWidth="1px" gap="2" p="3" rounded="md">
@@ -86,7 +97,7 @@ const WidgetFailureCard = ({
<Button alignSelf="start" size="2xs" variant="outline" onClick={onRetry}>
{t('widgets.failure.retry')}
</Button>
- <Button alignSelf="start" size="2xs" variant="outline" onClick={onCopy}>
+ <Button alignSelf="start" size="2xs" variant="outline" onClick={copy}>
{t('widgets.failure.copyError')}
</Button>
</Stack>
@@ -210,12 +221,12 @@ export class WidgetFailureBoundary extends Component<WidgetFailureBoundaryProps,
this.setState({ details: undefined, error: undefined, resetKey: this.props.resetKey });
};
- private handleCopyError = () => {
+ private handleCopyError = (): Promise<void> => {
const { details, error } = this.state;
- if (error) {
- void navigator.clipboard?.writeText(details ?? error.message);
- }
+ return error && navigator.clipboard
+ ? navigator.clipboard.writeText(details ?? error.message)
+ : Promise.reject(new Error('clipboard unavailable'));
};
static getDerivedStateFromProps(
diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/graphPreviewSource.test.ts b/invokeai/frontend/webv2/src/workbench/widget-frame/graphPreviewSource.test.ts
index 152ac4186e..ab38ac7426 100644
--- a/invokeai/frontend/webv2/src/workbench/widget-frame/graphPreviewSource.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/graphPreviewSource.test.ts
@@ -1,4 +1,5 @@
import type { GenerateWidgetValues } from '@features/generation/contracts';
+import type * as GenerationPreview from '@features/generation/preview';
import type { ModelConfig } from '@features/models';
import type { InvocationTemplatesSnapshot } from '@features/workflow/react';
import type { GraphContract } from '@workbench/graphContracts';
@@ -11,10 +12,20 @@ import { canvasWidgetManifest } from '@workbench/widgets/canvas/manifest';
import { generateWidgetManifest } from '@workbench/widgets/generate/manifest';
import { workflowWidgetManifest } from '@workbench/widgets/workflow/manifest';
import { createInitialWorkbenchState, workbenchReducer } from '@workbench/workbenchState.testing';
-import { describe, expect, it } from 'vitest';
+import { describe, expect, it, vi } from 'vitest';
import { buildGraphPreviewSource } from './graphPreviewSource';
+const compileGeneratePreviewGraph = vi.hoisted(() => vi.fn());
+
+vi.mock('@features/generation/preview', async (importOriginal) => {
+ const original = await importOriginal<typeof GenerationPreview>();
+
+ compileGeneratePreviewGraph.mockImplementation(original.compileGeneratePreviewGraph);
+
+ return { ...original, compileGeneratePreviewGraph };
+});
+
const t = ((key: string) => key) as TFunction;
const sdxlModel = {
@@ -88,6 +99,18 @@ describe('buildGraphPreviewSource', () => {
expect(source.isLive).toBe(true);
});
+ it('reports a compile that throws as an invalid reason instead of propagating', () => {
+ compileGeneratePreviewGraph.mockImplementationOnce(() => {
+ throw new Error('unsupported sampler combination');
+ });
+
+ const source = buildGraphPreviewSource({ models, project, surface: generateSurface, t, templates: idleTemplates });
+
+ expect(source.graph).toBeNull();
+ expect(source.invalidReasons).toEqual(['unsupported sampler combination']);
+ expect(source.isLive).toBe(true);
+ });
+
it('compiles the workflow source from the live document with position hints', () => {
const source = buildGraphPreviewSource({
models: undefined,
diff --git a/invokeai/frontend/webv2/src/workbench/widget-frame/graphPreviewSource.ts b/invokeai/frontend/webv2/src/workbench/widget-frame/graphPreviewSource.ts
index e518cb1a56..306f8e1178 100644
--- a/invokeai/frontend/webv2/src/workbench/widget-frame/graphPreviewSource.ts
+++ b/invokeai/frontend/webv2/src/workbench/widget-frame/graphPreviewSource.ts
@@ -95,12 +95,26 @@ const buildGenerateSource = (
models: readonly ModelConfig[] | undefined,
t: TFunction
): GraphPreviewSourceWithoutDestination => {
- const result = compileGeneratePreviewGraph({
- destination: project.invocation.destination,
- models: models ?? [],
- storedValues: getProjectWidgetValues(project, 'generate'),
- useCpuNoise: project.settings.useCpuNoise,
- });
+ let result: ReturnType<typeof compileGeneratePreviewGraph>;
+
+ // A compile that throws (a model policy the stored values violate, a graph
+ // builder edge case) is a reason the preview shows, not a render error that
+ // takes the widget's chrome down with it.
+ try {
+ result = compileGeneratePreviewGraph({
+ destination: project.invocation.destination,
+ models: models ?? [],
+ storedValues: getProjectWidgetValues(project, 'generate'),
+ useCpuNoise: project.settings.useCpuNoise,
+ });
+ } catch (error) {
+ return {
+ ...EMPTY_SOURCE_BASE,
+ graph: null,
+ invalidReasons: [error instanceof Error ? error.message : String(error)],
+ isLive: true,
+ };
+ }
if (result.status === 'invalid') {
return { ...EMPTY_SOURCE_BASE, graph: null, invalidReasons: result.reasons, isLive: true };