@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/cjs/mcp.js
ADDED
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_TOOL_SELECTION = exports.TOOL_CATEGORIES = void 0;
|
|
4
|
+
exports.selectTools = selectTools;
|
|
5
|
+
exports.registerLiteraryUniverseTools = registerLiteraryUniverseTools;
|
|
6
|
+
const node_crypto_1 = require("node:crypto");
|
|
7
|
+
const node_path_1 = require("node:path");
|
|
8
|
+
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
9
|
+
const zod_1 = require("zod");
|
|
10
|
+
const index_js_1 = require("./index.js");
|
|
11
|
+
const NOT_SIGNED_IN = 'Not signed in to Literary Universe. Run `lu login` (or set LU_TOKEN) and restart the agent.';
|
|
12
|
+
const DOCS = 'https://literaryuniverse.com';
|
|
13
|
+
const tool = (spec) => spec;
|
|
14
|
+
const hints = (readOnlyHint, destructiveHint, idempotentHint, openWorldHint = false) => ({
|
|
15
|
+
readOnlyHint,
|
|
16
|
+
destructiveHint,
|
|
17
|
+
idempotentHint,
|
|
18
|
+
openWorldHint,
|
|
19
|
+
});
|
|
20
|
+
const READ = hints(true, false, true);
|
|
21
|
+
const WRITE = hints(false, false, true);
|
|
22
|
+
const CREATE = hints(false, false, false);
|
|
23
|
+
const DESTROY = hints(false, true, false);
|
|
24
|
+
const WEB = hints(true, false, true, true);
|
|
25
|
+
const id = zod_1.z.string().min(1);
|
|
26
|
+
const page = {
|
|
27
|
+
first: zod_1.z.number().int().min(1).max(100).optional().describe('Page size'),
|
|
28
|
+
after: zod_1.z.string().optional().describe('endCursor of the previous page'),
|
|
29
|
+
};
|
|
30
|
+
const language = zod_1.z
|
|
31
|
+
.string()
|
|
32
|
+
.optional()
|
|
33
|
+
.describe('Locale code such as en; the original language when omitted');
|
|
34
|
+
const json = zod_1.z
|
|
35
|
+
.record(zod_1.z.string(), zod_1.z.unknown())
|
|
36
|
+
.describe('Tiptap JSON document, as get-chapter-draft returns with format json');
|
|
37
|
+
const operationId = zod_1.z
|
|
38
|
+
.string()
|
|
39
|
+
.optional()
|
|
40
|
+
.describe('Idempotency key; keep it for a retry, omit for a fresh one');
|
|
41
|
+
const textField = { title: zod_1.z.string(), text: zod_1.z.string() };
|
|
42
|
+
const timeField = {
|
|
43
|
+
title: zod_1.z.string(),
|
|
44
|
+
value: zod_1.z.string().describe('A date like 1402-03-01, or HH:MM'),
|
|
45
|
+
};
|
|
46
|
+
function draftView(draft, format) {
|
|
47
|
+
const { content, html, text, oldHtml, markdown, ...meta } = draft;
|
|
48
|
+
return format === 'json' ? { ...meta, content } : { ...meta, markdown };
|
|
49
|
+
}
|
|
50
|
+
const draftText = (result) => `Chapter ${result.sequence} "${result.title}" (${result.id}, ${result.language}, version ${result.version})\n\n${result.markdown ?? ''}`;
|
|
51
|
+
const stories = {
|
|
52
|
+
'list-stories': tool({
|
|
53
|
+
title: 'List stories',
|
|
54
|
+
description: 'A page of the stories this account can write.',
|
|
55
|
+
input: page,
|
|
56
|
+
annotations: READ,
|
|
57
|
+
run: (client, args) => client.stories(args),
|
|
58
|
+
}),
|
|
59
|
+
'get-story': tool({
|
|
60
|
+
title: 'Get story',
|
|
61
|
+
description: 'A story with its description and notes.',
|
|
62
|
+
input: { id },
|
|
63
|
+
annotations: READ,
|
|
64
|
+
run: (client, args) => client.story(args),
|
|
65
|
+
}),
|
|
66
|
+
'list-chapters': tool({
|
|
67
|
+
title: 'List chapters',
|
|
68
|
+
description: 'A page of chapters of a story in reading order, drafts included.',
|
|
69
|
+
input: { storyId: id, language, ...page },
|
|
70
|
+
annotations: READ,
|
|
71
|
+
run: (client, args) => client.chapters(args),
|
|
72
|
+
}),
|
|
73
|
+
'get-chapter-draft': tool({
|
|
74
|
+
title: 'Get chapter draft',
|
|
75
|
+
description: 'The current draft of a chapter as Markdown (default) or Tiptap JSON, with the version a save must name.',
|
|
76
|
+
input: {
|
|
77
|
+
id,
|
|
78
|
+
language,
|
|
79
|
+
format: zod_1.z.enum(['markdown', 'json']).default('markdown'),
|
|
80
|
+
},
|
|
81
|
+
annotations: READ,
|
|
82
|
+
run: async (client, { format, ...args }) => draftView(await client.draft(args), format),
|
|
83
|
+
text: (result) => 'markdown' in result
|
|
84
|
+
? draftText(result)
|
|
85
|
+
: JSON.stringify(result, null, 2),
|
|
86
|
+
}),
|
|
87
|
+
};
|
|
88
|
+
const bodyShape = {
|
|
89
|
+
markdown: zod_1.z
|
|
90
|
+
.string()
|
|
91
|
+
.optional()
|
|
92
|
+
.describe('The whole chapter body as Markdown'),
|
|
93
|
+
content: json.optional(),
|
|
94
|
+
operationId,
|
|
95
|
+
};
|
|
96
|
+
const saveShape = { language, ...bodyShape };
|
|
97
|
+
const storiesWrite = {
|
|
98
|
+
'save-chapter-draft': tool({
|
|
99
|
+
title: 'Save chapter draft',
|
|
100
|
+
description: 'Replace a chapter draft with markdown or content (exactly one). expectedVersion must be the version last read; a changed draft fails with VERSION_CONFLICT.',
|
|
101
|
+
input: { chapterId: id, expectedVersion: zod_1.z.string(), ...saveShape },
|
|
102
|
+
annotations: WRITE,
|
|
103
|
+
run: async (client, { chapterId, content, ...args }) => {
|
|
104
|
+
const result = await client.saveDraft({
|
|
105
|
+
...args,
|
|
106
|
+
id: chapterId,
|
|
107
|
+
operationId: args.operationId ?? (0, node_crypto_1.randomUUID)(),
|
|
108
|
+
content: content,
|
|
109
|
+
});
|
|
110
|
+
const { version, recoveryId, warnings } = result;
|
|
111
|
+
return { version, recoveryId, warnings };
|
|
112
|
+
},
|
|
113
|
+
}),
|
|
114
|
+
'create-chapter': tool({
|
|
115
|
+
title: 'Create chapter',
|
|
116
|
+
description: 'Append a draft chapter to a story; it is not published.',
|
|
117
|
+
input: {
|
|
118
|
+
storyId: id,
|
|
119
|
+
title: zod_1.z.string().optional(),
|
|
120
|
+
isAIGenerated: zod_1.z.boolean().optional(),
|
|
121
|
+
...bodyShape,
|
|
122
|
+
},
|
|
123
|
+
annotations: CREATE,
|
|
124
|
+
run: (client, { content, ...args }) => client.createChapter({
|
|
125
|
+
...args,
|
|
126
|
+
operationId: args.operationId ?? (0, node_crypto_1.randomUUID)(),
|
|
127
|
+
content: content,
|
|
128
|
+
}),
|
|
129
|
+
}),
|
|
130
|
+
'update-story': tool({
|
|
131
|
+
title: 'Update story',
|
|
132
|
+
description: 'Change story details; only the given fields change.',
|
|
133
|
+
input: {
|
|
134
|
+
storyId: id,
|
|
135
|
+
title: zod_1.z.string().optional(),
|
|
136
|
+
description: zod_1.z.string().optional(),
|
|
137
|
+
genres: zod_1.z.array(zod_1.z.string()).optional(),
|
|
138
|
+
tags: zod_1.z.array(zod_1.z.string()).optional(),
|
|
139
|
+
rating: zod_1.z.string().optional(),
|
|
140
|
+
license: zod_1.z.string().optional(),
|
|
141
|
+
language,
|
|
142
|
+
universeId: zod_1.z
|
|
143
|
+
.string()
|
|
144
|
+
.nullable()
|
|
145
|
+
.optional()
|
|
146
|
+
.describe('null makes the story standalone'),
|
|
147
|
+
},
|
|
148
|
+
annotations: WRITE,
|
|
149
|
+
run: (client, { storyId, ...input }) => client.updateStory({ storyId, input }),
|
|
150
|
+
}),
|
|
151
|
+
'set-story-visibility': tool({
|
|
152
|
+
title: 'Set story visibility',
|
|
153
|
+
description: 'Hide a published story from readers (PRIVATE) or show it again (PUBLIC).',
|
|
154
|
+
input: { storyId: id, visibility: zod_1.z.enum(['PUBLIC', 'PRIVATE']) },
|
|
155
|
+
annotations: WRITE,
|
|
156
|
+
run: (client, args) => client.setStoryVisibility(args),
|
|
157
|
+
}),
|
|
158
|
+
'rename-chapter': tool({
|
|
159
|
+
title: 'Rename chapter',
|
|
160
|
+
description: 'Rename a chapter; readers see the change at once when it is published.',
|
|
161
|
+
input: { id, language, title: zod_1.z.string().min(1) },
|
|
162
|
+
annotations: WRITE,
|
|
163
|
+
run: (client, args) => client.renameChapter(args),
|
|
164
|
+
}),
|
|
165
|
+
'reorder-chapters': tool({
|
|
166
|
+
title: 'Reorder chapters',
|
|
167
|
+
description: 'Put every chapter of a story, each once, in a new reading order.',
|
|
168
|
+
input: { storyId: id, chapterIds: zod_1.z.array(zod_1.z.string()).min(1) },
|
|
169
|
+
annotations: WRITE,
|
|
170
|
+
run: (client, args) => client.reorderChapters(args),
|
|
171
|
+
}),
|
|
172
|
+
'remove-chapter': tool({
|
|
173
|
+
title: 'Remove chapter',
|
|
174
|
+
description: 'Delete a chapter that was never published and renumber the rest.',
|
|
175
|
+
input: { id },
|
|
176
|
+
annotations: DESTROY,
|
|
177
|
+
run: (client, args) => client.removeChapter(args),
|
|
178
|
+
}),
|
|
179
|
+
};
|
|
180
|
+
const publish = {
|
|
181
|
+
'publish-chapter': tool({
|
|
182
|
+
title: 'Publish chapter',
|
|
183
|
+
description: 'Publish the draft at expectedVersion to readers or beta readers, now or at publishAt. Without confirm: true nothing happens and the request is echoed back; ask the author first.',
|
|
184
|
+
input: {
|
|
185
|
+
id,
|
|
186
|
+
language,
|
|
187
|
+
expectedVersion: zod_1.z.string(),
|
|
188
|
+
publishAt: zod_1.z
|
|
189
|
+
.string()
|
|
190
|
+
.optional()
|
|
191
|
+
.describe('ISO 8601 with timezone; now when omitted'),
|
|
192
|
+
audience: zod_1.z.enum(['READERS', 'BETA']).default('READERS'),
|
|
193
|
+
operationId,
|
|
194
|
+
confirm: zod_1.z.boolean().default(false),
|
|
195
|
+
},
|
|
196
|
+
annotations: hints(false, false, true, true),
|
|
197
|
+
run: async (client, { confirm, ...input }) => {
|
|
198
|
+
if (!confirm)
|
|
199
|
+
return { published: false, wouldPublish: input };
|
|
200
|
+
const result = await client.publishChapter({
|
|
201
|
+
...input,
|
|
202
|
+
operationId: input.operationId ?? (0, node_crypto_1.randomUUID)(),
|
|
203
|
+
});
|
|
204
|
+
return { published: true, ...result };
|
|
205
|
+
},
|
|
206
|
+
text: (result) => result.published
|
|
207
|
+
? JSON.stringify(result, null, 2)
|
|
208
|
+
: `Nothing published. Call publish-chapter again with confirm: true to publish ${JSON.stringify(result.wouldPublish)}.`,
|
|
209
|
+
}),
|
|
210
|
+
};
|
|
211
|
+
const universes = {
|
|
212
|
+
'list-universes': tool({
|
|
213
|
+
title: 'List universes',
|
|
214
|
+
description: 'A page of the universes this account can write.',
|
|
215
|
+
input: page,
|
|
216
|
+
annotations: READ,
|
|
217
|
+
run: (client, args) => client.universes(args),
|
|
218
|
+
}),
|
|
219
|
+
'get-universe': tool({
|
|
220
|
+
title: 'Get universe',
|
|
221
|
+
description: 'A universe with its description and translations.',
|
|
222
|
+
input: { id },
|
|
223
|
+
annotations: READ,
|
|
224
|
+
run: (client, args) => client.universe(args),
|
|
225
|
+
}),
|
|
226
|
+
'list-encyclopedia-entries': tool({
|
|
227
|
+
title: 'List encyclopedia entries',
|
|
228
|
+
description: 'A page of a universe encyclopedia, drafts included.',
|
|
229
|
+
input: { universeId: id, ...page },
|
|
230
|
+
annotations: READ,
|
|
231
|
+
run: (client, args) => client.encyclopediaEntries(args),
|
|
232
|
+
}),
|
|
233
|
+
'get-encyclopedia-entry': tool({
|
|
234
|
+
title: 'Get encyclopedia entry',
|
|
235
|
+
description: 'An entry with its summary, fields, parent and related entries.',
|
|
236
|
+
input: { id, language },
|
|
237
|
+
annotations: READ,
|
|
238
|
+
run: (client, args) => client.encyclopediaEntry(args),
|
|
239
|
+
}),
|
|
240
|
+
'list-series': tool({
|
|
241
|
+
title: 'List series',
|
|
242
|
+
description: 'The series of a universe with their stories in reading order.',
|
|
243
|
+
input: { universeId: id },
|
|
244
|
+
annotations: READ,
|
|
245
|
+
run: async (client, args) => ({ series: await client.series(args) }),
|
|
246
|
+
}),
|
|
247
|
+
};
|
|
248
|
+
const translation = {
|
|
249
|
+
title: zod_1.z.string().optional(),
|
|
250
|
+
description: zod_1.z.string().optional(),
|
|
251
|
+
};
|
|
252
|
+
const universesWrite = {
|
|
253
|
+
'update-universe': tool({
|
|
254
|
+
title: 'Update universe',
|
|
255
|
+
description: 'Change universe details; only the given fields change.',
|
|
256
|
+
input: {
|
|
257
|
+
universeId: id,
|
|
258
|
+
...translation,
|
|
259
|
+
slug: zod_1.z.string().optional(),
|
|
260
|
+
genre: zod_1.z.string().optional(),
|
|
261
|
+
rating: zod_1.z.string().optional(),
|
|
262
|
+
license: zod_1.z.string().optional(),
|
|
263
|
+
language,
|
|
264
|
+
},
|
|
265
|
+
annotations: WRITE,
|
|
266
|
+
run: (client, { universeId, ...input }) => client.updateUniverse({ universeId, input }),
|
|
267
|
+
}),
|
|
268
|
+
'create-universe-translation': tool({
|
|
269
|
+
title: 'Create universe translation',
|
|
270
|
+
description: 'Open a language for a universe; an existing translation is returned unchanged.',
|
|
271
|
+
input: { universeId: id, language: zod_1.z.string(), ...translation },
|
|
272
|
+
annotations: WRITE,
|
|
273
|
+
run: (client, { universeId, language, ...input }) => client.createUniverseTranslation({ universeId, language, input }),
|
|
274
|
+
}),
|
|
275
|
+
'update-universe-translation': tool({
|
|
276
|
+
title: 'Update universe translation',
|
|
277
|
+
description: 'Change a translation at expectedRevision; a stale revision fails with VERSION_CONFLICT.',
|
|
278
|
+
input: {
|
|
279
|
+
universeId: id,
|
|
280
|
+
language: zod_1.z.string(),
|
|
281
|
+
expectedRevision: zod_1.z.number().int(),
|
|
282
|
+
...translation,
|
|
283
|
+
},
|
|
284
|
+
annotations: WRITE,
|
|
285
|
+
run: (client, { universeId, language, expectedRevision, ...input }) => client.updateUniverseTranslation({
|
|
286
|
+
universeId,
|
|
287
|
+
language,
|
|
288
|
+
expectedRevision,
|
|
289
|
+
input,
|
|
290
|
+
}),
|
|
291
|
+
}),
|
|
292
|
+
'create-encyclopedia-entry': tool({
|
|
293
|
+
title: 'Create encyclopedia entry',
|
|
294
|
+
description: 'Add a draft entry in a category; a taken name fails with CONFLICT.',
|
|
295
|
+
input: {
|
|
296
|
+
universeId: id,
|
|
297
|
+
name: zod_1.z.string().min(1),
|
|
298
|
+
categoryTitle: zod_1.z.string().min(1),
|
|
299
|
+
pronunciation: zod_1.z.string().optional(),
|
|
300
|
+
tags: zod_1.z.array(zod_1.z.string()).optional(),
|
|
301
|
+
parentName: zod_1.z.string().optional(),
|
|
302
|
+
relatedAdd: zod_1.z.array(zod_1.z.string()).optional(),
|
|
303
|
+
summary: zod_1.z.string().optional(),
|
|
304
|
+
fields: zod_1.z.array(zod_1.z.object(textField)).optional(),
|
|
305
|
+
timeFields: zod_1.z.array(zod_1.z.object(timeField)).optional(),
|
|
306
|
+
},
|
|
307
|
+
annotations: CREATE,
|
|
308
|
+
run: (client, args) => client.createEncyclopediaEntry(args),
|
|
309
|
+
}),
|
|
310
|
+
'update-encyclopedia-entry': tool({
|
|
311
|
+
title: 'Update encyclopedia entry',
|
|
312
|
+
description: 'Change an entry in its original language; only the given values change.',
|
|
313
|
+
input: {
|
|
314
|
+
id,
|
|
315
|
+
name: zod_1.z.string().optional(),
|
|
316
|
+
pronunciation: zod_1.z.string().optional(),
|
|
317
|
+
tags: zod_1.z.array(zod_1.z.string()).optional(),
|
|
318
|
+
categoryTitle: zod_1.z.string().optional(),
|
|
319
|
+
parentName: zod_1.z
|
|
320
|
+
.string()
|
|
321
|
+
.nullable()
|
|
322
|
+
.optional()
|
|
323
|
+
.describe('null removes the parent'),
|
|
324
|
+
relatedAdd: zod_1.z.array(zod_1.z.string()).optional(),
|
|
325
|
+
relatedRemove: zod_1.z.array(zod_1.z.string()).optional(),
|
|
326
|
+
summary: zod_1.z.string().optional(),
|
|
327
|
+
fields: zod_1.z.array(zod_1.z.object(textField)).optional(),
|
|
328
|
+
timeFields: zod_1.z.array(zod_1.z.object(timeField)).optional(),
|
|
329
|
+
},
|
|
330
|
+
annotations: WRITE,
|
|
331
|
+
run: (client, { id, ...changes }) => client.updateEncyclopediaEntry({ id, changes }),
|
|
332
|
+
}),
|
|
333
|
+
'translate-encyclopedia-entry': tool({
|
|
334
|
+
title: 'Translate encyclopedia entry',
|
|
335
|
+
description: 'Write an entry edition in a language the universe is translated into.',
|
|
336
|
+
input: {
|
|
337
|
+
id,
|
|
338
|
+
language: zod_1.z.string(),
|
|
339
|
+
name: zod_1.z.string().optional(),
|
|
340
|
+
pronunciation: zod_1.z.string().optional(),
|
|
341
|
+
summary: zod_1.z.string().optional(),
|
|
342
|
+
fields: zod_1.z
|
|
343
|
+
.array(zod_1.z.object({ ...textField, translatedTitle: zod_1.z.string().optional() }))
|
|
344
|
+
.optional(),
|
|
345
|
+
},
|
|
346
|
+
annotations: WRITE,
|
|
347
|
+
run: (client, args) => client.translateEncyclopediaEntry(args),
|
|
348
|
+
}),
|
|
349
|
+
'set-encyclopedia-entry-publication': tool({
|
|
350
|
+
title: 'Set encyclopedia entry publication',
|
|
351
|
+
description: 'Publish an entry to readers, or take it back to a draft.',
|
|
352
|
+
input: { id, published: zod_1.z.boolean() },
|
|
353
|
+
annotations: WRITE,
|
|
354
|
+
run: (client, args) => client.setEncyclopediaEntryPublication(args),
|
|
355
|
+
}),
|
|
356
|
+
'delete-encyclopedia-entry': tool({
|
|
357
|
+
title: 'Delete encyclopedia entry',
|
|
358
|
+
description: 'Delete an entry and its links from other entries.',
|
|
359
|
+
input: { id },
|
|
360
|
+
annotations: DESTROY,
|
|
361
|
+
run: (client, args) => client.deleteEncyclopediaEntry(args),
|
|
362
|
+
}),
|
|
363
|
+
'save-series': tool({
|
|
364
|
+
title: 'Save series',
|
|
365
|
+
description: 'Create a series (no seriesId, expectedVersion 0) or replace one at expectedVersion.',
|
|
366
|
+
input: {
|
|
367
|
+
universeId: id,
|
|
368
|
+
seriesId: zod_1.z.string().optional(),
|
|
369
|
+
expectedVersion: zod_1.z.number().int(),
|
|
370
|
+
title: zod_1.z.string().min(1),
|
|
371
|
+
storyIds: zod_1.z.array(zod_1.z.string()),
|
|
372
|
+
},
|
|
373
|
+
annotations: WRITE,
|
|
374
|
+
run: (client, args) => client.saveSeries(args),
|
|
375
|
+
}),
|
|
376
|
+
};
|
|
377
|
+
const imageTypes = {
|
|
378
|
+
'.png': 'image/png',
|
|
379
|
+
'.webp': 'image/webp',
|
|
380
|
+
};
|
|
381
|
+
const images = {
|
|
382
|
+
'get-image-policy': tool({
|
|
383
|
+
title: 'Get image policy',
|
|
384
|
+
description: 'Size, type and aspect limits for every image purpose.',
|
|
385
|
+
input: {},
|
|
386
|
+
annotations: READ,
|
|
387
|
+
run: (client) => client.imagePolicy(),
|
|
388
|
+
}),
|
|
389
|
+
'set-cover': tool({
|
|
390
|
+
title: 'Set cover',
|
|
391
|
+
description: 'Replace a story or universe cover with a local image file under 5 MB; the server crops it.',
|
|
392
|
+
input: {
|
|
393
|
+
kind: zod_1.z.enum(['story', 'universe']),
|
|
394
|
+
id,
|
|
395
|
+
path: zod_1.z.string().describe('Local path of a JPEG, PNG or WebP file'),
|
|
396
|
+
},
|
|
397
|
+
annotations: WRITE,
|
|
398
|
+
run: async (client, { path, ...args }, options) => {
|
|
399
|
+
if (!options.readLocalFile)
|
|
400
|
+
throw new Error('set-cover needs local file access');
|
|
401
|
+
const bytes = await options.readLocalFile(path);
|
|
402
|
+
const type = imageTypes[(0, node_path_1.extname)(path).toLowerCase()] ?? 'image/jpeg';
|
|
403
|
+
return client.setCover({ ...args, bytes, type });
|
|
404
|
+
},
|
|
405
|
+
}),
|
|
406
|
+
};
|
|
407
|
+
const docsIndex = (base) => [
|
|
408
|
+
{ title: 'About Literary Universe', url: `${base}/llms.txt` },
|
|
409
|
+
{
|
|
410
|
+
title: 'Help centre: every article, each linked as Markdown',
|
|
411
|
+
url: `${base}/feedback/help/llms.txt`,
|
|
412
|
+
},
|
|
413
|
+
{
|
|
414
|
+
title: 'Help centre: all articles in one file',
|
|
415
|
+
url: `${base}/feedback/help/llms-full.txt`,
|
|
416
|
+
},
|
|
417
|
+
{ title: 'A story for agents', url: `${base}/story/<slug>/llms.txt` },
|
|
418
|
+
{ title: 'A universe for agents', url: `${base}/universe/<slug>/llms.txt` },
|
|
419
|
+
{
|
|
420
|
+
title: 'A published chapter for agents',
|
|
421
|
+
url: `${base}/read/<slug>/<language>/llms.txt`,
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
title: 'A universe encyclopedia for agents (author access)',
|
|
425
|
+
url: `${base}/workshop/universe/<slug>/encyclopedia/llms.txt`,
|
|
426
|
+
},
|
|
427
|
+
];
|
|
428
|
+
/** llms.txt pages, the full help file and help articles as Markdown. */
|
|
429
|
+
const DOCS_PATH = /\/llms(?:-full)?\.txt$|^\/feedback\/help\/article\/[^/]+\.md$/;
|
|
430
|
+
async function fetchDocs(options, url) {
|
|
431
|
+
const base = new URL(options.docsBaseUrl ?? DOCS);
|
|
432
|
+
const target = new URL(url, base);
|
|
433
|
+
if (target.origin !== base.origin || !DOCS_PATH.test(target.pathname))
|
|
434
|
+
throw new index_js_1.LiteraryUniverseApiError('BAD_USER_INPUT', `Only ${base.origin} llms.txt pages and help articles can be fetched`);
|
|
435
|
+
const fetcher = options.fetch ?? globalThis.fetch;
|
|
436
|
+
const response = await fetcher(target, {
|
|
437
|
+
signal: AbortSignal.timeout(30_000),
|
|
438
|
+
redirect: 'manual',
|
|
439
|
+
});
|
|
440
|
+
if (response.status >= 300 && response.status < 400) {
|
|
441
|
+
await response.body?.cancel();
|
|
442
|
+
throw new index_js_1.LiteraryUniverseApiError('REDIRECTED', 'Docs redirects are not followed');
|
|
443
|
+
}
|
|
444
|
+
const reader = typeof response.body?.getReader === 'function'
|
|
445
|
+
? response.body.getReader()
|
|
446
|
+
: undefined;
|
|
447
|
+
// Nonstreaming fetch polyfills must buffer before the limit can be checked.
|
|
448
|
+
if (!reader) {
|
|
449
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
450
|
+
if (bytes.byteLength > 1_000_000)
|
|
451
|
+
throw new index_js_1.LiteraryUniverseApiError('PAYLOAD_TOO_LARGE', 'Docs page exceeds the read size limit');
|
|
452
|
+
return {
|
|
453
|
+
url: target.href,
|
|
454
|
+
status: response.status,
|
|
455
|
+
text: new TextDecoder().decode(bytes),
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
const chunks = [];
|
|
459
|
+
let size = 0;
|
|
460
|
+
try {
|
|
461
|
+
while (true) {
|
|
462
|
+
const { done, value } = await reader.read();
|
|
463
|
+
if (done)
|
|
464
|
+
break;
|
|
465
|
+
size += value.byteLength;
|
|
466
|
+
if (size > 1_000_000) {
|
|
467
|
+
await reader.cancel();
|
|
468
|
+
throw new index_js_1.LiteraryUniverseApiError('PAYLOAD_TOO_LARGE', 'Docs page exceeds the read size limit');
|
|
469
|
+
}
|
|
470
|
+
chunks.push(value);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
finally {
|
|
474
|
+
reader.releaseLock();
|
|
475
|
+
}
|
|
476
|
+
const text = new TextDecoder().decode(Buffer.concat(chunks));
|
|
477
|
+
return { url: target.href, status: response.status, text };
|
|
478
|
+
}
|
|
479
|
+
const docs = {
|
|
480
|
+
'search-lu-docs': tool({
|
|
481
|
+
title: 'Search Literary Universe docs',
|
|
482
|
+
description: 'Pages about Literary Universe and its works that an agent can read; replace the <placeholders>.',
|
|
483
|
+
input: { query: zod_1.z.string().optional() },
|
|
484
|
+
annotations: WEB,
|
|
485
|
+
docs: true,
|
|
486
|
+
run: async (options, { query }) => {
|
|
487
|
+
const index = docsIndex(options.docsBaseUrl ?? DOCS);
|
|
488
|
+
const q = query?.toLowerCase() ?? '';
|
|
489
|
+
const hits = index.filter((entry) => `${entry.title} ${entry.url}`.toLowerCase().includes(q));
|
|
490
|
+
return { results: hits.length ? hits : index };
|
|
491
|
+
},
|
|
492
|
+
}),
|
|
493
|
+
'fetch-lu-docs': tool({
|
|
494
|
+
title: 'Fetch Literary Universe docs',
|
|
495
|
+
description: 'Read one llms.txt page or help article (.md) of the Literary Universe site as text.',
|
|
496
|
+
input: { url: zod_1.z.string().describe('A URL from search-lu-docs') },
|
|
497
|
+
annotations: WEB,
|
|
498
|
+
docs: true,
|
|
499
|
+
run: (options, { url }) => fetchDocs(options, url),
|
|
500
|
+
text: (result) => String(result.text),
|
|
501
|
+
}),
|
|
502
|
+
};
|
|
503
|
+
const account = {
|
|
504
|
+
'get-identity': tool({
|
|
505
|
+
title: 'Get identity',
|
|
506
|
+
description: 'Who is signed in, with scopes, limits and the deployment.',
|
|
507
|
+
input: {},
|
|
508
|
+
annotations: READ,
|
|
509
|
+
run: (client) => client.identity(),
|
|
510
|
+
}),
|
|
511
|
+
};
|
|
512
|
+
const CATEGORIES = {
|
|
513
|
+
account,
|
|
514
|
+
stories,
|
|
515
|
+
'stories-write': storiesWrite,
|
|
516
|
+
publish,
|
|
517
|
+
universes,
|
|
518
|
+
'universes-write': universesWrite,
|
|
519
|
+
images,
|
|
520
|
+
docs,
|
|
521
|
+
};
|
|
522
|
+
const TOOLS = Object.assign({}, ...Object.values(CATEGORIES));
|
|
523
|
+
exports.TOOL_CATEGORIES = Object.fromEntries(Object.entries(CATEGORIES).map(([name, tools]) => [
|
|
524
|
+
name,
|
|
525
|
+
Object.keys(tools),
|
|
526
|
+
]));
|
|
527
|
+
exports.DEFAULT_TOOL_SELECTION = [
|
|
528
|
+
'account',
|
|
529
|
+
'stories',
|
|
530
|
+
'stories-write',
|
|
531
|
+
'universes',
|
|
532
|
+
'universes-write',
|
|
533
|
+
'images',
|
|
534
|
+
'docs',
|
|
535
|
+
];
|
|
536
|
+
/** Expands categories and tool names into tool names; throws on an unknown entry. */
|
|
537
|
+
function selectTools(selection) {
|
|
538
|
+
const names = (selection ?? exports.DEFAULT_TOOL_SELECTION).flatMap((entry) => {
|
|
539
|
+
if (entry === 'all')
|
|
540
|
+
return Object.keys(TOOLS);
|
|
541
|
+
if (Object.hasOwn(exports.TOOL_CATEGORIES, entry))
|
|
542
|
+
return exports.TOOL_CATEGORIES[entry];
|
|
543
|
+
if (Object.hasOwn(TOOLS, entry))
|
|
544
|
+
return [entry];
|
|
545
|
+
throw new index_js_1.LiteraryUniverseApiError('INVALID_CONFIGURATION', `Unknown tool or category: ${entry}`);
|
|
546
|
+
});
|
|
547
|
+
return [...new Set(names)];
|
|
548
|
+
}
|
|
549
|
+
const failed = (text) => ({
|
|
550
|
+
isError: true,
|
|
551
|
+
content: [{ type: 'text', text }],
|
|
552
|
+
});
|
|
553
|
+
function describe(error, spec) {
|
|
554
|
+
const request = error.requestId ? ` (request ${error.requestId})` : '';
|
|
555
|
+
const schema = error.code === 'BAD_USER_INPUT'
|
|
556
|
+
? `\nInput schema: ${JSON.stringify(zod_1.z.toJSONSchema(zod_1.z.object(spec.input)))}`
|
|
557
|
+
: '';
|
|
558
|
+
return `${error.code}: ${error.message}${request}${schema}`;
|
|
559
|
+
}
|
|
560
|
+
function run(spec, args, options) {
|
|
561
|
+
if (spec.docs)
|
|
562
|
+
return spec.run(options, args);
|
|
563
|
+
const client = options.client();
|
|
564
|
+
if (!client)
|
|
565
|
+
throw new index_js_1.LiteraryUniverseApiError('UNAUTHENTICATED', NOT_SIGNED_IN);
|
|
566
|
+
return spec.run(client, args, options);
|
|
567
|
+
}
|
|
568
|
+
const handler = (spec, options) => async (args) => {
|
|
569
|
+
try {
|
|
570
|
+
const result = await run(spec, args, options);
|
|
571
|
+
const text = spec.text?.(result) ?? JSON.stringify(result, null, 2);
|
|
572
|
+
return { content: [{ type: 'text', text }], structuredContent: result };
|
|
573
|
+
}
|
|
574
|
+
catch (error) {
|
|
575
|
+
if (!(error instanceof index_js_1.LiteraryUniverseApiError))
|
|
576
|
+
throw error;
|
|
577
|
+
return failed(error.message === NOT_SIGNED_IN ? NOT_SIGNED_IN : describe(error, spec));
|
|
578
|
+
}
|
|
579
|
+
};
|
|
580
|
+
function signedIn(options) {
|
|
581
|
+
const client = options.client();
|
|
582
|
+
if (!client)
|
|
583
|
+
throw new Error(NOT_SIGNED_IN);
|
|
584
|
+
return client;
|
|
585
|
+
}
|
|
586
|
+
function registerResources(server, options) {
|
|
587
|
+
server.registerResource('story', new mcp_js_1.ResourceTemplate('lu://story/{id}', { list: undefined }), {
|
|
588
|
+
title: 'Story',
|
|
589
|
+
description: 'A story as get-story returns it',
|
|
590
|
+
mimeType: 'application/json',
|
|
591
|
+
}, async (uri, { id }) => ({
|
|
592
|
+
contents: [
|
|
593
|
+
{
|
|
594
|
+
uri: uri.href,
|
|
595
|
+
mimeType: 'application/json',
|
|
596
|
+
text: JSON.stringify(await signedIn(options).story({ id: String(id) })),
|
|
597
|
+
},
|
|
598
|
+
],
|
|
599
|
+
}));
|
|
600
|
+
server.registerResource('chapter-draft', new mcp_js_1.ResourceTemplate('lu://chapter/{id}/draft', { list: undefined }), {
|
|
601
|
+
title: 'Chapter draft',
|
|
602
|
+
description: 'A chapter draft as Markdown',
|
|
603
|
+
mimeType: 'text/markdown',
|
|
604
|
+
}, async (uri, { id }) => {
|
|
605
|
+
const draft = await signedIn(options).draft({ id: String(id) });
|
|
606
|
+
const text = draftText(draftView(draft, 'markdown'));
|
|
607
|
+
return { contents: [{ uri: uri.href, mimeType: 'text/markdown', text }] };
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
/** Registers the selected tools and the story/draft resources on an MCP server. */
|
|
611
|
+
function registerLiteraryUniverseTools(server, options) {
|
|
612
|
+
const registered = selectTools(options.tools).filter((name) => name !== 'set-cover' || options.readLocalFile);
|
|
613
|
+
for (const name of registered) {
|
|
614
|
+
const spec = TOOLS[name];
|
|
615
|
+
server.registerTool(name, {
|
|
616
|
+
title: spec.title,
|
|
617
|
+
description: spec.description,
|
|
618
|
+
inputSchema: spec.input,
|
|
619
|
+
annotations: spec.annotations,
|
|
620
|
+
}, handler(spec, options));
|
|
621
|
+
}
|
|
622
|
+
registerResources(server, options);
|
|
623
|
+
return { registered };
|
|
624
|
+
}
|
package/dist/cjs/project.d.ts
CHANGED
|
@@ -1,24 +1,41 @@
|
|
|
1
1
|
import { type LiteraryUniverseClient } from './index.js';
|
|
2
2
|
import type { JsonValue } from './json.js';
|
|
3
|
+
type Pending = {
|
|
4
|
+
operationId: string;
|
|
5
|
+
expectedVersion: string;
|
|
6
|
+
/** Digest of the form sent: `content`, or the Markdown body. */
|
|
7
|
+
hash: string;
|
|
8
|
+
content?: JsonValue;
|
|
9
|
+
markdown?: string;
|
|
10
|
+
};
|
|
3
11
|
type ChapterFile = {
|
|
4
12
|
id: string;
|
|
5
13
|
language: string;
|
|
6
14
|
file: string;
|
|
7
15
|
version: string;
|
|
8
16
|
hash: string;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
17
|
+
/** The Markdown mirror; absent in projects pulled before it existed. */
|
|
18
|
+
markdown?: {
|
|
19
|
+
file: string;
|
|
12
20
|
hash: string;
|
|
13
|
-
content: JsonValue;
|
|
14
21
|
};
|
|
22
|
+
pending?: Pending;
|
|
15
23
|
};
|
|
16
24
|
export type LocalProject = {
|
|
17
25
|
version: 1;
|
|
18
26
|
baseUrl: string;
|
|
19
27
|
storyId: string;
|
|
28
|
+
/** The language filter the project was pulled with. */
|
|
29
|
+
language?: string;
|
|
20
30
|
chapters: ChapterFile[];
|
|
21
31
|
};
|
|
32
|
+
export type ChangeSource = 'none' | 'json' | 'markdown' | 'both';
|
|
33
|
+
/**
|
|
34
|
+
* What the server's Markdown has no form for, so a `markdown` save is refused
|
|
35
|
+
* with MARKDOWN_UNSUPPORTED: node and mark types, plus the paragraph/heading
|
|
36
|
+
* attributes `textAlign` (other than left) and `initialLetter`.
|
|
37
|
+
*/
|
|
38
|
+
export declare const MARKDOWN_LOSSY_TYPES: readonly string[];
|
|
22
39
|
/** Loads only the fixed managed-project layout, never arbitrary manifest paths. */
|
|
23
40
|
export declare function readLocalProject(directory: string): LocalProject;
|
|
24
41
|
export declare function localProjectStatus(directory: string): {
|
|
@@ -27,6 +44,7 @@ export declare function localProjectStatus(directory: string): {
|
|
|
27
44
|
file: string;
|
|
28
45
|
version: string;
|
|
29
46
|
changed: boolean;
|
|
47
|
+
source: ChangeSource;
|
|
30
48
|
pending: boolean;
|
|
31
49
|
}[];
|
|
32
50
|
/** Pulls into a new directory, preserving existing local files. */
|
|
@@ -35,7 +53,22 @@ export declare function pullLocalProject(client: LiteraryUniverseClient, input:
|
|
|
35
53
|
baseUrl: string;
|
|
36
54
|
storyId: string;
|
|
37
55
|
language?: string;
|
|
38
|
-
|
|
56
|
+
/** Re-pull an existing project instead; see refreshLocalProject. */
|
|
57
|
+
refresh?: boolean;
|
|
58
|
+
}): Promise<LocalProject | {
|
|
59
|
+
added: string[];
|
|
60
|
+
updated: string[];
|
|
61
|
+
removed: string[];
|
|
62
|
+
}>;
|
|
63
|
+
/** Re-pulls every chapter of an existing project; refuses while anything local is unpushed. */
|
|
64
|
+
export declare function refreshLocalProject(client: LiteraryUniverseClient, input: {
|
|
65
|
+
directory: string;
|
|
66
|
+
baseUrl: string;
|
|
67
|
+
}): Promise<{
|
|
68
|
+
added: string[];
|
|
69
|
+
updated: string[];
|
|
70
|
+
removed: string[];
|
|
71
|
+
}>;
|
|
39
72
|
/** Saves each changed draft once; pending intents survive a lost response or stopped process. */
|
|
40
73
|
export declare function pushLocalProject(client: LiteraryUniverseClient, input: {
|
|
41
74
|
directory: string;
|