@flutchai/flutch-sdk 0.1.19 → 0.1.22

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
@@ -4448,8 +4448,14 @@ exports.EventProcessor = class EventProcessor {
4448
4448
  createAccumulator() {
4449
4449
  return {
4450
4450
  channels: /* @__PURE__ */ new Map([
4451
- ["text" /* TEXT */, { contentChain: [], currentBlock: null }],
4452
- ["processing" /* PROCESSING */, { contentChain: [], currentBlock: null }]
4451
+ [
4452
+ "text" /* TEXT */,
4453
+ { contentChain: [], currentBlock: null, pendingToolBlocks: [] }
4454
+ ],
4455
+ [
4456
+ "processing" /* PROCESSING */,
4457
+ { contentChain: [], currentBlock: null, pendingToolBlocks: [] }
4458
+ ]
4453
4459
  ]),
4454
4460
  attachments: [],
4455
4461
  metadata: {},
@@ -4488,6 +4494,11 @@ exports.EventProcessor = class EventProcessor {
4488
4494
  */
4489
4495
  sendDelta(channel, delta, onPartial) {
4490
4496
  if (!onPartial) return;
4497
+ if (delta.type === "step_started" || delta.type === "tool_output_chunk" || delta.type === "tool_input_chunk") {
4498
+ this.logger.debug(
4499
+ `[DELTA] type=${delta.type} channel=${channel} stepId=${delta.stepId || delta.step?.id} name=${delta.step?.name || "N/A"}`
4500
+ );
4501
+ }
4491
4502
  onPartial(
4492
4503
  JSON.stringify({
4493
4504
  channel,
@@ -4513,6 +4524,7 @@ exports.EventProcessor = class EventProcessor {
4513
4524
  input: block.input || "",
4514
4525
  output: ""
4515
4526
  };
4527
+ state.pendingToolBlocks.push(state.currentBlock);
4516
4528
  this.sendDelta(
4517
4529
  channel,
4518
4530
  {
@@ -4631,24 +4643,39 @@ exports.EventProcessor = class EventProcessor {
4631
4643
  if (event.event === "on_tool_end") {
4632
4644
  const channel = event.metadata?.stream_channel ?? "text" /* TEXT */;
4633
4645
  const state = acc.channels.get(channel);
4634
- if (state?.currentBlock && state.currentBlock.type === "tool_use") {
4646
+ if (!state) return;
4647
+ this.logger.debug(
4648
+ `[on_tool_end] channel=${channel} pendingCount=${state.pendingToolBlocks.length} pendingIds=${state.pendingToolBlocks.map((b) => b.id).join(",")} currentBlock=${state.currentBlock?.id}`
4649
+ );
4650
+ const toolBlock = state.pendingToolBlocks.shift();
4651
+ if (toolBlock && toolBlock.type === "tool_use") {
4635
4652
  const output = event.data?.output;
4636
4653
  const outputString = typeof output === "string" ? output : JSON.stringify(output, null, 2);
4637
- state.currentBlock.output = outputString;
4654
+ toolBlock.output = outputString;
4638
4655
  this.sendDelta(
4639
4656
  channel,
4640
4657
  {
4641
4658
  type: "tool_output_chunk",
4642
- stepId: state.currentBlock.id,
4659
+ stepId: toolBlock.id,
4643
4660
  chunk: outputString
4644
4661
  },
4645
4662
  onPartial
4646
4663
  );
4647
4664
  this.logger.log("\u2705 Tool execution completed", {
4648
4665
  toolName: event.name,
4666
+ toolBlockId: toolBlock.id,
4649
4667
  outputPreview: outputString.substring(0, 200) + (outputString.length > 200 ? "..." : ""),
4650
4668
  runId: event.run_id
4651
4669
  });
4670
+ } else {
4671
+ this.logger.warn(
4672
+ "\u26A0\uFE0F on_tool_end received but no pending tool block found",
4673
+ {
4674
+ toolName: event.name,
4675
+ runId: event.run_id,
4676
+ pendingCount: state.pendingToolBlocks.length
4677
+ }
4678
+ );
4652
4679
  }
4653
4680
  return;
4654
4681
  }
@@ -5513,12 +5540,22 @@ ${paramDescriptions.join("\n")}`;
5513
5540
  name: mcpTool.name,
5514
5541
  description: enhancedDescription,
5515
5542
  schema,
5516
- func: async (input) => {
5543
+ func: async (input, _runManager, config) => {
5517
5544
  logger2.log(`\u{1F527} [${mcpTool.name}] LLM INPUT: ${JSON.stringify(input)}`);
5545
+ const configurable = config?.configurable;
5546
+ const context = {
5547
+ agentId: configurable?.agentId,
5548
+ userId: configurable?.userId,
5549
+ threadId: configurable?.thread_id
5550
+ };
5551
+ logger2.debug(
5552
+ `\u{1F527} [${mcpTool.name}] Execution context: ${JSON.stringify(context)}`
5553
+ );
5518
5554
  try {
5519
5555
  const request = {
5520
5556
  name: mcpTool.name,
5521
- arguments: input ?? {}
5557
+ arguments: input ?? {},
5558
+ context
5522
5559
  };
5523
5560
  logger2.log(`\u{1F527} [${mcpTool.name}] Calling MCP Runtime...`);
5524
5561
  const response = await axios2__default.default.post(
@@ -5639,13 +5676,14 @@ var McpToolFilter = class _McpToolFilter {
5639
5676
  );
5640
5677
  const mcpClient = {
5641
5678
  getTools: async () => dynamicTools,
5642
- executeTool: async (name, args) => {
5679
+ executeTool: async (name, args, context) => {
5643
5680
  this.logger.debug(`[DEBUG] Executing tool ${name} with args:`, args);
5644
5681
  const response2 = await axios2__default.default.post(
5645
5682
  `${this.mcpRuntimeUrl}/tools/execute`,
5646
5683
  {
5647
5684
  name,
5648
- arguments: args || {}
5685
+ arguments: args || {},
5686
+ context
5649
5687
  }
5650
5688
  );
5651
5689
  return response2.data;
@@ -5688,12 +5726,13 @@ var McpToolFilter = class _McpToolFilter {
5688
5726
  this.logger.debug(`Retrieved ${allTools.length} total MCP tools`);
5689
5727
  const mcpClient = {
5690
5728
  getTools: async () => allTools,
5691
- executeTool: async (name, args) => {
5729
+ executeTool: async (name, args, context) => {
5692
5730
  const response2 = await axios2__default.default.post(
5693
5731
  `${this.mcpRuntimeUrl}/tools/execute`,
5694
5732
  {
5695
5733
  name,
5696
- arguments: args || {}
5734
+ arguments: args || {},
5735
+ context
5697
5736
  }
5698
5737
  );
5699
5738
  return response2.data;