@botdojo/chat-sdk 0.0.12 → 0.0.13

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/lib/index.js CHANGED
@@ -2518,40 +2518,40 @@ var require_BotDojoConnector = __commonJS({
2518
2518
  const displayName = ((_c = tool2._meta) === null || _c === void 0 ? void 0 : _c["botdojo/display-name"]) || toolName;
2519
2519
  this.connection.callbacks.set(toolName, {
2520
2520
  source: this.connection.sender,
2521
- func: async (...args) => {
2522
- var _a2;
2523
- const firstArg = args[0];
2524
- const argCanvasId = (firstArg === null || firstArg === void 0 ? void 0 : firstArg.__canvasId) || (firstArg === null || firstArg === void 0 ? void 0 : firstArg.canvasId) || (firstArg === null || firstArg === void 0 ? void 0 : firstArg.canvas_id);
2525
- const argStepId = (_a2 = firstArg === null || firstArg === void 0 ? void 0 : firstArg.__stepId) !== null && _a2 !== void 0 ? _a2 : argCanvasId;
2526
- const fallbackCanvasId = this.defaultCanvasId || this.getCanvasIdFromUrl() || this.generateChannelId();
2521
+ // Callback receives (toolArgs, toolContext) where toolContext = { stepId, canvasId }
2522
+ func: async (toolArgs, toolContext) => {
2523
+ const stepId = toolContext === null || toolContext === void 0 ? void 0 : toolContext.stepId;
2524
+ const canvasId = toolContext === null || toolContext === void 0 ? void 0 : toolContext.canvasId;
2525
+ if (!stepId) {
2526
+ console.warn(`[BotDojoConnector] Tool ${toolName} called without stepId in toolContext`);
2527
+ }
2527
2528
  if (hasResourceTemplate) {
2528
- const canvasId = argCanvasId || fallbackCanvasId;
2529
- const stepId = argStepId !== null && argStepId !== void 0 ? argStepId : canvasId;
2530
2529
  const baseToolInfo2 = {
2531
2530
  toolName,
2532
2531
  toolLabel: displayName,
2533
- arguments: firstArg
2532
+ arguments: toolArgs
2534
2533
  };
2535
- if (!canvasId && !stepId) {
2536
- console.warn(`[BotDojoConnector] Tool has resource template but no __canvasId/__stepId found in arguments`, firstArg);
2534
+ if (!canvasId) {
2535
+ console.warn(`[BotDojoConnector] Canvas tool ${toolName} called without canvasId in toolContext`);
2537
2536
  }
2538
2537
  const canvasType = "mcp-app";
2539
- const context2 = {
2538
+ const toolExecContext2 = {
2540
2539
  toolName,
2540
+ stepId,
2541
2541
  canvasId,
2542
2542
  updateState: async (state) => {
2543
2543
  if (canvasId || stepId) {
2544
2544
  await this.sendCanvasUpdate(stepId, canvasId, state, canvasType, baseToolInfo2);
2545
2545
  }
2546
2546
  },
2547
- notifyToolInputPartial: async (args2) => {
2547
+ notifyToolInputPartial: async (args) => {
2548
2548
  if (canvasId || stepId) {
2549
- await this.sendCanvasUpdate(stepId, canvasId, void 0, canvasType, Object.assign(Object.assign({}, baseToolInfo2), { arguments: args2 }));
2549
+ await this.sendCanvasUpdate(stepId, canvasId, void 0, canvasType, Object.assign(Object.assign({}, baseToolInfo2), { arguments: args }));
2550
2550
  }
2551
2551
  },
2552
- notifyToolInput: async (args2) => {
2552
+ notifyToolInput: async (args) => {
2553
2553
  if (canvasId || stepId) {
2554
- await this.sendCanvasUpdate(stepId, canvasId, void 0, canvasType, Object.assign(Object.assign({}, baseToolInfo2), { arguments: args2 }));
2554
+ await this.sendCanvasUpdate(stepId, canvasId, void 0, canvasType, Object.assign(Object.assign({}, baseToolInfo2), { arguments: args }));
2555
2555
  }
2556
2556
  },
2557
2557
  notifyToolResult: async (result) => {
@@ -2560,35 +2560,34 @@ var require_BotDojoConnector = __commonJS({
2560
2560
  }
2561
2561
  }
2562
2562
  };
2563
- const rawresult = await tool2.execute(...args, context2);
2564
- await context2.notifyToolResult(rawresult);
2563
+ const rawresult = await tool2.execute(toolArgs, toolExecContext2);
2564
+ await toolExecContext2.notifyToolResult(rawresult);
2565
2565
  return await (0, sdk_types_1.mapToolResponseToBackend)(rawresult);
2566
2566
  }
2567
- const defaultCanvasId = argCanvasId || fallbackCanvasId;
2568
- const defaultStepId = argStepId !== null && argStepId !== void 0 ? argStepId : defaultCanvasId;
2569
2567
  const baseToolInfo = {
2570
2568
  toolName,
2571
2569
  toolLabel: displayName,
2572
- arguments: firstArg
2570
+ arguments: toolArgs
2573
2571
  };
2574
- const context = {
2572
+ const toolExecContext = {
2575
2573
  toolName,
2576
- canvasId: defaultCanvasId,
2574
+ stepId,
2575
+ canvasId,
2577
2576
  updateState: async (state) => {
2578
- await this.sendCanvasUpdate(defaultStepId, defaultCanvasId, state, void 0, baseToolInfo);
2577
+ await this.sendCanvasUpdate(stepId, canvasId, state, void 0, baseToolInfo);
2579
2578
  },
2580
- notifyToolInputPartial: async (args2) => {
2581
- await this.sendCanvasUpdate(defaultStepId, defaultCanvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { arguments: args2 }));
2579
+ notifyToolInputPartial: async (args) => {
2580
+ await this.sendCanvasUpdate(stepId, canvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { arguments: args }));
2582
2581
  },
2583
- notifyToolInput: async (args2) => {
2584
- await this.sendCanvasUpdate(defaultStepId, defaultCanvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { arguments: args2 }));
2582
+ notifyToolInput: async (args) => {
2583
+ await this.sendCanvasUpdate(stepId, canvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { arguments: args }));
2585
2584
  },
2586
2585
  notifyToolResult: async (result) => {
2587
- await this.sendCanvasUpdate(defaultStepId, defaultCanvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { result }));
2586
+ await this.sendCanvasUpdate(stepId, canvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { result }));
2588
2587
  }
2589
2588
  };
2590
- const rawResult = await tool2.execute(...args, context);
2591
- await context.notifyToolResult(rawResult);
2589
+ const rawResult = await tool2.execute(toolArgs, toolExecContext);
2590
+ await toolExecContext.notifyToolResult(rawResult);
2592
2591
  return await (0, sdk_types_1.mapToolResponseToBackend)(rawResult);
2593
2592
  }
2594
2593
  });
@@ -2910,29 +2909,29 @@ var require_BotDojoConnector = __commonJS({
2910
2909
  console.log(`\u{1F527} Registering modelContext tool on host client: ${toolName}, hasResource: ${hasResourceTemplate}`);
2911
2910
  this._hostClient.callbacks.set(toolName, {
2912
2911
  source: this._hostClient.sender,
2913
- func: async (...args) => {
2914
- var _a2;
2915
- console.log(`\u{1F527} Executing modelContext tool: ${toolName} with args:`, args);
2912
+ // Callback receives (toolArgs, toolContext) where toolContext = { stepId, canvasId }
2913
+ func: async (toolArgs, toolContext) => {
2914
+ console.log(`\u{1F527} Executing modelContext tool: ${toolName}`, { toolArgs, toolContext });
2916
2915
  try {
2917
- const firstArg = args[0];
2918
- const argCanvasId = (firstArg === null || firstArg === void 0 ? void 0 : firstArg.__canvasId) || (firstArg === null || firstArg === void 0 ? void 0 : firstArg.canvasId) || (firstArg === null || firstArg === void 0 ? void 0 : firstArg.canvas_id);
2919
- const argStepId = (_a2 = firstArg === null || firstArg === void 0 ? void 0 : firstArg.__stepId) !== null && _a2 !== void 0 ? _a2 : argCanvasId;
2920
- const fallbackCanvasId = this.defaultCanvasId || this.getCanvasIdFromUrl() || this.generateChannelId();
2916
+ const stepId = toolContext === null || toolContext === void 0 ? void 0 : toolContext.stepId;
2917
+ const canvasId = toolContext === null || toolContext === void 0 ? void 0 : toolContext.canvasId;
2918
+ if (!stepId) {
2919
+ console.warn(`[BotDojoConnector hostClient] Tool ${toolName} called without stepId in toolContext`);
2920
+ }
2921
2921
  if (hasResourceTemplate) {
2922
- const canvasId = argCanvasId || fallbackCanvasId;
2923
- const stepId = argStepId !== null && argStepId !== void 0 ? argStepId : canvasId;
2924
2922
  const baseToolInfo2 = {
2925
2923
  toolName,
2926
2924
  toolLabel: displayName,
2927
- arguments: firstArg
2925
+ arguments: toolArgs
2928
2926
  };
2929
- if (!canvasId && !stepId) {
2930
- console.warn(`[BotDojoConnector hostClient] Tool has resource template but no __canvasId/__stepId found in arguments`, firstArg);
2927
+ if (!canvasId) {
2928
+ console.warn(`[BotDojoConnector hostClient] Canvas tool ${toolName} called without canvasId in toolContext`);
2931
2929
  }
2932
2930
  console.log(`[BotDojoConnector hostClient] Tool has resource, ids \u2192 step: ${stepId}, canvas: ${canvasId}`);
2933
2931
  const canvasType = "mcp-app";
2934
- const context2 = {
2932
+ const toolExecContext2 = {
2935
2933
  toolName,
2934
+ stepId,
2936
2935
  canvasId,
2937
2936
  updateState: async (state) => {
2938
2937
  console.log(`[BotDojoConnector hostClient] updateState called for canvasId: ${canvasId !== null && canvasId !== void 0 ? canvasId : stepId}`, state);
@@ -2940,14 +2939,14 @@ var require_BotDojoConnector = __commonJS({
2940
2939
  await this.sendCanvasUpdate(stepId, canvasId, state, canvasType, baseToolInfo2);
2941
2940
  }
2942
2941
  },
2943
- notifyToolInputPartial: async (args2) => {
2942
+ notifyToolInputPartial: async (args) => {
2944
2943
  if (canvasId || stepId) {
2945
- await this.sendCanvasUpdate(stepId, canvasId, void 0, canvasType, Object.assign(Object.assign({}, baseToolInfo2), { arguments: args2 }));
2944
+ await this.sendCanvasUpdate(stepId, canvasId, void 0, canvasType, Object.assign(Object.assign({}, baseToolInfo2), { arguments: args }));
2946
2945
  }
2947
2946
  },
2948
- notifyToolInput: async (args2) => {
2947
+ notifyToolInput: async (args) => {
2949
2948
  if (canvasId || stepId) {
2950
- await this.sendCanvasUpdate(stepId, canvasId, void 0, canvasType, Object.assign(Object.assign({}, baseToolInfo2), { arguments: args2 }));
2949
+ await this.sendCanvasUpdate(stepId, canvasId, void 0, canvasType, Object.assign(Object.assign({}, baseToolInfo2), { arguments: args }));
2951
2950
  }
2952
2951
  },
2953
2952
  notifyToolResult: async (result3) => {
@@ -2956,37 +2955,36 @@ var require_BotDojoConnector = __commonJS({
2956
2955
  }
2957
2956
  }
2958
2957
  };
2959
- const rawresult2 = await tool2.execute(...args, context2);
2960
- await context2.notifyToolResult(rawresult2);
2958
+ const rawresult2 = await tool2.execute(toolArgs, toolExecContext2);
2959
+ await toolExecContext2.notifyToolResult(rawresult2);
2961
2960
  const result2 = await (0, sdk_types_1.mapToolResponseToBackend)(rawresult2);
2962
2961
  console.log(`\u2705 Tool executed successfully: ${toolName}`);
2963
2962
  return result2;
2964
2963
  }
2965
- const defaultCanvasId = argCanvasId || fallbackCanvasId;
2966
- const defaultStepId = argStepId !== null && argStepId !== void 0 ? argStepId : defaultCanvasId;
2967
2964
  const baseToolInfo = {
2968
2965
  toolName,
2969
2966
  toolLabel: displayName,
2970
- arguments: firstArg
2967
+ arguments: toolArgs
2971
2968
  };
2972
- const context = {
2969
+ const toolExecContext = {
2973
2970
  toolName,
2974
- canvasId: defaultCanvasId,
2971
+ stepId,
2972
+ canvasId,
2975
2973
  updateState: async (state) => {
2976
- await this.sendCanvasUpdate(defaultStepId, defaultCanvasId, state, void 0, baseToolInfo);
2974
+ await this.sendCanvasUpdate(stepId, canvasId, state, void 0, baseToolInfo);
2977
2975
  },
2978
- notifyToolInputPartial: async (args2) => {
2979
- await this.sendCanvasUpdate(defaultStepId, defaultCanvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { arguments: args2 }));
2976
+ notifyToolInputPartial: async (args) => {
2977
+ await this.sendCanvasUpdate(stepId, canvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { arguments: args }));
2980
2978
  },
2981
- notifyToolInput: async (args2) => {
2982
- await this.sendCanvasUpdate(defaultStepId, defaultCanvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { arguments: args2 }));
2979
+ notifyToolInput: async (args) => {
2980
+ await this.sendCanvasUpdate(stepId, canvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { arguments: args }));
2983
2981
  },
2984
2982
  notifyToolResult: async (result2) => {
2985
- await this.sendCanvasUpdate(defaultStepId, defaultCanvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { result: result2 }));
2983
+ await this.sendCanvasUpdate(stepId, canvasId, void 0, void 0, Object.assign(Object.assign({}, baseToolInfo), { result: result2 }));
2986
2984
  }
2987
2985
  };
2988
- const rawresult = await tool2.execute(...args, context);
2989
- await context.notifyToolResult(rawresult);
2986
+ const rawresult = await tool2.execute(toolArgs, toolExecContext);
2987
+ await toolExecContext.notifyToolResult(rawresult);
2990
2988
  const result = await (0, sdk_types_1.mapToolResponseToBackend)(rawresult);
2991
2989
  console.log(`\u2705 Tool executed successfully: ${toolName}`);
2992
2990
  return result;
@@ -3169,45 +3167,28 @@ var require_BotDojoConnector = __commonJS({
3169
3167
  * @param stepId - Optional step ID for canvas updates (if tool has canvas template)
3170
3168
  */
3171
3169
  async executeToolCall(toolName, ...args) {
3172
- var _a, _b, _c;
3173
- let stepId;
3174
- let canvasId;
3175
- let toolArgs = args;
3176
- if (args.length > 0 && typeof args[args.length - 1] === "object" && (((_a = args[args.length - 1]) === null || _a === void 0 ? void 0 : _a.__stepId) || ((_b = args[args.length - 1]) === null || _b === void 0 ? void 0 : _b.__canvasId))) {
3177
- const options = args[args.length - 1];
3178
- stepId = options.__stepId;
3179
- canvasId = options.__canvasId;
3180
- toolArgs = args.slice(0, -1);
3181
- }
3182
- const firstArg = toolArgs[0];
3183
- if (typeof firstArg === "object") {
3184
- canvasId = canvasId !== null && canvasId !== void 0 ? canvasId : firstArg === null || firstArg === void 0 ? void 0 : firstArg.__canvasId;
3185
- stepId = (_c = stepId !== null && stepId !== void 0 ? stepId : firstArg === null || firstArg === void 0 ? void 0 : firstArg.__stepId) !== null && _c !== void 0 ? _c : canvasId;
3186
- }
3187
3170
  console.log("[BotDojoConnector] executeToolCall", {
3188
3171
  transport: this.transport,
3189
3172
  hasConnection: !!this.connection,
3190
3173
  hasHostClient: !!this._hostClient,
3191
3174
  hasInit: this.hasInit,
3192
- stepId,
3193
- canvasId,
3194
3175
  toolName,
3195
- args: toolArgs
3176
+ args
3196
3177
  });
3197
3178
  if (this.transport === "postmessage" && this.connection) {
3198
3179
  const currentOrigin = typeof window !== "undefined" ? window.location.origin : "unknown";
3199
3180
  console.log(`[BotDojoConnector] [CORS DEBUG] Executing tool via PostMessage RPCConnection: ${toolName}`, {
3200
3181
  toolName,
3201
3182
  currentOrigin,
3202
- toolArgs,
3183
+ args,
3203
3184
  hasConnection: !!this.connection,
3204
3185
  connectorClientId: this.clientId
3205
3186
  });
3206
- return await this.connection.sendRequest("agent_host", toolName, toolArgs, 3e4);
3187
+ return await this.connection.sendRequest("agent_host", toolName, args, 3e4);
3207
3188
  }
3208
3189
  if (this._hostClient) {
3209
- console.log(`[BotDojoConnector] Executing tool via Socket.IO RPCConnection: ${toolName}`, toolArgs);
3210
- return await this._hostClient.sendRequest("agent_host", toolName, toolArgs, 3e4);
3190
+ console.log(`[BotDojoConnector] Executing tool via Socket.IO RPCConnection: ${toolName}`, args);
3191
+ return await this._hostClient.sendRequest("agent_host", toolName, args, 3e4);
3211
3192
  }
3212
3193
  throw new Error(`No connection available for tool call: ${toolName}. Tools must be executed remotely via ModelContext.`);
3213
3194
  }