@dsh-agentic-roleplay/core 0.3.0 → 0.3.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 (41) hide show
  1. package/lib/authoring.js +18 -223
  2. package/lib/client.js +1838 -489
  3. package/lib/client.js.map +1 -1
  4. package/lib/compaction.js +1 -1
  5. package/lib/deliver-BSsDtF6X.js +3443 -0
  6. package/lib/{documents-7GMc5NnW.js → documents-B5XAdfo2.js} +992 -14
  7. package/lib/{gallery-DUeBMHQX.js → gallery-DS5-qH9F.js} +2 -2
  8. package/lib/{generator-HPifO3Kk.js → generator-BzeepLd2.js} +3 -3
  9. package/lib/images.js +3 -3
  10. package/lib/index.js +351 -13
  11. package/lib/narration-DK2kiIjE.js +141 -0
  12. package/lib/orchestration.js +867 -1547
  13. package/lib/{packs-alJTiDM2.js → packs-BCGK9KZV.js} +51 -7
  14. package/lib/{presenter-agent-PX1WVaJB.js → presenter-agent-Cp-J0onD.js} +3 -3
  15. package/lib/{narration-CKZTk5UE.js → settings-CA6icLL_.js} +158 -142
  16. package/package.json +4 -4
  17. package/packs/scene/strip.js +1 -1
  18. package/prompts/en/roleplay/character-decide.md +1 -1
  19. package/prompts/en/roleplay/character-rules.md +1 -1
  20. package/prompts/en/roleplay/chronicler.md +1 -1
  21. package/prompts/en/roleplay/dice.md +7 -0
  22. package/prompts/en/roleplay/game-master.md +3 -1
  23. package/prompts/en/roleplay/reflection.md +4 -2
  24. package/prompts/en/roleplay/style.md +1 -1
  25. package/prompts/en/roleplay/suggester.md +1 -1
  26. package/prompts/en/translate/document.md +6 -0
  27. package/prompts/en/translate/glossary.md +5 -0
  28. package/prompts/en/translate/notice.md +2 -0
  29. package/prompts/zh/roleplay/character-decide.md +1 -1
  30. package/prompts/zh/roleplay/character-rules.md +1 -1
  31. package/prompts/zh/roleplay/chronicler.md +1 -1
  32. package/prompts/zh/roleplay/dice.md +7 -0
  33. package/prompts/zh/roleplay/game-master.md +3 -1
  34. package/prompts/zh/roleplay/reflection.md +4 -2
  35. package/prompts/zh/roleplay/style.md +1 -1
  36. package/prompts/zh/roleplay/suggester.md +1 -1
  37. package/prompts/zh/translate/document.md +6 -0
  38. package/prompts/zh/translate/glossary.md +5 -0
  39. package/prompts/zh/translate/notice.md +2 -0
  40. package/lib/deliver--uMhC6oO.js +0 -1878
  41. package/lib/src-CWTNeL6Y.js +0 -128
@@ -0,0 +1,141 @@
1
+ import { r as parseSettings, t as DEFAULT_SETTINGS } from "./settings-CA6icLL_.js";
2
+ import { join } from "node:path";
3
+ import { existsSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
4
+ //#region src/host/sessions/settings.ts
5
+ const SETTINGS_FILE = "settings.json";
6
+ const known = /* @__PURE__ */ new Map();
7
+ let childReasoning = "";
8
+ /** The thinking level helpers get when a chat's own setting is empty (the profile's choice); returns the function that takes it back. */
9
+ function useChildReasoning(level) {
10
+ childReasoning = level;
11
+ return () => {
12
+ childReasoning = "";
13
+ };
14
+ }
15
+ /** The settings are not acceptable; `problems` say what to fix. */
16
+ var InvalidSettingsError = class extends Error {
17
+ problems;
18
+ constructor(problems) {
19
+ super(`the settings are not acceptable: ${problems.join("; ")}`);
20
+ this.problems = problems;
21
+ }
22
+ };
23
+ /**
24
+ * The settings of the session in `folder`; a session that has none yet has the defaults.
25
+ * @throws {InvalidSettingsError} when the file exists and is not valid.
26
+ */
27
+ function readSettings(folder) {
28
+ const file = join(folder, SETTINGS_FILE);
29
+ if (!existsSync(file)) return { ...DEFAULT_SETTINGS };
30
+ let value;
31
+ try {
32
+ value = JSON.parse(readFileSync(file, "utf8"));
33
+ } catch (error) {
34
+ throw new InvalidSettingsError([`${SETTINGS_FILE} is not valid JSON (${error instanceof Error ? error.message : String(error)})`]);
35
+ }
36
+ const parsed = parseSettings(value);
37
+ if (!parsed.ok) throw new InvalidSettingsError(parsed.problems);
38
+ return parsed.value;
39
+ }
40
+ /**
41
+ * Store the settings of the session in `folder`, replacing what was there.
42
+ * @throws {InvalidSettingsError} when the value is not acceptable; nothing is written then.
43
+ */
44
+ function writeSettings(folder, value) {
45
+ const parsed = parseSettings(value);
46
+ if (!parsed.ok) throw new InvalidSettingsError(parsed.problems);
47
+ const file = join(folder, SETTINGS_FILE);
48
+ const temporary = `${file}.${process.pid}.tmp`;
49
+ writeFileSync(temporary, `${JSON.stringify(parsed.value, null, 2)}\n`);
50
+ renameSync(temporary, file);
51
+ known.delete(folder);
52
+ return parsed.value;
53
+ }
54
+ /**
55
+ * The settings to use right now. Called before each use, so an edit to the file applies at the
56
+ * next round, step or run without a restart; the file is parsed again only when it changed.
57
+ * A file that is not valid is reported once through `problem` and the last good settings
58
+ * (the defaults when there were none) stay in force, so a typo cannot stop a story in the middle.
59
+ */
60
+ function liveSettings(folder, problem = () => void 0) {
61
+ const value = loadedSettings(folder, problem);
62
+ return value.reasoning.children === "" && childReasoning !== "" ? {
63
+ ...value,
64
+ reasoning: {
65
+ ...value.reasoning,
66
+ children: childReasoning
67
+ }
68
+ } : value;
69
+ }
70
+ function loadedSettings(folder, problem) {
71
+ let modified = -1;
72
+ try {
73
+ modified = statSync(join(folder, SETTINGS_FILE)).mtimeMs;
74
+ } catch {}
75
+ const last = known.get(folder);
76
+ if (last?.modified === modified) return last.value;
77
+ try {
78
+ const value = readSettings(folder);
79
+ known.set(folder, {
80
+ modified,
81
+ value
82
+ });
83
+ return value;
84
+ } catch (error) {
85
+ if (!(error instanceof InvalidSettingsError)) throw error;
86
+ problem(`${error.message} (the last good settings stay in force)`);
87
+ const value = last?.value ?? { ...DEFAULT_SETTINGS };
88
+ known.set(folder, {
89
+ modified,
90
+ value
91
+ });
92
+ return value;
93
+ }
94
+ }
95
+ //#endregion
96
+ //#region src/shared/narration.ts
97
+ const OPEN = "<roleplay-output>";
98
+ const CLOSE = "</roleplay-output>";
99
+ const PREVIEW_OPEN = "<roleplay-preview>";
100
+ const PREVIEW_CLOSE = "</roleplay-preview>";
101
+ /** What the game master shows the player while the characters are still being asked: a scene-setting opening that the delivered output replaces in the view. */
102
+ function encodePreview(text) {
103
+ return `${PREVIEW_OPEN}${JSON.stringify({ text })}${PREVIEW_CLOSE}`;
104
+ }
105
+ function encodeOutput(output) {
106
+ return `${OPEN}${JSON.stringify(output)}${CLOSE}`;
107
+ }
108
+ function isSegment(value) {
109
+ if (typeof value !== "object" || value === null) return false;
110
+ const segment = value;
111
+ if (segment["type"] === "narration") return typeof segment["text"] === "string";
112
+ return segment["type"] === "utterance" && typeof segment["character"] === "string" && typeof segment["name"] === "string" && typeof segment["text"] === "string";
113
+ }
114
+ /** The output encoded in a tool result's text, or nothing when the text carries none. */
115
+ function decodeOutput(text) {
116
+ const start = text.indexOf(OPEN);
117
+ const end = text.indexOf(CLOSE, start + 17);
118
+ if (start < 0 || end < 0) return void 0;
119
+ try {
120
+ const value = JSON.parse(text.slice(start + 17, end));
121
+ const segments = value["segments"];
122
+ if (!Array.isArray(segments) || !segments.every(isSegment) || typeof value["repaired"] !== "boolean") return void 0;
123
+ const ooc = value["ooc"];
124
+ const ruled = typeof ooc === "object" && ooc !== null && typeof ooc["legal"] === "boolean" && typeof ooc["note"] === "string" ? ooc : void 0;
125
+ const timelineNode = typeof value["timelineNode"] === "string" ? value["timelineNode"] : void 0;
126
+ const rolls = Array.isArray(value["rolls"]) ? value["rolls"].filter((entry) => typeof entry === "object" && entry !== null && typeof entry.id === "number" && typeof entry.text === "string" && (entry.by === "agent" || entry.by === "player")) : [];
127
+ return {
128
+ segments,
129
+ repaired: value["repaired"],
130
+ ...ruled === void 0 ? {} : { ooc: ruled },
131
+ ...rolls.length === 0 ? {} : { rolls },
132
+ ...timelineNode === void 0 ? {} : { timelineNode }
133
+ };
134
+ } catch {
135
+ return;
136
+ }
137
+ }
138
+ //#endregion
139
+ export { liveSettings as a, writeSettings as c, InvalidSettingsError as i, encodeOutput as n, readSettings as o, encodePreview as r, useChildReasoning as s, decodeOutput as t };
140
+
141
+ //# sourceMappingURL=narration-DK2kiIjE.js.map