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
/**
* CoS Reports, Briefings, Scripts, Watcher, and Activity Routes
*/
import { Router } from 'express';
import { z } from 'zod';
import { generateReport, getBriefing, getLatestBriefing, getReport, getTodayActivity, getTodayReport, getWhileAwayActivity, listBriefings, listReports } from '../services/cosReports.js';
import { getScript, listScripts } from '../services/cos.js';
import * as appActivity from '../services/appActivity.js';
import * as claudeChangelog from '../services/claudeChangelog.js';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import { validateRequest } from '../lib/validation.js';
const router = Router();
const dateParamSchema = z.object({
date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'date must be YYYY-MM-DD')
});
// The date is both a date-bucket key and the report's filename, so it must be a
// bare YYYY-MM-DD day. A full ISO timestamp would match no bucket (and no
// agent's `completedAt` prefix), silently writing an all-zero report under a
// filename `listReports`/`getReport` can never round-trip.
const generateReportSchema = z.object({
date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, 'date must be YYYY-MM-DD').optional(),
});
// `since` is the client's last-visit marker (ISO-8601). Optional and tolerant:
// `getWhileAwayActivity` already clamps an absent/garbage/future marker to a
// 24h fallback, so a malformed value preprocesses to `undefined` (let the
// service apply its fallback) rather than 400-ing the dashboard card into a
// blank state. A valid datetime string passes through unchanged.
const whileAwayQuerySchema = z.object({
since: z.preprocess(
(v) => (typeof v === 'string' && !Number.isNaN(Date.parse(v)) ? v : undefined),
z.string().optional()
)
});
// GET /api/cos/reports - List all reports
router.get('/reports', asyncHandler(async (req, res) => {
const reports = await listReports();
res.json(reports);
}));
// GET /api/cos/reports/today - Get today's report
router.get('/reports/today', asyncHandler(async (req, res) => {
const report = await getTodayReport();
res.json(report);
}));
// GET /api/cos/reports/:date - Get report by date
router.get('/reports/:date', asyncHandler(async (req, res) => {
const { date } = validateRequest(dateParamSchema, req.params);
const report = await getReport(date);
if (!report) {
throw new ServerError('Report not found', { status: 404, code: 'NOT_FOUND' });
}
res.json(report);
}));
// POST /api/cos/reports/generate - Generate report for date
router.post('/reports/generate', asyncHandler(async (req, res) => {
const { date } = validateRequest(generateReportSchema, req.body);
const report = await generateReport(date);
res.json(report);
}));
// GET /api/cos/briefings - List all briefings
router.get('/briefings', asyncHandler(async (req, res) => {
const briefings = await listBriefings();
res.json({ briefings });
}));
// GET /api/cos/briefings/latest - Get latest briefing
router.get('/briefings/latest', asyncHandler(async (req, res) => {
const briefing = await getLatestBriefing();
res.json(briefing);
}));
// GET /api/cos/briefings/:date - Get briefing by date
router.get('/briefings/:date', asyncHandler(async (req, res) => {
const { date } = validateRequest(dateParamSchema, req.params);
const briefing = await getBriefing(date);
if (!briefing) {
throw new ServerError('Briefing not found', { status: 404, code: 'NOT_FOUND' });
}
res.json(briefing);
}));
// GET /api/cos/claude-changelog - Get Claude Code changelog (fetches Atom feed)
router.get('/claude-changelog', asyncHandler(async (req, res) => {
const result = await claudeChangelog.checkChangelog();
res.json(result);
}));
// GET /api/cos/claude-changelog/cached - Get cached changelog without fetching
router.get('/claude-changelog/cached', asyncHandler(async (req, res) => {
const result = await claudeChangelog.getCachedChangelog();
res.json(result);
}));
// GET /api/cos/scripts - List generated scripts
router.get('/scripts', asyncHandler(async (req, res) => {
const scripts = await listScripts();
res.json(scripts);
}));
// GET /api/cos/scripts/:name - Get script content
router.get('/scripts/:name', asyncHandler(async (req, res) => {
const script = await getScript(req.params.name);
if (!script) {
throw new ServerError('Script not found', { status: 404, code: 'NOT_FOUND' });
}
res.json(script);
}));
// GET /api/cos/app-activity - Get per-app activity data
router.get('/app-activity', asyncHandler(async (req, res) => {
const activity = await appActivity.loadAppActivity();
res.json(activity);
}));
// GET /api/cos/app-activity/:appId - Get activity for specific app
router.get('/app-activity/:appId', asyncHandler(async (req, res) => {
const activity = await appActivity.getAppActivityById(req.params.appId);
if (!activity) {
res.json({ appId: req.params.appId, activity: null, message: 'No activity recorded for this app' });
return;
}
res.json({ appId: req.params.appId, activity });
}));
// POST /api/cos/app-activity/:appId/clear-cooldown - Clear cooldown for an app
router.post('/app-activity/:appId/clear-cooldown', asyncHandler(async (req, res) => {
const result = await appActivity.clearAppCooldown(req.params.appId);
res.json({ success: true, appId: req.params.appId, activity: result });
}));
// GET /api/cos/activity/today - Get today's activity summary
router.get('/activity/today', asyncHandler(async (req, res) => {
const activity = await getTodayActivity();
res.json(activity);
}));
// GET /api/cos/activity/while-away - What agents did since ?since=<ISO> (the
// client's last-visit marker). Powers the "While You Were Away" dashboard card.
router.get('/activity/while-away', asyncHandler(async (req, res) => {
const { since } = validateRequest(whileAwayQuerySchema, req.query);
const activity = await getWhileAwayActivity(since);
res.json(activity);
}));
export default router;