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

feat(04-01): create insights routes, Zod validation, mount in index.js, add client API functions

Adam Eivye4dc2becb545
diff --git a/.changelog/v1.14.x.md b/.changelog/v1.14.x.md
index 7c0420bd82..ed80f374df 100644
--- a/.changelog/v1.14.x.md
+++ b/.changelog/v1.14.x.md
@@ -18,6 +18,10 @@ Genome migration cleanup — completed the meatspace data migration so genome, C
- **Backup client API** — `getBackupStatus`, `triggerBackup`, `getBackupSnapshots`, `restoreBackup` added to `client/src/services/api.js` for widget consumption
- **BackupWidget dashboard component** — health status indicator (green/yellow/red), manual "Backup Now" trigger with toast feedback, expandable snapshot list, inline restore panel with dry-run preview and selective subdirectory filter
+- **Cross-domain insights engine** — `insightsService.js` with 5 exported functions: genome-health correlations (rule-based, groups markers by clinical category and matches blood test values), taste-identity theme generation (LLM, cached to `data/insights/themes.json`), and cross-domain narrative (LLM, cached to `data/insights/narrative.json` with previousText for diff)
+- **Insights REST API** — `GET /api/insights/genome-health`, `GET /api/insights/themes`, `POST /api/insights/themes/refresh`, `GET /api/insights/narrative`, `POST /api/insights/narrative/refresh` — all endpoints degrade gracefully with `{ available: false, reason }` when source data is missing
+- **Insights client API** — `getGenomeHealthCorrelations`, `getInsightThemes`, `refreshInsightThemes`, `getInsightNarrative`, `refreshInsightNarrative` added to `client/src/services/api.js`
+
## Improvements
- **Genome migration: service data paths** — updated `genome.js`, `clinvar.js`, and `epigenetic.js` services to use `PATHS.meatspace` instead of `PATHS.digitalTwin`, completing the migration from digital-twin to meatspace storage
diff --git a/client/src/services/api.js b/client/src/services/api.js
index 972f7fd8fc..b307b8f66c 100644
--- a/client/src/services/api.js
+++ b/client/src/services/api.js
@@ -1394,6 +1394,19 @@ export const triggerBackup = () => request('/backup/run', { method: 'POST' });
export const getBackupSnapshots = (options) => request('/backup/snapshots', options);
export const restoreBackup = (data) => request('/backup/restore', { method: 'POST', body: JSON.stringify(data) });
+// Insights
+export const getGenomeHealthCorrelations = () => request('/insights/genome-health');
+export const getInsightThemes = () => request('/insights/themes');
+export const refreshInsightThemes = (providerId, model) => request('/insights/themes/refresh', {
+ method: 'POST',
+ body: JSON.stringify({ providerId, model })
+});
+export const getInsightNarrative = () => request('/insights/narrative');
+export const refreshInsightNarrative = (providerId, model) => request('/insights/narrative/refresh', {
+ method: 'POST',
+ body: JSON.stringify({ providerId, model })
+});
+
// Instances (Federation)
export const getInstances = () => request('/instances');
export const getSelfInstance = () => request('/instances/self');
diff --git a/server/index.js b/server/index.js
index 7463c485a4..1b7450a03a 100644
--- a/server/index.js
+++ b/server/index.js
@@ -39,6 +39,7 @@ import lmstudioRoutes from './routes/lmstudio.js';
import browserRoutes from './routes/browser.js';
import moltworldToolsRoutes from './routes/moltworldTools.js';
import moltworldWsRoutes from './routes/moltworldWs.js';
+import insightsRoutes from './routes/insights.js';
import jiraRoutes from './routes/jira.js';
import autobiographyRoutes from './routes/autobiography.js';
import backupRoutes from './routes/backup.js';
@@ -218,6 +219,7 @@ app.use('/api/lmstudio', lmstudioRoutes);
app.use('/api/browser', browserRoutes);
app.use('/api/jira', jiraRoutes);
app.use('/api/health', appleHealthRoutes);
+app.use('/api/insights', insightsRoutes);
app.use('/api/instances', instancesRoutes);
app.use('/api/meatspace', meatspaceRoutes);
app.use('/api/settings', settingsRoutes);
diff --git a/server/lib/validation.js b/server/lib/validation.js
index 6e7a873f64..cb1bf06450 100644
--- a/server/lib/validation.js
+++ b/server/lib/validation.js
@@ -398,6 +398,15 @@ export const moltworldQueueAddSchema = z.object({
scheduledFor: z.string().datetime().optional().nullable()
});
+// =============================================================================
+// INSIGHTS SCHEMAS
+// =============================================================================
+
+export const insightRefreshSchema = z.object({
+ providerId: z.string().optional(),
+ model: z.string().optional()
+});
+
// =============================================================================
// BACKUP SCHEMAS
// =============================================================================
diff --git a/server/routes/insights.js b/server/routes/insights.js
new file mode 100644
index 0000000000..ec67e075b2
--- /dev/null
+++ b/server/routes/insights.js
@@ -0,0 +1,51 @@
+/**
+ * Insights Routes
+ *
+ * REST API for the cross-domain insights engine:
+ * GET /api/insights/genome-health — genome markers + blood correlations
+ * GET /api/insights/themes — cached taste-identity themes
+ * POST /api/insights/themes/refresh — (re)generate taste-identity themes via LLM
+ * GET /api/insights/narrative — cached cross-domain narrative
+ * POST /api/insights/narrative/refresh — (re)generate narrative via LLM
+ */
+
+import { Router } from 'express';
+import { asyncHandler } from '../lib/errorHandler.js';
+import { validateRequest, insightRefreshSchema } from '../lib/validation.js';
+import * as insightsService from '../services/insightsService.js';
+
+const router = Router();
+
+// GET /api/insights/genome-health
+router.get('/genome-health', asyncHandler(async (req, res) => {
+ const result = await insightsService.getGenomeHealthCorrelations();
+ res.json(result);
+}));
+
+// GET /api/insights/themes
+router.get('/themes', asyncHandler(async (req, res) => {
+ const result = await insightsService.getThemeAnalysis();
+ res.json(result);
+}));
+
+// POST /api/insights/themes/refresh
+router.post('/themes/refresh', asyncHandler(async (req, res) => {
+ const { providerId, model } = validateRequest(insightRefreshSchema, req.body);
+ const result = await insightsService.generateThemeAnalysis(providerId, model);
+ res.json(result);
+}));
+
+// GET /api/insights/narrative
+router.get('/narrative', asyncHandler(async (req, res) => {
+ const result = await insightsService.getCrossDomainNarrative();
+ res.json(result);
+}));
+
+// POST /api/insights/narrative/refresh
+router.post('/narrative/refresh', asyncHandler(async (req, res) => {
+ const { providerId, model } = validateRequest(insightRefreshSchema, req.body);
+ const result = await insightsService.refreshCrossDomainNarrative(providerId, model);
+ res.json(result);
+}));
+
+export default router;