Line data Source code
1 : import { nodesMap } from '@comfyorg/comfy-multi-player'
2 : import type { Op } from '@comfyorg/comfy-multi-player'
3 : import type * as Y from 'yjs'
4 :
5 : import type { LGraph } from '@/lib/litegraph/src/litegraph'
6 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
7 :
8 : import { recordDevEvent } from './devPanelLog'
9 : import { DocChangeCollector } from './docChangeCollector'
10 : import type { DocUpdate } from './docFrameClient'
11 : import type { FollowerDoc } from './followerDoc'
12 : import type { GraphOperation } from './graphOperations'
13 : import type { ClassifiedDocUpdate } from './layoutFollowerBridge'
14 : import { isLiveAgentUpdate } from './layoutFollowerBridge'
15 : import { LiveGraphApplier, readDocWidgetValue } from './liveGraphApplier'
16 : import type {
17 : ApplyMode,
18 : FrameChanges,
19 : LiveGraphApplierDeps,
20 : NodeChange,
21 : RemoteApplyContext
22 : } from './liveGraphApplier'
23 : import { LocalWidgetWrites } from './localWidgetWrites'
24 : import { changesForRejectedOps } from './rejectedOpChanges'
25 :
26 : interface BoundTarget {
27 : follower: FollowerDoc
28 : collector: DocChangeCollector
29 : reportedPendingNodes: Map<string, NodeChange>
30 : failedProjections: FailedProjection[]
31 : }
32 :
33 : interface FailedProjection {
34 : changes: FrameChanges
35 : context: RemoteApplyContext
36 : mode: ApplyMode
37 : }
38 :
39 : /**
40 : * Document node entries a projection outcome reports as added and removed.
41 : * Not necessarily one frame's: `applyCollected` reports a whole collected
42 : * batch, and the no-graph `applyFrame` path omits entries already reported
43 : * on an earlier pending frame (`reportedPendingNodes`).
44 : */
45 : export interface DocNodeDelta {
46 : added: readonly string[]
47 : removed: readonly string[]
48 : }
49 :
50 : export type FrameOutcome =
51 : | { applied: false; nodes: DocNodeDelta }
52 : | {
53 : applied: true
54 : nodes: DocNodeDelta
55 : createdNodeIds: NodeId[]
56 : failureCount: number
57 : }
58 :
59 1 : const EMPTY_DELTA: DocNodeDelta = { added: [], removed: [] }
60 :
61 0 : function docNodeDelta(changes: FrameChanges): DocNodeDelta {
62 0 : const added: string[] = []
63 0 : const removed: string[] = []
64 0 : for (const [id, change] of changes.nodes) {
65 0 : if (change === 'add') added.push(id)
66 0 : else if (change === 'delete') removed.push(id)
67 : }
68 0 : return { added, removed }
69 : }
70 :
71 0 : function changesAgainstDoc(doc: Y.Doc, changes: FrameChanges): FrameChanges {
72 0 : const docNodes = nodesMap(doc)
73 0 : const nodes = new Map<string, NodeChange>()
74 0 : for (const [id, change] of changes.nodes) {
75 0 : if (!docNodes.has(id)) nodes.set(id, 'delete')
76 0 : else nodes.set(id, change === 'delete' ? 'update' : change)
77 : }
78 0 : return { ...changes, nodes }
79 : }
80 :
81 0 : function pendingNodeDelta(target: BoundTarget, changes: FrameChanges) {
82 0 : const frameNodes = new Map(
83 : [...changes.nodes].filter(
84 : ([id, change]) => target.reportedPendingNodes.get(id) !== change
85 : )
86 : )
87 0 : target.reportedPendingNodes = new Map(changes.nodes)
88 0 : return docNodeDelta({ ...changes, nodes: frameNodes })
89 : }
90 :
91 : /**
92 : * Projects a follower document onto the live graph through the graph API.
93 : * One collector per bound workflow records what each delivered frame changed;
94 : * the applier replays those changes as ordinary graph operations.
95 : */
96 1 : export class AgentCrdtProjection {
97 : private readonly targets = new Map<string, BoundTarget>()
98 : private readonly replacedLineages = new Set<string>()
99 : private readonly localWrites = new LocalWidgetWrites()
100 : private readonly applier: LiveGraphApplier
101 :
102 : constructor(
103 1 : private readonly getGraph: () => LGraph | null,
104 : deps: Omit<LiveGraphApplierDeps, 'getGraph' | 'holdsLocalWrite'> = {}
105 : ) {
106 1 : this.applier = new LiveGraphApplier({
107 : ...deps,
108 : getGraph,
109 : holdsLocalWrite: (nodeId, widget, docValue) => {
110 0 : const held = this.localWrites.holds(nodeId, widget, docValue)
111 0 : if (held) {
112 0 : recordDevEvent('local_widget_write_held', {
113 : node_id: nodeId,
114 : name: widget
115 : })
116 : }
117 0 : return held
118 : }
119 : })
120 : }
121 :
122 : /**
123 : * Local edits on their way to the host. A frame's value for one of these
124 : * registers is held back until the document holds the local value; see
125 : * `LocalWidgetWrites`.
126 : */
127 : noteLocalWrites(operations: readonly GraphOperation[]): void {
128 0 : this.localWrites.note(operations)
129 : }
130 :
131 : /** The host has answered for these ops, or they will never reach it. */
132 : settleLocalWrites(operations: readonly GraphOperation[]): void {
133 0 : this.localWrites.settle(operations)
134 : }
135 :
136 : /** Binding the same follower again keeps its collected, unapplied changes. */
137 : bind(workflowId: string, follower: FollowerDoc): void {
138 0 : if (this.targets.get(workflowId)?.follower === follower) return
139 0 : this.unbind(workflowId)
140 0 : this.targets.set(workflowId, {
141 : follower,
142 : collector: new DocChangeCollector(follower.doc),
143 : reportedPendingNodes: new Map(),
144 : failedProjections: []
145 : })
146 : }
147 :
148 : unbind(workflowId: string): void {
149 0 : const target = this.targets.get(workflowId)
150 0 : if (!target) return
151 0 : target.collector.destroy()
152 0 : this.targets.delete(workflowId)
153 : }
154 :
155 : /**
156 : * Applies one delivered frame's changes to the live graph. Without a graph
157 : * the changes stay collected for `applyCollected` once one appears.
158 : */
159 : applyFrame(
160 : update: DocUpdate & Partial<Pick<ClassifiedDocUpdate, 'catchUp'>>
161 : ): FrameOutcome {
162 0 : const target = this.targets.get(update.workflowId)
163 0 : if (!target) return { applied: false, nodes: EMPTY_DELTA }
164 0 : if (!this.getGraph()) {
165 : const changes = target.collector.peek()
166 0 : return { applied: false, nodes: pendingNodeDelta(target, changes) }
167 : }
168 0 : const changes = target.collector.take()
169 0 : target.reportedPendingNodes.clear()
170 0 : return this.apply(
171 : update.workflowId,
172 : target,
173 : changes,
174 : {
175 : actor: update.actor ?? 'agent-remote',
176 : opIds: update.opIds?.filter((id) => id.length > 0) ?? [],
177 : liveAgentTurn: isLiveAgentUpdate(update)
178 : },
179 : this.takeApplyMode(update.workflowId)
180 : )
181 : }
182 :
183 : /**
184 : * Applies the changes collected while no graph could take them: frames
185 : * delivered before the graph loaded, or while its tab was inactive.
186 : * @returns the apply result for the whole collected batch.
187 : */
188 : applyCollected(workflowId: string): FrameOutcome {
189 0 : const target = this.targets.get(workflowId)
190 0 : if (!target || !this.getGraph())
191 0 : return { applied: false, nodes: EMPTY_DELTA }
192 0 : target.reportedPendingNodes.clear()
193 0 : return this.apply(
194 : workflowId,
195 : target,
196 : target.collector.take(),
197 : { actor: 'agent-collected', opIds: [] },
198 : this.takeApplyMode(workflowId)
199 : )
200 : }
201 :
202 : /** Replays failed frames oldest first until one fails again. */
203 : retryFailed(workflowId: string): {
204 : applied: true
205 : createdNodeIds: NodeId[]
206 : failureCount: number
207 : } | null {
208 0 : const target = this.targets.get(workflowId)
209 0 : if (!target || target.failedProjections.length === 0 || !this.getGraph())
210 0 : return null
211 0 : const createdNodeIds: NodeId[] = []
212 0 : while (target.failedProjections.length > 0) {
213 0 : const failed = target.failedProjections[0]
214 0 : const outcome = this.project(
215 : workflowId,
216 : target,
217 : changesAgainstDoc(target.follower.doc, failed.changes),
218 : failed.context,
219 : failed.mode
220 : )
221 0 : createdNodeIds.push(...outcome.createdNodeIds)
222 0 : if (outcome.failureCount > 0)
223 0 : return {
224 : applied: true,
225 : createdNodeIds,
226 : failureCount: outcome.failureCount
227 : }
228 0 : target.failedProjections.shift()
229 : }
230 0 : return { applied: true, createdNodeIds, failureCount: 0 }
231 : }
232 :
233 : hasPendingFailure(workflowId: string): boolean {
234 0 : return (this.targets.get(workflowId)?.failedProjections.length ?? 0) > 0
235 : }
236 :
237 : /**
238 : * Puts the registers a rejected human batch claimed back the way the
239 : * document has them. Only those registers are touched: the rejection says
240 : * nothing about the rest of the live graph.
241 : * @returns the apply result for the rejected operations.
242 : */
243 : revertRejected(workflowId: string, ops: readonly Op[]): FrameOutcome {
244 0 : const target = this.targets.get(workflowId)
245 0 : if (!target || ops.length === 0 || !this.getGraph())
246 0 : return { applied: false, nodes: EMPTY_DELTA }
247 0 : return this.apply(
248 : workflowId,
249 : target,
250 : changesForRejectedOps(target.follower.doc, ops),
251 : {
252 : actor: 'agent:revert',
253 : opIds: ops.map((op) => op.op_id)
254 : }
255 : )
256 : }
257 :
258 : /**
259 : * Explicit lineage reset (`doc_reset`): the old lineage's undelivered
260 : * changes are dropped and the new lineage's first frame, which carries its
261 : * whole state, replaces the graph. Nothing is cleared before that frame
262 : * arrives, so a first mint of the canvas the user already sees changes
263 : * nothing visible.
264 : */
265 : replaceOnNextFrame(workflowId: string): void {
266 0 : const target = this.targets.get(workflowId)
267 0 : target?.collector.discard()
268 0 : target?.reportedPendingNodes.clear()
269 0 : if (target) target.failedProjections.length = 0
270 0 : this.replacedLineages.add(workflowId)
271 0 : this.localWrites.clear()
272 : }
273 :
274 : private takeApplyMode(workflowId: string): ApplyMode {
275 0 : return this.replacedLineages.delete(workflowId) ? 'replace' : 'merge'
276 : }
277 :
278 : /**
279 : * Drops a frame the graph already holds, reporting what it changed in the
280 : * document. Such a frame is where the document catches up on local writes,
281 : * so their holds are settled against it.
282 : */
283 : discardPending(workflowId: string): DocNodeDelta {
284 0 : const target = this.targets.get(workflowId)
285 0 : if (!target) return EMPTY_DELTA
286 0 : const changes = target.collector.take()
287 0 : target.reportedPendingNodes.clear()
288 0 : this.localWrites.settleAgainst((nodeId, widget) =>
289 : readDocWidgetValue(target.follower.doc, nodeId, widget)
290 : )
291 0 : return docNodeDelta(changes)
292 : }
293 :
294 : destroy(): void {
295 0 : for (const workflowId of Array.from(this.targets.keys()))
296 0 : this.unbind(workflowId)
297 0 : this.replacedLineages.clear()
298 0 : this.localWrites.clear()
299 : }
300 :
301 : private apply(
302 : workflowId: string,
303 : target: BoundTarget,
304 : changes: FrameChanges,
305 : context: RemoteApplyContext,
306 : mode: ApplyMode = 'merge'
307 : ): Extract<FrameOutcome, { applied: true }> {
308 0 : const outcome = this.project(workflowId, target, changes, context, mode)
309 0 : if (outcome.failureCount > 0)
310 0 : target.failedProjections.push({ changes, context, mode })
311 0 : return outcome
312 : }
313 :
314 : private project(
315 : workflowId: string,
316 : target: BoundTarget,
317 : changes: FrameChanges,
318 : context: RemoteApplyContext,
319 : mode: ApplyMode
320 : ): Extract<FrameOutcome, { applied: true }> {
321 0 : const { createdNodeIds, failureCount } = this.applier.applyChanges(
322 : target.follower.doc,
323 : changes,
324 : context,
325 : mode
326 : )
327 0 : if (createdNodeIds.length > 0) {
328 0 : recordDevEvent('agent_node_adapters_materialized', {
329 : workflowId,
330 : nodeIds: createdNodeIds
331 : })
332 : }
333 0 : return {
334 : applied: true,
335 : nodes: docNodeDelta(changes),
336 : createdNodeIds,
337 : failureCount
338 : }
339 : }
340 : }
|