@dsh-agentic-roleplay/core 0.3.6 → 0.4.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/lib/{tools-Dy1fgP6B.js → authoring-tools-C2BHkEFP.js} +129 -25
- package/lib/authoring.js +7 -5
- package/lib/{child-CFjnaTZt.js → child-DsixtqTw.js} +94 -2
- package/lib/client.js +3758 -2276
- package/lib/client.js.map +1 -1
- package/lib/compaction.js +3 -2
- package/lib/doctor.js +2 -4
- package/lib/{gallery-DIEgETzU.js → gallery-CDYKJf_O.js} +2 -2
- package/lib/{generator-DJ48HxPI.js → generator-BR1ugzln.js} +2 -2
- package/lib/{deliver-BBgciEEK.js → hooks-CHmUksFS.js} +1963 -236
- package/lib/images.js +4 -4
- package/lib/index.js +847 -254
- package/lib/{library-9JB9zw5a.js → library-COIktKBa.js} +4 -3
- package/lib/{lorebook-author-tools-Bj31z1cu.js → lorebook-author-tools-CaT74Q7v.js} +118 -43
- package/lib/lorebook-authoring.js +5 -3
- package/lib/{settings-CA6icLL_.js → narration-DstnwrvX.js} +148 -2
- package/lib/orchestration.js +344 -37
- package/lib/{pack-jail-2XztyYAX.js → pack-jail-CEDDI4Xw.js} +170 -13
- package/lib/{packs-xHRCcGdb.js → packs-DB_9xiCp.js} +7 -4
- package/lib/{presenter-agent-DnNEG_Mn.js → presenter-agent-Gu34bZwd.js} +6 -6
- package/lib/preset-BL1d40oG.js +242 -0
- package/lib/{preset-author-tools-BxycmoAr.js → preset-author-tools-B2wZXKZo.js} +7 -7
- package/lib/preset-authoring.js +6 -3
- package/lib/{presets-gkBWAiVb.js → presets-Dc15L6OU.js} +9 -26
- package/lib/{reflection-q0UpwXlx.js → reflection-BWMVdHhR.js} +51 -17
- package/package.json +2 -2
- package/packs/scene/map.html +1 -0
- package/packs/scene/strip.html +1 -0
- package/prompts/en/import/lorebook.md +2 -1
- package/prompts/en/import/style.md +1 -0
- package/prompts/en/lorebook/procedure.md +1 -1
- package/prompts/en/preset/procedure.md +1 -1
- package/prompts/en/roleplay/character-lookup.md +1 -0
- package/prompts/en/roleplay/game-master.md +1 -1
- package/prompts/en/roleplay/record-keeper.md +3 -3
- package/prompts/en/world/procedure.md +1 -1
- package/prompts/zh/import/lorebook.md +2 -1
- package/prompts/zh/import/style.md +1 -0
- package/prompts/zh/lorebook/procedure.md +1 -1
- package/prompts/zh/preset/procedure.md +1 -1
- package/prompts/zh/roleplay/character-lookup.md +1 -0
- package/prompts/zh/roleplay/game-master.md +1 -1
- package/prompts/zh/roleplay/record-keeper.md +3 -3
- package/prompts/zh/world/procedure.md +1 -1
- package/lib/hooks-DkwFGtNH.js +0 -1060
- package/lib/language-BgyxtB2h.js +0 -38
- package/lib/load-CVwJjIJ5.js +0 -486
- package/lib/narration-DK2kiIjE.js +0 -141
- package/lib/preset-D0FMSeZ9.js +0 -59
package/lib/language-BgyxtB2h.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
//#region src/host/translate/language.ts
|
|
2
|
-
const STORY_LANGUAGES = ["en", "zh"];
|
|
3
|
-
/** Less than this many letters of either script is too little to tell a language. */
|
|
4
|
-
const MIN_LETTERS = 20;
|
|
5
|
-
/** A text is Chinese when this share of its Han and Latin letters is Han: names in the other script do not decide it. */
|
|
6
|
-
const HAN_SHARE = .3;
|
|
7
|
-
const count = (text, pattern) => text.match(pattern)?.length ?? 0;
|
|
8
|
-
/** The language a text is written in, by the script of its letters; nothing when there is too little text to tell. */
|
|
9
|
-
function textLanguage(text) {
|
|
10
|
-
const han = count(text, /\p{Script=Han}/gu);
|
|
11
|
-
const latin = count(text, /\p{Script=Latin}/gu);
|
|
12
|
-
if (han + latin < MIN_LETTERS) return void 0;
|
|
13
|
-
return han / (han + latin) >= HAN_SHARE ? "zh" : "en";
|
|
14
|
-
}
|
|
15
|
-
const isStoryLanguage = (value) => value === "en" || value === "zh";
|
|
16
|
-
const LANGUAGE_NAMES = {
|
|
17
|
-
en: {
|
|
18
|
-
en: "English",
|
|
19
|
-
zh: "英文"
|
|
20
|
-
},
|
|
21
|
-
zh: {
|
|
22
|
-
en: "Chinese",
|
|
23
|
-
zh: "中文"
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
/** 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. */
|
|
27
|
-
function wrongLanguage(wanted, texts) {
|
|
28
|
-
if (!isStoryLanguage(wanted)) return void 0;
|
|
29
|
-
const wrong = Object.entries(texts).flatMap(([name, text]) => {
|
|
30
|
-
const found = text === void 0 ? void 0 : textLanguage(text);
|
|
31
|
-
return found === void 0 || found === wanted ? [] : [`${name} is written in ${LANGUAGE_NAMES[found].en}`];
|
|
32
|
-
});
|
|
33
|
-
return wrong.length === 0 ? void 0 : `${wrong.join(" and ")}: write it in ${LANGUAGE_NAMES[wanted].en} (${LANGUAGE_NAMES[wanted][wanted]})`;
|
|
34
|
-
}
|
|
35
|
-
//#endregion
|
|
36
|
-
export { wrongLanguage as a, textLanguage as i, STORY_LANGUAGES as n, isStoryLanguage as r, LANGUAGE_NAMES as t };
|
|
37
|
-
|
|
38
|
-
//# sourceMappingURL=language-BgyxtB2h.js.map
|
package/lib/load-CVwJjIJ5.js
DELETED
|
@@ -1,486 +0,0 @@
|
|
|
1
|
-
import "./settings-CA6icLL_.js";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
3
|
-
import { copyFileSync, cpSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
|
-
import { Service } from "@deepseek-ai/cordis";
|
|
6
|
-
import { readFile, stat } from "node:fs/promises";
|
|
7
|
-
//#region ../dsh-roleplay-model/src/plot.ts
|
|
8
|
-
const PLOT_STATUSES = [
|
|
9
|
-
"dormant",
|
|
10
|
-
"available",
|
|
11
|
-
"engaged",
|
|
12
|
-
"cooling",
|
|
13
|
-
"resolved",
|
|
14
|
-
"abandoned"
|
|
15
|
-
];
|
|
16
|
-
const PLOT_NUDGE_MODES = [
|
|
17
|
-
"opportunity",
|
|
18
|
-
"reminder",
|
|
19
|
-
"character",
|
|
20
|
-
"consequence",
|
|
21
|
-
"escalation",
|
|
22
|
-
"breathing_room"
|
|
23
|
-
];
|
|
24
|
-
//#endregion
|
|
25
|
-
//#region src/host/sessions/errors.ts
|
|
26
|
-
var WorldNotFoundError = class extends Error {};
|
|
27
|
-
/** The world cannot be played as it is; `problems` say what to fix. */
|
|
28
|
-
var InvalidWorldError = class extends Error {
|
|
29
|
-
problems;
|
|
30
|
-
constructor(message, problems) {
|
|
31
|
-
super(message);
|
|
32
|
-
this.problems = problems;
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
/** The request cannot be honoured for this world, such as a character that cannot be played. */
|
|
36
|
-
var InvalidStartError = class extends Error {};
|
|
37
|
-
var SessionNotFoundError = class extends Error {};
|
|
38
|
-
/** DSH's session services are not available, so no chat can be created or retired. */
|
|
39
|
-
var AgentUnavailableError = class extends Error {};
|
|
40
|
-
/** The session is put away and cannot be played until it is brought back. */
|
|
41
|
-
var SessionArchivedError = class extends Error {};
|
|
42
|
-
/** A reflection cannot be started now: a turn is being played, or one is already running. */
|
|
43
|
-
var ReflectionRefusedError = class extends Error {};
|
|
44
|
-
//#endregion
|
|
45
|
-
//#region src/host/prompts/library.ts
|
|
46
|
-
const PROMPTS_FOLDER = "prompts";
|
|
47
|
-
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}$/;
|
|
48
|
-
const ID = /^[a-z0-9-]+(\/[a-z0-9-]+)*$/;
|
|
49
|
-
const LANGUAGE_FOLDER = /^[a-z]{2,8}$/;
|
|
50
|
-
const LANGUAGE = LANGUAGE_FOLDER;
|
|
51
|
-
const VARIABLE = /\$\{([A-Za-z]+)\}/g;
|
|
52
|
-
/** The folder of the prompts that ship with the package, found from this module so that it works from source and from the build. */
|
|
53
|
-
function shippedPromptsDirectory(from = dirname(fileURLToPath(import.meta.url))) {
|
|
54
|
-
for (let directory = from;; directory = dirname(directory)) {
|
|
55
|
-
const candidate = join(directory, PROMPTS_FOLDER);
|
|
56
|
-
if (existsSync(join(directory, "package.json")) && existsSync(candidate)) return candidate;
|
|
57
|
-
if (dirname(directory) === directory) throw new Error("the prompts shipped with the package were not found");
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
/** `zh-CN` and `ZH` are both `zh`; anything that is not a language tag is refused. */
|
|
61
|
-
function languageOf(tag) {
|
|
62
|
-
const primary = (tag ?? "en").trim().toLowerCase().split(/[-_]/)[0] ?? "";
|
|
63
|
-
if (!LANGUAGE.test(primary)) throw new Error(`"${tag}" is not a language`);
|
|
64
|
-
return primary;
|
|
65
|
-
}
|
|
66
|
-
/** How a language is named inside a prompt, for the instruction to write in it. */
|
|
67
|
-
const LANGUAGE_NAMES = {
|
|
68
|
-
en: "English",
|
|
69
|
-
zh: "中文"
|
|
70
|
-
};
|
|
71
|
-
function fill(id, text, given, tag) {
|
|
72
|
-
const variables = {
|
|
73
|
-
language: LANGUAGE_NAMES[tag] ?? tag,
|
|
74
|
-
...given
|
|
75
|
-
};
|
|
76
|
-
return text.trim().replace(/^\$\{([A-Za-z]+)\}[ \t]*(\r?\n|$)/gm, (match, name) => variables[name] === "" ? "" : match).replace(VARIABLE, (_match, name) => {
|
|
77
|
-
const value = variables[name];
|
|
78
|
-
if (value === void 0) throw new Error(`prompt ${id} uses \${${name}}, which is not given a value (given: ${Object.keys(variables).join(", ") || "none"})`);
|
|
79
|
-
return String(value);
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
/** A prompt as the package ships it, without a roleplay root: for code that has none, such as tests. */
|
|
83
|
-
function shippedText(id, language = "en", variables = {}) {
|
|
84
|
-
return fill(id, readFileSync(join(shippedPromptsDirectory(), language, `${id}.md`), "utf8"), variables, language);
|
|
85
|
-
}
|
|
86
|
-
function listFiles(directory, prefix = "") {
|
|
87
|
-
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
|
|
88
|
-
const name = `${prefix}${entry.name}`;
|
|
89
|
-
if (entry.isDirectory()) return listFiles(join(directory, entry.name), `${name}/`);
|
|
90
|
-
return entry.name.endsWith(".md") ? [name.slice(0, -3)] : [];
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Every instruction the models are given is a file under `<root>/prompts/<language>/`, so that the person can edit them. The files
|
|
95
|
-
* come from the package: one that is not there (never written, or deleted to restore it) is copied again when it is needed.
|
|
96
|
-
*/
|
|
97
|
-
var PromptLibrary = class extends Service {
|
|
98
|
-
root;
|
|
99
|
-
shipped;
|
|
100
|
-
constructor(ctx, root, shipped = shippedPromptsDirectory()) {
|
|
101
|
-
super(ctx, "roleplayPrompts");
|
|
102
|
-
this.root = root;
|
|
103
|
-
this.shipped = shipped;
|
|
104
|
-
}
|
|
105
|
-
get directory() {
|
|
106
|
-
return join(this.root.path, PROMPTS_FOLDER);
|
|
107
|
-
}
|
|
108
|
-
/** The languages the package ships prompts in. */
|
|
109
|
-
get languages() {
|
|
110
|
-
return readdirSync(this.shipped, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
|
|
111
|
-
}
|
|
112
|
-
/** Writes every shipped prompt that is missing, so that they can be found and edited before they are first used. */
|
|
113
|
-
bootstrap() {
|
|
114
|
-
for (const language of this.languages) for (const id of listFiles(join(this.shipped, language))) this.file(id, language);
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* The prompt `id` in the language (the primary subtag of a language tag), with `${name}` replaced by the variables.
|
|
118
|
-
* 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.
|
|
119
|
-
*/
|
|
120
|
-
text(id, language, variables = {}, session) {
|
|
121
|
-
if (!ID.test(id)) throw new Error(`"${id}" is not a prompt id`);
|
|
122
|
-
const tag = languageOf(language);
|
|
123
|
-
const own = session === void 0 ? void 0 : this.override(id, tag, session);
|
|
124
|
-
return fill(id, readFileSync(own ?? this.file(id, tag), "utf8"), variables, tag);
|
|
125
|
-
}
|
|
126
|
-
override(id, language, session) {
|
|
127
|
-
if (!SESSION_ID.test(session)) throw new Error(`"${session}" is not a session id`);
|
|
128
|
-
const found = join(this.root.path, "sessions", session, PROMPTS_FOLDER, language, `${id}.md`);
|
|
129
|
-
return this.usable(found, id) ? found : void 0;
|
|
130
|
-
}
|
|
131
|
-
/** 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. */
|
|
132
|
-
file(id, language) {
|
|
133
|
-
const found = join(this.directory, language, `${id}.md`);
|
|
134
|
-
if (this.usable(found, id)) return found;
|
|
135
|
-
const source = [language, "en"].map((name) => join(this.shipped, name, `${id}.md`)).find((path) => existsSync(path));
|
|
136
|
-
if (source === void 0) throw new Error(`there is no prompt ${id} in ${language} or en`);
|
|
137
|
-
const target = source.startsWith(join(this.shipped, language)) ? found : join(this.directory, "en", `${id}.md`);
|
|
138
|
-
if (!this.usable(target, id)) {
|
|
139
|
-
mkdirSync(dirname(target), { recursive: true });
|
|
140
|
-
copyFileSync(source, target);
|
|
141
|
-
}
|
|
142
|
-
return target;
|
|
143
|
-
}
|
|
144
|
-
usable(path, id) {
|
|
145
|
-
if (!existsSync(path)) return false;
|
|
146
|
-
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`);
|
|
147
|
-
return true;
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
const names = (text) => new Set([...text.matchAll(VARIABLE)].map((match) => match[1]));
|
|
151
|
-
/** 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. */
|
|
152
|
-
function promptProblem(language, id, text, shipped = shippedPromptsDirectory()) {
|
|
153
|
-
const original = ID.test(id) ? [language, "en"].map((name) => join(shipped, name, `${id}.md`)).find((path) => existsSync(path)) : void 0;
|
|
154
|
-
if (original === void 0) return `there is no prompt "${id}"`;
|
|
155
|
-
if (text.trim() === "") return "the prompt is empty";
|
|
156
|
-
const allowed = names(readFileSync(original, "utf8"));
|
|
157
|
-
const unknown = [...names(text)].filter((name) => !allowed.has(name));
|
|
158
|
-
return unknown.length === 0 ? void 0 : `uses ${unknown.map((name) => `\${${name}}`).join(", ")}, which the prompt is not given (it is given: ${[...allowed].join(", ") || "none"})`;
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* 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.
|
|
162
|
-
* 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.
|
|
163
|
-
* @returns the problems found, each with the file; nothing is copied when there are any.
|
|
164
|
-
*/
|
|
165
|
-
function copyWorldPrompts(worldDirectory, target, shipped = shippedPromptsDirectory()) {
|
|
166
|
-
const source = join(worldDirectory, PROMPTS_FOLDER);
|
|
167
|
-
if (!existsSync(source)) return [];
|
|
168
|
-
const problems = [];
|
|
169
|
-
const found = [];
|
|
170
|
-
for (const entry of readdirSync(source, { withFileTypes: true })) {
|
|
171
|
-
const folder = join(source, entry.name);
|
|
172
|
-
if (!entry.isDirectory() || !LANGUAGE.test(entry.name)) {
|
|
173
|
-
problems.push({
|
|
174
|
-
path: folder,
|
|
175
|
-
message: "prompts are kept in a folder named by the language, such as en or zh"
|
|
176
|
-
});
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
for (const id of listFiles(folder)) {
|
|
180
|
-
const from = join(folder, `${id}.md`);
|
|
181
|
-
const message = promptProblem(entry.name, id, readFileSync(from, "utf8"), shipped);
|
|
182
|
-
if (message !== void 0) {
|
|
183
|
-
problems.push({
|
|
184
|
-
path: from,
|
|
185
|
-
message
|
|
186
|
-
});
|
|
187
|
-
continue;
|
|
188
|
-
}
|
|
189
|
-
found.push({
|
|
190
|
-
from,
|
|
191
|
-
to: join(target, PROMPTS_FOLDER, entry.name, `${id}.md`)
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
if (problems.length > 0) return problems;
|
|
196
|
-
for (const { from, to } of found) {
|
|
197
|
-
mkdirSync(dirname(to), { recursive: true });
|
|
198
|
-
copyFileSync(from, to);
|
|
199
|
-
}
|
|
200
|
-
return [];
|
|
201
|
-
}
|
|
202
|
-
/** A branch plays with the prompts of the session it was cut from. */
|
|
203
|
-
function inheritPrompts(from, to) {
|
|
204
|
-
if (existsSync(join(from, "prompts"))) cpSync(join(from, PROMPTS_FOLDER), join(to, PROMPTS_FOLDER), { recursive: true });
|
|
205
|
-
}
|
|
206
|
-
/** The languages the package ships prompts in. */
|
|
207
|
-
function shippedLanguages(shipped = shippedPromptsDirectory()) {
|
|
208
|
-
return readdirSync(shipped, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
|
|
209
|
-
}
|
|
210
|
-
//#endregion
|
|
211
|
-
//#region src/host/errors.ts
|
|
212
|
-
function errorMessage(error) {
|
|
213
|
-
return error instanceof Error ? error.message : String(error);
|
|
214
|
-
}
|
|
215
|
-
//#endregion
|
|
216
|
-
//#region src/host/validation.ts
|
|
217
|
-
function formatIssue({ path, message }) {
|
|
218
|
-
return path === "" ? message : `${path}: ${message}`;
|
|
219
|
-
}
|
|
220
|
-
function isRecord(value) {
|
|
221
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Reads a JSON object field by field. Problems are collected with their path
|
|
225
|
-
* instead of stopping at the first one, so an author sees everything wrong in
|
|
226
|
-
* a file at once. A missing field, `null` and a blank string all mean "absent".
|
|
227
|
-
*/
|
|
228
|
-
var Reader = class Reader {
|
|
229
|
-
data;
|
|
230
|
-
path;
|
|
231
|
-
issues;
|
|
232
|
-
constructor(data, path, issues) {
|
|
233
|
-
this.data = data;
|
|
234
|
-
this.path = path;
|
|
235
|
-
this.issues = issues;
|
|
236
|
-
}
|
|
237
|
-
/** Parse JSON text whose top level must be an object; reports why when it is not. */
|
|
238
|
-
static fromJson(text, issues) {
|
|
239
|
-
let data;
|
|
240
|
-
try {
|
|
241
|
-
data = JSON.parse(text.replace(/^/, ""));
|
|
242
|
-
} catch (error) {
|
|
243
|
-
issues.push({
|
|
244
|
-
path: "",
|
|
245
|
-
message: `invalid JSON: ${error instanceof Error ? error.message : String(error)}`
|
|
246
|
-
});
|
|
247
|
-
return;
|
|
248
|
-
}
|
|
249
|
-
if (!isRecord(data)) {
|
|
250
|
-
issues.push({
|
|
251
|
-
path: "",
|
|
252
|
-
message: "expected a JSON object"
|
|
253
|
-
});
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
return new Reader(data, "", issues);
|
|
257
|
-
}
|
|
258
|
-
fail(key, message) {
|
|
259
|
-
this.issues.push({
|
|
260
|
-
path: this.child(key),
|
|
261
|
-
message
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
/** The value as written, with `null` read as absent. */
|
|
265
|
-
raw(key) {
|
|
266
|
-
const value = this.data[key];
|
|
267
|
-
return value === null ? void 0 : value;
|
|
268
|
-
}
|
|
269
|
-
string(key, options = {}) {
|
|
270
|
-
const value = this.raw(key);
|
|
271
|
-
if (value !== void 0 && typeof value !== "string") {
|
|
272
|
-
this.fail(key, "must be a string");
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
const text = value?.trim();
|
|
276
|
-
if (text === void 0 || text === "") {
|
|
277
|
-
if (options.required === true) this.fail(key, "is required");
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
return text;
|
|
281
|
-
}
|
|
282
|
-
/** A string that is empty when absent. */
|
|
283
|
-
text(key) {
|
|
284
|
-
return this.string(key) ?? "";
|
|
285
|
-
}
|
|
286
|
-
number(key, range = {}) {
|
|
287
|
-
const value = this.raw(key);
|
|
288
|
-
if (value === void 0) return void 0;
|
|
289
|
-
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
290
|
-
this.fail(key, "must be a number");
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
if (range.min !== void 0 && value < range.min || range.max !== void 0 && value > range.max) {
|
|
294
|
-
this.fail(key, `must be between ${range.min ?? "-∞"} and ${range.max ?? "∞"}`);
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
return value;
|
|
298
|
-
}
|
|
299
|
-
oneOf(key, allowed, fallback) {
|
|
300
|
-
const value = this.string(key);
|
|
301
|
-
if (value === void 0) return fallback;
|
|
302
|
-
const match = allowed.find((candidate) => candidate === value);
|
|
303
|
-
if (match === void 0) {
|
|
304
|
-
this.fail(key, `must be one of: ${allowed.join(", ")}`);
|
|
305
|
-
return fallback;
|
|
306
|
-
}
|
|
307
|
-
return match;
|
|
308
|
-
}
|
|
309
|
-
/** An array of strings, trimmed, without blanks or repeats. */
|
|
310
|
-
strings(key) {
|
|
311
|
-
const value = this.raw(key);
|
|
312
|
-
if (value === void 0) return [];
|
|
313
|
-
if (!Array.isArray(value)) {
|
|
314
|
-
this.fail(key, "must be an array of strings");
|
|
315
|
-
return [];
|
|
316
|
-
}
|
|
317
|
-
const result = /* @__PURE__ */ new Set();
|
|
318
|
-
value.forEach((entry, index) => {
|
|
319
|
-
if (typeof entry !== "string") this.fail(`${key}[${index}]`, "must be a string");
|
|
320
|
-
else if (entry.trim() !== "") result.add(entry.trim());
|
|
321
|
-
});
|
|
322
|
-
return [...result];
|
|
323
|
-
}
|
|
324
|
-
object(key) {
|
|
325
|
-
const value = this.raw(key);
|
|
326
|
-
if (value === void 0) return void 0;
|
|
327
|
-
if (!isRecord(value)) {
|
|
328
|
-
this.fail(key, "must be an object");
|
|
329
|
-
return;
|
|
330
|
-
}
|
|
331
|
-
return new Reader(value, this.child(key), this.issues);
|
|
332
|
-
}
|
|
333
|
-
objects(key) {
|
|
334
|
-
const value = this.raw(key);
|
|
335
|
-
if (value === void 0) return [];
|
|
336
|
-
if (!Array.isArray(value)) {
|
|
337
|
-
this.fail(key, "must be an array of objects");
|
|
338
|
-
return [];
|
|
339
|
-
}
|
|
340
|
-
return value.flatMap((entry, index) => {
|
|
341
|
-
if (isRecord(entry)) return [new Reader(entry, this.child(`${key}[${index}]`), this.issues)];
|
|
342
|
-
this.fail(`${key}[${index}]`, "must be an object");
|
|
343
|
-
return [];
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
child(key) {
|
|
347
|
-
return this.path === "" ? key : key === "" ? this.path : `${this.path}.${key}`;
|
|
348
|
-
}
|
|
349
|
-
};
|
|
350
|
-
//#endregion
|
|
351
|
-
//#region src/host/read-document.ts
|
|
352
|
-
/** Authored documents are short; a larger file is not one. */
|
|
353
|
-
const MAX_DOCUMENT_BYTES = 1048576;
|
|
354
|
-
/**
|
|
355
|
-
* Read and parse one authored document. Every problem is added to `problems`
|
|
356
|
-
* with the file's path, and the document is skipped.
|
|
357
|
-
*/
|
|
358
|
-
async function readDocument(file, parse, problems) {
|
|
359
|
-
if ((await stat(file)).size > MAX_DOCUMENT_BYTES) {
|
|
360
|
-
problems.push({
|
|
361
|
-
path: file,
|
|
362
|
-
message: "file is too large"
|
|
363
|
-
});
|
|
364
|
-
return;
|
|
365
|
-
}
|
|
366
|
-
const result = parse(await readFile(file, "utf8"));
|
|
367
|
-
if (result.ok) return result.value;
|
|
368
|
-
for (const issue of result.issues) problems.push({
|
|
369
|
-
path: file,
|
|
370
|
-
message: formatIssue(issue)
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
//#endregion
|
|
374
|
-
//#region src/host/plots/load.ts
|
|
375
|
-
const PLOTS_FILE_NAME = "plots.json";
|
|
376
|
-
function development(reader, fallback) {
|
|
377
|
-
return {
|
|
378
|
-
id: reader.string("id") ?? fallback,
|
|
379
|
-
text: reader.string("text", { required: true }) ?? "",
|
|
380
|
-
mode: reader.oneOf("mode", PLOT_NUDGE_MODES, "opportunity"),
|
|
381
|
-
prerequisites: reader.strings("prerequisites"),
|
|
382
|
-
guidance: reader.text("guidance"),
|
|
383
|
-
avoid: reader.strings("avoid")
|
|
384
|
-
};
|
|
385
|
-
}
|
|
386
|
-
/** Parse the optional world-level list of dramatic possibilities. */
|
|
387
|
-
function parsePlotsFile(text) {
|
|
388
|
-
const issues = [];
|
|
389
|
-
const wrapper = Reader.fromJson(`{"plots":${text.replace(/^/, "")}}`, issues);
|
|
390
|
-
if (wrapper === void 0) return {
|
|
391
|
-
ok: false,
|
|
392
|
-
issues
|
|
393
|
-
};
|
|
394
|
-
const plots = wrapper.objects("plots").map((reader, plotIndex) => {
|
|
395
|
-
const pacing = reader.object("pacing");
|
|
396
|
-
const hooks = reader.objects("hooks").map((entry, index) => development(entry, `hook-${index + 1}`));
|
|
397
|
-
const developments = reader.objects("developments").map((entry, index) => development(entry, `development-${index + 1}`));
|
|
398
|
-
return {
|
|
399
|
-
id: reader.string("id", { required: true }) ?? `plot-${plotIndex + 1}`,
|
|
400
|
-
name: reader.string("name", { required: true }) ?? "",
|
|
401
|
-
premise: reader.string("premise", { required: true }) ?? "",
|
|
402
|
-
themes: reader.strings("themes"),
|
|
403
|
-
hooks,
|
|
404
|
-
developments,
|
|
405
|
-
stakes: reader.strings("stakes"),
|
|
406
|
-
resolutions: reader.strings("resolutions"),
|
|
407
|
-
participants: reader.strings("participants"),
|
|
408
|
-
start: reader.oneOf("start", PLOT_STATUSES, "available"),
|
|
409
|
-
pacing: {
|
|
410
|
-
quietTurns: pacing?.number("quiet_turns", {
|
|
411
|
-
min: 1,
|
|
412
|
-
max: 20
|
|
413
|
-
}) ?? pacing?.number("quietTurns", {
|
|
414
|
-
min: 1,
|
|
415
|
-
max: 20
|
|
416
|
-
}) ?? 3,
|
|
417
|
-
cooldownTurns: pacing?.number("cooldown_turns", {
|
|
418
|
-
min: 1,
|
|
419
|
-
max: 50
|
|
420
|
-
}) ?? pacing?.number("cooldownTurns", {
|
|
421
|
-
min: 1,
|
|
422
|
-
max: 50
|
|
423
|
-
}) ?? 4,
|
|
424
|
-
cooldownDays: pacing?.number("cooldown_days", {
|
|
425
|
-
min: 0,
|
|
426
|
-
max: 365
|
|
427
|
-
}) ?? pacing?.number("cooldownDays", {
|
|
428
|
-
min: 0,
|
|
429
|
-
max: 365
|
|
430
|
-
}) ?? 1
|
|
431
|
-
}
|
|
432
|
-
};
|
|
433
|
-
});
|
|
434
|
-
const ids = plots.map((plot) => plot.id);
|
|
435
|
-
ids.forEach((id, index) => {
|
|
436
|
-
if (ids.indexOf(id) !== index) issues.push({
|
|
437
|
-
path: `plots[${index}].id`,
|
|
438
|
-
message: `plot id "${id}" is defined twice`
|
|
439
|
-
});
|
|
440
|
-
});
|
|
441
|
-
for (const [plotIndex, plot] of plots.entries()) {
|
|
442
|
-
const steps = [...plot.hooks, ...plot.developments];
|
|
443
|
-
const stepIds = steps.map((step) => step.id);
|
|
444
|
-
stepIds.forEach((id, index) => {
|
|
445
|
-
if (stepIds.indexOf(id) !== index) issues.push({
|
|
446
|
-
path: `plots[${plotIndex}]`,
|
|
447
|
-
message: `development id "${id}" is defined twice`
|
|
448
|
-
});
|
|
449
|
-
});
|
|
450
|
-
for (const step of steps) for (const required of step.prerequisites) if (!stepIds.includes(required)) issues.push({
|
|
451
|
-
path: `plots[${plotIndex}]`,
|
|
452
|
-
message: `development "${step.id}" requires unknown development "${required}"`
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
return issues.length === 0 ? {
|
|
456
|
-
ok: true,
|
|
457
|
-
value: plots
|
|
458
|
-
} : {
|
|
459
|
-
ok: false,
|
|
460
|
-
issues
|
|
461
|
-
};
|
|
462
|
-
}
|
|
463
|
-
async function loadPlots(worldDir) {
|
|
464
|
-
const result = {
|
|
465
|
-
plots: [],
|
|
466
|
-
problems: []
|
|
467
|
-
};
|
|
468
|
-
const file = join(worldDir, PLOTS_FILE_NAME);
|
|
469
|
-
try {
|
|
470
|
-
result.plots = await readDocument(file, parsePlotsFile, result.problems) ?? [];
|
|
471
|
-
} catch (error) {
|
|
472
|
-
if (error.code !== "ENOENT") result.problems.push({
|
|
473
|
-
path: file,
|
|
474
|
-
message: errorMessage(error)
|
|
475
|
-
});
|
|
476
|
-
}
|
|
477
|
-
return result;
|
|
478
|
-
}
|
|
479
|
-
function plotReferenceProblems(plots, entities) {
|
|
480
|
-
const known = new Set(entities);
|
|
481
|
-
return plots.flatMap((plot) => plot.participants.filter((participant) => !known.has(participant)).map((participant) => `plot "${plot.id}" names unknown participant "${participant}"`));
|
|
482
|
-
}
|
|
483
|
-
//#endregion
|
|
484
|
-
export { ReflectionRefusedError as C, WorldNotFoundError as E, InvalidWorldError as S, SessionNotFoundError as T, shippedLanguages as _, readDocument as a, AgentUnavailableError as b, errorMessage as c, PromptLibrary as d, copyWorldPrompts as f, promptProblem as g, listFiles as h, plotReferenceProblems as i, LANGUAGE_FOLDER as l, languageOf as m, loadPlots as n, Reader as o, inheritPrompts as p, parsePlotsFile as r, formatIssue as s, PLOTS_FILE_NAME as t, PROMPTS_FOLDER as u, shippedPromptsDirectory as v, SessionArchivedError as w, InvalidStartError as x, shippedText as y };
|
|
485
|
-
|
|
486
|
-
//# sourceMappingURL=load-CVwJjIJ5.js.map
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { r as parseSettings, t as DEFAULT_SETTINGS } from "./settings-CA6icLL_.js";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { existsSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
4
|
-
//#region src/host/sessions/settings.ts
|
|
5
|
-
const SETTINGS_FILE = "settings.json";
|
|
6
|
-
const known = /* @__PURE__ */ new Map();
|
|
7
|
-
let childReasoning = "";
|
|
8
|
-
/** The thinking level helpers get when a chat's own setting is empty (the profile's choice); returns the function that takes it back. */
|
|
9
|
-
function useChildReasoning(level) {
|
|
10
|
-
childReasoning = level;
|
|
11
|
-
return () => {
|
|
12
|
-
childReasoning = "";
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
/** The settings are not acceptable; `problems` say what to fix. */
|
|
16
|
-
var InvalidSettingsError = class extends Error {
|
|
17
|
-
problems;
|
|
18
|
-
constructor(problems) {
|
|
19
|
-
super(`the settings are not acceptable: ${problems.join("; ")}`);
|
|
20
|
-
this.problems = problems;
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* The settings of the session in `folder`; a session that has none yet has the defaults.
|
|
25
|
-
* @throws {InvalidSettingsError} when the file exists and is not valid.
|
|
26
|
-
*/
|
|
27
|
-
function readSettings(folder) {
|
|
28
|
-
const file = join(folder, SETTINGS_FILE);
|
|
29
|
-
if (!existsSync(file)) return { ...DEFAULT_SETTINGS };
|
|
30
|
-
let value;
|
|
31
|
-
try {
|
|
32
|
-
value = JSON.parse(readFileSync(file, "utf8"));
|
|
33
|
-
} catch (error) {
|
|
34
|
-
throw new InvalidSettingsError([`${SETTINGS_FILE} is not valid JSON (${error instanceof Error ? error.message : String(error)})`]);
|
|
35
|
-
}
|
|
36
|
-
const parsed = parseSettings(value);
|
|
37
|
-
if (!parsed.ok) throw new InvalidSettingsError(parsed.problems);
|
|
38
|
-
return parsed.value;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Store the settings of the session in `folder`, replacing what was there.
|
|
42
|
-
* @throws {InvalidSettingsError} when the value is not acceptable; nothing is written then.
|
|
43
|
-
*/
|
|
44
|
-
function writeSettings(folder, value) {
|
|
45
|
-
const parsed = parseSettings(value);
|
|
46
|
-
if (!parsed.ok) throw new InvalidSettingsError(parsed.problems);
|
|
47
|
-
const file = join(folder, SETTINGS_FILE);
|
|
48
|
-
const temporary = `${file}.${process.pid}.tmp`;
|
|
49
|
-
writeFileSync(temporary, `${JSON.stringify(parsed.value, null, 2)}\n`);
|
|
50
|
-
renameSync(temporary, file);
|
|
51
|
-
known.delete(folder);
|
|
52
|
-
return parsed.value;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* The settings to use right now. Called before each use, so an edit to the file applies at the
|
|
56
|
-
* next round, step or run without a restart; the file is parsed again only when it changed.
|
|
57
|
-
* A file that is not valid is reported once through `problem` and the last good settings
|
|
58
|
-
* (the defaults when there were none) stay in force, so a typo cannot stop a story in the middle.
|
|
59
|
-
*/
|
|
60
|
-
function liveSettings(folder, problem = () => void 0) {
|
|
61
|
-
const value = loadedSettings(folder, problem);
|
|
62
|
-
return value.reasoning.children === "" && childReasoning !== "" ? {
|
|
63
|
-
...value,
|
|
64
|
-
reasoning: {
|
|
65
|
-
...value.reasoning,
|
|
66
|
-
children: childReasoning
|
|
67
|
-
}
|
|
68
|
-
} : value;
|
|
69
|
-
}
|
|
70
|
-
function loadedSettings(folder, problem) {
|
|
71
|
-
let modified = -1;
|
|
72
|
-
try {
|
|
73
|
-
modified = statSync(join(folder, SETTINGS_FILE)).mtimeMs;
|
|
74
|
-
} catch {}
|
|
75
|
-
const last = known.get(folder);
|
|
76
|
-
if (last?.modified === modified) return last.value;
|
|
77
|
-
try {
|
|
78
|
-
const value = readSettings(folder);
|
|
79
|
-
known.set(folder, {
|
|
80
|
-
modified,
|
|
81
|
-
value
|
|
82
|
-
});
|
|
83
|
-
return value;
|
|
84
|
-
} catch (error) {
|
|
85
|
-
if (!(error instanceof InvalidSettingsError)) throw error;
|
|
86
|
-
problem(`${error.message} (the last good settings stay in force)`);
|
|
87
|
-
const value = last?.value ?? { ...DEFAULT_SETTINGS };
|
|
88
|
-
known.set(folder, {
|
|
89
|
-
modified,
|
|
90
|
-
value
|
|
91
|
-
});
|
|
92
|
-
return value;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
//#endregion
|
|
96
|
-
//#region src/shared/narration.ts
|
|
97
|
-
const OPEN = "<roleplay-output>";
|
|
98
|
-
const CLOSE = "</roleplay-output>";
|
|
99
|
-
const PREVIEW_OPEN = "<roleplay-preview>";
|
|
100
|
-
const PREVIEW_CLOSE = "</roleplay-preview>";
|
|
101
|
-
/** What the game master shows the player while the characters are still being asked: a scene-setting opening that the delivered output replaces in the view. */
|
|
102
|
-
function encodePreview(text) {
|
|
103
|
-
return `${PREVIEW_OPEN}${JSON.stringify({ text })}${PREVIEW_CLOSE}`;
|
|
104
|
-
}
|
|
105
|
-
function encodeOutput(output) {
|
|
106
|
-
return `${OPEN}${JSON.stringify(output)}${CLOSE}`;
|
|
107
|
-
}
|
|
108
|
-
function isSegment(value) {
|
|
109
|
-
if (typeof value !== "object" || value === null) return false;
|
|
110
|
-
const segment = value;
|
|
111
|
-
if (segment["type"] === "narration") return typeof segment["text"] === "string";
|
|
112
|
-
return segment["type"] === "utterance" && typeof segment["character"] === "string" && typeof segment["name"] === "string" && typeof segment["text"] === "string";
|
|
113
|
-
}
|
|
114
|
-
/** The output encoded in a tool result's text, or nothing when the text carries none. */
|
|
115
|
-
function decodeOutput(text) {
|
|
116
|
-
const start = text.indexOf(OPEN);
|
|
117
|
-
const end = text.indexOf(CLOSE, start + 17);
|
|
118
|
-
if (start < 0 || end < 0) return void 0;
|
|
119
|
-
try {
|
|
120
|
-
const value = JSON.parse(text.slice(start + 17, end));
|
|
121
|
-
const segments = value["segments"];
|
|
122
|
-
if (!Array.isArray(segments) || !segments.every(isSegment) || typeof value["repaired"] !== "boolean") return void 0;
|
|
123
|
-
const ooc = value["ooc"];
|
|
124
|
-
const ruled = typeof ooc === "object" && ooc !== null && typeof ooc["legal"] === "boolean" && typeof ooc["note"] === "string" ? ooc : void 0;
|
|
125
|
-
const timelineNode = typeof value["timelineNode"] === "string" ? value["timelineNode"] : void 0;
|
|
126
|
-
const rolls = Array.isArray(value["rolls"]) ? value["rolls"].filter((entry) => typeof entry === "object" && entry !== null && typeof entry.id === "number" && typeof entry.text === "string" && (entry.by === "agent" || entry.by === "player")) : [];
|
|
127
|
-
return {
|
|
128
|
-
segments,
|
|
129
|
-
repaired: value["repaired"],
|
|
130
|
-
...ruled === void 0 ? {} : { ooc: ruled },
|
|
131
|
-
...rolls.length === 0 ? {} : { rolls },
|
|
132
|
-
...timelineNode === void 0 ? {} : { timelineNode }
|
|
133
|
-
};
|
|
134
|
-
} catch {
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
//#endregion
|
|
139
|
-
export { liveSettings as a, writeSettings as c, InvalidSettingsError as i, encodeOutput as n, readSettings as o, encodePreview as r, useChildReasoning as s, decodeOutput as t };
|
|
140
|
-
|
|
141
|
-
//# sourceMappingURL=narration-DK2kiIjE.js.map
|