Line data Source code
1 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
2 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
3 : import { isCloud } from '@/platform/distribution/types'
4 : import { isHostTelemetryEnabled } from '@/platform/telemetry/hostTelemetryEnabled'
5 : import { reportError } from '@comfyorg/telemetry/reportError'
6 : import { isSubgraph } from '@/utils/typeGuardUtil'
7 :
8 : type RowOperation = 'add_row' | 'remove_row' | 'remote_apply' | 'remote_remount'
9 :
10 2356 : export const MAX_DYNAMIC_GROUP_ROW_RESOURCES = 4096
11 : export const MAX_DYNAMIC_GROUP_ROWS = 256
12 :
13 2356 : const rowFieldCounts = new WeakMap<IBaseWidget, number>()
14 2356 : const reported = new Set<string>()
15 2356 : let remoteRowCountNode: LGraphNode | undefined
16 :
17 40 : export function markDynamicGroupController(
18 : widget: IBaseWidget,
19 : rowFields: number
20 : ): void {
21 40 : rowFieldCounts.set(widget, rowFields)
22 : }
23 :
24 : /** True for the hidden widget whose value is a DynamicGroup's row count. */
25 0 : export function isDynamicGroupController(widget: IBaseWidget): boolean {
26 0 : return rowFieldCounts.has(widget)
27 : }
28 :
29 : /** Rows `widget`'s group may hold before its row widgets exceed the budget. */
30 310 : export function dynamicGroupRowAllocationBound(widget: IBaseWidget): number {
31 310 : const fields = rowFieldCounts.get(widget)
32 310 : if (fields === undefined) return 0
33 310 : return Math.min(
34 : MAX_DYNAMIC_GROUP_ROWS,
35 : Math.floor(MAX_DYNAMIC_GROUP_ROW_RESOURCES / (fields * 3 + 1))
36 : )
37 : }
38 :
39 : /** True while a document row count is being written to `node`. */
40 0 : export function isRemoteDynamicGroupRowWrite(node: LGraphNode): boolean {
41 0 : return remoteRowCountNode === node
42 : }
43 :
44 : /** Runs a document row-count write with node-scoped provenance. */
45 0 : export function applyRemoteDynamicGroupRowCount<T>(
46 : node: LGraphNode,
47 : write: () => T
48 : ): T {
49 0 : const previous = remoteRowCountNode
50 0 : remoteRowCountNode = node
51 0 : try {
52 0 : return write()
53 : } finally {
54 0 : remoteRowCountNode = previous
55 : }
56 : }
57 :
58 : /** Whether a controller holds the row count its setter derives from `value`. */
59 0 : export function reachesDynamicGroupRowCount(
60 : widget: IBaseWidget,
61 : value: unknown
62 : ): boolean {
63 0 : if (typeof value !== 'number' || !Number.isFinite(value)) return false
64 0 : return widget.value === Math.max(widget.options.min ?? 0, Math.trunc(value))
65 : }
66 :
67 0 : function reportRowCount(
68 : node: LGraphNode,
69 : operation: RowOperation,
70 : counts: { requested: number | null; actual: number },
71 : report: {
72 : message: string
73 : errorType: string
74 : surface: 'graph' | 'agent'
75 : level?: 'warning'
76 : tags?: Record<string, string>
77 : }
78 : ): void {
79 0 : const key = `${report.errorType}:${node.type}:${operation}`
80 0 : if (!(isCloud || isHostTelemetryEnabled()) || reported.has(key)) return
81 0 : reported.add(key)
82 0 : reportError(new Error(report.message), {
83 : errorType: report.errorType,
84 : surface: report.surface,
85 : level: report.level,
86 : tags: {
87 : ...report.tags,
88 : node_type: node.type,
89 : operation,
90 : in_subgraph: isSubgraph(node.graph)
91 : },
92 : context: counts
93 : })
94 : }
95 :
96 : /** Reports a local DynamicGroup row change that left a different row count. */
97 0 : export function reportDynamicGroupRowRefusal(
98 : node: LGraphNode,
99 : operation: 'add_row' | 'remove_row',
100 : requested: number,
101 : actual: number
102 : ): void {
103 0 : if (isRemoteDynamicGroupRowWrite(node)) return
104 0 : reportRowCount(
105 : node,
106 : operation,
107 : { requested, actual },
108 : {
109 : message: 'Refused a DynamicGroup row change',
110 : errorType: 'failure_resizing_dynamic_group_rows',
111 : surface: 'graph',
112 : level: 'warning'
113 : }
114 : )
115 : }
116 :
117 : /** Reports a remote DynamicGroup row count the live group did not reach. */
118 0 : export function reportRemoteDynamicGroupRowMismatch(
119 : node: LGraphNode,
120 : requested: unknown,
121 : actual: number,
122 : tags: Record<string, string>,
123 : operation: 'remote_apply' | 'remote_remount' = 'remote_apply'
124 : ): void {
125 0 : reportRowCount(
126 : node,
127 : operation,
128 : { requested: typeof requested === 'number' ? requested : null, actual },
129 : {
130 : message: 'Remote DynamicGroup row count did not apply',
131 : errorType: 'agent_graph_dynamic_group_rows_mismatch',
132 : surface: 'agent',
133 : tags
134 : }
135 : )
136 : }
|