import {z} from 'zod' export type InstallPersonaRequest = {name_override?: (string | null) | undefined}; export type CreateApiV1LibraryPersonasPersonaIdInstallArgs = {persona_id: string, body: InstallPersonaRequest}; export type CreateApiV1LibraryPersonasPersonaIdInstallOptions = {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 }; /** * Install a library persona into the caller's workspace * * POST /v1/library/personas/{persona_id}/install */ export const createApiV1LibraryPersonasPersonaIdInstall = async (args: CreateApiV1LibraryPersonasPersonaIdInstallArgs, options: CreateApiV1LibraryPersonasPersonaIdInstallOptions) => { 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/library/personas/${encodeURIComponent(String(args.persona_id))}/install${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(personaResponse, await res.json()) };