import {z} from 'zod' export type GetApiIntegratorUsageSummaryArgs = {workspace_id: string, period: string}; export type GetApiIntegratorUsageSummaryOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const operationSummary = z.object({prompt_tokens: z.number().int().optional(), completion_tokens: z.number().int().optional(), cached_prompt_tokens: z.number().int().optional(), cache_write_tokens: z.number().int().optional(), events: z.number().int().optional()}); export const sourceSummary = z.object({prompt_tokens: z.number().int().optional(), completion_tokens: z.number().int().optional(), cached_prompt_tokens: z.number().int().optional(), cache_write_tokens: z.number().int().optional(), events: z.number().int().optional()}); export const usageSummaryResponse = z.object({workspace_id: z.string(), period: z.string(), total_prompt_tokens: z.number().int().optional(), total_completion_tokens: z.number().int().optional(), total_cached_prompt_tokens: z.number().int().optional(), total_cache_write_tokens: z.number().int().optional(), total_events: z.number().int().optional(), by_operation: z.record(z.string(), operationSummary).optional(), by_source: z.record(z.string(), sourceSummary).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 }; /** * Get workspace usage summary * * GET /integrator/usage/summary */ export const getApiIntegratorUsageSummary = async (args: GetApiIntegratorUsageSummaryArgs, options: GetApiIntegratorUsageSummaryOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.workspace_id != null) query.append("workspace_id", String(args.workspace_id)) if (args.period != null) query.append("period", String(args.period)) const url = `${baseUrl.replace(/\/+$/, '')}/integrator/usage/summary${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(usageSummaryResponse, await res.json()) };