import {z} from 'zod' 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 V1VoiceSample = {audioData: string, transcription?: string | undefined}; export type V1AudioProcessingConfig = {removeBackgroundNoise?: boolean | undefined}; export type VoiceServiceCloneVoiceBody = {displayName: string, langCode?: InworldttslanguageCodesLanguageCode | undefined, languageCode?: string | undefined, voiceSamples: Array, description?: string | undefined, tags?: Array | undefined, audioProcessingConfig?: V1AudioProcessingConfig | undefined}; export type CreateApiVoicesV1VoicesCloneArgs = {body: VoiceServiceCloneVoiceBody}; export type CreateApiVoicesV1VoicesCloneOptions = {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 inworldvoicev1Voice = z.object({voiceId: z.string().optional(), langCode: inworldttslanguageCodesLanguageCode.optional(), languageCode: z.string().optional(), displayName: z.string().optional(), description: z.string().optional(), tags: z.array(z.string()).optional(), name: z.string().optional(), source: z.enum(["SYSTEM", "IVC", "PVC"]).optional(), gender: z.enum(["male", "female", "neutral", ""]).optional(), ageGroup: z.enum(["young", "middle_aged", "elderly", ""]).optional(), categories: z.array(z.enum(["companions", "enterprise", "education_training", "developer_assistants", "healthcare", "interactive_media"])).optional(), promptLanguages: z.array(z.string()).optional()}); export const audioSampleValidatedWarning = z.object({text: z.string().optional()}); export const audioSampleValidatedError = z.object({text: z.string().optional()}); export const v1AudioSampleValidated = z.object({langCode: inworldttslanguageCodesLanguageCode.optional(), warnings: z.array(audioSampleValidatedWarning).optional(), errors: z.array(audioSampleValidatedError).optional(), transcription: z.string().optional(), audioData: z.string().optional()}); export const v1CloneVoiceResponse = z.object({voice: inworldvoicev1Voice.optional(), audioSamplesValidated: z.array(v1AudioSampleValidated).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 }; /** * Clone a voice * * POST /voices/v1/voices:clone */ export const createApiVoicesV1VoicesClone = async (args: CreateApiVoicesV1VoicesCloneArgs, options?: CreateApiVoicesV1VoicesCloneOptions) => { 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:clone${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(v1CloneVoiceResponse, await res.json()) };