import {z} from 'zod' export type LogLevel = 'DEBUG' | 'INFO' | 'WARNING' | 'ERROR' | 'CRITICAL'; export type LogSource = 'stdout' | 'stderr' | 'server'; export type GetApiLogsArgs = {agent_name: string, branch_id?: (string | null) | undefined, task_id?: (string | null) | undefined, trace_id?: (string | null) | undefined, level?: (LogLevel | null) | undefined, source?: (LogSource | null) | undefined, since?: (string | null) | undefined, until?: (string | null) | undefined, search?: (string | null) | undefined, after_id?: (string | null) | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiLogsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const logLevel = z.enum(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]); export const logSource = z.enum(["stdout", "stderr", "server"]); export const logQueryEntry = z.object({agent_id: z.string(), environment: z.string().nullable().optional(), level: logLevel, log_id: z.string(), logger: z.string().nullable().optional(), message: z.string(), method: z.string().nullable().optional(), source: logSource, span_id: z.string().nullable().optional(), task_id: z.string().nullable().optional(), timestamp: z.string(), trace_id: z.string().nullable().optional(), version: z.string().nullable().optional()}); export const logListResponse = z.object({has_more: z.boolean(), logs: z.array(logQueryEntry), 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 Agent Logs * * GET /logs */ export const getApiLogs = async (args: GetApiLogsArgs, options: GetApiLogsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agent_name != null) query.append("agent_name", String(args.agent_name)) if (args.branch_id != null) query.append("branch_id", String(args.branch_id)) if (args.task_id != null) query.append("task_id", String(args.task_id)) if (args.trace_id != null) query.append("trace_id", String(args.trace_id)) if (args.level != null) query.append("level", String(args.level)) if (args.source != null) query.append("source", String(args.source)) if (args.since != null) query.append("since", String(args.since)) if (args.until != null) query.append("until", String(args.until)) if (args.search != null) query.append("search", String(args.search)) if (args.after_id != null) query.append("after_id", String(args.after_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(/\/+$/, '')}/logs${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(logListResponse, await res.json()) };