import {z} from 'zod' export type UpdatePhoneNumberBody = {label?: string | undefined, agent_id?: (string | null) | undefined, provider?: {id: string} | undefined}; export type PatchApiAgentsPhoneNumbersIdArgs = {'Cartesia-Version': '2026-03-01', id: string, body: UpdatePhoneNumberBody}; export type PatchApiAgentsPhoneNumbersIdOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const telephonyRegion = z.enum(["us1", "ie1", "au1"]); export const twilioProviderResponse = z.object({type: z.literal("twilio"), id: z.string(), account_sid: z.string(), api_key_sid: z.string(), region: telephonyRegion}); export const cartesiaProviderResponse = z.object({type: z.literal("cartesia")}); export const providerResponse = z.discriminatedUnion("type", [twilioProviderResponse, cartesiaProviderResponse]); export const phoneNumberResponse = z.object({id: z.string(), label: z.string().nullable().optional(), number: z.string(), agent: z.object({id: z.string(), name: z.string()}).nullable(), created_at: z.string(), updated_at: z.string(), provider: providerResponse}); 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 Phone Number * * PATCH /agents/phone-numbers/{id} */ export const patchApiAgentsPhoneNumbersId = async (args: PatchApiAgentsPhoneNumbersIdArgs, options?: PatchApiAgentsPhoneNumbersIdOptions) => { const {baseUrl = "https://api.cartesia.ai", 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 (args['Cartesia-Version'] != null) headers.set("Cartesia-Version", String(args['Cartesia-Version'])) if (APIKeyAuth !== undefined) headers.set('authorization', `Bearer ${APIKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/agents/phone-numbers/${encodeURIComponent(String(args.id))}${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(phoneNumberResponse, await res.json()) };