import {z} from 'zod' export type ContextBlock = {id?: string | undefined, query?: string | undefined, didAgentChange?: boolean | undefined, results?: (Array<{[key: string]: unknown}> | null) | undefined, title?: (string | null) | undefined}; export type E2BSandboxTier = 'small' | 'medium' | 'large'; export type Fi3ThreadRequest = {method: 'upsert' | 'delete' | 'stop' | 'marimo_start' | 'update_metadata' | 'show_artifacts' | 'warm_sandbox', thread_id?: string | undefined, user_message?: string | undefined, context_blocks?: Array | undefined, table_integration_id?: string | undefined, cube_integration_id?: string | undefined, application_context?: (string | null) | undefined, model?: (string | null) | undefined, doc_id?: (string | null) | undefined, doc_yaml_content?: (string | null) | undefined, thread_title?: (string | null) | undefined, doc_discovery_mode?: ('preload_recent' | 'preload_all' | 'lazy') | undefined, preload_doc_ids?: Array | undefined, max_preload_docs?: number | undefined, max_files?: number | undefined, app_events?: Array<{[key: string]: unknown}> | undefined, project_id?: (string | null) | undefined, sandboxTier?: (E2BSandboxTier | null) | undefined}; export type CreateApiV3TeamsTeamIdUsersUserIdFi3ThreadArgs = {team_id: string, user_id: string, body: Fi3ThreadRequest}; export type CreateApiV3TeamsTeamIdUsersUserIdFi3ThreadOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const fi2BackgroundResponse = z.object({success: z.boolean(), message: z.string().optional(), result: z.unknown(), raw_output: z.string().optional(), error: z.string().nullable().optional(), execution_time: z.number().optional(), sandbox_id: z.string().nullable().optional(), html_files: z.array(z.record(z.string(), z.string())).optional()}); 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, update, or read a fi3 thread * * POST /v3/teams/{team_id}/users/{user_id}/fi3/thread */ export const createApiV3TeamsTeamIdUsersUserIdFi3Thread = async (args: CreateApiV3TeamsTeamIdUsersUserIdFi3ThreadArgs, options: CreateApiV3TeamsTeamIdUsersUserIdFi3ThreadOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v3/teams/${encodeURIComponent(String(args.team_id))}/users/${encodeURIComponent(String(args.user_id))}/fi3/thread${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(fi2BackgroundResponse, await res.json()) };