import {z} from 'zod' export type GlobalVariable = {key?: string | undefined, type?: ('string' | 'number' | 'model' | 'processline') | undefined, value?: Record | undefined}; export type WTSSVariables = {run_date?: string | undefined, run_date_std?: string | undefined, run_mon?: string | undefined, run_mon_end?: string | undefined, run_mon_end_std?: string | undefined}; export type RunConfig = {global_variables?: Array | undefined, wtss_variables?: WTSSVariables | undefined}; export type CreateExperimentRunRequest = {exp_id?: string | undefined, exp_version_name?: string | undefined, run_config?: RunConfig | undefined}; export type CreateApiDiV2ExperimentRunArgs = {body: CreateExperimentRunRequest}; export type CreateApiDiV2ExperimentRunOptions = {baseUrl?: string | undefined, basicAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createExperimentRunResponse = z.object({exp_run_id: z.string().optional(), exp_id: z.string().optional(), exp_version_name: z.string().optional(), create_user: z.string().optional(), create_time: z.string().optional(), end_time: 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 }; /** * CreateExperimentRun * * POST /di/v2/experiment_run * * Also accepts auth: watsonAuthToken, watsonAuthTokenQuery */ export const createApiDiV2ExperimentRun = async (args: CreateApiDiV2ExperimentRunArgs, options?: CreateApiDiV2ExperimentRunOptions) => { const {baseUrl = "https://di.prophecis.com", fetch: fetchFn = fetch, basicAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuthToken !== undefined) headers.set("Authorization", basicAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/di/v2/experiment_run${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(createExperimentRunResponse, await res.json()) };