Skip to content

Commit cf1131f

Browse files
committed
Update how "Chrome AI" provider handled in settings.
1 parent 48cdc20 commit cf1131f

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

hookdocs/chrome-built-in-ai.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Chrome is experimenting with adding [built-in AI](https://developer.chrome.com/docs/ai/built-in) directly to the browser, allowing websites and web applications to perform AI-powered tasks without needing to deploy or manage their own AI models. Please follow the steps below to use Chrome’s built-in AI:
2+
3+
1. **Install Chrome Canary:** Ensure you have version v128.0.6545.0 or newer.
4+
2. **Enable Optimization Guide:** Open `chrome://flags/#optimization-guide-on-device-model`, set it to "Enabled BypassPerfRequirement".
5+
3. **Enable Prompt API:** Open `chrome://flags/#prompt-api-for-gemini-nano`, set it to "Enabled".
6+
4. **Relaunch Chrome**.
7+
5. **Open DevTools** and send `await ai.languageModel.capabilities().available;` in the console. If this returns “readily,” then you are all set.
8+
6. **If it fails**, force Chrome to recognize that you want to use this API. To do so, open DevTools and send `await ai.languageModel.create();` in the console. This will likely fail, but it’s intended.
9+
7. **Relaunch Chrome**.
10+
8. **Open a new tab in Chrome**, go to `chrome://components`.
11+
9. **Confirm** that Gemini Nano is either available or is being downloaded. You'll want to see the Optimization Guide On Device Model present with a version greater or equal to 2024.5.21.1031. If there is no version listed, click on "Check for update" to force the download.
12+
10. **Once the model has downloaded and has reached a version greater than shown above**, open DevTools and send `await ai.languageModel.capabilities().available;` in the console. If this returns “readily,” then you are all set.

hookdocs/meta.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@
1010
},
1111
"prompt-examples": {
1212
"title": "Prompt examples"
13+
},
14+
"chrome-built-in-ai": {
15+
"title": "Chrome built-in AI"
1316
}
1417
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { useState, useEffect } from '@wordpress/element';
5+
import { Icon } from '@wordpress/components';
6+
import { __ } from '@wordpress/i18n';
7+
8+
/**
9+
* Internal dependencies
10+
*/
11+
import { SettingsRow } from '../settings-row';
12+
13+
/**
14+
* Component for Chrome AI Provider settings.
15+
*
16+
* This component is used within the ProviderSettings component to allow users to configure the Chrome AI Provider settings.
17+
*
18+
* @return {React.ReactElement} AzureAIVisionSettings component.
19+
*/
20+
export const ChromeAISettings = () => {
21+
const [ supported, setSupported ] = useState( false );
22+
23+
useEffect( () => {
24+
const checkBrowserSupport = async () => {
25+
if ( ! window.ai ) {
26+
return setSupported( false );
27+
}
28+
29+
try {
30+
const capabilities =
31+
await window.ai.languageModel.capabilities();
32+
if (
33+
capabilities &&
34+
capabilities.available &&
35+
'readily' === capabilities.available
36+
) {
37+
setSupported( true );
38+
}
39+
} catch ( error ) {
40+
// eslint-disable-next-line no-console
41+
console.error( 'Error getting capabilities: ', error );
42+
setSupported( false );
43+
}
44+
};
45+
46+
checkBrowserSupport();
47+
}, [] );
48+
49+
if ( ! window.ai ) {
50+
return null;
51+
}
52+
53+
const Description = ( { hasSupport = true } ) => {
54+
if ( hasSupport ) {
55+
return null;
56+
}
57+
58+
return (
59+
<>
60+
{ __(
61+
'Chrome built-in AI is not available on your browser. Please follow the steps ',
62+
'classifai'
63+
) }
64+
<a
65+
href="https://10up.github.io/classifai/tutorial-chrome-built-in-ai.html"
66+
target="_blank"
67+
rel="noopener noreferrer"
68+
>
69+
{ __( 'here', 'classifai' ) }
70+
</a>
71+
{ __( ' to enable it.', 'classifai' ) }
72+
</>
73+
);
74+
};
75+
76+
return (
77+
<>
78+
<SettingsRow
79+
label={ __( 'Built-in AI Support', 'classifai' ) }
80+
description={ <Description hasSupport={ supported } /> }
81+
>
82+
{ supported && <Icon icon="yes-alt" /> }
83+
{ ! supported && <Icon icon="dismiss" /> }
84+
{ supported
85+
? __( ' Supported', 'classifai' )
86+
: __( ' Not Supported', 'classifai' ) }
87+
</SettingsRow>
88+
</>
89+
);
90+
};

src/js/settings/components/provider-settings/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { OpenAIDallESettings } from './openai-dalle';
2727
import { AmazonPollySettings } from './amazon-polly';
2828
import { AzureTextToSpeechSettings } from './azure-text-to-speech';
2929
import { OpenAITextToSpeachSettings } from './openai-text-to-speech';
30+
import { ChromeAISettings } from './chrome-ai';
3031

3132
/**
3233
* Component for rendering provider setting fields based on the selected provider.
@@ -84,6 +85,9 @@ const ProviderFields = ( { provider, isConfigured } ) => {
8485
case 'openai_text_to_speech':
8586
return <OpenAITextToSpeachSettings isConfigured={ isConfigured } />;
8687

88+
case 'chrome_ai':
89+
return <ChromeAISettings />;
90+
8791
default:
8892
return null;
8993
}
@@ -110,6 +114,11 @@ export const ProviderSettings = () => {
110114
select( STORE_NAME ).getFeatureSettings()
111115
);
112116

117+
// Remove the Chrome AI provider from the list of providers if the browser AI is not available.
118+
if ( feature?.providers?.chrome_ai && ! window.ai ) {
119+
delete feature.providers.chrome_ai;
120+
}
121+
113122
const providerLabel = feature.providers[ provider ] || '';
114123
const providers = Object.keys( feature?.providers || {} ).map(
115124
( value ) => {

src/scss/settings.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@
272272
color: #46b450;
273273
}
274274

275+
span.dashicons.dashicons-dismiss {
276+
color: #cc0000;
277+
}
278+
275279
.classifai-settings-edit-provider {
276280
cursor: pointer;
277281

0 commit comments

Comments
 (0)