import {z} from 'zod' export type GetApiEngineManifestOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const engineName = z.string(); export const brandName = z.string(); export const updateInfo = z.object({version: z.string(), descriptions: z.array(z.string()), contributors: z.array(z.string()).optional()}); export const licenseInfo = z.object({name: z.string(), version: z.string().optional(), license: z.string().optional(), text: z.string()}); export const supportedFeatures = z.object({adjust_mora_pitch: z.boolean(), adjust_phoneme_length: z.boolean(), adjust_speed_scale: z.boolean(), adjust_pitch_scale: z.boolean(), adjust_intonation_scale: z.boolean(), adjust_volume_scale: z.boolean(), adjust_pause_length: z.boolean().optional(), interrogative_upspeak: z.boolean(), synthesis_morphing: z.boolean(), sing: z.boolean().optional(), manage_library: z.boolean().optional(), return_resource_url: z.boolean().optional(), apply_katakana_english: z.boolean().optional(), streaming_synthesis: z.boolean().optional()}); export const engineManifest = z.object({manifest_version: z.string(), name: engineName, brand_name: brandName, uuid: z.string(), url: z.string(), icon: z.string(), default_sampling_rate: z.number().int(), frame_rate: z.number(), terms_of_service: z.string(), update_infos: z.array(updateInfo), dependency_licenses: z.array(licenseInfo), supported_vvlib_manifest_version: z.string().optional(), supported_features: supportedFeatures}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Engine Manifest * * GET /engine_manifest */ export const getApiEngineManifest = async (options: GetApiEngineManifestOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/engine_manifest${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(engineManifest, await res.json()) };