Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

3.1 KB
Raw Blame
/**
 * Agent Personalities Routes
 *
 * CRUD operations for AI agent personalities.
 */

import { Router } from 'express';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import {
  validateRequest,
  agentSchema,
  agentUpdateSchema,
  agentGenerateSchema,
  agentToggleSchema,
} from '../lib/validation.js';
import * as agentPersonalities from '../services/agentPersonalities.js';
import { generateAgentPersonality } from '../services/agentPersonalityGenerator.js';
import { logAction } from '../services/history.js';
import { assertHostControl } from '../services/authGate.js';

const router = Router();

// GET / - Get all agent personalities
router.get('/', asyncHandler(async (req, res) => {
  const { userId } = req.query;

  const agents = userId
    ? await agentPersonalities.getAgentsByUser(userId)
    : await agentPersonalities.getAllAgents();

  res.json(agents);
}));

// GET /:id - Get agent by ID
router.get('/:id', asyncHandler(async (req, res) => {
  const { id } = req.params;

  const agent = await agentPersonalities.getAgentById(id);
  if (!agent) {
    throw new ServerError('Agent not found', { status: 404, code: 'NOT_FOUND' });
  }

  res.json(agent);
}));

// POST / - Create new agent
router.post('/', asyncHandler(async (req, res) => {
  const data = validateRequest(agentSchema, req.body);
  const agent = await agentPersonalities.createAgent(data);
  await logAction('create', 'agent-personality', agent.id, { name: agent.name });

  res.status(201).json(agent);
}));

// PUT /:id - Update agent
router.put('/:id', asyncHandler(async (req, res) => {
  const { id } = req.params;
  const data = validateRequest(agentUpdateSchema, req.body);
  const agent = await agentPersonalities.updateAgent(id, data);
  if (!agent) {
    throw new ServerError('Agent not found', { status: 404, code: 'NOT_FOUND' });
  }

  await logAction('update', 'agent-personality', id, { name: agent.name });
  res.json(agent);
}));

// DELETE /:id - Delete agent
router.delete('/:id', asyncHandler(async (req, res) => {
  const { id } = req.params;

  const deleted = await agentPersonalities.deleteAgent(id);
  if (!deleted) {
    throw new ServerError('Agent not found', { status: 404, code: 'NOT_FOUND' });
  }

  await logAction('delete', 'agent-personality', id, {});
  res.json({ success: true });
}));

// POST /generate - Generate agent personality using AI
router.post('/generate', asyncHandler(async (req, res) => {
  const { seed, providerId, model } = validateRequest(agentGenerateSchema, req.body);

  const generated = await generateAgentPersonality(seed, providerId, model);

  res.json(generated);
}));

// POST /:id/toggle - Toggle agent enabled status
router.post('/:id/toggle', asyncHandler(async (req, res) => {
  const { id } = req.params;
  const { enabled } = validateRequest(agentToggleSchema, req.body);
  if (enabled) assertHostControl(req);

  const agent = await agentPersonalities.toggleAgent(id, enabled);
  if (!agent) {
    throw new ServerError('Agent not found', { status: 404, code: 'NOT_FOUND' });
  }

  await logAction('toggle', 'agent-personality', id, { enabled });
  res.json(agent);
}));

export default router;