@dsh-agentic-roleplay/core 0.7.0 → 0.7.1

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 (40) hide show
  1. package/lib/access-DtOxFKBK.js +150 -0
  2. package/lib/{authoring-tools-BWjEr_lF.js → authoring-tools-BKltdiea.js} +160 -74
  3. package/lib/authoring.js +10 -5
  4. package/lib/{child-D5SVNk4O.js → child-CyCViUKa.js} +48 -7
  5. package/lib/client.js +1046 -606
  6. package/lib/client.js.map +1 -1
  7. package/lib/compaction.js +3 -3
  8. package/lib/doctor.js +3 -3
  9. package/lib/{gallery-C7VOJ3xH.js → gallery-BxZGOgMb.js} +2 -2
  10. package/lib/{generator-CO8usAjE.js → generator-BFmSYvR8.js} +2 -2
  11. package/lib/helper.js +1 -1
  12. package/lib/{hooks-DufjUf1p.js → hooks-HnqXO8mM.js} +6 -245
  13. package/lib/images.js +4 -4
  14. package/lib/index.js +129 -103
  15. package/lib/{library-DsDc6fQa.js → library-BGxPKs9a.js} +6 -3
  16. package/lib/{lorebook-author-tools-v-PM8Kbs.js → lorebook-author-tools-BAnUyRtS.js} +31 -28
  17. package/lib/lorebook-authoring.js +8 -3
  18. package/lib/{narration-_dA9oIt2.js → narration-DvFBqmqe.js} +9 -132
  19. package/lib/orchestration.js +11 -10
  20. package/lib/{pack-jail--Ij90LeY.js → pack-jail-DLGguGS3.js} +14 -12
  21. package/lib/{packs-DWKnL4YX.js → packs-jowC-MN_.js} +2 -2
  22. package/lib/{presenter-agent-BHSbyWwZ.js → presenter-agent-DvZtnf0Q.js} +6 -6
  23. package/lib/{preset-CDNyJhfU.js → preset-CXbC0NeU.js} +3 -3
  24. package/lib/{preset-author-tools-D7Azdlaq.js → preset-author-tools-C26kj6t5.js} +11 -10
  25. package/lib/preset-authoring.js +9 -4
  26. package/lib/{reflection-CRCWK2E1.js → reflection-CvOZGjmn.js} +9 -170
  27. package/lib/{presets-D11bxWX0.js → route-DRTJ_vcA.js} +165 -4
  28. package/lib/text-JbWfTHbr.js +295 -0
  29. package/lib/turn-guard-BvCs1VxB.js +53 -0
  30. package/package.json +2 -2
  31. package/prompts/en/import/cut-off.md +1 -0
  32. package/prompts/en/import/finish-failed.md +1 -1
  33. package/prompts/en/roleplay/decide-instruction.md +1 -0
  34. package/prompts/en/roleplay/decide-irrelevant.md +1 -0
  35. package/prompts/zh/import/cut-off.md +1 -0
  36. package/prompts/zh/import/finish-failed.md +1 -1
  37. package/prompts/zh/roleplay/decide-instruction.md +1 -0
  38. package/prompts/zh/roleplay/decide-irrelevant.md +1 -0
  39. package/lib/access-EbNdZFbk.js +0 -25
  40. package/lib/text-CT1re6XT.js +0 -18
@@ -19,6 +19,16 @@ function estimateAnswer(limits, language) {
19
19
  return limits.reduce((sum, limit) => sum + tokensOf$1(limit, language) + 8, 0);
20
20
  }
21
21
  //#endregion
22
+ //#region src/host/agents/cut-off.ts
23
+ /** How the model's last reply ended (`stop`, `tool-calls`, `max-tokens`), or nothing when it has made none. */
24
+ function finishOf(agent) {
25
+ const last = agent.session.snapshotEvents().findLast((event) => event.type === "assistant/message");
26
+ if (last === void 0 || last.type !== "assistant/message") return void 0;
27
+ return (last.data.stream ?? []).find((entry) => entry.chunk?.type === "finish")?.chunk?.reason?.kind;
28
+ }
29
+ /** Whether the model's last reply ended because it reached the most it may write in one request. */
30
+ const cutOff = (agent) => finishOf(agent) === "max-tokens";
31
+ //#endregion
22
32
  //#region src/host/orchestration/current-turn.ts
23
33
  /** The last turn the session started. */
24
34
  function currentTurn(agent) {
@@ -160,6 +170,12 @@ const REPEATS_TOLD = 1;
160
170
  const REPEATS_STOPPED = 4;
161
171
  /** 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. */
162
172
  const MAX_REFUSALS = 5;
173
+ /** A child ran out of the time it was given; asking again would only spend it again. */
174
+ var ChildTimeoutError = class extends Error {
175
+ constructor(minutes) {
176
+ super(`the agent was stopped after ${minutes} minute${minutes === 1 ? "" : "s"} without a result (a model that keeps thinking never answers: lower its thinking level, or raise the limit in the settings)`);
177
+ }
178
+ };
163
179
  /**
164
180
  * What the settings of a chat say about how a kind of helper is asked: prose is free, the keepers of records exact. A helper whose answer has a declared size (its `estimate`, in tokens)
165
181
  * is let produce that with room to spare, no more than the chat's ceiling for its kind.
@@ -198,7 +214,11 @@ function childTrace(store, kind, label, extra = {}, named) {
198
214
  }
199
215
  /** The `model` the settings give a kind of helper, to spread into its task; nothing when it runs on the chat's own model. */
200
216
  function childModel(settings, kind, override) {
201
- const named = (override ?? settings.models[kind]).trim();
217
+ return namedModel(override ?? settings.models[kind]);
218
+ }
219
+ /** The `model` for a name written `provider/model`; nothing for an empty name. */
220
+ function namedModel(name) {
221
+ const named = name.trim();
202
222
  if (named === "") return {};
203
223
  const slash = named.indexOf("/");
204
224
  return { model: {
@@ -252,8 +272,17 @@ async function runChild(task) {
252
272
  calls: stats.record,
253
273
  ...stats.result === void 0 ? {} : { result: stats.result }
254
274
  });
275
+ const minutes = task.timeoutMinutes ?? 0;
276
+ const clock = new AbortController();
277
+ const timer = minutes > 0 ? setTimeout(() => {
278
+ clock.abort();
279
+ }, minutes * 6e4) : void 0;
280
+ const limited = minutes > 0 ? {
281
+ ...task,
282
+ signal: AbortSignal.any([task.signal, clock.signal])
283
+ } : task;
255
284
  try {
256
- const result = await execute(task, stats);
285
+ const result = await execute(limited, stats);
257
286
  task.trace?.({
258
287
  ms: Date.now() - began,
259
288
  calls: stats.calls,
@@ -262,7 +291,8 @@ async function runChild(task) {
262
291
  tokens: stats.tokens ?? NO_TOKENS
263
292
  });
264
293
  return result;
265
- } catch (error) {
294
+ } catch (cause) {
295
+ const error = clock.signal.aborted && !task.signal.aborted ? new ChildTimeoutError(minutes) : cause;
266
296
  task.trace?.({
267
297
  ms: Date.now() - began,
268
298
  calls: stats.calls,
@@ -271,7 +301,9 @@ async function runChild(task) {
271
301
  turn: currentTurn(task.parent),
272
302
  tokens: stats.tokens ?? NO_TOKENS
273
303
  });
274
- if (attempt >= 2 || task.signal.aborted || !isUnfinishedCall(error)) throw error;
304
+ if (attempt >= 2 || task.signal.aborted || error instanceof ChildTimeoutError || !isUnfinishedCall(error)) throw error;
305
+ } finally {
306
+ if (timer !== void 0) clearTimeout(timer);
275
307
  }
276
308
  }
277
309
  }
@@ -301,6 +333,7 @@ async function execute(task, stats) {
301
333
  let retire;
302
334
  let stepsAfterRetire = -1;
303
335
  let overran = false;
336
+ let rambled = false;
304
337
  let refusals = 0;
305
338
  let lastRefusal = "";
306
339
  let previous;
@@ -445,7 +478,14 @@ async function execute(task, stats) {
445
478
  });
446
479
  childCtx.tools.register(submit);
447
480
  childCtx.on("agent/turn-stopping", () => {
448
- if (submitted !== void 0 || reminders >= MAX_REMINDERS) return;
481
+ if (submitted !== void 0) return;
482
+ if (cutOff(child)) {
483
+ rambled = true;
484
+ overran = true;
485
+ stopped?.();
486
+ return;
487
+ }
488
+ if (reminders >= MAX_REMINDERS) return;
449
489
  reminders += 1;
450
490
  child.steer(createUserMessage({
451
491
  content: [{
@@ -499,6 +539,7 @@ async function execute(task, stats) {
499
539
  samplings.delete(childId);
500
540
  }
501
541
  if (signal.aborted) throw new Error("cancelled");
542
+ 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`);
502
543
  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`);
503
544
  if (submitted === void 0) {
504
545
  const ended = entry.agent?.session.snapshotEvents().findLast((event) => event.type === "turn/end");
@@ -509,6 +550,6 @@ async function execute(task, stats) {
509
550
  return submitted.value;
510
551
  }
511
552
  //#endregion
512
- export { helpers as a, parseSampling as c, NO_TOKENS as d, addTokens as f, tokensOf$1 as h, childTrace as i, sharedSamplingOf as l, estimateAnswer as m, childSampling as n, runChild as o, currentTurn as p, childSamplingOf as r, SAMPLING_RANGES as s, childModel as t, tokensOf as u };
553
+ 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 };
513
554
 
514
- //# sourceMappingURL=child-D5SVNk4O.js.map
555
+ //# sourceMappingURL=child-CyCViUKa.js.map