-
Notifications
You must be signed in to change notification settings - Fork 2k
Reimplement unified ai chat to use server side flag. #107601
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
Merged
+87
−24
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
30ce083
Reimplement unified ai chat to use server side flag.
aaronfc 509ff8c
Use hybrid approach for unified AI chat flag
aaronfc 62f5b25
Help Center: Use /me/preferences for unified AI chat flag
aaronfc 96793ae
Move agentsManagerData type to global.d.ts
aaronfc 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
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,14 @@ | ||
| /** | ||
| * Global type declarations for the Agents Manager package. | ||
| */ | ||
|
|
||
| /** | ||
| * agentsManagerData is set as a global const via wp_add_inline_script | ||
| * in Jetpack's Agents Manager feature. | ||
| */ | ||
| declare const agentsManagerData: | ||
| | { | ||
| agentProviders?: string[]; | ||
| useUnifiedExperience?: boolean; | ||
| } | ||
| | undefined; |
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,53 @@ | ||
| import { getUseUnifiedExperienceFromInlineData } from '@automattic/agents-manager'; | ||
| import { useQuery } from '@tanstack/react-query'; | ||
| import wpcomRequest, { canAccessWpcomApis } from 'wpcom-proxy-request'; | ||
|
|
||
| interface CalypsoPreferencesResponse { | ||
| calypso_preferences?: { | ||
| unified_ai_chat?: boolean; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Determines if the user should see the unified AI chat experience. | ||
| * | ||
| * This hook uses a hybrid approach to work across all environments: | ||
| * | ||
| * 1. **wp-admin environments** (Atomic, Garden, Simple sites): | ||
| * The flag is available via `agentsManagerData.useUnifiedExperience`, | ||
| * injected server-side by Jetpack's Agents Manager. | ||
| * | ||
| * 2. **Calypso app** (wordpress.com): | ||
| * The flag is fetched from the `/me/preferences` endpoint. | ||
| * | ||
| * The rollout logic lives in Agents Manager (Jetpack) via the | ||
| * `agents_manager_use_unified_experience` filter. | ||
| */ | ||
| export function useUnifiedAiChat( enabled = true ) { | ||
| return useQuery< boolean, Error >( { | ||
| queryKey: [ 'unified-ai-chat' ], | ||
| queryFn: async () => { | ||
| // 1. Check inline script first (available on wp-admin via Jetpack's Agents Manager) | ||
| const inlineValue = getUseUnifiedExperienceFromInlineData(); | ||
| if ( inlineValue !== undefined ) { | ||
| return inlineValue; | ||
| } | ||
|
|
||
| // 2. Fall back to /me/preferences endpoint for Calypso app (wordpress.com) | ||
| if ( canAccessWpcomApis() ) { | ||
| const response: CalypsoPreferencesResponse = await wpcomRequest( { | ||
| path: '/me/preferences', | ||
| apiVersion: '1.1', | ||
| } ); | ||
|
|
||
| return response.calypso_preferences?.unified_ai_chat ?? false; | ||
| } | ||
|
|
||
| // 3. No data available - default to false | ||
| return false; | ||
| }, | ||
| enabled, | ||
| refetchOnWindowFocus: false, | ||
| staleTime: 300000, // 5 minutes | ||
| } ); | ||
| } | ||
24 changes: 3 additions & 21 deletions
24
packages/help-center/src/hooks/use-should-use-unified-agent.ts
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 |
|---|---|---|
| @@ -1,26 +1,8 @@ | ||
| /* eslint-disable no-restricted-imports */ | ||
| import config from '@automattic/calypso-config'; | ||
| import { useSupportStatus } from '../data/use-support-status'; | ||
|
|
||
| function isUnifiedAgentFlagSetInURL(): boolean { | ||
| const currentUrl = window.location.href; | ||
| const urlParams = new URLSearchParams( new URL( currentUrl ).search ); | ||
| const flags = urlParams.get( 'flags' ); | ||
| return flags?.split( ',' ).includes( 'unified-agent' ) ?? false; | ||
| } | ||
| import { useUnifiedAiChat } from '../data/use-unified-ai-chat'; | ||
|
|
||
| export const useShouldUseUnifiedAgent = () => { | ||
| const { data: supportStatus } = useSupportStatus(); | ||
|
|
||
| // Check if user is eligible via support status API | ||
| // Note: This will need to be added to the backend support-status endpoint | ||
| const isEligibleViaAPI = Boolean( supportStatus?.eligibility?.unified_agent_enabled ); | ||
|
|
||
| // Force unified agent via URL flag (for testing) | ||
| const isFlagSetInURL = isUnifiedAgentFlagSetInURL(); | ||
|
|
||
| // Unified agent can be enabled via config | ||
| const isConfigEnabled = config.isEnabled( 'unified-agent' ); | ||
| const { data: isEligibleViaAPI } = useUnifiedAiChat(); | ||
|
|
||
| return isEligibleViaAPI || isFlagSetInURL || isConfigEnabled; | ||
| return isEligibleViaAPI; | ||
| }; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you happen to look to see if this has already been loaded elsewhere or is otherwise in Calypso state? I think it might be preloaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked now. React Query already caches this with 5-minute staleTime, so subsequent renders don't make additional requests.
I gave it a go anyway and the required changes seemed too big for the benefit IMO.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're currently making 3-4
GETrequests to various versions of/me/preferenceson Calypso load. This is adding another. Theclient/does havegetPreferenceinclient/state/preferences/selectors.js, but we'd need to pass it down inclient/layout/help-center-loader.tsx, I think. It could be a followup task, but it seems to be a systemic problem in Calypso.Maybe it can wait until we add an async loader for
agents-manager.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right.
This is not what I saw. When I checked I didn't see this adding any extra request. On further reviewed it seemed to be because of
staleTime. Do you see something different? If this is adding an extra request I am happy changing the implementation not to do it. Otherwise maybe we can create a ticket and tackle down this multiple requests as a followup task.