@hanna84/mcp-writing 2.0.4 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,11 +4,31 @@ All notable changes to this project will be documented in this file. Dates are d
4
4
 
5
5
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
6
6
 
7
+ #### [v2.2.0](https://github.com/hannasdev/mcp-writing.git
8
+ /compare/v2.1.0...v2.2.0)
9
+
10
+ - feat(styleguide): add prose styleguide config resolution and MCP tools [`#84`](https://github.com/hannasdev/mcp-writing.git
11
+ /pull/84)
12
+
13
+ #### [v2.1.0](https://github.com/hannasdev/mcp-writing.git
14
+ /compare/v2.0.4...v2.1.0)
15
+
16
+ > 25 April 2026
17
+
18
+ - feat(tools): add scene character normalization utility [`#83`](https://github.com/hannasdev/mcp-writing.git
19
+ /pull/83)
20
+ - Release 2.1.0 [`33b5b31`](https://github.com/hannasdev/mcp-writing.git
21
+ /commit/33b5b31846576ef18385e6678f74cc109995fcac)
22
+
7
23
  #### [v2.0.4](https://github.com/hannasdev/mcp-writing.git
8
24
  /compare/v2.0.3...v2.0.4)
9
25
 
26
+ > 25 April 2026
27
+
10
28
  - docs(prd): mark review-bundles and scrivener-direct as done [`#82`](https://github.com/hannasdev/mcp-writing.git
11
29
  /pull/82)
30
+ - Release 2.0.4 [`775bbcf`](https://github.com/hannasdev/mcp-writing.git
31
+ /commit/775bbcfa0fb93e2b9828a94fa60ad53b29c7313c)
12
32
 
13
33
  #### [v2.0.3](https://github.com/hannasdev/mcp-writing.git
14
34
  /compare/v2.0.2...v2.0.3)
package/index.js CHANGED
@@ -17,6 +17,12 @@ import { isGitAvailable, isGitRepository, initGitRepository, createSnapshot, lis
17
17
  import { renderCharacterArcTemplate, renderCharacterSheetTemplate, renderPlaceSheetTemplate, slugifyEntityName } from "./world-entity-templates.js";
18
18
  import { importScrivenerSync, validateProjectId } from "./importer.js";
19
19
  import { ASYNC_PROGRESS_PREFIX } from "./async-progress.js";
20
+ import {
21
+ STYLEGUIDE_CONFIG_BASENAME,
22
+ STYLEGUIDE_ENUMS,
23
+ buildStyleguideConfigDraft,
24
+ resolveStyleguideConfig,
25
+ } from "./prose-styleguide.js";
20
26
  import {
21
27
  REVIEW_BUNDLE_PROFILES,
22
28
  REVIEW_BUNDLE_STRICTNESS,
@@ -51,6 +57,31 @@ function isPathInsideSyncDir(candidatePath) {
51
57
  return !(rel.startsWith("..") || path.isAbsolute(rel));
52
58
  }
53
59
 
60
+ // Like isPathInsideSyncDir, but works for paths that do not yet exist by
61
+ // walking up to the nearest existing ancestor before canonicalising.
62
+ function isPathCandidateInsideSyncDir(candidatePath) {
63
+ const resolvedCandidate = path.resolve(candidatePath);
64
+
65
+ let existingAncestor = resolvedCandidate;
66
+ while (!fs.existsSync(existingAncestor)) {
67
+ const parent = path.dirname(existingAncestor);
68
+ if (parent === existingAncestor) break;
69
+ existingAncestor = parent;
70
+ }
71
+
72
+ const canonicalBase = (() => {
73
+ try {
74
+ return fs.realpathSync(existingAncestor);
75
+ } catch {
76
+ return existingAncestor;
77
+ }
78
+ })();
79
+
80
+ const canonical = path.resolve(canonicalBase, path.relative(existingAncestor, resolvedCandidate));
81
+ const rel = path.relative(SYNC_DIR_REAL, canonical);
82
+ return !(rel.startsWith("..") || path.isAbsolute(rel));
83
+ }
84
+
54
85
  function resolveOutputDirWithinSync(outputDir) {
55
86
  let resolvedOutputDir = path.resolve(outputDir);
56
87
  let existingAncestor = resolvedOutputDir;
@@ -1346,6 +1377,142 @@ function createMcpServer() {
1346
1377
  }
1347
1378
  );
1348
1379
 
1380
+ // ---- prose styleguide ---------------------------------------------------
1381
+ s.tool(
1382
+ "setup_prose_styleguide_config",
1383
+ "Create prose-styleguide.config.yaml at sync root or project root using language defaults plus optional explicit overrides.",
1384
+ {
1385
+ scope: z.enum(["sync_root", "project_root"]).optional().describe("Config write target scope. Defaults to project_root when project_id is supplied, otherwise sync_root."),
1386
+ project_id: z.string().optional().describe("Project ID when writing project_root config (e.g. 'the-lamb' or 'universe-1/book-1')."),
1387
+ language: z.enum(STYLEGUIDE_ENUMS.language).describe("Primary writing language. Seeds language-specific defaults."),
1388
+ overrides: z.object({
1389
+ spelling: z.enum(STYLEGUIDE_ENUMS.spelling).optional(),
1390
+ quotation_style: z.enum(STYLEGUIDE_ENUMS.quotation_style).optional(),
1391
+ quotation_style_nested: z.enum(STYLEGUIDE_ENUMS.quotation_style_nested).optional(),
1392
+ em_dash_spacing: z.enum(STYLEGUIDE_ENUMS.em_dash_spacing).optional(),
1393
+ ellipsis_style: z.enum(STYLEGUIDE_ENUMS.ellipsis_style).optional(),
1394
+ abbreviation_periods: z.enum(STYLEGUIDE_ENUMS.abbreviation_periods).optional(),
1395
+ oxford_comma: z.enum(STYLEGUIDE_ENUMS.oxford_comma).optional(),
1396
+ numbers: z.enum(STYLEGUIDE_ENUMS.numbers).optional(),
1397
+ date_format: z.enum(STYLEGUIDE_ENUMS.date_format).optional(),
1398
+ time_format: z.enum(STYLEGUIDE_ENUMS.time_format).optional(),
1399
+ tense: z.string().optional(),
1400
+ pov: z.enum(STYLEGUIDE_ENUMS.pov).optional(),
1401
+ dialogue_tags: z.enum(STYLEGUIDE_ENUMS.dialogue_tags).optional(),
1402
+ sentence_fragments: z.enum(STYLEGUIDE_ENUMS.sentence_fragments).optional(),
1403
+ }).optional().describe("Optional overrides layered on top of language defaults."),
1404
+ voice_notes: z.string().optional().describe("Optional freeform voice notes to include in config."),
1405
+ overwrite: z.boolean().optional().describe("If true, replaces an existing config file at the target location."),
1406
+ },
1407
+ async ({ scope, project_id, language, overrides = {}, voice_notes, overwrite = false }) => {
1408
+ const resolvedScope = scope ?? (project_id ? "project_root" : "sync_root");
1409
+
1410
+ if (project_id !== undefined) {
1411
+ const projectIdCheck = validateProjectId(project_id);
1412
+ if (!projectIdCheck.ok) {
1413
+ return errorResponse("INVALID_PROJECT_ID", projectIdCheck.reason, { project_id });
1414
+ }
1415
+ }
1416
+
1417
+ if (resolvedScope === "project_root" && !project_id) {
1418
+ return errorResponse(
1419
+ "PROJECT_ID_REQUIRED",
1420
+ "project_id is required when scope=project_root."
1421
+ );
1422
+ }
1423
+
1424
+ if (!SYNC_DIR_WRITABLE) {
1425
+ return errorResponse(
1426
+ "SYNC_DIR_NOT_WRITABLE",
1427
+ "Cannot write styleguide config because WRITING_SYNC_DIR is not writable in this runtime.",
1428
+ { sync_dir: SYNC_DIR_ABS }
1429
+ );
1430
+ }
1431
+
1432
+ const targetPath = resolvedScope === "sync_root"
1433
+ ? path.join(SYNC_DIR, STYLEGUIDE_CONFIG_BASENAME)
1434
+ : path.join(resolveProjectRoot(project_id), STYLEGUIDE_CONFIG_BASENAME);
1435
+
1436
+ if (!isPathCandidateInsideSyncDir(targetPath)) {
1437
+ return errorResponse(
1438
+ "INVALID_CONFIG_PATH",
1439
+ "Resolved styleguide config path must be inside WRITING_SYNC_DIR.",
1440
+ { target_path: path.resolve(targetPath), sync_dir: SYNC_DIR_ABS }
1441
+ );
1442
+ }
1443
+
1444
+ if (fs.existsSync(targetPath) && !overwrite) {
1445
+ return errorResponse(
1446
+ "STYLEGUIDE_CONFIG_EXISTS",
1447
+ "Styleguide config already exists at target path. Set overwrite=true to replace it.",
1448
+ { target_path: path.resolve(targetPath) }
1449
+ );
1450
+ }
1451
+
1452
+ const draft = buildStyleguideConfigDraft({
1453
+ language,
1454
+ overrides,
1455
+ voice_notes,
1456
+ });
1457
+ if (!draft.ok) {
1458
+ return errorResponse(
1459
+ draft.error.code,
1460
+ draft.error.message,
1461
+ draft.error.details
1462
+ );
1463
+ }
1464
+
1465
+ fs.mkdirSync(path.dirname(targetPath), { recursive: true });
1466
+ fs.writeFileSync(targetPath, yaml.dump(draft.config, { lineWidth: 120 }), "utf8");
1467
+
1468
+ return jsonResponse({
1469
+ ok: true,
1470
+ scope: resolvedScope,
1471
+ file_path: path.resolve(targetPath),
1472
+ config: draft.config,
1473
+ inferred_defaults: draft.inferred_defaults,
1474
+ warnings: draft.warnings,
1475
+ });
1476
+ }
1477
+ );
1478
+
1479
+ s.tool(
1480
+ "get_prose_styleguide_config",
1481
+ "Resolve prose-styleguide.config.yaml with cascading precedence (sync root, then universe root, then project root). Applies language-derived defaults and nested quotation defaults when omitted.",
1482
+ {
1483
+ project_id: z.string().optional().describe("Optional project ID for project-scoped resolution (e.g. 'the-lamb' or 'universe-1/book-1')."),
1484
+ },
1485
+ async ({ project_id }) => {
1486
+ if (project_id !== undefined) {
1487
+ const projectIdCheck = validateProjectId(project_id);
1488
+ if (!projectIdCheck.ok) {
1489
+ return errorResponse("INVALID_PROJECT_ID", projectIdCheck.reason, { project_id });
1490
+ }
1491
+ }
1492
+
1493
+ const resolved = resolveStyleguideConfig({
1494
+ syncDir: SYNC_DIR,
1495
+ projectId: project_id,
1496
+ });
1497
+
1498
+ if (!resolved.ok) {
1499
+ return errorResponse(
1500
+ resolved.error.code,
1501
+ resolved.error.message,
1502
+ resolved.error.details
1503
+ );
1504
+ }
1505
+
1506
+ return jsonResponse({
1507
+ ok: true,
1508
+ styleguide: resolved,
1509
+ next_step: resolved.setup_required
1510
+ ? "No prose-styleguide.config.yaml was found. Run setup to create one at sync root or project root."
1511
+ : "Config resolved successfully.",
1512
+ });
1513
+ }
1514
+ );
1515
+
1349
1516
  // ---- preview_review_bundle ----------------------------------------------
1350
1517
  s.tool(
1351
1518
  "preview_review_bundle",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanna84/mcp-writing",
3
- "version": "2.0.4",
3
+ "version": "2.2.0",
4
4
  "description": "MCP service for AI-assisted reasoning and editing on long-form fiction projects",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -15,7 +15,9 @@
15
15
  "git.js",
16
16
  "world-entity-templates.js",
17
17
  "metadata-lint.js",
18
+ "scene-character-normalization.js",
18
19
  "review-bundles.js",
20
+ "prose-styleguide.js",
19
21
  "scripts/",
20
22
  "README.md",
21
23
  "CHANGELOG.md"
@@ -31,6 +33,7 @@
31
33
  "manual:scenarios": "node scripts/manual/test-scenarios.mjs",
32
34
  "manual:merge-beta-test": "node scripts/manual/run_mcp_test.js",
33
35
  "manual:review-bundle": "node scripts/manual/run_create_review_bundle.js",
36
+ "normalize:scene-characters": "node --experimental-sqlite scripts/normalize-scene-characters.mjs",
34
37
  "setup:openclaw-env": "sh scripts/setup-openclaw-env.sh",
35
38
  "release": "release-it",
36
39
  "lint": "eslint index.js importer.js db.js sync.js metadata-lint.js scripts/",
@@ -0,0 +1,494 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import yaml from "js-yaml";
4
+
5
+ export const STYLEGUIDE_CONFIG_BASENAME = "prose-styleguide.config.yaml";
6
+
7
+ const ENUMS = {
8
+ language: [
9
+ "english_us",
10
+ "english_uk",
11
+ "english_au",
12
+ "english_ca",
13
+ "swedish",
14
+ "norwegian",
15
+ "danish",
16
+ "finnish",
17
+ "french",
18
+ "italian",
19
+ "russian",
20
+ "portuguese_pt",
21
+ "portuguese_br",
22
+ "german",
23
+ "dutch",
24
+ "polish",
25
+ "czech",
26
+ "hungarian",
27
+ "spanish",
28
+ "irish",
29
+ "japanese",
30
+ "korean",
31
+ "chinese_traditional",
32
+ "chinese_simplified",
33
+ ],
34
+ spelling: ["uk", "us", "au", "ca"],
35
+ quotation_style: [
36
+ "double",
37
+ "single",
38
+ "guillemets",
39
+ "low9",
40
+ "dialogue_dash_en",
41
+ "dialogue_dash_em",
42
+ "corner_brackets",
43
+ ],
44
+ quotation_style_nested: [
45
+ "double",
46
+ "single",
47
+ "guillemets_single",
48
+ "low9_single",
49
+ "corner_brackets_double",
50
+ ],
51
+ em_dash_spacing: ["closed", "spaced"],
52
+ ellipsis_style: ["three_periods", "ellipsis_char", "spaced"],
53
+ abbreviation_periods: ["with", "without"],
54
+ oxford_comma: ["yes", "no"],
55
+ numbers: ["spell_under_10", "spell_under_100", "always_spell", "numerals"],
56
+ date_format: ["mdy", "dmy"],
57
+ time_format: ["12h", "24h"],
58
+ tense: ["present", "past"],
59
+ pov: ["first", "third_limited", "third_omniscient"],
60
+ dialogue_tags: ["minimal", "expressive"],
61
+ sentence_fragments: ["disallow", "intentional"],
62
+ };
63
+
64
+ export const STYLEGUIDE_ENUMS = Object.freeze(
65
+ Object.fromEntries(
66
+ Object.entries(ENUMS).map(([key, values]) => [key, [...values]])
67
+ )
68
+ );
69
+
70
+ // Fields that are valid in a config but are not enum-constrained.
71
+ const SPECIAL_FIELDS = new Set(["voice_notes"]);
72
+
73
+ const LANGUAGE_DEFAULTS = {
74
+ english_us: {
75
+ spelling: "us",
76
+ quotation_style: "double",
77
+ em_dash_spacing: "closed",
78
+ abbreviation_periods: "with",
79
+ oxford_comma: "yes",
80
+ date_format: "mdy",
81
+ },
82
+ english_uk: {
83
+ spelling: "uk",
84
+ quotation_style: "single",
85
+ em_dash_spacing: "spaced",
86
+ abbreviation_periods: "without",
87
+ oxford_comma: "no",
88
+ date_format: "dmy",
89
+ },
90
+ english_au: {
91
+ spelling: "au",
92
+ quotation_style: "double",
93
+ em_dash_spacing: "closed",
94
+ abbreviation_periods: "without",
95
+ oxford_comma: "yes",
96
+ date_format: "dmy",
97
+ },
98
+ english_ca: {
99
+ spelling: "ca",
100
+ quotation_style: "double",
101
+ em_dash_spacing: "spaced",
102
+ abbreviation_periods: "without",
103
+ oxford_comma: "yes",
104
+ date_format: "dmy",
105
+ },
106
+ swedish: {
107
+ quotation_style: "dialogue_dash_en",
108
+ em_dash_spacing: "spaced",
109
+ date_format: "dmy",
110
+ },
111
+ norwegian: {
112
+ quotation_style: "dialogue_dash_en",
113
+ em_dash_spacing: "spaced",
114
+ date_format: "dmy",
115
+ },
116
+ danish: {
117
+ quotation_style: "dialogue_dash_en",
118
+ em_dash_spacing: "spaced",
119
+ date_format: "dmy",
120
+ },
121
+ finnish: {
122
+ quotation_style: "guillemets",
123
+ em_dash_spacing: "spaced",
124
+ date_format: "dmy",
125
+ },
126
+ french: {
127
+ quotation_style: "guillemets",
128
+ em_dash_spacing: "spaced",
129
+ date_format: "dmy",
130
+ },
131
+ italian: {
132
+ quotation_style: "guillemets",
133
+ em_dash_spacing: "spaced",
134
+ date_format: "dmy",
135
+ },
136
+ russian: {
137
+ quotation_style: "guillemets",
138
+ em_dash_spacing: "spaced",
139
+ date_format: "dmy",
140
+ },
141
+ portuguese_pt: {
142
+ quotation_style: "guillemets",
143
+ em_dash_spacing: "spaced",
144
+ date_format: "dmy",
145
+ },
146
+ portuguese_br: {
147
+ quotation_style: "double",
148
+ em_dash_spacing: "closed",
149
+ date_format: "dmy",
150
+ },
151
+ german: {
152
+ quotation_style: "low9",
153
+ em_dash_spacing: "spaced",
154
+ date_format: "dmy",
155
+ },
156
+ dutch: {
157
+ quotation_style: "low9",
158
+ em_dash_spacing: "spaced",
159
+ date_format: "dmy",
160
+ },
161
+ polish: {
162
+ quotation_style: "low9",
163
+ em_dash_spacing: "spaced",
164
+ date_format: "dmy",
165
+ },
166
+ czech: {
167
+ quotation_style: "low9",
168
+ em_dash_spacing: "spaced",
169
+ date_format: "dmy",
170
+ },
171
+ hungarian: {
172
+ quotation_style: "low9",
173
+ em_dash_spacing: "spaced",
174
+ date_format: "dmy",
175
+ },
176
+ spanish: {
177
+ quotation_style: "dialogue_dash_em",
178
+ em_dash_spacing: "spaced",
179
+ date_format: "dmy",
180
+ },
181
+ irish: {
182
+ quotation_style: "dialogue_dash_em",
183
+ em_dash_spacing: "spaced",
184
+ date_format: "dmy",
185
+ },
186
+ japanese: {
187
+ quotation_style: "corner_brackets",
188
+ },
189
+ korean: {
190
+ quotation_style: "corner_brackets",
191
+ },
192
+ chinese_traditional: {
193
+ quotation_style: "corner_brackets",
194
+ },
195
+ chinese_simplified: {
196
+ quotation_style: "double",
197
+ },
198
+ };
199
+
200
+ function projectRootFromId(syncDir, projectId) {
201
+ if (!projectId.includes("/")) {
202
+ return path.join(syncDir, "projects", projectId);
203
+ }
204
+ const [universeId, projectSlug] = projectId.split("/");
205
+ return path.join(syncDir, "universes", universeId, projectSlug);
206
+ }
207
+
208
+ function inferNestedQuotationStyle(quotationStyle) {
209
+ if (quotationStyle === "double") return "single";
210
+ if (quotationStyle === "single") return "double";
211
+ if (quotationStyle === "guillemets") return "guillemets_single";
212
+ if (quotationStyle === "low9") return "low9_single";
213
+ if (quotationStyle === "corner_brackets") return "corner_brackets_double";
214
+ return null;
215
+ }
216
+
217
+ function normalizeTense(value) {
218
+ if (typeof value !== "string") return null;
219
+ const trimmed = value.trim().toLowerCase();
220
+ if (!trimmed) return null;
221
+
222
+ if (trimmed.startsWith("present")) return "present";
223
+ if (trimmed.startsWith("past")) return "past";
224
+ return trimmed;
225
+ }
226
+
227
+ function normalizeConfigShape(rawConfig) {
228
+ const normalized = Object.create(null);
229
+ for (const [key, value] of Object.entries(rawConfig ?? {})) {
230
+ // Skip null/undefined — treat as unset, same as a missing key.
231
+ if (value === null || value === undefined) continue;
232
+ if (typeof value === "string") {
233
+ const trimmed = value.trim();
234
+ if (trimmed !== "") {
235
+ normalized[key] = trimmed;
236
+ }
237
+ continue;
238
+ }
239
+ normalized[key] = value;
240
+ }
241
+ return normalized;
242
+ }
243
+
244
+ function validateConfig(config, sourcePath) {
245
+ const normalized = normalizeConfigShape(config);
246
+ const sanitized = Object.create(null);
247
+ const errors = [];
248
+ const unknownFields = [];
249
+
250
+ for (const [key, value] of Object.entries(normalized)) {
251
+ if (!Object.hasOwn(ENUMS, key) && !SPECIAL_FIELDS.has(key)) {
252
+ unknownFields.push(key);
253
+ continue;
254
+ }
255
+
256
+ if (SPECIAL_FIELDS.has(key)) {
257
+ if (typeof value !== "string") {
258
+ errors.push({
259
+ code: "INVALID_TYPE",
260
+ field: key,
261
+ message: `${key} must be a string.`,
262
+ source: sourcePath,
263
+ });
264
+ }
265
+ if (typeof value === "string") {
266
+ sanitized[key] = value;
267
+ }
268
+ continue;
269
+ }
270
+
271
+ if (typeof value !== "string") {
272
+ errors.push({
273
+ code: "INVALID_TYPE",
274
+ field: key,
275
+ message: `${key} must be a string enum value.`,
276
+ source: sourcePath,
277
+ });
278
+ continue;
279
+ }
280
+
281
+ const valueToCheck = key === "tense" ? normalizeTense(value) : value;
282
+ if (!ENUMS[key].includes(valueToCheck)) {
283
+ errors.push({
284
+ code: "INVALID_ENUM",
285
+ field: key,
286
+ message: `${key} must be one of: ${ENUMS[key].join(", ")}.`,
287
+ source: sourcePath,
288
+ received: value,
289
+ });
290
+ continue;
291
+ }
292
+
293
+ sanitized[key] = value;
294
+ }
295
+
296
+ return {
297
+ normalized: sanitized,
298
+ errors,
299
+ unknownFields,
300
+ };
301
+ }
302
+
303
+ function readConfigFile(filePath) {
304
+ if (!fs.existsSync(filePath)) return null;
305
+
306
+ let parsed;
307
+ try {
308
+ parsed = yaml.load(fs.readFileSync(filePath, "utf8"));
309
+ } catch (error) {
310
+ return {
311
+ ok: false,
312
+ errors: [{
313
+ code: "INVALID_YAML",
314
+ message: error instanceof Error ? error.message : "Invalid YAML.",
315
+ source: filePath,
316
+ }],
317
+ };
318
+ }
319
+
320
+ if (parsed === null || parsed === undefined) {
321
+ return { ok: true, config: {} };
322
+ }
323
+
324
+ if (typeof parsed !== "object" || Array.isArray(parsed)) {
325
+ return {
326
+ ok: false,
327
+ errors: [{
328
+ code: "INVALID_CONFIG",
329
+ message: "Config file must parse to an object.",
330
+ source: filePath,
331
+ }],
332
+ };
333
+ }
334
+
335
+ const { normalized, errors, unknownFields } = validateConfig(parsed, filePath);
336
+ if (errors.length > 0) {
337
+ return { ok: false, errors };
338
+ }
339
+
340
+ return {
341
+ ok: true,
342
+ config: normalized,
343
+ unknown_fields: unknownFields,
344
+ };
345
+ }
346
+
347
+ function getConfigCandidates(syncDir, projectId) {
348
+ const candidates = [
349
+ {
350
+ scope: "sync_root",
351
+ file_path: path.join(syncDir, STYLEGUIDE_CONFIG_BASENAME),
352
+ },
353
+ ];
354
+
355
+ if (!projectId) return candidates;
356
+
357
+ if (projectId.includes("/")) {
358
+ const [universeId] = projectId.split("/");
359
+ candidates.push({
360
+ scope: "universe_root",
361
+ file_path: path.join(syncDir, "universes", universeId, STYLEGUIDE_CONFIG_BASENAME),
362
+ });
363
+ }
364
+
365
+ candidates.push({
366
+ scope: "project_root",
367
+ file_path: path.join(projectRootFromId(syncDir, projectId), STYLEGUIDE_CONFIG_BASENAME),
368
+ });
369
+
370
+ return candidates;
371
+ }
372
+
373
+ function applyDerivedDefaults(config) {
374
+ const resolved = { ...config };
375
+ const inferred_defaults = {};
376
+
377
+ if (resolved.language && LANGUAGE_DEFAULTS[resolved.language]) {
378
+ const defaults = LANGUAGE_DEFAULTS[resolved.language];
379
+ for (const [key, value] of Object.entries(defaults)) {
380
+ if (resolved[key] === undefined) {
381
+ resolved[key] = value;
382
+ inferred_defaults[key] = value;
383
+ }
384
+ }
385
+ }
386
+
387
+ if (!resolved.quotation_style_nested && resolved.quotation_style) {
388
+ const nested = inferNestedQuotationStyle(resolved.quotation_style);
389
+ if (nested) {
390
+ resolved.quotation_style_nested = nested;
391
+ inferred_defaults.quotation_style_nested = nested;
392
+ }
393
+ }
394
+
395
+ if (resolved.tense) {
396
+ resolved.tense = normalizeTense(resolved.tense);
397
+ }
398
+
399
+ return { resolved, inferred_defaults };
400
+ }
401
+
402
+ export function buildStyleguideConfigDraft({ language, overrides = {}, voice_notes }) {
403
+ const overrideValidation = validateConfig(overrides, "<overrides>");
404
+ if (overrideValidation.errors.length > 0) {
405
+ return {
406
+ ok: false,
407
+ error: {
408
+ code: "INVALID_STYLEGUIDE_OVERRIDE",
409
+ message: "Requested styleguide overrides failed validation.",
410
+ details: overrideValidation.errors,
411
+ },
412
+ };
413
+ }
414
+
415
+ if (!ENUMS.language.includes(language)) {
416
+ return {
417
+ ok: false,
418
+ error: {
419
+ code: "INVALID_STYLEGUIDE_LANGUAGE",
420
+ message: `language must be one of: ${ENUMS.language.join(", ")}.`,
421
+ },
422
+ };
423
+ }
424
+
425
+ const merged = {
426
+ ...overrideValidation.normalized,
427
+ language,
428
+ };
429
+
430
+ if (typeof voice_notes === "string" && voice_notes.trim()) {
431
+ merged.voice_notes = voice_notes.trim();
432
+ }
433
+
434
+ const { resolved, inferred_defaults } = applyDerivedDefaults(merged);
435
+ return {
436
+ ok: true,
437
+ config: resolved,
438
+ inferred_defaults,
439
+ warnings: {
440
+ unknown_fields: overrideValidation.unknownFields,
441
+ },
442
+ };
443
+ }
444
+
445
+ export function resolveStyleguideConfig({ syncDir, projectId }) {
446
+ const candidates = getConfigCandidates(syncDir, projectId);
447
+ const sources = [];
448
+ const unknownFields = [];
449
+ const merged = Object.create(null);
450
+
451
+ for (const candidate of candidates) {
452
+ const loaded = readConfigFile(candidate.file_path);
453
+ if (loaded === null) continue;
454
+
455
+ if (!loaded.ok) {
456
+ return {
457
+ ok: false,
458
+ error: {
459
+ code: "INVALID_STYLEGUIDE_CONFIG",
460
+ message: "Styleguide config validation failed.",
461
+ details: {
462
+ file_path: candidate.file_path,
463
+ issues: loaded.errors,
464
+ },
465
+ },
466
+ };
467
+ }
468
+
469
+ Object.assign(merged, loaded.config);
470
+ if (loaded.unknown_fields?.length) {
471
+ for (const field of loaded.unknown_fields) {
472
+ unknownFields.push({ scope: candidate.scope, field, source: candidate.file_path });
473
+ }
474
+ }
475
+
476
+ sources.push({
477
+ scope: candidate.scope,
478
+ file_path: candidate.file_path,
479
+ });
480
+ }
481
+
482
+ const { resolved, inferred_defaults } = applyDerivedDefaults(merged);
483
+ return {
484
+ ok: true,
485
+ config_found: sources.length > 0,
486
+ setup_required: sources.length === 0,
487
+ resolved_config: sources.length > 0 ? resolved : null,
488
+ inferred_defaults,
489
+ sources,
490
+ warnings: {
491
+ unknown_fields: unknownFields,
492
+ },
493
+ };
494
+ }
@@ -1,69 +1,10 @@
1
1
  import fs from "node:fs";
2
2
  import matter from "gray-matter";
3
+ import { buildCharacterNormalizationContext, escapeRegex, resolveCharacterReference } from "./scene-character-normalization.js";
3
4
  import { normalizeSceneMetaForPath, readMeta, writeMeta } from "./sync.js";
4
5
 
5
- const NON_DISTINCTIVE_TOKENS = new Set([
6
- "the",
7
- "and",
8
- "for",
9
- "with",
10
- "from",
11
- "into",
12
- "onto",
13
- "over",
14
- "under",
15
- "after",
16
- "before",
17
- "about",
18
- "around",
19
- ]);
20
-
21
- function escapeRegex(text) {
22
- return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
23
- }
24
-
25
- function isDistinctiveToken(token) {
26
- return Boolean(token) && token.length >= 3 && !NON_DISTINCTIVE_TOKENS.has(token);
27
- }
28
-
29
6
  function normalizeCharacterRows(rows) {
30
- const clean = rows
31
- .filter(row => row?.character_id && row?.name)
32
- .map(row => {
33
- const phrase_tokens = String(row.name).toLowerCase().split(/\s+/).filter(Boolean);
34
- const tokens = [...new Set(phrase_tokens)];
35
- return {
36
- character_id: row.character_id,
37
- name: String(row.name).trim(),
38
- phrase_tokens,
39
- tokens,
40
- informative_tokens: tokens.filter(isDistinctiveToken),
41
- full_name_regex: phrase_tokens.length > 1
42
- ? new RegExp(`\\b${phrase_tokens.map(escapeRegex).join("\\s+")}\\b`, "i")
43
- : null,
44
- };
45
- })
46
- .filter(row => row.name.length > 0);
47
-
48
- const tokenMap = new Map();
49
- const byId = new Map();
50
- const nameMap = new Map();
51
- for (const row of clean) {
52
- byId.set(row.character_id, row);
53
-
54
- const normalizedName = row.name.toLowerCase();
55
- const exactNameIds = nameMap.get(normalizedName) ?? [];
56
- exactNameIds.push(row.character_id);
57
- nameMap.set(normalizedName, exactNameIds);
58
-
59
- for (const token of row.informative_tokens) {
60
- const ids = tokenMap.get(token) ?? [];
61
- ids.push(row.character_id);
62
- tokenMap.set(token, ids);
63
- }
64
- }
65
-
66
- return { clean, tokenMap, byId, nameMap };
7
+ return buildCharacterNormalizationContext(rows);
67
8
  }
68
9
 
69
10
  function inferCharactersFromProse(prose, characterRows) {
@@ -113,32 +54,7 @@ function inferCharactersFromProse(prose, characterRows) {
113
54
  }
114
55
 
115
56
  function resolveCharacterEntry(entry, characterRows) {
116
- const value = String(entry ?? "").trim();
117
- if (!value) return null;
118
-
119
- if (characterRows.byId.has(value)) {
120
- return value;
121
- }
122
-
123
- const exactNameIds = characterRows.nameMap.get(value.toLowerCase());
124
- if (exactNameIds?.length === 1) {
125
- return exactNameIds[0];
126
- }
127
-
128
- const words = value.toLowerCase().split(/\s+/).filter(isDistinctiveToken);
129
- if (words.length === 0) {
130
- return value;
131
- }
132
-
133
- const matches = characterRows.clean.filter(row =>
134
- words.every(word => row.informative_tokens.includes(word))
135
- );
136
-
137
- if (matches.length === 1) {
138
- return matches[0].character_id;
139
- }
140
-
141
- return value;
57
+ return resolveCharacterReference(entry, characterRows);
142
58
  }
143
59
 
144
60
  function pruneLessSpecificCharacters(characterIds, fullNameMatches, characterRows) {
@@ -0,0 +1,199 @@
1
+ export const NON_DISTINCTIVE_TOKENS = new Set([
2
+ "the",
3
+ "and",
4
+ "for",
5
+ "with",
6
+ "from",
7
+ "into",
8
+ "onto",
9
+ "over",
10
+ "under",
11
+ "after",
12
+ "before",
13
+ "about",
14
+ "around",
15
+ ]);
16
+
17
+ export function escapeRegex(text) {
18
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
19
+ }
20
+
21
+ export function isDistinctiveToken(token) {
22
+ const normalized = String(token ?? "").trim().toLowerCase();
23
+ return Boolean(normalized) && normalized.length >= 3 && !NON_DISTINCTIVE_TOKENS.has(normalized);
24
+ }
25
+
26
+ function normalizeRawCharacterValues(values) {
27
+ const raw = Array.isArray(values) ? values : [];
28
+ const seen = new Set();
29
+ const normalized = [];
30
+
31
+ for (const value of raw) {
32
+ const text = String(value ?? "").trim();
33
+ if (!text || seen.has(text)) continue;
34
+ seen.add(text);
35
+ normalized.push(text);
36
+ }
37
+
38
+ return normalized;
39
+ }
40
+
41
+ function tokenizeValue(value) {
42
+ return [...new Set(String(value ?? "").toLowerCase().split(/\s+/).filter(isDistinctiveToken))];
43
+ }
44
+
45
+ export function buildCharacterNormalizationContext(rows) {
46
+ const clean = (Array.isArray(rows) ? rows : [])
47
+ .filter(row => row?.character_id && row?.name)
48
+ .map(row => {
49
+ const character_id = String(row.character_id).trim();
50
+ const name = String(row.name).trim();
51
+ const phrase_tokens = name.toLowerCase().split(/\s+/).filter(Boolean);
52
+ const tokens = [...new Set(phrase_tokens)];
53
+ return {
54
+ character_id,
55
+ name,
56
+ phrase_tokens,
57
+ tokens,
58
+ informative_tokens: tokens.filter(isDistinctiveToken),
59
+ full_name_regex: phrase_tokens.length > 1
60
+ ? new RegExp(`\\b${phrase_tokens.map(escapeRegex).join("\\s+")}\\b`, "i")
61
+ : null,
62
+ };
63
+ })
64
+ .filter(row => row.character_id.length > 0 && row.name.length > 0);
65
+
66
+ const byId = new Map();
67
+ const nameMap = new Map();
68
+ const tokenMap = new Map();
69
+ for (const row of clean) {
70
+ byId.set(row.character_id, row);
71
+ const normalizedName = row.name.toLowerCase();
72
+ const ids = nameMap.get(normalizedName) ?? [];
73
+ ids.push(row.character_id);
74
+ nameMap.set(normalizedName, ids);
75
+
76
+ for (const token of row.informative_tokens) {
77
+ const tokenIds = tokenMap.get(token) ?? [];
78
+ tokenIds.push(row.character_id);
79
+ tokenMap.set(token, tokenIds);
80
+ }
81
+ }
82
+
83
+ return { clean, byId, nameMap, tokenMap };
84
+ }
85
+
86
+ export function resolveCharacterReference(value, context) {
87
+ const text = String(value ?? "").trim();
88
+ if (!text) return null;
89
+
90
+ if (context.byId.has(text)) {
91
+ return text;
92
+ }
93
+
94
+ const exactNameIds = context.nameMap.get(text.toLowerCase());
95
+ if (exactNameIds?.length === 1) {
96
+ return exactNameIds[0];
97
+ }
98
+
99
+ const words = text.toLowerCase().split(/\s+/).filter(isDistinctiveToken);
100
+ if (words.length === 0) {
101
+ return text;
102
+ }
103
+
104
+ const matches = context.clean.filter(row =>
105
+ words.every(word => row.informative_tokens.includes(word))
106
+ );
107
+
108
+ if (matches.length === 1) {
109
+ return matches[0].character_id;
110
+ }
111
+
112
+ return text;
113
+ }
114
+
115
+ function isProperSubset(subsetTokens, supersetTokens) {
116
+ if (subsetTokens.length < 2 || subsetTokens.length >= supersetTokens.length) {
117
+ return false;
118
+ }
119
+ return subsetTokens.every(token => supersetTokens.includes(token));
120
+ }
121
+
122
+ function hasMoreSpecificNonCanonicalSource(candidate, sourceInfo) {
123
+ if (!sourceInfo || sourceInfo.hadCanonicalSource || sourceInfo.nonCanonicalTokens.length === 0) {
124
+ return false;
125
+ }
126
+
127
+ return sourceInfo.nonCanonicalTokens.some(tokens => isProperSubset(candidate.informative_tokens, tokens));
128
+ }
129
+
130
+ function pruneLessSpecificCanonicalIds(values, context, sourceMap) {
131
+ return values.filter((value, idx) => {
132
+ const row = context.byId.get(value);
133
+ if (!row || row.informative_tokens.length === 0) {
134
+ return true;
135
+ }
136
+
137
+ const rowSource = sourceMap.get(value);
138
+ if (!rowSource?.hadCanonicalSource) {
139
+ return true;
140
+ }
141
+
142
+ for (let i = 0; i < values.length; i++) {
143
+ if (i === idx) continue;
144
+
145
+ const otherId = values[i];
146
+ const other = context.byId.get(otherId);
147
+ if (!other || other.informative_tokens.length === 0) continue;
148
+
149
+ if (
150
+ isProperSubset(row.informative_tokens, other.informative_tokens)
151
+ && hasMoreSpecificNonCanonicalSource(row, sourceMap.get(otherId))
152
+ ) {
153
+ return false;
154
+ }
155
+ }
156
+
157
+ return true;
158
+ });
159
+ }
160
+
161
+ export function normalizeSceneCharacters(values, context) {
162
+ const before = normalizeRawCharacterValues(values);
163
+ const resolved = [];
164
+ const seen = new Set();
165
+ const sourceMap = new Map();
166
+
167
+ for (const value of before) {
168
+ const normalized = resolveCharacterReference(value, context);
169
+ if (!normalized) continue;
170
+
171
+ const source = sourceMap.get(normalized) ?? {
172
+ hadCanonicalSource: false,
173
+ nonCanonicalTokens: [],
174
+ };
175
+
176
+ if (context.byId.has(value)) {
177
+ source.hadCanonicalSource = true;
178
+ } else {
179
+ source.nonCanonicalTokens.push(tokenizeValue(value));
180
+ }
181
+ sourceMap.set(normalized, source);
182
+
183
+ if (seen.has(normalized)) continue;
184
+ seen.add(normalized);
185
+ resolved.push(normalized);
186
+ }
187
+
188
+ const after = pruneLessSpecificCanonicalIds(resolved, context, sourceMap);
189
+ const beforeSet = new Set(before);
190
+ const afterSet = new Set(after);
191
+
192
+ return {
193
+ before,
194
+ after,
195
+ changed: before.length !== after.length || before.some((value, idx) => after[idx] !== value),
196
+ added: after.filter(value => !beforeSet.has(value)),
197
+ removed: before.filter(value => !afterSet.has(value)),
198
+ };
199
+ }
@@ -0,0 +1,225 @@
1
+ #!/usr/bin/env node
2
+ import path from "node:path";
3
+ import { openDb } from "../db.js";
4
+ import { buildCharacterNormalizationContext, normalizeSceneCharacters } from "../scene-character-normalization.js";
5
+ import { normalizeSceneMetaForPath, readMeta, syncAll, writeMeta } from "../sync.js";
6
+
7
+ function readRequiredValue(argv, index, option) {
8
+ const value = argv[index + 1];
9
+ if (value === undefined || value.startsWith("-")) {
10
+ throw new Error(`${option} requires a value.`);
11
+ }
12
+ return value;
13
+ }
14
+
15
+ function parseArgs(argv) {
16
+ const opts = {
17
+ syncDir: process.env.WRITING_SYNC_DIR ?? "./sync",
18
+ projectId: null,
19
+ write: false,
20
+ json: false,
21
+ limit: null,
22
+ help: false,
23
+ };
24
+
25
+ for (let i = 0; i < argv.length; i++) {
26
+ const arg = argv[i];
27
+ if (arg === "--sync-dir" || arg === "-d") {
28
+ opts.syncDir = readRequiredValue(argv, i, arg);
29
+ i++;
30
+ } else if (arg === "--project-id" || arg === "-p") {
31
+ opts.projectId = readRequiredValue(argv, i, arg);
32
+ i++;
33
+ } else if (arg === "--limit" || arg === "-n") {
34
+ opts.limit = Number.parseInt(readRequiredValue(argv, i, arg), 10);
35
+ i++;
36
+ } else if (arg === "--write") {
37
+ opts.write = true;
38
+ } else if (arg === "--json") {
39
+ opts.json = true;
40
+ } else if (arg === "--help" || arg === "-h") {
41
+ opts.help = true;
42
+ } else {
43
+ throw new Error(`Unknown argument: ${arg}`);
44
+ }
45
+ }
46
+
47
+ if (opts.limit !== null && (!Number.isInteger(opts.limit) || opts.limit <= 0)) {
48
+ throw new Error("--limit must be a positive integer.");
49
+ }
50
+
51
+ return opts;
52
+ }
53
+
54
+ function usage() {
55
+ return [
56
+ "Usage:",
57
+ " node --experimental-sqlite scripts/normalize-scene-characters.mjs [--sync-dir <dir>] [--project-id <id>] [--limit <n>] [--write] [--json]",
58
+ "",
59
+ "Options:",
60
+ " --sync-dir, -d WRITING_SYNC_DIR root (default: env WRITING_SYNC_DIR or ./sync)",
61
+ " --project-id, -p Restrict to one project_id",
62
+ " --limit, -n Process at most N scenes",
63
+ " --write Apply changes (default: dry-run)",
64
+ " --json Emit machine-readable JSON summary",
65
+ "",
66
+ "Note: Uses an in-memory sqlite index for analysis; no mcp.sqlite file is created in sync_dir.",
67
+ ].join("\n");
68
+ }
69
+
70
+ function queryRows(db, sql, ...params) {
71
+ return db.prepare(sql).all(...params);
72
+ }
73
+
74
+ function resolveCharacterRows(db, projectId) {
75
+ return queryRows(
76
+ db,
77
+ `SELECT character_id, name
78
+ FROM characters
79
+ WHERE project_id = ?
80
+ OR universe_id = (SELECT universe_id FROM projects WHERE project_id = ?)
81
+ ORDER BY length(name) DESC`,
82
+ projectId,
83
+ projectId
84
+ );
85
+ }
86
+
87
+ function resolveScenes(db, projectId, limit) {
88
+ const limitClause = Number.isInteger(limit) ? ` LIMIT ${limit}` : "";
89
+ if (!projectId) {
90
+ return queryRows(
91
+ db,
92
+ `SELECT scene_id, project_id, file_path
93
+ FROM scenes
94
+ ORDER BY project_id, part, chapter, timeline_position, scene_id${limitClause}`
95
+ );
96
+ }
97
+ return queryRows(
98
+ db,
99
+ `SELECT scene_id, project_id, file_path
100
+ FROM scenes
101
+ WHERE project_id = ?
102
+ ORDER BY part, chapter, timeline_position, scene_id${limitClause}`,
103
+ projectId
104
+ );
105
+ }
106
+
107
+ function runNormalization({ syncDir, projectId, write, limit }) {
108
+ const db = openDb(":memory:");
109
+ try {
110
+ // Refresh index so character/name resolution uses current canonical sheets and sidecars.
111
+ syncAll(db, syncDir, { quiet: true, writable: false });
112
+
113
+ const scenes = resolveScenes(db, projectId, limit);
114
+ const contextCache = new Map();
115
+
116
+ const getContextForProject = (sceneProjectId) => {
117
+ const key = sceneProjectId ?? "__none__";
118
+ if (contextCache.has(key)) return contextCache.get(key);
119
+
120
+ const context = buildCharacterNormalizationContext(resolveCharacterRows(db, sceneProjectId));
121
+ contextCache.set(key, context);
122
+ return context;
123
+ };
124
+
125
+ const changed = [];
126
+ let processedScenes = 0;
127
+
128
+ for (const scene of scenes) {
129
+ const { meta } = readMeta(scene.file_path, syncDir, { writable: false });
130
+ if (!Array.isArray(meta.characters) || meta.characters.length === 0) {
131
+ processedScenes++;
132
+ continue;
133
+ }
134
+
135
+ const normalized = normalizeSceneCharacters(meta.characters, getContextForProject(scene.project_id));
136
+ processedScenes++;
137
+
138
+ if (!normalized.changed) continue;
139
+
140
+ if (write) {
141
+ const updatedMeta = normalizeSceneMetaForPath(syncDir, scene.file_path, {
142
+ ...meta,
143
+ characters: normalized.after,
144
+ }).meta;
145
+ writeMeta(scene.file_path, updatedMeta);
146
+ }
147
+
148
+ changed.push({
149
+ scene_id: scene.scene_id,
150
+ project_id: scene.project_id,
151
+ file_path: scene.file_path,
152
+ before_characters: normalized.before,
153
+ after_characters: normalized.after,
154
+ added: normalized.added,
155
+ removed: normalized.removed,
156
+ });
157
+ }
158
+
159
+ return {
160
+ ok: true,
161
+ mode: write ? "write" : "dry_run",
162
+ sync_dir: path.resolve(syncDir),
163
+ project_id: projectId,
164
+ processed_scenes: processedScenes,
165
+ scenes_changed: changed.length,
166
+ character_reference_count: [...contextCache.values()].reduce((sum, ctx) => sum + ctx.clean.length, 0),
167
+ changes: changed,
168
+ };
169
+ } finally {
170
+ db.close();
171
+ }
172
+ }
173
+
174
+ function printTextSummary(result) {
175
+ process.stdout.write(`normalize-scene-characters (${result.mode})\n`);
176
+ process.stdout.write(`sync_dir: ${result.sync_dir}\n`);
177
+ process.stdout.write(`project_id: ${result.project_id ?? "(all projects)"}\n`);
178
+ process.stdout.write(`processed_scenes: ${result.processed_scenes}\n`);
179
+ process.stdout.write(`scenes_changed: ${result.scenes_changed}\n`);
180
+ process.stdout.write(`character_reference_count: ${result.character_reference_count}\n`);
181
+
182
+ const preview = result.changes.slice(0, 20);
183
+ for (const row of preview) {
184
+ process.stdout.write(`- ${row.scene_id} (${row.project_id})\n`);
185
+ process.stdout.write(` added: ${row.added.join(", ") || "(none)"}\n`);
186
+ process.stdout.write(` removed: ${row.removed.join(", ") || "(none)"}\n`);
187
+ }
188
+ if (result.changes.length > preview.length) {
189
+ process.stdout.write(`... ${result.changes.length - preview.length} more changed scene(s)\n`);
190
+ }
191
+
192
+ if (result.mode === "write") {
193
+ process.stdout.write("next_step: run sync() to refresh DB indexes from updated sidecars\n");
194
+ }
195
+ }
196
+
197
+ function main() {
198
+ try {
199
+ const opts = parseArgs(process.argv.slice(2));
200
+ if (opts.help) {
201
+ process.stdout.write(`${usage()}\n`);
202
+ return;
203
+ }
204
+
205
+ const result = runNormalization({
206
+ syncDir: path.resolve(opts.syncDir),
207
+ projectId: opts.projectId,
208
+ write: opts.write,
209
+ limit: opts.limit,
210
+ });
211
+
212
+ if (opts.json) {
213
+ process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
214
+ return;
215
+ }
216
+ printTextSummary(result);
217
+ } catch (err) {
218
+ const message = err instanceof Error ? err.message : String(err);
219
+ process.stderr.write(`${message}\n`);
220
+ process.stderr.write(`${usage()}\n`);
221
+ process.exit(1);
222
+ }
223
+ }
224
+
225
+ main();