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