import {z} from 'zod' export type GetApiV1PersonasArgs = {search?: (string | null) | undefined, is_active?: (boolean | null) | undefined, limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1PersonasOptions = {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 cursorPagePersonaResponse = z.object({items: z.array(personaResponse), next_cursor: z.string().nullable().optional(), has_more: z.boolean()}); 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 }; /** * List personas * * GET /v1/personas */ export const getApiV1Personas = async (args: GetApiV1PersonasArgs, options: GetApiV1PersonasOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.is_active != null) query.append("is_active", String(args.is_active)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/personas${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(cursorPagePersonaResponse, await res.json()) };