@hanna84/mcp-writing 2.1.0 → 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,21 @@ 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
+
7
13
  #### [v2.1.0](https://github.com/hannasdev/mcp-writing.git
8
14
  /compare/v2.0.4...v2.1.0)
9
15
 
16
+ > 25 April 2026
17
+
10
18
  - feat(tools): add scene character normalization utility [`#83`](https://github.com/hannasdev/mcp-writing.git
11
19
  /pull/83)
20
+ - Release 2.1.0 [`33b5b31`](https://github.com/hannasdev/mcp-writing.git
21
+ /commit/33b5b31846576ef18385e6678f74cc109995fcac)
12
22
 
13
23
  #### [v2.0.4](https://github.com/hannasdev/mcp-writing.git
14
24
  /compare/v2.0.3...v2.0.4)
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.1.0",
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",
@@ -17,6 +17,7 @@
17
17
  "metadata-lint.js",
18
18
  "scene-character-normalization.js",
19
19
  "review-bundles.js",
20
+ "prose-styleguide.js",
20
21
  "scripts/",
21
22
  "README.md",
22
23
  "CHANGELOG.md"
@@ -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
+ }