import {z} from 'zod' export type AgentProvider = 'openai' | 'anthropic' | 'google' | 'mistral' | 'zavu'; export type AgentVoiceConfig = {enabled: boolean, greeting?: string | undefined, greetings?: {[key: string]: string} | undefined, language?: string | undefined, model?: string | undefined, sttProvider?: string | undefined, sttModel?: string | undefined, ttsProvider?: string | undefined, ttsVoiceId?: string | undefined, voiceSpeed?: number | undefined, interruptible?: boolean | undefined, maxCallDurationMinutes?: number | undefined, maxIdleSeconds?: number | undefined, recordCalls?: boolean | undefined, voicemailAction?: ('hangup' | 'leave_message') | undefined, voicemailMessage?: string | undefined, transferPhoneNumber?: string | undefined}; export type AgentUpdateRequest = {name?: string | undefined, enabled?: boolean | undefined, provider?: AgentProvider | undefined, model?: string | undefined, systemPrompt?: string | undefined, apiKey?: string | undefined, contextWindowMessages?: number | undefined, includeContactMetadata?: boolean | undefined, maxTokens?: (number | null) | undefined, temperature?: (number | null) | undefined, triggerOnChannels?: Array | undefined, triggerOnMessageTypes?: Array | undefined, voice?: AgentVoiceConfig | undefined}; export type PatchApiV1SendersSenderIdAgentArgs = {senderId: string, body: AgentUpdateRequest}; export type PatchApiV1SendersSenderIdAgentOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const agentProvider = z.enum(["openai", "anthropic", "google", "mistral", "zavu"]); export const agentVoiceConfig = z.object({enabled: z.boolean(), greeting: z.string().max(1000).optional(), greetings: z.record(z.string(), z.string()).optional(), language: z.string().optional(), model: z.string().optional(), sttProvider: z.string().optional(), sttModel: z.string().optional(), ttsProvider: z.string().optional(), ttsVoiceId: z.string().optional(), voiceSpeed: z.number().gte(0.5).lte(1.5).optional(), interruptible: z.boolean().optional(), maxCallDurationMinutes: z.number().int().gte(1).lte(120).optional(), maxIdleSeconds: z.number().int().gte(5).lte(300).optional(), recordCalls: z.boolean().optional(), voicemailAction: z.enum(["hangup", "leave_message"]).optional(), voicemailMessage: z.string().max(1000).optional(), transferPhoneNumber: z.string().optional()}); export const agent = z.object({id: z.string(), senderId: z.string().optional(), name: z.string(), enabled: z.boolean(), provider: agentProvider, model: z.string(), systemPrompt: z.string(), contextWindowMessages: z.number().int().optional(), includeContactMetadata: z.boolean().optional(), maxTokens: z.number().int().nullable().optional(), temperature: z.number().nullable().optional(), triggerOnChannels: z.array(z.string()).optional(), triggerOnMessageTypes: z.array(z.string()).optional(), stats: z.object({totalInvocations: z.number().int().optional(), totalTokensUsed: z.number().int().optional(), totalCost: z.number().optional()}).optional(), createdAt: z.string(), updatedAt: z.string(), voice: agentVoiceConfig.optional(), senderIds: z.array(z.string()).optional()}); export const agentResponse = z.object({agent: agent}); 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 agent * * PATCH /v1/senders/{senderId}/agent */ export const patchApiV1SendersSenderIdAgent = async (args: PatchApiV1SendersSenderIdAgentArgs, options?: PatchApiV1SendersSenderIdAgentOptions) => { const {baseUrl = "https://api.zavu.dev", 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(/\/+$/, '')}/v1/senders/${encodeURIComponent(String(args.senderId))}/agent${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(agentResponse, await res.json()) };