@dsh-agentic-roleplay/core 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/{access-CfQWbNZP.js → access-CXtaZNbx.js} +50 -2
- package/lib/{authoring-tools-CdHlvx32.js → authoring-tools-B4mEdKp_.js} +1349 -604
- package/lib/authoring.js +23 -19
- package/lib/{child-lYYeOwl2.js → child-Dtv76jNx.js} +116 -11
- package/lib/client.js +2810 -795
- package/lib/client.js.map +1 -1
- package/lib/compaction.js +3 -3
- package/lib/doctor.js +3 -3
- package/lib/{gallery-BKh7eu80.js → gallery-BZJleAeH.js} +5 -9
- package/lib/{generator-DdZ1-sgv.js → generator-Cg6CQc5S.js} +2 -2
- package/lib/helper.js +1 -1
- package/lib/{hooks-CcQj_dJo.js → hooks-DTjHWTfx.js} +168 -28
- package/lib/images.js +4 -4
- package/lib/index.js +362 -33
- package/lib/{library-QFL6Z_Nr.js → library-GIlrkOim.js} +9 -14
- package/lib/{lorebook-author-tools-DWBnYo1E.js → lorebook-author-tools-BCFFJSle.js} +6 -6
- package/lib/lorebook-authoring.js +4 -4
- package/lib/{narration-CjB_KwwY.js → narration-BVzew4-K.js} +7 -52
- package/lib/orchestration.js +155 -10
- package/lib/{pack-jail-BMy6NcCY.js → pack-jail-hsNQNTOf.js} +25 -15
- package/lib/{packs-BREPSfHy.js → packs-UpK_cvI4.js} +2 -2
- package/lib/{presenter-agent-B49-_iUm.js → presenter-agent-DZOgBOR9.js} +12 -17
- package/lib/{preset-BqgY1Tz4.js → preset-Cyoj1ZWa.js} +4 -4
- package/lib/{preset-author-tools-BB_Dcv2o.js → preset-author-tools-RSta6pHK.js} +6 -6
- package/lib/preset-authoring.js +5 -5
- package/lib/{presets-Bwnrx7p0.js → presets-CvpsW_10.js} +17 -9
- package/lib/{reflection-CUWG-6Qg.js → reflection-0qTvw2It.js} +53 -26
- package/lib/{turn-guard-Bb8Ew-1z.js → turn-guard-BzNeGNnc.js} +3 -3
- package/package.json +2 -2
- package/prompts/en/checks/conflict.md +1 -0
- package/prompts/en/checks/decide-conflict.md +1 -0
- package/prompts/en/checks/decide-covered.md +1 -0
- package/prompts/en/checks/decide-opening.md +1 -0
- package/prompts/en/checks/decide-same-thing.md +1 -0
- package/prompts/en/checks/decide-same-value.md +1 -0
- package/prompts/en/checks/decide-sheet.md +1 -1
- package/prompts/en/checks/fidelity.md +1 -0
- package/prompts/en/checks/notice.md +2 -2
- package/prompts/zh/checks/conflict.md +1 -0
- package/prompts/zh/checks/decide-conflict.md +1 -0
- package/prompts/zh/checks/decide-covered.md +1 -0
- package/prompts/zh/checks/decide-opening.md +1 -0
- package/prompts/zh/checks/decide-same-thing.md +1 -0
- package/prompts/zh/checks/decide-same-value.md +1 -0
- package/prompts/zh/checks/decide-sheet.md +1 -1
- package/prompts/zh/checks/fidelity.md +1 -0
- package/prompts/zh/checks/notice.md +2 -2
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import "./access-
|
|
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-
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { n as atomicWrite, r as atomicWriteJson } from "./access-CXtaZNbx.js";
|
|
2
|
+
import { A as sourcesFor, B as noAt, C as inert, Ct as CHARACTER_FILE_NAME, D as pruneLoreSubjects, Dt as InvalidEditProblems, E as pruneLinks, Et as readIdentifier, F as isLeftOut, Ft as LORE_SCOPES, H as yesAt, It as WEEKDAY_NAMES, J as cleanText, L as askYesNo, Lt as DEFAULT_SLOTS, M as classifyItems, N as findEntity, O as pruneMembers, P as ROLES, Pt as LORE_KINDS, Q as IMPORT_FOLDER, S as extractVariables, St as parseFactionFile, T as pruneKnowledge, Tt as readVariableValues, Y as itemise, Z as withPlayer, _ as characterDocument, _t as ITEM_FILE_NAME, a as isPresetName, b as extractStyle, bt as parseSlotsFile, ft as variableProblems, g as asWritten, gt as parseLocationFile, h as addLore, ht as LOCATION_FILE_NAME, it as readImportFolder, j as buildRoster, k as pruneRelationships, lt as VARIABLES_FILE_NAME, mt as parseLoreFile, nt as openingText, pt as LORE_FILE_NAME, st as parseImportCommand, tt as PICTURE_FILE, ut as loadVariables, v as emptyExtracted, vt as SLOTS_FILE_NAME, w as loreDocuments, wt as parseCharacterFile, xt as FACTION_FILE_NAME, y as extractEntities, yt as parseItemFile } from "./presets-CvpsW_10.js";
|
|
3
|
+
import { f as parseSwitchboard } from "./narration-BVzew4-K.js";
|
|
4
|
+
import { G as commandFlagKey, J as Reader, Q as PROMPTS_FOLDER, U as plotReferenceProblems, V as loadPlots, X as errorMessage, Y as formatIssue, a as externalWithheld, at as shippedLanguages, et as copyWorldPrompts, gt as COMPARISONS, it as promptProblem, q as readDocument$1, r as externalGiven, rt as listFiles, st as shippedText, t as externalToolLimit, ut as InvalidWorldError } from "./hooks-DTjHWTfx.js";
|
|
5
5
|
import { t as trimCharacters } from "./text-CbGQhK1-.js";
|
|
6
|
-
import { c as runChild,
|
|
7
|
-
import { C as
|
|
6
|
+
import { c as runChild, s as namedModel, t as ChildTimeoutError, u as parseSampling, v as currentTurn } from "./child-Dtv76jNx.js";
|
|
7
|
+
import { C as slug, S as norm, T as uniqueKey, _ as writeImageFileMetadata, b as NoEmbeddingService, i as libraryHashes, n as addToLibraryOnce, p as MEDIA_TYPES, r as imageExtensionOf, u as referenceImagePath, w as trOf, x as mapLimit } from "./library-GIlrkOim.js";
|
|
8
8
|
import { dirname, join, relative } from "node:path";
|
|
9
9
|
import { existsSync, readFileSync } from "node:fs";
|
|
10
10
|
import { createHash } from "node:crypto";
|
|
@@ -1313,6 +1313,37 @@ function populateTriggers(session, definitions) {
|
|
|
1313
1313
|
})));
|
|
1314
1314
|
for (const definition of definitions) session.triggers.add(definition, "world", 0);
|
|
1315
1315
|
}
|
|
1316
|
+
// src/host/worlds/scratch.ts
|
|
1317
|
+
/**
|
|
1318
|
+
* Runs `use` against an in-memory story made from the world's files, the way a session start fills one, and closes it again. The populate functions only use the repositories
|
|
1319
|
+
* a store has; the search a session adds is not needed.
|
|
1320
|
+
*/
|
|
1321
|
+
function withScratchStory(parts, use) {
|
|
1322
|
+
const scratch = createSessionStore(":memory:", {
|
|
1323
|
+
id: "scratch",
|
|
1324
|
+
name: "scratch",
|
|
1325
|
+
world: {
|
|
1326
|
+
id: "scratch",
|
|
1327
|
+
name: "scratch"
|
|
1328
|
+
}
|
|
1329
|
+
}, { vectors: false });
|
|
1330
|
+
try {
|
|
1331
|
+
const session = scratch;
|
|
1332
|
+
if (parts.calendar !== void 0) writeCalendar(scratch.metadata, parts.calendar);
|
|
1333
|
+
populateLocations(session, parts.locations);
|
|
1334
|
+
populateCharacters(session, parts.characters, parts.characters.find((character) => character.role === "playable")?.id ?? "");
|
|
1335
|
+
populateItems(session, parts.items, parts.slots, parts.locations);
|
|
1336
|
+
populateVariables(session, parts.variables, {
|
|
1337
|
+
characters: parts.characters,
|
|
1338
|
+
items: parts.items,
|
|
1339
|
+
locations: parts.locations,
|
|
1340
|
+
values: void 0
|
|
1341
|
+
});
|
|
1342
|
+
return use(session);
|
|
1343
|
+
} finally {
|
|
1344
|
+
scratch.close();
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1316
1347
|
// src/host/worlds/validated-world.ts
|
|
1317
1348
|
/**
|
|
1318
1349
|
* Reads everything a world's files hold and checks it all as a session start does: every file reads, and what refers to something refers to something that is there.
|
|
@@ -1408,6 +1439,16 @@ async function loadValidatedWorld(world) {
|
|
|
1408
1439
|
path: world.directory,
|
|
1409
1440
|
message
|
|
1410
1441
|
})));
|
|
1442
|
+
withScratchStory({
|
|
1443
|
+
characters,
|
|
1444
|
+
locations,
|
|
1445
|
+
items,
|
|
1446
|
+
slots,
|
|
1447
|
+
variables: declared.defs,
|
|
1448
|
+
calendar
|
|
1449
|
+
}, (story) => {
|
|
1450
|
+
populateTriggers(story, scripted.triggers);
|
|
1451
|
+
});
|
|
1411
1452
|
return {
|
|
1412
1453
|
characters,
|
|
1413
1454
|
factions,
|
|
@@ -3046,19 +3087,19 @@ async function editsSince(directory) {
|
|
|
3046
3087
|
};
|
|
3047
3088
|
}
|
|
3048
3089
|
// src/host/import/provenance.ts
|
|
3049
|
-
const FILE$
|
|
3090
|
+
const FILE$2 = "provenance.json";
|
|
3050
3091
|
const PROVENANCE_SCHEMA = "dsh-roleplay-import/v1";
|
|
3051
3092
|
/** The import record of a world, or nothing for a world imported before records were kept. */
|
|
3052
3093
|
async function readProvenance(directory) {
|
|
3053
3094
|
let text;
|
|
3054
3095
|
try {
|
|
3055
|
-
text = await readFile(join(directory, IMPORT_FOLDER, FILE$
|
|
3096
|
+
text = await readFile(join(directory, IMPORT_FOLDER, FILE$2), "utf8");
|
|
3056
3097
|
} catch (error) {
|
|
3057
3098
|
if (error.code === "ENOENT") return void 0;
|
|
3058
3099
|
throw error;
|
|
3059
3100
|
}
|
|
3060
3101
|
const value = JSON.parse(text);
|
|
3061
|
-
if (value.schema !== "dsh-roleplay-import/v1" || !Array.isArray(value.history)) throw new Error(`${FILE$
|
|
3102
|
+
if (value.schema !== "dsh-roleplay-import/v1" || !Array.isArray(value.history)) throw new Error(`${FILE$2} is not an import record this version reads`);
|
|
3062
3103
|
return value;
|
|
3063
3104
|
}
|
|
3064
3105
|
/** Adds one line to the world's import record: what card it came from and, for an import, what was written. */
|
|
@@ -3072,7 +3113,7 @@ async function recordProvenance(directory, entry, cardJson) {
|
|
|
3072
3113
|
sha256: createHash("sha256").update(cardJson).digest("hex")
|
|
3073
3114
|
}
|
|
3074
3115
|
};
|
|
3075
|
-
await atomicWriteJson(join(directory, IMPORT_FOLDER, FILE$
|
|
3116
|
+
await atomicWriteJson(join(directory, IMPORT_FOLDER, FILE$2), {
|
|
3076
3117
|
schema: PROVENANCE_SCHEMA,
|
|
3077
3118
|
history: [...earlier?.history ?? [], line]
|
|
3078
3119
|
});
|
|
@@ -3082,7 +3123,8 @@ async function recordProvenance(directory, entry, cardJson) {
|
|
|
3082
3123
|
const KITS = [
|
|
3083
3124
|
"consistency",
|
|
3084
3125
|
"fidelity",
|
|
3085
|
-
"quality"
|
|
3126
|
+
"quality",
|
|
3127
|
+
"playability"
|
|
3086
3128
|
];
|
|
3087
3129
|
const SEVERITIES = [
|
|
3088
3130
|
"error",
|
|
@@ -3091,19 +3133,12 @@ const SEVERITIES = [
|
|
|
3091
3133
|
];
|
|
3092
3134
|
/** The commands of an authoring chat that start checks, and the kits each runs. */
|
|
3093
3135
|
const CHECK_COMMANDS = {
|
|
3094
|
-
check:
|
|
3095
|
-
|
|
3096
|
-
"fidelity",
|
|
3097
|
-
"quality"
|
|
3098
|
-
],
|
|
3099
|
-
"check-all": [
|
|
3100
|
-
"consistency",
|
|
3101
|
-
"fidelity",
|
|
3102
|
-
"quality"
|
|
3103
|
-
],
|
|
3136
|
+
check: KITS,
|
|
3137
|
+
"check-all": KITS,
|
|
3104
3138
|
"check-world": ["consistency"],
|
|
3105
3139
|
"check-card": ["fidelity"],
|
|
3106
|
-
"check-quality": ["quality"]
|
|
3140
|
+
"check-quality": ["quality"],
|
|
3141
|
+
"check-play": ["playability"]
|
|
3107
3142
|
};
|
|
3108
3143
|
const findingKey = (finding) => `${finding.check}\n${finding.code}\n${finding.where}\n${finding.message}`;
|
|
3109
3144
|
// src/host/import/card-tools.ts
|
|
@@ -3454,12 +3489,11 @@ function entitiesByPiece(source) {
|
|
|
3454
3489
|
return made;
|
|
3455
3490
|
}
|
|
3456
3491
|
/** The pieces an entity was made from, as the build chose them: the ones the survey said are about it, then those that name it. */
|
|
3457
|
-
function originOf(card, choices, pieces, records, name) {
|
|
3492
|
+
function originOf(card, choices, pieces, records, name, items = itemise(card, choices)) {
|
|
3458
3493
|
if (records.roster === void 0 || records.classified === void 0) return [];
|
|
3459
3494
|
const entity = findEntity(records.roster, name);
|
|
3460
3495
|
if (entity === void 0) return [];
|
|
3461
3496
|
const names = [entity.name, ...entity.aliases].map(norm);
|
|
3462
|
-
const items = itemise(card, choices);
|
|
3463
3497
|
const chosen = sourcesFor(entity, items, records.classified);
|
|
3464
3498
|
const about = (id) => (records.classified?.[id]?.targets ?? []).some((target) => target.kind === entity.kind && names.includes(norm(target.name)));
|
|
3465
3499
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -3499,6 +3533,8 @@ function shareOf(of, within) {
|
|
|
3499
3533
|
}
|
|
3500
3534
|
/** A text as names are looked up in it: width- and case-folded. */
|
|
3501
3535
|
const folded = (text) => text.normalize("NFKC").toLowerCase();
|
|
3536
|
+
/** A text as a quote of it is looked up: folded, with runs of white space as one space. */
|
|
3537
|
+
const squashed = (text) => folded(text).replace(/\s+/gu, " ").trim();
|
|
3502
3538
|
// src/host/checks/card-evidence.ts
|
|
3503
3539
|
const exists = async (file) => await stat(file).then(() => true, () => false);
|
|
3504
3540
|
/** @returns the card of the world, or nothing when the world has none (or no newer version waits, for `new`). */
|
|
@@ -3544,20 +3580,31 @@ function madeInto(source, world, only = "any") {
|
|
|
3544
3580
|
faction: "factions",
|
|
3545
3581
|
thing: "items"
|
|
3546
3582
|
};
|
|
3583
|
+
const items = itemise(source.card, source.choices);
|
|
3547
3584
|
for (const entity of source.records.roster ?? []) {
|
|
3548
3585
|
const where = `${group[entity.kind]}/${entity.key}`;
|
|
3549
3586
|
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);
|
|
3587
|
+
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
3588
|
}
|
|
3552
3589
|
const pieces = usedPieces(source).map((piece) => ({
|
|
3553
3590
|
id: baseId(piece.id),
|
|
3554
3591
|
terms: termsOf(piece.text)
|
|
3555
3592
|
}));
|
|
3593
|
+
const holders = /* @__PURE__ */ new Map();
|
|
3594
|
+
pieces.forEach((piece, at) => {
|
|
3595
|
+
for (const term of piece.terms) {
|
|
3596
|
+
const list = holders.get(term);
|
|
3597
|
+
if (list === void 0) holders.set(term, [at]);
|
|
3598
|
+
else list.push(at);
|
|
3599
|
+
}
|
|
3600
|
+
});
|
|
3556
3601
|
for (const [where, text] of world.texts) {
|
|
3557
3602
|
if (!where.startsWith(`${ENTITY_FOLDERS[4].directory}/`) && !where.startsWith(`world.json#`)) continue;
|
|
3558
3603
|
const terms = termsOf(text);
|
|
3559
3604
|
if (terms.size < MIN_TERMS) continue;
|
|
3560
|
-
|
|
3605
|
+
const shared = /* @__PURE__ */ new Map();
|
|
3606
|
+
for (const term of terms) for (const at of holders.get(term) ?? []) shared.set(at, (shared.get(at) ?? 0) + 1);
|
|
3607
|
+
for (const [at, count] of shared) if (count / terms.size >= MADE_FROM) add(pieces[at].id, where);
|
|
3561
3608
|
}
|
|
3562
3609
|
return made;
|
|
3563
3610
|
}
|
|
@@ -3597,240 +3644,498 @@ async function loadWorldParts(directory) {
|
|
|
3597
3644
|
calendar
|
|
3598
3645
|
};
|
|
3599
3646
|
}
|
|
3600
|
-
// src/host/worlds/
|
|
3647
|
+
// src/host/worlds/world-info.ts
|
|
3648
|
+
const textOf = (value) => typeof value === "string" ? value : "";
|
|
3649
|
+
const toolsOf = (value) => {
|
|
3650
|
+
const board = value === void 0 ? {} : parseSwitchboard(value, "tools");
|
|
3651
|
+
return typeof board === "string" ? {} : board;
|
|
3652
|
+
};
|
|
3653
|
+
function infoOf(document) {
|
|
3654
|
+
const opening = document["opening_message"];
|
|
3655
|
+
const parts = Array.isArray(opening) && opening.some((part) => typeof part === "object" && part !== null && part["type"] === "utterance");
|
|
3656
|
+
const plain = typeof opening === "string" ? opening : Array.isArray(opening) ? opening.map((part) => textOf(part?.["text"])).filter((text) => text !== "").join("\n\n") : "";
|
|
3657
|
+
const metadata = typeof document["metadata"] === "object" && document["metadata"] !== null ? document["metadata"] : {};
|
|
3658
|
+
const author = typeof document["author"] === "object" && document["author"] !== null ? document["author"] : {};
|
|
3659
|
+
const rules = document["rules"];
|
|
3660
|
+
return {
|
|
3661
|
+
name: textOf(document["name"]),
|
|
3662
|
+
description: textOf(document["description"]),
|
|
3663
|
+
tags: Array.isArray(metadata["tags"]) ? metadata["tags"].filter((tag) => typeof tag === "string") : [],
|
|
3664
|
+
rules: typeof rules === "string" ? [rules] : Array.isArray(rules) ? rules.filter((rule) => typeof rule === "string") : [],
|
|
3665
|
+
opening: plain,
|
|
3666
|
+
openingParts: parts,
|
|
3667
|
+
authorName: textOf(author["name"]),
|
|
3668
|
+
authorUrl: textOf(author["url"]),
|
|
3669
|
+
timeZone: textOf(document["time_zone"]),
|
|
3670
|
+
startingTime: textOf(document["starting_time"]),
|
|
3671
|
+
preset: textOf(document["preset"]),
|
|
3672
|
+
lorebooks: Array.isArray(document["lorebooks"]) ? document["lorebooks"].filter((name) => typeof name === "string") : [],
|
|
3673
|
+
version: typeof document["version"] === "number" ? String(document["version"]) : textOf(document["version"]),
|
|
3674
|
+
sampling: parseSampling(document["sampling"], "sampling", []),
|
|
3675
|
+
tools: toolsOf(document["tools"]),
|
|
3676
|
+
alternateOpenings: Array.isArray(document["alternate_openings"]) ? document["alternate_openings"].flatMap((entry) => typeof entry === "object" && entry !== null && typeof entry["text"] === "string" ? [{
|
|
3677
|
+
title: textOf(entry["title"]),
|
|
3678
|
+
text: entry["text"],
|
|
3679
|
+
...textOf(entry["character"]) === "" ? {} : { character: textOf(entry["character"]) }
|
|
3680
|
+
}] : []) : []
|
|
3681
|
+
};
|
|
3682
|
+
}
|
|
3683
|
+
async function readDocument(directory) {
|
|
3684
|
+
const text = await readFile(join(directory, WORLD_FILE_NAME), "utf8");
|
|
3685
|
+
return {
|
|
3686
|
+
text,
|
|
3687
|
+
document: JSON.parse(text.replace(/^/, ""))
|
|
3688
|
+
};
|
|
3689
|
+
}
|
|
3690
|
+
async function readWorldInfo(directory) {
|
|
3691
|
+
return infoOf((await readDocument(directory)).document);
|
|
3692
|
+
}
|
|
3693
|
+
function setOrRemove(document, key, value) {
|
|
3694
|
+
if (value === "") delete document[key];
|
|
3695
|
+
else document[key] = value;
|
|
3696
|
+
}
|
|
3601
3697
|
/**
|
|
3602
|
-
*
|
|
3603
|
-
*
|
|
3698
|
+
* 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
|
|
3699
|
+
* back as the world will be read before it is written.
|
|
3700
|
+
* @throws {InvalidEditProblems} when the world would no longer be valid.
|
|
3604
3701
|
*/
|
|
3605
|
-
function
|
|
3606
|
-
|
|
3607
|
-
id: "scratch",
|
|
3608
|
-
name: "scratch",
|
|
3609
|
-
world: {
|
|
3610
|
-
id: "scratch",
|
|
3611
|
-
name: "scratch"
|
|
3612
|
-
}
|
|
3613
|
-
}, { vectors: false });
|
|
3614
|
-
try {
|
|
3615
|
-
const session = scratch;
|
|
3616
|
-
if (parts.calendar !== void 0) writeCalendar(scratch.metadata, parts.calendar);
|
|
3617
|
-
populateLocations(session, parts.locations);
|
|
3618
|
-
populateCharacters(session, parts.characters, parts.characters.find((character) => character.role === "playable")?.id ?? "");
|
|
3619
|
-
populateItems(session, parts.items, parts.slots, parts.locations);
|
|
3620
|
-
populateVariables(session, parts.variables, {
|
|
3621
|
-
characters: parts.characters,
|
|
3622
|
-
items: parts.items,
|
|
3623
|
-
locations: parts.locations,
|
|
3624
|
-
values: void 0
|
|
3625
|
-
});
|
|
3626
|
-
return use(session);
|
|
3627
|
-
} finally {
|
|
3628
|
-
scratch.close();
|
|
3629
|
-
}
|
|
3702
|
+
function editWorldInfo(directory, edit) {
|
|
3703
|
+
return serialized(directory, () => changeWorldInfo(directory, edit));
|
|
3630
3704
|
}
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
const
|
|
3634
|
-
[
|
|
3635
|
-
|
|
3636
|
-
|
|
3637
|
-
|
|
3638
|
-
"
|
|
3639
|
-
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
"
|
|
3644
|
-
"mana",
|
|
3645
|
-
"magicpoints",
|
|
3646
|
-
"magicpower"
|
|
3647
|
-
],
|
|
3648
|
-
[
|
|
3649
|
-
"xp",
|
|
3650
|
-
"exp",
|
|
3651
|
-
"experience",
|
|
3652
|
-
"experiencepoints"
|
|
3653
|
-
],
|
|
3654
|
-
[
|
|
3655
|
-
"gold",
|
|
3656
|
-
"money",
|
|
3657
|
-
"coins",
|
|
3658
|
-
"cash",
|
|
3659
|
-
"currency"
|
|
3660
|
-
],
|
|
3661
|
-
[
|
|
3662
|
-
"stamina",
|
|
3663
|
-
"energy",
|
|
3664
|
-
"endurance"
|
|
3665
|
-
],
|
|
3666
|
-
["sanity", "san"],
|
|
3667
|
-
["level", "lvl"]
|
|
3668
|
-
];
|
|
3669
|
-
const squash = (text) => text.normalize("NFKC").toLowerCase().replace(/[^\p{L}\p{N}]+/gu, "");
|
|
3670
|
-
/** The group of meaning a variable's key or name falls in: a synonym group, else the word itself. */
|
|
3671
|
-
const meanings = (def) => {
|
|
3672
|
-
const found = /* @__PURE__ */ new Set();
|
|
3673
|
-
for (const word of [def.key, def.name].map(squash).filter((word) => word !== "")) {
|
|
3674
|
-
const group = SYNONYMS.findIndex((names) => names.includes(word));
|
|
3675
|
-
found.add(group < 0 ? word : `group:${group}`);
|
|
3705
|
+
async function changeWorldInfo(directory, edit) {
|
|
3706
|
+
const { text, document } = await readDocument(directory);
|
|
3707
|
+
const before = infoOf(document);
|
|
3708
|
+
if (edit.opening !== void 0 && before.openingParts && edit.opening !== before.opening) throw new InvalidEditProblems(["the opening has lines spoken by characters: change it in the file, or ask the authoring assistant"]);
|
|
3709
|
+
if (edit.name !== void 0) document["name"] = edit.name;
|
|
3710
|
+
if (edit.description !== void 0) document["description"] = edit.description;
|
|
3711
|
+
if (edit.tags !== void 0) document["metadata"] = {
|
|
3712
|
+
...typeof document["metadata"] === "object" && document["metadata"] !== null ? document["metadata"] : {},
|
|
3713
|
+
tags: edit.tags
|
|
3714
|
+
};
|
|
3715
|
+
if (edit.rules !== void 0) {
|
|
3716
|
+
if (edit.rules.length === 0) delete document["rules"];
|
|
3717
|
+
else document["rules"] = edit.rules;
|
|
3676
3718
|
}
|
|
3677
|
-
|
|
3678
|
-
|
|
3679
|
-
const
|
|
3680
|
-
|
|
3681
|
-
|
|
3682
|
-
|
|
3683
|
-
|
|
3684
|
-
}
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
const own = meanings(defs[i]);
|
|
3690
|
-
for (let j = i + 1; j < defs.length; j++) if (overlapping(defs[i], defs[j]) && [...meanings(defs[j])].some((meaning) => own.has(meaning))) pairs.push([defs[i], defs[j]]);
|
|
3719
|
+
if (edit.opening !== void 0 && !before.openingParts) setOrRemove(document, "opening_message", edit.opening);
|
|
3720
|
+
if (edit.authorName !== void 0 || edit.authorUrl !== void 0) {
|
|
3721
|
+
const name = edit.authorName ?? before.authorName;
|
|
3722
|
+
const url = edit.authorUrl ?? before.authorUrl;
|
|
3723
|
+
if (name === "" && url !== "") throw new InvalidEditProblems(["an author link needs the author's name"]);
|
|
3724
|
+
if (name === "") delete document["author"];
|
|
3725
|
+
else document["author"] = {
|
|
3726
|
+
...typeof document["author"] === "object" && document["author"] !== null ? document["author"] : {},
|
|
3727
|
+
name,
|
|
3728
|
+
...url === "" ? {} : { url }
|
|
3729
|
+
};
|
|
3730
|
+
if (url === "" && typeof document["author"] === "object") delete document["author"]["url"];
|
|
3691
3731
|
}
|
|
3692
|
-
|
|
3732
|
+
if (edit.timeZone !== void 0) setOrRemove(document, "time_zone", edit.timeZone);
|
|
3733
|
+
if (edit.startingTime !== void 0) setOrRemove(document, "starting_time", edit.startingTime);
|
|
3734
|
+
if (edit.version !== void 0) setOrRemove(document, "version", edit.version);
|
|
3735
|
+
if (edit.preset !== void 0) setOrRemove(document, "preset", edit.preset);
|
|
3736
|
+
if (edit.lorebooks !== void 0) {
|
|
3737
|
+
if (edit.lorebooks.length === 0) delete document["lorebooks"];
|
|
3738
|
+
else document["lorebooks"] = edit.lorebooks;
|
|
3739
|
+
}
|
|
3740
|
+
if (edit.alternateOpenings !== void 0) {
|
|
3741
|
+
if (edit.alternateOpenings.length === 0) delete document["alternate_openings"];
|
|
3742
|
+
else document["alternate_openings"] = edit.alternateOpenings.map((entry) => ({
|
|
3743
|
+
title: entry.title === "" ? entry.text.slice(0, 40) : entry.title,
|
|
3744
|
+
text: entry.text,
|
|
3745
|
+
...entry.character === void 0 || entry.character === "" ? {} : { character: entry.character }
|
|
3746
|
+
}));
|
|
3747
|
+
}
|
|
3748
|
+
if (edit.sampling !== void 0) {
|
|
3749
|
+
if (Object.keys(edit.sampling).length === 0) delete document["sampling"];
|
|
3750
|
+
else document["sampling"] = edit.sampling;
|
|
3751
|
+
}
|
|
3752
|
+
if (edit.tools !== void 0) {
|
|
3753
|
+
if (Object.keys(edit.tools).length === 0) delete document["tools"];
|
|
3754
|
+
else document["tools"] = edit.tools;
|
|
3755
|
+
}
|
|
3756
|
+
const next = JSON.stringify(document, null, /^([ \t]+)"/m.exec(text)?.[1] ?? " ") + (text.endsWith("\n") ? "\n" : "");
|
|
3757
|
+
const parsed = parseWorldFile(next);
|
|
3758
|
+
if (!parsed.ok) throw new InvalidEditProblems(parsed.issues.map(formatIssue));
|
|
3759
|
+
await atomicWrite(join(directory, WORLD_FILE_NAME), next);
|
|
3760
|
+
return infoOf(document);
|
|
3693
3761
|
}
|
|
3694
|
-
|
|
3695
|
-
|
|
3696
|
-
|
|
3697
|
-
|
|
3698
|
-
const
|
|
3699
|
-
|
|
3700
|
-
|
|
3701
|
-
|
|
3702
|
-
|
|
3703
|
-
|
|
3704
|
-
|
|
3705
|
-
|
|
3706
|
-
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
|
|
3710
|
-
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
|
|
3721
|
-
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3762
|
+
// src/host/checks/world-text.ts
|
|
3763
|
+
/** Every string a document holds, joined: what the world says in it, without the structure around it. */
|
|
3764
|
+
const stringsOf = (value) => typeof value === "string" ? [value] : Array.isArray(value) ? value.flatMap(stringsOf) : typeof value === "object" && value !== null ? Object.values(value).flatMap(stringsOf) : [];
|
|
3765
|
+
/** 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. */
|
|
3766
|
+
const STRUCTURE = /* @__PURE__ */ new Set([
|
|
3767
|
+
"id",
|
|
3768
|
+
"role",
|
|
3769
|
+
"tier",
|
|
3770
|
+
"kind",
|
|
3771
|
+
"scope",
|
|
3772
|
+
"type",
|
|
3773
|
+
"status",
|
|
3774
|
+
"control",
|
|
3775
|
+
"known_by",
|
|
3776
|
+
"related",
|
|
3777
|
+
"subjects",
|
|
3778
|
+
"sources",
|
|
3779
|
+
"to",
|
|
3780
|
+
"location",
|
|
3781
|
+
"parent",
|
|
3782
|
+
"held_by",
|
|
3783
|
+
"owner",
|
|
3784
|
+
"members",
|
|
3785
|
+
"knowledge",
|
|
3786
|
+
"relationships",
|
|
3787
|
+
"exits",
|
|
3788
|
+
"start",
|
|
3789
|
+
"visual_identity",
|
|
3790
|
+
"routine",
|
|
3791
|
+
"variables"
|
|
3792
|
+
]);
|
|
3793
|
+
/** What a document says in words, one `field: text` line each, without the structure around it. */
|
|
3794
|
+
function proseOf(value, field = "") {
|
|
3795
|
+
if (typeof value === "string") return value.trim() === "" ? [] : [field === "" ? value : `${field}: ${value}`];
|
|
3796
|
+
if (Array.isArray(value)) return value.flatMap((entry) => proseOf(entry, field));
|
|
3797
|
+
if (typeof value === "object" && value !== null) return Object.entries(value).filter(([key]) => !STRUCTURE.has(key)).flatMap(([key, entry]) => proseOf(entry, key));
|
|
3798
|
+
return [];
|
|
3799
|
+
}
|
|
3800
|
+
async function readJson$1(file) {
|
|
3801
|
+
try {
|
|
3802
|
+
return JSON.parse(await readFile(file, "utf8"));
|
|
3803
|
+
} catch (error) {
|
|
3804
|
+
if (error.code === "ENOENT") return void 0;
|
|
3805
|
+
throw error;
|
|
3806
|
+
}
|
|
3807
|
+
}
|
|
3808
|
+
/** 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. */
|
|
3809
|
+
async function readWorldText(directory) {
|
|
3810
|
+
const texts = /* @__PURE__ */ new Map();
|
|
3811
|
+
const prose = /* @__PURE__ */ new Map();
|
|
3812
|
+
const subjects = /* @__PURE__ */ new Map();
|
|
3813
|
+
const names = [];
|
|
3814
|
+
const KIND = {
|
|
3815
|
+
characters: "character",
|
|
3816
|
+
locations: "place",
|
|
3817
|
+
factions: "organisation",
|
|
3818
|
+
items: "thing",
|
|
3819
|
+
lore: "background entry"
|
|
3820
|
+
};
|
|
3821
|
+
for (const { directory: folder, file } of ENTITY_FOLDERS) for (const id of await subfolders(join(directory, folder))) {
|
|
3822
|
+
const document = await readJson$1(join(directory, folder, id, file)).catch(() => void 0);
|
|
3823
|
+
if (document === void 0 || typeof document !== "object") continue;
|
|
3824
|
+
texts.set(`${folder}/${id}`, stringsOf(document).join("\n"));
|
|
3825
|
+
prose.set(`${folder}/${id}`, proseOf(document).join("\n"));
|
|
3826
|
+
const label = document[folder === "lore" ? "title" : "name"];
|
|
3827
|
+
subjects.set(`${folder}/${id}`, `${KIND[folder]}${typeof label === "string" ? ` ${label}` : ""}`);
|
|
3828
|
+
if (folder !== "lore") {
|
|
3829
|
+
const own = [document["name"], ...Array.isArray(document["aliases"]) ? document["aliases"] : []].filter((name) => typeof name === "string" && name.trim() !== "");
|
|
3830
|
+
names.push({
|
|
3831
|
+
where: `${folder}/${id}`,
|
|
3832
|
+
kind: folder,
|
|
3833
|
+
names: own,
|
|
3834
|
+
playable: document["role"] === "playable"
|
|
3835
|
+
});
|
|
3738
3836
|
}
|
|
3739
|
-
}
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
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
|
-
})));
|
|
3837
|
+
}
|
|
3838
|
+
const info = await readJson$1(join(directory, WORLD_FILE_NAME)).catch(() => void 0);
|
|
3839
|
+
if (typeof info === "object" && info !== null) for (const [field, value] of Object.entries(info)) {
|
|
3840
|
+
const text = stringsOf(value).join("\n");
|
|
3841
|
+
if (text.trim() !== "") {
|
|
3842
|
+
texts.set(`${WORLD_FILE_NAME}#${field}`, text);
|
|
3843
|
+
prose.set(`${WORLD_FILE_NAME}#${field}`, text);
|
|
3844
|
+
subjects.set(`${WORLD_FILE_NAME}#${field}`, `the world's ${field}`);
|
|
3755
3845
|
}
|
|
3846
|
+
}
|
|
3847
|
+
const prompts = join(directory, PROMPTS_FOLDER);
|
|
3848
|
+
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"));
|
|
3849
|
+
return {
|
|
3850
|
+
texts,
|
|
3851
|
+
prose,
|
|
3852
|
+
subjects,
|
|
3853
|
+
names
|
|
3854
|
+
};
|
|
3855
|
+
}
|
|
3856
|
+
// src/host/checks/loaded.ts
|
|
3857
|
+
function once(input, key, load) {
|
|
3858
|
+
if (input.memo === void 0) return load();
|
|
3859
|
+
let loaded = input.memo.get(key);
|
|
3860
|
+
if (loaded === void 0) {
|
|
3861
|
+
loaded = load();
|
|
3862
|
+
input.memo.set(key, loaded);
|
|
3863
|
+
}
|
|
3864
|
+
return loaded;
|
|
3865
|
+
}
|
|
3866
|
+
/** The parts of the world as the checks of one run read them. */
|
|
3867
|
+
const loaded = {
|
|
3868
|
+
validity: (input) => once(input, "validity", () => checkWorld(input.world)),
|
|
3869
|
+
characters: (input) => once(input, "characters", () => loadCharacters(input.world.directory)),
|
|
3870
|
+
locations: (input) => once(input, "locations", () => loadLocations(input.world.directory)),
|
|
3871
|
+
factions: (input) => once(input, "factions", () => loadFactions(input.world.directory)),
|
|
3872
|
+
lore: (input) => once(input, "lore", () => loadLore(input.world.directory)),
|
|
3873
|
+
variables: (input) => once(input, "variables", () => loadVariables(input.world.directory)),
|
|
3874
|
+
info: (input) => once(input, "info", () => readWorldInfo(input.world.directory)),
|
|
3875
|
+
text: (input) => once(input, "text", () => readWorldText(input.world.directory)),
|
|
3876
|
+
/** Which parts of the world each piece of the card the records are about was made into. */
|
|
3877
|
+
made: async (input, card, only = "any") => {
|
|
3878
|
+
const world = await loaded.text(input);
|
|
3879
|
+
return once(input, `made:${only}`, async () => madeInto(card.previous ?? card.source, world, only));
|
|
3756
3880
|
},
|
|
3757
|
-
{
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
|
|
3761
|
-
|
|
3762
|
-
|
|
3763
|
-
|
|
3764
|
-
|
|
3765
|
-
|
|
3766
|
-
|
|
3767
|
-
|
|
3768
|
-
|
|
3769
|
-
|
|
3770
|
-
|
|
3771
|
-
|
|
3772
|
-
|
|
3773
|
-
|
|
3774
|
-
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3778
|
-
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
|
|
3881
|
+
items: async (input) => {
|
|
3882
|
+
const [{ characters }, { locations }] = await Promise.all([loaded.characters(input), loaded.locations(input)]);
|
|
3883
|
+
return once(input, "items", () => loadItems(input.world.directory, {
|
|
3884
|
+
characters: characters.map((character) => character.id),
|
|
3885
|
+
locations
|
|
3886
|
+
}));
|
|
3887
|
+
}
|
|
3888
|
+
};
|
|
3889
|
+
// src/host/checks/consistency.ts
|
|
3890
|
+
/** Words that name one thing a story tracks; a world that declares two of one group tracks it twice. */
|
|
3891
|
+
const SYNONYMS = [
|
|
3892
|
+
[
|
|
3893
|
+
"hp",
|
|
3894
|
+
"health",
|
|
3895
|
+
"hitpoints",
|
|
3896
|
+
"healthpoints",
|
|
3897
|
+
"life",
|
|
3898
|
+
"vitality"
|
|
3899
|
+
],
|
|
3900
|
+
[
|
|
3901
|
+
"mp",
|
|
3902
|
+
"mana",
|
|
3903
|
+
"magicpoints",
|
|
3904
|
+
"magicpower"
|
|
3905
|
+
],
|
|
3906
|
+
[
|
|
3907
|
+
"xp",
|
|
3908
|
+
"exp",
|
|
3909
|
+
"experience",
|
|
3910
|
+
"experiencepoints"
|
|
3911
|
+
],
|
|
3912
|
+
[
|
|
3913
|
+
"gold",
|
|
3914
|
+
"money",
|
|
3915
|
+
"coins",
|
|
3916
|
+
"cash",
|
|
3917
|
+
"currency"
|
|
3918
|
+
],
|
|
3919
|
+
[
|
|
3920
|
+
"stamina",
|
|
3921
|
+
"energy",
|
|
3922
|
+
"endurance"
|
|
3923
|
+
],
|
|
3924
|
+
["sanity", "san"],
|
|
3925
|
+
["level", "lvl"]
|
|
3926
|
+
];
|
|
3927
|
+
/** The group of meaning a variable's key or name falls in: a synonym group, else the word itself. */
|
|
3928
|
+
const meanings = (def) => {
|
|
3929
|
+
const found = /* @__PURE__ */ new Set();
|
|
3930
|
+
for (const word of [def.key, def.name].map(norm).filter((word) => word !== "")) {
|
|
3931
|
+
const group = SYNONYMS.findIndex((names) => names.includes(word));
|
|
3932
|
+
found.add(group < 0 ? word : `group:${group}`);
|
|
3933
|
+
}
|
|
3934
|
+
return found;
|
|
3935
|
+
};
|
|
3936
|
+
const overlapping = (a, b) => {
|
|
3937
|
+
if (a.subject !== b.subject || a.kind !== b.kind) return false;
|
|
3938
|
+
const left = a.only ?? [];
|
|
3939
|
+
const right = b.only ?? [];
|
|
3940
|
+
return left.length === 0 || right.length === 0 || left.some((key) => right.includes(key));
|
|
3941
|
+
};
|
|
3942
|
+
/** Pairs of tracked values that mean the same thing for the same subjects. */
|
|
3943
|
+
function duplicateValues(defs) {
|
|
3944
|
+
const pairs = [];
|
|
3945
|
+
for (let i = 0; i < defs.length; i++) {
|
|
3946
|
+
const own = meanings(defs[i]);
|
|
3947
|
+
for (let j = i + 1; j < defs.length; j++) if (overlapping(defs[i], defs[j]) && [...meanings(defs[j])].some((meaning) => own.has(meaning))) pairs.push([defs[i], defs[j]]);
|
|
3948
|
+
}
|
|
3949
|
+
return pairs;
|
|
3950
|
+
}
|
|
3951
|
+
const FIELDS_LISTED = 6;
|
|
3952
|
+
/** What fixes a finding of the graph lint whose fix is the same every time. */
|
|
3953
|
+
const GRAPH_HINTS = {
|
|
3954
|
+
"character-unplaced": "Give the character a starting place in their own file (start.location); a relationship does not place anyone.",
|
|
3955
|
+
"location-no-exit": "Add a way (an exit) between this place and one the story can reach.",
|
|
3956
|
+
"location-unreachable": "Add a way from a place the story can reach to this one, or from the place the story starts in."
|
|
3957
|
+
};
|
|
3958
|
+
const asSkip = (error) => {
|
|
3959
|
+
if (error instanceof InvalidEditProblems) throw new CheckSkipped("other files of the world have problems that come first");
|
|
3960
|
+
throw error;
|
|
3961
|
+
};
|
|
3962
|
+
const validity = {
|
|
3963
|
+
id: "world-valid",
|
|
3964
|
+
kit: "consistency",
|
|
3965
|
+
mode: "code",
|
|
3966
|
+
needs: [],
|
|
3967
|
+
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).",
|
|
3968
|
+
async run(input) {
|
|
3969
|
+
const check = await loaded.validity(input);
|
|
3970
|
+
const found = check.problems.map((problem) => ({
|
|
3971
|
+
severity: "error",
|
|
3972
|
+
code: "invalid",
|
|
3973
|
+
where: problem.where,
|
|
3974
|
+
message: problem.message,
|
|
3975
|
+
hint: "Fix it, then run the check again: the next part of the world may have problems of its own."
|
|
3976
|
+
}));
|
|
3977
|
+
if (check.more > 0) found.push({
|
|
3978
|
+
severity: "error",
|
|
3979
|
+
code: "invalid",
|
|
3980
|
+
where: "(the world)",
|
|
3981
|
+
message: `${check.more} more problems are not listed`
|
|
3982
|
+
});
|
|
3983
|
+
return foundBy("world-valid", found);
|
|
3984
|
+
}
|
|
3985
|
+
};
|
|
3986
|
+
const graph = {
|
|
3987
|
+
id: "world-graph",
|
|
3988
|
+
kit: "consistency",
|
|
3989
|
+
mode: "code",
|
|
3990
|
+
needs: [],
|
|
3991
|
+
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.",
|
|
3992
|
+
async run(input) {
|
|
3993
|
+
const { world } = input;
|
|
3994
|
+
if (!(await loaded.validity(input)).ok) throw new CheckSkipped("the world has problems that come first (see world-valid)");
|
|
3995
|
+
const parts = await loadWorldParts(world.directory).catch(asSkip);
|
|
3996
|
+
return foundBy("world-graph", withScratchStory(parts, (story) => lintWorld(story).map((finding) => ({
|
|
3997
|
+
severity: finding.severity,
|
|
3998
|
+
code: finding.code,
|
|
3999
|
+
where: `${finding.subject.kind}:${finding.subject.key}`,
|
|
4000
|
+
message: finding.message,
|
|
4001
|
+
...GRAPH_HINTS[finding.code] === void 0 ? {} : { hint: GRAPH_HINTS[finding.code] }
|
|
4002
|
+
}))));
|
|
4003
|
+
}
|
|
4004
|
+
};
|
|
4005
|
+
const values = {
|
|
4006
|
+
id: "duplicate-values",
|
|
4007
|
+
kit: "consistency",
|
|
4008
|
+
mode: "code",
|
|
4009
|
+
needs: [],
|
|
4010
|
+
description: "Tracked values that mean the same thing for the same subjects (health and hp), which the story would keep twice.",
|
|
4011
|
+
async run(input) {
|
|
4012
|
+
const { defs } = await loaded.variables(input);
|
|
4013
|
+
return foundBy("duplicate-values", duplicateValues(defs).map(([a, b]) => ({
|
|
4014
|
+
severity: "warning",
|
|
4015
|
+
code: "duplicate-value",
|
|
4016
|
+
where: "variables.json",
|
|
4017
|
+
message: `${a.key} (${a.name}) and ${b.key} (${b.name}) look like the same tracked value for the same ${a.subject}s`,
|
|
4018
|
+
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."
|
|
4019
|
+
})));
|
|
4020
|
+
}
|
|
4021
|
+
};
|
|
4022
|
+
const names$1 = {
|
|
4023
|
+
id: "name-clashes",
|
|
4024
|
+
kit: "consistency",
|
|
4025
|
+
mode: "code",
|
|
4026
|
+
needs: [],
|
|
4027
|
+
description: "A character's alias that is another character's name or alias, two organisations or two lore entries with one name.",
|
|
4028
|
+
async run(input) {
|
|
4029
|
+
const [{ characters }, { factions }, { lore }] = await Promise.all([
|
|
4030
|
+
loaded.characters(input),
|
|
4031
|
+
loaded.factions(input),
|
|
4032
|
+
loaded.lore(input)
|
|
4033
|
+
]);
|
|
4034
|
+
const found = [];
|
|
4035
|
+
const owners = /* @__PURE__ */ new Map();
|
|
4036
|
+
for (const character of characters) {
|
|
4037
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4038
|
+
for (const label of [character.name, ...character.aliases]) {
|
|
4039
|
+
const word = norm(label);
|
|
4040
|
+
if (word === "" || seen.has(word)) continue;
|
|
4041
|
+
seen.add(word);
|
|
4042
|
+
const other = owners.get(word);
|
|
4043
|
+
if (other !== void 0 && other !== character.id) found.push({
|
|
4044
|
+
severity: "warning",
|
|
4045
|
+
code: "alias-clash",
|
|
4046
|
+
where: `characters/${character.id}`,
|
|
4047
|
+
message: `"${label}" names both ${other} and ${character.id}, so a mention cannot be told apart`,
|
|
4048
|
+
hint: "Give one of them a name or alias of its own."
|
|
4049
|
+
});
|
|
4050
|
+
else owners.set(word, character.id);
|
|
3787
4051
|
}
|
|
3788
|
-
|
|
3789
|
-
|
|
3790
|
-
|
|
3791
|
-
|
|
3792
|
-
|
|
3793
|
-
|
|
3794
|
-
|
|
3795
|
-
|
|
3796
|
-
|
|
3797
|
-
|
|
3798
|
-
}
|
|
3799
|
-
|
|
3800
|
-
|
|
3801
|
-
}
|
|
3802
|
-
|
|
3803
|
-
|
|
3804
|
-
|
|
3805
|
-
|
|
3806
|
-
|
|
3807
|
-
|
|
3808
|
-
|
|
4052
|
+
}
|
|
4053
|
+
const twice = (kind, entries) => {
|
|
4054
|
+
const seen = /* @__PURE__ */ new Map();
|
|
4055
|
+
for (const entry of entries) {
|
|
4056
|
+
const word = norm(entry.label);
|
|
4057
|
+
const other = seen.get(word);
|
|
4058
|
+
if (word !== "" && other !== void 0) found.push({
|
|
4059
|
+
severity: "warning",
|
|
4060
|
+
code: "name-twice",
|
|
4061
|
+
where: `${kind}/${entry.id}`,
|
|
4062
|
+
message: `${kind}/${other} and ${kind}/${entry.id} are both called "${entry.label}"`
|
|
4063
|
+
});
|
|
4064
|
+
else seen.set(word, entry.id);
|
|
4065
|
+
}
|
|
4066
|
+
};
|
|
4067
|
+
twice("factions", factions.map((entry) => ({
|
|
4068
|
+
id: entry.id,
|
|
4069
|
+
label: entry.name
|
|
4070
|
+
})));
|
|
4071
|
+
twice("lore", lore.map((entry) => ({
|
|
4072
|
+
id: entry.id,
|
|
4073
|
+
label: entry.title
|
|
4074
|
+
})));
|
|
4075
|
+
return foundBy("name-clashes", found);
|
|
4076
|
+
}
|
|
4077
|
+
};
|
|
4078
|
+
const prompts = {
|
|
4079
|
+
id: "world-prompts",
|
|
4080
|
+
kit: "consistency",
|
|
4081
|
+
mode: "code",
|
|
4082
|
+
needs: [],
|
|
4083
|
+
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.",
|
|
4084
|
+
async run({ world }) {
|
|
4085
|
+
const scratch = await mkdtemp(join(tmpdir(), "world-prompts-"));
|
|
4086
|
+
try {
|
|
4087
|
+
return foundBy("world-prompts", copyWorldPrompts(world.directory, scratch).map((problem) => ({
|
|
4088
|
+
severity: "error",
|
|
4089
|
+
code: "prompt",
|
|
4090
|
+
where: problem.path.slice(world.directory.length + 1) || problem.path,
|
|
4091
|
+
message: problem.message
|
|
3809
4092
|
})));
|
|
3810
|
-
|
|
4093
|
+
} finally {
|
|
4094
|
+
await rm(scratch, {
|
|
4095
|
+
recursive: true,
|
|
4096
|
+
force: true
|
|
4097
|
+
});
|
|
3811
4098
|
}
|
|
3812
|
-
}
|
|
4099
|
+
}
|
|
4100
|
+
};
|
|
4101
|
+
const PARSERS = {
|
|
4102
|
+
characters: parseCharacterFile,
|
|
4103
|
+
locations: parseLocationFile,
|
|
4104
|
+
factions: parseFactionFile,
|
|
4105
|
+
items: parseItemFile,
|
|
4106
|
+
lore: parseLoreFile
|
|
4107
|
+
};
|
|
4108
|
+
const consistencyChecks = [
|
|
4109
|
+
validity,
|
|
4110
|
+
graph,
|
|
4111
|
+
values,
|
|
4112
|
+
names$1,
|
|
4113
|
+
prompts,
|
|
3813
4114
|
{
|
|
3814
|
-
id: "
|
|
4115
|
+
id: "unread-fields",
|
|
3815
4116
|
kit: "consistency",
|
|
3816
4117
|
mode: "code",
|
|
3817
4118
|
needs: [],
|
|
3818
|
-
description: "
|
|
4119
|
+
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
4120
|
async run({ world }) {
|
|
3820
|
-
const
|
|
3821
|
-
|
|
3822
|
-
|
|
3823
|
-
|
|
3824
|
-
|
|
3825
|
-
|
|
3826
|
-
|
|
3827
|
-
})
|
|
3828
|
-
|
|
3829
|
-
|
|
3830
|
-
|
|
3831
|
-
|
|
4121
|
+
const found = [];
|
|
4122
|
+
for (const { key, directory, file } of ENTITY_FOLDERS) for (const id of await subfolders(join(world.directory, directory)).catch(() => [])) {
|
|
4123
|
+
const text = await readFile(join(world.directory, directory, id, file), "utf8").catch(() => void 0);
|
|
4124
|
+
if (text === void 0) continue;
|
|
4125
|
+
let parsed = false;
|
|
4126
|
+
const { unread: fields, read } = Reader.unreadBy(() => {
|
|
4127
|
+
parsed = PARSERS[key](text, id).ok;
|
|
4128
|
+
});
|
|
4129
|
+
if (!parsed || fields.length === 0) continue;
|
|
4130
|
+
found.push({
|
|
4131
|
+
severity: "warning",
|
|
4132
|
+
code: "field-unread",
|
|
4133
|
+
where: `${directory}/${id}`,
|
|
4134
|
+
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"}`,
|
|
4135
|
+
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
4136
|
});
|
|
3833
4137
|
}
|
|
4138
|
+
return foundBy("unread-fields", found);
|
|
3834
4139
|
}
|
|
3835
4140
|
}
|
|
3836
4141
|
];
|
|
@@ -4006,103 +4311,223 @@ function worldFieldChanges(before, after) {
|
|
|
4006
4311
|
});
|
|
4007
4312
|
return changes;
|
|
4008
4313
|
}
|
|
4009
|
-
// src/host/checks/
|
|
4010
|
-
/**
|
|
4011
|
-
|
|
4012
|
-
|
|
4013
|
-
|
|
4014
|
-
|
|
4015
|
-
|
|
4016
|
-
|
|
4017
|
-
|
|
4018
|
-
|
|
4019
|
-
|
|
4020
|
-
|
|
4021
|
-
|
|
4022
|
-
|
|
4023
|
-
|
|
4024
|
-
|
|
4025
|
-
|
|
4026
|
-
|
|
4027
|
-
|
|
4028
|
-
|
|
4029
|
-
|
|
4030
|
-
|
|
4031
|
-
|
|
4032
|
-
|
|
4033
|
-
|
|
4034
|
-
|
|
4035
|
-
|
|
4036
|
-
|
|
4037
|
-
|
|
4038
|
-
|
|
4039
|
-
|
|
4040
|
-
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
|
|
4045
|
-
|
|
4314
|
+
// src/host/checks/judge.ts
|
|
4315
|
+
/**
|
|
4316
|
+
* 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
|
|
4317
|
+
* question to the language model. The decision model is never the only reader of a thing that may be wrong.
|
|
4318
|
+
*/
|
|
4319
|
+
async function verdictOf(model, state, key, instructions, ask) {
|
|
4320
|
+
if (model.decide !== void 0) try {
|
|
4321
|
+
const probability = (await model.decide(state, [{
|
|
4322
|
+
key,
|
|
4323
|
+
instructions
|
|
4324
|
+
}])).get(key);
|
|
4325
|
+
const confidence = model.confidence ?? .7;
|
|
4326
|
+
if (probability !== void 0 && probability >= yesAt(confidence)) return {
|
|
4327
|
+
yes: true,
|
|
4328
|
+
by: "decision"
|
|
4329
|
+
};
|
|
4330
|
+
if (probability !== void 0 && probability <= noAt(confidence)) return {
|
|
4331
|
+
yes: false,
|
|
4332
|
+
by: "decision"
|
|
4333
|
+
};
|
|
4334
|
+
} catch (error) {
|
|
4335
|
+
if (model.cancelled?.() === true) throw error;
|
|
4336
|
+
}
|
|
4337
|
+
return {
|
|
4338
|
+
yes: await ask(),
|
|
4339
|
+
by: "language"
|
|
4340
|
+
};
|
|
4341
|
+
}
|
|
4342
|
+
/** A decision model's no only rules a pair out when it is this sure: the language model that follows must quote both sides, so passing on a doubtful pair costs a request, and dropping it loses a finding. */
|
|
4343
|
+
const RULED_OUT_CONFIDENCE = .95;
|
|
4344
|
+
/** Whether a pair is worth the language model's reading: everything except what the decision model is very sure is nothing. */
|
|
4345
|
+
async function worthReading(model, state, key, instructions) {
|
|
4346
|
+
if (model.decide === void 0) return true;
|
|
4347
|
+
try {
|
|
4348
|
+
const probability = (await model.decide(state, [{
|
|
4349
|
+
key,
|
|
4350
|
+
instructions
|
|
4351
|
+
}])).get(key);
|
|
4352
|
+
return probability === void 0 || probability > noAt(RULED_OUT_CONFIDENCE);
|
|
4353
|
+
} catch (error) {
|
|
4354
|
+
if (model.cancelled?.() === true) throw error;
|
|
4355
|
+
return true;
|
|
4356
|
+
}
|
|
4357
|
+
}
|
|
4358
|
+
// src/host/checks/results.ts
|
|
4359
|
+
/** Where a world keeps what its checks found: a dot folder, so what is published leaves it out, like the import's. */
|
|
4360
|
+
const CHECKS_FOLDER = ".checks";
|
|
4361
|
+
const FILE$1 = "results.json";
|
|
4362
|
+
async function read$1(directory) {
|
|
4363
|
+
try {
|
|
4364
|
+
const stored = JSON.parse(await readFile(join(directory, CHECKS_FOLDER, FILE$1), "utf8"));
|
|
4365
|
+
return stored.version === 1 && typeof stored.checks === "object" && stored.checks !== null ? stored : {
|
|
4366
|
+
version: 1,
|
|
4367
|
+
checks: {}
|
|
4368
|
+
};
|
|
4369
|
+
} catch (error) {
|
|
4370
|
+
if (error.code === "ENOENT" || error instanceof SyntaxError) return {
|
|
4371
|
+
version: 1,
|
|
4372
|
+
checks: {}
|
|
4373
|
+
};
|
|
4374
|
+
throw error;
|
|
4375
|
+
}
|
|
4376
|
+
}
|
|
4377
|
+
/**
|
|
4378
|
+
* 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.
|
|
4379
|
+
* @returns `undefined` when none of these checks had run before.
|
|
4380
|
+
*/
|
|
4381
|
+
async function recordResults(directory, at, results) {
|
|
4382
|
+
const stored = await read$1(directory);
|
|
4383
|
+
const before = Object.keys(results).flatMap((id) => stored.checks[id]?.findings ?? []);
|
|
4384
|
+
const known = Object.keys(results).some((id) => stored.checks[id] !== void 0);
|
|
4385
|
+
const now = Object.values(results).flat();
|
|
4386
|
+
const was = new Set(before.map(findingKey));
|
|
4387
|
+
const is = new Set(now.map(findingKey));
|
|
4388
|
+
for (const [id, findings] of Object.entries(results)) stored.checks[id] = {
|
|
4389
|
+
at,
|
|
4390
|
+
findings
|
|
4391
|
+
};
|
|
4392
|
+
await atomicWrite(join(directory, CHECKS_FOLDER, FILE$1), JSON.stringify(stored, null, 2), { createDirectory: true });
|
|
4393
|
+
return known ? {
|
|
4394
|
+
added: now.filter((finding) => !was.has(findingKey(finding))).length,
|
|
4395
|
+
resolved: before.filter((finding) => !is.has(findingKey(finding)))
|
|
4396
|
+
} : void 0;
|
|
4397
|
+
}
|
|
4398
|
+
// src/host/checks/similar.ts
|
|
4399
|
+
const FILE = "vectors.json";
|
|
4400
|
+
/** How many texts one request to the embedding provider carries. */
|
|
4401
|
+
const BATCH = 32;
|
|
4402
|
+
const hashOf = (text) => createHash("sha256").update(text).digest("hex").slice(0, 32);
|
|
4403
|
+
const pack = (vector) => Buffer.from(new Float32Array(vector).buffer).toString("base64");
|
|
4404
|
+
const unpack = (text) => {
|
|
4405
|
+
const bytes = Buffer.from(text, "base64");
|
|
4406
|
+
return [...new Float32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4)];
|
|
4407
|
+
};
|
|
4408
|
+
async function read(directory) {
|
|
4409
|
+
try {
|
|
4410
|
+
const stored = JSON.parse(await readFile(join(directory, CHECKS_FOLDER, FILE), "utf8"));
|
|
4411
|
+
return stored.version === 1 && typeof stored.vectors === "object" && stored.vectors !== null ? stored : void 0;
|
|
4412
|
+
} catch (error) {
|
|
4413
|
+
if (error.code === "ENOENT" || error instanceof SyntaxError) return void 0;
|
|
4414
|
+
throw error;
|
|
4415
|
+
}
|
|
4416
|
+
}
|
|
4417
|
+
function cosine(a, b) {
|
|
4418
|
+
let dot = 0;
|
|
4419
|
+
let left = 0;
|
|
4420
|
+
let right = 0;
|
|
4421
|
+
for (let at = 0; at < a.length; at++) {
|
|
4422
|
+
dot += a[at] * b[at];
|
|
4423
|
+
left += a[at] * a[at];
|
|
4424
|
+
right += b[at] * b[at];
|
|
4425
|
+
}
|
|
4426
|
+
return left === 0 || right === 0 ? 0 : dot / Math.sqrt(left * right);
|
|
4046
4427
|
}
|
|
4047
|
-
|
|
4428
|
+
/**
|
|
4429
|
+
* 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
|
|
4430
|
+
* another model or size are not used. `undefined` when the run has no embedding provider.
|
|
4431
|
+
*/
|
|
4432
|
+
async function vectorsOf(model, directory, texts) {
|
|
4433
|
+
if (model.embed === void 0) return void 0;
|
|
4434
|
+
const stored = await read(directory);
|
|
4435
|
+
const known = new Map(Object.entries(stored?.vectors ?? {}).map(([hash, packed]) => [hash, unpack(packed)]));
|
|
4436
|
+
const hashes = texts.map(hashOf);
|
|
4437
|
+
const missing = [...new Set(hashes.filter((hash) => !known.has(hash)))];
|
|
4438
|
+
if (missing.length === 0) return hashes.map((hash) => known.get(hash));
|
|
4048
4439
|
try {
|
|
4049
|
-
return
|
|
4440
|
+
return await embedded(model, directory, texts, hashes, missing, known, stored?.model ?? "");
|
|
4050
4441
|
} catch (error) {
|
|
4051
|
-
if (error
|
|
4442
|
+
if (error instanceof NoEmbeddingService) return void 0;
|
|
4052
4443
|
throw error;
|
|
4053
4444
|
}
|
|
4054
4445
|
}
|
|
4055
|
-
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
const
|
|
4059
|
-
const
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
};
|
|
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
|
-
}
|
|
4084
|
-
}
|
|
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}`);
|
|
4446
|
+
async function embedded(model, directory, texts, hashes, missing, known, stored) {
|
|
4447
|
+
const byHash = new Map(texts.map((text, at) => [hashes[at], text]));
|
|
4448
|
+
let name = stored;
|
|
4449
|
+
const made = /* @__PURE__ */ new Map();
|
|
4450
|
+
const embed = async (wanted) => {
|
|
4451
|
+
for (let at = 0; at < wanted.length; at += BATCH) {
|
|
4452
|
+
const batch = wanted.slice(at, at + BATCH);
|
|
4453
|
+
const { vectors, model: used } = await model.embed(batch.map((hash) => byHash.get(hash)));
|
|
4454
|
+
if (vectors.length !== batch.length) throw new Error(`the embedding provider returned ${vectors.length} vectors for ${batch.length} texts`);
|
|
4455
|
+
if (name !== "" && name !== used) known.clear();
|
|
4456
|
+
name = used;
|
|
4457
|
+
batch.forEach((hash, index) => made.set(hash, vectors[index]));
|
|
4092
4458
|
}
|
|
4093
|
-
}
|
|
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
|
|
4101
4459
|
};
|
|
4460
|
+
await embed(missing);
|
|
4461
|
+
const size = [...made.values()][0].length;
|
|
4462
|
+
for (const [hash, vector] of known) if (vector.length !== size) known.delete(hash);
|
|
4463
|
+
await embed([...new Set(hashes.filter((hash) => !known.has(hash) && !made.has(hash)))]);
|
|
4464
|
+
const all = new Map([...known, ...made]);
|
|
4465
|
+
const result = hashes.map((hash) => all.get(hash));
|
|
4466
|
+
const kept = [...all].slice(-4e3);
|
|
4467
|
+
await atomicWrite(join(directory, CHECKS_FOLDER, FILE), JSON.stringify({
|
|
4468
|
+
version: 1,
|
|
4469
|
+
model: name,
|
|
4470
|
+
dimensions: size,
|
|
4471
|
+
vectors: Object.fromEntries(kept.map(([hash, vector]) => [hash, pack(vector)]))
|
|
4472
|
+
}), { createDirectory: true });
|
|
4473
|
+
return result;
|
|
4474
|
+
}
|
|
4475
|
+
/** How many of its nearest neighbours set how crowded an entry's neighbourhood is. */
|
|
4476
|
+
const CROWD = 5;
|
|
4477
|
+
/** The most pairs asked however large the world, and the share of its entries a world may ask about. */
|
|
4478
|
+
const MOST_ASKED = 30;
|
|
4479
|
+
const ASKED_PER_ENTRY = 1 / 3;
|
|
4480
|
+
/**
|
|
4481
|
+
* The pairs of vectors that reach `threshold` and that `allowed` accepts, at most `limit`, best first. In a world of one theme everything is close to everything, so a double is
|
|
4482
|
+
* not among the closest by cosine; a pair is ranked by how much closer it is than each of the two is to its own neighbours (cosine minus the mean of each side's nearest, doubled
|
|
4483
|
+
* for the pair itself), which puts a copy of a crowded entry ahead of two crowded entries that merely share the theme.
|
|
4484
|
+
*/
|
|
4485
|
+
function nearPairs(vectors, allowed, threshold, limit) {
|
|
4486
|
+
const scored = [];
|
|
4487
|
+
const around = vectors.map(() => []);
|
|
4488
|
+
for (let a = 0; a < vectors.length; a++) for (let b = a + 1; b < vectors.length; b++) {
|
|
4489
|
+
if (!allowed(a, b)) continue;
|
|
4490
|
+
const score = cosine(vectors[a], vectors[b]);
|
|
4491
|
+
around[a].push(score);
|
|
4492
|
+
around[b].push(score);
|
|
4493
|
+
if (score >= threshold) scored.push({
|
|
4494
|
+
a,
|
|
4495
|
+
b,
|
|
4496
|
+
score
|
|
4497
|
+
});
|
|
4498
|
+
}
|
|
4499
|
+
const crowd = around.map((scores) => {
|
|
4500
|
+
const nearest = scores.sort((x, y) => y - x).slice(0, CROWD);
|
|
4501
|
+
return nearest.length === 0 ? 0 : nearest.reduce((sum, score) => sum + score, 0) / nearest.length;
|
|
4502
|
+
});
|
|
4503
|
+
const rank = (pair) => 2 * pair.score - crowd[pair.a] - crowd[pair.b];
|
|
4504
|
+
return scored.sort((x, y) => rank(y) - rank(x)).slice(0, limit);
|
|
4505
|
+
}
|
|
4506
|
+
/** What a world of `entries` asks about: the base number, more for a large world. */
|
|
4507
|
+
const askedFor = (entries, base) => Math.min(MOST_ASKED, Math.max(base, Math.ceil(entries * ASKED_PER_ENTRY)));
|
|
4508
|
+
/**
|
|
4509
|
+
* 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
|
|
4510
|
+
* 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.
|
|
4511
|
+
*/
|
|
4512
|
+
async function candidates(model, directory, texts, allowed, threshold, limit, withoutProvider = "none") {
|
|
4513
|
+
const vectors = await vectorsOf(model, directory, texts);
|
|
4514
|
+
if (vectors !== void 0) return nearPairs(vectors, allowed, threshold, askedFor(texts.length, limit));
|
|
4515
|
+
if (withoutProvider === "none") return void 0;
|
|
4516
|
+
const pairs = [];
|
|
4517
|
+
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({
|
|
4518
|
+
a,
|
|
4519
|
+
b,
|
|
4520
|
+
score: 0
|
|
4521
|
+
});
|
|
4522
|
+
return pairs;
|
|
4102
4523
|
}
|
|
4103
4524
|
// src/host/checks/fidelity.ts
|
|
4525
|
+
/** The most uncovered pieces listed one by one: past that the import itself is the problem. */
|
|
4526
|
+
const PIECES_LISTED = 12;
|
|
4104
4527
|
/** The most numbers one finding lists. */
|
|
4105
4528
|
const NUMBERS_LISTED = 8;
|
|
4529
|
+
/** The most parts of the world a finding names as made from a piece. */
|
|
4530
|
+
const PARTS_LISTED = 3;
|
|
4106
4531
|
/** 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
4532
|
const REFLECTED = .5;
|
|
4108
4533
|
const MIN_PASSAGE_TERMS = 3;
|
|
@@ -4123,25 +4548,129 @@ const recordsNeeded = (evidence) => {
|
|
|
4123
4548
|
const snippet = (text) => `${text.replace(/\s+/gu, " ").trim().slice(0, SNIPPET)}${text.trim().length > SNIPPET ? "…" : ""}`;
|
|
4124
4549
|
/** The pieces the import read for the world and the card the records are about. */
|
|
4125
4550
|
const basis = (evidence) => evidence.previous ?? evidence.source;
|
|
4551
|
+
/** How many of the uncovered pieces are asked about by meaning, largest first. */
|
|
4552
|
+
const RESCUE_PIECES = 24;
|
|
4553
|
+
/** How many documents of the world are asked about for one piece: the closest in meaning. */
|
|
4554
|
+
const RESCUE_DOCUMENTS = 3;
|
|
4555
|
+
/** How close in meaning a document is to a piece before it is asked about; low on purpose, the question settles it. */
|
|
4556
|
+
const RESCUE_CLOSE = .5;
|
|
4557
|
+
const RESCUE_CHARS = 1800;
|
|
4126
4558
|
const coverage = {
|
|
4127
4559
|
id: "card-coverage",
|
|
4128
4560
|
kit: "fidelity",
|
|
4129
4561
|
mode: "code",
|
|
4130
4562
|
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.",
|
|
4563
|
+
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
4564
|
async run(input) {
|
|
4133
4565
|
const evidence = need$1(input);
|
|
4134
4566
|
recordsNeeded(evidence);
|
|
4135
4567
|
const source = basis(evidence);
|
|
4136
|
-
const made =
|
|
4137
|
-
|
|
4568
|
+
const made = await loaded.made(input, evidence);
|
|
4569
|
+
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);
|
|
4570
|
+
const { saved, looked, problem } = await sayingByMeaning(input, missing);
|
|
4571
|
+
const lost = missing.filter((piece) => !saved.has(piece.id));
|
|
4572
|
+
const found = lost.slice(0, PIECES_LISTED).map((piece) => ({
|
|
4138
4573
|
severity: "warning",
|
|
4139
4574
|
code: "piece-uncovered",
|
|
4140
4575
|
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`,
|
|
4576
|
+
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
4577
|
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
|
-
}))
|
|
4578
|
+
}));
|
|
4579
|
+
if (lost.length > PIECES_LISTED) found.push({
|
|
4580
|
+
severity: "warning",
|
|
4581
|
+
code: "pieces-uncovered-more",
|
|
4582
|
+
where: "card",
|
|
4583
|
+
message: `${lost.length - PIECES_LISTED} more pieces of the card, smaller than these, have nothing in the world made from them`,
|
|
4584
|
+
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."
|
|
4585
|
+
});
|
|
4586
|
+
if (problem !== void 0) found.push({
|
|
4587
|
+
severity: "info",
|
|
4588
|
+
code: "meaning-unavailable",
|
|
4589
|
+
where: "card",
|
|
4590
|
+
message: `the pieces were not looked for by meaning: ${problem}`
|
|
4591
|
+
});
|
|
4592
|
+
return foundBy("card-coverage", found);
|
|
4593
|
+
}
|
|
4594
|
+
};
|
|
4595
|
+
/** Which documents of the world a model found to say a piece in other words, by the piece's id: they were made from it, though their words no longer show it. */
|
|
4596
|
+
const rescuedBy = /* @__PURE__ */ new WeakMap();
|
|
4597
|
+
function rescued(input, piece, where) {
|
|
4598
|
+
if (input.memo === void 0) return;
|
|
4599
|
+
const all = rescuedBy.get(input.memo) ?? /* @__PURE__ */ new Map();
|
|
4600
|
+
all.set(piece, [.../* @__PURE__ */ new Set([...all.get(piece) ?? [], where])]);
|
|
4601
|
+
rescuedBy.set(input.memo, all);
|
|
4602
|
+
}
|
|
4603
|
+
const rescuedFor = (input, piece) => input.memo === void 0 ? [] : rescuedBy.get(input.memo)?.get(piece) ?? [];
|
|
4604
|
+
/** 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. */
|
|
4605
|
+
async function sayingByMeaning(input, pieces) {
|
|
4606
|
+
const model = input.model;
|
|
4607
|
+
const saved = /* @__PURE__ */ new Set();
|
|
4608
|
+
if (model?.embed === void 0 || pieces.length === 0) return {
|
|
4609
|
+
saved,
|
|
4610
|
+
looked: false
|
|
4611
|
+
};
|
|
4612
|
+
try {
|
|
4613
|
+
return await lookedByMeaning(input, model, pieces, saved);
|
|
4614
|
+
} catch (error) {
|
|
4615
|
+
if (model.cancelled?.() === true) throw error;
|
|
4616
|
+
return {
|
|
4617
|
+
saved,
|
|
4618
|
+
looked: false,
|
|
4619
|
+
problem: error instanceof Error ? error.message : String(error)
|
|
4620
|
+
};
|
|
4144
4621
|
}
|
|
4622
|
+
}
|
|
4623
|
+
async function lookedByMeaning(input, model, pieces, saved) {
|
|
4624
|
+
const documents = [...(await loaded.text(input)).prose].filter(([, prose]) => prose.trim() !== "");
|
|
4625
|
+
if (documents.length === 0) return {
|
|
4626
|
+
saved,
|
|
4627
|
+
looked: false
|
|
4628
|
+
};
|
|
4629
|
+
const asked = pieces.slice(0, RESCUE_PIECES);
|
|
4630
|
+
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))]);
|
|
4631
|
+
if (vectors === void 0) return {
|
|
4632
|
+
saved,
|
|
4633
|
+
looked: false
|
|
4634
|
+
};
|
|
4635
|
+
const instructions = model.text("checks/decide-covered");
|
|
4636
|
+
await mapLimit(asked, model.concurrency, async (piece, at) => {
|
|
4637
|
+
const near = documents.map(([where, prose], index) => ({
|
|
4638
|
+
where,
|
|
4639
|
+
prose,
|
|
4640
|
+
score: cosine(vectors[at], vectors[asked.length + index])
|
|
4641
|
+
})).filter((entry) => entry.score >= RESCUE_CLOSE).sort((a, b) => b.score - a.score).slice(0, RESCUE_DOCUMENTS);
|
|
4642
|
+
for (const document of near) {
|
|
4643
|
+
if (model.cancelled?.() === true) return;
|
|
4644
|
+
if ((await verdictOf(model, {
|
|
4645
|
+
passage: piece.text.slice(0, RESCUE_CHARS),
|
|
4646
|
+
document: document.prose.slice(0, RESCUE_CHARS)
|
|
4647
|
+
}, "covered", instructions, async () => {
|
|
4648
|
+
return (await model.run({
|
|
4649
|
+
persona: instructions,
|
|
4650
|
+
prompt: `${piece.text.slice(0, RESCUE_CHARS)}\n\n${document.prose.slice(0, RESCUE_CHARS)}`,
|
|
4651
|
+
result: COVERED_SCHEMA,
|
|
4652
|
+
maxTokens: 500
|
|
4653
|
+
})).covered;
|
|
4654
|
+
})).yes) {
|
|
4655
|
+
saved.add(piece.id);
|
|
4656
|
+
rescued(input, piece.id, document.where);
|
|
4657
|
+
return;
|
|
4658
|
+
}
|
|
4659
|
+
}
|
|
4660
|
+
});
|
|
4661
|
+
return {
|
|
4662
|
+
saved,
|
|
4663
|
+
looked: true
|
|
4664
|
+
};
|
|
4665
|
+
}
|
|
4666
|
+
const COVERED_SCHEMA = {
|
|
4667
|
+
type: "object",
|
|
4668
|
+
properties: {
|
|
4669
|
+
reason: { type: "string" },
|
|
4670
|
+
covered: { type: "boolean" }
|
|
4671
|
+
},
|
|
4672
|
+
required: ["reason", "covered"],
|
|
4673
|
+
additionalProperties: false
|
|
4145
4674
|
};
|
|
4146
4675
|
const names = {
|
|
4147
4676
|
id: "card-names",
|
|
@@ -4152,12 +4681,17 @@ const names = {
|
|
|
4152
4681
|
async run(input) {
|
|
4153
4682
|
const evidence = need$1(input);
|
|
4154
4683
|
const source = basis(evidence);
|
|
4155
|
-
const world = await
|
|
4684
|
+
const world = await loaded.text(input);
|
|
4156
4685
|
const text = folded([source.card.name, ...source.pieces.map((piece) => piece.text)].join("\n"));
|
|
4686
|
+
const wide = /[\u3000-\u9fff\uac00-\ud7af]/u;
|
|
4687
|
+
const cardHasWide = wide.test(text);
|
|
4688
|
+
const cardHasLatin = /[a-z]/u.test(text);
|
|
4689
|
+
const comparable = (names) => names.every((name) => wide.test(name) ? cardHasWide : !/[a-z]/iu.test(name) || cardHasLatin);
|
|
4157
4690
|
const named = (candidate) => {
|
|
4158
4691
|
return [candidate, candidate.replace(/\s*[((][^))]*[))]/gu, "").trim()].some((name) => folded(name).trim() !== "" && text.includes(folded(name).trim()));
|
|
4159
4692
|
};
|
|
4160
|
-
const checked = world.names.filter((thing) => !thing.playable && thing.names.length > 0);
|
|
4693
|
+
const checked = world.names.filter((thing) => !thing.playable && thing.names.length > 0 && comparable(thing.names));
|
|
4694
|
+
const elsewhere = world.names.filter((thing) => !thing.playable && thing.names.length > 0 && !comparable(thing.names)).length;
|
|
4161
4695
|
const missing = checked.filter((thing) => !thing.names.some(named));
|
|
4162
4696
|
if (checked.length >= MIN_NAMES_FOR_PATTERN && missing.length / checked.length > MOSTLY_MISSING) return foundBy("card-names", [{
|
|
4163
4697
|
severity: "info",
|
|
@@ -4166,6 +4700,13 @@ const names = {
|
|
|
4166
4700
|
message: `${missing.length} of ${checked.length} names do not occur in the card: the world is probably in another language than the card, or was made from another card`,
|
|
4167
4701
|
hint: "Compare by meaning instead; the judged checks do."
|
|
4168
4702
|
}]);
|
|
4703
|
+
if (elsewhere > 0 && missing.length === 0) return foundBy("card-names", [{
|
|
4704
|
+
severity: "info",
|
|
4705
|
+
code: "names-elsewhere",
|
|
4706
|
+
where: "(the world)",
|
|
4707
|
+
message: `${elsewhere} names are in another script than the card, so they cannot be looked for in it: the world is in another language than the card`,
|
|
4708
|
+
hint: "Compare by meaning instead; the judged checks do."
|
|
4709
|
+
}]);
|
|
4169
4710
|
return foundBy("card-names", missing.map((thing) => ({
|
|
4170
4711
|
severity: thing.kind === "items" ? "info" : "warning",
|
|
4171
4712
|
code: "name-not-in-card",
|
|
@@ -4191,8 +4732,8 @@ const facts = {
|
|
|
4191
4732
|
const evidence = need$1(input);
|
|
4192
4733
|
recordsNeeded(evidence);
|
|
4193
4734
|
const source = basis(evidence);
|
|
4194
|
-
const world = await
|
|
4195
|
-
const made =
|
|
4735
|
+
const world = await loaded.text(input);
|
|
4736
|
+
const made = await loaded.made(input, evidence);
|
|
4196
4737
|
const found = [];
|
|
4197
4738
|
for (const piece of usedPieces(source)) {
|
|
4198
4739
|
const into = made.get(baseId(piece.id));
|
|
@@ -4203,7 +4744,7 @@ const facts = {
|
|
|
4203
4744
|
severity: "info",
|
|
4204
4745
|
code: "number-lost",
|
|
4205
4746
|
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`,
|
|
4747
|
+
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
4748
|
hint: "Read the piece with card_read: a number that matters to the story belongs in the world as the card has it."
|
|
4208
4749
|
});
|
|
4209
4750
|
}
|
|
@@ -4226,8 +4767,8 @@ const drift = {
|
|
|
4226
4767
|
const evidence = need$1(input);
|
|
4227
4768
|
if (evidence.previous === void 0) throw new CheckSkipped("no newer card is waiting; ask for the card that is waiting with version new");
|
|
4228
4769
|
recordsNeeded(evidence);
|
|
4229
|
-
const world = await
|
|
4230
|
-
const made =
|
|
4770
|
+
const world = await loaded.text(input);
|
|
4771
|
+
const made = await loaded.made(input, evidence);
|
|
4231
4772
|
const match = matchPieces(evidence.previous.pieces, evidence.source.pieces);
|
|
4232
4773
|
const found = [];
|
|
4233
4774
|
for (const { before, after } of match.pairs) {
|
|
@@ -4308,7 +4849,6 @@ const FIDELITY_SCHEMA = {
|
|
|
4308
4849
|
required: ["dropped", "invented"],
|
|
4309
4850
|
additionalProperties: false
|
|
4310
4851
|
};
|
|
4311
|
-
const squashed = (text) => folded(text).replace(/\s+/gu, " ").trim();
|
|
4312
4852
|
/** 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
4853
|
function comparisonProblem(value, source, document) {
|
|
4314
4854
|
if (value.dropped.length > MAX_REPORTED || value.invented.length > MAX_REPORTED) return `report at most ${MAX_REPORTED} of each`;
|
|
@@ -4328,220 +4868,422 @@ const fidelityChecks = [
|
|
|
4328
4868
|
needs: ["card", "model"],
|
|
4329
4869
|
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
4870
|
async run(input) {
|
|
4331
|
-
const evidence = need$1(input);
|
|
4332
|
-
recordsNeeded(evidence);
|
|
4871
|
+
const evidence = need$1(input);
|
|
4872
|
+
recordsNeeded(evidence);
|
|
4873
|
+
const model = input.model;
|
|
4874
|
+
const tr = trOf(model);
|
|
4875
|
+
const source = basis(evidence);
|
|
4876
|
+
const world = await loaded.text(input);
|
|
4877
|
+
const made = await loaded.made(input, evidence, "about");
|
|
4878
|
+
const from = /* @__PURE__ */ new Map();
|
|
4879
|
+
for (const piece of usedPieces(source)) for (const where of /* @__PURE__ */ new Set([...made.get(baseId(piece.id)) ?? [], ...rescuedFor(input, piece.id)])) from.set(where, [...from.get(where) ?? [], piece]);
|
|
4880
|
+
const ranked = [...from].map(([where, pieces]) => ({
|
|
4881
|
+
where,
|
|
4882
|
+
pieces,
|
|
4883
|
+
size: pieces.reduce((sum, piece) => sum + piece.text.length, 0)
|
|
4884
|
+
})).sort((a, b) => b.size - a.size);
|
|
4885
|
+
const chosen = ranked.slice(0, MAX_COMPARED);
|
|
4886
|
+
const found = [];
|
|
4887
|
+
if (ranked.length > chosen.length) found.push({
|
|
4888
|
+
severity: "info",
|
|
4889
|
+
code: "not-compared",
|
|
4890
|
+
where: "(the world)",
|
|
4891
|
+
message: `${ranked.length - chosen.length} more documents were not compared: a run compares at most ${MAX_COMPARED}, the ones with the most source first`,
|
|
4892
|
+
hint: "Compare the rest with check_card again after fixing these, or read them against the card yourself."
|
|
4893
|
+
});
|
|
4894
|
+
await mapLimit(chosen, model.concurrency, async ({ where, pieces }) => {
|
|
4895
|
+
if (model.cancelled?.() === true) return;
|
|
4896
|
+
const document = world.prose.get(where) ?? "";
|
|
4897
|
+
const subject = world.subjects.get(where) ?? where;
|
|
4898
|
+
const text = pieces.map((piece) => `[${piece.label}]\n${piece.text.slice(0, SOURCE_CHARS)}`).join("\n\n").slice(0, SOURCE_CHARS * 2);
|
|
4899
|
+
try {
|
|
4900
|
+
const answer = await model.run({
|
|
4901
|
+
persona: model.text("checks/fidelity"),
|
|
4902
|
+
prompt: `${tr("Source pieces:")}\n${text}\n\n${tr("Document ({where}), about {subject}:", {
|
|
4903
|
+
where,
|
|
4904
|
+
subject
|
|
4905
|
+
})}\n${document}`,
|
|
4906
|
+
result: FIDELITY_SCHEMA,
|
|
4907
|
+
maxTokens: 1500,
|
|
4908
|
+
check: (value) => comparisonProblem(value, text, document)
|
|
4909
|
+
});
|
|
4910
|
+
const once = (list) => list.filter((entry, at) => list.findIndex((other) => squashed(other.quote) === squashed(entry.quote)) === at);
|
|
4911
|
+
for (const entry of once(answer.dropped)) found.push({
|
|
4912
|
+
severity: "warning",
|
|
4913
|
+
code: "fact-dropped",
|
|
4914
|
+
where,
|
|
4915
|
+
message: `the card says “${snippet(entry.quote)}” (${entry.fact}), which ${where} does not say`,
|
|
4916
|
+
hint: "Read the piece with card_read; if the fact matters to the story, add it to the document as the card has it."
|
|
4917
|
+
});
|
|
4918
|
+
for (const entry of once(answer.invented)) found.push({
|
|
4919
|
+
severity: "warning",
|
|
4920
|
+
code: "fact-invented",
|
|
4921
|
+
where,
|
|
4922
|
+
message: `${where} says “${snippet(entry.quote)}” (${entry.fact}), which the card does not give`,
|
|
4923
|
+
hint: "Remove it unless the person wants it; the world should not state what its source does not."
|
|
4924
|
+
});
|
|
4925
|
+
} catch (error) {
|
|
4926
|
+
if (model.cancelled?.() === true) throw error;
|
|
4927
|
+
found.push({
|
|
4928
|
+
severity: "info",
|
|
4929
|
+
code: "not-compared",
|
|
4930
|
+
where,
|
|
4931
|
+
message: `${where} could not be compared: ${error instanceof Error ? error.message : String(error)}`
|
|
4932
|
+
});
|
|
4933
|
+
}
|
|
4934
|
+
});
|
|
4935
|
+
return foundBy("card-fidelity", found);
|
|
4936
|
+
}
|
|
4937
|
+
}
|
|
4938
|
+
];
|
|
4939
|
+
// src/host/checks/playability.ts
|
|
4940
|
+
/** The most players' first scenes a model reads: a world with many playable characters is judged by the first few. */
|
|
4941
|
+
const SCENES_JUDGED = 3;
|
|
4942
|
+
const OPENING_CHARS = 2500;
|
|
4943
|
+
const SCENE_CHARS = 3500;
|
|
4944
|
+
/** Where each character the player may play starts and what is around them, as the files say it. */
|
|
4945
|
+
async function scenesOf(input) {
|
|
4946
|
+
const [{ characters }, { locations }, { items }] = await Promise.all([
|
|
4947
|
+
loaded.characters(input),
|
|
4948
|
+
loaded.locations(input),
|
|
4949
|
+
loaded.items(input)
|
|
4950
|
+
]);
|
|
4951
|
+
const players = characters.filter((character) => character.role === "playable");
|
|
4952
|
+
if (players.length === 0) throw new CheckSkipped("no character can be played (see quality-cast)");
|
|
4953
|
+
return players.map((player) => {
|
|
4954
|
+
const at = player.start.place?.location;
|
|
4955
|
+
const place = locations.find((location) => location.id === at);
|
|
4956
|
+
return {
|
|
4957
|
+
player,
|
|
4958
|
+
place,
|
|
4959
|
+
others: place === void 0 ? [] : characters.filter((other) => other.id !== player.id && other.kind === "person" && other.start.place?.location === place.id),
|
|
4960
|
+
things: place === void 0 ? [] : items.filter((item) => item.start.kind === "location" && item.start.location === place.id && !item.hidden).map((item) => item.name),
|
|
4961
|
+
ways: place === void 0 ? 0 : place.exits.length + locations.filter((other) => other.id !== place.id && other.exits.some((exit) => exit.to === place.id)).length
|
|
4962
|
+
};
|
|
4963
|
+
});
|
|
4964
|
+
}
|
|
4965
|
+
const firstScene = {
|
|
4966
|
+
id: "first-scene",
|
|
4967
|
+
kit: "playability",
|
|
4968
|
+
mode: "code",
|
|
4969
|
+
needs: [],
|
|
4970
|
+
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.)",
|
|
4971
|
+
async run(input) {
|
|
4972
|
+
const { locations } = await loaded.locations(input);
|
|
4973
|
+
const found = [];
|
|
4974
|
+
for (const scene of await scenesOf(input)) {
|
|
4975
|
+
const where = `characters/${scene.player.id}`;
|
|
4976
|
+
if (scene.place === void 0) {
|
|
4977
|
+
if (locations.length > 0) found.push({
|
|
4978
|
+
severity: "warning",
|
|
4979
|
+
code: "start-nowhere",
|
|
4980
|
+
where,
|
|
4981
|
+
message: `${scene.player.name} starts in no place of the world, though the world has places`,
|
|
4982
|
+
hint: "Give the character a start place."
|
|
4983
|
+
});
|
|
4984
|
+
continue;
|
|
4985
|
+
}
|
|
4986
|
+
if (scene.others.length === 0 && scene.things.length === 0 && scene.place.features.length === 0) found.push({
|
|
4987
|
+
severity: "info",
|
|
4988
|
+
code: "start-empty",
|
|
4989
|
+
where,
|
|
4990
|
+
message: `${scene.player.name} starts alone in ${scene.place.name}, with no people, things or features there to act on`
|
|
4991
|
+
});
|
|
4992
|
+
else if (scene.others.length === 0) found.push({
|
|
4993
|
+
severity: "info",
|
|
4994
|
+
code: "start-alone",
|
|
4995
|
+
where,
|
|
4996
|
+
message: `${scene.player.name} starts alone in ${scene.place.name}: nothing answers until they move or something happens`
|
|
4997
|
+
});
|
|
4998
|
+
}
|
|
4999
|
+
return foundBy("first-scene", found);
|
|
5000
|
+
}
|
|
5001
|
+
};
|
|
5002
|
+
const HOOK_SCHEMA = {
|
|
5003
|
+
type: "object",
|
|
5004
|
+
properties: {
|
|
5005
|
+
reason: { type: "string" },
|
|
5006
|
+
playable: { type: "boolean" }
|
|
5007
|
+
},
|
|
5008
|
+
required: ["reason", "playable"],
|
|
5009
|
+
additionalProperties: false
|
|
5010
|
+
};
|
|
5011
|
+
const playabilityChecks = [firstScene, {
|
|
5012
|
+
id: "opening-playable",
|
|
5013
|
+
kit: "playability",
|
|
5014
|
+
mode: "decision",
|
|
5015
|
+
needs: ["model"],
|
|
5016
|
+
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.",
|
|
5017
|
+
async run(input) {
|
|
5018
|
+
const model = input.model;
|
|
5019
|
+
const scenes = (await scenesOf(input)).slice(0, SCENES_JUDGED);
|
|
5020
|
+
const info = await loaded.info(input);
|
|
5021
|
+
const found = [];
|
|
5022
|
+
await mapLimit(scenes, model.concurrency, async (scene) => {
|
|
5023
|
+
const text = [
|
|
5024
|
+
`opening: ${info.opening.trim() === "" ? "(none: the story starts with \"The story begins.\")" : info.opening.slice(0, OPENING_CHARS)}`,
|
|
5025
|
+
`the player's character: ${scene.player.name}: ${scene.player.summary}`,
|
|
5026
|
+
...scene.player.start.activity === "" ? [] : [`doing: ${scene.player.start.activity}`],
|
|
5027
|
+
...scene.player.start.intents.slice(0, 3).map((intent) => `wants: ${intent.name}: ${intent.description}`),
|
|
5028
|
+
scene.place === void 0 ? "place: (none)" : `place: ${scene.place.name}: ${scene.place.description}`,
|
|
5029
|
+
...(scene.place?.features ?? []).slice(0, 8).map((feature) => `feature: ${feature.name}`),
|
|
5030
|
+
...scene.others.slice(0, 8).map((other) => `here: ${other.name}: ${other.summary}`),
|
|
5031
|
+
...scene.things.slice(0, 8).map((thing) => `thing here: ${thing}`),
|
|
5032
|
+
`ways out: ${scene.ways}`
|
|
5033
|
+
].join("\n").slice(0, SCENE_CHARS);
|
|
5034
|
+
let reason = "";
|
|
5035
|
+
const instructions = model.text("checks/decide-opening");
|
|
5036
|
+
if (!(await verdictOf(model, { scene: text }, "playable", instructions, async () => {
|
|
5037
|
+
const answer = await model.run({
|
|
5038
|
+
persona: instructions,
|
|
5039
|
+
prompt: text,
|
|
5040
|
+
result: HOOK_SCHEMA,
|
|
5041
|
+
maxTokens: 500
|
|
5042
|
+
});
|
|
5043
|
+
reason = answer.reason;
|
|
5044
|
+
return answer.playable;
|
|
5045
|
+
})).yes) found.push({
|
|
5046
|
+
severity: "warning",
|
|
5047
|
+
code: "opening-flat",
|
|
5048
|
+
where: `characters/${scene.player.id}`,
|
|
5049
|
+
message: `a player of ${scene.player.name} may not know what to do at the start${reason === "" ? "" : `: ${reason}`}`,
|
|
5050
|
+
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."
|
|
5051
|
+
});
|
|
5052
|
+
});
|
|
5053
|
+
return foundBy("opening-playable", found);
|
|
5054
|
+
}
|
|
5055
|
+
}];
|
|
5056
|
+
// src/host/checks/semantic.ts
|
|
5057
|
+
/** The most pairs one check asks a model about: past that the world has a different problem than a few doubles. */
|
|
5058
|
+
const PAIRS_ASKED = 12;
|
|
5059
|
+
/** 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). */
|
|
5060
|
+
const VALUES_CLOSE = .55;
|
|
5061
|
+
const THINGS_CLOSE = .6;
|
|
5062
|
+
const LORE_CLOSE = .6;
|
|
5063
|
+
const TEXT_CHARS = 1200;
|
|
5064
|
+
const yesNo = (key, meaning) => ({
|
|
5065
|
+
type: "object",
|
|
5066
|
+
properties: {
|
|
5067
|
+
reason: { type: "string" },
|
|
5068
|
+
[key]: {
|
|
5069
|
+
type: "boolean",
|
|
5070
|
+
description: meaning
|
|
5071
|
+
}
|
|
5072
|
+
},
|
|
5073
|
+
required: ["reason", key],
|
|
5074
|
+
additionalProperties: false
|
|
5075
|
+
});
|
|
5076
|
+
/** The pairs a model says yes to, each with the reason the language model gave (the decision model gives none). */
|
|
5077
|
+
async function asked(model, pairs, prompt, key, meaning, extra = {}) {
|
|
5078
|
+
const instructions = model.text(prompt);
|
|
5079
|
+
return (await mapLimit(pairs, model.concurrency, async (pair) => {
|
|
5080
|
+
if (model.cancelled?.() === true) return void 0;
|
|
5081
|
+
let reason = "";
|
|
5082
|
+
return (await verdictOf(model, {
|
|
5083
|
+
...extra,
|
|
5084
|
+
first: pair.first,
|
|
5085
|
+
second: pair.second
|
|
5086
|
+
}, key, instructions, async () => {
|
|
5087
|
+
const answer = await model.run({
|
|
5088
|
+
persona: instructions,
|
|
5089
|
+
prompt: `A:\n${pair.first}\n\nB:\n${pair.second}`,
|
|
5090
|
+
result: yesNo(key, meaning),
|
|
5091
|
+
maxTokens: 500
|
|
5092
|
+
});
|
|
5093
|
+
reason = answer.reason;
|
|
5094
|
+
return answer[key] === true;
|
|
5095
|
+
})).yes ? {
|
|
5096
|
+
pair,
|
|
5097
|
+
reason
|
|
5098
|
+
} : void 0;
|
|
5099
|
+
})).filter((entry) => entry !== void 0);
|
|
5100
|
+
}
|
|
5101
|
+
const because = (reason) => reason.trim() === "" ? "" : `: ${reason.trim()}`;
|
|
5102
|
+
const valueText = (def) => `${def.name} (${def.key}): ${def.description ?? ""}`.slice(0, TEXT_CHARS);
|
|
5103
|
+
const sameValue = {
|
|
5104
|
+
id: "duplicate-values-meaning",
|
|
5105
|
+
kit: "consistency",
|
|
5106
|
+
mode: "decision",
|
|
5107
|
+
needs: ["model"],
|
|
5108
|
+
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.",
|
|
5109
|
+
async run(input) {
|
|
5110
|
+
const model = input.model;
|
|
5111
|
+
const { defs } = await loaded.variables(input);
|
|
5112
|
+
const known = new Set(duplicateValues(defs).map(([a, b]) => `${a.key}\n${b.key}`));
|
|
5113
|
+
const allowed = (a, b) => overlapping(defs[a], defs[b]) && !known.has(`${defs[a].key}\n${defs[b].key}`);
|
|
5114
|
+
const said = await asked(model, (await candidates(model, input.world.directory, defs.map(valueText), allowed, VALUES_CLOSE, PAIRS_ASKED, "first") ?? []).map((entry) => ({
|
|
5115
|
+
near: entry,
|
|
5116
|
+
first: valueText(defs[entry.a]),
|
|
5117
|
+
second: valueText(defs[entry.b])
|
|
5118
|
+
})), "checks/decide-same-value", "same", "true only when A and B are one and the same value; false when they are different, or one is a kind, cause or part of the other");
|
|
5119
|
+
return foundBy("duplicate-values-meaning", said.map(({ pair, reason }) => {
|
|
5120
|
+
const a = defs[pair.near.a];
|
|
5121
|
+
const b = defs[pair.near.b];
|
|
5122
|
+
return {
|
|
5123
|
+
severity: "warning",
|
|
5124
|
+
code: "duplicate-value",
|
|
5125
|
+
where: "variables.json",
|
|
5126
|
+
message: `${a.key} (${a.name}) and ${b.key} (${b.name}) seem to track the same thing for the same ${a.subject}s${because(reason)}`,
|
|
5127
|
+
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."
|
|
5128
|
+
};
|
|
5129
|
+
}));
|
|
5130
|
+
}
|
|
5131
|
+
};
|
|
5132
|
+
const KIND_NAME = {
|
|
5133
|
+
characters: "character",
|
|
5134
|
+
locations: "place",
|
|
5135
|
+
factions: "organisation",
|
|
5136
|
+
items: "thing"
|
|
5137
|
+
};
|
|
5138
|
+
/** Whether one name is inside the other (an "Old Mill" and a "Mill"): such a pair is asked about whatever the embedding says. */
|
|
5139
|
+
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))));
|
|
5140
|
+
const nearNames = {
|
|
5141
|
+
id: "name-near",
|
|
5142
|
+
kit: "consistency",
|
|
5143
|
+
mode: "decision",
|
|
5144
|
+
needs: ["model"],
|
|
5145
|
+
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.",
|
|
5146
|
+
async run(input) {
|
|
5147
|
+
const model = input.model;
|
|
5148
|
+
const world = await loaded.text(input);
|
|
5149
|
+
const things = world.names.map((thing) => ({
|
|
5150
|
+
where: thing.where,
|
|
5151
|
+
kind: KIND_NAME[thing.kind],
|
|
5152
|
+
names: [...new Set(thing.names.map(norm).filter((name) => name !== ""))],
|
|
5153
|
+
text: `${thing.names[0] ?? thing.where}: ${(world.prose.get(thing.where) ?? "").slice(0, TEXT_CHARS)}`
|
|
5154
|
+
}));
|
|
5155
|
+
const sameKind = (a, b) => things[a].kind === things[b].kind && !things[a].names.some((name) => things[b].names.includes(name));
|
|
5156
|
+
const byName = [];
|
|
5157
|
+
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({
|
|
5158
|
+
a,
|
|
5159
|
+
b,
|
|
5160
|
+
score: 1
|
|
5161
|
+
});
|
|
5162
|
+
const byMeaning = [];
|
|
5163
|
+
for (const kind of new Set(things.map((thing) => thing.kind))) {
|
|
5164
|
+
const members = things.flatMap((thing, at) => thing.kind === kind ? [at] : []);
|
|
5165
|
+
const own = await candidates(model, input.world.directory, members.map((at) => things[at].text), (a, b) => sameKind(members[a], members[b]), THINGS_CLOSE, PAIRS_ASKED);
|
|
5166
|
+
byMeaning.push(...(own ?? []).map((entry) => ({
|
|
5167
|
+
...entry,
|
|
5168
|
+
a: members[entry.a],
|
|
5169
|
+
b: members[entry.b]
|
|
5170
|
+
})));
|
|
5171
|
+
}
|
|
5172
|
+
const seen = new Set(byName.map((entry) => `${entry.a}:${entry.b}`));
|
|
5173
|
+
const said = await asked(model, [...byName, ...byMeaning.filter((entry) => !seen.has(`${entry.a}:${entry.b}`))].map((entry) => ({
|
|
5174
|
+
near: entry,
|
|
5175
|
+
first: `${things[entry.a].kind} ${things[entry.a].text}`,
|
|
5176
|
+
second: `${things[entry.b].kind} ${things[entry.b].text}`
|
|
5177
|
+
})), "checks/decide-same-thing", "same", "true only when A and B are one and the same entry written twice; false when they are different, or one is a part of the other");
|
|
5178
|
+
return foundBy("name-near", said.map(({ pair, reason }) => {
|
|
5179
|
+
const a = things[pair.near.a];
|
|
5180
|
+
const b = things[pair.near.b];
|
|
5181
|
+
return {
|
|
5182
|
+
severity: "warning",
|
|
5183
|
+
code: "possible-duplicate",
|
|
5184
|
+
where: a.where,
|
|
5185
|
+
message: `${a.where} and ${b.where} may be the same ${a.kind}, written twice${because(reason)}`,
|
|
5186
|
+
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."
|
|
5187
|
+
};
|
|
5188
|
+
}));
|
|
5189
|
+
}
|
|
5190
|
+
};
|
|
5191
|
+
const CONFLICT_SCHEMA = {
|
|
5192
|
+
type: "object",
|
|
5193
|
+
properties: {
|
|
5194
|
+
reason: { type: "string" },
|
|
5195
|
+
conflict: { type: "boolean" },
|
|
5196
|
+
first_quote: { type: "string" },
|
|
5197
|
+
second_quote: { type: "string" }
|
|
5198
|
+
},
|
|
5199
|
+
required: [
|
|
5200
|
+
"reason",
|
|
5201
|
+
"conflict",
|
|
5202
|
+
"first_quote",
|
|
5203
|
+
"second_quote"
|
|
5204
|
+
],
|
|
5205
|
+
additionalProperties: false
|
|
5206
|
+
};
|
|
5207
|
+
/** The shortest quote that can state a fact: a word or a title is not one. */
|
|
5208
|
+
const MIN_QUOTE_CHARS = 10;
|
|
5209
|
+
/** 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. */
|
|
5210
|
+
function conflictProblem(value, first, second, titles = ["", ""]) {
|
|
5211
|
+
if (!value.conflict) return void 0;
|
|
5212
|
+
const there = (text, quote) => squashed(quote) !== "" && squashed(text).includes(squashed(quote));
|
|
5213
|
+
const statement = (quote, title) => squashed(quote).replace(/\s/gu, "").length >= MIN_QUOTE_CHARS && !squashed(title).includes(squashed(quote)) && !squashed(quote).includes(squashed(title) || "\0");
|
|
5214
|
+
if (!there(first, value.first_quote)) return "first_quote must be a passage of the first entry, exactly as it is written there";
|
|
5215
|
+
if (!there(second, value.second_quote)) return "second_quote must be a passage of the second entry, exactly as it is written there";
|
|
5216
|
+
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";
|
|
5217
|
+
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";
|
|
5218
|
+
}
|
|
5219
|
+
const loreText = (entry) => `${entry.title}\n${entry.summary}\n${entry.body}`.slice(0, TEXT_CHARS * 2);
|
|
5220
|
+
const semanticChecks = [
|
|
5221
|
+
sameValue,
|
|
5222
|
+
nearNames,
|
|
5223
|
+
{
|
|
5224
|
+
id: "lore-conflict",
|
|
5225
|
+
kit: "consistency",
|
|
5226
|
+
mode: "decision",
|
|
5227
|
+
needs: ["model"],
|
|
5228
|
+
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.",
|
|
5229
|
+
async run(input) {
|
|
4333
5230
|
const model = input.model;
|
|
4334
|
-
const
|
|
4335
|
-
const
|
|
4336
|
-
|
|
4337
|
-
|
|
4338
|
-
|
|
4339
|
-
|
|
4340
|
-
const
|
|
4341
|
-
|
|
4342
|
-
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
5231
|
+
const { lore } = await loaded.lore(input);
|
|
5232
|
+
const apart = (a, b) => {
|
|
5233
|
+
const x = lore[a];
|
|
5234
|
+
const y = lore[b];
|
|
5235
|
+
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;
|
|
5236
|
+
};
|
|
5237
|
+
const near = await candidates(model, input.world.directory, lore.map(loreText), (a, b) => !apart(a, b), LORE_CLOSE, PAIRS_ASKED);
|
|
5238
|
+
if (near === void 0) throw new CheckSkipped("no embedding provider is selected to find which entries to compare");
|
|
5239
|
+
const pairs = near.map((entry) => ({
|
|
5240
|
+
near: entry,
|
|
5241
|
+
first: `entry ${lore[entry.a].id}:\n${loreText(lore[entry.a])}`,
|
|
5242
|
+
second: `entry ${lore[entry.b].id}:\n${loreText(lore[entry.b])}`
|
|
5243
|
+
}));
|
|
5244
|
+
const screening = model.text("checks/decide-conflict");
|
|
5245
|
+
const kept = await mapLimit(pairs, model.concurrency, async (pair) => model.cancelled?.() === true ? false : worthReading(model, {
|
|
5246
|
+
first: pair.first,
|
|
5247
|
+
second: pair.second
|
|
5248
|
+
}, "conflict", screening));
|
|
5249
|
+
const screened = pairs.filter((_, at) => kept[at]);
|
|
5250
|
+
const instructions = model.text("checks/conflict");
|
|
4346
5251
|
const found = [];
|
|
4347
|
-
|
|
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 }) => {
|
|
5252
|
+
await mapLimit(screened, model.concurrency, async (pair) => {
|
|
4355
5253
|
if (model.cancelled?.() === true) return;
|
|
4356
|
-
const
|
|
4357
|
-
const
|
|
4358
|
-
const text = pieces.map((piece) => `[${piece.label}]\n${piece.text.slice(0, SOURCE_CHARS)}`).join("\n\n").slice(0, SOURCE_CHARS * 2);
|
|
5254
|
+
const first = loreText(lore[pair.near.a]);
|
|
5255
|
+
const second = loreText(lore[pair.near.b]);
|
|
4359
5256
|
try {
|
|
4360
5257
|
const answer = await model.run({
|
|
4361
|
-
persona:
|
|
4362
|
-
prompt: `${
|
|
4363
|
-
|
|
4364
|
-
|
|
4365
|
-
|
|
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."
|
|
5258
|
+
persona: instructions,
|
|
5259
|
+
prompt: `${pair.first}\n\n${pair.second}`,
|
|
5260
|
+
result: CONFLICT_SCHEMA,
|
|
5261
|
+
maxTokens: 800,
|
|
5262
|
+
check: (value) => conflictProblem(value, first, second, [lore[pair.near.a].title, lore[pair.near.b].title])
|
|
4377
5263
|
});
|
|
4378
|
-
|
|
5264
|
+
if (!answer.conflict) return;
|
|
5265
|
+
const a = lore[pair.near.a];
|
|
5266
|
+
const b = lore[pair.near.b];
|
|
5267
|
+
found.push({
|
|
4379
5268
|
severity: "warning",
|
|
4380
|
-
code: "
|
|
4381
|
-
where
|
|
4382
|
-
message:
|
|
4383
|
-
hint: "
|
|
5269
|
+
code: "lore-conflict",
|
|
5270
|
+
where: `lore/${a.id}`,
|
|
5271
|
+
message: `lore/${a.id} says “${answer.first_quote.trim()}” and lore/${b.id} says “${answer.second_quote.trim()}”${because(answer.reason)}`,
|
|
5272
|
+
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
5273
|
});
|
|
4385
5274
|
} catch (error) {
|
|
4386
5275
|
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
5276
|
}
|
|
4394
5277
|
});
|
|
4395
|
-
return foundBy("
|
|
5278
|
+
return foundBy("lore-conflict", found);
|
|
4396
5279
|
}
|
|
4397
5280
|
}
|
|
4398
5281
|
];
|
|
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
5282
|
// src/host/checks/quality.ts
|
|
4543
|
-
/** A place described in fewer
|
|
5283
|
+
/** A place described in fewer letters than this has nothing to play with (a Chinese, Japanese or Korean character counts as three: it says as much as a short word). */
|
|
4544
5284
|
const THIN_PLACE = 20;
|
|
5285
|
+
const WIDE = /[\u3000-\u9fff\uac00-\ud7af\uff00-\uffef]/gu;
|
|
5286
|
+
const lettersOf = (value) => value.length + 2 * (value.match(WIDE)?.length ?? 0);
|
|
4545
5287
|
const SHEET_CHARS = 3e3;
|
|
4546
5288
|
const DIGEST_CHARS = 7e3;
|
|
4547
5289
|
const GAPS_LISTED = 8;
|
|
@@ -4552,8 +5294,8 @@ const cast = {
|
|
|
4552
5294
|
mode: "code",
|
|
4553
5295
|
needs: [],
|
|
4554
5296
|
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(
|
|
4556
|
-
const { characters } = await
|
|
5297
|
+
async run(input) {
|
|
5298
|
+
const { characters } = await loaded.characters(input);
|
|
4557
5299
|
if (characters.length === 0) throw new CheckSkipped("the world has no characters yet");
|
|
4558
5300
|
const found = [];
|
|
4559
5301
|
if (!characters.some((character) => character.role === "playable")) found.push({
|
|
@@ -4602,10 +5344,10 @@ const places = {
|
|
|
4602
5344
|
mode: "code",
|
|
4603
5345
|
needs: [],
|
|
4604
5346
|
description: "Places with no description, or one too short to play in.",
|
|
4605
|
-
async run(
|
|
4606
|
-
const { locations } = await
|
|
5347
|
+
async run(input) {
|
|
5348
|
+
const { locations } = await loaded.locations(input);
|
|
4607
5349
|
return foundBy("quality-places", locations.flatMap((place) => {
|
|
4608
|
-
const length = text(place.description)
|
|
5350
|
+
const length = lettersOf(text(place.description));
|
|
4609
5351
|
if (length === 0) return [{
|
|
4610
5352
|
severity: "warning",
|
|
4611
5353
|
code: "place-undescribed",
|
|
@@ -4627,8 +5369,8 @@ const about = {
|
|
|
4627
5369
|
mode: "code",
|
|
4628
5370
|
needs: [],
|
|
4629
5371
|
description: "What the world says about itself: a description, tags for the catalogue, an opening for the story.",
|
|
4630
|
-
async run(
|
|
4631
|
-
const info = await
|
|
5372
|
+
async run(input) {
|
|
5373
|
+
const info = await loaded.info(input);
|
|
4632
5374
|
const found = [];
|
|
4633
5375
|
if (text(info.description) === "") found.push({
|
|
4634
5376
|
severity: "info",
|
|
@@ -4666,8 +5408,9 @@ const sheets = {
|
|
|
4666
5408
|
mode: "decision",
|
|
4667
5409
|
needs: ["model"],
|
|
4668
5410
|
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(
|
|
4670
|
-
const {
|
|
5411
|
+
async run(input) {
|
|
5412
|
+
const { model } = input;
|
|
5413
|
+
const { characters } = await loaded.characters(input);
|
|
4671
5414
|
const asked = characters.filter((character) => character.tier !== "background" && character.kind === "person");
|
|
4672
5415
|
const found = [];
|
|
4673
5416
|
await mapLimit(asked, model.concurrency, async (character) => {
|
|
@@ -4735,13 +5478,14 @@ const qualityChecks = [
|
|
|
4735
5478
|
mode: "judged",
|
|
4736
5479
|
needs: ["model"],
|
|
4737
5480
|
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(
|
|
5481
|
+
async run(input) {
|
|
5482
|
+
const { world, model } = input;
|
|
4739
5483
|
const [{ characters }, { locations }, { factions }, { lore }, { defs }] = await Promise.all([
|
|
4740
|
-
|
|
4741
|
-
|
|
4742
|
-
|
|
4743
|
-
|
|
4744
|
-
|
|
5484
|
+
loaded.characters(input),
|
|
5485
|
+
loaded.locations(input),
|
|
5486
|
+
loaded.factions(input),
|
|
5487
|
+
loaded.lore(input),
|
|
5488
|
+
loaded.variables(input)
|
|
4745
5489
|
]);
|
|
4746
5490
|
const named = /* @__PURE__ */ new Map();
|
|
4747
5491
|
for (const [kind, entries] of [
|
|
@@ -4770,7 +5514,7 @@ const qualityChecks = [
|
|
|
4770
5514
|
/** What the model named, as the key of that thing; anything else is about the world as a whole. */
|
|
4771
5515
|
const keyOf = (about) => named.get(norm(about)) ?? "(the world)";
|
|
4772
5516
|
const digest = [
|
|
4773
|
-
`world: ${world.summary.name}: ${text((await
|
|
5517
|
+
`world: ${world.summary.name}: ${text((await loaded.info(input)).description)}`,
|
|
4774
5518
|
...characters.map((entry) => `character ${entry.id} (${entry.role}, ${entry.tier}): ${entry.name}: ${entry.summary}`),
|
|
4775
5519
|
...locations.map((entry) => `place ${entry.id}: ${entry.name}: ${text(entry.description).slice(0, 160)} (${entry.exits.length} ways out)`),
|
|
4776
5520
|
...factions.map((entry) => `organisation ${entry.id}: ${entry.name}: ${entry.summary}`),
|
|
@@ -4794,65 +5538,31 @@ const qualityChecks = [
|
|
|
4794
5538
|
}
|
|
4795
5539
|
}
|
|
4796
5540
|
];
|
|
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
5541
|
// src/host/checks/run.ts
|
|
4838
5542
|
/** Every check of the suite, grouped by kit; the kits are the unit a command or a tool asks for. */
|
|
4839
5543
|
const CHECKS = [
|
|
4840
5544
|
...consistencyChecks,
|
|
5545
|
+
...semanticChecks,
|
|
4841
5546
|
...fidelityChecks,
|
|
4842
|
-
...qualityChecks
|
|
5547
|
+
...qualityChecks,
|
|
5548
|
+
...playabilityChecks
|
|
4843
5549
|
];
|
|
4844
5550
|
const checksOf = (kits, checks = CHECKS) => checks.filter((check) => kits.includes(check.kit));
|
|
4845
5551
|
/**
|
|
4846
5552
|
* 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
5553
|
* reports it with what changed since the checks last ran.
|
|
4848
5554
|
*/
|
|
4849
|
-
async function runChecks(
|
|
5555
|
+
async function runChecks(given, run = {}, checks = CHECKS, now = () => /* @__PURE__ */ new Date()) {
|
|
5556
|
+
const input = {
|
|
5557
|
+
...given,
|
|
5558
|
+
memo: /* @__PURE__ */ new Map()
|
|
5559
|
+
};
|
|
4850
5560
|
const kits = run.kits ?? KITS;
|
|
4851
5561
|
const chosen = checksOf(kits, checks).filter((check) => run.ids === void 0 || run.ids.includes(check.id));
|
|
4852
5562
|
const results = {};
|
|
4853
5563
|
const skipped = [];
|
|
4854
5564
|
for (const check of chosen) {
|
|
4855
|
-
if (check.mode !== "code" && run.deep !== true) {
|
|
5565
|
+
if (check.mode !== "code" && run.deep !== true && !(run.deep === "decision" && check.mode === "decision")) {
|
|
4856
5566
|
skipped.push({
|
|
4857
5567
|
check: check.id,
|
|
4858
5568
|
reason: "judged by a model: ask for a deep check to run it"
|
|
@@ -4893,14 +5603,30 @@ async function runChecks(input, run = {}, checks = CHECKS, now = () => /* @__PUR
|
|
|
4893
5603
|
/** The most findings a tool result lists: the rest are counted. */
|
|
4894
5604
|
const LISTED = 40;
|
|
4895
5605
|
const RESOLVED_LISTED = 10;
|
|
5606
|
+
/** 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. */
|
|
5607
|
+
const GROUPED_AT = 5;
|
|
5608
|
+
/** 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. */
|
|
5609
|
+
function grouped(findings) {
|
|
5610
|
+
const kinds = /* @__PURE__ */ new Map();
|
|
5611
|
+
for (const finding of findings) {
|
|
5612
|
+
const key = `${finding.check}\n${finding.code}`;
|
|
5613
|
+
kinds.set(key, [...kinds.get(key) ?? [], finding]);
|
|
5614
|
+
}
|
|
5615
|
+
return [...kinds.values()].flatMap((same) => same.length <= GROUPED_AT ? same : [{
|
|
5616
|
+
...same[0],
|
|
5617
|
+
count: same.length,
|
|
5618
|
+
also: same.slice(1, 9).map((finding) => finding.where)
|
|
5619
|
+
}]).sort((a, b) => SEVERITIES.indexOf(a.severity) - SEVERITIES.indexOf(b.severity));
|
|
5620
|
+
}
|
|
4896
5621
|
/** What the assistant reads of a report: counts first, the worst findings, then what changed since the last run. */
|
|
4897
5622
|
function presentReport(report) {
|
|
4898
5623
|
const count = (severity) => report.findings.filter((finding) => finding.severity === severity).length;
|
|
5624
|
+
const presented = grouped(report.findings);
|
|
4899
5625
|
return {
|
|
4900
5626
|
ok: count("error") === 0,
|
|
4901
5627
|
counts: Object.fromEntries(SEVERITIES.map((severity) => [severity, count(severity)])),
|
|
4902
|
-
findings:
|
|
4903
|
-
more: Math.max(0,
|
|
5628
|
+
findings: presented.slice(0, LISTED),
|
|
5629
|
+
more: Math.max(0, presented.length - LISTED),
|
|
4904
5630
|
skipped: report.skipped,
|
|
4905
5631
|
...report.changes === void 0 ? {} : { since_last_run: {
|
|
4906
5632
|
new: report.changes.added,
|
|
@@ -4912,18 +5638,24 @@ function presentReport(report) {
|
|
|
4912
5638
|
// src/host/checks/imported.ts
|
|
4913
5639
|
/**
|
|
4914
5640
|
* 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.
|
|
5641
|
+
* 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
|
|
5642
|
+
* yes-or-no checks, which run when the import has a decision model to answer them.
|
|
4916
5643
|
* @returns what the assistant reads of the report.
|
|
4917
5644
|
*/
|
|
4918
|
-
async function checkImported(directory, name) {
|
|
5645
|
+
async function checkImported(directory, name, model) {
|
|
4919
5646
|
const card = await loadCardEvidence(directory, "current");
|
|
5647
|
+
const deciding = model?.decide !== void 0;
|
|
4920
5648
|
return presentReport(await runChecks({
|
|
4921
5649
|
world: {
|
|
4922
5650
|
directory,
|
|
4923
5651
|
summary: { name }
|
|
4924
5652
|
},
|
|
4925
|
-
...card === void 0 ? {} : { card }
|
|
4926
|
-
|
|
5653
|
+
...card === void 0 ? {} : { card },
|
|
5654
|
+
...deciding ? { model } : {}
|
|
5655
|
+
}, {
|
|
5656
|
+
kits: KITS,
|
|
5657
|
+
...deciding ? { deep: "decision" } : {}
|
|
5658
|
+
}));
|
|
4927
5659
|
}
|
|
4928
5660
|
// src/host/import/run-import.ts
|
|
4929
5661
|
/** 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 +5896,7 @@ async function finish(ctx, world) {
|
|
|
5164
5896
|
}, await readFile(join(folder(world), "card.json"), "utf8"));
|
|
5165
5897
|
return {
|
|
5166
5898
|
...result,
|
|
5167
|
-
checks: await checkImported(world, choices.name)
|
|
5899
|
+
checks: await checkImported(world, choices.name, ctx)
|
|
5168
5900
|
};
|
|
5169
5901
|
}
|
|
5170
5902
|
return result;
|
|
@@ -5304,8 +6036,21 @@ function contextOf(exec, settings, text, language) {
|
|
|
5304
6036
|
if (service === void 0) return Promise.reject(/* @__PURE__ */ new Error("no decision service is selected, or the selected one is not loaded"));
|
|
5305
6037
|
return askYesNo(service, state, asks, exec.signal);
|
|
5306
6038
|
} : void 0;
|
|
6039
|
+
const embed = async (texts) => {
|
|
6040
|
+
const service = agent.ctx.get("embeddings");
|
|
6041
|
+
if (service === void 0) throw new NoEmbeddingService("no embedding provider is selected, or the selected one is not loaded");
|
|
6042
|
+
const { vectors, model } = await service.embed(texts, {
|
|
6043
|
+
inputType: "document",
|
|
6044
|
+
signal: exec.signal
|
|
6045
|
+
});
|
|
6046
|
+
return {
|
|
6047
|
+
vectors,
|
|
6048
|
+
model
|
|
6049
|
+
};
|
|
6050
|
+
};
|
|
5307
6051
|
return {
|
|
5308
6052
|
run,
|
|
6053
|
+
embed,
|
|
5309
6054
|
cancelled: () => exec.signal.aborted,
|
|
5310
6055
|
concurrency: settings.concurrency,
|
|
5311
6056
|
variablesPerRequest: settings.variablesPerRequest,
|
|
@@ -5542,6 +6287,6 @@ function limitAuthoringAgents(ctx, kindOf) {
|
|
|
5542
6287
|
return await next();
|
|
5543
6288
|
});
|
|
5544
6289
|
}
|
|
5545
|
-
export {
|
|
6290
|
+
export { parseWorldFile as $, pictureOf as $t, entitiesByPiece as A, routineProblems as At, readOrigin as B, loadFactions as Bt, readWorldInfo as C, populateItems as Ct, baseId as D, populateRoutines as Dt, UPDATE_FOLDER as E, populateLore as Et, writeSnapshot as F, LOCATIONS_DIR as Ft, candidateUrls as G, loadCalendar as Gt, CatalogueError as H, loadDice as Ht, describeChanges as I, loadLocations as It, mergeReports as J, loadCharacters as Jt, fetchPictures as K, parseCalendarFile as Kt, hashWorld as L, ITEMS_DIR as Lt, CHECK_COMMANDS as M, loadTriggers as Mt, recordProvenance as N, LORE_DIR as Nt, cardPieces as O, populateTriggers as Ot, editsSince as P, loadLore as Pt, WORLD_FILE_NAME as Q, isPng as Qt, isUnchanged as R, loadItems as Rt, editWorldInfo as S, populateFactions as St, loadCardEvidence as T, populateLocations as Tt, addressKey as U, parseDiceFile as Ut, writeOrigin as V, DICE_FILE_NAME as Vt, addressesToFetch as W, CALENDAR_FILE_NAME as Wt, cardCharacter as X, knownOutputCeiling as Xt, vetUrl as Y, subfolders as Yt, writePortrait as Z, ImportCardError as Zt, runChecks as _, knowsCharacter as _t, importHooks as a, authoringChatSessionId as an, entriesOf as at, matchPieces as b, populateClock as bt, importPhase as c, loadLorebook as ct, markReady as d, reserveLorebook as dt, readCard as en, parseWorldInfoEdit as et, settleDraft as f, updateLorebookInfo as ft, CHECKS as g, withScratchStory as gt, presentReport as h, loadValidatedWorld as ht, contextOf as i, authoredWorldOf as in, deleteLorebookEntry as it, readSource as j, TRIGGERS_FILE_NAME as jt, cardTools as k, populateVariables as kt, isReviewing as l, lorebookDirectory as lt, checkImported as m, checkWorld as mt, limitAuthoringAgents as n, DICE_READINGS as nn, createLorebook as nt, importTools as o, isLorebookName as ot, structureImport as p, writeLorebookEntry as pt, guardedLookup as q, CHARACTERS_DIR as qt, MAX_PREVIEW_TEXT as r, DICE_RESULTS as rn, deleteLorebook as rt, worldOf as s, listLorebooks as st, AuthoringTools as t, DICE_COMPARISONS as tn, addLorebookEntries as tt, keepDraft as u, readLorebook as ut, cardChanges as v, populateCalendar as vt, loadWorldParts as w, populateKnowledge as wt, worldFieldChanges as x, populateDice as xt, diffText as y, populateCharacters as yt, localChanges as z, FACTIONS_DIR as zt };
|
|
5546
6291
|
|
|
5547
|
-
//# sourceMappingURL=authoring-tools-
|
|
6292
|
+
//# sourceMappingURL=authoring-tools-B4mEdKp_.js.map
|