import {z} from 'zod' export type AutomationResultSortField = 'default' | 'completed'; export type AutomationExecutionStatus = 'pending' | 'executing' | 'completed' | 'failed' | 'cancelled'; export type SignalSourceType = 'purchase_order' | 'meeting_minutes' | 'annual_budgets' | 'procurement_guidelines' | 'youtube' | 'rfp' | 'grants' | 'press_releases'; export type GetApiApiV3AutomationsAutomationIdRunsRunIdResultsArgs = {automation_id: string, run_id: string, offset?: number | undefined, limit?: number | undefined, crm_id?: (string | null) | undefined, sort?: (AutomationResultSortField | null) | undefined, statuses?: (Array | null) | undefined, institution_ids?: (Array | null) | undefined, states?: (Array | null) | undefined, crm_linked?: (boolean | null) | undefined, search?: (string | null) | undefined, crm_pushed?: (boolean | null) | undefined, crm_pushed_after?: (string | null) | undefined, crm_pushed_before?: (string | null) | undefined, vendors?: (Array | null) | undefined, products?: (Array | null) | undefined, start_date_after?: (string | null) | undefined, start_date_before?: (string | null) | undefined, end_date_after?: (string | null) | undefined, end_date_before?: (string | null) | undefined, signal_setting_ids?: (Array | null) | undefined, data_sources?: (Array | null) | undefined, roles?: (Array | null) | undefined}; export type GetApiApiV3AutomationsAutomationIdRunsRunIdResultsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const automationExecutionStatus = z.enum(["pending", "executing", "completed", "failed", "cancelled"]); export const cRMType = z.enum(["hubspot", "salesforce", "zapier"]); export const cRMPushStatus = z.object({pushed_at: z.string().nullable().optional()}); export const signalSignificance = z.enum(["low", "medium", "high"]); export const institutionEnum = z.enum(["k12_districts", "cities", "counties", "higher_education", "federal_agencies", "federal_departments", "individual_units", "k12_schools", "police_agencies", "usfa_registry", "state_agencies", "states"]); export const signalInstitution = z.object({name: z.string(), state: z.string(), type: institutionEnum, linked_providers: z.array(cRMType).optional()}); export const signalStatusEnum = z.enum(["active", "dismissed", "pending", "acknowledged"]); export const signalSourceType = z.enum(["purchase_order", "meeting_minutes", "annual_budgets", "procurement_guidelines", "youtube", "rfp", "grants", "press_releases"]); export const enrichedSignal = z.object({id: z.string(), signal_setting_id: z.string(), institution_id: z.string(), significance: signalSignificance, reason: z.string().nullable(), summary: z.string().nullable(), title: z.string().nullable(), metadata_: z.record(z.string(), z.unknown()).nullable(), created_at: z.string(), document_date: z.string().nullable().optional(), query: z.string(), institution: signalInstitution, status: signalStatusEnum, source_type: signalSourceType, keywords: z.array(z.string()).optional(), list_ids: z.array(z.string()).optional()}); export const automationResultResponse = z.object({id: z.string(), resource_type: z.string(), resource_id: z.string(), rank: z.number(), position: z.number().int(), status: automationExecutionStatus, data: z.record(z.string(), z.unknown()), linked_providers: z.array(cRMType).optional(), crm_push_status_by_provider: z.record(z.string(), cRMPushStatus).optional(), signals: z.array(enrichedSignal).optional()}); export const paginatedResponseAutomationResultResponse = z.object({results: z.array(automationResultResponse), total: z.number().int(), offset: z.number().int(), limit: z.number().int()}); 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 }; /** * Get Run Results * * GET /api/v3/automations/{automation_id}/runs/{run_id}/results */ export const getApiApiV3AutomationsAutomationIdRunsRunIdResults = async (args: GetApiApiV3AutomationsAutomationIdRunsRunIdResultsArgs, options: GetApiApiV3AutomationsAutomationIdRunsRunIdResultsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.crm_id != null) query.append("crm_id", String(args.crm_id)) if (args.sort != null) query.append("sort", String(args.sort)) for (const item of args.statuses ?? []) query.append("statuses", String(item)) for (const item of args.institution_ids ?? []) query.append("institution_ids", String(item)) for (const item of args.states ?? []) query.append("states", String(item)) if (args.crm_linked != null) query.append("crm_linked", String(args.crm_linked)) if (args.search != null) query.append("search", String(args.search)) if (args.crm_pushed != null) query.append("crm_pushed", String(args.crm_pushed)) if (args.crm_pushed_after != null) query.append("crm_pushed_after", String(args.crm_pushed_after)) if (args.crm_pushed_before != null) query.append("crm_pushed_before", String(args.crm_pushed_before)) for (const item of args.vendors ?? []) query.append("vendors", String(item)) for (const item of args.products ?? []) query.append("products", String(item)) if (args.start_date_after != null) query.append("start_date_after", String(args.start_date_after)) if (args.start_date_before != null) query.append("start_date_before", String(args.start_date_before)) if (args.end_date_after != null) query.append("end_date_after", String(args.end_date_after)) if (args.end_date_before != null) query.append("end_date_before", String(args.end_date_before)) for (const item of args.signal_setting_ids ?? []) query.append("signal_setting_ids", String(item)) for (const item of args.data_sources ?? []) query.append("data_sources", String(item)) for (const item of args.roles ?? []) query.append("roles", String(item)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/automations/${encodeURIComponent(String(args.automation_id))}/runs/${encodeURIComponent(String(args.run_id))}/results${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedResponseAutomationResultResponse, await res.json()) };