import {z} from 'zod' export type NERClassifiedExample = {text: string, entities: Array>, feedback?: ('positive' | 'negative' | null) | undefined}; export type ClassifiedExample = {text: string, label: string, feedback?: ('positive' | 'negative' | null) | undefined}; export type RecordField = {name: string, type?: string | undefined, description?: (string | null) | undefined, allowed_values?: (Array | null) | undefined}; export type ConstraintRequest = {description: string, choices?: (Array | null) | undefined, weights?: (Array | null) | undefined, probability?: (number | null) | undefined}; export type MultiplicatorRequest = {prompt: string, choices: Array}; export type SplitRatioConfig = {training?: number | undefined, evaluation?: number | undefined}; export type GenerateRequest = {task_type: 'ner' | 'classification' | 'custom' | 'decoder' | 'records' | 'fields', dataset_name: string, num_examples?: number | undefined, domain_description?: (string | null) | undefined, temperature?: number | undefined, quality?: (string | null) | undefined, generation_profile?: ('auto' | 'fast' | 'balanced' | 'quality') | undefined, session_id?: (string | null) | undefined, config_num_examples?: number | undefined, seed?: (number | null) | undefined, labels?: (Array | null) | undefined, classified_examples?: (Array | null) | undefined, multi_label?: boolean | undefined, class_balance?: ({[key: string]: number} | null) | undefined, batch_size?: number | undefined, negative_ratio?: (number | null) | undefined, fields?: (Array | null) | undefined, input_fields?: (Array | null) | undefined, output_fields?: (Array | null) | undefined, prompt?: (string | null) | undefined, output_format?: ({[key: string]: unknown} | null) | undefined, infer_output_format?: boolean | undefined, instruction?: (string | null) | undefined, include_reasoning_trace?: boolean | undefined, reasoning_effort?: (string | null) | undefined, constraints?: (Array | null) | undefined, multiplicator?: (MultiplicatorRequest | null) | undefined, use_meta_synthesizer?: boolean | undefined, min_criteria?: number | undefined, target_choices?: number | undefined, project_id?: (string | null) | undefined, type?: string | undefined, visibility?: string | undefined, split_ratio?: (SplitRatioConfig | null) | undefined, output_style?: ('prose' | 'code' | 'mixed' | null) | undefined}; export type CreateApiV1GenerateArgs = {is_seed?: boolean | undefined, synthesis_session_id?: (string | null) | undefined, body: GenerateRequest}; export type CreateApiV1GenerateOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const generateAsyncResponse = z.object({job_id: z.string(), status: z.string().optional(), dataset_name: z.string(), task_type: z.string(), is_seed: z.boolean().optional(), message: z.string().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 }; /** * Create Generation Job * * POST /v1/generate * * Also accepts auth: BearerAuth */ export const createApiV1Generate = async (args: CreateApiV1GenerateArgs, options: CreateApiV1GenerateOptions) => { 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 (args.is_seed != null) query.append("is_seed", String(args.is_seed)) if (args.synthesis_session_id != null) query.append("synthesis_session_id", String(args.synthesis_session_id)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/generate${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(generateAsyncResponse, await res.json()) };