@dsh-agentic-roleplay/core 0.9.0 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/lib/{authoring-tools-CdHlvx32.js → authoring-tools-C_l07RYn.js} +1168 -509
  2. package/lib/authoring.js +22 -18
  3. package/lib/client.js +338 -338
  4. package/lib/doctor.js +1 -1
  5. package/lib/{gallery-BKh7eu80.js → gallery-q7niLtWn.js} +2 -2
  6. package/lib/{generator-DdZ1-sgv.js → generator-BpjPrvHe.js} +2 -2
  7. package/lib/{hooks-CcQj_dJo.js → hooks-DNaDzMjW.js} +37 -4
  8. package/lib/images.js +4 -4
  9. package/lib/index.js +12 -12
  10. package/lib/{library-QFL6Z_Nr.js → library-CIkxhf_R.js} +4 -2
  11. package/lib/{lorebook-author-tools-DWBnYo1E.js → lorebook-author-tools-BiMDmdFi.js} +5 -5
  12. package/lib/lorebook-authoring.js +4 -4
  13. package/lib/orchestration.js +7 -7
  14. package/lib/{pack-jail-BMy6NcCY.js → pack-jail-B35Vx3hT.js} +23 -13
  15. package/lib/{packs-BREPSfHy.js → packs-BajKw7S9.js} +2 -2
  16. package/lib/{presenter-agent-B49-_iUm.js → presenter-agent-jgtuTwZr.js} +4 -4
  17. package/lib/{preset-BqgY1Tz4.js → preset-DWgkcPau.js} +3 -3
  18. package/lib/{preset-author-tools-BB_Dcv2o.js → preset-author-tools-BhuYIKms.js} +5 -5
  19. package/lib/preset-authoring.js +5 -5
  20. package/lib/{presets-Bwnrx7p0.js → presets-9GzaSELc.js} +13 -4
  21. package/lib/{reflection-CUWG-6Qg.js → reflection-4L0Plvct.js} +5 -5
  22. package/lib/{turn-guard-Bb8Ew-1z.js → turn-guard-sCUXmp-5.js} +2 -2
  23. package/package.json +2 -2
  24. package/prompts/en/checks/conflict.md +1 -0
  25. package/prompts/en/checks/decide-conflict.md +1 -0
  26. package/prompts/en/checks/decide-covered.md +1 -0
  27. package/prompts/en/checks/decide-opening.md +1 -0
  28. package/prompts/en/checks/decide-same-thing.md +1 -0
  29. package/prompts/en/checks/decide-same-value.md +1 -0
  30. package/prompts/en/checks/notice.md +2 -2
  31. package/prompts/zh/checks/conflict.md +1 -0
  32. package/prompts/zh/checks/decide-conflict.md +1 -0
  33. package/prompts/zh/checks/decide-covered.md +1 -0
  34. package/prompts/zh/checks/decide-opening.md +1 -0
  35. package/prompts/zh/checks/decide-same-thing.md +1 -0
  36. package/prompts/zh/checks/decide-same-value.md +1 -0
  37. package/prompts/zh/checks/notice.md +2 -2
@@ -1,10 +1,10 @@
1
1
  import "./access-CfQWbNZP.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-Bwnrx7p0.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-9GzaSELc.js";
3
3
  import { l as atomicWrite, m as parseSwitchboard, u as atomicWriteJson } from "./narration-CjB_KwwY.js";
4
- import { $ as copyWorldPrompts, B as loadPlots, H as plotReferenceProblems, J as formatIssue, K as readDocument$1, W as commandFlagKey, Y as errorMessage, Z as PROMPTS_FOLDER, a as externalWithheld, ht as COMPARISONS, it as shippedLanguages, lt as InvalidWorldError, nt as listFiles, ot as shippedText, q as Reader, r as externalGiven, rt as promptProblem, t as externalToolLimit } from "./hooks-CcQj_dJo.js";
4
+ import { $ as copyWorldPrompts, B as loadPlots, H as plotReferenceProblems, J as formatIssue, K as readDocument$1, W as commandFlagKey, Y as errorMessage, Z as PROMPTS_FOLDER, a as externalWithheld, ht as COMPARISONS, it as shippedLanguages, lt as InvalidWorldError, nt as listFiles, ot as shippedText, q as Reader, r as externalGiven, rt as promptProblem, t as externalToolLimit } from "./hooks-DNaDzMjW.js";
5
5
  import { t as trimCharacters } from "./text-CbGQhK1-.js";
6
6
  import { c as runChild, h as currentTurn, s as namedModel, t as ChildTimeoutError, u as parseSampling } from "./child-lYYeOwl2.js";
7
- import { C as trOf, S as slug, _ as writeImageFileMetadata, b as mapLimit, i as libraryHashes, n as addToLibraryOnce, p as MEDIA_TYPES, r as imageExtensionOf, u as referenceImagePath, w as uniqueKey, x as norm } from "./library-QFL6Z_Nr.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-CIkxhf_R.js";
8
8
  import { dirname, join, relative } from "node:path";
9
9
  import { existsSync, readFileSync } from "node:fs";
10
10
  import { createHash } from "node:crypto";
@@ -3046,19 +3046,19 @@ async function editsSince(directory) {
3046
3046
  };
3047
3047
  }
3048
3048
  // src/host/import/provenance.ts
3049
- const FILE$1 = "provenance.json";
3049
+ const FILE$2 = "provenance.json";
3050
3050
  const PROVENANCE_SCHEMA = "dsh-roleplay-import/v1";
3051
3051
  /** The import record of a world, or nothing for a world imported before records were kept. */
3052
3052
  async function readProvenance(directory) {
3053
3053
  let text;
3054
3054
  try {
3055
- text = await readFile(join(directory, IMPORT_FOLDER, FILE$1), "utf8");
3055
+ text = await readFile(join(directory, IMPORT_FOLDER, FILE$2), "utf8");
3056
3056
  } catch (error) {
3057
3057
  if (error.code === "ENOENT") return void 0;
3058
3058
  throw error;
3059
3059
  }
3060
3060
  const value = JSON.parse(text);
3061
- if (value.schema !== "dsh-roleplay-import/v1" || !Array.isArray(value.history)) throw new Error(`${FILE$1} is not an import record this version reads`);
3061
+ if (value.schema !== "dsh-roleplay-import/v1" || !Array.isArray(value.history)) throw new Error(`${FILE$2} is not an import record this version reads`);
3062
3062
  return value;
3063
3063
  }
3064
3064
  /** Adds one line to the world's import record: what card it came from and, for an import, what was written. */
@@ -3072,7 +3072,7 @@ async function recordProvenance(directory, entry, cardJson) {
3072
3072
  sha256: createHash("sha256").update(cardJson).digest("hex")
3073
3073
  }
3074
3074
  };
3075
- await atomicWriteJson(join(directory, IMPORT_FOLDER, FILE$1), {
3075
+ await atomicWriteJson(join(directory, IMPORT_FOLDER, FILE$2), {
3076
3076
  schema: PROVENANCE_SCHEMA,
3077
3077
  history: [...earlier?.history ?? [], line]
3078
3078
  });
@@ -3082,7 +3082,8 @@ async function recordProvenance(directory, entry, cardJson) {
3082
3082
  const KITS = [
3083
3083
  "consistency",
3084
3084
  "fidelity",
3085
- "quality"
3085
+ "quality",
3086
+ "playability"
3086
3087
  ];
3087
3088
  const SEVERITIES = [
3088
3089
  "error",
@@ -3091,19 +3092,12 @@ const SEVERITIES = [
3091
3092
  ];
3092
3093
  /** The commands of an authoring chat that start checks, and the kits each runs. */
3093
3094
  const CHECK_COMMANDS = {
3094
- check: [
3095
- "consistency",
3096
- "fidelity",
3097
- "quality"
3098
- ],
3099
- "check-all": [
3100
- "consistency",
3101
- "fidelity",
3102
- "quality"
3103
- ],
3095
+ check: KITS,
3096
+ "check-all": KITS,
3104
3097
  "check-world": ["consistency"],
3105
3098
  "check-card": ["fidelity"],
3106
- "check-quality": ["quality"]
3099
+ "check-quality": ["quality"],
3100
+ "check-play": ["playability"]
3107
3101
  };
3108
3102
  const findingKey = (finding) => `${finding.check}\n${finding.code}\n${finding.where}\n${finding.message}`;
3109
3103
  // src/host/import/card-tools.ts
@@ -3454,12 +3448,11 @@ function entitiesByPiece(source) {
3454
3448
  return made;
3455
3449
  }
3456
3450
  /** 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. */
3457
- function originOf(card, choices, pieces, records, name) {
3451
+ function originOf(card, choices, pieces, records, name, items = itemise(card, choices)) {
3458
3452
  if (records.roster === void 0 || records.classified === void 0) return [];
3459
3453
  const entity = findEntity(records.roster, name);
3460
3454
  if (entity === void 0) return [];
3461
3455
  const names = [entity.name, ...entity.aliases].map(norm);
3462
- const items = itemise(card, choices);
3463
3456
  const chosen = sourcesFor(entity, items, records.classified);
3464
3457
  const about = (id) => (records.classified?.[id]?.targets ?? []).some((target) => target.kind === entity.kind && names.includes(norm(target.name)));
3465
3458
  const seen = /* @__PURE__ */ new Set();
@@ -3499,6 +3492,8 @@ function shareOf(of, within) {
3499
3492
  }
3500
3493
  /** A text as names are looked up in it: width- and case-folded. */
3501
3494
  const folded = (text) => text.normalize("NFKC").toLowerCase();
3495
+ /** A text as a quote of it is looked up: folded, with runs of white space as one space. */
3496
+ const squashed = (text) => folded(text).replace(/\s+/gu, " ").trim();
3502
3497
  // src/host/checks/card-evidence.ts
3503
3498
  const exists = async (file) => await stat(file).then(() => true, () => false);
3504
3499
  /** @returns the card of the world, or nothing when the world has none (or no newer version waits, for `new`). */
@@ -3544,20 +3539,31 @@ function madeInto(source, world, only = "any") {
3544
3539
  faction: "factions",
3545
3540
  thing: "items"
3546
3541
  };
3542
+ const items = itemise(source.card, source.choices);
3547
3543
  for (const entity of source.records.roster ?? []) {
3548
3544
  const where = `${group[entity.kind]}/${entity.key}`;
3549
3545
  if (!world.texts.has(where)) continue;
3550
- for (const entry of originOf(source.card, source.choices, source.pieces, source.records, entity.name)) if (only === "any" || entry.how === "about it") add(entry.piece, where);
3546
+ for (const entry of originOf(source.card, source.choices, source.pieces, source.records, entity.name, items)) if (only === "any" || entry.how === "about it") add(entry.piece, where);
3551
3547
  }
3552
3548
  const pieces = usedPieces(source).map((piece) => ({
3553
3549
  id: baseId(piece.id),
3554
3550
  terms: termsOf(piece.text)
3555
3551
  }));
3552
+ const holders = /* @__PURE__ */ new Map();
3553
+ pieces.forEach((piece, at) => {
3554
+ for (const term of piece.terms) {
3555
+ const list = holders.get(term);
3556
+ if (list === void 0) holders.set(term, [at]);
3557
+ else list.push(at);
3558
+ }
3559
+ });
3556
3560
  for (const [where, text] of world.texts) {
3557
3561
  if (!where.startsWith(`${ENTITY_FOLDERS[4].directory}/`) && !where.startsWith(`world.json#`)) continue;
3558
3562
  const terms = termsOf(text);
3559
3563
  if (terms.size < MIN_TERMS) continue;
3560
- for (const piece of pieces) if (shareOf(terms, piece.terms) >= MADE_FROM) add(piece.id, where);
3564
+ const shared = /* @__PURE__ */ new Map();
3565
+ for (const term of terms) for (const at of holders.get(term) ?? []) shared.set(at, (shared.get(at) ?? 0) + 1);
3566
+ for (const [at, count] of shared) if (count / terms.size >= MADE_FROM) add(pieces[at].id, where);
3561
3567
  }
3562
3568
  return made;
3563
3569
  }
@@ -3628,6 +3634,248 @@ function withScratchStory(parts, use) {
3628
3634
  scratch.close();
3629
3635
  }
3630
3636
  }
3637
+ // src/host/worlds/world-info.ts
3638
+ const textOf = (value) => typeof value === "string" ? value : "";
3639
+ const toolsOf = (value) => {
3640
+ const board = value === void 0 ? {} : parseSwitchboard(value, "tools");
3641
+ return typeof board === "string" ? {} : board;
3642
+ };
3643
+ function infoOf(document) {
3644
+ const opening = document["opening_message"];
3645
+ const parts = Array.isArray(opening) && opening.some((part) => typeof part === "object" && part !== null && part["type"] === "utterance");
3646
+ const plain = typeof opening === "string" ? opening : Array.isArray(opening) ? opening.map((part) => textOf(part?.["text"])).filter((text) => text !== "").join("\n\n") : "";
3647
+ const metadata = typeof document["metadata"] === "object" && document["metadata"] !== null ? document["metadata"] : {};
3648
+ const author = typeof document["author"] === "object" && document["author"] !== null ? document["author"] : {};
3649
+ const rules = document["rules"];
3650
+ return {
3651
+ name: textOf(document["name"]),
3652
+ description: textOf(document["description"]),
3653
+ tags: Array.isArray(metadata["tags"]) ? metadata["tags"].filter((tag) => typeof tag === "string") : [],
3654
+ rules: typeof rules === "string" ? [rules] : Array.isArray(rules) ? rules.filter((rule) => typeof rule === "string") : [],
3655
+ opening: plain,
3656
+ openingParts: parts,
3657
+ authorName: textOf(author["name"]),
3658
+ authorUrl: textOf(author["url"]),
3659
+ timeZone: textOf(document["time_zone"]),
3660
+ startingTime: textOf(document["starting_time"]),
3661
+ preset: textOf(document["preset"]),
3662
+ lorebooks: Array.isArray(document["lorebooks"]) ? document["lorebooks"].filter((name) => typeof name === "string") : [],
3663
+ version: typeof document["version"] === "number" ? String(document["version"]) : textOf(document["version"]),
3664
+ sampling: parseSampling(document["sampling"], "sampling", []),
3665
+ tools: toolsOf(document["tools"]),
3666
+ alternateOpenings: Array.isArray(document["alternate_openings"]) ? document["alternate_openings"].flatMap((entry) => typeof entry === "object" && entry !== null && typeof entry["text"] === "string" ? [{
3667
+ title: textOf(entry["title"]),
3668
+ text: entry["text"],
3669
+ ...textOf(entry["character"]) === "" ? {} : { character: textOf(entry["character"]) }
3670
+ }] : []) : []
3671
+ };
3672
+ }
3673
+ async function readDocument(directory) {
3674
+ const text = await readFile(join(directory, WORLD_FILE_NAME), "utf8");
3675
+ return {
3676
+ text,
3677
+ document: JSON.parse(text.replace(/^/, ""))
3678
+ };
3679
+ }
3680
+ async function readWorldInfo(directory) {
3681
+ return infoOf((await readDocument(directory)).document);
3682
+ }
3683
+ function setOrRemove(document, key, value) {
3684
+ if (value === "") delete document[key];
3685
+ else document[key] = value;
3686
+ }
3687
+ /**
3688
+ * Changes the fields of `world.json` the edit names and nothing else, keeping the file's other fields and its indentation. The result is read
3689
+ * back as the world will be read before it is written.
3690
+ * @throws {InvalidEditProblems} when the world would no longer be valid.
3691
+ */
3692
+ function editWorldInfo(directory, edit) {
3693
+ return serialized(directory, () => changeWorldInfo(directory, edit));
3694
+ }
3695
+ async function changeWorldInfo(directory, edit) {
3696
+ const { text, document } = await readDocument(directory);
3697
+ const before = infoOf(document);
3698
+ 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"]);
3699
+ if (edit.name !== void 0) document["name"] = edit.name;
3700
+ if (edit.description !== void 0) document["description"] = edit.description;
3701
+ if (edit.tags !== void 0) document["metadata"] = {
3702
+ ...typeof document["metadata"] === "object" && document["metadata"] !== null ? document["metadata"] : {},
3703
+ tags: edit.tags
3704
+ };
3705
+ if (edit.rules !== void 0) {
3706
+ if (edit.rules.length === 0) delete document["rules"];
3707
+ else document["rules"] = edit.rules;
3708
+ }
3709
+ if (edit.opening !== void 0 && !before.openingParts) setOrRemove(document, "opening_message", edit.opening);
3710
+ if (edit.authorName !== void 0 || edit.authorUrl !== void 0) {
3711
+ const name = edit.authorName ?? before.authorName;
3712
+ const url = edit.authorUrl ?? before.authorUrl;
3713
+ if (name === "" && url !== "") throw new InvalidEditProblems(["an author link needs the author's name"]);
3714
+ if (name === "") delete document["author"];
3715
+ else document["author"] = {
3716
+ ...typeof document["author"] === "object" && document["author"] !== null ? document["author"] : {},
3717
+ name,
3718
+ ...url === "" ? {} : { url }
3719
+ };
3720
+ if (url === "" && typeof document["author"] === "object") delete document["author"]["url"];
3721
+ }
3722
+ if (edit.timeZone !== void 0) setOrRemove(document, "time_zone", edit.timeZone);
3723
+ if (edit.startingTime !== void 0) setOrRemove(document, "starting_time", edit.startingTime);
3724
+ if (edit.version !== void 0) setOrRemove(document, "version", edit.version);
3725
+ if (edit.preset !== void 0) setOrRemove(document, "preset", edit.preset);
3726
+ if (edit.lorebooks !== void 0) {
3727
+ if (edit.lorebooks.length === 0) delete document["lorebooks"];
3728
+ else document["lorebooks"] = edit.lorebooks;
3729
+ }
3730
+ if (edit.alternateOpenings !== void 0) {
3731
+ if (edit.alternateOpenings.length === 0) delete document["alternate_openings"];
3732
+ else document["alternate_openings"] = edit.alternateOpenings.map((entry) => ({
3733
+ title: entry.title === "" ? entry.text.slice(0, 40) : entry.title,
3734
+ text: entry.text,
3735
+ ...entry.character === void 0 || entry.character === "" ? {} : { character: entry.character }
3736
+ }));
3737
+ }
3738
+ if (edit.sampling !== void 0) {
3739
+ if (Object.keys(edit.sampling).length === 0) delete document["sampling"];
3740
+ else document["sampling"] = edit.sampling;
3741
+ }
3742
+ if (edit.tools !== void 0) {
3743
+ if (Object.keys(edit.tools).length === 0) delete document["tools"];
3744
+ else document["tools"] = edit.tools;
3745
+ }
3746
+ const next = JSON.stringify(document, null, /^([ \t]+)"/m.exec(text)?.[1] ?? " ") + (text.endsWith("\n") ? "\n" : "");
3747
+ const parsed = parseWorldFile(next);
3748
+ if (!parsed.ok) throw new InvalidEditProblems(parsed.issues.map(formatIssue));
3749
+ await atomicWrite(join(directory, WORLD_FILE_NAME), next);
3750
+ return infoOf(document);
3751
+ }
3752
+ // src/host/checks/world-text.ts
3753
+ /** Every string a document holds, joined: what the world says in it, without the structure around it. */
3754
+ const stringsOf = (value) => typeof value === "string" ? [value] : Array.isArray(value) ? value.flatMap(stringsOf) : typeof value === "object" && value !== null ? Object.values(value).flatMap(stringsOf) : [];
3755
+ /** Fields that say what a document is or point at another document, not what the world says: a model comparing prose with a card is not shown them. */
3756
+ const STRUCTURE = /* @__PURE__ */ new Set([
3757
+ "id",
3758
+ "role",
3759
+ "tier",
3760
+ "kind",
3761
+ "scope",
3762
+ "type",
3763
+ "status",
3764
+ "control",
3765
+ "known_by",
3766
+ "related",
3767
+ "subjects",
3768
+ "sources",
3769
+ "to",
3770
+ "location",
3771
+ "parent",
3772
+ "held_by",
3773
+ "owner",
3774
+ "members",
3775
+ "knowledge",
3776
+ "relationships",
3777
+ "exits",
3778
+ "start",
3779
+ "visual_identity",
3780
+ "routine",
3781
+ "variables"
3782
+ ]);
3783
+ /** What a document says in words, one `field: text` line each, without the structure around it. */
3784
+ function proseOf(value, field = "") {
3785
+ if (typeof value === "string") return value.trim() === "" ? [] : [field === "" ? value : `${field}: ${value}`];
3786
+ if (Array.isArray(value)) return value.flatMap((entry) => proseOf(entry, field));
3787
+ if (typeof value === "object" && value !== null) return Object.entries(value).filter(([key]) => !STRUCTURE.has(key)).flatMap(([key, entry]) => proseOf(entry, key));
3788
+ return [];
3789
+ }
3790
+ async function readJson$1(file) {
3791
+ try {
3792
+ return JSON.parse(await readFile(file, "utf8"));
3793
+ } catch (error) {
3794
+ if (error.code === "ENOENT") return void 0;
3795
+ throw error;
3796
+ }
3797
+ }
3798
+ /** What the files of a world say, read afresh, for comparing with a card. A file that does not read is left out: the consistency kit names it. */
3799
+ async function readWorldText(directory) {
3800
+ const texts = /* @__PURE__ */ new Map();
3801
+ const prose = /* @__PURE__ */ new Map();
3802
+ const subjects = /* @__PURE__ */ new Map();
3803
+ const names = [];
3804
+ const KIND = {
3805
+ characters: "character",
3806
+ locations: "place",
3807
+ factions: "organisation",
3808
+ items: "thing",
3809
+ lore: "background entry"
3810
+ };
3811
+ for (const { directory: folder, file } of ENTITY_FOLDERS) for (const id of await subfolders(join(directory, folder))) {
3812
+ const document = await readJson$1(join(directory, folder, id, file)).catch(() => void 0);
3813
+ if (document === void 0 || typeof document !== "object") continue;
3814
+ texts.set(`${folder}/${id}`, stringsOf(document).join("\n"));
3815
+ prose.set(`${folder}/${id}`, proseOf(document).join("\n"));
3816
+ const label = document[folder === "lore" ? "title" : "name"];
3817
+ subjects.set(`${folder}/${id}`, `${KIND[folder]}${typeof label === "string" ? ` ${label}` : ""}`);
3818
+ if (folder !== "lore") {
3819
+ const own = [document["name"], ...Array.isArray(document["aliases"]) ? document["aliases"] : []].filter((name) => typeof name === "string" && name.trim() !== "");
3820
+ names.push({
3821
+ where: `${folder}/${id}`,
3822
+ kind: folder,
3823
+ names: own,
3824
+ playable: document["role"] === "playable"
3825
+ });
3826
+ }
3827
+ }
3828
+ const info = await readJson$1(join(directory, WORLD_FILE_NAME)).catch(() => void 0);
3829
+ if (typeof info === "object" && info !== null) for (const [field, value] of Object.entries(info)) {
3830
+ const text = stringsOf(value).join("\n");
3831
+ if (text.trim() !== "") {
3832
+ texts.set(`${WORLD_FILE_NAME}#${field}`, text);
3833
+ prose.set(`${WORLD_FILE_NAME}#${field}`, text);
3834
+ subjects.set(`${WORLD_FILE_NAME}#${field}`, `the world's ${field}`);
3835
+ }
3836
+ }
3837
+ const prompts = join(directory, PROMPTS_FOLDER);
3838
+ for (const language of await readdir(prompts).catch(() => [])) for (const id of listFiles(join(prompts, language))) texts.set(`${PROMPTS_FOLDER}/${language}/${id}`, await readFile(join(prompts, language, `${id}.md`), "utf8"));
3839
+ return {
3840
+ texts,
3841
+ prose,
3842
+ subjects,
3843
+ names
3844
+ };
3845
+ }
3846
+ // src/host/checks/loaded.ts
3847
+ function once(input, key, load) {
3848
+ if (input.memo === void 0) return load();
3849
+ let loaded = input.memo.get(key);
3850
+ if (loaded === void 0) {
3851
+ loaded = load();
3852
+ input.memo.set(key, loaded);
3853
+ }
3854
+ return loaded;
3855
+ }
3856
+ /** The parts of the world as the checks of one run read them. */
3857
+ const loaded = {
3858
+ validity: (input) => once(input, "validity", () => checkWorld(input.world)),
3859
+ characters: (input) => once(input, "characters", () => loadCharacters(input.world.directory)),
3860
+ locations: (input) => once(input, "locations", () => loadLocations(input.world.directory)),
3861
+ factions: (input) => once(input, "factions", () => loadFactions(input.world.directory)),
3862
+ lore: (input) => once(input, "lore", () => loadLore(input.world.directory)),
3863
+ variables: (input) => once(input, "variables", () => loadVariables(input.world.directory)),
3864
+ info: (input) => once(input, "info", () => readWorldInfo(input.world.directory)),
3865
+ text: (input) => once(input, "text", () => readWorldText(input.world.directory)),
3866
+ /** Which parts of the world each piece of the card the records are about was made into. */
3867
+ made: async (input, card, only = "any") => {
3868
+ const world = await loaded.text(input);
3869
+ return once(input, `made:${only}`, async () => madeInto(card.previous ?? card.source, world, only));
3870
+ },
3871
+ items: async (input) => {
3872
+ const [{ characters }, { locations }] = await Promise.all([loaded.characters(input), loaded.locations(input)]);
3873
+ return once(input, "items", () => loadItems(input.world.directory, {
3874
+ characters: characters.map((character) => character.id),
3875
+ locations
3876
+ }));
3877
+ }
3878
+ };
3631
3879
  // src/host/checks/consistency.ts
3632
3880
  /** Words that name one thing a story tracks; a world that declares two of one group tracks it twice. */
3633
3881
  const SYNONYMS = [
@@ -3666,11 +3914,10 @@ const SYNONYMS = [
3666
3914
  ["sanity", "san"],
3667
3915
  ["level", "lvl"]
3668
3916
  ];
3669
- const squash = (text) => text.normalize("NFKC").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, "");
3670
3917
  /** The group of meaning a variable's key or name falls in: a synonym group, else the word itself. */
3671
3918
  const meanings = (def) => {
3672
3919
  const found = /* @__PURE__ */ new Set();
3673
- for (const word of [def.key, def.name].map(squash).filter((word) => word !== "")) {
3920
+ for (const word of [def.key, def.name].map(norm).filter((word) => word !== "")) {
3674
3921
  const group = SYNONYMS.findIndex((names) => names.includes(word));
3675
3922
  found.add(group < 0 ? word : `group:${group}`);
3676
3923
  }
@@ -3691,146 +3938,194 @@ function duplicateValues(defs) {
3691
3938
  }
3692
3939
  return pairs;
3693
3940
  }
3941
+ const FIELDS_LISTED = 6;
3942
+ /** What fixes a finding of the graph lint whose fix is the same every time. */
3943
+ const GRAPH_HINTS = {
3944
+ "character-unplaced": "Give the character a starting place in their own file (start.location); a relationship does not place anyone.",
3945
+ "location-no-exit": "Add a way (an exit) between this place and one the story can reach.",
3946
+ "location-unreachable": "Add a way from a place the story can reach to this one, or from the place the story starts in."
3947
+ };
3694
3948
  const asSkip = (error) => {
3695
3949
  if (error instanceof InvalidEditProblems) throw new CheckSkipped("other files of the world have problems that come first");
3696
3950
  throw error;
3697
3951
  };
3698
- const consistencyChecks = [
3699
- {
3700
- id: "world-valid",
3701
- kit: "consistency",
3702
- mode: "code",
3703
- needs: [],
3704
- description: "Every file of the world reads and everything that refers to something refers to something that is there, as a story start checks it (it names the problems of the first part of the world that has any).",
3705
- async run({ world }) {
3706
- const check = await checkWorld(world);
3707
- const found = check.problems.map((problem) => ({
3708
- severity: "error",
3709
- code: "invalid",
3710
- where: problem.where,
3711
- message: problem.message,
3712
- hint: "Fix it, then run the check again: the next part of the world may have problems of its own."
3713
- }));
3714
- if (check.more > 0) found.push({
3715
- severity: "error",
3716
- code: "invalid",
3717
- where: "(the world)",
3718
- message: `${check.more} more problems are not listed`
3719
- });
3720
- return foundBy("world-valid", found);
3721
- }
3722
- },
3723
- {
3724
- id: "world-graph",
3725
- kit: "consistency",
3726
- mode: "code",
3727
- needs: [],
3728
- description: "Places no one can reach or leave, ways with no way back, a lock whose key does not exist, two characters, places or things with one name, characters nobody knows.",
3729
- async run({ world }) {
3730
- if (!(await checkWorld(world)).ok) throw new CheckSkipped("the world has problems that come first (see world-valid)");
3731
- const parts = await loadWorldParts(world.directory).catch(asSkip);
3732
- return foundBy("world-graph", withScratchStory(parts, (story) => lintWorld(story).map((finding) => ({
3733
- severity: finding.severity,
3734
- code: finding.code,
3735
- where: `${finding.subject.kind}:${finding.subject.key}`,
3736
- message: finding.message
3737
- }))));
3738
- }
3739
- },
3740
- {
3741
- id: "duplicate-values",
3742
- kit: "consistency",
3743
- mode: "code",
3744
- needs: [],
3745
- description: "Tracked values that mean the same thing for the same subjects (health and hp), which the story would keep twice.",
3746
- async run({ world }) {
3747
- const { defs } = await loadVariables(world.directory);
3748
- return foundBy("duplicate-values", duplicateValues(defs).map(([a, b]) => ({
3749
- severity: "warning",
3750
- code: "duplicate-value",
3751
- where: "variables.json",
3752
- message: `${a.key} (${a.name}) and ${b.key} (${b.name}) look like the same tracked value for the same ${a.subject}s`,
3753
- hint: "Keep one and remove the other from variables.json and from the files that give it a starting value; which one is right is a choice."
3754
- })));
3755
- }
3756
- },
3757
- {
3758
- id: "name-clashes",
3759
- kit: "consistency",
3760
- mode: "code",
3761
- needs: [],
3762
- description: "A character's alias that is another character's name or alias, two organisations or two lore entries with one name.",
3763
- async run({ world }) {
3764
- const [{ characters }, { factions }, { lore }] = await Promise.all([
3765
- loadCharacters(world.directory),
3766
- loadFactions(world.directory),
3767
- loadLore(world.directory)
3768
- ]);
3769
- const found = [];
3770
- const owners = /* @__PURE__ */ new Map();
3771
- for (const character of characters) {
3772
- const seen = /* @__PURE__ */ new Set();
3773
- for (const label of [character.name, ...character.aliases]) {
3774
- const word = squash(label);
3775
- if (word === "" || seen.has(word)) continue;
3776
- seen.add(word);
3777
- const other = owners.get(word);
3778
- if (other !== void 0 && other !== character.id) found.push({
3779
- severity: "warning",
3780
- code: "alias-clash",
3781
- where: `characters/${character.id}`,
3782
- message: `"${label}" names both ${other} and ${character.id}, so a mention cannot be told apart`,
3783
- hint: "Give one of them a name or alias of its own."
3784
- });
3785
- else owners.set(word, character.id);
3786
- }
3952
+ const validity = {
3953
+ id: "world-valid",
3954
+ kit: "consistency",
3955
+ mode: "code",
3956
+ needs: [],
3957
+ description: "Every file of the world reads and everything that refers to something refers to something that is there, as a story start checks it (it names the problems of the first part of the world that has any).",
3958
+ async run(input) {
3959
+ const check = await loaded.validity(input);
3960
+ const found = check.problems.map((problem) => ({
3961
+ severity: "error",
3962
+ code: "invalid",
3963
+ where: problem.where,
3964
+ message: problem.message,
3965
+ hint: "Fix it, then run the check again: the next part of the world may have problems of its own."
3966
+ }));
3967
+ if (check.more > 0) found.push({
3968
+ severity: "error",
3969
+ code: "invalid",
3970
+ where: "(the world)",
3971
+ message: `${check.more} more problems are not listed`
3972
+ });
3973
+ return foundBy("world-valid", found);
3974
+ }
3975
+ };
3976
+ const graph = {
3977
+ id: "world-graph",
3978
+ kit: "consistency",
3979
+ mode: "code",
3980
+ needs: [],
3981
+ description: "Places no one can reach or leave, ways with no way back, a lock whose key does not exist, two characters, places or things with one name, characters nobody knows.",
3982
+ async run(input) {
3983
+ const { world } = input;
3984
+ if (!(await loaded.validity(input)).ok) throw new CheckSkipped("the world has problems that come first (see world-valid)");
3985
+ const parts = await loadWorldParts(world.directory).catch(asSkip);
3986
+ return foundBy("world-graph", withScratchStory(parts, (story) => lintWorld(story).map((finding) => ({
3987
+ severity: finding.severity,
3988
+ code: finding.code,
3989
+ where: `${finding.subject.kind}:${finding.subject.key}`,
3990
+ message: finding.message,
3991
+ ...GRAPH_HINTS[finding.code] === void 0 ? {} : { hint: GRAPH_HINTS[finding.code] }
3992
+ }))));
3993
+ }
3994
+ };
3995
+ const values = {
3996
+ id: "duplicate-values",
3997
+ kit: "consistency",
3998
+ mode: "code",
3999
+ needs: [],
4000
+ description: "Tracked values that mean the same thing for the same subjects (health and hp), which the story would keep twice.",
4001
+ async run(input) {
4002
+ const { defs } = await loaded.variables(input);
4003
+ return foundBy("duplicate-values", duplicateValues(defs).map(([a, b]) => ({
4004
+ severity: "warning",
4005
+ code: "duplicate-value",
4006
+ where: "variables.json",
4007
+ message: `${a.key} (${a.name}) and ${b.key} (${b.name}) look like the same tracked value for the same ${a.subject}s`,
4008
+ hint: "Keep one and remove the other from variables.json and from the files that give it a starting value; which one is right is a choice."
4009
+ })));
4010
+ }
4011
+ };
4012
+ const names$1 = {
4013
+ id: "name-clashes",
4014
+ kit: "consistency",
4015
+ mode: "code",
4016
+ needs: [],
4017
+ description: "A character's alias that is another character's name or alias, two organisations or two lore entries with one name.",
4018
+ async run(input) {
4019
+ const [{ characters }, { factions }, { lore }] = await Promise.all([
4020
+ loaded.characters(input),
4021
+ loaded.factions(input),
4022
+ loaded.lore(input)
4023
+ ]);
4024
+ const found = [];
4025
+ const owners = /* @__PURE__ */ new Map();
4026
+ for (const character of characters) {
4027
+ const seen = /* @__PURE__ */ new Set();
4028
+ for (const label of [character.name, ...character.aliases]) {
4029
+ const word = norm(label);
4030
+ if (word === "" || seen.has(word)) continue;
4031
+ seen.add(word);
4032
+ const other = owners.get(word);
4033
+ if (other !== void 0 && other !== character.id) found.push({
4034
+ severity: "warning",
4035
+ code: "alias-clash",
4036
+ where: `characters/${character.id}`,
4037
+ message: `"${label}" names both ${other} and ${character.id}, so a mention cannot be told apart`,
4038
+ hint: "Give one of them a name or alias of its own."
4039
+ });
4040
+ else owners.set(word, character.id);
3787
4041
  }
3788
- const twice = (kind, entries) => {
3789
- const seen = /* @__PURE__ */ new Map();
3790
- for (const entry of entries) {
3791
- const word = squash(entry.label);
3792
- const other = seen.get(word);
3793
- if (word !== "" && other !== void 0) found.push({
3794
- severity: "warning",
3795
- code: "name-twice",
3796
- where: `${kind}/${entry.id}`,
3797
- message: `${kind}/${other} and ${kind}/${entry.id} are both called "${entry.label}"`
3798
- });
3799
- else seen.set(word, entry.id);
3800
- }
3801
- };
3802
- twice("factions", factions.map((entry) => ({
3803
- id: entry.id,
3804
- label: entry.name
3805
- })));
3806
- twice("lore", lore.map((entry) => ({
3807
- id: entry.id,
3808
- label: entry.title
4042
+ }
4043
+ const twice = (kind, entries) => {
4044
+ const seen = /* @__PURE__ */ new Map();
4045
+ for (const entry of entries) {
4046
+ const word = norm(entry.label);
4047
+ const other = seen.get(word);
4048
+ if (word !== "" && other !== void 0) found.push({
4049
+ severity: "warning",
4050
+ code: "name-twice",
4051
+ where: `${kind}/${entry.id}`,
4052
+ message: `${kind}/${other} and ${kind}/${entry.id} are both called "${entry.label}"`
4053
+ });
4054
+ else seen.set(word, entry.id);
4055
+ }
4056
+ };
4057
+ twice("factions", factions.map((entry) => ({
4058
+ id: entry.id,
4059
+ label: entry.name
4060
+ })));
4061
+ twice("lore", lore.map((entry) => ({
4062
+ id: entry.id,
4063
+ label: entry.title
4064
+ })));
4065
+ return foundBy("name-clashes", found);
4066
+ }
4067
+ };
4068
+ const prompts = {
4069
+ id: "world-prompts",
4070
+ kit: "consistency",
4071
+ mode: "code",
4072
+ needs: [],
4073
+ description: "The prompts the world carries are prompts the product has, are not empty and use only the variables each is given: a world with a bad one cannot start a story.",
4074
+ async run({ world }) {
4075
+ const scratch = await mkdtemp(join(tmpdir(), "world-prompts-"));
4076
+ try {
4077
+ return foundBy("world-prompts", copyWorldPrompts(world.directory, scratch).map((problem) => ({
4078
+ severity: "error",
4079
+ code: "prompt",
4080
+ where: problem.path.slice(world.directory.length + 1) || problem.path,
4081
+ message: problem.message
3809
4082
  })));
3810
- return foundBy("name-clashes", found);
4083
+ } finally {
4084
+ await rm(scratch, {
4085
+ recursive: true,
4086
+ force: true
4087
+ });
3811
4088
  }
3812
- },
4089
+ }
4090
+ };
4091
+ const PARSERS = {
4092
+ characters: parseCharacterFile,
4093
+ locations: parseLocationFile,
4094
+ factions: parseFactionFile,
4095
+ items: parseItemFile,
4096
+ lore: parseLoreFile
4097
+ };
4098
+ const consistencyChecks = [
4099
+ validity,
4100
+ graph,
4101
+ values,
4102
+ names$1,
4103
+ prompts,
3813
4104
  {
3814
- id: "world-prompts",
4105
+ id: "unread-fields",
3815
4106
  kit: "consistency",
3816
4107
  mode: "code",
3817
4108
  needs: [],
3818
- description: "The prompts the world carries are prompts the product has, are not empty and use only the variables each is given: a world with a bad one cannot start a story.",
4109
+ description: "Fields in a character, place, organisation, thing or lore file that nothing reads, so what they say never reaches a story: usually a field written under a name the format does not have.",
3819
4110
  async run({ world }) {
3820
- const scratch = await mkdtemp(join(tmpdir(), "world-prompts-"));
3821
- try {
3822
- return foundBy("world-prompts", copyWorldPrompts(world.directory, scratch).map((problem) => ({
3823
- severity: "error",
3824
- code: "prompt",
3825
- where: problem.path.slice(world.directory.length + 1) || problem.path,
3826
- message: problem.message
3827
- })));
3828
- } finally {
3829
- await rm(scratch, {
3830
- recursive: true,
3831
- force: true
4111
+ const found = [];
4112
+ for (const { key, directory, file } of ENTITY_FOLDERS) for (const id of await subfolders(join(world.directory, directory)).catch(() => [])) {
4113
+ const text = await readFile(join(world.directory, directory, id, file), "utf8").catch(() => void 0);
4114
+ if (text === void 0) continue;
4115
+ let parsed = false;
4116
+ const { unread: fields, read } = Reader.unreadBy(() => {
4117
+ parsed = PARSERS[key](text, id).ok;
4118
+ });
4119
+ if (!parsed || fields.length === 0) continue;
4120
+ found.push({
4121
+ severity: "warning",
4122
+ code: "field-unread",
4123
+ where: `${directory}/${id}`,
4124
+ message: `${fields.slice(0, FIELDS_LISTED).join(", ")}${fields.length > FIELDS_LISTED ? ` and ${fields.length - FIELDS_LISTED} more` : ""} in ${file} ${fields.length === 1 ? "is" : "are"} not read by anything, so a story never sees ${fields.length === 1 ? "it" : "them"}`,
4125
+ hint: `Move the words to the field that holds them, and remove the one that is not read. The fields a ${file} reads: ${read.join(", ")}.`
3832
4126
  });
3833
4127
  }
4128
+ return foundBy("unread-fields", found);
3834
4129
  }
3835
4130
  }
3836
4131
  ];
@@ -4006,103 +4301,188 @@ function worldFieldChanges(before, after) {
4006
4301
  });
4007
4302
  return changes;
4008
4303
  }
4009
- // src/host/checks/world-text.ts
4010
- /** Every string a document holds, joined: what the world says in it, without the structure around it. */
4011
- const stringsOf = (value) => typeof value === "string" ? [value] : Array.isArray(value) ? value.flatMap(stringsOf) : typeof value === "object" && value !== null ? Object.values(value).flatMap(stringsOf) : [];
4012
- /** Fields that say what a document is or point at another document, not what the world says: a model comparing prose with a card is not shown them. */
4013
- const STRUCTURE = /* @__PURE__ */ new Set([
4014
- "id",
4015
- "role",
4016
- "tier",
4017
- "kind",
4018
- "scope",
4019
- "type",
4020
- "status",
4021
- "control",
4022
- "known_by",
4023
- "related",
4024
- "subjects",
4025
- "sources",
4026
- "to",
4027
- "location",
4028
- "parent",
4029
- "held_by",
4030
- "owner",
4031
- "members",
4032
- "knowledge",
4033
- "relationships",
4034
- "exits",
4035
- "start",
4036
- "visual_identity",
4037
- "routine",
4038
- "variables"
4039
- ]);
4040
- /** What a document says in words, one `field: text` line each, without the structure around it. */
4041
- function proseOf(value, field = "") {
4042
- if (typeof value === "string") return value.trim() === "" ? [] : [field === "" ? value : `${field}: ${value}`];
4043
- if (Array.isArray(value)) return value.flatMap((entry) => proseOf(entry, field));
4044
- if (typeof value === "object" && value !== null) return Object.entries(value).filter(([key]) => !STRUCTURE.has(key)).flatMap(([key, entry]) => proseOf(entry, key));
4045
- return [];
4304
+ // src/host/checks/judge.ts
4305
+ /**
4306
+ * 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
4307
+ * question to the language model. The decision model is never the only reader of a thing that may be wrong.
4308
+ */
4309
+ async function verdictOf(model, state, key, instructions, ask) {
4310
+ if (model.decide !== void 0) try {
4311
+ const probability = (await model.decide(state, [{
4312
+ key,
4313
+ instructions
4314
+ }])).get(key);
4315
+ const confidence = model.confidence ?? .7;
4316
+ if (probability !== void 0 && probability >= yesAt(confidence)) return {
4317
+ yes: true,
4318
+ by: "decision"
4319
+ };
4320
+ if (probability !== void 0 && probability <= noAt(confidence)) return {
4321
+ yes: false,
4322
+ by: "decision"
4323
+ };
4324
+ } catch (error) {
4325
+ if (model.cancelled?.() === true) throw error;
4326
+ }
4327
+ return {
4328
+ yes: await ask(),
4329
+ by: "language"
4330
+ };
4046
4331
  }
4047
- async function readJson$1(file) {
4332
+ // src/host/checks/results.ts
4333
+ /** Where a world keeps what its checks found: a dot folder, so what is published leaves it out, like the import's. */
4334
+ const CHECKS_FOLDER = ".checks";
4335
+ const FILE$1 = "results.json";
4336
+ async function read$1(directory) {
4048
4337
  try {
4049
- return JSON.parse(await readFile(file, "utf8"));
4338
+ const stored = JSON.parse(await readFile(join(directory, CHECKS_FOLDER, FILE$1), "utf8"));
4339
+ return stored.version === 1 && typeof stored.checks === "object" && stored.checks !== null ? stored : {
4340
+ version: 1,
4341
+ checks: {}
4342
+ };
4050
4343
  } catch (error) {
4051
- if (error.code === "ENOENT") return void 0;
4344
+ if (error.code === "ENOENT" || error instanceof SyntaxError) return {
4345
+ version: 1,
4346
+ checks: {}
4347
+ };
4052
4348
  throw error;
4053
4349
  }
4054
4350
  }
4055
- /** What the files of a world say, read afresh, for comparing with a card. A file that does not read is left out: the consistency kit names it. */
4056
- async function readWorldText(directory) {
4057
- const texts = /* @__PURE__ */ new Map();
4058
- const prose = /* @__PURE__ */ new Map();
4059
- const subjects = /* @__PURE__ */ new Map();
4060
- const names = [];
4061
- const KIND = {
4062
- characters: "character",
4063
- locations: "place",
4064
- factions: "organisation",
4065
- items: "thing",
4066
- lore: "background entry"
4351
+ /**
4352
+ * Stores the findings of the checks that ran, replacing their last ones, and says what changed against those: results of checks that did not run stay.
4353
+ * @returns `undefined` when none of these checks had run before.
4354
+ */
4355
+ async function recordResults(directory, at, results) {
4356
+ const stored = await read$1(directory);
4357
+ const before = Object.keys(results).flatMap((id) => stored.checks[id]?.findings ?? []);
4358
+ const known = Object.keys(results).some((id) => stored.checks[id] !== void 0);
4359
+ const now = Object.values(results).flat();
4360
+ const was = new Set(before.map(findingKey));
4361
+ const is = new Set(now.map(findingKey));
4362
+ for (const [id, findings] of Object.entries(results)) stored.checks[id] = {
4363
+ at,
4364
+ findings
4067
4365
  };
4068
- for (const { directory: folder, file } of ENTITY_FOLDERS) for (const id of await subfolders(join(directory, folder))) {
4069
- const document = await readJson$1(join(directory, folder, id, file)).catch(() => void 0);
4070
- if (document === void 0 || typeof document !== "object") continue;
4071
- texts.set(`${folder}/${id}`, stringsOf(document).join("\n"));
4072
- prose.set(`${folder}/${id}`, proseOf(document).join("\n"));
4073
- const label = document[folder === "lore" ? "title" : "name"];
4074
- subjects.set(`${folder}/${id}`, `${KIND[folder]}${typeof label === "string" ? ` ${label}` : ""}`);
4075
- if (folder !== "lore") {
4076
- const own = [document["name"], ...Array.isArray(document["aliases"]) ? document["aliases"] : []].filter((name) => typeof name === "string" && name.trim() !== "");
4077
- names.push({
4078
- where: `${folder}/${id}`,
4079
- kind: folder,
4080
- names: own,
4081
- playable: document["role"] === "playable"
4082
- });
4083
- }
4366
+ await atomicWrite(join(directory, CHECKS_FOLDER, FILE$1), JSON.stringify(stored, null, 2), { createDirectory: true });
4367
+ return known ? {
4368
+ added: now.filter((finding) => !was.has(findingKey(finding))).length,
4369
+ resolved: before.filter((finding) => !is.has(findingKey(finding)))
4370
+ } : void 0;
4371
+ }
4372
+ // src/host/checks/similar.ts
4373
+ const FILE = "vectors.json";
4374
+ /** How many texts one request to the embedding provider carries. */
4375
+ const BATCH = 32;
4376
+ const hashOf = (text) => createHash("sha256").update(text).digest("hex").slice(0, 32);
4377
+ const pack = (vector) => Buffer.from(new Float32Array(vector).buffer).toString("base64");
4378
+ const unpack = (text) => {
4379
+ const bytes = Buffer.from(text, "base64");
4380
+ return [...new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4)];
4381
+ };
4382
+ async function read(directory) {
4383
+ try {
4384
+ const stored = JSON.parse(await readFile(join(directory, CHECKS_FOLDER, FILE), "utf8"));
4385
+ return stored.version === 1 && typeof stored.vectors === "object" && stored.vectors !== null ? stored : void 0;
4386
+ } catch (error) {
4387
+ if (error.code === "ENOENT" || error instanceof SyntaxError) return void 0;
4388
+ throw error;
4084
4389
  }
4085
- const info = await readJson$1(join(directory, WORLD_FILE_NAME)).catch(() => void 0);
4086
- if (typeof info === "object" && info !== null) for (const [field, value] of Object.entries(info)) {
4087
- const text = stringsOf(value).join("\n");
4088
- if (text.trim() !== "") {
4089
- texts.set(`${WORLD_FILE_NAME}#${field}`, text);
4090
- prose.set(`${WORLD_FILE_NAME}#${field}`, text);
4091
- subjects.set(`${WORLD_FILE_NAME}#${field}`, `the world's ${field}`);
4092
- }
4390
+ }
4391
+ function cosine(a, b) {
4392
+ let dot = 0;
4393
+ let left = 0;
4394
+ let right = 0;
4395
+ for (let at = 0; at < a.length; at++) {
4396
+ dot += a[at] * b[at];
4397
+ left += a[at] * a[at];
4398
+ right += b[at] * b[at];
4093
4399
  }
4094
- const prompts = join(directory, PROMPTS_FOLDER);
4095
- for (const language of await readdir(prompts).catch(() => [])) for (const id of listFiles(join(prompts, language))) texts.set(`${PROMPTS_FOLDER}/${language}/${id}`, await readFile(join(prompts, language, `${id}.md`), "utf8"));
4096
- return {
4097
- texts,
4098
- prose,
4099
- subjects,
4100
- names
4400
+ return left === 0 || right === 0 ? 0 : dot / Math.sqrt(left * right);
4401
+ }
4402
+ /**
4403
+ * The vectors of texts, in order. What the world has embedded before is kept in its dot folder by the text's hash, so a check run again embeds only what changed; vectors of
4404
+ * another model or size are not used. `undefined` when the run has no embedding provider.
4405
+ */
4406
+ async function vectorsOf(model, directory, texts) {
4407
+ if (model.embed === void 0) return void 0;
4408
+ const stored = await read(directory);
4409
+ const known = new Map(Object.entries(stored?.vectors ?? {}).map(([hash, packed]) => [hash, unpack(packed)]));
4410
+ const hashes = texts.map(hashOf);
4411
+ const missing = [...new Set(hashes.filter((hash) => !known.has(hash)))];
4412
+ if (missing.length === 0) return hashes.map((hash) => known.get(hash));
4413
+ try {
4414
+ return await embedded(model, directory, texts, hashes, missing, known, stored?.model ?? "");
4415
+ } catch (error) {
4416
+ if (error instanceof NoEmbeddingService) return void 0;
4417
+ throw error;
4418
+ }
4419
+ }
4420
+ async function embedded(model, directory, texts, hashes, missing, known, stored) {
4421
+ const byHash = new Map(texts.map((text, at) => [hashes[at], text]));
4422
+ let name = stored;
4423
+ const made = /* @__PURE__ */ new Map();
4424
+ const embed = async (wanted) => {
4425
+ for (let at = 0; at < wanted.length; at += BATCH) {
4426
+ const batch = wanted.slice(at, at + BATCH);
4427
+ const { vectors, model: used } = await model.embed(batch.map((hash) => byHash.get(hash)));
4428
+ if (vectors.length !== batch.length) throw new Error(`the embedding provider returned ${vectors.length} vectors for ${batch.length} texts`);
4429
+ if (name !== "" && name !== used) known.clear();
4430
+ name = used;
4431
+ batch.forEach((hash, index) => made.set(hash, vectors[index]));
4432
+ }
4101
4433
  };
4434
+ await embed(missing);
4435
+ const size = [...made.values()][0].length;
4436
+ for (const [hash, vector] of known) if (vector.length !== size) known.delete(hash);
4437
+ await embed([...new Set(hashes.filter((hash) => !known.has(hash) && !made.has(hash)))]);
4438
+ const all = new Map([...known, ...made]);
4439
+ const result = hashes.map((hash) => all.get(hash));
4440
+ const kept = [...all].slice(-4e3);
4441
+ await atomicWrite(join(directory, CHECKS_FOLDER, FILE), JSON.stringify({
4442
+ version: 1,
4443
+ model: name,
4444
+ dimensions: size,
4445
+ vectors: Object.fromEntries(kept.map(([hash, vector]) => [hash, pack(vector)]))
4446
+ }), { createDirectory: true });
4447
+ return result;
4448
+ }
4449
+ /** The pairs of vectors closest to each other that `allowed` accepts and that reach `threshold`, closest first, at most `limit`. */
4450
+ function nearPairs(vectors, allowed, threshold, limit) {
4451
+ const pairs = [];
4452
+ for (let a = 0; a < vectors.length; a++) for (let b = a + 1; b < vectors.length; b++) {
4453
+ if (!allowed(a, b)) continue;
4454
+ const score = cosine(vectors[a], vectors[b]);
4455
+ if (score >= threshold) pairs.push({
4456
+ a,
4457
+ b,
4458
+ score
4459
+ });
4460
+ }
4461
+ return pairs.sort((x, y) => y.score - x.score).slice(0, limit);
4462
+ }
4463
+ /**
4464
+ * The pairs of texts worth asking a model about: the closest in meaning when there is an embedding provider, otherwise, when asked to, the first `limit` pairs `allowed` accepts (for a short list), else nothing. Closeness only
4465
+ * says two texts are about the same thing (trust and affection are close, and not one value), so it picks what to ask and never what is true.
4466
+ */
4467
+ async function candidates(model, directory, texts, allowed, threshold, limit, withoutProvider = "none") {
4468
+ const vectors = await vectorsOf(model, directory, texts);
4469
+ if (vectors !== void 0) return nearPairs(vectors, allowed, threshold, limit);
4470
+ if (withoutProvider === "none") return void 0;
4471
+ const pairs = [];
4472
+ for (let a = 0; a < texts.length && pairs.length < limit; a++) for (let b = a + 1; b < texts.length && pairs.length < limit; b++) if (allowed(a, b)) pairs.push({
4473
+ a,
4474
+ b,
4475
+ score: 0
4476
+ });
4477
+ return pairs;
4102
4478
  }
4103
4479
  // src/host/checks/fidelity.ts
4480
+ /** The most uncovered pieces listed one by one: past that the import itself is the problem. */
4481
+ const PIECES_LISTED = 12;
4104
4482
  /** The most numbers one finding lists. */
4105
4483
  const NUMBERS_LISTED = 8;
4484
+ /** The most parts of the world a finding names as made from a piece. */
4485
+ const PARTS_LISTED = 3;
4106
4486
  /** A changed passage of the new card is taken as reflected in the world when this much of its terms are in what was made from its piece. */
4107
4487
  const REFLECTED = .5;
4108
4488
  const MIN_PASSAGE_TERMS = 3;
@@ -4123,26 +4503,120 @@ const recordsNeeded = (evidence) => {
4123
4503
  const snippet = (text) => `${text.replace(/\s+/gu, " ").trim().slice(0, SNIPPET)}${text.trim().length > SNIPPET ? "…" : ""}`;
4124
4504
  /** The pieces the import read for the world and the card the records are about. */
4125
4505
  const basis = (evidence) => evidence.previous ?? evidence.source;
4506
+ /** How many of the uncovered pieces are asked about by meaning, largest first. */
4507
+ const RESCUE_PIECES = 24;
4508
+ /** How many documents of the world are asked about for one piece: the closest in meaning. */
4509
+ const RESCUE_DOCUMENTS = 3;
4510
+ /** How close in meaning a document is to a piece before it is asked about; low on purpose, the question settles it. */
4511
+ const RESCUE_CLOSE = .5;
4512
+ const RESCUE_CHARS = 1800;
4126
4513
  const coverage = {
4127
4514
  id: "card-coverage",
4128
4515
  kit: "fidelity",
4129
4516
  mode: "code",
4130
4517
  needs: ["card"],
4131
- description: "Every piece of the card the import read for the world is in it somewhere: made into a character, place, organisation or thing, or into lore. A piece nothing comes from was lost or left out without a word.",
4518
+ description: "Every piece of the card the import read for the world is in it somewhere: made into a character, place, organisation or thing, or into lore. A piece nothing comes from was lost or left out without a word. With an embedding provider and a model, a piece no part of the world shares words with (a card in another language, a rewrite) is also looked for by meaning before it is called lost.",
4132
4519
  async run(input) {
4133
4520
  const evidence = need$1(input);
4134
4521
  recordsNeeded(evidence);
4135
4522
  const source = basis(evidence);
4136
- const made = madeInto(source, await readWorldText(input.world.directory));
4137
- return foundBy("card-coverage", usedPieces(source).filter((piece) => termsOf(piece.text).size >= MIN_PIECE_TERMS && !made.has(baseId(piece.id))).map((piece) => ({
4523
+ const made = await loaded.made(input, evidence);
4524
+ const missing = usedPieces(source).filter((piece) => termsOf(piece.text).size >= MIN_PIECE_TERMS && !made.has(baseId(piece.id))).sort((a, b) => b.text.length - a.text.length);
4525
+ const { saved, looked, problem } = await sayingByMeaning(input, missing);
4526
+ const lost = missing.filter((piece) => !saved.has(piece.id));
4527
+ const found = lost.slice(0, PIECES_LISTED).map((piece) => ({
4138
4528
  severity: "warning",
4139
4529
  code: "piece-uncovered",
4140
4530
  where: `card:${piece.id}`,
4141
- message: `${piece.label} (${piece.text.length} characters) was read for the world, but nothing in the world seems to be made from it`,
4531
+ message: `${piece.label} (${piece.text.length} characters) was read for the world, but nothing in the world seems to be made from it${looked ? ", by words or by meaning" : ""}`,
4142
4532
  hint: "Read it with card_read. If it says something about the world, add that where it belongs; if it is not meant for the story, say so."
4143
- })));
4533
+ }));
4534
+ if (lost.length > PIECES_LISTED) found.push({
4535
+ severity: "warning",
4536
+ code: "pieces-uncovered-more",
4537
+ where: "card",
4538
+ message: `${lost.length - PIECES_LISTED} more pieces of the card, smaller than these, have nothing in the world made from them`,
4539
+ hint: "The import probably stopped early or read the card as something else: look at the import report before going through them one by one."
4540
+ });
4541
+ if (problem !== void 0) found.push({
4542
+ severity: "info",
4543
+ code: "meaning-unavailable",
4544
+ where: "card",
4545
+ message: `the pieces were not looked for by meaning: ${problem}`
4546
+ });
4547
+ return foundBy("card-coverage", found);
4144
4548
  }
4145
4549
  };
4550
+ /** The ids of the pieces whose facts a document of the world says in other words (or another language): the closest documents by meaning, and a model's yes. */
4551
+ async function sayingByMeaning(input, pieces) {
4552
+ const model = input.model;
4553
+ const saved = /* @__PURE__ */ new Set();
4554
+ if (model?.embed === void 0 || pieces.length === 0) return {
4555
+ saved,
4556
+ looked: false
4557
+ };
4558
+ try {
4559
+ return await lookedByMeaning(input, model, pieces, saved);
4560
+ } catch (error) {
4561
+ if (model.cancelled?.() === true) throw error;
4562
+ return {
4563
+ saved,
4564
+ looked: false,
4565
+ problem: error instanceof Error ? error.message : String(error)
4566
+ };
4567
+ }
4568
+ }
4569
+ async function lookedByMeaning(input, model, pieces, saved) {
4570
+ const documents = [...(await loaded.text(input)).prose].filter(([, prose]) => prose.trim() !== "");
4571
+ if (documents.length === 0) return {
4572
+ saved,
4573
+ looked: false
4574
+ };
4575
+ const asked = pieces.slice(0, RESCUE_PIECES);
4576
+ const vectors = await vectorsOf(model, input.world.directory, [...asked.map((piece) => piece.text.slice(0, RESCUE_CHARS)), ...documents.map(([, prose]) => prose.slice(0, RESCUE_CHARS))]);
4577
+ if (vectors === void 0) return {
4578
+ saved,
4579
+ looked: false
4580
+ };
4581
+ const instructions = model.text("checks/decide-covered");
4582
+ await mapLimit(asked, model.concurrency, async (piece, at) => {
4583
+ const near = documents.map(([where, prose], index) => ({
4584
+ where,
4585
+ prose,
4586
+ score: cosine(vectors[at], vectors[asked.length + index])
4587
+ })).filter((entry) => entry.score >= RESCUE_CLOSE).sort((a, b) => b.score - a.score).slice(0, RESCUE_DOCUMENTS);
4588
+ for (const document of near) {
4589
+ if (model.cancelled?.() === true) return;
4590
+ if ((await verdictOf(model, {
4591
+ passage: piece.text.slice(0, RESCUE_CHARS),
4592
+ document: document.prose.slice(0, RESCUE_CHARS)
4593
+ }, "covered", instructions, async () => {
4594
+ return (await model.run({
4595
+ persona: instructions,
4596
+ prompt: `${piece.text.slice(0, RESCUE_CHARS)}\n\n${document.prose.slice(0, RESCUE_CHARS)}`,
4597
+ result: COVERED_SCHEMA,
4598
+ maxTokens: 500
4599
+ })).covered;
4600
+ })).yes) {
4601
+ saved.add(piece.id);
4602
+ return;
4603
+ }
4604
+ }
4605
+ });
4606
+ return {
4607
+ saved,
4608
+ looked: true
4609
+ };
4610
+ }
4611
+ const COVERED_SCHEMA = {
4612
+ type: "object",
4613
+ properties: {
4614
+ reason: { type: "string" },
4615
+ covered: { type: "boolean" }
4616
+ },
4617
+ required: ["reason", "covered"],
4618
+ additionalProperties: false
4619
+ };
4146
4620
  const names = {
4147
4621
  id: "card-names",
4148
4622
  kit: "fidelity",
@@ -4152,7 +4626,7 @@ const names = {
4152
4626
  async run(input) {
4153
4627
  const evidence = need$1(input);
4154
4628
  const source = basis(evidence);
4155
- const world = await readWorldText(input.world.directory);
4629
+ const world = await loaded.text(input);
4156
4630
  const text = folded([source.card.name, ...source.pieces.map((piece) => piece.text)].join("\n"));
4157
4631
  const named = (candidate) => {
4158
4632
  return [candidate, candidate.replace(/\s*[((][^))]*[))]/gu, "").trim()].some((name) => folded(name).trim() !== "" && text.includes(folded(name).trim()));
@@ -4191,8 +4665,8 @@ const facts = {
4191
4665
  const evidence = need$1(input);
4192
4666
  recordsNeeded(evidence);
4193
4667
  const source = basis(evidence);
4194
- const world = await readWorldText(input.world.directory);
4195
- const made = madeInto(source, world);
4668
+ const world = await loaded.text(input);
4669
+ const made = await loaded.made(input, evidence);
4196
4670
  const found = [];
4197
4671
  for (const piece of usedPieces(source)) {
4198
4672
  const into = made.get(baseId(piece.id));
@@ -4203,7 +4677,7 @@ const facts = {
4203
4677
  severity: "info",
4204
4678
  code: "number-lost",
4205
4679
  where: `card:${piece.id}`,
4206
- message: `${piece.label} gives ${lost.slice(0, NUMBERS_LISTED).join(", ")}${lost.length > NUMBERS_LISTED ? ` and ${lost.length - NUMBERS_LISTED} more` : ""}, which ${into.join(", ")} do not have`,
4680
+ message: `${piece.label} gives ${lost.slice(0, NUMBERS_LISTED).join(", ")}${lost.length > NUMBERS_LISTED ? ` and ${lost.length - NUMBERS_LISTED} more` : ""}, which ${into.slice(0, PARTS_LISTED).join(", ")}${into.length > PARTS_LISTED ? ` and ${into.length - PARTS_LISTED} more` : ""} do not have`,
4207
4681
  hint: "Read the piece with card_read: a number that matters to the story belongs in the world as the card has it."
4208
4682
  });
4209
4683
  }
@@ -4226,8 +4700,8 @@ const drift = {
4226
4700
  const evidence = need$1(input);
4227
4701
  if (evidence.previous === void 0) throw new CheckSkipped("no newer card is waiting; ask for the card that is waiting with version new");
4228
4702
  recordsNeeded(evidence);
4229
- const world = await readWorldText(input.world.directory);
4230
- const made = madeInto(evidence.previous, world);
4703
+ const world = await loaded.text(input);
4704
+ const made = await loaded.made(input, evidence);
4231
4705
  const match = matchPieces(evidence.previous.pieces, evidence.source.pieces);
4232
4706
  const found = [];
4233
4707
  for (const { before, after } of match.pairs) {
@@ -4308,7 +4782,6 @@ const FIDELITY_SCHEMA = {
4308
4782
  required: ["dropped", "invented"],
4309
4783
  additionalProperties: false
4310
4784
  };
4311
- const squashed = (text) => folded(text).replace(/\s+/gu, " ").trim();
4312
4785
  /** Why the comparison cannot be used: a quote that is not in the text it is said to come from is a guess, not a finding. */
4313
4786
  function comparisonProblem(value, source, document) {
4314
4787
  if (value.dropped.length > MAX_REPORTED || value.invented.length > MAX_REPORTED) return `report at most ${MAX_REPORTED} of each`;
@@ -4328,217 +4801,400 @@ const fidelityChecks = [
4328
4801
  needs: ["card", "model"],
4329
4802
  description: "For each character, place, organisation, thing and background entry made from the card: facts the card states that the document lost, and facts the document states that the card does not give, each with the quoted passage.",
4330
4803
  async run(input) {
4331
- const evidence = need$1(input);
4332
- recordsNeeded(evidence);
4804
+ const evidence = need$1(input);
4805
+ recordsNeeded(evidence);
4806
+ const model = input.model;
4807
+ const tr = trOf(model);
4808
+ const source = basis(evidence);
4809
+ const world = await loaded.text(input);
4810
+ const made = await loaded.made(input, evidence, "about");
4811
+ const from = /* @__PURE__ */ new Map();
4812
+ for (const piece of usedPieces(source)) for (const where of made.get(baseId(piece.id)) ?? []) from.set(where, [...from.get(where) ?? [], piece]);
4813
+ const ranked = [...from].map(([where, pieces]) => ({
4814
+ where,
4815
+ pieces,
4816
+ size: pieces.reduce((sum, piece) => sum + piece.text.length, 0)
4817
+ })).sort((a, b) => b.size - a.size);
4818
+ const chosen = ranked.slice(0, MAX_COMPARED);
4819
+ const found = [];
4820
+ if (ranked.length > chosen.length) found.push({
4821
+ severity: "info",
4822
+ code: "not-compared",
4823
+ where: "(the world)",
4824
+ message: `${ranked.length - chosen.length} more documents were not compared: a run compares at most ${MAX_COMPARED}, the ones with the most source first`,
4825
+ hint: "Compare the rest with check_card again after fixing these, or read them against the card yourself."
4826
+ });
4827
+ await mapLimit(chosen, model.concurrency, async ({ where, pieces }) => {
4828
+ if (model.cancelled?.() === true) return;
4829
+ const document = world.prose.get(where) ?? "";
4830
+ const subject = world.subjects.get(where) ?? where;
4831
+ const text = pieces.map((piece) => `[${piece.label}]\n${piece.text.slice(0, SOURCE_CHARS)}`).join("\n\n").slice(0, SOURCE_CHARS * 2);
4832
+ try {
4833
+ const answer = await model.run({
4834
+ persona: model.text("checks/fidelity"),
4835
+ prompt: `${tr("Source pieces:")}\n${text}\n\n${tr("Document ({where}), about {subject}:", {
4836
+ where,
4837
+ subject
4838
+ })}\n${document}`,
4839
+ result: FIDELITY_SCHEMA,
4840
+ maxTokens: 1500,
4841
+ check: (value) => comparisonProblem(value, text, document)
4842
+ });
4843
+ const once = (list) => list.filter((entry, at) => list.findIndex((other) => squashed(other.quote) === squashed(entry.quote)) === at);
4844
+ for (const entry of once(answer.dropped)) found.push({
4845
+ severity: "warning",
4846
+ code: "fact-dropped",
4847
+ where,
4848
+ message: `the card says “${snippet(entry.quote)}” (${entry.fact}), which ${where} does not say`,
4849
+ hint: "Read the piece with card_read; if the fact matters to the story, add it to the document as the card has it."
4850
+ });
4851
+ for (const entry of once(answer.invented)) found.push({
4852
+ severity: "warning",
4853
+ code: "fact-invented",
4854
+ where,
4855
+ message: `${where} says “${snippet(entry.quote)}” (${entry.fact}), which the card does not give`,
4856
+ hint: "Remove it unless the person wants it; the world should not state what its source does not."
4857
+ });
4858
+ } catch (error) {
4859
+ if (model.cancelled?.() === true) throw error;
4860
+ found.push({
4861
+ severity: "info",
4862
+ code: "not-compared",
4863
+ where,
4864
+ message: `${where} could not be compared: ${error instanceof Error ? error.message : String(error)}`
4865
+ });
4866
+ }
4867
+ });
4868
+ return foundBy("card-fidelity", found);
4869
+ }
4870
+ }
4871
+ ];
4872
+ // src/host/checks/playability.ts
4873
+ /** The most players' first scenes a model reads: a world with many playable characters is judged by the first few. */
4874
+ const SCENES_JUDGED = 3;
4875
+ const OPENING_CHARS = 2500;
4876
+ const SCENE_CHARS = 3500;
4877
+ /** Where each character the player may play starts and what is around them, as the files say it. */
4878
+ async function scenesOf(input) {
4879
+ const [{ characters }, { locations }, { items }] = await Promise.all([
4880
+ loaded.characters(input),
4881
+ loaded.locations(input),
4882
+ loaded.items(input)
4883
+ ]);
4884
+ const players = characters.filter((character) => character.role === "playable");
4885
+ if (players.length === 0) throw new CheckSkipped("no character can be played (see quality-cast)");
4886
+ return players.map((player) => {
4887
+ const at = player.start.place?.location;
4888
+ const place = locations.find((location) => location.id === at);
4889
+ return {
4890
+ player,
4891
+ place,
4892
+ others: place === void 0 ? [] : characters.filter((other) => other.id !== player.id && other.kind === "person" && other.start.place?.location === place.id),
4893
+ things: place === void 0 ? [] : items.filter((item) => item.start.kind === "location" && item.start.location === place.id && !item.hidden).map((item) => item.name),
4894
+ ways: place === void 0 ? 0 : place.exits.length + locations.filter((other) => other.id !== place.id && other.exits.some((exit) => exit.to === place.id)).length
4895
+ };
4896
+ });
4897
+ }
4898
+ const firstScene = {
4899
+ id: "first-scene",
4900
+ kit: "playability",
4901
+ mode: "code",
4902
+ needs: [],
4903
+ description: "For each character the player may play: that they start in a place of the world, with other people, things or features there, so the first turn has something to react to. (A place with no way out is named by world-graph.)",
4904
+ async run(input) {
4905
+ const { locations } = await loaded.locations(input);
4906
+ const found = [];
4907
+ for (const scene of await scenesOf(input)) {
4908
+ const where = `characters/${scene.player.id}`;
4909
+ if (scene.place === void 0) {
4910
+ if (locations.length > 0) found.push({
4911
+ severity: "warning",
4912
+ code: "start-nowhere",
4913
+ where,
4914
+ message: `${scene.player.name} starts in no place of the world, though the world has places`,
4915
+ hint: "Give the character a start place."
4916
+ });
4917
+ continue;
4918
+ }
4919
+ if (scene.others.length === 0 && scene.things.length === 0 && scene.place.features.length === 0) found.push({
4920
+ severity: "info",
4921
+ code: "start-empty",
4922
+ where,
4923
+ message: `${scene.player.name} starts alone in ${scene.place.name}, with no people, things or features there to act on`
4924
+ });
4925
+ else if (scene.others.length === 0) found.push({
4926
+ severity: "info",
4927
+ code: "start-alone",
4928
+ where,
4929
+ message: `${scene.player.name} starts alone in ${scene.place.name}: nothing answers until they move or something happens`
4930
+ });
4931
+ }
4932
+ return foundBy("first-scene", found);
4933
+ }
4934
+ };
4935
+ const HOOK_SCHEMA = {
4936
+ type: "object",
4937
+ properties: {
4938
+ reason: { type: "string" },
4939
+ playable: { type: "boolean" }
4940
+ },
4941
+ required: ["reason", "playable"],
4942
+ additionalProperties: false
4943
+ };
4944
+ const playabilityChecks = [firstScene, {
4945
+ id: "opening-playable",
4946
+ kit: "playability",
4947
+ mode: "decision",
4948
+ needs: ["model"],
4949
+ description: "Whether a player who has read the opening and the first scene of their character knows where they are and has something to do. A decision model may answer; what it is unsure of goes to the language model.",
4950
+ async run(input) {
4951
+ const model = input.model;
4952
+ const scenes = (await scenesOf(input)).slice(0, SCENES_JUDGED);
4953
+ const info = await loaded.info(input);
4954
+ const found = [];
4955
+ await mapLimit(scenes, model.concurrency, async (scene) => {
4956
+ const text = [
4957
+ `opening: ${info.opening.trim() === "" ? "(none: the story starts with \"The story begins.\")" : info.opening.slice(0, OPENING_CHARS)}`,
4958
+ `the player's character: ${scene.player.name}: ${scene.player.summary}`,
4959
+ ...scene.player.start.activity === "" ? [] : [`doing: ${scene.player.start.activity}`],
4960
+ ...scene.player.start.intents.slice(0, 3).map((intent) => `wants: ${intent.name}: ${intent.description}`),
4961
+ scene.place === void 0 ? "place: (none)" : `place: ${scene.place.name}: ${scene.place.description}`,
4962
+ ...(scene.place?.features ?? []).slice(0, 8).map((feature) => `feature: ${feature.name}`),
4963
+ ...scene.others.slice(0, 8).map((other) => `here: ${other.name}: ${other.summary}`),
4964
+ ...scene.things.slice(0, 8).map((thing) => `thing here: ${thing}`),
4965
+ `ways out: ${scene.ways}`
4966
+ ].join("\n").slice(0, SCENE_CHARS);
4967
+ let reason = "";
4968
+ const instructions = model.text("checks/decide-opening");
4969
+ if (!(await verdictOf(model, { scene: text }, "playable", instructions, async () => {
4970
+ const answer = await model.run({
4971
+ persona: instructions,
4972
+ prompt: text,
4973
+ result: HOOK_SCHEMA,
4974
+ maxTokens: 500
4975
+ });
4976
+ reason = answer.reason;
4977
+ return answer.playable;
4978
+ })).yes) found.push({
4979
+ severity: "warning",
4980
+ code: "opening-flat",
4981
+ where: `characters/${scene.player.id}`,
4982
+ message: `a player of ${scene.player.name} may not know what to do at the start${reason === "" ? "" : `: ${reason}`}`,
4983
+ hint: "Give the opening or the first scene a situation, a person or a want to act on, from what the card says; do not make it up."
4984
+ });
4985
+ });
4986
+ return foundBy("opening-playable", found);
4987
+ }
4988
+ }];
4989
+ // src/host/checks/semantic.ts
4990
+ /** The most pairs one check asks a model about: past that the world has a different problem than a few doubles. */
4991
+ const PAIRS_ASKED = 12;
4992
+ /** How close two texts are in meaning before they are asked about: low on purpose, because closeness only picks what to ask (trust and affection are as close as gold and coins). */
4993
+ const VALUES_CLOSE = .55;
4994
+ const THINGS_CLOSE = .6;
4995
+ const LORE_CLOSE = .6;
4996
+ const TEXT_CHARS = 1200;
4997
+ const yesNo = (key) => ({
4998
+ type: "object",
4999
+ properties: {
5000
+ reason: { type: "string" },
5001
+ [key]: { type: "boolean" }
5002
+ },
5003
+ required: ["reason", key],
5004
+ additionalProperties: false
5005
+ });
5006
+ /** The pairs a model says yes to, each with the reason the language model gave (the decision model gives none). */
5007
+ async function asked(model, pairs, prompt, key, extra = {}) {
5008
+ const instructions = model.text(prompt);
5009
+ return (await mapLimit(pairs, model.concurrency, async (pair) => {
5010
+ if (model.cancelled?.() === true) return void 0;
5011
+ let reason = "";
5012
+ return (await verdictOf(model, {
5013
+ ...extra,
5014
+ first: pair.first,
5015
+ second: pair.second
5016
+ }, key, instructions, async () => {
5017
+ const answer = await model.run({
5018
+ persona: instructions,
5019
+ prompt: `A:\n${pair.first}\n\nB:\n${pair.second}`,
5020
+ result: yesNo(key),
5021
+ maxTokens: 500
5022
+ });
5023
+ reason = answer.reason;
5024
+ return answer[key] === true;
5025
+ })).yes ? {
5026
+ pair,
5027
+ reason
5028
+ } : void 0;
5029
+ })).filter((entry) => entry !== void 0);
5030
+ }
5031
+ const because = (reason) => reason.trim() === "" ? "" : `: ${reason.trim()}`;
5032
+ const valueText = (def) => `${def.name} (${def.key}): ${def.description ?? ""}`.slice(0, TEXT_CHARS);
5033
+ const sameValue = {
5034
+ id: "duplicate-values-meaning",
5035
+ kit: "consistency",
5036
+ mode: "decision",
5037
+ needs: ["model"],
5038
+ description: "Tracked values that are the same thing under different words or languages (the table of duplicate-values does not know them): the closest in meaning are found with the embedding provider and a decision model says whether they are one.",
5039
+ async run(input) {
5040
+ const model = input.model;
5041
+ const { defs } = await loaded.variables(input);
5042
+ const known = new Set(duplicateValues(defs).map(([a, b]) => `${a.key}\n${b.key}`));
5043
+ const allowed = (a, b) => overlapping(defs[a], defs[b]) && !known.has(`${defs[a].key}\n${defs[b].key}`);
5044
+ const said = await asked(model, (await candidates(model, input.world.directory, defs.map(valueText), allowed, VALUES_CLOSE, PAIRS_ASKED, "first") ?? []).map((entry) => ({
5045
+ near: entry,
5046
+ first: valueText(defs[entry.a]),
5047
+ second: valueText(defs[entry.b])
5048
+ })), "checks/decide-same-value", "same");
5049
+ return foundBy("duplicate-values-meaning", said.map(({ pair, reason }) => {
5050
+ const a = defs[pair.near.a];
5051
+ const b = defs[pair.near.b];
5052
+ return {
5053
+ severity: "warning",
5054
+ code: "duplicate-value",
5055
+ where: "variables.json",
5056
+ message: `${a.key} (${a.name}) and ${b.key} (${b.name}) seem to track the same thing for the same ${a.subject}s${because(reason)}`,
5057
+ hint: "Keep one and remove the other from variables.json and from the files that give it a starting value; which one is right is a choice."
5058
+ };
5059
+ }));
5060
+ }
5061
+ };
5062
+ const KIND_NAME = {
5063
+ characters: "character",
5064
+ locations: "place",
5065
+ factions: "organisation",
5066
+ items: "thing"
5067
+ };
5068
+ /** Whether one name is inside the other (an "Old Mill" and a "Mill"): such a pair is asked about whatever the embedding says. */
5069
+ const nested = (a, b) => a.some((left) => b.some((right) => left !== right && Math.min(left.length, right.length) >= 4 && (left.includes(right) || right.includes(left))));
5070
+ const nearNames = {
5071
+ id: "name-near",
5072
+ kit: "consistency",
5073
+ mode: "decision",
5074
+ needs: ["model"],
5075
+ description: "Two characters, places, organisations or things that may be one written twice under different names (\"The Mill\" and \"Old Mill\"): found by name and, with an embedding provider, by what the sheets say; a decision model says whether they are one.",
5076
+ async run(input) {
5077
+ const model = input.model;
5078
+ const world = await loaded.text(input);
5079
+ const things = world.names.map((thing) => ({
5080
+ where: thing.where,
5081
+ kind: KIND_NAME[thing.kind],
5082
+ names: [...new Set(thing.names.map(norm).filter((name) => name !== ""))],
5083
+ text: `${thing.names[0] ?? thing.where}: ${(world.prose.get(thing.where) ?? "").slice(0, TEXT_CHARS)}`
5084
+ }));
5085
+ const sameKind = (a, b) => things[a].kind === things[b].kind && !things[a].names.some((name) => things[b].names.includes(name));
5086
+ const byName = [];
5087
+ for (let a = 0; a < things.length; a++) for (let b = a + 1; b < things.length; b++) if (sameKind(a, b) && nested(things[a].names, things[b].names)) byName.push({
5088
+ a,
5089
+ b,
5090
+ score: 1
5091
+ });
5092
+ const byMeaning = await candidates(model, input.world.directory, things.map((thing) => thing.text), sameKind, THINGS_CLOSE, PAIRS_ASKED) ?? [];
5093
+ const seen = new Set(byName.map((entry) => `${entry.a}:${entry.b}`));
5094
+ const said = await asked(model, [...byName, ...byMeaning.filter((entry) => !seen.has(`${entry.a}:${entry.b}`))].slice(0, PAIRS_ASKED).map((entry) => ({
5095
+ near: entry,
5096
+ first: `${things[entry.a].kind} ${things[entry.a].text}`,
5097
+ second: `${things[entry.b].kind} ${things[entry.b].text}`
5098
+ })), "checks/decide-same-thing", "same");
5099
+ return foundBy("name-near", said.map(({ pair, reason }) => {
5100
+ const a = things[pair.near.a];
5101
+ const b = things[pair.near.b];
5102
+ return {
5103
+ severity: "warning",
5104
+ code: "possible-duplicate",
5105
+ where: a.where,
5106
+ message: `${a.where} and ${b.where} may be the same ${a.kind}, written twice${because(reason)}`,
5107
+ hint: "Read both. If they are one, keep one and point everything that names the other at it; if they are two, make what tells them apart plain in each. Which stays is a choice."
5108
+ };
5109
+ }));
5110
+ }
5111
+ };
5112
+ const CONFLICT_SCHEMA = {
5113
+ type: "object",
5114
+ properties: {
5115
+ reason: { type: "string" },
5116
+ conflict: { type: "boolean" },
5117
+ first_quote: { type: "string" },
5118
+ second_quote: { type: "string" }
5119
+ },
5120
+ required: [
5121
+ "reason",
5122
+ "conflict",
5123
+ "first_quote",
5124
+ "second_quote"
5125
+ ],
5126
+ additionalProperties: false
5127
+ };
5128
+ /** The shortest quote that can state a fact: a word or a title is not one. */
5129
+ const MIN_QUOTE_CHARS = 10;
5130
+ /** Why a conflict cannot be used: it must quote, from each entry, a statement that rules out the other's, not a title and not the same words twice. */
5131
+ function conflictProblem(value, first, second, titles = ["", ""]) {
5132
+ if (!value.conflict) return void 0;
5133
+ const there = (text, quote) => squashed(quote) !== "" && squashed(text).includes(squashed(quote));
5134
+ const statement = (quote, title) => squashed(quote).replace(/\s/gu, "").length >= MIN_QUOTE_CHARS && !squashed(title).includes(squashed(quote)) && !squashed(quote).includes(squashed(title) || "\0");
5135
+ if (!there(first, value.first_quote)) return "first_quote must be a passage of the first entry, exactly as it is written there";
5136
+ if (!there(second, value.second_quote)) return "second_quote must be a passage of the second entry, exactly as it is written there";
5137
+ if (!statement(value.first_quote, titles[0]) || !statement(value.second_quote, titles[1])) return "each quote must be a statement of fact of its entry (at least a clause long), not a title or a heading";
5138
+ if (squashed(value.first_quote).includes(squashed(value.second_quote)) || squashed(value.second_quote).includes(squashed(value.first_quote))) return "the two quotes are the same words: quote two different statements that cannot both be true";
5139
+ }
5140
+ const loreText = (entry) => `${entry.title}\n${entry.summary}\n${entry.body}`.slice(0, TEXT_CHARS * 2);
5141
+ const semanticChecks = [
5142
+ sameValue,
5143
+ nearNames,
5144
+ {
5145
+ id: "lore-conflict",
5146
+ kit: "consistency",
5147
+ mode: "decision",
5148
+ needs: ["model"],
5149
+ description: "Two background entries that say things that cannot both be true at the same time: the closest in meaning are found with the embedding provider, a decision model screens them, and the language model must quote the passage of each that conflicts.",
5150
+ async run(input) {
4333
5151
  const model = input.model;
4334
- const tr = trOf(model);
4335
- const source = basis(evidence);
4336
- const world = await readWorldText(input.world.directory);
4337
- const made = madeInto(source, world, "about");
4338
- const from = /* @__PURE__ */ new Map();
4339
- for (const piece of usedPieces(source)) for (const where of made.get(baseId(piece.id)) ?? []) from.set(where, [...from.get(where) ?? [], piece]);
4340
- const ranked = [...from].map(([where, pieces]) => ({
4341
- where,
4342
- pieces,
4343
- size: pieces.reduce((sum, piece) => sum + piece.text.length, 0)
4344
- })).sort((a, b) => b.size - a.size);
4345
- const chosen = ranked.slice(0, MAX_COMPARED);
5152
+ const { lore } = await loaded.lore(input);
5153
+ const apart = (a, b) => {
5154
+ const x = lore[a];
5155
+ const y = lore[b];
5156
+ return x.endsAt !== void 0 && y.startsAt !== void 0 && x.endsAt < y.startsAt || y.endsAt !== void 0 && x.startsAt !== void 0 && y.endsAt < x.startsAt;
5157
+ };
5158
+ const near = await candidates(model, input.world.directory, lore.map(loreText), (a, b) => !apart(a, b), LORE_CLOSE, PAIRS_ASKED);
5159
+ if (near === void 0) throw new CheckSkipped("no embedding provider is selected to find which entries to compare");
5160
+ const pairs = near.map((entry) => ({
5161
+ near: entry,
5162
+ first: `entry ${lore[entry.a].id}:\n${loreText(lore[entry.a])}`,
5163
+ second: `entry ${lore[entry.b].id}:\n${loreText(lore[entry.b])}`
5164
+ }));
5165
+ const screened = model.decide === void 0 ? pairs : (await asked(model, pairs, "checks/decide-conflict", "conflict")).map((entry) => entry.pair);
5166
+ const instructions = model.text("checks/conflict");
4346
5167
  const found = [];
4347
- if (ranked.length > chosen.length) found.push({
4348
- severity: "info",
4349
- code: "not-compared",
4350
- where: "(the world)",
4351
- message: `${ranked.length - chosen.length} more documents were not compared: a run compares at most ${MAX_COMPARED}, the ones with the most source first`,
4352
- hint: "Compare the rest with check_card again after fixing these, or read them against the card yourself."
4353
- });
4354
- await mapLimit(chosen, model.concurrency, async ({ where, pieces }) => {
5168
+ await mapLimit(screened.slice(0, 12), model.concurrency, async (pair) => {
4355
5169
  if (model.cancelled?.() === true) return;
4356
- const document = world.prose.get(where) ?? "";
4357
- const subject = world.subjects.get(where) ?? where;
4358
- const text = pieces.map((piece) => `[${piece.label}]\n${piece.text.slice(0, SOURCE_CHARS)}`).join("\n\n").slice(0, SOURCE_CHARS * 2);
5170
+ const first = loreText(lore[pair.near.a]);
5171
+ const second = loreText(lore[pair.near.b]);
4359
5172
  try {
4360
5173
  const answer = await model.run({
4361
- persona: model.text("checks/fidelity"),
4362
- prompt: `${tr("Source pieces:")}\n${text}\n\n${tr("Document ({where}), about {subject}:", {
4363
- where,
4364
- subject
4365
- })}\n${document}`,
4366
- result: FIDELITY_SCHEMA,
4367
- maxTokens: 1500,
4368
- check: (value) => comparisonProblem(value, text, document)
4369
- });
4370
- const once = (list) => list.filter((entry, at) => list.findIndex((other) => squashed(other.quote) === squashed(entry.quote)) === at);
4371
- for (const entry of once(answer.dropped)) found.push({
4372
- severity: "warning",
4373
- code: "fact-dropped",
4374
- where,
4375
- message: `the card says “${snippet(entry.quote)}” (${entry.fact}), which ${where} does not say`,
4376
- hint: "Read the piece with card_read; if the fact matters to the story, add it to the document as the card has it."
5174
+ persona: instructions,
5175
+ prompt: `${pair.first}\n\n${pair.second}`,
5176
+ result: CONFLICT_SCHEMA,
5177
+ maxTokens: 800,
5178
+ check: (value) => conflictProblem(value, first, second, [lore[pair.near.a].title, lore[pair.near.b].title])
4377
5179
  });
4378
- for (const entry of once(answer.invented)) found.push({
5180
+ if (!answer.conflict) return;
5181
+ const a = lore[pair.near.a];
5182
+ const b = lore[pair.near.b];
5183
+ found.push({
4379
5184
  severity: "warning",
4380
- code: "fact-invented",
4381
- where,
4382
- message: `${where} says “${snippet(entry.quote)}” (${entry.fact}), which the card does not give`,
4383
- hint: "Remove it unless the person wants it; the world should not state what its source does not."
5185
+ code: "lore-conflict",
5186
+ where: `lore/${a.id}`,
5187
+ message: `lore/${a.id} says “${answer.first_quote.trim()}” and lore/${b.id} says “${answer.second_quote.trim()}”${because(answer.reason)}`,
5188
+ hint: "Read both entries. Keep what the card or the person says is true and correct the other; if both are true at different times, give each its dates."
4384
5189
  });
4385
5190
  } catch (error) {
4386
5191
  if (model.cancelled?.() === true) throw error;
4387
- found.push({
4388
- severity: "info",
4389
- code: "not-compared",
4390
- where,
4391
- message: `${where} could not be compared: ${error instanceof Error ? error.message : String(error)}`
4392
- });
4393
5192
  }
4394
5193
  });
4395
- return foundBy("card-fidelity", found);
5194
+ return foundBy("lore-conflict", found);
4396
5195
  }
4397
5196
  }
4398
5197
  ];
4399
- // src/host/worlds/world-info.ts
4400
- const textOf = (value) => typeof value === "string" ? value : "";
4401
- const toolsOf = (value) => {
4402
- const board = value === void 0 ? {} : parseSwitchboard(value, "tools");
4403
- return typeof board === "string" ? {} : board;
4404
- };
4405
- function infoOf(document) {
4406
- const opening = document["opening_message"];
4407
- const parts = Array.isArray(opening) && opening.some((part) => typeof part === "object" && part !== null && part["type"] === "utterance");
4408
- const plain = typeof opening === "string" ? opening : Array.isArray(opening) ? opening.map((part) => textOf(part?.["text"])).filter((text) => text !== "").join("\n\n") : "";
4409
- const metadata = typeof document["metadata"] === "object" && document["metadata"] !== null ? document["metadata"] : {};
4410
- const author = typeof document["author"] === "object" && document["author"] !== null ? document["author"] : {};
4411
- const rules = document["rules"];
4412
- return {
4413
- name: textOf(document["name"]),
4414
- description: textOf(document["description"]),
4415
- tags: Array.isArray(metadata["tags"]) ? metadata["tags"].filter((tag) => typeof tag === "string") : [],
4416
- rules: typeof rules === "string" ? [rules] : Array.isArray(rules) ? rules.filter((rule) => typeof rule === "string") : [],
4417
- opening: plain,
4418
- openingParts: parts,
4419
- authorName: textOf(author["name"]),
4420
- authorUrl: textOf(author["url"]),
4421
- timeZone: textOf(document["time_zone"]),
4422
- startingTime: textOf(document["starting_time"]),
4423
- preset: textOf(document["preset"]),
4424
- lorebooks: Array.isArray(document["lorebooks"]) ? document["lorebooks"].filter((name) => typeof name === "string") : [],
4425
- version: typeof document["version"] === "number" ? String(document["version"]) : textOf(document["version"]),
4426
- sampling: parseSampling(document["sampling"], "sampling", []),
4427
- tools: toolsOf(document["tools"]),
4428
- alternateOpenings: Array.isArray(document["alternate_openings"]) ? document["alternate_openings"].flatMap((entry) => typeof entry === "object" && entry !== null && typeof entry["text"] === "string" ? [{
4429
- title: textOf(entry["title"]),
4430
- text: entry["text"],
4431
- ...textOf(entry["character"]) === "" ? {} : { character: textOf(entry["character"]) }
4432
- }] : []) : []
4433
- };
4434
- }
4435
- async function readDocument(directory) {
4436
- const text = await readFile(join(directory, WORLD_FILE_NAME), "utf8");
4437
- return {
4438
- text,
4439
- document: JSON.parse(text.replace(/^/, ""))
4440
- };
4441
- }
4442
- async function readWorldInfo(directory) {
4443
- return infoOf((await readDocument(directory)).document);
4444
- }
4445
- function setOrRemove(document, key, value) {
4446
- if (value === "") delete document[key];
4447
- else document[key] = value;
4448
- }
4449
- /**
4450
- * Changes the fields of `world.json` the edit names and nothing else, keeping the file's other fields and its indentation. The result is read
4451
- * back as the world will be read before it is written.
4452
- * @throws {InvalidEditProblems} when the world would no longer be valid.
4453
- */
4454
- function editWorldInfo(directory, edit) {
4455
- return serialized(directory, () => changeWorldInfo(directory, edit));
4456
- }
4457
- async function changeWorldInfo(directory, edit) {
4458
- const { text, document } = await readDocument(directory);
4459
- const before = infoOf(document);
4460
- 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"]);
4461
- if (edit.name !== void 0) document["name"] = edit.name;
4462
- if (edit.description !== void 0) document["description"] = edit.description;
4463
- if (edit.tags !== void 0) document["metadata"] = {
4464
- ...typeof document["metadata"] === "object" && document["metadata"] !== null ? document["metadata"] : {},
4465
- tags: edit.tags
4466
- };
4467
- if (edit.rules !== void 0) {
4468
- if (edit.rules.length === 0) delete document["rules"];
4469
- else document["rules"] = edit.rules;
4470
- }
4471
- if (edit.opening !== void 0 && !before.openingParts) setOrRemove(document, "opening_message", edit.opening);
4472
- if (edit.authorName !== void 0 || edit.authorUrl !== void 0) {
4473
- const name = edit.authorName ?? before.authorName;
4474
- const url = edit.authorUrl ?? before.authorUrl;
4475
- if (name === "" && url !== "") throw new InvalidEditProblems(["an author link needs the author's name"]);
4476
- if (name === "") delete document["author"];
4477
- else document["author"] = {
4478
- ...typeof document["author"] === "object" && document["author"] !== null ? document["author"] : {},
4479
- name,
4480
- ...url === "" ? {} : { url }
4481
- };
4482
- if (url === "" && typeof document["author"] === "object") delete document["author"]["url"];
4483
- }
4484
- if (edit.timeZone !== void 0) setOrRemove(document, "time_zone", edit.timeZone);
4485
- if (edit.startingTime !== void 0) setOrRemove(document, "starting_time", edit.startingTime);
4486
- if (edit.version !== void 0) setOrRemove(document, "version", edit.version);
4487
- if (edit.preset !== void 0) setOrRemove(document, "preset", edit.preset);
4488
- if (edit.lorebooks !== void 0) {
4489
- if (edit.lorebooks.length === 0) delete document["lorebooks"];
4490
- else document["lorebooks"] = edit.lorebooks;
4491
- }
4492
- if (edit.alternateOpenings !== void 0) {
4493
- if (edit.alternateOpenings.length === 0) delete document["alternate_openings"];
4494
- else document["alternate_openings"] = edit.alternateOpenings.map((entry) => ({
4495
- title: entry.title === "" ? entry.text.slice(0, 40) : entry.title,
4496
- text: entry.text,
4497
- ...entry.character === void 0 || entry.character === "" ? {} : { character: entry.character }
4498
- }));
4499
- }
4500
- if (edit.sampling !== void 0) {
4501
- if (Object.keys(edit.sampling).length === 0) delete document["sampling"];
4502
- else document["sampling"] = edit.sampling;
4503
- }
4504
- if (edit.tools !== void 0) {
4505
- if (Object.keys(edit.tools).length === 0) delete document["tools"];
4506
- else document["tools"] = edit.tools;
4507
- }
4508
- const next = JSON.stringify(document, null, /^([ \t]+)"/m.exec(text)?.[1] ?? " ") + (text.endsWith("\n") ? "\n" : "");
4509
- const parsed = parseWorldFile(next);
4510
- if (!parsed.ok) throw new InvalidEditProblems(parsed.issues.map(formatIssue));
4511
- await atomicWrite(join(directory, WORLD_FILE_NAME), next);
4512
- return infoOf(document);
4513
- }
4514
- // src/host/checks/judge.ts
4515
- /**
4516
- * 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
4517
- * question to the language model. The decision model is never the only reader of a thing that may be wrong.
4518
- */
4519
- async function verdictOf(model, state, key, instructions, ask) {
4520
- if (model.decide !== void 0) try {
4521
- const probability = (await model.decide(state, [{
4522
- key,
4523
- instructions
4524
- }])).get(key);
4525
- const confidence = model.confidence ?? .7;
4526
- if (probability !== void 0 && probability >= yesAt(confidence)) return {
4527
- yes: true,
4528
- by: "decision"
4529
- };
4530
- if (probability !== void 0 && probability <= noAt(confidence)) return {
4531
- yes: false,
4532
- by: "decision"
4533
- };
4534
- } catch (error) {
4535
- if (model.cancelled?.() === true) throw error;
4536
- }
4537
- return {
4538
- yes: await ask(),
4539
- by: "language"
4540
- };
4541
- }
4542
5198
  // src/host/checks/quality.ts
4543
5199
  /** A place described in fewer characters than this has nothing to play with. */
4544
5200
  const THIN_PLACE = 20;
@@ -4552,8 +5208,8 @@ const cast = {
4552
5208
  mode: "code",
4553
5209
  needs: [],
4554
5210
  description: "Whether the cast can be played: someone to play, sheets with more than a summary for the characters that matter, voices, and characters that are one description twice.",
4555
- async run({ world }) {
4556
- const { characters } = await loadCharacters(world.directory);
5211
+ async run(input) {
5212
+ const { characters } = await loaded.characters(input);
4557
5213
  if (characters.length === 0) throw new CheckSkipped("the world has no characters yet");
4558
5214
  const found = [];
4559
5215
  if (!characters.some((character) => character.role === "playable")) found.push({
@@ -4602,8 +5258,8 @@ const places = {
4602
5258
  mode: "code",
4603
5259
  needs: [],
4604
5260
  description: "Places with no description, or one too short to play in.",
4605
- async run({ world }) {
4606
- const { locations } = await loadLocations(world.directory);
5261
+ async run(input) {
5262
+ const { locations } = await loaded.locations(input);
4607
5263
  return foundBy("quality-places", locations.flatMap((place) => {
4608
5264
  const length = text(place.description).length;
4609
5265
  if (length === 0) return [{
@@ -4627,8 +5283,8 @@ const about = {
4627
5283
  mode: "code",
4628
5284
  needs: [],
4629
5285
  description: "What the world says about itself: a description, tags for the catalogue, an opening for the story.",
4630
- async run({ world }) {
4631
- const info = await readWorldInfo(world.directory);
5286
+ async run(input) {
5287
+ const info = await loaded.info(input);
4632
5288
  const found = [];
4633
5289
  if (text(info.description) === "") found.push({
4634
5290
  severity: "info",
@@ -4666,8 +5322,9 @@ const sheets = {
4666
5322
  mode: "decision",
4667
5323
  needs: ["model"],
4668
5324
  description: "Whether each principal or supporting character's sheet is enough to play them (who they are, how they talk, what they want). A decision model may answer; what it is unsure of goes to the language model.",
4669
- async run({ world, model }) {
4670
- const { characters } = await loadCharacters(world.directory);
5325
+ async run(input) {
5326
+ const { model } = input;
5327
+ const { characters } = await loaded.characters(input);
4671
5328
  const asked = characters.filter((character) => character.tier !== "background" && character.kind === "person");
4672
5329
  const found = [];
4673
5330
  await mapLimit(asked, model.concurrency, async (character) => {
@@ -4735,13 +5392,14 @@ const qualityChecks = [
4735
5392
  mode: "judged",
4736
5393
  needs: ["model"],
4737
5394
  description: "What a model reading a digest of the world finds missing for it to play well: places with nothing to do, conflicts nobody carries, openings the world never sets up.",
4738
- async run({ world, model }) {
5395
+ async run(input) {
5396
+ const { world, model } = input;
4739
5397
  const [{ characters }, { locations }, { factions }, { lore }, { defs }] = await Promise.all([
4740
- loadCharacters(world.directory),
4741
- loadLocations(world.directory),
4742
- loadFactions(world.directory),
4743
- loadLore(world.directory),
4744
- loadVariables(world.directory)
5398
+ loaded.characters(input),
5399
+ loaded.locations(input),
5400
+ loaded.factions(input),
5401
+ loaded.lore(input),
5402
+ loaded.variables(input)
4745
5403
  ]);
4746
5404
  const named = /* @__PURE__ */ new Map();
4747
5405
  for (const [kind, entries] of [
@@ -4770,7 +5428,7 @@ const qualityChecks = [
4770
5428
  /** What the model named, as the key of that thing; anything else is about the world as a whole. */
4771
5429
  const keyOf = (about) => named.get(norm(about)) ?? "(the world)";
4772
5430
  const digest = [
4773
- `world: ${world.summary.name}: ${text((await readWorldInfo(world.directory)).description)}`,
5431
+ `world: ${world.summary.name}: ${text((await loaded.info(input)).description)}`,
4774
5432
  ...characters.map((entry) => `character ${entry.id} (${entry.role}, ${entry.tier}): ${entry.name}: ${entry.summary}`),
4775
5433
  ...locations.map((entry) => `place ${entry.id}: ${entry.name}: ${text(entry.description).slice(0, 160)} (${entry.exits.length} ways out)`),
4776
5434
  ...factions.map((entry) => `organisation ${entry.id}: ${entry.name}: ${entry.summary}`),
@@ -4794,65 +5452,31 @@ const qualityChecks = [
4794
5452
  }
4795
5453
  }
4796
5454
  ];
4797
- // src/host/checks/results.ts
4798
- /** Where a world keeps what its checks found: a dot folder, so what is published leaves it out, like the import's. */
4799
- const CHECKS_FOLDER = ".checks";
4800
- const FILE = "results.json";
4801
- async function read(directory) {
4802
- try {
4803
- const stored = JSON.parse(await readFile(join(directory, CHECKS_FOLDER, FILE), "utf8"));
4804
- return stored.version === 1 && typeof stored.checks === "object" && stored.checks !== null ? stored : {
4805
- version: 1,
4806
- checks: {}
4807
- };
4808
- } catch (error) {
4809
- if (error.code === "ENOENT" || error instanceof SyntaxError) return {
4810
- version: 1,
4811
- checks: {}
4812
- };
4813
- throw error;
4814
- }
4815
- }
4816
- /**
4817
- * Stores the findings of the checks that ran, replacing their last ones, and says what changed against those: results of checks that did not run stay.
4818
- * @returns `undefined` when none of these checks had run before.
4819
- */
4820
- async function recordResults(directory, at, results) {
4821
- const stored = await read(directory);
4822
- const before = Object.keys(results).flatMap((id) => stored.checks[id]?.findings ?? []);
4823
- const known = Object.keys(results).some((id) => stored.checks[id] !== void 0);
4824
- const now = Object.values(results).flat();
4825
- const was = new Set(before.map(findingKey));
4826
- const is = new Set(now.map(findingKey));
4827
- for (const [id, findings] of Object.entries(results)) stored.checks[id] = {
4828
- at,
4829
- findings
4830
- };
4831
- await atomicWrite(join(directory, CHECKS_FOLDER, FILE), JSON.stringify(stored, null, 2), { createDirectory: true });
4832
- return known ? {
4833
- added: now.filter((finding) => !was.has(findingKey(finding))).length,
4834
- resolved: before.filter((finding) => !is.has(findingKey(finding)))
4835
- } : void 0;
4836
- }
4837
5455
  // src/host/checks/run.ts
4838
5456
  /** Every check of the suite, grouped by kit; the kits are the unit a command or a tool asks for. */
4839
5457
  const CHECKS = [
4840
5458
  ...consistencyChecks,
5459
+ ...semanticChecks,
4841
5460
  ...fidelityChecks,
4842
- ...qualityChecks
5461
+ ...qualityChecks,
5462
+ ...playabilityChecks
4843
5463
  ];
4844
5464
  const checksOf = (kits, checks = CHECKS) => checks.filter((check) => kits.includes(check.kit));
4845
5465
  /**
4846
5466
  * Runs the checks asked for in order (a check that cannot run on this world, or lacks what it needs, is listed as skipped with the reason), stores what each found and
4847
5467
  * reports it with what changed since the checks last ran.
4848
5468
  */
4849
- async function runChecks(input, run = {}, checks = CHECKS, now = () => /* @__PURE__ */ new Date()) {
5469
+ async function runChecks(given, run = {}, checks = CHECKS, now = () => /* @__PURE__ */ new Date()) {
5470
+ const input = {
5471
+ ...given,
5472
+ memo: /* @__PURE__ */ new Map()
5473
+ };
4850
5474
  const kits = run.kits ?? KITS;
4851
5475
  const chosen = checksOf(kits, checks).filter((check) => run.ids === void 0 || run.ids.includes(check.id));
4852
5476
  const results = {};
4853
5477
  const skipped = [];
4854
5478
  for (const check of chosen) {
4855
- if (check.mode !== "code" && run.deep !== true) {
5479
+ if (check.mode !== "code" && run.deep !== true && !(run.deep === "decision" && check.mode === "decision")) {
4856
5480
  skipped.push({
4857
5481
  check: check.id,
4858
5482
  reason: "judged by a model: ask for a deep check to run it"
@@ -4893,14 +5517,30 @@ async function runChecks(input, run = {}, checks = CHECKS, now = () => /* @__PUR
4893
5517
  /** The most findings a tool result lists: the rest are counted. */
4894
5518
  const LISTED = 40;
4895
5519
  const RESOLVED_LISTED = 10;
5520
+ /** More findings than this of one kind are told as one, with the others' places, so a big world's hundred alike do not bury the rest. */
5521
+ const GROUPED_AT = 5;
5522
+ /** The findings as the assistant reads them: those of one check and code beyond a few are one entry that says how many there are and where. */
5523
+ function grouped(findings) {
5524
+ const kinds = /* @__PURE__ */ new Map();
5525
+ for (const finding of findings) {
5526
+ const key = `${finding.check}\n${finding.code}`;
5527
+ kinds.set(key, [...kinds.get(key) ?? [], finding]);
5528
+ }
5529
+ return [...kinds.values()].flatMap((same) => same.length <= GROUPED_AT ? same : [{
5530
+ ...same[0],
5531
+ count: same.length,
5532
+ also: same.slice(1, 9).map((finding) => finding.where)
5533
+ }]).sort((a, b) => SEVERITIES.indexOf(a.severity) - SEVERITIES.indexOf(b.severity));
5534
+ }
4896
5535
  /** What the assistant reads of a report: counts first, the worst findings, then what changed since the last run. */
4897
5536
  function presentReport(report) {
4898
5537
  const count = (severity) => report.findings.filter((finding) => finding.severity === severity).length;
5538
+ const presented = grouped(report.findings);
4899
5539
  return {
4900
5540
  ok: count("error") === 0,
4901
5541
  counts: Object.fromEntries(SEVERITIES.map((severity) => [severity, count(severity)])),
4902
- findings: report.findings.slice(0, LISTED),
4903
- more: Math.max(0, report.findings.length - LISTED),
5542
+ findings: presented.slice(0, LISTED),
5543
+ more: Math.max(0, presented.length - LISTED),
4904
5544
  skipped: report.skipped,
4905
5545
  ...report.changes === void 0 ? {} : { since_last_run: {
4906
5546
  new: report.changes.added,
@@ -4912,18 +5552,24 @@ function presentReport(report) {
4912
5552
  // src/host/checks/imported.ts
4913
5553
  /**
4914
5554
  * What every import and update is checked with when it is written: all the checks the program decides, against the card the world now comes from, stored with the world. The
4915
- * checks a model judges are not run here: the assistant that reviews the import asks for them (`deep`) when the card and these findings call for it.
5555
+ * checks a model judges are not run here: the assistant that reviews the import asks for them (`deep`) when the card and these findings call for it. The exception is the
5556
+ * yes-or-no checks, which run when the import has a decision model to answer them.
4916
5557
  * @returns what the assistant reads of the report.
4917
5558
  */
4918
- async function checkImported(directory, name) {
5559
+ async function checkImported(directory, name, model) {
4919
5560
  const card = await loadCardEvidence(directory, "current");
5561
+ const deciding = model?.decide !== void 0;
4920
5562
  return presentReport(await runChecks({
4921
5563
  world: {
4922
5564
  directory,
4923
5565
  summary: { name }
4924
5566
  },
4925
- ...card === void 0 ? {} : { card }
4926
- }, { kits: KITS }));
5567
+ ...card === void 0 ? {} : { card },
5568
+ ...deciding ? { model } : {}
5569
+ }, {
5570
+ kits: KITS,
5571
+ ...deciding ? { deep: "decision" } : {}
5572
+ }));
4927
5573
  }
4928
5574
  // src/host/import/run-import.ts
4929
5575
  /** The import in a world is not in the state the step needs, or there is none. The message says what to do. */
@@ -5164,7 +5810,7 @@ async function finish(ctx, world) {
5164
5810
  }, await readFile(join(folder(world), "card.json"), "utf8"));
5165
5811
  return {
5166
5812
  ...result,
5167
- checks: await checkImported(world, choices.name)
5813
+ checks: await checkImported(world, choices.name, ctx)
5168
5814
  };
5169
5815
  }
5170
5816
  return result;
@@ -5304,8 +5950,21 @@ function contextOf(exec, settings, text, language) {
5304
5950
  if (service === void 0) return Promise.reject(/* @__PURE__ */ new Error("no decision service is selected, or the selected one is not loaded"));
5305
5951
  return askYesNo(service, state, asks, exec.signal);
5306
5952
  } : void 0;
5953
+ const embed = async (texts) => {
5954
+ const service = agent.ctx.get("embeddings");
5955
+ if (service === void 0) throw new NoEmbeddingService("no embedding provider is selected, or the selected one is not loaded");
5956
+ const { vectors, model } = await service.embed(texts, {
5957
+ inputType: "document",
5958
+ signal: exec.signal
5959
+ });
5960
+ return {
5961
+ vectors,
5962
+ model
5963
+ };
5964
+ };
5307
5965
  return {
5308
5966
  run,
5967
+ embed,
5309
5968
  cancelled: () => exec.signal.aborted,
5310
5969
  concurrency: settings.concurrency,
5311
5970
  variablesPerRequest: settings.variablesPerRequest,
@@ -5542,6 +6201,6 @@ function limitAuthoringAgents(ctx, kindOf) {
5542
6201
  return await next();
5543
6202
  });
5544
6203
  }
5545
- export { WORLD_FILE_NAME as $, pictureOf as $t, cardTools as A, routineProblems as At, localChanges as B, loadFactions as Bt, worldFieldChanges as C, populateItems as Ct, UPDATE_FOLDER as D, populateRoutines as Dt, loadCardEvidence as E, populateLore as Et, editsSince as F, LOCATIONS_DIR as Ft, addressesToFetch as G, loadCalendar as Gt, writeOrigin as H, loadDice as Ht, writeSnapshot as I, loadLocations as It, guardedLookup as J, loadCharacters as Jt, candidateUrls as K, parseCalendarFile as Kt, describeChanges as L, ITEMS_DIR as Lt, readSource as M, loadTriggers as Mt, CHECK_COMMANDS as N, LORE_DIR as Nt, baseId as O, populateTriggers as Ot, recordProvenance as P, loadLore as Pt, writePortrait as Q, isPng as Qt, hashWorld as R, loadItems as Rt, matchPieces as S, populateFactions as St, loadWorldParts as T, populateLocations as Tt, CatalogueError as U, parseDiceFile as Ut, readOrigin as V, DICE_FILE_NAME as Vt, addressKey as W, CALENDAR_FILE_NAME as Wt, vetUrl as X, knownOutputCeiling as Xt, mergeReports as Y, subfolders as Yt, cardCharacter as Z, ImportCardError as Zt, runChecks as _, knowsCharacter as _t, importHooks as a, authoringChatSessionId as an, deleteLorebookEntry as at, cardChanges as b, populateClock as bt, importPhase as c, listLorebooks as ct, markReady as d, readLorebook as dt, readCard as en, parseWorldFile as et, settleDraft as f, reserveLorebook as ft, CHECKS as g, loadValidatedWorld as gt, presentReport as h, checkWorld as ht, contextOf as i, authoredWorldOf as in, deleteLorebook as it, entitiesByPiece as j, TRIGGERS_FILE_NAME as jt, cardPieces as k, populateVariables as kt, isReviewing as l, loadLorebook as lt, checkImported as m, writeLorebookEntry as mt, limitAuthoringAgents as n, DICE_READINGS as nn, addLorebookEntries as nt, importTools as o, entriesOf as ot, structureImport as p, updateLorebookInfo as pt, fetchPictures as q, CHARACTERS_DIR as qt, MAX_PREVIEW_TEXT as r, DICE_RESULTS as rn, createLorebook as rt, worldOf as s, isLorebookName as st, AuthoringTools as t, DICE_COMPARISONS as tn, parseWorldInfoEdit as tt, keepDraft as u, lorebookDirectory as ut, editWorldInfo as v, populateCalendar as vt, withScratchStory as w, populateKnowledge as wt, diffText as x, populateDice as xt, readWorldInfo as y, populateCharacters as yt, isUnchanged as z, FACTIONS_DIR as zt };
6204
+ export { WORLD_FILE_NAME as $, pictureOf as $t, cardTools as A, routineProblems as At, localChanges as B, loadFactions as Bt, readWorldInfo as C, populateItems as Ct, UPDATE_FOLDER as D, populateRoutines as Dt, loadCardEvidence as E, populateLore as Et, editsSince as F, LOCATIONS_DIR as Ft, addressesToFetch as G, loadCalendar as Gt, writeOrigin as H, loadDice as Ht, writeSnapshot as I, loadLocations as It, guardedLookup as J, loadCharacters as Jt, candidateUrls as K, parseCalendarFile as Kt, describeChanges as L, ITEMS_DIR as Lt, readSource as M, loadTriggers as Mt, CHECK_COMMANDS as N, LORE_DIR as Nt, baseId as O, populateTriggers as Ot, recordProvenance as P, loadLore as Pt, writePortrait as Q, isPng as Qt, hashWorld as R, loadItems as Rt, editWorldInfo as S, populateFactions as St, loadWorldParts as T, populateLocations as Tt, CatalogueError as U, parseDiceFile as Ut, readOrigin as V, DICE_FILE_NAME as Vt, addressKey as W, CALENDAR_FILE_NAME as Wt, vetUrl as X, knownOutputCeiling as Xt, mergeReports as Y, subfolders as Yt, cardCharacter as Z, ImportCardError as Zt, runChecks as _, knowsCharacter as _t, importHooks as a, authoringChatSessionId as an, deleteLorebookEntry as at, matchPieces as b, populateClock as bt, importPhase as c, listLorebooks as ct, markReady as d, readLorebook as dt, readCard as en, parseWorldFile as et, settleDraft as f, reserveLorebook as ft, CHECKS as g, loadValidatedWorld as gt, presentReport as h, checkWorld as ht, contextOf as i, authoredWorldOf as in, deleteLorebook as it, entitiesByPiece as j, TRIGGERS_FILE_NAME as jt, cardPieces as k, populateVariables as kt, isReviewing as l, loadLorebook as lt, checkImported as m, writeLorebookEntry as mt, limitAuthoringAgents as n, DICE_READINGS as nn, addLorebookEntries as nt, importTools as o, entriesOf as ot, structureImport as p, updateLorebookInfo as pt, fetchPictures as q, CHARACTERS_DIR as qt, MAX_PREVIEW_TEXT as r, DICE_RESULTS as rn, createLorebook as rt, worldOf as s, isLorebookName as st, AuthoringTools as t, DICE_COMPARISONS as tn, parseWorldInfoEdit as tt, keepDraft as u, lorebookDirectory as ut, cardChanges as v, populateCalendar as vt, withScratchStory as w, populateKnowledge as wt, worldFieldChanges as x, populateDice as xt, diffText as y, populateCharacters as yt, isUnchanged as z, FACTIONS_DIR as zt };
5546
6205
 
5547
- //# sourceMappingURL=authoring-tools-CdHlvx32.js.map
6206
+ //# sourceMappingURL=authoring-tools-C_l07RYn.js.map