| 1 |
import { AI_HOST } from '@constants'; |
| 2 |
import { useAIConsentStore } from '@shared/state/ai-consent'; |
| 3 |
|
| 4 |
// Additional data to send with requests |
| 5 |
const allowList = [ |
| 6 |
'siteId', |
| 7 |
'partnerId', |
| 8 |
'wpVersion', |
| 9 |
'wpLanguage', |
| 10 |
'devbuild', |
| 11 |
'isBlockTheme', |
| 12 |
'userId', |
| 13 |
'siteProfile', |
| 14 |
]; |
| 15 |
|
| 16 |
const { showAIConsent, userGaveConsent } = useAIConsentStore.getState(); |
| 17 |
|
| 18 |
const extraBody = { |
| 19 |
...Object.fromEntries( |
| 20 |
Object.entries(window.extSharedData).filter(([key]) => |
| 21 |
allowList.includes(key), |
| 22 |
), |
| 23 |
), |
| 24 |
showAIConsent, |
| 25 |
userGaveConsent, |
| 26 |
}; |
| 27 |
|
| 28 |
export const getAnswer = ({ question, experienceLevel }) => |
| 29 |
fetch(`${AI_HOST}/api/chat/ask-question`, { |
| 30 |
method: 'POST', |
| 31 |
headers: { 'Content-Type': 'application/json' }, |
| 32 |
body: JSON.stringify({ question, experienceLevel, ...extraBody }), |
| 33 |
}); |
| 34 |
|
| 35 |
export const rateAnswer = ({ answerId, rating }) => |
| 36 |
fetch(`${AI_HOST}/api/chat/rate-answer`, { |
| 37 |
method: 'POST', |
| 38 |
headers: { 'Content-Type': 'application/json' }, |
| 39 |
body: JSON.stringify({ answerId, rating }), |
| 40 |
}); |
| 41 |
|