diff --git a/browser_tests/tests/agent/agentWorkspaceSwitch.spec.ts b/browser_tests/tests/agent/agentWorkspaceSwitch.spec.ts new file mode 100644 index 00000000000..79ef92ff92a --- /dev/null +++ b/browser_tests/tests/agent/agentWorkspaceSwitch.spec.ts @@ -0,0 +1,50 @@ +import { expect } from '@playwright/test' +import { + PERSONAL_WORKSPACE_NAME, + TEAM_WORKSPACE_NAME +} from '@e2e/fixtures/data/workspaceSwitcher' +import { agentWorkspaceSwitcherTest as test } from '@e2e/tests/agent/agentWorkspaceSwitchFixture' + +test.describe('Agent workspace switching', { tag: '@ui' }, () => { + test('retires the old socket and recovers the panel under the new workspace identity', async ({ + agentIdentityReads, + agentPanel, + getAgentSocket, + nextAgentSocket, + page + }) => { + await agentPanel.open() + await expect(agentPanel.composer).toBeVisible() + const originalSocket = await getAgentSocket() + expect(new URL(originalSocket.url()).searchParams.get('token')).toBe( + 'mock-workspace-token-ws-personal' + ) + let originalSocketClosed = false + originalSocket.onClose(() => { + originalSocketClosed = true + }) + const replacementSocketOpened = nextAgentSocket() + + await page.getByRole('button', { name: 'Current user' }).click() + await expect(page.getByTestId('workspace-switcher-trigger')).toContainText( + PERSONAL_WORKSPACE_NAME + ) + await page.getByTestId('workspace-switcher-trigger').click() + await page + .getByTestId('workspace-switcher-panel') + .getByText(TEAM_WORKSPACE_NAME, { exact: true }) + .click() + + const replacementSocket = await replacementSocketOpened + await expect.poll(() => originalSocketClosed).toBe(true) + expect(new URL(replacementSocket.url()).searchParams.get('token')).toBe( + 'mock-workspace-token-ws-team' + ) + await expect + .poll(() => agentIdentityReads) + .toContainEqual({ + workspaceId: 'ws-team' + }) + await expect(agentPanel.composer).toBeVisible() + }) +}) diff --git a/browser_tests/tests/agent/agentWorkspaceSwitchFixture.ts b/browser_tests/tests/agent/agentWorkspaceSwitchFixture.ts new file mode 100644 index 00000000000..20671e0398e --- /dev/null +++ b/browser_tests/tests/agent/agentWorkspaceSwitchFixture.ts @@ -0,0 +1,145 @@ +import type { + AgentIdentity, + AgentRunMode, + ExchangeTokenRequest, + ListMembersResponse +} from '@comfyorg/ingest-types' +import { mergeTests } from '@playwright/test' + +import type { WorkspaceTokenResponse } from '@/platform/workspace/stores/workspaceAuthStore' + +import { agentTest, bootAgentApp } from '@e2e/fixtures/agentPanelFixture' +import { AgentPanel } from '@e2e/fixtures/components/AgentPanel' +import { emptyAgentThreadList } from '@e2e/fixtures/data/agent/agentThreadList' +import { createBillingCapabilities } from '@e2e/fixtures/data/billingCapabilities' +import { + EMPTY_BILLING_BALANCE, + EMPTY_BILLING_PLANS, + ENDED_STANDARD_BILLING_STATUS +} from '@e2e/fixtures/data/cloudWorkspace' +import { + WORKSPACE_SWITCHER_REMOTE_CONFIG, + WORKSPACE_SWITCHER_WORKSPACES +} from '@e2e/fixtures/data/workspaceSwitcher' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' +import { mockWorkspaceList } from '@e2e/fixtures/utils/workspaceMocks' +import { webSocketFixture } from '@e2e/fixtures/ws' + +interface AgentIdentityRead { + workspaceId: string +} + +const base = mergeTests(agentTest, webSocketFixture) + +export const agentWorkspaceSwitcherTest = base.extend<{ + agentIdentityReads: AgentIdentityRead[] + agentPanel: AgentPanel +}>({ + agentIdentityReads: async ({ page, agentFlagEnabled }, use) => { + const reads: AgentIdentityRead[] = [] + let workspaceId = 'ws-personal' + const runMode: AgentRunMode = { + mode: 'ask_approval', + credit_limit: null + } + + await page.addInitScript( + (workspaceIds) => { + for (const workspaceId of workspaceIds) { + localStorage.setItem( + `Comfy.AgentPanel.onboarded.test-user-e2e.${workspaceId}`, + 'true' + ) + } + }, + ['ws-personal', 'ws-team'] + ) + + await bootAgentApp(page, agentFlagEnabled, { + features: WORKSPACE_SWITCHER_REMOTE_CONFIG, + settings: { 'Comfy.UseNewMenu': 'Top' }, + beforeNavigate: async (page) => { + await mockWorkspaceList(page, WORKSPACE_SWITCHER_WORKSPACES) + await page.route('**/api/workspace/members**', (route) => + route.fulfill( + jsonRoute({ + members: [], + pagination: { offset: 0, limit: 50, total: 0, has_more: false } + } satisfies ListMembersResponse) + ) + ) + await page.route('**/api/auth/token', async (route) => { + const request = route.request().postDataJSON() as ExchangeTokenRequest + workspaceId = request.workspace_id ?? 'ws-personal' + const workspace = WORKSPACE_SWITCHER_WORKSPACES.find( + ({ id }) => id === workspaceId + ) + if (!workspace) { + await route.fulfill({ status: 404 }) + return + } + const response: WorkspaceTokenResponse = { + token: `mock-workspace-token-${workspace.id}`, + expires_at: new Date(Date.now() + 60 * 60 * 1000).toISOString(), + user_id: 'uid-1', + workspace: { + id: workspace.id, + name: workspace.name, + type: workspace.type + }, + role: workspace.role, + permissions: [] + } + await route.fulfill(jsonRoute(response)) + }) + await page.route('**/api/auth/session', (route) => + route.fulfill({ status: 204 }) + ) + await page.route('**/api/billing/status', (route) => + route.fulfill(jsonRoute(ENDED_STANDARD_BILLING_STATUS)) + ) + await page.route('**/api/billing/balance', (route) => + route.fulfill(jsonRoute(EMPTY_BILLING_BALANCE)) + ) + await page.route('**/api/billing/plans', (route) => + route.fulfill(jsonRoute(EMPTY_BILLING_PLANS)) + ) + await page.route('**/api/billing/capabilities', (route) => + route.fulfill(jsonRoute(createBillingCapabilities(workspaceId))) + ) + await page.route('**/api/experiment/models', (route) => + route.fulfill(jsonRoute([])) + ) + await page.route('**/api/agent/threads?*', (route) => + route.fulfill(jsonRoute(emptyAgentThreadList())) + ) + await page.route('**/api/agent/run-mode', (route) => + route.fulfill(jsonRoute(runMode)) + ) + await page.route('**/api/agent/identity', (route) => { + const token = route + .request() + .headers() + ['authorization']?.replace(/^Bearer /, '') + const identityWorkspace = WORKSPACE_SWITCHER_WORKSPACES.find( + ({ id }) => token === `mock-workspace-token-${id}` + ) + if (!identityWorkspace) { + return route.fulfill({ status: 401 }) + } + reads.push({ workspaceId: identityWorkspace.id }) + const identity: AgentIdentity = { + workspace_id: identityWorkspace.id, + user_id: 'uid-1' + } + return route.fulfill(jsonRoute(identity)) + }) + } + }) + + await use(reads) + }, + agentPanel: async ({ page }, use) => { + await use(new AgentPanel(page)) + } +}) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index f9d7e10f73c..3f913259726 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -1,5 +1,10 @@ import { fetchRequests, respondToFetch } from '@comfyorg/test-utils/fetch' import { fromPartial } from '@total-typescript/shoehorn' +import { + startDesktopHostSession, + stopDesktopHostSession +} from '@/platform/auth/desktopHost/desktopHostSession' +import type { DesktopHostAuthState } from '@/platform/auth/desktopHost/desktopHostAuthBridge' import { setImmediate } from 'node:timers/promises' import type { @@ -149,6 +154,7 @@ vi.mock(import('@/scripts/api'), () => ({ fetch(route.startsWith('/api') ? route : `/api${route}`, options), getServerFeature, socket: { readyState: 1, send: socketSend }, + resetSocket: vi.fn(async () => {}), dispatchCustomEvent: vi.fn(), addEventListener: ws.add, removeEventListener: ws.remove, @@ -15979,6 +15985,31 @@ function signInAs({ firebase, apiKeyUser }: AgentAccount): void { apiKeyUser === null ? null : { id: apiKeyUser } } +async function startDesktopHostSessionOn(initialWorkspaceId: string) { + const hostOn = (workspaceId: string) => + fromPartial({ + status: 'signed_in', + userId: 'account-a', + workspaceId + }) + let notify: (state: DesktopHostAuthState) => void = () => {} + await startDesktopHostSession({ + getState: async () => hostOn(initialWorkspaceId), + getWorkspaceToken: async () => 'host-token', + requestSignIn: async () => hostOn(initialWorkspaceId), + signOut: async () => ({ status: 'signed_out' }), + switchWorkspace: async (workspaceId) => hostOn(workspaceId), + onChanged: (listener) => { + notify = listener + return () => {} + } + }) + return { + hostOn, + notifyHostChanged: (state: DesktopHostAuthState) => notify(state) + } +} + describe('AgentPanelRoot agent socket', () => { it.for([ { @@ -16025,6 +16056,141 @@ describe('AgentPanelRoot agent socket', () => { } ) + it('reconnects the socket and looks the identity up again on a workspace switch', async () => { + const reset = vi.fn() + vi.mocked(resolveAgentIdentity).mockReturnValue({ + state: ref({ + status: 'resolved', + userId: 'account-a' + }), + reset, + stop: () => {} + }) + signInAs({ firebase: 'account-a', apiKeyUser: null }) + const workspaces = useTeamWorkspaceStore() + Object.assign(workspaces, { activeWorkspaceId: 'ws-a' }) + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + agentSocket.reconnect.mockClear() + + Object.assign(workspaces, { activeWorkspaceId: 'ws-b' }) + await nextTick() + + expect(agentSocket.reconnect).toHaveBeenCalledOnce() + expect(reset).toHaveBeenCalledOnce() + }) + + it('handshakes once for a workspace switch that passes through null', async () => { + const reset = vi.fn() + vi.mocked(resolveAgentIdentity).mockReturnValue({ + state: ref({ + status: 'resolved', + userId: 'account-a' + }), + reset, + stop: () => {} + }) + signInAs({ firebase: 'account-a', apiKeyUser: null }) + const workspaces = useTeamWorkspaceStore() + Object.assign(workspaces, { activeWorkspaceId: 'ws-a' }) + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + agentSocket.reconnect.mockClear() + + Object.assign(workspaces, { activeWorkspaceId: null }) + await nextTick() + expect(agentSocket.reconnect).not.toHaveBeenCalled() + expect(reset).not.toHaveBeenCalled() + + Object.assign(workspaces, { activeWorkspaceId: 'ws-b' }) + await nextTick() + + expect(agentSocket.reconnect).toHaveBeenCalledOnce() + expect(reset).toHaveBeenCalledOnce() + }) + + it('drops the socket when the workspace goes null while Desktop pins another', async () => { + const reset = vi.fn() + vi.mocked(resolveAgentIdentity).mockReturnValue({ + state: ref({ + status: 'resolved', + userId: 'account-a' + }), + reset, + stop: () => {} + }) + await startDesktopHostSessionOn('ws-host') + try { + signInAs({ firebase: 'account-a', apiKeyUser: null }) + const workspaces = useTeamWorkspaceStore() + Object.assign(workspaces, { activeWorkspaceId: 'ws-a' }) + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + agentSocket.reconnect.mockClear() + + Object.assign(workspaces, { activeWorkspaceId: null }) + await nextTick() + + expect(agentSocket.reconnect).toHaveBeenCalledOnce() + expect(reset).toHaveBeenCalledOnce() + } finally { + stopDesktopHostSession() + } + }) + + it('reconnects when only the Desktop host workspace moves', async () => { + const reset = vi.fn() + vi.mocked(resolveAgentIdentity).mockReturnValue({ + state: ref({ + status: 'resolved', + userId: 'account-a' + }), + reset, + stop: () => {} + }) + const { hostOn, notifyHostChanged } = + await startDesktopHostSessionOn('ws-a') + try { + signInAs({ firebase: 'account-a', apiKeyUser: null }) + const workspaces = useTeamWorkspaceStore() + Object.assign(workspaces, { activeWorkspaceId: null }) + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + agentSocket.reconnect.mockClear() + + notifyHostChanged(hostOn('ws-b')) + await nextTick() + + expect(agentSocket.reconnect).toHaveBeenCalledOnce() + expect(reset).toHaveBeenCalledOnce() + } finally { + stopDesktopHostSession() + } + }) + + it('still drops the socket when a sign-out clears the workspace too', async () => { + signInAs({ firebase: 'account-a', apiKeyUser: null }) + const workspaces = useTeamWorkspaceStore() + Object.assign(workspaces, { activeWorkspaceId: 'ws-a' }) + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + agentSocket.reconnect.mockClear() + + signInAs({ firebase: null, apiKeyUser: null }) + Object.assign(workspaces, { activeWorkspaceId: null }) + await nextTick() + + expect(agentSocket.reconnect).toHaveBeenCalledOnce() + }) + + it('leaves the socket alone when neither the account nor the workspace moves', async () => { + signInAs({ firebase: 'account-a', apiKeyUser: null }) + const workspaces = useTeamWorkspaceStore() + Object.assign(workspaces, { activeWorkspaceId: 'ws-a' }) + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + agentSocket.reconnect.mockClear() + + Object.assign(workspaces, { activeWorkspaceId: 'ws-a' }) + await nextTick() + + expect(agentSocket.reconnect).not.toHaveBeenCalled() + }) + it('opens the events socket under the api base path', async () => { vi.mocked(api.apiURL).mockImplementation((route) => `/comfy/api${route}`) render(AgentPanelRoot, { global: { plugins: [i18n] } }) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index b0bac994b68..329ae1dca17 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -47,6 +47,7 @@ import { isHdrImageFilename } from '@/utils/hdrFormatUtil' import type { DroppedAsset } from '@/utils/eventUtils' import { useAssetsStore } from '@/stores/assetsStore' import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore' +import { desktopHostUser } from '@/platform/auth/desktopHost/desktopHostSession' import { useAuthStore } from '@/stores/authStore' import { AGENT_ATTACH_ACCEPT, @@ -270,13 +271,35 @@ onBeforeUnmount(() => agentIdentity.stop()) const authStore = useAuthStore() const apiKeyAuthStore = useApiKeyAuthStore() -const agentAccountId = computed( - () => authStore.userId ?? apiKeyAuthStore.currentUser?.id ?? null +const teamWorkspaceStore = useTeamWorkspaceStore() +const agentSocketIdentity = computed(() => [ + authStore.userId ?? apiKeyAuthStore.currentUser?.id ?? null, + teamWorkspaceStore.activeWorkspaceId ?? null, + desktopHostUser.value?.workspaceId ?? null +]) +watch( + agentSocketIdentity, + ( + [account, workspace, hostWorkspace], + [wasAccount, wasWorkspace, wasHostWorkspace] + ) => { + if ( + account === wasAccount && + workspace === wasWorkspace && + hostWorkspace === wasHostWorkspace + ) + return + if ( + workspace === null && + account === wasAccount && + account !== null && + hostWorkspace === null + ) + return + events.reconnect() + agentIdentity.reset() + } ) -watch(agentAccountId, () => { - events.reconnect() - agentIdentity.reset() -}) function onPaywallAction( action: AgentPaywallAction, @@ -638,7 +661,7 @@ onBeforeUnmount(() => }) ) const { withConsent } = useAgentConsent() -const workspaceStore = useTeamWorkspaceStore() +const workspaceStore = teamWorkspaceStore const onboardingKey = computed(() => scopedOnboardingKey( resolvedUserInfo.value?.id,