@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.cjs CHANGED
@@ -4450,11 +4450,21 @@ exports.EventProcessor = class EventProcessor {
4450
4450
  channels: /* @__PURE__ */ new Map([
4451
4451
  [
4452
4452
  "text" /* TEXT */,
4453
- { contentChain: [], currentBlock: null, pendingToolBlocks: [] }
4453
+ {
4454
+ contentChain: [],
4455
+ currentBlock: null,
4456
+ pendingToolBlocks: [],
4457
+ toolBlocksByRunId: /* @__PURE__ */ new Map()
4458
+ }
4454
4459
  ],
4455
4460
  [
4456
4461
  "processing" /* PROCESSING */,
4457
- { contentChain: [], currentBlock: null, pendingToolBlocks: [] }
4462
+ {
4463
+ contentChain: [],
4464
+ currentBlock: null,
4465
+ pendingToolBlocks: [],
4466
+ toolBlocksByRunId: /* @__PURE__ */ new Map()
4467
+ }
4458
4468
  ]
4459
4469
  ]),
4460
4470
  attachments: [],
@@ -4627,11 +4637,20 @@ exports.EventProcessor = class EventProcessor {
4627
4637
  return;
4628
4638
  }
4629
4639
  if (event.event === "on_tool_start") {
4640
+ const channel = event.metadata?.stream_channel ?? "text" /* TEXT */;
4641
+ const state = acc.channels.get(channel);
4642
+ if (state && event.run_id) {
4643
+ const idx = state.pendingToolBlocks.findIndex(
4644
+ (b) => b.name === event.name
4645
+ );
4646
+ if (idx !== -1) {
4647
+ const block = state.pendingToolBlocks.splice(idx, 1)[0];
4648
+ state.toolBlocksByRunId.set(event.run_id, block);
4649
+ }
4650
+ }
4630
4651
  this.logger.log("\u{1F527} Tool execution started", {
4631
4652
  toolName: event.name,
4632
- input: event.data?.input,
4633
- runId: event.run_id,
4634
- metadata: event.metadata
4653
+ runId: event.run_id
4635
4654
  });
4636
4655
  return;
4637
4656
  }
@@ -4639,7 +4658,13 @@ exports.EventProcessor = class EventProcessor {
4639
4658
  const channel = event.metadata?.stream_channel ?? "text" /* TEXT */;
4640
4659
  const state = acc.channels.get(channel);
4641
4660
  if (!state) return;
4642
- const toolBlock = state.pendingToolBlocks.shift();
4661
+ let toolBlock;
4662
+ if (event.run_id && state.toolBlocksByRunId.has(event.run_id)) {
4663
+ toolBlock = state.toolBlocksByRunId.get(event.run_id);
4664
+ state.toolBlocksByRunId.delete(event.run_id);
4665
+ } else {
4666
+ toolBlock = state.pendingToolBlocks.shift();
4667
+ }
4643
4668
  if (toolBlock && toolBlock.type === "tool_use") {
4644
4669
  const output = event.data?.output;
4645
4670
  const outputString = typeof output === "string" ? output : JSON.stringify(output, null, 2);
@@ -4653,26 +4678,26 @@ exports.EventProcessor = class EventProcessor {
4653
4678
  },
4654
4679
  onPartial
4655
4680
  );
4656
- this.logger.log("\u2705 Tool execution completed", {
4681
+ this.logger.log("\u2705 Tool completed", {
4657
4682
  toolName: event.name,
4658
4683
  toolBlockId: toolBlock.id,
4659
- outputPreview: outputString.substring(0, 200) + (outputString.length > 200 ? "..." : ""),
4660
4684
  runId: event.run_id
4661
4685
  });
4662
4686
  } else {
4663
- this.logger.warn(
4664
- "\u26A0\uFE0F on_tool_end received but no pending tool block found",
4665
- {
4666
- toolName: event.name,
4667
- runId: event.run_id,
4668
- pendingCount: state.pendingToolBlocks.length
4669
- }
4670
- );
4687
+ this.logger.warn("\u26A0\uFE0F on_tool_end: no matching tool block", {
4688
+ toolName: event.name,
4689
+ runId: event.run_id
4690
+ });
4671
4691
  }
4672
4692
  return;
4673
4693
  }
4674
4694
  if (event.event === "on_tool_error") {
4675
- this.logger.error("\u274C Tool execution failed", {
4695
+ const channel = event.metadata?.stream_channel ?? "text" /* TEXT */;
4696
+ const state = acc.channels.get(channel);
4697
+ if (state && event.run_id) {
4698
+ state.toolBlocksByRunId.delete(event.run_id);
4699
+ }
4700
+ this.logger.error("\u274C Tool failed", {
4676
4701
  toolName: event.name,
4677
4702
  error: event.data?.error,
4678
4703
  runId: event.run_id
@@ -4720,6 +4745,13 @@ exports.EventProcessor = class EventProcessor {
4720
4745
  getResult(acc) {
4721
4746
  const allChains = [];
4722
4747
  for (const [channel, state] of acc.channels.entries()) {
4748
+ if (state.pendingToolBlocks.length > 0 || state.toolBlocksByRunId.size > 0) {
4749
+ this.logger.warn("\u26A0\uFE0F Orphaned tool blocks detected at finalization", {
4750
+ channel,
4751
+ pendingCount: state.pendingToolBlocks.length,
4752
+ mappedCount: state.toolBlocksByRunId.size
4753
+ });
4754
+ }
4723
4755
  if (state.currentBlock) {
4724
4756
  state.contentChain.push(state.currentBlock);
4725
4757
  }