diff --git a/browser_tests/tests/agent/agentPanel.spec.ts b/browser_tests/tests/agent/agentPanel.spec.ts index f02ebe49166..7a4c7d1720e 100644 --- a/browser_tests/tests/agent/agentPanel.spec.ts +++ b/browser_tests/tests/agent/agentPanel.spec.ts @@ -1,11 +1,17 @@ +import assert from 'node:assert/strict' + import type { WebSocketRoute } from '@playwright/test' import { expect, mergeTests } from '@playwright/test' +import { TopUpCreditsDialog } from '@e2e/fixtures/components/TopUpCreditsDialog' import { webSocketFixture } from '@e2e/fixtures/ws' import enMessages from '@/locales/en/main.json' with { type: 'json' } import type { AgentWsEvent } from '@/workbench/extensions/agent/schemas/agentApiSchema' -import { zAgentAdmissionError } from '@/workbench/extensions/agent/schemas/agentApiSchema' +import { + zAgentAdmissionError, + zAgentWsEvent +} from '@/workbench/extensions/agent/schemas/agentApiSchema' import { INTERMEDIATE_MESSAGE_EVENT, @@ -214,6 +220,94 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { }) }) + test('keeps the standing paywall in sync across turn completion and panel close', async ({ + acceptedTurns, + agentBilling, + agentPanel, + comfyPage, + getWebSocket, + postedMessages + }) => { + test.setTimeout(30_000) + const page = comfyPage.page + const paywall = page.getByTestId('agent-credits-exhausted-paywall') + const ws = await getWebSocket() + + await test.step('show the standing paywall after an accepted turn exhausts credits', async () => { + await agentPanel.open() + await agentPanel.selectWorkflow() + agentBilling.setAgentFunds(false) + await agentPanel.sendMessage('Complete this workflow without a refusal') + await expect.poll(() => postedMessages.length).toBe(1) + pushEvent(ws, THINKING_EVENT) + await expect( + agentPanel.root.getByRole('button', { name: 'Stop' }) + ).toBeVisible() + await expect(paywall).toHaveCount(0) + pushEvent(ws, MESSAGE_DONE_EVENT) + await expect(paywall).toBeVisible() + }) + + await test.step('finish the funded recovery while the panel is closed', async () => { + agentBilling.setAgentFunds(true) + const recovery = agentBilling.holdNextFundedRefresh() + await paywall.getByRole('button', { name: 'Add Credits' }).click() + const topUpDialog = new TopUpCreditsDialog(page) + await topUpDialog.waitForVisible() + await recovery.entered + await topUpDialog.close() + await agentPanel.root + .getByRole('button', { name: enMessages.g.close }) + .click() + await expect(agentPanel.root).toHaveCount(0) + recovery.release() + await recovery.completed + }) + + await test.step('reopen without the stale paywall', async () => { + await agentPanel.open() + await expect(paywall).toHaveCount(0) + }) + + await test.step('show the standing paywall after a second exhaustion', async () => { + agentBilling.setAgentFunds(false) + await agentPanel.sendMessage( + 'Complete another workflow without a refusal' + ) + await expect.poll(() => postedMessages.length).toBe(2) + expect(acceptedTurns).toHaveLength(2) + const secondTurn = acceptedTurns.at(-1) + assert(secondTurn) + pushEvent( + ws, + zAgentWsEvent.parse({ + ...THINKING_EVENT, + data: { ...THINKING_EVENT.data, message_id: secondTurn.message_id } + }) + ) + await expect( + agentPanel.root.getByRole('button', { name: 'Stop' }) + ).toBeVisible() + pushEvent( + ws, + zAgentWsEvent.parse({ + ...MESSAGE_DONE_EVENT, + data: { + ...MESSAGE_DONE_EVENT.data, + message_id: secondTurn.message_id + } + }) + ) + await expect(paywall).toBeVisible() + }) + + await test.step('remove the standing paywall after the final funded refresh', async () => { + agentBilling.setAgentFunds(true) + await paywall.getByRole('button', { name: 'Add Credits' }).click() + await expect(paywall).toHaveCount(0) + }) + }) + test('keeps a failed prompt available to retry', async ({ agentPanel, comfyPage diff --git a/browser_tests/tests/agent/agentPanelMocks.ts b/browser_tests/tests/agent/agentPanelMocks.ts index 6a87ca36077..e6767259df5 100644 --- a/browser_tests/tests/agent/agentPanelMocks.ts +++ b/browser_tests/tests/agent/agentPanelMocks.ts @@ -3,6 +3,7 @@ import type { Page, Route } from '@playwright/test' import type { AgentThreadListResponse, + BillingStatusResponse, GlobalSetting, WorkflowListResponse } from '@comfyorg/ingest-types' @@ -37,6 +38,89 @@ const TURN_ACCEPTED: AgentTurnAccepted = { const CANCEL_ACCEPTED: AgentCancelAccepted = { status: 'cancelling' } +const FUNDED_BILLING_STATUS = { + billing_rail: 'stripe', + billing_status: 'paid', + has_funds: true, + is_active: true, + max_seats: 1, + occupied_seats: 1, + scoped_effective_has_funds: { agent: true }, + scheduled_change: null, + subscription_duration: 'MONTHLY', + subscription_status: 'active', + subscription_tier: 'STANDARD', + team_credit_stop: null +} satisfies BillingStatusResponse + +type HeldBillingRefresh = { + entered: Promise + completed: Promise + release: () => void +} + +class AgentBillingFixture { + private status: BillingStatusResponse = FUNDED_BILLING_STATUS + private heldRefresh: + | { + resolveEntered: () => void + resolveCompleted: () => void + releaseRequest?: () => void + } + | undefined + + setAgentFunds(hasFunds: boolean): void { + this.status = { + ...FUNDED_BILLING_STATUS, + has_funds: hasFunds, + scoped_effective_has_funds: { agent: hasFunds } + } + } + + holdNextFundedRefresh(): HeldBillingRefresh { + let resolveEntered!: () => void + let resolveCompleted!: () => void + const entered = new Promise((resolve) => { + resolveEntered = resolve + }) + const completed = new Promise((resolve) => { + resolveCompleted = resolve + }) + const heldRefresh: NonNullable = { + resolveEntered, + resolveCompleted + } + this.heldRefresh = heldRefresh + + return { + entered, + completed, + release: () => { + if (heldRefresh.releaseRequest === undefined) { + throw new Error('Funded billing refresh has not entered the fixture') + } + heldRefresh.releaseRequest() + } + } + } + + async fulfillStatus(route: Route): Promise { + const response = this.status + const heldRefresh = this.heldRefresh + let completedHeldRefresh: typeof heldRefresh + if (heldRefresh && response.scoped_effective_has_funds?.agent) { + this.heldRefresh = undefined + completedHeldRefresh = heldRefresh + heldRefresh.resolveEntered() + await new Promise((resolve) => { + heldRefresh.releaseRequest = resolve + }) + } + await route.fulfill(jsonRoute(response)) + completedHeldRefresh?.resolveCompleted() + } +} + export const FUNDS_UNAVAILABLE_MESSAGE = 'Billing status is temporarily unavailable; please retry.' const FUNDS_UNAVAILABLE = zAgentAdmissionError.parse({ @@ -160,6 +244,8 @@ async function mockAgentBoot( agentPanelInitiallyOpen, agentOnboardingCompleted, agentRetryAfter, + agentBilling, + acceptedTurns, crdtDebugEnabled, initialFeatureFlags, initialSettings, @@ -215,6 +301,9 @@ async function mockAgentBoot( ) await mockBilling(page) + await page.route('**/api/billing/status', (route) => + agentBilling.fulfillStatus(route) + ) await page.route( 'https://media.comfy.org/website/comfy-agent/**', (route) => { @@ -391,6 +480,7 @@ async function mockAgentBoot( ? TURN_ID : `${TURN_ID}-${postedMessages.length}` } + acceptedTurns.push(accepted) return route.fulfill({ status: 202, contentType: 'application/json', @@ -407,6 +497,8 @@ async function mockAgentBoot( type AgentFixtures = { agentAutoShownReadProbe: boolean + acceptedTurns: AgentTurnAccepted[] + agentBilling: AgentBillingFixture agentConsentAccepted: boolean agentConsentReads: boolean[] agentConsentSave: { status: number; pending?: Promise } @@ -424,6 +516,12 @@ type AgentFixtures = { export const agentTest = comfyPageFixture.extend({ agentAutoShownReadProbe: [false, { option: true }], + acceptedTurns: async ({ agentFlagEnabled: _agentFlagEnabled }, use) => { + await use([]) + }, + agentBilling: async ({ agentFlagEnabled: _agentFlagEnabled }, use) => { + await use(new AgentBillingFixture()) + }, agentConsentAccepted: [true, { option: true }], agentConsentReads: async ({ agentFlagEnabled: _agentFlagEnabled }, use) => { await use([]) @@ -454,6 +552,8 @@ export const agentTest = comfyPageFixture.extend({ agentPanelInitiallyOpen, agentOnboardingCompleted, agentRetryAfter, + agentBilling, + acceptedTurns, crdtDebugEnabled, initialFeatureFlags, initialSettings, @@ -474,6 +574,8 @@ export const agentTest = comfyPageFixture.extend({ agentPanelInitiallyOpen, agentOnboardingCompleted, agentRetryAfter, + agentBilling, + acceptedTurns, crdtDebugEnabled, initialFeatureFlags, initialSettings, diff --git a/src/components/topbar/CurrentUserPopoverLegacy.test.ts b/src/components/topbar/CurrentUserPopoverLegacy.test.ts index 15e0b2f62d2..edd1722625e 100644 --- a/src/components/topbar/CurrentUserPopoverLegacy.test.ts +++ b/src/components/topbar/CurrentUserPopoverLegacy.test.ts @@ -65,6 +65,7 @@ function makeSubscription( endDate: null, isCancelled: false, hasFunds: true, + agentHasFunds: true, ...overrides } } diff --git a/src/composables/billing/types.ts b/src/composables/billing/types.ts index 6beb096ae7a..772ed7e116d 100644 --- a/src/composables/billing/types.ts +++ b/src/composables/billing/types.ts @@ -32,6 +32,8 @@ export interface SubscriptionInfo { endDate: string | null isCancelled: boolean hasFunds: boolean + /** Agent funds across shared credits and the Agent-scoped balance. */ + agentHasFunds: boolean } export interface BalanceInfo { diff --git a/src/composables/billing/useBillingContext.test.ts b/src/composables/billing/useBillingContext.test.ts index e91d77f6adc..feb43f4aca0 100644 --- a/src/composables/billing/useBillingContext.test.ts +++ b/src/composables/billing/useBillingContext.test.ts @@ -18,6 +18,8 @@ import { prepareChurnkey } from '@/platform/cloud/churnkey/churnkeyClient' import { launchCancellationFlow } from '@/platform/cloud/subscription/launchCancellationFlow' import { reportError } from '@/platform/telemetry/reportError' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' +import { useAgentDockMount } from '@/workbench/extensions/agent/composables/useAgentDockMount' +import { useAgentPanelStore } from '@/workbench/extensions/agent/stores/agent/agentPanelStore' import { workspaceApi } from '@/platform/workspace/api/workspaceApi' import type { @@ -224,10 +226,39 @@ describe('useBillingContext', () => { renewalDate: '2025-01-01T00:00:00Z', endDate: null, isCancelled: false, - hasFunds: true + hasFunds: true, + agentHasFunds: true }) }) + it('re-arms the exhaustion impression after workspace-scoped Agent funds recover while the dock is closed', async () => { + vi.stubGlobal('__DISTRIBUTION__', 'cloud') + mockBillingRail.value = 'stripe' + mockBillingStatus.value = { + ...DEFAULT_BILLING_STATUS, + has_funds: false, + scoped_effective_has_funds: { agent: false } + } + const scope = effectScope() + onTestFinished(() => scope.stop()) + const billing = scope.run(useSharedBillingContext) + assert.exists(billing) + const dock = scope.run(useAgentDockMount) + assert.exists(dock) + const agentPanelStore = useAgentPanelStore() + agentPanelStore.isOpen = false + agentPanelStore.reportedExhaustionIdentity = 'account-a:workspace-a' + + await billing.fetchStatus() + expect(dock.docked.value).toBe(false) + + mockBillingStatus.value.scoped_effective_has_funds = { agent: true } + await billing.fetchStatus() + await nextTick() + + expect(agentPanelStore.reportedExhaustionIdentity).toBeNull() + }) + describe('canRunWorkflows', () => { it('is true when not on free tier', async () => { remoteConfigState.value = 'authenticated' diff --git a/src/composables/billing/useLegacyBilling.ts b/src/composables/billing/useLegacyBilling.ts index 99595634545..11f45d0dc98 100644 --- a/src/composables/billing/useLegacyBilling.ts +++ b/src/composables/billing/useLegacyBilling.ts @@ -62,6 +62,7 @@ export function useLegacyBilling(): BillingState & BillingActions { const maxSeats = computed(() => null) const occupiedSeats = computed(() => null) + const hasFunds = computed(() => (authStore.balance?.amount_micros ?? 0) > 0) const subscription = computed(() => { if (!legacyCanAccessSubscriptionFeatures.value && !subscriptionTier.value) { return null @@ -76,7 +77,8 @@ export function useLegacyBilling(): BillingState & BillingActions { renewalDate: legacySubscriptionStatus.value?.renewal_date ?? null, endDate: legacySubscriptionStatus.value?.cancel_at ?? null, isCancelled: isCancelled.value, - hasFunds: (authStore.balance?.amount_micros ?? 0) > 0 + hasFunds: hasFunds.value, + agentHasFunds: hasFunds.value } }) diff --git a/src/composables/billing/useNextInvoice.test.ts b/src/composables/billing/useNextInvoice.test.ts index 65937019756..d85e5a4d1ff 100644 --- a/src/composables/billing/useNextInvoice.test.ts +++ b/src/composables/billing/useNextInvoice.test.ts @@ -22,6 +22,7 @@ function makeSubscription( endDate: null, isCancelled: false, hasFunds: true, + agentHasFunds: true, ...overrides } } diff --git a/src/platform/cloud/subscription/components/CreditsTile.test.ts b/src/platform/cloud/subscription/components/CreditsTile.test.ts index f9e867e9f9e..b9de255b4ec 100644 --- a/src/platform/cloud/subscription/components/CreditsTile.test.ts +++ b/src/platform/cloud/subscription/components/CreditsTile.test.ts @@ -174,6 +174,7 @@ describe('CreditsTile', () => { endDate: null, isCancelled: false, hasFunds: true, + agentHasFunds: true, ...state.subscription } : null diff --git a/src/platform/cloud/subscription/components/PricingTable.test.ts b/src/platform/cloud/subscription/components/PricingTable.test.ts index 73f1a8e2541..b450514bd7a 100644 --- a/src/platform/cloud/subscription/components/PricingTable.test.ts +++ b/src/platform/cloud/subscription/components/PricingTable.test.ts @@ -196,7 +196,8 @@ beforeEach(() => { renewalDate: null, endDate: null, isCancelled: false, - hasFunds: true + hasFunds: true, + agentHasFunds: true } : null ) diff --git a/src/platform/cloud/subscription/composables/useSubscriptionDialog.test.ts b/src/platform/cloud/subscription/composables/useSubscriptionDialog.test.ts index 5dd93fe262e..50cd81d7627 100644 --- a/src/platform/cloud/subscription/composables/useSubscriptionDialog.test.ts +++ b/src/platform/cloud/subscription/composables/useSubscriptionDialog.test.ts @@ -134,6 +134,7 @@ beforeEach(() => { endDate: null, isCancelled: false, hasFunds: true, + agentHasFunds: true, ...mockSubscription.value } : null diff --git a/src/platform/cloud/subscription/launchCancellationFlow.test.ts b/src/platform/cloud/subscription/launchCancellationFlow.test.ts index 4bd0175f135..bd7c0b8635f 100644 --- a/src/platform/cloud/subscription/launchCancellationFlow.test.ts +++ b/src/platform/cloud/subscription/launchCancellationFlow.test.ts @@ -70,6 +70,7 @@ beforeEach(() => { renewalDate: null, isCancelled: false, hasFunds: true, + agentHasFunds: true, ...mocks.subscription.value } : null diff --git a/src/platform/telemetry/TelemetryRegistry.test.ts b/src/platform/telemetry/TelemetryRegistry.test.ts index d9697f21ddc..dca9a90d497 100644 --- a/src/platform/telemetry/TelemetryRegistry.test.ts +++ b/src/platform/telemetry/TelemetryRegistry.test.ts @@ -11,6 +11,8 @@ import type { AgentPanelClosedMetadata, AgentPanelOpenedMetadata, AgentStarterPromptClickedMetadata, + AgentPaywallCtaMetadata, + AgentPaywallShownMetadata, AgentWorkflowAppliedMetadata, BillingTelemetryEvent, CheckoutJourneyTelemetryEvent, @@ -322,6 +324,14 @@ describe('TelemetryRegistry', () => { workflow_id: 'w1', target: 'active_tab_open' } satisfies AgentWorkflowAppliedMetadata + const paywallShownMetadata = { + reason: 'subscription_inactive', + surface: 'credits_exhausted' + } satisfies AgentPaywallShownMetadata + const paywallCtaMetadata = { + cta: 'add_credits', + surface: 'refused_send' + } satisfies AgentPaywallCtaMetadata const cases: Array<{ method: keyof TelemetryProvider & `trackAgent${string}` @@ -438,6 +448,18 @@ describe('TelemetryRegistry', () => { expected: { reason: 'app_mode' }, invoke: (registry) => registry.trackAgentOnboardingNotShown({ reason: 'app_mode' }) + }, + { + method: 'trackAgentPaywallShown', + expected: { ...paywallShownMetadata }, + invoke: (registry) => + registry.trackAgentPaywallShown(paywallShownMetadata) + }, + { + method: 'trackAgentPaywallCtaClicked', + expected: { ...paywallCtaMetadata }, + invoke: (registry) => + registry.trackAgentPaywallCtaClicked(paywallCtaMetadata) } ] diff --git a/src/platform/telemetry/TelemetryRegistry.ts b/src/platform/telemetry/TelemetryRegistry.ts index bdb9a2e656b..e12e5842a58 100644 --- a/src/platform/telemetry/TelemetryRegistry.ts +++ b/src/platform/telemetry/TelemetryRegistry.ts @@ -13,6 +13,8 @@ import type { AgentOnboardingNotShownMetadata, AgentPanelClosedMetadata, AgentPanelOpenedMetadata, + AgentPaywallCtaMetadata, + AgentPaywallShownMetadata, AgentWorkflowAppliedMetadata, AuthErrorMetadata, AuthMetadata, @@ -205,6 +207,16 @@ export class TelemetryRegistry implements TelemetryDispatcher { this.dispatch((provider) => provider.trackCheckoutJourneyEvent?.(event)) } + trackAgentPaywallShown(metadata: AgentPaywallShownMetadata): void { + this.dispatch((provider) => provider.trackAgentPaywallShown?.(metadata)) + } + + trackAgentPaywallCtaClicked(metadata: AgentPaywallCtaMetadata): void { + this.dispatch((provider) => + provider.trackAgentPaywallCtaClicked?.(metadata) + ) + } + trackRunButton(properties: RunButtonProperties): void { this.dispatch((provider) => provider.trackRunButton?.(properties)) } diff --git a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts index 4ff19937507..60072794c0f 100644 --- a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts +++ b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.test.ts @@ -735,6 +735,37 @@ describe('PostHogTelemetryProvider', () => { } ) + it.for([ + { + event: TelemetryEvents.AGENT_PAYWALL_SHOWN, + track: (provider: PostHogTelemetryProvider) => + provider.trackAgentPaywallShown({ + reason: 'subscription_inactive', + surface: 'credits_exhausted' + }), + properties: { + reason: 'subscription_inactive', + surface: 'credits_exhausted' + } + }, + { + event: TelemetryEvents.AGENT_PAYWALL_CTA_CLICKED, + track: (provider: PostHogTelemetryProvider) => + provider.trackAgentPaywallCtaClicked({ + cta: 'add_credits', + surface: 'refused_send' + }), + properties: { cta: 'add_credits', surface: 'refused_send' } + } + ])('captures $event', async ({ event, track, properties }) => { + const provider = createProvider() + await vi.dynamicImportSettled() + + track(provider) + + expect(hoisted.mockCapture).toHaveBeenCalledWith(event, properties) + }) + it('captures resubscribe clicks with their source', async () => { const provider = createProvider() await vi.dynamicImportSettled() diff --git a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.ts b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.ts index b76651d8c1e..a6f47e9b02a 100644 --- a/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.ts +++ b/src/platform/telemetry/providers/cloud/PostHogTelemetryProvider.ts @@ -26,6 +26,8 @@ import type { AgentOnboardingNotShownMetadata, AgentPanelClosedMetadata, AgentPanelOpenedMetadata, + AgentPaywallCtaMetadata, + AgentPaywallShownMetadata, AgentWorkflowAppliedMetadata, AuthErrorMetadata, AuthMetadata, @@ -453,6 +455,14 @@ export class PostHogTelemetryProvider implements TelemetryProvider { this.trackEvent(TelemetryEvents.ADD_API_CREDIT_BUTTON_CLICKED, metadata) } + trackAgentPaywallShown(metadata: AgentPaywallShownMetadata): void { + this.trackEvent(TelemetryEvents.AGENT_PAYWALL_SHOWN, metadata) + } + + trackAgentPaywallCtaClicked(metadata: AgentPaywallCtaMetadata): void { + this.trackEvent(TelemetryEvents.AGENT_PAYWALL_CTA_CLICKED, metadata) + } + trackBeginCheckout(metadata: BeginCheckoutMetadata): void { this.trackEvent(TelemetryEvents.BEGIN_CHECKOUT, metadata) } diff --git a/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts b/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts index 915594877c3..ec987ef0ab6 100644 --- a/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts +++ b/src/platform/telemetry/providers/host/HostTelemetrySink.test.ts @@ -155,6 +155,30 @@ describe('HostTelemetrySink', () => { ) }) + it('forwards agent paywall impressions with their reason and surface', () => { + new HostTelemetrySink().trackAgentPaywallShown({ + reason: 'subscription_inactive', + surface: 'credits_exhausted' + }) + + expect(state.capture).toHaveBeenCalledExactlyOnceWith( + TelemetryEvents.AGENT_PAYWALL_SHOWN, + { reason: 'subscription_inactive', surface: 'credits_exhausted' } + ) + }) + + it('forwards agent paywall CTA clicks with their cta and surface', () => { + new HostTelemetrySink().trackAgentPaywallCtaClicked({ + cta: 'add_credits', + surface: 'refused_send' + }) + + expect(state.capture).toHaveBeenCalledExactlyOnceWith( + TelemetryEvents.AGENT_PAYWALL_CTA_CLICKED, + { cta: 'add_credits', surface: 'refused_send' } + ) + }) + it('forwards canonical billing events using the derived name and payload', () => { new HostTelemetrySink().trackBillingEvent({ operation: 'operation', diff --git a/src/platform/telemetry/providers/host/HostTelemetrySink.ts b/src/platform/telemetry/providers/host/HostTelemetrySink.ts index 586f04bd984..16c3145ea8c 100644 --- a/src/platform/telemetry/providers/host/HostTelemetrySink.ts +++ b/src/platform/telemetry/providers/host/HostTelemetrySink.ts @@ -16,6 +16,8 @@ import type { AgentOnboardingNotShownMetadata, AgentPanelClosedMetadata, AgentPanelOpenedMetadata, + AgentPaywallCtaMetadata, + AgentPaywallShownMetadata, AgentWorkflowAppliedMetadata, AuthMetadata, BeginCheckoutMetadata, @@ -166,6 +168,14 @@ export class HostTelemetrySink implements TelemetryProvider { this.capture(TelemetryEvents.ADD_API_CREDIT_BUTTON_CLICKED, metadata) } + trackAgentPaywallShown(metadata: AgentPaywallShownMetadata): void { + this.capture(TelemetryEvents.AGENT_PAYWALL_SHOWN, metadata) + } + + trackAgentPaywallCtaClicked(metadata: AgentPaywallCtaMetadata): void { + this.capture(TelemetryEvents.AGENT_PAYWALL_CTA_CLICKED, metadata) + } + trackApiCreditTopupButtonPurchaseClicked(amount: number): void { this.capture(TelemetryEvents.API_CREDIT_TOPUP_BUTTON_PURCHASE_CLICKED, { credit_amount: amount diff --git a/src/platform/telemetry/types.ts b/src/platform/telemetry/types.ts index a3928dbeb3e..a360bb7c050 100644 --- a/src/platform/telemetry/types.ts +++ b/src/platform/telemetry/types.ts @@ -982,6 +982,43 @@ export interface AddCreditsClickMetadata { | 'avatar_menu' | 'settings_billing_panel' | 'deep_link' + | 'agent_paywall' +} + +export type AgentPaywallReason = + | 'no_funds' + | 'subscription_inactive' + | 'member_cannot_pay' + | 'sales_managed' + | 'unknown' + +/** + * Which moment put the paywall in front of the user. The two are not + * interchangeable and collapsing them made the funnel unreadable: + * + * - `refused_send` is reactive — a turn POST came back 402/`no_funds`, so the + * user had to compose and send a message to discover they could not. + * - `credits_exhausted` is standing — the client already knows the workspace + * has no funds and says so beside the composer, without a refusal first. + * + * Reported because `app:agent_paywall_shown` alone cannot tell a rise in + * impressions caused by the standing surface from one caused by more users + * being refused. Without the split, "the paywall is showing more" is + * ambiguous between the fix working and the product getting worse. + */ +export type AgentPaywallSurface = 'refused_send' | 'credits_exhausted' + +export interface AgentPaywallShownMetadata { + reason: AgentPaywallReason + surface: AgentPaywallSurface +} + +export type AgentPaywallCta = 'subscribe' | 'add_credits' | 'upgrade' + +export interface AgentPaywallCtaMetadata { + cta: AgentPaywallCta + /** The surface whose impression this click follows. */ + surface: AgentPaywallSurface } export interface SubscriptionCancellationMetadata { @@ -1458,6 +1495,9 @@ export interface TelemetryProvider { /** Emit a checkout-journey lifecycle event to this provider. */ trackCheckoutJourneyEvent?(event: CheckoutJourneyTelemetryEvent): void + trackAgentPaywallShown?(metadata: AgentPaywallShownMetadata): void + trackAgentPaywallCtaClicked?(metadata: AgentPaywallCtaMetadata): void + // Survey flow events trackSurvey?(stage: 'opened' | 'submitted', responses?: SurveyResponses): void @@ -1618,6 +1658,9 @@ export const TelemetryEvents = { WORKSPACE_INVITE_FAILED: 'app:workspace_invite_failed', BEGIN_CHECKOUT: 'begin_checkout', + AGENT_PAYWALL_SHOWN: 'app:agent_paywall_shown', + AGENT_PAYWALL_CTA_CLICKED: 'app:agent_paywall_cta_clicked', + // Canonical Billing Lifecycle BILLING_SUBSCRIPTION_CHECKOUT_STARTED: 'billing.subscription_checkout.started', @@ -1839,4 +1882,6 @@ export type TelemetryEventProperties = | WorkspaceInviteFailedMetadata | BillingTelemetryEvent | CheckoutJourneyTelemetryEventPayload + | AgentPaywallShownMetadata + | AgentPaywallCtaMetadata | FetchTimeoutMetadata diff --git a/src/platform/workspace/components/SubscriptionPanelContentWorkspace.test.ts b/src/platform/workspace/components/SubscriptionPanelContentWorkspace.test.ts index 2fdf0954c83..5aaccd42640 100644 --- a/src/platform/workspace/components/SubscriptionPanelContentWorkspace.test.ts +++ b/src/platform/workspace/components/SubscriptionPanelContentWorkspace.test.ts @@ -159,7 +159,8 @@ const mockSubscription = computed(() => renewalDate: mockRenewalDate.value, endDate: mockEndDate.value, isCancelled: mockSubscriptionStatus.value === 'canceled', - hasFunds: true + hasFunds: true, + agentHasFunds: true } : null ) diff --git a/src/platform/workspace/components/SubscriptionTransitionPreviewWorkspace.stories.ts b/src/platform/workspace/components/SubscriptionTransitionPreviewWorkspace.stories.ts index 31826dda169..a5af7900a4e 100644 --- a/src/platform/workspace/components/SubscriptionTransitionPreviewWorkspace.stories.ts +++ b/src/platform/workspace/components/SubscriptionTransitionPreviewWorkspace.stories.ts @@ -77,7 +77,8 @@ function cancelledSubscription( renewalDate: null, endDate: CANCEL_DATE, isCancelled: true, - hasFunds: true + hasFunds: true, + agentHasFunds: true } } @@ -90,7 +91,8 @@ const notCancelledSubscription: SubscriptionInfo = { renewalDate: NEXT_MONTHLY_RENEWAL, endDate: null, isCancelled: false, - hasFunds: true + hasFunds: true, + agentHasFunds: true } function story( diff --git a/src/platform/workspace/components/dialogs/settings/BillingStatusBanner.stories.ts b/src/platform/workspace/components/dialogs/settings/BillingStatusBanner.stories.ts index d22305c4bbc..f091966ef67 100644 --- a/src/platform/workspace/components/dialogs/settings/BillingStatusBanner.stories.ts +++ b/src/platform/workspace/components/dialogs/settings/BillingStatusBanner.stories.ts @@ -48,7 +48,8 @@ const teamSubscription: SubscriptionInfo = { renewalDate: RENEWAL_DATE, endDate: null, isCancelled: false, - hasFunds: true + hasFunds: true, + agentHasFunds: true } const funded = teamSubscription diff --git a/src/platform/workspace/composables/useWorkspaceBilling.test.ts b/src/platform/workspace/composables/useWorkspaceBilling.test.ts index 575bebe8e53..a4b07ca2be9 100644 --- a/src/platform/workspace/composables/useWorkspaceBilling.test.ts +++ b/src/platform/workspace/composables/useWorkspaceBilling.test.ts @@ -297,6 +297,8 @@ describe('useWorkspaceBilling', () => { it('maps status response into subscription info', async () => { mockWorkspaceApi.getBillingStatus.mockResolvedValue({ ...activeStatus, + scoped_effective_has_funds: { agent: false }, + scoped_has_funds: { agent: false }, billing_rail: 'stripe', subscription_status: 'canceled', cancel_at: '2026-06-01T00:00:00Z' @@ -313,7 +315,8 @@ describe('useWorkspaceBilling', () => { renewalDate: '2026-05-01T00:00:00Z', endDate: '2026-06-01T00:00:00Z', isCancelled: true, - hasFunds: true + hasFunds: true, + agentHasFunds: false }) expect(billing.canAccessSubscriptionFeatures.value).toBe(true) expect(billing.isFreeTier.value).toBe(false) @@ -322,6 +325,30 @@ describe('useWorkspaceBilling', () => { ).toHaveBeenCalledWith('workspace-1', 'stripe') }) + it.for([ + { sharedFunds: true, scopedAgentFunds: undefined, expected: true }, + { sharedFunds: false, scopedAgentFunds: undefined, expected: false }, + { sharedFunds: true, scopedAgentFunds: false, expected: false }, + { sharedFunds: false, scopedAgentFunds: true, expected: true } + ])( + 'maps shared=$sharedFunds and scoped Agent=$scopedAgentFunds to $expected', + async ({ sharedFunds, scopedAgentFunds, expected }) => { + mockWorkspaceApi.getBillingStatus.mockResolvedValue({ + ...activeStatus, + has_funds: sharedFunds, + scoped_effective_has_funds: + scopedAgentFunds === undefined + ? undefined + : { agent: scopedAgentFunds } + }) + + const billing = setupBilling() + await billing.fetchStatus() + + expect(billing.subscription.value?.agentHasFunds).toBe(expected) + } + ) + it('maps a scheduled plan change into subscription info', async () => { const scheduledChange = { plan_slug: 'team-annual', diff --git a/src/platform/workspace/composables/useWorkspaceBilling.ts b/src/platform/workspace/composables/useWorkspaceBilling.ts index 0d5fd2192b9..dad8805d983 100644 --- a/src/platform/workspace/composables/useWorkspaceBilling.ts +++ b/src/platform/workspace/composables/useWorkspaceBilling.ts @@ -216,7 +216,9 @@ export function useWorkspaceBilling(): WorkspaceBilling { renewalDate: status.renewal_date ?? null, endDate: status.cancel_at ?? null, isCancelled: status.subscription_status === 'canceled', - hasFunds: status.has_funds + hasFunds: status.has_funds, + agentHasFunds: + status.scoped_effective_has_funds?.agent ?? status.has_funds } }) diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 0a02136c95c..02ea7f26cda 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -3,13 +3,15 @@ import { fromPartial } from '@total-typescript/shoehorn' import type { AgentThreadListResponse, AgentThreadSummary, + BillingStatus, SubscriptionTier } from '@comfyorg/ingest-types' import { render, screen, waitFor, within } from '@testing-library/vue' import userEvent from '@testing-library/user-event' import { assert, beforeEach, describe, expect, it, vi } from 'vitest' import type { Mocked } from 'vitest' -import { computed, defineComponent, h, nextTick, ref } from 'vue' +import { computed, defineComponent, h, nextTick, reactive, ref } from 'vue' +import type { Ref } from 'vue' import { useClipboard } from '@vueuse/core' vi.mock(import('firebase/auth')) @@ -200,16 +202,21 @@ vi.mock( const paywallWorkspace = vi.hoisted(() => ({ role: 'owner' as 'owner' | 'member' | undefined })) -const paywallCapabilities = vi.hoisted(() => ({ +const paywallCapabilities = reactive({ canTopUp: true, canSubscribeSelfServe: true, isReady: true, - hasResolvedCapabilities: true -})) + hasResolvedCapabilities: true, + snapshotAuthoritative: true +}) const paywallBilling = vi.hoisted(() => ({ - tier: 'STANDARD' as SubscriptionTier | null + tier: 'STANDARD' as SubscriptionTier | null, + type: 'workspace' as 'workspace' | 'legacy', + status: 'paid' as BillingStatus | null, + fetchStatus: vi.fn<() => Promise>() })) const paywallHasFunds = ref(false) +const paywallAgentHasFunds = ref() vi.mock(import('@/platform/workspace/composables/useWorkspaceUI'), { spy: true @@ -288,9 +295,15 @@ beforeEach(() => { subscription: computed(() => paywallHasFunds.value === null ? null - : fromPartial({ hasFunds: paywallHasFunds.value }) + : fromPartial({ + hasFunds: paywallHasFunds.value, + agentHasFunds: paywallAgentHasFunds.value ?? paywallHasFunds.value + }) ), - tier: computed(() => paywallBilling.tier) + tier: computed(() => paywallBilling.tier), + type: computed(() => paywallBilling.type), + billingStatus: computed(() => paywallBilling.status), + fetchStatus: paywallBilling.fetchStatus }) ) vi.mocked(useBillingCapabilities).mockReturnValue( @@ -302,6 +315,9 @@ beforeEach(() => { isReady: computed(() => paywallCapabilities.isReady), hasResolvedCapabilities: computed( () => paywallCapabilities.hasResolvedCapabilities + ), + snapshotAuthoritative: computed( + () => paywallCapabilities.snapshotAuthoritative ) }) ) @@ -388,7 +404,11 @@ beforeEach(() => { paywallCapabilities.isReady = true paywallCapabilities.hasResolvedCapabilities = true paywallBilling.tier = 'STANDARD' + paywallBilling.type = 'workspace' + paywallBilling.status = 'paid' + paywallBilling.fetchStatus.mockReset().mockResolvedValue(undefined) paywallHasFunds.value = false + paywallAgentHasFunds.value = undefined }) const zAgentWsEventForTest = (raw: unknown): AgentChatEvent => @@ -731,6 +751,35 @@ describe('AgentPanelRoot onboarding', () => { }) }) +/** + * The transcript, for assertions that mean the INLINE paywall card - the one + * rendered against the turn a 402 refused. The panel now also has a standing + * credits-exhausted card beside the composer, and both render the same + * component with the same copy, so an unscoped + * `getByRole('button', { name: 'Subscribe' })` no longer says which it found. + */ +function inlinePaywall() { + return within(screen.getByTestId('agent-conversation')) +} + +function findInlinePaywallButton(name: string): Promise { + return vi.waitFor(() => inlinePaywall().getByRole('button', { name })) +} + +function queryInlinePaywallButton(name: string): HTMLElement | null { + const view = screen.queryByTestId('agent-conversation') + return view ? within(view).queryByRole('button', { name }) : null +} + +function findInlinePaywallText(text: string): Promise { + return vi.waitFor(() => inlinePaywall().getByText(text)) +} + +function queryInlinePaywallText(text: string): HTMLElement | null { + const view = screen.queryByTestId('agent-conversation') + return view ? within(view).queryByText(text) : null +} + describe('AgentPanelRoot paywall actions', () => { beforeEach(() => { ws.clear() @@ -747,14 +796,14 @@ describe('AgentPanelRoot paywall actions', () => { thinking: false }) - await userEvent.click( - await screen.findByRole('button', { name: 'Upgrade plan' }) - ) + await userEvent.click(await findInlinePaywallButton('Upgrade plan')) expect(openAccountPrecondition).toHaveBeenCalledExactlyOnceWith( 'subscription' ) - await userEvent.click(screen.getByRole('button', { name: 'Add credits' })) + await userEvent.click( + inlinePaywall().getByRole('button', { name: 'Add credits' }) + ) expect(openAccountPrecondition.mock.calls).toEqual([ ['subscription'], ['credits'] @@ -772,9 +821,7 @@ describe('AgentPanelRoot paywall actions', () => { thinking: false }) - await userEvent.click( - await screen.findByRole('button', { name: 'Subscribe' }) - ) + await userEvent.click(await findInlinePaywallButton('Subscribe')) expect(openAccountPrecondition).toHaveBeenCalledExactlyOnceWith( 'subscription' @@ -789,14 +836,12 @@ describe('AgentPanelRoot paywall actions', () => { 'continue' ) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() paywallHasFunds.value = true await vi.waitFor(() => - expect(screen.queryByText('Out of credits')).not.toBeInTheDocument() + expect(queryInlinePaywallText('Out of credits')).not.toBeInTheDocument() ) }) @@ -807,16 +852,14 @@ describe('AgentPanelRoot paywall actions', () => { toTurnId('msg-paywall'), 'continue' ) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() panel.unmount() paywallHasFunds.value = true render(AgentPanelRoot, { global: { plugins: [i18n] } }) await vi.waitFor(() => - expect(screen.queryByText('Out of credits')).not.toBeInTheDocument() + expect(queryInlinePaywallText('Out of credits')).not.toBeInTheDocument() ) }) @@ -831,9 +874,7 @@ describe('AgentPanelRoot paywall actions', () => { 'render one more frame' ) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() }) it('keeps a resolved paywall hidden while billing state is unknown', async () => { @@ -843,20 +884,16 @@ describe('AgentPanelRoot paywall actions', () => { toTurnId('msg-paywall'), 'continue' ) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() paywallHasFunds.value = true await vi.waitFor(() => - expect( - screen.queryByRole('button', { name: 'Subscribe' }) - ).not.toBeInTheDocument() + expect(queryInlinePaywallButton('Subscribe')).not.toBeInTheDocument() ) paywallHasFunds.value = null await nextTick() - expect(screen.queryByText('Out of credits')).not.toBeInTheDocument() + expect(queryInlinePaywallText('Out of credits')).not.toBeInTheDocument() }) it('shows only a new denial after funds run out again', async () => { @@ -866,20 +903,16 @@ describe('AgentPanelRoot paywall actions', () => { toTurnId('msg-paywall'), 'continue' ) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() paywallHasFunds.value = true await vi.waitFor(() => - expect( - screen.queryByRole('button', { name: 'Subscribe' }) - ).not.toBeInTheDocument() + expect(queryInlinePaywallButton('Subscribe')).not.toBeInTheDocument() ) paywallHasFunds.value = false await nextTick() - expect(screen.queryByText('Out of credits')).not.toBeInTheDocument() + expect(queryInlinePaywallText('Out of credits')).not.toBeInTheDocument() useAgentConversationStore().recordPaywall( toTurnId('msg-paywall-2'), @@ -887,7 +920,7 @@ describe('AgentPanelRoot paywall actions', () => { ) await vi.waitFor(() => expect( - screen.getByRole('button', { name: 'Subscribe' }) + inlinePaywall().getByRole('button', { name: 'Subscribe' }) ).toBeInTheDocument() ) }) @@ -908,9 +941,7 @@ describe('AgentPanelRoot paywall actions', () => { await screen.findByText( 'This workspace has used all its credits. Ask your workspace owner to add more.' ) - expect( - screen.queryByRole('button', { name: 'Add credits' }) - ).not.toBeInTheDocument() + expect(queryInlinePaywallButton('Add credits')).not.toBeInTheDocument() expect( screen.queryByRole('button', { name: /upgrade|subscribe/i }) ).not.toBeInTheDocument() @@ -929,9 +960,7 @@ describe('AgentPanelRoot paywall actions', () => { thinking: false }) - expect( - await screen.findByRole('button', { name: 'Add credits' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Add credits')).toBeInTheDocument() expect( screen.queryByRole('button', { name: /upgrade|subscribe/i }) ).not.toBeInTheDocument() @@ -949,9 +978,7 @@ describe('AgentPanelRoot paywall actions', () => { thinking: false }) - expect( - await screen.findByRole('button', { name: 'Add credits' }) - ).toBeInTheDocument() + expect(await findInlinePaywallButton('Add credits')).toBeInTheDocument() expect( screen.queryByRole('button', { name: /upgrade|subscribe/i }) ).not.toBeInTheDocument() @@ -968,12 +995,8 @@ describe('AgentPanelRoot paywall actions', () => { thinking: false }) - expect( - await screen.findByRole('button', { name: 'Subscribe' }) - ).toBeInTheDocument() - expect( - screen.queryByRole('button', { name: 'Add credits' }) - ).not.toBeInTheDocument() + expect(await findInlinePaywallButton('Subscribe')).toBeInTheDocument() + expect(queryInlinePaywallButton('Add credits')).not.toBeInTheDocument() }) it('shows sales-managed remediation for a ready owner with no self-serve capability', async () => { @@ -989,13 +1012,11 @@ describe('AgentPanelRoot paywall actions', () => { }) expect( - await screen.findByText( + await findInlinePaywallText( 'This workspace is billed through your Comfy account team. Contact them to add credits.' ) ).toBeInTheDocument() - expect( - screen.queryByRole('button', { name: 'Add credits' }) - ).not.toBeInTheDocument() + expect(queryInlinePaywallButton('Add credits')).not.toBeInTheDocument() expect( screen.queryByRole('button', { name: /upgrade|subscribe/i }) ).not.toBeInTheDocument() @@ -1042,6 +1063,811 @@ describe('AgentPanelRoot paywall actions', () => { }) }) +describe('AgentPanelRoot paywall telemetry', () => { + let canTopUp: Ref + let canSubscribeSelfServe: Ref + let hasResolvedCapabilities: Ref + let capabilityReadSettled: Ref + let snapshotAuthoritative: Ref + let workspaceRole: Ref<'owner' | 'member' | undefined> + let tier: Ref + let hasFunds: Ref + + beforeEach(() => { + ws.clear() + openAccountPrecondition.mockClear() + vi.mocked(useTelemetry())!.trackAgentPaywallShown.mockClear() + vi.mocked(useTelemetry())!.trackAgentPaywallCtaClicked.mockClear() + vi.mocked(useTelemetry())!.trackAddApiCreditButtonClicked.mockClear() + + canTopUp = ref(true) + canSubscribeSelfServe = ref(true) + hasResolvedCapabilities = ref(true) + capabilityReadSettled = ref(true) + snapshotAuthoritative = ref(true) + workspaceRole = ref<'owner' | 'member' | undefined>('owner') + tier = ref('STANDARD') + // Funded, so the standing credits-exhausted card stays off and every + // assertion below is unambiguously about the refusal-driven inline card. + // The standing surface has its own describe. + hasFunds = ref(true) + + vi.mocked(useBillingCapabilities).mockReturnValue( + fromPartial({ + canTopUp, + canSubscribeSelfServe, + isReady: capabilityReadSettled, + hasResolvedCapabilities, + snapshotAuthoritative + }) + ) + vi.mocked(useWorkspaceUI).mockReturnValue(fromPartial({ workspaceRole })) + vi.mocked(useBillingContext).mockReturnValue( + fromPartial({ + subscription: computed(() => fromPartial({ hasFunds: hasFunds.value })), + tier, + type: computed(() => 'workspace'), + billingStatus: computed(() => 'paid'), + fetchStatus: vi.fn().mockResolvedValue(undefined) + }) + ) + }) + + function showPaywall(id = 'msg-paywall'): void { + useAgentConversationStore().messages.push({ + id: toTurnId(id), + role: 'assistant', + parts: [{ type: 'paywall' }], + streaming: false, + thinking: false + }) + } + + it.for([ + { + name: 'an owner who can top up', + topUp: true, + selfServe: true, + role: 'owner' as const, + reason: 'no_funds' + }, + { + name: 'an owner who must subscribe first', + topUp: false, + selfServe: true, + role: 'owner' as const, + reason: 'subscription_inactive' + }, + { + name: 'a member without billing permissions', + topUp: false, + selfServe: false, + role: 'member' as const, + reason: 'member_cannot_pay' + }, + { + name: 'a sales-managed workspace', + topUp: false, + selfServe: false, + role: 'owner' as const, + reason: 'sales_managed' + } + ])( + 'reports the paywall shown to $name as $reason', + async ({ topUp, selfServe, role, reason }) => { + canTopUp.value = topUp + canSubscribeSelfServe.value = selfServe + workspaceRole.value = role + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + showPaywall() + + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledWith({ + reason, + surface: 'refused_send' + }) + ) + } + ) + + it('does not report a paywall that was never shown', async () => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(useTelemetry()!.trackAgentPaywallShown).not.toHaveBeenCalled() + }) + + it('does not report the same paywall again after the panel is closed and reopened', async () => { + const panel = render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + panel.unmount() + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + await nextTick() + + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + }) + + it('reports a paywall raised after the panel is closed and reopened', async () => { + const panel = render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall('msg-paywall-1') + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + panel.unmount() + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + showPaywall('msg-paywall-2') + + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(2) + ) + }) + + it('reports one impression per paywall even as billing state re-evaluates', async () => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + tier.value = 'PRO' + await nextTick() + canSubscribeSelfServe.value = false + await nextTick() + canTopUp.value = false + await nextTick() + await nextTick() + + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + }) + + it('reports a second paywall later in the conversation', async () => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall('msg-paywall-1') + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + showPaywall('msg-paywall-2') + + await waitFor(() => + expect(useTelemetry()!.trackAgentPaywallShown).toHaveBeenCalledTimes(2) + ) + }) + + it('withholds the paywall report until the capability read settles, then reports it once', async () => { + canTopUp.value = false + hasResolvedCapabilities.value = false + capabilityReadSettled.value = false + snapshotAuthoritative.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + showPaywall() + await nextTick() + await nextTick() + expect(useTelemetry()!.trackAgentPaywallShown).not.toHaveBeenCalled() + + hasResolvedCapabilities.value = true + capabilityReadSettled.value = true + snapshotAuthoritative.value = true + + await waitFor(() => + expect( + useTelemetry()!.trackAgentPaywallShown + ).toHaveBeenCalledExactlyOnceWith({ + reason: 'subscription_inactive', + surface: 'refused_send' + }) + ) + }) + + it('does not report a confident reason from an unsettled read, and reports the real one once it settles', async () => { + canTopUp.value = true + canSubscribeSelfServe.value = false + hasResolvedCapabilities.value = false + capabilityReadSettled.value = false + snapshotAuthoritative.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + showPaywall() + await nextTick() + await nextTick() + expect(useTelemetry()!.trackAgentPaywallShown).not.toHaveBeenCalled() + + canTopUp.value = false + canSubscribeSelfServe.value = true + hasResolvedCapabilities.value = true + capabilityReadSettled.value = true + snapshotAuthoritative.value = true + + await waitFor(() => + expect( + useTelemetry()!.trackAgentPaywallShown + ).toHaveBeenCalledExactlyOnceWith({ + reason: 'subscription_inactive', + surface: 'refused_send' + }) + ) + }) + + // An unavailable read is settled but not authoritative, and it leaves + // `canTopUp` guessing true for an owner. Gating on authority alone stranded + // these sessions: the card renders, its Add credits CTA is reported below, + // and the funnel saw the click with no impression behind it. + it('reports an impression once when the read settles as unavailable', async () => { + canTopUp.value = true + canSubscribeSelfServe.value = true + hasResolvedCapabilities.value = false + capabilityReadSettled.value = true + snapshotAuthoritative.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + showPaywall() + + await waitFor(() => + expect( + useTelemetry()!.trackAgentPaywallShown + ).toHaveBeenCalledExactlyOnceWith({ + reason: 'unknown', + surface: 'refused_send' + }) + ) + }) + + it('reports a visible paywall as unknown when the read settles without resolving', async () => { + canTopUp.value = false + canSubscribeSelfServe.value = false + hasResolvedCapabilities.value = false + snapshotAuthoritative.value = true + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + showPaywall() + + await waitFor(() => + expect( + useTelemetry()!.trackAgentPaywallShown + ).toHaveBeenCalledExactlyOnceWith({ + reason: 'unknown', + surface: 'refused_send' + }) + ) + }) + + it.for([ + { button: 'Add credits', cta: 'add_credits' }, + { button: 'Upgrade plan', cta: 'upgrade' } + ])('reports the $button CTA as $cta', async ({ button, cta }) => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + + await userEvent.click(await screen.findByRole('button', { name: button })) + + expect( + useTelemetry()!.trackAgentPaywallCtaClicked + ).toHaveBeenCalledExactlyOnceWith({ cta, surface: 'refused_send' }) + }) + + it('reports the Subscribe CTA as subscribe', async () => { + canTopUp.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + + await userEvent.click( + await screen.findByRole('button', { name: 'Subscribe' }) + ) + + expect( + useTelemetry()!.trackAgentPaywallCtaClicked + ).toHaveBeenCalledExactlyOnceWith({ + cta: 'subscribe', + surface: 'refused_send' + }) + }) + + it('attributes the add-credits click to the agent paywall', async () => { + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + + await userEvent.click( + await screen.findByRole('button', { name: 'Add credits' }) + ) + + expect( + useTelemetry()!.trackAddApiCreditButtonClicked + ).toHaveBeenCalledExactlyOnceWith({ source: 'agent_paywall' }) + }) + + it('does not report an add-credits click for a subscribe CTA', async () => { + canTopUp.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + showPaywall() + + await userEvent.click( + await screen.findByRole('button', { name: 'Subscribe' }) + ) + + expect( + useTelemetry()!.trackAddApiCreditButtonClicked + ).not.toHaveBeenCalled() + }) +}) + +/** + * The standing credits-exhausted surface. + * + * Distinct from the describes above, which cover the inline card raised by a + * turn POST that came back 402/`no_funds`. That card was the ONLY route to + * `app:agent_paywall_shown`, so a user who met the ceiling any other way - a + * turn that died at the agent runtime's own LLM hop, or one who simply did not + * type again - got no upgrade path. These cases pin the surface that reads the + * funds signal directly instead of waiting for a refusal to carry it. + */ +describe('AgentPanelRoot standing credits-exhausted paywall', () => { + const STANDING = 'agent-credits-exhausted-paywall' + + beforeEach(() => { + ws.clear() + openAccountPrecondition.mockClear() + telemetry.trackAgentPaywallShown.mockClear() + telemetry.trackAgentPaywallCtaClicked.mockClear() + telemetry.trackAddApiCreditButtonClicked.mockClear() + }) + + it.for([ + { + subscription: null, + effectiveFunds: true + }, + { + subscription: 'STANDARD' as const, + effectiveFunds: true + } + ])( + 'stays hidden for subscription=$subscription with effective funds', + async ({ subscription, effectiveFunds }) => { + paywallBilling.tier = subscription + paywallHasFunds.value = effectiveFunds + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + } + ) + + it.for([ + { + subscription: null, + effectiveFunds: false + }, + { + subscription: 'STANDARD' as const, + effectiveFunds: false + } + ])( + 'shows for subscription=$subscription without effective funds', + async ({ subscription, effectiveFunds }) => { + paywallBilling.tier = subscription + paywallHasFunds.value = effectiveFunds + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + expect(await screen.findByTestId(STANDING)).toBeInTheDocument() + } + ) + + it('shows an upgrade path when the workspace is out of credits, with no refusal first', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + const card = await screen.findByTestId(STANDING) + + expect( + within(card).getByRole('button', { name: 'Add credits' }) + ).toBeInTheDocument() + }) + + it('allows a new turn while the standing card is visible', async () => { + const messageBodies: unknown[] = [] + vi.stubGlobal( + 'fetch', + vi.fn(async (url: string, init?: RequestInit) => { + if (url.endsWith('/api/agent/threads')) + return json(200, agentThreadList()) + messageBodies.push(JSON.parse(String(init?.body))) + return json(202, { thread_id: 'th-1', message_id: 'm-1' }) + }) + ) + paywallHasFunds.value = false + workflowStore.activeWorkflow = addTab('workflows/current.json') + renderWithSelectedTarget() + await screen.findByTestId(STANDING) + + await sendFromComposer('continue anyway') + + expect(messageBodies).toHaveLength(1) + expect(messageBodies[0]).toMatchObject({ content: 'continue anyway' }) + }) + + it('reports the impression against the credits_exhausted surface', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + + await waitFor(() => + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledExactlyOnceWith({ + reason: 'no_funds', + surface: 'credits_exhausted' + }) + ) + }) + + it('attributes its CTA to the credits_exhausted surface and opens the credits flow', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + const card = await screen.findByTestId(STANDING) + + await userEvent.click( + within(card).getByRole('button', { name: 'Add credits' }) + ) + + expect( + telemetry.trackAgentPaywallCtaClicked + ).toHaveBeenCalledExactlyOnceWith({ + cta: 'add_credits', + surface: 'credits_exhausted' + }) + expect(openAccountPrecondition).toHaveBeenCalledExactlyOnceWith('credits') + }) + + it('stays hidden while the workspace has funds', async () => { + paywallHasFunds.value = true + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + expect(telemetry.trackAgentPaywallShown).not.toHaveBeenCalled() + }) + + it('stays hidden while Agent-scoped gratis can fund the turn', async () => { + paywallHasFunds.value = false + paywallAgentHasFunds.value = true + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + expect(telemetry.trackAgentPaywallShown).not.toHaveBeenCalled() + }) + + it('stays hidden on the legacy rail whose unloaded balance reads as false', async () => { + paywallHasFunds.value = false + paywallBilling.type = 'legacy' + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + }) + + it.for(['payment_failed', 'paused'] as const)( + 'defers to the %s billing-recovery surface', + async (status) => { + paywallHasFunds.value = false + paywallBilling.status = status + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + } + ) + + // `null` is "not loaded yet", not "no funds". Treating it as exhaustion would + // flash an out-of-credits card at every funded user on every cold start. + it('stays hidden while the billing read has not answered', async () => { + paywallHasFunds.value = null + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + expect(telemetry.trackAgentPaywallShown).not.toHaveBeenCalled() + }) + + // An unsettled capability read cannot say which remediation is right, and a + // card naming the wrong one is worse than no card. Same gate the inline + // card's impression already used. + it('waits for the capability read to settle before showing anything', async () => { + paywallHasFunds.value = false + paywallCapabilities.isReady = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + await nextTick() + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + expect(telemetry.trackAgentPaywallShown).not.toHaveBeenCalled() + }) + + // `unavailable` renders a body with no action at all, so standing it up would + // be noise the user cannot act on. + it('stays hidden when the presentation offers no remediation', async () => { + paywallHasFunds.value = false + paywallCapabilities.canTopUp = false + paywallCapabilities.hasResolvedCapabilities = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + await nextTick() + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + }) + + it('stays hidden when the current member cannot act on billing', async () => { + paywallHasFunds.value = false + paywallWorkspace.role = 'member' + paywallCapabilities.canTopUp = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + }) + + // Two cards with identical copy is a bug, not redundancy. The inline one is + // anchored to the turn that was refused, so it wins. + it('yields to an inline paywall card rather than duplicating it', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + + useAgentConversationStore().recordPaywall( + toTurnId('msg-paywall'), + 'continue' + ) + + await waitFor(() => + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + ) + expect(await findInlinePaywallButton('Add credits')).toBeInTheDocument() + }) + + it('shows the standing card when a normal reply follows an older unresolved inline card', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + const conversation = useAgentConversationStore() + conversation.recordPaywall(toTurnId('msg-paywall'), 'continue') + await waitFor(() => + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + ) + + conversation.messages.push({ + id: toTurnId('msg-later'), + role: 'assistant', + parts: [{ type: 'text', text: 'Later reply', state: 'done' }], + streaming: false, + thinking: false + }) + + expect(await screen.findByTestId(STANDING)).toBeInTheDocument() + }) + + it('returns after the inline paywall is resolved and credits run out again', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + + useAgentConversationStore().recordPaywall( + toTurnId('msg-paywall'), + 'continue' + ) + await waitFor(() => + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + ) + + paywallHasFunds.value = true + await waitFor(() => + expect(queryInlinePaywallButton('Add credits')).not.toBeInTheDocument() + ) + paywallHasFunds.value = false + + expect(await screen.findByTestId(STANDING)).toBeInTheDocument() + }) + + it('reports only after returning from chat history', async () => { + paywallHasFunds.value = true + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByRole('textbox') + + await userEvent.click( + screen.getByRole('button', { + name: i18n.global.t('agent.showChatHistory') + }) + ) + await screen.findByRole('heading', { name: i18n.global.t('agent.history') }) + + paywallHasFunds.value = false + await nextTick() + expect(telemetry.trackAgentPaywallShown).not.toHaveBeenCalled() + + await userEvent.click( + screen.getByRole('button', { + name: i18n.global.t('agent.backToPreviousChat') + }) + ) + + await waitFor(() => + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledExactlyOnceWith({ + reason: 'no_funds', + surface: 'credits_exhausted' + }) + ) + }) + + it('reports a new identity only after its standing card is shown', async () => { + const accountId = ref('account-a') + useCurrentUser().resolvedUserInfo = computed(() => ({ + id: accountId.value + })) + paywallHasFunds.value = false + const firstPanel = render(AgentPanelRoot, { + global: { plugins: [i18n] } + }) + await screen.findByTestId(STANDING) + await waitFor(() => + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + await userEvent.click( + screen.getByRole('button', { + name: i18n.global.t('agent.showChatHistory') + }) + ) + await screen.findByRole('heading', { name: i18n.global.t('agent.history') }) + accountId.value = 'account-b' + await nextTick() + + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + + await userEvent.click( + screen.getByRole('button', { + name: i18n.global.t('agent.backToPreviousChat') + }) + ) + await waitFor(() => + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledTimes(2) + ) + + firstPanel.unmount() + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledTimes(2) + }) + + it('disappears once funds arrive', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + + paywallHasFunds.value = true + + await waitFor(() => + expect(screen.queryByTestId(STANDING)).not.toBeInTheDocument() + ) + }) + + // One impression per exhaustion episode: the surface is standing, so a + // per-render report would make impressions a function of session length. + it('reports one impression per exhaustion episode, not per re-evaluation', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + await waitFor(() => + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + paywallCapabilities.canSubscribeSelfServe = false + await nextTick() + await nextTick() + + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + }) + + it('does not re-arm the impression when capabilities refresh while still exhausted', async () => { + paywallHasFunds.value = false + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + await waitFor(() => + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + paywallCapabilities.isReady = false + await nextTick() + paywallCapabilities.isReady = true + await screen.findByTestId(STANDING) + + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + }) + + it('does not report the same exhaustion episode after a panel remount', async () => { + paywallHasFunds.value = false + const firstPanel = render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + await waitFor(() => + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + ) + + firstPanel.unmount() + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + await screen.findByTestId(STANDING) + + expect(telemetry.trackAgentPaywallShown).toHaveBeenCalledTimes(1) + }) + it('refreshes workspace billing after an active turn becomes idle', async () => { + paywallHasFunds.value = true + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + const conversation = useAgentConversationStore() + const turnId = toTurnId('billing-refresh') + + conversation.startTurn(turnId) + await nextTick() + paywallBilling.fetchStatus.mockClear() + conversation.ingest( + zAgentWsEventForTest({ + type: 'agent_message_done', + data: { message_id: turnId, thread_id: 'th', usage: null } + }) + ) + + await waitFor(() => + expect(paywallBilling.fetchStatus).toHaveBeenCalledOnce() + ) + }) + + it('does not refresh workspace status for a legacy billing turn', async () => { + paywallHasFunds.value = true + paywallBilling.type = 'legacy' + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + const conversation = useAgentConversationStore() + const turnId = toTurnId('legacy-billing-refresh') + + conversation.startTurn(turnId) + paywallBilling.fetchStatus.mockClear() + conversation.ingest( + zAgentWsEventForTest({ + type: 'agent_message_done', + data: { message_id: turnId, thread_id: 'th', usage: null } + }) + ) + await nextTick() + + expect(paywallBilling.fetchStatus).not.toHaveBeenCalled() + }) + + it('reports a rejected post-turn billing refresh', async () => { + const refreshError = new Error('status unavailable') + paywallHasFunds.value = true + paywallBilling.fetchStatus.mockRejectedValueOnce(refreshError) + render(AgentPanelRoot, { global: { plugins: [i18n] } }) + const conversation = useAgentConversationStore() + const turnId = toTurnId('failed-billing-refresh') + + conversation.startTurn(turnId) + paywallBilling.fetchStatus.mockClear() + conversation.ingest( + zAgentWsEventForTest({ + type: 'agent_message_done', + data: { message_id: turnId, thread_id: 'th', usage: null } + }) + ) + + await waitFor(() => + expect(reportError).toHaveBeenCalledWith(refreshError, { + errorType: 'error_refreshing_agent_billing_status' + }) + ) + }) +}) + describe('AgentPanelRoot session notices', () => { beforeEach(() => { ws.clear() @@ -2298,6 +3124,7 @@ describe('AgentPanelRoot attach flow', () => { ).toBeInTheDocument() executionErrors.showErrorOverlay.mockClear() + vi.mocked(reportError).mockClear() failUpload() await vi.waitFor(() => expect(screen.queryByText('cat.png')).not.toBeInTheDocument() diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 250b8e4360e..4a9046bd689 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -19,7 +19,10 @@ import { useI18n } from 'vue-i18n' import { useCurrentUser } from '@/composables/auth/useCurrentUser' import { useTelemetry } from '@/platform/telemetry' import { reportError } from '@/platform/telemetry/reportError' -import type { AgentErrorMetadata } from '@/platform/telemetry/types' +import type { + AgentErrorMetadata, + AgentPaywallSurface +} from '@/platform/telemetry/types' import { useSettingStore } from '@/platform/settings/settingStore' import { formatWorkflowSyncErrorDetail } from '@/workbench/extensions/agent/crdt/workflowSyncErrorDetail' import { useWorkflowService } from '@/platform/workflow/core/services/workflowService' @@ -107,7 +110,9 @@ import type { DraftSnapshot } from './services/agent/agentRestClient' import type { AgentPaywallAction } from './services/agent/agentPaywallPresentation' import { DEFAULT_AGENT_PAYWALL_PRESENTATION, - resolveAgentPaywallPresentation + resolveAgentPaywallPresentation, + toAgentPaywallCta, + toAgentPaywallReason } from './services/agent/agentPaywallPresentation' import { createAgentEventSource } from './services/agent/agentEventSource' import { createStandaloneAgentEventSource } from './services/agent/standaloneAgentEventSource' @@ -133,17 +138,28 @@ const { t } = useI18n() const toast = useToastStore() const { open: openAccountPrecondition } = useAccountPreconditionDialog() const { workspaceRole } = useWorkspaceUI() -const { subscription, tier: subscriptionTier } = useBillingContext() +const { + subscription, + tier: subscriptionTier, + type: billingType, + billingStatus, + fetchStatus: refreshBillingStatus +} = useBillingContext() const conversationStore = useAgentConversationStore() watch( subscription, (currentSubscription) => { - if (currentSubscription?.hasFunds) conversationStore.resolvePaywalls() + if (currentSubscription?.agentHasFunds) conversationStore.resolvePaywalls() }, { immediate: true } ) -const { canTopUp, canSubscribeSelfServe, hasResolvedCapabilities } = - useBillingCapabilities() +const { + canTopUp, + canSubscribeSelfServe, + hasResolvedCapabilities, + isReady: capabilityReadSettled, + snapshotAuthoritative +} = useBillingCapabilities() const paywallPresentation = computed(() => { if (isCloud && !hasResolvedCapabilities.value && !canTopUp.value) { return DEFAULT_AGENT_PAYWALL_PRESENTATION @@ -160,6 +176,7 @@ const sidebarTabStore = useSidebarTabStore() const { isBuilderMode } = useAppMode() const { resolvedUserInfo, userDisplayName } = useCurrentUser() +const teamWorkspaceStore = useTeamWorkspaceStore() const userName = computed( () => userDisplayName.value?.trim().split(/\s+/)[0] || undefined ) @@ -171,14 +188,150 @@ const events = ? createStandaloneAgentEventSource() : createAgentEventSource(api) -function onPaywallAction(action: AgentPaywallAction): void { - openAccountPrecondition(action === 'addCredits' ? 'credits' : 'subscription') +function onPaywallAction( + action: AgentPaywallAction, + surface: AgentPaywallSurface +): void { + useTelemetry()?.trackAgentPaywallCtaClicked({ + cta: toAgentPaywallCta(action), + surface + }) + if (action === 'addCredits') { + if (canTopUp.value) { + useTelemetry()?.trackAddApiCreditButtonClicked({ + source: 'agent_paywall' + }) + } + openAccountPrecondition('credits') + return + } + openAccountPrecondition('subscription') +} + +const { messages: conversationMessages, entries: conversationEntries } = + storeToRefs(conversationStore) +watch( + () => + // Gated on the read having *settled*, which includes settling as + // unavailable. `snapshotAuthoritative` is narrower — it excludes that + // outage state — and gating on it stranded those sessions: the paywall + // still renders, an owner still gets the fallback Add credits CTA, and its + // click is still reported, so the funnel saw CTAs with no impression. + capabilityReadSettled.value + ? conversationMessages.value + .filter((message) => + message.parts.some((part) => part.type === 'paywall') + ) + .map((message) => message.id) + : [], + (paywallMessageIds) => { + const telemetry = useTelemetry() + if (!telemetry) return + for (const id of paywallMessageIds) { + if (!conversationStore.claimPaywallImpression(id)) continue + telemetry.trackAgentPaywallShown({ + // An unavailable read leaves `canTopUp` guessing true for an owner, so + // the presentation would name a confident reason drawn from a fallback + // rather than from capabilities. Report the impression as `unknown`. + reason: snapshotAuthoritative.value + ? toAgentPaywallReason(paywallPresentation.value) + : 'unknown', + surface: 'refused_send' + }) + } + }, + { immediate: true } +) + +/** + * The standing credit-exhaustion surface. + * + * The inline card above is reachable from exactly one moment: a turn POST that + * came back 402/`no_funds` (`recordSendError` -> `recordPaywall`). Every other + * way a user meets the ceiling — including a refusal inside a server-side LLM + * hop that the browser cannot observe — needs a durable upgrade path. + * + * So this reads the funds signal the client already holds rather than waiting + * for a refusal to carry it. It is deliberately the symmetric half of the + * `hasFunds` watch above, which already clears paywalls when funds return. + * + * Consolidated cloud billing only: legacy balance loading currently collapses + * unknown into false, so it cannot safely drive this surface. + * + * Non-blocking by construction — it renders beside the composer and disables + * nothing. + */ +const agentHasFunds = computed(() => subscription.value?.agentHasFunds) +const creditsExhausted = computed(() => { + if (billingType.value !== 'workspace') return false + // Same gate as the impression report above: an unsettled read cannot say + // which presentation is right, and a card naming the wrong remediation is + // worse than no card. + if (!capabilityReadSettled.value) return false + if (agentHasFunds.value !== false) return false + if ( + billingStatus.value === 'paused' || + billingStatus.value === 'payment_failed' + ) + return false + // A standing card must offer a next step. Refusal-anchored inline cards can + // still explain member, sales-managed, or unavailable states without a CTA. + return ['subscribed', 'subscriptionRequired', 'local'].includes( + paywallPresentation.value.kind + ) +}) + +/** + * Suppressed while the latest transcript entry is an unresolved inline card: + * the two render the same copy. An older card may be scrolled away and must not + * hide the standing recovery path after a later server-side refusal. + */ +const showStandingPaywall = computed(() => { + if (!creditsExhausted.value) return false + const latestEntry = conversationEntries.value.at(-1) + return !( + latestEntry?.role === 'assistant' && + latestEntry.parts.some((part) => part.type === 'paywall') + ) +}) + +const agentPanelStore = useAgentPanelStore() +const billingIdentity = computed( + () => + `${resolvedUserInfo.value?.id ?? 'anonymous'}:${teamWorkspaceStore.workspaceId ?? 'none'}` +) + +/** + * One impression per exhaustion episode, not per render: the surface is + * standing, so it is visible for as long as the workspace is out of credits and + * a per-render report would make impressions a function of session length. + * Reset when funds return, so a later exhaustion reports again — mirroring how + * `useBillingBanner` scopes its dismissal to one episode. + */ +watch(billingIdentity, () => { + agentPanelStore.reportedExhaustionIdentity = null +}) + +function onStandingPaywallShown(): void { + if ( + !showStandingPaywall.value || + agentPanelStore.reportedExhaustionIdentity === billingIdentity.value + ) + return + const telemetry = useTelemetry() + if (!telemetry) return + agentPanelStore.reportedExhaustionIdentity = billingIdentity.value + telemetry.trackAgentPaywallShown({ + reason: snapshotAuthoritative.value + ? toAgentPaywallReason(paywallPresentation.value) + : 'unknown', + surface: 'credits_exhausted' + }) } const workflowStore = useWorkflowStore() const workflowService = useWorkflowService() const bindingStore = useAgentWorkflowTabBindingStore() -const agentPanelStore = useAgentPanelStore() const composerStore = useAgentComposerStore() const { selectedWorkflow: selectedTarget } = storeToRefs(agentPanelStore) const { dismissedSelectionSignature, enabled: agentEnabled } = @@ -696,17 +849,28 @@ function resumedTurnTabPath(): string | null { // Adoption (onWorkflowAdopted) and tab activation (onAgentActiveTab) are the // primary spinner setters; the non-idle branch only re-arms it after the // stash/resume flip of a panel remount, where those setters never run. -let observedActivityStatus = false +let wasTurnActive = false watch( [status, conversationTurnId], ([value, turnId]) => { + const completedTurn = wasTurnActive && value === 'idle' if (value === 'idle') { // The immediate idle value on remount is a hydration snapshot, not a // completed turn. A real idle transition is observed after this pass. - if (observedActivityStatus) graphActivity.finishTurn() + if (completedTurn) graphActivity.finishTurn() } else graphActivity.startTurn(turnId) - observedActivityStatus = true + wasTurnActive = value !== 'idle' if (value === 'idle') { + // A server-side LLM-hop refusal does not reach the browser as a 402. + // Refresh the authoritative effective-funds verdict after every observed + // turn completion so both exhaustion and external top-ups converge. + if (completedTurn && billingType.value === 'workspace') { + void refreshBillingStatus().catch((error: unknown) => { + reportError(error, { + errorType: 'error_refreshing_agent_billing_status' + }) + }) + } const completedPath = tabActivity.editingTabPath tabActivity.setEditing(null) if (completedPath !== null) tabActivity.markModified(completedPath) @@ -1375,6 +1539,7 @@ function onPanelDrop(event: DragEvent): void { :workflow-detached="workflowDetached" :get-mention-nodes="mentionableNodes" :paywall-presentation="paywallPresentation" + :credits-exhausted="showStandingPaywall" @send="onSend" @stop="onStop" @attach="onAttach" @@ -1389,6 +1554,7 @@ function onPanelDrop(event: DragEvent): void { @open-workflow="onOpenApprovalWorkflow" @open-reference-workflow="onNavigateToReferenceWorkflow" @paywall-action="onPaywallAction" + @standing-paywall-shown="onStandingPaywallShown" @new-chat="onNewChat" @toggle-size="agentPanelStore.toggleMaximize()" @close="onClosePanel" diff --git a/src/workbench/extensions/agent/components/agent/AgentPanel.vue b/src/workbench/extensions/agent/components/agent/AgentPanel.vue index 574faf400aa..dbe5984bd15 100644 --- a/src/workbench/extensions/agent/components/agent/AgentPanel.vue +++ b/src/workbench/extensions/agent/components/agent/AgentPanel.vue @@ -7,12 +7,13 @@ import { DropdownMenuSeparator, DropdownMenuTrigger } from 'reka-ui' -import { computed, nextTick, ref } from 'vue' +import { computed, nextTick, ref, watch } from 'vue' import { useI18n } from 'vue-i18n' import Button from '@/components/ui/button/Button.vue' import Input from '@/components/ui/input/Input.vue' import { buildTooltipConfig } from '@/composables/useTooltipConfig' +import type { AgentPaywallSurface } from '@/platform/telemetry/types' import type { ActiveTab } from '../../types/activeTab' import type { @@ -39,6 +40,7 @@ import EmptyState from './EmptyState.vue' import PanelHeader from './PanelHeader.vue' import RunNoticeBanner from './RunNoticeBanner.vue' import WorkflowSelectorChip from './composer/WorkflowSelectorChip.vue' +import AgentPaywallCard from './message/AgentPaywallCard.vue' const { entries, @@ -62,6 +64,7 @@ const { workflowDetached = false, getMentionNodes = () => [], paywallPresentation = DEFAULT_AGENT_PAYWALL_PRESENTATION, + creditsExhausted = false, sessionId = null, customTitle, historyGroups, @@ -91,6 +94,12 @@ const { workflowDetached?: boolean getMentionNodes?: () => SelectedNode[] paywallPresentation?: AgentPaywallPresentation + /** + * The workspace is out of credits right now, and no inline paywall card is + * already on screen saying so. Renders the standing surface beside the + * composer; see AgentPanelRoot's `creditsExhausted` for why it exists. + */ + creditsExhausted?: boolean sessionId?: string | null customTitle?: string historyGroups: HistoryGroups @@ -112,7 +121,8 @@ const emit = defineEmits<{ requestWorkflowReferences: [] removeWorkflowReference: [id: string] feedback: [turnId: string, vote: 'up' | 'down' | null] - paywallAction: [action: AgentPaywallAction] + paywallAction: [action: AgentPaywallAction, surface: AgentPaywallSurface] + standingPaywallShown: [] newChat: [] toggleSize: [] close: [] @@ -129,6 +139,14 @@ const emit = defineEmits<{ const showHistory = ref(false) +watch( + [showHistory, () => creditsExhausted], + ([historyVisible, exhausted]) => { + if (!historyVisible && exhausted) emit('standingPaywallShown') + }, + { immediate: true } +) + function onNewChat(): void { showHistory.value = false emit('newChat') @@ -369,7 +387,7 @@ defineExpose({ addAttachment, updateAttachment, removeAttachment }) (workflowId, workflowName) => emit('openReferenceWorkflow', workflowId, workflowName) " - @paywall-action="emit('paywallAction', $event)" + @paywall-action="emit('paywallAction', $event, 'refused_send')" /> @@ -378,6 +396,12 @@ defineExpose({ addAttachment, updateAttachment, removeAttachment })