@dsh-agentic-roleplay/core 0.7.3 → 0.8.2

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.
Files changed (58) hide show
  1. package/README.md +1 -1
  2. package/lib/{access-DtOxFKBK.js → access-CfQWbNZP.js} +4 -7
  3. package/lib/authoring-tools-BgmC2oqU.js +3691 -0
  4. package/lib/authoring.js +59 -24
  5. package/lib/{child-CyCViUKa.js → child-lYYeOwl2.js} +147 -46
  6. package/lib/client.js +931 -860
  7. package/lib/client.js.map +1 -1
  8. package/lib/compaction.js +8 -13
  9. package/lib/{doctor-chat-ByDfCMeo.js → doctor-chat-hL1a61hp.js} +2 -3
  10. package/lib/doctor.js +6 -8
  11. package/lib/{gallery-DmBZ2L5l.js → gallery-D1V4h8vh.js} +3 -4
  12. package/lib/{generator-BmdoTIL2.js → generator-CLPosooC.js} +3 -4
  13. package/lib/helper.js +2 -3
  14. package/lib/{hooks-C8eELrni.js → hooks-rQTTeOC4.js} +144 -88
  15. package/lib/images.js +4 -4
  16. package/lib/index.js +1024 -913
  17. package/lib/{library-lVNf9qG_.js → library-Dfu0dgRj.js} +32 -21
  18. package/lib/{lorebook-author-tools-BDOs4i5C.js → lorebook-author-tools-BAaAqZZm.js} +11 -14
  19. package/lib/lorebook-authoring.js +5 -6
  20. package/lib/{narration-DvFBqmqe.js → narration-CjB_KwwY.js} +58 -20
  21. package/lib/orchestration.js +138 -72
  22. package/lib/{pack-jail-DeTscGT2.js → pack-jail-CN-314ln.js} +94 -137
  23. package/lib/{packs-CY8ms8v_.js → packs-DIj5BEfq.js} +6 -10
  24. package/lib/{presenter-agent-cFR4sos3.js → presenter-agent-DR0KvLE0.js} +13 -19
  25. package/lib/{preset-n4-j6k4S.js → preset-CscHj1-Y.js} +5 -5
  26. package/lib/{preset-author-tools-2m4BLtws.js → preset-author-tools-CxluF8qc.js} +13 -15
  27. package/lib/preset-authoring.js +6 -7
  28. package/lib/presets-HB8kJTaE.js +3993 -0
  29. package/lib/{reflection-CJB5s0ip.js → reflection-Dg1xtOTW.js} +137 -148
  30. package/lib/{text-Cwl5s0j0.js → text-DyRN4nDP.js} +13 -7
  31. package/lib/{turn-guard-BvCs1VxB.js → turn-guard-g_hDn0jR.js} +6 -7
  32. package/package.json +29 -29
  33. package/prompts/en/import/casting.md +14 -0
  34. package/prompts/en/import/character.md +1 -0
  35. package/prompts/en/import/classifier.md +9 -0
  36. package/prompts/en/import/finish-draft.md +1 -0
  37. package/prompts/en/import/generator.md +8 -0
  38. package/prompts/en/import/notice-repair.md +4 -0
  39. package/prompts/en/import/repair-again.md +3 -0
  40. package/prompts/en/import/roster.md +1 -0
  41. package/prompts/en/import/variables.md +1 -0
  42. package/prompts/en/roleplay/game-master.md +3 -0
  43. package/prompts/en/roleplay/standing.md +1 -0
  44. package/prompts/en/world/author.md +1 -1
  45. package/prompts/zh/import/casting.md +14 -0
  46. package/prompts/zh/import/character.md +1 -0
  47. package/prompts/zh/import/classifier.md +9 -0
  48. package/prompts/zh/import/finish-draft.md +1 -0
  49. package/prompts/zh/import/generator.md +8 -0
  50. package/prompts/zh/import/notice-repair.md +4 -0
  51. package/prompts/zh/import/repair-again.md +3 -0
  52. package/prompts/zh/import/roster.md +1 -0
  53. package/prompts/zh/import/variables.md +1 -0
  54. package/prompts/zh/roleplay/game-master.md +3 -0
  55. package/prompts/zh/roleplay/standing.md +1 -0
  56. package/prompts/zh/world/author.md +1 -1
  57. package/lib/authoring-tools-CIsWbq0B.js +0 -6317
  58. package/lib/route-C6SHz9j2.js +0 -518
@@ -1,10 +1,10 @@
1
- import "./access-DtOxFKBK.js";
2
- import { i as translator, n as withoutExtension, r as languageOf, t as trimCharacters } from "./text-Cwl5s0j0.js";
1
+ import "./access-CfQWbNZP.js";
2
+ import { i as translator, n as withoutExtension, r as languageOf, t as trimCharacters } from "./text-DyRN4nDP.js";
3
3
  import { dirname, extname, join } from "node:path";
4
4
  import { existsSync } from "node:fs";
5
5
  import { createHash, randomUUID } from "node:crypto";
6
6
  import { copyFile, link, mkdir, readFile, readdir, rename, unlink, writeFile } from "node:fs/promises";
7
- //#region src/host/import/run.ts
7
+ // src/host/import/run.ts
8
8
  /** The labels of a job's prompt, in the language of the import. */
9
9
  const trOf = (ctx) => translator(languageOf(ctx.language));
10
10
  /** Runs `work` on every item, at most `limit` at a time, and returns the results in the order of the items. */
@@ -38,8 +38,7 @@ function uniqueKey(base, taken) {
38
38
  taken.add(key);
39
39
  return key;
40
40
  }
41
- //#endregion
42
- //#region src/host/images/png-metadata.ts
41
+ // src/host/images/png-metadata.ts
43
42
  const SIGNATURE = Buffer.from([
44
43
  137,
45
44
  80,
@@ -145,8 +144,7 @@ function readPngMetadata(png) {
145
144
  return;
146
145
  }
147
146
  }
148
- //#endregion
149
- //#region src/host/images/image-files.ts
147
+ // src/host/images/image-files.ts
150
148
  /** Raster formats a person may bring. Only PNG can carry its metadata inside; the others have a sidecar `<file>.json`. */
151
149
  const MEDIA_TYPES = {
152
150
  ".png": "image/png",
@@ -180,8 +178,7 @@ async function writeImageFileMetadata(file, metadata) {
180
178
  if (extname(file).toLowerCase() === ".png") await replaceAtomically(file, withPngMetadata(await readFile(file), metadata));
181
179
  else await replaceAtomically(sidecarOf(file), `${JSON.stringify(metadata, null, 2)}\n`);
182
180
  }
183
- //#endregion
184
- //#region src/host/images/storage.ts
181
+ // src/host/images/storage.ts
185
182
  const PLURAL = {
186
183
  character: "characters",
187
184
  location: "locations",
@@ -280,12 +277,10 @@ async function linkOrCopy(from, to) {
280
277
  await copyFile(from, to);
281
278
  }
282
279
  }
283
- //#endregion
284
- //#region src/shared/gallery.ts
280
+ // src/shared/gallery.ts
285
281
  /** The folder of images the person brought, inside a session's `images/`. */
286
282
  const LIBRARY_FOLDER = "library";
287
- //#endregion
288
- //#region src/host/images/library.ts
283
+ // src/host/images/library.ts
289
284
  const MAGIC = [
290
285
  {
291
286
  type: ".png",
@@ -338,14 +333,22 @@ function libraryName(original, extension, taken) {
338
333
  }
339
334
  }
340
335
  /**
341
- * Puts an image the person brought into the session's library, with metadata that says where it came from and that it is reusable. What it
342
- * shows is not known yet: the interpreter fills that in.
336
+ * Puts a picture into the library unless the library already holds that picture (the same image, whatever metadata the file carries), whatever it came from: an upload, an address, a generation.
337
+ * Metadata says where it came from and that it is reusable; what it shows is not known yet, and the interpreter fills that in.
338
+ * @param known the hashes of the library when the caller has read them (kept up to date here), to avoid reading the library again for each picture.
343
339
  * @throws {Error} when the bytes are not a PNG, JPEG, WebP or GIF image.
344
- * @returns its path under `images/`.
340
+ * @returns its path under `images/`, and whether it was added or an equal picture was there.
345
341
  */
346
- async function addToLibrary(sessionFolder, bytes, originalName) {
342
+ async function addToLibraryOnce(sessionFolder, bytes, originalName, known) {
347
343
  const extension = imageExtensionOf(bytes);
348
344
  if (extension === void 0 || MEDIA_TYPES[extension] === void 0) throw new Error("only PNG, JPEG, WebP and GIF images can be added");
345
+ const hashes = known ?? await libraryHashes(sessionFolder);
346
+ const hash = pictureHash(bytes);
347
+ const present = hashes.get(hash);
348
+ if (present !== void 0) return {
349
+ path: present,
350
+ added: false
351
+ };
349
352
  const directory = join(sessionFolder, "images", LIBRARY_FOLDER);
350
353
  await mkdir(directory, { recursive: true });
351
354
  const name = libraryName(originalName, extension, (candidate) => existsSync(join(directory, candidate)));
@@ -363,9 +366,17 @@ async function addToLibrary(sessionFolder, bytes, originalName) {
363
366
  subjects: [],
364
367
  tags: []
365
368
  });
366
- return `${LIBRARY_FOLDER}/${name}`;
369
+ const path = `${LIBRARY_FOLDER}/${name}`;
370
+ hashes.set(hash, path);
371
+ return {
372
+ path,
373
+ added: true
374
+ };
375
+ }
376
+ /** {@link addToLibraryOnce} for a caller that only wants the path: a picture the library holds already is not added again. */
377
+ async function addToLibrary(sessionFolder, bytes, originalName) {
378
+ return (await addToLibraryOnce(sessionFolder, bytes, originalName)).path;
367
379
  }
368
- //#endregion
369
- export { trOf as C, slug as S, writeImageFileMetadata as _, LIBRARY_FOLDER as a, mapLimit as b, nextSceneSerial as c, sceneImagePath as d, writeImage as f, sidecarOf as g, readImageFileMetadata as h, pictureHash as i, readImageMetadata as l, mediaTypeOf as m, imageExtensionOf as n, entityImageDirectory as o, MEDIA_TYPES as p, libraryHashes as r, inheritImages as s, addToLibrary as t, referenceImagePath as u, readPngMetadata as v, uniqueKey as w, norm as x, withPngMetadata as y };
380
+ export { trOf as C, slug as S, writeImageFileMetadata as _, LIBRARY_FOLDER as a, mapLimit as b, nextSceneSerial as c, sceneImagePath as d, writeImage as f, sidecarOf as g, readImageFileMetadata as h, libraryHashes as i, readImageMetadata as l, mediaTypeOf as m, addToLibraryOnce as n, entityImageDirectory as o, MEDIA_TYPES as p, imageExtensionOf as r, inheritImages as s, addToLibrary as t, referenceImagePath as u, readPngMetadata as v, uniqueKey as w, norm as x, withPngMetadata as y };
370
381
 
371
- //# sourceMappingURL=library-lVNf9qG_.js.map
382
+ //# sourceMappingURL=library-Dfu0dgRj.js.map
@@ -1,14 +1,14 @@
1
- import "./access-DtOxFKBK.js";
2
- import { Dt as IMPORT_FOLDER, Gt as loadLorebook, Ht as entriesOf, Kt as lorebookDirectory, Vt as deleteLorebookEntry, Xt as writeLorebookEntry, Yt as updateLorebookInfo, dt as cleanText, hn as LORE_SCOPES, i as contextOf, mn as LORE_KINDS, mt as withPlayer, pt as partsOf, qt as readLorebook$1, r as MAX_PREVIEW_TEXT, un as ImportCardError } from "./authoring-tools-CIsWbq0B.js";
3
- import { c as createPresetFromGuidance } from "./route-C6SHz9j2.js";
4
- import { nt as shippedPromptsDirectory } from "./hooks-C8eELrni.js";
5
- import { i as translator, r as languageOf } from "./text-Cwl5s0j0.js";
6
- import { C as trOf, S as slug, b as mapLimit, w as uniqueKey } from "./library-lVNf9qG_.js";
1
+ import "./access-CfQWbNZP.js";
2
+ import { H as deleteLorebookEntry, J as readLorebook$1, K as loadLorebook, Pt as ImportCardError, U as entriesOf, X as updateLorebookInfo, Z as writeLorebookEntry, i as contextOf, q as lorebookDirectory, r as MAX_PREVIEW_TEXT } from "./authoring-tools-BgmC2oqU.js";
3
+ import { J as cleanText, Nt as LORE_KINDS, Pt as LORE_SCOPES, Q as IMPORT_FOLDER, X as partsOf, Z as withPlayer, n as createPresetFromGuidance } from "./presets-HB8kJTaE.js";
4
+ import { C as trOf, S as slug, b as mapLimit, w as uniqueKey } from "./library-Dfu0dgRj.js";
5
+ import { i as translator, r as languageOf } from "./text-DyRN4nDP.js";
6
+ import { at as shippedPromptsDirectory } from "./hooks-rQTTeOC4.js";
7
7
  import { join } from "node:path";
8
8
  import { existsSync, readFileSync } from "node:fs";
9
- import { defineTool } from "@deepseek-ai/dsh-tools";
10
9
  import { readFile, rm, writeFile } from "node:fs/promises";
11
- //#region src/shared/lorebook-chat.ts
10
+ import { defineTool } from "@deepseek-ai/dsh-tools";
11
+ // src/shared/lorebook-chat.ts
12
12
  const PREFIX = "lorebook-author-";
13
13
  /** Stable DSH chat identity for the authoring conversation of one library lorebook. */
14
14
  function lorebookChatSessionId(name) {
@@ -25,8 +25,7 @@ function lorebookOfChat(chatSessionId) {
25
25
  return;
26
26
  }
27
27
  }
28
- //#endregion
29
- //#region src/host/import/lorebook.ts
28
+ // src/host/import/lorebook.ts
30
29
  const MAX_ENTRIES = 500;
31
30
  const BATCH_CHARS = 6e3;
32
31
  const BATCH_ENTRIES = 8;
@@ -322,8 +321,7 @@ async function classifyEntries(ctx, book) {
322
321
  }
323
322
  return result;
324
323
  }
325
- //#endregion
326
- //#region src/host/authoring/lorebook-author-tools.ts
324
+ // src/host/authoring/lorebook-author-tools.ts
327
325
  const LOREBOOK_MUTATION_TOOLS = [
328
326
  "write_lorebook_entry",
329
327
  "delete_lorebook_entry",
@@ -696,7 +694,6 @@ async function lorebookApproval(exec, next, state) {
696
694
  }
697
695
  };
698
696
  }
699
- //#endregion
700
697
  export { lorebookImportMessage as a, readLorebook as c, lorebookOfChat as d, lorebookAuthorTools as i, selectEntries as l, LorebookReader as n, pendingTargets as o, lorebookApproval as r, previewLorebook as s, LOREBOOK_UPLOAD as t, lorebookChatSessionId as u };
701
698
 
702
- //# sourceMappingURL=lorebook-author-tools-BDOs4i5C.js.map
699
+ //# sourceMappingURL=lorebook-author-tools-BAaAqZZm.js.map
@@ -1,10 +1,10 @@
1
- import { Kt as lorebookDirectory, n as limitAuthoringAgents } from "./authoring-tools-CIsWbq0B.js";
2
- import { d as lorebookOfChat, i as lorebookAuthorTools, n as LorebookReader, o as pendingTargets, r as lorebookApproval } from "./lorebook-author-tools-BDOs4i5C.js";
3
- import "./hooks-C8eELrni.js";
4
- import { t as guardAuthoringTurns } from "./turn-guard-BvCs1VxB.js";
1
+ import { n as limitAuthoringAgents, q as lorebookDirectory } from "./authoring-tools-BgmC2oqU.js";
2
+ import { t as guardAuthoringTurns } from "./turn-guard-g_hDn0jR.js";
3
+ import { d as lorebookOfChat, i as lorebookAuthorTools, n as LorebookReader, o as pendingTargets, r as lorebookApproval } from "./lorebook-author-tools-BAaAqZZm.js";
4
+ import "./hooks-rQTTeOC4.js";
5
5
  import { join } from "node:path";
6
6
  import { existsSync, readdirSync } from "node:fs";
7
- //#region src/lorebook-authoring.ts
7
+ // src/lorebook-authoring.ts
8
8
  const name = "@dsh-agentic-roleplay/core/lorebook-authoring";
9
9
  const inject = [
10
10
  "roleplayRoot",
@@ -68,7 +68,6 @@ function apply(ctx) {
68
68
  text: (context) => ctx.roleplayPrompts.text(id, languageOf(context))
69
69
  });
70
70
  }
71
- //#endregion
72
71
  export { apply, inject, name };
73
72
 
74
73
  //# sourceMappingURL=lorebook-authoring.js.map
@@ -1,14 +1,15 @@
1
- import "./access-DtOxFKBK.js";
2
- import { join } from "node:path";
3
- import { existsSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
4
- //#region ../dsh-roleplay-model/src/clock.ts
1
+ import "./access-CfQWbNZP.js";
2
+ import { basename, dirname, join } from "node:path";
3
+ import { chmodSync, existsSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
4
+ import { randomUUID } from "node:crypto";
5
+ import { chmod, mkdir, rename, rm, stat, writeFile } from "node:fs/promises";
6
+ // ../dsh-roleplay-model/src/clock.ts
5
7
  /**
6
8
  * How the story's time passes: `simulated` moves only when the story does (the game master says how
7
9
  * long things take and may skip ahead), `real` follows the wall clock.
8
10
  */
9
11
  const CLOCK_MODES = ["simulated", "real"];
10
- //#endregion
11
- //#region src/shared/decisions.ts
12
+ // src/shared/decisions.ts
12
13
  /** The places where a language model's closed-choice judgement can be given to a decision model instead, or, for the gates, a check can be made at all. */
13
14
  const DECISION_SITES = [
14
15
  "triggers",
@@ -52,8 +53,7 @@ const emptyStats = (since) => ({
52
53
  since,
53
54
  sites: Object.fromEntries(DECISION_SITES.map((site) => [site, emptySiteStats()]))
54
55
  });
55
- //#endregion
56
- //#region src/shared/rules.ts
56
+ // src/shared/rules.ts
57
57
  /** The key under which a session keeps the rules of its world, which it copies at the start so that it stays whole without the world's files. */
58
58
  const WORLD_RULES_KEY = "rules";
59
59
  /** The longest text of the player's own rules. */
@@ -184,7 +184,7 @@ const SETTINGS_SPEC = {
184
184
  judgedPerTurn: integer(0, 5, 2)
185
185
  },
186
186
  decisions: {
187
- triggers: choice(DECISION_MODES, "llm"),
187
+ triggers: choice(DECISION_MODES, "hybrid"),
188
188
  reflection: choice(DECISION_MODES, "llm"),
189
189
  agency: choice(GATE_MODES, "off"),
190
190
  refusal: choice(GATE_MODES, "off"),
@@ -319,8 +319,51 @@ function parseSettings(value) {
319
319
  value: parsed
320
320
  };
321
321
  }
322
- //#endregion
323
- //#region src/host/sessions/settings.ts
322
+ // src/host/atomic-write.ts
323
+ /**
324
+ * Replaces a file in one step: the data goes to a temporary file in the same folder and is renamed over the file, so an interruption leaves the old file or the new one,
325
+ * never half of either. Every writer of a file someone else may read at the same time goes through here.
326
+ */
327
+ async function atomicWrite(file, data, options = {}) {
328
+ if (options.createDirectory === true) await mkdir(dirname(file), { recursive: true });
329
+ const temporary = join(dirname(file), `.${basename(file)}.${process.pid}.${randomUUID()}.tmp`);
330
+ try {
331
+ const mode = options.mode ?? await stat(file).then((value) => value.mode & 511, (error) => {
332
+ if (error.code === "ENOENT") return 420;
333
+ throw error;
334
+ });
335
+ await writeFile(temporary, data, { mode });
336
+ await chmod(temporary, mode);
337
+ await rename(temporary, file);
338
+ } catch (error) {
339
+ await rm(temporary, { force: true });
340
+ throw error;
341
+ }
342
+ }
343
+ /** A JSON file written the same way, with the indentation given (none by default) and a final newline when it is indented. */
344
+ async function atomicWriteJson(file, value, options = {}) {
345
+ const { indent, ...rest } = options;
346
+ await atomicWrite(file, indent === void 0 ? JSON.stringify(value) : `${JSON.stringify(value, null, indent)}\n`, rest);
347
+ }
348
+ /** The synchronous form, for the few writers that cannot wait (the chat settings, read at every step). */
349
+ function atomicWriteSync(file, data) {
350
+ const temporary = join(dirname(file), `.${basename(file)}.${process.pid}.${randomUUID()}.tmp`);
351
+ try {
352
+ let mode = 420;
353
+ try {
354
+ mode = statSync(file).mode & 511;
355
+ } catch (error) {
356
+ if (error.code !== "ENOENT") throw error;
357
+ }
358
+ writeFileSync(temporary, data, { mode });
359
+ chmodSync(temporary, mode);
360
+ renameSync(temporary, file);
361
+ } catch (error) {
362
+ rmSync(temporary, { force: true });
363
+ throw error;
364
+ }
365
+ }
366
+ // src/host/sessions/settings.ts
324
367
  const SETTINGS_FILE = "settings.json";
325
368
  const known = /* @__PURE__ */ new Map();
326
369
  let childReasoning = () => "";
@@ -363,10 +406,7 @@ function readSettings(folder) {
363
406
  function writeSettings(folder, value) {
364
407
  const parsed = parseSettings(value);
365
408
  if (!parsed.ok) throw new InvalidSettingsError(parsed.problems);
366
- const file = join(folder, SETTINGS_FILE);
367
- const temporary = `${file}.${process.pid}.tmp`;
368
- writeFileSync(temporary, `${JSON.stringify(parsed.value, null, 2)}\n`);
369
- renameSync(temporary, file);
409
+ atomicWriteSync(join(folder, SETTINGS_FILE), `${JSON.stringify(parsed.value, null, 2)}\n`);
370
410
  known.delete(folder);
371
411
  return parsed.value;
372
412
  }
@@ -412,8 +452,7 @@ function loadedSettings(folder, problem) {
412
452
  return value;
413
453
  }
414
454
  }
415
- //#endregion
416
- //#region src/shared/narration.ts
455
+ // src/shared/narration.ts
417
456
  const OPEN = "<roleplay-output>";
418
457
  const CLOSE = "</roleplay-output>";
419
458
  const PREVIEW_OPEN = "<roleplay-preview>";
@@ -455,7 +494,6 @@ function decodeOutput(text) {
455
494
  return;
456
495
  }
457
496
  }
458
- //#endregion
459
- export { emptyStats as _, liveSettings as a, writeSettings as c, parseSettings as d, parseSwitchboard as f, GATE_SITES as g, DECISION_SITES as h, InvalidSettingsError as i, DEFAULT_SETTINGS as l, rulesOf as m, encodeOutput as n, readSettings as o, WORLD_RULES_KEY as p, encodePreview as r, useChildReasoning as s, decodeOutput as t, SETTINGS_SPEC as u };
497
+ export { DECISION_SITES as _, liveSettings as a, writeSettings as c, DEFAULT_SETTINGS as d, SETTINGS_SPEC as f, rulesOf as g, WORLD_RULES_KEY as h, InvalidSettingsError as i, atomicWrite as l, parseSwitchboard as m, encodeOutput as n, readSettings as o, parseSettings as p, encodePreview as r, useChildReasoning as s, decodeOutput as t, atomicWriteJson as u, GATE_SITES as v, emptyStats as y };
460
498
 
461
- //# sourceMappingURL=narration-DvFBqmqe.js.map
499
+ //# sourceMappingURL=narration-CjB_KwwY.js.map