@flutchai/flutch-sdk 0.7.15 → 0.7.16

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.d.cts CHANGED
@@ -1733,6 +1733,7 @@ declare class EventProcessor {
1733
1733
  private processContentStream;
1734
1734
  private mapReasoningSteps;
1735
1735
  processEvent(acc: StreamAccumulator, event: any, onPartial?: (chunk: string) => void): void;
1736
+ private closeToolBlock;
1736
1737
  getResult(acc: StreamAccumulator): {
1737
1738
  content: IStoredMessageContent;
1738
1739
  trace: {
package/dist/index.d.ts CHANGED
@@ -1733,6 +1733,7 @@ declare class EventProcessor {
1733
1733
  private processContentStream;
1734
1734
  private mapReasoningSteps;
1735
1735
  processEvent(acc: StreamAccumulator, event: any, onPartial?: (chunk: string) => void): void;
1736
+ private closeToolBlock;
1736
1737
  getResult(acc: StreamAccumulator): {
1737
1738
  content: IStoredMessageContent;
1738
1739
  trace: {
package/dist/index.js CHANGED
@@ -5261,6 +5261,7 @@ function joinTextSteps(parts) {
5261
5261
  __name(joinTextSteps, "joinTextSteps");
5262
5262
  var MIN_DUPLICATE_WORDS = 8;
5263
5263
  var DUPLICATE_SIMILARITY = 0.75;
5264
+ var NO_TOOL_END = "No result: the call's end was not received.";
5264
5265
  function paragraphsOf(text) {
5265
5266
  return text.split(/\n\s*\n/).map((p) => p.trim()).filter(Boolean);
5266
5267
  }
@@ -5685,21 +5686,7 @@ var EventProcessor = class _EventProcessor {
5685
5686
  } catch {
5686
5687
  outputString = typeof output?.content === "string" ? output.content : "[Output too large to display]";
5687
5688
  }
5688
- const MAX_TOOL_OUTPUT_LENGTH = 5e4;
5689
- if (outputString.length > MAX_TOOL_OUTPUT_LENGTH) {
5690
- let cutAt = outputString.lastIndexOf("\n", MAX_TOOL_OUTPUT_LENGTH);
5691
- if (cutAt < MAX_TOOL_OUTPUT_LENGTH * 0.8) {
5692
- cutAt = MAX_TOOL_OUTPUT_LENGTH;
5693
- }
5694
- outputString = outputString.slice(0, cutAt) + `
5695
- ... [truncated: ${outputString.length - cutAt} chars]`;
5696
- }
5697
- toolBlock.output = outputString;
5698
- this.sendDelta(channel, {
5699
- type: "tool_output_chunk",
5700
- stepId: toolBlock.id,
5701
- chunk: outputString
5702
- }, onPartial);
5689
+ this.closeToolBlock(channel, toolBlock, outputString, onPartial);
5703
5690
  this.logger.log("\u2705 Tool completed", {
5704
5691
  toolName: event.name,
5705
5692
  toolBlockId: toolBlock.id,
@@ -5716,8 +5703,18 @@ var EventProcessor = class _EventProcessor {
5716
5703
  if (event.event === "on_tool_error") {
5717
5704
  const channel = event.metadata?.stream_channel ?? StreamChannel.TEXT;
5718
5705
  const state = acc.channels.get(channel);
5719
- if (state && event.run_id) {
5706
+ let toolBlock;
5707
+ if (state && event.run_id && state.toolBlocksByRunId.has(event.run_id)) {
5708
+ toolBlock = state.toolBlocksByRunId.get(event.run_id);
5720
5709
  state.toolBlocksByRunId.delete(event.run_id);
5710
+ } else if (state) {
5711
+ const idx = state.pendingToolBlocks.findIndex((b) => b.name === event.name);
5712
+ if (idx !== -1) toolBlock = state.pendingToolBlocks.splice(idx, 1)[0];
5713
+ }
5714
+ if (toolBlock && toolBlock.type === "tool_use") {
5715
+ const error = event.data?.error;
5716
+ const message = error instanceof Error ? error.message : String(error ?? "failed");
5717
+ this.closeToolBlock(channel, toolBlock, `Error: ${message}`, onPartial);
5721
5718
  }
5722
5719
  this.logger.error("\u274C Tool failed", {
5723
5720
  toolName: event.name,
@@ -5758,6 +5755,28 @@ var EventProcessor = class _EventProcessor {
5758
5755
  }
5759
5756
  }
5760
5757
  /**
5758
+ * A call's result on its block and to the client: the client closes the
5759
+ * step on it. Cut to keep the message document under MongoDB's 16MB.
5760
+ */
5761
+ closeToolBlock(channel, toolBlock, output, onPartial) {
5762
+ const MAX_TOOL_OUTPUT_LENGTH = 5e4;
5763
+ let outputString = output;
5764
+ if (outputString.length > MAX_TOOL_OUTPUT_LENGTH) {
5765
+ let cutAt = outputString.lastIndexOf("\n", MAX_TOOL_OUTPUT_LENGTH);
5766
+ if (cutAt < MAX_TOOL_OUTPUT_LENGTH * 0.8) {
5767
+ cutAt = MAX_TOOL_OUTPUT_LENGTH;
5768
+ }
5769
+ outputString = outputString.slice(0, cutAt) + `
5770
+ ... [truncated: ${outputString.length - cutAt} chars]`;
5771
+ }
5772
+ toolBlock.output = outputString;
5773
+ this.sendDelta(channel, {
5774
+ type: "tool_output_chunk",
5775
+ stepId: toolBlock.id,
5776
+ chunk: outputString
5777
+ }, onPartial);
5778
+ }
5779
+ /**
5761
5780
  * Build final result from accumulator
5762
5781
  * Returns unified content chains from all channels
5763
5782
  */
@@ -5770,6 +5789,12 @@ var EventProcessor = class _EventProcessor {
5770
5789
  pendingCount: state.pendingToolBlocks.length,
5771
5790
  mappedCount: state.toolBlocksByRunId.size
5772
5791
  });
5792
+ for (const block of [
5793
+ ...state.pendingToolBlocks,
5794
+ ...state.toolBlocksByRunId.values()
5795
+ ]) if (block.type === "tool_use" && !block.output) block.output = NO_TOOL_END;
5796
+ state.pendingToolBlocks = [];
5797
+ state.toolBlocksByRunId.clear();
5773
5798
  }
5774
5799
  if (state.currentBlock) {
5775
5800
  state.contentChain.push(state.currentBlock);