import {z} from 'zod' export type NotificationChannel = 'in_app' | 'push' | 'sms' | 'email' | 'whatsapp'; export type JobNotificationTrigger = 'complete' | 'passed' | 'failed'; export type JobNotificationRule = {recipient_user_ids?: Array | undefined, notify_job_owner?: boolean | undefined, delivery_channels?: Array | undefined, email_recipients?: Array | undefined, enabled?: boolean | undefined, on?: JobNotificationTrigger | undefined, include_report?: boolean | undefined}; export type ReportImageSize = 'small' | 'medium' | 'large'; export type ReportConfig = {image_size?: ReportImageSize | undefined}; export type WorkflowUpdate = {name?: (string | null) | undefined, description?: (string | null) | undefined, tags?: (Array | null) | undefined, allow_step_skip?: (boolean | null) | undefined, enforce_live_capture?: (boolean | null) | undefined, allow_out_of_order_steps?: (boolean | null) | undefined, allow_step_failure?: (boolean | null) | undefined, fail_threshold_percent?: (number | null) | undefined, allow_adhoc_step?: (boolean | null) | undefined, notification_rule?: (JobNotificationRule | null) | undefined, is_tiliter_created?: (boolean | null) | undefined, report_config?: (ReportConfig | null) | undefined}; export type PatchApiApiV2WorkflowsWorkflowIdArgs = {workflow_id: string, body: WorkflowUpdate}; export type PatchApiApiV2WorkflowsWorkflowIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const workflowStatus = z.enum(["active", "draft", "deleted"]); export const notificationChannel = z.enum(["in_app", "push", "sms", "email", "whatsapp"]); export const jobNotificationTrigger = z.enum(["complete", "passed", "failed"]); export const jobNotificationRule = z.object({recipient_user_ids: z.array(z.string()).optional(), notify_job_owner: z.boolean().optional(), delivery_channels: z.array(notificationChannel).optional(), email_recipients: z.array(z.string()).optional(), enabled: z.boolean().optional(), on: jobNotificationTrigger.optional(), include_report: z.boolean().optional()}); export const stepType = z.enum(["vision_agent", "selection", "barcode"]); export const passCriteriaOperator = z.enum(["eq", "ne", "gt", "gte", "lt", "lte", "contains", "not_contains", "in", "not_in", "exists", "not_exists", "length_gte", "length_lt"]); export const passCriteriaRule = z.object({path: z.string().min(1), op: passCriteriaOperator, value: z.unknown()}); export const passCriteria = z.object({combinator: z.enum(["and", "or"]), rules: z.array(passCriteriaRule)}); export const stepNotificationTrigger = z.enum(["passed", "failed", "criteria"]); export const stepNotificationRule = z.object({recipient_user_ids: z.array(z.string()).optional(), notify_job_owner: z.boolean().optional(), delivery_channels: z.array(notificationChannel).optional(), email_recipients: z.array(z.string()).optional(), on: stepNotificationTrigger, criteria: passCriteria.nullable().optional()}); export const workflowStepRead = z.object({id: z.string(), step_key: z.string().nullable().optional(), sort_order: z.number().int(), name: z.string(), workflow_note: z.string().nullable().optional(), reference_file_paths: z.array(z.string()).nullable().optional(), reference_file_urls: z.array(z.string()).nullable().optional(), operator_reference_file_urls: z.array(z.string()).optional(), step_type: stepType.optional(), step_config: z.record(z.string(), z.unknown()).optional(), agent_id: z.string().nullable().optional(), agent_parameters: z.record(z.string(), z.unknown()).nullable().optional(), pass_criteria: passCriteria.nullable().optional(), notification_rule: stepNotificationRule.nullable().optional(), allow_multiple_images: z.boolean().optional()}); export const reportImageSize = z.enum(["small", "medium", "large"]); export const reportConfig = z.object({image_size: reportImageSize.optional()}); export const workflowRead = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), tags: z.array(z.string()).optional(), is_system: z.boolean(), system_source: z.enum(["registry", "user"]).nullable().optional(), is_tiliter_created: z.boolean().optional(), is_deleted: z.boolean().optional(), status: workflowStatus.optional(), allow_step_skip: z.boolean().optional(), enforce_live_capture: z.boolean().optional(), allow_out_of_order_steps: z.boolean().optional(), allow_step_failure: z.boolean().optional(), fail_threshold_percent: z.number().int().gte(0).lte(100).optional(), allow_adhoc_step: z.boolean().optional(), notification_rule: jobNotificationRule, owner_user_id: z.string().nullable().optional(), organization_id: z.string().nullable().optional(), parent_workflow_id: z.string().nullable().optional(), unpublished_draft_id: z.string().nullable().optional(), step_count: z.number().int().optional(), active_job_count: z.number().int().optional(), access_user_count: z.number().int().optional(), created_at: z.string(), updated_at: z.string().nullable().optional(), steps: z.array(workflowStepRead).optional(), report_config: reportConfig.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 metadata * * PATCH /api/v2/workflows/{workflow_id} */ export const patchApiApiV2WorkflowsWorkflowId = async (args: PatchApiApiV2WorkflowsWorkflowIdArgs, options?: PatchApiApiV2WorkflowsWorkflowIdOptions) => { const {baseUrl = "https://api.ai.vision.tiliter.com", 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(/\/+$/, '')}/api/v2/workflows/${encodeURIComponent(String(args.workflow_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(workflowRead, await res.json()) };