import {z} from 'zod' export type GetApiV1AnalyticsWorkspaceMetricsArgs = {window?: ('24h' | '7d' | '30d' | '90d' | 'all') | undefined, scope?: ('personal' | 'team') | undefined, since?: (string | null) | undefined, until?: (string | null) | undefined}; export type GetApiV1AnalyticsWorkspaceMetricsOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const workspaceMetrics = z.object({scope: z.enum(["personal", "team"]).optional(), window: z.enum(["24h", "7d", "30d", "90d", "all"]).optional(), inference_count: z.number().int().optional(), inference_count_previous: z.number().int().nullable().optional(), inference_count_all_time: z.number().int().nullable().optional(), error_count: z.number().int().optional(), avg_latency_ms: z.number().nullable().optional(), avg_ttft_ms: z.number().nullable().optional(), p99_latency_ms: z.number().nullable().optional(), median_latency_ms: z.number().nullable().optional(), p99_ttft_ms: z.number().nullable().optional(), median_ttft_ms: z.number().nullable().optional(), p99_ttft_ms_previous: z.number().nullable().optional(), p99_latency_ms_previous: z.number().nullable().optional(), spend_usd: z.number().nullable().optional(), spend_previous_usd: z.number().nullable().optional(), spend_per_1k_calls_usd: z.number().nullable().optional(), open_issue_count: z.number().int().nullable().optional(), last_inference_at: z.string().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 }; /** * Get Workspace Metrics * * GET /v1/analytics/workspace-metrics * * Also accepts auth: BearerAuth */ export const getApiV1AnalyticsWorkspaceMetrics = async (args: GetApiV1AnalyticsWorkspaceMetricsArgs, options: GetApiV1AnalyticsWorkspaceMetricsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.window != null) query.append("window", String(args.window)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.since != null) query.append("since", String(args.since)) if (args.until != null) query.append("until", String(args.until)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/workspace-metrics${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(workspaceMetrics, await res.json()) };