@dsh-agentic-roleplay/core 0.3.4 → 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/index.js CHANGED
@@ -1,15 +1,19 @@
1
1
  import { n as chatSessionId, r as roleplaySessionOf } from "./access-EbNdZFbk.js";
2
- import { $ as populateCalendar, $t as readCard, A as worldEntityImageFile, At as loadVariables, B as translateMessage, Bt as parseWorldFile, C as readWorldEditor, Ct as subfolders, D as uploadWorldCharacterPortrait, Dt as readWorldPrompt, E as replaceFile$1, Et as listWorldPrompts, F as loadWorldParts, Ft as IMPORT_FOLDER, G as authoringChatSessionId, Gt as parseLoreEdit, Ht as InvalidEditProblems, I as PRESET_FILE, It as ImportNotFoundError, Jt as parseThingEdit, K as languageOfDocuments, Kt as parsePlaceEdit, L as presetMessage, Lt as ImportStore, Mt as variableProblems, Nt as CHARACTER_FILE_NAME, O as uploadWorldEntityImage, Ot as writeWorldPrompt, Pt as parseCharacterFile, Q as loadValidatedWorld, Qt as pictureOf, Rt as parseChoices, S as generateWorldEntityImage, St as loadCharacters, T as removeWorldTrigger, Tt as deleteWorldPrompt, Ut as parseCharacterEdit, Vt as parseWorldInfoEdit, Wt as parseFactionEdit, X as guardedLookup, Xt as ImportCardError, Y as markReady, Yt as IMPORT_COMMAND, Z as vetUrl, Zt as isPng, _ as editWorldPlots$1, _t as loadCalendar, a as deleteWorldPackFile, at as populateKnowledge, b as entityFolder, bt as loadItems, c as createAuthoredEntity, ct as populateRoutines, dt as routineProblems, en as DICE_COMPARISONS, et as populateCharacters, f as editWorldCharacter$1, ft as TRIGGERS_FILE_NAME, g as editWorldPlace$1, gt as CALENDAR_FILE_NAME, h as editWorldNarrationVoice, ht as parseDiceFile, it as populateItems, j as writeWorldTrigger, jt as parseVariablesFile, k as worldCharacterPortraitFile, kt as VARIABLES_FILE_NAME, l as deleteAuthoredEntity, lt as populateTriggers, m as editWorldLore$1, mt as loadDice, n as packFileProblem, nn as DICE_RESULTS, nt as populateDice, ot as populateLocations, p as editWorldFaction$1, pt as DICE_FILE_NAME, q as readWorldDocuments, qt as parseRelationEdit, rt as populateFactions, st as populateLore, t as MAX_PACK_TEXT, tn as DICE_READINGS, tt as populateClock, u as readAuthoredEntityDocument, ut as populateVariables, v as editWorldRelation$1, vt as parseCalendarFile, w as readWorldTrigger, x as generateWorldCharacterPortrait, xt as CHARACTERS_DIR, y as editWorldThing$1, yt as loadLocations, z as readPreset, zt as WORLD_FILE_NAME } from "./pack-jail-CybSNzxr.js";
3
- import { _ as API_PREFIX, a as packIds, c as describePack, d as packPath, f as readPack, l as isPackId, m as PACK_LIMITS, n as anyTab, o as syncWorldPacks, r as copyWorldPacks, s as worldPackStates, t as activePack, u as packFile } from "./packs-BYjrAen6.js";
4
- import { c as readTimeline, i as reflectionStatus, l as pathTo, o as TimelineError, u as defaultBoundaries } from "./reflection-waxFg1x-.js";
2
+ import { A as parseDiceFile, C as populateTriggers, Ct as isPng, Dt as DICE_READINGS, E as TRIGGERS_FILE_NAME, Et as DICE_COMPARISONS, H as CHARACTERS_DIR, J as loadVariables, L as loadLocations, M as loadCalendar, N as parseCalendarFile, O as DICE_FILE_NAME, Ot as DICE_RESULTS, S as populateRoutines, St as ImportCardError, T as routineProblems, Tt as readCard, U as loadCharacters, W as subfolders, X as variableProblems, Y as parseVariablesFile, _ as populateFactions, _t as parseLoreEdit, at as CHARACTER_FILE_NAME, b as populateLocations, bt as parseThingEdit, ct as ImportNotFoundError, d as vetUrl, dt as WORLD_FILE_NAME, f as loadValidatedWorld, ft as parseWorldFile, g as populateDice, gt as parseFactionEdit, h as populateClock, ht as parseCharacterEdit, j as CALENDAR_FILE_NAME, k as loadDice, l as markReady, lt as ImportStore, m as populateCharacters, mt as InvalidEditProblems, ot as parseCharacterFile, p as populateCalendar, pt as parseWorldInfoEdit, q as VARIABLES_FILE_NAME, s as authoringChatSessionId, st as IMPORT_FOLDER, t as readPreset, u as guardedLookup, ut as parseChoices, v as populateItems, vt as parsePlaceEdit, w as populateVariables, wt as pictureOf, x as populateLore, xt as IMPORT_COMMAND, y as populateKnowledge, yt as parseRelationEdit, z as loadItems } from "./preset-ClQm7KPQ.js";
3
+ import { _ as writeWorldPrompt, a as listPresets, f as updatePresetInfo, g as readWorldPrompt, h as listWorldPrompts, i as isPresetName, l as reservePreset, m as deleteWorldPrompt, n as createPresetFromShipped, o as loadPreset, p as PROSE_PROMPTS, r as deletePreset, s as presetDirectory, t as PRESET_FILE, u as savePresetFromWorld } from "./presets-DNjNxQL5.js";
4
+ import { c as copyPreset, d as switchPreset, f as readTimeline, i as reflectionStatus, l as presetStatus, m as defaultBoundaries, o as TimelineError, p as pathTo, u as recordPreset } from "./reflection-Bju_LPFn.js";
5
5
  import { a as WORLD_RULES_KEY, c as VARIABLE_SUBJECTS, i as parseSwitchboard, l as VARIABLE_VISIBILITIES, n as SETTINGS_SPEC, r as parseSettings, s as VARIABLE_KINDS, t as DEFAULT_SETTINGS } from "./settings-CA6icLL_.js";
6
- import { A as WorldNotFoundError, D as ReflectionRefusedError, E as InvalidWorldError, O as SessionArchivedError, T as InvalidStartError, c as parsePlotsFile, d as errorMessage, g as copyWorldPrompts, h as PromptLibrary, k as SessionNotFoundError, l as plotReferenceProblems, p as formatIssue, u as readDocument$1, v as languageOf, w as AgentUnavailableError } from "./language-DWVwi1R7.js";
7
- import { A as observeAsPlayer, D as notesOfSession, T as addToNote, b as populatePlots, c as readRunDetail, d as externalTools, h as PLOTS_METADATA_KEY, i as readUsage, k as readNotes, n as readWithdrawn, s as readActivity, y as plotProgressOf, z as zoneOf } from "./deliver-CpZOxBX5.js";
6
+ import { C as ReflectionRefusedError, E as WorldNotFoundError, S as InvalidWorldError, T as SessionNotFoundError, a as readDocument$1, b as AgentUnavailableError, c as formatIssue, d as PromptLibrary, f as copyWorldPrompts, i as plotReferenceProblems, m as languageOf, o as errorMessage, r as parsePlotsFile, w as SessionArchivedError, x as InvalidStartError } from "./load-Clkl0T7C.js";
7
+ import { A as observeAsPlayer, D as notesOfSession, T as addToNote, b as populatePlots, c as readRunDetail, d as externalTools, h as PLOTS_METADATA_KEY, i as readUsage, k as readNotes, n as readWithdrawn, s as readActivity, y as plotProgressOf, z as zoneOf } from "./deliver-Cl6bjp7g.js";
8
8
  import { a as liveSettings, c as writeSettings, i as InvalidSettingsError, n as encodeOutput, o as readSettings, s as useChildReasoning } from "./narration-DK2kiIjE.js";
9
9
  import { r as childSamplingOf } from "./child-CFjnaTZt.js";
10
- import { T as uniqueKey, a as uploadReferenceImage, m as sceneImagePath, o as addToLibrary, p as referenceImagePath, t as generateReferenceImage, u as inheritImages, w as slug } from "./generator-B6FxlKOV.js";
10
+ import { a as inheritImages, b as slug, c as referenceImagePath, l as sceneImagePath, t as addToLibrary, x as uniqueKey } from "./library-9JB9zw5a.js";
11
+ import { A as worldEntityImageFile, C as readWorldEditor, D as uploadWorldCharacterPortrait, E as replaceFile$1, F as loadWorldParts, H as readWorldDocuments, I as PRESET_FILE$1, L as presetMessage, O as uploadWorldEntityImage, S as generateWorldEntityImage, T as removeWorldTrigger, V as languageOfDocuments, _ as editWorldPlots$1, a as deleteWorldPackFile, b as entityFolder, c as createAuthoredEntity, f as editWorldCharacter$1, g as editWorldPlace$1, h as editWorldNarrationVoice, j as writeWorldTrigger, k as worldCharacterPortraitFile, l as deleteAuthoredEntity, m as editWorldLore$1, n as packFileProblem, p as editWorldFaction$1, t as MAX_PACK_TEXT, u as readAuthoredEntityDocument, v as editWorldRelation$1, w as readWorldTrigger, x as generateWorldCharacterPortrait, y as editWorldThing$1, z as translateMessage } from "./pack-jail-ClMpVc__.js";
12
+ import { a as presetChatSessionId, n as narrationImportMessage } from "./preset-author-tools-Dibp-LY9.js";
11
13
  import { t as doctorChatSessionId } from "./doctor-chat-ByDfCMeo.js";
12
- import { i as updateImage, n as galleryFile, r as listGallery } from "./gallery-CtFDL22W.js";
14
+ import { _ as API_PREFIX, a as packIds, c as describePack, d as packPath, f as readPack, l as isPackId, m as PACK_LIMITS, n as anyTab, o as syncWorldPacks, r as copyWorldPacks, s as worldPackStates, t as activePack, u as packFile } from "./packs-BC2A3geF.js";
15
+ import { a as uploadReferenceImage, t as generateReferenceImage } from "./generator-DJ48HxPI.js";
16
+ import { i as updateImage, n as galleryFile, r as listGallery } from "./gallery-DIEgETzU.js";
13
17
  import { basename, dirname, extname, isAbsolute, join, normalize, relative, resolve, sep } from "node:path";
14
18
  import { cpSync, createReadStream, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, statSync, writeFileSync } from "node:fs";
15
19
  import { createHash, randomBytes, randomUUID } from "node:crypto";
@@ -379,6 +383,7 @@ async function createWorld(catalog, input) {
379
383
  if (folder === "" || !FOLDER.test(folder) || folder === "." || folder === ".." || /[. ]$/.test(folder)) problems.push("folder must be 1–100 letters, numbers, spaces, dots, underscores or hyphens; it cannot end in a dot or space");
380
384
  if (name === "") problems.push("world name is required");
381
385
  if (name.length > 200) problems.push("world name must be at most 200 characters");
386
+ if (input.preset !== void 0 && input.preset !== "" && !isPresetName(input.preset)) problems.push("preset must be the name of a preset in the library");
382
387
  if (problems.length > 0) throw new InvalidEditProblems(problems);
383
388
  await mkdir(catalog.directory, { recursive: true });
384
389
  const directory = join(catalog.directory, folder);
@@ -394,7 +399,8 @@ async function createWorld(catalog, input) {
394
399
  id,
395
400
  name,
396
401
  description: "",
397
- metadata: { tags: [] }
402
+ metadata: { tags: [] },
403
+ ...input.preset === void 0 || input.preset === "" ? {} : { preset: input.preset }
398
404
  }, null, 2) + "\n", {
399
405
  flag: "wx",
400
406
  mode: 420
@@ -498,7 +504,7 @@ async function uploadPreset(deps, worldId, file, name, language) {
498
504
  if (world === void 0) throw new WorldNotFoundError(`there is no world with id "${worldId}"`);
499
505
  const preset = readPreset(file, name);
500
506
  await mkdir(join(world.directory, IMPORT_FOLDER), { recursive: true });
501
- await writeFile(join(world.directory, IMPORT_FOLDER, PRESET_FILE), file);
507
+ await writeFile(join(world.directory, IMPORT_FOLDER, PRESET_FILE$1), file);
502
508
  const chatSessionId = await deps.authoring.ensure(world.summary.id, world.summary.name, `Authoring ${world.summary.name}`, language);
503
509
  let failure;
504
510
  try {
@@ -568,6 +574,58 @@ var AuthoringSessions = class {
568
574
  }
569
575
  };
570
576
  //#endregion
577
+ //#region src/host/agent/preset-authoring-sessions.ts
578
+ const PRESET_AUTHORING_PROJECT_TITLE = "Preset Authoring";
579
+ const PRESET_AUTHORING_PRESET = "preset-authoring";
580
+ /** Creates or reopens the dedicated authoring chat of a library preset, and speaks in it for the person when asked to. */
581
+ var PresetAuthoringSessions = class {
582
+ services;
583
+ roleplayRoot;
584
+ prompts;
585
+ languages;
586
+ constructor(services, roleplayRoot, prompts, languages) {
587
+ this.services = services;
588
+ this.roleplayRoot = roleplayRoot;
589
+ this.prompts = prompts;
590
+ this.languages = languages;
591
+ }
592
+ async ensure(name, title, language) {
593
+ this.languages.set(`preset:${name}`, language);
594
+ const services = this.services();
595
+ const projectPath = join(this.roleplayRoot(), "presets");
596
+ const project = await services.registry.create(projectPath, PRESET_AUTHORING_PROJECT_TITLE);
597
+ const sessionId = presetChatSessionId(name);
598
+ const stored = !project.sessionIds.includes(sessionId) && await isStored(services.persistence, sessionId);
599
+ if (!project.sessionIds.includes(sessionId) && !stored) {
600
+ if (services.persistence === void 0) throw new AgentUnavailableError("this DSH profile cannot store the opening of an authoring chat");
601
+ await storeAuthoringOpening(services.persistence, {
602
+ id: sessionId,
603
+ cwd: project.path,
604
+ agentPreset: PRESET_AUTHORING_PRESET
605
+ }, this.prompts.text("preset/welcome", this.languages.get(`preset:${name}`), { preset: title }));
606
+ }
607
+ return await ensureAgentChat(services, {
608
+ projectPath,
609
+ projectTitle: PRESET_AUTHORING_PROJECT_TITLE,
610
+ sessionId,
611
+ agentPreset: PRESET_AUTHORING_PRESET,
612
+ title: `Preset ${title}`,
613
+ renameExisting: true
614
+ }, project);
615
+ }
616
+ async send(sessionId, text) {
617
+ await this.services().controller.prompt({
618
+ requestId: randomUUID(),
619
+ sessionId,
620
+ mode: "queue",
621
+ content: [{
622
+ type: "text",
623
+ text
624
+ }]
625
+ }, new AbortController().signal);
626
+ }
627
+ };
628
+ //#endregion
571
629
  //#region src/host/agent/doctor-sessions.ts
572
630
  const DOCTOR_PROJECT_TITLE = "Session Doctor";
573
631
  const DOCTOR_AGENT_PRESET = "session-doctor";
@@ -617,6 +675,8 @@ var RoleplayOrchestration = class extends Service {
617
675
  halt = () => Promise.resolve();
618
676
  /** Starts a reflection of a chat's characters on what they have taken in, in the background; there while the orchestration is. */
619
677
  reflect = () => Promise.reject(/* @__PURE__ */ new Error("the orchestration is not running"));
678
+ /** Whether a turn of the chat is being played; there while the orchestration is. */
679
+ playing = () => false;
620
680
  /** Whether a reflection of the chat is running; there while the orchestration is. */
621
681
  reflecting = () => false;
622
682
  /** What the story's turns are and taking the story back to one; there while the orchestration is. */
@@ -1063,6 +1123,249 @@ async function readBinaryBody(req, limit) {
1063
1123
  if (size === 0) throw new HttpError(400, "the upload is empty");
1064
1124
  return Buffer.concat(chunks);
1065
1125
  }
1126
+ //#endregion
1127
+ //#region src/host/presets/service.ts
1128
+ /** The story's prompts cannot change right now. */
1129
+ var PresetBusyError = class extends Error {};
1130
+ /** The preset library and the presets of stories. Refusals are plain `Error`s with the reason; unknown stories and worlds are not-found errors. */
1131
+ var Presets = class {
1132
+ deps;
1133
+ constructor(deps) {
1134
+ this.deps = deps;
1135
+ }
1136
+ get maxUploadBytes() {
1137
+ return this.deps.maxUploadBytes;
1138
+ }
1139
+ list() {
1140
+ return listPresets(this.deps.root()).map((entry) => entry.summary);
1141
+ }
1142
+ /** A preset with the text of each prompt it carries, keyed `<language>/<id>`. */
1143
+ read(name) {
1144
+ const loaded = loadPreset(this.deps.root(), name);
1145
+ return {
1146
+ summary: loaded.summary,
1147
+ prompts: Object.fromEntries([...loaded.files].map(([path, text]) => [path.slice(0, -3), text]))
1148
+ };
1149
+ }
1150
+ async saveFromWorld(request) {
1151
+ const directory = await this.deps.worldDirectory(request.fromWorld);
1152
+ if (directory === void 0) throw new WorldNotFoundError(`there is no world with id "${request.fromWorld}"`);
1153
+ return (await savePresetFromWorld(this.deps.root(), directory, request)).summary;
1154
+ }
1155
+ /** Makes a preset from a world's own prose prompts, or from the shipped ones as a starting point when no world is given. */
1156
+ async create(request) {
1157
+ if (request.fromWorld !== void 0) return await this.saveFromWorld({
1158
+ ...request,
1159
+ fromWorld: request.fromWorld
1160
+ });
1161
+ return (await createPresetFromShipped(this.deps.root(), request)).summary;
1162
+ }
1163
+ async updateInfo(name, change) {
1164
+ return (await updatePresetInfo(this.deps.root(), name, change)).summary;
1165
+ }
1166
+ /** Writes the person's own text of a prompt; `undefined` takes the preset's version out. */
1167
+ async writePrompt(name, language, id, text) {
1168
+ const directory = presetDirectory(this.deps.root(), name);
1169
+ if (!existsSync(directory)) throw new Error(`there is no preset "${name}"`);
1170
+ if (!PROSE_PROMPTS.includes(id)) throw new Error(`a preset carries only ${PROSE_PROMPTS.join(", ")}`);
1171
+ if (text === void 0) await deleteWorldPrompt(directory, language, id);
1172
+ else await writeWorldPrompt(directory, language, id, text);
1173
+ return loadPreset(this.deps.root(), name).summary;
1174
+ }
1175
+ /** Like `importFile`, under a name made from the title that is not yet taken. */
1176
+ async importTitled(title, file, language) {
1177
+ const base = title.normalize("NFKD").replace(/[^A-Za-z0-9_-]+/g, "-").replace(/^[-_]+|[-_]+$/g, "").slice(0, 56) || "preset";
1178
+ const taken = new Set(listPresets(this.deps.root()).map((entry) => entry.summary.name));
1179
+ let name = base;
1180
+ for (let index = 2; taken.has(name); index += 1) name = `${base}-${index}`;
1181
+ return await this.importFile(name, file, language, title.trim() === "" ? void 0 : title.trim().slice(0, 300));
1182
+ }
1183
+ /** Opens the authoring chat of a preset. */
1184
+ async author(name, language) {
1185
+ const loaded = loadPreset(this.deps.root(), name);
1186
+ return { chatSessionId: await this.deps.authoring.ensure(name, loaded.summary.title, language) };
1187
+ }
1188
+ /** Keeps an uploaded SillyTavern preset in a new library preset and has its authoring chat turn it into prompts. @throws {ImportCardError} when the file is not a preset. */
1189
+ async importFile(name, file, language, title) {
1190
+ const uploaded = readPreset(file, name);
1191
+ const root = this.deps.root();
1192
+ const directory = await reservePreset(root, name, title ?? uploaded.name);
1193
+ try {
1194
+ await mkdir(join(directory, IMPORT_FOLDER), { recursive: true });
1195
+ await writeFile(join(directory, IMPORT_FOLDER, PRESET_FILE), file);
1196
+ } catch (error) {
1197
+ await deletePreset(root, name);
1198
+ throw error;
1199
+ }
1200
+ const chatSessionId = await this.deps.authoring.ensure(name, title ?? uploaded.name, language);
1201
+ let failure;
1202
+ try {
1203
+ await this.deps.authoring.send(chatSessionId, this.deps.importMessage());
1204
+ } catch (problem) {
1205
+ failure = problem instanceof Error ? problem.message : String(problem);
1206
+ }
1207
+ return {
1208
+ name,
1209
+ chatSessionId,
1210
+ started: failure === void 0,
1211
+ ...failure === void 0 ? {} : { failure }
1212
+ };
1213
+ }
1214
+ async remove(name) {
1215
+ await deletePreset(this.deps.root(), name);
1216
+ }
1217
+ status(session) {
1218
+ return presetStatus(this.deps.root(), this.folder(session));
1219
+ }
1220
+ /** Puts the named preset into a story, or takes the story's preset out when no name is given. */
1221
+ apply(session, name) {
1222
+ const folder = this.folder(session);
1223
+ if (this.deps.playing(session)) throw new PresetBusyError("a turn is being played; change the preset when it is over");
1224
+ return { files: switchPreset(this.deps.root(), folder, name) };
1225
+ }
1226
+ folder(id) {
1227
+ const sessions = `${this.deps.root()}/sessions`;
1228
+ if (!isSessionId(id) || !existsSync(sessionDatabase(sessions, id))) throw new SessionNotFoundError(`no session "${id}"`);
1229
+ return sessionFolder(sessions, id);
1230
+ }
1231
+ };
1232
+ //#endregion
1233
+ //#region src/host/presets/routes.ts
1234
+ /** Not found is 404 and a request the library refuses is 422 with its reason; anything else is a fault. */
1235
+ function failure(res, error) {
1236
+ if (error instanceof SessionNotFoundError || error instanceof WorldNotFoundError) return sendError(res, 404, error.message);
1237
+ if (error instanceof PresetBusyError) return sendError(res, 409, error.message);
1238
+ if (error instanceof Error) return sendError(res, 422, error.message);
1239
+ throw error;
1240
+ }
1241
+ function samplingOf(value) {
1242
+ if (value === void 0) return {};
1243
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return "\"sampling\" must be an object";
1244
+ const { master, characters } = value;
1245
+ for (const number of [master, characters]) if (number !== void 0 && typeof number !== "number") return "sampling values must be numbers";
1246
+ return {
1247
+ ...master === void 0 ? {} : { master },
1248
+ ...characters === void 0 ? {} : { characters }
1249
+ };
1250
+ }
1251
+ function presetRoutes(api) {
1252
+ return {
1253
+ "/presets": {
1254
+ GET: async (_req, res) => {
1255
+ try {
1256
+ sendJson(res, 200, api.list());
1257
+ } catch (error) {
1258
+ failure(res, error);
1259
+ }
1260
+ },
1261
+ POST: async (req, res) => {
1262
+ const { name, title, description, sampling, fromWorld } = await readJsonBody(req);
1263
+ if (typeof name !== "string" || name === "") return sendError(res, 400, "\"name\" is required");
1264
+ if (fromWorld !== void 0 && (typeof fromWorld !== "string" || fromWorld === "")) return sendError(res, 400, "\"fromWorld\" must be a world id, or left out to start from the shipped prompts");
1265
+ if (title !== void 0 && typeof title !== "string" || description !== void 0 && typeof description !== "string") return sendError(res, 400, "\"title\" and \"description\" must be text");
1266
+ const given = samplingOf(sampling);
1267
+ if (typeof given === "string") return sendError(res, 400, given);
1268
+ try {
1269
+ sendJson(res, 201, await api.create({
1270
+ name,
1271
+ ...fromWorld === void 0 ? {} : { fromWorld },
1272
+ ...title === void 0 ? {} : { title },
1273
+ ...description === void 0 ? {} : { description },
1274
+ sampling: given
1275
+ }));
1276
+ } catch (error) {
1277
+ failure(res, error);
1278
+ }
1279
+ }
1280
+ },
1281
+ "/presets/item": {
1282
+ GET: async (_req, res, url) => {
1283
+ try {
1284
+ sendJson(res, 200, api.read(url.searchParams.get("name") ?? ""));
1285
+ } catch (error) {
1286
+ failure(res, error);
1287
+ }
1288
+ },
1289
+ PUT: async (req, res, url) => {
1290
+ const { title, description, sampling } = await readJsonBody(req);
1291
+ if (title !== void 0 && typeof title !== "string" || description !== void 0 && typeof description !== "string") return sendError(res, 400, "\"title\" and \"description\" must be text");
1292
+ const given = sampling === void 0 ? void 0 : samplingOf(sampling);
1293
+ if (typeof given === "string") return sendError(res, 400, given);
1294
+ try {
1295
+ sendJson(res, 200, await api.updateInfo(url.searchParams.get("name") ?? "", {
1296
+ ...title === void 0 ? {} : { title },
1297
+ ...description === void 0 ? {} : { description },
1298
+ ...given === void 0 ? {} : { sampling: given }
1299
+ }));
1300
+ } catch (error) {
1301
+ failure(res, error);
1302
+ }
1303
+ },
1304
+ DELETE: async (_req, res, url) => {
1305
+ try {
1306
+ await api.remove(url.searchParams.get("name") ?? "");
1307
+ res.statusCode = 204;
1308
+ res.end();
1309
+ } catch (error) {
1310
+ failure(res, error);
1311
+ }
1312
+ }
1313
+ },
1314
+ "/presets/prompt": {
1315
+ PUT: async (req, res, url) => {
1316
+ const { text } = await readJsonBody(req);
1317
+ if (typeof text !== "string" || text.trim() === "") return sendError(res, 400, "\"text\" is required");
1318
+ try {
1319
+ sendJson(res, 200, await api.writePrompt(url.searchParams.get("name") ?? "", languageOf(url.searchParams.get("language") ?? ""), url.searchParams.get("id") ?? "", text));
1320
+ } catch (error) {
1321
+ failure(res, error);
1322
+ }
1323
+ },
1324
+ DELETE: async (_req, res, url) => {
1325
+ try {
1326
+ sendJson(res, 200, await api.writePrompt(url.searchParams.get("name") ?? "", languageOf(url.searchParams.get("language") ?? ""), url.searchParams.get("id") ?? "", void 0));
1327
+ } catch (error) {
1328
+ failure(res, error);
1329
+ }
1330
+ }
1331
+ },
1332
+ "/presets/authoring": { POST: async (req, res, url) => {
1333
+ const { language } = await readJsonBody(req);
1334
+ if (language !== void 0 && typeof language !== "string") return sendError(res, 400, "\"language\" must be text");
1335
+ try {
1336
+ sendJson(res, 200, await api.author(url.searchParams.get("name") ?? "", language === void 0 ? void 0 : languageOf(language)));
1337
+ } catch (error) {
1338
+ failure(res, error);
1339
+ }
1340
+ } },
1341
+ "/presets/import": { POST: async (req, res, url) => {
1342
+ const language = url.searchParams.get("language");
1343
+ try {
1344
+ sendJson(res, 200, await api.importFile(url.searchParams.get("name") ?? "", await readBinaryBody(req, api.maxUploadBytes), language === null ? void 0 : languageOf(language)));
1345
+ } catch (error) {
1346
+ failure(res, error);
1347
+ }
1348
+ } },
1349
+ "/sessions/preset": {
1350
+ GET: async (_req, res, url) => {
1351
+ try {
1352
+ sendJson(res, 200, api.status(url.searchParams.get("id") ?? ""));
1353
+ } catch (error) {
1354
+ failure(res, error);
1355
+ }
1356
+ },
1357
+ POST: async (req, res, url) => {
1358
+ const { name } = await readJsonBody(req);
1359
+ if (name !== void 0 && name !== null && (typeof name !== "string" || name === "")) return sendError(res, 400, "\"name\" must be a preset name, or left out to take the preset out");
1360
+ try {
1361
+ sendJson(res, 200, api.apply(url.searchParams.get("id") ?? "", typeof name === "string" ? name : void 0));
1362
+ } catch (error) {
1363
+ failure(res, error);
1364
+ }
1365
+ }
1366
+ }
1367
+ };
1368
+ }
1066
1369
  /** The headers of every file of a pack. */
1067
1370
  const PACK_HEADERS = {
1068
1371
  "content-security-policy": [
@@ -1860,7 +2163,7 @@ function languageField(value) {
1860
2163
  throw new InvalidEditProblems([error instanceof Error ? error.message : String(error)]);
1861
2164
  }
1862
2165
  }
1863
- function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld, translation, worldCharacters, importer, avatars, worldFiles, worldEditor, sessions, packs, connection, speech, externalTools }) {
2166
+ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld, translation, worldCharacters, importer, avatars, worldFiles, worldEditor, sessions, packs, presets, connection, speech, externalTools }) {
1864
2167
  const routes = {
1865
2168
  "/speech/capabilities": { GET: async (_req, res) => {
1866
2169
  sendJson(res, 200, { streaming: speech.streaming() });
@@ -1887,9 +2190,11 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
1887
2190
  try {
1888
2191
  const body = await readJsonBody(req);
1889
2192
  if (typeof body.folder !== "string" || typeof body.name !== "string") return sendError(res, 400, "folder and name must be text");
2193
+ if (body.preset !== void 0 && typeof body.preset !== "string") return sendError(res, 400, "preset must be text");
1890
2194
  sendJson(res, 201, await createWorld({
1891
2195
  folder: body.folder,
1892
- name: body.name
2196
+ name: body.name,
2197
+ ...body.preset === void 0 ? {} : { preset: body.preset }
1893
2198
  }));
1894
2199
  } catch (error) {
1895
2200
  if (error instanceof InvalidEditProblems) return sendJson(res, 422, {
@@ -1909,7 +2214,7 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
1909
2214
  const sort = url.searchParams.get("sort") === "popular" ? "popular" : "recent";
1910
2215
  try {
1911
2216
  sendJson(res, 200, await catalogue.worlds(url.searchParams.get("source") ?? "", {
1912
- kind: url.searchParams.get("kind") === "card" ? "card" : "world",
2217
+ kind: ["card", "preset"].find((entry) => entry === url.searchParams.get("kind")) ?? "world",
1913
2218
  search: url.searchParams.get("search") ?? "",
1914
2219
  offset: Number.isFinite(offset) ? offset : 0,
1915
2220
  sort
@@ -1976,6 +2281,22 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
1976
2281
  return error instanceof CatalogueError ? catalogueFailure(res, error) : importFailure(res, error);
1977
2282
  }
1978
2283
  } },
2284
+ "/catalogue/convert/preset": { POST: async (req, res) => {
2285
+ const body = await readJsonBody(req);
2286
+ if (typeof body.source !== "string" || typeof body.versionId !== "string") return sendError(res, 400, "source and versionId must be text");
2287
+ if (body.title !== void 0 && typeof body.title !== "string") return sendError(res, 400, "title must be text");
2288
+ try {
2289
+ const language = languageField(body.language);
2290
+ sendJson(res, 201, await catalogue.convertPreset({
2291
+ source: body.source,
2292
+ versionId: body.versionId,
2293
+ ...body.title === void 0 ? {} : { title: body.title },
2294
+ ...language === void 0 ? {} : { language }
2295
+ }));
2296
+ } catch (error) {
2297
+ return error instanceof CatalogueError ? catalogueFailure(res, error) : importFailure(res, error);
2298
+ }
2299
+ } },
1979
2300
  "/worlds/starter": {
1980
2301
  GET: async (_req, res) => {
1981
2302
  sendJson(res, 200, await starter.list());
@@ -2631,7 +2952,7 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
2631
2952
  sendJson(res, 200, sessions.list());
2632
2953
  },
2633
2954
  POST: async (req, res) => {
2634
- const { worldId, playerCharacterId, playerAvatar, avatars: brought, name, settings, opening, startingTime } = await readJsonBody(req);
2955
+ const { worldId, playerCharacterId, playerAvatar, avatars: brought, name, settings, opening, startingTime, preset } = await readJsonBody(req);
2635
2956
  if (typeof worldId !== "string" || worldId.trim() === "") return sendError(res, 400, "\"worldId\" is required");
2636
2957
  if (playerCharacterId === void 0 && playerAvatar === void 0) return sendError(res, 400, "\"playerCharacterId\" or \"playerAvatar\" is required");
2637
2958
  if (playerCharacterId !== void 0 && (typeof playerCharacterId !== "string" || playerCharacterId.trim() === "")) return sendError(res, 400, "\"playerCharacterId\" must be text");
@@ -2640,8 +2961,10 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
2640
2961
  if (name !== void 0 && typeof name !== "string") return sendError(res, 400, "\"name\" must be a string");
2641
2962
  if (opening !== void 0 && (typeof opening !== "number" || !Number.isInteger(opening))) return sendError(res, 400, "\"opening\" must be a whole number");
2642
2963
  if (startingTime !== void 0 && typeof startingTime !== "string") return sendError(res, 400, "\"startingTime\" must be text");
2964
+ if (preset !== void 0 && typeof preset !== "string") return sendError(res, 400, "\"preset\" must be text");
2643
2965
  try {
2644
2966
  sendJson(res, 201, await sessions.start({
2967
+ ...preset === void 0 || preset === "" ? {} : { preset },
2645
2968
  ...opening === void 0 ? {} : { opening },
2646
2969
  ...startingTime === void 0 ? {} : { startingTime },
2647
2970
  worldId,
@@ -3028,6 +3351,7 @@ function createApiHandler({ catalogue, worlds, createWorld, starter, authorWorld
3028
3351
  res.end();
3029
3352
  } },
3030
3353
  ...interfaceRoutes(packs),
3354
+ ...presetRoutes(presets),
3031
3355
  "/worlds/cover": { GET: async (_req, res, url) => {
3032
3356
  const cover = await worlds.cover(url.searchParams.get("id") ?? "");
3033
3357
  if (cover === void 0) return sendError(res, 404, "no cover");
@@ -4317,6 +4641,7 @@ function infoOf(document) {
4317
4641
  authorUrl: textOf(author["url"]),
4318
4642
  timeZone: textOf(document["time_zone"]),
4319
4643
  startingTime: textOf(document["starting_time"]),
4644
+ preset: textOf(document["preset"]),
4320
4645
  version: typeof document["version"] === "number" ? String(document["version"]) : textOf(document["version"]),
4321
4646
  tools: toolsOf(document["tools"]),
4322
4647
  alternateOpenings: Array.isArray(document["alternate_openings"]) ? document["alternate_openings"].flatMap((entry) => typeof entry === "object" && entry !== null && typeof entry["text"] === "string" ? [{
@@ -4375,6 +4700,7 @@ async function editWorldInfo(directory, edit) {
4375
4700
  if (edit.timeZone !== void 0) setOrRemove(document, "time_zone", edit.timeZone);
4376
4701
  if (edit.startingTime !== void 0) setOrRemove(document, "starting_time", edit.startingTime);
4377
4702
  if (edit.version !== void 0) setOrRemove(document, "version", edit.version);
4703
+ if (edit.preset !== void 0) setOrRemove(document, "preset", edit.preset);
4378
4704
  if (edit.alternateOpenings !== void 0) {
4379
4705
  if (edit.alternateOpenings.length === 0) delete document["alternate_openings"];
4380
4706
  else document["alternate_openings"] = edit.alternateOpenings.map((entry) => ({
@@ -4457,6 +4783,17 @@ async function startSession(deps, request) {
4457
4783
  },
4458
4784
  tools
4459
4785
  };
4786
+ const { preset, unavailable } = presetFor(deps, request.preset, world.preset);
4787
+ if (preset !== void 0) {
4788
+ const given = request.settings?.sampling ?? {};
4789
+ settings = {
4790
+ ...settings,
4791
+ sampling: {
4792
+ ...settings.sampling,
4793
+ ...Object.fromEntries(Object.entries(preset.summary.sampling).filter(([key]) => given[key] === void 0))
4794
+ }
4795
+ };
4796
+ }
4460
4797
  let startsAt = world.clock?.startingTime;
4461
4798
  if (request.startingTime !== void 0 && request.startingTime.trim() !== "") {
4462
4799
  if (settings.clock.mode === "real") throw new InvalidStartError("the story's time follows the wall clock, so it has no starting time to choose");
@@ -4503,12 +4840,18 @@ async function startSession(deps, request) {
4503
4840
  });
4504
4841
  await inheritImages(world.directory, sessionFolder(sessionsDir, id), []);
4505
4842
  if (avatars !== void 0) for (const entry of brought) await copyPortrait(avatars, entry.from, entry.document, sessionFolder(sessionsDir, id), entry.id);
4506
- const promptProblems = copyWorldPrompts(world.directory, sessionFolder(sessionsDir, id));
4843
+ const folder = sessionFolder(sessionsDir, id);
4844
+ if (preset !== void 0 && request.preset === void 0) copyPreset(preset, folder);
4845
+ const promptProblems = copyWorldPrompts(world.directory, folder);
4507
4846
  if (promptProblems.length > 0) throw new InvalidWorldError(`the prompts of "${world.summary.name}" have ${promptProblems.length} problem(s)`, promptProblems);
4847
+ if (preset !== void 0) {
4848
+ if (request.preset !== void 0) copyPreset(preset, folder);
4849
+ recordPreset(folder, preset);
4850
+ }
4508
4851
  const packProblems = copyWorldPacks(world.directory, sessionFolder(sessionsDir, id));
4509
4852
  if (packProblems.length > 0) throw new InvalidWorldError(`the interface packs of "${world.summary.name}" have ${packProblems.length} problem(s)`, packProblems);
4510
4853
  writeSettings(sessionFolder(sessionsDir, id), settings);
4511
- addToNote(session, "world", 1, worldFindings(session));
4854
+ addToNote(session, "world", 1, [...unavailable, ...worldFindings(session)]);
4512
4855
  chatSessionId = await deps.agent.ensure(id, name, opening);
4513
4856
  } catch (error) {
4514
4857
  deps.state.deleteSession(sessionsDir, id);
@@ -4524,6 +4867,30 @@ async function startSession(deps, request) {
4524
4867
  characterCount: characters.length
4525
4868
  };
4526
4869
  }
4870
+ /**
4871
+ * The preset a story uses: the one asked for, else the one the world suggests. Asking for one that cannot be used refuses the start;
4872
+ * a suggestion that cannot be followed is told to the player instead, because the world may come from someone whose library differs.
4873
+ */
4874
+ function presetFor(deps, asked, suggested) {
4875
+ const name = asked ?? suggested;
4876
+ if (name === void 0 || name === "none") return { unavailable: [] };
4877
+ const load = () => {
4878
+ if (deps.root === void 0) throw new Error("the preset library is not available");
4879
+ const loaded = loadPreset(deps.root(), name);
4880
+ if (loaded.summary.problems.length > 0) throw new Error(`it has problems: ${loaded.summary.problems.join("; ")}`);
4881
+ return loaded;
4882
+ };
4883
+ try {
4884
+ return {
4885
+ preset: load(),
4886
+ unavailable: []
4887
+ };
4888
+ } catch (error) {
4889
+ const reason = error instanceof Error ? error.message : String(error);
4890
+ if (asked !== void 0) throw new InvalidStartError(`the preset "${name}" cannot be used: ${reason}`);
4891
+ return { unavailable: [`The world suggests the preset "${name}", which cannot be used here (${reason}), so the story is told in the default style.`] };
4892
+ }
4893
+ }
4527
4894
  /** What a story opens with when its world gives no opening: a chat with nothing in it is DSH's start screen, which has none of the story's tabs. */
4528
4895
  const DEFAULT_OPENING = "The story begins.";
4529
4896
  /** The opening of the world with each speaker's name, or the reason it cannot be used. */
@@ -4880,6 +5247,7 @@ async function worldCharacters(worlds, worldId) {
4880
5247
  })),
4881
5248
  startingTime: world.clock?.startingTime === void 0 ? "" : localInstant(world.clock.startingTime, world.clock.timeZone ?? "UTC"),
4882
5249
  timeZone: world.clock?.timeZone ?? "",
5250
+ ...world.preset === void 0 ? {} : { preset: world.preset },
4883
5251
  problems
4884
5252
  };
4885
5253
  }
@@ -5052,7 +5420,8 @@ async function loadWorld(folder, dir, file, problems) {
5052
5420
  ...world.alternateOpenings === void 0 ? {} : { alternateOpenings: world.alternateOpenings },
5053
5421
  ...world.rules === void 0 ? {} : { rules: world.rules },
5054
5422
  ...world.tools === void 0 ? {} : { tools: world.tools },
5055
- ...world.narrationVoice === void 0 ? {} : { narrationVoice: world.narrationVoice }
5423
+ ...world.narrationVoice === void 0 ? {} : { narrationVoice: world.narrationVoice },
5424
+ ...world.preset === void 0 ? {} : { preset: world.preset }
5056
5425
  };
5057
5426
  if (world.coverMediaId !== void 0) try {
5058
5427
  record.cover = await resolveCover(dir, world.coverMediaId);
@@ -5182,6 +5551,7 @@ var WorldCatalog = class extends Service {
5182
5551
  //#endregion
5183
5552
  //#region src/shared/catalogue.ts
5184
5553
  const CATALOGUE_CARD_TYPE = "sillytavern/character";
5554
+ const CATALOGUE_PRESET_TYPE = "sillytavern/preset";
5185
5555
  const CATALOGUE_WORLD_TYPE = "dsh-agentic-roleplay/world";
5186
5556
  //#endregion
5187
5557
  //#region src/host/catalogue/origin.ts
@@ -5466,6 +5836,56 @@ async function installWorld(catalog, archive, maxBytes, options) {
5466
5836
  }
5467
5837
  }
5468
5838
  //#endregion
5839
+ //#region src/host/catalogue/trash.ts
5840
+ const DAY_MS = 864e5;
5841
+ const STAMP = /-(\d{4}-\d{2}-\d{2})T(\d{2})-(\d{2})-(\d{2})-(\d{3})Z$/;
5842
+ /** Staging folders left by an interrupted install are removed after this long. */
5843
+ const STALE_STAGING_MS = DAY_MS;
5844
+ /** When a world's old files were moved to the trash, read from the folder name the update gave them; entries without such a stamp are not ours. */
5845
+ function movedAt(name) {
5846
+ const match = STAMP.exec(name);
5847
+ if (match === null) return void 0;
5848
+ const time = Date.parse(`${match[1]}T${match[2]}:${match[3]}:${match[4]}.${match[5]}Z`);
5849
+ return Number.isNaN(time) ? void 0 : time;
5850
+ }
5851
+ async function entries(directory) {
5852
+ try {
5853
+ return await readdir(directory);
5854
+ } catch (error) {
5855
+ if (error.code === "ENOENT") return [];
5856
+ throw error;
5857
+ }
5858
+ }
5859
+ /**
5860
+ * Removes what updates and installs leave behind: world files moved to `.trash/worlds/` more than `keepDays` days ago (0 keeps them for
5861
+ * good), and staging folders of an install that never finished. A folder that cannot be removed is reported and the rest still goes.
5862
+ * @returns the folders removed and the failures.
5863
+ */
5864
+ async function pruneTrash(root, keepDays, now = Date.now()) {
5865
+ const removed = [];
5866
+ const failures = [];
5867
+ const drop = async (path, label) => {
5868
+ try {
5869
+ await rm(path, {
5870
+ recursive: true,
5871
+ force: true
5872
+ });
5873
+ removed.push(label);
5874
+ } catch (error) {
5875
+ failures.push(`${label}: ${error instanceof Error ? error.message : String(error)}`);
5876
+ }
5877
+ };
5878
+ if (keepDays > 0) for (const name of await entries(join(root, ".trash", "worlds"))) {
5879
+ const at = movedAt(name);
5880
+ if (at !== void 0 && now - at > keepDays * DAY_MS) await drop(join(root, ".trash", "worlds", name), `.trash/worlds/${name}`);
5881
+ }
5882
+ for (const name of await entries(join(root, ".install"))) if (now - (await stat(join(root, ".install", name))).mtimeMs > STALE_STAGING_MS) await drop(join(root, ".install", name), `.install/${name}`);
5883
+ return {
5884
+ removed,
5885
+ failures
5886
+ };
5887
+ }
5888
+ //#endregion
5469
5889
  //#region src/host/catalogue/transport.ts
5470
5890
  const TIMEOUT_MS = 6e4;
5471
5891
  const MAX_REDIRECTS = 5;
@@ -5594,6 +6014,12 @@ const object = (value, what) => {
5594
6014
  };
5595
6015
  const text = (value) => typeof value === "string" ? value : "";
5596
6016
  const count = (value) => typeof value === "number" && Number.isFinite(value) ? value : 0;
6017
+ const kindOf = (type) => type === "sillytavern/character" ? "card" : type === "sillytavern/preset" ? "preset" : "world";
6018
+ const TYPES = {
6019
+ world: CATALOGUE_WORLD_TYPE,
6020
+ card: CATALOGUE_CARD_TYPE,
6021
+ preset: CATALOGUE_PRESET_TYPE
6022
+ };
5597
6023
  function worldOf(value) {
5598
6024
  const item = object(value, "a world entry");
5599
6025
  const metadata = object(item["metadata"], "a world entry without metadata");
@@ -5602,7 +6028,7 @@ function worldOf(value) {
5602
6028
  const visibility = text(metadata["visibility"]);
5603
6029
  return {
5604
6030
  id,
5605
- kind: text(item["resourceType"]) === "sillytavern/character" ? "card" : "world",
6031
+ kind: kindOf(text(item["resourceType"])),
5606
6032
  name: text(metadata["name"]),
5607
6033
  description: text(metadata["description"]),
5608
6034
  tags: Array.isArray(metadata["tags"]) ? metadata["tags"].filter((tag) => typeof tag === "string") : [],
@@ -5644,7 +6070,7 @@ var Catalogue = class {
5644
6070
  const source = this.deps.sources.get(name);
5645
6071
  const offset = Math.max(0, Math.floor(query.offset ?? 0));
5646
6072
  const params = {
5647
- resourceType: query.kind === "card" ? CATALOGUE_CARD_TYPE : CATALOGUE_WORLD_TYPE,
6073
+ resourceType: TYPES[query.kind ?? "world"],
5648
6074
  publishedOnly: "true",
5649
6075
  limit: String(24),
5650
6076
  offset: String(offset),
@@ -5779,11 +6205,22 @@ var Catalogue = class {
5779
6205
  if (reply.body.length < 4 || reply.body.readUInt32LE(0) !== 67324752) throw new CatalogueError(`${url.href} did not answer with a ZIP archive${reply.type === "" ? "" : ` (it sent ${reply.type})`}`, "invalid");
5780
6206
  if (request.sha256 !== void 0 && createHash("sha256").update(reply.body).digest("hex") !== request.sha256.toLowerCase()) throw new CatalogueError("the download does not match the checksum the catalogue published for it", "invalid");
5781
6207
  const provenance = await this.provenance(url);
5782
- return await installWorld(this.deps.worlds, reply.body, this.deps.maxBytes(), {
6208
+ const installed = await installWorld(this.deps.worlds, reply.body, this.deps.maxBytes(), {
5783
6209
  mode: request.mode ?? "new",
5784
6210
  overwriteChanges: request.overwriteChanges === true,
5785
6211
  ...provenance === void 0 ? {} : { provenance }
5786
6212
  });
6213
+ if (installed.backup !== void 0) await this.tidy();
6214
+ return installed;
6215
+ }
6216
+ /** Deletes what updates left in the trash past the configured time; a failure is reported and never fails the request that triggered it. */
6217
+ async tidy() {
6218
+ try {
6219
+ const { failures } = await pruneTrash(dirname(this.deps.worlds.directory), this.deps.trashDays());
6220
+ for (const failure of failures) this.deps.problem(`could not remove old catalogue files: ${failure}`);
6221
+ } catch (error) {
6222
+ this.deps.problem(`could not clean the catalogue trash: ${error instanceof Error ? error.message : String(error)}`);
6223
+ }
5787
6224
  }
5788
6225
  /**
5789
6226
  * Turns a character card of a catalogue into a new world: the card is downloaded with the catalogue's key and handed to the importer, whose
@@ -5792,9 +6229,35 @@ var Catalogue = class {
5792
6229
  */
5793
6230
  async convert(request) {
5794
6231
  const source = this.deps.sources.get(request.source);
5795
- const versionId = this.identifier(request.versionId);
6232
+ const release = await this.download(source, request.versionId, CATALOGUE_CARD_TYPE, "a character card");
6233
+ const png = release.body.subarray(0, 4).equals(Buffer.from([
6234
+ 137,
6235
+ 80,
6236
+ 78,
6237
+ 71
6238
+ ]));
6239
+ const converted = await this.deps.importCard(release.body, `${release.versionId}${png ? ".png" : ".json"}`, request.language);
6240
+ const record = await this.deps.worlds.find(converted.worldId);
6241
+ if (record === void 0) throw new CatalogueError("the world was made but cannot be found", "upstream");
6242
+ await writeOrigin(record.directory, {
6243
+ kind: "card",
6244
+ root: source.root,
6245
+ resourceId: release.resourceId,
6246
+ versionId: release.versionId,
6247
+ version: release.version
6248
+ });
6249
+ return converted;
6250
+ }
6251
+ /** Turns a SillyTavern preset of a catalogue into a new library preset, which its authoring chat fills with prompts. Like a card it is never an update of an earlier one. */
6252
+ async convertPreset(request) {
6253
+ const release = await this.download(this.deps.sources.get(request.source), request.versionId, CATALOGUE_PRESET_TYPE, "a preset");
6254
+ return await this.deps.importPreset(release.body, request.title ?? "", request.language);
6255
+ }
6256
+ /** A release of the given type, downloaded with the catalogue's key and checked against the checksum it published. */
6257
+ async download(source, versionIdText, type, what) {
6258
+ const versionId = this.identifier(versionIdText);
5796
6259
  const version = object(await this.json(source, `/versions/${versionId}`), "a version");
5797
- if (text(version["resourceType"]) !== "sillytavern/character") throw new CatalogueError("that release is not a character card", "invalid");
6260
+ if (text(version["resourceType"]) !== type) throw new CatalogueError(`that release is not ${what}`, "invalid");
5798
6261
  const resourceId = text(version["resourceId"]);
5799
6262
  if (!ID.test(resourceId)) throw unexpected("a version without a resource id");
5800
6263
  const reply = await fetchAddress(this.address(source, `/versions/${versionId}/download`), {
@@ -5805,23 +6268,12 @@ var Catalogue = class {
5805
6268
  });
5806
6269
  const sha256 = text(version["artifactSha256"]);
5807
6270
  if (sha256 !== "" && createHash("sha256").update(reply.body).digest("hex") !== sha256.toLowerCase()) throw new CatalogueError("the download does not match the checksum the catalogue published for it", "invalid");
5808
- const png = reply.body.subarray(0, 4).equals(Buffer.from([
5809
- 137,
5810
- 80,
5811
- 78,
5812
- 71
5813
- ]));
5814
- const converted = await this.deps.importCard(reply.body, `${versionId}${png ? ".png" : ".json"}`, request.language);
5815
- const record = await this.deps.worlds.find(converted.worldId);
5816
- if (record === void 0) throw new CatalogueError("the world was made but cannot be found", "upstream");
5817
- await writeOrigin(record.directory, {
5818
- kind: "card",
5819
- root: source.root,
5820
- resourceId,
6271
+ return {
6272
+ body: reply.body,
5821
6273
  versionId,
6274
+ resourceId,
5822
6275
  version: text(version["version"])
5823
- });
5824
- return converted;
6276
+ };
5825
6277
  }
5826
6278
  /** The catalogue release an address names, when it is a release download on a configured catalogue. */
5827
6279
  async provenance(url) {
@@ -6047,6 +6499,7 @@ const Config = z.object({
6047
6499
  apiKey: z.string().default("")
6048
6500
  })).default([]).volatile(),
6049
6501
  catalogueMaxBytes: z.number().min(1024).default(268435456),
6502
+ catalogueTrashDays: z.number().min(0).default(30),
6050
6503
  importMaxUploadBytes: z.number().min(1024).default(104857600),
6051
6504
  importConcurrency: z.number().min(1).max(16).default(4),
6052
6505
  importReasoning: z.string().default(""),
@@ -6084,6 +6537,7 @@ function apply(ctx, config) {
6084
6537
  const languages = new AuthoringLanguages(ctx, () => join(root.path, "worlds"));
6085
6538
  const agent = new AgentSessions(agentServicesOf(ctx), () => root.path);
6086
6539
  const authoring = new AuthoringSessions(agentServicesOf(ctx), () => root.path, prompts, languages);
6540
+ const presetAuthoring = new PresetAuthoringSessions(agentServicesOf(ctx), () => root.path, prompts, languages);
6087
6541
  const doctors = new DoctorSessions(agentServicesOf(ctx), () => root.path, prompts);
6088
6542
  const imports = new ImportStore(() => root.path);
6089
6543
  new RoleplayImport(ctx, {
@@ -6110,24 +6564,41 @@ function apply(ctx, config) {
6110
6564
  state: ctx.roleplayState,
6111
6565
  agent,
6112
6566
  sessionsDir: () => join(root.path, "sessions"),
6567
+ root: () => root.path,
6113
6568
  avatars: () => avatarsWorld(root.path)
6114
6569
  };
6570
+ const presetLibrary = new Presets({
6571
+ root: () => root.path,
6572
+ worldDirectory: async (worldId) => (await worlds.find(worldId))?.directory,
6573
+ authoring: presetAuthoring,
6574
+ importMessage: narrationImportMessage,
6575
+ maxUploadBytes: config.importMaxUploadBytes,
6576
+ playing: (id) => ctx.roleplayOrchestration.playing(chatSessionId(id))
6577
+ });
6578
+ const catalogue = new Catalogue({
6579
+ sources: new Sources(() => config.catalogues.get()),
6580
+ worlds,
6581
+ maxBytes: () => config.catalogueMaxBytes,
6582
+ maxCardBytes: () => config.importMaxUploadBytes,
6583
+ trashDays: () => config.catalogueTrashDays,
6584
+ problem: (message) => {
6585
+ ctx.logger.warn(message);
6586
+ },
6587
+ importPreset: async (file, title, language) => await presetLibrary.importTitled(title, file, language),
6588
+ importCard: async (file, name, language) => {
6589
+ const preview = await imports.create(readCard(file, name), isPng(file) ? pictureOf(file) : void 0);
6590
+ return await startImport({
6591
+ store: imports,
6592
+ worlds,
6593
+ authoring,
6594
+ chatTitle: (title) => `Authoring ${title}`
6595
+ }, preview.id, preview.choices, language);
6596
+ }
6597
+ });
6598
+ catalogue.tidy();
6115
6599
  registerApi(ctx, {
6116
- catalogue: new Catalogue({
6117
- sources: new Sources(() => config.catalogues.get()),
6118
- worlds,
6119
- maxBytes: () => config.catalogueMaxBytes,
6120
- maxCardBytes: () => config.importMaxUploadBytes,
6121
- importCard: async (file, name, language) => {
6122
- const preview = await imports.create(readCard(file, name), isPng(file) ? pictureOf(file) : void 0);
6123
- return await startImport({
6124
- store: imports,
6125
- worlds,
6126
- authoring,
6127
- chatTitle: (title) => `Authoring ${title}`
6128
- }, preview.id, preview.choices, language);
6129
- }
6130
- }),
6600
+ catalogue,
6601
+ presets: presetLibrary,
6131
6602
  packs: new InterfaceService({
6132
6603
  open: (id) => ctx.roleplayState.openSession(deps.sessionsDir(), id),
6133
6604
  root: () => root.path,