import {z} from 'zod' export type GetApiV1FinetuneAgentSessionsArgs = {limit?: number | undefined, offset?: number | undefined, include_archived?: boolean | undefined}; export type GetApiV1FinetuneAgentSessionsOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const attachedResourceRef = z.object({id: z.string(), name: z.string(), type: z.string()}); export const chatSessionResponse = z.object({id: z.string(), user_id: z.string(), title: z.string(), created_at: z.string(), updated_at: z.string(), is_archived: z.boolean().optional(), project_id: z.string().nullable().optional(), modal_sandbox_id: z.string().nullable().optional(), first_user_attached_resources: z.array(attachedResourceRef).optional()}); export const chatSessionListResponse = z.object({success: z.boolean().optional(), sessions: z.array(chatSessionResponse), count: z.number().int(), 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 Sessions * * GET /v1/finetune-agent/sessions * * Also accepts auth: BearerAuth */ export const getApiV1FinetuneAgentSessions = async (args: GetApiV1FinetuneAgentSessionsArgs, options: GetApiV1FinetuneAgentSessionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = 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.include_archived != null) query.append("include_archived", String(args.include_archived)) if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/finetune-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(chatSessionListResponse, await res.json()) };