feat(entity): Chunk #4 data layer + entity UI (characters & world-building)

Data layer:
- Fuzzy scorer (packages/core/src/fuzzy.ts) with index recovery + unit tests
- Backlink computation (packages/core/src/backlinks.ts) incl. entity-level backlinks
- Consistency checker (packages/core/src/consistency.ts) incl. reciprocity/scope + unit tests
- StorageAdapter contract extended with listEntities/searchEntities/saveEntity/deleteEntity;
  implemented in Local (Dexie) and Server adapters; contract tests cover entity CRUD + ?q= search
- WorldItem.fields now supports list values (keyPeople, rivals); expanded RelationshipKind
- @mention autocomplete extension + picker in the editor package

Entity UI (apps/web/src/features/entities/):
- Generic field renderer (EntityFields) driven by ENTITY_FIELDS/FieldDef
- Tabbed directory with fuzzy search + entity cards (EntitiesView)
- Create/edit/delete drawer (EntityEditor) reusing shared fields
- Relationship editor + force-directed SVG relationship map
- Entity engine stylesheet; wired into nav

Store: project-store entity methods (load/create/update/delete) over the StorageAdapter contract.

CI-green: typecheck, lint, 65 tests, and web build all pass.
This commit is contained in:
Schwitzz
2026-10-25 22:42:55 -05:00
parent 81a44b6e95
commit 4efa71515e
36 changed files with 2764 additions and 82 deletions
+14 -4
View File
@@ -1,3 +1,4 @@
import { fileURLToPath } from "node:url";
import Fastify from "fastify";
import cors from "@fastify/cors";
import cookie from "@fastify/cookie";
@@ -74,7 +75,16 @@ export async function start() {
return app;
}
start().catch((err) => {
console.error("Failed to start server:", err);
process.exit(1);
});
// Auto-boot only when this file is the CLI entry point (pnpm dev / start),
// not when imported by tests (vitest) — otherwise start() would try to bind
// the port and tear the test process down.
const invokedDirectly =
process.argv[1] !== undefined &&
fileURLToPath(import.meta.url) === process.argv[1];
if (invokedDirectly) {
start().catch((err) => {
console.error("Failed to start server:", err);
process.exit(1);
});
}
+16 -1
View File
@@ -183,7 +183,7 @@ export function toEntity(r: EntityRow): Entity {
bio: f.bio ?? "",
appearance: f.appearance ?? "",
voice: f.voice ?? "",
relationships: f.relationships ?? "",
relationships: typeof f.relationships === "string" ? JSON.parse(f.relationships) : (f.relationships ?? []),
arc: f.arc ?? "",
status: f.status ?? "active",
tags: f.tags ?? [],
@@ -251,6 +251,21 @@ export function entityToRow(e: Entity): Omit<EntityRow, "created_at" | "updated_
}
// ---- helpers ----
/** Concatenate every human-readable field of an entity into one searchable string. */
export function entitySearchText(e: Entity): string {
if ("fields" in e) {
const parts = [e.name, e.kind];
for (const v of Object.values(e.fields)) if (typeof v === "string") parts.push(v);
return parts.join(" ");
}
if ("body" in e) {
return [e.body, ...e.tags].join(" ");
}
const c = e as Character;
const relText = (c.relationships ?? []).map((r) => `${r.targetId} ${r.kind} ${r.note}`).join(" ");
return [c.name, c.role, c.bio, c.appearance, c.voice, relText, c.arc, c.status, ...c.tags].join(" ");
}
/** Count words in a ProseMirror doc (text nodes only). */
export function wordCount(doc: unknown): number {
let count = 0;
+11 -5
View File
@@ -15,6 +15,7 @@ import {
toAISession,
toEntity,
entityToRow,
entitySearchText,
wordCount,
type ProjectRow,
type ShareRow,
@@ -58,7 +59,7 @@ const entityCreate = z.object({
bio: z.string().optional(),
appearance: z.string().optional(),
voice: z.string().optional(),
relationships: z.string().optional(),
relationships: z.array(z.any()).optional(),
arc: z.string().optional(),
status: z.enum(["active", "archived"]).optional(),
tags: z.array(z.string()).optional(),
@@ -315,7 +316,7 @@ export function registerRoutes(app: FastifyInstance): void {
// ---------- Entities (characters / world / notes) ----------
app.get("/api/projects/:id/entities", async (req) => {
const { id } = req.params as { id: string };
const { kind } = req.query as { kind?: string };
const { kind, q } = req.query as { kind?: string; q?: string };
requireProjectAccess(app, req, id);
let rows: EntityRow[];
if (kind) {
@@ -323,7 +324,12 @@ export function registerRoutes(app: FastifyInstance): void {
} else {
rows = db.prepare("SELECT * FROM entities WHERE project_id = ?").all(id) as EntityRow[];
}
return { entities: rows.map(toEntity) };
let entities = rows.map(toEntity);
if (q) {
const needle = q.toLowerCase();
entities = entities.filter((e) => entitySearchText(e).toLowerCase().includes(needle));
}
return { entities };
});
app.post("/api/projects/:id/entities", async (req) => {
@@ -337,7 +343,7 @@ export function registerRoutes(app: FastifyInstance): void {
if (body.kind === "character") {
entity = {
id: eid, projectId: id, name: body.name ?? "", role: body.role ?? "", bio: body.bio ?? "",
appearance: body.appearance ?? "", voice: body.voice ?? "", relationships: body.relationships ?? "",
appearance: body.appearance ?? "", voice: body.voice ?? "", relationships: body.relationships ?? [],
arc: body.arc ?? "", status: body.status ?? "active", tags: body.tags ?? [],
};
} else if (body.kind === "world") {
@@ -374,7 +380,7 @@ export function registerRoutes(app: FastifyInstance): void {
if (kind === "character") {
entity = {
id, projectId, name: body.name ?? existing.name, role: body.role ?? "", bio: body.bio ?? "",
appearance: body.appearance ?? "", voice: body.voice ?? "", relationships: body.relationships ?? "",
appearance: body.appearance ?? "", voice: body.voice ?? "", relationships: body.relationships ?? [],
arc: body.arc ?? "", status: body.status ?? "active", tags: body.tags ?? [],
};
} else if (kind === "world") {