import {z} from 'zod' export type SessionType = 'agent' | 'team' | 'workflow'; export type CreateSessionRequest = {session_id?: (string | null) | undefined, session_name?: (string | null) | undefined, session_state?: ({[key: string]: unknown} | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, user_id?: (string | null) | undefined, agent_id?: (string | null) | undefined, team_id?: (string | null) | undefined, workflow_id?: (string | null) | undefined}; export type CreateApiSessionsArgs = {type?: SessionType | undefined, db_id?: (string | null) | undefined, body: CreateSessionRequest}; export type CreateApiSessionsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const agentSessionDetailSchema = z.object({user_id: z.string().nullable().optional(), agent_session_id: z.string(), session_id: z.string(), session_name: z.string(), session_summary: z.record(z.string(), z.unknown()).nullable().optional(), session_state: z.record(z.string(), z.unknown()).nullable().optional(), agent_id: z.string().nullable().optional(), total_tokens: z.number().int().nullable().optional(), agent_data: z.record(z.string(), z.unknown()).nullable().optional(), metrics: z.record(z.string(), z.unknown()).nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), chat_history: z.array(z.record(z.string(), z.unknown())).nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const teamSessionDetailSchema = z.object({session_id: z.string(), session_name: z.string(), user_id: z.string().nullable().optional(), team_id: z.string().nullable().optional(), session_summary: z.record(z.string(), z.unknown()).nullable().optional(), session_state: z.record(z.string(), z.unknown()).nullable().optional(), metrics: z.record(z.string(), z.unknown()).nullable().optional(), team_data: z.record(z.string(), z.unknown()).nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), chat_history: z.array(z.record(z.string(), z.unknown())).nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional(), total_tokens: z.number().int().nullable().optional()}); export const workflowSessionDetailSchema = z.object({user_id: z.string().nullable().optional(), workflow_id: z.string().nullable().optional(), workflow_name: z.string().nullable().optional(), session_id: z.string(), session_name: z.string(), session_data: z.record(z.string(), z.unknown()).nullable().optional(), session_state: z.record(z.string(), z.unknown()).nullable().optional(), workflow_data: z.record(z.string(), z.unknown()).nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const createApiSessionsResponse = z.union([agentSessionDetailSchema, teamSessionDetailSchema, workflowSessionDetailSchema]); 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 }; /** * Create New Session * * POST /sessions */ export const createApiSessions = async (args: CreateApiSessionsArgs, options: CreateApiSessionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (args.db_id != null) query.append("db_id", String(args.db_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/sessions${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiSessionsResponse, await res.json()) };