import {z} from 'zod' export type PersonaCutInConfig = {min_words?: (number | null) | undefined, min_duration_ms?: (number | null) | undefined, backoff_ms?: number | undefined}; export type PersonaYieldConfig = {after_words?: (number | null) | undefined, after_seconds?: (number | null) | undefined}; export type TextBurstConfig = {enabled?: boolean | undefined, policy?: ('live' | 'debounced') | undefined}; export type PersonaBehaviorConfig = {first_speaker?: ('persona' | 'agent') | undefined, cut_in?: PersonaCutInConfig | undefined, language?: (string | null) | undefined, background_noise?: ('none' | 'office' | 'cafe' | 'city' | 'keyboard_typing') | undefined, background_noise_volume?: number | undefined, wait_seconds?: number | undefined, yield_on_overlap?: PersonaYieldConfig | undefined, interruption_mode?: ('off' | 'low' | 'medium' | 'high') | undefined, backchannel_style?: ('neutral' | 'impatient' | 'polite') | undefined, backchannel_overrides?: (Array | null) | undefined, reaction_pause_ms_min?: number | undefined, reaction_pause_ms_max?: number | undefined, text_burst?: TextBurstConfig | undefined}; export type PersonaUpdate = {name?: (string | null) | undefined, description?: (string | null) | undefined, is_active?: (boolean | null) | undefined, body?: (string | null) | undefined, traits?: ({[key: string]: unknown} | null) | undefined, behavior_config?: (PersonaBehaviorConfig | null) | undefined}; export type UpdateApiV1PersonasPersonaIdArgs = {persona_id: string, body: PersonaUpdate}; export type UpdateApiV1PersonasPersonaIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const personaCutInConfig = z.object({min_words: z.number().int().gte(0).lte(20).nullable().optional(), min_duration_ms: z.number().int().gte(0).lte(10000).nullable().optional(), backoff_ms: z.number().int().gte(0).lte(10000).optional()}); export const personaYieldConfig = z.object({after_words: z.number().int().gte(1).lte(20).nullable().optional(), after_seconds: z.number().gte(0.1).lte(10).nullable().optional()}); export const textBurstConfig = z.object({enabled: z.boolean().optional(), policy: z.enum(["live", "debounced"]).optional()}); export const personaBehaviorConfig = z.object({first_speaker: z.enum(["persona", "agent"]).optional(), cut_in: personaCutInConfig.optional(), language: z.string().max(20).nullable().optional(), background_noise: z.enum(["none", "office", "cafe", "city", "keyboard_typing"]).optional(), background_noise_volume: z.number().gte(0).lte(1).optional(), wait_seconds: z.number().gte(0).lte(30).optional(), yield_on_overlap: personaYieldConfig.optional(), interruption_mode: z.enum(["off", "low", "medium", "high"]).optional(), backchannel_style: z.enum(["neutral", "impatient", "polite"]).optional(), backchannel_overrides: z.array(z.string()).nullable().optional(), reaction_pause_ms_min: z.number().int().gte(0).lte(5000).optional(), reaction_pause_ms_max: z.number().int().gte(0).lte(5000).optional(), text_burst: textBurstConfig.optional()}); export const personaResponse = z.object({id: z.string(), organization_id: z.string(), name: z.string(), description: z.string().nullable(), is_active: z.boolean(), body: z.string(), traits: z.record(z.string(), z.unknown()), behavior_config: personaBehaviorConfig, cloned_from_id: z.string().nullable().optional(), created_by: z.string().nullable(), created_at: z.string(), updated_at: z.string()}); 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 a persona * * PUT /v1/personas/{persona_id} */ export const updateApiV1PersonasPersonaId = async (args: UpdateApiV1PersonasPersonaIdArgs, options: UpdateApiV1PersonasPersonaIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/personas/${encodeURIComponent(String(args.persona_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(personaResponse, await res.json()) };