| 1 |
import { __ } from '@wordpress/i18n'; |
| 2 |
import { AI_HOST } from '@constants'; |
| 3 |
import { useAIConsentStore } from '@shared/state/ai-consent'; |
| 4 |
import { useGlobalStore } from '@draft/state/global.js'; |
| 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: useGlobalStore.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 |
|
| 56 |
export const generateImage = async (imageData, signal) => { |
| 57 |
const response = await fetch(`${AI_HOST}/api/draft/image`, { |
| 58 |
method: 'POST', |
| 59 |
mode: 'cors', |
| 60 |
headers: { 'Content-Type': 'application/json' }, |
| 61 |
signal: signal, |
| 62 |
body: JSON.stringify({ |
| 63 |
...imageData, |
| 64 |
globalState: useGlobalStore.getState(), |
| 65 |
...extraBody, |
| 66 |
}), |
| 67 |
}); |
| 68 |
|
| 69 |
const body = await response.json(); |
| 70 |
|
| 71 |
const imageCredits = { |
| 72 |
remaining: response.headers.get('x-ratelimit-remaining'), |
| 73 |
total: response.headers.get('x-ratelimit-limit'), |
| 74 |
refresh: response.headers.get('x-ratelimit-reset'), |
| 75 |
}; |
| 76 |
|
| 77 |
if (!response.ok) { |
| 78 |
if (body.status && body.status === 'content-policy-violation') { |
| 79 |
throw { |
| 80 |
message: __( |
| 81 |
'Your request was rejected as a result of our safety system. Your prompt may contain text that is not allowed by our safety system.', |
| 82 |
'extendify-local', |
| 83 |
), |
| 84 |
imageCredits, |
| 85 |
}; |
| 86 |
} |
| 87 |
throw { |
| 88 |
message: __('Service temporarily unavailable', 'extendify-local'), |
| 89 |
imageCredits, |
| 90 |
}; |
| 91 |
} |
| 92 |
return { |
| 93 |
images: body, |
| 94 |
imageCredits, |
| 95 |
id: response.headers.get('x-request-id'), |
| 96 |
}; |
| 97 |
}; |
| 98 |
|
| 99 |
export const downloadPing = (id, source, details = {}) => |
| 100 |
fetch(`${AI_HOST}/api/draft/image/download`, { |
| 101 |
method: 'POST', |
| 102 |
headers: { 'Content-Type': 'application/json' }, |
| 103 |
body: JSON.stringify({ id, source, ...details }), |
| 104 |
}); |
| 105 |
|