import {z} from 'zod' export type PlanSuccessComparison = 'beat_baseline' | 'absolute_threshold'; export type SuccessCriterionEdit = {success_metric: string, success_comparison: PlanSuccessComparison, success_threshold: number, success_suite_id: string, success_rounds_required?: number | undefined}; export type FinetunePlanApproveRequest = {revision_hash: string, success_criterion?: (SuccessCriterionEdit | null) | undefined}; export type CreateApiV1ExperimentsExperimentIdFinetunePlansPlanIdApproveArgs = {experiment_id: string, plan_id: string, body: FinetunePlanApproveRequest}; export type CreateApiV1ExperimentsExperimentIdFinetunePlansPlanIdApproveOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const planSuccessComparison = z.enum(["beat_baseline", "absolute_threshold"]); export const finetunePlanEnvelope = z.object({expires_at: z.string(), allow_synthetic_data: z.boolean().optional(), allow_eval_upload: z.boolean().optional(), allowed_base_models: z.array(z.string().min(1).max(200)).optional(), baseline_model_ids: z.array(z.string().min(1).max(200)).optional(), allow_preview_deployment: z.boolean().optional(), allow_deployment: z.boolean().optional(), success_metric: z.string().min(1).max(200).nullable().optional(), success_comparison: planSuccessComparison.nullable().optional(), success_threshold: z.number().gte(0.000001).lte(999999.999999).nullable().optional(), success_suite_id: z.string().nullable().optional(), success_rounds_required: z.number().int().gte(1).lte(10).optional()}); export const finetunePlanIntent = z.object({objective: z.string().min(1).max(4000), success_criteria: z.string().min(1).max(4000).nullable().optional(), baseline: z.string().min(1).max(4000).nullable().optional(), dataset_reasoning: z.string().min(1).max(4000).nullable().optional(), prose_constraints: z.string().min(1).max(4000).nullable().optional(), extra: z.record(z.string(), z.string().min(1).max(4000)).optional(), estimated_cost_usd: z.string().nullable().optional(), estimated_cost_low_usd: z.string().nullable().optional(), estimated_cost_high_usd: z.string().nullable().optional()}); export const finetunePlanQuoteBasisOutput = z.object({quote_model: z.string().min(1).max(200), estimated_gpu_minutes: z.string(), estimated_instance_type: z.string().min(1).max(64)}); export const finetunePlanResponse = z.object({id: z.string(), experiment_id: z.string(), revision: z.number().int(), revision_hash: z.string(), envelope: finetunePlanEnvelope, intent: finetunePlanIntent, quote: finetunePlanQuoteBasisOutput.nullable().optional(), is_current: z.boolean(), proposed_by: z.string(), proposed_at: z.string(), approved_by: z.string().nullable(), approved_at: z.string().nullable(), rejected_by: z.string().nullable(), rejected_at: z.string().nullable()}); 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 }; /** * Approve a proposed plan revision * * POST /v1/experiments/{experiment_id}/finetune-plans/{plan_id}/approve * * Also accepts auth: BearerAuth */ export const createApiV1ExperimentsExperimentIdFinetunePlansPlanIdApprove = async (args: CreateApiV1ExperimentsExperimentIdFinetunePlansPlanIdApproveArgs, options: CreateApiV1ExperimentsExperimentIdFinetunePlansPlanIdApproveOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/experiments/${encodeURIComponent(String(args.experiment_id))}/finetune-plans/${encodeURIComponent(String(args.plan_id))}/approve${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(finetunePlanResponse, await res.json()) };