@dsh-agentic-roleplay/core 0.8.1 → 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 (35) hide show
  1. package/lib/{access-DtOxFKBK.js → access-CfQWbNZP.js} +4 -7
  2. package/lib/{authoring-tools-DNnx0Nts.js → authoring-tools-BgmC2oqU.js} +223 -106
  3. package/lib/authoring.js +15 -22
  4. package/lib/{child-D6mpkkCE.js → child-lYYeOwl2.js} +28 -51
  5. package/lib/client.js +615 -778
  6. package/lib/client.js.map +1 -1
  7. package/lib/compaction.js +8 -13
  8. package/lib/{doctor-chat-ByDfCMeo.js → doctor-chat-hL1a61hp.js} +2 -3
  9. package/lib/doctor.js +6 -8
  10. package/lib/{gallery-D9nG3C78.js → gallery-D1V4h8vh.js} +3 -4
  11. package/lib/{generator-r1Czm7oi.js → generator-CLPosooC.js} +3 -4
  12. package/lib/helper.js +2 -3
  13. package/lib/{hooks-BQCzgggH.js → hooks-rQTTeOC4.js} +69 -97
  14. package/lib/images.js +4 -4
  15. package/lib/index.js +194 -264
  16. package/lib/{library-zI-2tG7h.js → library-Dfu0dgRj.js} +9 -15
  17. package/lib/{lorebook-author-tools-ywaYicwd.js → lorebook-author-tools-BAaAqZZm.js} +10 -13
  18. package/lib/lorebook-authoring.js +5 -6
  19. package/lib/{narration-CnBlVure.js → narration-CjB_KwwY.js} +9 -15
  20. package/lib/orchestration.js +53 -64
  21. package/lib/{pack-jail-5EJ-WYyW.js → pack-jail-CN-314ln.js} +76 -81
  22. package/lib/{packs-Cv8hanXJ.js → packs-DIj5BEfq.js} +6 -10
  23. package/lib/{presenter-agent-BSaMSd3G.js → presenter-agent-DR0KvLE0.js} +12 -18
  24. package/lib/{preset-CWI3-0r1.js → preset-CscHj1-Y.js} +5 -6
  25. package/lib/{preset-author-tools-BrJYa2cr.js → preset-author-tools-CxluF8qc.js} +12 -14
  26. package/lib/preset-authoring.js +6 -7
  27. package/lib/{presets-MwOSMMyu.js → presets-HB8kJTaE.js} +51 -56
  28. package/lib/{reflection-zSaMsjKj.js → reflection-Dg1xtOTW.js} +19 -30
  29. package/lib/{text-BzudpQLS.js → text-DyRN4nDP.js} +6 -8
  30. package/lib/{turn-guard-Cv8pfHEA.js → turn-guard-g_hDn0jR.js} +6 -11
  31. package/package.json +29 -29
  32. package/prompts/en/import/casting.md +1 -1
  33. package/prompts/en/import/character.md +1 -0
  34. package/prompts/zh/import/casting.md +1 -1
  35. package/prompts/zh/import/character.md +1 -0
package/lib/authoring.js CHANGED
@@ -1,17 +1,17 @@
1
- import "./access-DtOxFKBK.js";
2
- import { Bt as authoredWorldOf, L as parseWorldInfoEdit, Z as checkWorld, a as importHooks, l as isReviewing, m as editsSince, n as limitAuthoringAgents, o as importTools } from "./authoring-tools-DNnx0Nts.js";
3
- import { B as readWorldPrompt, K as IMPORT_FOLDER, L as PROSE_PROMPTS, R as deleteWorldPrompt, Tt as parseRelationEdit, V as writeWorldPrompt, bt as InvalidEditProblems, z as listWorldPrompts } from "./presets-MwOSMMyu.js";
4
- import { H as parsePlotsFile, J as formatIssue, at as shippedPromptsDirectory } from "./hooks-BQCzgggH.js";
5
- import { i as translator, r as languageOf } from "./text-BzudpQLS.js";
6
- import { $ as matchPieces, B as readWorldInfo, C as readWorldEditor, F as loadWorldParts, H as translateTools, K as finishUpdate, M as deleteTriggerDocument, N as readTriggerDocuments, P as writeTriggerDocument, Q as diffText, R as presetTools, Z as cardChanges, _ as editWorldPlots, a as deleteWorldPackFile, c as createAuthoredEntity, d as replaceAuthoredEntity, et as worldFieldChanges, i as checkWorldPack, l as deleteAuthoredEntity, nt as entitiesByPiece, o as listWorldPacks, q as pendingUpdate, r as registerPackJail, rt as readSource, s as AUTHORED_ENTITY_KINDS, tt as cardTools, u as readAuthoredEntityDocument, v as editWorldRelation, z as editWorldInfo } from "./pack-jail-5EJ-WYyW.js";
7
- import { g as SURFACE_NAMES, m as PACK_LIMITS, p as PACK_ACTIONS } from "./packs-Cv8hanXJ.js";
8
- import { t as guardAuthoringTurns } from "./turn-guard-Cv8pfHEA.js";
1
+ import { Q as checkWorld, R as parseWorldInfoEdit, Vt as authoredWorldOf, a as importHooks, h as editsSince, l as isReviewing, n as limitAuthoringAgents, o as importTools } from "./authoring-tools-BgmC2oqU.js";
2
+ import { t as guardAuthoringTurns } from "./turn-guard-g_hDn0jR.js";
3
+ import { $ as matchPieces, B as readWorldInfo, C as readWorldEditor, F as loadWorldParts, H as translateTools, K as finishUpdate, M as deleteTriggerDocument, N as readTriggerDocuments, P as writeTriggerDocument, Q as diffText, R as presetTools, Z as cardChanges, _ as editWorldPlots, a as deleteWorldPackFile, c as createAuthoredEntity, d as replaceAuthoredEntity, et as worldFieldChanges, i as checkWorldPack, l as deleteAuthoredEntity, nt as entitiesByPiece, o as listWorldPacks, q as pendingUpdate, r as registerPackJail, rt as readSource, s as AUTHORED_ENTITY_KINDS, tt as cardTools, u as readAuthoredEntityDocument, v as editWorldRelation, z as editWorldInfo } from "./pack-jail-CN-314ln.js";
4
+ import { Et as InvalidEditProblems, G as listWorldPrompts, K as readWorldPrompt, Q as IMPORT_FOLDER, U as PROSE_PROMPTS, W as deleteWorldPrompt, jt as parseRelationEdit, q as writeWorldPrompt } from "./presets-HB8kJTaE.js";
5
+ import { i as translator, r as languageOf } from "./text-DyRN4nDP.js";
6
+ import { J as formatIssue, V as parsePlotsFile, at as shippedPromptsDirectory } from "./hooks-rQTTeOC4.js";
7
+ import { g as SURFACE_NAMES, m as PACK_LIMITS, p as PACK_ACTIONS } from "./packs-DIj5BEfq.js";
8
+ import "./access-CfQWbNZP.js";
9
9
  import { join, relative } from "node:path";
10
10
  import { readFile, readdir } from "node:fs/promises";
11
11
  import { createUserMessage } from "@deepseek-ai/dsh-llm";
12
12
  import { defineTool } from "@deepseek-ai/dsh-tools";
13
13
  import { isDeepStrictEqual } from "node:util";
14
- //#region src/host/authoring/read-tools.ts
14
+ // src/host/authoring/read-tools.ts
15
15
  const output$4 = (value) => [{
16
16
  type: "text",
17
17
  text: JSON.stringify(value, null, 2)
@@ -261,8 +261,7 @@ function authoredWorldReadTools(reader) {
261
261
  })
262
262
  ];
263
263
  }
264
- //#endregion
265
- //#region src/host/authoring/pack-tools.ts
264
+ // src/host/authoring/pack-tools.ts
266
265
  const PACK_MUTATION_TOOLS = ["delete_world_pack_file"];
267
266
  const output$3 = (value) => [{
268
267
  type: "text",
@@ -370,8 +369,7 @@ function worldPackTools(reader) {
370
369
  })
371
370
  ];
372
371
  }
373
- //#endregion
374
- //#region src/host/authoring/prompt-tools.ts
372
+ // src/host/authoring/prompt-tools.ts
375
373
  const PROMPT_MUTATION_TOOLS = ["write_world_prompt", "delete_world_prompt"];
376
374
  const output$2 = (value) => [{
377
375
  type: "text",
@@ -466,8 +464,7 @@ function worldPromptTools(reader, shipped = shippedPromptsDirectory()) {
466
464
  })
467
465
  ];
468
466
  }
469
- //#endregion
470
- //#region src/host/authoring/approval-summary.ts
467
+ // src/host/authoring/approval-summary.ts
471
468
  const MAX_FIELDS = 5;
472
469
  function clip(value, max) {
473
470
  const flat = (typeof value === "string" ? `“${value}”` : JSON.stringify(value) ?? "").replace(/\s+/g, " ");
@@ -534,8 +531,7 @@ function plain(value, max = 220) {
534
531
  zh: text
535
532
  };
536
533
  }
537
- //#endregion
538
- //#region src/host/authoring/write-tools.ts
534
+ // src/host/authoring/write-tools.ts
539
535
  const output$1 = (value) => [{
540
536
  type: "text",
541
537
  text: JSON.stringify(value, null, 2)
@@ -920,8 +916,7 @@ async function detailOf(exec, kind, key, current) {
920
916
  default: return;
921
917
  }
922
918
  }
923
- //#endregion
924
- //#region src/host/import/update-tools.ts
919
+ // src/host/import/update-tools.ts
925
920
  const MAX_HUNKS = 10;
926
921
  const MAX_PIECES = 60;
927
922
  const PREVIEW_CHARS = 300;
@@ -1104,8 +1099,7 @@ function updateHooks(ctx, worlds, languageOf$1) {
1104
1099
  }));
1105
1100
  });
1106
1101
  }
1107
- //#endregion
1108
- //#region src/authoring.ts
1102
+ // src/authoring.ts
1109
1103
  const name = "@dsh-agentic-roleplay/core/authoring";
1110
1104
  const inject = [
1111
1105
  "roleplayWorlds",
@@ -1188,7 +1182,6 @@ function apply(ctx) {
1188
1182
  text: (context) => ctx.roleplayPrompts.text(id, languageOf(context))
1189
1183
  });
1190
1184
  }
1191
- //#endregion
1192
1185
  export { apply, inject, name };
1193
1186
 
1194
1187
  //# sourceMappingURL=authoring.js.map
@@ -2,7 +2,7 @@ import { randomUUID } from "node:crypto";
2
2
  import { SessionId } from "@deepseek-ai/dsh-session";
3
3
  import { ReasoningEffortId, createUserMessage, lastAssistantStreamChunk } from "@deepseek-ai/dsh-llm";
4
4
  import { ToolArgsError, validateJsonSchemaValue } from "@deepseek-ai/dsh-tools";
5
- //#region src/host/agents/budget.ts
5
+ // src/host/agents/budget.ts
6
6
  /** Tokens a text of a number of characters comes to: a character of a script written without spaces takes about one and a half, of the others about a third. */
7
7
  function tokensOf$1(characters, language) {
8
8
  return Math.ceil(language.toLowerCase().startsWith("zh") ? characters * 1.5 : characters / 3);
@@ -18,8 +18,7 @@ function outputBudget(estimate, headroom) {
18
18
  function estimateAnswer(limits, language) {
19
19
  return limits.reduce((sum, limit) => sum + tokensOf$1(limit, language) + 8, 0);
20
20
  }
21
- //#endregion
22
- //#region src/host/agents/cut-off.ts
21
+ // src/host/agents/cut-off.ts
23
22
  /** How the model's last reply ended (`stop`, `tool-calls`, `max-tokens`), or nothing when it has made none. */
24
23
  function finishOf(agent) {
25
24
  const last = agent.session.snapshotEvents().findLast((event) => event.type === "assistant/message");
@@ -28,8 +27,7 @@ function finishOf(agent) {
28
27
  }
29
28
  /** Whether the model's last reply ended because it reached the most it may write in one request. */
30
29
  const cutOff = (agent) => finishOf(agent) === "max-tokens";
31
- //#endregion
32
- //#region src/host/orchestration/current-turn.ts
30
+ // src/host/orchestration/current-turn.ts
33
31
  /** The last turn the session started. */
34
32
  function currentTurn(agent) {
35
33
  const events = agent.session.snapshotEvents();
@@ -39,8 +37,7 @@ function currentTurn(agent) {
39
37
  }
40
38
  return 0;
41
39
  }
42
- //#endregion
43
- //#region src/shared/usage.ts
40
+ // src/shared/usage.ts
44
41
  const NO_TOKENS = {
45
42
  input: 0,
46
43
  cached: 0,
@@ -51,8 +48,7 @@ const addTokens = (a, b) => ({
51
48
  cached: a.cached + b.cached,
52
49
  output: a.output + b.output
53
50
  });
54
- //#endregion
55
- //#region src/host/agents/usage.ts
51
+ // src/host/agents/usage.ts
56
52
  /** The tokens the model service reported for the requests it answered in a session's events; a request it reported nothing for adds nothing. */
57
53
  function tokensOf(events) {
58
54
  let total = NO_TOKENS;
@@ -68,13 +64,17 @@ function tokensOf(events) {
68
64
  }
69
65
  return total;
70
66
  }
71
- //#endregion
72
- //#region src/shared/activity.ts
67
+ // src/shared/activity.ts
73
68
  /** Longest text kept for one call's arguments or outcome, and for the persona, the prompt and the result. */
74
69
  const MAX_RUN_TEXT = 4e3;
75
70
  const MAX_RUN_PROMPT = 24e3;
76
- //#endregion
77
- //#region src/shared/sampling.ts
71
+ // src/host/agents/limits.ts
72
+ /**
73
+ * How long a child that was stopped has to wind down. Found live twice: a stopped child's activation never settled and the caller waited for ever (an import stood still for
74
+ * minutes, a game master's turn never ended), so what the stop decided is not waited on past this.
75
+ */
76
+ const STOP_GRACE_MS = 15e3;
77
+ // src/shared/sampling.ts
78
78
  /** The controls beyond temperature, which apply to the game master and the characters (the helpers that must be exact are never asked more freely). */
79
79
  const SHARED_SAMPLING = [
80
80
  "topP",
@@ -149,33 +149,12 @@ function parseSampling(value, where, problems) {
149
149
  function sharedSamplingOf(settings) {
150
150
  return Object.fromEntries(SHARED_SAMPLING.flatMap((key) => settings[key] === SAMPLING_RANGES[key].neutral ? [] : [[key, settings[key]]]));
151
151
  }
152
- //#endregion
153
- //#region src/host/agents/child.ts
152
+ // src/host/agents/child.ts
154
153
  /** The tool every child ends with: its arguments are the child's result. */
155
154
  const SUBMIT_TOOL = "submit_result";
156
155
  /** Said to every child once, after its persona, so it is the same for all of them and the start of every request stays the same. */
157
156
  const SUBMIT_RULE = `When you are done, call ${SUBMIT_TOOL} with your result. It is the only way to finish: text you write instead is lost.`;
158
157
  const STRUCTURED_RULE = "Your whole reply is the result, as one JSON object of the shape you were given: write nothing before or after it.";
159
- /** Calls a child may make before it is given up; a child that keeps going is not converging. */
160
- const DEFAULT_MAX_CALLS = 40;
161
- /** A child that stops without a result is told to submit this many times before it is given up. */
162
- const MAX_REMINDERS = 2;
163
- /** A child that goes on calling tools this many times past its budget, in spite of being told to submit, is stopped: it would otherwise loop for as long as the model keeps at it. */
164
- const OVERRUN = 5;
165
- /** Steps a child gets to submit once its other tools are withdrawn: a model that keeps calling tools that are gone is stopped. */
166
- const STEPS_AFTER_RETIRE = 3;
167
- /** The most model steps a child may take past its call budget in all (calls the harness refuses before they run, such as a result that does not fit its schema, are not counted as calls). */
168
- const STEPS_PAST_BUDGET = 20;
169
- /** The same call, with the same arguments, that failed before is refused after this many repeats, and the child is stopped at twice that: the model is not reading the reasons it was given. */
170
- const REPEATS_TOLD = 1;
171
- const REPEATS_STOPPED = 4;
172
- /** A result that is refused this many times is not going to be fixed by asking again: the child is stopped and the caller decides what to do without it. */
173
- const MAX_REFUSALS = 5;
174
- /**
175
- * How long a child that was stopped has to wind down. Found live twice: a stopped child's activation never settled and the caller waited for ever (an import stood still for
176
- * minutes, a game master's turn never ended), so what the stop decided is not waited on past this.
177
- */
178
- const STOP_GRACE_MS = 15e3;
179
158
  /** A child ran out of the time it was given; asking again would only spend it again. */
180
159
  var ChildTimeoutError = class extends Error {
181
160
  constructor(minutes) {
@@ -329,7 +308,7 @@ async function supportedEffort(task) {
329
308
  /**
330
309
  * A JSON text that was cut off, closed after the last whole entry it holds: what a reply that ran into its limit in the middle of a long list still says. Nothing when no such cut makes it JSON.
331
310
  */
332
- function repairedJson(text) {
311
+ function repairedJson(text, accepts = () => true) {
333
312
  const cuts = [];
334
313
  for (const marks of ["}]", ","]) for (let at = text.length - 1, found = 0; at >= 0 && found < 300; at -= 1) if (marks.includes(text[at])) {
335
314
  cuts.push(at);
@@ -354,8 +333,7 @@ function repairedJson(text) {
354
333
  if (inString) continue;
355
334
  const closed = head + stack.reverse().join("");
356
335
  try {
357
- JSON.parse(closed);
358
- return closed;
336
+ if (accepts(JSON.parse(closed))) return closed;
359
337
  } catch {
360
338
  continue;
361
339
  }
@@ -370,7 +348,7 @@ function structuredReply(child, schema, cut = false) {
370
348
  try {
371
349
  value = JSON.parse(text);
372
350
  } catch (error) {
373
- const mended = cut ? repairedJson(text) : void 0;
351
+ const mended = cut ? repairedJson(text, (candidate) => validateJsonSchemaValue(schema, candidate).length === 0) : void 0;
374
352
  if (mended === void 0) return { problem: `the reply is not valid JSON (${error instanceof Error ? error.message : String(error)})` };
375
353
  value = JSON.parse(mended);
376
354
  }
@@ -398,7 +376,7 @@ async function execute(task, stats) {
398
376
  let refusals = 0;
399
377
  let lastRefusal = "";
400
378
  let previous;
401
- const budget = task.maxCalls ?? DEFAULT_MAX_CALLS;
379
+ const budget = task.maxCalls ?? 40;
402
380
  const counted = (tool) => ({
403
381
  ...tool,
404
382
  execute: async (args, exec) => {
@@ -411,7 +389,7 @@ async function execute(task, stats) {
411
389
  failed
412
390
  });
413
391
  };
414
- if (stats.calls > budget + OVERRUN) {
392
+ if (stats.calls > budget + 5) {
415
393
  overran = true;
416
394
  stopped?.();
417
395
  }
@@ -422,11 +400,11 @@ async function execute(task, stats) {
422
400
  }
423
401
  const signature = JSON.stringify(args);
424
402
  const repeats = previous?.name === tool.name && previous.args === signature && previous.failed ? previous.repeats + 1 : 0;
425
- if (repeats >= REPEATS_STOPPED) {
403
+ if (repeats >= 4) {
426
404
  overran = true;
427
405
  stopped?.();
428
406
  }
429
- if (repeats >= REPEATS_TOLD) {
407
+ if (repeats >= 1) {
430
408
  previous = {
431
409
  name: tool.name,
432
410
  args: signature,
@@ -488,7 +466,7 @@ async function execute(task, stats) {
488
466
  });
489
467
  refusals += 1;
490
468
  lastRefusal = refusal;
491
- if (refusals >= MAX_REFUSALS) {
469
+ if (refusals >= 5) {
492
470
  overran = true;
493
471
  stopped?.();
494
472
  }
@@ -528,13 +506,13 @@ async function execute(task, stats) {
528
506
  childCtx.on("agent/pre-step", async (_payload, next) => {
529
507
  const decision = await next();
530
508
  steps += 1;
531
- if (steps > budget + STEPS_PAST_BUDGET && submitted === void 0) {
509
+ if (steps > budget + 20 && submitted === void 0) {
532
510
  overran = true;
533
511
  stopped?.();
534
512
  }
535
513
  if (stepsAfterRetire < 0) return decision;
536
514
  stepsAfterRetire += 1;
537
- if (stepsAfterRetire > STEPS_AFTER_RETIRE && submitted === void 0) {
515
+ if (stepsAfterRetire > 3 && submitted === void 0) {
538
516
  overran = true;
539
517
  stopped?.();
540
518
  }
@@ -571,7 +549,7 @@ async function execute(task, stats) {
571
549
  outcome: cut(lastRefusal),
572
550
  failed: true
573
551
  });
574
- if (refusals >= MAX_REFUSALS) {
552
+ if (refusals >= 5) {
575
553
  overran = true;
576
554
  stopped?.();
577
555
  return;
@@ -591,7 +569,7 @@ async function execute(task, stats) {
591
569
  stopped?.();
592
570
  return;
593
571
  }
594
- if (reminders >= MAX_REMINDERS) return;
572
+ if (reminders >= 2) return;
595
573
  reminders += 1;
596
574
  child.steer(createUserMessage({
597
575
  content: [{
@@ -664,7 +642,7 @@ async function execute(task, stats) {
664
642
  }
665
643
  if (signal.aborted) throw new Error("cancelled");
666
644
  if (rambled && submitted === void 0) throw new Error(`the agent wrote text up to the limit of ${task.maxTokens ?? task.sampling?.maxTokens ?? "its model"} tokens and never called ${SUBMIT_TOOL}, and was stopped`);
667
- if (overran && submitted === void 0) throw new Error(refusals >= MAX_REFUSALS ? `the result was refused ${refusals} times and the agent was stopped; the last reason: ${lastRefusal}` : `the agent went on past its ${budget} calls, or kept repeating a call that failed, without submitting a result and was stopped`);
645
+ if (overran && submitted === void 0) throw new Error(refusals >= 5 ? `the result was refused ${refusals} times and the agent was stopped; the last reason: ${lastRefusal}` : `the agent went on past its ${budget} calls, or kept repeating a call that failed, without submitting a result and was stopped`);
668
646
  if (submitted === void 0) {
669
647
  const ended = entry.agent?.session.snapshotEvents().findLast((event) => event.type === "turn/end");
670
648
  const reason = ended?.type === "turn/end" ? ended.data.reason : void 0;
@@ -673,7 +651,6 @@ async function execute(task, stats) {
673
651
  }
674
652
  return submitted.value;
675
653
  }
676
- //#endregion
677
654
  export { finishOf as _, childTrace as a, runChild as c, sharedSamplingOf as d, tokensOf as f, cutOff as g, currentTurn as h, childSamplingOf as i, SAMPLING_RANGES as l, addTokens as m, childModel as n, helpers as o, NO_TOKENS as p, childSampling as r, namedModel as s, ChildTimeoutError as t, parseSampling as u, estimateAnswer as v, tokensOf$1 as y };
678
655
 
679
- //# sourceMappingURL=child-D6mpkkCE.js.map
656
+ //# sourceMappingURL=child-lYYeOwl2.js.map