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

4.0 KB
Raw Blame
import { Router } from 'express';
import { asyncHandler, createServiceErrorMapper } from '../lib/errorHandler.js';
import {
  validateRequest,
  importerAnalyzeSchema,
  importerCommitSchema,
  importerClassifySchema,
  importerRetryIssuesSchema,
} from '../lib/validation.js';
import {
  analyzeImport,
  classifyImportContent,
  commitImport,
  retryIssueSplit,
  ERR_VALIDATION,
  ERR_LOCKED,
  ERR_PARTIAL_COMMIT_ISSUES,
  ERR_IMPORT_IN_PROGRESS,
  IMPORTER_SOURCE_CHAR_LIMIT,
} from '../services/importer.js';
import { ARC_ROLES, ARC_SHAPE_IDS } from '../lib/storyArc.js';
import * as universeSvc from '../services/universeBuilder.js';
import * as seriesSvc from '../services/pipeline/series.js';

const router = Router();

// The orchestrator throws its own validation/locked codes directly. Missing
// universe / series codes bubble through `getUniverse` / `getSeries` with
// their service-native codes (`NOT_FOUND` / `PIPELINE_SERIES_NOT_FOUND`),
// not an importer-prefixed alias — map those to 404.
// `ERR_PARTIAL_COMMIT_ISSUES` maps to 422 (not 500) because the universe +
// series writes succeeded and only the issue loop was rolled back —
// alerting / generic-retry middleware that pages on 5xx would otherwise
// treat this user-action-recoverable state as an infra failure. We picked
// 422 over the original 207 because the client `request()` helper treats
// 207 as a 2xx success (response.ok === true) and would interpret the
// error body as a normal commit result, mis-toasting "Imported 0 issues"
// and crashing on the missing createdIssueIds. 422 makes the helper throw
// so the existing error toast surfaces the orchestrator's message
// ("universe + series saved, retry to create the remaining issues").
// `ERR_IMPORT_IN_PROGRESS` is a 409: the retry's issue list conflicts with the
// issues an unfinished earlier attempt already created (#10762).
const SERVICE_ERROR_STATUS = {
  [ERR_VALIDATION]: 400,
  [ERR_LOCKED]: 409,
  [ERR_IMPORT_IN_PROGRESS]: 409,
  [ERR_PARTIAL_COMMIT_ISSUES]: 422,
  [universeSvc.ERR_NOT_FOUND]: 404,
  [seriesSvc.ERR_NOT_FOUND]: 404,
};

// Preserve `err.context` — ERR_PARTIAL_COMMIT_ISSUES carries the
// arcAlreadyPersisted retry signal the client gates on.
const mapServiceError = createServiceErrorMapper(SERVICE_ERROR_STATUS, (err) => err.context);

// Surfaces server-canonical constants so the client doesn't hardcode (and
// drift from) the source-char limit, the arc-role enum, or the arc-shape
// id list. Read by the Importer intake form on mount; the analyze response
// also includes these for clients that skip the config call.
router.get('/config', asyncHandler(async (req, res) => {
  res.json({
    sourceCharLimit: IMPORTER_SOURCE_CHAR_LIMIT,
    arcRoles: [...ARC_ROLES],
    arcShapeIds: [...ARC_SHAPE_IDS],
  });
}));

router.post('/classify', asyncHandler(async (req, res) => {
  const input = validateRequest(importerClassifySchema, req.body || {});
  const result = await classifyImportContent(input).catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

router.post('/analyze', asyncHandler(async (req, res) => {
  const input = validateRequest(importerAnalyzeSchema, req.body || {});
  const result = await analyzeImport(input).catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Recovery path for a failed issue split — re-runs ONLY the split (canon +
// arc are preserved client-side from the prior analyze). Cheap relative to a
// full re-analyze; comic scripts resolve mechanically with no LLM call.
router.post('/retry-issues', asyncHandler(async (req, res) => {
  const input = validateRequest(importerRetryIssuesSchema, req.body || {});
  const result = await retryIssueSplit(input).catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

router.post('/commit', asyncHandler(async (req, res) => {
  const input = validateRequest(importerCommitSchema, req.body || {});
  const result = await commitImport(input).catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

export default router;