@flutchai/flutch-sdk 0.7.2 → 0.7.4

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
@@ -5212,6 +5212,10 @@ exports.EventProcessor = class _EventProcessor {
5212
5212
  attachments: [],
5213
5213
  metadata: {},
5214
5214
  traceEvents: [],
5215
+ traceMessages: {
5216
+ messages: {},
5217
+ keys: /* @__PURE__ */ new WeakMap()
5218
+ },
5215
5219
  traceStartedAt: null,
5216
5220
  traceCompletedAt: null
5217
5221
  };
@@ -5613,6 +5617,7 @@ exports.EventProcessor = class _EventProcessor {
5613
5617
  const completedAt = acc.traceCompletedAt ?? startedAt;
5614
5618
  const trace = acc.traceEvents.length > 0 ? {
5615
5619
  events: acc.traceEvents,
5620
+ messages: acc.traceMessages.messages,
5616
5621
  startedAt,
5617
5622
  completedAt,
5618
5623
  durationMs: Math.max(0, completedAt - startedAt),
@@ -5640,14 +5645,14 @@ exports.EventProcessor = class _EventProcessor {
5640
5645
  };
5641
5646
  }
5642
5647
  captureTraceEvent(acc, event) {
5643
- const normalized = this.normalizeTraceEvent(event);
5648
+ const normalized = this.normalizeTraceEvent(event, acc.traceMessages);
5644
5649
  if (!normalized) {
5645
5650
  return;
5646
5651
  }
5647
5652
  acc.traceEvents.push(normalized);
5648
5653
  this.logger.debug(`[TRACE] Captured trace event ${normalized.type} for node ${normalized.nodeName} (${acc.traceEvents.length} total)`);
5649
5654
  }
5650
- normalizeTraceEvent(event) {
5655
+ normalizeTraceEvent(event, traceMessages) {
5651
5656
  const type = event?.event ?? event?.type;
5652
5657
  if (!type) {
5653
5658
  return null;
@@ -5686,7 +5691,7 @@ exports.EventProcessor = class _EventProcessor {
5686
5691
  const metadata = sanitizeTraceData(event?.metadata, 0, /* @__PURE__ */ new WeakSet(), {
5687
5692
  maxStringLength: TRACE_MAX_STRING
5688
5693
  });
5689
- const data = sanitizeTraceData(compactTraceMessages(event?.data), 0, /* @__PURE__ */ new WeakSet(), {
5694
+ const data = sanitizeTraceData(refTraceMessages(event?.data, traceMessages), 0, /* @__PURE__ */ new WeakSet(), {
5690
5695
  maxStringLength: TRACE_MAX_STRING
5691
5696
  });
5692
5697
  const timestampSource = event?.timestamp ?? event?.time ?? event?.data?.timestamp ?? event?.data?.ts ?? Date.now();
@@ -5708,39 +5713,39 @@ exports.EventProcessor = class _EventProcessor {
5708
5713
  exports.EventProcessor = _ts_decorate21([
5709
5714
  common.Injectable()
5710
5715
  ], exports.EventProcessor);
5711
- var TRACE_MAX_STRING = 2e4;
5712
- var TRACE_KEEP_LAST_MESSAGES = 6;
5713
- function compactTraceMessages(value, depth = 0) {
5716
+ var TRACE_MAX_STRING = 1e5;
5717
+ function refTraceMessages(value, store, depth = 0) {
5714
5718
  if (depth > 6 || value === null || typeof value !== "object") return value;
5715
5719
  if (Array.isArray(value)) {
5716
- const items = isHistory(value) ? compactHistory(value) : value;
5717
- return items.map((item) => compactTraceMessages(item, depth + 1));
5720
+ if (isHistory(value)) {
5721
+ return value.map((message) => ({
5722
+ _traceMsg: messageKey(message, store)
5723
+ }));
5724
+ }
5725
+ return value.map((item) => refTraceMessages(item, store, depth + 1));
5718
5726
  }
5719
5727
  const out = {};
5720
5728
  for (const [key, item] of Object.entries(value)) {
5721
- out[key] = compactTraceMessages(item, depth + 1);
5729
+ out[key] = refTraceMessages(item, store, depth + 1);
5722
5730
  }
5723
5731
  return out;
5724
5732
  }
5725
- __name(compactTraceMessages, "compactTraceMessages");
5733
+ __name(refTraceMessages, "refTraceMessages");
5734
+ function messageKey(message, store) {
5735
+ const known = store.keys.get(message);
5736
+ if (known) return known;
5737
+ const key = `m${Object.keys(store.messages).length + 1}`;
5738
+ store.keys.set(message, key);
5739
+ store.messages[key] = sanitizeTraceData(message, 0, /* @__PURE__ */ new WeakSet(), {
5740
+ maxStringLength: TRACE_MAX_STRING
5741
+ });
5742
+ return key;
5743
+ }
5744
+ __name(messageKey, "messageKey");
5726
5745
  function isHistory(items) {
5727
- return items.length > TRACE_KEEP_LAST_MESSAGES + 2 && items.every((item) => item !== null && typeof item === "object" && !Array.isArray(item) && ("lc_kwargs" in item || "content" in item && ("type" in item || "role" in item)));
5746
+ return items.length > 0 && items.every((item) => item !== null && typeof item === "object" && !Array.isArray(item) && ("lc_kwargs" in item || "content" in item && ("type" in item || "role" in item)));
5728
5747
  }
5729
5748
  __name(isHistory, "isHistory");
5730
- function compactHistory(messages) {
5731
- const keep = TRACE_KEEP_LAST_MESSAGES + 1;
5732
- if (messages.length <= keep + 1) return messages;
5733
- const omitted = messages.length - keep;
5734
- return [
5735
- messages[0],
5736
- {
5737
- type: "trace_omitted",
5738
- omittedMessages: omitted
5739
- },
5740
- ...messages.slice(-TRACE_KEEP_LAST_MESSAGES)
5741
- ];
5742
- }
5743
- __name(compactHistory, "compactHistory");
5744
5749
  var als = new async_hooks.AsyncLocalStorage();
5745
5750
  function withFlutchContext(ctx, fn) {
5746
5751
  return als.run(ctx, fn);
@@ -5985,6 +5990,10 @@ exports.LangGraphEngine = class _LangGraphEngine {
5985
5990
  traceEvents: trace?.events?.length || 0,
5986
5991
  hadError: !!streamError
5987
5992
  });
5993
+ if (streamError && isRecursionLimit(streamError) && content?.text?.trim()) {
5994
+ this.logger.warn(`[STREAM-ERROR] Recursion limit reached after the answer was written; returning the answer`);
5995
+ return content;
5996
+ }
5988
5997
  if (streamError) {
5989
5998
  throw streamError;
5990
5999
  }
@@ -6014,6 +6023,7 @@ exports.LangGraphEngine = class _LangGraphEngine {
6014
6023
  agentId: context.agentId || "unknown",
6015
6024
  companyId: context.companyId || "unknown",
6016
6025
  events: trace.events,
6026
+ messages: trace.messages,
6017
6027
  totalEvents: trace.totalEvents,
6018
6028
  startedAt: trace.startedAt,
6019
6029
  completedAt: trace.completedAt || Date.now(),
@@ -6100,6 +6110,7 @@ exports.LangGraphEngine = class _LangGraphEngine {
6100
6110
  durationMs: payload.durationMs,
6101
6111
  status: payload.status || "success",
6102
6112
  error: payload.error,
6113
+ messages: payload.messages,
6103
6114
  events: payload.events.map((event) => ({
6104
6115
  timestamp: event.timestamp ? new Date(event.timestamp).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
6105
6116
  meta: {
@@ -6191,6 +6202,10 @@ exports.LangGraphEngine = _ts_decorate22([
6191
6202
  typeof config.ConfigService === "undefined" ? Object : config.ConfigService
6192
6203
  ])
6193
6204
  ], exports.LangGraphEngine);
6205
+ function isRecursionLimit(error) {
6206
+ return error.name === "GraphRecursionError" || /Recursion limit of \d+ reached/.test(error.message);
6207
+ }
6208
+ __name(isRecursionLimit, "isRecursionLimit");
6194
6209
 
6195
6210
  // src/engines/graph-engine.factory.ts
6196
6211
  function _ts_decorate23(decorators, target, key, desc) {
@@ -9133,7 +9148,6 @@ exports.buildEffortRequestFields = buildEffortRequestFields;
9133
9148
  exports.buildOAuthAuthorizationUrl = buildOAuthAuthorizationUrl;
9134
9149
  exports.buildOpenAIModelConfig = buildOpenAIModelConfig;
9135
9150
  exports.clearAttachmentDataStore = clearAttachmentDataStore;
9136
- exports.compactTraceMessages = compactTraceMessages;
9137
9151
  exports.createEndpointDescriptors = createEndpointDescriptors;
9138
9152
  exports.createGraphAttachment = createGraphAttachment;
9139
9153
  exports.createMongoClientAdapter = createMongoClientAdapter;
@@ -9175,6 +9189,7 @@ exports.modelSupportsPromptCache = modelSupportsPromptCache;
9175
9189
  exports.normalizeToolConfigs = normalizeToolConfigs;
9176
9190
  exports.parseToolSearchResult = parseToolSearchResult;
9177
9191
  exports.redactConnectionString = redactConnectionString;
9192
+ exports.refTraceMessages = refTraceMessages;
9178
9193
  exports.registerFinanceExampleCallback = registerFinanceExampleCallback;
9179
9194
  exports.registerUIEndpointsFromClass = registerUIEndpointsFromClass;
9180
9195
  exports.resolveOAuthProviderConfig = resolveOAuthProviderConfig;