import {z} from 'zod' export type SessionType = 'agent' | 'team' | 'workflow'; export type SortOrder = 'asc' | 'desc'; export type GetApiSessionsArgs = {type?: (SessionType | null) | undefined, component_id?: (string | null) | undefined, user_id?: (string | null) | undefined, session_name?: (string | null) | undefined, limit?: (number | null) | undefined, page?: (number | null) | undefined, sort_by?: (string | null) | undefined, sort_order?: (SortOrder | null) | undefined, db_id?: (string | null) | undefined, table?: (string | null) | undefined}; export type GetApiSessionsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const sessionSchema = z.object({session_id: z.string(), session_name: z.string(), session_state: z.record(z.string(), z.unknown()).nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), session_type: z.string().nullable().optional(), user_id: z.string().nullable().optional(), agent_id: z.string().nullable().optional(), team_id: z.string().nullable().optional(), workflow_id: z.string().nullable().optional(), session_summary: z.record(z.string(), z.unknown()).nullable().optional(), metrics: z.record(z.string(), z.unknown()).nullable().optional(), total_tokens: z.number().int().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional()}); export const paginationInfo = z.object({page: z.number().int().gte(0).optional(), limit: z.number().int().gte(1).optional(), total_pages: z.number().int().gte(0).optional(), total_count: z.number().int().gte(0).optional(), search_time_ms: z.number().gte(0).optional()}); export const paginatedResponseSessionSchema = z.object({data: z.array(sessionSchema), meta: paginationInfo}); 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 Sessions * * GET /sessions */ export const getApiSessions = async (args: GetApiSessionsArgs, options: GetApiSessionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.component_id != null) query.append("component_id", String(args.component_id)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.session_name != null) query.append("session_name", String(args.session_name)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.db_id != null) query.append("db_id", String(args.db_id)) if (args.table != null) query.append("table", String(args.table)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(paginatedResponseSessionSchema, await res.json()) };