@dsh-agentic-roleplay/core 0.10.0 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/README.md +1 -1
  2. package/lib/{authoring-tools-B4mEdKp_.js → authoring-tools-C6Hzy8jU.js} +1425 -1241
  3. package/lib/authoring.js +18 -7
  4. package/lib/{child-Dtv76jNx.js → child-C8r8AFpm.js} +36 -5
  5. package/lib/client.js +1780 -829
  6. package/lib/client.js.map +1 -1
  7. package/lib/compaction.js +2 -2
  8. package/lib/doctor.js +2 -2
  9. package/lib/{gallery-BZJleAeH.js → gallery-CtG-bDzz.js} +2 -2
  10. package/lib/{generator-Cg6CQc5S.js → generator-nLoPM5MF.js} +2 -2
  11. package/lib/helper.js +1 -1
  12. package/lib/{hooks-DTjHWTfx.js → hooks-BuQ61nvQ.js} +105 -61
  13. package/lib/images.js +4 -4
  14. package/lib/index.js +964 -106
  15. package/lib/{library-GIlrkOim.js → library-CH-ahlm0.js} +2 -2
  16. package/lib/{lorebook-author-tools-BCFFJSle.js → lorebook-author-tools-B6_Jj8oe.js} +6 -6
  17. package/lib/lorebook-authoring.js +4 -4
  18. package/lib/{narration-BVzew4-K.js → narration-B0hConSw.js} +5 -2
  19. package/lib/orchestration.js +50 -13
  20. package/lib/{pack-jail-hsNQNTOf.js → pack-jail-OZSgU85i.js} +10 -10
  21. package/lib/{packs-UpK_cvI4.js → packs-B88bxt8E.js} +2 -2
  22. package/lib/{presenter-agent-DZOgBOR9.js → presenter-agent-D-WNs626.js} +5 -5
  23. package/lib/{preset-Cyoj1ZWa.js → preset-DNuqvMan.js} +4 -4
  24. package/lib/{preset-author-tools-RSta6pHK.js → preset-author-tools-CDwavcTb.js} +6 -6
  25. package/lib/preset-authoring.js +5 -5
  26. package/lib/{presets-CvpsW_10.js → presets-9_MNxwaw.js} +103 -7
  27. package/lib/{reflection-0qTvw2It.js → reflection-BvM0w1R4.js} +132 -92
  28. package/lib/{text-CbGQhK1-.js → text-CzqhC8sT.js} +11 -1
  29. package/lib/{turn-guard-BzNeGNnc.js → turn-guard-Ygdww1Wt.js} +3 -3
  30. package/package.json +2 -2
  31. package/prompts/en/checks/decide-stated.md +1 -0
  32. package/prompts/zh/checks/decide-stated.md +1 -0
@@ -1,10 +1,10 @@
1
1
  import { n as atomicWrite, r as atomicWriteJson } from "./access-CXtaZNbx.js";
2
- import { A as sourcesFor, B as noAt, C as inert, Ct as CHARACTER_FILE_NAME, D as pruneLoreSubjects, Dt as InvalidEditProblems, E as pruneLinks, Et as readIdentifier, F as isLeftOut, Ft as LORE_SCOPES, H as yesAt, It as WEEKDAY_NAMES, J as cleanText, L as askYesNo, Lt as DEFAULT_SLOTS, M as classifyItems, N as findEntity, O as pruneMembers, P as ROLES, Pt as LORE_KINDS, Q as IMPORT_FOLDER, S as extractVariables, St as parseFactionFile, T as pruneKnowledge, Tt as readVariableValues, Y as itemise, Z as withPlayer, _ as characterDocument, _t as ITEM_FILE_NAME, a as isPresetName, b as extractStyle, bt as parseSlotsFile, ft as variableProblems, g as asWritten, gt as parseLocationFile, h as addLore, ht as LOCATION_FILE_NAME, it as readImportFolder, j as buildRoster, k as pruneRelationships, lt as VARIABLES_FILE_NAME, mt as parseLoreFile, nt as openingText, pt as LORE_FILE_NAME, st as parseImportCommand, tt as PICTURE_FILE, ut as loadVariables, v as emptyExtracted, vt as SLOTS_FILE_NAME, w as loreDocuments, wt as parseCharacterFile, xt as FACTION_FILE_NAME, y as extractEntities, yt as parseItemFile } from "./presets-CvpsW_10.js";
3
- import { f as parseSwitchboard } from "./narration-BVzew4-K.js";
4
- import { G as commandFlagKey, J as Reader, Q as PROMPTS_FOLDER, U as plotReferenceProblems, V as loadPlots, X as errorMessage, Y as formatIssue, a as externalWithheld, at as shippedLanguages, et as copyWorldPrompts, gt as COMPARISONS, it as promptProblem, q as readDocument$1, r as externalGiven, rt as listFiles, st as shippedText, t as externalToolLimit, ut as InvalidWorldError } from "./hooks-DTjHWTfx.js";
5
- import { t as trimCharacters } from "./text-CbGQhK1-.js";
6
- import { c as runChild, s as namedModel, t as ChildTimeoutError, u as parseSampling, v as currentTurn } from "./child-Dtv76jNx.js";
7
- import { C as slug, S as norm, T as uniqueKey, _ as writeImageFileMetadata, b as NoEmbeddingService, i as libraryHashes, n as addToLibraryOnce, p as MEDIA_TYPES, r as imageExtensionOf, u as referenceImagePath, w as trOf, x as mapLimit } from "./library-GIlrkOim.js";
2
+ import { $ as IMPORT_FOLDER, A as sourcesFor, At as InvalidEditProblems, Bt as DEFAULT_SLOTS, C as inert, Ct as parseSlotsFile, D as pruneLoreSubjects, Dt as parseCharacterFile, E as pruneLinks, Et as CHARACTER_FILE_NAME, F as ROLE_TABLE, I as isLeftOut, Lt as LORE_KINDS, M as classifyItems, N as findEntity, O as pruneMembers, Ot as readVariableValues, P as ROLES, Q as withPlayer, R as askYesNo, Rt as LORE_SCOPES, S as extractVariables, St as parseItemFile, T as pruneKnowledge, Tt as parseFactionFile, U as yesAt, V as noAt, X as itemise, Y as cleanText, _ as characterDocument, _t as parseLoreFile, a as isPresetName, at as readImportFolder, b as extractStyle, bt as ITEM_FILE_NAME, ft as VARIABLES_FILE_NAME, g as asWritten, gt as LORE_FILE_NAME, h as addLore, ht as variableProblems, j as buildRoster, k as pruneRelationships, kt as readIdentifier, nt as PICTURE_FILE, pt as loadVariables, rt as openingText, st as dialectOf, ut as parseImportCommand, v as emptyExtracted, vt as LOCATION_FILE_NAME, w as loreDocuments, wt as FACTION_FILE_NAME, xt as SLOTS_FILE_NAME, y as extractEntities, yt as parseLocationFile, zt as WEEKDAY_NAMES } from "./presets-9_MNxwaw.js";
3
+ import { f as parseSwitchboard } from "./narration-B0hConSw.js";
4
+ import { G as commandFlagKey, J as Reader, Q as PROMPTS_FOLDER, U as plotReferenceProblems, V as loadPlots, X as errorMessage, Y as formatIssue, a as externalWithheld, at as shippedLanguages, et as copyWorldPrompts, gt as COMPARISONS, it as promptProblem, q as readDocument$1, r as externalGiven, rt as listFiles, st as shippedText, t as externalToolLimit, ut as InvalidWorldError } from "./hooks-BuQ61nvQ.js";
5
+ import { t as trimCharacters } from "./text-CzqhC8sT.js";
6
+ import { c as runChild, s as namedModel, t as ChildTimeoutError, u as parseSampling, y as currentTurn } from "./child-C8r8AFpm.js";
7
+ import { C as slug, S as norm, T as uniqueKey, _ as writeImageFileMetadata, b as NoEmbeddingService, i as libraryHashes, n as addToLibraryOnce, p as MEDIA_TYPES, r as imageExtensionOf, u as referenceImagePath, w as trOf, x as mapLimit } from "./library-CH-ahlm0.js";
8
8
  import { dirname, join, relative } from "node:path";
9
9
  import { existsSync, readFileSync } from "node:fs";
10
10
  import { createHash } from "node:crypto";
@@ -191,7 +191,9 @@ function readCard(file, name) {
191
191
  const book = object(data["character_book"]);
192
192
  const entries = Array.isArray(book?.["entries"]) ? book["entries"].flatMap((entry, index) => entryOf(entry, index) ?? []) : [];
193
193
  const extensions = object(data["extensions"]) ?? {};
194
- const scripts = [extensions["regex_scripts"], object(extensions["tavern_helper"])?.["scripts"]].reduce((count, list) => count + (Array.isArray(list) ? list.length : 0), 0);
194
+ const listed = (list) => Array.isArray(list) ? list.length : 0;
195
+ const regexScripts = listed(extensions["regex_scripts"]);
196
+ const helperScripts = listed(object(extensions["tavern_helper"])?.["scripts"]);
195
197
  return {
196
198
  sourceSpec: spec,
197
199
  name: cardName,
@@ -213,7 +215,9 @@ function readCard(file, name) {
213
215
  entries
214
216
  } },
215
217
  ignored: {
216
- scripts,
218
+ scripts: regexScripts + helperScripts,
219
+ regexScripts,
220
+ helperScripts,
217
221
  extensions: Object.keys(extensions).filter((key) => ![
218
222
  "talkativeness",
219
223
  "fav",
@@ -2197,1318 +2201,1411 @@ function parseWorldFile(text) {
2197
2201
  }
2198
2202
  };
2199
2203
  }
2200
- // src/host/worlds/world-file-io.ts
2201
- const queues = /* @__PURE__ */ new Map();
2204
+ // src/host/import/outcomes.ts
2202
2205
  /**
2203
- * Runs `work` after everything asked for the same folder before it has finished. The import, the editor and the assistant each read `world.json`, change a field and write it
2204
- * back; run side by side, the last one to write would take back what the others changed.
2206
+ * What became of one piece of a card: `carried` is kept word for word (an opening), `converted` made characters, places, things or other entries of the world, `guidance`
2207
+ * became instructions to the narrator, `read` was read for the world but nothing named came of it, `omitted` was left out on purpose, `awaiting` has not been looked at yet.
2205
2208
  */
2206
- function serialized(directory, work) {
2207
- const run = (queues.get(directory) ?? Promise.resolve()).then(work, work);
2208
- const tail = run.then(() => void 0, () => void 0);
2209
- queues.set(directory, tail);
2210
- tail.then(() => {
2211
- if (queues.get(directory) === tail) queues.delete(directory);
2212
- });
2213
- return run;
2214
- }
2215
- /** Changes `world.json` in place: read, `update`, written back in one step with the indentation the file had, nothing else moving in between. @returns the document as written. */
2216
- function updateWorldFile(directory, update) {
2217
- return serialized(directory, async () => {
2218
- const file = join(directory, WORLD_FILE_NAME);
2219
- const text = await readFile(file, "utf8");
2220
- const document = JSON.parse(text.replace(/^/, ""));
2221
- await update(document);
2222
- await atomicWrite(file, JSON.stringify(document, null, /^([ \t]+)"/m.exec(text)?.[1] ?? " ") + (text.endsWith("\n") || text.trim() === "" ? "\n" : ""));
2223
- return document;
2224
- });
2225
- }
2226
- // src/host/worlds/entity-folders.ts
2227
- /** The five kinds of entity a world is made of: where each lives (`<directory>/<id>/<file>`) and the key of its group in the importer's output. */
2228
- const ENTITY_FOLDERS = [
2229
- {
2230
- key: "characters",
2231
- directory: CHARACTERS_DIR,
2232
- file: CHARACTER_FILE_NAME
2233
- },
2234
- {
2235
- key: "locations",
2236
- directory: LOCATIONS_DIR,
2237
- file: LOCATION_FILE_NAME
2238
- },
2239
- {
2240
- key: "factions",
2241
- directory: FACTIONS_DIR,
2242
- file: FACTION_FILE_NAME
2243
- },
2244
- {
2245
- key: "items",
2246
- directory: ITEMS_DIR,
2247
- file: ITEM_FILE_NAME
2248
- },
2249
- {
2250
- key: "lore",
2251
- directory: LORE_DIR,
2252
- file: LORE_FILE_NAME
2253
- }
2209
+ const OUTCOMES = [
2210
+ "carried",
2211
+ "converted",
2212
+ "guidance",
2213
+ "read",
2214
+ "omitted",
2215
+ "awaiting"
2254
2216
  ];
2255
- // src/host/import/finish.ts
2256
- const STAGE = "stage";
2257
- const MAX_DESCRIPTION = 500;
2258
- const MAX_TAGS = 8;
2259
- const MAX_TAG = 30;
2260
- /** The card's other greetings, as the other openings a player may start with: the first message and the alternates that were not chosen. */
2261
- function alternateOpenings(card, chosen, player) {
2262
- if (chosen.kind === "none") return [];
2263
- return [{
2264
- title: "First message",
2265
- text: card.firstMessage,
2266
- taken: chosen.kind === "first"
2267
- }, ...card.alternateGreetings.map((text, index) => ({
2268
- title: `Alternate ${index + 1}`,
2269
- text,
2270
- taken: chosen.kind === "alternate" && chosen.index === index
2271
- }))].flatMap(({ title, text, taken }) => {
2272
- const cleaned = taken || text.trim() === "" ? "" : openingMessage(text, card.name, player);
2273
- return cleaned === "" || cleaned.length > 2e4 ? [] : [{
2274
- title,
2275
- text: cleaned
2276
- }];
2277
- }).slice(0, 30);
2278
- }
2279
- /** The character the card's picture shows: the one that carries the card's name, or the only one there is. */
2280
- function cardCharacter(characters, cardName) {
2281
- return characters.find((document) => norm(String(document["name"])) === norm(cardName)) ?? (characters.length === 1 ? characters[0] : void 0);
2217
+ const rolesOf$1 = (source, id) => [...new Set(Object.entries(source.records.classified ?? {}).filter(([key]) => baseId(key) === id).flatMap(([, entry]) => entry.roles))];
2218
+ function outcomeOf(source, piece, made) {
2219
+ const { choices, card } = source;
2220
+ const reported = source.records.report !== void 0;
2221
+ if (piece.id === "field:creator-notes") return {
2222
+ outcome: "omitted",
2223
+ because: "notes to people, not part of the world"
2224
+ };
2225
+ if (piece.id === "field:first-message" || piece.id.startsWith("field:greeting-")) {
2226
+ if (choices.opening.kind === "none") return {
2227
+ outcome: "omitted",
2228
+ because: "the person chose no opening"
2229
+ };
2230
+ return reported ? {
2231
+ outcome: "carried",
2232
+ because: "an opening of the world"
2233
+ } : { outcome: "awaiting" };
2234
+ }
2235
+ if (piece.id === "field:system-prompt" || piece.id === "field:post-history") return reported ? {
2236
+ outcome: "guidance",
2237
+ because: "instructions to the narrator"
2238
+ } : { outcome: "awaiting" };
2239
+ if (piece.enabled !== void 0 && choices.disabledEntries.includes(piece.id)) return {
2240
+ outcome: "omitted",
2241
+ because: "left off by the person"
2242
+ };
2243
+ if (piece.enabled === false && !(card.book?.entries.find((entry) => entry.id === piece.id)?.enabled ?? true) && !piece.used) return {
2244
+ outcome: "omitted",
2245
+ because: "switched off in the card"
2246
+ };
2247
+ if (source.records.classified === void 0) return { outcome: "awaiting" };
2248
+ const roles = rolesOf$1(source, piece.id);
2249
+ if (roles.length === 0) return {
2250
+ outcome: "awaiting",
2251
+ because: "the import did not classify it"
2252
+ };
2253
+ if (roles.every((role) => ROLE_TABLE[role].leftOut)) return roles.includes("instruction") ? {
2254
+ outcome: "guidance",
2255
+ because: "how to run a story, not facts of this one"
2256
+ } : {
2257
+ outcome: "omitted",
2258
+ because: roles.includes("template") ? "a template or format of the source tool" : "nothing to do with the story"
2259
+ };
2260
+ return made.length > 0 ? { outcome: "converted" } : {
2261
+ outcome: "read",
2262
+ because: "no character, place or thing is named after it"
2263
+ };
2282
2264
  }
2283
- /** Makes the card's picture the canonical reference portrait of its character, as an image the person brought. */
2284
- async function writePortrait(directory, picture, characters, cardName) {
2285
- const character = cardCharacter(characters, cardName);
2286
- if (character === void 0) return false;
2287
- const id = String(character["id"]);
2288
- const file = referenceImagePath(directory, {
2289
- kind: "character",
2290
- localId: id
2291
- });
2292
- await writeAtomically(file, picture);
2293
- await writeImageFileMetadata(file, {
2294
- schema: "dsh-roleplay-image/v3",
2295
- purpose: "reference",
2296
- origin: "uploaded",
2297
- reusable: false,
2298
- createdAt: (/* @__PURE__ */ new Date()).toISOString(),
2299
- originalName: PICTURE_FILE,
2300
- description: `${String(character["name"])}: the portrait that came with the character card.`,
2301
- tags: [String(character["name"])],
2302
- subjects: [{
2303
- kind: "character",
2304
- localId: id,
2305
- name: String(character["name"])
2306
- }]
2307
- });
2308
- return true;
2265
+ /** What became of every piece of the card, from the import's records; `made` is `entitiesByPiece` of the same source. */
2266
+ function pieceOutcomes(source, made) {
2267
+ return source.pieces.map((piece) => ({
2268
+ piece: piece.id,
2269
+ label: piece.label,
2270
+ ...outcomeOf(source, piece, made.get(piece.id) ?? []),
2271
+ madeInto: [...made.get(piece.id) ?? []]
2272
+ }));
2309
2273
  }
2310
- /** The card's opening as the world's opening message: plain paragraphs, without the markup the card's tool read as emphasis. */
2311
- function openingMessage(raw, cardName, player) {
2312
- const text = cleanText(raw, cardName).replace(/<\/?[a-z][^>\n]*>/gi, "").replace(/```[a-z]*\n?|```/g, "").replace(/\*\*([^*\n]+)\*\*/g, "$1").replace(/(^|[^*])\*(?!\s)([^*\n]+?)(?<!\s)\*(?!\*)/g, "$1$2").replace(/\n{3,}/g, "\n\n").trim();
2313
- return withPlayer(text, player);
2274
+ const outcomeCounts = (outcomes) => Object.fromEntries(OUTCOMES.map((outcome) => [outcome, outcomes.filter((entry) => entry.outcome === outcome).length]));
2275
+ // src/host/import/card-tools.ts
2276
+ /** The most of a piece one `card_read` returns; a long lorebook entry is read in turns. */
2277
+ const READ_CHARS = 6e3;
2278
+ const SNIPPET_CHARS = 240;
2279
+ const DEFAULT_HITS = 8;
2280
+ const MAX_HITS = 20;
2281
+ const baseId = (id) => id.replace(/#\d+$/, "");
2282
+ /** The folder inside `.import` where a newer version of the card waits to be applied to the world. */
2283
+ const UPDATE_FOLDER = "update";
2284
+ /** Everything the card holds, in the order the card has it. */
2285
+ function cardPieces(card, choices) {
2286
+ const read = new Set(itemise(card, choices).map((item) => baseId(item.id)));
2287
+ const field = (id, label, text) => text.trim() === "" ? [] : [{
2288
+ id,
2289
+ label,
2290
+ title: "",
2291
+ keys: [],
2292
+ text,
2293
+ used: read.has(id)
2294
+ }];
2295
+ return [
2296
+ ...field("field:description", "description", card.description),
2297
+ ...field("field:personality", "personality", card.personality),
2298
+ ...field("field:scenario", "scenario", card.scenario),
2299
+ ...field("field:example", "example messages", card.messageExample),
2300
+ ...field("field:first-message", "first message", card.firstMessage),
2301
+ ...card.alternateGreetings.flatMap((text, index) => field(`field:greeting-${index + 1}`, `alternate greeting ${index + 1}`, text)),
2302
+ ...field("field:creator-notes", "creator's notes", card.creatorNotes),
2303
+ ...field("field:system-prompt", "system prompt", card.systemPrompt),
2304
+ ...field("field:post-history", "post-history instructions", card.postHistoryInstructions),
2305
+ ...(card.book?.entries ?? []).filter((entry) => entry.content.trim() !== "").map((entry) => ({
2306
+ id: entry.id,
2307
+ label: entry.title === "" ? entry.id : `entry “${entry.title}”`,
2308
+ title: entry.title,
2309
+ keys: entry.keys,
2310
+ text: entry.content,
2311
+ used: read.has(entry.id),
2312
+ enabled: entry.enabled
2313
+ }))
2314
+ ];
2314
2315
  }
2315
- /** Every player marker in the strings of a value, replaced by the player's character or a plain word for the player; the value is changed in place. */
2316
- function replacePlayer(value, player) {
2317
- if (Array.isArray(value)) value.forEach((entry, index) => {
2318
- if (typeof entry === "string") value[index] = withPlayer(entry, player);
2319
- else replacePlayer(entry, player);
2320
- });
2321
- else if (typeof value === "object" && value !== null) {
2322
- const record = value;
2323
- for (const key of Object.keys(record)) {
2324
- const entry = record[key];
2325
- if (typeof entry === "string") record[key] = withPlayer(entry, player);
2326
- else replacePlayer(entry, player);
2327
- }
2316
+ async function optional(file) {
2317
+ try {
2318
+ return JSON.parse(await readFile(file, "utf8"));
2319
+ } catch (error) {
2320
+ if (error.code === "ENOENT") return void 0;
2321
+ throw error;
2328
2322
  }
2329
2323
  }
2330
- /**
2331
- * Every character knows every place of the imported world and the ways between them, unless its own file already says what it knows: a card describes a
2332
- * world its people live in, and without this the closed-world rules would forbid a walk to a famous street. Changes the documents in place.
2333
- */
2334
- function shareThePlaces(characters, locations) {
2335
- const places = locations.map((location) => String(location["id"]));
2336
- const ways = [];
2337
- for (const location of locations) {
2338
- const from = String(location["id"]);
2339
- for (const exit of Array.isArray(location["exits"]) ? location["exits"] : []) {
2340
- const to = String(exit["to"]);
2341
- ways.push({
2342
- from,
2343
- to
2344
- });
2345
- if (exit["one_way"] !== true) ways.push({
2346
- from: to,
2347
- to: from
2348
- });
2349
- }
2350
- }
2351
- for (const character of characters) {
2352
- const knowledge = typeof character["knowledge"] === "object" && character["knowledge"] !== null ? character["knowledge"] : {};
2353
- if (knowledge["locations"] !== void 0 || knowledge["exits"] !== void 0) continue;
2354
- knowledge["locations"] = places;
2355
- knowledge["exits"] = ways.filter((way, index) => ways.findIndex((other) => other.from === way.from && other.to === way.to) === index);
2356
- character["knowledge"] = knowledge;
2324
+ /** The card kept in a world's import folder, and what the import made of it. The tools exist as long as the card does. */
2325
+ async function readSource(directory, withRecords, version = "current") {
2326
+ const folder = join(directory, IMPORT_FOLDER);
2327
+ let read;
2328
+ try {
2329
+ read = await readImportFolder(version === "new" ? join(folder, UPDATE_FOLDER) : folder);
2330
+ } catch (error) {
2331
+ if (error.code === "ENOENT") throw new Error(version === "new" ? "no newer version of the card is waiting to update this world" : "this world has no source card: it was not imported from one, or its .import folder was removed");
2332
+ throw error;
2357
2333
  }
2334
+ const records = withRecords && version === "current" ? {
2335
+ classified: await optional(join(folder, "classification.json")),
2336
+ roster: await optional(join(folder, "roster.json")),
2337
+ report: await optional(join(folder, "report.json"))
2338
+ } : {};
2339
+ return {
2340
+ card: read.card,
2341
+ choices: read.choices,
2342
+ pieces: cardPieces(read.card, read.choices),
2343
+ records
2344
+ };
2358
2345
  }
2359
- /** Places that contain each other in a circle are untangled by taking the parent off the one that closes it. */
2360
- function breakParentCircles(locations) {
2361
- const notes = [];
2362
- const byId = new Map(locations.map((location) => [String(location["id"]), location]));
2363
- for (const location of locations) {
2364
- const seen = /* @__PURE__ */ new Set([String(location["id"])]);
2365
- for (let up = location["parent"]; up !== void 0; up = byId.get(up)?.["parent"]) {
2366
- if (seen.has(up)) {
2367
- notes.push(`${String(location["name"])}: the places contain each other in a circle, so it is not put inside another place`);
2368
- delete location["parent"];
2369
- break;
2370
- }
2371
- seen.add(up);
2372
- }
2373
- }
2374
- return notes;
2346
+ const rolesOf = (classified, id) => {
2347
+ if (classified === void 0) return [];
2348
+ return [...new Set(Object.entries(classified).filter(([key]) => baseId(key) === id).flatMap(([, entry]) => entry.roles))];
2349
+ };
2350
+ function snippet$1(text, at, length) {
2351
+ const start = Math.max(0, at - Math.floor((SNIPPET_CHARS - length) / 2));
2352
+ const end = Math.min(text.length, start + SNIPPET_CHARS);
2353
+ return `${start > 0 ? "…" : ""}${text.slice(start, end).replace(/\s+/g, " ")}${end < text.length ? "…" : ""}`;
2375
2354
  }
2376
- const WORLD_SCHEMA = {
2377
- type: "object",
2378
- properties: {
2379
- description: {
2380
- type: "string",
2381
- description: `Two or three sentences, at most ${MAX_DESCRIPTION} characters.`
2382
- },
2383
- tags: {
2384
- type: "array",
2385
- items: { type: "string" },
2386
- description: `At most ${MAX_TAGS} short tags.`
2387
- },
2388
- starting_time: {
2389
- type: "string",
2390
- description: "When the story begins, as an ISO 8601 date and time such as 1921-10-23T08:00:00, only when the text states the date or year; otherwise an empty string."
2391
- }
2392
- },
2393
- required: [
2394
- "description",
2395
- "tags",
2396
- "starting_time"
2397
- ],
2398
- additionalProperties: false
2399
- };
2400
- const STARTING_TIME = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2})?)?$/;
2401
- function worldJob(text, tr, card, roster, scenario, opening) {
2402
- const names = (kind) => roster.filter((entity) => entity.kind === kind).slice(0, 15).map((entity) => entity.name).join("; ");
2403
- return {
2404
- persona: text("import/world-summary"),
2405
- maxTokens: 1500,
2406
- prompt: [
2407
- tr("Card: {name}", { name: card.name }),
2408
- ...card.tags.length === 0 ? [] : [tr("Card tags: {tags}", { tags: card.tags.join(", ") })],
2409
- `${tr("Characters:")} ${names("character") || tr("(none)")}`,
2410
- `${tr("Places:")} ${names("place") || tr("(none)")}`,
2411
- `${tr("Scenario:")}\n${scenario.slice(0, 1200) || tr("(none)")}`,
2412
- `${tr("Opening:")}\n${opening.slice(0, 1200) || tr("(none)")}`,
2413
- `${tr("Description:")}\n${cleanText(card.description, card.name).slice(0, 1200) || tr("(none)")}`,
2414
- `${tr("Creator's notes:")}\n${cleanText(card.creatorNotes, card.name).slice(0, 600) || tr("(none)")}`
2415
- ].join("\n\n"),
2416
- result: WORLD_SCHEMA,
2417
- check: (value) => {
2418
- if (value.description.trim() === "" || value.description.length > MAX_DESCRIPTION) return `the description must be written and at most ${MAX_DESCRIPTION} characters`;
2419
- if (value.tags.length > MAX_TAGS || value.tags.some((tag) => tag.trim() === "" || tag.length > MAX_TAG)) return `give at most ${MAX_TAGS} tags of at most ${MAX_TAG} characters`;
2420
- if ((value.starting_time ?? "").trim() !== "" && (!STARTING_TIME.test(value.starting_time.trim()) || Number.isNaN(Date.parse(value.starting_time.trim())))) return "starting_time must be an ISO 8601 date and time such as 1921-10-23T08:00:00, or empty";
2355
+ function searchPieces(pieces, query, limit) {
2356
+ const phrase = query.trim().toLowerCase();
2357
+ if (phrase === "") return [];
2358
+ const words = [...new Set(phrase.split(/[\s,;,、]+/).filter((word) => word.length >= (/^[\x00-\x7f]+$/.test(word) ? 3 : 2)))];
2359
+ const count = (text, needle) => {
2360
+ let n = 0;
2361
+ let first = -1;
2362
+ for (let at = text.indexOf(needle); at >= 0; at = text.indexOf(needle, at + needle.length)) {
2363
+ n += 1;
2364
+ if (first < 0) first = at;
2421
2365
  }
2422
- };
2423
- }
2424
- /** The description and tags of the world: asked of one call, and from the card's own fields when that fails. */
2425
- async function describeWorld(ctx, card, roster, items, opening) {
2426
- const scenario = items.filter((item) => item.id.startsWith("field:scenario")).map((item) => item.text).join("\n\n");
2427
- if (ctx.noModel === true) return {
2428
- description: (scenario || cleanText(card.description, card.name)).slice(0, MAX_DESCRIPTION).trim(),
2429
- tags: card.tags.filter((tag) => tag.trim() !== "" && tag.length <= MAX_TAG).slice(0, MAX_TAGS)
2430
- };
2431
- try {
2432
- const answer = await ctx.run(worldJob(ctx.text, trOf(ctx), card, roster, scenario, opening));
2433
2366
  return {
2434
- description: answer.description.trim(),
2435
- tags: [...new Set(answer.tags.map((tag) => tag.trim()))],
2436
- ...(answer.starting_time ?? "").trim() === "" ? {} : { startingTime: answer.starting_time.trim() }
2367
+ n,
2368
+ first
2437
2369
  };
2438
- } catch (error) {
2370
+ };
2371
+ const look = (text) => {
2372
+ const lowered = text.toLowerCase();
2373
+ const whole = count(lowered, phrase);
2374
+ if (whole.n > 0) return {
2375
+ matches: whole.n,
2376
+ score: whole.n * 4 + 100,
2377
+ at: whole.first,
2378
+ length: phrase.length
2379
+ };
2380
+ const found = words.map((word) => ({
2381
+ word,
2382
+ ...count(lowered, word)
2383
+ })).filter((entry) => entry.n > 0);
2384
+ if (found.length === 0) return void 0;
2385
+ const best = found.reduce((a, b) => a.first <= b.first ? a : b, found[0]);
2439
2386
  return {
2440
- description: (scenario || cleanText(card.description, card.name)).slice(0, MAX_DESCRIPTION).trim(),
2441
- tags: card.tags.filter((tag) => tag.trim() !== "" && tag.length <= MAX_TAG).slice(0, MAX_TAGS),
2442
- note: `the description of the world was taken from the card as it is: ${error instanceof Error ? error.message : String(error)}`
2387
+ matches: found.reduce((sum, entry) => sum + entry.n, 0),
2388
+ score: found.length * 10 + found.reduce((sum, entry) => sum + entry.n, 0),
2389
+ at: best.first,
2390
+ length: best.word.length
2443
2391
  };
2444
- }
2445
- }
2446
- async function hasEntities(world) {
2447
- for (const { directory } of ENTITY_FOLDERS) try {
2448
- if ((await readdir(join(world, directory))).some((name) => !name.startsWith("."))) return true;
2449
- } catch (error) {
2450
- if (error.code !== "ENOENT") throw error;
2451
- }
2452
- return false;
2392
+ };
2393
+ return pieces.flatMap((piece) => {
2394
+ const inText = look(piece.text);
2395
+ if (inText !== void 0) return [{
2396
+ piece,
2397
+ in: "text",
2398
+ matches: inText.matches,
2399
+ score: inText.score,
2400
+ snippet: snippet$1(piece.text, inText.at, inText.length)
2401
+ }];
2402
+ const head = [piece.title, ...piece.keys].filter((part) => part !== "").join(" · ");
2403
+ const inHead = look(head);
2404
+ return inHead === void 0 ? [] : [{
2405
+ piece,
2406
+ in: "title or keys",
2407
+ matches: inHead.matches,
2408
+ score: inHead.score - 50,
2409
+ snippet: `${piece.title === "" ? "" : `title: ${piece.title}`}${piece.keys.length === 0 ? "" : `${piece.title === "" ? "" : "; "}keys: ${piece.keys.join(", ")}`}`
2410
+ }];
2411
+ }).sort((a, b) => b.score - a.score).slice(0, limit).map(({ piece, matches, in: where, snippet: text }) => ({
2412
+ piece,
2413
+ matches,
2414
+ in: where,
2415
+ snippet: text
2416
+ }));
2453
2417
  }
2454
- const writeAtomically = (file, text) => atomicWrite(file, text, { createDirectory: true });
2418
+ const versionParameter = {
2419
+ type: "string",
2420
+ enum: ["current", "new"],
2421
+ description: "current (the default) is the card the world was made from; new is the newer version waiting to update it, when card_diff says there is one. Piece ids are those of that version."
2422
+ };
2423
+ const output$1 = {
2424
+ schema: { type: "json" },
2425
+ render: (_args, value) => [{
2426
+ type: "text",
2427
+ text: JSON.stringify(value, null, 1)
2428
+ }]
2429
+ };
2430
+ const json$1 = (value) => JSON.parse(JSON.stringify(value));
2431
+ const idOf = (exec) => {
2432
+ const chat = exec.agent?.session.id;
2433
+ const world = chat === void 0 ? void 0 : authoredWorldOf(chat);
2434
+ if (world === void 0) throw new Error("this tool is available only in a world authoring chat");
2435
+ return world;
2436
+ };
2455
2437
  /**
2456
- * Writes what was extracted into the world, all at once. The documents are first written to a staging folder and read back as the world will
2457
- * read them when a session starts; only when every part of the world is valid are they moved in. The world's own file gets its description, tags, opening and cover.
2438
+ * Read-only tools over the card a world was imported from, which stays in the world's `.import` folder: they work while that folder holds the card and say so when it does not.
2439
+ * `records` adds what the import made of the card (the role of each piece, who it was used for); it is off only when a test compares the agent with and without it.
2458
2440
  */
2459
- async function finishImport(ctx, input) {
2460
- const { directory, card, choices, roster, items, classified, extracted } = input;
2461
- if (await hasEntities(directory)) return {
2462
- ok: false,
2463
- problems: ["the world already has characters, places, factions, things or lore: an import only fills an empty world"]
2441
+ function cardTools(worlds, options = {}) {
2442
+ const withRecords = options.records !== false;
2443
+ const source = async (exec, version = "current") => {
2444
+ const id = idOf(exec);
2445
+ const record = await worlds.find(id);
2446
+ if (record === void 0) throw new Error(`the authored world "${id}" no longer exists`);
2447
+ return await readSource(record.directory, withRecords, version);
2464
2448
  };
2465
- const playing = extracted.characters.find((document) => document["role"] === "playable");
2466
- const playerName = playing === void 0 ? void 0 : String(playing["name"]);
2467
- for (const key of [
2468
- "characters",
2469
- "locations",
2470
- "factions",
2471
- "items",
2472
- "lore",
2473
- "commands",
2474
- "triggers",
2475
- "variables"
2476
- ]) replacePlayer(extracted[key], playerName);
2477
- const notes = [
2478
- ...input.notes,
2479
- ...extracted.notes,
2480
- ...breakParentCircles(extracted.locations)
2481
- ];
2482
- shareThePlaces(extracted.characters, extracted.locations);
2483
- const stage = join(directory, IMPORT_FOLDER, STAGE);
2484
- await rm(stage, {
2485
- recursive: true,
2486
- force: true
2487
- });
2488
- const mend = () => {
2489
- const view = {
2490
- ...extracted,
2491
- notes
2492
- };
2493
- pruneLoreSubjects(view);
2494
- pruneMembers(view);
2495
- pruneLinks(view);
2496
- pruneKnowledge(view);
2497
- pruneRelationships(view);
2449
+ const versionOf = (value) => {
2450
+ if (value === void 0 || value === "current") return "current";
2451
+ if (value === "new") return "new";
2452
+ throw new Error("version must be current or new");
2498
2453
  };
2499
- mend();
2500
- try {
2501
- if (extracted.variables.length > 0) await writeAtomically(join(stage, VARIABLES_FILE_NAME), `${JSON.stringify(extracted.variables, null, 2)}\n`);
2502
- if (extracted.commands.length > 0) await writeAtomically(join(stage, COMMANDS_FILE_NAME), `${JSON.stringify({ commands: extracted.commands }, null, 2)}\n`);
2503
- if (extracted.triggers.length > 0) await writeAtomically(join(stage, TRIGGERS_FILE_NAME), `${JSON.stringify(extracted.triggers, null, 2)}\n`);
2504
- for (const { key, directory: folder, file } of ENTITY_FOLDERS) for (const document of extracted[key]) await writeAtomically(join(stage, folder, String(document["id"]), file), `${JSON.stringify(document, null, 2)}\n`);
2505
- for (const { id, text } of extracted.prompts) {
2506
- const problem = promptProblem("en", id, text);
2507
- if (problem !== void 0) return {
2508
- ok: false,
2509
- problems: [`prompt ${id}: ${problem}`]
2510
- };
2511
- }
2512
- if (extracted.variables.length > 0) await rename(join(stage, VARIABLES_FILE_NAME), join(directory, VARIABLES_FILE_NAME));
2513
- if (extracted.commands.length > 0) await rename(join(stage, COMMANDS_FILE_NAME), join(directory, COMMANDS_FILE_NAME));
2514
- if (extracted.triggers.length > 0) await rename(join(stage, TRIGGERS_FILE_NAME), join(directory, TRIGGERS_FILE_NAME));
2515
- for (const { directory: folder } of ENTITY_FOLDERS) {
2516
- const from = join(stage, folder);
2517
- if (await stat(from).then(() => true, () => false)) await rename(from, join(directory, folder));
2518
- }
2519
- } finally {
2520
- await rm(stage, {
2521
- recursive: true,
2522
- force: true
2523
- });
2524
- }
2525
- const check = await checkWorld({
2526
- directory,
2527
- summary: { name: choices.name }
2528
- });
2529
- if (!check.ok) notes.push(`the world was written as a draft: it would not start yet (${check.problems.length + check.more} problem(s) in ${check.summary ?? "the world"})`);
2530
- const player = extracted.characters.find((document) => document["role"] === "playable");
2531
- for (const { id, text } of extracted.prompts) for (const language of shippedLanguages()) await writeAtomically(join(directory, PROMPTS_FOLDER, language, `${id}.md`), `${withPlayer(text, player === void 0 ? void 0 : String(player["name"]))}\n`);
2532
- const opening = openingText(card, choices.opening);
2533
- const described = await describeWorld(ctx, card, roster, items, opening ?? "");
2534
- if (described.note !== void 0) notes.push(described.note);
2535
- described.description = withPlayer(described.description, player === void 0 ? void 0 : String(player["name"]));
2536
- let cover = false;
2537
- if (input.picture !== void 0) {
2538
- await writeAtomically(join(directory, "media", "files", "cover.png"), input.picture);
2539
- await writeAtomically(join(directory, "media", "manifest.jsonl"), `${JSON.stringify({
2540
- id: "cover",
2541
- file: "media/files/cover.png"
2542
- })}\n`);
2543
- cover = true;
2544
- }
2545
- const alternates = alternateOpenings(card, choices.opening, player === void 0 ? void 0 : String(player["name"]));
2546
- await updateWorldFile(directory, (world) => {
2547
- world["description"] = described.description;
2548
- if (described.startingTime !== void 0 && world["starting_time"] === void 0) world["starting_time"] = described.startingTime;
2549
- world["metadata"] = {
2550
- ...typeof world["metadata"] === "object" && world["metadata"] !== null ? world["metadata"] : {},
2551
- tags: described.tags
2552
- };
2553
- if (opening !== void 0) world["opening_message"] = openingMessage(opening, card.name, player === void 0 ? void 0 : String(player["name"]));
2554
- if (alternates.length > 0) world["alternate_openings"] = alternates;
2555
- if (card.creator.trim() !== "" && world["author"] === void 0) world["author"] = { name: card.creator.trim() };
2556
- if (cover) world["cover_media_id"] = "cover";
2557
- });
2558
- let portrait = false;
2559
- if (input.picture !== void 0) try {
2560
- portrait = await writePortrait(directory, input.picture, extracted.characters, card.name);
2561
- } catch (error) {
2562
- notes.push(`the card's picture could not be used as a portrait: ${error instanceof Error ? error.message : String(error)}`);
2563
- }
2564
- const taken = (role) => items.filter((item) => {
2565
- const classification = classified[item.id];
2566
- return classification !== void 0 && isLeftOut(classification) && classification.roles[0] === role;
2567
- }).length;
2568
- const report = {
2569
- prompts: extracted.prompts.map((entry) => entry.id),
2570
- variables: extracted.variables.length,
2571
- counts: {
2572
- characters: extracted.characters.length,
2573
- places: extracted.locations.length,
2574
- factions: extracted.factions.length,
2575
- things: extracted.items.length,
2576
- lore: extracted.lore.length
2577
- },
2578
- hasPlayableCharacter: player !== void 0,
2579
- opening: opening === void 0 ? "none" : "imported",
2580
- alternateOpenings: alternates.length,
2581
- cover,
2582
- portrait,
2583
- leftOut: {
2584
- instructions: taken("instruction"),
2585
- templates: taken("template"),
2586
- irrelevant: taken("irrelevant"),
2587
- scripts: card.ignored.scripts
2588
- },
2589
- pictures: input.pictures ?? {
2590
- found: 0,
2591
- added: 0
2592
- },
2593
- notes
2594
- };
2595
- await writeAtomically(join(directory, IMPORT_FOLDER, "report.json"), JSON.stringify(report, null, 2));
2596
- return {
2597
- ok: true,
2598
- report,
2599
- ...check.ok ? {} : { draft: check }
2454
+ const find = (pieces, id) => {
2455
+ const found = pieces.find((piece) => piece.id === id);
2456
+ if (found === void 0) throw new Error(`the card has no piece "${id}": use an id from card_overview or card_search`);
2457
+ return found;
2600
2458
  };
2459
+ return [
2460
+ defineTool({
2461
+ name: "card_overview",
2462
+ description: "The card this world was imported from, which is kept: its name, creator and tags, every piece of it (field, greeting or lorebook entry) with its size, and, from the import's own records, what each piece was taken to be and which characters, places and things were made from it. Start here when the person asks where something in the world came from.",
2463
+ parameters: {},
2464
+ output: output$1,
2465
+ isConcurrencySafe: () => true,
2466
+ async execute(_args, exec) {
2467
+ const { card, choices, pieces, records } = await source(exec);
2468
+ const madeFrom = entitiesByPiece({
2469
+ card,
2470
+ choices,
2471
+ pieces,
2472
+ records
2473
+ });
2474
+ const made = (id) => madeFrom.get(id) ?? [];
2475
+ const outcomes = withRecords ? pieceOutcomes({
2476
+ card,
2477
+ choices,
2478
+ pieces,
2479
+ records
2480
+ }, madeFrom) : [];
2481
+ return json$1({
2482
+ card: {
2483
+ name: card.name,
2484
+ creator: card.creator,
2485
+ version: card.characterVersion,
2486
+ tags: card.tags,
2487
+ format: card.sourceSpec,
2488
+ ignoredScripts: card.ignored.scripts
2489
+ },
2490
+ ...withRecords ? { outcomes: outcomeCounts(outcomes) } : {},
2491
+ pieces: pieces.map((piece) => ({
2492
+ id: piece.id,
2493
+ label: piece.label,
2494
+ characters: piece.text.length,
2495
+ readByTheImport: piece.used,
2496
+ ...withRecords ? {
2497
+ outcome: outcomes.find((entry) => entry.piece === piece.id)?.outcome,
2498
+ because: outcomes.find((entry) => entry.piece === piece.id)?.because
2499
+ } : {},
2500
+ ...piece.enabled === void 0 ? {} : { switchedOnInCard: piece.enabled },
2501
+ ...withRecords ? {
2502
+ takenToBe: rolesOf(records.classified, piece.id),
2503
+ madeInto: made(piece.id)
2504
+ } : {}
2505
+ })),
2506
+ ...records.roster === void 0 ? {} : { imported: records.roster.map((entity) => ({
2507
+ name: entity.name,
2508
+ kind: entity.kind,
2509
+ aliases: entity.aliases,
2510
+ playedByThePlayer: entity.player
2511
+ })) },
2512
+ ...records.report === void 0 ? {} : {
2513
+ importNotes: records.report.notes,
2514
+ leftOut: records.report.leftOut
2515
+ },
2516
+ note: "The import's records are its own judgement and can be wrong or incomplete; check them against the text of the piece with card_read."
2517
+ });
2518
+ }
2519
+ }),
2520
+ defineTool({
2521
+ name: "card_search",
2522
+ description: "Searches everything the card says (fields, greetings, lorebook entries and their keys) for a name or phrase and returns the pieces that contain it, best first, each with a short snippet and whether it was found in the text or only in the title or keys of an entry. A hit is a pointer, not evidence: read the piece with card_read before you answer from it.",
2523
+ parameters: {
2524
+ query: {
2525
+ type: "string",
2526
+ required: true,
2527
+ description: "A name or phrase to look for, in the card's own language."
2528
+ },
2529
+ version: versionParameter,
2530
+ limit: {
2531
+ type: "number",
2532
+ description: `How many pieces to return, at most ${MAX_HITS} (default ${DEFAULT_HITS}).`
2533
+ }
2534
+ },
2535
+ output: output$1,
2536
+ isConcurrencySafe: () => true,
2537
+ async execute(args, exec) {
2538
+ const { pieces } = await source(exec, versionOf(args.version));
2539
+ const limit = Math.min(MAX_HITS, Math.max(1, Math.floor(typeof args.limit === "number" && Number.isFinite(args.limit) ? args.limit : DEFAULT_HITS)));
2540
+ const hits = searchPieces(pieces, args.query, limit);
2541
+ return json$1({
2542
+ query: args.query,
2543
+ found: hits.length,
2544
+ hits: hits.map(({ piece, matches, in: where, snippet: text }) => ({
2545
+ piece: piece.id,
2546
+ label: piece.label,
2547
+ matches,
2548
+ foundIn: where,
2549
+ snippet: text
2550
+ }))
2551
+ });
2552
+ }
2553
+ }),
2554
+ defineTool({
2555
+ name: "card_read",
2556
+ description: `The text of one piece of the card, word for word as the card has it (the card's own placeholders such as {{user}} included). A long piece is read in turns of ${READ_CHARS} characters: when "next" is given, call again with that offset.`,
2557
+ parameters: {
2558
+ piece: {
2559
+ type: "string",
2560
+ required: true,
2561
+ description: "A piece id from card_overview, card_search or card_diff, such as field:description or entry:3."
2562
+ },
2563
+ version: versionParameter,
2564
+ offset: {
2565
+ type: "number",
2566
+ description: "Where to start reading, in characters (default 0)."
2567
+ }
2568
+ },
2569
+ output: output$1,
2570
+ isConcurrencySafe: () => true,
2571
+ async execute(args, exec) {
2572
+ const { pieces } = await source(exec, versionOf(args.version));
2573
+ const piece = find(pieces, args.piece);
2574
+ const from = Math.min(piece.text.length, Math.max(0, Math.floor(typeof args.offset === "number" && Number.isFinite(args.offset) ? args.offset : 0)));
2575
+ const to = Math.min(piece.text.length, from + READ_CHARS);
2576
+ return json$1({
2577
+ piece: piece.id,
2578
+ label: piece.label,
2579
+ ...piece.keys.length === 0 ? {} : { keys: piece.keys },
2580
+ from,
2581
+ to,
2582
+ of: piece.text.length,
2583
+ ...to < piece.text.length ? { next: to } : {},
2584
+ text: piece.text.slice(from, to)
2585
+ });
2586
+ }
2587
+ }),
2588
+ defineTool({
2589
+ name: "card_origin",
2590
+ description: "Where a character, place, organisation or thing of this world came from: the pieces of the card the import read to make it (the pieces about it first, then those that only mention it), with what each was taken to be. For a name the import did not make anything of, the pieces of the card that mention it. Then read the pieces with card_read to answer from their text.",
2591
+ parameters: { name: {
2592
+ type: "string",
2593
+ required: true,
2594
+ description: "The name or an alias, as the world or the card has it."
2595
+ } },
2596
+ output: output$1,
2597
+ isConcurrencySafe: () => true,
2598
+ async execute(args, exec) {
2599
+ const { card, choices, pieces, records } = await source(exec);
2600
+ const entity = records.roster === void 0 ? void 0 : findEntity(records.roster, args.name);
2601
+ if (entity === void 0 || records.classified === void 0) return json$1({
2602
+ name: args.name,
2603
+ madeByTheImport: false,
2604
+ mentions: searchPieces(pieces, args.name, DEFAULT_HITS).map(({ piece, in: where, snippet: text }) => ({
2605
+ piece: piece.id,
2606
+ label: piece.label,
2607
+ foundIn: where,
2608
+ snippet: text
2609
+ }))
2610
+ });
2611
+ return json$1({
2612
+ name: entity.name,
2613
+ kind: entity.kind,
2614
+ aliases: entity.aliases,
2615
+ madeByTheImport: true,
2616
+ usedPieces: originOf(card, choices, pieces, records, entity.name).map((entry) => ({
2617
+ piece: entry.piece,
2618
+ label: entry.label,
2619
+ how: entry.how,
2620
+ takenToBe: rolesOf(records.classified, entry.piece)
2621
+ }))
2622
+ });
2623
+ }
2624
+ })
2625
+ ];
2601
2626
  }
2602
- // src/host/import/pictures.ts
2603
- /** The address is not one a picture may be fetched from: it names this machine or a private network. */
2604
- var BlockedAddressError = class extends Error {};
2605
- const MAX_BYTES = 20971520;
2606
- const MIN_BYTES = 1024;
2607
- const MAX_REDIRECTS = 4;
2608
- const TIMEOUT_MS = 3e4;
2609
- const CONCURRENCY = 8;
2610
- const NON_IMAGE_EXTENSIONS = /* @__PURE__ */ new Set([
2611
- "js",
2612
- "mjs",
2613
- "cjs",
2614
- "css",
2615
- "html",
2616
- "htm",
2617
- "json",
2618
- "xml",
2619
- "txt",
2620
- "md",
2621
- "map",
2622
- "wasm",
2623
- "php",
2624
- "py",
2625
- "woff",
2626
- "woff2",
2627
- "ttf",
2628
- "otf",
2629
- "eot",
2630
- "mp3",
2631
- "wav",
2632
- "ogg",
2633
- "flac",
2634
- "m4a",
2635
- "mp4",
2636
- "webm",
2637
- "mkv",
2638
- "mov",
2639
- "avi",
2640
- "zip",
2641
- "rar",
2642
- "gz",
2643
- "7z",
2644
- "pdf",
2645
- "doc",
2646
- "docx"
2647
- ]);
2648
- const blocked = new BlockList();
2649
- for (const [network, prefix] of [
2650
- ["0.0.0.0", 8],
2651
- ["10.0.0.0", 8],
2652
- ["100.64.0.0", 10],
2653
- ["127.0.0.0", 8],
2654
- ["169.254.0.0", 16],
2655
- ["172.16.0.0", 12],
2656
- ["192.0.0.0", 24],
2657
- ["192.0.2.0", 24],
2658
- ["192.88.99.0", 24],
2659
- ["192.168.0.0", 16],
2660
- ["198.18.0.0", 15],
2661
- ["198.51.100.0", 24],
2662
- ["203.0.113.0", 24],
2663
- ["224.0.0.0", 4],
2664
- ["240.0.0.0", 4]
2665
- ]) blocked.addSubnet(network, prefix, "ipv4");
2666
- for (const [network, prefix] of [
2667
- ["::", 128],
2668
- ["::1", 128],
2669
- ["64:ff9b::", 96],
2670
- ["100::", 64],
2671
- ["2001::", 32],
2672
- ["2001:db8::", 32],
2673
- ["2002::", 16],
2674
- ["fc00::", 7],
2675
- ["fe80::", 10],
2676
- ["ff00::", 8]
2677
- ]) blocked.addSubnet(network, prefix, "ipv6");
2678
- /** Whether an address may be connected to: only public unicast addresses may. */
2679
- function isPublicAddress(address) {
2680
- const family = isIP(address);
2681
- if (family === 0) return false;
2682
- if (family === 4) return !blocked.check(address, "ipv4");
2683
- const mapped = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i.exec(new URL(`http://[${address}]/`).hostname.replace(/^\[|\]$/g, ""));
2684
- if (mapped !== null) {
2685
- const high = Number.parseInt(mapped[1], 16);
2686
- const low = Number.parseInt(mapped[2], 16);
2687
- return isPublicAddress(`${high >> 8}.${high & 255}.${low >> 8}.${low & 255}`);
2688
- }
2689
- return !blocked.check(address, "ipv6");
2690
- }
2691
- /** The address a picture may be asked for, or why not: web only, standard ports, no credentials, no private literal address. */
2692
- function vetUrl(raw) {
2693
- let url;
2694
- try {
2695
- url = new URL(raw);
2696
- } catch {
2697
- return "not an address";
2698
- }
2699
- if (url.protocol !== "http:" && url.protocol !== "https:") return "not a web address";
2700
- if (url.username !== "" || url.password !== "") return "has credentials";
2701
- if (url.port !== "" && url.port !== "80" && url.port !== "443") return "uses an unusual port";
2702
- const host = url.hostname.replace(/^\[|\]$/g, "");
2703
- if (isIP(host) !== 0 && !isPublicAddress(host)) return "is a private address";
2704
- return url;
2627
+ /** For each piece of the card, the names of the characters, places and things the import made from it. */
2628
+ function entitiesByPiece(source) {
2629
+ const made = /* @__PURE__ */ new Map();
2630
+ for (const entity of source.records.roster ?? []) for (const entry of originOf(source.card, source.choices, source.pieces, source.records, entity.name)) made.set(entry.piece, [...made.get(entry.piece) ?? [], entity.name]);
2631
+ return made;
2705
2632
  }
2706
- /** The addresses worth asking about: a picture file name, or a name with no file type at all. */
2707
- function candidateUrls(urls) {
2708
- const candidates = [];
2709
- const skipped = [];
2633
+ /** The pieces an entity was made from, as the build chose them: the ones the survey said are about it, then those that name it. */
2634
+ function originOf(card, choices, pieces, records, name, items = itemise(card, choices)) {
2635
+ if (records.roster === void 0 || records.classified === void 0) return [];
2636
+ const entity = findEntity(records.roster, name);
2637
+ if (entity === void 0) return [];
2638
+ const names = [entity.name, ...entity.aliases].map(norm);
2639
+ const chosen = sourcesFor(entity, items, records.classified);
2640
+ const about = (id) => (records.classified?.[id]?.targets ?? []).some((target) => target.kind === entity.kind && names.includes(norm(target.name)));
2710
2641
  const seen = /* @__PURE__ */ new Set();
2711
- for (const raw of urls) {
2712
- const url = vetUrl(raw);
2713
- if (typeof url === "string") {
2714
- skipped.push({
2715
- url: raw,
2716
- reason: url
2717
- });
2718
- continue;
2719
- }
2720
- url.hash = "";
2721
- const key = url.href;
2722
- if (seen.has(key)) continue;
2723
- seen.add(key);
2724
- const extension = /\.([a-z0-9]{1,5})$/i.exec(url.pathname)?.[1]?.toLowerCase();
2725
- if (extension !== void 0 && MEDIA_TYPES[`.${extension}`] === void 0) skipped.push({
2726
- url: raw,
2727
- reason: NON_IMAGE_EXTENSIONS.has(extension) ? "not an image" : "format not supported"
2728
- });
2729
- else candidates.push(key);
2730
- }
2731
- return {
2732
- candidates,
2733
- skipped
2734
- };
2642
+ return chosen.flatMap((item) => {
2643
+ const id = baseId(item.id);
2644
+ const piece = pieces.find((entry) => entry.id === id);
2645
+ if (piece === void 0 || seen.has(id)) return [];
2646
+ seen.add(id);
2647
+ return [{
2648
+ piece: id,
2649
+ label: piece.label,
2650
+ how: about(item.id) ? "about it" : "mentions it"
2651
+ }];
2652
+ });
2735
2653
  }
2736
- /** Looks the name up and refuses it when any answer is not public, at the moment of connecting, so a name cannot change between the check and the use. */
2737
- const guardedLookup = (hostname, options, callback) => {
2738
- lookup(hostname, {
2739
- ...options,
2740
- all: true
2741
- }, (error, addresses) => {
2742
- if (error !== null) {
2743
- callback(error, "", 0);
2744
- return;
2745
- }
2746
- if (addresses.find((entry) => !isPublicAddress(entry.address)) !== void 0 || addresses.length === 0) {
2747
- callback(new BlockedAddressError(`${hostname} is not a public address`), "", 0);
2748
- return;
2749
- }
2750
- if (options.all === true) callback(null, addresses);
2751
- else callback(null, addresses[0].address, addresses[0].family);
2654
+ // src/host/worlds/world-file-io.ts
2655
+ const queues = /* @__PURE__ */ new Map();
2656
+ /**
2657
+ * Runs `work` after everything asked for the same folder before it has finished. The import, the editor and the assistant each read `world.json`, change a field and write it
2658
+ * back; run side by side, the last one to write would take back what the others changed.
2659
+ */
2660
+ function serialized(directory, work) {
2661
+ const run = (queues.get(directory) ?? Promise.resolve()).then(work, work);
2662
+ const tail = run.then(() => void 0, () => void 0);
2663
+ queues.set(directory, tail);
2664
+ tail.then(() => {
2665
+ if (queues.get(directory) === tail) queues.delete(directory);
2752
2666
  });
2753
- };
2754
- async function readBody(response) {
2755
- const chunks = [];
2756
- let size = 0;
2757
- for await (const chunk of response) {
2758
- size += chunk.length;
2759
- if (size > MAX_BYTES) {
2760
- response.destroy();
2761
- throw new Error("too large");
2762
- }
2763
- chunks.push(chunk);
2667
+ return run;
2668
+ }
2669
+ /** Changes `world.json` in place: read, `update`, written back in one step with the indentation the file had, nothing else moving in between. @returns the document as written. */
2670
+ function updateWorldFile(directory, update) {
2671
+ return serialized(directory, async () => {
2672
+ const file = join(directory, WORLD_FILE_NAME);
2673
+ const text = await readFile(file, "utf8");
2674
+ const document = JSON.parse(text.replace(/^/, ""));
2675
+ await update(document);
2676
+ await atomicWrite(file, JSON.stringify(document, null, /^([ \t]+)"/m.exec(text)?.[1] ?? " ") + (text.endsWith("\n") || text.trim() === "" ? "\n" : ""));
2677
+ return document;
2678
+ });
2679
+ }
2680
+ // src/host/worlds/entity-folders.ts
2681
+ /** The five kinds of entity a world is made of: where each lives (`<directory>/<id>/<file>`) and the key of its group in the importer's output. */
2682
+ const ENTITY_FOLDERS = [
2683
+ {
2684
+ key: "characters",
2685
+ directory: CHARACTERS_DIR,
2686
+ file: CHARACTER_FILE_NAME
2687
+ },
2688
+ {
2689
+ key: "locations",
2690
+ directory: LOCATIONS_DIR,
2691
+ file: LOCATION_FILE_NAME
2692
+ },
2693
+ {
2694
+ key: "factions",
2695
+ directory: FACTIONS_DIR,
2696
+ file: FACTION_FILE_NAME
2697
+ },
2698
+ {
2699
+ key: "items",
2700
+ directory: ITEMS_DIR,
2701
+ file: ITEM_FILE_NAME
2702
+ },
2703
+ {
2704
+ key: "lore",
2705
+ directory: LORE_DIR,
2706
+ file: LORE_FILE_NAME
2764
2707
  }
2765
- return Buffer.concat(chunks);
2708
+ ];
2709
+ // src/host/import/finish.ts
2710
+ const STAGE = "stage";
2711
+ const MAX_DESCRIPTION = 500;
2712
+ const MAX_TAGS = 8;
2713
+ const MAX_TAG = 30;
2714
+ /** The card's other greetings, as the other openings a player may start with: the first message and the alternates that were not chosen. */
2715
+ function alternateOpenings(card, chosen, player) {
2716
+ if (chosen.kind === "none") return [];
2717
+ return [{
2718
+ title: "First message",
2719
+ text: card.firstMessage,
2720
+ taken: chosen.kind === "first"
2721
+ }, ...card.alternateGreetings.map((text, index) => ({
2722
+ title: `Alternate ${index + 1}`,
2723
+ text,
2724
+ taken: chosen.kind === "alternate" && chosen.index === index
2725
+ }))].flatMap(({ title, text, taken }) => {
2726
+ const cleaned = taken || text.trim() === "" ? "" : openingMessage(text, card.name, player);
2727
+ return cleaned === "" || cleaned.length > 2e4 ? [] : [{
2728
+ title,
2729
+ text: cleaned
2730
+ }];
2731
+ }).slice(0, 30);
2766
2732
  }
2767
- /** One request, never following a redirect, with the connection itself checked against private networks. */
2768
- const requestOnce = (url, method, signal) => new Promise((resolveReply, reject) => {
2769
- const request$2 = (url.protocol === "https:" ? request$1 : request)(url, {
2770
- method,
2771
- lookup: guardedLookup,
2772
- signal,
2773
- timeout: TIMEOUT_MS,
2774
- agent: false,
2775
- headers: {
2776
- "user-agent": "Mozilla/5.0 (compatible; dsh-roleplay-import)",
2777
- accept: "image/*,*/*;q=0.5",
2778
- "accept-encoding": "identity"
2779
- }
2780
- }, (response) => {
2781
- const status = response.statusCode ?? 0;
2782
- const type = String(response.headers["content-type"] ?? "").split(";")[0].trim().toLowerCase();
2783
- const location = typeof response.headers.location === "string" ? response.headers.location : void 0;
2784
- const length = Number(response.headers["content-length"] ?? 0);
2785
- if (method === "HEAD" || status < 200 || status >= 300 || location !== void 0 && status >= 300) {
2786
- response.destroy();
2787
- resolveReply({
2788
- status,
2789
- type,
2790
- location,
2791
- body: Buffer.alloc(0)
2792
- });
2793
- return;
2794
- }
2795
- if (length > MAX_BYTES) {
2796
- response.destroy();
2797
- reject(/* @__PURE__ */ new Error("too large"));
2798
- return;
2799
- }
2800
- readBody(response).then((body) => {
2801
- resolveReply({
2802
- status,
2803
- type,
2804
- location,
2805
- body
2806
- });
2807
- }, reject);
2733
+ /** The character the card's picture shows: the one that carries the card's name, or the only one there is. */
2734
+ function cardCharacter(characters, cardName) {
2735
+ return characters.find((document) => norm(String(document["name"])) === norm(cardName)) ?? (characters.length === 1 ? characters[0] : void 0);
2736
+ }
2737
+ /** Makes the card's picture the canonical reference portrait of its character, as an image the person brought. */
2738
+ async function writePortrait(directory, picture, characters, cardName) {
2739
+ const character = cardCharacter(characters, cardName);
2740
+ if (character === void 0) return false;
2741
+ const id = String(character["id"]);
2742
+ const file = referenceImagePath(directory, {
2743
+ kind: "character",
2744
+ localId: id
2808
2745
  });
2809
- request$2.on("timeout", () => {
2810
- request$2.destroy(/* @__PURE__ */ new Error("timed out"));
2746
+ await writeAtomically(file, picture);
2747
+ await writeImageFileMetadata(file, {
2748
+ schema: "dsh-roleplay-image/v3",
2749
+ purpose: "reference",
2750
+ origin: "uploaded",
2751
+ reusable: false,
2752
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
2753
+ originalName: PICTURE_FILE,
2754
+ description: `${String(character["name"])}: the portrait that came with the character card.`,
2755
+ tags: [String(character["name"])],
2756
+ subjects: [{
2757
+ kind: "character",
2758
+ localId: id,
2759
+ name: String(character["name"])
2760
+ }]
2811
2761
  });
2812
- request$2.on("error", reject);
2813
- request$2.end();
2814
- });
2815
- /** Asks for the address, following up to a few redirects, each vetted like the first address. */
2816
- async function follow(fetcher, start, method, signal) {
2817
- let url = start;
2818
- for (let hop = 0;; hop += 1) {
2819
- const reply = await fetcher(url, method, signal);
2820
- if (reply.status < 300 || reply.status >= 400 || reply.location === void 0) return reply;
2821
- if (hop >= MAX_REDIRECTS) throw new Error("too many redirects");
2822
- const next = vetUrl(new URL(reply.location, url).href);
2823
- if (typeof next === "string") throw new BlockedAddressError(`a redirect ${next}`);
2824
- url = next;
2825
- }
2762
+ return true;
2826
2763
  }
2827
- const GENERIC_TYPES = /* @__PURE__ */ new Set([
2828
- "",
2829
- "application/octet-stream",
2830
- "binary/octet-stream",
2831
- "application/binary",
2832
- "application/x-binary"
2833
- ]);
2834
- const SUPPORTED_TYPES = new Set(Object.values(MEDIA_TYPES));
2835
- /** What a HEAD request says about an address: not a picture, or worth fetching (a picture, or an answer that settles nothing because the service does not do HEAD). */
2836
- async function worthFetching(fetcher, url, signal) {
2837
- let reply;
2838
- try {
2839
- reply = await follow(fetcher, url, "HEAD", signal);
2840
- } catch (error) {
2841
- return error instanceof BlockedAddressError ? error.message : void 0;
2842
- }
2843
- if (reply.status === 404 || reply.status === 410) return "gone";
2844
- if (reply.status < 200 || reply.status >= 300) return void 0;
2845
- if (reply.type.startsWith("image/")) return SUPPORTED_TYPES.has(reply.type) ? void 0 : "format not supported";
2846
- return GENERIC_TYPES.has(reply.type) ? void 0 : `not an image (${reply.type})`;
2764
+ /** The card's opening as the world's opening message: plain paragraphs, without the markup the card's tool read as emphasis. */
2765
+ function openingMessage(raw, cardName, player) {
2766
+ const text = cleanText(raw, cardName).replace(/<\/?[a-z][^>\n]*>/gi, "").replace(/```[a-z]*\n?|```/g, "").replace(/\*\*([^*\n]+)\*\*/g, "$1").replace(/(^|[^*])\*(?!\s)([^*\n]+?)(?<!\s)\*(?!\*)/g, "$1$2").replace(/\n{3,}/g, "\n\n").trim();
2767
+ return withPlayer(text, player);
2847
2768
  }
2848
- function fileNameOf(url) {
2849
- const last = url.pathname.split("/").pop() ?? "";
2850
- try {
2851
- return decodeURIComponent(last) || "image";
2852
- } catch {
2853
- return last || "image";
2769
+ /** Every player marker in the strings of a value, replaced by the player's character or a plain word for the player; the value is changed in place. */
2770
+ function replacePlayer(value, player) {
2771
+ if (Array.isArray(value)) value.forEach((entry, index) => {
2772
+ if (typeof entry === "string") value[index] = withPlayer(entry, player);
2773
+ else replacePlayer(entry, player);
2774
+ });
2775
+ else if (typeof value === "object" && value !== null) {
2776
+ const record = value;
2777
+ for (const key of Object.keys(record)) {
2778
+ const entry = record[key];
2779
+ if (typeof entry === "string") record[key] = withPlayer(entry, player);
2780
+ else replacePlayer(entry, player);
2781
+ }
2854
2782
  }
2855
2783
  }
2856
2784
  /**
2857
- * Fetches the pictures a card points to into the world's image library, as if the person had added them. Each address is vetted, asked about
2858
- * with HEAD, and only then downloaded; what is kept is decided by the first bytes of the file, never by its name or declared type.
2785
+ * Every character knows every place of the imported world and the ways between them, unless its own file already says what it knows: a card describes a
2786
+ * world its people live in, and without this the closed-world rules would forbid a walk to a famous street. Changes the documents in place.
2859
2787
  */
2860
- async function fetchPictures(world, urls, deps = {}) {
2861
- const fetcher = deps.fetcher ?? requestOnce;
2862
- const { candidates, skipped: refused } = candidateUrls(urls);
2863
- const skipped = [...refused];
2864
- const added = [];
2865
- const hashes = await libraryHashes(world);
2866
- await mapLimit(candidates, deps.concurrency ?? CONCURRENCY, async (address) => {
2867
- const signal = AbortSignal.any([AbortSignal.timeout(TIMEOUT_MS * 2), ...deps.signal === void 0 ? [] : [deps.signal]]);
2868
- const url = new URL(address);
2869
- try {
2870
- const refusal = await worthFetching(fetcher, url, signal);
2871
- if (refusal !== void 0) {
2872
- skipped.push({
2873
- url: address,
2874
- reason: refusal
2875
- });
2876
- return;
2877
- }
2878
- const reply = await follow(fetcher, url, "GET", signal);
2879
- if (reply.status < 200 || reply.status >= 300) {
2880
- skipped.push({
2881
- url: address,
2882
- reason: `answered ${reply.status}`
2883
- });
2884
- return;
2885
- }
2886
- if (imageExtensionOf(reply.body) === void 0) {
2887
- skipped.push({
2888
- url: address,
2889
- reason: "not an image"
2890
- });
2891
- return;
2892
- }
2893
- if (reply.body.length < MIN_BYTES) {
2894
- skipped.push({
2895
- url: address,
2896
- reason: "too small"
2897
- });
2898
- return;
2899
- }
2900
- const { path, added: isNew } = await addToLibraryOnce(world, reply.body, fileNameOf(url), hashes);
2901
- if (!isNew) {
2902
- skipped.push({
2903
- url: address,
2904
- reason: "a copy of another picture",
2905
- of: path
2906
- });
2907
- return;
2908
- }
2909
- added.push({
2910
- url: address,
2911
- path
2788
+ function shareThePlaces(characters, locations) {
2789
+ const places = locations.map((location) => String(location["id"]));
2790
+ const ways = [];
2791
+ for (const location of locations) {
2792
+ const from = String(location["id"]);
2793
+ for (const exit of Array.isArray(location["exits"]) ? location["exits"] : []) {
2794
+ const to = String(exit["to"]);
2795
+ ways.push({
2796
+ from,
2797
+ to
2912
2798
  });
2913
- } catch (error) {
2914
- skipped.push({
2915
- url: address,
2916
- reason: error instanceof Error ? error.message : String(error)
2799
+ if (exit["one_way"] !== true) ways.push({
2800
+ from: to,
2801
+ to: from
2917
2802
  });
2918
2803
  }
2919
- });
2920
- return {
2921
- found: candidates.length,
2922
- added,
2923
- skipped
2924
- };
2925
- }
2926
- /** The key an address is known by: the way `candidateUrls` reads it, without a fragment. */
2927
- function addressKey(raw) {
2928
- try {
2929
- const url = new URL(raw);
2930
- url.hash = "";
2931
- return url.href;
2932
- } catch {
2933
- return raw;
2934
2804
  }
2935
- }
2936
- const FINAL = [
2937
- /^not an? /,
2938
- /^format not supported/,
2939
- /^gone$/,
2940
- /^too small$/,
2941
- /^too large$/,
2942
- /^a copy of /,
2943
- /^has credentials/,
2944
- /^uses an unusual port/,
2945
- /^is a private address/,
2946
- /^a redirect /,
2947
- /is not a public address$/,
2948
- /^answered (4(?!08|29)\d\d)$/
2949
- ];
2950
- /** Whether asking again could give another answer: a picture that is not one, is gone or may not be fetched stays settled; a timeout, a server error or a refusal to wait does not. */
2951
- const worthAskingAgain = (reason) => !FINAL.some((pattern) => pattern.test(reason));
2952
- /**
2953
- * The addresses of a newer card to ask for: those the earlier card did not have, and those it had that were not settled (the record says a request failed). With no record
2954
- * of that fetch every address of the earlier card counts as settled, so what the person removed from the library is not brought back.
2955
- */
2956
- function addressesToFetch(earlier, later, record) {
2957
- const settled = new Set(earlier.map(addressKey));
2958
- if (record !== void 0) {
2959
- const unsettled = new Set(record.skipped.filter((entry) => worthAskingAgain(entry.reason)).map((entry) => addressKey(entry.url)));
2960
- const fetched = new Set(record.added.map((entry) => addressKey(entry.url)));
2961
- for (const key of [...settled]) if (unsettled.has(key) && !fetched.has(key)) settled.delete(key);
2805
+ for (const character of characters) {
2806
+ const knowledge = typeof character["knowledge"] === "object" && character["knowledge"] !== null ? character["knowledge"] : {};
2807
+ if (knowledge["locations"] !== void 0 || knowledge["exits"] !== void 0) continue;
2808
+ knowledge["locations"] = places;
2809
+ knowledge["exits"] = ways.filter((way, index) => ways.findIndex((other) => other.from === way.from && other.to === way.to) === index);
2810
+ character["knowledge"] = knowledge;
2962
2811
  }
2963
- return later.filter((url) => !settled.has(addressKey(url)));
2964
- }
2965
- /** The record after a second fetch: what was added stays, an address asked again takes its new answer. */
2966
- function mergeReports(earlier, later, found) {
2967
- const asked = new Set([...later.added.map((entry) => entry.url), ...later.skipped.map((entry) => entry.url)].map(addressKey));
2968
- return {
2969
- found,
2970
- added: [...earlier?.added ?? [], ...later.added],
2971
- skipped: [...(earlier?.skipped ?? []).filter((entry) => !asked.has(addressKey(entry.url))), ...later.skipped]
2972
- };
2973
2812
  }
2974
- // src/host/catalogue/errors.ts
2975
- var CatalogueError = class extends Error {
2976
- failure;
2977
- detail;
2978
- constructor(message, failure, detail) {
2979
- super(message);
2980
- this.failure = failure;
2981
- this.detail = detail;
2813
+ /** Places that contain each other in a circle are untangled by taking the parent off the one that closes it. */
2814
+ function breakParentCircles(locations) {
2815
+ const notes = [];
2816
+ const byId = new Map(locations.map((location) => [String(location["id"]), location]));
2817
+ for (const location of locations) {
2818
+ const seen = /* @__PURE__ */ new Set([String(location["id"])]);
2819
+ for (let up = location["parent"]; up !== void 0; up = byId.get(up)?.["parent"]) {
2820
+ if (seen.has(up)) {
2821
+ notes.push(`${String(location["name"])}: the places contain each other in a circle, so it is not put inside another place`);
2822
+ delete location["parent"];
2823
+ break;
2824
+ }
2825
+ seen.add(up);
2826
+ }
2982
2827
  }
2828
+ return notes;
2829
+ }
2830
+ const WORLD_SCHEMA = {
2831
+ type: "object",
2832
+ properties: {
2833
+ description: {
2834
+ type: "string",
2835
+ description: `Two or three sentences, at most ${MAX_DESCRIPTION} characters.`
2836
+ },
2837
+ tags: {
2838
+ type: "array",
2839
+ items: { type: "string" },
2840
+ description: `At most ${MAX_TAGS} short tags.`
2841
+ },
2842
+ starting_time: {
2843
+ type: "string",
2844
+ description: "When the story begins, as an ISO 8601 date and time such as 1921-10-23T08:00:00, only when the text states the date or year; otherwise an empty string."
2845
+ }
2846
+ },
2847
+ required: [
2848
+ "description",
2849
+ "tags",
2850
+ "starting_time"
2851
+ ],
2852
+ additionalProperties: false
2983
2853
  };
2984
- // src/host/catalogue/origin.ts
2985
- const ORIGIN_FILE = ".catalogue.json";
2986
- const MAX_LISTED_CHANGES = 12;
2987
- const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
2988
- /** The origin recorded in a world's folder, or nothing when it has none. @throws {CatalogueError} when the record is damaged. */
2989
- async function readOrigin(directory) {
2990
- let text;
2991
- try {
2992
- text = await readFile(join(directory, ORIGIN_FILE), "utf8");
2993
- } catch (error) {
2994
- if (error.code === "ENOENT") return void 0;
2995
- throw error;
2996
- }
2997
- const damaged = () => new CatalogueError(`${ORIGIN_FILE} in ${directory} is damaged`, "invalid");
2998
- let value;
2999
- try {
3000
- value = JSON.parse(text);
3001
- } catch {
3002
- throw damaged();
3003
- }
3004
- if (!isRecord(value)) throw damaged();
3005
- const { kind, root, resourceId, versionId, version, files } = value;
3006
- if (kind !== "release" && kind !== "card" || typeof root !== "string" || typeof resourceId !== "string" || typeof versionId !== "string" || typeof version !== "string") throw damaged();
3007
- if (kind === "card") return {
3008
- kind,
3009
- root,
3010
- resourceId,
3011
- versionId,
3012
- version
3013
- };
3014
- if (!isRecord(files) || !Object.values(files).every((hash) => typeof hash === "string")) throw damaged();
2854
+ const STARTING_TIME = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2})?)?$/;
2855
+ function worldJob(text, tr, card, roster, scenario, opening) {
2856
+ const names = (kind) => roster.filter((entity) => entity.kind === kind).slice(0, 15).map((entity) => entity.name).join("; ");
3015
2857
  return {
3016
- kind,
3017
- root,
3018
- resourceId,
3019
- versionId,
3020
- version,
3021
- files
3022
- };
3023
- }
3024
- async function writeOrigin(directory, origin) {
3025
- const file = join(directory, ORIGIN_FILE);
3026
- await atomicWriteJson(file, origin, { indent: 2 });
3027
- }
3028
- /** SHA-256 of every file in a world folder; entries whose names start with a dot (this record, the trash, import state) are not the world's own. */
3029
- async function hashWorld(directory, skip = () => false) {
3030
- const hashes = {};
3031
- const walk = async (relative) => {
3032
- for (const entry of await readdir(join(directory, relative), { withFileTypes: true })) {
3033
- if (relative === "" && entry.name.startsWith(".")) continue;
3034
- const path = relative === "" ? entry.name : `${relative}/${entry.name}`;
3035
- if (entry.isDirectory()) await walk(path);
3036
- else if (entry.isFile() && !skip(path)) hashes[path] = createHash("sha256").update(await readFile(join(directory, path))).digest("hex");
2858
+ persona: text("import/world-summary"),
2859
+ maxTokens: 1500,
2860
+ prompt: [
2861
+ tr("Card: {name}", { name: card.name }),
2862
+ ...card.tags.length === 0 ? [] : [tr("Card tags: {tags}", { tags: card.tags.join(", ") })],
2863
+ `${tr("Characters:")} ${names("character") || tr("(none)")}`,
2864
+ `${tr("Places:")} ${names("place") || tr("(none)")}`,
2865
+ `${tr("Scenario:")}\n${scenario.slice(0, 1200) || tr("(none)")}`,
2866
+ `${tr("Opening:")}\n${opening.slice(0, 1200) || tr("(none)")}`,
2867
+ `${tr("Description:")}\n${cleanText(card.description, card.name).slice(0, 1200) || tr("(none)")}`,
2868
+ `${tr("Creator's notes:")}\n${cleanText(card.creatorNotes, card.name).slice(0, 600) || tr("(none)")}`
2869
+ ].join("\n\n"),
2870
+ result: WORLD_SCHEMA,
2871
+ check: (value) => {
2872
+ if (value.description.trim() === "" || value.description.length > MAX_DESCRIPTION) return `the description must be written and at most ${MAX_DESCRIPTION} characters`;
2873
+ if (value.tags.length > MAX_TAGS || value.tags.some((tag) => tag.trim() === "" || tag.length > MAX_TAG)) return `give at most ${MAX_TAGS} tags of at most ${MAX_TAG} characters`;
2874
+ if ((value.starting_time ?? "").trim() !== "" && (!STARTING_TIME.test(value.starting_time.trim()) || Number.isNaN(Date.parse(value.starting_time.trim())))) return "starting_time must be an ISO 8601 date and time such as 1921-10-23T08:00:00, or empty";
3037
2875
  }
3038
2876
  };
3039
- await walk("");
3040
- return hashes;
3041
2877
  }
3042
- function compare(recorded, current) {
3043
- return {
3044
- changed: Object.keys(recorded).filter((path) => current[path] !== void 0 && current[path] !== recorded[path]),
3045
- added: Object.keys(current).filter((path) => recorded[path] === void 0),
3046
- removed: Object.keys(recorded).filter((path) => current[path] === void 0)
2878
+ /** The description and tags of the world: asked of one call, and from the card's own fields when that fails. */
2879
+ async function describeWorld(ctx, card, roster, items, opening) {
2880
+ const scenario = items.filter((item) => item.id.startsWith("field:scenario")).map((item) => item.text).join("\n\n");
2881
+ if (ctx.noModel === true) return {
2882
+ description: (scenario || cleanText(card.description, card.name)).slice(0, MAX_DESCRIPTION).trim(),
2883
+ tags: card.tags.filter((tag) => tag.trim() !== "" && tag.length <= MAX_TAG).slice(0, MAX_TAGS)
3047
2884
  };
3048
- }
3049
- const isUnchanged = (changes) => changes.changed.length + changes.added.length + changes.removed.length === 0;
3050
- /** The differences as short lines for a person to read, capped. */
3051
- function describeChanges(changes) {
3052
- const lines = [
3053
- ...changes.changed.map((path) => `changed: ${path}`),
3054
- ...changes.added.map((path) => `added: ${path}`),
3055
- ...changes.removed.map((path) => `removed: ${path}`)
3056
- ];
3057
- return lines.length <= MAX_LISTED_CHANGES ? lines : [...lines.slice(0, MAX_LISTED_CHANGES), `and ${lines.length - MAX_LISTED_CHANGES} more`];
3058
- }
3059
- /** How a world's files compare with the release it came from: the differences, or `unverified` when no record of the release exists. */
3060
- async function localChanges(directory) {
3061
- const origin = await readOrigin(directory);
3062
- if (origin?.kind !== "release") return "unverified";
3063
- return compare(origin.files, await hashWorld(directory));
3064
- }
3065
- // src/host/import/snapshot.ts
3066
- const SNAPSHOT = "files.json";
3067
- /** The pictures of a world are fetched and generated in the background and are not what a card says. */
3068
- const notTheCards = (path) => path.startsWith("images/");
3069
- /** What a world's files were when its import was over; "changed since" is measured against it. */
3070
- async function writeSnapshot(directory) {
3071
- const file = join(directory, IMPORT_FOLDER, SNAPSHOT);
3072
- await atomicWriteJson(file, await hashWorld(directory, notTheCards));
3073
- }
3074
- /** How a world compares with its snapshot: `no` for no change, `yes` with the files, and `unknown` when the import left no snapshot (an older import). */
3075
- async function editsSince(directory) {
3076
- let recorded;
3077
2885
  try {
3078
- recorded = JSON.parse(await readFile(join(directory, IMPORT_FOLDER, SNAPSHOT), "utf8"));
2886
+ const answer = await ctx.run(worldJob(ctx.text, trOf(ctx), card, roster, scenario, opening));
2887
+ return {
2888
+ description: answer.description.trim(),
2889
+ tags: [...new Set(answer.tags.map((tag) => tag.trim()))],
2890
+ ...(answer.starting_time ?? "").trim() === "" ? {} : { startingTime: answer.starting_time.trim() }
2891
+ };
3079
2892
  } catch (error) {
3080
- if (error.code === "ENOENT") return { edited: "unknown" };
3081
- throw error;
2893
+ return {
2894
+ description: (scenario || cleanText(card.description, card.name)).slice(0, MAX_DESCRIPTION).trim(),
2895
+ tags: card.tags.filter((tag) => tag.trim() !== "" && tag.length <= MAX_TAG).slice(0, MAX_TAGS),
2896
+ note: `the description of the world was taken from the card as it is: ${error instanceof Error ? error.message : String(error)}`
2897
+ };
3082
2898
  }
3083
- const changes = compare(recorded, await hashWorld(directory, notTheCards));
3084
- return isUnchanged(changes) ? { edited: "no" } : {
3085
- edited: "yes",
3086
- changes: describeChanges(changes)
3087
- };
3088
2899
  }
3089
- // src/host/import/provenance.ts
3090
- const FILE$2 = "provenance.json";
3091
- const PROVENANCE_SCHEMA = "dsh-roleplay-import/v1";
3092
- /** The import record of a world, or nothing for a world imported before records were kept. */
3093
- async function readProvenance(directory) {
3094
- let text;
2900
+ async function hasEntities(world) {
2901
+ for (const { directory } of ENTITY_FOLDERS) try {
2902
+ if ((await readdir(join(world, directory))).some((name) => !name.startsWith("."))) return true;
2903
+ } catch (error) {
2904
+ if (error.code !== "ENOENT") throw error;
2905
+ }
2906
+ return false;
2907
+ }
2908
+ const writeAtomically = (file, text) => atomicWrite(file, text, { createDirectory: true });
2909
+ /**
2910
+ * Writes what was extracted into the world, all at once. The documents are first written to a staging folder and read back as the world will
2911
+ * read them when a session starts; only when every part of the world is valid are they moved in. The world's own file gets its description, tags, opening and cover.
2912
+ */
2913
+ async function finishImport(ctx, input) {
2914
+ const { directory, card, choices, roster, items, classified, extracted } = input;
2915
+ if (await hasEntities(directory)) return {
2916
+ ok: false,
2917
+ problems: ["the world already has characters, places, factions, things or lore: an import only fills an empty world"]
2918
+ };
2919
+ const playing = extracted.characters.find((document) => document["role"] === "playable");
2920
+ const playerName = playing === void 0 ? void 0 : String(playing["name"]);
2921
+ for (const key of [
2922
+ "characters",
2923
+ "locations",
2924
+ "factions",
2925
+ "items",
2926
+ "lore",
2927
+ "commands",
2928
+ "triggers",
2929
+ "variables"
2930
+ ]) replacePlayer(extracted[key], playerName);
2931
+ const notes = [
2932
+ ...input.notes,
2933
+ ...extracted.notes,
2934
+ ...breakParentCircles(extracted.locations)
2935
+ ];
2936
+ shareThePlaces(extracted.characters, extracted.locations);
2937
+ const stage = join(directory, IMPORT_FOLDER, STAGE);
2938
+ await rm(stage, {
2939
+ recursive: true,
2940
+ force: true
2941
+ });
2942
+ const mend = () => {
2943
+ const view = {
2944
+ ...extracted,
2945
+ notes
2946
+ };
2947
+ pruneLoreSubjects(view);
2948
+ pruneMembers(view);
2949
+ pruneLinks(view);
2950
+ pruneKnowledge(view);
2951
+ pruneRelationships(view);
2952
+ };
2953
+ mend();
3095
2954
  try {
3096
- text = await readFile(join(directory, IMPORT_FOLDER, FILE$2), "utf8");
2955
+ if (extracted.variables.length > 0) await writeAtomically(join(stage, VARIABLES_FILE_NAME), `${JSON.stringify(extracted.variables, null, 2)}\n`);
2956
+ if (extracted.commands.length > 0) await writeAtomically(join(stage, COMMANDS_FILE_NAME), `${JSON.stringify({ commands: extracted.commands }, null, 2)}\n`);
2957
+ if (extracted.triggers.length > 0) await writeAtomically(join(stage, TRIGGERS_FILE_NAME), `${JSON.stringify(extracted.triggers, null, 2)}\n`);
2958
+ for (const { key, directory: folder, file } of ENTITY_FOLDERS) for (const document of extracted[key]) await writeAtomically(join(stage, folder, String(document["id"]), file), `${JSON.stringify(document, null, 2)}\n`);
2959
+ for (const { id, text } of extracted.prompts) {
2960
+ const problem = promptProblem("en", id, text);
2961
+ if (problem !== void 0) return {
2962
+ ok: false,
2963
+ problems: [`prompt ${id}: ${problem}`]
2964
+ };
2965
+ }
2966
+ if (extracted.variables.length > 0) await rename(join(stage, VARIABLES_FILE_NAME), join(directory, VARIABLES_FILE_NAME));
2967
+ if (extracted.commands.length > 0) await rename(join(stage, COMMANDS_FILE_NAME), join(directory, COMMANDS_FILE_NAME));
2968
+ if (extracted.triggers.length > 0) await rename(join(stage, TRIGGERS_FILE_NAME), join(directory, TRIGGERS_FILE_NAME));
2969
+ for (const { directory: folder } of ENTITY_FOLDERS) {
2970
+ const from = join(stage, folder);
2971
+ if (await stat(from).then(() => true, () => false)) await rename(from, join(directory, folder));
2972
+ }
2973
+ } finally {
2974
+ await rm(stage, {
2975
+ recursive: true,
2976
+ force: true
2977
+ });
2978
+ }
2979
+ const check = await checkWorld({
2980
+ directory,
2981
+ summary: { name: choices.name }
2982
+ });
2983
+ if (!check.ok) notes.push(`the world was written as a draft: it would not start yet (${check.problems.length + check.more} problem(s) in ${check.summary ?? "the world"})`);
2984
+ const player = extracted.characters.find((document) => document["role"] === "playable");
2985
+ for (const { id, text } of extracted.prompts) for (const language of shippedLanguages()) await writeAtomically(join(directory, PROMPTS_FOLDER, language, `${id}.md`), `${withPlayer(text, player === void 0 ? void 0 : String(player["name"]))}\n`);
2986
+ const opening = openingText(card, choices.opening);
2987
+ const described = await describeWorld(ctx, card, roster, items, opening ?? "");
2988
+ if (described.note !== void 0) notes.push(described.note);
2989
+ described.description = withPlayer(described.description, player === void 0 ? void 0 : String(player["name"]));
2990
+ let cover = false;
2991
+ if (input.picture !== void 0) {
2992
+ await writeAtomically(join(directory, "media", "files", "cover.png"), input.picture);
2993
+ await writeAtomically(join(directory, "media", "manifest.jsonl"), `${JSON.stringify({
2994
+ id: "cover",
2995
+ file: "media/files/cover.png"
2996
+ })}\n`);
2997
+ cover = true;
2998
+ }
2999
+ const alternates = alternateOpenings(card, choices.opening, player === void 0 ? void 0 : String(player["name"]));
3000
+ await updateWorldFile(directory, (world) => {
3001
+ world["description"] = described.description;
3002
+ if (described.startingTime !== void 0 && world["starting_time"] === void 0) world["starting_time"] = described.startingTime;
3003
+ world["metadata"] = {
3004
+ ...typeof world["metadata"] === "object" && world["metadata"] !== null ? world["metadata"] : {},
3005
+ tags: described.tags
3006
+ };
3007
+ if (opening !== void 0) world["opening_message"] = openingMessage(opening, card.name, player === void 0 ? void 0 : String(player["name"]));
3008
+ if (alternates.length > 0) world["alternate_openings"] = alternates;
3009
+ if (card.creator.trim() !== "" && world["author"] === void 0) world["author"] = { name: card.creator.trim() };
3010
+ if (cover) world["cover_media_id"] = "cover";
3011
+ });
3012
+ let portrait = false;
3013
+ if (input.picture !== void 0) try {
3014
+ portrait = await writePortrait(directory, input.picture, extracted.characters, card.name);
3097
3015
  } catch (error) {
3098
- if (error.code === "ENOENT") return void 0;
3099
- throw error;
3016
+ notes.push(`the card's picture could not be used as a portrait: ${error instanceof Error ? error.message : String(error)}`);
3100
3017
  }
3101
- const value = JSON.parse(text);
3102
- if (value.schema !== "dsh-roleplay-import/v1" || !Array.isArray(value.history)) throw new Error(`${FILE$2} is not an import record this version reads`);
3103
- return value;
3104
- }
3105
- /** Adds one line to the world's import record: what card it came from and, for an import, what was written. */
3106
- async function recordProvenance(directory, entry, cardJson) {
3107
- const earlier = await readProvenance(directory);
3108
- const line = {
3109
- ...entry,
3110
- at: (/* @__PURE__ */ new Date()).toISOString(),
3111
- card: {
3112
- name: entry.card.name,
3113
- sha256: createHash("sha256").update(cardJson).digest("hex")
3018
+ const taken = (role) => items.filter((item) => {
3019
+ const classification = classified[item.id];
3020
+ return classification !== void 0 && isLeftOut(classification) && classification.roles[0] === role;
3021
+ }).length;
3022
+ const sourceOf = {
3023
+ card,
3024
+ choices,
3025
+ pieces: cardPieces(card, choices),
3026
+ records: {
3027
+ classified,
3028
+ roster
3114
3029
  }
3115
3030
  };
3116
- await atomicWriteJson(join(directory, IMPORT_FOLDER, FILE$2), {
3117
- schema: PROVENANCE_SCHEMA,
3118
- history: [...earlier?.history ?? [], line]
3119
- });
3031
+ const report = {
3032
+ prompts: extracted.prompts.map((entry) => entry.id),
3033
+ variables: extracted.variables.length,
3034
+ counts: {
3035
+ characters: extracted.characters.length,
3036
+ places: extracted.locations.length,
3037
+ factions: extracted.factions.length,
3038
+ things: extracted.items.length,
3039
+ lore: extracted.lore.length
3040
+ },
3041
+ hasPlayableCharacter: player !== void 0,
3042
+ opening: opening === void 0 ? "none" : "imported",
3043
+ alternateOpenings: alternates.length,
3044
+ cover,
3045
+ portrait,
3046
+ leftOut: {
3047
+ instructions: taken("instruction"),
3048
+ templates: taken("template"),
3049
+ irrelevant: taken("irrelevant"),
3050
+ scripts: card.ignored.scripts,
3051
+ dialect: dialectOf(card)
3052
+ },
3053
+ pictures: input.pictures ?? {
3054
+ found: 0,
3055
+ added: 0
3056
+ },
3057
+ pieces: outcomeCounts(pieceOutcomes(sourceOf, entitiesByPiece(sourceOf))),
3058
+ notes
3059
+ };
3060
+ await writeAtomically(join(directory, IMPORT_FOLDER, "report.json"), JSON.stringify(report, null, 2));
3061
+ return {
3062
+ ok: true,
3063
+ report,
3064
+ ...check.ok ? {} : { draft: check }
3065
+ };
3120
3066
  }
3121
- // src/shared/checks.ts
3122
- /** A check suite is made of kits; a person asks for kits with a command and an assistant with a tool. */
3123
- const KITS = [
3124
- "consistency",
3125
- "fidelity",
3126
- "quality",
3127
- "playability"
3128
- ];
3129
- const SEVERITIES = [
3130
- "error",
3131
- "warning",
3132
- "info"
3133
- ];
3134
- /** The commands of an authoring chat that start checks, and the kits each runs. */
3135
- const CHECK_COMMANDS = {
3136
- check: KITS,
3137
- "check-all": KITS,
3138
- "check-world": ["consistency"],
3139
- "check-card": ["fidelity"],
3140
- "check-quality": ["quality"],
3141
- "check-play": ["playability"]
3142
- };
3143
- const findingKey = (finding) => `${finding.check}\n${finding.code}\n${finding.where}\n${finding.message}`;
3144
- // src/host/import/card-tools.ts
3145
- /** The most of a piece one `card_read` returns; a long lorebook entry is read in turns. */
3146
- const READ_CHARS = 6e3;
3147
- const SNIPPET_CHARS = 240;
3148
- const DEFAULT_HITS = 8;
3149
- const MAX_HITS = 20;
3150
- const baseId = (id) => id.replace(/#\d+$/, "");
3151
- /** The folder inside `.import` where a newer version of the card waits to be applied to the world. */
3152
- const UPDATE_FOLDER = "update";
3153
- /** Everything the card holds, in the order the card has it. */
3154
- function cardPieces(card, choices) {
3155
- const read = new Set(itemise(card, choices).map((item) => baseId(item.id)));
3156
- const field = (id, label, text) => text.trim() === "" ? [] : [{
3157
- id,
3158
- label,
3159
- title: "",
3160
- keys: [],
3161
- text,
3162
- used: read.has(id)
3163
- }];
3164
- return [
3165
- ...field("field:description", "description", card.description),
3166
- ...field("field:personality", "personality", card.personality),
3167
- ...field("field:scenario", "scenario", card.scenario),
3168
- ...field("field:example", "example messages", card.messageExample),
3169
- ...field("field:first-message", "first message", card.firstMessage),
3170
- ...card.alternateGreetings.flatMap((text, index) => field(`field:greeting-${index + 1}`, `alternate greeting ${index + 1}`, text)),
3171
- ...field("field:creator-notes", "creator's notes", card.creatorNotes),
3172
- ...field("field:system-prompt", "system prompt", card.systemPrompt),
3173
- ...field("field:post-history", "post-history instructions", card.postHistoryInstructions),
3174
- ...(card.book?.entries ?? []).filter((entry) => entry.content.trim() !== "").map((entry) => ({
3175
- id: entry.id,
3176
- label: entry.title === "" ? entry.id : `entry “${entry.title}”`,
3177
- title: entry.title,
3178
- keys: entry.keys,
3179
- text: entry.content,
3180
- used: read.has(entry.id),
3181
- enabled: entry.enabled
3182
- }))
3183
- ];
3067
+ // src/host/import/pictures.ts
3068
+ /** The address is not one a picture may be fetched from: it names this machine or a private network. */
3069
+ var BlockedAddressError = class extends Error {};
3070
+ const MAX_BYTES = 20971520;
3071
+ const MIN_BYTES = 1024;
3072
+ const MAX_REDIRECTS = 4;
3073
+ const TIMEOUT_MS = 3e4;
3074
+ const CONCURRENCY = 8;
3075
+ const NON_IMAGE_EXTENSIONS = /* @__PURE__ */ new Set([
3076
+ "js",
3077
+ "mjs",
3078
+ "cjs",
3079
+ "css",
3080
+ "html",
3081
+ "htm",
3082
+ "json",
3083
+ "xml",
3084
+ "txt",
3085
+ "md",
3086
+ "map",
3087
+ "wasm",
3088
+ "php",
3089
+ "py",
3090
+ "woff",
3091
+ "woff2",
3092
+ "ttf",
3093
+ "otf",
3094
+ "eot",
3095
+ "mp3",
3096
+ "wav",
3097
+ "ogg",
3098
+ "flac",
3099
+ "m4a",
3100
+ "mp4",
3101
+ "webm",
3102
+ "mkv",
3103
+ "mov",
3104
+ "avi",
3105
+ "zip",
3106
+ "rar",
3107
+ "gz",
3108
+ "7z",
3109
+ "pdf",
3110
+ "doc",
3111
+ "docx"
3112
+ ]);
3113
+ const blocked = new BlockList();
3114
+ for (const [network, prefix] of [
3115
+ ["0.0.0.0", 8],
3116
+ ["10.0.0.0", 8],
3117
+ ["100.64.0.0", 10],
3118
+ ["127.0.0.0", 8],
3119
+ ["169.254.0.0", 16],
3120
+ ["172.16.0.0", 12],
3121
+ ["192.0.0.0", 24],
3122
+ ["192.0.2.0", 24],
3123
+ ["192.88.99.0", 24],
3124
+ ["192.168.0.0", 16],
3125
+ ["198.18.0.0", 15],
3126
+ ["198.51.100.0", 24],
3127
+ ["203.0.113.0", 24],
3128
+ ["224.0.0.0", 4],
3129
+ ["240.0.0.0", 4]
3130
+ ]) blocked.addSubnet(network, prefix, "ipv4");
3131
+ for (const [network, prefix] of [
3132
+ ["::", 128],
3133
+ ["::1", 128],
3134
+ ["64:ff9b::", 96],
3135
+ ["100::", 64],
3136
+ ["2001::", 32],
3137
+ ["2001:db8::", 32],
3138
+ ["2002::", 16],
3139
+ ["fc00::", 7],
3140
+ ["fe80::", 10],
3141
+ ["ff00::", 8]
3142
+ ]) blocked.addSubnet(network, prefix, "ipv6");
3143
+ /** Whether an address may be connected to: only public unicast addresses may. */
3144
+ function isPublicAddress(address) {
3145
+ const family = isIP(address);
3146
+ if (family === 0) return false;
3147
+ if (family === 4) return !blocked.check(address, "ipv4");
3148
+ const mapped = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i.exec(new URL(`http://[${address}]/`).hostname.replace(/^\[|\]$/g, ""));
3149
+ if (mapped !== null) {
3150
+ const high = Number.parseInt(mapped[1], 16);
3151
+ const low = Number.parseInt(mapped[2], 16);
3152
+ return isPublicAddress(`${high >> 8}.${high & 255}.${low >> 8}.${low & 255}`);
3153
+ }
3154
+ return !blocked.check(address, "ipv6");
3184
3155
  }
3185
- async function optional(file) {
3156
+ /** The address a picture may be asked for, or why not: web only, standard ports, no credentials, no private literal address. */
3157
+ function vetUrl(raw) {
3158
+ let url;
3186
3159
  try {
3187
- return JSON.parse(await readFile(file, "utf8"));
3188
- } catch (error) {
3189
- if (error.code === "ENOENT") return void 0;
3190
- throw error;
3160
+ url = new URL(raw);
3161
+ } catch {
3162
+ return "not an address";
3191
3163
  }
3164
+ if (url.protocol !== "http:" && url.protocol !== "https:") return "not a web address";
3165
+ if (url.username !== "" || url.password !== "") return "has credentials";
3166
+ if (url.port !== "" && url.port !== "80" && url.port !== "443") return "uses an unusual port";
3167
+ const host = url.hostname.replace(/^\[|\]$/g, "");
3168
+ if (isIP(host) !== 0 && !isPublicAddress(host)) return "is a private address";
3169
+ return url;
3192
3170
  }
3193
- /** The card kept in a world's import folder, and what the import made of it. The tools exist as long as the card does. */
3194
- async function readSource(directory, withRecords, version = "current") {
3195
- const folder = join(directory, IMPORT_FOLDER);
3196
- let read;
3197
- try {
3198
- read = await readImportFolder(version === "new" ? join(folder, UPDATE_FOLDER) : folder);
3199
- } catch (error) {
3200
- if (error.code === "ENOENT") throw new Error(version === "new" ? "no newer version of the card is waiting to update this world" : "this world has no source card: it was not imported from one, or its .import folder was removed");
3201
- throw error;
3171
+ /** The addresses worth asking about: a picture file name, or a name with no file type at all. */
3172
+ function candidateUrls(urls) {
3173
+ const candidates = [];
3174
+ const skipped = [];
3175
+ const seen = /* @__PURE__ */ new Set();
3176
+ for (const raw of urls) {
3177
+ const url = vetUrl(raw);
3178
+ if (typeof url === "string") {
3179
+ skipped.push({
3180
+ url: raw,
3181
+ reason: url
3182
+ });
3183
+ continue;
3184
+ }
3185
+ url.hash = "";
3186
+ const key = url.href;
3187
+ if (seen.has(key)) continue;
3188
+ seen.add(key);
3189
+ const extension = /\.([a-z0-9]{1,5})$/i.exec(url.pathname)?.[1]?.toLowerCase();
3190
+ if (extension !== void 0 && MEDIA_TYPES[`.${extension}`] === void 0) skipped.push({
3191
+ url: raw,
3192
+ reason: NON_IMAGE_EXTENSIONS.has(extension) ? "not an image" : "format not supported"
3193
+ });
3194
+ else candidates.push(key);
3202
3195
  }
3203
- const records = withRecords && version === "current" ? {
3204
- classified: await optional(join(folder, "classification.json")),
3205
- roster: await optional(join(folder, "roster.json")),
3206
- report: await optional(join(folder, "report.json"))
3207
- } : {};
3208
3196
  return {
3209
- card: read.card,
3210
- choices: read.choices,
3211
- pieces: cardPieces(read.card, read.choices),
3212
- records
3197
+ candidates,
3198
+ skipped
3213
3199
  };
3214
3200
  }
3215
- const rolesOf = (classified, id) => {
3216
- if (classified === void 0) return [];
3217
- return [...new Set(Object.entries(classified).filter(([key]) => baseId(key) === id).flatMap(([, entry]) => entry.roles))];
3201
+ /** Looks the name up and refuses it when any answer is not public, at the moment of connecting, so a name cannot change between the check and the use. */
3202
+ const guardedLookup = (hostname, options, callback) => {
3203
+ lookup(hostname, {
3204
+ ...options,
3205
+ all: true
3206
+ }, (error, addresses) => {
3207
+ if (error !== null) {
3208
+ callback(error, "", 0);
3209
+ return;
3210
+ }
3211
+ if (addresses.find((entry) => !isPublicAddress(entry.address)) !== void 0 || addresses.length === 0) {
3212
+ callback(new BlockedAddressError(`${hostname} is not a public address`), "", 0);
3213
+ return;
3214
+ }
3215
+ if (options.all === true) callback(null, addresses);
3216
+ else callback(null, addresses[0].address, addresses[0].family);
3217
+ });
3218
3218
  };
3219
- function snippet$1(text, at, length) {
3220
- const start = Math.max(0, at - Math.floor((SNIPPET_CHARS - length) / 2));
3221
- const end = Math.min(text.length, start + SNIPPET_CHARS);
3222
- return `${start > 0 ? "…" : ""}${text.slice(start, end).replace(/\s+/g, " ")}${end < text.length ? "…" : ""}`;
3219
+ async function readBody(response) {
3220
+ const chunks = [];
3221
+ let size = 0;
3222
+ for await (const chunk of response) {
3223
+ size += chunk.length;
3224
+ if (size > MAX_BYTES) {
3225
+ response.destroy();
3226
+ throw new Error("too large");
3227
+ }
3228
+ chunks.push(chunk);
3229
+ }
3230
+ return Buffer.concat(chunks);
3223
3231
  }
3224
- function searchPieces(pieces, query, limit) {
3225
- const phrase = query.trim().toLowerCase();
3226
- if (phrase === "") return [];
3227
- const words = [...new Set(phrase.split(/[\s,;,、]+/).filter((word) => word.length >= (/^[\x00-\x7f]+$/.test(word) ? 3 : 2)))];
3228
- const count = (text, needle) => {
3229
- let n = 0;
3230
- let first = -1;
3231
- for (let at = text.indexOf(needle); at >= 0; at = text.indexOf(needle, at + needle.length)) {
3232
- n += 1;
3233
- if (first < 0) first = at;
3232
+ /** One request, never following a redirect, with the connection itself checked against private networks. */
3233
+ const requestOnce = (url, method, signal) => new Promise((resolveReply, reject) => {
3234
+ const request$2 = (url.protocol === "https:" ? request$1 : request)(url, {
3235
+ method,
3236
+ lookup: guardedLookup,
3237
+ signal,
3238
+ timeout: TIMEOUT_MS,
3239
+ agent: false,
3240
+ headers: {
3241
+ "user-agent": "Mozilla/5.0 (compatible; dsh-roleplay-import)",
3242
+ accept: "image/*,*/*;q=0.5",
3243
+ "accept-encoding": "identity"
3234
3244
  }
3235
- return {
3236
- n,
3237
- first
3238
- };
3239
- };
3240
- const look = (text) => {
3241
- const lowered = text.toLowerCase();
3242
- const whole = count(lowered, phrase);
3243
- if (whole.n > 0) return {
3244
- matches: whole.n,
3245
- score: whole.n * 4 + 100,
3246
- at: whole.first,
3247
- length: phrase.length
3248
- };
3249
- const found = words.map((word) => ({
3250
- word,
3251
- ...count(lowered, word)
3252
- })).filter((entry) => entry.n > 0);
3253
- if (found.length === 0) return void 0;
3254
- const best = found.reduce((a, b) => a.first <= b.first ? a : b, found[0]);
3255
- return {
3256
- matches: found.reduce((sum, entry) => sum + entry.n, 0),
3257
- score: found.length * 10 + found.reduce((sum, entry) => sum + entry.n, 0),
3258
- at: best.first,
3259
- length: best.word.length
3260
- };
3261
- };
3262
- return pieces.flatMap((piece) => {
3263
- const inText = look(piece.text);
3264
- if (inText !== void 0) return [{
3265
- piece,
3266
- in: "text",
3267
- matches: inText.matches,
3268
- score: inText.score,
3269
- snippet: snippet$1(piece.text, inText.at, inText.length)
3270
- }];
3271
- const head = [piece.title, ...piece.keys].filter((part) => part !== "").join(" · ");
3272
- const inHead = look(head);
3273
- return inHead === void 0 ? [] : [{
3274
- piece,
3275
- in: "title or keys",
3276
- matches: inHead.matches,
3277
- score: inHead.score - 50,
3278
- snippet: `${piece.title === "" ? "" : `title: ${piece.title}`}${piece.keys.length === 0 ? "" : `${piece.title === "" ? "" : "; "}keys: ${piece.keys.join(", ")}`}`
3279
- }];
3280
- }).sort((a, b) => b.score - a.score).slice(0, limit).map(({ piece, matches, in: where, snippet: text }) => ({
3281
- piece,
3282
- matches,
3283
- in: where,
3284
- snippet: text
3285
- }));
3245
+ }, (response) => {
3246
+ const status = response.statusCode ?? 0;
3247
+ const type = String(response.headers["content-type"] ?? "").split(";")[0].trim().toLowerCase();
3248
+ const location = typeof response.headers.location === "string" ? response.headers.location : void 0;
3249
+ const length = Number(response.headers["content-length"] ?? 0);
3250
+ if (method === "HEAD" || status < 200 || status >= 300 || location !== void 0 && status >= 300) {
3251
+ response.destroy();
3252
+ resolveReply({
3253
+ status,
3254
+ type,
3255
+ location,
3256
+ body: Buffer.alloc(0)
3257
+ });
3258
+ return;
3259
+ }
3260
+ if (length > MAX_BYTES) {
3261
+ response.destroy();
3262
+ reject(/* @__PURE__ */ new Error("too large"));
3263
+ return;
3264
+ }
3265
+ readBody(response).then((body) => {
3266
+ resolveReply({
3267
+ status,
3268
+ type,
3269
+ location,
3270
+ body
3271
+ });
3272
+ }, reject);
3273
+ });
3274
+ request$2.on("timeout", () => {
3275
+ request$2.destroy(/* @__PURE__ */ new Error("timed out"));
3276
+ });
3277
+ request$2.on("error", reject);
3278
+ request$2.end();
3279
+ });
3280
+ /** Asks for the address, following up to a few redirects, each vetted like the first address. */
3281
+ async function follow(fetcher, start, method, signal) {
3282
+ let url = start;
3283
+ for (let hop = 0;; hop += 1) {
3284
+ const reply = await fetcher(url, method, signal);
3285
+ if (reply.status < 300 || reply.status >= 400 || reply.location === void 0) return reply;
3286
+ if (hop >= MAX_REDIRECTS) throw new Error("too many redirects");
3287
+ const next = vetUrl(new URL(reply.location, url).href);
3288
+ if (typeof next === "string") throw new BlockedAddressError(`a redirect ${next}`);
3289
+ url = next;
3290
+ }
3291
+ }
3292
+ const GENERIC_TYPES = /* @__PURE__ */ new Set([
3293
+ "",
3294
+ "application/octet-stream",
3295
+ "binary/octet-stream",
3296
+ "application/binary",
3297
+ "application/x-binary"
3298
+ ]);
3299
+ const SUPPORTED_TYPES = new Set(Object.values(MEDIA_TYPES));
3300
+ /** What a HEAD request says about an address: not a picture, or worth fetching (a picture, or an answer that settles nothing because the service does not do HEAD). */
3301
+ async function worthFetching(fetcher, url, signal) {
3302
+ let reply;
3303
+ try {
3304
+ reply = await follow(fetcher, url, "HEAD", signal);
3305
+ } catch (error) {
3306
+ return error instanceof BlockedAddressError ? error.message : void 0;
3307
+ }
3308
+ if (reply.status === 404 || reply.status === 410) return "gone";
3309
+ if (reply.status < 200 || reply.status >= 300) return void 0;
3310
+ if (reply.type.startsWith("image/")) return SUPPORTED_TYPES.has(reply.type) ? void 0 : "format not supported";
3311
+ return GENERIC_TYPES.has(reply.type) ? void 0 : `not an image (${reply.type})`;
3312
+ }
3313
+ function fileNameOf(url) {
3314
+ const last = url.pathname.split("/").pop() ?? "";
3315
+ try {
3316
+ return decodeURIComponent(last) || "image";
3317
+ } catch {
3318
+ return last || "image";
3319
+ }
3286
3320
  }
3287
- const versionParameter = {
3288
- type: "string",
3289
- enum: ["current", "new"],
3290
- description: "current (the default) is the card the world was made from; new is the newer version waiting to update it, when card_diff says there is one. Piece ids are those of that version."
3291
- };
3292
- const output$1 = {
3293
- schema: { type: "json" },
3294
- render: (_args, value) => [{
3295
- type: "text",
3296
- text: JSON.stringify(value, null, 1)
3297
- }]
3298
- };
3299
- const json$1 = (value) => JSON.parse(JSON.stringify(value));
3300
- const idOf = (exec) => {
3301
- const chat = exec.agent?.session.id;
3302
- const world = chat === void 0 ? void 0 : authoredWorldOf(chat);
3303
- if (world === void 0) throw new Error("this tool is available only in a world authoring chat");
3304
- return world;
3305
- };
3306
3321
  /**
3307
- * Read-only tools over the card a world was imported from, which stays in the world's `.import` folder: they work while that folder holds the card and say so when it does not.
3308
- * `records` adds what the import made of the card (the role of each piece, who it was used for); it is off only when a test compares the agent with and without it.
3322
+ * Fetches the pictures a card points to into the world's image library, as if the person had added them. Each address is vetted, asked about
3323
+ * with HEAD, and only then downloaded; what is kept is decided by the first bytes of the file, never by its name or declared type.
3309
3324
  */
3310
- function cardTools(worlds, options = {}) {
3311
- const withRecords = options.records !== false;
3312
- const source = async (exec, version = "current") => {
3313
- const id = idOf(exec);
3314
- const record = await worlds.find(id);
3315
- if (record === void 0) throw new Error(`the authored world "${id}" no longer exists`);
3316
- return await readSource(record.directory, withRecords, version);
3317
- };
3318
- const versionOf = (value) => {
3319
- if (value === void 0 || value === "current") return "current";
3320
- if (value === "new") return "new";
3321
- throw new Error("version must be current or new");
3322
- };
3323
- const find = (pieces, id) => {
3324
- const found = pieces.find((piece) => piece.id === id);
3325
- if (found === void 0) throw new Error(`the card has no piece "${id}": use an id from card_overview or card_search`);
3326
- return found;
3327
- };
3328
- return [
3329
- defineTool({
3330
- name: "card_overview",
3331
- description: "The card this world was imported from, which is kept: its name, creator and tags, every piece of it (field, greeting or lorebook entry) with its size, and, from the import's own records, what each piece was taken to be and which characters, places and things were made from it. Start here when the person asks where something in the world came from.",
3332
- parameters: {},
3333
- output: output$1,
3334
- isConcurrencySafe: () => true,
3335
- async execute(_args, exec) {
3336
- const { card, choices, pieces, records } = await source(exec);
3337
- const madeFrom = entitiesByPiece({
3338
- card,
3339
- choices,
3340
- pieces,
3341
- records
3342
- });
3343
- const made = (id) => madeFrom.get(id) ?? [];
3344
- return json$1({
3345
- card: {
3346
- name: card.name,
3347
- creator: card.creator,
3348
- version: card.characterVersion,
3349
- tags: card.tags,
3350
- format: card.sourceSpec,
3351
- ignoredScripts: card.ignored.scripts
3352
- },
3353
- pieces: pieces.map((piece) => ({
3354
- id: piece.id,
3355
- label: piece.label,
3356
- characters: piece.text.length,
3357
- readByTheImport: piece.used,
3358
- ...piece.enabled === void 0 ? {} : { switchedOnInCard: piece.enabled },
3359
- ...withRecords ? {
3360
- takenToBe: rolesOf(records.classified, piece.id),
3361
- madeInto: made(piece.id)
3362
- } : {}
3363
- })),
3364
- ...records.roster === void 0 ? {} : { imported: records.roster.map((entity) => ({
3365
- name: entity.name,
3366
- kind: entity.kind,
3367
- aliases: entity.aliases,
3368
- playedByThePlayer: entity.player
3369
- })) },
3370
- ...records.report === void 0 ? {} : {
3371
- importNotes: records.report.notes,
3372
- leftOut: records.report.leftOut
3373
- },
3374
- note: "The import's records are its own judgement and can be wrong or incomplete; check them against the text of the piece with card_read."
3325
+ async function fetchPictures(world, urls, deps = {}) {
3326
+ const fetcher = deps.fetcher ?? requestOnce;
3327
+ const { candidates, skipped: refused } = candidateUrls(urls);
3328
+ const skipped = [...refused];
3329
+ const added = [];
3330
+ const hashes = await libraryHashes(world);
3331
+ await mapLimit(candidates, deps.concurrency ?? CONCURRENCY, async (address) => {
3332
+ const signal = AbortSignal.any([AbortSignal.timeout(TIMEOUT_MS * 2), ...deps.signal === void 0 ? [] : [deps.signal]]);
3333
+ const url = new URL(address);
3334
+ try {
3335
+ const refusal = await worthFetching(fetcher, url, signal);
3336
+ if (refusal !== void 0) {
3337
+ skipped.push({
3338
+ url: address,
3339
+ reason: refusal
3375
3340
  });
3341
+ return;
3376
3342
  }
3377
- }),
3378
- defineTool({
3379
- name: "card_search",
3380
- description: "Searches everything the card says (fields, greetings, lorebook entries and their keys) for a name or phrase and returns the pieces that contain it, best first, each with a short snippet and whether it was found in the text or only in the title or keys of an entry. A hit is a pointer, not evidence: read the piece with card_read before you answer from it.",
3381
- parameters: {
3382
- query: {
3383
- type: "string",
3384
- required: true,
3385
- description: "A name or phrase to look for, in the card's own language."
3386
- },
3387
- version: versionParameter,
3388
- limit: {
3389
- type: "number",
3390
- description: `How many pieces to return, at most ${MAX_HITS} (default ${DEFAULT_HITS}).`
3391
- }
3392
- },
3393
- output: output$1,
3394
- isConcurrencySafe: () => true,
3395
- async execute(args, exec) {
3396
- const { pieces } = await source(exec, versionOf(args.version));
3397
- const limit = Math.min(MAX_HITS, Math.max(1, Math.floor(typeof args.limit === "number" && Number.isFinite(args.limit) ? args.limit : DEFAULT_HITS)));
3398
- const hits = searchPieces(pieces, args.query, limit);
3399
- return json$1({
3400
- query: args.query,
3401
- found: hits.length,
3402
- hits: hits.map(({ piece, matches, in: where, snippet: text }) => ({
3403
- piece: piece.id,
3404
- label: piece.label,
3405
- matches,
3406
- foundIn: where,
3407
- snippet: text
3408
- }))
3343
+ const reply = await follow(fetcher, url, "GET", signal);
3344
+ if (reply.status < 200 || reply.status >= 300) {
3345
+ skipped.push({
3346
+ url: address,
3347
+ reason: `answered ${reply.status}`
3409
3348
  });
3349
+ return;
3410
3350
  }
3411
- }),
3412
- defineTool({
3413
- name: "card_read",
3414
- description: `The text of one piece of the card, word for word as the card has it (the card's own placeholders such as {{user}} included). A long piece is read in turns of ${READ_CHARS} characters: when "next" is given, call again with that offset.`,
3415
- parameters: {
3416
- piece: {
3417
- type: "string",
3418
- required: true,
3419
- description: "A piece id from card_overview, card_search or card_diff, such as field:description or entry:3."
3420
- },
3421
- version: versionParameter,
3422
- offset: {
3423
- type: "number",
3424
- description: "Where to start reading, in characters (default 0)."
3425
- }
3426
- },
3427
- output: output$1,
3428
- isConcurrencySafe: () => true,
3429
- async execute(args, exec) {
3430
- const { pieces } = await source(exec, versionOf(args.version));
3431
- const piece = find(pieces, args.piece);
3432
- const from = Math.min(piece.text.length, Math.max(0, Math.floor(typeof args.offset === "number" && Number.isFinite(args.offset) ? args.offset : 0)));
3433
- const to = Math.min(piece.text.length, from + READ_CHARS);
3434
- return json$1({
3435
- piece: piece.id,
3436
- label: piece.label,
3437
- ...piece.keys.length === 0 ? {} : { keys: piece.keys },
3438
- from,
3439
- to,
3440
- of: piece.text.length,
3441
- ...to < piece.text.length ? { next: to } : {},
3442
- text: piece.text.slice(from, to)
3351
+ if (imageExtensionOf(reply.body) === void 0) {
3352
+ skipped.push({
3353
+ url: address,
3354
+ reason: "not an image"
3443
3355
  });
3356
+ return;
3444
3357
  }
3445
- }),
3446
- defineTool({
3447
- name: "card_origin",
3448
- description: "Where a character, place, organisation or thing of this world came from: the pieces of the card the import read to make it (the pieces about it first, then those that only mention it), with what each was taken to be. For a name the import did not make anything of, the pieces of the card that mention it. Then read the pieces with card_read to answer from their text.",
3449
- parameters: { name: {
3450
- type: "string",
3451
- required: true,
3452
- description: "The name or an alias, as the world or the card has it."
3453
- } },
3454
- output: output$1,
3455
- isConcurrencySafe: () => true,
3456
- async execute(args, exec) {
3457
- const { card, choices, pieces, records } = await source(exec);
3458
- const entity = records.roster === void 0 ? void 0 : findEntity(records.roster, args.name);
3459
- if (entity === void 0 || records.classified === void 0) return json$1({
3460
- name: args.name,
3461
- madeByTheImport: false,
3462
- mentions: searchPieces(pieces, args.name, DEFAULT_HITS).map(({ piece, in: where, snippet: text }) => ({
3463
- piece: piece.id,
3464
- label: piece.label,
3465
- foundIn: where,
3466
- snippet: text
3467
- }))
3358
+ if (reply.body.length < MIN_BYTES) {
3359
+ skipped.push({
3360
+ url: address,
3361
+ reason: "too small"
3468
3362
  });
3469
- return json$1({
3470
- name: entity.name,
3471
- kind: entity.kind,
3472
- aliases: entity.aliases,
3473
- madeByTheImport: true,
3474
- usedPieces: originOf(card, choices, pieces, records, entity.name).map((entry) => ({
3475
- piece: entry.piece,
3476
- label: entry.label,
3477
- how: entry.how,
3478
- takenToBe: rolesOf(records.classified, entry.piece)
3479
- }))
3363
+ return;
3364
+ }
3365
+ const { path, added: isNew } = await addToLibraryOnce(world, reply.body, fileNameOf(url), hashes);
3366
+ if (!isNew) {
3367
+ skipped.push({
3368
+ url: address,
3369
+ reason: "a copy of another picture",
3370
+ of: path
3480
3371
  });
3372
+ return;
3481
3373
  }
3482
- })
3374
+ added.push({
3375
+ url: address,
3376
+ path
3377
+ });
3378
+ } catch (error) {
3379
+ skipped.push({
3380
+ url: address,
3381
+ reason: error instanceof Error ? error.message : String(error)
3382
+ });
3383
+ }
3384
+ });
3385
+ return {
3386
+ found: candidates.length,
3387
+ added,
3388
+ skipped
3389
+ };
3390
+ }
3391
+ /** The key an address is known by: the way `candidateUrls` reads it, without a fragment. */
3392
+ function addressKey(raw) {
3393
+ try {
3394
+ const url = new URL(raw);
3395
+ url.hash = "";
3396
+ return url.href;
3397
+ } catch {
3398
+ return raw;
3399
+ }
3400
+ }
3401
+ const FINAL = [
3402
+ /^not an? /,
3403
+ /^format not supported/,
3404
+ /^gone$/,
3405
+ /^too small$/,
3406
+ /^too large$/,
3407
+ /^a copy of /,
3408
+ /^has credentials/,
3409
+ /^uses an unusual port/,
3410
+ /^is a private address/,
3411
+ /^a redirect /,
3412
+ /is not a public address$/,
3413
+ /^answered (4(?!08|29)\d\d)$/
3414
+ ];
3415
+ /** Whether asking again could give another answer: a picture that is not one, is gone or may not be fetched stays settled; a timeout, a server error or a refusal to wait does not. */
3416
+ const worthAskingAgain = (reason) => !FINAL.some((pattern) => pattern.test(reason));
3417
+ /**
3418
+ * The addresses of a newer card to ask for: those the earlier card did not have, and those it had that were not settled (the record says a request failed). With no record
3419
+ * of that fetch every address of the earlier card counts as settled, so what the person removed from the library is not brought back.
3420
+ */
3421
+ function addressesToFetch(earlier, later, record) {
3422
+ const settled = new Set(earlier.map(addressKey));
3423
+ if (record !== void 0) {
3424
+ const unsettled = new Set(record.skipped.filter((entry) => worthAskingAgain(entry.reason)).map((entry) => addressKey(entry.url)));
3425
+ const fetched = new Set(record.added.map((entry) => addressKey(entry.url)));
3426
+ for (const key of [...settled]) if (unsettled.has(key) && !fetched.has(key)) settled.delete(key);
3427
+ }
3428
+ return later.filter((url) => !settled.has(addressKey(url)));
3429
+ }
3430
+ /** The record after a second fetch: what was added stays, an address asked again takes its new answer. */
3431
+ function mergeReports(earlier, later, found) {
3432
+ const asked = new Set([...later.added.map((entry) => entry.url), ...later.skipped.map((entry) => entry.url)].map(addressKey));
3433
+ return {
3434
+ found,
3435
+ added: [...earlier?.added ?? [], ...later.added],
3436
+ skipped: [...(earlier?.skipped ?? []).filter((entry) => !asked.has(addressKey(entry.url))), ...later.skipped]
3437
+ };
3438
+ }
3439
+ // src/host/catalogue/errors.ts
3440
+ var CatalogueError = class extends Error {
3441
+ failure;
3442
+ detail;
3443
+ constructor(message, failure, detail) {
3444
+ super(message);
3445
+ this.failure = failure;
3446
+ this.detail = detail;
3447
+ }
3448
+ };
3449
+ // src/host/catalogue/origin.ts
3450
+ const ORIGIN_FILE = ".catalogue.json";
3451
+ const MAX_LISTED_CHANGES = 12;
3452
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
3453
+ /** The origin recorded in a world's folder, or nothing when it has none. @throws {CatalogueError} when the record is damaged. */
3454
+ async function readOrigin(directory) {
3455
+ let text;
3456
+ try {
3457
+ text = await readFile(join(directory, ORIGIN_FILE), "utf8");
3458
+ } catch (error) {
3459
+ if (error.code === "ENOENT") return void 0;
3460
+ throw error;
3461
+ }
3462
+ const damaged = () => new CatalogueError(`${ORIGIN_FILE} in ${directory} is damaged`, "invalid");
3463
+ let value;
3464
+ try {
3465
+ value = JSON.parse(text);
3466
+ } catch {
3467
+ throw damaged();
3468
+ }
3469
+ if (!isRecord(value)) throw damaged();
3470
+ const { kind, root, resourceId, versionId, version, files } = value;
3471
+ if (kind !== "release" && kind !== "card" || typeof root !== "string" || typeof resourceId !== "string" || typeof versionId !== "string" || typeof version !== "string") throw damaged();
3472
+ if (kind === "card") return {
3473
+ kind,
3474
+ root,
3475
+ resourceId,
3476
+ versionId,
3477
+ version
3478
+ };
3479
+ if (!isRecord(files) || !Object.values(files).every((hash) => typeof hash === "string")) throw damaged();
3480
+ return {
3481
+ kind,
3482
+ root,
3483
+ resourceId,
3484
+ versionId,
3485
+ version,
3486
+ files
3487
+ };
3488
+ }
3489
+ async function writeOrigin(directory, origin) {
3490
+ const file = join(directory, ORIGIN_FILE);
3491
+ await atomicWriteJson(file, origin, { indent: 2 });
3492
+ }
3493
+ /** SHA-256 of every file in a world folder; entries whose names start with a dot (this record, the trash, import state) are not the world's own. */
3494
+ async function hashWorld(directory, skip = () => false) {
3495
+ const hashes = {};
3496
+ const walk = async (relative) => {
3497
+ for (const entry of await readdir(join(directory, relative), { withFileTypes: true })) {
3498
+ if (relative === "" && entry.name.startsWith(".")) continue;
3499
+ const path = relative === "" ? entry.name : `${relative}/${entry.name}`;
3500
+ if (entry.isDirectory()) await walk(path);
3501
+ else if (entry.isFile() && !skip(path)) hashes[path] = createHash("sha256").update(await readFile(join(directory, path))).digest("hex");
3502
+ }
3503
+ };
3504
+ await walk("");
3505
+ return hashes;
3506
+ }
3507
+ function compare(recorded, current) {
3508
+ return {
3509
+ changed: Object.keys(recorded).filter((path) => current[path] !== void 0 && current[path] !== recorded[path]),
3510
+ added: Object.keys(current).filter((path) => recorded[path] === void 0),
3511
+ removed: Object.keys(recorded).filter((path) => current[path] === void 0)
3512
+ };
3513
+ }
3514
+ const isUnchanged = (changes) => changes.changed.length + changes.added.length + changes.removed.length === 0;
3515
+ /** The differences as short lines for a person to read, capped. */
3516
+ function describeChanges(changes) {
3517
+ const lines = [
3518
+ ...changes.changed.map((path) => `changed: ${path}`),
3519
+ ...changes.added.map((path) => `added: ${path}`),
3520
+ ...changes.removed.map((path) => `removed: ${path}`)
3483
3521
  ];
3522
+ return lines.length <= MAX_LISTED_CHANGES ? lines : [...lines.slice(0, MAX_LISTED_CHANGES), `and ${lines.length - MAX_LISTED_CHANGES} more`];
3484
3523
  }
3485
- /** For each piece of the card, the names of the characters, places and things the import made from it. */
3486
- function entitiesByPiece(source) {
3487
- const made = /* @__PURE__ */ new Map();
3488
- for (const entity of source.records.roster ?? []) for (const entry of originOf(source.card, source.choices, source.pieces, source.records, entity.name)) made.set(entry.piece, [...made.get(entry.piece) ?? [], entity.name]);
3489
- return made;
3524
+ /** How a world's files compare with the release it came from: the differences, or `unverified` when no record of the release exists. */
3525
+ async function localChanges(directory) {
3526
+ const origin = await readOrigin(directory);
3527
+ if (origin?.kind !== "release") return "unverified";
3528
+ return compare(origin.files, await hashWorld(directory));
3490
3529
  }
3491
- /** The pieces an entity was made from, as the build chose them: the ones the survey said are about it, then those that name it. */
3492
- function originOf(card, choices, pieces, records, name, items = itemise(card, choices)) {
3493
- if (records.roster === void 0 || records.classified === void 0) return [];
3494
- const entity = findEntity(records.roster, name);
3495
- if (entity === void 0) return [];
3496
- const names = [entity.name, ...entity.aliases].map(norm);
3497
- const chosen = sourcesFor(entity, items, records.classified);
3498
- const about = (id) => (records.classified?.[id]?.targets ?? []).some((target) => target.kind === entity.kind && names.includes(norm(target.name)));
3499
- const seen = /* @__PURE__ */ new Set();
3500
- return chosen.flatMap((item) => {
3501
- const id = baseId(item.id);
3502
- const piece = pieces.find((entry) => entry.id === id);
3503
- if (piece === void 0 || seen.has(id)) return [];
3504
- seen.add(id);
3505
- return [{
3506
- piece: id,
3507
- label: piece.label,
3508
- how: about(item.id) ? "about it" : "mentions it"
3509
- }];
3530
+ // src/host/import/snapshot.ts
3531
+ const SNAPSHOT = "files.json";
3532
+ /** The pictures of a world are fetched and generated in the background and are not what a card says. */
3533
+ const notTheCards = (path) => path.startsWith("images/");
3534
+ /** What a world's files were when its import was over; "changed since" is measured against it. */
3535
+ async function writeSnapshot(directory) {
3536
+ const file = join(directory, IMPORT_FOLDER, SNAPSHOT);
3537
+ await atomicWriteJson(file, await hashWorld(directory, notTheCards));
3538
+ }
3539
+ /** How a world compares with its snapshot: `no` for no change, `yes` with the files, and `unknown` when the import left no snapshot (an older import). */
3540
+ async function editsSince(directory) {
3541
+ let recorded;
3542
+ try {
3543
+ recorded = JSON.parse(await readFile(join(directory, IMPORT_FOLDER, SNAPSHOT), "utf8"));
3544
+ } catch (error) {
3545
+ if (error.code === "ENOENT") return { edited: "unknown" };
3546
+ throw error;
3547
+ }
3548
+ const changes = compare(recorded, await hashWorld(directory, notTheCards));
3549
+ return isUnchanged(changes) ? { edited: "no" } : {
3550
+ edited: "yes",
3551
+ changes: describeChanges(changes)
3552
+ };
3553
+ }
3554
+ // src/host/import/provenance.ts
3555
+ const FILE$2 = "provenance.json";
3556
+ const PROVENANCE_SCHEMA = "dsh-roleplay-import/v1";
3557
+ /** The import record of a world, or nothing for a world imported before records were kept. */
3558
+ async function readProvenance(directory) {
3559
+ let text;
3560
+ try {
3561
+ text = await readFile(join(directory, IMPORT_FOLDER, FILE$2), "utf8");
3562
+ } catch (error) {
3563
+ if (error.code === "ENOENT") return void 0;
3564
+ throw error;
3565
+ }
3566
+ const value = JSON.parse(text);
3567
+ if (value.schema !== "dsh-roleplay-import/v1" || !Array.isArray(value.history)) throw new Error(`${FILE$2} is not an import record this version reads`);
3568
+ return value;
3569
+ }
3570
+ /** Adds one line to the world's import record: what card it came from and, for an import, what was written. */
3571
+ async function recordProvenance(directory, entry, cardJson) {
3572
+ const earlier = await readProvenance(directory);
3573
+ const line = {
3574
+ ...entry,
3575
+ at: (/* @__PURE__ */ new Date()).toISOString(),
3576
+ card: {
3577
+ name: entry.card.name,
3578
+ sha256: createHash("sha256").update(cardJson).digest("hex")
3579
+ }
3580
+ };
3581
+ await atomicWriteJson(join(directory, IMPORT_FOLDER, FILE$2), {
3582
+ schema: PROVENANCE_SCHEMA,
3583
+ history: [...earlier?.history ?? [], line]
3510
3584
  });
3511
3585
  }
3586
+ // src/shared/checks.ts
3587
+ /** A check suite is made of kits; a person asks for kits with a command and an assistant with a tool. */
3588
+ const KITS = [
3589
+ "consistency",
3590
+ "fidelity",
3591
+ "quality",
3592
+ "playability"
3593
+ ];
3594
+ const SEVERITIES = [
3595
+ "error",
3596
+ "warning",
3597
+ "info"
3598
+ ];
3599
+ /** The commands of an authoring chat that start checks, and the kits each runs. */
3600
+ const CHECK_COMMANDS = {
3601
+ check: KITS,
3602
+ "check-all": KITS,
3603
+ "check-world": ["consistency"],
3604
+ "check-card": ["fidelity"],
3605
+ "check-quality": ["quality"],
3606
+ "check-play": ["playability"]
3607
+ };
3608
+ const findingKey = (finding) => `${finding.check}\n${finding.code}\n${finding.where}\n${finding.message}`;
3512
3609
  // src/host/checks/terms.ts
3513
3610
  const HAN = /[㐀-鿿]+/gu;
3514
3611
  /** Words of fewer letters are mostly the glue of a language, which says nothing about whether two texts share facts. */
@@ -4311,6 +4408,20 @@ function worldFieldChanges(before, after) {
4311
4408
  });
4312
4409
  return changes;
4313
4410
  }
4411
+ /** What a newer card does to the card's own picture: the hashes of the two files, or nothing for a card with none. */
4412
+ function pictureChange(before, after) {
4413
+ if (before === void 0) return after === void 0 ? "none" : "added";
4414
+ return after === void 0 ? "removed" : before === after ? "unchanged" : "changed";
4415
+ }
4416
+ /** The picture addresses a newer card points to that the older did not, and those it no longer points to. */
4417
+ function addressChanges(before, after) {
4418
+ const was = new Set(before.urls ?? []);
4419
+ const now = new Set(after.urls ?? []);
4420
+ return {
4421
+ added: [...now].filter((url) => !was.has(url)),
4422
+ dropped: [...was].filter((url) => !now.has(url))
4423
+ };
4424
+ }
4314
4425
  // src/host/checks/judge.ts
4315
4426
  /**
4316
4427
  * A yes-or-no about one thing: the decision model answers when the import is set to use one and it is sure; when it is not sure, cannot be used, or none is set, `ask` puts the
@@ -4856,6 +4967,40 @@ function comparisonProblem(value, source, document) {
4856
4967
  if (missing(value.dropped, source)) return "the quote of every dropped fact must be a passage of the source, exactly as it is written there";
4857
4968
  if (missing(value.invented, document)) return "the quote of every invented fact must be a passage of the document, exactly as it is written there";
4858
4969
  }
4970
+ const STATED_SCHEMA = {
4971
+ type: "object",
4972
+ properties: {
4973
+ reason: { type: "string" },
4974
+ stated: { type: "boolean" }
4975
+ },
4976
+ required: ["reason", "stated"],
4977
+ additionalProperties: false
4978
+ };
4979
+ /**
4980
+ * The reported facts that a second, closed question does not find in the text they are said to be missing from: the comparison reads two texts at once, and across two
4981
+ * languages it calls a fact missing that the other text does say.
4982
+ */
4983
+ async function unstated(model, facts, text, subject) {
4984
+ const instructions = model.text("checks/decide-stated");
4985
+ const kept = [];
4986
+ for (const entry of facts) {
4987
+ if (model.cancelled?.() === true) break;
4988
+ const state = {
4989
+ ...subject === void 0 ? {} : { subject },
4990
+ fact: entry.fact,
4991
+ text: text.slice(0, SOURCE_CHARS * 2)
4992
+ };
4993
+ if (!(await verdictOf(model, state, "stated", instructions, async () => {
4994
+ return (await model.run({
4995
+ persona: instructions,
4996
+ prompt: `${subject === void 0 ? "" : `${subject}\n\n`}${entry.fact}\n\n${state.text}`,
4997
+ result: STATED_SCHEMA,
4998
+ maxTokens: 500
4999
+ })).stated;
5000
+ })).yes) kept.push(entry);
5001
+ }
5002
+ return kept;
5003
+ }
4859
5004
  const fidelityChecks = [
4860
5005
  coverage,
4861
5006
  names,
@@ -4908,14 +5053,16 @@ const fidelityChecks = [
4908
5053
  check: (value) => comparisonProblem(value, text, document)
4909
5054
  });
4910
5055
  const once = (list) => list.filter((entry, at) => list.findIndex((other) => squashed(other.quote) === squashed(entry.quote)) === at);
4911
- for (const entry of once(answer.dropped)) found.push({
5056
+ const dropped = await unstated(model, once(answer.dropped), document, subject);
5057
+ const invented = await unstated(model, once(answer.invented), text);
5058
+ for (const entry of dropped) found.push({
4912
5059
  severity: "warning",
4913
5060
  code: "fact-dropped",
4914
5061
  where,
4915
5062
  message: `the card says “${snippet(entry.quote)}” (${entry.fact}), which ${where} does not say`,
4916
5063
  hint: "Read the piece with card_read; if the fact matters to the story, add it to the document as the card has it."
4917
5064
  });
4918
- for (const entry of once(answer.invented)) found.push({
5065
+ for (const entry of invented) found.push({
4919
5066
  severity: "warning",
4920
5067
  code: "fact-invented",
4921
5068
  where,
@@ -5948,6 +6095,40 @@ async function structureImport(world, language) {
5948
6095
  ...result.draft === void 0 ? {} : { draft: result.draft }
5949
6096
  };
5950
6097
  }
6098
+ // src/host/import/job-cache.ts
6099
+ const FOLDER = "jobs";
6100
+ const folderOf = (directory) => join(directory, IMPORT_FOLDER, FOLDER);
6101
+ /** A job is the same job when its instructions, its question and the shape of its answer are the same. */
6102
+ const keyOf = (job) => createHash("sha256").update(JSON.stringify([
6103
+ job.persona,
6104
+ job.prompt,
6105
+ job.result
6106
+ ])).digest("hex");
6107
+ /**
6108
+ * Keeps the answer of every job that finished, so that a step run again after a failure, a stop or a restart asks only for the jobs that did not finish. An answer that the
6109
+ * job's own check refuses now is asked for again.
6110
+ */
6111
+ function cachedJobs(directory, run) {
6112
+ return async (job) => {
6113
+ const file = join(folderOf(directory), `${keyOf(job)}.json`);
6114
+ try {
6115
+ const kept = JSON.parse(await readFile(file, "utf8"));
6116
+ if (job.check?.(kept) === void 0) return kept;
6117
+ } catch (error) {
6118
+ if (error.code !== "ENOENT" && !(error instanceof SyntaxError)) throw error;
6119
+ }
6120
+ const answer = await run(job);
6121
+ await atomicWrite(file, JSON.stringify(answer), { createDirectory: true });
6122
+ return answer;
6123
+ };
6124
+ }
6125
+ /** Forgets the kept answers: a new survey starts the work over, and a finished import has no use for them. */
6126
+ async function forgetJobs(directory) {
6127
+ await rm(folderOf(directory), {
6128
+ recursive: true,
6129
+ force: true
6130
+ });
6131
+ }
5951
6132
  // src/host/import/tools.ts
5952
6133
  const SURVEY_TOOL = "import_survey";
5953
6134
  const BUILD_TOOL = "import_build";
@@ -5994,10 +6175,10 @@ function modelKeyOf(agent, named) {
5994
6175
  return model === void 0 ? "" : `${model.provider}/${model.model}`;
5995
6176
  }
5996
6177
  /** The runner of an import: every job is a narrow agent below the chat's own, on the chat's model. */
5997
- function contextOf(exec, settings, text, language) {
6178
+ function contextOf(exec, settings, text, language, keepIn) {
5998
6179
  const agent = exec.agent;
5999
6180
  if (agent === void 0) throw new Error("the import tools can only be called by an agent");
6000
- const run = async (job) => {
6181
+ const ask = async (job) => {
6001
6182
  for (let attempt = 1;; attempt += 1) {
6002
6183
  const named = namedModel(settings.model);
6003
6184
  const model = modelKeyOf(agent, named.model);
@@ -6031,6 +6212,7 @@ function contextOf(exec, settings, text, language) {
6031
6212
  }
6032
6213
  }
6033
6214
  };
6215
+ const run = keepIn === void 0 ? ask : cachedJobs(keepIn, ask);
6034
6216
  const decide = settings.decisions === "decision" ? (state, asks) => {
6035
6217
  const service = agent.ctx.get("decisions");
6036
6218
  if (service === void 0) return Promise.reject(/* @__PURE__ */ new Error("no decision service is selected, or the selected one is not loaded"));
@@ -6083,14 +6265,16 @@ const toolNames = {
6083
6265
  function importTools(worlds, settings, prompting) {
6084
6266
  const builds = /* @__PURE__ */ new Map();
6085
6267
  /** Runs one step for the world of the calling chat; what the model should do next is told in the result, in the person's language. */
6086
- const step = async (exec, work, next) => {
6268
+ const step = async (exec, work, next, fresh = false) => {
6087
6269
  const world = await worldOf(worlds, exec);
6270
+ if (fresh) await forgetJobs(world.directory);
6088
6271
  const language = prompting.languages().get(world.id);
6089
6272
  const text = (id, variables) => prompting.prompts().text(id, language, {
6090
6273
  ...toolNames,
6091
6274
  ...variables
6092
6275
  });
6093
- const result = await work(contextOf(exec, settings(), text, language), world.directory);
6276
+ const result = await work(contextOf(exec, settings(), text, language, world.directory), world.directory);
6277
+ if (result.ok === true) await forgetJobs(world.directory);
6094
6278
  return json({
6095
6279
  ...result,
6096
6280
  next: next(text, result)
@@ -6105,7 +6289,7 @@ function importTools(worlds, settings, prompting) {
6105
6289
  isConcurrencySafe: () => false,
6106
6290
  async execute(args, exec) {
6107
6291
  checkedCard(args);
6108
- return await step(exec, survey, (text) => text("import/next-survey"));
6292
+ return await step(exec, survey, (text) => text("import/next-survey"), true);
6109
6293
  }
6110
6294
  }),
6111
6295
  defineTool({
@@ -6287,6 +6471,6 @@ function limitAuthoringAgents(ctx, kindOf) {
6287
6471
  return await next();
6288
6472
  });
6289
6473
  }
6290
- export { parseWorldFile as $, pictureOf as $t, entitiesByPiece as A, routineProblems as At, readOrigin as B, loadFactions as Bt, readWorldInfo as C, populateItems as Ct, baseId as D, populateRoutines as Dt, UPDATE_FOLDER as E, populateLore as Et, writeSnapshot as F, LOCATIONS_DIR as Ft, candidateUrls as G, loadCalendar as Gt, CatalogueError as H, loadDice as Ht, describeChanges as I, loadLocations as It, mergeReports as J, loadCharacters as Jt, fetchPictures as K, parseCalendarFile as Kt, hashWorld as L, ITEMS_DIR as Lt, CHECK_COMMANDS as M, loadTriggers as Mt, recordProvenance as N, LORE_DIR as Nt, cardPieces as O, populateTriggers as Ot, editsSince as P, loadLore as Pt, WORLD_FILE_NAME as Q, isPng as Qt, isUnchanged as R, loadItems as Rt, editWorldInfo as S, populateFactions as St, loadCardEvidence as T, populateLocations as Tt, addressKey as U, parseDiceFile as Ut, writeOrigin as V, DICE_FILE_NAME as Vt, addressesToFetch as W, CALENDAR_FILE_NAME as Wt, cardCharacter as X, knownOutputCeiling as Xt, vetUrl as Y, subfolders as Yt, writePortrait as Z, ImportCardError as Zt, runChecks as _, knowsCharacter as _t, importHooks as a, authoringChatSessionId as an, entriesOf as at, matchPieces as b, populateClock as bt, importPhase as c, loadLorebook as ct, markReady as d, reserveLorebook as dt, readCard as en, parseWorldInfoEdit as et, settleDraft as f, updateLorebookInfo as ft, CHECKS as g, withScratchStory as gt, presentReport as h, loadValidatedWorld as ht, contextOf as i, authoredWorldOf as in, deleteLorebookEntry as it, readSource as j, TRIGGERS_FILE_NAME as jt, cardTools as k, populateVariables as kt, isReviewing as l, lorebookDirectory as lt, checkImported as m, checkWorld as mt, limitAuthoringAgents as n, DICE_READINGS as nn, createLorebook as nt, importTools as o, isLorebookName as ot, structureImport as p, writeLorebookEntry as pt, guardedLookup as q, CHARACTERS_DIR as qt, MAX_PREVIEW_TEXT as r, DICE_RESULTS as rn, deleteLorebook as rt, worldOf as s, listLorebooks as st, AuthoringTools as t, DICE_COMPARISONS as tn, addLorebookEntries as tt, keepDraft as u, readLorebook as ut, cardChanges as v, populateCalendar as vt, loadWorldParts as w, populateKnowledge as wt, worldFieldChanges as x, populateDice as xt, diffText as y, populateCharacters as yt, localChanges as z, FACTIONS_DIR as zt };
6474
+ export { readSource as $, ImportCardError as $t, editsSince as A, populateTriggers as At, addressesToFetch as B, loadItems as Bt, worldFieldChanges as C, populateDice as Ct, loadCardEvidence as D, populateLocations as Dt, loadWorldParts as E, populateKnowledge as Et, localChanges as F, LORE_DIR as Ft, vetUrl as G, parseDiceFile as Gt, fetchPictures as H, loadFactions as Ht, readOrigin as I, loadLore as It, UPDATE_FOLDER as J, parseCalendarFile as Jt, cardCharacter as K, CALENDAR_FILE_NAME as Kt, writeOrigin as L, LOCATIONS_DIR as Lt, describeChanges as M, routineProblems as Mt, hashWorld as N, TRIGGERS_FILE_NAME as Nt, CHECK_COMMANDS as O, populateLore as Ot, isUnchanged as P, loadTriggers as Pt, entitiesByPiece as Q, knownOutputCeiling as Qt, CatalogueError as R, loadLocations as Rt, pictureChange as S, populateClock as St, readWorldInfo as T, populateItems as Tt, guardedLookup as U, DICE_FILE_NAME as Ut, candidateUrls as V, FACTIONS_DIR as Vt, mergeReports as W, loadDice as Wt, cardPieces as X, loadCharacters as Xt, baseId as Y, CHARACTERS_DIR as Yt, cardTools as Z, subfolders as Zt, runChecks as _, loadValidatedWorld as _t, importHooks as a, DICE_RESULTS as an, deleteLorebook as at, diffText as b, populateCalendar as bt, importPhase as c, isLorebookName as ct, markReady as d, lorebookDirectory as dt, isPng as en, WORLD_FILE_NAME as et, settleDraft as f, readLorebook as ft, CHECKS as g, checkWorld as gt, presentReport as h, writeLorebookEntry as ht, contextOf as i, DICE_READINGS as in, createLorebook as it, writeSnapshot as j, populateVariables as jt, recordProvenance as k, populateRoutines as kt, isReviewing as l, listLorebooks as lt, checkImported as m, updateLorebookInfo as mt, limitAuthoringAgents as n, readCard as nn, parseWorldInfoEdit as nt, importTools as o, authoredWorldOf as on, deleteLorebookEntry as ot, structureImport as p, reserveLorebook as pt, writePortrait as q, loadCalendar as qt, MAX_PREVIEW_TEXT as r, DICE_COMPARISONS as rn, addLorebookEntries as rt, worldOf as s, authoringChatSessionId as sn, entriesOf as st, AuthoringTools as t, pictureOf as tn, parseWorldFile as tt, keepDraft as u, loadLorebook as ut, addressChanges as v, withScratchStory as vt, editWorldInfo as w, populateFactions as wt, matchPieces as x, populateCharacters as xt, cardChanges as y, knowsCharacter as yt, addressKey as z, ITEMS_DIR as zt };
6291
6475
 
6292
- //# sourceMappingURL=authoring-tools-B4mEdKp_.js.map
6476
+ //# sourceMappingURL=authoring-tools-C6Hzy8jU.js.map