import {z} from 'zod' export type CreateTaskRequest = {title: string, description?: (string | null) | undefined, priority?: string | undefined, runner?: string | undefined}; export type CreateApiAdminCompaniesCompanyIdTasksArgs = {company_id: string, body: CreateTaskRequest}; export type CreateApiAdminCompaniesCompanyIdTasksOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const taskStatus = z.enum(["pending", "dispatched", "in_progress", "completed", "failed", "moderated"]); export const taskPriority = z.enum(["critical", "high", "normal", "low"]); export const taskCreatedBy = z.enum(["user", "ceo_agent", "worker_agent", "agent", "platform"]); export const taskResponse = z.object({id: z.string(), company_id: z.string(), title: z.string(), description: z.string().nullable().optional(), status: taskStatus, priority: taskPriority, created_by: taskCreatedBy, runner: z.string().optional(), assigned_to: z.string().nullable().optional(), result_summary: z.string().nullable().optional(), error_message: z.string().nullable().optional(), retry_count: z.number().int(), max_retries: z.number().int(), parent_task_id: z.string().nullable().optional(), cycle_id: z.string().nullable().optional(), due_at: z.string().nullable().optional(), recurrence: z.string().nullable().optional(), effective_status: z.string().nullable().optional(), due_annotation: z.string().nullable().optional(), last_run_at: z.string().nullable().optional(), last_run_missed: z.boolean().optional(), created_at: z.string(), started_at: z.string().nullable().optional(), completed_at: z.string().nullable().optional(), updated_at: z.string()}); 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 }; /** * Admin Create Task * * POST /admin/companies/{company_id}/tasks */ export const createApiAdminCompaniesCompanyIdTasks = async (args: CreateApiAdminCompaniesCompanyIdTasksArgs, options: CreateApiAdminCompaniesCompanyIdTasksOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/admin/companies/${encodeURIComponent(String(args.company_id))}/tasks${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(taskResponse, await res.json()) };