import {z} from 'zod' export type GetApiV1EvalRunsActivityArgs = {created_from?: (string | null) | undefined, created_to?: (string | null) | undefined, scope?: ('live' | 'simulation' | 'test' | null) | undefined, channel?: (string | null) | undefined, evaluator_id?: (string | null) | undefined, agent_id?: (string | null) | undefined}; export type GetApiV1EvalRunsActivityOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const evalActivityBucket = z.object({date: z.string(), total: z.number().int().optional(), failed: z.number().int().optional(), errored: z.number().int().optional()}); export const evalActivityResponse = z.object({period_start: z.string(), period_end: z.string(), buckets: z.array(evalActivityBucket).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 }; /** * Day-bucketed eval-run activity * * GET /v1/eval-runs/activity */ export const getApiV1EvalRunsActivity = async (args: GetApiV1EvalRunsActivityArgs, options: GetApiV1EvalRunsActivityOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.created_from != null) query.append("created_from", String(args.created_from)) if (args.created_to != null) query.append("created_to", String(args.created_to)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.channel != null) query.append("channel", String(args.channel)) if (args.evaluator_id != null) query.append("evaluator_id", String(args.evaluator_id)) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/eval-runs/activity${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(evalActivityResponse, await res.json()) };