import {z} from 'zod' export type GetApiWorkflowsArgs = {status?: ('draft' | 'active' | 'archived') | undefined, name_contains?: string | undefined, created_after?: string | undefined, created_before?: string | undefined}; export type GetApiWorkflowsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const workflow = z.object({id: z.string(), name: z.string(), slug: z.string(), description: z.string().nullable().optional(), status: z.enum(["draft", "active", "archived"]), lock_version: z.number().int().nullable().optional(), message_debounce_seconds: z.number().int().nullable().optional(), agent_default_tools_version: z.number().int().nullable().optional(), inbound_message_read_mode: z.enum(["disabled", "read_only", "read_with_typing"]).optional(), created_at: z.string(), updated_at: z.string(), project_id: z.string().optional(), execution_count: z.number().int().gte(0).optional(), last_executed_at: z.string().nullable().optional()}); export const workflowListResponse = z.object({data: z.array(workflow)}); 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 workflows * * GET /workflows */ export const getApiWorkflows = async (args: GetApiWorkflowsArgs, options?: GetApiWorkflowsOptions) => { 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.name_contains != null) query.append("name_contains", String(args.name_contains)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/workflows${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(workflowListResponse, await res.json()) };