Line data Source code
1 : <script setup lang="ts">
2 : import { computed, onMounted, ref } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import CanvasBanner from '@comfyorg/design-system/common/CanvasBanner'
6 : import Button from '@comfyorg/design-system/button/Button'
7 : import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph'
8 : import { app } from '@/scripts/app'
9 : import { createNodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
10 : import { getNodeByLocatorId } from '@/utils/graphTraversalUtil'
11 : import { createPositionBounds } from '@/utils/positionBounds'
12 :
13 : import { useAgentGraphActivityStore } from '@/workbench/extensions/agent/stores/agent/agentGraphActivityStore'
14 :
15 1 : const { t } = useI18n()
16 : const { canvas } = defineProps<{ canvas: LGraphCanvas | null }>()
17 1 : const activity = useAgentGraphActivityStore()
18 1 : const teleportTarget = ref<HTMLElement | null>(null)
19 1 : onMounted(() => {
20 1 : teleportTarget.value = document.querySelector('.graph-canvas-panel')
21 : })
22 :
23 1 : const visibleState = computed(() => {
24 1 : const state = activity.state
25 1 : if (
26 : state.phase === 'idle' ||
27 : !canvas?.graph ||
28 : String(canvas.graph.id) !== state.rootGraphId
29 : )
30 1 : return null
31 0 : return state
32 : })
33 1 : const isComplete = computed(() => visibleState.value?.phase === 'complete')
34 1 : const presentation = computed(() => ({
35 : testId: isComplete.value
36 : ? 'agent-graph-added-toast'
37 : : 'agent-graph-activity-bar',
38 : accent: isComplete.value ? 'border-l-success' : 'border-l-foreground',
39 : iconClass: isComplete.value
40 : ? 'icon-[lucide--check] text-success'
41 : : 'icon-[lucide--loader-circle] text-muted-foreground motion-safe:animate-spin',
42 : title: isComplete.value
43 : ? t('agent.nodesAdded', visibleState.value?.nodeIds.length ?? 0)
44 : : t('agent.updatingGraph')
45 : }))
46 1 : const liveNodes = computed(() => {
47 0 : const state = visibleState.value
48 0 : if (!state || !canvas) return []
49 0 : return state.nodeIds.flatMap((nodeId) => {
50 0 : const locator = createNodeLocatorId(null, nodeId)
51 0 : const node = getNodeByLocatorId(app.rootGraph, locator)
52 0 : return locator && node?.graph === canvas.graph ? [node] : []
53 : })
54 : })
55 :
56 0 : function viewNodes(): void {
57 0 : const panel = document.querySelector<HTMLElement>('.graph-canvas-panel')
58 0 : if (!canvas || !panel) return
59 0 : const bounds = createPositionBounds(liveNodes.value, 40)
60 0 : if (!bounds) return
61 0 : const canvasRect = canvas.canvas.getBoundingClientRect()
62 0 : const panelRect = panel.getBoundingClientRect()
63 0 : canvas.animateToBounds(bounds, {
64 : viewport: [
65 : panelRect.left - canvasRect.left,
66 : panelRect.top - canvasRect.top,
67 : panelRect.width,
68 : panelRect.height
69 : ]
70 : })
71 : }
72 1 : </script>
73 :
74 : <template>
75 2 : <Teleport v-if="teleportTarget" :to="teleportTarget">
76 : <div
77 : v-if="visibleState"
78 : class="pointer-events-none absolute inset-x-2 bottom-8 flex justify-center"
79 : >
80 : <CanvasBanner
81 : :data-testid="presentation.testId"
82 : role="status"
83 : :accent="presentation.accent"
84 : class="max-w-full flex-wrap gap-3"
85 : >
86 : <template #icon>
87 : <i :class="presentation.iconClass" aria-hidden="true" />
88 : </template>
89 : <template #title>
90 : {{ presentation.title }}
91 : </template>
92 : <template v-if="!isComplete" #description>
93 : {{ t('agent.editWhileWorking') }}
94 : </template>
95 : <template #actions>
96 : <div class="flex min-w-0 items-center gap-1">
97 : <Button
98 : v-if="liveNodes.length > 0"
99 : variant="secondary"
100 : size="sm"
101 : @click="viewNodes"
102 : >
103 : {{ t('agent.viewAddedNodes', liveNodes.length) }}
104 : </Button>
105 : <Button
106 : v-if="isComplete"
107 : variant="muted-textonly"
108 : size="icon"
109 : class="shrink-0"
110 : :aria-label="t('agent.close')"
111 : @click="activity.dismiss()"
112 : >
113 : <i class="icon-[lucide--x]" aria-hidden="true" />
114 : </Button>
115 : </div>
116 : </template>
117 : </CanvasBanner>
118 : </div>
119 : </Teleport>
120 : </template>
|