PluginProbe
Extendify / 1.17.1
Extendify v1.17.1
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Draft / api / Data.js

Data.js in Extendify 1.17.1, at src/Draft/api/Data.js

105 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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