import {z} from 'zod' export type GetApiApiV1UsageRecordsArgs = {'X-Org-ID': string, start_time?: string | undefined, end_time?: string | undefined, provider?: string | undefined, model?: string | undefined, agent_id?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiApiV1UsageRecordsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const usageRecord = z.object({id: z.string().optional(), request_id: z.string().optional(), org_id: z.string().optional(), tenant_id: z.string().optional(), team_id: z.string().optional(), agent_id: z.string().optional(), user_id: z.string().optional(), workflow_id: z.string().optional(), provider: z.string().optional(), model: z.string().optional(), tokens_in: z.number().int().optional(), tokens_out: z.number().int().optional(), cost_usd: z.number().optional(), latency_ms: z.number().int().optional(), success: z.boolean().optional(), error_message: z.string().optional(), created_at: z.string().optional(), timestamp: z.string().optional()}); export const usageRecordList = z.object({records: z.array(usageRecord).optional(), count: z.number().int().optional(), total: z.number().int().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 }; /** * List usage records * * GET /api/v1/usage/records */ export const getApiApiV1UsageRecords = async (args: GetApiApiV1UsageRecordsArgs, options?: GetApiApiV1UsageRecordsOptions) => { const {baseUrl = "https://orchestrator.getaxonflow.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Org-ID'] != null) headers.set("X-Org-ID", String(args['X-Org-ID'])) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.provider != null) query.append("provider", String(args.provider)) if (args.model != null) query.append("model", String(args.model)) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/usage/records${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(usageRecordList, await res.json()) };