import {z} from 'zod' export type GetApiWhatsappConversationsConversationIdFlowExecutionsArgs = {status?: ('running' | 'waiting' | 'ended' | 'failed' | 'handoff') | undefined, limit?: number | undefined, before?: string | undefined, after?: string | undefined}; export type GetApiWhatsappConversationsConversationIdFlowExecutionsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowMinimal = z.object({id: z.string(), name: z.string(), status: z.string()}); export const workflowStepReference = z.object({id: z.string(), identifier: z.string(), stepable_type: z.string().nullable().optional(), position: z.object({x: z.number().optional(), y: z.number().optional()}).and(z.record(z.string(), z.unknown())).nullable().optional()}).and(z.record(z.string(), z.unknown())); export const workflowExecutionSummary = z.object({id: z.string(), status: z.enum(["running", "waiting", "ended", "failed", "handoff"]), started_at: z.string(), ended_at: z.string().nullable().optional(), last_event_at: z.string(), tracking_id: z.string().nullable().optional(), whatsapp_conversation_id: z.string().nullable().optional(), workflow: workflowMinimal.optional(), current_step: workflowStepReference.optional()}); export const paginationCursor = z.object({before: z.string().optional(), after: z.string().optional()}); export const paging = z.object({cursors: paginationCursor.optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional()}); export const workflowExecutionListResponse = z.object({data: z.array(workflowExecutionSummary), paging: paging}); 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 conversation workflow executions * * GET /whatsapp/conversations/{conversation_id}/flow_executions */ export const getApiWhatsappConversationsConversationIdFlowExecutions = async (args: GetApiWhatsappConversationsConversationIdFlowExecutionsArgs, options?: GetApiWhatsappConversationsConversationIdFlowExecutionsOptions) => { const {baseUrl = "https://api.kapso.ai/platform/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/whatsapp/conversations/${encodeURIComponent(String(args.conversation_id))}/flow_executions${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()) };