@flutchai/flutch-sdk 0.7.13 → 0.7.15

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
@@ -5203,6 +5203,29 @@ function sanitizeTraceError(error, options) {
5203
5203
  }
5204
5204
  __name(sanitizeTraceError, "sanitizeTraceError");
5205
5205
 
5206
+ // src/tools/tool-usage.ts
5207
+ function toolUsageOf(value) {
5208
+ const cost = value?.total_cost;
5209
+ return typeof cost === "number" && Number.isFinite(cost) && cost >= 0 ? {
5210
+ total_cost: cost
5211
+ } : void 0;
5212
+ }
5213
+ __name(toolUsageOf, "toolUsageOf");
5214
+ function toolEndOutput(content, usage) {
5215
+ return usage ? {
5216
+ content,
5217
+ usage_metadata: usage
5218
+ } : content;
5219
+ }
5220
+ __name(toolEndOutput, "toolEndOutput");
5221
+ function toolEndContent(output) {
5222
+ if (!output || typeof output !== "object") return void 0;
5223
+ const o = output;
5224
+ if (typeof o.content !== "string") return void 0;
5225
+ return toolUsageOf(o.usage_metadata) || toolUsageOf(o.artifact?.usage_metadata) ? o.content : void 0;
5226
+ }
5227
+ __name(toolEndContent, "toolEndContent");
5228
+
5206
5229
  // src/engines/langgraph/stream-thinking.ts
5207
5230
  function splitThinking(blocks) {
5208
5231
  const thoughts = [];
@@ -5311,9 +5334,10 @@ exports.EventProcessor = class _EventProcessor {
5311
5334
  ].filter((b) => b.type === "tool_use");
5312
5335
  const block = (data.id ? blocks.find((b) => b.id === data.id) : void 0) ?? [
5313
5336
  ...blocks
5314
- ].reverse().find((b) => b.name === data.name && !b.label);
5337
+ ].reverse().find((b) => b.name === data.name && !b.labelled);
5315
5338
  if (!block) return;
5316
5339
  block.label = String(data.label);
5340
+ block.labelled = true;
5317
5341
  this.sendDelta(channel, {
5318
5342
  type: "tool_label",
5319
5343
  stepId: block.id,
@@ -5442,20 +5466,24 @@ exports.EventProcessor = class _EventProcessor {
5442
5466
  /**
5443
5467
  * Process content stream blocks (universal for all channels)
5444
5468
  */
5445
- processContentStream(acc, channel, blocks, onPartial) {
5469
+ processContentStream(acc, channel, blocks, onPartial, titles) {
5446
5470
  const state = acc.channels.get(channel);
5447
5471
  for (const block of blocks) {
5448
5472
  if (block.type === "tool_use" || block.type === "tool_call") {
5449
5473
  if (state.currentBlock) {
5450
5474
  state.contentChain.push(state.currentBlock);
5451
5475
  }
5476
+ const title = titles?.[String(block.name)];
5452
5477
  state.currentBlock = {
5453
5478
  index: state.contentChain.length,
5454
5479
  type: "tool_use",
5455
5480
  name: block.name,
5456
5481
  id: block.id,
5457
5482
  input: block.input || "",
5458
- output: ""
5483
+ output: "",
5484
+ ...typeof title === "string" && title ? {
5485
+ label: title
5486
+ } : {}
5459
5487
  };
5460
5488
  state.pendingToolBlocks.push(state.currentBlock);
5461
5489
  this.sendDelta(channel, {
@@ -5615,7 +5643,7 @@ exports.EventProcessor = class _EventProcessor {
5615
5643
  blocks.push(...this.toolCallChunksToBlocks(chunk.tool_call_chunks));
5616
5644
  }
5617
5645
  if (blocks.length > 0) {
5618
- this.processContentStream(acc, channel, blocks, onPartial);
5646
+ this.processContentStream(acc, channel, blocks, onPartial, event.metadata?.tool_titles);
5619
5647
  }
5620
5648
  return;
5621
5649
  }
@@ -5683,7 +5711,7 @@ exports.EventProcessor = class _EventProcessor {
5683
5711
  const output = event.data?.output;
5684
5712
  let outputString;
5685
5713
  try {
5686
- outputString = typeof output === "string" ? output : JSON.stringify(output, null, 2);
5714
+ outputString = typeof output === "string" ? output : toolEndContent(output) ?? JSON.stringify(output, null, 2);
5687
5715
  } catch {
5688
5716
  outputString = typeof output?.content === "string" ? output.content : "[Output too large to display]";
5689
5717
  }
@@ -6952,6 +6980,9 @@ ${paramDescriptions.join("\n")}`;
6952
6980
  name: mcpTool.name,
6953
6981
  description: enhancedDescription,
6954
6982
  schema,
6983
+ // The call's cost rides on the ToolMessage's artifact, which the model
6984
+ // never sees and the trace keeps (see tool-usage.ts).
6985
+ responseFormat: "content_and_artifact",
6955
6986
  func: /* @__PURE__ */ __name(async (input, _runManager, config) => {
6956
6987
  logger3.log(`\u{1F527} [${mcpTool.name}] LLM INPUT: ${JSON.stringify(input)}`);
6957
6988
  const configurable = config?.configurable;
@@ -6980,7 +7011,13 @@ ${paramDescriptions.join("\n")}`;
6980
7011
  }
6981
7012
  const output = typeof result.result === "string" ? result.result : JSON.stringify(result.result, null, 2);
6982
7013
  logger3.log(`\u{1F527} [${mcpTool.name}] TOOL OUTPUT: ${output.length > 200 ? output.substring(0, 200) + "..." : output}`);
6983
- return output;
7014
+ const usage = toolUsageOf(result.usage_metadata);
7015
+ return [
7016
+ output,
7017
+ usage ? {
7018
+ usage_metadata: usage
7019
+ } : void 0
7020
+ ];
6984
7021
  } catch (error) {
6985
7022
  logger3.error(`\u{1F527} [${mcpTool.name}] Exception during execution`, error);
6986
7023
  if (axios2__default.default.isAxiosError(error)) {
@@ -7300,12 +7337,17 @@ exports.McpRuntimeHttpClient = class _McpRuntimeHttpClient {
7300
7337
  ]
7301
7338
  }, JSON.stringify(enrichedArgs), parsedConfig.runId, void 0, parsedConfig.tags, parsedConfig.metadata, toolName);
7302
7339
  const result = await this.executeTool(toolName, enrichedArgs, executionContext, mcpServers, config?.signal);
7303
- const content = result.success ? JSON.stringify(result) : result.error || JSON.stringify(result);
7304
- await runManager?.handleToolEnd(content);
7340
+ const { usage_metadata: rawUsage, ...shown } = result;
7341
+ const usageMetadata = toolUsageOf(rawUsage);
7342
+ const content = shown.success ? JSON.stringify(shown) : shown.error || JSON.stringify(shown);
7343
+ await runManager?.handleToolEnd(toolEndOutput(content, usageMetadata));
7305
7344
  return {
7306
7345
  content,
7307
7346
  success: result.success,
7308
- rawResult: result.success ? result.result : void 0
7347
+ rawResult: result.success ? result.result : void 0,
7348
+ ...usageMetadata && {
7349
+ usageMetadata
7350
+ }
7309
7351
  };
7310
7352
  } catch (error) {
7311
7353
  this.logger.error(`Error executing tool ${toolName}:`, error);
@@ -9486,8 +9528,11 @@ exports.splitTools = splitTools;
9486
9528
  exports.storeAttachmentData = storeAttachmentData;
9487
9529
  exports.toOpenAIEffort = toOpenAIEffort;
9488
9530
  exports.toOptionalNumber = toOptionalNumber;
9531
+ exports.toolEndContent = toolEndContent;
9532
+ exports.toolEndOutput = toolEndOutput;
9489
9533
  exports.toolSearchApplies = toolSearchApplies;
9490
9534
  exports.toolSearchTerms = toolSearchTerms;
9535
+ exports.toolUsageOf = toolUsageOf;
9491
9536
  exports.traceApiCall = traceApiCall;
9492
9537
  exports.withFlutchContext = withFlutchContext;
9493
9538
  exports.withToolReferences = withToolReferences;