@dsh-agentic-roleplay/core 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/authoring.js +13 -2
- package/lib/child-lPncV1ps.js +380 -0
- package/lib/client.js +1526 -356
- package/lib/client.js.map +1 -1
- package/lib/compaction.js +1 -1
- package/lib/deliver-De62a8i9.js +1929 -0
- package/lib/doctor-chat-ByDfCMeo.js +17 -0
- package/lib/doctor.js +95 -0
- package/lib/{documents-DtlY2IRt.js → documents-Do4lz4SB.js} +549 -64
- package/lib/{gallery-DACG5eh2.js → gallery-CtFDL22W.js} +2 -2
- package/lib/{generator-DGYcLMp7.js → generator-B6FxlKOV.js} +2 -378
- package/lib/hooks-Ka22sFNy.js +1019 -0
- package/lib/images.js +3 -3
- package/lib/index.js +303 -190
- package/lib/language-DWVwi1R7.js +520 -0
- package/lib/narration-DK2kiIjE.js +141 -0
- package/lib/orchestration.js +777 -2542
- package/lib/{packs-CkRZDCsV.js → packs-BYjrAen6.js} +4 -518
- package/lib/{presenter-agent-PK9JYExk.js → presenter-agent-Dt21K96b.js} +4 -3
- package/lib/{deliver-BDW5WZvc.js → reflection-md7BCYCl.js} +350 -769
- package/lib/{narration-5ig7Dg28.js → settings-CA6icLL_.js} +156 -141
- package/package.json +3 -2
- package/prompts/en/doctor/persona.md +1 -0
- package/prompts/en/doctor/procedure.md +5 -0
- package/prompts/en/doctor/welcome.md +1 -0
- package/prompts/en/import/character.md +1 -0
- package/prompts/en/roleplay/dice.md +7 -0
- package/prompts/en/roleplay/game-master.md +2 -0
- package/prompts/en/roleplay/reflection-check.md +5 -0
- package/prompts/en/roleplay/reflection.md +5 -1
- package/prompts/zh/doctor/persona.md +1 -0
- package/prompts/zh/doctor/procedure.md +5 -0
- package/prompts/zh/doctor/welcome.md +1 -0
- package/prompts/zh/import/character.md +1 -0
- package/prompts/zh/roleplay/dice.md +7 -0
- package/prompts/zh/roleplay/game-master.md +2 -0
- package/prompts/zh/roleplay/reflection-check.md +5 -0
- package/prompts/zh/roleplay/reflection.md +5 -1
- package/lib/src-CWTNeL6Y.js +0 -128
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import "./
|
|
1
|
+
import { f as Reader } from "./language-DWVwi1R7.js";
|
|
2
2
|
import { dirname, extname, join, posix, sep } from "node:path";
|
|
3
|
-
import { copyFileSync, cpSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, renameSync
|
|
3
|
+
import { copyFileSync, cpSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, renameSync } from "node:fs";
|
|
4
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
5
|
//#region ../dsh-roleplay-model/src/intent.ts
|
|
9
6
|
const INTENT_TYPES = [
|
|
10
7
|
"need",
|
|
@@ -31,517 +28,6 @@ const INTENT_HORIZONS = [
|
|
|
31
28
|
"open_ended"
|
|
32
29
|
];
|
|
33
30
|
//#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
|
-
/** A reflection cannot be started now: a turn is being played, or one is already running. */
|
|
70
|
-
var ReflectionRefusedError = class extends Error {};
|
|
71
|
-
//#endregion
|
|
72
|
-
//#region src/host/prompts/library.ts
|
|
73
|
-
const PROMPTS_FOLDER = "prompts";
|
|
74
|
-
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}$/;
|
|
75
|
-
const ID$1 = /^[a-z0-9-]+(\/[a-z0-9-]+)*$/;
|
|
76
|
-
const LANGUAGE = /^[a-z]{2,8}$/;
|
|
77
|
-
const VARIABLE = /\$\{([A-Za-z]+)\}/g;
|
|
78
|
-
/** The folder of the prompts that ship with the package, found from this module so that it works from source and from the build. */
|
|
79
|
-
function shippedPromptsDirectory(from = dirname(fileURLToPath(import.meta.url))) {
|
|
80
|
-
for (let directory = from;; directory = dirname(directory)) {
|
|
81
|
-
const candidate = join(directory, PROMPTS_FOLDER);
|
|
82
|
-
if (existsSync(join(directory, "package.json")) && existsSync(candidate)) return candidate;
|
|
83
|
-
if (dirname(directory) === directory) throw new Error("the prompts shipped with the package were not found");
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
/** `zh-CN` and `ZH` are both `zh`; anything that is not a language tag is refused. */
|
|
87
|
-
function languageOf(tag) {
|
|
88
|
-
const primary = (tag ?? "en").trim().toLowerCase().split(/[-_]/)[0] ?? "";
|
|
89
|
-
if (!LANGUAGE.test(primary)) throw new Error(`"${tag}" is not a language`);
|
|
90
|
-
return primary;
|
|
91
|
-
}
|
|
92
|
-
/** How a language is named inside a prompt, for the instruction to write in it. */
|
|
93
|
-
const LANGUAGE_NAMES$1 = {
|
|
94
|
-
en: "English",
|
|
95
|
-
zh: "中文"
|
|
96
|
-
};
|
|
97
|
-
function fill(id, text, given, tag) {
|
|
98
|
-
const variables = {
|
|
99
|
-
language: LANGUAGE_NAMES$1[tag] ?? tag,
|
|
100
|
-
...given
|
|
101
|
-
};
|
|
102
|
-
return text.trim().replace(/^\$\{([A-Za-z]+)\}[ \t]*(\r?\n|$)/gm, (match, name) => variables[name] === "" ? "" : match).replace(VARIABLE, (_match, name) => {
|
|
103
|
-
const value = variables[name];
|
|
104
|
-
if (value === void 0) throw new Error(`prompt ${id} uses \${${name}}, which is not given a value (given: ${Object.keys(variables).join(", ") || "none"})`);
|
|
105
|
-
return String(value);
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
/** A prompt as the package ships it, without a roleplay root: for code that has none, such as tests. */
|
|
109
|
-
function shippedText(id, language = "en", variables = {}) {
|
|
110
|
-
return fill(id, readFileSync(join(shippedPromptsDirectory(), language, `${id}.md`), "utf8"), variables, language);
|
|
111
|
-
}
|
|
112
|
-
function listFiles(directory, prefix = "") {
|
|
113
|
-
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
|
|
114
|
-
const name = `${prefix}${entry.name}`;
|
|
115
|
-
if (entry.isDirectory()) return listFiles(join(directory, entry.name), `${name}/`);
|
|
116
|
-
return entry.name.endsWith(".md") ? [name.slice(0, -3)] : [];
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Every instruction the models are given is a file under `<root>/prompts/<language>/`, so that the person can edit them. The files
|
|
121
|
-
* come from the package: one that is not there (never written, or deleted to restore it) is copied again when it is needed.
|
|
122
|
-
*/
|
|
123
|
-
var PromptLibrary = class extends Service {
|
|
124
|
-
root;
|
|
125
|
-
shipped;
|
|
126
|
-
constructor(ctx, root, shipped = shippedPromptsDirectory()) {
|
|
127
|
-
super(ctx, "roleplayPrompts");
|
|
128
|
-
this.root = root;
|
|
129
|
-
this.shipped = shipped;
|
|
130
|
-
}
|
|
131
|
-
get directory() {
|
|
132
|
-
return join(this.root.path, PROMPTS_FOLDER);
|
|
133
|
-
}
|
|
134
|
-
/** The languages the package ships prompts in. */
|
|
135
|
-
get languages() {
|
|
136
|
-
return readdirSync(this.shipped, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
|
|
137
|
-
}
|
|
138
|
-
/** Writes every shipped prompt that is missing, so that they can be found and edited before they are first used. */
|
|
139
|
-
bootstrap() {
|
|
140
|
-
for (const language of this.languages) for (const id of listFiles(join(this.shipped, language))) this.file(id, language);
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* The prompt `id` in the language (the primary subtag of a language tag), with `${name}` replaced by the variables.
|
|
144
|
-
* 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.
|
|
145
|
-
*/
|
|
146
|
-
text(id, language, variables = {}, session) {
|
|
147
|
-
if (!ID$1.test(id)) throw new Error(`"${id}" is not a prompt id`);
|
|
148
|
-
const tag = languageOf(language);
|
|
149
|
-
const own = session === void 0 ? void 0 : this.override(id, tag, session);
|
|
150
|
-
return fill(id, readFileSync(own ?? this.file(id, tag), "utf8"), variables, tag);
|
|
151
|
-
}
|
|
152
|
-
override(id, language, session) {
|
|
153
|
-
if (!SESSION_ID.test(session)) throw new Error(`"${session}" is not a session id`);
|
|
154
|
-
const found = join(this.root.path, "sessions", session, PROMPTS_FOLDER, language, `${id}.md`);
|
|
155
|
-
return this.usable(found, id) ? found : void 0;
|
|
156
|
-
}
|
|
157
|
-
/** 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. */
|
|
158
|
-
file(id, language) {
|
|
159
|
-
const found = join(this.directory, language, `${id}.md`);
|
|
160
|
-
if (this.usable(found, id)) return found;
|
|
161
|
-
const source = [language, "en"].map((name) => join(this.shipped, name, `${id}.md`)).find((path) => existsSync(path));
|
|
162
|
-
if (source === void 0) throw new Error(`there is no prompt ${id} in ${language} or en`);
|
|
163
|
-
const target = source.startsWith(join(this.shipped, language)) ? found : join(this.directory, "en", `${id}.md`);
|
|
164
|
-
if (!this.usable(target, id)) {
|
|
165
|
-
mkdirSync(dirname(target), { recursive: true });
|
|
166
|
-
copyFileSync(source, target);
|
|
167
|
-
}
|
|
168
|
-
return target;
|
|
169
|
-
}
|
|
170
|
-
usable(path, id) {
|
|
171
|
-
if (!existsSync(path)) return false;
|
|
172
|
-
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`);
|
|
173
|
-
return true;
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
const names = (text) => new Set([...text.matchAll(VARIABLE)].map((match) => match[1]));
|
|
177
|
-
/** 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. */
|
|
178
|
-
function promptProblem(language, id, text, shipped = shippedPromptsDirectory()) {
|
|
179
|
-
const original = ID$1.test(id) ? [language, "en"].map((name) => join(shipped, name, `${id}.md`)).find((path) => existsSync(path)) : void 0;
|
|
180
|
-
if (original === void 0) return `there is no prompt "${id}"`;
|
|
181
|
-
if (text.trim() === "") return "the prompt is empty";
|
|
182
|
-
const allowed = names(readFileSync(original, "utf8"));
|
|
183
|
-
const unknown = [...names(text)].filter((name) => !allowed.has(name));
|
|
184
|
-
return unknown.length === 0 ? void 0 : `uses ${unknown.map((name) => `\${${name}}`).join(", ")}, which the prompt is not given (it is given: ${[...allowed].join(", ") || "none"})`;
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* 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.
|
|
188
|
-
* 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.
|
|
189
|
-
* @returns the problems found, each with the file; nothing is copied when there are any.
|
|
190
|
-
*/
|
|
191
|
-
function copyWorldPrompts(worldDirectory, target, shipped = shippedPromptsDirectory()) {
|
|
192
|
-
const source = join(worldDirectory, PROMPTS_FOLDER);
|
|
193
|
-
if (!existsSync(source)) return [];
|
|
194
|
-
const problems = [];
|
|
195
|
-
const found = [];
|
|
196
|
-
for (const entry of readdirSync(source, { withFileTypes: true })) {
|
|
197
|
-
const folder = join(source, entry.name);
|
|
198
|
-
if (!entry.isDirectory() || !LANGUAGE.test(entry.name)) {
|
|
199
|
-
problems.push({
|
|
200
|
-
path: folder,
|
|
201
|
-
message: "prompts are kept in a folder named by the language, such as en or zh"
|
|
202
|
-
});
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
for (const id of listFiles(folder)) {
|
|
206
|
-
const from = join(folder, `${id}.md`);
|
|
207
|
-
const message = promptProblem(entry.name, id, readFileSync(from, "utf8"), shipped);
|
|
208
|
-
if (message !== void 0) {
|
|
209
|
-
problems.push({
|
|
210
|
-
path: from,
|
|
211
|
-
message
|
|
212
|
-
});
|
|
213
|
-
continue;
|
|
214
|
-
}
|
|
215
|
-
found.push({
|
|
216
|
-
from,
|
|
217
|
-
to: join(target, PROMPTS_FOLDER, entry.name, `${id}.md`)
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
if (problems.length > 0) return problems;
|
|
222
|
-
for (const { from, to } of found) {
|
|
223
|
-
mkdirSync(dirname(to), { recursive: true });
|
|
224
|
-
copyFileSync(from, to);
|
|
225
|
-
}
|
|
226
|
-
return [];
|
|
227
|
-
}
|
|
228
|
-
/** A branch plays with the prompts of the session it was cut from. */
|
|
229
|
-
function inheritPrompts(from, to) {
|
|
230
|
-
if (existsSync(join(from, "prompts"))) cpSync(join(from, PROMPTS_FOLDER), join(to, PROMPTS_FOLDER), { recursive: true });
|
|
231
|
-
}
|
|
232
|
-
/** The languages the package ships prompts in. */
|
|
233
|
-
function shippedLanguages(shipped = shippedPromptsDirectory()) {
|
|
234
|
-
return readdirSync(shipped, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
|
|
235
|
-
}
|
|
236
|
-
//#endregion
|
|
237
|
-
//#region src/host/validation.ts
|
|
238
|
-
function formatIssue({ path, message }) {
|
|
239
|
-
return path === "" ? message : `${path}: ${message}`;
|
|
240
|
-
}
|
|
241
|
-
function isRecord(value) {
|
|
242
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Reads a JSON object field by field. Problems are collected with their path
|
|
246
|
-
* instead of stopping at the first one, so an author sees everything wrong in
|
|
247
|
-
* a file at once. A missing field, `null` and a blank string all mean "absent".
|
|
248
|
-
*/
|
|
249
|
-
var Reader = class Reader {
|
|
250
|
-
data;
|
|
251
|
-
path;
|
|
252
|
-
issues;
|
|
253
|
-
constructor(data, path, issues) {
|
|
254
|
-
this.data = data;
|
|
255
|
-
this.path = path;
|
|
256
|
-
this.issues = issues;
|
|
257
|
-
}
|
|
258
|
-
/** Parse JSON text whose top level must be an object; reports why when it is not. */
|
|
259
|
-
static fromJson(text, issues) {
|
|
260
|
-
let data;
|
|
261
|
-
try {
|
|
262
|
-
data = JSON.parse(text.replace(/^/, ""));
|
|
263
|
-
} catch (error) {
|
|
264
|
-
issues.push({
|
|
265
|
-
path: "",
|
|
266
|
-
message: `invalid JSON: ${error instanceof Error ? error.message : String(error)}`
|
|
267
|
-
});
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
if (!isRecord(data)) {
|
|
271
|
-
issues.push({
|
|
272
|
-
path: "",
|
|
273
|
-
message: "expected a JSON object"
|
|
274
|
-
});
|
|
275
|
-
return;
|
|
276
|
-
}
|
|
277
|
-
return new Reader(data, "", issues);
|
|
278
|
-
}
|
|
279
|
-
fail(key, message) {
|
|
280
|
-
this.issues.push({
|
|
281
|
-
path: this.child(key),
|
|
282
|
-
message
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
/** The value as written, with `null` read as absent. */
|
|
286
|
-
raw(key) {
|
|
287
|
-
const value = this.data[key];
|
|
288
|
-
return value === null ? void 0 : value;
|
|
289
|
-
}
|
|
290
|
-
string(key, options = {}) {
|
|
291
|
-
const value = this.raw(key);
|
|
292
|
-
if (value !== void 0 && typeof value !== "string") {
|
|
293
|
-
this.fail(key, "must be a string");
|
|
294
|
-
return;
|
|
295
|
-
}
|
|
296
|
-
const text = value?.trim();
|
|
297
|
-
if (text === void 0 || text === "") {
|
|
298
|
-
if (options.required === true) this.fail(key, "is required");
|
|
299
|
-
return;
|
|
300
|
-
}
|
|
301
|
-
return text;
|
|
302
|
-
}
|
|
303
|
-
/** A string that is empty when absent. */
|
|
304
|
-
text(key) {
|
|
305
|
-
return this.string(key) ?? "";
|
|
306
|
-
}
|
|
307
|
-
number(key, range = {}) {
|
|
308
|
-
const value = this.raw(key);
|
|
309
|
-
if (value === void 0) return void 0;
|
|
310
|
-
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
311
|
-
this.fail(key, "must be a number");
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
if (range.min !== void 0 && value < range.min || range.max !== void 0 && value > range.max) {
|
|
315
|
-
this.fail(key, `must be between ${range.min ?? "-∞"} and ${range.max ?? "∞"}`);
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
return value;
|
|
319
|
-
}
|
|
320
|
-
oneOf(key, allowed, fallback) {
|
|
321
|
-
const value = this.string(key);
|
|
322
|
-
if (value === void 0) return fallback;
|
|
323
|
-
const match = allowed.find((candidate) => candidate === value);
|
|
324
|
-
if (match === void 0) {
|
|
325
|
-
this.fail(key, `must be one of: ${allowed.join(", ")}`);
|
|
326
|
-
return fallback;
|
|
327
|
-
}
|
|
328
|
-
return match;
|
|
329
|
-
}
|
|
330
|
-
/** An array of strings, trimmed, without blanks or repeats. */
|
|
331
|
-
strings(key) {
|
|
332
|
-
const value = this.raw(key);
|
|
333
|
-
if (value === void 0) return [];
|
|
334
|
-
if (!Array.isArray(value)) {
|
|
335
|
-
this.fail(key, "must be an array of strings");
|
|
336
|
-
return [];
|
|
337
|
-
}
|
|
338
|
-
const result = /* @__PURE__ */ new Set();
|
|
339
|
-
value.forEach((entry, index) => {
|
|
340
|
-
if (typeof entry !== "string") this.fail(`${key}[${index}]`, "must be a string");
|
|
341
|
-
else if (entry.trim() !== "") result.add(entry.trim());
|
|
342
|
-
});
|
|
343
|
-
return [...result];
|
|
344
|
-
}
|
|
345
|
-
object(key) {
|
|
346
|
-
const value = this.raw(key);
|
|
347
|
-
if (value === void 0) return void 0;
|
|
348
|
-
if (!isRecord(value)) {
|
|
349
|
-
this.fail(key, "must be an object");
|
|
350
|
-
return;
|
|
351
|
-
}
|
|
352
|
-
return new Reader(value, this.child(key), this.issues);
|
|
353
|
-
}
|
|
354
|
-
objects(key) {
|
|
355
|
-
const value = this.raw(key);
|
|
356
|
-
if (value === void 0) return [];
|
|
357
|
-
if (!Array.isArray(value)) {
|
|
358
|
-
this.fail(key, "must be an array of objects");
|
|
359
|
-
return [];
|
|
360
|
-
}
|
|
361
|
-
return value.flatMap((entry, index) => {
|
|
362
|
-
if (isRecord(entry)) return [new Reader(entry, this.child(`${key}[${index}]`), this.issues)];
|
|
363
|
-
this.fail(`${key}[${index}]`, "must be an object");
|
|
364
|
-
return [];
|
|
365
|
-
});
|
|
366
|
-
}
|
|
367
|
-
child(key) {
|
|
368
|
-
return this.path === "" ? key : `${this.path}.${key}`;
|
|
369
|
-
}
|
|
370
|
-
};
|
|
371
|
-
//#endregion
|
|
372
|
-
//#region src/host/errors.ts
|
|
373
|
-
function errorMessage(error) {
|
|
374
|
-
return error instanceof Error ? error.message : String(error);
|
|
375
|
-
}
|
|
376
|
-
//#endregion
|
|
377
|
-
//#region src/host/read-document.ts
|
|
378
|
-
/** Authored documents are short; a larger file is not one. */
|
|
379
|
-
const MAX_DOCUMENT_BYTES = 1048576;
|
|
380
|
-
/**
|
|
381
|
-
* Read and parse one authored document. Every problem is added to `problems`
|
|
382
|
-
* with the file's path, and the document is skipped.
|
|
383
|
-
*/
|
|
384
|
-
async function readDocument(file, parse, problems) {
|
|
385
|
-
if ((await stat(file)).size > MAX_DOCUMENT_BYTES) {
|
|
386
|
-
problems.push({
|
|
387
|
-
path: file,
|
|
388
|
-
message: "file is too large"
|
|
389
|
-
});
|
|
390
|
-
return;
|
|
391
|
-
}
|
|
392
|
-
const result = parse(await readFile(file, "utf8"));
|
|
393
|
-
if (result.ok) return result.value;
|
|
394
|
-
for (const issue of result.issues) problems.push({
|
|
395
|
-
path: file,
|
|
396
|
-
message: formatIssue(issue)
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
//#endregion
|
|
400
|
-
//#region src/host/plots/load.ts
|
|
401
|
-
const PLOTS_FILE_NAME = "plots.json";
|
|
402
|
-
function development(reader, fallback) {
|
|
403
|
-
return {
|
|
404
|
-
id: reader.string("id") ?? fallback,
|
|
405
|
-
text: reader.string("text", { required: true }) ?? "",
|
|
406
|
-
mode: reader.oneOf("mode", PLOT_NUDGE_MODES, "opportunity"),
|
|
407
|
-
prerequisites: reader.strings("prerequisites"),
|
|
408
|
-
guidance: reader.text("guidance"),
|
|
409
|
-
avoid: reader.strings("avoid")
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
/** Parse the optional world-level list of dramatic possibilities. */
|
|
413
|
-
function parsePlotsFile(text) {
|
|
414
|
-
const issues = [];
|
|
415
|
-
const wrapper = Reader.fromJson(`{"plots":${text.replace(/^/, "")}}`, issues);
|
|
416
|
-
if (wrapper === void 0) return {
|
|
417
|
-
ok: false,
|
|
418
|
-
issues
|
|
419
|
-
};
|
|
420
|
-
const plots = wrapper.objects("plots").map((reader, plotIndex) => {
|
|
421
|
-
const pacing = reader.object("pacing");
|
|
422
|
-
const hooks = reader.objects("hooks").map((entry, index) => development(entry, `hook-${index + 1}`));
|
|
423
|
-
const developments = reader.objects("developments").map((entry, index) => development(entry, `development-${index + 1}`));
|
|
424
|
-
return {
|
|
425
|
-
id: reader.string("id", { required: true }) ?? `plot-${plotIndex + 1}`,
|
|
426
|
-
name: reader.string("name", { required: true }) ?? "",
|
|
427
|
-
premise: reader.string("premise", { required: true }) ?? "",
|
|
428
|
-
themes: reader.strings("themes"),
|
|
429
|
-
hooks,
|
|
430
|
-
developments,
|
|
431
|
-
stakes: reader.strings("stakes"),
|
|
432
|
-
resolutions: reader.strings("resolutions"),
|
|
433
|
-
participants: reader.strings("participants"),
|
|
434
|
-
start: reader.oneOf("start", PLOT_STATUSES, "available"),
|
|
435
|
-
pacing: {
|
|
436
|
-
quietTurns: pacing?.number("quiet_turns", {
|
|
437
|
-
min: 1,
|
|
438
|
-
max: 20
|
|
439
|
-
}) ?? pacing?.number("quietTurns", {
|
|
440
|
-
min: 1,
|
|
441
|
-
max: 20
|
|
442
|
-
}) ?? 3,
|
|
443
|
-
cooldownTurns: pacing?.number("cooldown_turns", {
|
|
444
|
-
min: 1,
|
|
445
|
-
max: 50
|
|
446
|
-
}) ?? pacing?.number("cooldownTurns", {
|
|
447
|
-
min: 1,
|
|
448
|
-
max: 50
|
|
449
|
-
}) ?? 4,
|
|
450
|
-
cooldownDays: pacing?.number("cooldown_days", {
|
|
451
|
-
min: 0,
|
|
452
|
-
max: 365
|
|
453
|
-
}) ?? pacing?.number("cooldownDays", {
|
|
454
|
-
min: 0,
|
|
455
|
-
max: 365
|
|
456
|
-
}) ?? 1
|
|
457
|
-
}
|
|
458
|
-
};
|
|
459
|
-
});
|
|
460
|
-
const ids = plots.map((plot) => plot.id);
|
|
461
|
-
ids.forEach((id, index) => {
|
|
462
|
-
if (ids.indexOf(id) !== index) issues.push({
|
|
463
|
-
path: `plots[${index}].id`,
|
|
464
|
-
message: `plot id "${id}" is defined twice`
|
|
465
|
-
});
|
|
466
|
-
});
|
|
467
|
-
for (const [plotIndex, plot] of plots.entries()) {
|
|
468
|
-
const steps = [...plot.hooks, ...plot.developments];
|
|
469
|
-
const stepIds = steps.map((step) => step.id);
|
|
470
|
-
stepIds.forEach((id, index) => {
|
|
471
|
-
if (stepIds.indexOf(id) !== index) issues.push({
|
|
472
|
-
path: `plots[${plotIndex}]`,
|
|
473
|
-
message: `development id "${id}" is defined twice`
|
|
474
|
-
});
|
|
475
|
-
});
|
|
476
|
-
for (const step of steps) for (const required of step.prerequisites) if (!stepIds.includes(required)) issues.push({
|
|
477
|
-
path: `plots[${plotIndex}]`,
|
|
478
|
-
message: `development "${step.id}" requires unknown development "${required}"`
|
|
479
|
-
});
|
|
480
|
-
}
|
|
481
|
-
return issues.length === 0 ? {
|
|
482
|
-
ok: true,
|
|
483
|
-
value: plots
|
|
484
|
-
} : {
|
|
485
|
-
ok: false,
|
|
486
|
-
issues
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
|
-
async function loadPlots(worldDir) {
|
|
490
|
-
const result = {
|
|
491
|
-
plots: [],
|
|
492
|
-
problems: []
|
|
493
|
-
};
|
|
494
|
-
const file = join(worldDir, PLOTS_FILE_NAME);
|
|
495
|
-
try {
|
|
496
|
-
result.plots = await readDocument(file, parsePlotsFile, result.problems) ?? [];
|
|
497
|
-
} catch (error) {
|
|
498
|
-
if (error.code !== "ENOENT") result.problems.push({
|
|
499
|
-
path: file,
|
|
500
|
-
message: errorMessage(error)
|
|
501
|
-
});
|
|
502
|
-
}
|
|
503
|
-
return result;
|
|
504
|
-
}
|
|
505
|
-
function plotReferenceProblems(plots, entities) {
|
|
506
|
-
const known = new Set(entities);
|
|
507
|
-
return plots.flatMap((plot) => plot.participants.filter((participant) => !known.has(participant)).map((participant) => `plot "${plot.id}" names unknown participant "${participant}"`));
|
|
508
|
-
}
|
|
509
|
-
//#endregion
|
|
510
|
-
//#region src/host/translate/language.ts
|
|
511
|
-
const STORY_LANGUAGES = ["en", "zh"];
|
|
512
|
-
/** Less than this many letters of either script is too little to tell a language. */
|
|
513
|
-
const MIN_LETTERS = 20;
|
|
514
|
-
/** A text is Chinese when this share of its Han and Latin letters is Han: names in the other script do not decide it. */
|
|
515
|
-
const HAN_SHARE = .3;
|
|
516
|
-
const count = (text, pattern) => text.match(pattern)?.length ?? 0;
|
|
517
|
-
/** The language a text is written in, by the script of its letters; nothing when there is too little text to tell. */
|
|
518
|
-
function textLanguage(text) {
|
|
519
|
-
const han = count(text, /\p{Script=Han}/gu);
|
|
520
|
-
const latin = count(text, /\p{Script=Latin}/gu);
|
|
521
|
-
if (han + latin < MIN_LETTERS) return void 0;
|
|
522
|
-
return han / (han + latin) >= HAN_SHARE ? "zh" : "en";
|
|
523
|
-
}
|
|
524
|
-
const isStoryLanguage = (value) => value === "en" || value === "zh";
|
|
525
|
-
const LANGUAGE_NAMES = {
|
|
526
|
-
en: {
|
|
527
|
-
en: "English",
|
|
528
|
-
zh: "英文"
|
|
529
|
-
},
|
|
530
|
-
zh: {
|
|
531
|
-
en: "Chinese",
|
|
532
|
-
zh: "中文"
|
|
533
|
-
}
|
|
534
|
-
};
|
|
535
|
-
/** What is wrong with texts that should be in the chat's language but are written in the other one; nothing when they are right, too short to tell, or the language is not one we can read. */
|
|
536
|
-
function wrongLanguage(wanted, texts) {
|
|
537
|
-
if (!isStoryLanguage(wanted)) return void 0;
|
|
538
|
-
const wrong = Object.entries(texts).flatMap(([name, text]) => {
|
|
539
|
-
const found = text === void 0 ? void 0 : textLanguage(text);
|
|
540
|
-
return found === void 0 || found === wanted ? [] : [`${name} is written in ${LANGUAGE_NAMES[found].en}`];
|
|
541
|
-
});
|
|
542
|
-
return wrong.length === 0 ? void 0 : `${wrong.join(" and ")}: write it in ${LANGUAGE_NAMES[wanted].en} (${LANGUAGE_NAMES[wanted][wanted]})`;
|
|
543
|
-
}
|
|
544
|
-
//#endregion
|
|
545
31
|
//#region src/shared/worlds.ts
|
|
546
32
|
/** Path prefix of the plugin's HTTP API. Relative, so a mounted deployment resolves it against the page. */
|
|
547
33
|
const API_PREFIX = "roleplay-api";
|
|
@@ -1375,6 +861,6 @@ function anyTab(bases) {
|
|
|
1375
861
|
}));
|
|
1376
862
|
}
|
|
1377
863
|
//#endregion
|
|
1378
|
-
export {
|
|
864
|
+
export { API_PREFIX as _, packIds as a, INTENT_TYPES as b, describePack as c, packPath as d, readPack as f, SURFACE_NAMES as g, PACK_MEDIA_TYPES as h, inheritPacks as i, 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, INTENT_HORIZONS as v, INTENT_STATUSES as y };
|
|
1379
865
|
|
|
1380
|
-
//# sourceMappingURL=packs-
|
|
866
|
+
//# sourceMappingURL=packs-BYjrAen6.js.map
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { t as promptFor } from "./access-EbNdZFbk.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { a as runChild } from "./child-lPncV1ps.js";
|
|
3
|
+
import { d as nextSceneSerial, n as generateTransientImage, p as referenceImagePath, t as generateReferenceImage } from "./generator-B6FxlKOV.js";
|
|
4
|
+
import { i as updateImage, n as galleryFile, r as listGallery } from "./gallery-CtFDL22W.js";
|
|
4
5
|
import { dirname, join, relative } from "node:path";
|
|
5
6
|
import { existsSync } from "node:fs";
|
|
6
7
|
import { createHash, randomUUID } from "node:crypto";
|
|
@@ -850,4 +851,4 @@ async function runImagePresenter(job) {
|
|
|
850
851
|
//#endregion
|
|
851
852
|
export { tokensOf as a, searchImages as i, interpretImage as n, present as o, imageTools as r, presentedFor as s, runImagePresenter as t };
|
|
852
853
|
|
|
853
|
-
//# sourceMappingURL=presenter-agent-
|
|
854
|
+
//# sourceMappingURL=presenter-agent-Dt21K96b.js.map
|