import {z} from 'zod' export type AIModel = 'gpt-oss-120b' | 'gpt-oss-20b' | 'gpt-5.2' | 'gpt-5.5' | 'gpt-5.6-terra' | 'gpt-6-luna' | 'gpt-6.1-sol' | 'gpt-6.1-terra' | 'claude-opus-5-5' | 'claude-sonnet-5-5' | 'claude-haiku-5-5' | 'gemini-2.5-pro' | 'gemini-3-flash' | 'gemini-3.1-flash-lite' | 'gemini-3.1-pro' | 'gemini-3.5-flash-lite' | 'gemini-3.6-flash' | 'gemini-3.8-flash' | 'grok-4.7' | 'DeepSeek-V3.1' | 'DeepSeek-R1' | 'DeepSeek-V4-Flash' | 'DeepSeek-V4.1-Flash' | 'kimi-k2.6' | 'mistral-medium-3.5' | 'mistral-small-2603' | 'glm-5.2' | 'glm-5.3-flash' | 'auto'; export type ChatbotStyles = {theme?: ('light' | 'dark') | undefined, buttonColor?: string | undefined, displayName?: string | undefined, alignChatButton?: ('left' | 'right') | undefined, userMessageColor?: string | undefined, autoOpenChatWindowAfter?: number | undefined}; export type CollectLeadsSettings = {title?: string | undefined, name?: ({active?: boolean | undefined, label?: string | undefined}) | undefined, email?: ({active?: boolean | undefined, label?: string | undefined}) | undefined, phone?: ({active?: boolean | undefined, label?: string | undefined}) | undefined}; export type CreateApiUpdateChatbotSettingsArgs = {body: {chatbotId: string, name?: string | undefined, instructions?: string | undefined, initialMessages?: Array | undefined, suggestedMessages?: Array | undefined, visibility?: ('public' | 'private') | undefined, domains?: Array | undefined, onlyAllowOnAddedDomains?: boolean | undefined, ipLimit?: number | undefined, ipLimitTimeframe?: number | undefined, ipLimitMessage?: string | undefined, model?: AIModel | undefined, temp?: number | undefined, styles?: ChatbotStyles | undefined, collectCustomerInformation?: CollectLeadsSettings | undefined}}; export type CreateApiUpdateChatbotSettingsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiUpdateChatbotSettingsResponse = z.object({message: 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 }; /** * Update chatbot settings * * POST /update-chatbot-settings */ export const createApiUpdateChatbotSettings = async (args: CreateApiUpdateChatbotSettingsArgs, options?: CreateApiUpdateChatbotSettingsOptions) => { const {baseUrl = "https://www.chatbase.co/api/v1", 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/update-chatbot-settings${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(createApiUpdateChatbotSettingsResponse, await res.json()) };