@dsh-agentic-roleplay/core 0.10.0 → 0.10.2

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 (37) hide show
  1. package/README.md +1 -1
  2. package/icon.svg +13 -0
  3. package/lib/{authoring-tools-B4mEdKp_.js → authoring-tools-C9cmcUbD.js} +1820 -1243
  4. package/lib/authoring.js +18 -7
  5. package/lib/{child-Dtv76jNx.js → child-C9nPIy9j.js} +40 -5
  6. package/lib/client.js +4668 -2249
  7. package/lib/client.js.map +1 -1
  8. package/lib/compaction.js +2 -2
  9. package/lib/doctor.js +3 -3
  10. package/lib/{gallery-BZJleAeH.js → gallery-CtG-bDzz.js} +2 -2
  11. package/lib/{generator-Cg6CQc5S.js → generator-nLoPM5MF.js} +2 -2
  12. package/lib/helper.js +8 -1
  13. package/lib/{hooks-DTjHWTfx.js → hooks-BpLN_hhT.js} +282 -211
  14. package/lib/images.js +4 -4
  15. package/lib/index.js +1399 -146
  16. package/lib/{library-GIlrkOim.js → library-CH-ahlm0.js} +2 -2
  17. package/lib/{lorebook-author-tools-BCFFJSle.js → lorebook-author-tools-Cg7QQemi.js} +6 -6
  18. package/lib/lorebook-authoring.js +4 -4
  19. package/lib/{narration-BVzew4-K.js → narration-B0hConSw.js} +5 -2
  20. package/lib/orchestration.js +108 -21
  21. package/lib/{pack-jail-hsNQNTOf.js → pack-jail-D5j5mIq6.js} +16 -12
  22. package/lib/{packs-UpK_cvI4.js → packs-Cc05FrId.js} +2 -2
  23. package/lib/{presenter-agent-DZOgBOR9.js → presenter-agent-UKbKYXMO.js} +6 -6
  24. package/lib/{preset-Cyoj1ZWa.js → preset-Be4lz6vB.js} +4 -4
  25. package/lib/{preset-author-tools-RSta6pHK.js → preset-author-tools-C3s0iSfv.js} +6 -6
  26. package/lib/preset-authoring.js +5 -5
  27. package/lib/{presets-CvpsW_10.js → presets-Cl9sU3Qs.js} +152 -14
  28. package/lib/{reflection-0qTvw2It.js → reflection-I9z-PvFK.js} +135 -93
  29. package/lib/{text-CbGQhK1-.js → text-CzqhC8sT.js} +11 -1
  30. package/lib/{turn-guard-BzNeGNnc.js → turn-guard-B9pGCeir.js} +3 -3
  31. package/package.json +4 -2
  32. package/prompts/en/checks/decide-opening.md +7 -1
  33. package/prompts/en/checks/decide-stated.md +1 -0
  34. package/prompts/en/roleplay/record-keeper.md +2 -1
  35. package/prompts/zh/checks/decide-opening.md +7 -1
  36. package/prompts/zh/checks/decide-stated.md +1 -0
  37. package/prompts/zh/roleplay/record-keeper.md +2 -1
@@ -1,11 +1,12 @@
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-Cl9sU3Qs.js";
3
+ import { f as parseSwitchboard } from "./narration-B0hConSw.js";
4
+ import { $ as listFiles, G as formatIssue, J as PROMPTS_FOLDER, K as errorMessage, L as loadPlots, U as readDocument$1, V as commandFlagKey, W as Reader, X as copyWorldPrompts, a as externalWithheld, et as promptProblem, gt as COMPARISONS, ot as InvalidWorldError, r as externalGiven, rt as shippedText, t as externalToolLimit, tt as shippedLanguages, z as plotReferenceProblems } from "./hooks-BpLN_hhT.js";
5
+ import { t as trimCharacters } from "./text-CzqhC8sT.js";
6
+ import { S as currentTurn, c as namedModel, d as parseSampling, l as runChild, t as ChildTimeoutError } from "./child-C9nPIy9j.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
+ import { MINUTE, MONTH_NAMES, SEASONS, checkProblems, createSessionStore, dayTokenProblems, isTimeZone, lintWorld, parseInstant, parseTimeOfDay, readTriggerDefinition, triggerReferenceProblems, writeCalendar } from "@dsh-agentic-roleplay/state";
9
10
  import { existsSync, readFileSync } from "node:fs";
10
11
  import { createHash } from "node:crypto";
11
12
  import { mkdir, mkdtemp, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
@@ -13,7 +14,6 @@ import { createUserMessage } from "@deepseek-ai/dsh-llm";
13
14
  import { inflateSync } from "node:zlib";
14
15
  import { defineTool } from "@deepseek-ai/dsh-tools";
15
16
  import { Service } from "@deepseek-ai/cordis";
16
- import { MINUTE, MONTH_NAMES, SEASONS, checkProblems, createSessionStore, dayTokenProblems, isTimeZone, lintWorld, parseInstant, parseTimeOfDay, readTriggerDefinition, triggerReferenceProblems, writeCalendar } from "@dsh-agentic-roleplay/state";
17
17
  import { lookup } from "node:dns";
18
18
  import { request } from "node:http";
19
19
  import { request as request$1 } from "node:https";
@@ -76,6 +76,7 @@ const TEXT_CHUNKS = /* @__PURE__ */ new Set([
76
76
  const CARD_KEYWORDS = ["ccv3", "chara"];
77
77
  function chunksOf(file) {
78
78
  const chunks = [];
79
+ if (file.length < PNG_SIGNATURE.length + 12 || file.toString("latin1", PNG_SIGNATURE.length + 4, PNG_SIGNATURE.length + 8) !== "IHDR") throw new ImportCardError("the PNG file is damaged: it does not start with a picture header");
79
80
  let at = PNG_SIGNATURE.length;
80
81
  while (at + 12 <= file.length) {
81
82
  const length = file.readUInt32BE(at);
@@ -118,8 +119,40 @@ function textOf$1(chunk) {
118
119
  function pictureOf(file) {
119
120
  return Buffer.concat([PNG_SIGNATURE, ...chunksOf(file).filter((chunk) => !TEXT_CHUNKS.has(chunk.type)).map((chunk) => chunk.raw)]);
120
121
  }
122
+ const BYTE_ORDER_MARK = [
123
+ 239,
124
+ 187,
125
+ 191
126
+ ];
127
+ const BLANKS = /* @__PURE__ */ new Set([
128
+ 32,
129
+ 9,
130
+ 10,
131
+ 13
132
+ ]);
133
+ const startOfText = (file) => {
134
+ let at = BYTE_ORDER_MARK.every((byte, index) => file[index] === byte) ? BYTE_ORDER_MARK.length : 0;
135
+ while (at < file.length && BLANKS.has(file[at])) at += 1;
136
+ return at;
137
+ };
138
+ /** What a file is by its first bytes: never by its name or by the type a sender claimed. A card is a PNG picture or a JSON object. */
139
+ function cardFormat(file) {
140
+ if (isPng(file)) return "png";
141
+ return file[startOfText(file)] === 123 ? "json" : void 0;
142
+ }
143
+ /** A few kinds of file people mistake for a card, named by their first bytes, so the refusal can say what arrived. */
144
+ function kindOf(file) {
145
+ const start = file.toString("latin1", startOfText(file), startOfText(file) + 15).toLowerCase();
146
+ if (start.startsWith("<!doctype") || start.startsWith("<html") || start.startsWith("<?xml") || start.startsWith("<")) return "a web page";
147
+ if (file.subarray(0, 2).toString("latin1") === "PK") return "a ZIP archive";
148
+ if (file[0] === 255 && file[1] === 216 && file[2] === 255) return "a JPEG picture";
149
+ if (file.toString("latin1", 0, 4) === "GIF8") return "a GIF picture";
150
+ if (file.toString("latin1", 0, 4) === "RIFF" && file.toString("latin1", 8, 12) === "WEBP") return "a WebP picture";
151
+ }
121
152
  function jsonOf(file, name) {
122
- if (file.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE)) {
153
+ const format = cardFormat(file);
154
+ if (format === void 0) throw new ImportCardError(`${name} is ${file.length === 0 ? "empty" : `${kindOf(file) ?? "neither a PNG picture nor a JSON file"}`}: a character card is a PNG picture with the card in it, or a JSON file`);
155
+ if (format === "png") {
123
156
  const found = /* @__PURE__ */ new Map();
124
157
  for (const chunk of chunksOf(file)) {
125
158
  if (!TEXT_CHUNKS.has(chunk.type)) continue;
@@ -137,7 +170,7 @@ function jsonOf(file, name) {
137
170
  try {
138
171
  return JSON.parse(file.toString("utf8").replace(/^/, ""));
139
172
  } catch {
140
- throw new ImportCardError(`${name} is neither a PNG picture with a card in it nor a JSON card`);
173
+ throw new ImportCardError(`${name} looks like JSON but cannot be read as JSON`);
141
174
  }
142
175
  }
143
176
  const URL_PATTERN = /https?:\/\/[^\s"'`<>()[\]{}\\|^]+/gi;
@@ -191,7 +224,9 @@ function readCard(file, name) {
191
224
  const book = object(data["character_book"]);
192
225
  const entries = Array.isArray(book?.["entries"]) ? book["entries"].flatMap((entry, index) => entryOf(entry, index) ?? []) : [];
193
226
  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);
227
+ const listed = (list) => Array.isArray(list) ? list.length : 0;
228
+ const regexScripts = listed(extensions["regex_scripts"]);
229
+ const helperScripts = listed(object(extensions["tavern_helper"])?.["scripts"]);
195
230
  return {
196
231
  sourceSpec: spec,
197
232
  name: cardName,
@@ -213,7 +248,9 @@ function readCard(file, name) {
213
248
  entries
214
249
  } },
215
250
  ignored: {
216
- scripts,
251
+ scripts: regexScripts + helperScripts,
252
+ regexScripts,
253
+ helperScripts,
217
254
  extensions: Object.keys(extensions).filter((key) => ![
218
255
  "talkativeness",
219
256
  "fav",
@@ -1965,6 +2002,126 @@ async function deleteLorebook(root, name, now = Date.now) {
1965
2002
  await rename(directory, aside);
1966
2003
  return aside;
1967
2004
  }
2005
+ // src/shared/start-questions.ts
2006
+ const START_PLOT_STATUSES = ["dormant", "available"];
2007
+ const questionView = (question) => ({
2008
+ id: question.id,
2009
+ text: question.text,
2010
+ options: question.options.map(({ id, label, description }) => ({
2011
+ id,
2012
+ label,
2013
+ ...description === void 0 ? {} : { description }
2014
+ }))
2015
+ });
2016
+ /** What the answers add up to, or why they cannot be combined. A question left unanswered changes nothing. */
2017
+ function combineAnswers(questions, answers) {
2018
+ const problems = [];
2019
+ const effect = {};
2020
+ for (const id of Object.keys(answers)) if (!questions.some((question) => question.id === id)) problems.push(`the world asks no question "${id}"`);
2021
+ const set = (key, value, question) => {
2022
+ if (value === void 0) return;
2023
+ if (effect[key] !== void 0 && effect[key] !== value) problems.push(`the answers disagree about the ${key === "startingTime" ? "starting time" : key === "clockMode" ? "clock" : key}; "${question}" changes it again`);
2024
+ else effect[key] = value;
2025
+ };
2026
+ for (const question of questions) {
2027
+ const answer = answers[question.id];
2028
+ if (answer === void 0) continue;
2029
+ const option = question.options.find((entry) => entry.id === answer);
2030
+ if (option === void 0) {
2031
+ problems.push(`"${answer}" is not an answer to "${question.id}"`);
2032
+ continue;
2033
+ }
2034
+ const found = option.effect;
2035
+ if (found.rules !== void 0) effect.rules = [...effect.rules ?? [], ...found.rules];
2036
+ set("opening", found.opening, question.id);
2037
+ set("startingTime", found.startingTime, question.id);
2038
+ set("clockMode", found.clockMode, question.id);
2039
+ for (const [agent, switches] of Object.entries(found.tools ?? {})) {
2040
+ const merged = { ...effect.tools?.[agent] };
2041
+ for (const [tool, value] of Object.entries(switches)) {
2042
+ if (merged[tool] !== void 0 && merged[tool] !== value) problems.push(`the answers disagree about the tool "${tool}"; "${question.id}" changes it again`);
2043
+ merged[tool] = value;
2044
+ }
2045
+ effect.tools = {
2046
+ ...effect.tools,
2047
+ [agent]: merged
2048
+ };
2049
+ }
2050
+ for (const [plot, status] of Object.entries(found.plots ?? {})) {
2051
+ if (effect.plots?.[plot] !== void 0 && effect.plots[plot] !== status) problems.push(`the answers disagree about the plot "${plot}"; "${question.id}" changes it again`);
2052
+ effect.plots = {
2053
+ ...effect.plots,
2054
+ [plot]: status
2055
+ };
2056
+ }
2057
+ }
2058
+ return {
2059
+ effect,
2060
+ problems
2061
+ };
2062
+ }
2063
+ /** The questions as `world.json` writes them: snake_case keys, and only what an answer sets. */
2064
+ function startQuestionsToFile(questions) {
2065
+ return questions.map((question) => ({
2066
+ id: question.id,
2067
+ text: question.text,
2068
+ options: question.options.map((option) => ({
2069
+ id: option.id,
2070
+ label: option.label,
2071
+ ...option.description === void 0 || option.description === "" ? {} : { description: option.description },
2072
+ effect: {
2073
+ ...option.effect.rules === void 0 || option.effect.rules.length === 0 ? {} : { rules: option.effect.rules },
2074
+ ...option.effect.opening === void 0 ? {} : { opening: option.effect.opening },
2075
+ ...option.effect.startingTime === void 0 || option.effect.startingTime === "" ? {} : { starting_time: option.effect.startingTime },
2076
+ ...option.effect.clockMode === void 0 ? {} : { clock_mode: option.effect.clockMode },
2077
+ ...option.effect.tools === void 0 ? {} : { tools: option.effect.tools },
2078
+ ...option.effect.plots === void 0 ? {} : { plots: option.effect.plots }
2079
+ }
2080
+ }))
2081
+ }));
2082
+ }
2083
+ const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
2084
+ /** Reads questions sent by the editor: the shape only, since the world file's own reader judges every value when the change is written. */
2085
+ function parseStartQuestionsEdit(value, problems) {
2086
+ if (!Array.isArray(value)) {
2087
+ problems.push("startQuestions must be a list");
2088
+ return;
2089
+ }
2090
+ const questions = [];
2091
+ for (const entry of value) {
2092
+ if (!isObject(entry) || typeof entry["id"] !== "string" || typeof entry["text"] !== "string" || !Array.isArray(entry["options"])) {
2093
+ problems.push("a start question needs an id, a text and a list of options");
2094
+ return;
2095
+ }
2096
+ const options = [];
2097
+ for (const option of entry["options"]) {
2098
+ if (!isObject(option) || typeof option["id"] !== "string" || typeof option["label"] !== "string" || !isObject(option["effect"]) || option["description"] !== void 0 && typeof option["description"] !== "string") {
2099
+ problems.push("an answer needs an id, a label and an effect");
2100
+ return;
2101
+ }
2102
+ const effect = option["effect"];
2103
+ const known = {};
2104
+ if (Array.isArray(effect["rules"])) known.rules = effect["rules"].filter((rule) => typeof rule === "string");
2105
+ if (typeof effect["opening"] === "number") known.opening = effect["opening"];
2106
+ if (typeof effect["startingTime"] === "string") known.startingTime = effect["startingTime"];
2107
+ if (effect["clockMode"] === "simulated" || effect["clockMode"] === "real") known.clockMode = effect["clockMode"];
2108
+ if (isObject(effect["tools"])) known.tools = effect["tools"];
2109
+ if (isObject(effect["plots"])) known.plots = effect["plots"];
2110
+ options.push({
2111
+ id: option["id"].trim(),
2112
+ label: option["label"].trim(),
2113
+ ...typeof option["description"] === "string" && option["description"].trim() !== "" ? { description: option["description"].trim() } : {},
2114
+ effect: known
2115
+ });
2116
+ }
2117
+ questions.push({
2118
+ id: entry["id"].trim(),
2119
+ text: entry["text"].trim(),
2120
+ options
2121
+ });
2122
+ }
2123
+ return questions;
2124
+ }
1968
2125
  const MAX_OPENING_CHARS = 2e4;
1969
2126
  const TEXT_FIELDS = [
1970
2127
  "name",
@@ -1988,7 +2145,7 @@ function parseWorldInfoEdit(value) {
1988
2145
  const input = typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
1989
2146
  if (input === void 0) throw new InvalidEditProblems(["the changes must be an object"]);
1990
2147
  const edit = {};
1991
- for (const key of Object.keys(input)) if (!TEXT_FIELDS.includes(key) && !LIST_FIELDS.includes(key) && key !== "alternateOpenings" && key !== "tools" && key !== "sampling") problems.push(`there is no field "${key}"`);
2148
+ for (const key of Object.keys(input)) if (!TEXT_FIELDS.includes(key) && !LIST_FIELDS.includes(key) && key !== "alternateOpenings" && key !== "startQuestions" && key !== "tools" && key !== "sampling") problems.push(`there is no field "${key}"`);
1992
2149
  for (const key of TEXT_FIELDS) {
1993
2150
  if (input[key] === void 0) continue;
1994
2151
  if (typeof input[key] === "string") edit[key] = input[key].trim();
@@ -2014,6 +2171,10 @@ function parseWorldInfoEdit(value) {
2014
2171
  edit["alternateOpenings"] = kept;
2015
2172
  }
2016
2173
  }
2174
+ if (input["startQuestions"] !== void 0) {
2175
+ const questions = parseStartQuestionsEdit(input["startQuestions"], problems);
2176
+ if (questions !== void 0) edit["startQuestions"] = questions;
2177
+ }
2017
2178
  if (input["tools"] !== void 0) {
2018
2179
  const board = parseSwitchboard(input["tools"], "tools");
2019
2180
  if (typeof board === "string") problems.push(board);
@@ -2051,6 +2212,108 @@ function readAlternateOpenings(reader) {
2051
2212
  }
2052
2213
  return openings.length === 0 ? void 0 : openings;
2053
2214
  }
2215
+ const EFFECT_KEYS = [
2216
+ "rules",
2217
+ "opening",
2218
+ "starting_time",
2219
+ "clock_mode",
2220
+ "tools",
2221
+ "plots"
2222
+ ];
2223
+ /** One answer's `effect`: only the allow-listed keys, each checked; an unknown key is refused so an answer can never reach a setting it was not meant to. */
2224
+ function readEffect(entry, openings) {
2225
+ const raw = entry.raw("effect");
2226
+ const effect = {};
2227
+ if (raw === void 0 || raw === null) return effect;
2228
+ if (typeof raw !== "object" || Array.isArray(raw)) {
2229
+ entry.fail("effect", "must be an object");
2230
+ return effect;
2231
+ }
2232
+ const body = raw;
2233
+ for (const key of Object.keys(body)) if (!EFFECT_KEYS.includes(key)) entry.fail(`effect.${key}`, `is not something an answer can change (${EFFECT_KEYS.join(", ")})`);
2234
+ if (body["rules"] !== void 0) {
2235
+ const rules = Array.isArray(body["rules"]) && body["rules"].every((rule) => typeof rule === "string") ? body["rules"].map((rule) => rule.trim()).filter((rule) => rule !== "") : void 0;
2236
+ if (rules === void 0) entry.fail("effect.rules", "must be a list of rules");
2237
+ else if (rules.some((rule) => rule.length > 300)) entry.fail("effect.rules", `has a rule longer than 300 characters`);
2238
+ else if (rules.length > 0) effect.rules = rules;
2239
+ }
2240
+ if (body["opening"] !== void 0) {
2241
+ if (typeof body["opening"] !== "number" || !Number.isInteger(body["opening"]) || body["opening"] < 0 || body["opening"] >= openings) entry.fail("effect.opening", `must be the position of one of the ${openings} alternate openings`);
2242
+ else effect.opening = body["opening"];
2243
+ }
2244
+ if (body["starting_time"] !== void 0) {
2245
+ if (typeof body["starting_time"] !== "string" || body["starting_time"].trim() === "") entry.fail("effect.starting_time", "must be a local time like 1912-04-14T23:40:00");
2246
+ else effect.startingTime = body["starting_time"].trim();
2247
+ }
2248
+ if (body["clock_mode"] !== void 0) {
2249
+ if (body["clock_mode"] !== "simulated" && body["clock_mode"] !== "real") entry.fail("effect.clock_mode", "must be \"simulated\" or \"real\"");
2250
+ else effect.clockMode = body["clock_mode"];
2251
+ }
2252
+ if (body["tools"] !== void 0) {
2253
+ const board = parseSwitchboard(body["tools"], "tools");
2254
+ if (typeof board === "string") entry.fail("effect.tools", board.replace(/^"tools"\s*/, ""));
2255
+ else if (Object.keys(board).length > 0) effect.tools = board;
2256
+ }
2257
+ if (body["plots"] !== void 0) {
2258
+ const plots = body["plots"];
2259
+ if (typeof plots !== "object" || plots === null || Array.isArray(plots)) entry.fail("effect.plots", "must map plot ids to \"dormant\" or \"available\"");
2260
+ else {
2261
+ const kept = {};
2262
+ for (const [id, status] of Object.entries(plots)) if (status !== "dormant" && status !== "available") entry.fail(`effect.plots.${id}`, `must be one of ${START_PLOT_STATUSES.join(", ")}`);
2263
+ else kept[id] = status;
2264
+ if (Object.keys(kept).length > 0) effect.plots = kept;
2265
+ }
2266
+ }
2267
+ return effect;
2268
+ }
2269
+ /** `start_questions`: `{ id, text, options: [{ id, label, description?, effect }] }`, asked in the start dialog. */
2270
+ function readStartQuestions(reader, openings) {
2271
+ const value = reader.raw("start_questions");
2272
+ if (value === void 0 || value === null) return void 0;
2273
+ if (!Array.isArray(value)) {
2274
+ reader.fail("start_questions", "must be a list");
2275
+ return;
2276
+ }
2277
+ if (value.length > 8) reader.fail("start_questions", `has more than 8 questions`);
2278
+ const questions = [];
2279
+ const seen = /* @__PURE__ */ new Set();
2280
+ for (const entry of reader.objects("start_questions")) {
2281
+ const id = entry.string("id", { required: true })?.trim();
2282
+ const text = entry.string("text", { required: true })?.trim();
2283
+ if (id === void 0 || text === void 0 || id === "" || text === "") continue;
2284
+ if (seen.has(id)) entry.fail("id", `repeats the question "${id}"`);
2285
+ seen.add(id);
2286
+ if (text.length > 200) entry.fail("text", `is longer than 200 characters`);
2287
+ const raw = entry.raw("options");
2288
+ if (!Array.isArray(raw) || raw.length < 2 || raw.length > 8) {
2289
+ entry.fail("options", `must list 2 to 8 answers`);
2290
+ continue;
2291
+ }
2292
+ const options = [];
2293
+ const ids = /* @__PURE__ */ new Set();
2294
+ for (const option of entry.objects("options")) {
2295
+ const optionId = option.string("id", { required: true })?.trim();
2296
+ const label = option.string("label", { required: true })?.trim();
2297
+ if (optionId === void 0 || label === void 0 || optionId === "" || label === "") continue;
2298
+ if (ids.has(optionId)) option.fail("id", `repeats the answer "${optionId}"`);
2299
+ ids.add(optionId);
2300
+ if (label.length > 200) option.fail("label", `is longer than 200 characters`);
2301
+ const description = option.string("description")?.trim();
2302
+ options.push({
2303
+ id: optionId,
2304
+ label,
2305
+ ...description === void 0 || description === "" ? {} : { description },
2306
+ effect: readEffect(option, openings)
2307
+ });
2308
+ }
2309
+ questions.push({
2310
+ id,
2311
+ text,
2312
+ options
2313
+ });
2314
+ }
2315
+ return questions.length === 0 ? void 0 : questions;
2316
+ }
2054
2317
  /** `tools`: switches by kind of agent and tool name; a world carried from a version with other tools still loads. */
2055
2318
  function readTools(reader) {
2056
2319
  const value = reader.raw("tools");
@@ -2164,6 +2427,7 @@ function parseWorldFile(text) {
2164
2427
  const rules = readRules(reader);
2165
2428
  const tools = readTools(reader);
2166
2429
  const alternateOpenings = readAlternateOpenings(reader);
2430
+ const startQuestions = readStartQuestions(reader, alternateOpenings?.length ?? 0);
2167
2431
  const narrationVoice = reader.string("narration_voice")?.trim() || void 0;
2168
2432
  const sampling = readSampling(reader);
2169
2433
  const preset = reader.string("preset")?.trim() || void 0;
@@ -2190,6 +2454,7 @@ function parseWorldFile(text) {
2190
2454
  ...rules === void 0 ? {} : { rules },
2191
2455
  ...tools === void 0 ? {} : { tools },
2192
2456
  ...alternateOpenings === void 0 ? {} : { alternateOpenings },
2457
+ ...startQuestions === void 0 ? {} : { startQuestions },
2193
2458
  ...narrationVoice === void 0 ? {} : { narrationVoice },
2194
2459
  ...sampling === void 0 ? {} : { sampling },
2195
2460
  ...preset === void 0 ? {} : { preset },
@@ -2197,1318 +2462,1518 @@ function parseWorldFile(text) {
2197
2462
  }
2198
2463
  };
2199
2464
  }
2200
- // src/host/worlds/world-file-io.ts
2201
- const queues = /* @__PURE__ */ new Map();
2465
+ // src/host/import/outcomes.ts
2202
2466
  /**
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.
2467
+ * 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`
2468
+ * 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
2469
  */
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
- }
2470
+ const OUTCOMES = [
2471
+ "carried",
2472
+ "converted",
2473
+ "guidance",
2474
+ "read",
2475
+ "omitted",
2476
+ "awaiting"
2254
2477
  ];
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);
2478
+ const rolesOf$1 = (source, id) => [...new Set(Object.entries(source.records.classified ?? {}).filter(([key]) => baseId(key) === id).flatMap(([, entry]) => entry.roles))];
2479
+ const MECHANICS = [
2480
+ "variable",
2481
+ "rules",
2482
+ "command",
2483
+ "trigger"
2484
+ ];
2485
+ function outcomeOf(source, piece, made, mechanics, finished) {
2486
+ const { choices, card } = source;
2487
+ const reported = source.records.report !== void 0 || finished;
2488
+ if (piece.id === "field:creator-notes") return {
2489
+ outcome: "omitted",
2490
+ because: "notes to people, not part of the world"
2491
+ };
2492
+ if (piece.id === "field:first-message" || piece.id.startsWith("field:greeting-")) {
2493
+ if (choices.opening.kind === "none") return {
2494
+ outcome: "omitted",
2495
+ because: "the person chose no opening"
2496
+ };
2497
+ return reported ? {
2498
+ outcome: "carried",
2499
+ because: "an opening of the world"
2500
+ } : { outcome: "awaiting" };
2501
+ }
2502
+ if (piece.id === "field:system-prompt" || piece.id === "field:post-history") return reported ? {
2503
+ outcome: "guidance",
2504
+ because: "instructions to the narrator"
2505
+ } : { outcome: "awaiting" };
2506
+ if (piece.enabled !== void 0 && choices.disabledEntries.includes(piece.id)) return {
2507
+ outcome: "omitted",
2508
+ because: "left off by the person"
2509
+ };
2510
+ if (piece.enabled === false && !(card.book?.entries.find((entry) => entry.id === piece.id)?.enabled ?? true) && !piece.used) return {
2511
+ outcome: "omitted",
2512
+ because: "switched off in the card"
2513
+ };
2514
+ if (source.records.classified === void 0) return { outcome: "awaiting" };
2515
+ const roles = rolesOf$1(source, piece.id);
2516
+ if (roles.length === 0) return {
2517
+ outcome: "awaiting",
2518
+ because: "the import did not classify it"
2519
+ };
2520
+ if (roles.every((role) => ROLE_TABLE[role].leftOut)) {
2521
+ if (mechanics) return {
2522
+ outcome: "converted",
2523
+ because: "its rules became tracked values, rules or triggers"
2524
+ };
2525
+ return roles.includes("instruction") ? {
2526
+ outcome: "guidance",
2527
+ because: "how to run a story, not facts of this one"
2528
+ } : {
2529
+ outcome: "omitted",
2530
+ because: roles.includes("template") ? "a template or format of the source tool" : "nothing to do with the story"
2531
+ };
2532
+ }
2533
+ if (made.length > 0 || mechanics) return {
2534
+ outcome: "converted",
2535
+ ...made.length === 0 ? { because: "its rules became tracked values, rules or triggers" } : {}
2536
+ };
2537
+ return {
2538
+ outcome: "read",
2539
+ because: "no character, place, thing or rule is made from it: its words survive only as background"
2540
+ };
2282
2541
  }
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
- }]
2542
+ /** What became of every piece of the card, from the import's records; `made` is `entitiesByPiece` of the same source. */
2543
+ function pieceOutcomes(source, made, options = {}) {
2544
+ const derived = options.derived ?? source.records.report?.derived ?? [];
2545
+ return source.pieces.map((piece) => {
2546
+ const from = derived.filter((entry) => entry.pieces.includes(piece.id));
2547
+ const labels = from.map((entry) => `${entry.kind}: ${entry.name}`);
2548
+ return {
2549
+ piece: piece.id,
2550
+ label: piece.label,
2551
+ ...outcomeOf(source, piece, made.get(piece.id) ?? [], from.some((entry) => MECHANICS.includes(entry.kind)), options.finished === true),
2552
+ madeInto: [...made.get(piece.id) ?? [], ...labels]
2553
+ };
2307
2554
  });
2308
- return true;
2309
2555
  }
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);
2556
+ const outcomeCounts = (outcomes) => Object.fromEntries(OUTCOMES.map((outcome) => [outcome, outcomes.filter((entry) => entry.outcome === outcome).length]));
2557
+ // src/host/import/card-tools.ts
2558
+ /** The most of a piece one `card_read` returns; a long lorebook entry is read in turns. */
2559
+ const READ_CHARS = 6e3;
2560
+ const SNIPPET_CHARS = 240;
2561
+ const DEFAULT_HITS = 8;
2562
+ const MAX_HITS = 20;
2563
+ const baseId = (id) => id.replace(/#\d+$/, "");
2564
+ /** The folder inside `.import` where a newer version of the card waits to be applied to the world. */
2565
+ const UPDATE_FOLDER = "update";
2566
+ /** Everything the card holds, in the order the card has it. */
2567
+ function cardPieces(card, choices) {
2568
+ const read = new Set(itemise(card, choices).map((item) => baseId(item.id)));
2569
+ const field = (id, label, text) => text.trim() === "" ? [] : [{
2570
+ id,
2571
+ label,
2572
+ title: "",
2573
+ keys: [],
2574
+ text,
2575
+ used: read.has(id)
2576
+ }];
2577
+ return [
2578
+ ...field("field:description", "description", card.description),
2579
+ ...field("field:personality", "personality", card.personality),
2580
+ ...field("field:scenario", "scenario", card.scenario),
2581
+ ...field("field:example", "example messages", card.messageExample),
2582
+ ...field("field:first-message", "first message", card.firstMessage),
2583
+ ...card.alternateGreetings.flatMap((text, index) => field(`field:greeting-${index + 1}`, `alternate greeting ${index + 1}`, text)),
2584
+ ...field("field:creator-notes", "creator's notes", card.creatorNotes),
2585
+ ...field("field:system-prompt", "system prompt", card.systemPrompt),
2586
+ ...field("field:post-history", "post-history instructions", card.postHistoryInstructions),
2587
+ ...(card.book?.entries ?? []).filter((entry) => entry.content.trim() !== "").map((entry) => ({
2588
+ id: entry.id,
2589
+ label: entry.title === "" ? entry.id : `entry “${entry.title}”`,
2590
+ title: entry.title,
2591
+ keys: entry.keys,
2592
+ text: entry.content,
2593
+ used: read.has(entry.id),
2594
+ enabled: entry.enabled
2595
+ }))
2596
+ ];
2314
2597
  }
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
- }
2598
+ async function optional(file) {
2599
+ try {
2600
+ return JSON.parse(await readFile(file, "utf8"));
2601
+ } catch (error) {
2602
+ if (error.code === "ENOENT") return void 0;
2603
+ throw error;
2328
2604
  }
2329
2605
  }
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;
2357
- }
2358
- }
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
- }
2606
+ /** 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. */
2607
+ async function readSource(directory, withRecords, version = "current") {
2608
+ const folder = join(directory, IMPORT_FOLDER);
2609
+ let read;
2610
+ try {
2611
+ read = await readImportFolder(version === "new" ? join(folder, UPDATE_FOLDER) : folder);
2612
+ } catch (error) {
2613
+ 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");
2614
+ throw error;
2373
2615
  }
2374
- return notes;
2375
- }
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("; ");
2616
+ const records = withRecords && version === "current" ? {
2617
+ classified: await optional(join(folder, "classification.json")),
2618
+ roster: await optional(join(folder, "roster.json")),
2619
+ report: await optional(join(folder, "report.json"))
2620
+ } : {};
2403
2621
  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";
2421
- }
2622
+ card: read.card,
2623
+ choices: read.choices,
2624
+ pieces: cardPieces(read.card, read.choices),
2625
+ records
2422
2626
  };
2423
2627
  }
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));
2628
+ const rolesOf = (classified, id) => {
2629
+ if (classified === void 0) return [];
2630
+ return [...new Set(Object.entries(classified).filter(([key]) => baseId(key) === id).flatMap(([, entry]) => entry.roles))];
2631
+ };
2632
+ function snippet$1(text, at, length) {
2633
+ const start = Math.max(0, at - Math.floor((SNIPPET_CHARS - length) / 2));
2634
+ const end = Math.min(text.length, start + SNIPPET_CHARS);
2635
+ return `${start > 0 ? "…" : ""}${text.slice(start, end).replace(/\s+/g, " ")}${end < text.length ? "…" : ""}`;
2636
+ }
2637
+ function searchPieces(pieces, query, limit) {
2638
+ const phrase = query.trim().toLowerCase();
2639
+ if (phrase === "") return [];
2640
+ const words = [...new Set(phrase.split(/[\s,;,、]+/).filter((word) => word.length >= (/^[\x00-\x7f]+$/.test(word) ? 3 : 2)))];
2641
+ const count = (text, needle) => {
2642
+ let n = 0;
2643
+ let first = -1;
2644
+ for (let at = text.indexOf(needle); at >= 0; at = text.indexOf(needle, at + needle.length)) {
2645
+ n += 1;
2646
+ if (first < 0) first = at;
2647
+ }
2433
2648
  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() }
2649
+ n,
2650
+ first
2437
2651
  };
2438
- } catch (error) {
2652
+ };
2653
+ const look = (text) => {
2654
+ const lowered = text.toLowerCase();
2655
+ const whole = count(lowered, phrase);
2656
+ if (whole.n > 0) return {
2657
+ matches: whole.n,
2658
+ score: whole.n * 4 + 100,
2659
+ at: whole.first,
2660
+ length: phrase.length
2661
+ };
2662
+ const found = words.map((word) => ({
2663
+ word,
2664
+ ...count(lowered, word)
2665
+ })).filter((entry) => entry.n > 0);
2666
+ if (found.length === 0) return void 0;
2667
+ const best = found.reduce((a, b) => a.first <= b.first ? a : b, found[0]);
2439
2668
  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)}`
2669
+ matches: found.reduce((sum, entry) => sum + entry.n, 0),
2670
+ score: found.length * 10 + found.reduce((sum, entry) => sum + entry.n, 0),
2671
+ at: best.first,
2672
+ length: best.word.length
2443
2673
  };
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;
2674
+ };
2675
+ return pieces.flatMap((piece) => {
2676
+ const inText = look(piece.text);
2677
+ if (inText !== void 0) return [{
2678
+ piece,
2679
+ in: "text",
2680
+ matches: inText.matches,
2681
+ score: inText.score,
2682
+ snippet: snippet$1(piece.text, inText.at, inText.length)
2683
+ }];
2684
+ const head = [piece.title, ...piece.keys].filter((part) => part !== "").join(" · ");
2685
+ const inHead = look(head);
2686
+ return inHead === void 0 ? [] : [{
2687
+ piece,
2688
+ in: "title or keys",
2689
+ matches: inHead.matches,
2690
+ score: inHead.score - 50,
2691
+ snippet: `${piece.title === "" ? "" : `title: ${piece.title}`}${piece.keys.length === 0 ? "" : `${piece.title === "" ? "" : "; "}keys: ${piece.keys.join(", ")}`}`
2692
+ }];
2693
+ }).sort((a, b) => b.score - a.score).slice(0, limit).map(({ piece, matches, in: where, snippet: text }) => ({
2694
+ piece,
2695
+ matches,
2696
+ in: where,
2697
+ snippet: text
2698
+ }));
2453
2699
  }
2454
- const writeAtomically = (file, text) => atomicWrite(file, text, { createDirectory: true });
2700
+ const versionParameter = {
2701
+ type: "string",
2702
+ enum: ["current", "new"],
2703
+ 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."
2704
+ };
2705
+ const output$1 = {
2706
+ schema: { type: "json" },
2707
+ render: (_args, value) => [{
2708
+ type: "text",
2709
+ text: JSON.stringify(value, null, 1)
2710
+ }]
2711
+ };
2712
+ const json$1 = (value) => JSON.parse(JSON.stringify(value));
2713
+ const idOf = (exec) => {
2714
+ const chat = exec.agent?.session.id;
2715
+ const world = chat === void 0 ? void 0 : authoredWorldOf(chat);
2716
+ if (world === void 0) throw new Error("this tool is available only in a world authoring chat");
2717
+ return world;
2718
+ };
2455
2719
  /**
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.
2720
+ * 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.
2721
+ * `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
2722
  */
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"]
2723
+ function cardTools(worlds, options = {}) {
2724
+ const withRecords = options.records !== false;
2725
+ const source = async (exec, version = "current") => {
2726
+ const id = idOf(exec);
2727
+ const record = await worlds.find(id);
2728
+ if (record === void 0) throw new Error(`the authored world "${id}" no longer exists`);
2729
+ return await readSource(record.directory, withRecords, version);
2464
2730
  };
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);
2498
- };
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
2731
+ const versionOf = (value) => {
2732
+ if (value === void 0 || value === "current") return "current";
2733
+ if (value === "new") return "new";
2734
+ throw new Error("version must be current or new");
2594
2735
  };
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 }
2736
+ const find = (pieces, id) => {
2737
+ const found = pieces.find((piece) => piece.id === id);
2738
+ if (found === void 0) throw new Error(`the card has no piece "${id}": use an id from card_overview or card_search`);
2739
+ return found;
2600
2740
  };
2741
+ return [
2742
+ defineTool({
2743
+ name: "card_overview",
2744
+ 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.",
2745
+ parameters: {},
2746
+ output: output$1,
2747
+ isConcurrencySafe: () => true,
2748
+ async execute(_args, exec) {
2749
+ const { card, choices, pieces, records } = await source(exec);
2750
+ const madeFrom = entitiesByPiece({
2751
+ card,
2752
+ choices,
2753
+ pieces,
2754
+ records
2755
+ });
2756
+ const made = (id) => madeFrom.get(id) ?? [];
2757
+ const outcomes = withRecords ? pieceOutcomes({
2758
+ card,
2759
+ choices,
2760
+ pieces,
2761
+ records
2762
+ }, madeFrom) : [];
2763
+ return json$1({
2764
+ card: {
2765
+ name: card.name,
2766
+ creator: card.creator,
2767
+ version: card.characterVersion,
2768
+ tags: card.tags,
2769
+ format: card.sourceSpec,
2770
+ ignoredScripts: card.ignored.scripts
2771
+ },
2772
+ ...withRecords ? { outcomes: outcomeCounts(outcomes) } : {},
2773
+ pieces: pieces.map((piece) => ({
2774
+ id: piece.id,
2775
+ label: piece.label,
2776
+ characters: piece.text.length,
2777
+ readByTheImport: piece.used,
2778
+ ...withRecords ? {
2779
+ outcome: outcomes.find((entry) => entry.piece === piece.id)?.outcome,
2780
+ because: outcomes.find((entry) => entry.piece === piece.id)?.because
2781
+ } : {},
2782
+ ...piece.enabled === void 0 ? {} : { switchedOnInCard: piece.enabled },
2783
+ ...withRecords ? {
2784
+ takenToBe: rolesOf(records.classified, piece.id),
2785
+ madeInto: made(piece.id)
2786
+ } : {}
2787
+ })),
2788
+ ...records.roster === void 0 ? {} : { imported: records.roster.map((entity) => ({
2789
+ name: entity.name,
2790
+ kind: entity.kind,
2791
+ aliases: entity.aliases,
2792
+ playedByThePlayer: entity.player
2793
+ })) },
2794
+ ...records.report === void 0 ? {} : {
2795
+ importNotes: records.report.notes,
2796
+ leftOut: records.report.leftOut
2797
+ },
2798
+ 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."
2799
+ });
2800
+ }
2801
+ }),
2802
+ defineTool({
2803
+ name: "card_search",
2804
+ 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.",
2805
+ parameters: {
2806
+ query: {
2807
+ type: "string",
2808
+ required: true,
2809
+ description: "A name or phrase to look for, in the card's own language."
2810
+ },
2811
+ version: versionParameter,
2812
+ limit: {
2813
+ type: "number",
2814
+ description: `How many pieces to return, at most ${MAX_HITS} (default ${DEFAULT_HITS}).`
2815
+ }
2816
+ },
2817
+ output: output$1,
2818
+ isConcurrencySafe: () => true,
2819
+ async execute(args, exec) {
2820
+ const { pieces } = await source(exec, versionOf(args.version));
2821
+ const limit = Math.min(MAX_HITS, Math.max(1, Math.floor(typeof args.limit === "number" && Number.isFinite(args.limit) ? args.limit : DEFAULT_HITS)));
2822
+ const hits = searchPieces(pieces, args.query, limit);
2823
+ return json$1({
2824
+ query: args.query,
2825
+ found: hits.length,
2826
+ hits: hits.map(({ piece, matches, in: where, snippet: text }) => ({
2827
+ piece: piece.id,
2828
+ label: piece.label,
2829
+ matches,
2830
+ foundIn: where,
2831
+ snippet: text
2832
+ }))
2833
+ });
2834
+ }
2835
+ }),
2836
+ defineTool({
2837
+ name: "card_read",
2838
+ 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.`,
2839
+ parameters: {
2840
+ piece: {
2841
+ type: "string",
2842
+ required: true,
2843
+ description: "A piece id from card_overview, card_search or card_diff, such as field:description or entry:3."
2844
+ },
2845
+ version: versionParameter,
2846
+ offset: {
2847
+ type: "number",
2848
+ description: "Where to start reading, in characters (default 0)."
2849
+ }
2850
+ },
2851
+ output: output$1,
2852
+ isConcurrencySafe: () => true,
2853
+ async execute(args, exec) {
2854
+ const { pieces } = await source(exec, versionOf(args.version));
2855
+ const piece = find(pieces, args.piece);
2856
+ const from = Math.min(piece.text.length, Math.max(0, Math.floor(typeof args.offset === "number" && Number.isFinite(args.offset) ? args.offset : 0)));
2857
+ const to = Math.min(piece.text.length, from + READ_CHARS);
2858
+ return json$1({
2859
+ piece: piece.id,
2860
+ label: piece.label,
2861
+ ...piece.keys.length === 0 ? {} : { keys: piece.keys },
2862
+ from,
2863
+ to,
2864
+ of: piece.text.length,
2865
+ ...to < piece.text.length ? { next: to } : {},
2866
+ text: piece.text.slice(from, to)
2867
+ });
2868
+ }
2869
+ }),
2870
+ defineTool({
2871
+ name: "card_origin",
2872
+ 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.",
2873
+ parameters: { name: {
2874
+ type: "string",
2875
+ required: true,
2876
+ description: "The name or an alias, as the world or the card has it."
2877
+ } },
2878
+ output: output$1,
2879
+ isConcurrencySafe: () => true,
2880
+ async execute(args, exec) {
2881
+ const { card, choices, pieces, records } = await source(exec);
2882
+ const entity = records.roster === void 0 ? void 0 : findEntity(records.roster, args.name);
2883
+ if (entity === void 0 || records.classified === void 0) return json$1({
2884
+ name: args.name,
2885
+ madeByTheImport: false,
2886
+ mentions: searchPieces(pieces, args.name, DEFAULT_HITS).map(({ piece, in: where, snippet: text }) => ({
2887
+ piece: piece.id,
2888
+ label: piece.label,
2889
+ foundIn: where,
2890
+ snippet: text
2891
+ }))
2892
+ });
2893
+ return json$1({
2894
+ name: entity.name,
2895
+ kind: entity.kind,
2896
+ aliases: entity.aliases,
2897
+ madeByTheImport: true,
2898
+ usedPieces: originOf(card, choices, pieces, records, entity.name).map((entry) => ({
2899
+ piece: entry.piece,
2900
+ label: entry.label,
2901
+ how: entry.how,
2902
+ takenToBe: rolesOf(records.classified, entry.piece)
2903
+ }))
2904
+ });
2905
+ }
2906
+ })
2907
+ ];
2908
+ }
2909
+ /** For each piece of the card, the names of the characters, places and things the import made from it. */
2910
+ function entitiesByPiece(source) {
2911
+ const made = /* @__PURE__ */ new Map();
2912
+ 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]);
2913
+ return made;
2601
2914
  }
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"
2915
+ /** 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. */
2916
+ function originOf(card, choices, pieces, records, name, items = itemise(card, choices)) {
2917
+ if (records.roster === void 0 || records.classified === void 0) return [];
2918
+ const entity = findEntity(records.roster, name);
2919
+ if (entity === void 0) return [];
2920
+ const names = [entity.name, ...entity.aliases].map(norm);
2921
+ const chosen = sourcesFor(entity, items, records.classified);
2922
+ const about = (id) => (records.classified?.[id]?.targets ?? []).some((target) => target.kind === entity.kind && names.includes(norm(target.name)));
2923
+ const seen = /* @__PURE__ */ new Set();
2924
+ return chosen.flatMap((item) => {
2925
+ const id = baseId(item.id);
2926
+ const piece = pieces.find((entry) => entry.id === id);
2927
+ if (piece === void 0 || seen.has(id)) return [];
2928
+ seen.add(id);
2929
+ return [{
2930
+ piece: id,
2931
+ label: piece.label,
2932
+ how: about(item.id) ? "about it" : "mentions it"
2933
+ }];
2934
+ });
2935
+ }
2936
+ // src/host/import/repair.ts
2937
+ const ENTITY_FILE = /^(characters|locations|factions|items|lore)\/[^/]+\/(character|location|faction|item|lore)\.json$/;
2938
+ /** Fields a document cannot do without, and fields that tie it to others: never dropped, whatever the problem says. */
2939
+ const KEPT = /* @__PURE__ */ new Set([
2940
+ "id",
2941
+ "name",
2942
+ "role",
2943
+ "kind",
2944
+ "tier",
2945
+ "from",
2946
+ "to",
2947
+ "parent",
2948
+ "summary"
2647
2949
  ]);
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");
2950
+ const ABOUT_VALUE = /^(must be|is longer than|has more than|is not one of|must not|cannot be)/;
2951
+ const STEP = /([^.[\]]+)|\[(\d+)\]/g;
2952
+ const stepsOf = (field) => [...field.matchAll(STEP)].map((match) => match[2] === void 0 ? match[1] : Number(match[2]));
2953
+ /** Removes what `steps` names; false when it is not there. */
2954
+ function remove(root, steps) {
2955
+ let at = root;
2956
+ for (const step of steps.slice(0, -1)) {
2957
+ at = at?.[step];
2958
+ if (typeof at !== "object" || at === null) return void 0;
2959
+ }
2960
+ const last = steps.at(-1);
2961
+ if (typeof at !== "object" || at === null || !(last in at)) return void 0;
2962
+ const value = at[last];
2963
+ if (Array.isArray(at) && typeof last === "number") at.splice(last, 1);
2964
+ else delete at[last];
2965
+ return { value };
2690
2966
  }
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";
2967
+ /**
2968
+ * Mends what the world check refused, one field at a time and only where that is safe: an optional field of an entity whose value cannot be used (wrong type, too long, not one of the
2969
+ * allowed words) is dropped. A missing required field, a name, an id or a link to another entity is never touched: those stay problems for a person or the assistant. Returns what was dropped, with the values.
2970
+ */
2971
+ async function repairFields(directory, problems) {
2972
+ const repairs = [];
2973
+ const byFile = /* @__PURE__ */ new Map();
2974
+ for (const problem of problems) {
2975
+ if (!ENTITY_FILE.test(problem.where)) continue;
2976
+ const split = problem.message.indexOf(": ");
2977
+ if (split < 0) continue;
2978
+ const field = problem.message.slice(0, split);
2979
+ const because = problem.message.slice(split + 2);
2980
+ if (!/^[\w.[\]]+$/.test(field) || !ABOUT_VALUE.test(because) || KEPT.has(stepsOf(field)[0])) continue;
2981
+ byFile.set(problem.where, [...byFile.get(problem.where) ?? [], {
2982
+ field,
2983
+ because
2984
+ }]);
2698
2985
  }
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;
2705
- }
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 = [];
2710
- 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
2986
+ for (const [file, fields] of byFile) {
2987
+ const document = JSON.parse(await readFile(join(directory, file), "utf8"));
2988
+ for (const { field, because } of fields.sort((a, b) => b.field.localeCompare(a.field, void 0, { numeric: true }))) {
2989
+ const dropped = remove(document, stepsOf(field));
2990
+ if (dropped !== void 0) repairs.push({
2991
+ file,
2992
+ field,
2993
+ value: dropped.value,
2994
+ because
2717
2995
  });
2718
- continue;
2719
2996
  }
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);
2997
+ if (repairs.some((repair) => repair.file === file)) await atomicWrite(join(directory, file), `${JSON.stringify(document, null, 2)}\n`);
2730
2998
  }
2731
- return {
2732
- candidates,
2733
- skipped
2734
- };
2999
+ return repairs;
2735
3000
  }
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);
3001
+ // src/host/worlds/world-file-io.ts
3002
+ const queues = /* @__PURE__ */ new Map();
3003
+ /**
3004
+ * 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
3005
+ * back; run side by side, the last one to write would take back what the others changed.
3006
+ */
3007
+ function serialized(directory, work) {
3008
+ const run = (queues.get(directory) ?? Promise.resolve()).then(work, work);
3009
+ const tail = run.then(() => void 0, () => void 0);
3010
+ queues.set(directory, tail);
3011
+ tail.then(() => {
3012
+ if (queues.get(directory) === tail) queues.delete(directory);
2752
3013
  });
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);
3014
+ return run;
3015
+ }
3016
+ /** 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. */
3017
+ function updateWorldFile(directory, update) {
3018
+ return serialized(directory, async () => {
3019
+ const file = join(directory, WORLD_FILE_NAME);
3020
+ const text = await readFile(file, "utf8");
3021
+ const document = JSON.parse(text.replace(/^/, ""));
3022
+ await update(document);
3023
+ await atomicWrite(file, JSON.stringify(document, null, /^([ \t]+)"/m.exec(text)?.[1] ?? " ") + (text.endsWith("\n") || text.trim() === "" ? "\n" : ""));
3024
+ return document;
3025
+ });
3026
+ }
3027
+ // src/host/worlds/entity-folders.ts
3028
+ /** 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. */
3029
+ const ENTITY_FOLDERS = [
3030
+ {
3031
+ key: "characters",
3032
+ directory: CHARACTERS_DIR,
3033
+ file: CHARACTER_FILE_NAME
3034
+ },
3035
+ {
3036
+ key: "locations",
3037
+ directory: LOCATIONS_DIR,
3038
+ file: LOCATION_FILE_NAME
3039
+ },
3040
+ {
3041
+ key: "factions",
3042
+ directory: FACTIONS_DIR,
3043
+ file: FACTION_FILE_NAME
3044
+ },
3045
+ {
3046
+ key: "items",
3047
+ directory: ITEMS_DIR,
3048
+ file: ITEM_FILE_NAME
3049
+ },
3050
+ {
3051
+ key: "lore",
3052
+ directory: LORE_DIR,
3053
+ file: LORE_FILE_NAME
2764
3054
  }
2765
- return Buffer.concat(chunks);
3055
+ ];
3056
+ // src/host/import/finish.ts
3057
+ const STAGE = "stage";
3058
+ const MAX_DESCRIPTION = 500;
3059
+ const MAX_TAGS = 8;
3060
+ const MAX_TAG = 30;
3061
+ /** The card's other greetings, as the other openings a player may start with: the first message and the alternates that were not chosen. */
3062
+ function alternateOpenings(card, chosen, player) {
3063
+ if (chosen.kind === "none") return [];
3064
+ return [{
3065
+ title: "First message",
3066
+ text: card.firstMessage,
3067
+ taken: chosen.kind === "first"
3068
+ }, ...card.alternateGreetings.map((text, index) => ({
3069
+ title: `Alternate ${index + 1}`,
3070
+ text,
3071
+ taken: chosen.kind === "alternate" && chosen.index === index
3072
+ }))].flatMap(({ title, text, taken }) => {
3073
+ const cleaned = taken || text.trim() === "" ? "" : openingMessage(text, card.name, player);
3074
+ return cleaned === "" || cleaned.length > 2e4 ? [] : [{
3075
+ title,
3076
+ text: cleaned
3077
+ }];
3078
+ }).slice(0, 30);
2766
3079
  }
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);
3080
+ /** The character the card's picture shows: the one that carries the card's name, or the only one there is. */
3081
+ function cardCharacter(characters, cardName) {
3082
+ return characters.find((document) => norm(String(document["name"])) === norm(cardName)) ?? (characters.length === 1 ? characters[0] : void 0);
3083
+ }
3084
+ /** Makes the card's picture the canonical reference portrait of its character, as an image the person brought. */
3085
+ async function writePortrait(directory, picture, characters, cardName) {
3086
+ const character = cardCharacter(characters, cardName);
3087
+ if (character === void 0) return false;
3088
+ const id = String(character["id"]);
3089
+ const file = referenceImagePath(directory, {
3090
+ kind: "character",
3091
+ localId: id
2808
3092
  });
2809
- request$2.on("timeout", () => {
2810
- request$2.destroy(/* @__PURE__ */ new Error("timed out"));
3093
+ await writeAtomically(file, picture);
3094
+ await writeImageFileMetadata(file, {
3095
+ schema: "dsh-roleplay-image/v3",
3096
+ purpose: "reference",
3097
+ origin: "uploaded",
3098
+ reusable: false,
3099
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
3100
+ originalName: PICTURE_FILE,
3101
+ description: `${String(character["name"])}: the portrait that came with the character card.`,
3102
+ tags: [String(character["name"])],
3103
+ subjects: [{
3104
+ kind: "character",
3105
+ localId: id,
3106
+ name: String(character["name"])
3107
+ }]
2811
3108
  });
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
- }
3109
+ return true;
2826
3110
  }
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})`;
3111
+ /** The card's opening as the world's opening message: plain paragraphs, without the markup the card's tool read as emphasis. */
3112
+ function openingMessage(raw, cardName, player) {
3113
+ 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();
3114
+ return withPlayer(text, player);
2847
3115
  }
2848
- function fileNameOf(url) {
2849
- const last = url.pathname.split("/").pop() ?? "";
2850
- try {
2851
- return decodeURIComponent(last) || "image";
2852
- } catch {
2853
- return last || "image";
3116
+ /** 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. */
3117
+ function replacePlayer(value, player) {
3118
+ if (Array.isArray(value)) value.forEach((entry, index) => {
3119
+ if (typeof entry === "string") value[index] = withPlayer(entry, player);
3120
+ else replacePlayer(entry, player);
3121
+ });
3122
+ else if (typeof value === "object" && value !== null) {
3123
+ const record = value;
3124
+ for (const key of Object.keys(record)) {
3125
+ const entry = record[key];
3126
+ if (typeof entry === "string") record[key] = withPlayer(entry, player);
3127
+ else replacePlayer(entry, player);
3128
+ }
2854
3129
  }
2855
3130
  }
2856
3131
  /**
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.
3132
+ * 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
3133
+ * 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
3134
  */
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
3135
+ function shareThePlaces(characters, locations) {
3136
+ const places = locations.map((location) => String(location["id"]));
3137
+ const ways = [];
3138
+ for (const location of locations) {
3139
+ const from = String(location["id"]);
3140
+ for (const exit of Array.isArray(location["exits"]) ? location["exits"] : []) {
3141
+ const to = String(exit["to"]);
3142
+ ways.push({
3143
+ from,
3144
+ to
2912
3145
  });
2913
- } catch (error) {
2914
- skipped.push({
2915
- url: address,
2916
- reason: error instanceof Error ? error.message : String(error)
3146
+ if (exit["one_way"] !== true) ways.push({
3147
+ from: to,
3148
+ to: from
2917
3149
  });
2918
3150
  }
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
3151
  }
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);
3152
+ for (const character of characters) {
3153
+ const knowledge = typeof character["knowledge"] === "object" && character["knowledge"] !== null ? character["knowledge"] : {};
3154
+ if (knowledge["locations"] !== void 0 || knowledge["exits"] !== void 0) continue;
3155
+ knowledge["locations"] = places;
3156
+ knowledge["exits"] = ways.filter((way, index) => ways.findIndex((other) => other.from === way.from && other.to === way.to) === index);
3157
+ character["knowledge"] = knowledge;
3158
+ }
3159
+ }
3160
+ /** Places that contain each other in a circle are untangled by taking the parent off the one that closes it. */
3161
+ function breakParentCircles(locations) {
3162
+ const notes = [];
3163
+ const byId = new Map(locations.map((location) => [String(location["id"]), location]));
3164
+ for (const location of locations) {
3165
+ const seen = /* @__PURE__ */ new Set([String(location["id"])]);
3166
+ for (let up = location["parent"]; up !== void 0; up = byId.get(up)?.["parent"]) {
3167
+ if (seen.has(up)) {
3168
+ notes.push(`${String(location["name"])}: the places contain each other in a circle, so it is not put inside another place`);
3169
+ delete location["parent"];
3170
+ break;
3171
+ }
3172
+ seen.add(up);
3173
+ }
2962
3174
  }
2963
- return later.filter((url) => !settled.has(addressKey(url)));
3175
+ return notes;
2964
3176
  }
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));
3177
+ const WORLD_SCHEMA = {
3178
+ type: "object",
3179
+ properties: {
3180
+ description: {
3181
+ type: "string",
3182
+ description: `Two or three sentences, at most ${MAX_DESCRIPTION} characters.`
3183
+ },
3184
+ tags: {
3185
+ type: "array",
3186
+ items: { type: "string" },
3187
+ description: `At most ${MAX_TAGS} short tags.`
3188
+ },
3189
+ starting_time: {
3190
+ type: "string",
3191
+ 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."
3192
+ }
3193
+ },
3194
+ required: [
3195
+ "description",
3196
+ "tags",
3197
+ "starting_time"
3198
+ ],
3199
+ additionalProperties: false
3200
+ };
3201
+ const STARTING_TIME = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2})?)?$/;
3202
+ function worldJob(text, tr, card, roster, scenario, opening) {
3203
+ const names = (kind) => roster.filter((entity) => entity.kind === kind).slice(0, 15).map((entity) => entity.name).join("; ");
2968
3204
  return {
2969
- found,
2970
- added: [...earlier?.added ?? [], ...later.added],
2971
- skipped: [...(earlier?.skipped ?? []).filter((entry) => !asked.has(addressKey(entry.url))), ...later.skipped]
3205
+ persona: text("import/world-summary"),
3206
+ maxTokens: 1500,
3207
+ prompt: [
3208
+ tr("Card: {name}", { name: card.name }),
3209
+ ...card.tags.length === 0 ? [] : [tr("Card tags: {tags}", { tags: card.tags.join(", ") })],
3210
+ `${tr("Characters:")} ${names("character") || tr("(none)")}`,
3211
+ `${tr("Places:")} ${names("place") || tr("(none)")}`,
3212
+ `${tr("Scenario:")}\n${scenario.slice(0, 1200) || tr("(none)")}`,
3213
+ `${tr("Opening:")}\n${opening.slice(0, 1200) || tr("(none)")}`,
3214
+ `${tr("Description:")}\n${cleanText(card.description, card.name).slice(0, 1200) || tr("(none)")}`,
3215
+ `${tr("Creator's notes:")}\n${cleanText(card.creatorNotes, card.name).slice(0, 600) || tr("(none)")}`
3216
+ ].join("\n\n"),
3217
+ result: WORLD_SCHEMA,
3218
+ check: (value) => {
3219
+ if (value.description.trim() === "" || value.description.length > MAX_DESCRIPTION) return `the description must be written and at most ${MAX_DESCRIPTION} characters`;
3220
+ 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`;
3221
+ 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";
3222
+ }
2972
3223
  };
2973
3224
  }
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;
2982
- }
2983
- };
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;
3225
+ /** The description and tags of the world: asked of one call, and from the card's own fields when that fails. */
3226
+ async function describeWorld(ctx, card, roster, items, opening) {
3227
+ const scenario = items.filter((item) => item.id.startsWith("field:scenario")).map((item) => item.text).join("\n\n");
3228
+ if (ctx.noModel === true) return {
3229
+ description: (scenario || cleanText(card.description, card.name)).slice(0, MAX_DESCRIPTION).trim(),
3230
+ tags: card.tags.filter((tag) => tag.trim() !== "" && tag.length <= MAX_TAG).slice(0, MAX_TAGS)
3231
+ };
2991
3232
  try {
2992
- text = await readFile(join(directory, ORIGIN_FILE), "utf8");
3233
+ const answer = await ctx.run(worldJob(ctx.text, trOf(ctx), card, roster, scenario, opening));
3234
+ return {
3235
+ description: answer.description.trim(),
3236
+ tags: [...new Set(answer.tags.map((tag) => tag.trim()))],
3237
+ ...(answer.starting_time ?? "").trim() === "" ? {} : { startingTime: answer.starting_time.trim() }
3238
+ };
2993
3239
  } 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();
3240
+ return {
3241
+ description: (scenario || cleanText(card.description, card.name)).slice(0, MAX_DESCRIPTION).trim(),
3242
+ tags: card.tags.filter((tag) => tag.trim() !== "" && tag.length <= MAX_TAG).slice(0, MAX_TAGS),
3243
+ note: `the description of the world was taken from the card as it is: ${error instanceof Error ? error.message : String(error)}`
3244
+ };
3003
3245
  }
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();
3015
- 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
3246
  }
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");
3037
- }
3038
- };
3039
- await walk("");
3040
- return hashes;
3247
+ async function hasEntities(world) {
3248
+ for (const { directory } of ENTITY_FOLDERS) try {
3249
+ if ((await readdir(join(world, directory))).some((name) => !name.startsWith("."))) return true;
3250
+ } catch (error) {
3251
+ if (error.code !== "ENOENT") throw error;
3252
+ }
3253
+ return false;
3041
3254
  }
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)
3255
+ const writeAtomically = (file, text) => atomicWrite(file, text, { createDirectory: true });
3256
+ /** The world as it is after the import: every document written into it, or nothing changed and the problems found. */
3257
+ const MAX_REPAIR_ROUNDS = 4;
3258
+ /**
3259
+ * 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
3260
+ * 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.
3261
+ */
3262
+ async function finishImport(ctx, input) {
3263
+ const { directory, card, choices, roster, items, classified, extracted } = input;
3264
+ if (await hasEntities(directory)) return {
3265
+ ok: false,
3266
+ problems: ["the world already has characters, places, factions, things or lore: an import only fills an empty world"]
3047
3267
  };
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}`)
3268
+ const playing = extracted.characters.find((document) => document["role"] === "playable");
3269
+ const playerName = playing === void 0 ? void 0 : String(playing["name"]);
3270
+ for (const key of [
3271
+ "characters",
3272
+ "locations",
3273
+ "factions",
3274
+ "items",
3275
+ "lore",
3276
+ "commands",
3277
+ "triggers",
3278
+ "variables"
3279
+ ]) replacePlayer(extracted[key], playerName);
3280
+ const notes = [
3281
+ ...input.notes,
3282
+ ...extracted.notes,
3283
+ ...breakParentCircles(extracted.locations)
3056
3284
  ];
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;
3285
+ shareThePlaces(extracted.characters, extracted.locations);
3286
+ const stage = join(directory, IMPORT_FOLDER, STAGE);
3287
+ await rm(stage, {
3288
+ recursive: true,
3289
+ force: true
3290
+ });
3291
+ const mend = () => {
3292
+ const view = {
3293
+ ...extracted,
3294
+ notes
3295
+ };
3296
+ pruneLoreSubjects(view);
3297
+ pruneMembers(view);
3298
+ pruneLinks(view);
3299
+ pruneKnowledge(view);
3300
+ pruneRelationships(view);
3301
+ };
3302
+ mend();
3077
3303
  try {
3078
- recorded = JSON.parse(await readFile(join(directory, IMPORT_FOLDER, SNAPSHOT), "utf8"));
3079
- } catch (error) {
3080
- if (error.code === "ENOENT") return { edited: "unknown" };
3081
- throw error;
3304
+ if (extracted.variables.length > 0) await writeAtomically(join(stage, VARIABLES_FILE_NAME), `${JSON.stringify(extracted.variables, null, 2)}\n`);
3305
+ if (extracted.commands.length > 0) await writeAtomically(join(stage, COMMANDS_FILE_NAME), `${JSON.stringify({ commands: extracted.commands }, null, 2)}\n`);
3306
+ if (extracted.triggers.length > 0) await writeAtomically(join(stage, TRIGGERS_FILE_NAME), `${JSON.stringify(extracted.triggers, null, 2)}\n`);
3307
+ 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`);
3308
+ for (const { id, text } of extracted.prompts) {
3309
+ const problem = promptProblem("en", id, text);
3310
+ if (problem !== void 0) return {
3311
+ ok: false,
3312
+ problems: [`prompt ${id}: ${problem}`]
3313
+ };
3314
+ }
3315
+ if (extracted.variables.length > 0) await rename(join(stage, VARIABLES_FILE_NAME), join(directory, VARIABLES_FILE_NAME));
3316
+ if (extracted.commands.length > 0) await rename(join(stage, COMMANDS_FILE_NAME), join(directory, COMMANDS_FILE_NAME));
3317
+ if (extracted.triggers.length > 0) await rename(join(stage, TRIGGERS_FILE_NAME), join(directory, TRIGGERS_FILE_NAME));
3318
+ for (const { directory: folder } of ENTITY_FOLDERS) {
3319
+ const from = join(stage, folder);
3320
+ if (await stat(from).then(() => true, () => false)) await rename(from, join(directory, folder));
3321
+ }
3322
+ } finally {
3323
+ await rm(stage, {
3324
+ recursive: true,
3325
+ force: true
3326
+ });
3082
3327
  }
3083
- const changes = compare(recorded, await hashWorld(directory, notTheCards));
3084
- return isUnchanged(changes) ? { edited: "no" } : {
3085
- edited: "yes",
3086
- changes: describeChanges(changes)
3087
- };
3088
- }
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;
3095
- try {
3096
- text = await readFile(join(directory, IMPORT_FOLDER, FILE$2), "utf8");
3328
+ let check = await checkWorld({
3329
+ directory,
3330
+ summary: { name: choices.name }
3331
+ });
3332
+ const repairs = [];
3333
+ for (let round = 0; round < MAX_REPAIR_ROUNDS && !check.ok; round += 1) {
3334
+ const made = await repairFields(directory, check.problems);
3335
+ if (made.length === 0) break;
3336
+ repairs.push(...made);
3337
+ check = await checkWorld({
3338
+ directory,
3339
+ summary: { name: choices.name }
3340
+ });
3341
+ }
3342
+ if (repairs.length > 0) {
3343
+ await writeAtomically(join(directory, IMPORT_FOLDER, "repairs.json"), JSON.stringify(repairs, null, 2));
3344
+ notes.push(`${repairs.length} field(s) were dropped because their value could not be used (${repairs.slice(0, 5).map((repair) => `${repair.file}: ${repair.field}`).join("; ")}${repairs.length > 5 ? "; ..." : ""}); the values are kept in ${IMPORT_FOLDER}/repairs.json`);
3345
+ }
3346
+ 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"})`);
3347
+ const player = extracted.characters.find((document) => document["role"] === "playable");
3348
+ 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`);
3349
+ const opening = openingText(card, choices.opening);
3350
+ const described = await describeWorld(ctx, card, roster, items, opening ?? "");
3351
+ if (described.note !== void 0) notes.push(described.note);
3352
+ described.description = withPlayer(described.description, player === void 0 ? void 0 : String(player["name"]));
3353
+ let cover = false;
3354
+ if (input.picture !== void 0) {
3355
+ await writeAtomically(join(directory, "media", "files", "cover.png"), input.picture);
3356
+ await writeAtomically(join(directory, "media", "manifest.jsonl"), `${JSON.stringify({
3357
+ id: "cover",
3358
+ file: "media/files/cover.png"
3359
+ })}\n`);
3360
+ cover = true;
3361
+ }
3362
+ const alternates = alternateOpenings(card, choices.opening, player === void 0 ? void 0 : String(player["name"]));
3363
+ await updateWorldFile(directory, (world) => {
3364
+ world["description"] = described.description;
3365
+ if (described.startingTime !== void 0 && world["starting_time"] === void 0) world["starting_time"] = described.startingTime;
3366
+ world["metadata"] = {
3367
+ ...typeof world["metadata"] === "object" && world["metadata"] !== null ? world["metadata"] : {},
3368
+ tags: described.tags
3369
+ };
3370
+ if (opening !== void 0) world["opening_message"] = openingMessage(opening, card.name, player === void 0 ? void 0 : String(player["name"]));
3371
+ if (alternates.length > 0) world["alternate_openings"] = alternates;
3372
+ if (card.creator.trim() !== "" && world["author"] === void 0) world["author"] = { name: card.creator.trim() };
3373
+ if (cover) world["cover_media_id"] = "cover";
3374
+ });
3375
+ let portrait = false;
3376
+ if (input.picture !== void 0) try {
3377
+ portrait = await writePortrait(directory, input.picture, extracted.characters, card.name);
3097
3378
  } catch (error) {
3098
- if (error.code === "ENOENT") return void 0;
3099
- throw error;
3379
+ notes.push(`the card's picture could not be used as a portrait: ${error instanceof Error ? error.message : String(error)}`);
3100
3380
  }
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")
3381
+ const taken = (role) => items.filter((item) => {
3382
+ const classification = classified[item.id];
3383
+ return classification !== void 0 && isLeftOut(classification) && classification.roles[0] === role;
3384
+ }).length;
3385
+ const derived = extracted.derived ?? [];
3386
+ const sourceOf = {
3387
+ card,
3388
+ choices,
3389
+ pieces: cardPieces(card, choices),
3390
+ records: {
3391
+ classified,
3392
+ roster
3114
3393
  }
3115
3394
  };
3116
- await atomicWriteJson(join(directory, IMPORT_FOLDER, FILE$2), {
3117
- schema: PROVENANCE_SCHEMA,
3118
- history: [...earlier?.history ?? [], line]
3119
- });
3395
+ const report = {
3396
+ prompts: extracted.prompts.map((entry) => entry.id),
3397
+ variables: extracted.variables.length,
3398
+ counts: {
3399
+ characters: extracted.characters.length,
3400
+ places: extracted.locations.length,
3401
+ factions: extracted.factions.length,
3402
+ things: extracted.items.length,
3403
+ lore: extracted.lore.length
3404
+ },
3405
+ hasPlayableCharacter: player !== void 0,
3406
+ opening: opening === void 0 ? "none" : "imported",
3407
+ alternateOpenings: alternates.length,
3408
+ cover,
3409
+ portrait,
3410
+ leftOut: {
3411
+ instructions: taken("instruction"),
3412
+ templates: taken("template"),
3413
+ irrelevant: taken("irrelevant"),
3414
+ scripts: card.ignored.scripts,
3415
+ dialect: dialectOf(card)
3416
+ },
3417
+ pictures: input.pictures ?? {
3418
+ found: 0,
3419
+ added: 0
3420
+ },
3421
+ ...derived.length === 0 ? {} : { derived },
3422
+ pieces: outcomeCounts(pieceOutcomes(sourceOf, entitiesByPiece(sourceOf), {
3423
+ derived,
3424
+ finished: true
3425
+ })),
3426
+ notes
3427
+ };
3428
+ await writeAtomically(join(directory, IMPORT_FOLDER, "report.json"), JSON.stringify(report, null, 2));
3429
+ return {
3430
+ ok: true,
3431
+ report,
3432
+ ...check.ok ? {} : { draft: check }
3433
+ };
3120
3434
  }
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
- ];
3435
+ // src/host/import/pictures.ts
3436
+ /** The address is not one a picture may be fetched from: it names this machine or a private network. */
3437
+ var BlockedAddressError = class extends Error {};
3438
+ const MAX_BYTES = 20971520;
3439
+ const MIN_BYTES = 1024;
3440
+ const MAX_REDIRECTS = 4;
3441
+ const TIMEOUT_MS = 3e4;
3442
+ const CONCURRENCY = 8;
3443
+ const NON_IMAGE_EXTENSIONS = /* @__PURE__ */ new Set([
3444
+ "js",
3445
+ "mjs",
3446
+ "cjs",
3447
+ "css",
3448
+ "html",
3449
+ "htm",
3450
+ "json",
3451
+ "xml",
3452
+ "txt",
3453
+ "md",
3454
+ "map",
3455
+ "wasm",
3456
+ "php",
3457
+ "py",
3458
+ "woff",
3459
+ "woff2",
3460
+ "ttf",
3461
+ "otf",
3462
+ "eot",
3463
+ "mp3",
3464
+ "wav",
3465
+ "ogg",
3466
+ "flac",
3467
+ "m4a",
3468
+ "mp4",
3469
+ "webm",
3470
+ "mkv",
3471
+ "mov",
3472
+ "avi",
3473
+ "zip",
3474
+ "rar",
3475
+ "gz",
3476
+ "7z",
3477
+ "pdf",
3478
+ "doc",
3479
+ "docx"
3480
+ ]);
3481
+ const blocked = new BlockList();
3482
+ for (const [network, prefix] of [
3483
+ ["0.0.0.0", 8],
3484
+ ["10.0.0.0", 8],
3485
+ ["100.64.0.0", 10],
3486
+ ["127.0.0.0", 8],
3487
+ ["169.254.0.0", 16],
3488
+ ["172.16.0.0", 12],
3489
+ ["192.0.0.0", 24],
3490
+ ["192.0.2.0", 24],
3491
+ ["192.88.99.0", 24],
3492
+ ["192.168.0.0", 16],
3493
+ ["198.18.0.0", 15],
3494
+ ["198.51.100.0", 24],
3495
+ ["203.0.113.0", 24],
3496
+ ["224.0.0.0", 4],
3497
+ ["240.0.0.0", 4]
3498
+ ]) blocked.addSubnet(network, prefix, "ipv4");
3499
+ for (const [network, prefix] of [
3500
+ ["::", 128],
3501
+ ["::1", 128],
3502
+ ["64:ff9b::", 96],
3503
+ ["100::", 64],
3504
+ ["2001::", 32],
3505
+ ["2001:db8::", 32],
3506
+ ["2002::", 16],
3507
+ ["fc00::", 7],
3508
+ ["fe80::", 10],
3509
+ ["ff00::", 8]
3510
+ ]) blocked.addSubnet(network, prefix, "ipv6");
3511
+ /** Whether an address may be connected to: only public unicast addresses may. */
3512
+ function isPublicAddress(address) {
3513
+ const family = isIP(address);
3514
+ if (family === 0) return false;
3515
+ if (family === 4) return !blocked.check(address, "ipv4");
3516
+ const mapped = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i.exec(new URL(`http://[${address}]/`).hostname.replace(/^\[|\]$/g, ""));
3517
+ if (mapped !== null) {
3518
+ const high = Number.parseInt(mapped[1], 16);
3519
+ const low = Number.parseInt(mapped[2], 16);
3520
+ return isPublicAddress(`${high >> 8}.${high & 255}.${low >> 8}.${low & 255}`);
3521
+ }
3522
+ return !blocked.check(address, "ipv6");
3184
3523
  }
3185
- async function optional(file) {
3524
+ /** The address a picture may be asked for, or why not: web only, standard ports, no credentials, no private literal address. */
3525
+ function vetUrl(raw) {
3526
+ let url;
3186
3527
  try {
3187
- return JSON.parse(await readFile(file, "utf8"));
3188
- } catch (error) {
3189
- if (error.code === "ENOENT") return void 0;
3190
- throw error;
3528
+ url = new URL(raw);
3529
+ } catch {
3530
+ return "not an address";
3191
3531
  }
3532
+ if (url.protocol !== "http:" && url.protocol !== "https:") return "not a web address";
3533
+ if (url.username !== "" || url.password !== "") return "has credentials";
3534
+ if (url.port !== "" && url.port !== "80" && url.port !== "443") return "uses an unusual port";
3535
+ const host = url.hostname.replace(/^\[|\]$/g, "");
3536
+ if (isIP(host) !== 0 && !isPublicAddress(host)) return "is a private address";
3537
+ return url;
3192
3538
  }
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;
3539
+ /** The addresses worth asking about: a picture file name, or a name with no file type at all. */
3540
+ function candidateUrls(urls) {
3541
+ const candidates = [];
3542
+ const skipped = [];
3543
+ const seen = /* @__PURE__ */ new Set();
3544
+ for (const raw of urls) {
3545
+ const url = vetUrl(raw);
3546
+ if (typeof url === "string") {
3547
+ skipped.push({
3548
+ url: raw,
3549
+ reason: url
3550
+ });
3551
+ continue;
3552
+ }
3553
+ url.hash = "";
3554
+ const key = url.href;
3555
+ if (seen.has(key)) continue;
3556
+ seen.add(key);
3557
+ const extension = /\.([a-z0-9]{1,5})$/i.exec(url.pathname)?.[1]?.toLowerCase();
3558
+ if (extension !== void 0 && MEDIA_TYPES[`.${extension}`] === void 0) skipped.push({
3559
+ url: raw,
3560
+ reason: NON_IMAGE_EXTENSIONS.has(extension) ? "not an image" : "format not supported"
3561
+ });
3562
+ else candidates.push(key);
3202
3563
  }
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
3564
  return {
3209
- card: read.card,
3210
- choices: read.choices,
3211
- pieces: cardPieces(read.card, read.choices),
3212
- records
3565
+ candidates,
3566
+ skipped
3213
3567
  };
3214
3568
  }
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))];
3569
+ /** 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. */
3570
+ const guardedLookup = (hostname, options, callback) => {
3571
+ lookup(hostname, {
3572
+ ...options,
3573
+ all: true
3574
+ }, (error, addresses) => {
3575
+ if (error !== null) {
3576
+ callback(error, "", 0);
3577
+ return;
3578
+ }
3579
+ if (addresses.find((entry) => !isPublicAddress(entry.address)) !== void 0 || addresses.length === 0) {
3580
+ callback(new BlockedAddressError(`${hostname} is not a public address`), "", 0);
3581
+ return;
3582
+ }
3583
+ if (options.all === true) callback(null, addresses);
3584
+ else callback(null, addresses[0].address, addresses[0].family);
3585
+ });
3218
3586
  };
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 ? "…" : ""}`;
3587
+ async function readBody(response) {
3588
+ const chunks = [];
3589
+ let size = 0;
3590
+ for await (const chunk of response) {
3591
+ size += chunk.length;
3592
+ if (size > MAX_BYTES) {
3593
+ response.destroy();
3594
+ throw new Error("too large");
3595
+ }
3596
+ chunks.push(chunk);
3597
+ }
3598
+ return Buffer.concat(chunks);
3223
3599
  }
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;
3600
+ /** One request, never following a redirect, with the connection itself checked against private networks. */
3601
+ const requestOnce = (url, method, signal) => new Promise((resolveReply, reject) => {
3602
+ const request$2 = (url.protocol === "https:" ? request$1 : request)(url, {
3603
+ method,
3604
+ lookup: guardedLookup,
3605
+ signal,
3606
+ timeout: TIMEOUT_MS,
3607
+ agent: false,
3608
+ headers: {
3609
+ "user-agent": "Mozilla/5.0 (compatible; dsh-roleplay-import)",
3610
+ accept: "image/*,*/*;q=0.5",
3611
+ "accept-encoding": "identity"
3234
3612
  }
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
- }));
3613
+ }, (response) => {
3614
+ const status = response.statusCode ?? 0;
3615
+ const type = String(response.headers["content-type"] ?? "").split(";")[0].trim().toLowerCase();
3616
+ const location = typeof response.headers.location === "string" ? response.headers.location : void 0;
3617
+ const length = Number(response.headers["content-length"] ?? 0);
3618
+ if (method === "HEAD" || status < 200 || status >= 300 || location !== void 0 && status >= 300) {
3619
+ response.destroy();
3620
+ resolveReply({
3621
+ status,
3622
+ type,
3623
+ location,
3624
+ body: Buffer.alloc(0)
3625
+ });
3626
+ return;
3627
+ }
3628
+ if (length > MAX_BYTES) {
3629
+ response.destroy();
3630
+ reject(/* @__PURE__ */ new Error("too large"));
3631
+ return;
3632
+ }
3633
+ readBody(response).then((body) => {
3634
+ resolveReply({
3635
+ status,
3636
+ type,
3637
+ location,
3638
+ body
3639
+ });
3640
+ }, reject);
3641
+ });
3642
+ request$2.on("timeout", () => {
3643
+ request$2.destroy(/* @__PURE__ */ new Error("timed out"));
3644
+ });
3645
+ request$2.on("error", reject);
3646
+ request$2.end();
3647
+ });
3648
+ /** Asks for the address, following up to a few redirects, each vetted like the first address. */
3649
+ async function follow(fetcher, start, method, signal) {
3650
+ let url = start;
3651
+ for (let hop = 0;; hop += 1) {
3652
+ const reply = await fetcher(url, method, signal);
3653
+ if (reply.status < 300 || reply.status >= 400 || reply.location === void 0) return reply;
3654
+ if (hop >= MAX_REDIRECTS) throw new Error("too many redirects");
3655
+ const next = vetUrl(new URL(reply.location, url).href);
3656
+ if (typeof next === "string") throw new BlockedAddressError(`a redirect ${next}`);
3657
+ url = next;
3658
+ }
3659
+ }
3660
+ const GENERIC_TYPES = /* @__PURE__ */ new Set([
3661
+ "",
3662
+ "application/octet-stream",
3663
+ "binary/octet-stream",
3664
+ "application/binary",
3665
+ "application/x-binary"
3666
+ ]);
3667
+ const SUPPORTED_TYPES = new Set(Object.values(MEDIA_TYPES));
3668
+ /** 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). */
3669
+ async function worthFetching(fetcher, url, signal) {
3670
+ let reply;
3671
+ try {
3672
+ reply = await follow(fetcher, url, "HEAD", signal);
3673
+ } catch (error) {
3674
+ return error instanceof BlockedAddressError ? error.message : void 0;
3675
+ }
3676
+ if (reply.status === 404 || reply.status === 410) return "gone";
3677
+ if (reply.status < 200 || reply.status >= 300) return void 0;
3678
+ if (reply.type.startsWith("image/")) return SUPPORTED_TYPES.has(reply.type) ? void 0 : "format not supported";
3679
+ return GENERIC_TYPES.has(reply.type) ? void 0 : `not an image (${reply.type})`;
3680
+ }
3681
+ function fileNameOf(url) {
3682
+ const last = url.pathname.split("/").pop() ?? "";
3683
+ try {
3684
+ return decodeURIComponent(last) || "image";
3685
+ } catch {
3686
+ return last || "image";
3687
+ }
3286
3688
  }
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
3689
  /**
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.
3690
+ * 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
3691
+ * 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
3692
  */
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."
3693
+ async function fetchPictures(world, urls, deps = {}) {
3694
+ const fetcher = deps.fetcher ?? requestOnce;
3695
+ const { candidates, skipped: refused } = candidateUrls(urls);
3696
+ const skipped = [...refused];
3697
+ const added = [];
3698
+ const hashes = await libraryHashes(world);
3699
+ await mapLimit(candidates, deps.concurrency ?? CONCURRENCY, async (address) => {
3700
+ const signal = AbortSignal.any([AbortSignal.timeout(TIMEOUT_MS * 2), ...deps.signal === void 0 ? [] : [deps.signal]]);
3701
+ const url = new URL(address);
3702
+ try {
3703
+ const refusal = await worthFetching(fetcher, url, signal);
3704
+ if (refusal !== void 0) {
3705
+ skipped.push({
3706
+ url: address,
3707
+ reason: refusal
3375
3708
  });
3709
+ return;
3376
3710
  }
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
- }))
3711
+ const reply = await follow(fetcher, url, "GET", signal);
3712
+ if (reply.status < 200 || reply.status >= 300) {
3713
+ skipped.push({
3714
+ url: address,
3715
+ reason: `answered ${reply.status}`
3409
3716
  });
3717
+ return;
3410
3718
  }
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)
3719
+ if (imageExtensionOf(reply.body) === void 0) {
3720
+ skipped.push({
3721
+ url: address,
3722
+ reason: "not an image"
3443
3723
  });
3724
+ return;
3444
3725
  }
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
- }))
3726
+ if (reply.body.length < MIN_BYTES) {
3727
+ skipped.push({
3728
+ url: address,
3729
+ reason: "too small"
3468
3730
  });
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
- }))
3731
+ return;
3732
+ }
3733
+ const { path, added: isNew } = await addToLibraryOnce(world, reply.body, fileNameOf(url), hashes);
3734
+ if (!isNew) {
3735
+ skipped.push({
3736
+ url: address,
3737
+ reason: "a copy of another picture",
3738
+ of: path
3480
3739
  });
3740
+ return;
3481
3741
  }
3482
- })
3742
+ added.push({
3743
+ url: address,
3744
+ path
3745
+ });
3746
+ } catch (error) {
3747
+ skipped.push({
3748
+ url: address,
3749
+ reason: error instanceof Error ? error.message : String(error)
3750
+ });
3751
+ }
3752
+ });
3753
+ return {
3754
+ found: candidates.length,
3755
+ added,
3756
+ skipped
3757
+ };
3758
+ }
3759
+ /** The key an address is known by: the way `candidateUrls` reads it, without a fragment. */
3760
+ function addressKey(raw) {
3761
+ try {
3762
+ const url = new URL(raw);
3763
+ url.hash = "";
3764
+ return url.href;
3765
+ } catch {
3766
+ return raw;
3767
+ }
3768
+ }
3769
+ const FINAL = [
3770
+ /^not an? /,
3771
+ /^format not supported/,
3772
+ /^gone$/,
3773
+ /^too small$/,
3774
+ /^too large$/,
3775
+ /^a copy of /,
3776
+ /^has credentials/,
3777
+ /^uses an unusual port/,
3778
+ /^is a private address/,
3779
+ /^a redirect /,
3780
+ /is not a public address$/,
3781
+ /^answered (4(?!08|29)\d\d)$/
3782
+ ];
3783
+ /** 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. */
3784
+ const worthAskingAgain = (reason) => !FINAL.some((pattern) => pattern.test(reason));
3785
+ /**
3786
+ * 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
3787
+ * of that fetch every address of the earlier card counts as settled, so what the person removed from the library is not brought back.
3788
+ */
3789
+ function addressesToFetch(earlier, later, record) {
3790
+ const settled = new Set(earlier.map(addressKey));
3791
+ if (record !== void 0) {
3792
+ const unsettled = new Set(record.skipped.filter((entry) => worthAskingAgain(entry.reason)).map((entry) => addressKey(entry.url)));
3793
+ const fetched = new Set(record.added.map((entry) => addressKey(entry.url)));
3794
+ for (const key of [...settled]) if (unsettled.has(key) && !fetched.has(key)) settled.delete(key);
3795
+ }
3796
+ return later.filter((url) => !settled.has(addressKey(url)));
3797
+ }
3798
+ /** The record after a second fetch: what was added stays, an address asked again takes its new answer. */
3799
+ function mergeReports(earlier, later, found) {
3800
+ const asked = new Set([...later.added.map((entry) => entry.url), ...later.skipped.map((entry) => entry.url)].map(addressKey));
3801
+ return {
3802
+ found,
3803
+ added: [...earlier?.added ?? [], ...later.added],
3804
+ skipped: [...(earlier?.skipped ?? []).filter((entry) => !asked.has(addressKey(entry.url))), ...later.skipped]
3805
+ };
3806
+ }
3807
+ // src/host/catalogue/errors.ts
3808
+ var CatalogueError = class extends Error {
3809
+ failure;
3810
+ detail;
3811
+ constructor(message, failure, detail) {
3812
+ super(message);
3813
+ this.failure = failure;
3814
+ this.detail = detail;
3815
+ }
3816
+ };
3817
+ // src/host/catalogue/origin.ts
3818
+ const ORIGIN_FILE = ".catalogue.json";
3819
+ const MAX_LISTED_CHANGES = 12;
3820
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
3821
+ /** The origin recorded in a world's folder, or nothing when it has none. @throws {CatalogueError} when the record is damaged. */
3822
+ async function readOrigin(directory) {
3823
+ let text;
3824
+ try {
3825
+ text = await readFile(join(directory, ORIGIN_FILE), "utf8");
3826
+ } catch (error) {
3827
+ if (error.code === "ENOENT") return void 0;
3828
+ throw error;
3829
+ }
3830
+ const damaged = () => new CatalogueError(`${ORIGIN_FILE} in ${directory} is damaged`, "invalid");
3831
+ let value;
3832
+ try {
3833
+ value = JSON.parse(text);
3834
+ } catch {
3835
+ throw damaged();
3836
+ }
3837
+ if (!isRecord(value)) throw damaged();
3838
+ const { kind, root, resourceId, versionId, version, files } = value;
3839
+ if (kind !== "release" && kind !== "card" || typeof root !== "string" || typeof resourceId !== "string" || typeof versionId !== "string" || typeof version !== "string") throw damaged();
3840
+ if (kind === "card") return {
3841
+ kind,
3842
+ root,
3843
+ resourceId,
3844
+ versionId,
3845
+ version
3846
+ };
3847
+ if (!isRecord(files) || !Object.values(files).every((hash) => typeof hash === "string")) throw damaged();
3848
+ return {
3849
+ kind,
3850
+ root,
3851
+ resourceId,
3852
+ versionId,
3853
+ version,
3854
+ files
3855
+ };
3856
+ }
3857
+ async function writeOrigin(directory, origin) {
3858
+ const file = join(directory, ORIGIN_FILE);
3859
+ await atomicWriteJson(file, origin, { indent: 2 });
3860
+ }
3861
+ /** 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. */
3862
+ async function hashWorld(directory, skip = () => false) {
3863
+ const hashes = {};
3864
+ const walk = async (relative) => {
3865
+ for (const entry of await readdir(join(directory, relative), { withFileTypes: true })) {
3866
+ if (relative === "" && entry.name.startsWith(".")) continue;
3867
+ const path = relative === "" ? entry.name : `${relative}/${entry.name}`;
3868
+ if (entry.isDirectory()) await walk(path);
3869
+ else if (entry.isFile() && !skip(path)) hashes[path] = createHash("sha256").update(await readFile(join(directory, path))).digest("hex");
3870
+ }
3871
+ };
3872
+ await walk("");
3873
+ return hashes;
3874
+ }
3875
+ function compare(recorded, current) {
3876
+ return {
3877
+ changed: Object.keys(recorded).filter((path) => current[path] !== void 0 && current[path] !== recorded[path]),
3878
+ added: Object.keys(current).filter((path) => recorded[path] === void 0),
3879
+ removed: Object.keys(recorded).filter((path) => current[path] === void 0)
3880
+ };
3881
+ }
3882
+ const isUnchanged = (changes) => changes.changed.length + changes.added.length + changes.removed.length === 0;
3883
+ /** The differences as short lines for a person to read, capped. */
3884
+ function describeChanges(changes) {
3885
+ const lines = [
3886
+ ...changes.changed.map((path) => `changed: ${path}`),
3887
+ ...changes.added.map((path) => `added: ${path}`),
3888
+ ...changes.removed.map((path) => `removed: ${path}`)
3483
3889
  ];
3890
+ return lines.length <= MAX_LISTED_CHANGES ? lines : [...lines.slice(0, MAX_LISTED_CHANGES), `and ${lines.length - MAX_LISTED_CHANGES} more`];
3484
3891
  }
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;
3892
+ /** How a world's files compare with the release it came from: the differences, or `unverified` when no record of the release exists. */
3893
+ async function localChanges(directory) {
3894
+ const origin = await readOrigin(directory);
3895
+ if (origin?.kind !== "release") return "unverified";
3896
+ return compare(origin.files, await hashWorld(directory));
3490
3897
  }
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
- }];
3898
+ // src/host/import/snapshot.ts
3899
+ const SNAPSHOT = "files.json";
3900
+ /** The pictures of a world are fetched and generated in the background and are not what a card says. */
3901
+ const notTheCards = (path) => path.startsWith("images/");
3902
+ /** What a world's files were when its import was over; "changed since" is measured against it. */
3903
+ async function writeSnapshot(directory) {
3904
+ const file = join(directory, IMPORT_FOLDER, SNAPSHOT);
3905
+ await atomicWriteJson(file, await hashWorld(directory, notTheCards));
3906
+ }
3907
+ /** 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). */
3908
+ async function editsSince(directory) {
3909
+ let recorded;
3910
+ try {
3911
+ recorded = JSON.parse(await readFile(join(directory, IMPORT_FOLDER, SNAPSHOT), "utf8"));
3912
+ } catch (error) {
3913
+ if (error.code === "ENOENT") return { edited: "unknown" };
3914
+ throw error;
3915
+ }
3916
+ const changes = compare(recorded, await hashWorld(directory, notTheCards));
3917
+ return isUnchanged(changes) ? { edited: "no" } : {
3918
+ edited: "yes",
3919
+ changes: describeChanges(changes)
3920
+ };
3921
+ }
3922
+ // src/host/import/provenance.ts
3923
+ const FILE$2 = "provenance.json";
3924
+ const PROVENANCE_SCHEMA = "dsh-roleplay-import/v1";
3925
+ /** The import record of a world, or nothing for a world imported before records were kept. */
3926
+ async function readProvenance(directory) {
3927
+ let text;
3928
+ try {
3929
+ text = await readFile(join(directory, IMPORT_FOLDER, FILE$2), "utf8");
3930
+ } catch (error) {
3931
+ if (error.code === "ENOENT") return void 0;
3932
+ throw error;
3933
+ }
3934
+ const value = JSON.parse(text);
3935
+ if (value.schema !== "dsh-roleplay-import/v1" || !Array.isArray(value.history)) throw new Error(`${FILE$2} is not an import record this version reads`);
3936
+ return value;
3937
+ }
3938
+ /** Adds one line to the world's import record: what card it came from and, for an import, what was written. */
3939
+ async function recordProvenance(directory, entry, cardJson) {
3940
+ const earlier = await readProvenance(directory);
3941
+ const line = {
3942
+ ...entry,
3943
+ at: (/* @__PURE__ */ new Date()).toISOString(),
3944
+ card: {
3945
+ name: entry.card.name,
3946
+ sha256: createHash("sha256").update(cardJson).digest("hex")
3947
+ }
3948
+ };
3949
+ await atomicWriteJson(join(directory, IMPORT_FOLDER, FILE$2), {
3950
+ schema: PROVENANCE_SCHEMA,
3951
+ history: [...earlier?.history ?? [], line]
3510
3952
  });
3511
3953
  }
3954
+ // src/shared/checks.ts
3955
+ /** A check suite is made of kits; a person asks for kits with a command and an assistant with a tool. */
3956
+ const KITS = [
3957
+ "consistency",
3958
+ "fidelity",
3959
+ "quality",
3960
+ "playability"
3961
+ ];
3962
+ const SEVERITIES = [
3963
+ "error",
3964
+ "warning",
3965
+ "info"
3966
+ ];
3967
+ /** The commands of an authoring chat that start checks, and the kits each runs. */
3968
+ const CHECK_COMMANDS = {
3969
+ check: KITS,
3970
+ "check-all": KITS,
3971
+ "check-world": ["consistency"],
3972
+ "check-card": ["fidelity"],
3973
+ "check-quality": ["quality"],
3974
+ "check-play": ["playability"]
3975
+ };
3976
+ const findingKey = (finding) => `${finding.check}\n${finding.code}\n${finding.where}\n${finding.message}`;
3512
3977
  // src/host/checks/terms.ts
3513
3978
  const HAN = /[㐀-鿿]+/gu;
3514
3979
  /** Words of fewer letters are mostly the glue of a language, which says nothing about whether two texts share facts. */
@@ -3650,7 +4115,7 @@ const toolsOf = (value) => {
3650
4115
  const board = value === void 0 ? {} : parseSwitchboard(value, "tools");
3651
4116
  return typeof board === "string" ? {} : board;
3652
4117
  };
3653
- function infoOf(document) {
4118
+ function infoOf(document, startQuestions = []) {
3654
4119
  const opening = document["opening_message"];
3655
4120
  const parts = Array.isArray(opening) && opening.some((part) => typeof part === "object" && part !== null && part["type"] === "utterance");
3656
4121
  const plain = typeof opening === "string" ? opening : Array.isArray(opening) ? opening.map((part) => textOf(part?.["text"])).filter((text) => text !== "").join("\n\n") : "";
@@ -3673,6 +4138,7 @@ function infoOf(document) {
3673
4138
  version: typeof document["version"] === "number" ? String(document["version"]) : textOf(document["version"]),
3674
4139
  sampling: parseSampling(document["sampling"], "sampling", []),
3675
4140
  tools: toolsOf(document["tools"]),
4141
+ startQuestions,
3676
4142
  alternateOpenings: Array.isArray(document["alternate_openings"]) ? document["alternate_openings"].flatMap((entry) => typeof entry === "object" && entry !== null && typeof entry["text"] === "string" ? [{
3677
4143
  title: textOf(entry["title"]),
3678
4144
  text: entry["text"],
@@ -3687,8 +4153,14 @@ async function readDocument(directory) {
3687
4153
  document: JSON.parse(text.replace(/^/, ""))
3688
4154
  };
3689
4155
  }
4156
+ /** The questions the file asks, as its own reader understands them; none when the file does not parse (its problems are reported elsewhere). */
4157
+ function questionsOf(text) {
4158
+ const parsed = parseWorldFile(text);
4159
+ return parsed.ok ? parsed.value.startQuestions ?? [] : [];
4160
+ }
3690
4161
  async function readWorldInfo(directory) {
3691
- return infoOf((await readDocument(directory)).document);
4162
+ const { text, document } = await readDocument(directory);
4163
+ return infoOf(document, questionsOf(text));
3692
4164
  }
3693
4165
  function setOrRemove(document, key, value) {
3694
4166
  if (value === "") delete document[key];
@@ -3705,6 +4177,10 @@ function editWorldInfo(directory, edit) {
3705
4177
  async function changeWorldInfo(directory, edit) {
3706
4178
  const { text, document } = await readDocument(directory);
3707
4179
  const before = infoOf(document);
4180
+ if (edit.startQuestions !== void 0) {
4181
+ if (edit.startQuestions.length === 0) delete document["start_questions"];
4182
+ else document["start_questions"] = startQuestionsToFile(edit.startQuestions);
4183
+ }
3708
4184
  if (edit.opening !== void 0 && before.openingParts && edit.opening !== before.opening) throw new InvalidEditProblems(["the opening has lines spoken by characters: change it in the file, or ask the authoring assistant"]);
3709
4185
  if (edit.name !== void 0) document["name"] = edit.name;
3710
4186
  if (edit.description !== void 0) document["description"] = edit.description;
@@ -3757,7 +4233,7 @@ async function changeWorldInfo(directory, edit) {
3757
4233
  const parsed = parseWorldFile(next);
3758
4234
  if (!parsed.ok) throw new InvalidEditProblems(parsed.issues.map(formatIssue));
3759
4235
  await atomicWrite(join(directory, WORLD_FILE_NAME), next);
3760
- return infoOf(document);
4236
+ return infoOf(document, questionsOf(next));
3761
4237
  }
3762
4238
  // src/host/checks/world-text.ts
3763
4239
  /** Every string a document holds, joined: what the world says in it, without the structure around it. */
@@ -4311,6 +4787,20 @@ function worldFieldChanges(before, after) {
4311
4787
  });
4312
4788
  return changes;
4313
4789
  }
4790
+ /** What a newer card does to the card's own picture: the hashes of the two files, or nothing for a card with none. */
4791
+ function pictureChange(before, after) {
4792
+ if (before === void 0) return after === void 0 ? "none" : "added";
4793
+ return after === void 0 ? "removed" : before === after ? "unchanged" : "changed";
4794
+ }
4795
+ /** The picture addresses a newer card points to that the older did not, and those it no longer points to. */
4796
+ function addressChanges(before, after) {
4797
+ const was = new Set(before.urls ?? []);
4798
+ const now = new Set(after.urls ?? []);
4799
+ return {
4800
+ added: [...now].filter((url) => !was.has(url)),
4801
+ dropped: [...was].filter((url) => !now.has(url))
4802
+ };
4803
+ }
4314
4804
  // src/host/checks/judge.ts
4315
4805
  /**
4316
4806
  * 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 +5346,40 @@ function comparisonProblem(value, source, document) {
4856
5346
  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
5347
  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
5348
  }
5349
+ const STATED_SCHEMA = {
5350
+ type: "object",
5351
+ properties: {
5352
+ reason: { type: "string" },
5353
+ stated: { type: "boolean" }
5354
+ },
5355
+ required: ["reason", "stated"],
5356
+ additionalProperties: false
5357
+ };
5358
+ /**
5359
+ * 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
5360
+ * languages it calls a fact missing that the other text does say.
5361
+ */
5362
+ async function unstated(model, facts, text, subject) {
5363
+ const instructions = model.text("checks/decide-stated");
5364
+ const kept = [];
5365
+ for (const entry of facts) {
5366
+ if (model.cancelled?.() === true) break;
5367
+ const state = {
5368
+ ...subject === void 0 ? {} : { subject },
5369
+ fact: entry.fact,
5370
+ text: text.slice(0, SOURCE_CHARS * 2)
5371
+ };
5372
+ if (!(await verdictOf(model, state, "stated", instructions, async () => {
5373
+ return (await model.run({
5374
+ persona: instructions,
5375
+ prompt: `${subject === void 0 ? "" : `${subject}\n\n`}${entry.fact}\n\n${state.text}`,
5376
+ result: STATED_SCHEMA,
5377
+ maxTokens: 500
5378
+ })).stated;
5379
+ })).yes) kept.push(entry);
5380
+ }
5381
+ return kept;
5382
+ }
4859
5383
  const fidelityChecks = [
4860
5384
  coverage,
4861
5385
  names,
@@ -4908,14 +5432,16 @@ const fidelityChecks = [
4908
5432
  check: (value) => comparisonProblem(value, text, document)
4909
5433
  });
4910
5434
  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({
5435
+ const dropped = await unstated(model, once(answer.dropped), document, subject);
5436
+ const invented = await unstated(model, once(answer.invented), text);
5437
+ for (const entry of dropped) found.push({
4912
5438
  severity: "warning",
4913
5439
  code: "fact-dropped",
4914
5440
  where,
4915
5441
  message: `the card says “${snippet(entry.quote)}” (${entry.fact}), which ${where} does not say`,
4916
5442
  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
5443
  });
4918
- for (const entry of once(answer.invented)) found.push({
5444
+ for (const entry of invented) found.push({
4919
5445
  severity: "warning",
4920
5446
  code: "fact-invented",
4921
5447
  where,
@@ -5832,13 +6358,27 @@ async function build(ctx, world) {
5832
6358
  const style = await extractStyle(ctx, card, items, classified);
5833
6359
  extracted.prompts.push(...style.prompts);
5834
6360
  extracted.notes.push(...style.notes);
6361
+ extracted.derived.push(...style.derived);
5835
6362
  const tracked = await extractVariables(ctx, card, items, classified, roster);
5836
6363
  extracted.variables.push(...tracked.variables);
5837
6364
  extracted.notes.push(...tracked.notes);
6365
+ extracted.derived.push(...tracked.derived);
5838
6366
  const cast = await extractCasting(ctx, card, extracted.casting);
5839
6367
  extracted.commands.push(...cast.commands);
5840
6368
  extracted.triggers.push(...cast.triggers);
5841
6369
  extracted.notes.push(...cast.notes);
6370
+ const castFrom = extracted.derived.filter((entry) => entry.kind === "command" && entry.name === "").flatMap((entry) => entry.pieces);
6371
+ extracted.derived = extracted.derived.filter((entry) => !(entry.kind === "command" && entry.name === ""));
6372
+ const named = (document) => typeof document["name"] === "string" && document["name"] !== "" ? document["name"] : typeof document["id"] === "string" ? document["id"] : typeof document["key"] === "string" ? document["key"] : "unnamed";
6373
+ extracted.derived.push(...cast.commands.map((document) => ({
6374
+ kind: "command",
6375
+ name: named(document),
6376
+ pieces: castFrom
6377
+ })), ...cast.triggers.map((document) => ({
6378
+ kind: "trigger",
6379
+ name: named(document),
6380
+ pieces: castFrom
6381
+ })));
5842
6382
  if (ctx.cancelled?.() === true) throw new Error("the build was stopped before it was done, so what it made is not kept");
5843
6383
  await rename(join(folder(world), EXTRACTED), join(folder(world), PREVIOUS_EXTRACTED)).catch((error) => {
5844
6384
  if (error.code !== "ENOENT") throw error;
@@ -5948,6 +6488,40 @@ async function structureImport(world, language) {
5948
6488
  ...result.draft === void 0 ? {} : { draft: result.draft }
5949
6489
  };
5950
6490
  }
6491
+ // src/host/import/job-cache.ts
6492
+ const FOLDER = "jobs";
6493
+ const folderOf = (directory) => join(directory, IMPORT_FOLDER, FOLDER);
6494
+ /** A job is the same job when its instructions, its question and the shape of its answer are the same. */
6495
+ const keyOf = (job) => createHash("sha256").update(JSON.stringify([
6496
+ job.persona,
6497
+ job.prompt,
6498
+ job.result
6499
+ ])).digest("hex");
6500
+ /**
6501
+ * 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
6502
+ * job's own check refuses now is asked for again.
6503
+ */
6504
+ function cachedJobs(directory, run) {
6505
+ return async (job) => {
6506
+ const file = join(folderOf(directory), `${keyOf(job)}.json`);
6507
+ try {
6508
+ const kept = JSON.parse(await readFile(file, "utf8"));
6509
+ if (job.check?.(kept) === void 0) return kept;
6510
+ } catch (error) {
6511
+ if (error.code !== "ENOENT" && !(error instanceof SyntaxError)) throw error;
6512
+ }
6513
+ const answer = await run(job);
6514
+ await atomicWrite(file, JSON.stringify(answer), { createDirectory: true });
6515
+ return answer;
6516
+ };
6517
+ }
6518
+ /** Forgets the kept answers: a new survey starts the work over, and a finished import has no use for them. */
6519
+ async function forgetJobs(directory) {
6520
+ await rm(folderOf(directory), {
6521
+ recursive: true,
6522
+ force: true
6523
+ });
6524
+ }
5951
6525
  // src/host/import/tools.ts
5952
6526
  const SURVEY_TOOL = "import_survey";
5953
6527
  const BUILD_TOOL = "import_build";
@@ -5994,10 +6568,10 @@ function modelKeyOf(agent, named) {
5994
6568
  return model === void 0 ? "" : `${model.provider}/${model.model}`;
5995
6569
  }
5996
6570
  /** 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) {
6571
+ function contextOf(exec, settings, text, language, keepIn) {
5998
6572
  const agent = exec.agent;
5999
6573
  if (agent === void 0) throw new Error("the import tools can only be called by an agent");
6000
- const run = async (job) => {
6574
+ const ask = async (job) => {
6001
6575
  for (let attempt = 1;; attempt += 1) {
6002
6576
  const named = namedModel(settings.model);
6003
6577
  const model = modelKeyOf(agent, named.model);
@@ -6031,6 +6605,7 @@ function contextOf(exec, settings, text, language) {
6031
6605
  }
6032
6606
  }
6033
6607
  };
6608
+ const run = keepIn === void 0 ? ask : cachedJobs(keepIn, ask);
6034
6609
  const decide = settings.decisions === "decision" ? (state, asks) => {
6035
6610
  const service = agent.ctx.get("decisions");
6036
6611
  if (service === void 0) return Promise.reject(/* @__PURE__ */ new Error("no decision service is selected, or the selected one is not loaded"));
@@ -6083,14 +6658,16 @@ const toolNames = {
6083
6658
  function importTools(worlds, settings, prompting) {
6084
6659
  const builds = /* @__PURE__ */ new Map();
6085
6660
  /** 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) => {
6661
+ const step = async (exec, work, next, fresh = false) => {
6087
6662
  const world = await worldOf(worlds, exec);
6663
+ if (fresh) await forgetJobs(world.directory);
6088
6664
  const language = prompting.languages().get(world.id);
6089
6665
  const text = (id, variables) => prompting.prompts().text(id, language, {
6090
6666
  ...toolNames,
6091
6667
  ...variables
6092
6668
  });
6093
- const result = await work(contextOf(exec, settings(), text, language), world.directory);
6669
+ const result = await work(contextOf(exec, settings(), text, language, world.directory), world.directory);
6670
+ if (result.ok === true) await forgetJobs(world.directory);
6094
6671
  return json({
6095
6672
  ...result,
6096
6673
  next: next(text, result)
@@ -6105,7 +6682,7 @@ function importTools(worlds, settings, prompting) {
6105
6682
  isConcurrencySafe: () => false,
6106
6683
  async execute(args, exec) {
6107
6684
  checkedCard(args);
6108
- return await step(exec, survey, (text) => text("import/next-survey"));
6685
+ return await step(exec, survey, (text) => text("import/next-survey"), true);
6109
6686
  }
6110
6687
  }),
6111
6688
  defineTool({
@@ -6287,6 +6864,6 @@ function limitAuthoringAgents(ctx, kindOf) {
6287
6864
  return await next();
6288
6865
  });
6289
6866
  }
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 };
6867
+ export { readSource as $, subfolders as $t, editsSince as A, populateLore as At, addressesToFetch as B, loadLocations as Bt, worldFieldChanges as C, populateCharacters as Ct, loadCardEvidence as D, populateItems as Dt, loadWorldParts as E, populateFactions as Et, localChanges as F, TRIGGERS_FILE_NAME as Ft, vetUrl as G, DICE_FILE_NAME as Gt, fetchPictures as H, loadItems as Ht, readOrigin as I, loadTriggers as It, UPDATE_FOLDER as J, CALENDAR_FILE_NAME as Jt, cardCharacter as K, loadDice as Kt, writeOrigin as L, LORE_DIR as Lt, describeChanges as M, populateTriggers as Mt, hashWorld as N, populateVariables as Nt, CHECK_COMMANDS as O, populateKnowledge as Ot, isUnchanged as P, routineProblems as Pt, entitiesByPiece as Q, loadCharacters as Qt, CatalogueError as R, loadLore as Rt, pictureChange as S, populateCalendar as St, readWorldInfo as T, populateDice as Tt, guardedLookup as U, FACTIONS_DIR as Ut, candidateUrls as V, ITEMS_DIR as Vt, mergeReports as W, loadFactions as Wt, cardPieces as X, parseCalendarFile as Xt, baseId as Y, loadCalendar as Yt, cardTools as Z, CHARACTERS_DIR as Zt, runChecks as _, writeLorebookEntry as _t, importHooks as a, readCard as an, addLorebookEntries as at, diffText as b, withScratchStory as bt, importPhase as c, DICE_RESULTS as cn, deleteLorebookEntry as ct, markReady as d, listLorebooks as dt, knownOutputCeiling as en, WORLD_FILE_NAME as et, settleDraft as f, loadLorebook as ft, CHECKS as g, updateLorebookInfo as gt, presentReport as h, reserveLorebook as ht, contextOf as i, pictureOf as in, questionView as it, writeSnapshot as j, populateRoutines as jt, recordProvenance as k, populateLocations as kt, isReviewing as l, authoredWorldOf as ln, entriesOf as lt, checkImported as m, readLorebook as mt, limitAuthoringAgents as n, cardFormat as nn, parseWorldInfoEdit as nt, importTools as o, DICE_COMPARISONS as on, createLorebook as ot, structureImport as p, lorebookDirectory as pt, writePortrait as q, parseDiceFile as qt, MAX_PREVIEW_TEXT as r, isPng as rn, combineAnswers as rt, worldOf as s, DICE_READINGS as sn, deleteLorebook as st, AuthoringTools as t, ImportCardError as tn, parseWorldFile as tt, keepDraft as u, authoringChatSessionId as un, isLorebookName as ut, addressChanges as v, checkWorld as vt, editWorldInfo as w, populateClock as wt, matchPieces as x, knowsCharacter as xt, cardChanges as y, loadValidatedWorld as yt, addressKey as z, LOCATIONS_DIR as zt };
6291
6868
 
6292
- //# sourceMappingURL=authoring-tools-B4mEdKp_.js.map
6869
+ //# sourceMappingURL=authoring-tools-C9cmcUbD.js.map