From 5d6def8d67e565a0f9b507b537273d6ba70d5cc6 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sat, 19 Sep 2026 21:05:46 -0700 Subject: [PATCH 1/8] Restore agent paywall (FE-1706) and admission-error handling (PM-518) (#16856) Ports the completed agent admission-error and paywall UI work onto current `main` from the closed slice successor sources. Includes permanent tests and Storybook coverage for admission errors, capability-driven actions, responsive layout, and billing-policy presentation. Carrier: [FE-2005](https://linear.app/comfy-org/issue/FE-2005) Fixes [FE-1706](https://linear.app/comfy-org/issue/FE-1706) (paywall handling in the agent panel) Fixes [PM-518](https://linear.app/comfy-org/issue/PM-518) (admission errors surfaced to the user) Verification: - Focused Vitest: 7 files, 246 tests passed - Pre-commit formatting, lint, and full typecheck passed - Pre-push Knip passed Backport note (cloud/1.54): - Main's end state (mechanical): AgentPanelRoot.vue imports both RootGraphId and isCloud, and its paywall block is main's combination of this change with #18405. AgentPanelRoot.test.ts takes the hasResolvedCapabilities stub and keeps #18405's paywall-dismissal test after this change's Subscribe test. - This line's side kept (test-only): AgentPanelRoot.test.ts's LGraph type import and useCurrentUser factory mock. Main's factory-less mock relies on a __mocks__ file this line does not have. - Not taken: __mocks__/useBillingCapabilities.ts, main's shared mock from #17726, which nothing here uses; and agentPanel.spec.ts's diagnostic report tests, from #17773. Only this change's admission test is added. - useAgentSession.ts takes this change's split of sendMessage, which drops this line's unbound-tab rule (PM-1430). The next commit restores it as main has it (#18278), labelled runtime. (cherry picked from commit 058efefa434fc3cdaa09fa8cdd50bdd360b90094) --- browser_tests/tests/agent/agentPanel.spec.ts | 40 ++ src/locales/en/main.json | 1 + .../useBillingCapabilities.test.ts | 4 + .../composables/useBillingCapabilities.ts | 2 + .../extensions/agent/AgentPanelRoot.test.ts | 94 +++-- .../extensions/agent/AgentPanelRoot.vue | 37 +- .../agent/message/AgentMessage.test.ts | 10 +- .../components/agent/message/AgentMessage.vue | 79 +--- .../agent/message/AgentMessageGroup.vue | 94 +++++ .../agent/message/AgentPaywallCard.stories.ts | 16 + .../agent/message/AgentPaywallCard.test.ts | 21 +- .../agent/message/AgentPaywallCard.vue | 14 +- .../agent/message/agentMessageGroup.ts | 15 + .../composables/agent/useAgentSession.test.ts | 39 +- .../composables/agent/useAgentSession.ts | 360 +++++++++++------- .../agent/services/agent/agentMessageParts.ts | 1 + .../agent/agentPaywallPresentation.test.ts | 39 +- .../agent/agentPaywallPresentation.ts | 12 +- .../services/agent/agentRestClient.test.ts | 24 +- .../agent/services/agent/agentRestClient.ts | 51 ++- .../stores/agent/agentConversationStore.ts | 8 +- 21 files changed, 654 insertions(+), 307 deletions(-) create mode 100644 src/workbench/extensions/agent/components/agent/message/AgentMessageGroup.vue create mode 100644 src/workbench/extensions/agent/components/agent/message/agentMessageGroup.ts diff --git a/browser_tests/tests/agent/agentPanel.spec.ts b/browser_tests/tests/agent/agentPanel.spec.ts index 0897a173d6b..bf4703f737a 100644 --- a/browser_tests/tests/agent/agentPanel.spec.ts +++ b/browser_tests/tests/agent/agentPanel.spec.ts @@ -178,6 +178,46 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { await expect(panel.getByText('Resize image node')).toBeVisible() }) + test('shows an admission paywall without losing the rejected prompt', async ({ + agentPanel, + comfyPage + }) => { + const page = comfyPage.page + await page.route('**/api/agent/threads/*/messages', (route) => + route.fulfill({ + status: 402, + contentType: 'application/json', + body: JSON.stringify({ + error: { + message: 'Add credits to continue.', + type: 'PAYMENT_REQUIRED', + reason: 'no_funds' + } + }) + }) + ) + + await agentPanel.open() + await agentPanel.selectWorkflow() + const panel = agentPanel.root + + const prompt = 'Build a product photo workflow' + await panel.getByRole('textbox', { name: /^Describe ideas/ }).fill(prompt) + await panel.getByRole('button', { name: 'Send' }).click() + + // The prompt survives the rejection in two places the user can act on: the + // sent message stays in the transcript, and the composer keeps the text so + // the same send can be retried once credits are added. A bare getByText is + // ambiguous here because the thread title button also carries the prompt. + await expect(panel.getByTestId('user-message-bubble')).toHaveText(prompt) + await expect( + panel.getByRole('textbox', { name: /^Describe ideas/ }) + ).toHaveText(prompt) + const paywall = panel.getByRole('alert') + await expect(paywall).toContainText(enMessages.agent.paywall.title) + await expect(paywall).toContainText('Add credits to continue.') + }) + test.describe('composer sizing', () => { test.use({ viewport: { width: 1920, height: 1080 }, diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 9686d0b0c26..95efbca5edd 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -5304,6 +5304,7 @@ "noNodesToMention": "No nodes in this workflow", "friend": "there", "sendFailed": "Message failed to send", + "retryAfter": "Try again in {count} second. | Try again in {count} seconds.", "sendBusy": "A message is already being sent", "retryAfterSeconds": "You can try again in {seconds}s.", "malformedEvent": "The agent sent an event this panel could not read", diff --git a/src/platform/workspace/composables/useBillingCapabilities.test.ts b/src/platform/workspace/composables/useBillingCapabilities.test.ts index 70a90dad7a1..142a38c1e70 100644 --- a/src/platform/workspace/composables/useBillingCapabilities.test.ts +++ b/src/platform/workspace/composables/useBillingCapabilities.test.ts @@ -206,11 +206,13 @@ describe('useBillingCapabilities', () => { expect(billingCapabilities.snapshotAuthoritative.value).toBe(false) const initialization = billingCapabilities.initialize() + expect(billingCapabilities.hasResolvedCapabilities.value).toBe(false) expect(billingCapabilities.canTopUp.value).toBe(false) expect(billingCapabilities.canSubscribeSelfServe.value).toBe(false) resolveRequest(capabilitiesResponse(true)) await initialization + expect(billingCapabilities.hasResolvedCapabilities.value).toBe(true) expect(billingCapabilities.canTopUp.value).toBe(true) expect(billingCapabilities.canSubscribeSelfServe.value).toBe(true) expect(billingCapabilities.canCancel.value).toBe(true) @@ -277,6 +279,7 @@ describe('useBillingCapabilities', () => { await billingCapabilities.initialize() + expect(billingCapabilities.hasResolvedCapabilities.value).toBe(false) expect(billingCapabilities.canTopUp.value).toBe(true) expect(billingCapabilities.canSubscribeSelfServe.value).toBe(false) expect(billingCapabilities.canCancel.value).toBe(false) @@ -310,6 +313,7 @@ describe('useBillingCapabilities', () => { await billingCapabilities.initialize() + expect(billingCapabilities.hasResolvedCapabilities.value).toBe(false) expect(billingCapabilities.canTopUp.value).toBe(false) expect(billingCapabilities.canSubscribeSelfServe.value).toBe(false) expect(billingCapabilities.isReady.value).toBe(true) diff --git a/src/platform/workspace/composables/useBillingCapabilities.ts b/src/platform/workspace/composables/useBillingCapabilities.ts index a85f5b86b2b..4109df79aff 100644 --- a/src/platform/workspace/composables/useBillingCapabilities.ts +++ b/src/platform/workspace/composables/useBillingCapabilities.ts @@ -85,6 +85,7 @@ function useBillingCapabilitiesInternal() { return state.response.capabilities }) + const hasResolvedCapabilities = computed(() => capabilities.value !== null) const readUnavailableForCurrentScope = computed(() => { const state = readState.value return ( @@ -426,6 +427,7 @@ function useBillingCapabilitiesInternal() { canInviteMembers, canDowngradeToPersonal, isReady, + hasResolvedCapabilities, snapshotAuthoritative, initialize, refresh diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 664a9fe0fcd..1926eea7e4c 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -257,12 +257,13 @@ vi.mock( ) const paywallWorkspace = vi.hoisted(() => ({ - role: 'owner' as 'owner' | 'member' + role: 'owner' as 'owner' | 'member' | undefined })) const paywallCapabilities = vi.hoisted(() => ({ canTopUp: true, canSubscribeSelfServe: true, - isReady: true + isReady: true, + hasResolvedCapabilities: true })) const paywallBilling = vi.hoisted(() => ({ tier: 'STANDARD' as SubscriptionTier | null @@ -345,7 +346,10 @@ beforeEach(() => { canSubscribeSelfServe: computed( () => paywallCapabilities.canSubscribeSelfServe ), - isReady: computed(() => paywallCapabilities.isReady) + isReady: computed(() => paywallCapabilities.isReady), + hasResolvedCapabilities: computed( + () => paywallCapabilities.hasResolvedCapabilities + ) }) ) workflowStore = useWorkflowStore() @@ -381,9 +385,19 @@ beforeEach(() => { focusNodeInstance.mockReset() socketSend.mockReset() paywallWorkspace.role = 'owner' + vi.spyOn( + useTeamWorkspaceStore(), + 'activeWorkspace', + 'get' + ).mockImplementation(() => + paywallWorkspace.role === undefined + ? null + : fromPartial({ role: paywallWorkspace.role }) + ) paywallCapabilities.canTopUp = true paywallCapabilities.canSubscribeSelfServe = true paywallCapabilities.isReady = true + paywallCapabilities.hasResolvedCapabilities = true paywallBilling.tier = 'STANDARD' paywallHasFunds.value = false }) @@ -723,14 +737,37 @@ describe('AgentPanelRoot paywall actions', () => { await userEvent.click( await screen.findByRole('button', { name: 'Upgrade plan' }) ) - await userEvent.click(screen.getByRole('button', { name: 'Add credits' })) + expect(openAccountPrecondition).toHaveBeenCalledExactlyOnceWith( + 'subscription' + ) + await userEvent.click(screen.getByRole('button', { name: 'Add credits' })) expect(openAccountPrecondition.mock.calls).toEqual([ ['subscription'], ['credits'] ]) }) + it('routes Subscribe through the subscription precondition', async () => { + paywallCapabilities.canTopUp = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + useAgentConversationStore().messages.push({ + id: 'msg-paywall' as TurnId, + role: 'assistant', + parts: [{ type: 'paywall' }], + streaming: false, + thinking: false + }) + + await userEvent.click( + await screen.findByRole('button', { name: 'Subscribe' }) + ) + + expect(openAccountPrecondition).toHaveBeenCalledExactlyOnceWith( + 'subscription' + ) + }) + it('dismisses the paywall after billing confirms funds are available', async () => { paywallCapabilities.canTopUp = false render(AgentPanelRoot, { global: { plugins: [i18n] } }) @@ -752,6 +789,8 @@ describe('AgentPanelRoot paywall actions', () => { it('hides purchase actions from a Team member without billing permissions', async () => { paywallWorkspace.role = 'member' + paywallCapabilities.canTopUp = false + paywallCapabilities.canSubscribeSelfServe = false render(AgentPanelRoot, { global: { plugins: [i18n] } }) useAgentConversationStore().messages.push({ id: 'msg-paywall' as TurnId, @@ -857,29 +896,32 @@ describe('AgentPanelRoot paywall actions', () => { ).not.toBeInTheDocument() }) - it('does not interpret the pending false pair as sales-managed', async () => { - paywallCapabilities.canTopUp = false - paywallCapabilities.canSubscribeSelfServe = false - paywallCapabilities.isReady = false - render(AgentPanelRoot, { global: { plugins: [i18n] } }) - useAgentConversationStore().messages.push({ - id: 'msg-paywall' as TurnId, - role: 'assistant', - parts: [{ type: 'paywall' }], - streaming: false, - thinking: false - }) + it.for(['pending', 'denied', 'unresolved-role'] as const)( + 'withholds purchase actions for %s without inventing a sales-managed plan', + async (state) => { + paywallCapabilities.canTopUp = false + paywallCapabilities.canSubscribeSelfServe = false + paywallCapabilities.isReady = state !== 'pending' + paywallCapabilities.hasResolvedCapabilities = state === 'unresolved-role' + if (state === 'unresolved-role') paywallWorkspace.role = undefined + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + useAgentConversationStore().messages.push({ + id: 'msg-paywall' as TurnId, + role: 'assistant', + parts: [{ type: 'paywall' }], + streaming: false, + thinking: false + }) - expect( - await screen.findByRole('button', { name: 'Add credits' }) - ).toBeInTheDocument() - expect( - screen.getByRole('button', { name: 'Upgrade plan' }) - ).toBeInTheDocument() - expect( - screen.queryByText(/billed through your Comfy account team/i) - ).not.toBeInTheDocument() - }) + expect(await screen.findByText('Out of credits')).toBeInTheDocument() + expect( + screen.queryByRole('button', { name: /add credits|upgrade|subscribe/i }) + ).not.toBeInTheDocument() + expect( + screen.queryByText(/billed through your Comfy account team/i) + ).not.toBeInTheDocument() + } + ) }) describe('AgentPanelRoot session notices', () => { diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 7c656311804..34124632095 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -56,18 +56,20 @@ import { isLGraphNode } from '@/utils/litegraphUtil' import { useToastStore } from '@/platform/updates/common/toastStore' import { toOwningGraphId, toRootGraphId } from '@/types/graphScopeId' import type { RootGraphId } from '@/types/graphScopeId' +import { isCloud } from '@/platform/distribution/types' import { parseNodeId } from '@/types/nodeId' import { useBillingContext } from '@/composables/billing/useBillingContext' import { useAccountPreconditionDialog } from '@/platform/cloud/subscription/composables/useAccountPreconditionDialog' import { useBillingCapabilities } from '@/platform/workspace/composables/useBillingCapabilities' import { useOnboardingTourStore } from '@/platform/onboarding/onboardingTourStore' +import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI' +import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' import { adoptSharedOnboardingFlag, hasSeenCoach, scopedOnboardingKey, trackCoachDeferral } from './composables/agent/useOnboarding' -import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI' import AgentPanel from './components/agent/AgentPanel.vue' import AgentGraphActivityBar from './components/AgentGraphActivityBar.vue' @@ -102,12 +104,14 @@ import { useAgentWorkflowTabBindingStore } from './stores/agent/agentWorkflowTab import { createAgentRestClient } from './services/agent/agentRestClient' import type { DraftSnapshot } from './services/agent/agentRestClient' import type { AgentPaywallAction } from './services/agent/agentPaywallPresentation' -import { resolveAgentPaywallPresentation } from './services/agent/agentPaywallPresentation' +import { + DEFAULT_AGENT_PAYWALL_PRESENTATION, + resolveAgentPaywallPresentation +} from './services/agent/agentPaywallPresentation' import { createAgentEventSource } from './services/agent/agentEventSource' import { useAgentChatHistoryStore } from './stores/agent/agentChatHistoryStore' import { agentMessageText } from './utils/agentMessageText' import { useAgentComposerStore } from './stores/agent/agentComposerStore' -import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' import { useAgentConsentStore } from './stores/agent/agentConsentStore' import { useAgentPanelStore } from './stores/agent/agentPanelStore' import { useAgentGraphActivityStore } from './stores/agent/agentGraphActivityStore' @@ -134,25 +138,20 @@ watch( (hasFunds) => conversationStore.setPaywallsResolved(hasFunds === true), { immediate: true } ) -const { - canTopUp, - canSubscribeSelfServe, - isReady: billingCapabilitiesReady -} = useBillingCapabilities() -const paywallPresentation = computed(() => - resolveAgentPaywallPresentation({ +const { canTopUp, canSubscribeSelfServe, hasResolvedCapabilities } = + useBillingCapabilities() +const paywallPresentation = computed(() => { + if (isCloud && !hasResolvedCapabilities.value && !canTopUp.value) { + return DEFAULT_AGENT_PAYWALL_PRESENTATION + } + return resolveAgentPaywallPresentation({ + distribution: isCloud ? 'cloud' : 'local', role: workspaceRole.value, tier: subscriptionTier.value, - // The initial false/false pair is not an authoritative sales-managed - // result while the shared capability source initializes in the background. - canTopUp: billingCapabilitiesReady.value - ? canTopUp.value - : workspaceRole.value === 'owner', - canSubscribeSelfServe: billingCapabilitiesReady.value - ? canSubscribeSelfServe.value - : workspaceRole.value === 'owner' + canTopUp: canTopUp.value, + canSubscribeSelfServe: canSubscribeSelfServe.value }) -) +}) const sidebarTabStore = useSidebarTabStore() const { isBuilderMode } = useAppMode() diff --git a/src/workbench/extensions/agent/components/agent/message/AgentMessage.test.ts b/src/workbench/extensions/agent/components/agent/message/AgentMessage.test.ts index 931df43faa5..3a887e712a7 100644 --- a/src/workbench/extensions/agent/components/agent/message/AgentMessage.test.ts +++ b/src/workbench/extensions/agent/components/agent/message/AgentMessage.test.ts @@ -46,7 +46,10 @@ function paywallMessage(): AssistantMessage { describe('AgentMessage paywall reply', () => { it('renders the usage-limit card as an inline assistant reply', () => { render(AgentMessage, { - props: { message: paywallMessage() }, + props: { + message: paywallMessage(), + paywallPresentation: { kind: 'subscribed', showUpgrade: true } + }, global: { plugins: [i18n] } }) @@ -68,7 +71,10 @@ describe('AgentMessage paywall reply', () => { const user = userEvent.setup() const onPaywallAction = vi.fn() render(AgentMessage, { - props: { message: paywallMessage() }, + props: { + message: paywallMessage(), + paywallPresentation: { kind: 'subscribed', showUpgrade: true } + }, attrs: { onPaywallAction }, global: { plugins: [i18n] } }) diff --git a/src/workbench/extensions/agent/components/agent/message/AgentMessage.vue b/src/workbench/extensions/agent/components/agent/message/AgentMessage.vue index 0684e3a2ed1..9ffb659fbae 100644 --- a/src/workbench/extensions/agent/components/agent/message/AgentMessage.vue +++ b/src/workbench/extensions/agent/components/agent/message/AgentMessage.vue @@ -5,23 +5,15 @@ import { useI18n } from 'vue-i18n' import type { ActivityPart, AssistantMessage, - NoticePart, - RunApprovalPart, - PaywallPart, - TabLinkPart, TextPart } from '../../../services/agent/agentMessageParts' import { htmlReplyAssets } from '../../../utils/replyAssets' import { cn } from '@comfyorg/tailwind-utils' import { renderMarkdownToHtml } from '@/utils/markdownRendererUtil' -import MarkdownStream from './MarkdownStream.vue' -import AgentPaywallCard from './AgentPaywallCard.vue' +import AgentMessageGroup from './AgentMessageGroup.vue' import MessageFeedback from './MessageFeedback.vue' -import RunApprovalCard from './RunApprovalCard.vue' -import TabLinkCard from './TabLinkCard.vue' -import ActivityTrace from './ActivityTrace.vue' -import WorkSummary from './WorkSummary.vue' +import type { AgentMessageGroup as Group } from './agentMessageGroup' import { DEFAULT_AGENT_PAYWALL_PRESENTATION } from '@/workbench/extensions/agent/services/agent/agentPaywallPresentation' import type { AgentPaywallAction, @@ -47,14 +39,6 @@ const emit = defineEmits<{ paywallAction: [action: AgentPaywallAction] }>() -type Group = - | { kind: 'text'; part: TextPart } - | { kind: 'notice'; part: NoticePart } - | { kind: 'paywall'; part: PaywallPart } - | { kind: 'trace' } - | { kind: 'tabLinks'; parts: TabLinkPart[] } - | { kind: 'runApproval'; part: RunApprovalPart } - // Every thinking and tool part of the turn reads as one trace, wherever they fall // between the reply's text parts, so the completed turn folds into a single summary. const activityParts = computed(() => @@ -141,66 +125,19 @@ const status = computed(() => {