@dsh-agentic-roleplay/core 0.3.3 → 0.3.5
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 +7 -256
- package/lib/{child-lPncV1ps.js → child-CFjnaTZt.js} +62 -7
- package/lib/client.js +10950 -7439
- package/lib/client.js.map +1 -1
- package/lib/{deliver-De62a8i9.js → deliver-Cl6bjp7g.js} +49 -11
- package/lib/doctor.js +83 -7
- package/lib/{gallery-CtFDL22W.js → gallery-DIEgETzU.js} +2 -2
- package/lib/generator-DJ48HxPI.js +145 -0
- package/lib/{hooks-Ka22sFNy.js → hooks-DdO_tNhu.js} +58 -17
- package/lib/images.js +4 -3
- package/lib/index.js +1761 -71
- package/lib/language-BgyxtB2h.js +38 -0
- package/lib/{generator-B6FxlKOV.js → library-9JB9zw5a.js} +2 -143
- package/lib/{language-DWVwi1R7.js → load-Clkl0T7C.js} +6 -40
- package/lib/orchestration.js +23 -26
- package/lib/pack-jail-ClMpVc__.js +1979 -0
- package/lib/{packs-BYjrAen6.js → packs-BC2A3geF.js} +3 -29
- package/lib/{presenter-agent-Dt21K96b.js → presenter-agent-DnNEG_Mn.js} +5 -4
- package/lib/{documents-Do4lz4SB.js → preset-ClQm7KPQ.js} +67 -1656
- package/lib/preset-author-tools-Dibp-LY9.js +270 -0
- package/lib/preset-authoring.js +53 -0
- package/lib/presets-DNjNxQL5.js +335 -0
- package/lib/{reflection-md7BCYCl.js → reflection-Bju_LPFn.js} +131 -9
- package/package.json +3 -2
- package/prompts/en/doctor/procedure.md +1 -1
- package/prompts/en/import/character.md +1 -1
- package/prompts/en/preset/persona.md +1 -0
- package/prompts/en/preset/procedure.md +1 -0
- package/prompts/en/preset/welcome.md +1 -0
- package/prompts/en/roleplay/record-keeper.md +2 -2
- package/prompts/zh/doctor/procedure.md +1 -1
- package/prompts/zh/import/character.md +1 -1
- package/prompts/zh/preset/persona.md +1 -0
- package/prompts/zh/preset/procedure.md +1 -0
- package/prompts/zh/preset/welcome.md +1 -0
- package/prompts/zh/roleplay/record-keeper.md +2 -2
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { G as extractStyleFrom, K as withPlayer, n as contextOf, st as IMPORT_FOLDER, t as readPreset } from "./preset-ClQm7KPQ.js";
|
|
2
|
+
import { _ as writeWorldPrompt, c as readPreset$1, f as updatePresetInfo, g as readWorldPrompt, m as deleteWorldPrompt, p as PROSE_PROMPTS, s as presetDirectory, t as PRESET_FILE } from "./presets-DNjNxQL5.js";
|
|
3
|
+
import { _ as shippedLanguages, m as languageOf, u as PROMPTS_FOLDER, v as shippedPromptsDirectory } from "./load-Clkl0T7C.js";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
7
|
+
import { readFile, rm } from "node:fs/promises";
|
|
8
|
+
//#region src/shared/preset-chat.ts
|
|
9
|
+
const PREFIX = "preset-author-";
|
|
10
|
+
/** Stable DSH chat identity for the authoring conversation of one library preset. */
|
|
11
|
+
function presetChatSessionId(name) {
|
|
12
|
+
if (name === "") throw new TypeError("preset name cannot be empty");
|
|
13
|
+
return `${PREFIX}${encodeURIComponent(name)}`;
|
|
14
|
+
}
|
|
15
|
+
/** The library preset named by a dedicated preset authoring chat. */
|
|
16
|
+
function presetOfChat(chatSessionId) {
|
|
17
|
+
if (!chatSessionId.startsWith(PREFIX)) return void 0;
|
|
18
|
+
try {
|
|
19
|
+
const name = decodeURIComponent(chatSessionId.slice(14));
|
|
20
|
+
return name === "" ? void 0 : name;
|
|
21
|
+
} catch {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/host/authoring/preset-author-tools.ts
|
|
27
|
+
const PRESET_MUTATION_TOOLS = [
|
|
28
|
+
"write_preset_prompt",
|
|
29
|
+
"delete_preset_prompt",
|
|
30
|
+
"write_preset_info",
|
|
31
|
+
"import_narration_preset"
|
|
32
|
+
];
|
|
33
|
+
const NARRATION_IMPORT_TOOL = "import_narration_preset";
|
|
34
|
+
const UPLOAD_PATH = `${IMPORT_FOLDER}/${PRESET_FILE}`;
|
|
35
|
+
/** The command that has a preset's authoring chat turn an uploaded SillyTavern preset into prompts. */
|
|
36
|
+
const narrationImportMessage = () => `Turn the SillyTavern preset I uploaded into this preset's prompts: call ${NARRATION_IMPORT_TOOL} with file ${UPLOAD_PATH}.`;
|
|
37
|
+
const output = (value) => [{
|
|
38
|
+
type: "text",
|
|
39
|
+
text: JSON.stringify(value, null, 2)
|
|
40
|
+
}];
|
|
41
|
+
const jsonOutput = {
|
|
42
|
+
schema: { type: "json" },
|
|
43
|
+
render: (_args, value) => output(value)
|
|
44
|
+
};
|
|
45
|
+
const json = (value) => JSON.parse(JSON.stringify(value));
|
|
46
|
+
const idParameter = {
|
|
47
|
+
type: "string",
|
|
48
|
+
required: true,
|
|
49
|
+
enum: [...PROSE_PROMPTS],
|
|
50
|
+
description: "The prompt: roleplay/persona, roleplay/style or roleplay/character-style."
|
|
51
|
+
};
|
|
52
|
+
const languageParameter = {
|
|
53
|
+
type: "string",
|
|
54
|
+
required: true,
|
|
55
|
+
description: "The language folder, such as en or zh."
|
|
56
|
+
};
|
|
57
|
+
/** Resolves every call from its preset authoring chat, so a tool can only reach the preset the chat is about. */
|
|
58
|
+
var PresetReader = class {
|
|
59
|
+
root;
|
|
60
|
+
constructor(root) {
|
|
61
|
+
this.root = root;
|
|
62
|
+
}
|
|
63
|
+
directory(exec) {
|
|
64
|
+
const chat = exec.agent?.session.id;
|
|
65
|
+
const name = chat === void 0 ? void 0 : presetOfChat(chat);
|
|
66
|
+
if (name === void 0) throw new Error("this tool is available only in a preset authoring chat");
|
|
67
|
+
const directory = presetDirectory(this.root(), name);
|
|
68
|
+
if (!existsSync(directory)) throw new Error(`the preset "${name}" no longer exists`);
|
|
69
|
+
return {
|
|
70
|
+
name,
|
|
71
|
+
directory
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
get library() {
|
|
75
|
+
return this.root();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
/** Read and write the prompts and the information of the preset the chat is about. The files are those a story start copies, so they are checked as it checks them. */
|
|
79
|
+
function presetAuthorTools(reader, settings, prompting, shipped = shippedPromptsDirectory()) {
|
|
80
|
+
return [
|
|
81
|
+
defineTool({
|
|
82
|
+
name: "read_preset_info",
|
|
83
|
+
description: "Read the preset's title, description, suggested temperatures, the languages it has prompts in and any problems it has.",
|
|
84
|
+
parameters: {},
|
|
85
|
+
output: jsonOutput,
|
|
86
|
+
isConcurrencySafe: () => true,
|
|
87
|
+
async execute(_args, exec) {
|
|
88
|
+
const { name, directory } = reader.directory(exec);
|
|
89
|
+
return json(readPreset$1(directory, name, shipped).summary);
|
|
90
|
+
}
|
|
91
|
+
}),
|
|
92
|
+
defineTool({
|
|
93
|
+
name: "list_preset_prompts",
|
|
94
|
+
description: "List the three prose prompts a preset can carry, in a language, and whether this preset carries its own version of each.",
|
|
95
|
+
parameters: { language: languageParameter },
|
|
96
|
+
output: jsonOutput,
|
|
97
|
+
isConcurrencySafe: () => true,
|
|
98
|
+
async execute(args, exec) {
|
|
99
|
+
const { directory } = reader.directory(exec);
|
|
100
|
+
const tag = languageOf(args.language);
|
|
101
|
+
return json(PROSE_PROMPTS.map((id) => ({
|
|
102
|
+
id,
|
|
103
|
+
carried: existsSync(join(directory, PROMPTS_FOLDER, tag, `${id}.md`))
|
|
104
|
+
})));
|
|
105
|
+
}
|
|
106
|
+
}),
|
|
107
|
+
defineTool({
|
|
108
|
+
name: "read_preset_prompt",
|
|
109
|
+
description: "Read a prompt of the preset. When the preset does not carry it in that language, the shipped text is returned (carried: false), which is what a story would use and a good starting point. ${name} marks a value the program fills in.",
|
|
110
|
+
parameters: {
|
|
111
|
+
id: idParameter,
|
|
112
|
+
language: languageParameter
|
|
113
|
+
},
|
|
114
|
+
output: jsonOutput,
|
|
115
|
+
isConcurrencySafe: () => true,
|
|
116
|
+
async execute(args, exec) {
|
|
117
|
+
const { directory } = reader.directory(exec);
|
|
118
|
+
return json(readWorldPrompt(directory, args.language, args.id, shipped));
|
|
119
|
+
}
|
|
120
|
+
}),
|
|
121
|
+
defineTool({
|
|
122
|
+
name: "write_preset_prompt",
|
|
123
|
+
description: "Create or replace the preset's version of a prompt in a language. It must not be empty and may use only the ${variables} the shipped prompt uses; tool names and field names in it must stay as they are. Read it first and change only what is needed. Requires user approval.",
|
|
124
|
+
parameters: {
|
|
125
|
+
id: idParameter,
|
|
126
|
+
language: languageParameter,
|
|
127
|
+
text: {
|
|
128
|
+
type: "string",
|
|
129
|
+
required: true,
|
|
130
|
+
description: "The complete prompt text."
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
output: jsonOutput,
|
|
134
|
+
async execute(args, exec) {
|
|
135
|
+
const { directory } = reader.directory(exec);
|
|
136
|
+
if (!PROSE_PROMPTS.includes(args.id)) throw new Error(`a preset carries only ${PROSE_PROMPTS.join(", ")}`);
|
|
137
|
+
await writeWorldPrompt(directory, args.language, args.id, args.text, shipped);
|
|
138
|
+
return json({
|
|
139
|
+
action: "written",
|
|
140
|
+
id: args.id,
|
|
141
|
+
language: languageOf(args.language)
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}),
|
|
145
|
+
defineTool({
|
|
146
|
+
name: "delete_preset_prompt",
|
|
147
|
+
description: "Remove the preset's version of a prompt in a language; stories started with the preset then use the shipped one. The file is moved into the preset's .trash. Requires user approval.",
|
|
148
|
+
parameters: {
|
|
149
|
+
id: idParameter,
|
|
150
|
+
language: languageParameter
|
|
151
|
+
},
|
|
152
|
+
output: jsonOutput,
|
|
153
|
+
async execute(args, exec) {
|
|
154
|
+
const { directory } = reader.directory(exec);
|
|
155
|
+
const trashPath = await deleteWorldPrompt(directory, args.language, args.id, shipped);
|
|
156
|
+
return json({
|
|
157
|
+
action: "deleted",
|
|
158
|
+
id: args.id,
|
|
159
|
+
language: languageOf(args.language),
|
|
160
|
+
trashPath
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}),
|
|
164
|
+
defineTool({
|
|
165
|
+
name: "write_preset_info",
|
|
166
|
+
description: "Change the preset's title, description (up to 300 characters each) and suggested temperatures. Give only what changes; sampling replaces the suggested temperatures as a whole ({} suggests none) and has only master (the narrator) and characters, each from 0 to 2. Requires user approval.",
|
|
167
|
+
parameters: {
|
|
168
|
+
title: {
|
|
169
|
+
type: "string",
|
|
170
|
+
description: "A short name to show; empty to clear."
|
|
171
|
+
},
|
|
172
|
+
description: {
|
|
173
|
+
type: "string",
|
|
174
|
+
description: "What the preset is for; empty to clear."
|
|
175
|
+
},
|
|
176
|
+
sampling: {
|
|
177
|
+
type: "json",
|
|
178
|
+
description: "For example {\"master\": 0.9, \"characters\": 0.85}."
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
output: jsonOutput,
|
|
182
|
+
async execute(args, exec) {
|
|
183
|
+
const { name } = reader.directory(exec);
|
|
184
|
+
const sampling = args.sampling;
|
|
185
|
+
if (sampling !== void 0 && (typeof sampling !== "object" || sampling === null || Array.isArray(sampling))) throw new Error("\"sampling\" must be an object");
|
|
186
|
+
const loaded = await updatePresetInfo(reader.library, name, {
|
|
187
|
+
...args.title === void 0 ? {} : { title: args.title },
|
|
188
|
+
...args.description === void 0 ? {} : { description: args.description },
|
|
189
|
+
...sampling === void 0 ? {} : { sampling }
|
|
190
|
+
});
|
|
191
|
+
return json({
|
|
192
|
+
action: "written",
|
|
193
|
+
summary: loaded.summary
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}),
|
|
197
|
+
defineTool({
|
|
198
|
+
name: NARRATION_IMPORT_TOOL,
|
|
199
|
+
description: `Turn the SillyTavern preset the person uploaded (${UPLOAD_PATH}) into this preset's prose prompts, in every language the program ships. Rules of the game, formats and scripts in the uploaded preset are not carried. It replaces this preset's versions of those prompts, so the person is asked first unless the preset has none yet.`,
|
|
200
|
+
parameters: { file: {
|
|
201
|
+
type: "string",
|
|
202
|
+
required: true,
|
|
203
|
+
description: `The uploaded preset: ${UPLOAD_PATH}.`
|
|
204
|
+
} },
|
|
205
|
+
output: jsonOutput,
|
|
206
|
+
isConcurrencySafe: () => false,
|
|
207
|
+
async execute(args, exec) {
|
|
208
|
+
if (args.file.trim() !== UPLOAD_PATH) throw new Error(`the uploaded preset is ${UPLOAD_PATH}`);
|
|
209
|
+
const { name, directory } = reader.directory(exec);
|
|
210
|
+
const file = join(directory, IMPORT_FOLDER, PRESET_FILE);
|
|
211
|
+
const uploaded = readPreset(await readFile(file).catch(() => {
|
|
212
|
+
throw new Error("no preset was uploaded here");
|
|
213
|
+
}), name);
|
|
214
|
+
const language = prompting.languages().get(`preset:${name}`);
|
|
215
|
+
const text = (id, variables) => prompting.prompts().text(id, language, variables);
|
|
216
|
+
const made = await extractStyleFrom(contextOf(exec, settings(), text), uploaded.name, uploaded.pieces);
|
|
217
|
+
for (const { id, text: body } of made.prompts) for (const tag of shippedLanguages(shipped)) await writeWorldPrompt(directory, tag, id, withPlayer(body), shipped);
|
|
218
|
+
await rm(join(directory, IMPORT_FOLDER), {
|
|
219
|
+
recursive: true,
|
|
220
|
+
force: true
|
|
221
|
+
});
|
|
222
|
+
const written = made.prompts.map((entry) => entry.id);
|
|
223
|
+
return json({
|
|
224
|
+
written,
|
|
225
|
+
notes: made.notes,
|
|
226
|
+
next: written.length === 0 ? "Nothing in the uploaded preset was about how to write the story, so no prompt was written: tell the person." : "Tell the person which prompts were written (they can read and edit them here or in the Presets page) and what was not carried over."
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
];
|
|
231
|
+
}
|
|
232
|
+
/** Every change to a preset is put to the person; the first import into an empty preset only fills it, so it is not. */
|
|
233
|
+
async function presetApproval(exec, next, carries) {
|
|
234
|
+
if (!PRESET_MUTATION_TOOLS.includes(exec.name) || exec.agent === void 0 || presetOfChat(exec.agent.session.id) === void 0) return await next();
|
|
235
|
+
const args = exec.arguments;
|
|
236
|
+
const chat = exec.agent.session.id;
|
|
237
|
+
if (exec.name === "import_narration_preset") {
|
|
238
|
+
if (!carries(chat)) return await next();
|
|
239
|
+
return {
|
|
240
|
+
kind: "ask",
|
|
241
|
+
reason: "replace the preset's prompts with ones made from the uploaded preset",
|
|
242
|
+
displayReason: {
|
|
243
|
+
en: "Replace this preset's prompts with ones made from the uploaded preset?",
|
|
244
|
+
zh: "用上传的预设生成的提示词,替换此预设的提示词?"
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
if (exec.name === "write_preset_info") return {
|
|
249
|
+
kind: "ask",
|
|
250
|
+
reason: "change the preset's title, description or suggested temperatures",
|
|
251
|
+
displayReason: {
|
|
252
|
+
en: "Change the preset's title, description or temperatures?",
|
|
253
|
+
zh: "修改预设的标题、描述或建议温度?"
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
const target = `prompt “${String(args["language"])}/${String(args["id"])}”`;
|
|
257
|
+
const remove = exec.name.startsWith("delete");
|
|
258
|
+
return {
|
|
259
|
+
kind: "ask",
|
|
260
|
+
reason: `${remove ? "delete" : "write"} preset ${target}`,
|
|
261
|
+
displayReason: {
|
|
262
|
+
en: `${remove ? "Delete" : "Write"} ${target} in the preset?`,
|
|
263
|
+
zh: `${remove ? "删除" : "写入"}预设中的${target}?`
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
//#endregion
|
|
268
|
+
export { presetChatSessionId as a, presetAuthorTools as i, narrationImportMessage as n, presetOfChat as o, presetApproval as r, PresetReader as t };
|
|
269
|
+
|
|
270
|
+
//# sourceMappingURL=preset-author-tools-Dibp-LY9.js.map
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { s as presetDirectory } from "./presets-DNjNxQL5.js";
|
|
2
|
+
import "./load-Clkl0T7C.js";
|
|
3
|
+
import { i as presetAuthorTools, o as presetOfChat, r as presetApproval, t as PresetReader } from "./preset-author-tools-Dibp-LY9.js";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
//#region src/preset-authoring.ts
|
|
7
|
+
const name = "@dsh-agentic-roleplay/core/preset-authoring";
|
|
8
|
+
const inject = [
|
|
9
|
+
"roleplayRoot",
|
|
10
|
+
"roleplayImport",
|
|
11
|
+
"roleplayPrompts",
|
|
12
|
+
"roleplayAuthoringLanguages",
|
|
13
|
+
"tools",
|
|
14
|
+
"systemPrompt"
|
|
15
|
+
];
|
|
16
|
+
/** The preset authoring chat: reads and writes the prose prompts of one library preset, each write approved by the person. */
|
|
17
|
+
function apply(ctx) {
|
|
18
|
+
const library = () => ctx.roleplayRoot.path;
|
|
19
|
+
const reader = new PresetReader(library);
|
|
20
|
+
const prompting = {
|
|
21
|
+
prompts: () => ctx.roleplayPrompts,
|
|
22
|
+
languages: () => ctx.roleplayAuthoringLanguages
|
|
23
|
+
};
|
|
24
|
+
for (const tool of presetAuthorTools(reader, () => ctx.roleplayImport.settings, prompting)) ctx.tools.register(tool);
|
|
25
|
+
const carries = (chat) => {
|
|
26
|
+
const preset = presetOfChat(chat);
|
|
27
|
+
return preset !== void 0 && existsSync(join(presetDirectory(library(), preset), "prompts"));
|
|
28
|
+
};
|
|
29
|
+
ctx.on("tools/pre-execute", (exec, next) => presetApproval(exec, next, carries));
|
|
30
|
+
const languageOf = (context) => {
|
|
31
|
+
const chat = context.agent?.session.id;
|
|
32
|
+
const preset = chat === void 0 ? void 0 : presetOfChat(chat);
|
|
33
|
+
return preset === void 0 ? "en" : ctx.roleplayAuthoringLanguages.get(`preset:${preset}`);
|
|
34
|
+
};
|
|
35
|
+
for (const [section, order, id] of [[
|
|
36
|
+
"PRESET_AUTHORING_PERSONA",
|
|
37
|
+
ctx.systemPrompt.getSectionOrder("DEPLOYMENT_PERSONA_PREFIX"),
|
|
38
|
+
"preset/persona"
|
|
39
|
+
], [
|
|
40
|
+
"PRESET_AUTHORING_PROCEDURE",
|
|
41
|
+
ctx.systemPrompt.getSectionOrder("DEPLOYMENT_PERSONA_SUFFIX"),
|
|
42
|
+
"preset/procedure"
|
|
43
|
+
]]) ctx.systemPrompt.section({
|
|
44
|
+
name: section,
|
|
45
|
+
order,
|
|
46
|
+
interpolate: false,
|
|
47
|
+
text: (context) => ctx.roleplayPrompts.text(id, languageOf(context))
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
export { apply, inject, name };
|
|
52
|
+
|
|
53
|
+
//# sourceMappingURL=preset-authoring.js.map
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { _ as shippedLanguages, g as promptProblem, h as listFiles, l as LANGUAGE_FOLDER, m as languageOf, u as PROMPTS_FOLDER, v as shippedPromptsDirectory } from "./load-Clkl0T7C.js";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
import { mkdir, rename, rm, writeFile } from "node:fs/promises";
|
|
6
|
+
//#region ../dsh-roleplay-model/src/intent.ts
|
|
7
|
+
const INTENT_TYPES = [
|
|
8
|
+
"need",
|
|
9
|
+
"obligation",
|
|
10
|
+
"quest",
|
|
11
|
+
"agenda",
|
|
12
|
+
"aspiration",
|
|
13
|
+
"relationship",
|
|
14
|
+
"habit",
|
|
15
|
+
"reaction"
|
|
16
|
+
];
|
|
17
|
+
const INTENT_STATUSES = [
|
|
18
|
+
"active",
|
|
19
|
+
"paused",
|
|
20
|
+
"completed",
|
|
21
|
+
"failed",
|
|
22
|
+
"abandoned"
|
|
23
|
+
];
|
|
24
|
+
const INTENT_HORIZONS = [
|
|
25
|
+
"immediate",
|
|
26
|
+
"short",
|
|
27
|
+
"day",
|
|
28
|
+
"long",
|
|
29
|
+
"open_ended"
|
|
30
|
+
];
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/host/prompts/world-prompts.ts
|
|
33
|
+
/** The prompts that decide how the story is written and who the models are (prose and persona); every other prompt holds rules of the game. */
|
|
34
|
+
const PROSE_PROMPTS = [
|
|
35
|
+
"roleplay/persona",
|
|
36
|
+
"roleplay/style",
|
|
37
|
+
"roleplay/character-style"
|
|
38
|
+
];
|
|
39
|
+
function shippedFile(shipped, language, id) {
|
|
40
|
+
return [language, "en"].map((name) => join(shipped, name, `${id}.md`)).find((path) => existsSync(path));
|
|
41
|
+
}
|
|
42
|
+
function fileOf(directory, language, id, shipped) {
|
|
43
|
+
const problem = promptProblem(language, id, "x", shipped);
|
|
44
|
+
if (!id.startsWith("roleplay/")) throw new Error(`"${id}" is not a prompt of the story; the ones that can be carried are listed by list_world_prompts`);
|
|
45
|
+
if (problem !== void 0 && problem.startsWith("there is no prompt")) throw new Error(problem);
|
|
46
|
+
return join(directory, PROMPTS_FOLDER, language, `${id}.md`);
|
|
47
|
+
}
|
|
48
|
+
/** Every prompt of the story, in a language, and whether the world carries its own version. */
|
|
49
|
+
function listWorldPrompts(directory, language, shipped = shippedPromptsDirectory()) {
|
|
50
|
+
const tag = languageOf(language);
|
|
51
|
+
return readdirSync(join(shipped, "en"), { withFileTypes: true }).filter((entry) => entry.isDirectory() && ![
|
|
52
|
+
"import",
|
|
53
|
+
"world",
|
|
54
|
+
"preset"
|
|
55
|
+
].includes(entry.name)).flatMap((entry) => listFiles(join(shipped, "en", entry.name), `${entry.name}/`)).sort().map((id) => ({
|
|
56
|
+
id,
|
|
57
|
+
kind: PROSE_PROMPTS.includes(id) ? "prose" : "rules",
|
|
58
|
+
carried: existsSync(join(directory, PROMPTS_FOLDER, tag, `${id}.md`))
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
/** A prompt as sessions of the world get it: the world's own version, else the shipped text. */
|
|
62
|
+
function readWorldPrompt(directory, language, id, shipped = shippedPromptsDirectory()) {
|
|
63
|
+
const tag = languageOf(language);
|
|
64
|
+
const own = fileOf(directory, tag, id, shipped);
|
|
65
|
+
if (existsSync(own)) return {
|
|
66
|
+
id,
|
|
67
|
+
language: tag,
|
|
68
|
+
carried: true,
|
|
69
|
+
text: readFileSync(own, "utf8")
|
|
70
|
+
};
|
|
71
|
+
const original = shippedFile(shipped, tag, id);
|
|
72
|
+
return {
|
|
73
|
+
id,
|
|
74
|
+
language: tag,
|
|
75
|
+
carried: false,
|
|
76
|
+
text: original === void 0 ? "" : readFileSync(original, "utf8")
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/** @throws {Error} when the text is not acceptable for the prompt: see `promptProblem`. */
|
|
80
|
+
async function writeWorldPrompt(directory, language, id, text, shipped = shippedPromptsDirectory()) {
|
|
81
|
+
const tag = languageOf(language);
|
|
82
|
+
const file = fileOf(directory, tag, id, shipped);
|
|
83
|
+
const problem = promptProblem(tag, id, text, shipped);
|
|
84
|
+
if (problem !== void 0) throw new Error(problem);
|
|
85
|
+
await mkdir(dirname(file), { recursive: true });
|
|
86
|
+
const temporary = `${file}.tmp`;
|
|
87
|
+
await writeFile(temporary, text.endsWith("\n") ? text : `${text}\n`);
|
|
88
|
+
await rename(temporary, file);
|
|
89
|
+
}
|
|
90
|
+
/** Moves the world's own version into its `.trash`, so sessions use the shipped one again. @returns where it went. */
|
|
91
|
+
async function deleteWorldPrompt(directory, language, id, shipped = shippedPromptsDirectory()) {
|
|
92
|
+
const tag = languageOf(language);
|
|
93
|
+
const file = fileOf(directory, tag, id, shipped);
|
|
94
|
+
if (!existsSync(file)) throw new Error(`the world carries no prompt "${id}" in ${tag}`);
|
|
95
|
+
const aside = join(directory, ".trash", PROMPTS_FOLDER, tag, `${id}.${Date.now()}.md`);
|
|
96
|
+
await mkdir(dirname(aside), { recursive: true });
|
|
97
|
+
await rename(file, aside);
|
|
98
|
+
await rm(dirname(file), { recursive: false }).catch(() => void 0);
|
|
99
|
+
return aside;
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/host/presets/presets.ts
|
|
103
|
+
const PRESETS_FOLDER = "presets";
|
|
104
|
+
const PRESET_FILE = "preset.json";
|
|
105
|
+
const NAME = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
|
|
106
|
+
const MAX_TEXT = 300;
|
|
107
|
+
const SAMPLING_KEYS = ["master", "characters"];
|
|
108
|
+
const isPresetName = (name) => NAME.test(name);
|
|
109
|
+
const presetsDirectory = (root) => join(root, PRESETS_FOLDER);
|
|
110
|
+
function presetDirectory(root, name) {
|
|
111
|
+
if (!isPresetName(name)) throw new Error(`"${name}" is not a usable preset name (letters, digits, "-" and "_", up to 64)`);
|
|
112
|
+
return join(presetsDirectory(root), name);
|
|
113
|
+
}
|
|
114
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
115
|
+
function parseHeader(value, problems) {
|
|
116
|
+
if (!isRecord(value)) {
|
|
117
|
+
problems.push(`${PRESET_FILE} must be an object`);
|
|
118
|
+
return { sampling: {} };
|
|
119
|
+
}
|
|
120
|
+
const text = (key) => {
|
|
121
|
+
const field = value[key];
|
|
122
|
+
if (field === void 0 || field === null) return void 0;
|
|
123
|
+
if (typeof field !== "string" || field.length > MAX_TEXT) {
|
|
124
|
+
problems.push(`${PRESET_FILE}: "${key}" must be text of at most ${MAX_TEXT} characters`);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
return field.trim() || void 0;
|
|
128
|
+
};
|
|
129
|
+
const sampling = {};
|
|
130
|
+
const given = value["sampling"];
|
|
131
|
+
if (given !== void 0 && given !== null) {
|
|
132
|
+
if (!isRecord(given)) problems.push(`${PRESET_FILE}: "sampling" must be an object`);
|
|
133
|
+
else {
|
|
134
|
+
for (const key of Object.keys(given)) if (!SAMPLING_KEYS.includes(key)) problems.push(`${PRESET_FILE}: sampling.${key} is not something a preset sets (${SAMPLING_KEYS.join(", ")})`);
|
|
135
|
+
for (const key of SAMPLING_KEYS) {
|
|
136
|
+
const number = given[key];
|
|
137
|
+
if (number === void 0) continue;
|
|
138
|
+
if (typeof number !== "number" || !Number.isFinite(number) || number < 0 || number > 2) problems.push(`${PRESET_FILE}: sampling.${key} must be a number from 0 to 2`);
|
|
139
|
+
else sampling[key] = number;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
const title = text("title");
|
|
144
|
+
const description = text("description");
|
|
145
|
+
return {
|
|
146
|
+
...title === void 0 ? {} : { title },
|
|
147
|
+
...description === void 0 ? {} : { description },
|
|
148
|
+
sampling
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function readHeader(directory, problems) {
|
|
152
|
+
const file = join(directory, PRESET_FILE);
|
|
153
|
+
if (!existsSync(file)) return { sampling: {} };
|
|
154
|
+
let value;
|
|
155
|
+
try {
|
|
156
|
+
value = JSON.parse(readFileSync(file, "utf8"));
|
|
157
|
+
} catch {
|
|
158
|
+
problems.push(`${PRESET_FILE} is not valid JSON`);
|
|
159
|
+
return { sampling: {} };
|
|
160
|
+
}
|
|
161
|
+
return parseHeader(value, problems);
|
|
162
|
+
}
|
|
163
|
+
/** Reads one preset folder, collecting every problem instead of stopping at the first. @throws {Error} when the folder does not exist. */
|
|
164
|
+
function readPreset(directory, name, shipped = shippedPromptsDirectory()) {
|
|
165
|
+
if (!existsSync(directory)) throw new Error(`there is no preset "${name}"`);
|
|
166
|
+
const problems = [];
|
|
167
|
+
const header = readHeader(directory, problems);
|
|
168
|
+
const files = /* @__PURE__ */ new Map();
|
|
169
|
+
const base = join(directory, PROMPTS_FOLDER);
|
|
170
|
+
if (existsSync(base)) for (const entry of readdirSync(base, { withFileTypes: true })) {
|
|
171
|
+
if (!entry.isDirectory() || !LANGUAGE_FOLDER.test(entry.name)) {
|
|
172
|
+
problems.push(`prompts/${entry.name}: prompts are kept in a folder named by the language, such as en or zh`);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
for (const id of listFiles(join(base, entry.name))) {
|
|
176
|
+
const path = `${entry.name}/${id}.md`;
|
|
177
|
+
const text = readFileSync(join(base, path), "utf8");
|
|
178
|
+
const problem = !PROSE_PROMPTS.includes(id) ? `"${id}" is not a prose prompt (${PROSE_PROMPTS.join(", ")}); the rules of the game belong to the world` : promptProblem(entry.name, id, text, shipped);
|
|
179
|
+
if (problem !== void 0) problems.push(`prompts/${path}: ${problem}`);
|
|
180
|
+
else files.set(path, text);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (files.size === 0 && problems.length === 0) problems.push("the preset carries no prompts");
|
|
184
|
+
const paths = [...files.keys()];
|
|
185
|
+
return {
|
|
186
|
+
summary: {
|
|
187
|
+
name,
|
|
188
|
+
title: header.title ?? name,
|
|
189
|
+
description: header.description ?? "",
|
|
190
|
+
languages: [...new Set(paths.map((path) => path.split("/")[0]))].sort(),
|
|
191
|
+
prompts: [...new Set(paths.map((path) => path.slice(path.indexOf("/") + 1, -3)))].sort(),
|
|
192
|
+
sampling: header.sampling,
|
|
193
|
+
problems
|
|
194
|
+
},
|
|
195
|
+
files
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/** Every preset in the library; one that cannot be read is listed with its problems. */
|
|
199
|
+
function listPresets(root) {
|
|
200
|
+
const folder = presetsDirectory(root);
|
|
201
|
+
if (!existsSync(folder)) return [];
|
|
202
|
+
return readdirSync(folder, { withFileTypes: true }).filter((entry) => entry.isDirectory() && isPresetName(entry.name)).map((entry) => readPreset(join(folder, entry.name), entry.name)).sort((a, b) => a.summary.name.localeCompare(b.summary.name));
|
|
203
|
+
}
|
|
204
|
+
function loadPreset(root, name) {
|
|
205
|
+
return readPreset(presetDirectory(root, name), name);
|
|
206
|
+
}
|
|
207
|
+
const sha = (text) => createHash("sha256").update(text).digest("hex");
|
|
208
|
+
/**
|
|
209
|
+
* Makes a preset of the prose prompts a world carries. Nothing is overwritten.
|
|
210
|
+
* @throws {Error} when the name is taken, the world carries no prose prompt, or what it carries is not acceptable.
|
|
211
|
+
*/
|
|
212
|
+
async function savePresetFromWorld(root, worldDirectory, request) {
|
|
213
|
+
const target = presetDirectory(root, request.name);
|
|
214
|
+
if (existsSync(target)) throw new Error(`there is already a preset "${request.name}"`);
|
|
215
|
+
const source = join(worldDirectory, PROMPTS_FOLDER);
|
|
216
|
+
const found = [];
|
|
217
|
+
if (existsSync(source)) {
|
|
218
|
+
for (const entry of readdirSync(source, { withFileTypes: true })) if (entry.isDirectory() && LANGUAGE_FOLDER.test(entry.name)) {
|
|
219
|
+
for (const id of listFiles(join(source, entry.name))) if (PROSE_PROMPTS.includes(id)) found.push({
|
|
220
|
+
language: entry.name,
|
|
221
|
+
id
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
if (found.length === 0) throw new Error("the world carries no prose prompts to make a preset of");
|
|
226
|
+
const staging = join(root, ".install", `preset-${request.name}-${Date.now()}`);
|
|
227
|
+
try {
|
|
228
|
+
for (const { language, id } of found) {
|
|
229
|
+
const to = join(staging, PROMPTS_FOLDER, language, `${id}.md`);
|
|
230
|
+
await mkdir(dirname(to), { recursive: true });
|
|
231
|
+
await writeFile(to, readFileSync(join(source, language, `${id}.md`), "utf8"));
|
|
232
|
+
}
|
|
233
|
+
const header = {
|
|
234
|
+
...request.title === void 0 || request.title.trim() === "" ? {} : { title: request.title.trim() },
|
|
235
|
+
...request.description === void 0 || request.description.trim() === "" ? {} : { description: request.description.trim() },
|
|
236
|
+
...request.sampling === void 0 || Object.keys(request.sampling).length === 0 ? {} : { sampling: request.sampling }
|
|
237
|
+
};
|
|
238
|
+
await writeFile(join(staging, PRESET_FILE), JSON.stringify(header, null, 2));
|
|
239
|
+
const loaded = readPreset(staging, request.name);
|
|
240
|
+
if (loaded.summary.problems.length > 0) throw new Error(`the preset would have problems: ${loaded.summary.problems.join("; ")}`);
|
|
241
|
+
await mkdir(presetsDirectory(root), { recursive: true });
|
|
242
|
+
await rename(staging, target);
|
|
243
|
+
return readPreset(target, request.name);
|
|
244
|
+
} finally {
|
|
245
|
+
await rm(staging, {
|
|
246
|
+
recursive: true,
|
|
247
|
+
force: true
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/** Moves a preset out of the library into `<root>/.trash/presets/`. @returns where it went. */
|
|
252
|
+
async function deletePreset(root, name, now = Date.now) {
|
|
253
|
+
const directory = presetDirectory(root, name);
|
|
254
|
+
if (!existsSync(directory)) throw new Error(`there is no preset "${name}"`);
|
|
255
|
+
const aside = join(root, ".trash", PRESETS_FOLDER, `${name}-${now()}`);
|
|
256
|
+
await mkdir(dirname(aside), { recursive: true });
|
|
257
|
+
await rename(directory, aside);
|
|
258
|
+
return aside;
|
|
259
|
+
}
|
|
260
|
+
function headerFile(title, description, sampling) {
|
|
261
|
+
return `${JSON.stringify({
|
|
262
|
+
...title === void 0 ? {} : { title },
|
|
263
|
+
...description === void 0 ? {} : { description },
|
|
264
|
+
...Object.keys(sampling).length === 0 ? {} : { sampling }
|
|
265
|
+
}, null, 2)}\n`;
|
|
266
|
+
}
|
|
267
|
+
async function writeHeader(directory, text) {
|
|
268
|
+
const file = join(directory, PRESET_FILE);
|
|
269
|
+
await writeFile(`${file}.tmp`, text);
|
|
270
|
+
await rename(`${file}.tmp`, file);
|
|
271
|
+
}
|
|
272
|
+
/** Changes what a preset says about itself. @throws {Error} when the preset does not exist or the change is not acceptable; nothing is written then. */
|
|
273
|
+
async function updatePresetInfo(root, name, change) {
|
|
274
|
+
const directory = presetDirectory(root, name);
|
|
275
|
+
if (!existsSync(directory)) throw new Error(`there is no preset "${name}"`);
|
|
276
|
+
const current = readHeader(directory, []);
|
|
277
|
+
const problems = [];
|
|
278
|
+
const next = parseHeader({
|
|
279
|
+
title: change.title ?? current.title,
|
|
280
|
+
description: change.description ?? current.description,
|
|
281
|
+
sampling: change.sampling ?? current.sampling
|
|
282
|
+
}, problems);
|
|
283
|
+
if (problems.length > 0) throw new Error(problems.join("; "));
|
|
284
|
+
await writeHeader(directory, headerFile(next.title, next.description, next.sampling));
|
|
285
|
+
return readPreset(directory, name);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Makes a preset holding the shipped prose prompts of every language, as a starting point to edit. Nothing is overwritten.
|
|
289
|
+
* @throws {Error} when the name is taken or the information is not acceptable.
|
|
290
|
+
*/
|
|
291
|
+
async function createPresetFromShipped(root, request, shipped = shippedPromptsDirectory()) {
|
|
292
|
+
const target = presetDirectory(root, request.name);
|
|
293
|
+
if (existsSync(target)) throw new Error(`there is already a preset "${request.name}"`);
|
|
294
|
+
const problems = [];
|
|
295
|
+
const header = parseHeader({
|
|
296
|
+
title: request.title?.trim(),
|
|
297
|
+
description: request.description?.trim(),
|
|
298
|
+
sampling: request.sampling
|
|
299
|
+
}, problems);
|
|
300
|
+
if (problems.length > 0) throw new Error(problems.join("; "));
|
|
301
|
+
const staging = join(root, ".install", `preset-${request.name}-${Date.now()}`);
|
|
302
|
+
try {
|
|
303
|
+
for (const language of shippedLanguages(shipped)) for (const id of PROSE_PROMPTS) {
|
|
304
|
+
const from = join(shipped, language, `${id}.md`);
|
|
305
|
+
if (!existsSync(from)) continue;
|
|
306
|
+
const to = join(staging, PROMPTS_FOLDER, language, `${id}.md`);
|
|
307
|
+
await mkdir(dirname(to), { recursive: true });
|
|
308
|
+
await writeFile(to, readFileSync(from, "utf8"));
|
|
309
|
+
}
|
|
310
|
+
await mkdir(staging, { recursive: true });
|
|
311
|
+
await writeHeader(staging, headerFile(header.title, header.description, header.sampling));
|
|
312
|
+
const loaded = readPreset(staging, request.name);
|
|
313
|
+
if (loaded.summary.problems.length > 0) throw new Error(`the preset would have problems: ${loaded.summary.problems.join("; ")}`);
|
|
314
|
+
await mkdir(presetsDirectory(root), { recursive: true });
|
|
315
|
+
await rename(staging, target);
|
|
316
|
+
return readPreset(target, request.name);
|
|
317
|
+
} finally {
|
|
318
|
+
await rm(staging, {
|
|
319
|
+
recursive: true,
|
|
320
|
+
force: true
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/** Makes an empty preset folder that the import is about to fill; it has no prompts yet, so it reads as having problems until it does. */
|
|
325
|
+
async function reservePreset(root, name, title) {
|
|
326
|
+
const target = presetDirectory(root, name);
|
|
327
|
+
if (existsSync(target)) throw new Error(`there is already a preset "${name}"`);
|
|
328
|
+
await mkdir(target, { recursive: true });
|
|
329
|
+
await writeHeader(target, headerFile(title.trim().slice(0, MAX_TEXT) || void 0, void 0, {}));
|
|
330
|
+
return target;
|
|
331
|
+
}
|
|
332
|
+
//#endregion
|
|
333
|
+
export { writeWorldPrompt as _, listPresets as a, INTENT_TYPES as b, readPreset as c, sha as d, updatePresetInfo as f, readWorldPrompt as g, listWorldPrompts as h, isPresetName as i, reservePreset as l, deleteWorldPrompt as m, createPresetFromShipped as n, loadPreset as o, PROSE_PROMPTS as p, deletePreset as r, presetDirectory as s, PRESET_FILE as t, savePresetFromWorld as u, INTENT_HORIZONS as v, INTENT_STATUSES as y };
|
|
334
|
+
|
|
335
|
+
//# sourceMappingURL=presets-DNjNxQL5.js.map
|