import {z} from 'zod' export type ParseIntentRequest = {description: string}; export type CreateApiApiV1WizardParseIntentArgs = {body: ParseIntentRequest}; export type CreateApiApiV1WizardParseIntentOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const wizardGoal = z.object({primary: z.string(), summary: z.string()}); export const wizardField = z.object({key: z.string(), label: z.string(), type: z.enum(["text", "textarea", "number", "select", "multiselect", "phone", "url"]), options: z.array(z.string()).nullable().optional(), value: z.unknown(), source: z.enum(["parsed", "default", "missing"]).optional(), required: z.boolean().optional(), placeholder: z.string().nullable().optional(), help_text: z.string().nullable().optional(), can_skip: z.boolean().optional(), skip_fallback: z.string().nullable().optional()}); export const recommendedTool = z.object({tool_key: z.string(), reason: z.string()}); export const recommendedContactSync = z.object({tool_key: z.string(), fields_to_be_synced: z.record(z.string(), z.string()), reason: z.string()}); export const recommendedConversationSync = z.object({tool_key: z.string(), reason: z.string()}); export const recommendedOutcome = z.object({key: z.string(), label: z.string(), default_on: z.boolean().optional(), mechanism: z.enum(["tool", "contact_sync", "conversation_sync", "summary_email", "webhook"]), tool_key: z.string().nullable().optional()}); export const voiceDefaults = z.object({first_sentence: z.string().nullable().optional(), speaks_first: z.enum(["bot", "user"]).optional(), detect_voicemail: z.enum(["hangup", "drop"]).nullable().optional(), voicemail_message: z.string().nullable().optional(), max_call_duration: z.number().int().optional(), use_turn_detector: z.boolean().optional(), listen_silence: z.number().optional(), listen_silence_message: z.string().nullable().optional()}); export const summarizeConfig = z.object({is_active: z.boolean().optional(), email: z.string().nullable().optional()}); export const behaviorDefaults = z.object({llm_temperature: z.number().optional(), has_human_agent: z.boolean().optional(), use_tools: z.boolean().optional(), summarize: summarizeConfig.nullable().optional()}); export const wizardConfig = z.object({channel: z.enum(["chat", "voice"]), business_summary: z.string(), industry_tag: z.string(), goal: wizardGoal, fields: z.array(wizardField).optional(), recommended_tools: z.array(recommendedTool).optional(), recommended_contact_syncs: z.array(recommendedContactSync).optional(), recommended_conversation_syncs: z.array(recommendedConversationSync).optional(), recommended_outcomes: z.array(recommendedOutcome).optional(), voice_defaults: voiceDefaults.nullable().optional(), behavior_defaults: behaviorDefaults.optional(), prompt_skeleton: z.string(), confidence: z.number().optional(), missing_critical_fields: z.array(z.string()).optional()}); export const iPostResponseBaseWizardConfig = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: wizardConfig.nullable().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 }; /** * Parse Intent Endpoint * * POST /api/v1/wizard/parse-intent * * Also accepts auth: HTTPBearer */ export const createApiApiV1WizardParseIntent = async (args: CreateApiApiV1WizardParseIntentArgs, options: CreateApiApiV1WizardParseIntentOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/wizard/parse-intent${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(iPostResponseBaseWizardConfig, await res.json()) };