Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

add xspace-agent/packages/server/src/personalities/loader.ts

overstepping8cf0a216680e
diff --git a/xspace-agent/packages/server/src/personalities/loader.ts b/xspace-agent/packages/server/src/personalities/loader.ts
new file mode 100644
index 000000000..2980c6de7
--- /dev/null
+++ b/xspace-agent/packages/server/src/personalities/loader.ts
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: Apache-2.0
+// Copyright 2026 nirholas (https://github.com/nirholas/xspace-agent) [§69]
+
+import fs from 'fs'
+import path from 'path'
+import type { Personality, PersonalityWithMeta } from './types'
+
+// JSON directories live at repo root, not inside the package
+const REPO_ROOT = path.resolve(__dirname, '..', '..', '..', '..')
+const PRESETS_DIR = path.join(REPO_ROOT, 'personalities', 'presets')
+const CUSTOM_DIR = path.join(REPO_ROOT, 'personalities', 'custom')
+
+function isValidPersonality(obj: unknown): obj is Personality {
+ if (!obj || typeof obj !== 'object') return false
+ const p = obj as Record<string, unknown>
+ return (
+ typeof p.id === 'string' &&
+ p.id.length > 0 &&
+ typeof p.name === 'string' &&
+ p.name.length > 0 &&
+ typeof p.description === 'string' &&
+ typeof p.systemPrompt === 'string' &&
+ p.systemPrompt.length > 0 &&
+ typeof p.voice === 'object' &&
+ p.voice !== null &&
+ Array.isArray(p.context)
+ )
+}
+
+function sanitizeId(id: string): string {
+ return id.replace(/[^a-zA-Z0-9_-]/g, '')
+}
+
+function loadFromDir(dir: string, isPreset: boolean): PersonalityWithMeta[] {
+ if (!fs.existsSync(dir)) return []
+ const results: PersonalityWithMeta[] = []
+ for (const file of fs.readdirSync(dir)) {
+ if (!file.endsWith('.json')) continue
+ try {
+ const raw = JSON.parse(fs.readFileSync(path.join(dir, file), 'utf-8'))
+ if (isValidPersonality(raw)) {
+ results.push({ ...raw, isPreset })
+ }
+ } catch {
+ // Skip malformed files
+ }
+ }
+ return results
+}
+
+export class PersonalityLoader {
+ private cache: Map<string, PersonalityWithMeta> = new Map()
+
+ constructor() {
+ this.reload()
+ }
+
+ reload(): void {
+ this.cache.clear()
+ for (const p of [...loadFromDir(PRESETS_DIR, true), ...loadFromDir(CUSTOM_DIR, false)]) {
+ this.cache.set(p.id, p)
+ }
+ }
+
+ list(): PersonalityWithMeta[] {
+ return Array.from(this.cache.values())
+ }
+
+ get(id: string): PersonalityWithMeta | undefined {
+ return this.cache.get(id)
+ }
+
+ create(personality: Personality): PersonalityWithMeta {
+ const id = sanitizeId(personality.id)
+ if (!id) throw new Error('Invalid personality ID')
+ if (this.cache.has(id) && this.cache.get(id)!.isPreset) {
+ throw new Error('Cannot overwrite a preset personality')
+ }
+ if (!fs.existsSync(CUSTOM_DIR)) {
+ fs.mkdirSync(CUSTOM_DIR, { recursive: true })
+ }
+ fs.writeFileSync(path.join(CUSTOM_DIR, `${id}.json`), JSON.stringify(personality, null, 2), 'utf-8')
+ const withMeta: PersonalityWithMeta = { ...personality, id, isPreset: false }
+ this.cache.set(id, withMeta)
+ return withMeta
+ }
+
+ update(id: string, updates: Partial<Personality>): PersonalityWithMeta {
+ const safeId = sanitizeId(id)
+ const existing = this.cache.get(safeId)
+ if (!existing) throw new Error(`Personality '${safeId}' not found`)
+ if (existing.isPreset) throw new Error('Cannot modify a preset personality')
+
+ const updated: PersonalityWithMeta = { ...existing, ...updates, id: safeId, isPreset: false }
+ if (!isValidPersonality(updated)) throw new Error('Invalid personality data after update')
+
+ const { isPreset: _, ...toWrite } = updated
+ fs.writeFileSync(path.join(CUSTOM_DIR, `${safeId}.json`), JSON.stringify(toWrite, null, 2), 'utf-8')
+ this.cache.set(safeId, updated)
+ return updated
+ }
+
+ delete(id: string): void {
+ const safeId = sanitizeId(id)
+ const existing = this.cache.get(safeId)
+ if (!existing) throw new Error(`Personality '${safeId}' not found`)
+ if (existing.isPreset) throw new Error('Cannot delete a preset personality')
+
+ const filePath = path.join(CUSTOM_DIR, `${safeId}.json`)
+ if (fs.existsSync(filePath)) fs.unlinkSync(filePath)
+ this.cache.delete(safeId)
+ }
+}
+
+