import {z} from 'zod' export type GetApiApiV1PublicUsageOpenrouterOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const publicShowcaseUsage = z.object({range: z.object({start: z.string(), end: z.string()}), totals: z.object({totalTokens: z.number(), inputTokens: z.number(), outputTokens: z.number(), requestCount: z.number(), carbonKg: z.number(), electricityKwh: z.number(), waterLiters: z.number(), waterLowerBoundL: z.number(), waterUpperBoundL: z.number()}), avgDailyTokens: z.number(), daily: z.array(z.object({date: z.string(), totalTokens: z.number(), inputTokens: z.number(), outputTokens: z.number(), carbonKg: z.number(), electricityKwh: z.number(), waterLiters: z.number()})), byProvider: z.array(z.object({provider: z.string(), totalTokens: z.number(), pct: z.number()})), methodology: z.object({factorsVersion: z.string(), framework: z.string()}), computedAt: z.string()}); export const publicOpenRouterUsage = z.object({asOf: z.string(), coverageEnd: z.string(), months: z.array(z.object({month: z.string(), tokens: z.number(), partial: z.boolean().optional()})), byMonth: z.array(z.object({month: z.string(), total: z.number(), providers: z.array(z.object({slug: z.string(), name: z.string(), tokens: z.number(), pct: z.number()})), models: z.array(z.object({slug: z.string(), name: z.string(), provider: z.string(), providerSlug: z.string(), version: z.string().optional(), free: z.boolean(), tokens: z.number(), pct: z.number()}))})), sourceUrl: z.string(), computedAt: z.string(), windows: z.object({"30d": publicShowcaseUsage.optional(), "365d": publicShowcaseUsage.optional()}).optional()}); export const getApiApiV1PublicUsageOpenrouterResponse = z.object({success: z.literal(true), data: publicOpenRouterUsage}); 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 }; /** * OpenRouter public usage series * * GET /api/v1/public/usage/openrouter */ export const getApiApiV1PublicUsageOpenrouter = async (options?: GetApiApiV1PublicUsageOpenrouterOptions) => { const {baseUrl = "https://api.crbonfree.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/public/usage/openrouter${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(getApiApiV1PublicUsageOpenrouterResponse, await res.json()) };