@hanna84/mcp-writing 2.0.0 → 2.0.1

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.0.0](https://github.com/hannasdev/mcp-writing.git
7
+ #### [v2.0.1](https://github.com/hannasdev/mcp-writing.git
8
+ /compare/v2.0.0...v2.0.1)
9
+
10
+ - fix(package): include scene-character-batch in published files [`#79`](https://github.com/hannasdev/mcp-writing.git
11
+ /pull/79)
12
+
13
+ ### [v2.0.0](https://github.com/hannasdev/mcp-writing.git
8
14
  /compare/v1.17.0...v2.0.0)
9
15
 
16
+ > 25 April 2026
17
+
10
18
  - feat(review-bundles)!: add PDF export via pdfkit [`#78`](https://github.com/hannasdev/mcp-writing.git
11
19
  /pull/78)
20
+ - Release 2.0.0 [`ddccb28`](https://github.com/hannasdev/mcp-writing.git
21
+ /commit/ddccb2869c6787b0cb64897d067886d59f7cb6e6)
12
22
 
13
23
  #### [v1.17.0](https://github.com/hannasdev/mcp-writing.git
14
24
  /compare/v1.16.2...v1.17.0)
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@hanna84/mcp-writing",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
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",
7
7
  "files": [
8
8
  "index.js",
9
9
  "async-progress.js",
10
+ "scene-character-batch.js",
10
11
  "scrivener-direct.js",
11
12
  "importer.js",
12
13
  "db.js",
@@ -0,0 +1,221 @@
1
+ import fs from "node:fs";
2
+ import matter from "gray-matter";
3
+ import { normalizeSceneMetaForPath, readMeta, writeMeta } from "./sync.js";
4
+
5
+ function escapeRegex(text) {
6
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
7
+ }
8
+
9
+ function normalizeCharacterRows(rows) {
10
+ const clean = rows
11
+ .filter(row => row?.character_id && row?.name)
12
+ .map(row => ({
13
+ character_id: row.character_id,
14
+ name: String(row.name).trim(),
15
+ tokens: [...new Set(String(row.name).toLowerCase().split(/\s+/).filter(Boolean))],
16
+ }))
17
+ .filter(row => row.name.length > 0);
18
+
19
+ const tokenMap = new Map();
20
+ for (const row of clean) {
21
+ for (const token of row.tokens) {
22
+ if (!token || token.length < 3) continue;
23
+ const ids = tokenMap.get(token) ?? [];
24
+ ids.push(row.character_id);
25
+ tokenMap.set(token, ids);
26
+ }
27
+ }
28
+
29
+ return { clean, tokenMap };
30
+ }
31
+
32
+ function inferCharactersFromProse(prose, characterRows) {
33
+ const { clean, tokenMap } = characterRows;
34
+ const inferred = new Set();
35
+ const ambiguous_tokens = [];
36
+
37
+ for (const row of clean) {
38
+ if (row.tokens.length > 1) {
39
+ const pattern = row.tokens.map(escapeRegex).join("\\s+");
40
+ const regex = new RegExp(`\\b${pattern}\\b`, "i");
41
+ if (regex.test(prose)) {
42
+ inferred.add(row.character_id);
43
+ continue;
44
+ }
45
+ }
46
+
47
+ for (const token of row.tokens) {
48
+ if (!token || token.length < 3) continue;
49
+ const tokenRegex = new RegExp(`\\b${escapeRegex(token)}\\b`, "i");
50
+ if (!tokenRegex.test(prose)) continue;
51
+
52
+ const tokenIds = tokenMap.get(token) ?? [];
53
+ if (tokenIds.length === 1) {
54
+ inferred.add(row.character_id);
55
+ } else if (!ambiguous_tokens.includes(token)) {
56
+ ambiguous_tokens.push(token);
57
+ }
58
+ }
59
+ }
60
+
61
+ return {
62
+ inferred_characters: [...inferred],
63
+ ambiguous_tokens,
64
+ };
65
+ }
66
+
67
+ function nextTurn() {
68
+ return new Promise(resolve => setImmediate(resolve));
69
+ }
70
+
71
+ function getInterSceneDelayMs() {
72
+ const raw = Number(process.env.MCP_WRITING_SCENE_CHARACTER_BATCH_DELAY_MS ?? 0);
73
+ return Number.isFinite(raw) && raw > 0 ? raw : 0;
74
+ }
75
+
76
+ function delay(ms) {
77
+ return new Promise(resolve => setTimeout(resolve, ms));
78
+ }
79
+
80
+ export async function runSceneCharacterBatch({ syncDir, args, onProgress, shouldCancel }) {
81
+ const {
82
+ project_id,
83
+ dry_run = true,
84
+ replace_mode = "merge",
85
+ include_match_details = false,
86
+ project_exists = true,
87
+ target_scenes = [],
88
+ character_rows = [],
89
+ } = args;
90
+
91
+ const targetScenes = Array.isArray(target_scenes) ? target_scenes : [];
92
+ const characterRows = Array.isArray(character_rows) ? character_rows : [];
93
+ const normalizedCharacterRows = normalizeCharacterRows(characterRows);
94
+
95
+ const results = [];
96
+ let processed_scenes = 0;
97
+ let scenes_changed = 0;
98
+ let failed_scenes = 0;
99
+ let links_added = 0;
100
+ let links_removed = 0;
101
+ const interSceneDelayMs = getInterSceneDelayMs();
102
+
103
+ const emitProgress = () => {
104
+ if (typeof onProgress !== "function") return;
105
+ onProgress({
106
+ total_scenes: targetScenes.length,
107
+ processed_scenes,
108
+ scenes_changed,
109
+ failed_scenes,
110
+ });
111
+ };
112
+
113
+ emitProgress();
114
+
115
+ for (const scene of targetScenes) {
116
+ await nextTurn();
117
+
118
+ if (typeof shouldCancel === "function" && shouldCancel()) {
119
+ break;
120
+ }
121
+
122
+ try {
123
+ const raw = fs.readFileSync(scene.file_path, "utf8");
124
+ const { content: prose } = matter(raw);
125
+ const { meta } = readMeta(scene.file_path, syncDir, { writable: !dry_run });
126
+
127
+ const before_characters = [...new Set((meta.characters ?? []).map(String).filter(Boolean))];
128
+ const inference = inferCharactersFromProse(prose, normalizedCharacterRows);
129
+ const inferred_characters = inference.inferred_characters;
130
+
131
+ const afterSet = new Set(before_characters);
132
+ if (replace_mode === "replace") {
133
+ afterSet.clear();
134
+ }
135
+ for (const characterId of inferred_characters) {
136
+ afterSet.add(characterId);
137
+ }
138
+
139
+ const after_characters = [...afterSet];
140
+ const beforeSet = new Set(before_characters);
141
+ const added = after_characters.filter(id => !beforeSet.has(id));
142
+ const afterSetLookup = new Set(after_characters);
143
+ const removed = before_characters.filter(id => !afterSetLookup.has(id));
144
+ const changed = added.length > 0 || removed.length > 0;
145
+
146
+ if (!dry_run && changed) {
147
+ const updatedMeta = normalizeSceneMetaForPath(syncDir, scene.file_path, {
148
+ ...meta,
149
+ characters: after_characters,
150
+ }).meta;
151
+
152
+ writeMeta(scene.file_path, updatedMeta);
153
+ }
154
+
155
+ scenes_changed += changed ? 1 : 0;
156
+ links_added += added.length;
157
+ links_removed += removed.length;
158
+
159
+ const hasInferredMatches = inferred_characters.length > 0;
160
+ const sceneStatus = changed
161
+ ? "changed"
162
+ : (!hasInferredMatches && inference.ambiguous_tokens.length > 0 ? "skipped_ambiguous" : "unchanged");
163
+
164
+ results.push({
165
+ scene_id: scene.scene_id,
166
+ file_path: scene.file_path,
167
+ before_characters,
168
+ inferred_characters,
169
+ after_characters,
170
+ added,
171
+ removed,
172
+ changed,
173
+ status: sceneStatus,
174
+ ...(include_match_details ? { match_details: { ambiguous_tokens: inference.ambiguous_tokens } } : {}),
175
+ });
176
+ } catch (error) {
177
+ failed_scenes += 1;
178
+ results.push({
179
+ scene_id: scene.scene_id,
180
+ file_path: scene.file_path,
181
+ before_characters: [],
182
+ inferred_characters: [],
183
+ after_characters: [],
184
+ added: [],
185
+ removed: [],
186
+ changed: false,
187
+ status: "failed",
188
+ error: error instanceof Error ? error.message : String(error),
189
+ });
190
+ } finally {
191
+ processed_scenes += 1;
192
+ emitProgress();
193
+ if (interSceneDelayMs > 0) {
194
+ await delay(interSceneDelayMs);
195
+ }
196
+ }
197
+ }
198
+
199
+ const warnings = [];
200
+ if (failed_scenes > 0) {
201
+ warnings.push("PARTIAL_SUCCESS: one or more scenes failed to process.");
202
+ }
203
+ if (!project_exists && targetScenes.length === 0) {
204
+ warnings.push(`PROJECT_NOT_FOUND_WARNING: project '${project_id}' was not found; nothing to process.`);
205
+ }
206
+
207
+ return {
208
+ ok: true,
209
+ cancelled: Boolean(typeof shouldCancel === "function" && shouldCancel() && processed_scenes < targetScenes.length),
210
+ project_id,
211
+ dry_run: Boolean(dry_run),
212
+ total_scenes: targetScenes.length,
213
+ processed_scenes,
214
+ scenes_changed,
215
+ failed_scenes,
216
+ links_added,
217
+ links_removed,
218
+ results,
219
+ ...(warnings.length > 0 ? { warning: warnings.join(" ") } : {}),
220
+ };
221
+ }