import {z} from 'zod' export type GetApiApiV1UsageStatsArgs = {start_date?: string | undefined, end_date?: string | undefined, model?: string | undefined, model_provider?: string | undefined, source?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiApiV1UsageStatsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const usageStatsResponse = z.object({id: z.string(), user_id: z.string(), org_id: z.string(), email: z.string(), model: z.string(), source: z.string(), input_tokens: z.number().int(), output_tokens: z.number().int(), total_tokens: z.number().int(), total_requests: z.number().int(), date: z.string(), created_at: z.string()}); export const getApiApiV1UsageStatsResponse = z.array(usageStatsResponse); 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 Usage Stats * * GET /api/v1/usage/stats */ export const getApiApiV1UsageStats = async (args: GetApiApiV1UsageStatsArgs, options?: GetApiApiV1UsageStatsOptions) => { const {baseUrl = "https://api.arcee.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.model != null) query.append("model", String(args.model)) if (args.model_provider != null) query.append("model_provider", String(args.model_provider)) if (args.source != null) query.append("source", String(args.source)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/usage/stats${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(getApiApiV1UsageStatsResponse, await res.json()) };