import {z} from 'zod' export type GetApiAgenticObservabilityLogsArgs = {agent_id: string, from?: (string | null) | undefined, to?: (string | null) | undefined, level?: string | undefined, levels?: (string | null) | undefined, search?: (string | null) | undefined, limit?: number | undefined, since?: (string | null) | undefined}; export type GetApiAgenticObservabilityLogsOptions = {baseUrl?: string | undefined, FronteggHTTPAuthentication?: string | undefined, fetch?: typeof fetch | undefined}; export const observabilityLogLine = z.object({timestamp: z.string(), level: z.string(), message: z.string()}); export const observabilityLogsResponse = z.object({lines: z.array(observabilityLogLine), scope: z.literal("agent").optional(), job_id_prefix: z.string()}); 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 Observability Logs * * GET /agentic/observability/logs */ export const getApiAgenticObservabilityLogs = async (args: GetApiAgenticObservabilityLogsArgs, options?: GetApiAgenticObservabilityLogsOptions) => { const {baseUrl = "https://api.streamkap.com", fetch: fetchFn = fetch, FronteggHTTPAuthentication} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.level != null) query.append("level", String(args.level)) if (args.levels != null) query.append("levels", String(args.levels)) if (args.search != null) query.append("search", String(args.search)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.since != null) query.append("since", String(args.since)) if (FronteggHTTPAuthentication !== undefined) headers.set('authorization', `Bearer ${FronteggHTTPAuthentication}`) const url = `${baseUrl.replace(/\/+$/, '')}/agentic/observability/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(observabilityLogsResponse, await res.json()) };