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
import { Router } from 'express';
import { ToolkitHttpError, defaultAsyncHandler } from '../internal/httpError.js';
import {
promptPreviewBodySchema,
promptStageUpdateBodySchema,
promptVariableCreateBodySchema,
promptVariableUpdateBodySchema,
validate
} from '../validation.js';
export function createPromptsRoutes(promptsService, options = {}) {
const router = Router();
// `asyncHandler`/`ServerError` are injected by the host (PortOS passes its
// real ServerError + asyncHandler so thrown errors normalize into
// `{ error, code, timestamp, context? }` and route to errorMiddleware).
// Standalone, the toolkit's own defaults serialize the same envelope.
const { asyncHandler = defaultAsyncHandler, ServerError = ToolkitHttpError } = options;
const parseBody = (schema, body, label) => {
const result = validate(schema, body);
if (!result.success) {
throw new ServerError(`Invalid ${label}`, { status: 400, code: 'VALIDATION_ERROR', context: { details: result.errors } });
}
return result.data;
};
router.get('/stages', asyncHandler(async (req, res) => {
const stages = promptsService.getStages();
res.json(stages);
}));
router.get('/stages/:name', asyncHandler(async (req, res) => {
const stage = promptsService.getStage(req.params.name);
if (!stage) {
throw new ServerError('Stage not found', { status: 404 });
}
const template = await promptsService.getStageTemplate(req.params.name);
res.json({ ...stage, template });
}));
router.put('/stages/:name', asyncHandler(async (req, res) => {
const { config, template } = parseBody(promptStageUpdateBodySchema, req.body, 'stage data');
if (config) {
await promptsService.updateStageConfig(req.params.name, config);
}
if (template) {
await promptsService.updateStageTemplate(req.params.name, template);
}
const updated = promptsService.getStage(req.params.name);
const updatedTemplate = await promptsService.getStageTemplate(req.params.name);
res.json({ ...updated, template: updatedTemplate });
}));
router.post('/stages/:name/preview', asyncHandler(async (req, res) => {
const preview = await promptsService.previewPrompt(req.params.name, parseBody(promptPreviewBodySchema, req.body, 'preview data'));
res.json({ preview });
}));
router.get('/variables', asyncHandler(async (req, res) => {
const variables = promptsService.getVariables();
res.json(variables);
}));
router.get('/variables/:key', asyncHandler(async (req, res) => {
const variable = promptsService.getVariable(req.params.key);
if (!variable) {
throw new ServerError('Variable not found', { status: 404 });
}
res.json(variable);
}));
router.post('/variables', asyncHandler(async (req, res) => {
const { key, ...data } = parseBody(promptVariableCreateBodySchema, req.body, 'variable data');
await promptsService.createVariable(key, data);
const created = promptsService.getVariable(key);
res.status(201).json(created);
}));
router.put('/variables/:key', asyncHandler(async (req, res) => {
await promptsService.updateVariable(req.params.key, parseBody(promptVariableUpdateBodySchema, req.body, 'variable data'));
const updated = promptsService.getVariable(req.params.key);
res.json(updated);
}));
router.delete('/variables/:key', asyncHandler(async (req, res) => {
await promptsService.deleteVariable(req.params.key);
res.status(204).send();
}));
return router;
}