import {z} from 'zod' export type GetApiDiV2ExperimentRunsArgs = {exp_id: string, exp_version_name?: string | undefined, status?: ('Initializing' | 'Running' | 'Succeed' | 'Failed' | 'Cancelled') | undefined, create_time_st?: string | undefined, create_time_ed?: string | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiDiV2ExperimentRunsOptions = {baseUrl?: string | undefined, basicAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const experimentRun = z.object({exp_run_id: z.string().optional(), exp_id: z.string().optional(), exp_version_name: z.string().optional(), execute_status: z.enum(["Initializing", "Running", "Succeed", "Failed", "Cancelled", "Deleted"]).optional(), create_user: z.string().optional(), create_time: z.string().optional(), end_time: z.string().optional()}); export const listExperimentRunsResponse = z.object({total_page: z.number().optional(), total: z.number().optional(), page_number: z.number().optional(), page_size: z.number().optional(), experiment_runs: z.array(experimentRun).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 }; /** * ListExperimentRuns * * GET /di/v2/experiment_runs * * Also accepts auth: watsonAuthToken, watsonAuthTokenQuery */ export const getApiDiV2ExperimentRuns = async (args: GetApiDiV2ExperimentRunsArgs, options?: GetApiDiV2ExperimentRunsOptions) => { const {baseUrl = "https://di.prophecis.com", fetch: fetchFn = fetch, basicAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.exp_id != null) query.append("exp_id", String(args.exp_id)) if (args.exp_version_name != null) query.append("exp_version_name", String(args.exp_version_name)) if (args.status != null) query.append("status", String(args.status)) if (args.create_time_st != null) query.append("create_time_st", String(args.create_time_st)) if (args.create_time_ed != null) query.append("create_time_ed", String(args.create_time_ed)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (basicAuthToken !== undefined) headers.set("Authorization", basicAuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/di/v2/experiment_runs${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(listExperimentRunsResponse, await res.json()) };