import {z} from 'zod' export type KnowledgeLevel = 'beginner' | 'intermediate' | 'advanced' | 'expert'; export type SkillLevel = 'novice' | 'competent' | 'proficient' | 'expert'; export type Intent = 'benign' | 'curious' | 'adversarial' | 'malicious'; export type PrivilegeLevel = 'guest' | 'user' | 'power_user' | 'admin' | 'super_admin'; export type DataAccessScope = 'own' | 'team' | 'organization' | 'global'; export type PersonaAccessPermissions = {privilege_level?: PrivilegeLevel | undefined, allowed_agents?: (Array | null) | undefined, allowed_tools?: (Array | null) | undefined, restricted_actions?: (Array | null) | undefined, data_access_scope?: (DataAccessScope | null) | undefined}; export type PersonaCreate = {id?: (string | null) | undefined, team_id: string, name: string, description?: (string | null) | undefined, role: string, role_description?: (string | null) | undefined, knowledge_level?: KnowledgeLevel | undefined, skill_level?: SkillLevel | undefined, intent?: Intent | undefined, language?: string | undefined, access_permissions?: (PersonaAccessPermissions | {[key: string]: unknown} | null) | undefined, is_preset?: boolean | undefined, preset_category?: (string | null) | undefined, tags?: (Array | null) | undefined, icon_filename?: (string | null) | undefined}; export type CreateApiV1PersonasArgs = {body: PersonaCreate}; export type CreateApiV1PersonasOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const knowledgeLevel = z.enum(["beginner", "intermediate", "advanced", "expert"]); export const skillLevel = z.enum(["novice", "competent", "proficient", "expert"]); export const intent = z.enum(["benign", "curious", "adversarial", "malicious"]); export const privilegeLevel = z.enum(["guest", "user", "power_user", "admin", "super_admin"]); export const dataAccessScope = z.enum(["own", "team", "organization", "global"]); export const personaAccessPermissions = z.object({privilege_level: privilegeLevel.optional(), allowed_agents: z.array(z.string()).nullable().optional(), allowed_tools: z.array(z.string()).nullable().optional(), restricted_actions: z.array(z.string()).nullable().optional(), data_access_scope: dataAccessScope.nullable().optional()}); export const persona = z.object({id: z.string(), team_id: z.string(), name: z.string(), description: z.string().nullable().optional(), role: z.string(), role_description: z.string().nullable().optional(), knowledge_level: knowledgeLevel.optional(), skill_level: skillLevel.optional(), intent: intent.optional(), language: z.string().optional(), access_permissions: z.union([personaAccessPermissions, z.record(z.string(), z.unknown())]).nullable().optional(), is_preset: z.boolean().optional(), preset_category: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional(), icon_filename: z.string().nullable().optional(), created_at: z.number().int(), updated_at: z.number().int(), created_by: 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 }; /** * Create Persona * * POST /v1/personas/ */ export const createApiV1Personas = async (args: CreateApiV1PersonasArgs, options: CreateApiV1PersonasOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/personas/${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(persona, await res.json()) };