import {z} from 'zod' export type GetApiV1BillingUsageRequestsByModelArgs = {start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, full_history?: boolean | undefined}; export type GetApiV1BillingUsageRequestsByModelOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const usageRequestModelSummaryItem = z.object({model: z.string(), request_count: z.number().int(), total_credits: z.number().optional(), total_tokens: z.number().int().optional(), total_input_tokens: z.number().int().optional(), total_output_tokens: z.number().int().optional(), total_cache_read_tokens: z.number().int().optional(), total_cache_write_tokens: z.number().int().optional(), total_cost: z.number().optional(), last_request_at: z.string()}); export const usageRequestModelSummaryResponse = z.object({items: z.array(usageRequestModelSummaryItem)}); 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 Billing Usage Requests By Model * * GET /v1/billing/usage/requests-by-model * * Also accepts auth: BearerAuth */ export const getApiV1BillingUsageRequestsByModel = async (args: GetApiV1BillingUsageRequestsByModelArgs, options: GetApiV1BillingUsageRequestsByModelOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = 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.full_history != null) query.append("full_history", String(args.full_history)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/billing/usage/requests-by-model${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(usageRequestModelSummaryResponse, await res.json()) };