@literary-universe/sdk 0.1.0 → 0.3.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/README.md +166 -18
- package/dist/cjs/generated.d.ts +449 -1
- package/dist/cjs/generated.js +16 -1
- package/dist/cjs/index.d.ts +257 -2
- package/dist/cjs/index.js +97 -29
- package/dist/cjs/mcp.d.ts +20 -0
- package/dist/cjs/mcp.js +624 -0
- package/dist/cjs/project.d.ts +38 -5
- package/dist/cjs/project.js +258 -29
- package/dist/esm/generated.d.ts +449 -1
- package/dist/esm/generated.js +15 -0
- package/dist/esm/index.d.ts +257 -2
- package/dist/esm/index.js +98 -30
- package/dist/esm/mcp.d.ts +20 -0
- package/dist/esm/mcp.js +619 -0
- package/dist/esm/project.d.ts +38 -5
- package/dist/esm/project.js +257 -30
- package/package.json +25 -5
- package/schema.graphql +307 -1
package/dist/esm/project.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
-
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, } from 'node:fs';
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, renameSync, rmSync, statSync, writeFileSync, } from 'node:fs';
|
|
3
3
|
import { hostname } from 'node:os';
|
|
4
4
|
import { join, resolve } from 'node:path';
|
|
5
5
|
import { LiteraryUniverseApiError, } from './index.js';
|
|
@@ -8,6 +8,64 @@ const failure = (code, message) => {
|
|
|
8
8
|
};
|
|
9
9
|
const digest = (content) => createHash('sha256').update(JSON.stringify(content)).digest('hex');
|
|
10
10
|
const filename = (id, language) => `chapters/chapter-${id}.${language}.json`;
|
|
11
|
+
/**
|
|
12
|
+
* What the server's Markdown has no form for, so a `markdown` save is refused
|
|
13
|
+
* with MARKDOWN_UNSUPPORTED: node and mark types, plus the paragraph/heading
|
|
14
|
+
* attributes `textAlign` (other than left) and `initialLetter`.
|
|
15
|
+
*/
|
|
16
|
+
export const MARKDOWN_LOSSY_TYPES = [
|
|
17
|
+
'ruby',
|
|
18
|
+
'connection',
|
|
19
|
+
'textStyle',
|
|
20
|
+
'emoji',
|
|
21
|
+
'video',
|
|
22
|
+
'workCard',
|
|
23
|
+
'textAlign',
|
|
24
|
+
'initialLetter',
|
|
25
|
+
'playSceneHeading',
|
|
26
|
+
'playCharacter',
|
|
27
|
+
'playParenthetical',
|
|
28
|
+
'playDialogue',
|
|
29
|
+
'playLyric',
|
|
30
|
+
'playTransition',
|
|
31
|
+
'playSynopsis',
|
|
32
|
+
'playNote',
|
|
33
|
+
'playBoneyard',
|
|
34
|
+
'playDialogueBlock',
|
|
35
|
+
'playDualDialogue',
|
|
36
|
+
];
|
|
37
|
+
/** Lossy types found in a Tiptap document, in document order. */
|
|
38
|
+
function lossyTypes(node, found = new Set()) {
|
|
39
|
+
if (!node || typeof node !== 'object')
|
|
40
|
+
return found;
|
|
41
|
+
const { type, attrs } = node;
|
|
42
|
+
if (typeof type === 'string' && MARKDOWN_LOSSY_TYPES.includes(type))
|
|
43
|
+
found.add(type);
|
|
44
|
+
const block = type === 'paragraph' || type === 'heading';
|
|
45
|
+
if (block && attrs?.textAlign != null && attrs.textAlign !== 'left')
|
|
46
|
+
found.add('textAlign');
|
|
47
|
+
if (block && attrs?.initialLetter)
|
|
48
|
+
found.add('initialLetter');
|
|
49
|
+
for (const value of Object.values(node))
|
|
50
|
+
lossyTypes(value, found);
|
|
51
|
+
return found;
|
|
52
|
+
}
|
|
53
|
+
const FRONTMATTER = /^---\n(?:[^\n]*\n)*?---\n/;
|
|
54
|
+
const MARKDOWN_NOTICE = /^# (?:This chapter has no Markdown form yet;[^\n]*|Edit chapters\/[^\n]* instead: this chapter contains [^\n]*)\n/;
|
|
55
|
+
/** Why the Markdown file must not be edited, or undefined when it may be. */
|
|
56
|
+
function markdownNotice(draft) {
|
|
57
|
+
if (draft.content == null || draft.markdown == null)
|
|
58
|
+
return '# This chapter has no Markdown form yet; open it once in the web editor to convert it, then run lu refresh';
|
|
59
|
+
const lossy = [...lossyTypes(draft.content)];
|
|
60
|
+
if (lossy.length)
|
|
61
|
+
return `# Edit ${filename(draft.id, draft.language)} instead: this chapter contains ${lossy.join(', ')}`;
|
|
62
|
+
}
|
|
63
|
+
function frontmatter(draft) {
|
|
64
|
+
const notice = markdownNotice(draft);
|
|
65
|
+
const fields = ['id', 'title', 'language', 'version'].map((key) => `${key}: ${JSON.stringify(draft[key])}`);
|
|
66
|
+
const header = ['---', ...fields, `editable: ${!notice}`, '---', ''];
|
|
67
|
+
return `${header.join('\n')}${notice ? `${notice}\n` : ''}\n`;
|
|
68
|
+
}
|
|
11
69
|
const rootFor = (directory) => realpathSync(resolve(directory));
|
|
12
70
|
function pathFor(root, file) {
|
|
13
71
|
if (!/^(\.lu|chapters)\/[a-zA-Z0-9._-]+(?:\/[a-zA-Z0-9._-]+)?$/.test(file) ||
|
|
@@ -22,13 +80,10 @@ function pathFor(root, file) {
|
|
|
22
80
|
}
|
|
23
81
|
return path;
|
|
24
82
|
}
|
|
25
|
-
function
|
|
83
|
+
function writeText(root, file, text) {
|
|
26
84
|
const path = pathFor(root, file);
|
|
27
85
|
const temporary = `${path}.${randomUUID()}.tmp`;
|
|
28
|
-
writeFileSync(temporary,
|
|
29
|
-
mode: 0o600,
|
|
30
|
-
flag: 'wx',
|
|
31
|
-
});
|
|
86
|
+
writeFileSync(temporary, text, { mode: 0o600, flag: 'wx' });
|
|
32
87
|
try {
|
|
33
88
|
renameSync(temporary, path);
|
|
34
89
|
}
|
|
@@ -36,17 +91,28 @@ function writeJson(root, file, value) {
|
|
|
36
91
|
rmSync(temporary, { force: true });
|
|
37
92
|
}
|
|
38
93
|
}
|
|
39
|
-
|
|
94
|
+
const writeJson = (root, file, value) => writeText(root, file, `${JSON.stringify(value, null, 2)}\n`);
|
|
95
|
+
const writeMarkdown = (root, file, draft) => writeText(root, file, frontmatter(draft) + (draft.markdown ?? ''));
|
|
96
|
+
function readText(root, file) {
|
|
40
97
|
const path = pathFor(root, file);
|
|
41
98
|
if (statSync(path).size > 16 * 1024 * 1024)
|
|
42
99
|
failure('PAYLOAD_TOO_LARGE', 'Project file exceeds the size limit');
|
|
100
|
+
return readFileSync(path, 'utf8').replace(/^\uFEFF/, '');
|
|
101
|
+
}
|
|
102
|
+
function readJson(root, file) {
|
|
43
103
|
try {
|
|
44
|
-
return JSON.parse(
|
|
104
|
+
return JSON.parse(readText(root, file));
|
|
45
105
|
}
|
|
46
106
|
catch {
|
|
47
107
|
return failure('INVALID_PROJECT', 'Project file contains invalid JSON');
|
|
48
108
|
}
|
|
49
109
|
}
|
|
110
|
+
/** The Markdown body: the file without its frontmatter, with LF line ends. */
|
|
111
|
+
const readBody = (root, file) => readText(root, file)
|
|
112
|
+
.replaceAll('\r\n', '\n')
|
|
113
|
+
.replace(FRONTMATTER, '')
|
|
114
|
+
.replace(MARKDOWN_NOTICE, '')
|
|
115
|
+
.replace(/^\n/, '');
|
|
50
116
|
function clearDeadLock(root) {
|
|
51
117
|
const existing = readJson(root, '.lu/lock');
|
|
52
118
|
if (existing.host !== hostname() ||
|
|
@@ -81,15 +147,27 @@ export function readLocalProject(directory) {
|
|
|
81
147
|
if (project?.version !== 1 ||
|
|
82
148
|
typeof project.baseUrl !== 'string' ||
|
|
83
149
|
!/^[a-zA-Z0-9_-]{1,100}$/.test(project.storyId) ||
|
|
150
|
+
(project.language !== undefined &&
|
|
151
|
+
!/^[a-zA-Z0-9-]{1,32}$/.test(project.language)) ||
|
|
84
152
|
!Array.isArray(project.chapters))
|
|
85
153
|
failure('INVALID_PROJECT', 'Invalid project manifest');
|
|
86
154
|
const files = new Set();
|
|
87
155
|
for (const chapter of project.chapters) {
|
|
88
156
|
validateChapter(chapter, files);
|
|
89
157
|
files.add(chapter.file.toLowerCase());
|
|
158
|
+
if (chapter.markdown)
|
|
159
|
+
validateMarkdown(chapter.markdown, files);
|
|
90
160
|
}
|
|
91
161
|
return project;
|
|
92
162
|
}
|
|
163
|
+
function validateMarkdown(markdown, files) {
|
|
164
|
+
if (typeof markdown.file !== 'string' ||
|
|
165
|
+
!/^chapters\/[a-zA-Z0-9._-]+\.md$/.test(markdown.file) ||
|
|
166
|
+
typeof markdown.hash !== 'string' ||
|
|
167
|
+
files.has(markdown.file.toLowerCase()))
|
|
168
|
+
failure('INVALID_PROJECT', 'Invalid chapter manifest');
|
|
169
|
+
files.add(markdown.file.toLowerCase());
|
|
170
|
+
}
|
|
93
171
|
function validateChapter(chapter, files) {
|
|
94
172
|
if (!chapter ||
|
|
95
173
|
typeof chapter.id !== 'string' ||
|
|
@@ -102,16 +180,29 @@ function validateChapter(chapter, files) {
|
|
|
102
180
|
files.has(chapter.file.toLowerCase()))
|
|
103
181
|
failure('INVALID_PROJECT', 'Invalid chapter manifest');
|
|
104
182
|
}
|
|
183
|
+
/** Which local file differs from the manifest; `both` cannot be pushed. */
|
|
184
|
+
function inspect(root, chapter) {
|
|
185
|
+
const content = readJson(root, chapter.file);
|
|
186
|
+
const body = chapter.markdown ? readBody(root, chapter.markdown.file) : '';
|
|
187
|
+
const json = digest(content) !== chapter.hash;
|
|
188
|
+
const markdown = chapter.markdown !== undefined && digest(body) !== chapter.markdown.hash;
|
|
189
|
+
const source = json && markdown ? 'both' : json ? 'json' : markdown ? 'markdown' : 'none';
|
|
190
|
+
return { content, body, source };
|
|
191
|
+
}
|
|
105
192
|
export function localProjectStatus(directory) {
|
|
106
193
|
const root = rootFor(directory);
|
|
107
|
-
return readLocalProject(root).chapters.map((chapter) =>
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
194
|
+
return readLocalProject(root).chapters.map((chapter) => {
|
|
195
|
+
const { source } = inspect(root, chapter);
|
|
196
|
+
return {
|
|
197
|
+
id: chapter.id,
|
|
198
|
+
language: chapter.language,
|
|
199
|
+
file: chapter.file,
|
|
200
|
+
version: chapter.version,
|
|
201
|
+
changed: source !== 'none',
|
|
202
|
+
source,
|
|
203
|
+
pending: Boolean(chapter.pending),
|
|
204
|
+
};
|
|
205
|
+
});
|
|
115
206
|
}
|
|
116
207
|
function initializeProject(directory) {
|
|
117
208
|
mkdirSync(directory, { recursive: true });
|
|
@@ -134,6 +225,8 @@ async function pullPage(client, root, project, input, after) {
|
|
|
134
225
|
language: input.language,
|
|
135
226
|
});
|
|
136
227
|
storePulledDraft(root, project, draft);
|
|
228
|
+
// A refresh stopped midway keeps a manifest that matches the files written so far.
|
|
229
|
+
writeJson(root, '.lu/project.json', project);
|
|
137
230
|
}
|
|
138
231
|
return page.pageInfo;
|
|
139
232
|
}
|
|
@@ -141,10 +234,27 @@ function assertDistinctFile(project, file) {
|
|
|
141
234
|
if (project.chapters.some((chapter) => chapter.file.toLowerCase() === file.toLowerCase()))
|
|
142
235
|
failure('INVALID_PROJECT', 'Chapter filenames differ only by case; use individual draft files');
|
|
143
236
|
}
|
|
237
|
+
/** `chapters/<seq>-<slug>.<lang>.md`, numbered on a collision. */
|
|
238
|
+
function markdownName(project, draft) {
|
|
239
|
+
const slug = draft.title
|
|
240
|
+
.toLowerCase()
|
|
241
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
242
|
+
.slice(0, 60)
|
|
243
|
+
.replace(/^-+|-+$/g, '') || 'chapter';
|
|
244
|
+
const base = `chapters/${String(draft.sequence).padStart(3, '0')}-${slug}`;
|
|
245
|
+
const taken = new Set(project.chapters.map((chapter) => chapter.markdown?.file.toLowerCase()));
|
|
246
|
+
for (let n = 1;; n++) {
|
|
247
|
+
const file = `${base}${n > 1 ? `-${n}` : ''}.${draft.language}.md`;
|
|
248
|
+
if (!taken.has(file.toLowerCase()))
|
|
249
|
+
return file;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
144
252
|
function storePulledDraft(root, project, draft) {
|
|
145
253
|
const file = filename(draft.id, draft.language);
|
|
146
254
|
assertDistinctFile(project, file);
|
|
255
|
+
const markdown = markdownName(project, draft);
|
|
147
256
|
writeJson(root, file, draft.content);
|
|
257
|
+
writeMarkdown(root, markdown, draft);
|
|
148
258
|
writeJson(root, `.lu/${file.split('/')[1]}`, draft);
|
|
149
259
|
project.chapters.push({
|
|
150
260
|
id: draft.id,
|
|
@@ -152,6 +262,7 @@ function storePulledDraft(root, project, draft) {
|
|
|
152
262
|
file,
|
|
153
263
|
version: draft.version,
|
|
154
264
|
hash: digest(draft.content),
|
|
265
|
+
markdown: { file: markdown, hash: digest(draft.markdown ?? '') },
|
|
155
266
|
});
|
|
156
267
|
}
|
|
157
268
|
async function pullPages(client, root, project, language) {
|
|
@@ -171,6 +282,8 @@ async function pullPages(client, root, project, language) {
|
|
|
171
282
|
}
|
|
172
283
|
/** Pulls into a new directory, preserving existing local files. */
|
|
173
284
|
export async function pullLocalProject(client, input) {
|
|
285
|
+
if (input.refresh)
|
|
286
|
+
return refreshLocalProject(client, input);
|
|
174
287
|
const root = initializeProject(input.directory);
|
|
175
288
|
const release = lock(root);
|
|
176
289
|
try {
|
|
@@ -178,6 +291,7 @@ export async function pullLocalProject(client, input) {
|
|
|
178
291
|
version: 1,
|
|
179
292
|
baseUrl: input.baseUrl,
|
|
180
293
|
storyId: input.storyId,
|
|
294
|
+
...(input.language ? { language: input.language } : {}),
|
|
181
295
|
chapters: [],
|
|
182
296
|
};
|
|
183
297
|
await pullPages(client, root, project, input.language);
|
|
@@ -194,6 +308,84 @@ export async function pullLocalProject(client, input) {
|
|
|
194
308
|
release();
|
|
195
309
|
}
|
|
196
310
|
}
|
|
311
|
+
/** Deletes the files of chapters the server no longer has and reports the difference. */
|
|
312
|
+
function reconcile(root, previous, project) {
|
|
313
|
+
const current = new Set(project.chapters.map((chapter) => chapter.file));
|
|
314
|
+
const kept = new Set(project.chapters.flatMap((chapter) => [
|
|
315
|
+
chapter.file,
|
|
316
|
+
chapter.markdown?.file,
|
|
317
|
+
]));
|
|
318
|
+
for (const chapter of previous) {
|
|
319
|
+
for (const file of [chapter.file, chapter.markdown?.file])
|
|
320
|
+
if (file && !kept.has(file))
|
|
321
|
+
rmSync(pathFor(root, file), { force: true });
|
|
322
|
+
if (!current.has(chapter.file))
|
|
323
|
+
rmSync(pathFor(root, `.lu/${chapter.file.split('/')[1]}`), {
|
|
324
|
+
force: true,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
return difference(previous, project.chapters);
|
|
328
|
+
}
|
|
329
|
+
const sameState = (before, after) => before.version === after.version &&
|
|
330
|
+
before.hash === after.hash &&
|
|
331
|
+
before.markdown?.hash === after.markdown?.hash;
|
|
332
|
+
function difference(previous, chapters) {
|
|
333
|
+
const was = new Map(previous.map((chapter) => [chapter.file, chapter]));
|
|
334
|
+
const current = new Set(chapters.map((chapter) => chapter.file));
|
|
335
|
+
const ids = (list) => [...new Set(list.map((c) => c.id))];
|
|
336
|
+
return {
|
|
337
|
+
added: ids(chapters.filter((chapter) => !was.has(chapter.file))),
|
|
338
|
+
updated: ids(chapters.filter((chapter) => {
|
|
339
|
+
const before = was.get(chapter.file);
|
|
340
|
+
return before && !sameState(before, chapter);
|
|
341
|
+
})),
|
|
342
|
+
removed: ids(previous.filter((chapter) => !current.has(chapter.file))),
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
/** Re-pulls every chapter of an existing project; refuses while anything local is unpushed. */
|
|
346
|
+
export async function refreshLocalProject(client, input) {
|
|
347
|
+
const root = rootFor(input.directory);
|
|
348
|
+
const release = lock(root);
|
|
349
|
+
try {
|
|
350
|
+
const project = openProject(root, input.baseUrl);
|
|
351
|
+
if (project.chapters.some((chapter) => chapter.pending || inspect(root, chapter).source !== 'none'))
|
|
352
|
+
failure('LOCAL_CHANGES', 'Push or revert local changes before refreshing');
|
|
353
|
+
const previous = project.chapters;
|
|
354
|
+
const staging = mkdtempSync(join(root, '.lu-refresh-'));
|
|
355
|
+
try {
|
|
356
|
+
initializeProject(staging);
|
|
357
|
+
project.chapters = [];
|
|
358
|
+
await pullPages(client, staging, project, project.language);
|
|
359
|
+
// Editors do not share our lock. Check again after the network reads.
|
|
360
|
+
if (previous.some((chapter) => inspect(root, chapter).source !== 'none'))
|
|
361
|
+
failure('LOCAL_CHANGES', 'Local changes appeared while refreshing');
|
|
362
|
+
const managed = new Set(previous.flatMap((chapter) => [
|
|
363
|
+
chapter.file,
|
|
364
|
+
chapter.markdown?.file,
|
|
365
|
+
`.lu/${chapter.file.split('/')[1]}`,
|
|
366
|
+
]));
|
|
367
|
+
const files = project.chapters.flatMap((chapter) => [
|
|
368
|
+
chapter.file,
|
|
369
|
+
...(chapter.markdown ? [chapter.markdown.file] : []),
|
|
370
|
+
`.lu/${chapter.file.split('/')[1]}`,
|
|
371
|
+
]);
|
|
372
|
+
for (const file of files) {
|
|
373
|
+
if (existsSync(pathFor(root, file)) && !managed.has(file))
|
|
374
|
+
failure('LOCAL_CHANGES', 'Refresh would overwrite an unmanaged file');
|
|
375
|
+
}
|
|
376
|
+
for (const file of files)
|
|
377
|
+
writeText(root, file, readText(staging, file));
|
|
378
|
+
writeJson(root, '.lu/project.json', project);
|
|
379
|
+
return reconcile(root, previous, project);
|
|
380
|
+
}
|
|
381
|
+
finally {
|
|
382
|
+
rmSync(staging, { recursive: true, force: true });
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
finally {
|
|
386
|
+
release();
|
|
387
|
+
}
|
|
388
|
+
}
|
|
197
389
|
/** Saves each changed draft once; pending intents survive a lost response or stopped process. */
|
|
198
390
|
export async function pushLocalProject(client, input) {
|
|
199
391
|
const root = rootFor(input.directory);
|
|
@@ -205,9 +397,18 @@ export async function pushLocalProject(client, input) {
|
|
|
205
397
|
release();
|
|
206
398
|
}
|
|
207
399
|
}
|
|
400
|
+
/** Rewrites both files from the server's draft unless they changed again meanwhile. */
|
|
208
401
|
function savedLocally(root, project, chapter, result) {
|
|
209
|
-
|
|
402
|
+
const { pending, markdown } = chapter;
|
|
403
|
+
const sentMarkdown = pending?.markdown !== undefined;
|
|
404
|
+
const jsonBefore = sentMarkdown ? chapter.hash : pending?.hash;
|
|
405
|
+
if (digest(readJson(root, chapter.file)) === jsonBefore)
|
|
210
406
|
writeJson(root, chapter.file, result.draft.content);
|
|
407
|
+
const file = markdown?.file ?? markdownName(project, result.draft);
|
|
408
|
+
const bodyBefore = sentMarkdown ? pending?.hash : markdown?.hash;
|
|
409
|
+
if (!markdown || digest(readBody(root, file)) === bodyBefore)
|
|
410
|
+
writeMarkdown(root, file, result.draft);
|
|
411
|
+
chapter.markdown = { file, hash: digest(result.draft.markdown ?? '') };
|
|
211
412
|
chapter.hash = digest(result.draft.content);
|
|
212
413
|
chapter.version = result.version;
|
|
213
414
|
delete chapter.pending;
|
|
@@ -259,33 +460,59 @@ const saveInput = ({ chapter, pending }) => ({
|
|
|
259
460
|
language: chapter.language,
|
|
260
461
|
expectedVersion: pending.expectedVersion,
|
|
261
462
|
operationId: pending.operationId,
|
|
262
|
-
|
|
463
|
+
...(pending.markdown === undefined
|
|
464
|
+
? { content: pending.content }
|
|
465
|
+
: { markdown: pending.markdown }),
|
|
263
466
|
});
|
|
467
|
+
/** The intent to send: an earlier pending one, else the changed file's; or why nothing can be sent. */
|
|
264
468
|
function snapshot(root, chapter) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
469
|
+
if (chapter.pending)
|
|
470
|
+
return { chapter, pending: chapter.pending };
|
|
471
|
+
const { content, body, source } = inspect(root, chapter);
|
|
472
|
+
if (source === 'none')
|
|
473
|
+
return source;
|
|
474
|
+
if (source === 'both')
|
|
475
|
+
return 'AMBIGUOUS_CHANGE';
|
|
476
|
+
// The stored server draft shows a missing Markdown form, which the JSON file alone cannot.
|
|
477
|
+
const stored = readJson(root, `.lu/${chapter.file.split('/')[1]}`);
|
|
478
|
+
if (source === 'markdown' && markdownNotice(stored))
|
|
479
|
+
return 'MARKDOWN_UNSUPPORTED';
|
|
480
|
+
const sent = source === 'markdown' ? { markdown: body } : { content };
|
|
481
|
+
const hash = digest(source === 'markdown' ? body : content);
|
|
482
|
+
const operationId = randomUUID();
|
|
483
|
+
const pending = {
|
|
484
|
+
operationId,
|
|
268
485
|
expectedVersion: chapter.version,
|
|
269
|
-
hash
|
|
270
|
-
|
|
486
|
+
hash,
|
|
487
|
+
...sent,
|
|
271
488
|
};
|
|
272
489
|
return { chapter, pending };
|
|
273
490
|
}
|
|
274
|
-
|
|
491
|
+
function openProject(root, baseUrl) {
|
|
275
492
|
const project = readLocalProject(root);
|
|
276
493
|
if (new URL(project.baseUrl).href !== new URL(baseUrl).href)
|
|
277
494
|
failure('DEPLOYMENT_MISMATCH', 'The selected deployment does not match this project');
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
495
|
+
return project;
|
|
496
|
+
}
|
|
497
|
+
async function preflight(client, root, baseUrl) {
|
|
498
|
+
const project = openProject(root, baseUrl);
|
|
499
|
+
const changes = [];
|
|
500
|
+
const unsendable = [];
|
|
501
|
+
for (const chapter of project.chapters) {
|
|
502
|
+
const change = snapshot(root, chapter);
|
|
503
|
+
if (typeof change === 'object')
|
|
504
|
+
changes.push(change);
|
|
505
|
+
else if (change !== 'none')
|
|
506
|
+
unsendable.push({ id: chapter.id, status: 'failed', code: change });
|
|
507
|
+
}
|
|
281
508
|
for (const change of changes.filter(({ chapter }) => !chapter.pending))
|
|
282
509
|
await client.validateDraft(saveInput(change));
|
|
283
|
-
return { project, changes };
|
|
510
|
+
return { project, changes, unsendable };
|
|
284
511
|
}
|
|
285
512
|
async function pushLocked(client, root, baseUrl) {
|
|
286
|
-
const { project, changes } = await preflight(client, root, baseUrl);
|
|
513
|
+
const { project, changes, unsendable } = await preflight(client, root, baseUrl);
|
|
287
514
|
// Chapters are independent: a failure keeps its pending intent and the rest still push.
|
|
288
|
-
const results = [];
|
|
515
|
+
const results = [...unsendable];
|
|
289
516
|
for (const change of changes)
|
|
290
517
|
results.push(await pushChapter(client, root, project, change));
|
|
291
518
|
return results;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@literary-universe/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Literary Universe authoring API client for Node and Bun",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
7
|
-
"node": ">=
|
|
7
|
+
"node": ">=26"
|
|
8
8
|
},
|
|
9
9
|
"main": "./dist/cjs/index.js",
|
|
10
10
|
"types": "./dist/cjs/index.d.ts",
|
|
@@ -38,6 +38,16 @@
|
|
|
38
38
|
"types": "./dist/cjs/images.d.ts",
|
|
39
39
|
"default": "./dist/cjs/images.js"
|
|
40
40
|
}
|
|
41
|
+
},
|
|
42
|
+
"./mcp": {
|
|
43
|
+
"import": {
|
|
44
|
+
"types": "./dist/esm/mcp.d.ts",
|
|
45
|
+
"default": "./dist/esm/mcp.js"
|
|
46
|
+
},
|
|
47
|
+
"require": {
|
|
48
|
+
"types": "./dist/cjs/mcp.d.ts",
|
|
49
|
+
"default": "./dist/cjs/mcp.js"
|
|
50
|
+
}
|
|
41
51
|
}
|
|
42
52
|
},
|
|
43
53
|
"files": [
|
|
@@ -58,21 +68,31 @@
|
|
|
58
68
|
},
|
|
59
69
|
"devDependencies": {
|
|
60
70
|
"@graphql-codegen/typescript-operations": "6.2.0",
|
|
71
|
+
"@modelcontextprotocol/sdk": "1.32.2",
|
|
72
|
+
"@types/node": "26.6.6",
|
|
61
73
|
"graphql": "16.14.2",
|
|
74
|
+
"sharp": "0.35.5",
|
|
62
75
|
"typescript": "5.9.3",
|
|
63
|
-
"
|
|
64
|
-
"sharp": "0.35.5"
|
|
76
|
+
"zod": "4.6.7"
|
|
65
77
|
},
|
|
66
78
|
"overrides": {
|
|
67
79
|
"@graphql-tools/utils": "12.1.0"
|
|
68
80
|
},
|
|
69
81
|
"license": "MIT",
|
|
70
82
|
"peerDependencies": {
|
|
71
|
-
"sharp": "0.35.5"
|
|
83
|
+
"sharp": "0.35.5",
|
|
84
|
+
"@modelcontextprotocol/sdk": "^1.32",
|
|
85
|
+
"zod": "^4"
|
|
72
86
|
},
|
|
73
87
|
"peerDependenciesMeta": {
|
|
74
88
|
"sharp": {
|
|
75
89
|
"optional": true
|
|
90
|
+
},
|
|
91
|
+
"@modelcontextprotocol/sdk": {
|
|
92
|
+
"optional": true
|
|
93
|
+
},
|
|
94
|
+
"zod": {
|
|
95
|
+
"optional": true
|
|
76
96
|
}
|
|
77
97
|
},
|
|
78
98
|
"repository": {
|