@dsh-agentic-roleplay/core 0.1.1 → 0.2.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/{activity-BbqFG9-3.js → activity-t7OBgHlc.js} +255 -61
- package/lib/authoring.js +295 -14
- package/lib/client.js +1896 -453
- package/lib/client.js.map +1 -1
- package/lib/compaction.js +1 -1
- package/lib/{documents-ZVoUTnD2.js → documents-Bu5uYRZB.js} +3 -3
- package/lib/{gallery-KGCdeCnM.js → gallery-DUeBMHQX.js} +2 -2
- package/lib/{generator-NYnNdpWb.js → generator-HPifO3Kk.js} +125 -20
- package/lib/images.js +3 -3
- package/lib/index.js +638 -14
- package/lib/{narration-DcnCqA1l.js → narration-BRyHU0H2.js} +10 -4
- package/lib/orchestration.js +303 -161
- package/lib/packs-ByP1J-zj.js +1334 -0
- package/lib/{presenter-agent-7j1c7QEz.js → presenter-agent-G43ODV8I.js} +3 -3
- package/package.json +4 -3
- package/prompts/en/roleplay/ask-answerer.md +3 -0
- package/prompts/en/roleplay/ask-question.md +2 -0
- package/prompts/en/roleplay/character-decide.md +1 -1
- package/prompts/en/roleplay/character-lookup.md +1 -1
- package/prompts/en/roleplay/game-master.md +5 -1
- package/prompts/en/roleplay/ooc-empty.md +1 -1
- package/prompts/en/world/procedure.md +1 -1
- package/prompts/zh/roleplay/ask-answerer.md +3 -0
- package/prompts/zh/roleplay/ask-question.md +2 -0
- package/prompts/zh/roleplay/character-decide.md +1 -1
- package/prompts/zh/roleplay/character-lookup.md +1 -1
- package/prompts/zh/roleplay/game-master.md +5 -1
- package/prompts/zh/roleplay/ooc-empty.md +1 -1
- package/prompts/zh/world/procedure.md +1 -1
- package/starter-worlds/blackwater-observatory/ui/notebook/map.html +12 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/map.js +40 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/notebook.html +29 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/notebook.js +45 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/pack.json +31 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/skin.css +16 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/strip.html +11 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/strip.js +12 -0
- package/starter-worlds/blackwater-observatory/ui/notebook/style.css +23 -0
- package/lib/worlds-DYgjdH8O.js +0 -504
|
@@ -0,0 +1,1334 @@
|
|
|
1
|
+
import "./src-CWTNeL6Y.js";
|
|
2
|
+
import { dirname, extname, join, posix, sep } from "node:path";
|
|
3
|
+
import { copyFileSync, cpSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync } from "node:fs";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { Service } from "@deepseek-ai/cordis";
|
|
7
|
+
import { readFile, stat } from "node:fs/promises";
|
|
8
|
+
//#region ../dsh-roleplay-model/src/intent.ts
|
|
9
|
+
const INTENT_TYPES = [
|
|
10
|
+
"need",
|
|
11
|
+
"obligation",
|
|
12
|
+
"quest",
|
|
13
|
+
"agenda",
|
|
14
|
+
"aspiration",
|
|
15
|
+
"relationship",
|
|
16
|
+
"habit",
|
|
17
|
+
"reaction"
|
|
18
|
+
];
|
|
19
|
+
const INTENT_STATUSES = [
|
|
20
|
+
"active",
|
|
21
|
+
"paused",
|
|
22
|
+
"completed",
|
|
23
|
+
"failed",
|
|
24
|
+
"abandoned"
|
|
25
|
+
];
|
|
26
|
+
const INTENT_HORIZONS = [
|
|
27
|
+
"immediate",
|
|
28
|
+
"short",
|
|
29
|
+
"day",
|
|
30
|
+
"long",
|
|
31
|
+
"open_ended"
|
|
32
|
+
];
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region ../dsh-roleplay-model/src/plot.ts
|
|
35
|
+
const PLOT_STATUSES = [
|
|
36
|
+
"dormant",
|
|
37
|
+
"available",
|
|
38
|
+
"engaged",
|
|
39
|
+
"cooling",
|
|
40
|
+
"resolved",
|
|
41
|
+
"abandoned"
|
|
42
|
+
];
|
|
43
|
+
const PLOT_NUDGE_MODES = [
|
|
44
|
+
"opportunity",
|
|
45
|
+
"reminder",
|
|
46
|
+
"character",
|
|
47
|
+
"consequence",
|
|
48
|
+
"escalation",
|
|
49
|
+
"breathing_room"
|
|
50
|
+
];
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/host/sessions/errors.ts
|
|
53
|
+
var WorldNotFoundError = class extends Error {};
|
|
54
|
+
/** The world cannot be played as it is; `problems` say what to fix. */
|
|
55
|
+
var InvalidWorldError = class extends Error {
|
|
56
|
+
problems;
|
|
57
|
+
constructor(message, problems) {
|
|
58
|
+
super(message);
|
|
59
|
+
this.problems = problems;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
/** The request cannot be honoured for this world, such as a character that cannot be played. */
|
|
63
|
+
var InvalidStartError = class extends Error {};
|
|
64
|
+
var SessionNotFoundError = class extends Error {};
|
|
65
|
+
/** DSH's session services are not available, so no chat can be created or retired. */
|
|
66
|
+
var AgentUnavailableError = class extends Error {};
|
|
67
|
+
/** The session is put away and cannot be played until it is brought back. */
|
|
68
|
+
var SessionArchivedError = class extends Error {};
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/host/prompts/library.ts
|
|
71
|
+
const PROMPTS_FOLDER = "prompts";
|
|
72
|
+
const SESSION_ID = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
|
|
73
|
+
const ID$1 = /^[a-z0-9-]+(\/[a-z0-9-]+)*$/;
|
|
74
|
+
const LANGUAGE = /^[a-z]{2,8}$/;
|
|
75
|
+
const VARIABLE = /\$\{([A-Za-z]+)\}/g;
|
|
76
|
+
/** The folder of the prompts that ship with the package, found from this module so that it works from source and from the build. */
|
|
77
|
+
function shippedPromptsDirectory(from = dirname(fileURLToPath(import.meta.url))) {
|
|
78
|
+
for (let directory = from;; directory = dirname(directory)) {
|
|
79
|
+
const candidate = join(directory, PROMPTS_FOLDER);
|
|
80
|
+
if (existsSync(join(directory, "package.json")) && existsSync(candidate)) return candidate;
|
|
81
|
+
if (dirname(directory) === directory) throw new Error("the prompts shipped with the package were not found");
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** `zh-CN` and `ZH` are both `zh`; anything that is not a language tag is refused. */
|
|
85
|
+
function languageOf(tag) {
|
|
86
|
+
const primary = (tag ?? "en").trim().toLowerCase().split(/[-_]/)[0] ?? "";
|
|
87
|
+
if (!LANGUAGE.test(primary)) throw new Error(`"${tag}" is not a language`);
|
|
88
|
+
return primary;
|
|
89
|
+
}
|
|
90
|
+
function fill(id, text, variables) {
|
|
91
|
+
return text.trim().replace(/^\$\{([A-Za-z]+)\}[ \t]*(\r?\n|$)/gm, (match, name) => variables[name] === "" ? "" : match).replace(VARIABLE, (_match, name) => {
|
|
92
|
+
const value = variables[name];
|
|
93
|
+
if (value === void 0) throw new Error(`prompt ${id} uses \${${name}}, which is not given a value (given: ${Object.keys(variables).join(", ") || "none"})`);
|
|
94
|
+
return String(value);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/** A prompt as the package ships it, without a roleplay root: for code that has none, such as tests. */
|
|
98
|
+
function shippedText(id, language = "en", variables = {}) {
|
|
99
|
+
return fill(id, readFileSync(join(shippedPromptsDirectory(), language, `${id}.md`), "utf8"), variables);
|
|
100
|
+
}
|
|
101
|
+
function listFiles(directory, prefix = "") {
|
|
102
|
+
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
|
|
103
|
+
const name = `${prefix}${entry.name}`;
|
|
104
|
+
if (entry.isDirectory()) return listFiles(join(directory, entry.name), `${name}/`);
|
|
105
|
+
return entry.name.endsWith(".md") ? [name.slice(0, -3)] : [];
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Every instruction the models are given is a file under `<root>/prompts/<language>/`, so that the person can edit them. The files
|
|
110
|
+
* come from the package: one that is not there (never written, or deleted to restore it) is copied again when it is needed.
|
|
111
|
+
*/
|
|
112
|
+
var PromptLibrary = class extends Service {
|
|
113
|
+
root;
|
|
114
|
+
shipped;
|
|
115
|
+
constructor(ctx, root, shipped = shippedPromptsDirectory()) {
|
|
116
|
+
super(ctx, "roleplayPrompts");
|
|
117
|
+
this.root = root;
|
|
118
|
+
this.shipped = shipped;
|
|
119
|
+
}
|
|
120
|
+
get directory() {
|
|
121
|
+
return join(this.root.path, PROMPTS_FOLDER);
|
|
122
|
+
}
|
|
123
|
+
/** The languages the package ships prompts in. */
|
|
124
|
+
get languages() {
|
|
125
|
+
return readdirSync(this.shipped, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
|
|
126
|
+
}
|
|
127
|
+
/** Writes every shipped prompt that is missing, so that they can be found and edited before they are first used. */
|
|
128
|
+
bootstrap() {
|
|
129
|
+
for (const language of this.languages) for (const id of listFiles(join(this.shipped, language))) this.file(id, language);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The prompt `id` in the language (the primary subtag of a language tag), with `${name}` replaced by the variables.
|
|
133
|
+
* A session may carry a file of its own under `prompts/<language>/` in its folder, which is used instead of the one in the root.
|
|
134
|
+
*/
|
|
135
|
+
text(id, language, variables = {}, session) {
|
|
136
|
+
if (!ID$1.test(id)) throw new Error(`"${id}" is not a prompt id`);
|
|
137
|
+
const tag = languageOf(language);
|
|
138
|
+
const own = session === void 0 ? void 0 : this.override(id, tag, session);
|
|
139
|
+
return fill(id, readFileSync(own ?? this.file(id, tag), "utf8"), variables);
|
|
140
|
+
}
|
|
141
|
+
override(id, language, session) {
|
|
142
|
+
if (!SESSION_ID.test(session)) throw new Error(`"${session}" is not a session id`);
|
|
143
|
+
const found = join(this.root.path, "sessions", session, PROMPTS_FOLDER, language, `${id}.md`);
|
|
144
|
+
return this.usable(found, id) ? found : void 0;
|
|
145
|
+
}
|
|
146
|
+
/** The path of the prompt in the root, copied from the package first when it is not there. A language the package has no prompt in uses the English one. */
|
|
147
|
+
file(id, language) {
|
|
148
|
+
const found = join(this.directory, language, `${id}.md`);
|
|
149
|
+
if (this.usable(found, id)) return found;
|
|
150
|
+
const source = [language, "en"].map((name) => join(this.shipped, name, `${id}.md`)).find((path) => existsSync(path));
|
|
151
|
+
if (source === void 0) throw new Error(`there is no prompt ${id} in ${language} or en`);
|
|
152
|
+
const target = source.startsWith(join(this.shipped, language)) ? found : join(this.directory, "en", `${id}.md`);
|
|
153
|
+
if (!this.usable(target, id)) {
|
|
154
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
155
|
+
copyFileSync(source, target);
|
|
156
|
+
}
|
|
157
|
+
return target;
|
|
158
|
+
}
|
|
159
|
+
usable(path, id) {
|
|
160
|
+
if (!existsSync(path)) return false;
|
|
161
|
+
if (statSync(path).size === 0 || readFileSync(path, "utf8").trim() === "") throw new Error(`the prompt file ${path} (${id}) is empty; delete it to restore the original`);
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
const names = (text) => new Set([...text.matchAll(VARIABLE)].map((match) => match[1]));
|
|
166
|
+
/** Why a text cannot be the prompt `id` in the language, or nothing when it can: it must be a prompt the package has, not empty, and use only the variables the prompt is given. */
|
|
167
|
+
function promptProblem(language, id, text, shipped = shippedPromptsDirectory()) {
|
|
168
|
+
const original = ID$1.test(id) ? [language, "en"].map((name) => join(shipped, name, `${id}.md`)).find((path) => existsSync(path)) : void 0;
|
|
169
|
+
if (original === void 0) return `there is no prompt "${id}"`;
|
|
170
|
+
if (text.trim() === "") return "the prompt is empty";
|
|
171
|
+
const allowed = names(readFileSync(original, "utf8"));
|
|
172
|
+
const unknown = [...names(text)].filter((name) => !allowed.has(name));
|
|
173
|
+
return unknown.length === 0 ? void 0 : `uses ${unknown.map((name) => `\${${name}}`).join(", ")}, which the prompt is not given (it is given: ${[...allowed].join(", ") || "none"})`;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Copies the prompts a world carries (`prompts/<language>/<id>.md`) into a session folder, where they take the place of the ones in the root.
|
|
177
|
+
* Each must be a prompt the package has, in a language it ships, not empty, and may use only the variables that prompt is given.
|
|
178
|
+
* @returns the problems found, each with the file; nothing is copied when there are any.
|
|
179
|
+
*/
|
|
180
|
+
function copyWorldPrompts(worldDirectory, target, shipped = shippedPromptsDirectory()) {
|
|
181
|
+
const source = join(worldDirectory, PROMPTS_FOLDER);
|
|
182
|
+
if (!existsSync(source)) return [];
|
|
183
|
+
const problems = [];
|
|
184
|
+
const found = [];
|
|
185
|
+
for (const entry of readdirSync(source, { withFileTypes: true })) {
|
|
186
|
+
const folder = join(source, entry.name);
|
|
187
|
+
if (!entry.isDirectory() || !LANGUAGE.test(entry.name)) {
|
|
188
|
+
problems.push({
|
|
189
|
+
path: folder,
|
|
190
|
+
message: "prompts are kept in a folder named by the language, such as en or zh"
|
|
191
|
+
});
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
for (const id of listFiles(folder)) {
|
|
195
|
+
const from = join(folder, `${id}.md`);
|
|
196
|
+
const message = promptProblem(entry.name, id, readFileSync(from, "utf8"), shipped);
|
|
197
|
+
if (message !== void 0) {
|
|
198
|
+
problems.push({
|
|
199
|
+
path: from,
|
|
200
|
+
message
|
|
201
|
+
});
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
found.push({
|
|
205
|
+
from,
|
|
206
|
+
to: join(target, PROMPTS_FOLDER, entry.name, `${id}.md`)
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
if (problems.length > 0) return problems;
|
|
211
|
+
for (const { from, to } of found) {
|
|
212
|
+
mkdirSync(dirname(to), { recursive: true });
|
|
213
|
+
copyFileSync(from, to);
|
|
214
|
+
}
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
/** A branch plays with the prompts of the session it was cut from. */
|
|
218
|
+
function inheritPrompts(from, to) {
|
|
219
|
+
if (existsSync(join(from, "prompts"))) cpSync(join(from, PROMPTS_FOLDER), join(to, PROMPTS_FOLDER), { recursive: true });
|
|
220
|
+
}
|
|
221
|
+
/** The languages the package ships prompts in. */
|
|
222
|
+
function shippedLanguages(shipped = shippedPromptsDirectory()) {
|
|
223
|
+
return readdirSync(shipped, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
|
|
224
|
+
}
|
|
225
|
+
//#endregion
|
|
226
|
+
//#region src/host/validation.ts
|
|
227
|
+
function formatIssue({ path, message }) {
|
|
228
|
+
return path === "" ? message : `${path}: ${message}`;
|
|
229
|
+
}
|
|
230
|
+
function isRecord(value) {
|
|
231
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Reads a JSON object field by field. Problems are collected with their path
|
|
235
|
+
* instead of stopping at the first one, so an author sees everything wrong in
|
|
236
|
+
* a file at once. A missing field, `null` and a blank string all mean "absent".
|
|
237
|
+
*/
|
|
238
|
+
var Reader = class Reader {
|
|
239
|
+
data;
|
|
240
|
+
path;
|
|
241
|
+
issues;
|
|
242
|
+
constructor(data, path, issues) {
|
|
243
|
+
this.data = data;
|
|
244
|
+
this.path = path;
|
|
245
|
+
this.issues = issues;
|
|
246
|
+
}
|
|
247
|
+
/** Parse JSON text whose top level must be an object; reports why when it is not. */
|
|
248
|
+
static fromJson(text, issues) {
|
|
249
|
+
let data;
|
|
250
|
+
try {
|
|
251
|
+
data = JSON.parse(text.replace(/^/, ""));
|
|
252
|
+
} catch (error) {
|
|
253
|
+
issues.push({
|
|
254
|
+
path: "",
|
|
255
|
+
message: `invalid JSON: ${error instanceof Error ? error.message : String(error)}`
|
|
256
|
+
});
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (!isRecord(data)) {
|
|
260
|
+
issues.push({
|
|
261
|
+
path: "",
|
|
262
|
+
message: "expected a JSON object"
|
|
263
|
+
});
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
return new Reader(data, "", issues);
|
|
267
|
+
}
|
|
268
|
+
fail(key, message) {
|
|
269
|
+
this.issues.push({
|
|
270
|
+
path: this.child(key),
|
|
271
|
+
message
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
/** The value as written, with `null` read as absent. */
|
|
275
|
+
raw(key) {
|
|
276
|
+
const value = this.data[key];
|
|
277
|
+
return value === null ? void 0 : value;
|
|
278
|
+
}
|
|
279
|
+
string(key, options = {}) {
|
|
280
|
+
const value = this.raw(key);
|
|
281
|
+
if (value !== void 0 && typeof value !== "string") {
|
|
282
|
+
this.fail(key, "must be a string");
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const text = value?.trim();
|
|
286
|
+
if (text === void 0 || text === "") {
|
|
287
|
+
if (options.required === true) this.fail(key, "is required");
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
return text;
|
|
291
|
+
}
|
|
292
|
+
/** A string that is empty when absent. */
|
|
293
|
+
text(key) {
|
|
294
|
+
return this.string(key) ?? "";
|
|
295
|
+
}
|
|
296
|
+
number(key, range = {}) {
|
|
297
|
+
const value = this.raw(key);
|
|
298
|
+
if (value === void 0) return void 0;
|
|
299
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
300
|
+
this.fail(key, "must be a number");
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (range.min !== void 0 && value < range.min || range.max !== void 0 && value > range.max) {
|
|
304
|
+
this.fail(key, `must be between ${range.min ?? "-∞"} and ${range.max ?? "∞"}`);
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
return value;
|
|
308
|
+
}
|
|
309
|
+
oneOf(key, allowed, fallback) {
|
|
310
|
+
const value = this.string(key);
|
|
311
|
+
if (value === void 0) return fallback;
|
|
312
|
+
const match = allowed.find((candidate) => candidate === value);
|
|
313
|
+
if (match === void 0) {
|
|
314
|
+
this.fail(key, `must be one of: ${allowed.join(", ")}`);
|
|
315
|
+
return fallback;
|
|
316
|
+
}
|
|
317
|
+
return match;
|
|
318
|
+
}
|
|
319
|
+
/** An array of strings, trimmed, without blanks or repeats. */
|
|
320
|
+
strings(key) {
|
|
321
|
+
const value = this.raw(key);
|
|
322
|
+
if (value === void 0) return [];
|
|
323
|
+
if (!Array.isArray(value)) {
|
|
324
|
+
this.fail(key, "must be an array of strings");
|
|
325
|
+
return [];
|
|
326
|
+
}
|
|
327
|
+
const result = /* @__PURE__ */ new Set();
|
|
328
|
+
value.forEach((entry, index) => {
|
|
329
|
+
if (typeof entry !== "string") this.fail(`${key}[${index}]`, "must be a string");
|
|
330
|
+
else if (entry.trim() !== "") result.add(entry.trim());
|
|
331
|
+
});
|
|
332
|
+
return [...result];
|
|
333
|
+
}
|
|
334
|
+
object(key) {
|
|
335
|
+
const value = this.raw(key);
|
|
336
|
+
if (value === void 0) return void 0;
|
|
337
|
+
if (!isRecord(value)) {
|
|
338
|
+
this.fail(key, "must be an object");
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
return new Reader(value, this.child(key), this.issues);
|
|
342
|
+
}
|
|
343
|
+
objects(key) {
|
|
344
|
+
const value = this.raw(key);
|
|
345
|
+
if (value === void 0) return [];
|
|
346
|
+
if (!Array.isArray(value)) {
|
|
347
|
+
this.fail(key, "must be an array of objects");
|
|
348
|
+
return [];
|
|
349
|
+
}
|
|
350
|
+
return value.flatMap((entry, index) => {
|
|
351
|
+
if (isRecord(entry)) return [new Reader(entry, this.child(`${key}[${index}]`), this.issues)];
|
|
352
|
+
this.fail(`${key}[${index}]`, "must be an object");
|
|
353
|
+
return [];
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
child(key) {
|
|
357
|
+
return this.path === "" ? key : `${this.path}.${key}`;
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
//#endregion
|
|
361
|
+
//#region src/host/errors.ts
|
|
362
|
+
function errorMessage(error) {
|
|
363
|
+
return error instanceof Error ? error.message : String(error);
|
|
364
|
+
}
|
|
365
|
+
//#endregion
|
|
366
|
+
//#region src/host/read-document.ts
|
|
367
|
+
/** Authored documents are short; a larger file is not one. */
|
|
368
|
+
const MAX_DOCUMENT_BYTES = 1048576;
|
|
369
|
+
/**
|
|
370
|
+
* Read and parse one authored document. Every problem is added to `problems`
|
|
371
|
+
* with the file's path, and the document is skipped.
|
|
372
|
+
*/
|
|
373
|
+
async function readDocument(file, parse, problems) {
|
|
374
|
+
if ((await stat(file)).size > MAX_DOCUMENT_BYTES) {
|
|
375
|
+
problems.push({
|
|
376
|
+
path: file,
|
|
377
|
+
message: "file is too large"
|
|
378
|
+
});
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
const result = parse(await readFile(file, "utf8"));
|
|
382
|
+
if (result.ok) return result.value;
|
|
383
|
+
for (const issue of result.issues) problems.push({
|
|
384
|
+
path: file,
|
|
385
|
+
message: formatIssue(issue)
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
//#endregion
|
|
389
|
+
//#region src/host/plots/load.ts
|
|
390
|
+
const PLOTS_FILE_NAME = "plots.json";
|
|
391
|
+
function development(reader, fallback) {
|
|
392
|
+
return {
|
|
393
|
+
id: reader.string("id") ?? fallback,
|
|
394
|
+
text: reader.string("text", { required: true }) ?? "",
|
|
395
|
+
mode: reader.oneOf("mode", PLOT_NUDGE_MODES, "opportunity"),
|
|
396
|
+
prerequisites: reader.strings("prerequisites"),
|
|
397
|
+
guidance: reader.text("guidance"),
|
|
398
|
+
avoid: reader.strings("avoid")
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
/** Parse the optional world-level list of dramatic possibilities. */
|
|
402
|
+
function parsePlotsFile(text) {
|
|
403
|
+
const issues = [];
|
|
404
|
+
const wrapper = Reader.fromJson(`{"plots":${text.replace(/^/, "")}}`, issues);
|
|
405
|
+
if (wrapper === void 0) return {
|
|
406
|
+
ok: false,
|
|
407
|
+
issues
|
|
408
|
+
};
|
|
409
|
+
const plots = wrapper.objects("plots").map((reader, plotIndex) => {
|
|
410
|
+
const pacing = reader.object("pacing");
|
|
411
|
+
const hooks = reader.objects("hooks").map((entry, index) => development(entry, `hook-${index + 1}`));
|
|
412
|
+
const developments = reader.objects("developments").map((entry, index) => development(entry, `development-${index + 1}`));
|
|
413
|
+
return {
|
|
414
|
+
id: reader.string("id", { required: true }) ?? `plot-${plotIndex + 1}`,
|
|
415
|
+
name: reader.string("name", { required: true }) ?? "",
|
|
416
|
+
premise: reader.string("premise", { required: true }) ?? "",
|
|
417
|
+
themes: reader.strings("themes"),
|
|
418
|
+
hooks,
|
|
419
|
+
developments,
|
|
420
|
+
stakes: reader.strings("stakes"),
|
|
421
|
+
resolutions: reader.strings("resolutions"),
|
|
422
|
+
participants: reader.strings("participants"),
|
|
423
|
+
start: reader.oneOf("start", PLOT_STATUSES, "available"),
|
|
424
|
+
pacing: {
|
|
425
|
+
quietTurns: pacing?.number("quiet_turns", {
|
|
426
|
+
min: 1,
|
|
427
|
+
max: 20
|
|
428
|
+
}) ?? pacing?.number("quietTurns", {
|
|
429
|
+
min: 1,
|
|
430
|
+
max: 20
|
|
431
|
+
}) ?? 3,
|
|
432
|
+
cooldownTurns: pacing?.number("cooldown_turns", {
|
|
433
|
+
min: 1,
|
|
434
|
+
max: 50
|
|
435
|
+
}) ?? pacing?.number("cooldownTurns", {
|
|
436
|
+
min: 1,
|
|
437
|
+
max: 50
|
|
438
|
+
}) ?? 4,
|
|
439
|
+
cooldownDays: pacing?.number("cooldown_days", {
|
|
440
|
+
min: 0,
|
|
441
|
+
max: 365
|
|
442
|
+
}) ?? pacing?.number("cooldownDays", {
|
|
443
|
+
min: 0,
|
|
444
|
+
max: 365
|
|
445
|
+
}) ?? 1
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
});
|
|
449
|
+
const ids = plots.map((plot) => plot.id);
|
|
450
|
+
ids.forEach((id, index) => {
|
|
451
|
+
if (ids.indexOf(id) !== index) issues.push({
|
|
452
|
+
path: `plots[${index}].id`,
|
|
453
|
+
message: `plot id "${id}" is defined twice`
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
for (const [plotIndex, plot] of plots.entries()) {
|
|
457
|
+
const steps = [...plot.hooks, ...plot.developments];
|
|
458
|
+
const stepIds = steps.map((step) => step.id);
|
|
459
|
+
stepIds.forEach((id, index) => {
|
|
460
|
+
if (stepIds.indexOf(id) !== index) issues.push({
|
|
461
|
+
path: `plots[${plotIndex}]`,
|
|
462
|
+
message: `development id "${id}" is defined twice`
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
for (const step of steps) for (const required of step.prerequisites) if (!stepIds.includes(required)) issues.push({
|
|
466
|
+
path: `plots[${plotIndex}]`,
|
|
467
|
+
message: `development "${step.id}" requires unknown development "${required}"`
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
return issues.length === 0 ? {
|
|
471
|
+
ok: true,
|
|
472
|
+
value: plots
|
|
473
|
+
} : {
|
|
474
|
+
ok: false,
|
|
475
|
+
issues
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
async function loadPlots(worldDir) {
|
|
479
|
+
const result = {
|
|
480
|
+
plots: [],
|
|
481
|
+
problems: []
|
|
482
|
+
};
|
|
483
|
+
const file = join(worldDir, PLOTS_FILE_NAME);
|
|
484
|
+
try {
|
|
485
|
+
result.plots = await readDocument(file, parsePlotsFile, result.problems) ?? [];
|
|
486
|
+
} catch (error) {
|
|
487
|
+
if (error.code !== "ENOENT") result.problems.push({
|
|
488
|
+
path: file,
|
|
489
|
+
message: errorMessage(error)
|
|
490
|
+
});
|
|
491
|
+
}
|
|
492
|
+
return result;
|
|
493
|
+
}
|
|
494
|
+
function plotReferenceProblems(plots, entities) {
|
|
495
|
+
const known = new Set(entities);
|
|
496
|
+
return plots.flatMap((plot) => plot.participants.filter((participant) => !known.has(participant)).map((participant) => `plot "${plot.id}" names unknown participant "${participant}"`));
|
|
497
|
+
}
|
|
498
|
+
//#endregion
|
|
499
|
+
//#region src/shared/worlds.ts
|
|
500
|
+
/** Path prefix of the plugin's HTTP API. Relative, so a mounted deployment resolves it against the page. */
|
|
501
|
+
const API_PREFIX = "roleplay-api";
|
|
502
|
+
//#endregion
|
|
503
|
+
//#region src/shared/interface-pack.ts
|
|
504
|
+
/** What a surface may do for the player; a pack lists the ones it uses. */
|
|
505
|
+
const PACK_ACTIONS = [
|
|
506
|
+
"say",
|
|
507
|
+
"fill",
|
|
508
|
+
"choose",
|
|
509
|
+
"open"
|
|
510
|
+
];
|
|
511
|
+
/** The places in the interface that take a surface. */
|
|
512
|
+
const SURFACE_NAMES = [
|
|
513
|
+
"hud",
|
|
514
|
+
"dock",
|
|
515
|
+
"opening",
|
|
516
|
+
"backdrop",
|
|
517
|
+
"tab",
|
|
518
|
+
"sidebar"
|
|
519
|
+
];
|
|
520
|
+
/** The surfaces that sit in a strip of the chat and so have a height of their own, with the heights they may ask for. */
|
|
521
|
+
const STRIP_HEIGHTS = {
|
|
522
|
+
hud: {
|
|
523
|
+
min: 24,
|
|
524
|
+
max: 320,
|
|
525
|
+
fallback: 56
|
|
526
|
+
},
|
|
527
|
+
dock: {
|
|
528
|
+
min: 24,
|
|
529
|
+
max: 320,
|
|
530
|
+
fallback: 48
|
|
531
|
+
},
|
|
532
|
+
opening: {
|
|
533
|
+
min: 120,
|
|
534
|
+
max: 640,
|
|
535
|
+
fallback: 320
|
|
536
|
+
}
|
|
537
|
+
};
|
|
538
|
+
const isStrip = (name) => name in STRIP_HEIGHTS;
|
|
539
|
+
const PACK_LIMITS = {
|
|
540
|
+
files: 200,
|
|
541
|
+
bytes: 5242880,
|
|
542
|
+
fileBytes: 1048576,
|
|
543
|
+
depth: 4,
|
|
544
|
+
storageBytes: 65536,
|
|
545
|
+
history: {
|
|
546
|
+
fallback: 20,
|
|
547
|
+
max: 100
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
/** The file types a pack may carry, by extension. */
|
|
551
|
+
const PACK_MEDIA_TYPES = {
|
|
552
|
+
html: "text/html; charset=utf-8",
|
|
553
|
+
css: "text/css; charset=utf-8",
|
|
554
|
+
js: "text/javascript; charset=utf-8",
|
|
555
|
+
json: "application/json; charset=utf-8",
|
|
556
|
+
txt: "text/plain; charset=utf-8",
|
|
557
|
+
png: "image/png",
|
|
558
|
+
jpg: "image/jpeg",
|
|
559
|
+
jpeg: "image/jpeg",
|
|
560
|
+
webp: "image/webp",
|
|
561
|
+
gif: "image/gif",
|
|
562
|
+
avif: "image/avif",
|
|
563
|
+
svg: "image/svg+xml",
|
|
564
|
+
woff: "font/woff",
|
|
565
|
+
woff2: "font/woff2",
|
|
566
|
+
ttf: "font/ttf",
|
|
567
|
+
otf: "font/otf",
|
|
568
|
+
mp3: "audio/mpeg",
|
|
569
|
+
ogg: "audio/ogg",
|
|
570
|
+
wav: "audio/wav"
|
|
571
|
+
};
|
|
572
|
+
//#endregion
|
|
573
|
+
//#region src/host/interface/skin.ts
|
|
574
|
+
const NESTABLE = /* @__PURE__ */ new Set(["media", "supports"]);
|
|
575
|
+
const FORBIDDEN_TEXT = [
|
|
576
|
+
"expression(",
|
|
577
|
+
"behavior",
|
|
578
|
+
"-moz-binding",
|
|
579
|
+
"javascript:",
|
|
580
|
+
"image-set(",
|
|
581
|
+
"src(",
|
|
582
|
+
"cross-fade(",
|
|
583
|
+
"element(",
|
|
584
|
+
"paint("
|
|
585
|
+
];
|
|
586
|
+
const MAX_DEPTH = 3;
|
|
587
|
+
/** Comments are blanked, newlines kept, so that lines still count. */
|
|
588
|
+
function withoutComments(css) {
|
|
589
|
+
return css.replace(/\/\*[\s\S]*?\*\//g, (match) => match.replace(/[^\n]/g, " "));
|
|
590
|
+
}
|
|
591
|
+
const lineAt = (text, offset) => text.slice(0, offset).split("\n").length;
|
|
592
|
+
/** The text up to the next `{` or `;` outside strings and brackets, with which of them ended it. */
|
|
593
|
+
function scanPrelude(text, from) {
|
|
594
|
+
let quote = "";
|
|
595
|
+
let depth = 0;
|
|
596
|
+
for (let index = from; index < text.length; index += 1) {
|
|
597
|
+
const char = text[index];
|
|
598
|
+
if (quote !== "") {
|
|
599
|
+
if (char === "\\") index += 1;
|
|
600
|
+
else if (char === quote) quote = "";
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
if (char === "\"" || char === "'") quote = char;
|
|
604
|
+
else if (char === "(" || char === "[") depth += 1;
|
|
605
|
+
else if (char === ")" || char === "]") depth = Math.max(0, depth - 1);
|
|
606
|
+
else if (depth === 0 && (char === "{" || char === ";" || char === "}")) return {
|
|
607
|
+
end: index,
|
|
608
|
+
stop: char
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
return {
|
|
612
|
+
end: text.length,
|
|
613
|
+
stop: void 0
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
/** The index of the `}` that closes the block opened just before `from`. */
|
|
617
|
+
function closing(text, from) {
|
|
618
|
+
let quote = "";
|
|
619
|
+
let depth = 1;
|
|
620
|
+
for (let index = from; index < text.length; index += 1) {
|
|
621
|
+
const char = text[index];
|
|
622
|
+
if (quote !== "") {
|
|
623
|
+
if (char === "\\") index += 1;
|
|
624
|
+
else if (char === quote) quote = "";
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
if (char === "\"" || char === "'") quote = char;
|
|
628
|
+
else if (char === "{") depth += 1;
|
|
629
|
+
else if (char === "}") {
|
|
630
|
+
depth -= 1;
|
|
631
|
+
if (depth === 0) return index;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
return -1;
|
|
635
|
+
}
|
|
636
|
+
/** Every compound selector of a selector must name an element of ours through `[data-rp...]` standing on its own, not inside a function. */
|
|
637
|
+
function selectorProblem(selector) {
|
|
638
|
+
const text = selector.trim();
|
|
639
|
+
if (text === "") return "an empty selector";
|
|
640
|
+
const compounds = [];
|
|
641
|
+
let current = "";
|
|
642
|
+
let depth = 0;
|
|
643
|
+
let bracket = 0;
|
|
644
|
+
let quote = "";
|
|
645
|
+
for (const char of text) {
|
|
646
|
+
if (quote !== "") {
|
|
647
|
+
current += char;
|
|
648
|
+
if (char === quote) quote = "";
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
if (char === "\"" || char === "'") {
|
|
652
|
+
quote = char;
|
|
653
|
+
current += char;
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
if (char === "[") bracket += 1;
|
|
657
|
+
if (char === "]") bracket = Math.max(0, bracket - 1);
|
|
658
|
+
if (char === "(") depth += 1;
|
|
659
|
+
if (char === ")") depth = Math.max(0, depth - 1);
|
|
660
|
+
if (depth === 0 && bracket === 0 && /[\s>+~]/.test(char)) {
|
|
661
|
+
if (current !== "") compounds.push(current);
|
|
662
|
+
current = "";
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
current += char;
|
|
666
|
+
}
|
|
667
|
+
if (current !== "") compounds.push(current);
|
|
668
|
+
for (const compound of compounds) {
|
|
669
|
+
if (compound.includes("&")) return `"${selector.trim()}": nesting is not supported`;
|
|
670
|
+
if (!standsOn(compound)) return `"${selector.trim()}": every part of a selector must name one of our elements with [data-rp="..."]`;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
/** Whether `[data-rp` occurs outside parentheses of the compound selector. */
|
|
674
|
+
function standsOn(compound) {
|
|
675
|
+
let depth = 0;
|
|
676
|
+
let quote = "";
|
|
677
|
+
for (let index = 0; index < compound.length; index += 1) {
|
|
678
|
+
const char = compound[index];
|
|
679
|
+
if (quote !== "") {
|
|
680
|
+
if (char === quote) quote = "";
|
|
681
|
+
continue;
|
|
682
|
+
}
|
|
683
|
+
if (char === "\"" || char === "'") quote = char;
|
|
684
|
+
else if (char === "(") depth += 1;
|
|
685
|
+
else if (char === ")") depth = Math.max(0, depth - 1);
|
|
686
|
+
else if (depth === 0 && char === "[" && /^\[\s*data-rp(?![\w-])/i.test(compound.slice(index))) return true;
|
|
687
|
+
}
|
|
688
|
+
return false;
|
|
689
|
+
}
|
|
690
|
+
/** Whether a backslash stands outside the strings of a text: escapes there could spell what is refused. */
|
|
691
|
+
function hasBareEscape(text) {
|
|
692
|
+
let quote = "";
|
|
693
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
694
|
+
const char = text[index];
|
|
695
|
+
if (quote !== "") {
|
|
696
|
+
if (char === "\\") index += 1;
|
|
697
|
+
else if (char === quote) quote = "";
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
if (char === "\"" || char === "'") quote = char;
|
|
701
|
+
else if (char === "\\") return true;
|
|
702
|
+
}
|
|
703
|
+
return false;
|
|
704
|
+
}
|
|
705
|
+
function declarationProblem(body, url) {
|
|
706
|
+
if (body.includes("{")) return {
|
|
707
|
+
css: body,
|
|
708
|
+
problem: "rules inside a rule (nesting) are not supported"
|
|
709
|
+
};
|
|
710
|
+
if (hasBareEscape(body)) return {
|
|
711
|
+
css: body,
|
|
712
|
+
problem: "escapes outside strings are not allowed"
|
|
713
|
+
};
|
|
714
|
+
const lower = body.toLowerCase();
|
|
715
|
+
const forbidden = FORBIDDEN_TEXT.find((text) => lower.includes(text));
|
|
716
|
+
if (forbidden !== void 0) return {
|
|
717
|
+
css: body,
|
|
718
|
+
problem: `"${forbidden}" is not allowed`
|
|
719
|
+
};
|
|
720
|
+
if (lower.includes("@import")) return {
|
|
721
|
+
css: body,
|
|
722
|
+
problem: "@import is not allowed"
|
|
723
|
+
};
|
|
724
|
+
let problem;
|
|
725
|
+
const css = body.replace(/url\(\s*(?:"([^"]*)"|'([^']*)'|([^)\s]*))\s*\)/gi, (match, double, single, bare) => {
|
|
726
|
+
const path = (double ?? single ?? bare ?? "").trim();
|
|
727
|
+
if (path === "" || /^[a-z][a-z0-9+.-]*:/i.test(path) || path.startsWith("/") || path.startsWith("#")) {
|
|
728
|
+
problem ??= `url(${path}) must be a path inside the pack`;
|
|
729
|
+
return match;
|
|
730
|
+
}
|
|
731
|
+
const address = url(path);
|
|
732
|
+
if (address === void 0) {
|
|
733
|
+
problem ??= `url(${path}): the pack has no such file`;
|
|
734
|
+
return match;
|
|
735
|
+
}
|
|
736
|
+
return `url("${address}")`;
|
|
737
|
+
});
|
|
738
|
+
return problem === void 0 ? { css } : {
|
|
739
|
+
css,
|
|
740
|
+
problem
|
|
741
|
+
};
|
|
742
|
+
}
|
|
743
|
+
function block(context, from, to, depth, topLevel) {
|
|
744
|
+
const { text } = context;
|
|
745
|
+
let out = "";
|
|
746
|
+
let index = from;
|
|
747
|
+
while (index < to) {
|
|
748
|
+
while (index < to && /\s/.test(text[index])) index += 1;
|
|
749
|
+
if (index >= to) break;
|
|
750
|
+
const prelude = scanPrelude(text, index);
|
|
751
|
+
const header = text.slice(index, Math.min(prelude.end, to)).trim();
|
|
752
|
+
const line = lineAt(text, index);
|
|
753
|
+
if (prelude.stop !== "{") {
|
|
754
|
+
context.problems.push({
|
|
755
|
+
line,
|
|
756
|
+
message: header.startsWith("@") ? `${header.split(/\s/)[0]} is not allowed` : `"${header.slice(0, 40)}" is not a rule`
|
|
757
|
+
});
|
|
758
|
+
index = prelude.end + 1;
|
|
759
|
+
continue;
|
|
760
|
+
}
|
|
761
|
+
const end = closing(text, prelude.end + 1);
|
|
762
|
+
if (end === -1) {
|
|
763
|
+
context.problems.push({
|
|
764
|
+
line,
|
|
765
|
+
message: "a rule is not closed"
|
|
766
|
+
});
|
|
767
|
+
break;
|
|
768
|
+
}
|
|
769
|
+
const inner = text.slice(prelude.end + 1, end);
|
|
770
|
+
if (header.startsWith("@")) out += atRule(context, header, inner, prelude.end + 1, end, line, depth, topLevel);
|
|
771
|
+
else out += styleRule(context, header, inner, line);
|
|
772
|
+
index = end + 1;
|
|
773
|
+
}
|
|
774
|
+
return out;
|
|
775
|
+
}
|
|
776
|
+
function styleRule(context, header, body, line) {
|
|
777
|
+
const selectors = splitSelectors(header);
|
|
778
|
+
for (const selector of selectors) {
|
|
779
|
+
const problem = selectorProblem(selector);
|
|
780
|
+
if (problem !== void 0) context.problems.push({
|
|
781
|
+
line,
|
|
782
|
+
message: problem
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
const checked = declarationProblem(body, context.url);
|
|
786
|
+
if (checked.problem !== void 0) context.problems.push({
|
|
787
|
+
line,
|
|
788
|
+
message: checked.problem
|
|
789
|
+
});
|
|
790
|
+
return `${header} {${checked.css}}\n`;
|
|
791
|
+
}
|
|
792
|
+
function atRule(context, header, inner, start, end, line, depth, topLevel) {
|
|
793
|
+
const name = /^@([a-z-]+)/i.exec(header)?.[1]?.toLowerCase() ?? "";
|
|
794
|
+
if (NESTABLE.has(name)) {
|
|
795
|
+
if (depth >= MAX_DEPTH) {
|
|
796
|
+
context.problems.push({
|
|
797
|
+
line,
|
|
798
|
+
message: `@${name} is nested too deep`
|
|
799
|
+
});
|
|
800
|
+
return "";
|
|
801
|
+
}
|
|
802
|
+
return `${header} {\n${block(context, start, end, depth + 1, false)}}\n`;
|
|
803
|
+
}
|
|
804
|
+
if (name === "keyframes") return `${header} {${inner.replace(/\{([^{}]*)\}/g, (_match, body) => {
|
|
805
|
+
const checked = declarationProblem(body, context.url);
|
|
806
|
+
if (checked.problem !== void 0) context.problems.push({
|
|
807
|
+
line,
|
|
808
|
+
message: `@keyframes: ${checked.problem}`
|
|
809
|
+
});
|
|
810
|
+
return `{${checked.css}}`;
|
|
811
|
+
})}}\n`;
|
|
812
|
+
if (name === "font-face") {
|
|
813
|
+
if (!topLevel) context.problems.push({
|
|
814
|
+
line,
|
|
815
|
+
message: "@font-face must stand at the top"
|
|
816
|
+
});
|
|
817
|
+
const checked = declarationProblem(inner, context.url);
|
|
818
|
+
if (checked.problem !== void 0) context.problems.push({
|
|
819
|
+
line,
|
|
820
|
+
message: `@font-face: ${checked.problem}`
|
|
821
|
+
});
|
|
822
|
+
return `${header} {${checked.css}}\n`;
|
|
823
|
+
}
|
|
824
|
+
context.problems.push({
|
|
825
|
+
line,
|
|
826
|
+
message: `@${name} is not allowed (allowed: @media, @supports, @keyframes, @font-face)`
|
|
827
|
+
});
|
|
828
|
+
return "";
|
|
829
|
+
}
|
|
830
|
+
/** Splits a selector list at the commas that are not inside brackets, parentheses or strings. */
|
|
831
|
+
function splitSelectors(header) {
|
|
832
|
+
const parts = [];
|
|
833
|
+
let current = "";
|
|
834
|
+
let depth = 0;
|
|
835
|
+
let quote = "";
|
|
836
|
+
for (const char of header) {
|
|
837
|
+
if (quote !== "") {
|
|
838
|
+
current += char;
|
|
839
|
+
if (char === quote) quote = "";
|
|
840
|
+
continue;
|
|
841
|
+
}
|
|
842
|
+
if (char === "\"" || char === "'") quote = char;
|
|
843
|
+
if (char === "(" || char === "[") depth += 1;
|
|
844
|
+
if (char === ")" || char === "]") depth = Math.max(0, depth - 1);
|
|
845
|
+
if (char === "," && depth === 0) {
|
|
846
|
+
parts.push(current);
|
|
847
|
+
current = "";
|
|
848
|
+
continue;
|
|
849
|
+
}
|
|
850
|
+
current += char;
|
|
851
|
+
}
|
|
852
|
+
parts.push(current);
|
|
853
|
+
return parts;
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* Checks a skin and puts it in the form that goes into the page: rules for our own elements only
|
|
857
|
+
* (`[data-rp]`), no imports, no scripts or escapes in disguise, and urls only to files of the pack.
|
|
858
|
+
* The returned css is empty when there are problems.
|
|
859
|
+
*/
|
|
860
|
+
function checkSkin(css, url) {
|
|
861
|
+
const text = withoutComments(css.replace(/^/, ""));
|
|
862
|
+
const context = {
|
|
863
|
+
text,
|
|
864
|
+
url,
|
|
865
|
+
problems: []
|
|
866
|
+
};
|
|
867
|
+
const out = block(context, 0, text.length, 0, true);
|
|
868
|
+
return context.problems.length === 0 ? {
|
|
869
|
+
css: out,
|
|
870
|
+
problems: []
|
|
871
|
+
} : {
|
|
872
|
+
css: "",
|
|
873
|
+
problems: context.problems
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
//#endregion
|
|
877
|
+
//#region src/host/interface/pack.ts
|
|
878
|
+
const ID = /^[\p{L}\p{N}][\p{L}\p{N}_-]{0,59}$/u;
|
|
879
|
+
const SEGMENT = /^[\p{L}\p{N}_.-]+$/u;
|
|
880
|
+
const TOKEN_NAME = /^--dsw-alias-[a-z0-9-]+$/;
|
|
881
|
+
const COLOUR_REFUSED = /url\(|var\(|expression|javascript:|[;{}]|@import/i;
|
|
882
|
+
const isPackId = (id) => ID.test(id);
|
|
883
|
+
/** Every file of a pack folder, relative with `/`, with what is wrong with the folder itself. */
|
|
884
|
+
function inventory(directory, problems) {
|
|
885
|
+
const files = /* @__PURE__ */ new Map();
|
|
886
|
+
let bytes = 0;
|
|
887
|
+
const walk = (folder, prefix, depth) => {
|
|
888
|
+
for (const entry of readdirSync(folder, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
889
|
+
const relative = prefix === "" ? entry.name : `${prefix}/${entry.name}`;
|
|
890
|
+
const path = join(folder, entry.name);
|
|
891
|
+
if (!SEGMENT.test(entry.name)) {
|
|
892
|
+
problems.push({
|
|
893
|
+
file: relative,
|
|
894
|
+
message: "the name may hold only letters, digits, \"-\", \"_\" and \".\""
|
|
895
|
+
});
|
|
896
|
+
continue;
|
|
897
|
+
}
|
|
898
|
+
if (entry.name === "." || entry.name === ".." || entry.name.startsWith(".")) {
|
|
899
|
+
problems.push({
|
|
900
|
+
file: relative,
|
|
901
|
+
message: "names may not start with a dot"
|
|
902
|
+
});
|
|
903
|
+
continue;
|
|
904
|
+
}
|
|
905
|
+
const status = lstatSync(path);
|
|
906
|
+
if (status.isSymbolicLink()) {
|
|
907
|
+
problems.push({
|
|
908
|
+
file: relative,
|
|
909
|
+
message: "symbolic links are not allowed"
|
|
910
|
+
});
|
|
911
|
+
continue;
|
|
912
|
+
}
|
|
913
|
+
if (status.isDirectory()) {
|
|
914
|
+
if (depth >= PACK_LIMITS.depth) problems.push({
|
|
915
|
+
file: relative,
|
|
916
|
+
message: `folders go at most ${PACK_LIMITS.depth} deep`
|
|
917
|
+
});
|
|
918
|
+
else walk(path, relative, depth + 1);
|
|
919
|
+
continue;
|
|
920
|
+
}
|
|
921
|
+
if (!status.isFile()) {
|
|
922
|
+
problems.push({
|
|
923
|
+
file: relative,
|
|
924
|
+
message: "only files and folders are allowed"
|
|
925
|
+
});
|
|
926
|
+
continue;
|
|
927
|
+
}
|
|
928
|
+
const mediaType = PACK_MEDIA_TYPES[extname(entry.name).slice(1).toLowerCase()];
|
|
929
|
+
if (mediaType === void 0) {
|
|
930
|
+
problems.push({
|
|
931
|
+
file: relative,
|
|
932
|
+
message: `this kind of file is not allowed (allowed: ${Object.keys(PACK_MEDIA_TYPES).join(", ")})`
|
|
933
|
+
});
|
|
934
|
+
continue;
|
|
935
|
+
}
|
|
936
|
+
if (status.size > PACK_LIMITS.fileBytes) {
|
|
937
|
+
problems.push({
|
|
938
|
+
file: relative,
|
|
939
|
+
message: `the file is larger than ${PACK_LIMITS.fileBytes / 1024} KB`
|
|
940
|
+
});
|
|
941
|
+
continue;
|
|
942
|
+
}
|
|
943
|
+
bytes += status.size;
|
|
944
|
+
files.set(relative, {
|
|
945
|
+
size: status.size,
|
|
946
|
+
mtimeMs: Math.floor(status.mtimeMs),
|
|
947
|
+
mediaType
|
|
948
|
+
});
|
|
949
|
+
}
|
|
950
|
+
};
|
|
951
|
+
walk(directory, "", 1);
|
|
952
|
+
if (files.size > PACK_LIMITS.files) problems.push({
|
|
953
|
+
file: "",
|
|
954
|
+
message: `a pack holds at most ${PACK_LIMITS.files} files`
|
|
955
|
+
});
|
|
956
|
+
if (bytes > PACK_LIMITS.bytes) problems.push({
|
|
957
|
+
file: "",
|
|
958
|
+
message: `a pack holds at most ${PACK_LIMITS.bytes / 1024 / 1024} MB`
|
|
959
|
+
});
|
|
960
|
+
return files;
|
|
961
|
+
}
|
|
962
|
+
/** A path as a pack names it: relative, with `/`, inside the folder. */
|
|
963
|
+
function packPath(path) {
|
|
964
|
+
if (path === "" || path.includes("\0") || path.includes("\\") || path.startsWith("/")) return void 0;
|
|
965
|
+
const normal = posix.normalize(path);
|
|
966
|
+
if (normal === "." || normal.startsWith("../") || normal === "..") return void 0;
|
|
967
|
+
return normal;
|
|
968
|
+
}
|
|
969
|
+
const issuesToProblems = (issues) => issues.map((issue) => ({
|
|
970
|
+
file: "pack.json",
|
|
971
|
+
message: issue.path === "" ? issue.message : `${issue.path}: ${issue.message}`
|
|
972
|
+
}));
|
|
973
|
+
/** The tokens of `theme`, each with both palettes. */
|
|
974
|
+
function themeOf(value, problems) {
|
|
975
|
+
const theme = {};
|
|
976
|
+
if (value === void 0 || value === null) return theme;
|
|
977
|
+
if (typeof value !== "object" || Array.isArray(value)) {
|
|
978
|
+
problems.push({
|
|
979
|
+
file: "pack.json",
|
|
980
|
+
message: "theme: must be an object of tokens"
|
|
981
|
+
});
|
|
982
|
+
return theme;
|
|
983
|
+
}
|
|
984
|
+
for (const [name, token] of Object.entries(value)) {
|
|
985
|
+
const place = `theme.${name}`;
|
|
986
|
+
if (!TOKEN_NAME.test(name)) {
|
|
987
|
+
problems.push({
|
|
988
|
+
file: "pack.json",
|
|
989
|
+
message: `${place}: only tokens named --dsw-alias-... can be set`
|
|
990
|
+
});
|
|
991
|
+
continue;
|
|
992
|
+
}
|
|
993
|
+
const pair = token;
|
|
994
|
+
if (typeof pair !== "object" || pair === null || typeof pair.light !== "string" || typeof pair.dark !== "string") {
|
|
995
|
+
problems.push({
|
|
996
|
+
file: "pack.json",
|
|
997
|
+
message: `${place}: must be { "light": "...", "dark": "..." }`
|
|
998
|
+
});
|
|
999
|
+
continue;
|
|
1000
|
+
}
|
|
1001
|
+
if ([pair.light, pair.dark].some((colour) => colour.trim() === "" || colour.length > 120 || COLOUR_REFUSED.test(colour))) {
|
|
1002
|
+
problems.push({
|
|
1003
|
+
file: "pack.json",
|
|
1004
|
+
message: `${place}: values must be plain CSS colours`
|
|
1005
|
+
});
|
|
1006
|
+
continue;
|
|
1007
|
+
}
|
|
1008
|
+
theme[name] = {
|
|
1009
|
+
light: pair.light.trim(),
|
|
1010
|
+
dark: pair.dark.trim()
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
return theme;
|
|
1014
|
+
}
|
|
1015
|
+
function surfacesOf(reader, written, files, problems) {
|
|
1016
|
+
const surfaces = {};
|
|
1017
|
+
if (reader === void 0) return surfaces;
|
|
1018
|
+
for (const key of Object.keys(written)) if (!SURFACE_NAMES.some((name) => name === key)) problems.push({
|
|
1019
|
+
file: "pack.json",
|
|
1020
|
+
message: `surfaces.${key}: there is no such place (${SURFACE_NAMES.join(", ")})`
|
|
1021
|
+
});
|
|
1022
|
+
for (const name of SURFACE_NAMES) {
|
|
1023
|
+
const entry = reader.object(name);
|
|
1024
|
+
if (entry === void 0) continue;
|
|
1025
|
+
const file = entry.string("file", { required: true });
|
|
1026
|
+
if (file === void 0) continue;
|
|
1027
|
+
const path = packPath(file);
|
|
1028
|
+
if (path === void 0 || !path.endsWith(".html")) {
|
|
1029
|
+
problems.push({
|
|
1030
|
+
file: "pack.json",
|
|
1031
|
+
message: `surfaces.${name}.file: must be a .html file inside the pack`
|
|
1032
|
+
});
|
|
1033
|
+
continue;
|
|
1034
|
+
}
|
|
1035
|
+
if (!files.has(path)) {
|
|
1036
|
+
problems.push({
|
|
1037
|
+
file: "pack.json",
|
|
1038
|
+
message: `surfaces.${name}.file: the pack has no file "${path}"`
|
|
1039
|
+
});
|
|
1040
|
+
continue;
|
|
1041
|
+
}
|
|
1042
|
+
const range = isStrip(name) ? STRIP_HEIGHTS[name] : void 0;
|
|
1043
|
+
const height = range === void 0 ? void 0 : entry.number("height", {
|
|
1044
|
+
min: range.min,
|
|
1045
|
+
max: range.max
|
|
1046
|
+
}) ?? range.fallback;
|
|
1047
|
+
surfaces[name] = {
|
|
1048
|
+
file: path,
|
|
1049
|
+
title: entry.text("title"),
|
|
1050
|
+
...height === void 0 ? {} : { height }
|
|
1051
|
+
};
|
|
1052
|
+
}
|
|
1053
|
+
return surfaces;
|
|
1054
|
+
}
|
|
1055
|
+
/** What changes when the pack does. */
|
|
1056
|
+
function revisionOf(files) {
|
|
1057
|
+
const hash = createHash("sha256");
|
|
1058
|
+
for (const [path, file] of [...files].sort(([a], [b]) => a.localeCompare(b))) hash.update(`${path}\0${file.size}\0${file.mtimeMs}\n`);
|
|
1059
|
+
return hash.digest("hex").slice(0, 16);
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Reads a pack folder and checks all of it: the manifest, the theme, the skin, the surfaces and every file.
|
|
1063
|
+
* @returns the pack when nothing is wrong, and every problem found otherwise.
|
|
1064
|
+
*/
|
|
1065
|
+
function readPack(directory, id, address) {
|
|
1066
|
+
const problems = [];
|
|
1067
|
+
if (!ID.test(id)) return { problems: [{
|
|
1068
|
+
file: "",
|
|
1069
|
+
message: `"${id}" is not a usable pack name (letters, digits, "-" and "_")`
|
|
1070
|
+
}] };
|
|
1071
|
+
let files;
|
|
1072
|
+
try {
|
|
1073
|
+
files = inventory(directory, problems);
|
|
1074
|
+
} catch (error) {
|
|
1075
|
+
return { problems: [{
|
|
1076
|
+
file: "",
|
|
1077
|
+
message: `the folder cannot be read: ${error instanceof Error ? error.message : String(error)}`
|
|
1078
|
+
}] };
|
|
1079
|
+
}
|
|
1080
|
+
if (files.get("pack.json") === void 0) return { problems: [...problems, {
|
|
1081
|
+
file: "pack.json",
|
|
1082
|
+
message: "the pack has no pack.json"
|
|
1083
|
+
}] };
|
|
1084
|
+
const issues = [];
|
|
1085
|
+
const reader = Reader.fromJson(readFileSync(join(directory, "pack.json"), "utf8"), issues);
|
|
1086
|
+
if (reader === void 0) return { problems: [...problems, ...issuesToProblems(issues)] };
|
|
1087
|
+
const api = reader.number("api", { min: 1 }) ?? 1;
|
|
1088
|
+
if (api !== 1) problems.push({
|
|
1089
|
+
file: "pack.json",
|
|
1090
|
+
message: `api: this version knows only api 1, the pack asks for ${api}`
|
|
1091
|
+
});
|
|
1092
|
+
const name = reader.string("name") ?? id;
|
|
1093
|
+
const actions = [];
|
|
1094
|
+
for (const action of reader.strings("actions")) {
|
|
1095
|
+
const known = PACK_ACTIONS.find((candidate) => candidate === action);
|
|
1096
|
+
if (known === void 0) problems.push({
|
|
1097
|
+
file: "pack.json",
|
|
1098
|
+
message: `actions: "${action}" is not an action (${PACK_ACTIONS.join(", ")})`
|
|
1099
|
+
});
|
|
1100
|
+
else actions.push(known);
|
|
1101
|
+
}
|
|
1102
|
+
const theme = themeOf(reader.raw("theme"), problems);
|
|
1103
|
+
const surfaces = surfacesOf(reader.object("surfaces"), reader.raw("surfaces"), files, problems);
|
|
1104
|
+
const skinName = reader.string("skin");
|
|
1105
|
+
let skin = "";
|
|
1106
|
+
let skinPath;
|
|
1107
|
+
if (skinName !== void 0) {
|
|
1108
|
+
skinPath = packPath(skinName);
|
|
1109
|
+
if (skinPath === void 0 || !skinPath.endsWith(".css")) problems.push({
|
|
1110
|
+
file: "pack.json",
|
|
1111
|
+
message: "skin: must be a .css file inside the pack"
|
|
1112
|
+
});
|
|
1113
|
+
else if (!files.has(skinPath)) problems.push({
|
|
1114
|
+
file: "pack.json",
|
|
1115
|
+
message: `skin: the pack has no file "${skinPath}"`
|
|
1116
|
+
});
|
|
1117
|
+
else {
|
|
1118
|
+
const checked = checkSkin(readFileSync(join(directory, ...skinPath.split("/")), "utf8"), (path) => {
|
|
1119
|
+
const resolved = packPath(posix.join(posix.dirname(skinPath), path));
|
|
1120
|
+
return resolved !== void 0 && files.has(resolved) ? address(resolved) : void 0;
|
|
1121
|
+
});
|
|
1122
|
+
for (const problem of checked.problems) problems.push({
|
|
1123
|
+
file: skinPath,
|
|
1124
|
+
message: `line ${problem.line}: ${problem.message}`
|
|
1125
|
+
});
|
|
1126
|
+
skin = checked.css;
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
problems.push(...issuesToProblems(issues));
|
|
1130
|
+
if (problems.length > 0) return { problems };
|
|
1131
|
+
return {
|
|
1132
|
+
pack: {
|
|
1133
|
+
id,
|
|
1134
|
+
directory,
|
|
1135
|
+
manifest: {
|
|
1136
|
+
id,
|
|
1137
|
+
name,
|
|
1138
|
+
version: reader.text("version"),
|
|
1139
|
+
description: reader.text("description"),
|
|
1140
|
+
api,
|
|
1141
|
+
theme,
|
|
1142
|
+
...skinPath === void 0 ? {} : { skin: skinPath },
|
|
1143
|
+
actions,
|
|
1144
|
+
surfaces
|
|
1145
|
+
},
|
|
1146
|
+
files,
|
|
1147
|
+
revision: revisionOf(files),
|
|
1148
|
+
skin
|
|
1149
|
+
},
|
|
1150
|
+
problems
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
/** What the client is told of a pack. */
|
|
1154
|
+
function describePack(pack, frame) {
|
|
1155
|
+
const { manifest } = pack;
|
|
1156
|
+
return {
|
|
1157
|
+
id: pack.id,
|
|
1158
|
+
name: manifest.name,
|
|
1159
|
+
version: manifest.version,
|
|
1160
|
+
description: manifest.description,
|
|
1161
|
+
revision: pack.revision,
|
|
1162
|
+
frame,
|
|
1163
|
+
theme: manifest.theme,
|
|
1164
|
+
skin: pack.skin,
|
|
1165
|
+
actions: manifest.actions,
|
|
1166
|
+
surfaces: manifest.surfaces
|
|
1167
|
+
};
|
|
1168
|
+
}
|
|
1169
|
+
/** A file of a pack by the path asked for, when the pack has it; the path cannot lead out of the folder. */
|
|
1170
|
+
function packFile(pack, requested) {
|
|
1171
|
+
const path = packPath(requested);
|
|
1172
|
+
const entry = path === void 0 ? void 0 : pack.files.get(path);
|
|
1173
|
+
if (path === void 0 || entry === void 0) return void 0;
|
|
1174
|
+
return {
|
|
1175
|
+
file: join(pack.directory, ...path.split("/").join(sep).split(sep)),
|
|
1176
|
+
size: entry.size,
|
|
1177
|
+
mediaType: entry.mediaType
|
|
1178
|
+
};
|
|
1179
|
+
}
|
|
1180
|
+
const packsIn = (base) => join(base, "ui");
|
|
1181
|
+
/** The ids of the packs a folder (a world, a session or the root) carries, in order. */
|
|
1182
|
+
function packIds(base) {
|
|
1183
|
+
const folder = packsIn(base);
|
|
1184
|
+
if (!existsSync(folder)) return [];
|
|
1185
|
+
return readdirSync(folder, { withFileTypes: true }).filter((entry) => entry.isDirectory() && isPackId(entry.name)).map((entry) => entry.name).sort();
|
|
1186
|
+
}
|
|
1187
|
+
/** Checks every pack a world carries. @returns the ids of the packs, and the problems found, each with the file. */
|
|
1188
|
+
function checkWorldPacks(worldDirectory) {
|
|
1189
|
+
const source = packsIn(worldDirectory);
|
|
1190
|
+
const problems = [];
|
|
1191
|
+
const ids = [];
|
|
1192
|
+
if (!existsSync(source)) return {
|
|
1193
|
+
ids,
|
|
1194
|
+
problems
|
|
1195
|
+
};
|
|
1196
|
+
for (const entry of readdirSync(source, { withFileTypes: true })) {
|
|
1197
|
+
const path = join(source, entry.name);
|
|
1198
|
+
if (!entry.isDirectory() || !isPackId(entry.name)) {
|
|
1199
|
+
problems.push({
|
|
1200
|
+
path,
|
|
1201
|
+
message: "a pack is a folder named with letters, digits, \"-\" and \"_\""
|
|
1202
|
+
});
|
|
1203
|
+
continue;
|
|
1204
|
+
}
|
|
1205
|
+
const checked = readPack(path, entry.name, (file) => file);
|
|
1206
|
+
for (const problem of checked.problems) problems.push({
|
|
1207
|
+
path: problem.file === "" ? path : join(path, problem.file),
|
|
1208
|
+
message: problem.message
|
|
1209
|
+
});
|
|
1210
|
+
ids.push(entry.name);
|
|
1211
|
+
}
|
|
1212
|
+
return {
|
|
1213
|
+
ids,
|
|
1214
|
+
problems
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Copies the packs a world carries into a session folder after checking each of them.
|
|
1219
|
+
* @returns the problems found, each with the file; nothing is copied when there are any.
|
|
1220
|
+
*/
|
|
1221
|
+
function copyWorldPacks(worldDirectory, target) {
|
|
1222
|
+
const { ids, problems } = checkWorldPacks(worldDirectory);
|
|
1223
|
+
if (problems.length > 0) return problems;
|
|
1224
|
+
for (const id of ids) copyTree(join(packsIn(worldDirectory), id), join(packsIn(target), id));
|
|
1225
|
+
return [];
|
|
1226
|
+
}
|
|
1227
|
+
/** A hash of what a pack folder holds: the names and the contents of its files, not their dates. */
|
|
1228
|
+
function packFingerprint(directory) {
|
|
1229
|
+
const hash = createHash("sha256");
|
|
1230
|
+
const walk = (folder, prefix) => {
|
|
1231
|
+
for (const entry of readdirSync(folder, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
1232
|
+
const path = join(folder, entry.name);
|
|
1233
|
+
if (lstatSync(path).isSymbolicLink()) continue;
|
|
1234
|
+
const name = prefix === "" ? entry.name : `${prefix}/${entry.name}`;
|
|
1235
|
+
if (entry.isDirectory()) walk(path, name);
|
|
1236
|
+
else if (entry.isFile()) hash.update(`${name}\0`).update(readFileSync(path)).update("\0");
|
|
1237
|
+
}
|
|
1238
|
+
};
|
|
1239
|
+
walk(directory, "");
|
|
1240
|
+
return hash.digest("hex");
|
|
1241
|
+
}
|
|
1242
|
+
/** How each pack of the world compares with the story's copy. */
|
|
1243
|
+
function worldPackStates(worldDirectory, sessionFolder) {
|
|
1244
|
+
return packIds(worldDirectory).map((id) => {
|
|
1245
|
+
const copy = join(packsIn(sessionFolder), id);
|
|
1246
|
+
return {
|
|
1247
|
+
id,
|
|
1248
|
+
state: !existsSync(copy) ? "new" : packFingerprint(copy) === packFingerprint(join(packsIn(worldDirectory), id)) ? "same" : "different"
|
|
1249
|
+
};
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
/**
|
|
1253
|
+
* Brings the packs of a world into a story's folder: new ones are added and a different copy is replaced, the old one
|
|
1254
|
+
* moved to the story's `.trash`. Every pack of the world is checked first and nothing is changed when one has problems.
|
|
1255
|
+
*/
|
|
1256
|
+
function syncWorldPacks(worldDirectory, sessionFolder, now = Date.now) {
|
|
1257
|
+
const { ids, problems } = checkWorldPacks(worldDirectory);
|
|
1258
|
+
const result = {
|
|
1259
|
+
problems,
|
|
1260
|
+
updated: [],
|
|
1261
|
+
added: [],
|
|
1262
|
+
unchanged: []
|
|
1263
|
+
};
|
|
1264
|
+
if (problems.length > 0) return result;
|
|
1265
|
+
for (const { id, state } of worldPackStates(worldDirectory, sessionFolder)) {
|
|
1266
|
+
if (!ids.includes(id) || state === "same") {
|
|
1267
|
+
result.unchanged.push(id);
|
|
1268
|
+
continue;
|
|
1269
|
+
}
|
|
1270
|
+
const copy = join(packsIn(sessionFolder), id);
|
|
1271
|
+
if (state === "different") {
|
|
1272
|
+
const aside = join(sessionFolder, ".trash", "ui", `${id}.${now()}`);
|
|
1273
|
+
mkdirSync(dirname(aside), { recursive: true });
|
|
1274
|
+
renameSync(copy, aside);
|
|
1275
|
+
result.updated.push(id);
|
|
1276
|
+
} else result.added.push(id);
|
|
1277
|
+
copyTree(join(packsIn(worldDirectory), id), copy);
|
|
1278
|
+
}
|
|
1279
|
+
return result;
|
|
1280
|
+
}
|
|
1281
|
+
function copyTree(from, to) {
|
|
1282
|
+
mkdirSync(to, { recursive: true });
|
|
1283
|
+
for (const entry of readdirSync(from, { withFileTypes: true })) {
|
|
1284
|
+
const source = join(from, entry.name);
|
|
1285
|
+
if (entry.isDirectory()) copyTree(source, join(to, entry.name));
|
|
1286
|
+
else if (entry.isFile()) {
|
|
1287
|
+
mkdirSync(dirname(join(to, entry.name)), { recursive: true });
|
|
1288
|
+
copyFileSync(source, join(to, entry.name));
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
/** A branch plays with the packs of the session it was cut from. */
|
|
1293
|
+
function inheritPacks(from, to) {
|
|
1294
|
+
if (existsSync(packsIn(from))) cpSync(packsIn(from), packsIn(to), { recursive: true });
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* The pack a session uses. `choice` is the setting: empty is the first pack the session carries,
|
|
1298
|
+
* `none` is no pack, an id is that pack of the session, else of the root. Packs of the root are used only when named.
|
|
1299
|
+
*/
|
|
1300
|
+
function activePack(sessionFolder, rootFolder, choice, address) {
|
|
1301
|
+
const wanted = choice.trim();
|
|
1302
|
+
if (wanted === "none") return { problems: [] };
|
|
1303
|
+
const id = wanted === "" ? packIds(sessionFolder)[0] : wanted;
|
|
1304
|
+
if (id === void 0) return { problems: [] };
|
|
1305
|
+
const from = packIds(sessionFolder).includes(id) ? "session" : packIds(rootFolder).includes(id) ? "root" : void 0;
|
|
1306
|
+
if (from === void 0) return { problems: [{
|
|
1307
|
+
file: "",
|
|
1308
|
+
message: `there is no pack "${id}" in this story or in the root`
|
|
1309
|
+
}] };
|
|
1310
|
+
const loaded = readPack(join(packsIn(from === "session" ? sessionFolder : rootFolder), id), id, address(id));
|
|
1311
|
+
return loaded.pack === void 0 ? {
|
|
1312
|
+
problems: loaded.problems,
|
|
1313
|
+
from
|
|
1314
|
+
} : {
|
|
1315
|
+
pack: loaded.pack,
|
|
1316
|
+
problems: [],
|
|
1317
|
+
from
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
/** Whether any pack with a tab surface stands in the folders given; only the manifests are looked at. */
|
|
1321
|
+
function anyTab(bases) {
|
|
1322
|
+
return bases.some((base) => packIds(base).some((id) => {
|
|
1323
|
+
try {
|
|
1324
|
+
const manifest = JSON.parse(readFileSync(join(packsIn(base), id, "pack.json"), "utf8"));
|
|
1325
|
+
return typeof manifest?.surfaces === "object" && manifest.surfaces !== null && manifest.surfaces.tab !== void 0;
|
|
1326
|
+
} catch {
|
|
1327
|
+
return false;
|
|
1328
|
+
}
|
|
1329
|
+
}));
|
|
1330
|
+
}
|
|
1331
|
+
//#endregion
|
|
1332
|
+
export { languageOf as A, SessionNotFoundError as B, errorMessage as C, PromptLibrary as D, PROMPTS_FOLDER as E, shippedText as F, INTENT_HORIZONS as H, AgentUnavailableError as I, InvalidStartError as L, promptProblem as M, shippedLanguages as N, copyWorldPrompts as O, shippedPromptsDirectory as P, InvalidWorldError as R, readDocument as S, formatIssue as T, INTENT_STATUSES as U, WorldNotFoundError as V, INTENT_TYPES as W, API_PREFIX as _, packIds as a, parsePlotsFile as b, describePack as c, packPath as d, readPack as f, SURFACE_NAMES as g, PACK_MEDIA_TYPES as h, inheritPacks as i, listFiles as j, inheritPrompts as k, isPackId as l, PACK_LIMITS as m, anyTab as n, syncWorldPacks as o, PACK_ACTIONS as p, copyWorldPacks as r, worldPackStates as s, activePack as t, packFile as u, PLOTS_FILE_NAME as v, Reader as w, plotReferenceProblems as x, loadPlots as y, SessionArchivedError as z };
|
|
1333
|
+
|
|
1334
|
+
//# sourceMappingURL=packs-ByP1J-zj.js.map
|