import {z} from 'zod' export type FilterOperator = 'eq' | 'ne' | 'gt' | 'lt' | 'gte' | 'lte' | 'in' | 'nin' | 'contains' | 'starts_with' | 'ends_with' | 'regex' | 'exists' | 'is_null' | 'text' | 'phrase' | 'words' | 'geo_radius' | 'geo_bounding_box' | 'geo_polygon'; export type DynamicValue = {type?: 'dynamic' | undefined, field: string}; export type FilterCondition = {field: string, operator?: FilterOperator | undefined, value: DynamicValue | unknown}; export type LogicalOperatorInput = {AND?: (Array | null) | undefined, OR?: (Array | null) | undefined, NOT?: (Array | null) | undefined, case_sensitive?: (boolean | null) | undefined} | {[key: string]: unknown}; export type SortDirection = 'asc' | 'desc'; export type SortOption = {field: string, direction?: SortDirection | undefined}; export type ListTaxonomiesRequest = {limit?: (number | null) | undefined, page_size?: (number | null) | undefined, offset?: (number | null) | undefined, page?: (number | null) | undefined, search?: (string | null) | undefined, filters?: (LogicalOperatorInput | null) | undefined, sort?: (SortOption | null) | undefined, sorts?: (Array<{[key: string]: unknown}> | null) | undefined, case_sensitive?: boolean | undefined}; export type GetApiV1TaxonomiesArgs = {limit?: (number | null) | undefined, page_size?: (number | null) | undefined, offset?: (number | null) | undefined, page?: (number | null) | undefined, cursor?: (string | null) | undefined, next_cursor?: (string | null) | undefined, after?: (string | null) | undefined, include_total?: boolean | undefined, body: ListTaxonomiesRequest}; export type GetApiV1TaxonomiesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, NamespaceHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const inputSourceType = z.enum(["payload", "literal", "vector", "blob"]); export const inputMapping = z.object({input_key: z.string(), source_type: inputSourceType.nullable().optional(), path: z.string().nullable().optional(), override: z.unknown()}); export const enrichmentMergeMode = z.enum(["replace", "append"]); export const enrichmentField = z.object({field_path: z.string(), target_field: z.string().nullable().optional(), merge_mode: enrichmentMergeMode.optional()}); export const sourceCollectionOutput = z.object({collection_id: z.string(), enrichment_fields: z.array(enrichmentField).nullable().optional()}); export const stepKeySource = z.enum(["assignment_label", "assignment_node_id", "field_path"]); export const covariateType = z.enum(["categorical", "numeric", "embedding", "cluster_id"]); export const covariateConfig = z.object({field_path: z.string(), covariate_type: covariateType, name: z.string().min(1).max(100), binning_strategy: z.enum(["quartiles", "deciles", "custom"]).nullable().optional(), clustering_method: z.enum(["kmeans", "hdbscan"]).nullable().optional(), n_clusters: z.number().int().gte(2).lte(100).nullable().optional()}); export const stepAnalyticsConfigOutput = z.object({timestamp_field: z.string(), sequence_id_field: z.string(), step_key_source: stepKeySource.optional(), step_key_field_path: z.string().nullable().optional(), covariates: z.array(covariateConfig).optional(), max_sequence_duration_days: z.number().int().gte(1).lte(365).nullable().optional()}); export const flatTaxonomyConfigOutput = z.object({taxonomy_type: z.literal("flat").optional(), retriever_id: z.string(), input_mappings: z.array(inputMapping), source_collection: sourceCollectionOutput, top_k: z.number().int().gte(1).lte(50).optional(), step_analytics: stepAnalyticsConfigOutput.nullable().optional()}); export const hierarchyInferenceStrategy = z.enum(["schema", "cluster", "llm"]); export const hierarchicalNodeOutput = z.object({collection_id: z.string(), parent_collection_id: z.string().nullable().optional(), label: z.string().nullable().optional(), summary: z.string().nullable().optional(), keywords: z.array(z.string()).nullable().optional(), retriever_id: z.string().nullable().optional(), enrichment_fields: z.array(enrichmentField).nullable().optional(), input_mappings: z.array(inputMapping).nullable().optional()}); export const hierarchicalTaxonomyConfigOutput = z.object({taxonomy_type: z.literal("hierarchical").optional(), retriever_id: z.string().nullable().optional(), input_mappings: z.array(inputMapping).nullable().optional(), inference_strategy: hierarchyInferenceStrategy.nullable().optional(), inference_collections: z.array(z.string()).nullable().optional(), llm_provider: z.literal("openai_chat_v1").nullable().optional(), llm_model: z.string().nullable().optional(), llm_prompt_template: z.string().nullable().optional(), llm_sample_size: z.number().int().optional(), cluster_ids: z.array(z.string()).nullable().optional(), cluster_overlap_threshold: z.number().optional(), hierarchical_nodes: z.array(hierarchicalNodeOutput).nullable().optional(), step_analytics: stepAnalyticsConfigOutput.nullable().optional(), path_field: z.string().nullable().optional(), path_separator: z.string().min(1).optional()}); export const taxonomyPathNode = z.object({node_id: z.string(), label: z.string(), path: z.array(z.string()), parent_id: z.string().nullable().optional(), level: z.number().int().gte(0)}); export const taxonomyResponse = z.object({taxonomy_id: z.string().optional(), version: z.number().int().gte(1).optional(), taxonomy_name: z.string(), description: z.string().nullable().optional(), retriever_id: z.string().nullable().optional(), input_mappings: z.array(inputMapping).nullable().optional(), config: z.discriminatedUnion("taxonomy_type", [flatTaxonomyConfigOutput, hierarchicalTaxonomyConfigOutput]), nodes: z.array(taxonomyPathNode).nullable().optional(), ready: z.boolean().optional(), created_at: z.string().optional(), metadata: z.record(z.string(), z.unknown()).optional()}); export const taxonomyListStats = z.object({total_taxonomies: z.number().int().optional(), flat_taxonomies: z.number().int().optional(), hierarchical_taxonomies: z.number().int().optional(), taxonomies_with_retrievers: z.number().int().optional()}); export const listTaxonomiesResponse = z.object({results: z.array(taxonomyResponse), pagination: z.record(z.string(), z.unknown()), total_count: z.number().int(), stats: taxonomyListStats.nullable().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 }; /** * List taxonomies (GET alias for POST /list). * * GET /v1/taxonomies */ export const getApiV1Taxonomies = async (args: GetApiV1TaxonomiesArgs, options?: GetApiV1TaxonomiesOptions) => { const {baseUrl = "https://api.mixpeek.com", fetch: fetchFn = fetch, BearerAuth, NamespaceHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.page != null) query.append("page", String(args.page)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.next_cursor != null) query.append("next_cursor", String(args.next_cursor)) if (args.after != null) query.append("after", String(args.after)) if (args.include_total != null) query.append("include_total", String(args.include_total)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) if (NamespaceHeader !== undefined) headers.set("X-Namespace", NamespaceHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/taxonomies${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", 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(listTaxonomiesResponse, await res.json()) };