import {z} from 'zod' export type WorkflowDefinitionNodePosition = {x: number, y: number}; export type WorkflowNodeStartConfig = Record; export type WorkflowNodeSendTextConfig = {message: string | {[key: string]: unknown}, phone_number_id?: (string | null) | undefined, whatsapp_config_id?: (string | null) | undefined, to_phone_number?: (string | null) | undefined, delay_seconds?: (number | null) | undefined, provider_model_id?: (string | null) | undefined, provider_model_name?: (string | null) | undefined, ai_field_config?: {[key: string]: unknown} | undefined}; export type WorkflowNodeSendTemplateConfig = {template_id: string, template_name?: (string | null) | undefined, parameters: unknown, phone_number_id?: (string | null) | undefined, whatsapp_config_id?: (string | null) | undefined, to_phone_number?: (string | null) | undefined, provider_model_id?: (string | null) | undefined, provider_model_name?: (string | null) | undefined, ai_field_config?: {[key: string]: unknown} | undefined}; export type WorkflowNodeInteractiveButton = {id: string, title: string}; export type WorkflowNodeInteractiveListRow = {id: string, title: string, description?: string | undefined}; export type WorkflowNodeInteractiveListSection = {title?: string | undefined, rows: Array}; export type WorkflowNodeSendInteractiveConfig = {interactive_type: 'button' | 'list' | 'cta_url' | 'flow' | 'product' | 'product_list' | 'catalog_message' | 'location_request_message', body_text: string | {[key: string]: unknown}, footer_text?: (string | null) | undefined, header_type?: ('none' | 'text' | 'image' | 'video' | 'document' | 'null' | null) | undefined, header_text?: (string | null) | undefined, header_media_url?: (string | null) | undefined, buttons?: Array | undefined, list_button_text?: (string | null) | undefined, list_sections?: Array | undefined, cta_display_text?: (string | null) | undefined, cta_url?: (string | null) | undefined, flow_id?: (string | null) | undefined, flow_cta?: (string | null) | undefined, flow_token?: (string | null) | undefined, flow_action?: ('navigate' | 'data_exchange' | 'null' | null) | undefined, flow_action_payload?: ({[key: string]: unknown} | null) | undefined, header_config?: ({[key: string]: unknown} | null) | undefined, action_config?: ({[key: string]: unknown} | null) | undefined, phone_number_id?: (string | null) | undefined, whatsapp_config_id?: (string | null) | undefined, to_phone_number?: (string | null) | undefined, provider_model_id?: (string | null) | undefined, provider_model_name?: (string | null) | undefined, ai_field_config?: {[key: string]: unknown} | undefined}; export type WorkflowNodeWaitForResponseConfig = {has_timeout?: boolean | undefined, timeout_seconds?: (number | null) | undefined, save_response_to?: (string | null) | undefined}; export type WorkflowNodeDecideCondition = {id: string, label: string, description?: (string | null) | undefined}; export type WorkflowNodeDecideConfig = {decision_type: 'ai' | 'function', conditions: Array, llm_configuration: {[key: string]: unknown}, function_id?: (string | null) | undefined, function_name?: (string | null) | undefined, provider_model_id?: (string | null) | undefined, provider_model_name?: (string | null) | undefined, llm_temperature?: (number | null) | undefined, llm_max_tokens?: (number | null) | undefined}; export type WorkflowNodeFunctionConfig = {function_id: string, function_name?: (string | null) | undefined, save_response_to?: (string | null) | undefined}; export type WorkflowNodeWebhookConfig = {url: string, method?: ('GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE') | undefined, headers?: ({[key: string]: string} | string) | undefined, body_template: unknown, provider_model_id?: (string | null) | undefined, provider_model_name?: (string | null) | undefined, ai_field_config?: {[key: string]: unknown} | undefined, save_response_to?: (string | null) | undefined}; export type WorkflowNodeAgentFunctionTool = {name?: string | undefined, description?: (string | null) | undefined, function_id?: (string | null) | undefined, function_name?: (string | null) | undefined, input_schema?: {[key: string]: unknown} | undefined}; export type WorkflowNodeAgentWebhook = {name?: string | undefined, description?: (string | null) | undefined, url?: (string | null) | undefined, http_method?: (string | null) | undefined, headers?: ({[key: string]: unknown} | null) | undefined, body: unknown, body_schema?: ({[key: string]: unknown} | null) | undefined, jmespath_query?: (string | null) | undefined, ai_field_config?: {[key: string]: unknown} | undefined}; export type WorkflowNodeAgentKnowledgeBase = {name?: string | undefined, description?: (string | null) | undefined, knowledge_base_text?: (string | null) | undefined}; export type WorkflowNodeAgentMcpServer = {name?: string | undefined, description?: (string | null) | undefined, url?: (string | null) | undefined, headers?: ({[key: string]: unknown} | null) | undefined}; export type WorkflowNodeAgentResource = {id?: (string | null) | undefined, resource_type?: 'github_repository' | undefined, repo_url?: string | undefined, owner?: (string | null) | undefined, repo_name?: (string | null) | undefined, branch?: (string | null) | undefined, pat?: (string | null) | undefined, has_pat?: (boolean | null) | undefined, imported_missing_pat?: (boolean | null) | undefined}; export type WorkflowNodeAgentConfig = {system_prompt?: (string | null) | undefined, provider_model_id?: (string | null) | undefined, provider_model_name?: (string | null) | undefined, temperature?: (number | null) | undefined, max_iterations?: (number | null) | undefined, max_tokens?: (number | null) | undefined, reasoning_effort?: ('none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'null' | null) | undefined, observer_prompt_mode?: ('analysis_only' | 'interactive_chat') | undefined, message_delivery_mode?: ('auto_send_assistant_text' | 'tool_only') | undefined, enabled_default_tools?: Array | undefined, default_tool_configs?: ({emit_event?: ({event_definition_ids?: Array | undefined}) | undefined} | {[key: string]: unknown}) | undefined, flow_agent_function_tools?: Array | undefined, flow_agent_webhooks?: Array | undefined, flow_agent_knowledge_bases?: Array | undefined, flow_agent_mcp_servers?: Array | undefined, flow_agent_resources?: Array | undefined}; export type WorkflowNodeCallConfig = {workflow_id?: (string | null) | undefined, workflow_name?: (string | null) | undefined, save_error_to?: (string | null) | undefined}; export type WorkflowNodeHandoffConfig = {reason?: (string | null) | undefined, context_data: unknown}; export type WorkflowNodeSetVariableConfig = {variable_name: string, variable_value: unknown, value_type?: ('string' | 'number' | 'boolean' | 'json') | undefined}; export type WorkflowNodeEmitEventConfig = {event_name: string, properties?: ({[key: string]: string | number | boolean | null}) | undefined, occurred_at?: (string | null) | undefined}; export type WorkflowDefinitionInputNodeData = {node_type: 'start' | 'send_text' | 'send_template' | 'send_interactive' | 'wait_for_response' | 'decide' | 'function' | 'webhook' | 'agent' | 'call' | 'handoff' | 'set_variable' | 'emit_event', config: WorkflowNodeStartConfig | WorkflowNodeSendTextConfig | WorkflowNodeSendTemplateConfig | WorkflowNodeSendInteractiveConfig | WorkflowNodeWaitForResponseConfig | WorkflowNodeDecideConfig | WorkflowNodeFunctionConfig | WorkflowNodeWebhookConfig | WorkflowNodeAgentConfig | WorkflowNodeCallConfig | WorkflowNodeHandoffConfig | WorkflowNodeSetVariableConfig | WorkflowNodeEmitEventConfig, display_name?: string | undefined}; export type WorkflowDefinitionInputNode = {id: string, type?: string | undefined, position: WorkflowDefinitionNodePosition, data: WorkflowDefinitionInputNodeData}; export type WorkflowDefinitionInputEdge = {id?: string | undefined, source: string, target: string, label?: string | undefined, type?: string | undefined, flow_condition_id?: (string | null) | undefined}; export type WorkflowDefinitionInput = {nodes?: Array | undefined, edges?: Array | undefined}; export type WorkflowCreateRequest = {workflow: {name: string, slug?: string | undefined, description?: (string | null) | undefined, definition?: WorkflowDefinitionInput | undefined}}; export type CreateApiWorkflowsArgs = {body: WorkflowCreateRequest}; export type CreateApiWorkflowsOptions = {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 workflowResponse = z.object({data: 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 }; /** * Create workflow * * POST /workflows */ export const createApiWorkflows = async (args: CreateApiWorkflowsArgs, options?: CreateApiWorkflowsOptions) => { const {baseUrl = "https://api.kapso.ai/platform/v1", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/workflows${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(workflowResponse, await res.json()) };