import {z} from 'zod' export type GetApiApiV1AnalyticsSummaryArgs = {days?: number | undefined}; export type GetApiApiV1AnalyticsSummaryOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1AnalyticsSummaryResponse = z.object({period_start: z.string(), period_end: z.string(), days: z.union([z.literal(30), z.literal(60), z.literal(90)]), totals: z.object({sources_ingested: z.number().int(), sources_failed: z.number().int(), memories_created: z.number().int(), tokens_ingested: z.number().int(), search_queries: z.number().int()}), previous_period_totals: z.object({sources_ingested: z.number().int(), sources_failed: z.number().int(), memories_created: z.number().int(), tokens_ingested: z.number().int(), search_queries: z.number().int()}), daily: z.array(z.object({sources_ingested: z.number().int(), sources_failed: z.number().int(), memories_created: z.number().int(), tokens_ingested: z.number().int(), search_queries: z.number().int(), date: z.string()})), sources_by_content_type: z.array(z.object({content_type: z.string(), count: z.number().int()})), spaces: z.array(z.object({space_id: z.string(), name: z.string().nullable(), totals: z.object({sources_ingested: z.number().int(), sources_failed: z.number().int(), memories_created: z.number().int(), tokens_ingested: z.number().int(), search_queries: 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 organization analytics * * GET /api/v1/analytics/summary */ export const getApiApiV1AnalyticsSummary = async (args: GetApiApiV1AnalyticsSummaryArgs, options: GetApiApiV1AnalyticsSummaryOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.days != null) query.append("days", String(args.days)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/analytics/summary${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(getApiApiV1AnalyticsSummaryResponse, await res.json()) };