import {z} from 'zod' export type Message = {id: string, conversation_id: string, sender_id: string, sender_role: 'ConstructionManager' | 'TradePartner', recipient_ids: Array, trade_names?: Array | undefined, date: string, body: string, images?: Array | undefined}; export type AdditionalContextImage = {image_url: string, description?: string | undefined}; export type AdditionalContext = {images?: Array | undefined} | {[key: string]: unknown}; export type PredictionRequest = {messages: Array, additional_context?: AdditionalContext | undefined}; export type CreateApiV1LennarMessagesPredictArgs = {body: PredictionRequest}; export type CreateApiV1LennarMessagesPredictOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const predictionGeneratedMessage = z.object({reasoning: z.string(), short_reasoning: z.string(), text: z.string(), confidence: z.enum(["high", "medium", "low"])}); export const predictionBoundingBox = z.object({x1: z.number(), y1: z.number(), x2: z.number(), y2: z.number()}); export const predictionAnnotation = z.object({title: z.string(), description: z.string(), confidence: z.enum(["high", "medium", "low"]), bounding_box: predictionBoundingBox}); export const predictionAnnotatedImage = z.object({image_url: z.string(), annotations: z.array(predictionAnnotation).optional()}); export const prediction = z.object({generated_messages: z.array(predictionGeneratedMessage), annotated_images: z.array(predictionAnnotatedImage)}); export const predictionResponse = z.object({prediction: prediction}); 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 }; /** * Predict Messages * * POST /v1/lennar/messages/predict */ export const createApiV1LennarMessagesPredict = async (args: CreateApiV1LennarMessagesPredictArgs, options?: CreateApiV1LennarMessagesPredictOptions) => { const {baseUrl = "https://api.navigateai.co", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/lennar/messages/predict${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(predictionResponse, await res.json()) };