import {z} from 'zod' export type DraftFollowupsRequest = {conversation_sid: string, every_days?: number | undefined, voice?: ('sender' | 'agent') | undefined, count?: number | undefined, guidance?: (string | null) | undefined}; export type CreateApiApiAgentPlansDraftFollowupsArgs = {body: DraftFollowupsRequest}; export type CreateApiApiAgentPlansDraftFollowupsOptions = {baseUrl?: string | undefined, BearerJwt?: string | undefined, TeamSid?: string | undefined, fetch?: typeof fetch | undefined}; export const draftFollowupsResponse = z.object({success: z.literal(true), operation: z.literal("action"), action: z.string(), item: z.unknown(), result: z.object({anchor: z.object({message_sid: z.string().nullable(), at: z.string(), author: z.string().nullable(), author_name: z.string().nullable()}), voice: z.string(), count: z.number().int(), every_days: z.number().int(), then: z.string(), followups: z.array(z.object({at: z.string(), weekend_skipped: z.boolean(), text: z.string()})), cost_usd: z.number().nullable()}), meta: z.object({trace_id: z.string(), span_id: z.string(), timestamp: z.string(), duration_ms: z.number().int().gte(0), team_sid: z.string().nullable().optional(), actor_type: z.string().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 }; /** * Draft follow-ups * * POST /api/agent-plans/draft-followups */ export const createApiApiAgentPlansDraftFollowups = async (args: CreateApiApiAgentPlansDraftFollowupsArgs, options?: CreateApiApiAgentPlansDraftFollowupsOptions) => { const {baseUrl = "https://beta.getsales.io/unibox/v1", fetch: fetchFn = fetch, BearerJwt, TeamSid} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerJwt !== undefined) headers.set('authorization', `Bearer ${BearerJwt}`) if (TeamSid !== undefined) headers.set("Team-SID", TeamSid) const url = `${baseUrl.replace(/\/+$/, '')}/api/agent-plans/draft-followups${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(draftFollowupsResponse, await res.json()) };