import {z} from 'zod' export type GetApiApiV0AgentSessionsArgs = {limit?: number | undefined, offset?: number | undefined, name?: (string | null) | undefined, q?: (string | null) | undefined, persona_id?: (string | null) | undefined, without_persona?: boolean | undefined, section_id?: (string | null) | undefined, without_section?: boolean | undefined}; export type GetApiApiV0AgentSessionsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const agentSessionSettings = z.object({max_iterations: z.number().int().nullable().optional()}); export const agentSessionGetResponse = z.object({id: z.string(), persona_id: z.string().nullable().optional(), name: z.string().nullable(), status: z.string(), settings: agentSessionSettings.nullable(), message_count: z.number().int(), is_roaming: z.boolean().optional(), section_id: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const paginationMetadata = z.object({total: z.number().int(), limit: z.number().int(), offset: z.number().int(), has_more: z.boolean()}); export const agentSessionListResponse = z.object({sessions: z.array(agentSessionGetResponse), pagination: paginationMetadata}); 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 sessions * * GET /api/v0/agent/sessions */ export const getApiApiV0AgentSessions = async (args: GetApiApiV0AgentSessionsArgs, options: GetApiApiV0AgentSessionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.name != null) query.append("name", String(args.name)) if (args.q != null) query.append("q", String(args.q)) if (args.persona_id != null) query.append("persona_id", String(args.persona_id)) if (args.without_persona != null) query.append("without_persona", String(args.without_persona)) if (args.section_id != null) query.append("section_id", String(args.section_id)) if (args.without_section != null) query.append("without_section", String(args.without_section)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v0/agent/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(agentSessionListResponse, await res.json()) };