Skip to content
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

Studio: Add prompt limit UI #197

Merged
merged 15 commits into from
Jun 7, 2024
Merged

Studio: Add prompt limit UI #197

merged 15 commits into from
Jun 7, 2024

Conversation

kozer
Copy link
Contributor

@kozer kozer commented May 31, 2024

Fixes https://github.com/Automattic/dotcom-forge/issues/7409

Proposed Changes

This diff, adds a prompt limit UI into user settings

Testing Instructions

  1. Start studio nvim use && STUDIO_AI=true npm start
  2. Ensure you are logged in
  3. Navigate to settings and ensure that you see the following UI screen
    2024-05-31T10:25:46,590050296+03:00
  4. Start studio without STUDIO_AI feature enabled. Ensure that the new UI is not shown.

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

@kozer kozer self-assigned this May 31, 2024
@kozer kozer marked this pull request as draft May 31, 2024 07:05
@kozer kozer requested a review from a team June 3, 2024 11:40
@kozer kozer marked this pull request as ready for review June 3, 2024 11:40
Copy link
Contributor

@derekblank derekblank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I was able to observe prompt limit changes within the Settings screen.

Screenshot 2024-06-04 at 12 09 40 pm

Copy link
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kozer , the happy path works great.
We need to fix the unit tests.
I'm seeing this error Error: Uncaught [TypeError: Cannot read properties of undefined (reading 'assistantEnabled')] in src/components/tests/user-settings.test.tsx

I suggested using the headers from fetchAssistant to update the current usage, but its not a strong opinion.

One last thing I noticed is that when starting the app offline I see the count of my demo sites but the count of my prompts goes to zero. Would be possible to keep that number for consistency, or refetch it when I'm back online?
If yo prefer to handle that edge case in a separate PR I can create a new issue.

Happy Path:

8vWz2G.mp4

Steps to reproduce Offline edge case:

  1. Disconnect your wifi/internet
  2. Run the app
  3. Open the settings modal by clicking on your user icon
  4. Observe your prompts indicate 0 when it should be some number.

src/hooks/use-prompt-usage.tsx Outdated Show resolved Hide resolved
src/components/content-tab-assistant.tsx Outdated Show resolved Hide resolved
@kozer
Copy link
Contributor Author

kozer commented Jun 5, 2024

If yo prefer to handle that edge case in a separate PR I can create a new issue.

Thank you for your comments!
Yes, indeed I did a last minute change to not show the bar when the feature is not enabled and the tests where broken. I'm working on that.

If yo prefer to handle that edge case in a separate PR I can create a new issue.

Yes, let's create another issue to handle that. Thanks!

@kozer
Copy link
Contributor Author

kozer commented Jun 5, 2024

@sejas , I updated the PR to use assistant API to fetch the headers, fixed tests, and did a minor refactor! Thanks for your suggestions!

cc: @katinthehatsite

@katinthehatsite
Copy link
Contributor

Hi @kozer - thanks for making updates! The changes look good on my end.

I tested for this scenario:

1.Disconnect your wifi/internet
2. Run the app
3. Open the settings modal by clicking on your user icon
4. Observe your prompts indicate 0 when it should be some number.

And after updates, I can see that the number goes back up once I start the app back after being offline which is great. Would it be possible to keep it there even when offline? (At the moment, it goes to 0 when offline)

Otherwise, everything looks 👍

@kozer
Copy link
Contributor Author

kozer commented Jun 5, 2024

Hi @kozer - thanks for making updates! The changes look good on my end.

I tested for this scenario:

1.Disconnect your wifi/internet

  1. Run the app

  2. Open the settings modal by clicking on your user icon

  3. Observe your prompts indicate 0 when it should be some number.

And after updates, I can see that the number goes back up once I start the app back after being offline which is great. Would it be possible to keep it there even when offline? (At the moment, it goes to 0 when offline)

Otherwise, everything looks 👍

As discussed above, in Antonio's comment, I'll tackle this in a different pr ( the offline stuff ).
Is it ok?

@katinthehatsite
Copy link
Contributor

Sounds good @kozer ! I approved the PR ✅ Nice work!!

Copy link
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks great. I tested it I was able to see my usage updated. I noticed that the limit was restored to 0 from yesterday.

My only concern is that we are changing the layout for current users even if we didn't released our feature flag yet.

@wojtekn , what do you think about submitting this change that affects current users?
I think this specific change is harmless, but I understand if we prefer to keep the old demo site limit bar intact.

Before
settings-usage-original
After
settings-usage-changed

@@ -7,6 +7,7 @@ import { useAssistant } from '../hooks/use-assistant';
import { useAssistantApi } from '../hooks/use-assistant-api';
import { useAuth } from '../hooks/use-auth';
import { useOffline } from '../hooks/use-offline';
import { usePromptUsage } from '../hooks/use-prompt-usage';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this unused import.

@@ -72,13 +74,11 @@ const SnapshotInfo = ( {
const isOffline = useOffline();
const offlineMessage = __( 'Deleting demo sites requires an internet connection.' );
return (
<div className="flex gap-5 flex-col">
<h2 className="a8c-subtitle-small">{ __( 'Usage' ) }</h2>
Copy link
Member

@sejas sejas Jun 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't change the old behaviour until we release the feature flag.
We are changing the old behaviour but that's ok in this case.

Comment on lines 60 to 79
await new Promise( ( resolve, reject ) => {
client.req.get(
{
method: 'HEAD',
path: '/studio-app/ai-assistant/chat',
apiNamespace: 'wpcom/v2',
},
( error: Error, _data: unknown, headers: Record< string, string > ) => {
if ( error ) {
return reject( error );
}
if ( ! headers ) {
reject( new Error( 'No headers in response' ) );
return;
}
updatePromptUsage( headers );
resolve( headers );
}
);
} );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to create this promise and resolve the headers? Maybe we can await the client.req.get directly.

Suggested change
await new Promise( ( resolve, reject ) => {
client.req.get(
{
method: 'HEAD',
path: '/studio-app/ai-assistant/chat',
apiNamespace: 'wpcom/v2',
},
( error: Error, _data: unknown, headers: Record< string, string > ) => {
if ( error ) {
return reject( error );
}
if ( ! headers ) {
reject( new Error( 'No headers in response' ) );
return;
}
updatePromptUsage( headers );
resolve( headers );
}
);
} );
await client.req.get(
{
method: 'HEAD',
path: '/studio-app/ai-assistant/chat',
apiNamespace: 'wpcom/v2',
},
( error: Error, _data: unknown, headers: Record< string, string > ) => {
if ( error ) {
return;
}
if ( ! headers ) {
return;
}
updatePromptUsage( headers );
}
);

} finally {
setIsLoading( false );
}
const message = response?.choices?.[ 0 ]?.message?.content;

updatePromptUsage( headers );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for optimizing this ! 🙏

@wojtekn
Copy link
Contributor

wojtekn commented Jun 6, 2024

My only concern is that we are changing the layout for current users even if we didn't released our feature flag yet.
@wojtekn , what do you think about submitting this change that affects current users?
I think this specific change is harmless, but I understand if we prefer to keep the old demo site limit bar intact.

It's okay. We are slightly changing the layout to prepare it for incoming changes, but we are not enabling AI assistant UI in any form.

Copy link
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@sejas sejas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied these changes and tested it again. It works as expected. Thank you!
As we discussed we can update the headers name later.

Changes:

update-usage.mp4

@sejas sejas merged commit 23eabf9 into trunk Jun 7, 2024
11 checks passed
@sejas sejas deleted the add/setting-prompt-limit-ui branch June 7, 2024 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants