@ljoukov/llm 4.0.5 → 4.0.6

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
@@ -2794,6 +2794,7 @@ var import_node_async_hooks = require("async_hooks");
2794
2794
  var import_node_buffer2 = require("buffer");
2795
2795
  var import_promises = require("fs/promises");
2796
2796
  var import_node_path3 = __toESM(require("path"), 1);
2797
+ var DEFAULT_THOUGHT_DELTA_LOG_THROTTLE_MS = 4e3;
2797
2798
  function toIsoNow() {
2798
2799
  return (/* @__PURE__ */ new Date()).toISOString();
2799
2800
  }
@@ -2957,6 +2958,63 @@ function appendAgentStreamEventLog(options) {
2957
2958
  }
2958
2959
  }
2959
2960
  }
2961
+ function createAgentStreamEventLogger(options) {
2962
+ const thoughtDeltaThrottleMs = Math.max(
2963
+ 0,
2964
+ options.thoughtDeltaThrottleMs ?? DEFAULT_THOUGHT_DELTA_LOG_THROTTLE_MS
2965
+ );
2966
+ let pendingThoughtDelta = "";
2967
+ let thoughtFlushTimer = null;
2968
+ const cancelThoughtFlushTimer = () => {
2969
+ if (thoughtFlushTimer === null) {
2970
+ return;
2971
+ }
2972
+ clearTimeout(thoughtFlushTimer);
2973
+ thoughtFlushTimer = null;
2974
+ };
2975
+ const flushThoughtDelta = () => {
2976
+ cancelThoughtFlushTimer();
2977
+ if (pendingThoughtDelta.length === 0) {
2978
+ return;
2979
+ }
2980
+ options.append(`thought_delta: ${pendingThoughtDelta}`);
2981
+ pendingThoughtDelta = "";
2982
+ };
2983
+ const scheduleThoughtFlush = () => {
2984
+ if (thoughtFlushTimer !== null || thoughtDeltaThrottleMs === 0) {
2985
+ return;
2986
+ }
2987
+ thoughtFlushTimer = setTimeout(() => {
2988
+ thoughtFlushTimer = null;
2989
+ flushThoughtDelta();
2990
+ }, thoughtDeltaThrottleMs);
2991
+ thoughtFlushTimer.unref?.();
2992
+ };
2993
+ return {
2994
+ appendEvent: (event) => {
2995
+ if (event.type === "delta" && event.channel === "thought") {
2996
+ if (event.text.length === 0) {
2997
+ return;
2998
+ }
2999
+ pendingThoughtDelta += event.text;
3000
+ if (thoughtDeltaThrottleMs === 0) {
3001
+ flushThoughtDelta();
3002
+ return;
3003
+ }
3004
+ scheduleThoughtFlush();
3005
+ return;
3006
+ }
3007
+ flushThoughtDelta();
3008
+ appendAgentStreamEventLog({
3009
+ event,
3010
+ append: options.append
3011
+ });
3012
+ },
3013
+ flush: () => {
3014
+ flushThoughtDelta();
3015
+ }
3016
+ };
3017
+ }
2960
3018
  var AgentLoggingSessionImpl = class {
2961
3019
  workspaceDir;
2962
3020
  logsRootDir;
@@ -10136,19 +10194,17 @@ async function runAgentLoopInternal(request, context) {
10136
10194
  );
10137
10195
  const sourceOnEvent = toolLoopRequestWithSteering.onEvent;
10138
10196
  const includeLlmStreamEvents = telemetrySession?.includeLlmStreamEvents === true;
10197
+ const streamEventLogger = loggingSession ? createAgentStreamEventLogger({
10198
+ append: (line) => {
10199
+ loggingSession.logLine(`[agent:${runId}] ${line}`);
10200
+ }
10201
+ }) : void 0;
10139
10202
  const wrappedOnEvent = sourceOnEvent || includeLlmStreamEvents ? (event) => {
10140
10203
  sourceOnEvent?.(event);
10141
10204
  if (includeLlmStreamEvents) {
10142
10205
  emitTelemetry({ type: "agent.run.stream", event });
10143
10206
  }
10144
- if (loggingSession) {
10145
- appendAgentStreamEventLog({
10146
- event,
10147
- append: (line) => {
10148
- loggingSession.logLine(`[agent:${runId}] ${line}`);
10149
- }
10150
- });
10151
- }
10207
+ streamEventLogger?.appendEvent(event);
10152
10208
  } : void 0;
10153
10209
  try {
10154
10210
  const result = await runToolLoop({
@@ -10157,6 +10213,7 @@ async function runAgentLoopInternal(request, context) {
10157
10213
  ...wrappedOnEvent ? { onEvent: wrappedOnEvent } : {},
10158
10214
  tools: mergedTools
10159
10215
  });
10216
+ streamEventLogger?.flush();
10160
10217
  emitTelemetry({
10161
10218
  type: "agent.run.completed",
10162
10219
  success: true,
@@ -10189,6 +10246,7 @@ async function runAgentLoopInternal(request, context) {
10189
10246
  }
10190
10247
  return result;
10191
10248
  } catch (error) {
10249
+ streamEventLogger?.flush();
10192
10250
  emitTelemetry({
10193
10251
  type: "agent.run.completed",
10194
10252
  success: false,
@@ -10205,6 +10263,7 @@ async function runAgentLoopInternal(request, context) {
10205
10263
  );
10206
10264
  throw error;
10207
10265
  } finally {
10266
+ streamEventLogger?.flush();
10208
10267
  await subagentController?.closeAll();
10209
10268
  }
10210
10269
  }