Line data Source code
1 : import { fromZodError } from 'zod-validation-error'
2 :
3 : import { t } from '@/i18n'
4 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
5 : import type {
6 : IBaseWidget,
7 : IWidgetAssetOptions
8 : } from '@comfyorg/graph-model/widgets'
9 : import { useAssetBrowserDialog } from '@/platform/assets/composables/useAssetBrowserDialog'
10 : import {
11 : assetFilenameSchema,
12 : assetItemSchema
13 : } from '@/platform/assets/schemas/assetSchema'
14 : import { useToast } from '@/components/ui/toast/toastStore'
15 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
16 : import { getAssetFilename } from '@/platform/assets/utils/assetMetadataUtils'
17 :
18 : type WidgetWithNode = IBaseWidget & { node: LGraphNode }
19 :
20 : interface CreateAssetWidgetParams {
21 : /** The node to add the widget to */
22 : node: LGraphNode
23 : /** The widget name */
24 : widgetName: string
25 : /** The node type to show in asset browser (may differ from node.comfyClass for PrimitiveNode) */
26 : nodeTypeForBrowser: string
27 : /** Input name for asset browser filtering (defaults to widgetName if not provided) */
28 : inputNameForBrowser?: string
29 : /** Default value for the widget */
30 : defaultValue?: string
31 : }
32 :
33 : interface CreateAssetWidgetOptionsParams {
34 : widgetName: string
35 : nodeTypeForBrowser: string
36 : inputNameForBrowser?: string
37 : }
38 :
39 0 : function hasOwnerNode(widget: IBaseWidget): widget is WidgetWithNode {
40 0 : return (
41 : 'node' in widget && typeof widget.node === 'object' && widget.node !== null
42 : )
43 : }
44 :
45 0 : function createAssetWidgetOptions({
46 : widgetName,
47 : nodeTypeForBrowser,
48 : inputNameForBrowser
49 : }: CreateAssetWidgetOptionsParams): IWidgetAssetOptions {
50 0 : const inputName = inputNameForBrowser ?? widgetName
51 0 : const assetBrowserDialog = useAssetBrowserDialog()
52 :
53 0 : async function openModal(widget: IBaseWidget) {
54 0 : const toast = useToast()
55 :
56 0 : await assetBrowserDialog.show({
57 : nodeType: nodeTypeForBrowser,
58 : inputName,
59 : currentValue: String(widget.value ?? ''),
60 : onAssetSelected: (asset) => {
61 0 : const validatedAsset = assetItemSchema.safeParse(asset)
62 :
63 0 : if (!validatedAsset.success) {
64 0 : console.error(
65 : 'Invalid asset item:',
66 : fromZodError(validatedAsset.error).message
67 : )
68 0 : toast.error(t('assetBrowser.invalidAsset'), {
69 : description: t('assetBrowser.invalidAssetDetail')
70 : })
71 0 : return
72 : }
73 :
74 0 : const filename = getAssetFilename(validatedAsset.data)
75 0 : const validatedFilename = assetFilenameSchema.safeParse(filename)
76 :
77 0 : if (!validatedFilename.success) {
78 0 : console.error(
79 : 'Invalid asset filename:',
80 : validatedFilename.error.issues,
81 : 'for asset:',
82 : validatedAsset.data.id
83 : )
84 0 : toast.error(t('assetBrowser.invalidFilename'), {
85 : description: t('assetBrowser.invalidFilenameDetail')
86 : })
87 0 : return
88 : }
89 :
90 0 : const oldValue = widget.value
91 0 : widget.value = validatedFilename.data
92 0 : widget.callback?.(widget.value)
93 0 : if (hasOwnerNode(widget)) {
94 0 : widget.node.onWidgetChanged?.(
95 : widget.name,
96 : validatedFilename.data,
97 : oldValue,
98 : widget
99 : )
100 : }
101 0 : if (oldValue !== validatedFilename.data) {
102 0 : useWorkflowStore().activeWorkflow?.changeTracker.captureCanvasState()
103 : }
104 : }
105 : })
106 : }
107 :
108 0 : return {
109 : openModal,
110 : nodeType: nodeTypeForBrowser
111 : }
112 : }
113 :
114 0 : export function createAssetWidget(
115 : params: CreateAssetWidgetParams
116 : ): IBaseWidget {
117 0 : const {
118 : node,
119 : widgetName,
120 : nodeTypeForBrowser,
121 : inputNameForBrowser,
122 : defaultValue
123 : } = params
124 :
125 0 : const displayLabel = defaultValue ?? t('widgets.selectModel')
126 0 : const options = createAssetWidgetOptions({
127 : widgetName,
128 : nodeTypeForBrowser,
129 : inputNameForBrowser
130 : })
131 0 : return node.addWidget('asset', widgetName, displayLabel, () => {}, options)
132 : }
|