import {z} from 'zod' export type TriggerNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'trigger', trigger_type?: ('manual' | 'schedule' | 'webhook' | 'gateway' | 'embed' | 'email' | 'object-store' | 'record' | 'task' | 'email-delivery' | 'mail') | undefined, payload: {[key: string]: unknown}}; export type AgentNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'agent', payload: {agent?: ({name: string, llm_provider: 'claude' | 'anthropic', model: string, description?: string | undefined, behaviour?: string | undefined, output_schema?: {[key: string]: string} | undefined, has_tools?: boolean | undefined, tools?: Array | undefined, knowledge?: Array | undefined, timeout?: number | undefined}) | undefined, subject?: string | undefined, node_subject?: string | undefined, prompt?: (string | Record) | undefined, await?: boolean | undefined, knowledge?: Array | undefined}}; export type NodeOptions = {flat_map?: boolean | undefined, backoff_ms?: number | undefined, timeout?: number | undefined, attempts?: number | undefined, ignore_response_codes?: Array | undefined}; export type FunctionNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'function', subject: string, payload: unknown, response_map: unknown, options?: NodeOptions | undefined}; export type HttpPayload = {base_url?: string | undefined, url: string, method: string, data?: Record | undefined, params?: Record | undefined, query?: Record | undefined, headers?: {[key: string]: string} | undefined, timeout?: number | undefined}; export type IntegrationNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'integration', integration_id?: string | undefined, request_type_id?: string | undefined, oauth?: string | undefined, payload: HttpPayload, options?: NodeOptions | undefined}; export type HttpNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'http', payload: HttpPayload, options?: NodeOptions | undefined}; export type FlowNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'flow', subject: string, payload: unknown, await?: boolean | undefined, response_map: unknown, options?: NodeOptions | undefined}; export type EvalNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'eval', payload: {code: string, params: {[key: string]: unknown}}, options?: NodeOptions | undefined}; export type ConditionNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'condition', payload: {rules?: (Array<{if?: string | undefined, then?: Array | undefined, else?: Array | undefined}>) | undefined}}; export type RulesNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'rules', payload: {rules: Record, facts?: Record | undefined, context?: Record | undefined}}; export type MapNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'map', payload: Record | Array, options?: ({flat_map?: boolean | undefined}) | undefined}; export type StaticNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'static', payload: {[key: string]: unknown}}; export type DelayNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'delay', payload: {time_ms: number}}; export type ScheduleNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'schedule', payload: {flow_subject: string, trigger: unknown, trigger_in?: string | undefined, trigger_on?: string | undefined}}; export type EscalationPayload = {message: string, title?: string | undefined, description?: string | undefined, priority?: string | undefined, assignees?: string | undefined, fields?: ({[key: string]: unknown} | string) | undefined, task_id?: string | undefined, keep_status?: boolean | undefined}; export type InputNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'input', payload: EscalationPayload}; export type HumanInTheLoopNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'human-in-the-loop', payload: EscalationPayload}; export type QuivaEndpointNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'quiva-endpoint', subject: string, payload: unknown}; export type ErrorNodeData = {id: string, name?: string | undefined, description?: string | undefined, node_type: 'error', payload: {status_code: number, message?: string | undefined}}; export type NodeData = TriggerNodeData | AgentNodeData | FunctionNodeData | IntegrationNodeData | HttpNodeData | FlowNodeData | EvalNodeData | ConditionNodeData | RulesNodeData | MapNodeData | StaticNodeData | DelayNodeData | ScheduleNodeData | InputNodeData | HumanInTheLoopNodeData | QuivaEndpointNodeData | ErrorNodeData; export type CreateNodeRequest = {collection: string, data: NodeData}; export type CreateApiHubNodesArgs = {body: CreateNodeRequest}; export type CreateApiHubNodesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const modified = z.number().int(); export const createNodeResponse = z.object({subject: z.string().optional(), collection: z.string().optional(), data: z.object({}).optional(), type: z.string().optional(), modified: modified.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 }; /** * Create a new node * * POST /hub/nodes * * Also accepts auth: apiKeyAuth */ export const createApiHubNodes = async (args: CreateApiHubNodesArgs, options?: CreateApiHubNodesOptions) => { const {baseUrl = "https://api.quiva.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/hub/nodes${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(createNodeResponse, await res.json()) };