import {z} from 'zod' export type SolutionInfo = {state_id: number, solution_index: number, name?: (string | null) | undefined}; export type IntermediateSolutionRequest = {problem_id: number, session_id?: (number | null) | undefined, parent_state_id?: (number | null) | undefined, context?: (string | null) | undefined, scalarization_options?: ({[key: string]: number | string | boolean} | null) | undefined, solver?: (string | null) | undefined, solver_options?: ({[key: string]: number | string | boolean} | null) | undefined, num_desired?: (number | null) | undefined, reference_solution_1: SolutionInfo, reference_solution_2: SolutionInfo}; export type CreateApiMethodGenericIntermediateArgs = {body: IntermediateSolutionRequest}; export type CreateApiMethodGenericIntermediateOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const tensor: z.ZodType = z.union([z.array(z.lazy(() => tensor)), z.array(z.union([z.number(), z.number().int(), z.boolean()])), z.number(), z.number().int(), z.boolean(), z.literal("List")]).nullable(); export const solutionReferenceResponse = z.object({name: z.string().nullable(), solution_index: z.number().int().nullable(), state_id: z.number().int(), objective_values: z.record(z.string(), z.number()).nullable(), variable_values: z.record(z.string(), z.union([z.number(), z.number().int(), z.boolean(), tensor])).nullable()}); export const genericIntermediateSolutionResponse = z.object({state_id: z.number().int().nullable(), reference_solution_1: solutionReferenceResponse, reference_solution_2: solutionReferenceResponse, intermediate_solutions: z.array(solutionReferenceResponse)}); 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 }; /** * Solve Intermediate * * POST /method/generic/intermediate * * Also accepts auth: APIKeyCookie */ export const createApiMethodGenericIntermediate = async (args: CreateApiMethodGenericIntermediateArgs, options: CreateApiMethodGenericIntermediateOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/method/generic/intermediate${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(genericIntermediateSolutionResponse, await res.json()) };