-
Notifications
You must be signed in to change notification settings - Fork 90
AI credits: Warn at 80% usage in the sidebar and above the Classic composer #4722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gcsecsey
wants to merge
5
commits into
gcsecsey/composer-credit-warning-states
from
gcsecsey/ai-credits-usage-notices
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e96a781
AI credits: share the usage-warning rule and title across surfaces
gcsecsey a4f274c
Agentic UI: warn in the sidebar at 80% AI credit usage
gcsecsey cb27ce7
Studio: warn above the composer as AI credits run down
gcsecsey 66f282b
Announce the Classic AI credits notice and align it for RTL
gcsecsey c27270a
Build the Classic AI credits warning on the design system Notice
gcsecsey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
apps/studio/src/components/ai-credits-threshold-notice.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { | ||
| formatAiCreditsThresholdDescription, | ||
| formatAiCreditsUsageTitle, | ||
| getAiCreditsMeter, | ||
| getAiCreditsMeterIntent, | ||
| getStudioCodeAiAccessState, | ||
| resolveAiCreditsThresholdNotice, | ||
| } from '@studio/common/lib/studio-assistant-quota'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { close } from '@wordpress/icons'; | ||
| import { Icon } from '@wordpress/ui'; | ||
| import { useEffect, type ReactNode } from 'react'; | ||
| import { AddAiCreditsButton } from 'src/components/add-ai-credits-button'; | ||
| import { useAppDispatch, useI18nLocale, useRootSelector } from 'src/stores'; | ||
| import { selectDismissedAiCreditsIntent, setDismissedAiCreditsIntent } from 'src/stores/ui-slice'; | ||
| import { useGetStudioAssistantQuota } from 'src/stores/wpcom-api'; | ||
|
|
||
| // Classic has no composer strip and no lockout banner of its own, so this one | ||
| // slot announces both warning steps. The agentic UI splits them across the | ||
| // sidebar and the composer instead. | ||
| const CLASSIC_NOTICE_INTENTS = [ 'warning', 'critical' ] as const; | ||
|
|
||
| function NoticeCard( { | ||
| title, | ||
| description, | ||
| action, | ||
| onDismiss, | ||
| }: { | ||
| title: string; | ||
| description?: string; | ||
| action?: ReactNode; | ||
| onDismiss: () => void; | ||
| } ) { | ||
| return ( | ||
| <div className="border-frame-border bg-frame-surface relative mb-2 flex flex-col items-start gap-1 rounded-lg border p-3 text-left"> | ||
|
gcsecsey marked this conversation as resolved.
Outdated
|
||
| <span className="text-frame-text pe-6 text-sm font-semibold">{ title }</span> | ||
| { description ? ( | ||
| <span className="text-frame-text-secondary text-xs">{ description }</span> | ||
| ) : null } | ||
| { action } | ||
| <button | ||
| type="button" | ||
| aria-label={ __( 'Dismiss' ) } | ||
| onClick={ onDismiss } | ||
| className="text-frame-text-secondary hover:text-frame-text absolute end-2 top-2" | ||
| > | ||
| <Icon icon={ close } size={ 16 } /> | ||
| </button> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Warns above the Classic composer as the AI credit balance runs down. Classic | ||
| * has no persistent-message surface, so the warning sits where the user is | ||
| * about to spend the credits. | ||
| */ | ||
| export function AiCreditsThresholdNotice() { | ||
| const dispatch = useAppDispatch(); | ||
| const locale = useI18nLocale(); | ||
| const dismissedIntent = useRootSelector( selectDismissedAiCreditsIntent ); | ||
| const { data: quota } = useGetStudioAssistantQuota(); | ||
| const meter = | ||
| quota && getStudioCodeAiAccessState( quota ) === 'available' | ||
| ? getAiCreditsMeter( quota ) | ||
| : null; | ||
| const intent = meter ? getAiCreditsMeterIntent( meter.fraction ) : null; | ||
| const notice = resolveAiCreditsThresholdNotice( intent, dismissedIntent, CLASSIC_NOTICE_INTENTS ); | ||
|
|
||
| // Drop a dismissal the current usage has left behind, so the notice can | ||
| // fire again if the account returns to that threshold. | ||
| useEffect( () => { | ||
| if ( notice.dismissedIntent !== dismissedIntent ) { | ||
| dispatch( setDismissedAiCreditsIntent( notice.dismissedIntent ) ); | ||
| } | ||
| }, [ dispatch, dismissedIntent, notice.dismissedIntent ] ); | ||
|
|
||
| const thresholdIntent = intent === 'warning' || intent === 'critical' ? intent : null; | ||
| if ( ! thresholdIntent || ! notice.visible || ! meter ) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <NoticeCard | ||
| title={ formatAiCreditsUsageTitle( meter.fraction, locale ) } | ||
| description={ formatAiCreditsThresholdDescription() } | ||
| action={ <AddAiCreditsButton className="mt-2" /> } | ||
| onDismiss={ () => dispatch( setDismissedAiCreditsIntent( thresholdIntent ) ) } | ||
| /> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
apps/studio/src/components/tests/ai-credits-threshold-notice.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { AiCreditsThresholdNotice } from 'src/components/ai-credits-threshold-notice'; | ||
| import { useAppDispatch, useRootSelector } from 'src/stores'; | ||
| import { setDismissedAiCreditsIntent } from 'src/stores/ui-slice'; | ||
| import { useGetStudioAssistantQuota } from 'src/stores/wpcom-api'; | ||
| import type { AiCreditsMeterIntent } from '@studio/common/lib/studio-assistant-quota'; | ||
|
|
||
| vi.mock( 'src/stores', () => ( { | ||
| useAppDispatch: vi.fn(), | ||
| useRootSelector: vi.fn(), | ||
| useI18nLocale: () => 'en', | ||
| } ) ); | ||
| vi.mock( 'src/stores/wpcom-api', () => ( { useGetStudioAssistantQuota: vi.fn() } ) ); | ||
| vi.mock( 'src/components/add-ai-credits-button', () => ( { | ||
| AddAiCreditsButton: () => <button type="button">Add AI credits</button>, | ||
| } ) ); | ||
|
|
||
| const dispatch = vi.fn(); | ||
|
|
||
| // 1,000,000-credit allowance, nothing purchased. | ||
| function mockUsage( allowanceRemaining: number ) { | ||
| vi.mocked( useGetStudioAssistantQuota, { partial: true } ).mockReturnValue( { | ||
| data: { costUsage: 0, costCap: 1000000, allowanceRemaining, purchasedRemaining: undefined }, | ||
| } ); | ||
| } | ||
|
|
||
| function mockState( { | ||
| dismissedIntent = null, | ||
| }: { dismissedIntent?: AiCreditsMeterIntent | null } = {} ) { | ||
| vi.mocked( useRootSelector ).mockImplementation( ( selector ) => | ||
| selector( { ui: { dismissedAiCreditsIntent: dismissedIntent } } as never ) | ||
| ); | ||
| } | ||
|
|
||
| describe( 'AiCreditsThresholdNotice', () => { | ||
| beforeEach( () => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked( useAppDispatch, { partial: true } ).mockReturnValue( dispatch ); | ||
| mockState(); | ||
| } ); | ||
|
|
||
| it( 'warns at 80% usage, reporting the live figure', () => { | ||
| mockUsage( 170000 ); | ||
| render( <AiCreditsThresholdNotice /> ); | ||
|
|
||
| expect( screen.getByText( 'At 83% usage' ) ).toBeInTheDocument(); | ||
| expect( screen.getByRole( 'button', { name: 'Add AI credits' } ) ).toBeInTheDocument(); | ||
| } ); | ||
|
|
||
| // Classic has no composer strip, so its one slot carries the 90% step too. | ||
| it( 'escalates at 90% usage', () => { | ||
| mockUsage( 100000 ); | ||
| render( <AiCreditsThresholdNotice /> ); | ||
|
|
||
| expect( screen.getByText( 'At 90% usage' ) ).toBeInTheDocument(); | ||
| } ); | ||
|
|
||
| it( 'shows nothing below 80%, or once the credits are spent', () => { | ||
| mockUsage( 500000 ); | ||
| const { rerender } = render( <AiCreditsThresholdNotice /> ); | ||
| expect( screen.queryByText( /usage$/ ) ).not.toBeInTheDocument(); | ||
|
|
||
| mockUsage( 0 ); | ||
| rerender( <AiCreditsThresholdNotice /> ); | ||
| expect( screen.queryByText( /usage$/ ) ).not.toBeInTheDocument(); | ||
| } ); | ||
|
|
||
| it( 'records the dismissal against the threshold it was made at', async () => { | ||
| const user = userEvent.setup(); | ||
| mockUsage( 200000 ); | ||
| render( <AiCreditsThresholdNotice /> ); | ||
|
|
||
| await user.click( screen.getByRole( 'button', { name: 'Dismiss' } ) ); | ||
|
|
||
| expect( dispatch ).toHaveBeenCalledWith( setDismissedAiCreditsIntent( 'warning' ) ); | ||
| } ); | ||
|
|
||
| it( 'stays hidden at a threshold already dismissed', () => { | ||
| mockUsage( 200000 ); | ||
| mockState( { dismissedIntent: 'warning' } ); | ||
| render( <AiCreditsThresholdNotice /> ); | ||
|
|
||
| expect( screen.queryByText( 'At 80% usage' ) ).not.toBeInTheDocument(); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { renderHook, act } from '@testing-library/react'; | ||
| import { beforeEach, describe, expect, it } from 'vitest'; | ||
| import { | ||
| resetAiCreditsNoticeForTests, | ||
| setDismissedAiCreditsIntent, | ||
| useDismissedAiCreditsIntent, | ||
| } from './ai-credits-notice'; | ||
|
|
||
| describe( 'ai credits notice dismissal', () => { | ||
| beforeEach( () => resetAiCreditsNoticeForTests() ); | ||
|
|
||
| it( 'starts undismissed, so a fresh run always warns', () => { | ||
| expect( renderHook( () => useDismissedAiCreditsIntent() ).result.current ).toBeNull(); | ||
| } ); | ||
|
|
||
| it( 'notifies subscribers when the dismissal changes', () => { | ||
| const { result } = renderHook( () => useDismissedAiCreditsIntent() ); | ||
|
|
||
| act( () => setDismissedAiCreditsIntent( 'warning' ) ); | ||
| expect( result.current ).toBe( 'warning' ); | ||
|
|
||
| act( () => setDismissedAiCreditsIntent( null ) ); | ||
| expect( result.current ).toBeNull(); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { useSyncExternalStore } from 'react'; | ||
| import type { AiCreditsMeterIntent } from '@studio/common/lib/studio-assistant-quota'; | ||
|
|
||
| // Session-only, and deliberately not persisted: a threshold notice describes | ||
| // the balance right now, so an account that reaches 80% again in a later run | ||
| // has spent a new pool's worth of credits and deserves the warning again. | ||
| let dismissedIntent: AiCreditsMeterIntent | null = null; | ||
| const listeners = new Set< () => void >(); | ||
|
|
||
| export function setDismissedAiCreditsIntent( intent: AiCreditsMeterIntent | null ): void { | ||
| if ( dismissedIntent === intent ) { | ||
| return; | ||
| } | ||
| dismissedIntent = intent; | ||
| for ( const listener of listeners ) { | ||
| listener(); | ||
| } | ||
| } | ||
|
|
||
| export function useDismissedAiCreditsIntent(): AiCreditsMeterIntent | null { | ||
| return useSyncExternalStore( | ||
| ( listener ) => { | ||
| listeners.add( listener ); | ||
| return () => { | ||
| listeners.delete( listener ); | ||
| }; | ||
| }, | ||
| () => dismissedIntent, | ||
| () => null | ||
| ); | ||
| } | ||
|
|
||
| export function resetAiCreditsNoticeForTests(): void { | ||
| dismissedIntent = null; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.