import {z} from 'zod' export type V1VoiceDesignConfig = {numberOfSamples?: number | undefined}; export type InworldttslanguageCodesLanguageCode = 'EN_US' | 'ZH_CN' | 'KO_KR' | 'JA_JP' | 'RU_RU' | 'AUTO' | 'IT_IT' | 'ES_ES' | 'PT_BR' | 'DE_DE' | 'FR_FR' | 'AR_SA' | 'PL_PL' | 'NL_NL' | 'HI_IN' | 'HE_IL'; export type VoiceServiceDesignVoiceBody = {voiceDesignConfig?: V1VoiceDesignConfig | undefined, designPrompt: string, langCode?: InworldttslanguageCodesLanguageCode | undefined, languageCode?: string | undefined, previewText: string}; export type CreateApiVoicesV1VoicesDesignArgs = {body: VoiceServiceDesignVoiceBody}; export type CreateApiVoicesV1VoicesDesignOptions = {baseUrl?: string | undefined, inworldBasic?: string | undefined, fetch?: typeof fetch | undefined}; export const inworldttslanguageCodesLanguageCode = z.enum(["EN_US", "ZH_CN", "KO_KR", "JA_JP", "RU_RU", "AUTO", "IT_IT", "ES_ES", "PT_BR", "DE_DE", "FR_FR", "AR_SA", "PL_PL", "NL_NL", "HI_IN", "HE_IL"]); export const v1PreviewVoice = z.object({voiceId: z.string().optional(), previewText: z.string().optional(), previewAudio: z.string().optional()}); export const v1DesignVoiceResponse = z.object({langCode: inworldttslanguageCodesLanguageCode.optional(), previewVoices: z.array(v1PreviewVoice).optional()}); 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 }; /** * Design a voice * * POST /voices/v1/voices:design */ export const createApiVoicesV1VoicesDesign = async (args: CreateApiVoicesV1VoicesDesignArgs, options?: CreateApiVoicesV1VoicesDesignOptions) => { const {baseUrl = "https://api.inworld.ai", fetch: fetchFn = fetch, inworldBasic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (inworldBasic !== undefined) headers.set("Authorization", inworldBasic) const url = `${baseUrl.replace(/\/+$/, '')}/voices/v1/voices:design${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(v1DesignVoiceResponse, await res.json()) };