-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AI Assistant: Load Breve spelling dictionaries from CDN (#38943)
* add function to get dictionary from endpoint * load the spelling dictionary context from the backend * changelog * change endpoint to CDN * update changelog
- Loading branch information
Showing
4 changed files
with
104 additions
and
10 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/update-jetpack-ai-breve-load-dictionary-from-server
This file contains 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,4 @@ | ||
Significance: patch | ||
Type: other | ||
|
||
AI Assistant: Load dictionaries from CDN |
This file contains 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 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
43 changes: 43 additions & 0 deletions
43
...s/jetpack/extensions/plugins/ai-assistant-plugin/components/breve/utils/get-dictionary.ts
This file contains 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,43 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import debugFactory from 'debug'; | ||
/** | ||
* Types | ||
*/ | ||
import { SpellingDictionaryContext } from '../types'; | ||
|
||
const debug = debugFactory( 'jetpack-ai-breve:get-dictionary' ); | ||
|
||
/** | ||
* A function that gets a Breve dictionary context object. | ||
* | ||
* @param {string} type | ||
* @param {string} language | ||
* @return {Promise<SpellingDictionaryContext>} - A promise that resolves a dictionary context object. | ||
*/ | ||
export default async function getDictionary( | ||
type: string, | ||
language: string | ||
): Promise< SpellingDictionaryContext > { | ||
debug( 'Asking dictionary for type: %s. language: %s', type, language ); | ||
|
||
// Randomize the server to balance the load | ||
const counter = Math.floor( Math.random() * 3 ); | ||
const url = `https://s${ counter }.wp.com/wp-content/lib/jetpack-ai/breve-dictionaries/${ type }/${ language }.json`; | ||
|
||
try { | ||
const data = await fetch( url ); | ||
|
||
if ( data.status === 404 ) { | ||
throw new Error( 'The requested dictionary does not exist' ); | ||
} else if ( data.status !== 200 ) { | ||
throw new Error( 'Failed to fetch dictionary' ); | ||
} | ||
|
||
return data.json(); | ||
} catch ( error ) { | ||
debug( 'Error getting dictionary: %o', error ); | ||
return Promise.reject( error ); | ||
} | ||
} |