| 1 |
import { AI_HOST, INSIGHTS_HOST } from '@constants'; |
| 2 |
import { reqDataBasics } from '@shared/lib/data'; |
| 3 |
import { useImageGenerationStore } from '@shared/state/generate-images'; |
| 4 |
import apiFetch from '@wordpress/api-fetch'; |
| 5 |
import { __ } from '@wordpress/i18n'; |
| 6 |
|
| 7 |
export const generateImage = async (imageData, signal) => { |
| 8 |
const response = await fetch(`${AI_HOST}/api/draft/image`, { |
| 9 |
method: 'POST', |
| 10 |
mode: 'cors', |
| 11 |
headers: { 'Content-Type': 'application/json' }, |
| 12 |
signal: signal, |
| 13 |
body: JSON.stringify({ |
| 14 |
...imageData, |
| 15 |
globalState: useImageGenerationStore.getState(), |
| 16 |
...reqDataBasics, |
| 17 |
}), |
| 18 |
}); |
| 19 |
|
| 20 |
const body = await response.json(); |
| 21 |
|
| 22 |
const imageCredits = { |
| 23 |
remaining: response.headers.get('x-ratelimit-remaining'), |
| 24 |
total: response.headers.get('x-ratelimit-limit'), |
| 25 |
refresh: response.headers.get('x-ratelimit-reset'), |
| 26 |
}; |
| 27 |
|
| 28 |
if (!response.ok) { |
| 29 |
if (body.status && body.status === 'content-policy-violation') { |
| 30 |
throw { |
| 31 |
message: __( |
| 32 |
'Your request was rejected as a result of our safety system. Your prompt may contain text that is not allowed by our safety system.', |
| 33 |
'extendify-local', |
| 34 |
), |
| 35 |
imageCredits, |
| 36 |
}; |
| 37 |
} |
| 38 |
throw { |
| 39 |
message: __('Service temporarily unavailable', 'extendify-local'), |
| 40 |
imageCredits, |
| 41 |
}; |
| 42 |
} |
| 43 |
return { |
| 44 |
images: body, |
| 45 |
imageCredits, |
| 46 |
id: response.headers.get('x-request-id'), |
| 47 |
}; |
| 48 |
}; |
| 49 |
|
| 50 |
export const recordPluginActivity = async ({ slug, source }) => { |
| 51 |
try { |
| 52 |
const res = await fetch(`${INSIGHTS_HOST}/api/v1/plugin-install`, { |
| 53 |
method: 'POST', |
| 54 |
headers: { 'Content-Type': 'application/json', 'X-Extendify': 'true' }, |
| 55 |
body: JSON.stringify({ |
| 56 |
...reqDataBasics, |
| 57 |
slug, |
| 58 |
source, |
| 59 |
siteCreatedAt: window.extSharedData?.siteCreatedAt, |
| 60 |
}), |
| 61 |
}); |
| 62 |
|
| 63 |
// this should not break the app. |
| 64 |
if (!res.ok) { |
| 65 |
console.error('Bad response from server'); |
| 66 |
return null; |
| 67 |
} |
| 68 |
|
| 69 |
return await res.json(); |
| 70 |
} catch (error) { |
| 71 |
console.error('Error sending plugin installation notification:', error); |
| 72 |
return null; |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
export const pingServer = async () => |
| 77 |
await apiFetch({ path: '/extendify/v1/shared/ping' }); |
| 78 |
|
| 79 |
export const getPartnerPlugins = async (key) => { |
| 80 |
const plugins = await apiFetch({ |
| 81 |
path: '/extendify/v1/shared/partner-plugins', |
| 82 |
}); |
| 83 |
if (!Object.keys(plugins?.data ?? {}).length) { |
| 84 |
throw new Error('Could not get plugins'); |
| 85 |
} |
| 86 |
if (key && plugins.data?.[key]) { |
| 87 |
return plugins.data[key]; |
| 88 |
} |
| 89 |
return plugins.data; |
| 90 |
}; |
| 91 |
|