@copilotkit/core 1.63.3-canary.rc-1 → 1.64.0

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
@@ -2000,22 +2000,35 @@ var RunHandler = class {
2000
2000
  const agentId = agent.agentId;
2001
2001
  let needsFollowUp = false;
2002
2002
  if (executeFrontendTools) {
2003
- for (const message of newMessages) if (message.role === "assistant") {
2004
- for (const toolCall of message.toolCalls || []) if (newMessages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id) === -1) {
2005
- const tool = this.getTool({
2006
- toolName: toolCall.function.name,
2003
+ for (const message of newMessages) if (message.role === "assistant") for (const toolCall of message.toolCalls || []) {
2004
+ const tool = this.getTool({
2005
+ toolName: toolCall.function.name,
2006
+ agentId: agent.agentId
2007
+ });
2008
+ let wildcardTool;
2009
+ const getWildcardTool = () => {
2010
+ if (tool || wildcardTool) return wildcardTool;
2011
+ wildcardTool = this.getTool({
2012
+ toolName: "*",
2007
2013
  agentId: agent.agentId
2008
2014
  });
2009
- if (tool) {
2010
- if (await this.executeSpecificTool(tool, toolCall, message, agent, agentId)) needsFollowUp = true;
2011
- } else {
2012
- const wildcardTool = this.getTool({
2013
- toolName: "*",
2014
- agentId: agent.agentId
2015
- });
2016
- if (wildcardTool) {
2017
- if (await this.executeWildcardTool(wildcardTool, toolCall, message, agent, agentId)) needsFollowUp = true;
2018
- }
2015
+ return wildcardTool;
2016
+ };
2017
+ let existingResultIndex = newMessages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
2018
+ const existingResult = existingResultIndex === -1 ? void 0 : newMessages[existingResultIndex];
2019
+ const executableTool = tool ?? getWildcardTool();
2020
+ if (existingResult && executableTool?.handler && this.isFrontendPlaceholderResult(existingResult)) {
2021
+ newMessages.splice(existingResultIndex, 1);
2022
+ existingResultIndex = -1;
2023
+ const agentMsgIdx = agent.messages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
2024
+ if (agentMsgIdx !== -1) agent.messages.splice(agentMsgIdx, 1);
2025
+ }
2026
+ if (existingResultIndex === -1) if (tool) {
2027
+ if (await this.executeSpecificTool(tool, toolCall, message, agent, agentId)) needsFollowUp = true;
2028
+ } else {
2029
+ const fallbackTool = getWildcardTool();
2030
+ if (fallbackTool) {
2031
+ if (await this.executeWildcardTool(fallbackTool, toolCall, message, agent, agentId)) needsFollowUp = true;
2019
2032
  }
2020
2033
  }
2021
2034
  }
@@ -2028,6 +2041,23 @@ var RunHandler = class {
2028
2041
  this._internal.suggestionEngine.reloadSuggestions(agentId);
2029
2042
  return runAgentResult;
2030
2043
  }
2044
+ isFrontendPlaceholderResult(message) {
2045
+ if (message.role !== "tool") return false;
2046
+ return this.normalizeToolResultContent(message.content) === "Forwarded to client";
2047
+ }
2048
+ normalizeToolResultContent(content) {
2049
+ if (typeof content === "string") return content.trim();
2050
+ if (Array.isArray(content)) {
2051
+ const text = content.flatMap((part) => {
2052
+ if (typeof part === "string") return [part];
2053
+ if (part && typeof part === "object" && "text" in part && typeof part.text === "string") return [part.text];
2054
+ return [];
2055
+ }).join("").trim();
2056
+ return text.length > 0 ? text : null;
2057
+ }
2058
+ if (content && typeof content === "object" && "text" in content && typeof content.text === "string") return content.text.trim();
2059
+ return null;
2060
+ }
2031
2061
  /**
2032
2062
  * Shared handler execution logic used by executeSpecificTool, executeWildcardTool, and runTool.
2033
2063
  * Handles arg parsing, subscriber notifications, handler invocation, result stringification,
@@ -2126,13 +2156,15 @@ var RunHandler = class {
2126
2156
  {
2127
2157
  const messageIndex = agent.messages.findIndex((m) => m.id === message.id);
2128
2158
  if (messageIndex === -1) return false;
2159
+ let insertAt = messageIndex + 1;
2160
+ while (insertAt < agent.messages.length && agent.messages[insertAt]?.role === "tool") insertAt++;
2129
2161
  const toolMessage = {
2130
2162
  id: (0, _copilotkit_shared.randomUUID)(),
2131
2163
  role: "tool",
2132
2164
  toolCallId: toolCall.id,
2133
2165
  content: handlerResult.result
2134
2166
  };
2135
- agent.messages.splice(messageIndex + 1, 0, toolMessage);
2167
+ agent.messages.splice(insertAt, 0, toolMessage);
2136
2168
  if (!handlerResult.error && tool?.followUp !== false) return true;
2137
2169
  }
2138
2170
  return false;
@@ -2215,13 +2247,15 @@ var RunHandler = class {
2215
2247
  {
2216
2248
  const messageIndex = agent.messages.findIndex((m) => m.id === message.id);
2217
2249
  if (messageIndex === -1) return false;
2250
+ let insertAt = messageIndex + 1;
2251
+ while (insertAt < agent.messages.length && agent.messages[insertAt]?.role === "tool") insertAt++;
2218
2252
  const toolMessage = {
2219
2253
  id: (0, _copilotkit_shared.randomUUID)(),
2220
2254
  role: "tool",
2221
2255
  toolCallId: toolCall.id,
2222
2256
  content: toolCallResult
2223
2257
  };
2224
- agent.messages.splice(messageIndex + 1, 0, toolMessage);
2258
+ agent.messages.splice(insertAt, 0, toolMessage);
2225
2259
  if (!errorMessage && wildcardTool?.followUp !== false) return true;
2226
2260
  }
2227
2261
  return false;