stratagate-dsh 0.3.12 → 0.3.13

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.
package/dist/plugin.js CHANGED
@@ -2304,7 +2304,6 @@ function updateDreaming(state, events, command, now) {
2304
2304
  let run = state.dreaming;
2305
2305
  if (command.kind === "start") {
2306
2306
  if (run && ["running", "pausing", "paused"].includes(run.status)) throw new Error("A Dreaming run already exists; resume or terminate it");
2307
- if (command.tokenLimit !== void 0 && (!Number.isSafeInteger(command.tokenLimit) || command.tokenLimit <= 0)) throw new Error("Invalid token boundary");
2308
2307
  const topics = state.topics.filter((t) => !t.invalidated && t.sourceEventIds.length && t.sourceEventIds.every((id) => sources2.has(id)));
2309
2308
  const units = topics.flatMap((topic2) => {
2310
2309
  const ids2 = topic2.sourceEventIds.filter((id) => sources2.has(id));
@@ -2343,7 +2342,7 @@ function updateDreaming(state, events, command, now) {
2343
2342
  changedBatches: 0,
2344
2343
  readOffsets: {},
2345
2344
  idleCalls: 0,
2346
- tokenLimit: command.tokenLimit ?? null,
2345
+ tokenLimit: null,
2347
2346
  activity: "\u6B63\u5728\u8BFB\u53D6\u76EE\u5F55",
2348
2347
  reason: null,
2349
2348
  createdAt: now,
@@ -2373,11 +2372,8 @@ function updateDreaming(state, events, command, now) {
2373
2372
  if (command.kind === "resume") {
2374
2373
  const expired = !!run.leaseUntil && Date.parse(run.leaseUntil) <= Date.parse(now);
2375
2374
  if (run.status !== "paused" && !(expired && ["running", "pausing"].includes(run.status))) throw new Error("Dreaming is not paused");
2376
- if (command.tokenLimit !== void 0) {
2377
- if (!Number.isSafeInteger(command.tokenLimit) || command.tokenLimit <= run.estimatedTokens) throw new Error("Token boundary must exceed consumed estimate");
2378
- run.tokenLimit = command.tokenLimit;
2379
- }
2380
2375
  if (run.policyVersion !== DREAMING_POLICY_VERSION) throw new Error("Dreaming policy changed; terminate this run and start again");
2376
+ run.tokenLimit = null;
2381
2377
  run.status = "running";
2382
2378
  run.owner = command.owner;
2383
2379
  run.epoch++;
@@ -10438,9 +10434,7 @@ function dreamingPayload(memory, run) {
10438
10434
  lastWriteFailure: lastWriteFailure ?? null,
10439
10435
  retrievedEvidenceIds: Object.keys(run.evidenceVersions).slice(-100),
10440
10436
  totalRetrievedEvidence: Object.keys(run.evidenceVersions).length,
10441
- recentTools,
10442
- estimatedTokensConsumed: run.estimatedTokens,
10443
- tokenBoundary: run.tokenLimit
10437
+ recentTools
10444
10438
  };
10445
10439
  }
10446
10440
  function dreamingEstimatedTokens(payload, action) {
@@ -10449,19 +10443,19 @@ function dreamingEstimatedTokens(payload, action) {
10449
10443
  function dreamingStatus(run) {
10450
10444
  if (!run) return null;
10451
10445
  const interrupted = ["running", "pausing"].includes(run.status) && !!run.leaseUntil && Date.parse(run.leaseUntil) <= Date.now();
10446
+ const legacyBoundary = run.status === "paused" && run.tokenLimit != null && /消耗边界/.test(run.reason ?? "");
10452
10447
  return {
10453
10448
  id: run.id,
10454
10449
  sessionId: run.sessionId ?? null,
10455
10450
  status: interrupted ? "paused" : run.status,
10456
10451
  scope: run.scope,
10457
10452
  activity: interrupted ? "\u5DF2\u6682\u505C" : run.activity,
10458
- reason: interrupted ? "\u4E0A\u6B21\u8FD0\u884C\u5DF2\u4E2D\u65AD\uFF0C\u53EF\u4EE5\u7EE7\u7EED" : run.reason,
10453
+ reason: interrupted ? "\u4E0A\u6B21\u8FD0\u884C\u5DF2\u4E2D\u65AD\uFF0C\u53EF\u4EE5\u7EE7\u7EED" : legacyBoundary ? "\u4E0A\u6B21\u6574\u7406\u5DF2\u6682\u505C\uFF0C\u53EF\u4EE5\u7EE7\u7EED\uFF1B\u5DF2\u5B8C\u6210\u7684\u4FEE\u6539\u5DF2\u4FDD\u5B58\u3002" : run.reason,
10459
10454
  completed: run.cursor,
10460
10455
  total: run.units.length,
10461
10456
  changedBatches: run.changedBatches,
10462
10457
  calls: run.calls,
10463
10458
  estimatedTokens: run.estimatedTokens,
10464
- tokenLimit: run.tokenLimit,
10465
10459
  updatedAt: run.updatedAt
10466
10460
  };
10467
10461
  }
@@ -11883,14 +11877,14 @@ var StrataGateRuntime = class {
11883
11877
  const run = await this.dreamingRun(namespace, id);
11884
11878
  return { sessionId: await this.dreamingHost.open(namespace, run) };
11885
11879
  }
11886
- async adminDreamingControl(namespace, action, id, tokenLimit) {
11880
+ async adminDreamingControl(namespace, action, id) {
11887
11881
  if (this.closed) throw new Error("Runtime is closed");
11888
11882
  if ((action === "start" || action === "resume") && [...this.dreamingRuns.keys()].some((key) => key !== namespace)) {
11889
11883
  throw new Error("\u53E6\u4E00\u4E2A\u8BB0\u5FC6\u7A7A\u95F4\u6B63\u5728 Dreaming\uFF0C\u8BF7\u7B49\u5F85\u5B8C\u6210\u6216\u5148\u6682\u505C\u5B83");
11890
11884
  }
11891
11885
  let command;
11892
- if (action === "start") command = { kind: "start", owner: this.dreamingOwner, ...tokenLimit !== void 0 ? { tokenLimit } : {} };
11893
- else if (action === "resume" && id) command = { kind: "resume", id, owner: this.dreamingOwner, ...tokenLimit !== void 0 ? { tokenLimit } : {} };
11886
+ if (action === "start") command = { kind: "start", owner: this.dreamingOwner };
11887
+ else if (action === "resume" && id) command = { kind: "resume", id, owner: this.dreamingOwner };
11894
11888
  else if ((action === "pause" || action === "terminate") && id) command = { kind: action, id };
11895
11889
  else throw new Error("Invalid Dreaming control");
11896
11890
  const run = await this.retryExternalImportWrite(namespace, (memory) => memory.dreaming(command.kind === "start" && this.agentMemoryRetrievalWeight <= 0 ? { ...command, allowedEventIds: memory.listEvents().filter((event) => event.status !== "forgotten" && event.status !== "archived").map((event) => event.id) } : command));
@@ -11936,10 +11930,6 @@ var StrataGateRuntime = class {
11936
11930
  } finally {
11937
11931
  if (owned) await memory.close();
11938
11932
  }
11939
- if (run.tokenLimit !== null && run.estimatedTokens + estimateTokens(JSON.stringify(payload)) + 256 > run.tokenLimit) {
11940
- await mutate({ kind: "suspend", ...claim, reason: "\u5DF2\u8FBE\u5230\u672C\u8F6E token \u6D88\u8017\u8FB9\u754C\uFF0C\u8FDB\u5EA6\u5DF2\u4FDD\u5B58\uFF1B\u63D0\u9AD8\u8FB9\u754C\u540E\u53EF\u4EE5\u7EE7\u7EED\uFF08\u6D88\u8017\u4E3A\u4F30\u7B97\uFF09" });
11941
- return;
11942
- }
11943
11933
  const action = await this.models.runDetached(`dreaming:${run.id}`, () => this.models.dreamingStep(payload), controller.signal);
11944
11934
  if (controller.signal.aborted) return;
11945
11935
  const repeated = run.trace.filter((entry) => entry.tool === action.tool && JSON.stringify(entry.args) === JSON.stringify(action.args)).length;
@@ -13397,19 +13387,7 @@ ${profile}
13397
13387
  });
13398
13388
  scoped.on("agent/request", async (_payload, next) => {
13399
13389
  const config = await next();
13400
- const run = await this.runtime.dreamingRun(binding.namespace, binding.id);
13401
- const input = estimateTokens(JSON.stringify({ messages: [
13402
- ...binding.agent.session.deriveMessages(),
13403
- ...binding.pendingMessages ?? [],
13404
- { role: "system", content: [{ type: "text", text: this.systemPrompt() }] }
13405
- ], tools: modelTools })) + 1024;
13406
- if (run.tokenLimit !== null && run.estimatedTokens + input + 256 > run.tokenLimit) {
13407
- this.keepDirections(binding, binding.pendingMessages ?? []);
13408
- if (run.status !== "completed") await this.runtime.dreamingMutate(binding.namespace, run, { kind: "suspend", reason: "\u5DF2\u8FBE\u5230\u672C\u8F6E\u6D88\u8017\u8FB9\u754C\uFF0C\u8FDB\u5EA6\u5DF2\u4FDD\u5B58\uFF1B\u63D0\u9AD8\u8FB9\u754C\u540E\u53EF\u4EE5\u7EE7\u7EED\uFF08\u6D88\u8017\u4E3A\u4F30\u7B97\uFF09" });
13409
- throw new Error("Dreaming consumption boundary reached");
13410
- }
13411
- const remaining = run.tokenLimit === null ? RESPONSE_TOKEN_LIMIT : run.tokenLimit - run.estimatedTokens - input;
13412
- return { ...config, maxTokens: Math.min(RESPONSE_TOKEN_LIMIT, remaining) };
13390
+ return { ...config, maxTokens: RESPONSE_TOKEN_LIMIT };
13413
13391
  });
13414
13392
  const runtime = this.runtime;
13415
13393
  scoped.on("llm/stream", async function* (options, next) {
@@ -13420,10 +13398,6 @@ ${profile}
13420
13398
  await binding.accounting;
13421
13399
  const run = await runtime.dreamingRun(binding.namespace, binding.id);
13422
13400
  const input = estimateTokens(JSON.stringify({ messages: options.messages, tools: options.tools ?? [] }));
13423
- if (run.tokenLimit !== null && run.estimatedTokens + input + (options.maxTokens ?? 32768) > run.tokenLimit) {
13424
- if (run.status !== "completed") await runtime.dreamingMutate(binding.namespace, run, { kind: "suspend", reason: "\u5B9E\u9645\u6A21\u578B\u8BF7\u6C42\u8FBE\u5230\u672C\u8F6E\u6D88\u8017\u8FB9\u754C\uFF0C\u8FDB\u5EA6\u5DF2\u4FDD\u5B58\uFF08\u6D88\u8017\u4E3A\u4F30\u7B97\uFF09" });
13425
- throw new Error("Dreaming consumption boundary reached before model dispatch");
13426
- }
13427
13401
  const saved = await runtime.dreamingMutate(binding.namespace, run, { kind: "model-step", estimatedTokens: input });
13428
13402
  if (saved.status !== "running" && saved.status !== "completed") throw new Error("Dreaming paused at safe boundary");
13429
13403
  binding.pendingMessages = [];
@@ -15352,10 +15326,8 @@ async function handleAdminRequest(runtime, req, res) {
15352
15326
  sendJson(res, 200, await runtime.adminDreamingSession(namespace, id));
15353
15327
  return;
15354
15328
  }
15355
- const rawLimit = url.searchParams.get("tokenLimit");
15356
- const tokenLimit = rawLimit === null || rawLimit === "" ? void 0 : Number(rawLimit);
15357
- if (!["start", "pause", "resume", "terminate"].includes(action) || action !== "start" && !id || tokenLimit !== void 0 && (!Number.isSafeInteger(tokenLimit) || tokenLimit <= 0)) throw new AdminHttpError(400, "Invalid Dreaming control");
15358
- sendJson(res, 200, await runtime.adminDreamingControl(namespace, action, id, tokenLimit));
15329
+ if (!["start", "pause", "resume", "terminate"].includes(action) || action !== "start" && !id) throw new AdminHttpError(400, "Invalid Dreaming control");
15330
+ sendJson(res, 200, await runtime.adminDreamingControl(namespace, action, id));
15359
15331
  } else throw new AdminHttpError(405, "Dreaming requires GET or POST");
15360
15332
  } else if (path === "/api/stratagate/import") {
15361
15333
  if (req.method === "GET") {