Line data Source code
1 : import { useAsyncState } from '@vueuse/core'
2 : import type { ComputedRef } from 'vue'
3 : import { computed, toValue, watchEffect } from 'vue'
4 :
5 : import { getOutputAssetMetadata } from '@/platform/assets/schemas/assetMetadataSchema'
6 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
7 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
8 : import { flattenNodeOutput } from '@/renderer/extensions/linearMode/flattenNodeOutput'
9 : import { useLinearOutputStore } from '@/renderer/extensions/linearMode/linearOutputStore'
10 : import { api } from '@/scripts/api'
11 : import { getJobDetail } from '@/services/jobOutputCache'
12 : import { WrappedList } from '@/utils/pagedList'
13 : import type { PagedList } from '@/utils/pagedList'
14 : import { useJobOutputHistory } from '@/platform/assets/composables/useJobOutputHistory'
15 : import { useAppModeStore } from '@/stores/appModeStore'
16 : import { useCommandStore } from '@/platform/keybindings/commandStore'
17 : import { useExecutionStore } from '@/stores/executionStore'
18 : import { useQueueStore } from '@/stores/queueStore'
19 : import type { AugmentedResultItem } from '@/utils/resultItem'
20 :
21 170 : export function useOutputHistory(): {
22 : outputs: PagedList<AssetItem>
23 : allOutputs: (item?: AssetItem) => AugmentedResultItem[]
24 : selectFirstHistory: () => void
25 : mayBeActiveWorkflowPending: ComputedRef<boolean>
26 : isWorkflowActive: ComputedRef<boolean>
27 : cancelActiveWorkflowJobs: () => Promise<void>
28 : } {
29 170 : const history = useJobOutputHistory()
30 170 : const linearStore = useLinearOutputStore()
31 170 : const workflowStore = useWorkflowStore()
32 170 : const executionStore = useExecutionStore()
33 170 : const appModeStore = useAppModeStore()
34 170 : const queueStore = useQueueStore()
35 :
36 3 : function matchesActiveWorkflow(task: { jobId: string | number }): boolean {
37 3 : const path = workflowStore.activeWorkflow?.path
38 3 : if (!path) return false
39 3 : return (
40 : executionStore.jobIdToSessionWorkflowPath.get(String(task.jobId)) === path
41 : )
42 : }
43 :
44 114 : function hasActiveWorkflowJobs(): boolean {
45 114 : if (!workflowStore.activeWorkflow?.path) return false
46 114 : return queueStore.runningTasks.some(matchesActiveWorkflow)
47 : }
48 :
49 170 : const mayBeActiveWorkflowPending = computed(() => {
50 46 : if (linearStore.activeWorkflowInProgressItems.length > 0) return false
51 8 : return hasActiveWorkflowJobs()
52 : })
53 :
54 170 : const isWorkflowActive = computed(
55 : () =>
56 : linearStore.activeWorkflowInProgressItems.length > 0 ||
57 : hasActiveWorkflowJobs()
58 : )
59 :
60 47 : function filterByOutputNodes(
61 : items: AugmentedResultItem[]
62 : ): AugmentedResultItem[] {
63 47 : const nodeIds = appModeStore.selectedOutputs
64 47 : if (!nodeIds.length) return []
65 47 : return items.filter((r) =>
66 : nodeIds.some((id) => String(id) === String(r.nodeId))
67 : )
68 : }
69 :
70 170 : const outputs = new WrappedList(history, (items) => {
71 124 : const path = workflowStore.activeWorkflow?.path
72 124 : if (!path) return []
73 :
74 124 : const pathMap = executionStore.jobIdToSessionWorkflowPath
75 :
76 124 : return items.filter((asset) => {
77 32 : const m = getOutputAssetMetadata(asset.user_metadata)
78 32 : return m ? pathMap.get(m.jobId) === path : false
79 : })
80 : })
81 :
82 170 : const resolvedCache = linearStore.resolvedOutputsCache
83 170 : const asyncRefs = new Map<
84 : string,
85 : ReturnType<typeof useAsyncState<AugmentedResultItem[]>>['state']
86 : >()
87 :
88 47 : function allOutputs(item?: AssetItem): AugmentedResultItem[] {
89 47 : if (!item?.id) return []
90 :
91 47 : const cached = resolvedCache.get(item.id)
92 47 : if (cached) return filterByOutputNodes(cached)
93 :
94 3 : const user_metadata = getOutputAssetMetadata(item.user_metadata)
95 47 : if (!user_metadata) return []
96 :
97 13 : if (linearStore.pendingResolve.has(user_metadata.jobId)) {
98 2 : const ordered = linearStore.inProgressItems
99 : .filter((i) => i.jobId === user_metadata.jobId && i.output)
100 : .map((i) => i.output!)
101 2 : if (ordered.length > 0) {
102 2 : resolvedCache.set(item.id, ordered)
103 2 : return filterByOutputNodes(ordered)
104 : }
105 : }
106 :
107 47 : if (
108 : user_metadata.allOutputs?.length &&
109 : (!user_metadata.outputCount ||
110 : user_metadata.outputCount <= user_metadata.allOutputs.length)
111 : ) {
112 1 : const reversed = [...user_metadata.allOutputs].reverse()
113 1 : resolvedCache.set(item.id, reversed)
114 1 : return filterByOutputNodes(reversed)
115 : }
116 :
117 0 : const existing = asyncRefs.get(item.id)
118 0 : if (existing) return filterByOutputNodes(existing.value)
119 :
120 0 : const itemId = item.id
121 0 : const outputRef = useAsyncState(
122 : getJobDetail(user_metadata.jobId).then((jobDetail) => {
123 0 : if (!jobDetail?.outputs) return []
124 0 : const results = Object.entries(jobDetail.outputs)
125 : .flatMap(flattenNodeOutput)
126 : .reverse()
127 0 : resolvedCache.set(itemId, results)
128 0 : return results
129 : }),
130 : []
131 : ).state
132 0 : asyncRefs.set(item.id, outputRef)
133 0 : return filterByOutputNodes(outputRef.value)
134 : }
135 :
136 75 : function selectFirstHistory() {
137 75 : const first = toValue(outputs.items).at(0)
138 75 : if (first) {
139 3 : linearStore.selectAsLatest(`history:${first.id}:0`)
140 : } else {
141 72 : linearStore.selectAsLatest(null)
142 : }
143 : }
144 :
145 170 : watchEffect(() => {
146 180 : if (linearStore.pendingResolve.size === 0) return
147 6 : for (const jobId of linearStore.pendingResolve) {
148 6 : const asset = toValue(outputs.items).find((a) => {
149 : const m = getOutputAssetMetadata(a.user_metadata)
150 2 : return m?.jobId === jobId
151 : })
152 6 : if (!asset) continue
153 : const loaded = allOutputs(asset).length > 0
154 2 : if (loaded) {
155 2 : linearStore.resolveIfReady(jobId, true)
156 2 : if (!linearStore.selectedId) selectFirstHistory()
157 : }
158 : }
159 : })
160 :
161 1 : async function cancelActiveWorkflowJobs() {
162 1 : if (!workflowStore.activeWorkflow?.path) return
163 :
164 1 : if (queueStore.runningTasks.some(matchesActiveWorkflow)) {
165 1 : void useCommandStore().execute('Comfy.Interrupt')
166 : } else {
167 0 : for (const task of queueStore.pendingTasks) {
168 0 : if (matchesActiveWorkflow(task)) {
169 0 : await api.deleteItem('queue', task.jobId)
170 0 : break
171 : }
172 : }
173 : }
174 : }
175 :
176 170 : return {
177 : outputs,
178 : allOutputs,
179 : selectFirstHistory,
180 : mayBeActiveWorkflowPending,
181 : isWorkflowActive,
182 : cancelActiveWorkflowJobs
183 : }
184 : }
|