| 1 |
import { getProfileShape } from '@auto-launch/fetchers/shape'; |
| 2 |
import { fetchWithTimeout, setStatus } from '@auto-launch/functions/helpers'; |
| 3 |
import { updateOption } from '@auto-launch/functions/wp'; |
| 4 |
import { overrideWithUrlParams } from '@auto-launch/state/url-params'; |
| 5 |
import { AI_HOST } from '@constants'; |
| 6 |
import { reqDataBasics } from '@shared/lib/data'; |
| 7 |
import { __ } from '@wordpress/i18n'; |
| 8 |
import { z } from 'zod'; |
| 9 |
|
| 10 |
const localShape = z.object({ |
| 11 |
siteProfile: getProfileShape, |
| 12 |
}); |
| 13 |
|
| 14 |
// No fallback here as we need this data |
| 15 |
const url = `${AI_HOST}/api/site-profile`; |
| 16 |
const method = 'POST'; |
| 17 |
const headers = { 'Content-Type': 'application/json' }; |
| 18 |
|
| 19 |
export const handleSiteProfile = async ({ |
| 20 |
title, |
| 21 |
descriptionRaw, |
| 22 |
urlParams, |
| 23 |
}) => { |
| 24 |
// translators: this is for a action log UI. Keep it short |
| 25 |
setStatus(__('Creating a site profile', 'extendify-local')); |
| 26 |
|
| 27 |
const body = JSON.stringify({ |
| 28 |
...reqDataBasics, |
| 29 |
title: title || window.extSharedData.siteTitle, |
| 30 |
description: descriptionRaw, |
| 31 |
autoLaunch: true, |
| 32 |
}); |
| 33 |
const response = await fetchWithTimeout( |
| 34 |
url, |
| 35 |
{ method, headers, body }, |
| 36 |
20_000, |
| 37 |
); |
| 38 |
|
| 39 |
// This should throw rather than have a fallback |
| 40 |
if (!response?.ok) throw new Error('Failed to fetch site profile'); |
| 41 |
|
| 42 |
// TODO: pull out followups here and save them for AI follow ups |
| 43 |
const { profile: p } = await response.json(); |
| 44 |
const profile = getProfileShape.parse(p); |
| 45 |
// Overwrite with a few url params if provided |
| 46 |
const profileWIthUrlOverrides = { |
| 47 |
...profile, |
| 48 |
...overrideWithUrlParams(urlParams), |
| 49 |
descriptionRaw, // Add the raw description to profile |
| 50 |
}; |
| 51 |
|
| 52 |
await updateOption( |
| 53 |
'extendify_site_profile', |
| 54 |
JSON.stringify(profileWIthUrlOverrides), |
| 55 |
); |
| 56 |
return localShape.parse({ siteProfile: profileWIthUrlOverrides }); |
| 57 |
}; |
| 58 |
|