@flutchai/flutch-sdk 0.1.23 → 0.1.24

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
@@ -1125,6 +1125,7 @@ interface ChannelState {
1125
1125
  contentChain: IContentBlock[];
1126
1126
  currentBlock: IContentBlock | null;
1127
1127
  pendingToolBlocks: IContentBlock[];
1128
+ toolBlocksByRunId: Map<string, IContentBlock>;
1128
1129
  }
1129
1130
  interface StreamAccumulator {
1130
1131
  channels: Map<StreamChannel, ChannelState>;
package/dist/index.d.ts CHANGED
@@ -1125,6 +1125,7 @@ interface ChannelState {
1125
1125
  contentChain: IContentBlock[];
1126
1126
  currentBlock: IContentBlock | null;
1127
1127
  pendingToolBlocks: IContentBlock[];
1128
+ toolBlocksByRunId: Map<string, IContentBlock>;
1128
1129
  }
1129
1130
  interface StreamAccumulator {
1130
1131
  channels: Map<StreamChannel, ChannelState>;
package/dist/index.js CHANGED
@@ -4421,11 +4421,21 @@ var EventProcessor = class {
4421
4421
  channels: /* @__PURE__ */ new Map([
4422
4422
  [
4423
4423
  "text" /* TEXT */,
4424
- { contentChain: [], currentBlock: null, pendingToolBlocks: [] }
4424
+ {
4425
+ contentChain: [],
4426
+ currentBlock: null,
4427
+ pendingToolBlocks: [],
4428
+ toolBlocksByRunId: /* @__PURE__ */ new Map()
4429
+ }
4425
4430
  ],
4426
4431
  [
4427
4432
  "processing" /* PROCESSING */,
4428
- { contentChain: [], currentBlock: null, pendingToolBlocks: [] }
4433
+ {
4434
+ contentChain: [],
4435
+ currentBlock: null,
4436
+ pendingToolBlocks: [],
4437
+ toolBlocksByRunId: /* @__PURE__ */ new Map()
4438
+ }
4429
4439
  ]
4430
4440
  ]),
4431
4441
  attachments: [],
@@ -4598,11 +4608,20 @@ var EventProcessor = class {
4598
4608
  return;
4599
4609
  }
4600
4610
  if (event.event === "on_tool_start") {
4611
+ const channel = event.metadata?.stream_channel ?? "text" /* TEXT */;
4612
+ const state = acc.channels.get(channel);
4613
+ if (state && event.run_id) {
4614
+ const idx = state.pendingToolBlocks.findIndex(
4615
+ (b) => b.name === event.name
4616
+ );
4617
+ if (idx !== -1) {
4618
+ const block = state.pendingToolBlocks.splice(idx, 1)[0];
4619
+ state.toolBlocksByRunId.set(event.run_id, block);
4620
+ }
4621
+ }
4601
4622
  this.logger.log("\u{1F527} Tool execution started", {
4602
4623
  toolName: event.name,
4603
- input: event.data?.input,
4604
- runId: event.run_id,
4605
- metadata: event.metadata
4624
+ runId: event.run_id
4606
4625
  });
4607
4626
  return;
4608
4627
  }
@@ -4610,7 +4629,13 @@ var EventProcessor = class {
4610
4629
  const channel = event.metadata?.stream_channel ?? "text" /* TEXT */;
4611
4630
  const state = acc.channels.get(channel);
4612
4631
  if (!state) return;
4613
- const toolBlock = state.pendingToolBlocks.shift();
4632
+ let toolBlock;
4633
+ if (event.run_id && state.toolBlocksByRunId.has(event.run_id)) {
4634
+ toolBlock = state.toolBlocksByRunId.get(event.run_id);
4635
+ state.toolBlocksByRunId.delete(event.run_id);
4636
+ } else {
4637
+ toolBlock = state.pendingToolBlocks.shift();
4638
+ }
4614
4639
  if (toolBlock && toolBlock.type === "tool_use") {
4615
4640
  const output = event.data?.output;
4616
4641
  const outputString = typeof output === "string" ? output : JSON.stringify(output, null, 2);
@@ -4624,26 +4649,26 @@ var EventProcessor = class {
4624
4649
  },
4625
4650
  onPartial
4626
4651
  );
4627
- this.logger.log("\u2705 Tool execution completed", {
4652
+ this.logger.log("\u2705 Tool completed", {
4628
4653
  toolName: event.name,
4629
4654
  toolBlockId: toolBlock.id,
4630
- outputPreview: outputString.substring(0, 200) + (outputString.length > 200 ? "..." : ""),
4631
4655
  runId: event.run_id
4632
4656
  });
4633
4657
  } else {
4634
- this.logger.warn(
4635
- "\u26A0\uFE0F on_tool_end received but no pending tool block found",
4636
- {
4637
- toolName: event.name,
4638
- runId: event.run_id,
4639
- pendingCount: state.pendingToolBlocks.length
4640
- }
4641
- );
4658
+ this.logger.warn("\u26A0\uFE0F on_tool_end: no matching tool block", {
4659
+ toolName: event.name,
4660
+ runId: event.run_id
4661
+ });
4642
4662
  }
4643
4663
  return;
4644
4664
  }
4645
4665
  if (event.event === "on_tool_error") {
4646
- this.logger.error("\u274C Tool execution failed", {
4666
+ const channel = event.metadata?.stream_channel ?? "text" /* TEXT */;
4667
+ const state = acc.channels.get(channel);
4668
+ if (state && event.run_id) {
4669
+ state.toolBlocksByRunId.delete(event.run_id);
4670
+ }
4671
+ this.logger.error("\u274C Tool failed", {
4647
4672
  toolName: event.name,
4648
4673
  error: event.data?.error,
4649
4674
  runId: event.run_id
@@ -4691,6 +4716,13 @@ var EventProcessor = class {
4691
4716
  getResult(acc) {
4692
4717
  const allChains = [];
4693
4718
  for (const [channel, state] of acc.channels.entries()) {
4719
+ if (state.pendingToolBlocks.length > 0 || state.toolBlocksByRunId.size > 0) {
4720
+ this.logger.warn("\u26A0\uFE0F Orphaned tool blocks detected at finalization", {
4721
+ channel,
4722
+ pendingCount: state.pendingToolBlocks.length,
4723
+ mappedCount: state.toolBlocksByRunId.size
4724
+ });
4725
+ }
4694
4726
  if (state.currentBlock) {
4695
4727
  state.contentChain.push(state.currentBlock);
4696
4728
  }