import {z} from 'zod' export type CreateApiApiV1ContextSearchSteerArgs = {mode?: ('fast' | 'standard') | undefined, context?: boolean | undefined, body: {user_id?: string | undefined, steeringPrompt?: string | undefined, query: string, similarity_threshold: number, minimum_similarity_threshold: number, scope?: ('internal' | 'external') | undefined, body_metadata?: ({fileName?: string | undefined, groupName?: Array | undefined, lastModifiedAt?: string | undefined, modalities?: (Array<'text' | 'image'>) | undefined} | {[key: string]: unknown}) | undefined, magic_key?: (string | null) | undefined}}; export type CreateApiApiV1ContextSearchSteerOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1ContextSearchSteerResponse = z.object({answer: z.string(), context: z.string().optional(), usage: z.object({inputTokens: z.number().optional(), outputTokens: z.number().optional(), totalTokens: z.number().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 }; /** * Search context and generate an LLM-powered answer * * POST /api/v1/context/search/steer */ export const createApiApiV1ContextSearchSteer = async (args: CreateApiApiV1ContextSearchSteerArgs, options?: CreateApiApiV1ContextSearchSteerOptions) => { const {baseUrl = "https://platform-backend.getalchemystai.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.mode != null) query.append("mode", String(args.mode)) if (args.context != null) query.append("context", String(args.context)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/context/search/steer${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(createApiApiV1ContextSearchSteerResponse, await res.json()) };