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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* Autobiography API Routes
*
* Handles endpoints for the autobiography story prompt feature:
* - Get themes, prompts, stories
* - Save and update stories
* - Configuration for prompt frequency
* - Manual prompt trigger
*/
import { Router } from 'express';
import { z } from 'zod';
import * as autobiographyService from '../services/autobiography.js';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import { validateRequest } from '../lib/validation.js';
import { HHMM_STRICT_RE } from '../lib/timezone.js';
const router = Router();
// Validation schemas
const saveStorySchema = z.object({
// Either a bank prompt id, `followup-<id>`, or the `custom` sentinel when the
// user wrote their own question — in which case `customPromptText` carries it.
promptId: z.string().min(1),
content: z.string().min(1).max(50000),
parentStoryId: z.string().optional(),
customPromptText: z.string().max(500).optional()
}).refine(
(v) => v.promptId !== autobiographyService.CUSTOM_PROMPT_ID || !!v.customPromptText?.trim(),
{ message: 'customPromptText is required when promptId is "custom"', path: ['customPromptText'] }
);
const generateFollowUpsSchema = z.object({
providerId: z.string().optional()
});
const weaveNarrativeSchema = z.object({
providerId: z.string().optional()
});
const updateStorySchema = z.object({
content: z.string().min(1).max(50000)
});
const evaluateStorySchema = z.object({
providerId: z.string().optional()
});
const nextPromptQuerySchema = z.object({ exclude: z.string().min(1).max(200).optional() });
const storiesQuerySchema = z.object({ theme: z.string().min(1).max(100).optional() });
const updateConfigSchema = z.object({
enabled: z.boolean().optional(),
intervalHours: z.number().min(1).max(168).optional(),
reminder: z.object({
enabled: z.boolean().optional(),
// Strict "HH:MM" — the scheduler refuses to register anything else, so a
// malformed time has to fail here rather than silently disable the cron.
time: z.string().regex(HHMM_STRICT_RE, 'Expected a 24-hour HH:MM time').optional()
}).optional()
});
// =============================================================================
// STATS & CONFIG
// =============================================================================
/**
* GET /api/digital-twin/autobiography
* Get autobiography stats and configuration
*/
router.get('/', asyncHandler(async (req, res) => {
const stats = await autobiographyService.getStats();
res.json(stats);
}));
/**
* GET /api/digital-twin/autobiography/config
* Get configuration
*/
router.get('/config', asyncHandler(async (req, res) => {
const config = await autobiographyService.getConfig();
res.json(config);
}));
/**
* PUT /api/digital-twin/autobiography/config
* Update configuration
*/
router.put('/config', asyncHandler(async (req, res) => {
const validated = validateRequest(updateConfigSchema, req.body);
const config = await autobiographyService.updateConfig(validated);
res.json(config);
}));
// =============================================================================
// THEMES & PROMPTS
// =============================================================================
/**
* GET /api/digital-twin/autobiography/themes
* Get all available themes
*/
router.get('/themes', asyncHandler(async (req, res) => {
const themes = autobiographyService.getThemes();
res.json(themes);
}));
/**
* GET /api/digital-twin/autobiography/prompt
* Get the next prompt
*/
router.get('/prompt', asyncHandler(async (req, res) => {
const { exclude } = validateRequest(nextPromptQuerySchema, req.query);
const prompt = await autobiographyService.getNextPrompt(exclude);
res.json(prompt);
}));
/**
* GET /api/digital-twin/autobiography/suggestions
* Get a short menu of story ideas (the same set the daily prompt offers)
*/
router.get('/suggestions', asyncHandler(async (req, res) => {
const suggestions = await autobiographyService.getPromptSuggestions();
res.json(suggestions);
}));
/**
* GET /api/digital-twin/autobiography/prompt/:id
* Get a specific prompt by ID
*/
router.get('/prompt/:id', asyncHandler(async (req, res) => {
const prompt = autobiographyService.getPromptById(req.params.id);
if (!prompt) {
throw new ServerError('Prompt not found', { status: 404, code: 'NOT_FOUND' });
}
res.json(prompt);
}));
// =============================================================================
// STORIES
// =============================================================================
/**
* GET /api/digital-twin/autobiography/stories
* Get all stories, optionally filtered by theme
*/
router.get('/stories', asyncHandler(async (req, res) => {
const { theme } = validateRequest(storiesQuerySchema, req.query);
const stories = await autobiographyService.getStories(theme ?? null);
res.json(stories);
}));
/**
* POST /api/digital-twin/autobiography/stories
* Save a new story
*/
router.post('/stories', asyncHandler(async (req, res) => {
const validated = validateRequest(saveStorySchema, req.body);
const story = await autobiographyService.saveStory({
promptId: validated.promptId,
content: validated.content,
parentStoryId: validated.parentStoryId,
customPromptText: validated.customPromptText
});
res.json(story);
}));
/**
* PUT /api/digital-twin/autobiography/stories/:id
* Update an existing story
*/
router.put('/stories/:id', asyncHandler(async (req, res) => {
const validated = validateRequest(updateStorySchema, req.body);
const story = await autobiographyService.updateStory(req.params.id, validated.content);
if (!story) {
throw new ServerError('Story not found', { status: 404, code: 'NOT_FOUND' });
}
res.json(story);
}));
/**
* DELETE /api/digital-twin/autobiography/stories/:id
* Delete a story
*/
router.delete('/stories/:id', asyncHandler(async (req, res) => {
const story = await autobiographyService.deleteStory(req.params.id);
if (!story) {
throw new ServerError('Story not found', { status: 404, code: 'NOT_FOUND' });
}
res.json({ success: true, story });
}));
/**
* POST /api/digital-twin/autobiography/stories/:id/evaluate
* Score the story against the storytelling-craft rubric (seven moves + CART)
*/
router.post('/stories/:id/evaluate', asyncHandler(async (req, res) => {
const validated = validateRequest(evaluateStorySchema, req.body);
const result = await autobiographyService.evaluateStory(req.params.id, validated.providerId);
if (result.error) {
throw new ServerError(result.error, { status: 400, code: 'EVALUATION_ERROR' });
}
res.json(result);
}));
// =============================================================================
// FOLLOW-UP CHAINS
// =============================================================================
/**
* POST /api/digital-twin/autobiography/stories/:id/follow-ups
* Generate LLM-powered follow-up questions for a story
*/
router.post('/stories/:id/follow-ups', asyncHandler(async (req, res) => {
const validated = validateRequest(generateFollowUpsSchema, req.body);
const result = await autobiographyService.generateFollowUps(req.params.id, validated.providerId);
if (result.error) {
throw new ServerError(result.error, { status: 400, code: 'FOLLOW_UP_ERROR' });
}
res.json(result);
}));
/**
* GET /api/digital-twin/autobiography/stories/:id/chain
* Get the full chain of stories linked to a story (ancestors + descendants)
*/
router.get('/stories/:id/chain', asyncHandler(async (req, res) => {
const chain = await autobiographyService.getStoryChain(req.params.id);
res.json(chain);
}));
/**
* POST /api/digital-twin/autobiography/stories/:id/weave
* Weave the story's full chain into a single cohesive first-person narrative
*/
router.post('/stories/:id/weave', asyncHandler(async (req, res) => {
const validated = validateRequest(weaveNarrativeSchema, req.body);
const result = await autobiographyService.weaveChainNarrative(req.params.id, validated.providerId);
if (result.error) {
throw new ServerError(result.error, { status: 400, code: 'WEAVE_ERROR' });
}
res.json(result);
}));
// =============================================================================
// PROMPT TRIGGER
// =============================================================================
/**
* POST /api/digital-twin/autobiography/trigger
* Manually trigger a story prompt notification
*/
router.post('/trigger', asyncHandler(async (req, res) => {
const result = await autobiographyService.checkAndPrompt();
res.json(result);
}));
export default router;