import {z} from 'zod' export type WorkflowExecutionStatus = 'RUNNING' | 'COMPLETED' | 'FAILED' | 'CANCELED' | 'TERMINATED' | 'CONTINUED_AS_NEW' | 'TIMED_OUT' | 'RETRYING_AFTER_ERROR'; export type GetApiV1WorkflowsRunsArgs = {workflow_identifier?: (string | null) | undefined, root_execution_id?: (string | null) | undefined, search?: (string | null) | undefined, status?: (WorkflowExecutionStatus | Array | null) | undefined, deployment_name?: (string | null) | undefined, sort_by?: ('start_time' | 'end_time' | null) | undefined, order?: ('asc' | 'desc') | undefined, start_time_after?: (string | null) | undefined, start_time_before?: (string | null) | undefined, end_time_after?: (string | null) | undefined, end_time_before?: (string | null) | undefined, user_id?: (string | null) | undefined, workflow_tags?: (Array | null) | undefined, include_internal?: boolean | undefined, page_size?: number | undefined, next_page_token?: (string | null) | undefined, search_key?: (Array | null) | undefined}; export type GetApiV1WorkflowsRunsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowExecutionStatus = z.enum(["RUNNING", "COMPLETED", "FAILED", "CANCELED", "TERMINATED", "CONTINUED_AS_NEW", "TIMED_OUT", "RETRYING_AFTER_ERROR"]); export const workflowExecutionWithoutResultResponse = z.object({workflow_name: z.string(), workflow_id: z.string().nullable().optional(), deployment_name: z.string().nullable().optional(), execution_id: z.string(), parent_execution_id: z.string().nullable().optional(), root_execution_id: z.string(), run_id: z.string().nullable().optional(), user_id: z.string().nullable().optional(), status: workflowExecutionStatus.nullable(), start_time: z.string(), end_time: z.string().nullable(), total_duration_ms: z.number().int().nullable().optional()}); export const workflowExecutionListResponse = z.object({executions: z.array(workflowExecutionWithoutResultResponse), next_page_token: z.string().nullable().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 }; /** * List Runs * * GET /v1/workflows/runs */ export const getApiV1WorkflowsRuns = async (args: GetApiV1WorkflowsRunsArgs, options?: GetApiV1WorkflowsRunsOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.workflow_identifier != null) query.append("workflow_identifier", String(args.workflow_identifier)) if (args.root_execution_id != null) query.append("root_execution_id", String(args.root_execution_id)) if (args.search != null) query.append("search", String(args.search)) if (args.status != null) query.append("status", String(args.status)) if (args.deployment_name != null) query.append("deployment_name", String(args.deployment_name)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.order != null) query.append("order", String(args.order)) if (args.start_time_after != null) query.append("start_time_after", String(args.start_time_after)) if (args.start_time_before != null) query.append("start_time_before", String(args.start_time_before)) if (args.end_time_after != null) query.append("end_time_after", String(args.end_time_after)) if (args.end_time_before != null) query.append("end_time_before", String(args.end_time_before)) if (args.user_id != null) query.append("user_id", String(args.user_id)) for (const item of args.workflow_tags ?? []) query.append("workflow_tags", String(item)) if (args.include_internal != null) query.append("include_internal", String(args.include_internal)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.next_page_token != null) query.append("next_page_token", String(args.next_page_token)) for (const item of args.search_key ?? []) query.append("search_key", String(item)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workflows/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(workflowExecutionListResponse, await res.json()) };