import {z} from 'zod' export type IdParam = string; export type UserParam = string; export type DeletedParam = number; export type CreatedByParam = string; export type UpdatedByParam = string; export type CreatedAtParam = string; export type UpdatedAtParam = string; export type AssistantInstanceRequestCreate = {_id?: IdParam | undefined, user?: UserParam | undefined, deleted?: DeletedParam | undefined, createdBy?: CreatedByParam | undefined, updatedBy?: UpdatedByParam | undefined, createdAt?: CreatedAtParam | undefined, updatedAt?: UpdatedAtParam | undefined, release?: ('current' | 'next' | 'preProd') | undefined, patient?: string | undefined, verified?: boolean | undefined, onBehalfOf?: string | undefined, status?: ('idle' | 'in-progress' | 'failure' | 'success') | undefined, statusUpdatedAt?: string | undefined, statusReason?: ('success-by-assistant' | 'success-by-transfer' | 'success-by-patient' | 'success-by-staff' | 'success-by-assistant-timeout' | 'failure-by-assistant' | 'failure-by-patient' | 'failure-by-staff' | 'incomplete-success-criteria' | 'timed-out' | 'no-new-messages' | 'maximum-retries') | undefined, assistant: string, entryPoints?: (Array<{channel?: ('sms' | 'email' | 'voice' | 'inapp' | 'chat' | 'fax' | 'whatsapp' | 'rcs' | 'lumabot' | 'internal') | undefined, lastMessageAt?: string | undefined, direction?: ('inbound' | 'outbound') | undefined}>) | undefined, startedAt?: string | undefined, finishedAt?: string | undefined, duration?: number | undefined, reporting?: ({callOutcome?: ('transferred' | 'assistant-ended' | 'patient-ended' | 'timed-out') | undefined, intents?: (Array<{intent?: string | undefined, inScope?: boolean | undefined, handled?: boolean | undefined, evidence?: string | undefined}>) | undefined, summary?: string | undefined, callTransferAttempt?: boolean | undefined, transferWasAfterHours?: boolean | undefined}) | undefined}; export type CreateApiAssistantInstancesArgs = {body: AssistantInstanceRequestCreate}; export type CreateApiAssistantInstancesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const idParam = z.string().min(24).max(24); export const userParam = z.string().min(24).max(24); export const deletedParam = z.union([z.literal(0), z.literal(1)]); export const createdByParam = z.string().min(24).max(24); export const updatedByParam = z.string().min(24).max(24); export const createdAtParam = z.string(); export const updatedAtParam = z.string(); export const assistantInstanceResponse = z.object({_id: idParam.optional(), user: userParam.optional(), deleted: deletedParam.optional(), createdBy: createdByParam.optional(), updatedBy: updatedByParam.optional(), createdAt: createdAtParam.optional(), updatedAt: updatedAtParam.optional(), release: z.enum(["current", "next", "preProd"]).optional(), patient: z.string().min(24).max(24).optional(), verified: z.boolean().optional(), onBehalfOf: z.string().min(24).max(24).optional(), status: z.enum(["idle", "in-progress", "failure", "success"]).optional(), statusUpdatedAt: z.string().optional(), statusReason: z.enum(["success-by-assistant", "success-by-transfer", "success-by-patient", "success-by-staff", "success-by-assistant-timeout", "failure-by-assistant", "failure-by-patient", "failure-by-staff", "incomplete-success-criteria", "timed-out", "no-new-messages", "maximum-retries"]).optional(), assistant: z.string().min(24).max(24).optional(), entryPoints: z.array(z.object({channel: z.enum(["sms", "email", "voice", "inapp", "chat", "fax", "whatsapp", "rcs", "lumabot", "internal"]).optional(), lastMessageAt: z.string().optional(), direction: z.enum(["inbound", "outbound"]).optional()})).optional(), startedAt: z.string().optional(), finishedAt: z.string().optional(), duration: z.number().optional(), reporting: z.object({callOutcome: z.enum(["transferred", "assistant-ended", "patient-ended", "timed-out"]).optional(), intents: z.array(z.object({intent: z.string().optional(), inScope: z.boolean().optional(), handled: z.boolean().optional(), evidence: z.string().min(24).max(24).optional()})).optional(), summary: z.string().optional(), callTransferAttempt: z.boolean().optional(), transferWasAfterHours: z.boolean().optional()}).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 an assistant instance * * POST /assistantInstances */ export const createApiAssistantInstances = async (args: CreateApiAssistantInstancesArgs, options?: CreateApiAssistantInstancesOptions) => { const {baseUrl = "https://api.lumahealth.io/api/v2", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/assistantInstances${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(assistantInstanceResponse, await res.json()) };