import {z} from 'zod' export type TuneType = 'lora_tune' | 'text_to_speech_latent_tune' | 'text_to_speech_rvc_tune' | 'text_to_speech_tortoise_tune'; export type GetApiV1TunesArgs = {offset?: number | undefined, limit?: number | undefined, name?: (string | null) | undefined, tune_type?: (TuneType | null) | undefined, base_checkpoint_id?: (string | null) | undefined, trigger_words?: (Array | null) | undefined, engine?: (string | null) | undefined}; export type GetApiV1TunesOptions = {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 listTunesResponse = z.object({data: z.array(tune), has_more: z.boolean(), total: z.number().int()}); 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 }; /** * List Tunes * * GET /v1/tunes */ export const getApiV1Tunes = async (args: GetApiV1TunesArgs, options: GetApiV1TunesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.name != null) query.append("name", String(args.name)) if (args.tune_type != null) query.append("tune_type", String(args.tune_type)) if (args.base_checkpoint_id != null) query.append("base_checkpoint_id", String(args.base_checkpoint_id)) for (const item of args.trigger_words ?? []) query.append("trigger_words", String(item)) if (args.engine != null) query.append("engine", String(args.engine)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/tunes${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(listTunesResponse, await res.json()) };