@zqagent/core 0.3.9 → 0.3.11

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.
@@ -1935,6 +1935,76 @@ function applyToolResultBudget(tool, result, opts = {}) {
1935
1935
  return { content, truncated: true, savedPath };
1936
1936
  }
1937
1937
 
1938
+ // src/engine/history.ts
1939
+ function repairToolCallHistoryDetailed(messages) {
1940
+ const out = [];
1941
+ let droppedCalls = 0;
1942
+ let droppedResults = 0;
1943
+ let droppedMessages = 0;
1944
+ let i = 0;
1945
+ while (i < messages.length) {
1946
+ const m = messages[i];
1947
+ if (m.role === "tool") {
1948
+ droppedResults += 1;
1949
+ i += 1;
1950
+ continue;
1951
+ }
1952
+ const calls = m.role === "assistant" ? m.tool_calls : void 0;
1953
+ if (!calls?.length) {
1954
+ if (m.role === "assistant" && m.tool_calls) {
1955
+ const { tool_calls, ...rest } = m;
1956
+ void tool_calls;
1957
+ out.push(rest);
1958
+ } else {
1959
+ out.push(m);
1960
+ }
1961
+ i += 1;
1962
+ continue;
1963
+ }
1964
+ const responders = /* @__PURE__ */ new Map();
1965
+ const notices = [];
1966
+ let j = i + 1;
1967
+ while (j < messages.length) {
1968
+ const t = messages[j];
1969
+ if (t.role === "notice") {
1970
+ notices.push(t);
1971
+ j += 1;
1972
+ continue;
1973
+ }
1974
+ if (t.role !== "tool") break;
1975
+ if (t.tool_call_id && !responders.has(t.tool_call_id)) {
1976
+ responders.set(t.tool_call_id, t);
1977
+ }
1978
+ j += 1;
1979
+ }
1980
+ const kept = calls.filter((tc) => tc.id && responders.has(tc.id));
1981
+ droppedCalls += calls.length - kept.length;
1982
+ droppedResults += responders.size - kept.length;
1983
+ if (kept.length === 0) {
1984
+ const { tool_calls, ...rest } = m;
1985
+ void tool_calls;
1986
+ if ((rest.content ?? "").trim() !== "") out.push(rest);
1987
+ else droppedMessages += 1;
1988
+ out.push(...notices);
1989
+ } else {
1990
+ out.push(kept.length === calls.length ? m : { ...m, tool_calls: kept });
1991
+ for (const tc of kept) out.push(responders.get(tc.id));
1992
+ out.push(...notices);
1993
+ }
1994
+ i = j;
1995
+ }
1996
+ const unchanged = out.length === messages.length && out.every((m, idx) => m === messages[idx]);
1997
+ return {
1998
+ messages: unchanged ? messages : out,
1999
+ droppedCalls,
2000
+ droppedResults,
2001
+ droppedMessages
2002
+ };
2003
+ }
2004
+ function repairToolCallHistory(messages) {
2005
+ return repairToolCallHistoryDetailed(messages).messages;
2006
+ }
2007
+
1938
2008
  // src/engine/TurnEngine.ts
1939
2009
  import * as crypto from "crypto";
1940
2010
  import * as fs3 from "fs";
@@ -2358,6 +2428,7 @@ ${notice}` : text,
2358
2428
  attachments = routed.attachments;
2359
2429
  this.taskId = taskId;
2360
2430
  this._turnStartTime = Date.now();
2431
+ this.messages = repairToolCallHistory(this.messages);
2361
2432
  this.messages.push({
2362
2433
  role: "user",
2363
2434
  content: userInput,
@@ -3359,7 +3430,7 @@ ${notice}` : text,
3359
3430
  /** 过滤 notice + 剥离 display-only 字段,得到最终发送视图 */
3360
3431
  _finalizeOutbound(sourceMessages) {
3361
3432
  const _STRIP = /* @__PURE__ */ new Set(["source", "_display", "ts", "reasoning", "usage", "attachmentRefs"]);
3362
- return sourceMessages.filter((m) => m.role !== "notice").map((m) => {
3433
+ const stripped = sourceMessages.filter((m) => m.role !== "notice").map((m) => {
3363
3434
  if (m.role === "tool") {
3364
3435
  return {
3365
3436
  role: "tool",
@@ -3368,15 +3439,16 @@ ${notice}` : text,
3368
3439
  };
3369
3440
  }
3370
3441
  const staleAttachments = Boolean(m.attachments?.length) && (m.ts ?? 0) < this._turnStartTime;
3371
- const stripped = {};
3442
+ const plain = {};
3372
3443
  for (const [k, v] of Object.entries(m)) {
3373
3444
  if (staleAttachments && k === "attachments") continue;
3374
3445
  if (!_STRIP.has(k)) {
3375
- stripped[k] = v;
3446
+ plain[k] = v;
3376
3447
  }
3377
3448
  }
3378
- return stripped;
3449
+ return plain;
3379
3450
  });
3451
+ return repairToolCallHistory(stripped);
3380
3452
  }
3381
3453
  /** 软阈值(token 数):达到它只淘汰旧工具结果 */
3382
3454
  _softTriggerTokens() {