diff --git a/browser_tests/fixtures/data/agent/agentPaywallPlanNote.ts b/browser_tests/fixtures/data/agent/agentPaywallPlanNote.ts new file mode 100644 index 00000000000..f859d90a628 --- /dev/null +++ b/browser_tests/fixtures/data/agent/agentPaywallPlanNote.ts @@ -0,0 +1,29 @@ +import type { BillingStatusResponse } from '@comfyorg/ingest-types' + +import { zAgentAdmissionError } from '@/workbench/extensions/agent/schemas/agentApiSchema' + +export const AGENT_NO_FUNDS_ERROR = zAgentAdmissionError.parse({ + error: { + message: + 'This workspace is out of funds. Add credits or subscribe to keep going.', + reason: 'no_funds', + type: 'PAYMENT_REQUIRED' + } +}) + +export const NEVER_SUBSCRIBED_BILLING_STATUS = { + billing_rail: 'stripe', + billing_status: 'inactive', + has_funds: false, + is_active: false, + max_seats: 1, + occupied_seats: 1, + scoped_effective_has_funds: { agent: false }, + scoped_has_funds: { agent: false }, + scheduled_change: null, + next_charge: null, + subscription_state: 'never_subscribed', + subscription_tier: 'FREE', + ended_at: null, + team_credit_stop: null +} satisfies BillingStatusResponse diff --git a/browser_tests/tests/agent/agentPaywallPlanNote.spec.ts b/browser_tests/tests/agent/agentPaywallPlanNote.spec.ts new file mode 100644 index 00000000000..cc8becef4ed --- /dev/null +++ b/browser_tests/tests/agent/agentPaywallPlanNote.spec.ts @@ -0,0 +1,71 @@ +import { expect } from '@playwright/test' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } + +import { waitForCloudApp } from '@e2e/fixtures/cloudAppFixture' +import { + AGENT_NO_FUNDS_ERROR, + NEVER_SUBSCRIBED_BILLING_STATUS +} from '@e2e/fixtures/data/agent/agentPaywallPlanNote' +import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute' +import { expectElementScreenshot } from '@e2e/fixtures/utils/screenshotRegion' +import { AGENT_COMPOSER_LABEL } from '@e2e/fixtures/utils/agentComposerLabel' +import { agentTest } from '@e2e/tests/agent/agentPanelMocks' + +agentTest.describe( + 'Agent paywall for an account without a plan', + { tag: ['@cloud', '@ui'] }, + () => { + agentTest.beforeEach(async ({ page, agentPanel }) => { + await page.route('**/api/billing/status', (route) => + route.fulfill(jsonRoute(NEVER_SUBSCRIBED_BILLING_STATUS)) + ) + await page.route('**/api/agent/threads/*/messages', (route) => + route.request().method() === 'POST' + ? route.fulfill({ ...jsonRoute(AGENT_NO_FUNDS_ERROR), status: 402 }) + : route.fallback() + ) + await page.reload() + await waitForCloudApp(page) + await agentPanel.open() + }) + + agentTest( + 'says buying credits does not start a Cloud plan', + { tag: '@screenshot' }, + async ({ page }) => { + const panel = page.locator('#agent-panel-root') + await panel + .getByRole('button', { name: enMessages.agent.switchWorkflow }) + .click() + await page + .getByRole('menuitemradio', { name: 'Unsaved Workflow', exact: true }) + .click() + await panel + .getByRole('textbox', { name: AGENT_COMPOSER_LABEL }) + .fill('Make a red fox in the snow') + await panel.getByRole('button', { name: enMessages.agent.send }).click() + + const card = panel + .getByRole('alert') + .filter({ hasText: enMessages.agent.paywall.title }) + await expect(card).toBeVisible() + await expect( + card.getByRole('button', { + name: enMessages.agent.paywall.addCredits + }) + ).toBeVisible() + await expect( + card.getByRole('button', { + name: enMessages.agent.paywall.subscribe, + exact: true + }) + ).toBeVisible() + await expect(card.getByTestId('agent-paywall-plan-note')).toHaveText( + enMessages.credits.topUp.planRequiredSubscribe + ) + await expectElementScreenshot(card, 'agent-paywall-plan-note.png') + } + ) + } +) diff --git a/browser_tests/tests/agent/agentPaywallPlanNote.spec.ts-snapshots/agent-paywall-plan-note-cloud-linux.png b/browser_tests/tests/agent/agentPaywallPlanNote.spec.ts-snapshots/agent-paywall-plan-note-cloud-linux.png new file mode 100644 index 00000000000..a925769e4df Binary files /dev/null and b/browser_tests/tests/agent/agentPaywallPlanNote.spec.ts-snapshots/agent-paywall-plan-note-cloud-linux.png differ diff --git a/src/platform/billing/composables/useTopUpPlanCreditCopy.ts b/src/platform/billing/composables/useTopUpPlanCreditCopy.ts index 0305dfa4183..c9c3cb33658 100644 --- a/src/platform/billing/composables/useTopUpPlanCreditCopy.ts +++ b/src/platform/billing/composables/useTopUpPlanCreditCopy.ts @@ -3,12 +3,13 @@ import { useI18n } from 'vue-i18n' import type { AddCreditsPlanLine } from '@comfyorg/account-core/billing' import { topUpPlanCreditLine } from '@/platform/billing/topUpPlanCreditLine' +import type { TopUpPlanCreditLine } from '@/platform/billing/topUpPlanCreditLine' import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' import { useBillingContext } from '@/platform/billing/composables/useBillingContext' import { useFreeTierQuota } from '@/platform/billing/subscription/composables/useFreeTierQuota' import { isCloud } from '@/platform/distribution/types' -const LINE_KEYS = { +export const TOP_UP_PLAN_CREDIT_LINE_KEYS = { planRequiredFreeRuns: 'credits.topUp.planRequiredFreeRuns', planRequiredSubscribe: 'credits.topUp.planRequiredSubscribe', planRequiredResubscribe: 'credits.topUp.planRequiredResubscribe' @@ -18,9 +19,10 @@ const ADD_CREDITS_LINES = { planRequiredFreeRuns: 'free_runs', planRequiredSubscribe: 'no_plan', planRequiredResubscribe: 'ended' -} as const satisfies Record +} as const satisfies Record -function usePlanCreditLine() { +/** Which plan-required line applies on Cloud, or null when none does. */ +export function usePlanCreditLine() { const { isInitialized, subscriptionState } = useBillingContext() const { canTopUp } = useBillingCapabilities() const { available: freeRunsRemaining } = useFreeTierQuota() @@ -40,7 +42,9 @@ function usePlanCreditLine() { export function useTopUpPlanCreditCopy() { const { t } = useI18n() const line = usePlanCreditLine() - return computed(() => (line.value === null ? null : t(LINE_KEYS[line.value]))) + return computed(() => + line.value === null ? null : t(TOP_UP_PLAN_CREDIT_LINE_KEYS[line.value]) + ) } /** The same line as the Add credits dialog's plan-line fact. */ diff --git a/src/platform/billing/topUpPlanCreditLine.ts b/src/platform/billing/topUpPlanCreditLine.ts index 839f86dcd85..c03906fd026 100644 --- a/src/platform/billing/topUpPlanCreditLine.ts +++ b/src/platform/billing/topUpPlanCreditLine.ts @@ -1,5 +1,10 @@ import type { SubscriptionState } from '@comfyorg/ingest-types' +export type TopUpPlanCreditLine = + | 'planRequiredFreeRuns' + | 'planRequiredSubscribe' + | 'planRequiredResubscribe' + /** Add credits line on Cloud for a served `subscription_state` that has no plan. */ export function topUpPlanCreditLine(facts: { isCloud: boolean @@ -7,11 +12,7 @@ export function topUpPlanCreditLine(facts: { canTopUp: boolean subscriptionState: SubscriptionState | null freeRunsRemaining: number -}): - | 'planRequiredFreeRuns' - | 'planRequiredSubscribe' - | 'planRequiredResubscribe' - | null { +}): TopUpPlanCreditLine | null { if (!facts.isCloud) return null if (!facts.isBillingInitialized) return null if (!facts.canTopUp) return null diff --git a/src/workbench/extensions/agent/AgentPanelRoot.tabActivity.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.tabActivity.test.ts index bc70a6811d0..287243cb16e 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.tabActivity.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.tabActivity.test.ts @@ -312,6 +312,8 @@ beforeEach(() => { type: computed(() => 'workspace' as const), billingStatus: computed(() => 'paid' as const), canAccessSubscriptionFeatures: computed(() => true), + isInitialized: computed(() => true), + subscriptionState: computed(() => 'active' as const), fetchStatus: vi.fn(async () => {}) }) ) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index ede01c149f6..a3f8596d76d 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -13,6 +13,7 @@ import type { BillingStatus, GlobalSetting, PaginationInfo, + SubscriptionState, SubscriptionTier } from '@comfyorg/ingest-types' import type { User } from 'firebase/auth' @@ -284,6 +285,7 @@ const paywallBilling = vi.hoisted(() => ({ type: 'workspace' as 'workspace' | 'unknown', status: 'paid' as BillingStatus | null, isActive: true, + subscriptionState: 'active' as SubscriptionState | null, fetchStatus: vi.fn<() => Promise>() })) const paywallHasFunds = ref(false) @@ -428,6 +430,8 @@ beforeEach(() => { type: computed(() => paywallBilling.type), billingStatus: computed(() => paywallBilling.status), canAccessSubscriptionFeatures: computed(() => paywallBilling.isActive), + isInitialized: computed(() => true), + subscriptionState: computed(() => paywallBilling.subscriptionState), fetchStatus: paywallBilling.fetchStatus }) ) @@ -545,6 +549,7 @@ beforeEach(() => { paywallBilling.tier = 'STANDARD' paywallBilling.type = 'workspace' paywallBilling.status = 'paid' + paywallBilling.subscriptionState = 'active' paywallBilling.isActive = true paywallBilling.fetchStatus.mockReset().mockResolvedValue(undefined) paywallHasFunds.value = false @@ -1319,6 +1324,39 @@ describe('AgentPanelRoot paywall actions', () => { ).not.toBeInTheDocument() }) + it('says Add credits does not start a Cloud plan for an owner without one', async () => { + paywallBilling.tier = 'FREE' + paywallBilling.isActive = false + paywallBilling.subscriptionState = 'never_subscribed' + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + useAgentConversationStore().messages.push({ + id: toTurnId('msg-paywall'), + role: 'assistant', + parts: [{ type: 'paywall' }], + streaming: false, + thinking: false + }) + + expect(await findInlinePaywallButton('Add credits')).toBeInTheDocument() + expect( + await screen.findByTestId('agent-paywall-plan-note') + ).toHaveTextContent("Buying credits doesn't start a Cloud plan.") + }) + + it('keeps the plan note out for an owner with an active plan', async () => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + useAgentConversationStore().messages.push({ + id: toTurnId('msg-paywall'), + role: 'assistant', + parts: [{ type: 'paywall' }], + streaming: false, + thinking: false + }) + + expect(await findInlinePaywallButton('Add credits')).toBeInTheDocument() + expect(screen.queryByTestId('agent-paywall-plan-note')).toBeNull() + }) + it('shows Subscribe only when the server allows self-serve subscription but not top-up', async () => { paywallCapabilities.canTopUp = false render(AgentPanelRoot, { global: { plugins: [i18n] } }) @@ -1443,6 +1481,8 @@ describe('AgentPanelRoot paywall telemetry', () => { tier, type: computed(() => 'workspace'), billingStatus: computed(() => 'paid'), + isInitialized: computed(() => true), + subscriptionState: computed(() => 'active' as const), fetchStatus: vi.fn().mockResolvedValue(undefined) }) ) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 1cadd28a788..4d927210994 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -85,6 +85,7 @@ import { parseNodeLocatorId } from '@comfyorg/graph-model/nodeIdentification' import { useBillingContext } from '@/platform/billing/composables/useBillingContext' import { useAccountPreconditionDialog } from '@/platform/billing/subscription/composables/useAccountPreconditionDialog' import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' +import { usePlanCreditLine } from '@/platform/billing/composables/useTopUpPlanCreditCopy' import { useOnboardingTourStore } from '@/platform/onboarding/onboardingTourStore' import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' @@ -231,6 +232,7 @@ const { isReady: capabilityReadSettled, snapshotAuthoritative } = useBillingCapabilities() +const planCreditLine = usePlanCreditLine() const paywallPresentation = computed(() => { if (isCloud && !hasResolvedCapabilities.value && !canTopUp.value) { return DEFAULT_AGENT_PAYWALL_PRESENTATION @@ -240,7 +242,8 @@ const paywallPresentation = computed(() => { role: workspaceRole.value, tier: subscriptionTier.value, canTopUp: canTopUp.value, - canSubscribeSelfServe: canSubscribeSelfServe.value + canSubscribeSelfServe: canSubscribeSelfServe.value, + planCreditLine: planCreditLine.value }) }) const sidebarTabStore = useSidebarTabStore() diff --git a/src/workbench/extensions/agent/components/agent/message/AgentPausedCard.test.ts b/src/workbench/extensions/agent/components/agent/message/AgentPausedCard.test.ts index 1f3bd2d65a2..cdd821be5d3 100644 --- a/src/workbench/extensions/agent/components/agent/message/AgentPausedCard.test.ts +++ b/src/workbench/extensions/agent/components/agent/message/AgentPausedCard.test.ts @@ -106,6 +106,40 @@ describe('AgentPausedCard', () => { } ) + it('says credits do not start a Cloud plan on a no-funds pause without a plan', () => { + render(AgentPausedCard, { + props: { + part: pausedPart(), + presentation: { + kind: 'subscribed', + showUpgrade: false, + planCreditLine: 'planRequiredSubscribe' + } + }, + global: { plugins: [i18n] } + }) + + expect(screen.getByTestId('agent-paywall-plan-note')).toHaveTextContent( + "Buying credits doesn't start a Cloud plan. Subscribe to run workflows on Cloud." + ) + }) + + it('keeps the plan note off a pause that is not about credits', () => { + render(AgentPausedCard, { + props: { + part: pausedPart({ reason: 'other' }), + presentation: { + kind: 'subscribed', + showUpgrade: false, + planCreditLine: 'planRequiredSubscribe' + } + }, + global: { plugins: [i18n] } + }) + + expect(screen.queryByTestId('agent-paywall-plan-note')).toBeNull() + }) + it('drops the recheck line when the server names no window', () => { render(AgentPausedCard, { props: { part: pausedPart({ recheckAfterSeconds: 0 }) }, diff --git a/src/workbench/extensions/agent/components/agent/message/AgentPausedCard.vue b/src/workbench/extensions/agent/components/agent/message/AgentPausedCard.vue index 2588756b2b0..6f50b7bcb42 100644 --- a/src/workbench/extensions/agent/components/agent/message/AgentPausedCard.vue +++ b/src/workbench/extensions/agent/components/agent/message/AgentPausedCard.vue @@ -8,6 +8,7 @@ import { cn } from '@comfyorg/tailwind-utils' import type { PausedPart } from '@/workbench/extensions/agent/services/agent/agentMessageParts' import { NO_FUNDS_PAUSE_REASON } from '@/workbench/extensions/agent/services/agent/agentMessageParts' +import AgentPaywallPlanNote from '@/workbench/extensions/agent/components/agent/message/AgentPaywallPlanNote.vue' import type { AgentPaywallPresentation, AgentPaywallRemediation @@ -75,6 +76,7 @@ const body = computed(() => part.message || part.prompt)

{{ body }}

+

{ expect(onPaywallAction.mock.calls).toEqual([['upgrade'], ['addCredits']]) }) + it.for([ + { + planCreditLine: 'planRequiredFreeRuns' as const, + note: "Buying credits doesn't start a Cloud plan. Subscribe to keep running workflows once your free runs are used." + }, + { + planCreditLine: 'planRequiredSubscribe' as const, + note: "Buying credits doesn't start a Cloud plan. Subscribe to run workflows on Cloud." + } + ])( + 'says Add credits does not start a plan for an owner with no plan ($planCreditLine)', + ({ planCreditLine, note }) => { + render(AgentPaywallCard, { + props: { + presentation: { + kind: 'subscribed', + showUpgrade: false, + planCreditLine + } + }, + global: { plugins: [i18n] } + }) + + expect(screen.getByTestId('agent-paywall-plan-note')).toHaveTextContent( + note + ) + expect( + screen.getByText( + 'Your account has no credits available for the agent. Add credits to use it.' + ) + ).toBeInTheDocument() + expect( + screen.queryByText(/spent its monthly credits/) + ).not.toBeInTheDocument() + expect( + screen.getByRole('button', { name: 'Add credits' }) + ).toBeInTheDocument() + expect( + screen.getByRole('button', { name: 'Subscribe' }) + ).toBeInTheDocument() + expect(screen.queryByRole('button', { name: 'Upgrade plan' })).toBeNull() + } + ) + + it('keeps the plan note out for a subscribed owner', () => { + render(AgentPaywallCard, { + props: { presentation: { kind: 'subscribed', showUpgrade: true } }, + global: { plugins: [i18n] } + }) + + expect(screen.queryByTestId('agent-paywall-plan-note')).toBeNull() + }) + it('announces the server denial without purchase actions when capabilities are unknown', () => { render(AgentPaywallCard, { props: { message: 'Your workspace spend limit was reached.' }, diff --git a/src/workbench/extensions/agent/components/agent/message/AgentPaywallCard.vue b/src/workbench/extensions/agent/components/agent/message/AgentPaywallCard.vue index 418f85637e0..99188db3914 100644 --- a/src/workbench/extensions/agent/components/agent/message/AgentPaywallCard.vue +++ b/src/workbench/extensions/agent/components/agent/message/AgentPaywallCard.vue @@ -4,6 +4,7 @@ import { computed } from 'vue' import Button from '@comfyorg/design-system/button/Button' +import AgentPaywallPlanNote from '@/workbench/extensions/agent/components/agent/message/AgentPaywallPlanNote.vue' import { DEFAULT_AGENT_PAYWALL_PRESENTATION, toAgentPaywallRemediation @@ -30,12 +31,19 @@ const bodyKeys: Record = { local: 'agent.paywall.body.local', unavailable: 'agent.paywall.body.subscriptionRequired' } -const bodyKey = computed(() => bodyKeys[presentation.kind]) +const hasNoPlan = computed( + () => presentation.kind === 'subscribed' && !!presentation.planCreditLine +) +const bodyKey = computed(() => + hasNoPlan.value ? bodyKeys.local : bodyKeys[presentation.kind] +) const showUpgrade = computed( () => presentation.kind === 'subscribed' && presentation.showUpgrade ) const remediation = computed(() => toAgentPaywallRemediation(presentation)) -const showSubscribe = computed(() => remediation.value === 'subscribe') +const showSubscribe = computed( + () => remediation.value === 'subscribe' || hasNoPlan.value +) const showAddCredits = computed(() => remediation.value === 'addCredits') @@ -57,6 +65,7 @@ const showAddCredits = computed(() => remediation.value === 'addCredits')

{{ message || $t(bodyKey) }}

+ @@ -74,7 +83,7 @@ const showAddCredits = computed(() => remediation.value === 'addCredits')