import {z} from 'zod' export type DatasetItemSourceBody = 'trace' | 'annotation' | 'manual' | 'playground'; export type DatasetSnapshotFilterBody = {logicalIds?: Array | undefined, metadata?: {[key: string]: unknown} | undefined, sources?: Array | undefined, tags?: Array | undefined}; export type ExperimentScorerRefBody = {id: string, version?: (number | null) | undefined}; export type PromptMessageBody = {content: string, role: string}; export type PromptRefOverridesBody = {model?: (string | null) | undefined, params: unknown, responseFormat: unknown, tools: unknown}; export type RemoteTaskOverridesBody = {maxConcurrency?: (number | null) | undefined, timeoutMs?: (number | null) | undefined}; export type ExperimentTaskBody = {messages: Array, model?: (string | null) | undefined, params: unknown, provider_id: string, type: 'inline_prompt'} | {id: string, params_overrides?: (PromptRefOverridesBody | null) | undefined, provider_id: string, type: 'prompt_ref', version: number} | {overrides?: (RemoteTaskOverridesBody | null) | undefined, task_ref: string, type: 'remote'} | {config: unknown, task_fingerprint: string, type: 'sdk'}; export type CreateApiApiOrgIdExperimentsPreviewArgs = {org_id: string, sampleSize?: (number | null) | undefined, body: {confirmCostEstimate?: boolean | undefined, datasetFilter?: (DatasetSnapshotFilterBody | null) | undefined, datasetId: string, datasetVersion: number, description?: (string | null) | undefined, idempotencyKey?: (string | null) | undefined, metadata: unknown, name: string, scorers: Array, task: ExperimentTaskBody, trialCount?: number | undefined}}; export type CreateApiApiOrgIdExperimentsPreviewOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const experimentScorerApplicabilityBody = z.object({eligibleRowCount: z.number().int().gte(0), eligibleSlotCount: z.number().int().gte(0), noReferenceRowCount: z.number().int().gte(0), noReferenceSlotCount: z.number().int().gte(0), scorerId: z.string(), scorerVersion: z.number().int()}); export const experimentApplicabilityPreviewBody = z.object({eligibleScoringDimensionCount: z.number().int().gte(0), eligibleTaskSlotCount: z.number().int().gte(0), fullySkippedRowCount: z.number().int().gte(0), fullySkippedSlotCount: z.number().int().gte(0), partiallySkippedRowCount: z.number().int().gte(0), partiallySkippedSlotCount: z.number().int().gte(0), scorerApplicability: z.array(experimentScorerApplicabilityBody), totalSlotCount: z.number().int().gte(0)}); export const experimentCostDimensionBody = z.object({callCount: z.number().int().gte(0), estimatedCost: z.number().nullable().optional(), inputTokensPerCall: z.number().int(), model: z.string().nullable().optional(), outputTokensPerCall: z.number().int(), scorerId: z.string().nullable().optional(), scorerVersion: z.number().int().nullable().optional()}); export const experimentCostEstimateBody = z.object({confirmationRequired: z.boolean(), currency: z.string(), dimensions: z.array(experimentCostDimensionBody), estimatedCost: z.number().nullable().optional(), incomplete: z.boolean(), slotCount: z.number().int().gte(0), taskCostEstimated: z.boolean(), warningThreshold: z.number()}); export const pinnedExperimentScorerBody = z.object({id: z.string(), version: z.number().int()}); export const experimentSlotBody = z.object({expectedOutput: z.unknown(), input: z.unknown(), logicalId: z.string(), metadata: z.unknown(), rowId: z.string(), trialIndex: z.number().int().gte(0)}); export const createApiApiOrgIdExperimentsPreviewResponse = z.object({applicability: experimentApplicabilityPreviewBody, costEstimate: experimentCostEstimateBody.nullable().optional(), datasetId: z.string(), datasetVersion: z.number().int(), pinnedScorers: z.array(pinnedExperimentScorerBody), rowCount: z.number().int().gte(0), sampleSlots: z.array(experimentSlotBody), slotCount: z.number().int().gte(0), trialCount: z.number().int().gte(0)}); 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 }; /** * POST /api/{org_id}/experiments/preview */ export const createApiApiOrgIdExperimentsPreview = async (args: CreateApiApiOrgIdExperimentsPreviewArgs, options: CreateApiApiOrgIdExperimentsPreviewOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.sampleSize != null) query.append("sampleSize", String(args.sampleSize)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/experiments/preview${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(createApiApiOrgIdExperimentsPreviewResponse, await res.json()) };