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

9.8 KB
Raw Blame
/**
 * Canon (Phase A of the Universe-as-canon refactor): prose extraction, the
 * per-character LLM refine/expand/differentiate operations, the read-only
 * cross-reference lookups, and the entry/kind lock toggles.
 *
 * Every path here is scoped under `/:id`, so mount order relative to crud.js
 * doesn't matter (no single-segment route can shadow these).
 */

import { Router } from 'express';
import { z } from 'zod';
import { asyncHandler } from '../../lib/errorHandler.js';
import { validateRequest } from '../../lib/validation.js';
import * as canonSvc from '../../services/universeCanon.js';
import { expandUniverseCharacter } from '../../services/universeCharacterExpand.js';
import {
  getUniverseCastIntegrity,
  reviewUniverseCast,
  proposeCharacterAugmentation,
  applyCharacterAugmentation,
} from '../../services/universeCastIntegrity.js';
import { characterAugmentProposeSchema, characterAugmentApplySchema } from '../../lib/characterAugmentValidation.js';
import { getUniverseCanonUsage, listLinkedSeriesNames } from '../../services/canonUsage.js';
import { mapServiceError, lockParamsSchema } from './shared.js';

const router = Router();

const extractCanonSchema = z.object({
  corpus: z.string().trim().min(1).max(200_000),
  kinds: z.array(z.string().trim().min(1)).optional(),
  parallel: z.boolean().optional(),
  providerOverride: z.string().trim().max(64).optional(),
});

// Extract characters/places/objects from a prose body into the universe's
// canon arrays. Same LLM path as the series-side extract — just targeting a
// universe so multiple series can share the cast.
router.post('/:id/extract-canon', asyncHandler(async (req, res) => {
  const body = validateRequest(extractCanonSchema, req.body ?? {});
  const result = await canonSvc.extractCanonFromProse(req.params.id, body)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

const refineCharSchema = z.object({
  providerId: z.string().trim().max(64).optional(),
  model: z.string().trim().max(128).optional(),
});

router.post('/:id/characters/:entryId/refine', asyncHandler(async (req, res) => {
  const body = validateRequest(refineCharSchema, req.body ?? {});
  const result = await canonSvc.refineUniverseCharacter(req.params.id, req.params.entryId, body)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Expand a character via one LLM call — fills BLANK extended fields
// (pronouns/age/stats/colorPalette/expressions/...) so a novelist + graphic
// novelist have full reference data. No-clobber on populated fields; locked
// characters return `{ locked: true }` with no LLM call.
router.post('/:id/characters/:entryId/expand', asyncHandler(async (req, res) => {
  const body = validateRequest(refineCharSchema, req.body ?? {});
  const result = await expandUniverseCharacter(req.params.id, req.params.entryId, body)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// ── Cast integrity (#6415) ───────────────────────────────────────────────
// Character ids the caller wants scoped. Omitted entirely = the whole cast;
// an explicit `[]` is an empty selection and reviews nobody. The two are
// deliberately different answers, so the schema must not default it to [].
const castScopeSchema = z.object({
  characterIds: z.array(z.string().trim().min(1).max(120)).max(500).optional(),
  providerId: z.string().trim().max(64).optional(),
  model: z.string().trim().max(128).optional(),
});

// DETERMINISTIC integrity report — no provider call, safe on page load. Also
// returns `reviewScope` (provider / model / batch size) so the UI can name what
// a semantic review would spend BEFORE the user opts into it.
router.get('/:id/characters/integrity', asyncHandler(async (req, res) => {
  const characterIds = typeof req.query.characterIds === 'string' && req.query.characterIds.trim()
    ? req.query.characterIds.split(',').map((s) => s.trim()).filter(Boolean)
    : undefined;
  const result = await getUniverseCastIntegrity(req.params.id, { characterIds })
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// SEMANTIC review — one bounded LLM call, explicit user action only. Characters
// past the batch cap come back `truncated`, never silently passed.
router.post('/:id/characters/integrity/review', asyncHandler(async (req, res) => {
  const body = validateRequest(castScopeSchema, req.body ?? {});
  const result = await reviewUniverseCast(req.params.id, body)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Propose sharper values for POPULATED fields. Writes NOTHING — returns
// before/after per field for the author to accept individually.
router.post('/:id/characters/:entryId/augment', asyncHandler(async (req, res) => {
  const body = validateRequest(characterAugmentProposeSchema, req.body ?? {});
  const result = await proposeCharacterAugmentation(req.params.id, req.params.entryId, body)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Apply only the proposals the author accepted. `fingerprint` is the character
// state the preview was reviewed against — a mismatch is a 409, not a silent
// overwrite of whatever was edited in the meantime.
router.post('/:id/characters/:entryId/augment/apply', asyncHandler(async (req, res) => {
  const body = validateRequest(characterAugmentApplySchema, req.body ?? {});
  const result = await applyCharacterAugmentation(req.params.id, req.params.entryId, body)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Cast-wide differentiate — one LLM call rewrites every character so the
// cast as a whole has no visually-colliding pairs. Returns counts + the
// updated universe.
router.post('/:id/characters/differentiate-cast', asyncHandler(async (req, res) => {
  const body = validateRequest(refineCharSchema, req.body ?? {});
  const result = await canonSvc.differentiateUniverseCast(req.params.id, body)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Deterministic migration for legacy canon entries whose image-ready `prompt`
// was persisted but whose canonical description field was left blank. This is
// fill-blanks-only, respects locks, and makes no LLM call.
router.post('/:id/canon/backfill-descriptions', asyncHandler(async (req, res) => {
  const result = await canonSvc.backfillCanonDescriptionsFromPrompts(req.params.id)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Cross-reference: per-canon-entry usage across the universe's linked series.
// Read-only aggregation; no LLM calls, no writes. Surfaces which series + how
// many issues each character / place / object appears in, so the user can
// see crossover/cameo footprint at a glance on the Universe Canon page.
router.get('/:id/canon-usage', asyncHandler(async (req, res) => {
  const result = await getUniverseCanonUsage(req.params.id)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Thin lookup: linked-series id/name pairs for callers (e.g. NounsStage) that
// only need to label canon-card "from <series>" chips. Skips the O(series ×
// issues × matchers) prose scan that /canon-usage runs.
router.get('/:id/series-names', asyncHandler(async (req, res) => {
  const result = await listLinkedSeriesNames(req.params.id)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Lock toggle for canon entries. Locked entries are protected from AI rewrite
// paths (refine, differentiate, re-extract field overwrites).
const setLockSchema = z.object({
  locked: z.boolean(),
});
router.patch('/:id/canon/:kind/:entryId/lock', asyncHandler(async (req, res) => {
  const { kind } = validateRequest(lockParamsSchema, req.params);
  const body = validateRequest(setLockSchema, req.body ?? {});
  const result = await canonSvc.setCanonEntryLock(
    req.params.id,
    kind,
    req.params.entryId,
    body.locked,
  ).catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Remove ONE canon entry from its bucket. Scoped to the universe's canon
// array only — rendered images, character reference sheets, and the shared
// Catalog ingredient the entry may point at are all left intact (see
// `removeCanonEntry`). Mirrors the X button on category variations /
// composite sheets, which likewise only drop the bucket entry.
router.delete('/:id/canon/:kind/:entryId', asyncHandler(async (req, res) => {
  const { kind } = validateRequest(lockParamsSchema, req.params);
  const result = await canonSvc.removeCanonEntry(req.params.id, kind, req.params.entryId)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Remove ONE reference image from a canon entry. PATCH can't do this: the
// stale-write guard (`preserveImageRefsById`) restores any shorter imageRefs
// list. The image file stays in the gallery.
const imageRefParamsSchema = lockParamsSchema.extend({
  filename: z.string().trim().min(1).max(512),
});
router.delete('/:id/canon/:kind/:entryId/image-refs/:filename', asyncHandler(async (req, res) => {
  const { kind, filename } = validateRequest(imageRefParamsSchema, req.params);
  const result = await canonSvc.removeCanonEntryImageRef(req.params.id, kind, req.params.entryId, filename)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

// Bulk lock/unlock every canon entry of a single kind. Powers the
// "Lock all / Unlock all" buttons in the Universe Builder canon section.
router.patch('/:id/canon/:kind/lock-all', asyncHandler(async (req, res) => {
  const { kind } = validateRequest(lockParamsSchema, req.params);
  const body = validateRequest(setLockSchema, req.body ?? {});
  const result = await canonSvc.setCanonKindLockAll(req.params.id, kind, body.locked)
    .catch((err) => { throw mapServiceError(err); });
  res.json(result);
}));

export default router;