import {z} from 'zod' export type GetApiV1TuneTuneIdArgs = {tune_id: string}; export type GetApiV1TuneTuneIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const baseEngine = z.enum(["text/llama-2-7b", "text/llama-2-13b", "text/llama-2-70b", "text/codellama-7b", "text/codellama-13b", "text/codellama-34b", "text/codellama-70b", "text/mistral-7b", "text/mixtral-8x7b", "image/controlnet-sd15", "image/controlnet-sdxl", "image/stable-diffusion-v1-5", "image/stable-diffusion-xl-v1-0", "image/stable-diffusion-ssd", "audio/tortoise-tts", "audio/rvc"]); export const loraTuneCheckpoint = z.object({checkpoint_id: z.string(), engine: baseEngine.nullable().optional(), name: z.string().nullable().optional()}); export const loraTuneFile = z.object({caption: z.string().nullable().optional(), file_id: z.string()}); export const loraTuneOutput = z.object({base_checkpoint: loraTuneCheckpoint, files: z.array(loraTuneFile), resize_images: z.boolean().nullable().optional(), seed: z.number().int().nullable().optional(), steps: z.number().int(), trigger_words: z.array(z.string()), tune_type: z.literal("lora_tune").optional()}); export const textToSpeechLatentTune = z.object({asset_ids: z.array(z.string()), inline_assets: z.boolean().nullable().optional(), tune_type: z.literal("text_to_speech_latent_tune").optional()}); export const tuneResult = z.object({number_of_available_assets: z.number().int().nullable().optional(), output_assets: z.array(z.string()).nullable().optional(), rejected_assets: z.array(z.string()).nullable().optional(), unready_assets: z.array(z.string()).nullable().optional()}); export const tuneStatus = z.enum(["pending", "running", "succeeded", "cancelled", "failed"]); export const tuneType = z.enum(["lora_tune", "text_to_speech_latent_tune", "text_to_speech_rvc_tune", "text_to_speech_tortoise_tune"]); export const tune = z.object({created_at: z.string(), deleted_at: z.string().nullable().optional(), description: z.string(), details: z.discriminatedUnion("tune_type", [loraTuneOutput, textToSpeechLatentTune]), id: z.string(), name: z.string(), output_lora_ids: z.array(z.string()), result: tuneResult.nullable().optional(), status: tuneStatus, status_details: z.string(), succeeded_at: z.string().nullable().optional(), tenant_id: z.string(), tune_type: tuneType, updated_at: z.string()}); 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 }; /** * Get Tune * * GET /v1/tune/{tune_id} */ export const getApiV1TuneTuneId = async (args: GetApiV1TuneTuneIdArgs, options: GetApiV1TuneTuneIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/tune/${encodeURIComponent(String(args.tune_id))}${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(tune, await res.json()) };