@dsh-agentic-roleplay/core 0.12.2 → 0.12.4
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/lib/{authoring-tools-BZr4bgBy.js → authoring-tools-B_qYKK8c.js} +2135 -1863
- package/lib/authoring.js +208 -35
- package/lib/client.js +850 -491
- package/lib/client.js.map +1 -1
- package/lib/doctor.js +1 -1
- package/lib/{gallery-BNg0rPTp.js → gallery-Bu6eZMKk.js} +2 -2
- package/lib/{generator-9-5at9cZ.js → generator-CTWQWbSW.js} +2 -2
- package/lib/{hooks-Dtq5KZQX.js → hooks-YXQZ9G2d.js} +200 -41
- package/lib/images.js +4 -4
- package/lib/index.js +62 -42
- package/lib/{library-DTxakw0z.js → library-BmlHNCah.js} +2 -2
- package/lib/{lorebook-author-tools-BjcEUo22.js → lorebook-author-tools-C3rRpMt7.js} +6 -6
- package/lib/lorebook-authoring.js +5 -5
- package/lib/orchestration.js +8 -8
- package/lib/{pack-jail-Dt0VTsu0.js → pack-jail-VbcXiukr.js} +42 -14
- package/lib/{packs-DWfR2Ted.js → packs-CzVh2ZNe.js} +2 -2
- package/lib/{presenter-agent-vFOxIuWv.js → presenter-agent-CF3fneuJ.js} +4 -4
- package/lib/{preset-Dv5YF2qj.js → preset-C595KRUM.js} +3 -3
- package/lib/{preset-author-tools-BUzO9mTW.js → preset-author-tools-CF_JKKTE.js} +6 -6
- package/lib/preset-authoring.js +5 -5
- package/lib/{reflection-Dd3pEiZB.js → reflection-ByIQSOKC.js} +6 -6
- package/lib/{serial-BUaJGwVy.js → serial-ULGUKWsu.js} +1136 -1056
- package/lib/{text-Dfv8S8eq.js → text-hiKTooVb.js} +3 -1
- package/lib/{turn-guard-87NE-Cnp.js → turn-guard-DXMeAJdv.js} +2 -2
- package/package.json +3 -2
- package/prompts/en/import/opening.md +17 -0
- package/prompts/en/roleplay/decide-opening.md +1 -0
- package/prompts/en/world/procedure.md +1 -1
- package/prompts/zh/import/opening.md +17 -0
- package/prompts/zh/roleplay/decide-opening.md +1 -0
- package/prompts/zh/world/procedure.md +1 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { m as parseSampling } from "./child-DTdY14S9.js";
|
|
2
2
|
import { f as parseSwitchboard } from "./narration--K2XVIHI.js";
|
|
3
3
|
import { a as VARIABLE_KINDS, n as atomicWrite, o as VARIABLE_SUBJECTS, s as VARIABLE_VISIBILITIES } from "./access-Csp80TFX.js";
|
|
4
|
-
import { G as readDocument, J as errorMessage, K as Reader, X as LANGUAGE_TAG, Y as LANGUAGE_FOLDER, Z as PROMPTS_FOLDER, at as shippedPromptsDirectory, it as shippedLanguages, nt as listFiles, q as formatIssue, rt as promptProblem, tt as languageOf } from "./hooks-
|
|
5
|
-
import { i as translator, r as languageOf$1 } from "./text-
|
|
6
|
-
import { C as slug, S as norm, T as uniqueKey, w as trOf, x as mapLimit } from "./library-
|
|
4
|
+
import { G as readDocument, J as errorMessage, K as Reader, X as LANGUAGE_TAG, Y as LANGUAGE_FOLDER, Z as PROMPTS_FOLDER, at as shippedPromptsDirectory, it as shippedLanguages, nt as listFiles, q as formatIssue, rt as promptProblem, tt as languageOf } from "./hooks-YXQZ9G2d.js";
|
|
5
|
+
import { i as translator, r as languageOf$1 } from "./text-hiKTooVb.js";
|
|
6
|
+
import { C as slug, S as norm, T as uniqueKey, w as trOf, x as mapLimit } from "./library-BmlHNCah.js";
|
|
7
7
|
import { dirname, join } from "node:path";
|
|
8
8
|
import { MAX_ROUTINE_WINDOWS, createSessionStore, dayTokenProblems, isTimeZone, parseInstant, valueIssue } from "@dsh-agentic-roleplay/state";
|
|
9
9
|
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
@@ -163,558 +163,6 @@ function guarded(residency, role, service, methods) {
|
|
|
163
163
|
return call.bind(target);
|
|
164
164
|
} });
|
|
165
165
|
}
|
|
166
|
-
// src/shared/world-state.ts
|
|
167
|
-
/** The problems with an edit, for the person who made it; nothing was changed. */
|
|
168
|
-
var InvalidEditProblems = class extends Error {
|
|
169
|
-
problems;
|
|
170
|
-
constructor(problems) {
|
|
171
|
-
super(`the edit is not acceptable: ${problems.join("; ")}`);
|
|
172
|
-
this.problems = problems;
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
const TEXT_LIMIT = 2e4;
|
|
176
|
-
const NAME_LIMIT = 200;
|
|
177
|
-
/** Reads an edit from what the browser sent: every unknown or mistyped field is a problem. */
|
|
178
|
-
function parseCharacterEdit(value) {
|
|
179
|
-
const problems = [];
|
|
180
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new InvalidEditProblems(["the edit must be an object"]);
|
|
181
|
-
const input = value;
|
|
182
|
-
const edit = {};
|
|
183
|
-
const text = (source, key, path, limit) => {
|
|
184
|
-
const found = source[key];
|
|
185
|
-
if (found === void 0) return void 0;
|
|
186
|
-
if (typeof found !== "string") {
|
|
187
|
-
problems.push(`${path}${key} must be text`);
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
if (found.length > limit) {
|
|
191
|
-
problems.push(`${path}${key} is longer than ${limit} characters`);
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
return found;
|
|
195
|
-
};
|
|
196
|
-
const list = (source, key, path) => {
|
|
197
|
-
const found = source[key];
|
|
198
|
-
if (found === void 0) return void 0;
|
|
199
|
-
if (!Array.isArray(found) || !found.every((entry) => typeof entry === "string" && entry.length <= NAME_LIMIT) || found.length > 50) {
|
|
200
|
-
problems.push(`${path}${key} must be a list of short texts`);
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
return found;
|
|
204
|
-
};
|
|
205
|
-
const choice = (key, options, source = input, path = "") => {
|
|
206
|
-
const found = source[key];
|
|
207
|
-
if (found === void 0) return void 0;
|
|
208
|
-
if (typeof found !== "string" || !options.includes(found)) {
|
|
209
|
-
problems.push(`${path}${key} must be one of: ${options.join(", ")}`);
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
return found;
|
|
213
|
-
};
|
|
214
|
-
const allowed = (source, keys, path) => {
|
|
215
|
-
for (const key of Object.keys(source)) if (!keys.includes(key)) problems.push(`${path}${key} is not something that can be edited`);
|
|
216
|
-
};
|
|
217
|
-
allowed(input, [
|
|
218
|
-
"name",
|
|
219
|
-
"aliases",
|
|
220
|
-
"age",
|
|
221
|
-
"gender",
|
|
222
|
-
"pronouns",
|
|
223
|
-
"role",
|
|
224
|
-
"tier",
|
|
225
|
-
"kind",
|
|
226
|
-
"summary",
|
|
227
|
-
"personality",
|
|
228
|
-
"background",
|
|
229
|
-
"appearance",
|
|
230
|
-
"voice",
|
|
231
|
-
"visualIdentity",
|
|
232
|
-
"speech",
|
|
233
|
-
"state",
|
|
234
|
-
"place",
|
|
235
|
-
"goals"
|
|
236
|
-
], "");
|
|
237
|
-
const name = text(input, "name", "", NAME_LIMIT);
|
|
238
|
-
if (name !== void 0) {
|
|
239
|
-
if (name.trim() === "") problems.push("name cannot be blank");
|
|
240
|
-
else edit.name = name.trim();
|
|
241
|
-
}
|
|
242
|
-
const aliases = list(input, "aliases", "");
|
|
243
|
-
if (aliases !== void 0) edit.aliases = aliases;
|
|
244
|
-
for (const key of [
|
|
245
|
-
"age",
|
|
246
|
-
"gender",
|
|
247
|
-
"pronouns"
|
|
248
|
-
]) {
|
|
249
|
-
const found = text(input, key, "", NAME_LIMIT);
|
|
250
|
-
if (found !== void 0) edit[key] = found;
|
|
251
|
-
}
|
|
252
|
-
const voice = text(input, "voice", "", NAME_LIMIT);
|
|
253
|
-
if (voice !== void 0) edit.voice = voice;
|
|
254
|
-
for (const key of [
|
|
255
|
-
"summary",
|
|
256
|
-
"personality",
|
|
257
|
-
"background",
|
|
258
|
-
"appearance"
|
|
259
|
-
]) {
|
|
260
|
-
const found = text(input, key, "", TEXT_LIMIT);
|
|
261
|
-
if (found !== void 0) edit[key] = found;
|
|
262
|
-
}
|
|
263
|
-
const role = choice("role", CHARACTER_ROLES);
|
|
264
|
-
if (role !== void 0) edit.role = role;
|
|
265
|
-
const tier = choice("tier", CHARACTER_TIERS);
|
|
266
|
-
if (tier !== void 0) edit.tier = tier;
|
|
267
|
-
const kind = choice("kind", CHARACTER_KINDS);
|
|
268
|
-
if (kind !== void 0) edit.kind = kind;
|
|
269
|
-
const object = (key) => {
|
|
270
|
-
const found = input[key];
|
|
271
|
-
if (found === void 0) return void 0;
|
|
272
|
-
if (typeof found !== "object" || found === null || Array.isArray(found)) {
|
|
273
|
-
problems.push(`${key} must be an object`);
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
return found;
|
|
277
|
-
};
|
|
278
|
-
if (input["visualIdentity"] === null) edit.visualIdentity = null;
|
|
279
|
-
else if (input["visualIdentity"] !== void 0) {
|
|
280
|
-
const visual = object("visualIdentity");
|
|
281
|
-
if (visual !== void 0) {
|
|
282
|
-
allowed(visual, [
|
|
283
|
-
"description",
|
|
284
|
-
"tags",
|
|
285
|
-
"negativeTags",
|
|
286
|
-
"globalId"
|
|
287
|
-
], "visualIdentity.");
|
|
288
|
-
const description = text(visual, "description", "visualIdentity.", 2e3);
|
|
289
|
-
const tags = list(visual, "tags", "visualIdentity.");
|
|
290
|
-
const negativeTags = list(visual, "negativeTags", "visualIdentity.");
|
|
291
|
-
const globalId = text(visual, "globalId", "visualIdentity.", NAME_LIMIT);
|
|
292
|
-
if (description === void 0 || description.trim() === "") problems.push("visualIdentity.description is needed");
|
|
293
|
-
else edit.visualIdentity = {
|
|
294
|
-
description: description.trim(),
|
|
295
|
-
tags: tags ?? [],
|
|
296
|
-
negativeTags: negativeTags ?? [],
|
|
297
|
-
...globalId === void 0 || globalId.trim() === "" ? {} : { globalId: globalId.trim() }
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
const speech = object("speech");
|
|
302
|
-
if (speech !== void 0) {
|
|
303
|
-
allowed(speech, ["style", "examples"], "speech.");
|
|
304
|
-
const style = text(speech, "style", "speech.", TEXT_LIMIT);
|
|
305
|
-
const examples = list(speech, "examples", "speech.");
|
|
306
|
-
edit.speech = {
|
|
307
|
-
...style === void 0 ? {} : { style },
|
|
308
|
-
...examples === void 0 ? {} : { examples }
|
|
309
|
-
};
|
|
310
|
-
}
|
|
311
|
-
const state = object("state");
|
|
312
|
-
if (state !== void 0) {
|
|
313
|
-
allowed(state, [
|
|
314
|
-
"publicState",
|
|
315
|
-
"innerState",
|
|
316
|
-
"activity"
|
|
317
|
-
], "state.");
|
|
318
|
-
edit.state = {};
|
|
319
|
-
for (const key of [
|
|
320
|
-
"publicState",
|
|
321
|
-
"innerState",
|
|
322
|
-
"activity"
|
|
323
|
-
]) {
|
|
324
|
-
const found = text(state, key, "state.", TEXT_LIMIT);
|
|
325
|
-
if (found !== void 0) edit.state[key] = found;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
const place = object("place");
|
|
329
|
-
if (place !== void 0) {
|
|
330
|
-
allowed(place, ["location", "position"], "place.");
|
|
331
|
-
const location = text(place, "location", "place.", NAME_LIMIT);
|
|
332
|
-
const position = text(place, "position", "place.", NAME_LIMIT);
|
|
333
|
-
if (location === void 0 || location.trim() === "") problems.push("place.location is needed");
|
|
334
|
-
else edit.place = {
|
|
335
|
-
location,
|
|
336
|
-
...position === void 0 ? {} : { position }
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
if (input["goals"] !== void 0) {
|
|
340
|
-
if (!Array.isArray(input["goals"])) problems.push("goals must be a list");
|
|
341
|
-
else {
|
|
342
|
-
const goals = [];
|
|
343
|
-
edit.goals = goals;
|
|
344
|
-
input["goals"].forEach((entry, index) => {
|
|
345
|
-
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) {
|
|
346
|
-
problems.push(`goals[${index}] must be an object`);
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
const goal = entry;
|
|
350
|
-
const path = `goals[${index}].`;
|
|
351
|
-
allowed(goal, [
|
|
352
|
-
"ref",
|
|
353
|
-
"name",
|
|
354
|
-
"description",
|
|
355
|
-
"status",
|
|
356
|
-
"priority"
|
|
357
|
-
], path);
|
|
358
|
-
const ref = text(goal, "ref", path, NAME_LIMIT);
|
|
359
|
-
if (ref === void 0) {
|
|
360
|
-
problems.push(`${path}ref is needed`);
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
const goalName = text(goal, "name", path, NAME_LIMIT);
|
|
364
|
-
if (goalName !== void 0 && goalName.trim() === "") problems.push(`${path}name cannot be blank`);
|
|
365
|
-
const description = text(goal, "description", path, TEXT_LIMIT);
|
|
366
|
-
const status = choice("status", INTENT_STATUSES, goal, path);
|
|
367
|
-
const priority = goal["priority"];
|
|
368
|
-
if (priority !== void 0 && (typeof priority !== "number" || !Number.isFinite(priority) || priority < 0 || priority > 1)) problems.push(`${path}priority must be a number from 0 to 1`);
|
|
369
|
-
goals.push({
|
|
370
|
-
ref,
|
|
371
|
-
...goalName === void 0 ? {} : { name: goalName.trim() },
|
|
372
|
-
...description === void 0 ? {} : { description },
|
|
373
|
-
...status === void 0 ? {} : { status },
|
|
374
|
-
...typeof priority === "number" ? { priority } : {}
|
|
375
|
-
});
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
380
|
-
return edit;
|
|
381
|
-
}
|
|
382
|
-
function parseEntityEdit(value, kind) {
|
|
383
|
-
const problems = [];
|
|
384
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new InvalidEditProblems(["the edit must be an object"]);
|
|
385
|
-
const input = value;
|
|
386
|
-
const edit = {};
|
|
387
|
-
const allowed = kind === "place" ? [
|
|
388
|
-
"name",
|
|
389
|
-
"description",
|
|
390
|
-
"features",
|
|
391
|
-
"visualIdentity",
|
|
392
|
-
"parent"
|
|
393
|
-
] : [
|
|
394
|
-
"name",
|
|
395
|
-
"description",
|
|
396
|
-
"quantity",
|
|
397
|
-
"quality",
|
|
398
|
-
"notable",
|
|
399
|
-
"hidden",
|
|
400
|
-
"containerState",
|
|
401
|
-
"visualIdentity",
|
|
402
|
-
"owner"
|
|
403
|
-
];
|
|
404
|
-
const link = kind === "place" ? "parent" : "owner";
|
|
405
|
-
if (link in input) {
|
|
406
|
-
const target = input[link];
|
|
407
|
-
if (target === null) edit[link] = null;
|
|
408
|
-
else if (typeof target !== "string") problems.push(`${link} must be a key or null`);
|
|
409
|
-
else edit[link] = target.trim() === "" ? null : target.trim();
|
|
410
|
-
}
|
|
411
|
-
for (const key of Object.keys(input)) if (!allowed.includes(key)) problems.push(`${key} is not something that can be edited`);
|
|
412
|
-
for (const key of [
|
|
413
|
-
"name",
|
|
414
|
-
"description",
|
|
415
|
-
"quality"
|
|
416
|
-
]) {
|
|
417
|
-
if (!(key in input)) continue;
|
|
418
|
-
const value = input[key];
|
|
419
|
-
if (typeof value !== "string" || value.length > (key === "name" ? NAME_LIMIT : TEXT_LIMIT)) problems.push(`${key} must be text`);
|
|
420
|
-
else if (key === "name" && value.trim() === "") problems.push("name cannot be blank");
|
|
421
|
-
else edit[key] = key === "name" ? value.trim() : value;
|
|
422
|
-
}
|
|
423
|
-
for (const key of ["notable", "hidden"]) if (key in input) {
|
|
424
|
-
if (typeof input[key] !== "boolean") problems.push(`${key} must be true or false`);
|
|
425
|
-
else edit[key] = input[key];
|
|
426
|
-
}
|
|
427
|
-
if ("quantity" in input) {
|
|
428
|
-
const quantity = input["quantity"];
|
|
429
|
-
if (typeof quantity !== "number" || !Number.isInteger(quantity) || quantity < 1) problems.push("quantity must be a whole number from 1");
|
|
430
|
-
else edit["quantity"] = quantity;
|
|
431
|
-
}
|
|
432
|
-
if ("containerState" in input) {
|
|
433
|
-
const state = input["containerState"];
|
|
434
|
-
if (state !== "open" && state !== "closed" && state !== "locked") problems.push("containerState must be one of: open, closed, locked");
|
|
435
|
-
else edit["containerState"] = state;
|
|
436
|
-
}
|
|
437
|
-
if ("features" in input) {
|
|
438
|
-
const features = input["features"];
|
|
439
|
-
if (!Array.isArray(features) || features.length > 30 || !features.every((feature) => typeof feature === "object" && feature !== null && typeof feature["key"] === "string" && typeof feature["name"] === "string" && typeof feature["description"] === "string")) problems.push("features must be a list of named features");
|
|
440
|
-
else edit["features"] = features;
|
|
441
|
-
}
|
|
442
|
-
if (input["visualIdentity"] === null) edit["visualIdentity"] = null;
|
|
443
|
-
else if ("visualIdentity" in input) {
|
|
444
|
-
const identity = input["visualIdentity"];
|
|
445
|
-
if (typeof identity !== "object" || identity === null || Array.isArray(identity)) problems.push("visualIdentity must be an object");
|
|
446
|
-
else {
|
|
447
|
-
const visual = identity;
|
|
448
|
-
const description = visual["description"];
|
|
449
|
-
const tags = visual["tags"];
|
|
450
|
-
const negativeTags = visual["negativeTags"];
|
|
451
|
-
const globalId = visual["globalId"];
|
|
452
|
-
if (typeof description !== "string" || description.trim() === "") problems.push("visualIdentity.description is needed");
|
|
453
|
-
else if (!Array.isArray(tags) || !tags.every((value) => typeof value === "string") || !Array.isArray(negativeTags) || !negativeTags.every((value) => typeof value === "string") || globalId !== void 0 && typeof globalId !== "string") problems.push("visualIdentity has invalid fields");
|
|
454
|
-
else edit["visualIdentity"] = {
|
|
455
|
-
description: description.trim(),
|
|
456
|
-
tags,
|
|
457
|
-
negativeTags,
|
|
458
|
-
...globalId === void 0 || globalId.trim() === "" ? {} : { globalId: globalId.trim() }
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
463
|
-
return edit;
|
|
464
|
-
}
|
|
465
|
-
const parsePlaceEdit = (value) => parseEntityEdit(value, "place");
|
|
466
|
-
const parseThingEdit = (value) => parseEntityEdit(value, "thing");
|
|
467
|
-
function parseFactionEdit(value) {
|
|
468
|
-
const problems = [];
|
|
469
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new InvalidEditProblems(["the edit must be an object"]);
|
|
470
|
-
const input = value;
|
|
471
|
-
const allowed = [
|
|
472
|
-
"name",
|
|
473
|
-
"aliases",
|
|
474
|
-
"summary",
|
|
475
|
-
"description",
|
|
476
|
-
"values",
|
|
477
|
-
"structure",
|
|
478
|
-
"state",
|
|
479
|
-
"goals"
|
|
480
|
-
];
|
|
481
|
-
for (const key of Object.keys(input)) if (!allowed.includes(key)) problems.push(`${key} is not something that can be edited`);
|
|
482
|
-
const edit = {};
|
|
483
|
-
for (const key of [
|
|
484
|
-
"name",
|
|
485
|
-
"summary",
|
|
486
|
-
"description",
|
|
487
|
-
"structure"
|
|
488
|
-
]) if (key in input) {
|
|
489
|
-
const found = input[key];
|
|
490
|
-
if (typeof found !== "string" || found.length > TEXT_LIMIT) problems.push(`${key} must be text`);
|
|
491
|
-
else if (key === "name" && found.trim() === "") problems.push("name cannot be blank");
|
|
492
|
-
else edit[key] = key === "name" ? found.trim() : found;
|
|
493
|
-
}
|
|
494
|
-
for (const key of ["aliases", "values"]) if (key in input) {
|
|
495
|
-
const found = input[key];
|
|
496
|
-
if (!Array.isArray(found) || !found.every((entry) => typeof entry === "string" && entry.length <= NAME_LIMIT)) problems.push(`${key} must be a list of short texts`);
|
|
497
|
-
else edit[key] = found;
|
|
498
|
-
}
|
|
499
|
-
if ("state" in input) {
|
|
500
|
-
const state = input["state"];
|
|
501
|
-
if (typeof state !== "object" || state === null || Array.isArray(state)) problems.push("state must be an object");
|
|
502
|
-
else {
|
|
503
|
-
const record = state;
|
|
504
|
-
edit.state = {};
|
|
505
|
-
for (const key of ["publicState", "innerState"]) if (key in record) {
|
|
506
|
-
if (typeof record[key] !== "string") problems.push(`state.${key} must be text`);
|
|
507
|
-
else edit.state[key] = record[key];
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
if ("goals" in input) try {
|
|
512
|
-
edit.goals = parseCharacterEdit({ goals: input["goals"] }).goals;
|
|
513
|
-
} catch (error) {
|
|
514
|
-
if (error instanceof InvalidEditProblems) problems.push(...error.problems);
|
|
515
|
-
else throw error;
|
|
516
|
-
}
|
|
517
|
-
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
518
|
-
return edit;
|
|
519
|
-
}
|
|
520
|
-
function parseLoreEdit(value) {
|
|
521
|
-
const problems = [];
|
|
522
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new InvalidEditProblems(["the edit must be an object"]);
|
|
523
|
-
const input = value;
|
|
524
|
-
const edit = {};
|
|
525
|
-
for (const key of Object.keys(input)) if (![
|
|
526
|
-
"title",
|
|
527
|
-
"kind",
|
|
528
|
-
"scope",
|
|
529
|
-
"knownBy",
|
|
530
|
-
"summary",
|
|
531
|
-
"body",
|
|
532
|
-
"startsAt",
|
|
533
|
-
"endsAt",
|
|
534
|
-
"sources"
|
|
535
|
-
].includes(key)) problems.push(`${key} is not something that can be edited`);
|
|
536
|
-
for (const key of [
|
|
537
|
-
"title",
|
|
538
|
-
"summary",
|
|
539
|
-
"body"
|
|
540
|
-
]) if (key in input) {
|
|
541
|
-
const found = input[key];
|
|
542
|
-
if (typeof found !== "string" || found.length > TEXT_LIMIT) problems.push(`${key} must be text`);
|
|
543
|
-
else if (key === "title" && found.trim() === "") problems.push("title cannot be blank");
|
|
544
|
-
else edit[key] = key === "title" ? found.trim() : found;
|
|
545
|
-
}
|
|
546
|
-
if ("kind" in input) {
|
|
547
|
-
if (typeof input["kind"] !== "string" || !LORE_KINDS.includes(input["kind"])) problems.push(`kind must be one of: ${LORE_KINDS.join(", ")}`);
|
|
548
|
-
else edit.kind = input["kind"];
|
|
549
|
-
}
|
|
550
|
-
if ("scope" in input) {
|
|
551
|
-
if (typeof input["scope"] !== "string" || !LORE_SCOPES.includes(input["scope"])) problems.push(`scope must be one of: ${LORE_SCOPES.join(", ")}`);
|
|
552
|
-
else edit.scope = input["scope"];
|
|
553
|
-
}
|
|
554
|
-
if ("knownBy" in input) {
|
|
555
|
-
const found = input["knownBy"];
|
|
556
|
-
if (!Array.isArray(found) || !found.every((entry) => typeof entry === "string" && entry.trim() !== "" && entry.length <= TEXT_LIMIT)) problems.push("knownBy must be a list of names");
|
|
557
|
-
else edit.knownBy = found.map((entry) => entry.trim());
|
|
558
|
-
}
|
|
559
|
-
if (edit.scope === "everyone" && (edit.knownBy?.length ?? 0) > 0) problems.push("an entry everyone knows has no list of who knows it");
|
|
560
|
-
for (const key of ["startsAt", "endsAt"]) if (key in input) {
|
|
561
|
-
const found = input[key];
|
|
562
|
-
if (found !== null && (typeof found !== "number" || !Number.isFinite(found))) problems.push(`${key} must be a timestamp or null`);
|
|
563
|
-
else edit[key] = found;
|
|
564
|
-
}
|
|
565
|
-
if ("sources" in input) {
|
|
566
|
-
const found = input["sources"];
|
|
567
|
-
if (!Array.isArray(found) || !found.every((entry) => typeof entry === "string" && entry.length <= TEXT_LIMIT)) problems.push("sources must be a list of texts");
|
|
568
|
-
else edit.sources = found;
|
|
569
|
-
}
|
|
570
|
-
const starts = edit.startsAt === null ? void 0 : edit.startsAt;
|
|
571
|
-
const ends = edit.endsAt === null ? void 0 : edit.endsAt;
|
|
572
|
-
if (starts !== void 0 && ends !== void 0 && ends < starts) problems.push("endsAt must not be before startsAt");
|
|
573
|
-
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
574
|
-
return edit;
|
|
575
|
-
}
|
|
576
|
-
function parseRelationEdit(value) {
|
|
577
|
-
const problems = [];
|
|
578
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new InvalidEditProblems(["the edit must be an object"]);
|
|
579
|
-
const input = value;
|
|
580
|
-
const kind = input["kind"];
|
|
581
|
-
const key = input["key"];
|
|
582
|
-
if (![
|
|
583
|
-
"character",
|
|
584
|
-
"knowledge",
|
|
585
|
-
"membership",
|
|
586
|
-
"social",
|
|
587
|
-
"exit"
|
|
588
|
-
].includes(String(kind))) problems.push("kind is not a relation kind");
|
|
589
|
-
const creating = input["create"] === true;
|
|
590
|
-
if (!creating && (typeof key !== "string" || key.trim() === "" || key.length > 500)) problems.push("key is needed");
|
|
591
|
-
if (!creating && typeof key === "string" && key.trim() !== "" && typeof input["from"] === "string" && typeof input["to"] === "string" && input["remove"] !== true) problems.push("to make a new relationship send create: true with from and to and no key; a key names one that exists, exactly as list_relationships shows it");
|
|
592
|
-
if ("create" in input && typeof input["create"] !== "boolean") problems.push("create must be true or false");
|
|
593
|
-
const allowedByKind = {
|
|
594
|
-
character: [
|
|
595
|
-
"kind",
|
|
596
|
-
"key",
|
|
597
|
-
"create",
|
|
598
|
-
"from",
|
|
599
|
-
"to",
|
|
600
|
-
"remove",
|
|
601
|
-
"label",
|
|
602
|
-
"summary",
|
|
603
|
-
"familiarity",
|
|
604
|
-
"trust",
|
|
605
|
-
"affinity",
|
|
606
|
-
"tension"
|
|
607
|
-
],
|
|
608
|
-
knowledge: [
|
|
609
|
-
"kind",
|
|
610
|
-
"key",
|
|
611
|
-
"create",
|
|
612
|
-
"from",
|
|
613
|
-
"toKind",
|
|
614
|
-
"to",
|
|
615
|
-
"observed",
|
|
616
|
-
"remove"
|
|
617
|
-
],
|
|
618
|
-
membership: [
|
|
619
|
-
"kind",
|
|
620
|
-
"key",
|
|
621
|
-
"create",
|
|
622
|
-
"from",
|
|
623
|
-
"to",
|
|
624
|
-
"remove",
|
|
625
|
-
"role",
|
|
626
|
-
"rank",
|
|
627
|
-
"status",
|
|
628
|
-
"public",
|
|
629
|
-
"commitment"
|
|
630
|
-
],
|
|
631
|
-
social: [
|
|
632
|
-
"kind",
|
|
633
|
-
"key",
|
|
634
|
-
"create",
|
|
635
|
-
"fromKind",
|
|
636
|
-
"from",
|
|
637
|
-
"toKind",
|
|
638
|
-
"to",
|
|
639
|
-
"remove",
|
|
640
|
-
"label",
|
|
641
|
-
"summary",
|
|
642
|
-
"familiarity",
|
|
643
|
-
"trust",
|
|
644
|
-
"affinity",
|
|
645
|
-
"tension"
|
|
646
|
-
],
|
|
647
|
-
exit: [
|
|
648
|
-
"kind",
|
|
649
|
-
"key",
|
|
650
|
-
"create",
|
|
651
|
-
"from",
|
|
652
|
-
"to",
|
|
653
|
-
"remove",
|
|
654
|
-
"description",
|
|
655
|
-
"travelSeconds",
|
|
656
|
-
"state",
|
|
657
|
-
"note"
|
|
658
|
-
]
|
|
659
|
-
};
|
|
660
|
-
for (const field of Object.keys(input)) if (!(allowedByKind[String(kind)] ?? []).includes(field)) problems.push(`${field} is not something that can be edited`);
|
|
661
|
-
if ("remove" in input && typeof input["remove"] !== "boolean") problems.push("remove must be true or false");
|
|
662
|
-
for (const field of [
|
|
663
|
-
"label",
|
|
664
|
-
"summary",
|
|
665
|
-
"role",
|
|
666
|
-
"rank",
|
|
667
|
-
"description",
|
|
668
|
-
"note"
|
|
669
|
-
]) if (field in input && (typeof input[field] !== "string" || input[field].length > TEXT_LIMIT)) problems.push(`${field} must be text`);
|
|
670
|
-
for (const field of [
|
|
671
|
-
"familiarity",
|
|
672
|
-
"trust",
|
|
673
|
-
"affinity",
|
|
674
|
-
"tension",
|
|
675
|
-
"commitment"
|
|
676
|
-
]) if (field in input) {
|
|
677
|
-
const number = input[field];
|
|
678
|
-
const signed = field === "trust" || field === "affinity";
|
|
679
|
-
if (typeof number !== "number" || !Number.isFinite(number) || number < (signed ? -1 : 0) || number > 1) problems.push(`${field} must be between ${signed ? "-1" : "0"} and 1`);
|
|
680
|
-
}
|
|
681
|
-
if ("travelSeconds" in input && input["travelSeconds"] !== null && (typeof input["travelSeconds"] !== "number" || !Number.isInteger(input["travelSeconds"]) || input["travelSeconds"] < 0)) problems.push("travelSeconds must be a non-negative whole number or null");
|
|
682
|
-
if ("public" in input && typeof input["public"] !== "boolean") problems.push("public must be true or false");
|
|
683
|
-
if ("observed" in input && typeof input["observed"] !== "boolean") problems.push("observed must be true or false");
|
|
684
|
-
if ("status" in input) {
|
|
685
|
-
const choices = kind === "exit" ? EXIT_STATES : MEMBERSHIP_STATUSES;
|
|
686
|
-
if (typeof input["status"] !== "string" || !choices.includes(input["status"])) problems.push(`status must be one of: ${choices.join(", ")}`);
|
|
687
|
-
}
|
|
688
|
-
if (creating) {
|
|
689
|
-
for (const field of ["from", "to"]) if (typeof input[field] !== "string" || input[field].trim() === "") problems.push(`${field} is needed: the key of the entity, written as plain text and not as an object`);
|
|
690
|
-
if ((kind === "character" || kind === "social") && (typeof input["label"] !== "string" || input["label"].trim() === "")) problems.push("label is needed");
|
|
691
|
-
if (kind === "membership" && (typeof input["role"] !== "string" || input["role"].trim() === "")) problems.push("role is needed");
|
|
692
|
-
if (kind === "exit" && typeof input["description"] !== "string") problems.push("description is needed");
|
|
693
|
-
if (kind === "knowledge" && ![
|
|
694
|
-
"location",
|
|
695
|
-
"item",
|
|
696
|
-
"exit"
|
|
697
|
-
].includes(String(input["toKind"]))) problems.push("toKind must be one of: location, item, exit");
|
|
698
|
-
if (kind === "social") {
|
|
699
|
-
for (const field of ["fromKind", "toKind"]) if (!["character", "faction"].includes(String(input[field]))) problems.push(`${field} must be character or faction`);
|
|
700
|
-
}
|
|
701
|
-
} else if (kind === "knowledge" && input["remove"] !== true) problems.push("knowledge editing supports creation or removal");
|
|
702
|
-
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
703
|
-
return input;
|
|
704
|
-
}
|
|
705
|
-
// src/host/folders.ts
|
|
706
|
-
/**
|
|
707
|
-
* Names of the subfolders of `dir`, sorted. Symbolic links are not followed
|
|
708
|
-
* and a missing directory has none; any other failure is thrown.
|
|
709
|
-
*/
|
|
710
|
-
async function subfolders(dir) {
|
|
711
|
-
try {
|
|
712
|
-
return (await readdir(dir, { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name).sort((a, b) => a.localeCompare(b));
|
|
713
|
-
} catch (error) {
|
|
714
|
-
if (error.code === "ENOENT") return [];
|
|
715
|
-
throw error;
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
166
|
/**
|
|
719
167
|
* Read an `id` field. Ids are opaque strings, unique within their scope, that
|
|
720
168
|
* stay the same when the file is copied or edited.
|
|
@@ -1380,514 +828,987 @@ function parseLoreFile(text, fallbackId) {
|
|
|
1380
828
|
issues
|
|
1381
829
|
};
|
|
1382
830
|
}
|
|
1383
|
-
// src/host/
|
|
1384
|
-
const
|
|
1385
|
-
|
|
1386
|
-
const
|
|
1387
|
-
const
|
|
1388
|
-
|
|
1389
|
-
|
|
831
|
+
// src/host/variables/load.ts
|
|
832
|
+
const VARIABLES_FILE_NAME = "variables.json";
|
|
833
|
+
function readDefinition(reader) {
|
|
834
|
+
const key = reader.string("key", { required: true });
|
|
835
|
+
const name = reader.string("name", { required: true });
|
|
836
|
+
const kind = reader.oneOf("kind", VARIABLE_KINDS, "number");
|
|
837
|
+
const subject = reader.oneOf("subject", VARIABLE_SUBJECTS, "character");
|
|
838
|
+
const visibility = reader.oneOf("visibility", VARIABLE_VISIBILITIES, "public");
|
|
839
|
+
const min = reader.number("min");
|
|
840
|
+
const max = reader.number("max");
|
|
841
|
+
const defaultValue = reader.raw("default");
|
|
842
|
+
if (defaultValue !== void 0 && typeof defaultValue !== "number" && typeof defaultValue !== "boolean" && typeof defaultValue !== "string") reader.fail("default", "must be a number, true or false, or text");
|
|
843
|
+
const flag = (field) => {
|
|
844
|
+
const value = reader.raw(field);
|
|
845
|
+
if (value !== void 0 && typeof value !== "boolean") reader.fail(field, "must be true or false");
|
|
846
|
+
return typeof value === "boolean" ? value : void 0;
|
|
847
|
+
};
|
|
848
|
+
const integer = flag("integer");
|
|
849
|
+
const baseline = flag("baseline");
|
|
850
|
+
const always = flag("always");
|
|
851
|
+
const bands = reader.objects("bands").flatMap((band) => {
|
|
852
|
+
const upTo = band.number("up_to", {});
|
|
853
|
+
const label = band.string("label", { required: true });
|
|
854
|
+
return upTo === void 0 || label === void 0 ? [] : [{
|
|
855
|
+
upTo,
|
|
856
|
+
label
|
|
857
|
+
}];
|
|
858
|
+
});
|
|
859
|
+
if (key === void 0 || name === void 0) return void 0;
|
|
860
|
+
const only = reader.strings("only");
|
|
861
|
+
const options = reader.strings("options");
|
|
862
|
+
return {
|
|
863
|
+
key,
|
|
864
|
+
name,
|
|
865
|
+
kind,
|
|
866
|
+
subject,
|
|
867
|
+
visibility,
|
|
868
|
+
...reader.raw("description") === void 0 ? {} : { description: reader.text("description") },
|
|
869
|
+
...reader.raw("update") === void 0 ? {} : { update: reader.text("update") },
|
|
870
|
+
only,
|
|
871
|
+
options,
|
|
872
|
+
bands,
|
|
873
|
+
...min === void 0 ? {} : { min },
|
|
874
|
+
...max === void 0 ? {} : { max },
|
|
875
|
+
...integer === void 0 ? {} : { integer },
|
|
876
|
+
...baseline === void 0 ? {} : { baseline },
|
|
877
|
+
...always === void 0 ? {} : { always },
|
|
878
|
+
...defaultValue === void 0 || typeof defaultValue === "number" && !Number.isFinite(defaultValue) ? {} : { default: defaultValue }
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
/** Read `variables.json`: a list of what the world tracks. */
|
|
882
|
+
function parseVariablesFile(text) {
|
|
883
|
+
const issues = [];
|
|
884
|
+
let data;
|
|
885
|
+
try {
|
|
886
|
+
data = JSON.parse(text.replace(/^/, ""));
|
|
887
|
+
} catch (error) {
|
|
888
|
+
return {
|
|
889
|
+
ok: false,
|
|
890
|
+
issues: [{
|
|
891
|
+
path: "",
|
|
892
|
+
message: `invalid JSON: ${error instanceof Error ? error.message : String(error)}`
|
|
893
|
+
}]
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
if (!Array.isArray(data)) return {
|
|
897
|
+
ok: false,
|
|
898
|
+
issues: [{
|
|
899
|
+
path: "",
|
|
900
|
+
message: "expected a list of variables"
|
|
901
|
+
}]
|
|
902
|
+
};
|
|
903
|
+
const reader = Reader.fromJson(JSON.stringify({ variables: data }), issues);
|
|
904
|
+
if (reader === void 0) return {
|
|
905
|
+
ok: false,
|
|
906
|
+
issues
|
|
907
|
+
};
|
|
908
|
+
const defs = reader.objects("variables").flatMap((entry) => readDefinition(entry) ?? []);
|
|
909
|
+
return issues.length > 0 ? {
|
|
910
|
+
ok: false,
|
|
911
|
+
issues: issues.map((issue) => ({
|
|
912
|
+
...issue,
|
|
913
|
+
path: issue.path.replace(/^variables/, "")
|
|
914
|
+
}))
|
|
915
|
+
} : {
|
|
916
|
+
ok: true,
|
|
917
|
+
value: defs
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
/** Read the world's own variables: an optional `<world>/variables.json`. A world with none tracks only what every story does. */
|
|
921
|
+
async function loadVariables(worldDir) {
|
|
922
|
+
const result = {
|
|
923
|
+
defs: [],
|
|
924
|
+
problems: []
|
|
925
|
+
};
|
|
926
|
+
const file = join(worldDir, VARIABLES_FILE_NAME);
|
|
927
|
+
try {
|
|
928
|
+
result.defs = await readDocument(file, parseVariablesFile, result.problems) ?? [];
|
|
929
|
+
} catch (error) {
|
|
930
|
+
if (error.code !== "ENOENT") result.problems.push({
|
|
931
|
+
path: file,
|
|
932
|
+
message: errorMessage(error)
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
return result;
|
|
936
|
+
}
|
|
937
|
+
/**
|
|
938
|
+
* What is wrong with the variables of a world and the values it starts with, found before anything is created: a
|
|
939
|
+
* declaration that cannot be one, a repeated key, a subject a variable is limited to that does not exist, a value for a
|
|
940
|
+
* variable that is not declared, does not apply to what has it, or is not one it can have.
|
|
941
|
+
*/
|
|
942
|
+
function variableProblems(world) {
|
|
943
|
+
const problems = [];
|
|
944
|
+
const scratch = createSessionStore(":memory:", {
|
|
945
|
+
id: "scratch",
|
|
946
|
+
name: "scratch",
|
|
947
|
+
world: {
|
|
948
|
+
id: "scratch",
|
|
949
|
+
name: "scratch"
|
|
950
|
+
}
|
|
951
|
+
}, { vectors: false });
|
|
952
|
+
try {
|
|
953
|
+
const seen = /* @__PURE__ */ new Set();
|
|
954
|
+
world.defs.forEach((def, index) => {
|
|
955
|
+
if (seen.has(def.key)) problems.push(`variables.json[${index}]: "${def.key}" is declared twice`);
|
|
956
|
+
seen.add(def.key);
|
|
957
|
+
try {
|
|
958
|
+
scratch.variables.define(def);
|
|
959
|
+
} catch (error) {
|
|
960
|
+
problems.push(`variables.json[${index}]: ${errorMessage(error)}`);
|
|
961
|
+
}
|
|
962
|
+
});
|
|
963
|
+
const keys = {
|
|
964
|
+
character: new Set(world.characters.map((entry) => entry.id)),
|
|
965
|
+
item: new Set(world.items.map((entry) => entry.id)),
|
|
966
|
+
location: new Set(world.locations.map((entry) => entry.id))
|
|
967
|
+
};
|
|
968
|
+
for (const def of scratch.variables.defs()) for (const key of def.only) if (def.subject !== "world" && !keys[def.subject].has(key)) problems.push(`variables.json: ${def.key}: "${key}" is not a ${def.subject} of this world`);
|
|
969
|
+
const check = (kind, who, values) => {
|
|
970
|
+
for (const [key, value] of Object.entries(values ?? {})) {
|
|
971
|
+
const def = scratch.variables.def(key);
|
|
972
|
+
if (def === void 0) {
|
|
973
|
+
problems.push(`${who}: variables.${key}: no such variable is declared (variables.json)`);
|
|
974
|
+
continue;
|
|
975
|
+
}
|
|
976
|
+
if (def.subject !== kind) {
|
|
977
|
+
problems.push(`${who}: variables.${key}: ${def.name} is about ${def.subject === "world" ? "the world" : `a ${def.subject}`}, not ${kind === "world" ? "the world" : `a ${kind}`}`);
|
|
978
|
+
continue;
|
|
979
|
+
}
|
|
980
|
+
if (kind !== "world" && def.only.length > 0 && !def.only.includes(who.split(": ")[0])) problems.push(`${who}: variables.${key}: ${def.name} does not apply to it`);
|
|
981
|
+
const issue = valueIssue(def, value);
|
|
982
|
+
if (issue !== void 0) problems.push(`${who}: variables.${key}: ${issue}`);
|
|
983
|
+
}
|
|
984
|
+
};
|
|
985
|
+
for (const entry of world.characters) check("character", `${entry.id}: character`, entry.variables);
|
|
986
|
+
for (const entry of world.items) check("item", `${entry.id}: item`, entry.variables);
|
|
987
|
+
for (const entry of world.locations) check("location", `${entry.id}: place`, entry.variables);
|
|
988
|
+
check("world", "world.json", world.values);
|
|
989
|
+
} finally {
|
|
990
|
+
scratch.close();
|
|
991
|
+
}
|
|
992
|
+
return problems;
|
|
993
|
+
}
|
|
994
|
+
// src/host/import/dialect.ts
|
|
995
|
+
const MAX_NAMES = 8;
|
|
996
|
+
const PLAIN_MACROS = /* @__PURE__ */ new Set([
|
|
997
|
+
"user",
|
|
998
|
+
"char",
|
|
999
|
+
"bot"
|
|
1000
|
+
]);
|
|
1001
|
+
const MARKUP = /* @__PURE__ */ new Set([
|
|
1002
|
+
"a",
|
|
1003
|
+
"b",
|
|
1004
|
+
"i",
|
|
1005
|
+
"u",
|
|
1006
|
+
"s",
|
|
1007
|
+
"p",
|
|
1008
|
+
"br",
|
|
1009
|
+
"hr",
|
|
1010
|
+
"em",
|
|
1011
|
+
"strong",
|
|
1012
|
+
"span",
|
|
1013
|
+
"div",
|
|
1014
|
+
"code",
|
|
1015
|
+
"pre",
|
|
1016
|
+
"center",
|
|
1017
|
+
"small",
|
|
1018
|
+
"img",
|
|
1019
|
+
"ul",
|
|
1020
|
+
"ol",
|
|
1021
|
+
"li",
|
|
1022
|
+
"blockquote",
|
|
1023
|
+
"details",
|
|
1024
|
+
"summary",
|
|
1025
|
+
"font",
|
|
1026
|
+
"sub",
|
|
1027
|
+
"sup",
|
|
1028
|
+
"h1",
|
|
1029
|
+
"h2",
|
|
1030
|
+
"h3",
|
|
1031
|
+
"table",
|
|
1032
|
+
"tr",
|
|
1033
|
+
"td",
|
|
1034
|
+
"th"
|
|
1035
|
+
]);
|
|
1036
|
+
const MACRO = /\{\{\s*([\p{L}_][\p{L}\p{N}_]*)/gu;
|
|
1037
|
+
const BLOCK = /<([\p{L}][\p{L}\p{N}_-]{1,30})(?:\s[^>]*)?>[\s\S]*?<\/\1\s*>/gu;
|
|
1038
|
+
function textsOf(card) {
|
|
1039
|
+
return [
|
|
1040
|
+
card.description,
|
|
1041
|
+
card.personality,
|
|
1042
|
+
card.scenario,
|
|
1043
|
+
card.firstMessage,
|
|
1044
|
+
...card.alternateGreetings,
|
|
1045
|
+
card.messageExample,
|
|
1046
|
+
card.systemPrompt,
|
|
1047
|
+
card.postHistoryInstructions,
|
|
1048
|
+
...(card.book?.entries ?? []).map((entry) => entry.content)
|
|
1049
|
+
];
|
|
1050
|
+
}
|
|
1051
|
+
/** The distinct values of a pattern's first group over some texts, in the order met, lowercased and cut to a few. */
|
|
1052
|
+
function namesIn(texts, pattern, skip) {
|
|
1053
|
+
const found = /* @__PURE__ */ new Set();
|
|
1054
|
+
for (const text of texts) for (const match of text.matchAll(pattern)) {
|
|
1055
|
+
const name = match[1].toLowerCase();
|
|
1056
|
+
if (!skip(name)) found.add(name);
|
|
1057
|
+
}
|
|
1058
|
+
return [...found].slice(0, MAX_NAMES);
|
|
1059
|
+
}
|
|
1060
|
+
/** Reads the machinery of the source tool out of a card: macros, tag blocks and the scripts the reader counted. */
|
|
1061
|
+
function dialectOf(card) {
|
|
1062
|
+
const texts = textsOf(card);
|
|
1063
|
+
return {
|
|
1064
|
+
macros: namesIn(texts, MACRO, (name) => PLAIN_MACROS.has(name)),
|
|
1065
|
+
tags: namesIn(texts, BLOCK, (name) => MARKUP.has(name)),
|
|
1066
|
+
regexScripts: card.ignored.regexScripts ?? 0,
|
|
1067
|
+
helperScripts: card.ignored.helperScripts ?? card.ignored.scripts - (card.ignored.regexScripts ?? 0)
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
/** What the person is told about a card before the import starts: what it is, and which of its machinery is left out. */
|
|
1071
|
+
function sourceSummary(card, language) {
|
|
1072
|
+
const tr = translator(languageOf$1(language));
|
|
1073
|
+
const dialect = dialectOf(card);
|
|
1074
|
+
const lines = [tr("The source is a {format} card with {entries} lorebook entries and {greetings} alternate openings.", {
|
|
1075
|
+
format: card.sourceSpec === "chara_card_v2" ? "V2" : "V3",
|
|
1076
|
+
entries: String(card.book?.entries.length ?? 0),
|
|
1077
|
+
greetings: String(card.alternateGreetings.length)
|
|
1078
|
+
})];
|
|
1079
|
+
const left = [
|
|
1080
|
+
...dialect.regexScripts > 0 ? [tr("{count} regular expressions", { count: String(dialect.regexScripts) })] : [],
|
|
1081
|
+
...dialect.helperScripts > 0 ? [tr("{count} helper scripts", { count: String(dialect.helperScripts) })] : [],
|
|
1082
|
+
...dialect.macros.length > 0 ? [tr("macros ({names})", { names: dialect.macros.join(", ") })] : [],
|
|
1083
|
+
...dialect.tags.length > 0 ? [tr("tag blocks ({names})", { names: dialect.tags.join(", ") })] : []
|
|
1084
|
+
];
|
|
1085
|
+
if (left.length > 0) lines.push(tr("Nothing runs them, so they are not carried: {parts}. Their effect is kept only where the card also says it in words.", { parts: left.join("; ") }));
|
|
1086
|
+
return lines.join(" ");
|
|
1087
|
+
}
|
|
1088
|
+
// src/host/import/panels.ts
|
|
1089
|
+
const FENCE = /```[^\n]*\n[\s\S]*?```/g;
|
|
1090
|
+
const TAG = /<(\/?)([\p{L}][\p{L}\p{N}_-]*)(?=[\s>/])[^>]*>/gu;
|
|
1091
|
+
const BLOCKS = /* @__PURE__ */ new Set([
|
|
1092
|
+
"div",
|
|
1093
|
+
"table",
|
|
1094
|
+
"details",
|
|
1095
|
+
"pre",
|
|
1096
|
+
"center",
|
|
1097
|
+
"section",
|
|
1098
|
+
"aside",
|
|
1099
|
+
"fieldset"
|
|
1100
|
+
]);
|
|
1101
|
+
const SEPARATORS = /\s[||│]\s|[||│]\s*\S+\s*[||│]/;
|
|
1102
|
+
const HEADER = /(?:^|\n)[ \t]*(?:【[^】\n]{1,30}】|\[[^\]\n]{1,30}\]|#+[ \t]*[^\n]{1,30})[ \t]*\n?\s*$/;
|
|
1103
|
+
const MAX_HTML_TEXT_CHARS = 600;
|
|
1104
|
+
const MAX_CUSTOM_CHARS = 4e3;
|
|
1105
|
+
/** A block of tags of a card's own invention (not markup) counts as a panel from this many tags: two or three tags are a form of address or an aside, a layout is more. */
|
|
1106
|
+
const MIN_CUSTOM_TAGS = 6;
|
|
1107
|
+
const MAX_PANELS = 6;
|
|
1108
|
+
const collapse = (text) => text.replace(/\n{3,}/g, "\n\n").trim();
|
|
1109
|
+
const plain = (html) => html.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
|
|
1110
|
+
/** The outermost elements of block markup and of invented tags in a text, balanced by their own names, with neighbours that only whitespace separates joined into one. */
|
|
1111
|
+
function tagBlocks(text) {
|
|
1112
|
+
const found = [];
|
|
1113
|
+
const open = [];
|
|
1114
|
+
let current = {
|
|
1115
|
+
from: 0,
|
|
1116
|
+
to: 0,
|
|
1117
|
+
custom: false,
|
|
1118
|
+
tags: 0
|
|
1390
1119
|
};
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
if (
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
continue;
|
|
1120
|
+
const closed = new Set([...text.matchAll(TAG)].filter((match) => match[1] === "/").map((match) => match[2].toLowerCase()));
|
|
1121
|
+
for (const match of text.matchAll(TAG)) {
|
|
1122
|
+
const name = match[2].toLowerCase();
|
|
1123
|
+
const custom = !MARKUP.has(name);
|
|
1124
|
+
if (!custom && !BLOCKS.has(name)) continue;
|
|
1125
|
+
if (match[1] === "" && !closed.has(name)) continue;
|
|
1126
|
+
if (match[1] === "") {
|
|
1127
|
+
if (open.length === 0) current = {
|
|
1128
|
+
from: match.index,
|
|
1129
|
+
to: 0,
|
|
1130
|
+
custom: false,
|
|
1131
|
+
tags: 0
|
|
1132
|
+
};
|
|
1133
|
+
open.push(name);
|
|
1134
|
+
current.tags += 1;
|
|
1135
|
+
current.custom ||= custom;
|
|
1136
|
+
} else if (open.includes(name)) {
|
|
1137
|
+
while (open.pop() !== name);
|
|
1138
|
+
current.tags += 1;
|
|
1139
|
+
if (open.length === 0) {
|
|
1140
|
+
current.to = match.index + match[0].length;
|
|
1141
|
+
found.push(current);
|
|
1414
1142
|
}
|
|
1415
|
-
owners.set(entry.id, folder);
|
|
1416
|
-
result.lore.push(entry);
|
|
1417
|
-
} catch (error) {
|
|
1418
|
-
if (error.code !== "ENOENT") result.problems.push({
|
|
1419
|
-
path: file,
|
|
1420
|
-
message: errorMessage(error)
|
|
1421
|
-
});
|
|
1422
1143
|
}
|
|
1423
1144
|
}
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1145
|
+
const joined = [];
|
|
1146
|
+
for (const block of found) {
|
|
1147
|
+
const last = joined.at(-1);
|
|
1148
|
+
if (last !== void 0 && text.slice(last.to, block.from).trim() === "") {
|
|
1149
|
+
last.to = block.to;
|
|
1150
|
+
last.custom ||= block.custom;
|
|
1151
|
+
last.tags += block.tags;
|
|
1152
|
+
} else joined.push({ ...block });
|
|
1153
|
+
}
|
|
1154
|
+
return joined.map((block) => {
|
|
1155
|
+
const header = block.custom ? HEADER.exec(text.slice(0, block.from)) : null;
|
|
1156
|
+
return header === null ? block : {
|
|
1157
|
+
...block,
|
|
1158
|
+
from: header.index + (header[0].startsWith("\n") ? 1 : 0)
|
|
1159
|
+
};
|
|
1427
1160
|
});
|
|
1428
|
-
return result;
|
|
1429
1161
|
}
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
const
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
label
|
|
1456
|
-
}];
|
|
1162
|
+
const statusLine = (line) => SEPARATORS.test(line) && /\d/.test(line);
|
|
1163
|
+
/** An update call of a state-tracking framework: a block that holds one is a panel however few tags it has. */
|
|
1164
|
+
const CALL = /\b_\.\w+\(|\bstat_data\b|\bJSONPatch\b/;
|
|
1165
|
+
/** The text before a removed panel without its last line when that line only introduces the panel ("end every reply with this:"). */
|
|
1166
|
+
const withoutLeadIn = (before) => before.replace(/(?:^|\n)[^\n]*[::][ \t]*\n?\s*$/, "\n");
|
|
1167
|
+
function fencesWithoutLeadIn(text, panels) {
|
|
1168
|
+
let out = "";
|
|
1169
|
+
let last = 0;
|
|
1170
|
+
for (const match of text.matchAll(FENCE)) {
|
|
1171
|
+
panels.push(match[0].replace(/^```[^\n]*\n|```$/g, "").trim());
|
|
1172
|
+
out += `${withoutLeadIn(text.slice(last, match.index))}\n\n`;
|
|
1173
|
+
last = match.index + match[0].length;
|
|
1174
|
+
}
|
|
1175
|
+
return out + text.slice(last);
|
|
1176
|
+
}
|
|
1177
|
+
/**
|
|
1178
|
+
* Takes the status bars out of a message: fenced code blocks, blocks of tags of the card's own invention (a layout of nested tags, which names may be in any language), short blocks
|
|
1179
|
+
* of markup that carry numbers (when the message has story text besides them: a message wrapped whole in a block is only styled), and closing lines of values set apart by bars.
|
|
1180
|
+
* What is left is the story; the panels are what the card shows its state with.
|
|
1181
|
+
*/
|
|
1182
|
+
function splitPanels(text, options = {}) {
|
|
1183
|
+
const panels = [];
|
|
1184
|
+
let rest = options.leadIn === true ? fencesWithoutLeadIn(text, panels) : text.replace(FENCE, (block) => {
|
|
1185
|
+
panels.push(block.replace(/^```[^\n]*\n|```$/g, "").trim());
|
|
1186
|
+
return "\n\n";
|
|
1457
1187
|
});
|
|
1458
|
-
|
|
1459
|
-
const
|
|
1460
|
-
|
|
1188
|
+
const blocks = tagBlocks(rest);
|
|
1189
|
+
const outside = blocks.reduceRight((left, { from, to }) => left.slice(0, from) + left.slice(to), rest);
|
|
1190
|
+
if (blocks.length > 0 && (options.alone === true || plain(outside) !== "")) for (const block of [...blocks].reverse()) {
|
|
1191
|
+
const body = rest.slice(block.from, block.to);
|
|
1192
|
+
if (!(block.custom ? (block.tags >= MIN_CUSTOM_TAGS || CALL.test(body)) && body.length <= MAX_CUSTOM_CHARS : plain(body).length <= MAX_HTML_TEXT_CHARS && /\d/.test(body))) continue;
|
|
1193
|
+
panels.unshift(body.trim());
|
|
1194
|
+
rest = (options.leadIn === true ? withoutLeadIn(rest.slice(0, block.from)) : rest.slice(0, block.from)) + "\n\n" + rest.slice(block.to);
|
|
1195
|
+
}
|
|
1196
|
+
const paragraphs = collapse(rest).split(/\n{2,}/);
|
|
1197
|
+
while (paragraphs.length > 1 && paragraphs.at(-1).split("\n").every(statusLine)) panels.push(paragraphs.pop().trim());
|
|
1461
1198
|
return {
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1199
|
+
prose: collapse(paragraphs.join("\n\n")),
|
|
1200
|
+
panels: panels.filter((panel) => panel !== "")
|
|
1201
|
+
};
|
|
1202
|
+
}
|
|
1203
|
+
/** The distinct panels in a card's own messages (openings and examples): they show which values it keeps and where they start. */
|
|
1204
|
+
function panelsOfCard(card) {
|
|
1205
|
+
const texts = [
|
|
1206
|
+
card.firstMessage,
|
|
1207
|
+
...card.alternateGreetings,
|
|
1208
|
+
card.messageExample
|
|
1209
|
+
];
|
|
1210
|
+
return [...new Set(texts.flatMap((text) => splitPanels(text).panels))].filter((panel) => panel.length <= MAX_CUSTOM_CHARS).slice(0, MAX_PANELS);
|
|
1211
|
+
}
|
|
1212
|
+
/** The sentences of one line; a full stop inside a word or a call (`_.set(`) does not end one. */
|
|
1213
|
+
const sentencesOf = (line) => line.match(/(?:[^.!?。!?]|[.!?](?!\s|$))+(?:[.!?]+(?=\s|$)|[。!?]+|$)\s*/gu) ?? [line];
|
|
1214
|
+
const MIN_LEFT_OUT_WORDS = 3;
|
|
1215
|
+
const ECHO_SHARE = .6;
|
|
1216
|
+
/** A short sentence right after a dropped one ("Never skip it.") has nothing left to refer to. */
|
|
1217
|
+
const MAX_TAIL_WORDS = 4;
|
|
1218
|
+
function wordsOf(text) {
|
|
1219
|
+
const words = /* @__PURE__ */ new Set();
|
|
1220
|
+
for (const word of text.toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? []) if (/[㐀-鿿]/.test(word)) for (let index = 0; index < word.length - 1; index += 1) words.add(word.slice(index, index + 2));
|
|
1221
|
+
else if (word.length >= 3) words.add(word);
|
|
1222
|
+
return words;
|
|
1223
|
+
}
|
|
1224
|
+
/**
|
|
1225
|
+
* The sentences of a prose prompt that repeat something the same answer lists as left out. A model that is told to drop an instruction about the shape of the reply (a status bar after
|
|
1226
|
+
* every message) tends to list it and keep it in the same breath; what it listed is not carried.
|
|
1227
|
+
*/
|
|
1228
|
+
function withoutLeftOut(prose, leftOut) {
|
|
1229
|
+
const dropped = leftOut.map(wordsOf).filter((words) => words.size >= MIN_LEFT_OUT_WORDS);
|
|
1230
|
+
if (dropped.length === 0) return prose;
|
|
1231
|
+
const echoes = (sentence) => {
|
|
1232
|
+
const words = wordsOf(sentence);
|
|
1233
|
+
return dropped.some((left) => [...left].filter((word) => words.has(word)).length >= left.size * ECHO_SHARE);
|
|
1234
|
+
};
|
|
1235
|
+
const kept = (line) => {
|
|
1236
|
+
let after = false;
|
|
1237
|
+
return sentencesOf(line).filter((sentence) => {
|
|
1238
|
+
const echo = echoes(sentence);
|
|
1239
|
+
const tail = after && sentence.trim().split(/\s+/).length <= MAX_TAIL_WORDS;
|
|
1240
|
+
after = echo;
|
|
1241
|
+
return !echo && !tail;
|
|
1242
|
+
}).join("").trim();
|
|
1478
1243
|
};
|
|
1244
|
+
return prose.split("\n").map(kept).filter((line) => line !== "").join("\n");
|
|
1245
|
+
}
|
|
1246
|
+
const OUTPUT_MARKUP = /<\/?[\p{L}][\p{L}\p{N}_-]*>|```|\b_\.\w+\(|\bstat_data\b|\{\{(?!\s*(?:user|char|bot)\s*\}\})[^{}]+\}\}/u;
|
|
1247
|
+
/** A text that is mostly a panel is a format specification, not a rule or a fact: it goes whole. */
|
|
1248
|
+
const PANEL_SHARE = .5;
|
|
1249
|
+
/** A standing rule without the panel it demands, the sentences that tell the game master to print markup, a code block or an update call after a reply, and the rule itself when the panel was most of it: the story shows its tracked values itself. */
|
|
1250
|
+
function withoutOutputMarkup(passage) {
|
|
1251
|
+
const { prose, panels } = splitPanels(passage, {
|
|
1252
|
+
alone: true,
|
|
1253
|
+
leadIn: true
|
|
1254
|
+
});
|
|
1255
|
+
if (panels.length > 0 && prose.length < passage.length * PANEL_SHARE) return "";
|
|
1256
|
+
return prose.split("\n").map((line) => sentencesOf(line).filter((sentence) => !OUTPUT_MARKUP.test(sentence)).join("").trim()).filter((line) => line !== "").join("\n");
|
|
1479
1257
|
}
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1258
|
+
const BARRED = /(?:\s[||│]\s[^||│\n]+){2,}/;
|
|
1259
|
+
/** The first status bar, panel or print-this-markup directive in a text, as written, or nothing: what a world should show with its tracked values rather than carry as text. */
|
|
1260
|
+
function findPanel(text) {
|
|
1261
|
+
const panel = splitPanels(text, { alone: true }).panels[0];
|
|
1262
|
+
if (panel !== void 0) return panel;
|
|
1263
|
+
for (const line of text.split("\n")) for (const sentence of sentencesOf(line)) if (OUTPUT_MARKUP.test(sentence) || BARRED.test(sentence) && /\d/.test(sentence)) return sentence.trim();
|
|
1264
|
+
}
|
|
1265
|
+
// src/shared/import-card.ts
|
|
1266
|
+
/** The command that imports the card waiting in a world, in the world's authoring chat. */
|
|
1267
|
+
const IMPORT_COMMAND = "/import-st";
|
|
1268
|
+
/** What follows the command's name: the card it names, then what the person asks for. The card is always the one in the world's import folder; only the request is passed on. */
|
|
1269
|
+
function importRequestOf(rawInput) {
|
|
1270
|
+
const rest = rawInput.trim();
|
|
1271
|
+
const boundary = rest.search(/\s/u);
|
|
1272
|
+
return boundary < 0 ? "" : rest.slice(boundary).trim();
|
|
1273
|
+
}
|
|
1274
|
+
// src/shared/world-state.ts
|
|
1275
|
+
/** The problems with an edit, for the person who made it; nothing was changed. */
|
|
1276
|
+
var InvalidEditProblems = class extends Error {
|
|
1277
|
+
problems;
|
|
1278
|
+
constructor(problems) {
|
|
1279
|
+
super(`the edit is not acceptable: ${problems.join("; ")}`);
|
|
1280
|
+
this.problems = problems;
|
|
1494
1281
|
}
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1282
|
+
};
|
|
1283
|
+
const TEXT_LIMIT = 2e4;
|
|
1284
|
+
const NAME_LIMIT = 200;
|
|
1285
|
+
/** Reads an edit from what the browser sent: every unknown or mistyped field is a problem. */
|
|
1286
|
+
function parseCharacterEdit(value) {
|
|
1287
|
+
const problems = [];
|
|
1288
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new InvalidEditProblems(["the edit must be an object"]);
|
|
1289
|
+
const input = value;
|
|
1290
|
+
const edit = {};
|
|
1291
|
+
const text = (source, key, path, limit) => {
|
|
1292
|
+
const found = source[key];
|
|
1293
|
+
if (found === void 0) return void 0;
|
|
1294
|
+
if (typeof found !== "string") {
|
|
1295
|
+
problems.push(`${path}${key} must be text`);
|
|
1296
|
+
return;
|
|
1297
|
+
}
|
|
1298
|
+
if (found.length > limit) {
|
|
1299
|
+
problems.push(`${path}${key} is longer than ${limit} characters`);
|
|
1300
|
+
return;
|
|
1301
|
+
}
|
|
1302
|
+
return found;
|
|
1501
1303
|
};
|
|
1502
|
-
const
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1304
|
+
const list = (source, key, path) => {
|
|
1305
|
+
const found = source[key];
|
|
1306
|
+
if (found === void 0) return void 0;
|
|
1307
|
+
if (!Array.isArray(found) || !found.every((entry) => typeof entry === "string" && entry.length <= NAME_LIMIT) || found.length > 50) {
|
|
1308
|
+
problems.push(`${path}${key} must be a list of short texts`);
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1311
|
+
return found;
|
|
1506
1312
|
};
|
|
1507
|
-
const
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
}
|
|
1514
|
-
|
|
1515
|
-
ok: true,
|
|
1516
|
-
value: defs
|
|
1313
|
+
const choice = (key, options, source = input, path = "") => {
|
|
1314
|
+
const found = source[key];
|
|
1315
|
+
if (found === void 0) return void 0;
|
|
1316
|
+
if (typeof found !== "string" || !options.includes(found)) {
|
|
1317
|
+
problems.push(`${path}${key} must be one of: ${options.join(", ")}`);
|
|
1318
|
+
return;
|
|
1319
|
+
}
|
|
1320
|
+
return found;
|
|
1517
1321
|
};
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
async function loadVariables(worldDir) {
|
|
1521
|
-
const result = {
|
|
1522
|
-
defs: [],
|
|
1523
|
-
problems: []
|
|
1322
|
+
const allowed = (source, keys, path) => {
|
|
1323
|
+
for (const key of Object.keys(source)) if (!keys.includes(key)) problems.push(`${path}${key} is not something that can be edited`);
|
|
1524
1324
|
};
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1325
|
+
allowed(input, [
|
|
1326
|
+
"name",
|
|
1327
|
+
"aliases",
|
|
1328
|
+
"age",
|
|
1329
|
+
"gender",
|
|
1330
|
+
"pronouns",
|
|
1331
|
+
"role",
|
|
1332
|
+
"tier",
|
|
1333
|
+
"kind",
|
|
1334
|
+
"summary",
|
|
1335
|
+
"personality",
|
|
1336
|
+
"background",
|
|
1337
|
+
"appearance",
|
|
1338
|
+
"voice",
|
|
1339
|
+
"visualIdentity",
|
|
1340
|
+
"speech",
|
|
1341
|
+
"state",
|
|
1342
|
+
"place",
|
|
1343
|
+
"goals"
|
|
1344
|
+
], "");
|
|
1345
|
+
const name = text(input, "name", "", NAME_LIMIT);
|
|
1346
|
+
if (name !== void 0) {
|
|
1347
|
+
if (name.trim() === "") problems.push("name cannot be blank");
|
|
1348
|
+
else edit.name = name.trim();
|
|
1533
1349
|
}
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1350
|
+
const aliases = list(input, "aliases", "");
|
|
1351
|
+
if (aliases !== void 0) edit.aliases = aliases;
|
|
1352
|
+
for (const key of [
|
|
1353
|
+
"age",
|
|
1354
|
+
"gender",
|
|
1355
|
+
"pronouns"
|
|
1356
|
+
]) {
|
|
1357
|
+
const found = text(input, key, "", NAME_LIMIT);
|
|
1358
|
+
if (found !== void 0) edit[key] = found;
|
|
1359
|
+
}
|
|
1360
|
+
const voice = text(input, "voice", "", NAME_LIMIT);
|
|
1361
|
+
if (voice !== void 0) edit.voice = voice;
|
|
1362
|
+
for (const key of [
|
|
1363
|
+
"summary",
|
|
1364
|
+
"personality",
|
|
1365
|
+
"background",
|
|
1366
|
+
"appearance"
|
|
1367
|
+
]) {
|
|
1368
|
+
const found = text(input, key, "", TEXT_LIMIT);
|
|
1369
|
+
if (found !== void 0) edit[key] = found;
|
|
1370
|
+
}
|
|
1371
|
+
const role = choice("role", CHARACTER_ROLES);
|
|
1372
|
+
if (role !== void 0) edit.role = role;
|
|
1373
|
+
const tier = choice("tier", CHARACTER_TIERS);
|
|
1374
|
+
if (tier !== void 0) edit.tier = tier;
|
|
1375
|
+
const kind = choice("kind", CHARACTER_KINDS);
|
|
1376
|
+
if (kind !== void 0) edit.kind = kind;
|
|
1377
|
+
const object = (key) => {
|
|
1378
|
+
const found = input[key];
|
|
1379
|
+
if (found === void 0) return void 0;
|
|
1380
|
+
if (typeof found !== "object" || found === null || Array.isArray(found)) {
|
|
1381
|
+
problems.push(`${key} must be an object`);
|
|
1382
|
+
return;
|
|
1549
1383
|
}
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1384
|
+
return found;
|
|
1385
|
+
};
|
|
1386
|
+
if (input["visualIdentity"] === null) edit.visualIdentity = null;
|
|
1387
|
+
else if (input["visualIdentity"] !== void 0) {
|
|
1388
|
+
const visual = object("visualIdentity");
|
|
1389
|
+
if (visual !== void 0) {
|
|
1390
|
+
allowed(visual, [
|
|
1391
|
+
"description",
|
|
1392
|
+
"tags",
|
|
1393
|
+
"negativeTags",
|
|
1394
|
+
"globalId"
|
|
1395
|
+
], "visualIdentity.");
|
|
1396
|
+
const description = text(visual, "description", "visualIdentity.", 2e3);
|
|
1397
|
+
const tags = list(visual, "tags", "visualIdentity.");
|
|
1398
|
+
const negativeTags = list(visual, "negativeTags", "visualIdentity.");
|
|
1399
|
+
const globalId = text(visual, "globalId", "visualIdentity.", NAME_LIMIT);
|
|
1400
|
+
if (description === void 0 || description.trim() === "") problems.push("visualIdentity.description is needed");
|
|
1401
|
+
else edit.visualIdentity = {
|
|
1402
|
+
description: description.trim(),
|
|
1403
|
+
tags: tags ?? [],
|
|
1404
|
+
negativeTags: negativeTags ?? [],
|
|
1405
|
+
...globalId === void 0 || globalId.trim() === "" ? {} : { globalId: globalId.trim() }
|
|
1406
|
+
};
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
const speech = object("speech");
|
|
1410
|
+
if (speech !== void 0) {
|
|
1411
|
+
allowed(speech, ["style", "examples"], "speech.");
|
|
1412
|
+
const style = text(speech, "style", "speech.", TEXT_LIMIT);
|
|
1413
|
+
const examples = list(speech, "examples", "speech.");
|
|
1414
|
+
edit.speech = {
|
|
1415
|
+
...style === void 0 ? {} : { style },
|
|
1416
|
+
...examples === void 0 ? {} : { examples }
|
|
1417
|
+
};
|
|
1418
|
+
}
|
|
1419
|
+
const state = object("state");
|
|
1420
|
+
if (state !== void 0) {
|
|
1421
|
+
allowed(state, [
|
|
1422
|
+
"publicState",
|
|
1423
|
+
"innerState",
|
|
1424
|
+
"activity"
|
|
1425
|
+
], "state.");
|
|
1426
|
+
edit.state = {};
|
|
1427
|
+
for (const key of [
|
|
1428
|
+
"publicState",
|
|
1429
|
+
"innerState",
|
|
1430
|
+
"activity"
|
|
1431
|
+
]) {
|
|
1432
|
+
const found = text(state, key, "state.", TEXT_LIMIT);
|
|
1433
|
+
if (found !== void 0) edit.state[key] = found;
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
const place = object("place");
|
|
1437
|
+
if (place !== void 0) {
|
|
1438
|
+
allowed(place, ["location", "position"], "place.");
|
|
1439
|
+
const location = text(place, "location", "place.", NAME_LIMIT);
|
|
1440
|
+
const position = text(place, "position", "place.", NAME_LIMIT);
|
|
1441
|
+
if (location === void 0 || location.trim() === "") problems.push("place.location is needed");
|
|
1442
|
+
else edit.place = {
|
|
1443
|
+
location,
|
|
1444
|
+
...position === void 0 ? {} : { position }
|
|
1566
1445
|
};
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1446
|
+
}
|
|
1447
|
+
if (input["goals"] !== void 0) {
|
|
1448
|
+
if (!Array.isArray(input["goals"])) problems.push("goals must be a list");
|
|
1449
|
+
else {
|
|
1450
|
+
const goals = [];
|
|
1451
|
+
edit.goals = goals;
|
|
1452
|
+
input["goals"].forEach((entry, index) => {
|
|
1453
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) {
|
|
1454
|
+
problems.push(`goals[${index}] must be an object`);
|
|
1455
|
+
return;
|
|
1574
1456
|
}
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1457
|
+
const goal = entry;
|
|
1458
|
+
const path = `goals[${index}].`;
|
|
1459
|
+
allowed(goal, [
|
|
1460
|
+
"ref",
|
|
1461
|
+
"name",
|
|
1462
|
+
"description",
|
|
1463
|
+
"status",
|
|
1464
|
+
"priority"
|
|
1465
|
+
], path);
|
|
1466
|
+
const ref = text(goal, "ref", path, NAME_LIMIT);
|
|
1467
|
+
if (ref === void 0) {
|
|
1468
|
+
problems.push(`${path}ref is needed`);
|
|
1469
|
+
return;
|
|
1578
1470
|
}
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1471
|
+
const goalName = text(goal, "name", path, NAME_LIMIT);
|
|
1472
|
+
if (goalName !== void 0 && goalName.trim() === "") problems.push(`${path}name cannot be blank`);
|
|
1473
|
+
const description = text(goal, "description", path, TEXT_LIMIT);
|
|
1474
|
+
const status = choice("status", INTENT_STATUSES, goal, path);
|
|
1475
|
+
const priority = goal["priority"];
|
|
1476
|
+
if (priority !== void 0 && (typeof priority !== "number" || !Number.isFinite(priority) || priority < 0 || priority > 1)) problems.push(`${path}priority must be a number from 0 to 1`);
|
|
1477
|
+
goals.push({
|
|
1478
|
+
ref,
|
|
1479
|
+
...goalName === void 0 ? {} : { name: goalName.trim() },
|
|
1480
|
+
...description === void 0 ? {} : { description },
|
|
1481
|
+
...status === void 0 ? {} : { status },
|
|
1482
|
+
...typeof priority === "number" ? { priority } : {}
|
|
1483
|
+
});
|
|
1484
|
+
});
|
|
1485
|
+
}
|
|
1590
1486
|
}
|
|
1591
|
-
|
|
1487
|
+
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
1488
|
+
return edit;
|
|
1592
1489
|
}
|
|
1593
|
-
|
|
1594
|
-
const
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
"
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
"pre",
|
|
1615
|
-
"center",
|
|
1616
|
-
"small",
|
|
1617
|
-
"img",
|
|
1618
|
-
"ul",
|
|
1619
|
-
"ol",
|
|
1620
|
-
"li",
|
|
1621
|
-
"blockquote",
|
|
1622
|
-
"details",
|
|
1623
|
-
"summary",
|
|
1624
|
-
"font",
|
|
1625
|
-
"sub",
|
|
1626
|
-
"sup",
|
|
1627
|
-
"h1",
|
|
1628
|
-
"h2",
|
|
1629
|
-
"h3",
|
|
1630
|
-
"table",
|
|
1631
|
-
"tr",
|
|
1632
|
-
"td",
|
|
1633
|
-
"th"
|
|
1634
|
-
]);
|
|
1635
|
-
const MACRO = /\{\{\s*([\p{L}_][\p{L}\p{N}_]*)/gu;
|
|
1636
|
-
const BLOCK = /<([\p{L}][\p{L}\p{N}_-]{1,30})(?:\s[^>]*)?>[\s\S]*?<\/\1\s*>/gu;
|
|
1637
|
-
function textsOf(card) {
|
|
1638
|
-
return [
|
|
1639
|
-
card.description,
|
|
1640
|
-
card.personality,
|
|
1641
|
-
card.scenario,
|
|
1642
|
-
card.firstMessage,
|
|
1643
|
-
...card.alternateGreetings,
|
|
1644
|
-
card.messageExample,
|
|
1645
|
-
card.systemPrompt,
|
|
1646
|
-
card.postHistoryInstructions,
|
|
1647
|
-
...(card.book?.entries ?? []).map((entry) => entry.content)
|
|
1490
|
+
function parseEntityEdit(value, kind) {
|
|
1491
|
+
const problems = [];
|
|
1492
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new InvalidEditProblems(["the edit must be an object"]);
|
|
1493
|
+
const input = value;
|
|
1494
|
+
const edit = {};
|
|
1495
|
+
const allowed = kind === "place" ? [
|
|
1496
|
+
"name",
|
|
1497
|
+
"description",
|
|
1498
|
+
"features",
|
|
1499
|
+
"visualIdentity",
|
|
1500
|
+
"parent"
|
|
1501
|
+
] : [
|
|
1502
|
+
"name",
|
|
1503
|
+
"description",
|
|
1504
|
+
"quantity",
|
|
1505
|
+
"quality",
|
|
1506
|
+
"notable",
|
|
1507
|
+
"hidden",
|
|
1508
|
+
"containerState",
|
|
1509
|
+
"visualIdentity",
|
|
1510
|
+
"owner"
|
|
1648
1511
|
];
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
if (!skip(name)) found.add(name);
|
|
1512
|
+
const link = kind === "place" ? "parent" : "owner";
|
|
1513
|
+
if (link in input) {
|
|
1514
|
+
const target = input[link];
|
|
1515
|
+
if (target === null) edit[link] = null;
|
|
1516
|
+
else if (typeof target !== "string") problems.push(`${link} must be a key or null`);
|
|
1517
|
+
else edit[link] = target.trim() === "" ? null : target.trim();
|
|
1656
1518
|
}
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
}
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
}
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
const
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
const MIN_CUSTOM_TAGS = 6;
|
|
1706
|
-
const MAX_PANELS = 6;
|
|
1707
|
-
const collapse = (text) => text.replace(/\n{3,}/g, "\n\n").trim();
|
|
1708
|
-
const plain = (html) => html.replace(/<[^>]*>/g, " ").replace(/\s+/g, " ").trim();
|
|
1709
|
-
/** The outermost elements of block markup and of invented tags in a text, balanced by their own names, with neighbours that only whitespace separates joined into one. */
|
|
1710
|
-
function tagBlocks(text) {
|
|
1711
|
-
const found = [];
|
|
1712
|
-
const open = [];
|
|
1713
|
-
let current = {
|
|
1714
|
-
from: 0,
|
|
1715
|
-
to: 0,
|
|
1716
|
-
custom: false,
|
|
1717
|
-
tags: 0
|
|
1718
|
-
};
|
|
1719
|
-
const closed = new Set([...text.matchAll(TAG)].filter((match) => match[1] === "/").map((match) => match[2].toLowerCase()));
|
|
1720
|
-
for (const match of text.matchAll(TAG)) {
|
|
1721
|
-
const name = match[2].toLowerCase();
|
|
1722
|
-
const custom = !MARKUP.has(name);
|
|
1723
|
-
if (!custom && !BLOCKS.has(name)) continue;
|
|
1724
|
-
if (match[1] === "" && !closed.has(name)) continue;
|
|
1725
|
-
if (match[1] === "") {
|
|
1726
|
-
if (open.length === 0) current = {
|
|
1727
|
-
from: match.index,
|
|
1728
|
-
to: 0,
|
|
1729
|
-
custom: false,
|
|
1730
|
-
tags: 0
|
|
1519
|
+
for (const key of Object.keys(input)) if (!allowed.includes(key)) problems.push(`${key} is not something that can be edited`);
|
|
1520
|
+
for (const key of [
|
|
1521
|
+
"name",
|
|
1522
|
+
"description",
|
|
1523
|
+
"quality"
|
|
1524
|
+
]) {
|
|
1525
|
+
if (!(key in input)) continue;
|
|
1526
|
+
const value = input[key];
|
|
1527
|
+
if (typeof value !== "string" || value.length > (key === "name" ? NAME_LIMIT : TEXT_LIMIT)) problems.push(`${key} must be text`);
|
|
1528
|
+
else if (key === "name" && value.trim() === "") problems.push("name cannot be blank");
|
|
1529
|
+
else edit[key] = key === "name" ? value.trim() : value;
|
|
1530
|
+
}
|
|
1531
|
+
for (const key of ["notable", "hidden"]) if (key in input) {
|
|
1532
|
+
if (typeof input[key] !== "boolean") problems.push(`${key} must be true or false`);
|
|
1533
|
+
else edit[key] = input[key];
|
|
1534
|
+
}
|
|
1535
|
+
if ("quantity" in input) {
|
|
1536
|
+
const quantity = input["quantity"];
|
|
1537
|
+
if (typeof quantity !== "number" || !Number.isInteger(quantity) || quantity < 1) problems.push("quantity must be a whole number from 1");
|
|
1538
|
+
else edit["quantity"] = quantity;
|
|
1539
|
+
}
|
|
1540
|
+
if ("containerState" in input) {
|
|
1541
|
+
const state = input["containerState"];
|
|
1542
|
+
if (state !== "open" && state !== "closed" && state !== "locked") problems.push("containerState must be one of: open, closed, locked");
|
|
1543
|
+
else edit["containerState"] = state;
|
|
1544
|
+
}
|
|
1545
|
+
if ("features" in input) {
|
|
1546
|
+
const features = input["features"];
|
|
1547
|
+
if (!Array.isArray(features) || features.length > 30 || !features.every((feature) => typeof feature === "object" && feature !== null && typeof feature["key"] === "string" && typeof feature["name"] === "string" && typeof feature["description"] === "string")) problems.push("features must be a list of named features");
|
|
1548
|
+
else edit["features"] = features;
|
|
1549
|
+
}
|
|
1550
|
+
if (input["visualIdentity"] === null) edit["visualIdentity"] = null;
|
|
1551
|
+
else if ("visualIdentity" in input) {
|
|
1552
|
+
const identity = input["visualIdentity"];
|
|
1553
|
+
if (typeof identity !== "object" || identity === null || Array.isArray(identity)) problems.push("visualIdentity must be an object");
|
|
1554
|
+
else {
|
|
1555
|
+
const visual = identity;
|
|
1556
|
+
const description = visual["description"];
|
|
1557
|
+
const tags = visual["tags"];
|
|
1558
|
+
const negativeTags = visual["negativeTags"];
|
|
1559
|
+
const globalId = visual["globalId"];
|
|
1560
|
+
if (typeof description !== "string" || description.trim() === "") problems.push("visualIdentity.description is needed");
|
|
1561
|
+
else if (!Array.isArray(tags) || !tags.every((value) => typeof value === "string") || !Array.isArray(negativeTags) || !negativeTags.every((value) => typeof value === "string") || globalId !== void 0 && typeof globalId !== "string") problems.push("visualIdentity has invalid fields");
|
|
1562
|
+
else edit["visualIdentity"] = {
|
|
1563
|
+
description: description.trim(),
|
|
1564
|
+
tags,
|
|
1565
|
+
negativeTags,
|
|
1566
|
+
...globalId === void 0 || globalId.trim() === "" ? {} : { globalId: globalId.trim() }
|
|
1731
1567
|
};
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
1571
|
+
return edit;
|
|
1572
|
+
}
|
|
1573
|
+
const parsePlaceEdit = (value) => parseEntityEdit(value, "place");
|
|
1574
|
+
const parseThingEdit = (value) => parseEntityEdit(value, "thing");
|
|
1575
|
+
function parseFactionEdit(value) {
|
|
1576
|
+
const problems = [];
|
|
1577
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new InvalidEditProblems(["the edit must be an object"]);
|
|
1578
|
+
const input = value;
|
|
1579
|
+
const allowed = [
|
|
1580
|
+
"name",
|
|
1581
|
+
"aliases",
|
|
1582
|
+
"summary",
|
|
1583
|
+
"description",
|
|
1584
|
+
"values",
|
|
1585
|
+
"structure",
|
|
1586
|
+
"state",
|
|
1587
|
+
"goals"
|
|
1588
|
+
];
|
|
1589
|
+
for (const key of Object.keys(input)) if (!allowed.includes(key)) problems.push(`${key} is not something that can be edited`);
|
|
1590
|
+
const edit = {};
|
|
1591
|
+
for (const key of [
|
|
1592
|
+
"name",
|
|
1593
|
+
"summary",
|
|
1594
|
+
"description",
|
|
1595
|
+
"structure"
|
|
1596
|
+
]) if (key in input) {
|
|
1597
|
+
const found = input[key];
|
|
1598
|
+
if (typeof found !== "string" || found.length > TEXT_LIMIT) problems.push(`${key} must be text`);
|
|
1599
|
+
else if (key === "name" && found.trim() === "") problems.push("name cannot be blank");
|
|
1600
|
+
else edit[key] = key === "name" ? found.trim() : found;
|
|
1601
|
+
}
|
|
1602
|
+
for (const key of ["aliases", "values"]) if (key in input) {
|
|
1603
|
+
const found = input[key];
|
|
1604
|
+
if (!Array.isArray(found) || !found.every((entry) => typeof entry === "string" && entry.length <= NAME_LIMIT)) problems.push(`${key} must be a list of short texts`);
|
|
1605
|
+
else edit[key] = found;
|
|
1606
|
+
}
|
|
1607
|
+
if ("state" in input) {
|
|
1608
|
+
const state = input["state"];
|
|
1609
|
+
if (typeof state !== "object" || state === null || Array.isArray(state)) problems.push("state must be an object");
|
|
1610
|
+
else {
|
|
1611
|
+
const record = state;
|
|
1612
|
+
edit.state = {};
|
|
1613
|
+
for (const key of ["publicState", "innerState"]) if (key in record) {
|
|
1614
|
+
if (typeof record[key] !== "string") problems.push(`state.${key} must be text`);
|
|
1615
|
+
else edit.state[key] = record[key];
|
|
1741
1616
|
}
|
|
1742
1617
|
}
|
|
1743
1618
|
}
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
if (
|
|
1748
|
-
|
|
1749
|
-
last.custom ||= block.custom;
|
|
1750
|
-
last.tags += block.tags;
|
|
1751
|
-
} else joined.push({ ...block });
|
|
1619
|
+
if ("goals" in input) try {
|
|
1620
|
+
edit.goals = parseCharacterEdit({ goals: input["goals"] }).goals;
|
|
1621
|
+
} catch (error) {
|
|
1622
|
+
if (error instanceof InvalidEditProblems) problems.push(...error.problems);
|
|
1623
|
+
else throw error;
|
|
1752
1624
|
}
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
return header === null ? block : {
|
|
1756
|
-
...block,
|
|
1757
|
-
from: header.index + (header[0].startsWith("\n") ? 1 : 0)
|
|
1758
|
-
};
|
|
1759
|
-
});
|
|
1625
|
+
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
1626
|
+
return edit;
|
|
1760
1627
|
}
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
const
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1628
|
+
function parseLoreEdit(value) {
|
|
1629
|
+
const problems = [];
|
|
1630
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new InvalidEditProblems(["the edit must be an object"]);
|
|
1631
|
+
const input = value;
|
|
1632
|
+
const edit = {};
|
|
1633
|
+
for (const key of Object.keys(input)) if (![
|
|
1634
|
+
"title",
|
|
1635
|
+
"kind",
|
|
1636
|
+
"scope",
|
|
1637
|
+
"knownBy",
|
|
1638
|
+
"summary",
|
|
1639
|
+
"body",
|
|
1640
|
+
"startsAt",
|
|
1641
|
+
"endsAt",
|
|
1642
|
+
"sources"
|
|
1643
|
+
].includes(key)) problems.push(`${key} is not something that can be edited`);
|
|
1644
|
+
for (const key of [
|
|
1645
|
+
"title",
|
|
1646
|
+
"summary",
|
|
1647
|
+
"body"
|
|
1648
|
+
]) if (key in input) {
|
|
1649
|
+
const found = input[key];
|
|
1650
|
+
if (typeof found !== "string" || found.length > TEXT_LIMIT) problems.push(`${key} must be text`);
|
|
1651
|
+
else if (key === "title" && found.trim() === "") problems.push("title cannot be blank");
|
|
1652
|
+
else edit[key] = key === "title" ? found.trim() : found;
|
|
1773
1653
|
}
|
|
1774
|
-
|
|
1775
|
-
}
|
|
1776
|
-
|
|
1777
|
-
* Takes the status bars out of a message: fenced code blocks, blocks of tags of the card's own invention (a layout of nested tags, which names may be in any language), short blocks
|
|
1778
|
-
* of markup that carry numbers (when the message has story text besides them: a message wrapped whole in a block is only styled), and closing lines of values set apart by bars.
|
|
1779
|
-
* What is left is the story; the panels are what the card shows its state with.
|
|
1780
|
-
*/
|
|
1781
|
-
function splitPanels(text, options = {}) {
|
|
1782
|
-
const panels = [];
|
|
1783
|
-
let rest = options.leadIn === true ? fencesWithoutLeadIn(text, panels) : text.replace(FENCE, (block) => {
|
|
1784
|
-
panels.push(block.replace(/^```[^\n]*\n|```$/g, "").trim());
|
|
1785
|
-
return "\n\n";
|
|
1786
|
-
});
|
|
1787
|
-
const blocks = tagBlocks(rest);
|
|
1788
|
-
const outside = blocks.reduceRight((left, { from, to }) => left.slice(0, from) + left.slice(to), rest);
|
|
1789
|
-
if (blocks.length > 0 && (options.alone === true || plain(outside) !== "")) for (const block of [...blocks].reverse()) {
|
|
1790
|
-
const body = rest.slice(block.from, block.to);
|
|
1791
|
-
if (!(block.custom ? (block.tags >= MIN_CUSTOM_TAGS || CALL.test(body)) && body.length <= MAX_CUSTOM_CHARS : plain(body).length <= MAX_HTML_TEXT_CHARS && /\d/.test(body))) continue;
|
|
1792
|
-
panels.unshift(body.trim());
|
|
1793
|
-
rest = (options.leadIn === true ? withoutLeadIn(rest.slice(0, block.from)) : rest.slice(0, block.from)) + "\n\n" + rest.slice(block.to);
|
|
1654
|
+
if ("kind" in input) {
|
|
1655
|
+
if (typeof input["kind"] !== "string" || !LORE_KINDS.includes(input["kind"])) problems.push(`kind must be one of: ${LORE_KINDS.join(", ")}`);
|
|
1656
|
+
else edit.kind = input["kind"];
|
|
1794
1657
|
}
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
const
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
else if (word.length >= 3) words.add(word);
|
|
1821
|
-
return words;
|
|
1822
|
-
}
|
|
1823
|
-
/**
|
|
1824
|
-
* The sentences of a prose prompt that repeat something the same answer lists as left out. A model that is told to drop an instruction about the shape of the reply (a status bar after
|
|
1825
|
-
* every message) tends to list it and keep it in the same breath; what it listed is not carried.
|
|
1826
|
-
*/
|
|
1827
|
-
function withoutLeftOut(prose, leftOut) {
|
|
1828
|
-
const dropped = leftOut.map(wordsOf).filter((words) => words.size >= MIN_LEFT_OUT_WORDS);
|
|
1829
|
-
if (dropped.length === 0) return prose;
|
|
1830
|
-
const echoes = (sentence) => {
|
|
1831
|
-
const words = wordsOf(sentence);
|
|
1832
|
-
return dropped.some((left) => [...left].filter((word) => words.has(word)).length >= left.size * ECHO_SHARE);
|
|
1833
|
-
};
|
|
1834
|
-
const kept = (line) => {
|
|
1835
|
-
let after = false;
|
|
1836
|
-
return sentencesOf(line).filter((sentence) => {
|
|
1837
|
-
const echo = echoes(sentence);
|
|
1838
|
-
const tail = after && sentence.trim().split(/\s+/).length <= MAX_TAIL_WORDS;
|
|
1839
|
-
after = echo;
|
|
1840
|
-
return !echo && !tail;
|
|
1841
|
-
}).join("").trim();
|
|
1842
|
-
};
|
|
1843
|
-
return prose.split("\n").map(kept).filter((line) => line !== "").join("\n");
|
|
1844
|
-
}
|
|
1845
|
-
const OUTPUT_MARKUP = /<\/?[\p{L}][\p{L}\p{N}_-]*>|```|\b_\.\w+\(|\bstat_data\b|\{\{(?!\s*(?:user|char|bot)\s*\}\})[^{}]+\}\}/u;
|
|
1846
|
-
/** A text that is mostly a panel is a format specification, not a rule or a fact: it goes whole. */
|
|
1847
|
-
const PANEL_SHARE = .5;
|
|
1848
|
-
/** A standing rule without the panel it demands, the sentences that tell the game master to print markup, a code block or an update call after a reply, and the rule itself when the panel was most of it: the story shows its tracked values itself. */
|
|
1849
|
-
function withoutOutputMarkup(passage) {
|
|
1850
|
-
const { prose, panels } = splitPanels(passage, {
|
|
1851
|
-
alone: true,
|
|
1852
|
-
leadIn: true
|
|
1853
|
-
});
|
|
1854
|
-
if (panels.length > 0 && prose.length < passage.length * PANEL_SHARE) return "";
|
|
1855
|
-
return prose.split("\n").map((line) => sentencesOf(line).filter((sentence) => !OUTPUT_MARKUP.test(sentence)).join("").trim()).filter((line) => line !== "").join("\n");
|
|
1856
|
-
}
|
|
1857
|
-
const BARRED = /(?:\s[||│]\s[^||│\n]+){2,}/;
|
|
1858
|
-
/** The first status bar, panel or print-this-markup directive in a text, as written, or nothing: what a world should show with its tracked values rather than carry as text. */
|
|
1859
|
-
function findPanel(text) {
|
|
1860
|
-
const panel = splitPanels(text, { alone: true }).panels[0];
|
|
1861
|
-
if (panel !== void 0) return panel;
|
|
1862
|
-
for (const line of text.split("\n")) for (const sentence of sentencesOf(line)) if (OUTPUT_MARKUP.test(sentence) || BARRED.test(sentence) && /\d/.test(sentence)) return sentence.trim();
|
|
1863
|
-
}
|
|
1864
|
-
// src/shared/authoring-commands.ts
|
|
1865
|
-
/** The command a message starts with, when its name is one of `names` (not a longer word that starts with it). */
|
|
1866
|
-
function parseAuthoringCommand(text, names) {
|
|
1867
|
-
const value = text.trimStart();
|
|
1868
|
-
if (!value.startsWith("/")) return void 0;
|
|
1869
|
-
const end = value.search(/\s/u);
|
|
1870
|
-
const word = (end < 0 ? value : value.slice(0, end)).slice(1).toLowerCase();
|
|
1871
|
-
return names.includes(word) ? {
|
|
1872
|
-
name: word,
|
|
1873
|
-
rest: end < 0 ? "" : value.slice(end).trim()
|
|
1874
|
-
} : void 0;
|
|
1658
|
+
if ("scope" in input) {
|
|
1659
|
+
if (typeof input["scope"] !== "string" || !LORE_SCOPES.includes(input["scope"])) problems.push(`scope must be one of: ${LORE_SCOPES.join(", ")}`);
|
|
1660
|
+
else edit.scope = input["scope"];
|
|
1661
|
+
}
|
|
1662
|
+
if ("knownBy" in input) {
|
|
1663
|
+
const found = input["knownBy"];
|
|
1664
|
+
if (!Array.isArray(found) || !found.every((entry) => typeof entry === "string" && entry.trim() !== "" && entry.length <= TEXT_LIMIT)) problems.push("knownBy must be a list of names");
|
|
1665
|
+
else edit.knownBy = found.map((entry) => entry.trim());
|
|
1666
|
+
}
|
|
1667
|
+
if (edit.scope === "everyone" && (edit.knownBy?.length ?? 0) > 0) problems.push("an entry everyone knows has no list of who knows it");
|
|
1668
|
+
for (const key of ["startsAt", "endsAt"]) if (key in input) {
|
|
1669
|
+
const found = input[key];
|
|
1670
|
+
if (found !== null && (typeof found !== "number" || !Number.isFinite(found))) problems.push(`${key} must be a timestamp or null`);
|
|
1671
|
+
else edit[key] = found;
|
|
1672
|
+
}
|
|
1673
|
+
if ("sources" in input) {
|
|
1674
|
+
const found = input["sources"];
|
|
1675
|
+
if (!Array.isArray(found) || !found.every((entry) => typeof entry === "string" && entry.length <= TEXT_LIMIT)) problems.push("sources must be a list of texts");
|
|
1676
|
+
else edit.sources = found;
|
|
1677
|
+
}
|
|
1678
|
+
const starts = edit.startsAt === null ? void 0 : edit.startsAt;
|
|
1679
|
+
const ends = edit.endsAt === null ? void 0 : edit.endsAt;
|
|
1680
|
+
if (starts !== void 0 && ends !== void 0 && ends < starts) problems.push("endsAt must not be before startsAt");
|
|
1681
|
+
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
1682
|
+
return edit;
|
|
1875
1683
|
}
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
const
|
|
1882
|
-
if (
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1684
|
+
function parseRelationEdit(value) {
|
|
1685
|
+
const problems = [];
|
|
1686
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) throw new InvalidEditProblems(["the edit must be an object"]);
|
|
1687
|
+
const input = value;
|
|
1688
|
+
const kind = input["kind"];
|
|
1689
|
+
const key = input["key"];
|
|
1690
|
+
if (![
|
|
1691
|
+
"character",
|
|
1692
|
+
"knowledge",
|
|
1693
|
+
"membership",
|
|
1694
|
+
"social",
|
|
1695
|
+
"exit"
|
|
1696
|
+
].includes(String(kind))) problems.push("kind is not a relation kind");
|
|
1697
|
+
const creating = input["create"] === true;
|
|
1698
|
+
if (!creating && (typeof key !== "string" || key.trim() === "" || key.length > 500)) problems.push("key is needed");
|
|
1699
|
+
if (!creating && typeof key === "string" && key.trim() !== "" && typeof input["from"] === "string" && typeof input["to"] === "string" && input["remove"] !== true) problems.push("to make a new relationship send create: true with from and to and no key; a key names one that exists, exactly as list_relationships shows it");
|
|
1700
|
+
if ("create" in input && typeof input["create"] !== "boolean") problems.push("create must be true or false");
|
|
1701
|
+
const allowedByKind = {
|
|
1702
|
+
character: [
|
|
1703
|
+
"kind",
|
|
1704
|
+
"key",
|
|
1705
|
+
"create",
|
|
1706
|
+
"from",
|
|
1707
|
+
"to",
|
|
1708
|
+
"remove",
|
|
1709
|
+
"label",
|
|
1710
|
+
"summary",
|
|
1711
|
+
"familiarity",
|
|
1712
|
+
"trust",
|
|
1713
|
+
"affinity",
|
|
1714
|
+
"tension"
|
|
1715
|
+
],
|
|
1716
|
+
knowledge: [
|
|
1717
|
+
"kind",
|
|
1718
|
+
"key",
|
|
1719
|
+
"create",
|
|
1720
|
+
"from",
|
|
1721
|
+
"toKind",
|
|
1722
|
+
"to",
|
|
1723
|
+
"observed",
|
|
1724
|
+
"remove"
|
|
1725
|
+
],
|
|
1726
|
+
membership: [
|
|
1727
|
+
"kind",
|
|
1728
|
+
"key",
|
|
1729
|
+
"create",
|
|
1730
|
+
"from",
|
|
1731
|
+
"to",
|
|
1732
|
+
"remove",
|
|
1733
|
+
"role",
|
|
1734
|
+
"rank",
|
|
1735
|
+
"status",
|
|
1736
|
+
"public",
|
|
1737
|
+
"commitment"
|
|
1738
|
+
],
|
|
1739
|
+
social: [
|
|
1740
|
+
"kind",
|
|
1741
|
+
"key",
|
|
1742
|
+
"create",
|
|
1743
|
+
"fromKind",
|
|
1744
|
+
"from",
|
|
1745
|
+
"toKind",
|
|
1746
|
+
"to",
|
|
1747
|
+
"remove",
|
|
1748
|
+
"label",
|
|
1749
|
+
"summary",
|
|
1750
|
+
"familiarity",
|
|
1751
|
+
"trust",
|
|
1752
|
+
"affinity",
|
|
1753
|
+
"tension"
|
|
1754
|
+
],
|
|
1755
|
+
exit: [
|
|
1756
|
+
"kind",
|
|
1757
|
+
"key",
|
|
1758
|
+
"create",
|
|
1759
|
+
"from",
|
|
1760
|
+
"to",
|
|
1761
|
+
"remove",
|
|
1762
|
+
"description",
|
|
1763
|
+
"travelSeconds",
|
|
1764
|
+
"state",
|
|
1765
|
+
"note"
|
|
1766
|
+
]
|
|
1890
1767
|
};
|
|
1768
|
+
for (const field of Object.keys(input)) if (!(allowedByKind[String(kind)] ?? []).includes(field)) problems.push(`${field} is not something that can be edited`);
|
|
1769
|
+
if ("remove" in input && typeof input["remove"] !== "boolean") problems.push("remove must be true or false");
|
|
1770
|
+
for (const field of [
|
|
1771
|
+
"label",
|
|
1772
|
+
"summary",
|
|
1773
|
+
"role",
|
|
1774
|
+
"rank",
|
|
1775
|
+
"description",
|
|
1776
|
+
"note"
|
|
1777
|
+
]) if (field in input && (typeof input[field] !== "string" || input[field].length > TEXT_LIMIT)) problems.push(`${field} must be text`);
|
|
1778
|
+
for (const field of [
|
|
1779
|
+
"familiarity",
|
|
1780
|
+
"trust",
|
|
1781
|
+
"affinity",
|
|
1782
|
+
"tension",
|
|
1783
|
+
"commitment"
|
|
1784
|
+
]) if (field in input) {
|
|
1785
|
+
const number = input[field];
|
|
1786
|
+
const signed = field === "trust" || field === "affinity";
|
|
1787
|
+
if (typeof number !== "number" || !Number.isFinite(number) || number < (signed ? -1 : 0) || number > 1) problems.push(`${field} must be between ${signed ? "-1" : "0"} and 1`);
|
|
1788
|
+
}
|
|
1789
|
+
if ("travelSeconds" in input && input["travelSeconds"] !== null && (typeof input["travelSeconds"] !== "number" || !Number.isInteger(input["travelSeconds"]) || input["travelSeconds"] < 0)) problems.push("travelSeconds must be a non-negative whole number or null");
|
|
1790
|
+
if ("public" in input && typeof input["public"] !== "boolean") problems.push("public must be true or false");
|
|
1791
|
+
if ("observed" in input && typeof input["observed"] !== "boolean") problems.push("observed must be true or false");
|
|
1792
|
+
if ("status" in input) {
|
|
1793
|
+
const choices = kind === "exit" ? EXIT_STATES : MEMBERSHIP_STATUSES;
|
|
1794
|
+
if (typeof input["status"] !== "string" || !choices.includes(input["status"])) problems.push(`status must be one of: ${choices.join(", ")}`);
|
|
1795
|
+
}
|
|
1796
|
+
if (creating) {
|
|
1797
|
+
for (const field of ["from", "to"]) if (typeof input[field] !== "string" || input[field].trim() === "") problems.push(`${field} is needed: the key of the entity, written as plain text and not as an object`);
|
|
1798
|
+
if ((kind === "character" || kind === "social") && (typeof input["label"] !== "string" || input["label"].trim() === "")) problems.push("label is needed");
|
|
1799
|
+
if (kind === "membership" && (typeof input["role"] !== "string" || input["role"].trim() === "")) problems.push("role is needed");
|
|
1800
|
+
if (kind === "exit" && typeof input["description"] !== "string") problems.push("description is needed");
|
|
1801
|
+
if (kind === "knowledge" && ![
|
|
1802
|
+
"location",
|
|
1803
|
+
"item",
|
|
1804
|
+
"exit"
|
|
1805
|
+
].includes(String(input["toKind"]))) problems.push("toKind must be one of: location, item, exit");
|
|
1806
|
+
if (kind === "social") {
|
|
1807
|
+
for (const field of ["fromKind", "toKind"]) if (!["character", "faction"].includes(String(input[field]))) problems.push(`${field} must be character or faction`);
|
|
1808
|
+
}
|
|
1809
|
+
} else if (kind === "knowledge" && input["remove"] !== true) problems.push("knowledge editing supports creation or removal");
|
|
1810
|
+
if (problems.length > 0) throw new InvalidEditProblems(problems);
|
|
1811
|
+
return input;
|
|
1891
1812
|
}
|
|
1892
1813
|
// src/host/import/store.ts
|
|
1893
1814
|
const IMPORT_FOLDER = ".import";
|
|
@@ -2137,6 +2058,66 @@ function playerWord(text, name) {
|
|
|
2137
2058
|
function withPlayer(text, name) {
|
|
2138
2059
|
return text.replace(/\[PLAYER\]|\{\{\s*user\s*\}\}/g, playerWord(text, name));
|
|
2139
2060
|
}
|
|
2061
|
+
// src/host/folders.ts
|
|
2062
|
+
/**
|
|
2063
|
+
* Names of the subfolders of `dir`, sorted. Symbolic links are not followed
|
|
2064
|
+
* and a missing directory has none; any other failure is thrown.
|
|
2065
|
+
*/
|
|
2066
|
+
async function subfolders(dir) {
|
|
2067
|
+
try {
|
|
2068
|
+
return (await readdir(dir, { withFileTypes: true })).filter((entry) => entry.isDirectory()).map((entry) => entry.name).sort((a, b) => a.localeCompare(b));
|
|
2069
|
+
} catch (error) {
|
|
2070
|
+
if (error.code === "ENOENT") return [];
|
|
2071
|
+
throw error;
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
// src/host/lore/load.ts
|
|
2075
|
+
const LORE_DIR = "lore";
|
|
2076
|
+
async function loadLore(worldDir) {
|
|
2077
|
+
const dir = join(worldDir, LORE_DIR);
|
|
2078
|
+
const result = {
|
|
2079
|
+
lore: [],
|
|
2080
|
+
problems: []
|
|
2081
|
+
};
|
|
2082
|
+
let folders;
|
|
2083
|
+
try {
|
|
2084
|
+
folders = await subfolders(dir);
|
|
2085
|
+
} catch (error) {
|
|
2086
|
+
if (error.code !== "ENOENT") result.problems.push({
|
|
2087
|
+
path: dir,
|
|
2088
|
+
message: errorMessage(error)
|
|
2089
|
+
});
|
|
2090
|
+
return result;
|
|
2091
|
+
}
|
|
2092
|
+
const owners = /* @__PURE__ */ new Map();
|
|
2093
|
+
for (const folder of folders) {
|
|
2094
|
+
const file = join(dir, folder, LORE_FILE_NAME);
|
|
2095
|
+
try {
|
|
2096
|
+
const entry = await readDocument(file, (text) => parseLoreFile(text, folder), result.problems);
|
|
2097
|
+
if (entry === void 0) continue;
|
|
2098
|
+
const owner = owners.get(entry.id);
|
|
2099
|
+
if (owner !== void 0) {
|
|
2100
|
+
result.problems.push({
|
|
2101
|
+
path: file,
|
|
2102
|
+
message: `id "${entry.id}" is already used by ${owner}`
|
|
2103
|
+
});
|
|
2104
|
+
continue;
|
|
2105
|
+
}
|
|
2106
|
+
owners.set(entry.id, folder);
|
|
2107
|
+
result.lore.push(entry);
|
|
2108
|
+
} catch (error) {
|
|
2109
|
+
if (error.code !== "ENOENT") result.problems.push({
|
|
2110
|
+
path: file,
|
|
2111
|
+
message: errorMessage(error)
|
|
2112
|
+
});
|
|
2113
|
+
}
|
|
2114
|
+
}
|
|
2115
|
+
if (result.lore.length > 500) result.problems.push({
|
|
2116
|
+
path: dir,
|
|
2117
|
+
message: `a world has at most 500 lore entries`
|
|
2118
|
+
});
|
|
2119
|
+
return result;
|
|
2120
|
+
}
|
|
2140
2121
|
// src/host/lorebooks/lorebooks.ts
|
|
2141
2122
|
const LOREBOOKS_FOLDER = "lorebooks";
|
|
2142
2123
|
const LOREBOOK_FILE = "lorebook.json";
|
|
@@ -2798,7 +2779,62 @@ function parseStartQuestionsEdit(value, problems) {
|
|
|
2798
2779
|
}
|
|
2799
2780
|
return questions;
|
|
2800
2781
|
}
|
|
2801
|
-
const
|
|
2782
|
+
const MAX_OPENING_TEXT = 2e4;
|
|
2783
|
+
/** The words of an opening as one text: its parts, a blank line between. */
|
|
2784
|
+
const partsText = (parts) => parts.map((part) => part.text).join("\n\n");
|
|
2785
|
+
/** Whether any part is spoken by a character: an opening with none is only narration, which a plain text says as well. */
|
|
2786
|
+
const hasSpeech = (parts) => parts.some((part) => part.type === "utterance");
|
|
2787
|
+
/**
|
|
2788
|
+
* Reads the parts of an opening from a body that came from a browser or a model: every part is checked and each problem is reported. Blank parts are left out.
|
|
2789
|
+
* @returns the parts, or nothing when the value is no list of parts (the problems say why).
|
|
2790
|
+
*/
|
|
2791
|
+
function parseOpeningParts(value, where, problems) {
|
|
2792
|
+
if (!Array.isArray(value)) {
|
|
2793
|
+
problems.push(`${where} must be a list of parts`);
|
|
2794
|
+
return;
|
|
2795
|
+
}
|
|
2796
|
+
const parts = [];
|
|
2797
|
+
for (const [index, entry] of value.entries()) {
|
|
2798
|
+
const at = `${where}[${index}]`;
|
|
2799
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) {
|
|
2800
|
+
problems.push(`${at} must be an object`);
|
|
2801
|
+
continue;
|
|
2802
|
+
}
|
|
2803
|
+
const part = entry;
|
|
2804
|
+
const type = part["type"] ?? "narration";
|
|
2805
|
+
if (type !== "narration" && type !== "utterance") {
|
|
2806
|
+
problems.push(`${at}.type must be "narration" or "utterance"`);
|
|
2807
|
+
continue;
|
|
2808
|
+
}
|
|
2809
|
+
if (typeof part["text"] !== "string") {
|
|
2810
|
+
problems.push(`${at}.text must be text`);
|
|
2811
|
+
continue;
|
|
2812
|
+
}
|
|
2813
|
+
const text = part["text"].trim();
|
|
2814
|
+
if (text === "") continue;
|
|
2815
|
+
if (type === "narration") {
|
|
2816
|
+
parts.push({
|
|
2817
|
+
type,
|
|
2818
|
+
text
|
|
2819
|
+
});
|
|
2820
|
+
continue;
|
|
2821
|
+
}
|
|
2822
|
+
const character = typeof part["character"] === "string" ? part["character"].trim() : "";
|
|
2823
|
+
if (character === "") {
|
|
2824
|
+
problems.push(`${at}.character is needed for a line a character speaks`);
|
|
2825
|
+
continue;
|
|
2826
|
+
}
|
|
2827
|
+
parts.push({
|
|
2828
|
+
type,
|
|
2829
|
+
character,
|
|
2830
|
+
text
|
|
2831
|
+
});
|
|
2832
|
+
}
|
|
2833
|
+
if (parts.length > 60) problems.push(`${where} has more than 60 parts`);
|
|
2834
|
+
if (partsText(parts).length > 2e4) problems.push(`${where} is longer than ${MAX_OPENING_TEXT} characters`);
|
|
2835
|
+
return parts;
|
|
2836
|
+
}
|
|
2837
|
+
const MAX_OPENING_CHARS = MAX_OPENING_TEXT;
|
|
2802
2838
|
const TEXT_FIELDS = [
|
|
2803
2839
|
"name",
|
|
2804
2840
|
"description",
|
|
@@ -2822,7 +2858,7 @@ function parseWorldInfoEdit(value) {
|
|
|
2822
2858
|
const input = typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
|
|
2823
2859
|
if (input === void 0) throw new InvalidEditProblems(["the changes must be an object"]);
|
|
2824
2860
|
const edit = {};
|
|
2825
|
-
for (const key of Object.keys(input)) if (!TEXT_FIELDS.includes(key) && !LIST_FIELDS.includes(key) && key !== "alternateOpenings" && key !== "startQuestions" && key !== "tools" && key !== "sampling" && key !== "autonomy") problems.push(`there is no field "${key}"`);
|
|
2861
|
+
for (const key of Object.keys(input)) if (!TEXT_FIELDS.includes(key) && !LIST_FIELDS.includes(key) && key !== "alternateOpenings" && key !== "openingParts" && key !== "startQuestions" && key !== "tools" && key !== "sampling" && key !== "autonomy") problems.push(`there is no field "${key}"`);
|
|
2826
2862
|
for (const key of TEXT_FIELDS) {
|
|
2827
2863
|
if (input[key] === void 0) continue;
|
|
2828
2864
|
if (typeof input[key] === "string") edit[key] = input[key].trim();
|
|
@@ -2833,18 +2869,54 @@ function parseWorldInfoEdit(value) {
|
|
|
2833
2869
|
if (Array.isArray(input[key]) && input[key].every((entry) => typeof entry === "string")) edit[key] = input[key].map((entry) => entry.trim()).filter((entry) => entry !== "");
|
|
2834
2870
|
else problems.push(`${key} must be a list of text`);
|
|
2835
2871
|
}
|
|
2872
|
+
if (input["openingParts"] !== void 0) {
|
|
2873
|
+
if (input["opening"] !== void 0) problems.push("give the opening as text or as parts, not both");
|
|
2874
|
+
const parts = parseOpeningParts(input["openingParts"], "openingParts", problems);
|
|
2875
|
+
if (parts !== void 0) edit["openingParts"] = parts;
|
|
2876
|
+
}
|
|
2836
2877
|
if (input["alternateOpenings"] !== void 0) {
|
|
2837
2878
|
const list = input["alternateOpenings"];
|
|
2838
2879
|
const entries = Array.isArray(list) ? list.map((entry) => typeof entry === "object" && entry !== null ? entry : void 0) : void 0;
|
|
2839
|
-
|
|
2880
|
+
const kept = [];
|
|
2881
|
+
if (entries === void 0) problems.push("alternateOpenings must be a list of { title, text or parts, character? }");
|
|
2840
2882
|
else {
|
|
2841
|
-
const
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2883
|
+
for (const [index, entry] of entries.entries()) {
|
|
2884
|
+
const at = `alternateOpenings[${index}]`;
|
|
2885
|
+
if (entry === void 0) {
|
|
2886
|
+
problems.push(`${at} must be an object`);
|
|
2887
|
+
continue;
|
|
2888
|
+
}
|
|
2889
|
+
if (entry["title"] !== void 0 && typeof entry["title"] !== "string" || entry["character"] !== void 0 && typeof entry["character"] !== "string") {
|
|
2890
|
+
problems.push(`${at}: title and character must be text`);
|
|
2891
|
+
continue;
|
|
2892
|
+
}
|
|
2893
|
+
const title = String(entry["title"] ?? "").trim();
|
|
2894
|
+
const character = String(entry["character"] ?? "").trim();
|
|
2895
|
+
const named = character === "" ? {} : { character };
|
|
2896
|
+
if (entry["parts"] !== void 0) {
|
|
2897
|
+
if (entry["text"] !== void 0) problems.push(`${at}: give the opening as text or as parts, not both`);
|
|
2898
|
+
const parts = parseOpeningParts(entry["parts"], `${at}.parts`, problems);
|
|
2899
|
+
if (parts !== void 0 && parts.length > 0) kept.push({
|
|
2900
|
+
title,
|
|
2901
|
+
text: partsText(parts),
|
|
2902
|
+
parts,
|
|
2903
|
+
...named
|
|
2904
|
+
});
|
|
2905
|
+
continue;
|
|
2906
|
+
}
|
|
2907
|
+
if (typeof entry["text"] !== "string") {
|
|
2908
|
+
problems.push(`${at}.text must be text`);
|
|
2909
|
+
continue;
|
|
2910
|
+
}
|
|
2911
|
+
const text = entry["text"].trim();
|
|
2912
|
+
if (text !== "") kept.push({
|
|
2913
|
+
title,
|
|
2914
|
+
text,
|
|
2915
|
+
...named
|
|
2916
|
+
});
|
|
2917
|
+
}
|
|
2846
2918
|
if (kept.length > 30) problems.push(`there are more than 30 alternate openings`);
|
|
2847
|
-
if (kept.some((entry) => entry.text.length >
|
|
2919
|
+
if (kept.some((entry) => entry.text.length > MAX_OPENING_CHARS)) problems.push(`an alternate opening is longer than ${MAX_OPENING_CHARS} characters`);
|
|
2848
2920
|
edit["alternateOpenings"] = kept;
|
|
2849
2921
|
}
|
|
2850
2922
|
}
|
|
@@ -2870,7 +2942,7 @@ function parseWorldInfoEdit(value) {
|
|
|
2870
2942
|
}
|
|
2871
2943
|
// src/host/worlds/world-file.ts
|
|
2872
2944
|
const WORLD_FILE_NAME = "world.json";
|
|
2873
|
-
/** `alternate_openings` is a list of `{ title, text, character? }`: other first messages the player may start with, each for any character or for one playable character. */
|
|
2945
|
+
/** `alternate_openings` is a list of `{ title, text | parts, character? }`: other first messages the player may start with, each for any character or for one playable character. */
|
|
2874
2946
|
function readAlternateOpenings(reader) {
|
|
2875
2947
|
const value = reader.raw("alternate_openings");
|
|
2876
2948
|
if (value === void 0 || value === null) return void 0;
|
|
@@ -2881,13 +2953,16 @@ function readAlternateOpenings(reader) {
|
|
|
2881
2953
|
if (value.length > 30) reader.fail("alternate_openings", `has more than 30 openings`);
|
|
2882
2954
|
const openings = [];
|
|
2883
2955
|
for (const entry of reader.objects("alternate_openings")) {
|
|
2884
|
-
const
|
|
2956
|
+
const given = entry.raw("parts");
|
|
2957
|
+
const parts = given === void 0 || given === null ? void 0 : readParts(entry, "parts");
|
|
2958
|
+
const text = parts === void 0 ? entry.string("text", { required: true })?.trim() : partsText(parts);
|
|
2885
2959
|
if (text === void 0 || text === "") continue;
|
|
2886
|
-
if (text.length >
|
|
2960
|
+
if (text.length > MAX_OPENING_CHARS) entry.fail(parts === void 0 ? "text" : "parts", `is longer than ${MAX_OPENING_CHARS} characters`);
|
|
2887
2961
|
const character = entry.string("character")?.trim();
|
|
2888
2962
|
openings.push({
|
|
2889
2963
|
title: entry.string("title")?.trim() || text.slice(0, 40),
|
|
2890
2964
|
text,
|
|
2965
|
+
...parts === void 0 ? {} : { parts },
|
|
2891
2966
|
...character === void 0 || character === "" ? {} : { character }
|
|
2892
2967
|
});
|
|
2893
2968
|
}
|
|
@@ -3023,20 +3098,10 @@ function readRules(reader) {
|
|
|
3023
3098
|
if (long >= 0) reader.fail("rules", `rule ${long + 1} is longer than 300 characters: keep rules short`);
|
|
3024
3099
|
return kept.length === 0 ? void 0 : kept;
|
|
3025
3100
|
}
|
|
3026
|
-
/**
|
|
3027
|
-
function
|
|
3028
|
-
const value = reader.raw("opening_message");
|
|
3029
|
-
if (value === void 0 || value === null) return void 0;
|
|
3030
|
-
if (typeof value === "string") return value.trim() === "" ? void 0 : [{
|
|
3031
|
-
type: "narration",
|
|
3032
|
-
text: value.trim()
|
|
3033
|
-
}];
|
|
3034
|
-
if (!Array.isArray(value)) {
|
|
3035
|
-
reader.fail("opening_message", "must be text or a list of parts");
|
|
3036
|
-
return;
|
|
3037
|
-
}
|
|
3101
|
+
/** The parts of an opening in the order the player reads them, from the list `reader` holds under `key`; a problem is reported for each part that cannot be read. */
|
|
3102
|
+
function readParts(reader, key) {
|
|
3038
3103
|
const parts = [];
|
|
3039
|
-
for (const part of reader.objects(
|
|
3104
|
+
for (const part of reader.objects(key)) {
|
|
3040
3105
|
const type = part.oneOf("type", ["narration", "utterance"], "narration");
|
|
3041
3106
|
const text = part.string("text", { required: true })?.trim();
|
|
3042
3107
|
if (text === void 0 || text === "") continue;
|
|
@@ -3054,8 +3119,23 @@ function readOpening(reader) {
|
|
|
3054
3119
|
text
|
|
3055
3120
|
});
|
|
3056
3121
|
}
|
|
3122
|
+
if (parts.length > 60) reader.fail(key, `has more than 60 parts`);
|
|
3057
3123
|
return parts.length === 0 ? void 0 : parts;
|
|
3058
3124
|
}
|
|
3125
|
+
/** `opening_message` is plain text (one narration), or a list of parts in the order the player reads them. */
|
|
3126
|
+
function readOpening(reader) {
|
|
3127
|
+
const value = reader.raw("opening_message");
|
|
3128
|
+
if (value === void 0 || value === null) return void 0;
|
|
3129
|
+
if (typeof value === "string") return value.trim() === "" ? void 0 : [{
|
|
3130
|
+
type: "narration",
|
|
3131
|
+
text: value.trim()
|
|
3132
|
+
}];
|
|
3133
|
+
if (!Array.isArray(value)) {
|
|
3134
|
+
reader.fail("opening_message", "must be text or a list of parts");
|
|
3135
|
+
return;
|
|
3136
|
+
}
|
|
3137
|
+
return readParts(reader, "opening_message");
|
|
3138
|
+
}
|
|
3059
3139
|
/** `time_zone` (an IANA name) and `starting_time` (an ISO 8601 date and time, in `time_zone` unless it carries an offset). */
|
|
3060
3140
|
function readClock(reader) {
|
|
3061
3141
|
const timeZone = reader.string("time_zone");
|
|
@@ -5198,6 +5278,6 @@ var SerialQueue = class {
|
|
|
5198
5278
|
return this.tails.size;
|
|
5199
5279
|
}
|
|
5200
5280
|
};
|
|
5201
|
-
export {
|
|
5281
|
+
export { sha as $, LOCATION_FILE_NAME as $t, routeVerdicts as A, openingText as At, questionView as B, parseThingEdit as Bt, ROLE_TABLE as C, itemise as Ct, mergeReports as D, ImportNotFoundError as Dt, askYesNo as E, IMPORT_FOLDER as Et, parseWorldInfoEdit as F, parseCharacterEdit as Ft, deletePreset as G, dialectOf as Gt, PRESET_FILE as H, importRequestOf as Ht, hasSpeech as I, parseFactionEdit as It, loadPreset as J, loadVariables as Jt, isPresetName as K, sourceSummary as Kt, parseOpeningParts as L, parseLoreEdit as Lt, WORLD_FILE_NAME as M, readImportFolder as Mt, parseWorldFile as N, writeChoices as Nt, modeFor as O, ImportStore as Ot, MAX_OPENING_CHARS as P, InvalidEditProblems as Pt, savePresetFromWorld as Q, parseLoreFile as Qt, partsText as R, parsePlaceEdit as Rt, ROLES as S, cleanText as St, announceProblem as T, withPlayer as Tt, createPresetFromGuidance as U, findPanel as Ut, startQuestionsToFile as V, IMPORT_COMMAND as Vt, createPresetFromShipped as W, splitPanels as Wt, readPreset as X, variableProblems as Xt, presetDirectory as Y, parseVariablesFile as Yt, reservePreset as Z, LORE_FILE_NAME as Zt, pruneRelationships as _, updateLorebookInfo as _t, characterDocument as a, FACTION_FILE_NAME as an, writeWorldPrompt as at, classifyItems as b, loadLore as bt, extractStyle as c, parseCharacterFile as cn, deleteLorebook as ct, inert as d, LORE_SCOPES as dn, isLorebookName as dt, parseLocationFile as en, updatePresetInfo as et, loreDocuments as f, WEEKDAY_NAMES as fn, listLorebooks as ft, pruneMembers as g, reserveLorebook as gt, pruneLoreSubjects as h, readLorebook as ht, asWritten as i, parseSlotsFile as in, readWorldPrompt as it, yesAt as j, parseChoices as jt, noAt as k, PICTURE_FILE as kt, extractStyleFrom as l, guarded as ln, deleteLorebookEntry as lt, pruneLinks as m, INTENT_STATUSES as mn, lorebookDirectory as mt, STANDING_PROMPT as n, SLOTS_FILE_NAME as nn, deleteWorldPrompt as nt, emptyExtracted as o, parseFactionFile as on, addLorebookEntries as ot, pruneKnowledge as p, DEFAULT_SLOTS as pn, loadLorebook as pt, listPresets as q, VARIABLES_FILE_NAME as qt, addLore as r, parseItemFile as rn, listWorldPrompts as rt, extractEntities as s, CHARACTER_FILE_NAME as sn, createLorebook as st, SerialQueue as t, ITEM_FILE_NAME as tn, PROSE_PROMPTS as tt, extractVariables as u, LORE_KINDS as un, entriesOf as ut, sourcesFor as v, writeLorebookEntry as vt, isLeftOut as w, partsOf as wt, findEntity as x, subfolders as xt, buildRoster as y, LORE_DIR as yt, combineAnswers as z, parseRelationEdit as zt };
|
|
5202
5282
|
|
|
5203
|
-
//# sourceMappingURL=serial-
|
|
5283
|
+
//# sourceMappingURL=serial-ULGUKWsu.js.map
|