@kenkaiiii/gg-agent 5.19.0 → 5.19.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.
package/dist/index.cjs CHANGED
@@ -216,6 +216,8 @@ async function* agentLoop(messages, options) {
216
216
  const maxContinuations = options.maxContinuations ?? 5;
217
217
  let toolMap = new Map((options.tools ?? []).map((t) => [t.name, t]));
218
218
  const totalUsage = { inputTokens: 0, outputTokens: 0 };
219
+ let latestProviderUsage;
220
+ let usageAnchorIndex;
219
221
  let turn = 0;
220
222
  let hitMaxTurns = false;
221
223
  let firstTurn = true;
@@ -292,7 +294,11 @@ async function* agentLoop(messages, options) {
292
294
  firstTurn = false;
293
295
  if (options.transformContext) {
294
296
  diag("transform_start");
295
- const transformed = await options.transformContext(messages);
297
+ const pendingMessages = usageAnchorIndex === void 0 ? [] : messages.slice(usageAnchorIndex + 1);
298
+ const transformed = await options.transformContext(messages, {
299
+ usage: latestProviderUsage,
300
+ pendingMessages
301
+ });
296
302
  if (transformed !== messages) {
297
303
  diag("transform_compacted", {
298
304
  before: messages.length,
@@ -300,6 +306,8 @@ async function* agentLoop(messages, options) {
300
306
  });
301
307
  messages.length = 0;
302
308
  messages.push(...transformed);
309
+ latestProviderUsage = void 0;
310
+ usageAnchorIndex = void 0;
303
311
  }
304
312
  diag("transform_end");
305
313
  }
@@ -554,10 +562,17 @@ async function* agentLoop(messages, options) {
554
562
  ...overflowDetails
555
563
  });
556
564
  try {
557
- const compacted = await options.transformContext(messages, { force: true });
565
+ const pendingMessages = usageAnchorIndex === void 0 ? [] : messages.slice(usageAnchorIndex + 1);
566
+ const compacted = await options.transformContext(messages, {
567
+ force: true,
568
+ usage: latestProviderUsage,
569
+ pendingMessages
570
+ });
558
571
  if (compacted !== messages && compacted.length < messages.length) {
559
572
  messages.length = 0;
560
573
  messages.push(...compacted);
574
+ latestProviderUsage = void 0;
575
+ usageAnchorIndex = void 0;
561
576
  diag("overflow_compact_success", {
562
577
  attempt: overflowCompactionAttempts,
563
578
  messages: messages.length,
@@ -763,6 +778,8 @@ async function* agentLoop(messages, options) {
763
778
  totalUsage.cacheWrite = (totalUsage.cacheWrite ?? 0) + response.usage.cacheWrite;
764
779
  }
765
780
  messages.push(response.message);
781
+ latestProviderUsage = response.usage;
782
+ usageAnchorIndex = messages.length - 1;
766
783
  const completedAt = Date.now();
767
784
  const outputTokensPerSecond = providerDurationMs > 0 && response.usage.outputTokens > 0 ? response.usage.outputTokens / (providerDurationMs / 1e3) : void 0;
768
785
  const timing = {
@@ -850,6 +867,7 @@ async function* agentLoop(messages, options) {
850
867
  const executionOptions = {
851
868
  signal: options.signal,
852
869
  maxToolResultChars: options.maxToolResultChars,
870
+ maxTurnToolResultChars: options.maxTurnToolResultChars,
853
871
  toolMap,
854
872
  invalidToolArgumentCounts,
855
873
  markFatalToolArgumentError
@@ -1086,6 +1104,7 @@ async function* executeToolCallsMixed(toolCalls, initialToolResults, options) {
1086
1104
  }
1087
1105
  const toolResults = buildToolResults(initialToolResults, toolCalls, resultsById);
1088
1106
  capToolResults(toolResults, options.maxToolResultChars);
1107
+ capTurnToolResults(toolResults, options.maxTurnToolResultChars);
1089
1108
  return { toolResults, aborted };
1090
1109
  }
1091
1110
  async function* executeToolCallsParallel(toolCalls, initialToolResults, options) {
@@ -1125,6 +1144,7 @@ async function* executeToolCallsParallel(toolCalls, initialToolResults, options)
1125
1144
  }
1126
1145
  const toolResults = buildToolResults(initialToolResults, toolCalls, resultsById);
1127
1146
  capToolResults(toolResults, options.maxToolResultChars);
1147
+ capTurnToolResults(toolResults, options.maxTurnToolResultChars);
1128
1148
  return { toolResults, aborted };
1129
1149
  }
1130
1150
  function buildToolResults(initialToolResults, toolCalls, resultsById) {
@@ -1167,6 +1187,34 @@ function capToolResults(toolResults, maxToolResultChars) {
1167
1187
  ` + tail;
1168
1188
  }
1169
1189
  }
1190
+ function capTurnToolResults(toolResults, maxTurnToolResultChars) {
1191
+ if (!maxTurnToolResultChars) return;
1192
+ const textResults = toolResults.filter(
1193
+ (toolResult) => typeof toolResult.content === "string"
1194
+ );
1195
+ const total = textResults.reduce((sum, toolResult) => sum + toolResult.content.length, 0);
1196
+ if (total <= maxTurnToolResultChars) return;
1197
+ const bySize = [...textResults].sort((a, b) => a.content.length - b.content.length);
1198
+ let remaining = maxTurnToolResultChars;
1199
+ let left = bySize.length;
1200
+ for (const toolResult of bySize) {
1201
+ const fairShare = Math.floor(remaining / left);
1202
+ left--;
1203
+ if (toolResult.content.length <= fairShare) {
1204
+ remaining -= toolResult.content.length;
1205
+ continue;
1206
+ }
1207
+ remaining -= fairShare;
1208
+ const headChars = Math.floor(fairShare * 0.7);
1209
+ const tailChars = fairShare - headChars;
1210
+ const omitted = toolResult.content.length - fairShare;
1211
+ toolResult.content = toolResult.content.slice(0, headChars) + `
1212
+
1213
+ [... ${omitted} characters trimmed: this turn's combined tool results exceeded the per-turn budget. Re-run this call alone with narrower filters or offset/limit if you need the omitted content ...]
1214
+
1215
+ ` + (tailChars > 0 ? toolResult.content.slice(-tailChars) : "");
1216
+ }
1217
+ }
1170
1218
  function normalizeToolResult(raw) {
1171
1219
  return typeof raw === "string" ? { content: raw } : raw;
1172
1220
  }