@kenkaiiii/gg-agent 5.16.0 → 5.18.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.
package/dist/index.cjs CHANGED
@@ -252,10 +252,14 @@ async function* agentLoop(messages, options) {
252
252
  const initialHardTimeoutMs = isSakana ? STREAM_THINKING_HARD_TIMEOUT_MS : STREAM_HARD_TIMEOUT_MS;
253
253
  const MAX_TOOLCALL_DELTA_CHARS = 1e6;
254
254
  const MAX_TOOLCALL_DELTA_EVENTS = 2e4;
255
+ let logicalTurnStartedAt = 0;
256
+ let firstProviderEventAt;
257
+ let providerDurationMs = 0;
255
258
  try {
256
259
  while (turn < maxTurns) {
257
260
  options.signal?.throwIfAborted();
258
261
  turn++;
262
+ if (logicalTurnStartedAt === 0) logicalTurnStartedAt = Date.now();
259
263
  toolMap = new Map((options.tools ?? []).map((t) => [t.name, t]));
260
264
  if (_diagFn) {
261
265
  let msgChars = 0;
@@ -305,6 +309,7 @@ async function* agentLoop(messages, options) {
305
309
  let idleTimer = null;
306
310
  let hardTimer = null;
307
311
  let idleTimedOut = false;
312
+ let providerAttemptStartedAt;
308
313
  let streamEventCount = 0;
309
314
  let lastEventTime = Date.now();
310
315
  let streamCallStart = Date.now();
@@ -349,12 +354,14 @@ async function* agentLoop(messages, options) {
349
354
  try {
350
355
  diag("stream_call", { nonStreaming: useNonStreamingFallback });
351
356
  streamCallStart = Date.now();
357
+ providerAttemptStartedAt = streamCallStart;
352
358
  const result = (0, import_gg_ai.stream)({
353
359
  provider: options.provider,
354
360
  model: options.model,
355
361
  messages,
356
362
  tools: options.tools,
357
363
  serverTools: options.serverTools,
364
+ toolChoice: options.toolChoice,
358
365
  webSearch: options.webSearch,
359
366
  maxTokens: options.maxTokens,
360
367
  temperature: options.temperature,
@@ -393,6 +400,7 @@ async function* agentLoop(messages, options) {
393
400
  maxConsumerLagMs = consumerLag;
394
401
  }
395
402
  streamEventCount++;
403
+ if (firstProviderEventAt === void 0) firstProviderEventAt = pullTime;
396
404
  eventTypeCounts[event.type] = (eventTypeCounts[event.type] ?? 0) + 1;
397
405
  lastEventType = event.type;
398
406
  if ((event.type === "text_delta" || event.type === "server_toolcall" || event.type === "toolcall_delta") && !hasReceivedEvent) {
@@ -485,6 +493,7 @@ async function* agentLoop(messages, options) {
485
493
  eventTypes: eventTypeCounts
486
494
  });
487
495
  response = await result.response;
496
+ if (firstProviderEventAt === void 0) firstProviderEventAt = Date.now();
488
497
  } catch (err) {
489
498
  const errMsg = err instanceof Error ? err.message : String(err);
490
499
  diag("stream_error", {
@@ -708,6 +717,9 @@ async function* agentLoop(messages, options) {
708
717
  });
709
718
  throw err;
710
719
  } finally {
720
+ if (providerAttemptStartedAt !== void 0) {
721
+ providerDurationMs += Date.now() - providerAttemptStartedAt;
722
+ }
711
723
  if (idleTimer) clearTimeout(idleTimer);
712
724
  if (hardTimer) clearTimeout(hardTimer);
713
725
  options.signal?.removeEventListener("abort", forwardAbort);
@@ -751,11 +763,27 @@ async function* agentLoop(messages, options) {
751
763
  totalUsage.cacheWrite = (totalUsage.cacheWrite ?? 0) + response.usage.cacheWrite;
752
764
  }
753
765
  messages.push(response.message);
766
+ const completedAt = Date.now();
767
+ const outputTokensPerSecond = providerDurationMs > 0 && response.usage.outputTokens > 0 ? response.usage.outputTokens / (providerDurationMs / 1e3) : void 0;
768
+ const timing = {
769
+ startedAt: logicalTurnStartedAt,
770
+ ...firstProviderEventAt !== void 0 ? {
771
+ firstProviderEventAt,
772
+ ttftMs: Math.max(0, firstProviderEventAt - logicalTurnStartedAt)
773
+ } : {},
774
+ completedAt,
775
+ providerDurationMs,
776
+ ...outputTokensPerSecond !== void 0 ? { outputTokensPerSecond } : {}
777
+ };
778
+ logicalTurnStartedAt = 0;
779
+ firstProviderEventAt = void 0;
780
+ providerDurationMs = 0;
754
781
  yield {
755
782
  type: "turn_end",
756
783
  turn,
757
784
  stopReason: response.stopReason,
758
- usage: response.usage
785
+ usage: response.usage,
786
+ timing
759
787
  };
760
788
  if (response.stopReason === "pause_turn") {
761
789
  consecutivePauses++;
@@ -936,8 +964,8 @@ async function executeSingleToolCall(toolCall, options, pushEvent) {
936
964
  };
937
965
  const raw = await tool.execute(parsed, ctx);
938
966
  const normalized = normalizeToolResult(raw);
939
- resultContent = normalized.content;
940
- details = normalized.details;
967
+ resultContent = (0, import_gg_ai.redactValue)(normalized.content);
968
+ details = (0, import_gg_ai.redactValue)(normalized.details);
941
969
  for (const key of options.invalidToolArgumentCounts.keys()) {
942
970
  if (key.startsWith(`${toolCall.name}:`)) options.invalidToolArgumentCounts.delete(key);
943
971
  }
@@ -965,10 +993,12 @@ async function executeSingleToolCall(toolCall, options, pushEvent) {
965
993
  );
966
994
  }
967
995
  } else {
968
- resultContent = err instanceof Error ? err.message : String(err);
996
+ resultContent = (0, import_gg_ai.redactValue)(err instanceof Error ? err.message : String(err));
969
997
  }
970
998
  }
971
999
  }
1000
+ resultContent = (0, import_gg_ai.redactValue)(resultContent);
1001
+ details = (0, import_gg_ai.redactValue)(details);
972
1002
  const durationMs = Date.now() - startTime;
973
1003
  pushEvent({
974
1004
  type: "tool_call_end",