dsh-multigameassistant 2.0.5 → 2.1.0

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.
@@ -4,7 +4,7 @@ import {
4
4
  LOW_CONTEXT_WINDOW_MS,
5
5
  MAX_ARCHIVE_DAYS,
6
6
  MIN_ARCHIVE_DAYS
7
- } from "./chunk-BTT2GBTQ.js";
7
+ } from "./chunk-V2X4PLF5.js";
8
8
 
9
9
  // packages/gateway/lib/low-context.js
10
10
  import { appendFile, mkdir, readFile, stat } from "node:fs/promises";
@@ -64,6 +64,44 @@ function lowContextRange(session, options) {
64
64
  return null;
65
65
  return { start: nodes[first], end: nodes[keepFrom - 1] };
66
66
  }
67
+ var IDLE_COMPACT_MS = 4 * 60 * 6e4;
68
+ var MIN_PRUNE_CHARS = 24e3;
69
+ function idleRange(session, options) {
70
+ const nodes = session.surface.nodes;
71
+ const lastSeq = nodes[nodes.length - 1];
72
+ const last = lastSeq === void 0 ? void 0 : session.eventAt(lastSeq);
73
+ if (last === void 0 || last.type !== "assistant/message")
74
+ return null;
75
+ if (options.now - last.time < (options.idleMs ?? IDLE_COMPACT_MS))
76
+ return null;
77
+ const events = nodes.map((seq) => session.eventAt(seq));
78
+ let first = events[0]?.type === "system/message" ? 1 : 0;
79
+ if (options.separate === true)
80
+ while (first < events.length && events[first] !== void 0 && isSummaryNode(events[first]))
81
+ first++;
82
+ if (!events.slice(first).some((event) => event !== void 0 && isMessage(event)))
83
+ return null;
84
+ return { start: nodes[first], end: lastSeq };
85
+ }
86
+ function prunableChars(pruner, session) {
87
+ let chars = 0;
88
+ for (const seq of session.surface.nodes) {
89
+ const event = session.eventAt(seq);
90
+ if (event?.type !== "tool/result")
91
+ continue;
92
+ const size = pruner.measureContent(session.deriveEventMessage(event).content);
93
+ if (size > pruner.config.thresholdChars)
94
+ chars += size - pruner.config.thresholdChars;
95
+ }
96
+ return chars;
97
+ }
98
+ function pruneStale(pruner, session, minChars = MIN_PRUNE_CHARS) {
99
+ if (typeof pruner?.pruneSession !== "function" || typeof pruner.measureContent !== "function")
100
+ return 0;
101
+ if (prunableChars(pruner, session) < minChars)
102
+ return 0;
103
+ return pruner.pruneSession(session).charsRemoved;
104
+ }
67
105
  var SPOKEN = /* @__PURE__ */ new Set(["user/message", "assistant/message", "tool/call", "tool/result"]);
68
106
  function spanTokens(meter, session, range) {
69
107
  if (typeof meter?.measure !== "function")
@@ -450,6 +488,31 @@ function apply(ctx, config = {}) {
450
488
  return engine.compactRegion(range.start, range.end, agent, signal);
451
489
  return own.run(range, agent, (input, owner, abort) => summarizeSpan(llm, engine.config, input, owner, abort), signal);
452
490
  };
491
+ const work = () => service("adgGateway");
492
+ ctx.on("agent/pre-step", (async (payload, next) => {
493
+ try {
494
+ const { lowContext, archiveDays } = settings();
495
+ if (lowContext && !payload.signal.aborted) {
496
+ const archiving = archiveDays !== null && own !== void 0 && summariesFile !== void 0;
497
+ const sessionId = payload.agent.session.id;
498
+ const range = idleRange(payload.agent.session, { now: Date.now(), ...archiving ? { separate: true } : {} });
499
+ if (range !== null && work()?.workPending?.(sessionId) !== true && !refused.has(sessionId, "idle", range)) {
500
+ const tokens = spanTokens(meter(), payload.agent.session, range);
501
+ if (tokens === void 0 || tokens >= MIN_SPAN_TOKENS) {
502
+ const result = await compact(range, payload.agent, payload.signal).catch((error) => {
503
+ if (refusedForSize(error))
504
+ refused.add(sessionId, "idle", range);
505
+ throw error;
506
+ });
507
+ log.info(`low-context: compacted ${result.shadowedSeqs.length} records (seqs ${range.start}-${range.end}, ${tokens ?? "?"} tokens) of ${sessionId} before its turn: quiet for over ${IDLE_COMPACT_MS / 36e5} hours`);
508
+ }
509
+ }
510
+ }
511
+ } catch (error) {
512
+ log.warn(`low-context compaction before the turn of ${payload.agent.session.id} failed: ${error instanceof Error ? error.message : String(error)}`);
513
+ }
514
+ return next();
515
+ }));
453
516
  ctx.on("agent/turn-stopping", (async ({ agent, signal }) => {
454
517
  const { lowContext, archiveDays } = settings();
455
518
  if (!lowContext || signal.aborted)
@@ -478,6 +541,13 @@ function apply(ctx, config = {}) {
478
541
  } catch (error) {
479
542
  log.warn(`low-context compaction of ${sessionId} failed: ${error instanceof Error ? error.message : String(error)}`);
480
543
  }
544
+ try {
545
+ const cut = pruneStale(service("toolResultPruner"), agent.session);
546
+ if (cut > 0)
547
+ log.info(`low-context: trimmed long tool results of ${sessionId} (${cut} characters)`);
548
+ } catch (error) {
549
+ log.warn(`low-context trimming of tool results of ${sessionId} failed: ${error instanceof Error ? error.message : String(error)}`);
550
+ }
481
551
  if (!archiving || signal.aborted)
482
552
  return;
483
553
  try {
@@ -505,6 +575,11 @@ export {
505
575
  summaryText,
506
576
  isMessage,
507
577
  lowContextRange,
578
+ IDLE_COMPACT_MS,
579
+ MIN_PRUNE_CHARS,
580
+ idleRange,
581
+ prunableChars,
582
+ pruneStale,
508
583
  spanTokens,
509
584
  refusedForSize,
510
585
  Refusals,