| 1 |
import { AI_HOST } from '@constants'; |
| 2 |
import { useAIConsentStore } from '@shared/state/ai-consent'; |
| 3 |
import { useImageGenerationStore } from '@shared/state/generate-images.js'; |
| 4 |
import { __ } from '@wordpress/i18n'; |
| 5 |
|
| 6 |
// Additional data to send with requests |
| 7 |
const allowList = [ |
| 8 |
'siteId', |
| 9 |
'partnerId', |
| 10 |
'wpVersion', |
| 11 |
'wpLanguage', |
| 12 |
'devbuild', |
| 13 |
'isBlockTheme', |
| 14 |
'userId', |
| 15 |
'siteProfile', |
| 16 |
]; |
| 17 |
|
| 18 |
const { showAIConsent, userGaveConsent } = useAIConsentStore.getState(); |
| 19 |
|
| 20 |
const extraBody = { |
| 21 |
...Object.fromEntries( |
| 22 |
Object.entries(window.extSharedData).filter(([key]) => |
| 23 |
allowList.includes(key), |
| 24 |
), |
| 25 |
), |
| 26 |
showAIConsent, |
| 27 |
userGaveConsent, |
| 28 |
}; |
| 29 |
|
| 30 |
export const completion = async ( |
| 31 |
prompt, |
| 32 |
promptType, |
| 33 |
systemMessageKey, |
| 34 |
details, |
| 35 |
) => { |
| 36 |
const response = await fetch(`${AI_HOST}/api/draft/completion`, { |
| 37 |
method: 'POST', |
| 38 |
headers: { 'Content-Type': 'application/json' }, |
| 39 |
body: JSON.stringify({ |
| 40 |
prompt, |
| 41 |
promptType, |
| 42 |
systemMessageKey, |
| 43 |
details, |
| 44 |
globalState: useImageGenerationStore.getState(), |
| 45 |
...extraBody, |
| 46 |
}), |
| 47 |
}); |
| 48 |
|
| 49 |
if (!response.ok) { |
| 50 |
throw new Error(__('Service temporarily unavailable', 'extendify-local')); |
| 51 |
} |
| 52 |
|
| 53 |
return response; |
| 54 |
}; |
| 55 |
|