import {z} from 'zod' export type IWorkflowTriggerEnum = 'inbound_call' | 'inbound_chat' | 'booking_created' | 'booking_missed' | 'manual'; export type IWorkflowStatusEnum = 'active' | 'paused'; export type IWorkflowUpdate = {name?: (string | null) | undefined, description?: (string | null) | undefined, assistant_id?: (string | null) | undefined, trigger?: (IWorkflowTriggerEnum | null) | undefined, status?: (IWorkflowStatusEnum | null) | undefined, definition?: ({[key: string]: unknown} | null) | undefined, priority?: (number | null) | undefined}; export type UpdateApiApiV1WorkflowWorkflowIdArgs = {workflow_id: string, body: IWorkflowUpdate}; export type UpdateApiApiV1WorkflowWorkflowIdOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const iWorkflowTriggerEnum = z.enum(["inbound_call", "inbound_chat", "booking_created", "booking_missed", "manual"]); export const iWorkflowStatusEnum = z.enum(["active", "paused"]); export const iWorkflowRead = z.object({name: z.string(), description: z.string().nullable().optional(), org_id: z.string().nullable().optional(), assistant_id: z.string().nullable().optional(), trigger: iWorkflowTriggerEnum, status: iWorkflowStatusEnum.optional(), definition: z.record(z.string(), z.unknown()).optional(), version: z.number().int().optional(), priority: z.number().int().optional(), id: z.string(), created_by_id: z.string().nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const iPutResponseBaseIWorkflowRead = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iWorkflowRead.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 }; /** * Update Workflow * * PUT /api/v1/workflow/{workflow_id} * * Also accepts auth: HTTPBearer */ export const updateApiApiV1WorkflowWorkflowId = async (args: UpdateApiApiV1WorkflowWorkflowIdArgs, options: UpdateApiApiV1WorkflowWorkflowIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/workflow/${encodeURIComponent(String(args.workflow_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(iPutResponseBaseIWorkflowRead, await res.json()) };