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
/**
* Brain Capture & Inbox Routes
*
* Capture/classify thoughts and manage the inbox log (review, fix, retry, done).
*/
import { Router } from 'express';
import * as brainService from '../services/brain.js';
import { recordUserAction } from '../services/userActions.js';
import { assertHostControl } from '../services/authGate.js';
import { parseBareUrl } from '../lib/bareUrl.js';
import { deriveRepoLinkFields } from '../lib/repoLinkFields.js';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import { validateRequest, brainInboxRetryBodySchema } from '../lib/validation.js';
import {
captureInputSchema,
resolveReviewInputSchema,
fixInputSchema,
updateInboxInputSchema,
markInboxSentToCatalogSchema,
inboxQuerySchema
} from '../lib/brainValidation.js';
const router = Router();
/**
* POST /api/brain/capture
* Capture a thought, classify it, and store it. A capture that is nothing but
* a repository URL is filed as a link and auto-cloned (with any `repoIntake`
* agents queued after), so it needs the same operator authority as creating
* that link directly (#11037).
*/
router.post('/capture', asyncHandler(async (req, res) => {
const { text, providerOverride, modelOverride, creative, repoIntake, note } = validateRequest(captureInputSchema, req.body);
const bareUrl = parseBareUrl(text);
if (bareUrl && deriveRepoLinkFields(bareUrl).isRepo) assertHostControl(req);
const captureOptions = { creative, repoIntake };
if (note !== undefined) captureOptions.note = note;
const result = await brainService.captureThought(text, providerOverride, modelOverride, captureOptions);
const inboxId = result?.inboxLog?.id;
if (inboxId) {
try {
const happenedAt = new Date().toISOString();
await recordUserAction({
type: 'brain.capture',
actor: 'user',
target: inboxId,
summary: 'captured thought',
payload: { id: inboxId },
source: { route: `${req.baseUrl}${req.route?.path ?? ''}`, method: req.method },
happenedAt,
dedupeKey: `brain.capture:${inboxId}`,
});
} catch (error) {
console.error(`❌ Failed to record brain.capture: ${error.message}`);
}
}
res.json(result);
}));
/**
* GET /api/brain/inbox
* Get inbox log entries with optional filters
*/
router.get('/inbox', asyncHandler(async (req, res) => {
const data = validateRequest(inboxQuerySchema, req.query);
const [result, counts] = await Promise.all([
brainService.getInboxLog(data),
brainService.getInboxLogCounts(),
]);
const entries = Array.isArray(result) ? result : (result?.entries || result?.items || []);
const total = result?.total ?? entries.length;
const nextCursor = result?.nextCursor ?? null;
res.json({
entries,
items: entries,
counts,
total,
nextCursor
});
}));
/**
* GET /api/brain/inbox/:id
* Get a single inbox log entry
*/
router.get('/inbox/:id', asyncHandler(async (req, res) => {
const entry = await brainService.getInboxLogById(req.params.id);
if (!entry) {
throw new ServerError('Inbox entry not found', { status: 404, code: 'NOT_FOUND' });
}
res.json(entry);
}));
/**
* POST /api/brain/review/resolve
* Resolve a needs_review inbox item
*/
router.post('/review/resolve', asyncHandler(async (req, res) => {
const { inboxLogId, destination, editedExtracted } = validateRequest(resolveReviewInputSchema, req.body);
const result = await brainService.resolveReview(inboxLogId, destination, editedExtracted);
res.json(result);
}));
/**
* POST /api/brain/fix
* Fix/correct a filed inbox item
*/
router.post('/fix', asyncHandler(async (req, res) => {
const { inboxLogId, newDestination, updatedFields, note } = validateRequest(fixInputSchema, req.body);
const result = await brainService.fixClassification(inboxLogId, newDestination, updatedFields, note);
res.json(result);
}));
/**
* POST /api/brain/inbox/:id/retry
* Retry AI classification for a needs_review item
*/
router.post('/inbox/:id/retry', asyncHandler(async (req, res) => {
const { providerOverride, modelOverride } = validateRequest(brainInboxRetryBodySchema, req.body ?? {});
const result = await brainService.retryClassification(req.params.id, providerOverride, modelOverride);
res.json(result);
}));
/**
* POST /api/brain/inbox/:id/done
* Mark an inbox entry as done
*/
router.post('/inbox/:id/done', asyncHandler(async (req, res) => {
const result = await brainService.markInboxDone(req.params.id);
if (!result) {
throw new ServerError('Inbox entry not found', { status: 404, code: 'NOT_FOUND' });
}
res.json(result);
}));
/**
* POST /api/brain/inbox/sent-to-catalog
* Mark a batch of creative inbox notes as consumed by a committed catalog ingest.
* Literal path — declared before the `/inbox/:id` routes so it isn't shadowed.
*/
router.post('/inbox/sent-to-catalog', asyncHandler(async (req, res) => {
const { ids } = validateRequest(markInboxSentToCatalogSchema, req.body);
const updated = await brainService.markInboxSentToCatalog(ids);
res.json({ updated, count: updated.length });
}));
/**
* PUT /api/brain/inbox/:id
* Update an inbox entry (edit captured text)
*/
router.put('/inbox/:id', asyncHandler(async (req, res) => {
const data = validateRequest(updateInboxInputSchema, req.body);
const result = await brainService.updateInboxEntry(req.params.id, data);
if (!result) {
throw new ServerError('Inbox entry not found', { status: 404, code: 'NOT_FOUND' });
}
res.json(result);
}));
/**
* DELETE /api/brain/inbox/:id
* Delete an inbox entry
*/
router.delete('/inbox/:id', asyncHandler(async (req, res) => {
const deleted = await brainService.deleteInboxEntry(req.params.id);
if (!deleted) {
throw new ServerError('Inbox entry not found', { status: 404, code: 'NOT_FOUND' });
}
res.status(204).send();
}));
export default router;