import {z} from 'zod' export type GetApiUsageSessionsArgs = {allocated_after: string, allocated_before: string, limit?: number | undefined, offset?: number | undefined, session_status?: (Array<'active' | 'completed' | 'cancelled' | 'expired'> | null) | undefined, processed_status?: (Array<'pending' | 'billed' | 'failed'> | null) | undefined, agent_id?: string | undefined, allocated_by?: (Array<'dashboard_run' | 'api_run' | 'interactive'> | null) | undefined, search?: string | undefined, order_by?: string | undefined}; export type GetApiUsageSessionsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const usageSession = z.object({agent_id: z.string(), allocated_at: z.string().optional(), allocated_by: z.enum(["dashboard_run", "api_run", "interactive"]).optional(), billing_plan_id: z.string().optional(), cost_microdollars: z.number().int(), deallocated_at: z.string().optional(), duration_seconds: z.number().int().optional(), inference_cost_microdollars: z.number().int(), is_dedicated_phone: z.boolean(), phone_id: z.string(), processed_status: z.enum(["pending", "billed", "failed"]).optional(), session_id: z.string(), session_metadata: z.record(z.string(), z.unknown()).optional(), session_status: z.enum(["active", "completed", "cancelled", "expired"]).optional(), total_cost_microdollars: z.number().int()}); export const usageSessionsResponse = z.object({limit: z.number().int(), offset: z.number().int(), sessions: z.array(usageSession).nullable(), total: z.number().int()}); 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 phone sessions with cost * * GET /usage/sessions */ export const getApiUsageSessions = async (args: GetApiUsageSessionsArgs, options?: GetApiUsageSessionsOptions) => { const {baseUrl = "https://api.axilio.ai/api/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.allocated_after != null) query.append("allocated_after", String(args.allocated_after)) if (args.allocated_before != null) query.append("allocated_before", String(args.allocated_before)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.session_status != null) query.append("session_status", args.session_status.join(',')) if (args.processed_status != null) query.append("processed_status", args.processed_status.join(',')) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.allocated_by != null) query.append("allocated_by", args.allocated_by.join(',')) if (args.search != null) query.append("search", String(args.search)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/usage/sessions${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(usageSessionsResponse, await res.json()) };