import {z} from 'zod' export type AutomationType = 'ranked_institutions' | 'vendor_contract_enrichment' | 'outreach_sequence' | 'deep_research' | 'contacts_export'; export type AutomationStatus = 'active' | 'archived' | 'deleted'; export type SignalSourceType = 'purchase_order' | 'meeting_minutes' | 'annual_budgets' | 'procurement_guidelines' | 'youtube' | 'rfp' | 'grants' | 'press_releases'; export type RankedInstitutionsFilters = {signal_setting_ids?: Array | undefined, data_sources?: Array | undefined}; export type RankedRanking = {ng_score?: number | undefined, population?: number | undefined, recency?: number | undefined, signal_count?: number | undefined}; export type RankedInstitutionsConfig = {version: string, filters: RankedInstitutionsFilters, ranking: RankedRanking}; export type TerritoryVisibility = 'private' | 'organization'; export type InstitutionFiltersEnum = 'institution_type' | 'institution_list'; export type InstitutionTypePayload = {name?: (string | null) | undefined, institution_type?: (string | null) | undefined, account_types?: (Array | null) | undefined, states?: (Array | null) | undefined, counties?: (Array | null) | undefined, cities?: (Array | null) | undefined, population_min?: (number | null) | undefined, population_max?: (number | null) | undefined, search_query?: (string | null) | undefined}; export type InstitutionListPayload = {institution_ids?: Array | undefined, name: string, original_filename?: (string | null) | undefined, original_filter?: (InstitutionTypePayload | null) | undefined}; export type UserProfileInstitutionFilter = {id?: (string | null) | undefined, visibility?: TerritoryVisibility | undefined, type: InstitutionFiltersEnum, payload: InstitutionTypePayload | InstitutionListPayload}; export type VendorWithProducts = {name: string, products: Array}; export type VendorContractEnrichmentConfig = {version: string, filters: Array, vendors: Array, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, workspace_id?: (string | null) | undefined}; export type OutreachSequenceFilters = {territories?: Array | undefined, signal_setting_ids?: Array | undefined, data_sources?: Array | undefined, signal_start_date?: (string | null) | undefined, signal_end_date?: (string | null) | undefined}; export type OutreachSequenceConfig = {version: string, filters: OutreachSequenceFilters, contact_titles_include?: Array | undefined, contact_titles_exclude?: Array | undefined, email_sequence_id: string}; export type DeepResearchQuery = {id?: string | undefined, query: string, output_type: string, column_name: string, options?: ({[key: string]: unknown} | null) | undefined}; export type DeepResearchConfig = {version: string, filters: Array, queries: Array, context?: (string | null) | undefined}; export type ContactsExportConfig = {version: string, filters: Array, role_types?: Array | undefined, exclude_titles?: Array | undefined, hard_match?: boolean | undefined}; export type CreateAutomationRequest = {name: string, type: AutomationType, status?: AutomationStatus | undefined, config: RankedInstitutionsConfig | VendorContractEnrichmentConfig | OutreachSequenceConfig | DeepResearchConfig | ContactsExportConfig}; export type CreateApiApiV3AutomationsArgs = {body: CreateAutomationRequest}; export type CreateApiApiV3AutomationsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const automationType = z.enum(["ranked_institutions", "vendor_contract_enrichment", "outreach_sequence", "deep_research", "contacts_export"]); export const automationStatus = z.enum(["active", "archived", "deleted"]); export const automationResponse = z.object({id: z.string(), name: z.string(), type: automationType, status: automationStatus, config: z.record(z.string(), z.unknown()), created_at: z.string(), 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 }; /** * Create Automation * * POST /api/v3/automations */ export const createApiApiV3Automations = async (args: CreateApiApiV3AutomationsArgs, options: CreateApiApiV3AutomationsOptions) => { 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(/\/+$/, '')}/api/v3/automations${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(automationResponse, await res.json()) };