import {z} from 'zod' export type DSSInfo = {flow_name?: string | undefined, flow_version?: string | undefined, flow_id?: number | undefined, project_name?: string | undefined, project_id?: number | undefined, workspace_name?: string | undefined, workspace_id?: number | undefined}; export type CreateExperimentRequest = {name: string, group_id?: string | undefined, group_name?: string | undefined, description?: string | undefined, tags?: Array | undefined, source_system?: ('MLSS' | 'DSS' | 'WTSS') | undefined, cluster_type?: ('BDAP' | 'BDP') | undefined, dss_info?: DSSInfo | undefined, flow_json?: string | undefined, flow_json_upload_id?: string | undefined}; export type CreateApiDiV2ExperimentArgs = {body: CreateExperimentRequest}; export type CreateApiDiV2ExperimentOptions = {baseUrl?: string | undefined, basicAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createExperimentResponse = z.object({id: z.string()}); 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 }; /** * CreateExperiment * * POST /di/v2/experiment * * Also accepts auth: watsonAuthToken, watsonAuthTokenQuery */ export const createApiDiV2Experiment = async (args: CreateApiDiV2ExperimentArgs, options?: CreateApiDiV2ExperimentOptions) => { 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${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(createExperimentResponse, await res.json()) };