import {z} from 'zod' export type GetApiV1AnalyticsKnowledgeConnectorsFreshnessArgs = {knowledge_base_id?: (string | null) | undefined}; export type GetApiV1AnalyticsKnowledgeConnectorsFreshnessOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const connectorFreshnessRow = z.object({knowledge_base_source_id: z.string(), knowledge_base_id: z.string(), provider: z.string(), last_status: z.string(), last_event_type: z.string(), last_sync_started_at: z.string().nullable().optional(), last_sync_completed_at: z.string().nullable().optional(), last_duration_ms: z.number().nullable().optional(), last_items_processed: z.number().int(), last_items_succeeded: z.number().int(), last_items_failed: z.number().int(), last_items_unchanged: z.number().int(), last_error_message: z.string().nullable().optional(), last_event_at: z.string(), time_since_last_event_seconds: z.number().int()}); export const getApiV1AnalyticsKnowledgeConnectorsFreshnessResponse = z.array(connectorFreshnessRow); 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 }; /** * Per-source latest sync state and time-since-last-event * * GET /v1/analytics/knowledge/connectors/freshness */ export const getApiV1AnalyticsKnowledgeConnectorsFreshness = async (args: GetApiV1AnalyticsKnowledgeConnectorsFreshnessArgs, options: GetApiV1AnalyticsKnowledgeConnectorsFreshnessOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.knowledge_base_id != null) query.append("knowledge_base_id", String(args.knowledge_base_id)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/knowledge/connectors/freshness${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(getApiV1AnalyticsKnowledgeConnectorsFreshnessResponse, await res.json()) };