diff --git a/browser_tests/tests/agent/agentConsent.spec.ts b/browser_tests/tests/agent/agentConsent.spec.ts index 65fa8a39aa2..4130689a4eb 100644 --- a/browser_tests/tests/agent/agentConsent.spec.ts +++ b/browser_tests/tests/agent/agentConsent.spec.ts @@ -444,7 +444,10 @@ test.describe('Manual agent consent gate', { tag: ['@cloud', '@ui'] }, () => { }) test.describe('Automatic agent consent', { tag: ['@cloud', '@ui'] }, () => { - test.use({ agentConsentAccepted: false }) + test.use({ + agentConsentAccepted: false, + agentPanelInitiallyOpen: true + }) test('offers once on first load and remains available manually after Skip', async ({ comfyPage, @@ -541,19 +544,22 @@ test.describe( ).toBeNull() }) - await test.step('Taking the blank canvas presents the deferred offer', async () => { + await test.step('Taking the blank canvas leaves the activated panel available for a message', async () => { await page.getByTestId('getting-started-blank').click() await expect(gettingStarted).toHaveCount(0) - await expect(consent).toBeVisible() + await expect(consent).toHaveCount(0) await expect(agentPanel.root).toBeVisible() expect(agentConsentWrites).toHaveLength(0) + expect( + await page.evaluate((key) => localStorage.getItem(key), autoShownKey) + ).toBeNull() }) }) } ) test.describe( - 'Automatic agent consent behind a desktop sign-in approval', + 'Agent activation behind a desktop sign-in approval', { tag: ['@cloud', '@ui'] }, () => { test.use({ agentConsentAccepted: false }) @@ -569,7 +575,7 @@ test.describe( ) }) - test('waits for the approval before offering Agent', async ({ + test('waits for the approval before leaving Agent available', async ({ comfyPage, agentPanel, agentConsentReads @@ -589,7 +595,8 @@ test.describe( 'Comfy.AgentConsent.AutoShown.test-user-e2e.ws-personal' await test.step('Boot again on a desktop sign-in link with the offer unspent', async () => { - await expect(consent).toBeVisible() + await expect(consent).toHaveCount(0) + await expect(agentPanel.root).toBeVisible() await page.evaluate((key) => localStorage.removeItem(key), autoShownKey) const readsBeforeLink = agentConsentReads.length await comfyPage.goto({ @@ -614,11 +621,111 @@ test.describe( ).toBeNull() }) - await test.step('Approving clears the screen and the offer follows', async () => { + await test.step('Approving clears the screen without covering the activated panel', async () => { await approve.click() await expect(approval).toHaveCount(0) + await expect(consent).toHaveCount(0) + await expect(agentPanel.root).toBeVisible() + }) + }) + } +) + +test.describe( + 'Fresh signed-in Agent consent and tour sequencing', + { tag: ['@cloud', '@ui'] }, + () => { + test.use({ agentConsentAccepted: false }) + + for (const tourAction of ['finish', 'skip'] as const) { + test(`resumes one held send after the user ${tourAction === 'finish' ? 'finishes' : 'skips'} the tour`, async ({ + agentPanel, + comfyPage, + postedMessages + }) => { + const page = comfyPage.page + const consent = page.getByRole('dialog', { + name: enMessages.agent.consent.title + }) + + await expect(agentPanel.root).toBeVisible() + await expect(consent).toHaveCount(0) + await agentPanel.selectWorkflow() + await agentPanel.composer.fill('Build a product photo workflow') + await agentPanel.sendButton.click() + await expect(consent).toBeVisible() + expect(postedMessages).toHaveLength(0) + await consent + .getByRole('button', { name: enMessages.agent.consent.accept }) + .click() + + const coachTitles = [ + enMessages.agent.coachTitle, + enMessages.agent.coachWorkflowTitle, + enMessages.agent.coachGraphTitle, + enMessages.agent.coachHistoryTitle + ] + await expect( + page.getByRole('dialog', { name: coachTitles[0] }) + ).toBeVisible() + expect(postedMessages).toHaveLength(0) + + if (tourAction === 'skip') { + await page + .getByRole('dialog', { name: coachTitles[0] }) + .getByRole('button', { name: enMessages.agent.skip }) + .click() + } else { + for (const [index, title] of coachTitles.entries()) { + await page + .getByRole('dialog', { name: title }) + .getByRole('button', { + name: + index === coachTitles.length - 1 + ? enMessages.onboardingCoachmarks.done + : enMessages.g.next + }) + .click() + } + } + + await expect.poll(() => postedMessages).toHaveLength(1) + await agentPanel.openButton.click() + await expect(agentPanel.root).toHaveCount(0) + await agentPanel.openButton.click() + await expect(agentPanel.root).toBeVisible() + await expect(consent).toHaveCount(0) + await expect.poll(() => postedMessages).toHaveLength(1) + }) + } + + test('resumes the held send when App Mode defers the active tour', async ({ + agentPanel, + comfyPage, + postedMessages + }) => { + const page = comfyPage.page + const consent = page.getByRole('dialog', { + name: enMessages.agent.consent.title }) + const coach = page.getByRole('dialog', { + name: enMessages.agent.coachTitle + }) + + await agentPanel.selectWorkflow() + await agentPanel.composer.fill('Build a product photo workflow') + await agentPanel.sendButton.click() + await consent + .getByRole('button', { name: enMessages.agent.consent.accept }) + .click() + + await expect(coach).toBeVisible() + expect(postedMessages).toHaveLength(0) + await comfyPage.command.executeCommand('Comfy.ToggleLinear') + + await expect(coach).toHaveCount(0) + await expect.poll(() => postedMessages).toHaveLength(1) }) } ) diff --git a/src/extensions/core/agentPanel.test.ts b/src/extensions/core/agentPanel.test.ts index 2b4bc8075ff..547ba34160c 100644 --- a/src/extensions/core/agentPanel.test.ts +++ b/src/extensions/core/agentPanel.test.ts @@ -223,6 +223,7 @@ describe('AgentPanel extension flag gate', () => { ) mocks.registerTracker.mockClear() localStorage.clear() + sessionStorage.clear() canvasStore.updateSelectedItems.mockClear() mocks.getNodeByLocatorId.mockReset() nodeSelectionStore.beginWorkflowLoad.mockClear() @@ -270,6 +271,37 @@ describe('AgentPanel extension flag gate', () => { }) }) + it('preserves activation ownership across an app remount', async () => { + agentFlagEnabled.value = true + Object.assign(consentStore, { accepted: false, isChecking: false }) + const trackAgentConsentOfferExited = vi.fn() + const { useTelemetry: useCurrentTelemetry } = + await import('@/platform/telemetry') + vi.mocked(useCurrentTelemetry).mockReturnValue( + fromPartial>>({ + trackAgentConsentOfferExited, + trackAgentPanelOpened: vi.fn() + }) + ) + sessionStorage.setItem( + 'Comfy.AgentPanel.ActivationOpened.account-a.workspace-a', + 'true' + ) + + await loadEntryAndSetup() + await flush() + + expect(agentStore.isOpen).toBe(true) + expect(useAgentConsent().withConsent).not.toHaveBeenCalled() + await vi.waitFor(() => + expect(trackAgentConsentOfferExited).toHaveBeenCalledExactlyOnceWith({ + exit: 'activation_opened_panel', + stage: 'offer', + retry_armed: false + }) + ) + }) + it('keeps the panel closed if the feature is disabled before acceptance', async () => { agentFlagEnabled.value = true Object.assign(consentStore, { accepted: false, isChecking: false }) diff --git a/src/extensions/core/agentPanel.ts b/src/extensions/core/agentPanel.ts index e493e6fd9b6..e5aee45056b 100644 --- a/src/extensions/core/agentPanel.ts +++ b/src/extensions/core/agentPanel.ts @@ -38,6 +38,68 @@ import { export const GATE_SETTLE_TIMEOUT_MS = 5_000 const CONSENT_AUTO_SHOWN_PREFIX = 'Comfy.AgentConsent.AutoShown' +const ACTIVATION_OPENED_PANEL_PREFIX = 'Comfy.AgentPanel.ActivationOpened' + +function activationOpenedPanelKey( + userId: string | undefined, + workspaceId: string | null +): string | null { + return userId && workspaceId + ? `${ACTIVATION_OPENED_PANEL_PREFIX}.${userId}.${workspaceId}` + : null +} + +function persistActivationOpenedPanel( + userId: string | undefined, + workspaceId: string | null +): void { + const key = activationOpenedPanelKey(userId, workspaceId) + if (!key) return + try { + sessionStorage.setItem(key, 'true') + } catch { + // The in-memory marker still preserves ownership until the next remount. + } +} + +function wasActivationOpenedPanel( + userId: string | undefined, + workspaceId: string | null +): boolean { + const key = activationOpenedPanelKey(userId, workspaceId) + if (!key) return false + try { + return sessionStorage.getItem(key) === 'true' + } catch { + return false + } +} + +function automaticConsentOfferScope( + { + activationOpenedPanel, + panelOpen, + userId, + workspaceId, + workspaceSwitching + }: { + activationOpenedPanel: boolean + panelOpen: boolean + userId: string | undefined + workspaceId: string | null + workspaceSwitching: boolean + }, + onActivatedPanel: () => void +): [userId: string, workspaceId: string] | null { + if (!userId || !workspaceId || workspaceSwitching) { + return null + } + if (activationOpenedPanel && panelOpen) { + onActivatedPanel() + return null + } + return [userId, workspaceId] +} function writeAutoShown(key: string, shown: boolean): boolean { try { @@ -235,6 +297,7 @@ export function registerAgentPanelExtension(): void { return userId && workspaceId ? `${userId}.${workspaceId}` : null } const consentCardSeenIn = new Set() + let activationOpenedPanel = false whenever( () => dialogStore.isDialogOpen(CONSENT_DIALOG_KEY), () => { @@ -262,9 +325,29 @@ export function registerAgentPanelExtension(): void { return } - const userId = resolvedUserInfo.value?.id - const workspaceId = workspaceStore.activeWorkspaceId - if (!userId || !workspaceId || workspaceStore.isSwitching) return + const candidateUserId = resolvedUserInfo.value?.id + const candidateWorkspaceId = workspaceStore.activeWorkspaceId + const offerScope = automaticConsentOfferScope( + { + activationOpenedPanel: + activationOpenedPanel || + wasActivationOpenedPanel(candidateUserId, candidateWorkspaceId), + panelOpen: agentPanelStore.isOpen, + userId: candidateUserId, + workspaceId: candidateWorkspaceId, + workspaceSwitching: workspaceStore.isSwitching + }, + () => { + useTelemetry()?.trackAgentConsentOfferExited({ + exit: 'activation_opened_panel', + stage: 'offer', + retry_armed: false + }) + offerHeld.value = false + } + ) + if (!offerScope) return + const [userId, workspaceId] = offerScope const key = `${CONSENT_AUTO_SHOWN_PREFIX}.${userId}.${workspaceId}` const autoShow = prepareAutoShow(key) if (autoShow === 'storage_unavailable') withholdOffer(autoShow) @@ -320,7 +403,14 @@ export function registerAgentPanelExtension(): void { .then((decided) => { if (decided && agentPanelStore.enabled) { activationOffered = true - if (!agentPanelStore.isOpen) agentPanelStore.open('activation') + if (!agentPanelStore.isOpen) { + agentPanelStore.open('activation') + activationOpenedPanel = true + persistActivationOpenedPanel( + resolvedUserInfo.value?.id, + workspaceStore.activeWorkspaceId + ) + } } }) .catch((error: unknown) => { diff --git a/src/platform/telemetry/types.ts b/src/platform/telemetry/types.ts index d43ecb34abd..d694cbdc298 100644 --- a/src/platform/telemetry/types.ts +++ b/src/platform/telemetry/types.ts @@ -644,6 +644,8 @@ export type AgentConsentOfferExit = | 'offer_in_flight' /** The card has already been on screen for this scope this page load. */ | 'card_already_seen' + /** Panel activation owns consent timing, so the automatic offer is dropped. */ + | 'activation_opened_panel' /** The one-shot auto-show key for this scope is already burned. */ | 'already_offered' /** The first-run startup probe rejected. */ diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 32d7ca4920b..86c29f59e9a 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -28,6 +28,7 @@ vi.mock(import('firebase/auth')) vi.mock(import('vuefire'), () => ({ useFirebaseAuth: vi.fn() })) import { i18n } from '@/i18n' +import type { AgentConsentTrigger } from '@/platform/telemetry/types' import { useAgentConsentStore } from '@/workbench/extensions/agent/stores/agent/agentConsentStore' import { setupInlinePromptEditorDom } from './components/agent/composer/inlinePromptEditorTestSetup' @@ -74,6 +75,15 @@ const getServerFeature = vi.hoisted(() => ) const focusNodeInstance = vi.hoisted(() => vi.fn()) const socketSend = vi.hoisted(() => vi.fn()) +const withConsent = vi.hoisted(() => + vi.fn<(trigger: AgentConsentTrigger, onAccept: () => void) => Promise>( + async (_trigger, onAccept) => onAccept() + ) +) + +vi.mock(import('./composables/agent/useAgentConsent'), () => ({ + useAgentConsent: () => ({ withConsent }) +})) vi.mock(import('@/composables/canvas/useFocusNode'), () => ({ useFocusNode: () => ({ focusNodeInstance }) @@ -3140,6 +3150,72 @@ describe('AgentPanelRoot workflow binding', () => { } ) + it('keeps the first send pending until consent succeeds, then resumes it', async () => { + makeTab('wf-42') + const bodies = mockMessagesEndpoint('wf-42') + Object.assign(useAgentConsentStore(), { accepted: false }) + let accept = () => {} + withConsent.mockImplementationOnce( + (_trigger, onAccept) => + new Promise((resolve) => { + accept = () => { + onAccept() + resolve() + } + }) + ) + renderWithSelectedTarget() + + const textbox = screen.getByRole('textbox') + await userEvent.click(textbox) + await userEvent.paste('build me a workflow') + await userEvent.click(screen.getByRole('button', { name: 'Send' })) + + expect(withConsent).toHaveBeenCalledExactlyOnceWith( + 'first_message', + expect.any(Function) + ) + expect(bodies).toHaveLength(0) + + accept() + await screen.findByRole('button', { name: 'Stop' }) + await waitFor(() => expect(bodies).toHaveLength(1)) + }) + + it('does not resume a consent-held send after closing in App Mode', async () => { + makeTab('wf-42') + const bodies = mockMessagesEndpoint('wf-42') + const settleSubmission = vi.spyOn( + useAgentComposerStore(), + 'settleSubmission' + ) + Object.assign(useAgentConsentStore(), { accepted: false }) + let accept = () => {} + withConsent.mockImplementationOnce( + (_trigger, onAccept) => + new Promise((resolve) => { + accept = () => { + onAccept() + resolve() + } + }) + ) + const { unmount } = renderWithSelectedTarget() + + const textbox = screen.getByRole('textbox') + await userEvent.click(textbox) + await userEvent.paste('build me a workflow') + await userEvent.click(screen.getByRole('button', { name: 'Send' })) + + canvasStore.linearMode = true + useAgentPanelStore().close('close_button') + unmount() + accept() + await waitFor(() => + expect(settleSubmission).toHaveBeenCalledWith(expect.any(Number), false) + ) + expect(bodies).toHaveLength(0) + }) it('reports a message sent from an empty-state suggestion chip as a suggestion', async () => { makeTab('wf-42') mockMessagesEndpoint('wf-42') diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 67672949629..1aaa3064e5f 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -403,10 +403,16 @@ const coachDeferredBy = computed(() => ? 'tour_active' : null ) +const coachCompletionWaiters = new Set<() => void>() +function releaseCoachCompletionWaiters(): void { + for (const resolve of coachCompletionWaiters) resolve() + coachCompletionWaiters.clear() +} watch( [consentAccepted, onboardingKey, coachDeferredBy], ([accepted, key, reason]) => { if (accepted && key && !hasSeenCoach(key)) trackCoachDeferral(key, reason) + if (reason !== null) releaseCoachCompletionWaiters() }, { immediate: true } ) @@ -518,6 +524,16 @@ const graphMutations = (workflowId: string) => { return mutations } +const coachRef = ref>() + +async function waitForCoachCompletion(): Promise { + const key = onboardingKey.value + if (!key || hasSeenCoach(key)) return + await nextTick() + if (coachDeferredBy.value !== null || !coachRef.value) return + await new Promise((resolve) => coachCompletionWaiters.add(resolve)) +} + function toSelectedNode(node: LGraphNode): SelectedNode { return { id: String(node.id), @@ -1094,7 +1110,11 @@ async function onAgentActiveTab( void refreshCloudWorkflowIds() onBeforeUnmount(() => { ++activeTabGeneration - if (composerStore.submission?.id === consentHeldSubmissionId) + releaseCoachCompletionWaiters() + if ( + (coachDeferredBy.value === null || !agentPanelStore.isVisible) && + composerStore.submission?.id === consentHeldSubmissionId + ) composerStore.invalidateSubmission() mintPortWiring.detach() exitNodeSelectionMode() @@ -1231,6 +1251,7 @@ async function consentAllowsSubmission( await withConsent('first_message', () => { hasConsent = true }) + if (hasConsent) await waitForCoachCompletion() } finally { if (consentHeldSubmissionId === submissionId) consentHeldSubmissionId = undefined @@ -1747,8 +1768,10 @@ function onPanelDrop(event: DragEvent): void { diff --git a/src/workbench/extensions/agent/components/agent/OnboardingCoach.vue b/src/workbench/extensions/agent/components/agent/OnboardingCoach.vue index dc7ac8d97d0..13165ccd245 100644 --- a/src/workbench/extensions/agent/components/agent/OnboardingCoach.vue +++ b/src/workbench/extensions/agent/components/agent/OnboardingCoach.vue @@ -30,6 +30,7 @@ const { steps, storageKey } = defineProps<{ steps: CoachStep[] storageKey?: string }>() +const emit = defineEmits<{ finished: [] }>() const { active, index, step, isLast, next, finish } = useOnboarding( () => steps, @@ -89,13 +90,16 @@ function reportStep(action: AgentOnboardingAction): void { } function onNext(): void { + const finished = isLast.value reportStep(isLast.value ? 'finish' : 'next') next() + if (finished) emit('finished') } function onSkip(): void { reportStep('skip') finish() + emit('finished') } const targetObserver = new MutationObserver(resolveTargets)