@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.umd.js CHANGED
@@ -2120,22 +2120,35 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2120
2120
  const agentId = agent.agentId;
2121
2121
  let needsFollowUp = false;
2122
2122
  if (executeFrontendTools) {
2123
- for (const message of newMessages) if (message.role === "assistant") {
2124
- for (const toolCall of message.toolCalls || []) if (newMessages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id) === -1) {
2125
- const tool = this.getTool({
2126
- toolName: toolCall.function.name,
2123
+ for (const message of newMessages) if (message.role === "assistant") for (const toolCall of message.toolCalls || []) {
2124
+ const tool = this.getTool({
2125
+ toolName: toolCall.function.name,
2126
+ agentId: agent.agentId
2127
+ });
2128
+ let wildcardTool;
2129
+ const getWildcardTool = () => {
2130
+ if (tool || wildcardTool) return wildcardTool;
2131
+ wildcardTool = this.getTool({
2132
+ toolName: "*",
2127
2133
  agentId: agent.agentId
2128
2134
  });
2129
- if (tool) {
2130
- if (await this.executeSpecificTool(tool, toolCall, message, agent, agentId)) needsFollowUp = true;
2131
- } else {
2132
- const wildcardTool = this.getTool({
2133
- toolName: "*",
2134
- agentId: agent.agentId
2135
- });
2136
- if (wildcardTool) {
2137
- if (await this.executeWildcardTool(wildcardTool, toolCall, message, agent, agentId)) needsFollowUp = true;
2138
- }
2135
+ return wildcardTool;
2136
+ };
2137
+ let existingResultIndex = newMessages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
2138
+ const existingResult = existingResultIndex === -1 ? void 0 : newMessages[existingResultIndex];
2139
+ const executableTool = tool !== null && tool !== void 0 ? tool : getWildcardTool();
2140
+ if (existingResult && (executableTool === null || executableTool === void 0 ? void 0 : executableTool.handler) && this.isFrontendPlaceholderResult(existingResult)) {
2141
+ newMessages.splice(existingResultIndex, 1);
2142
+ existingResultIndex = -1;
2143
+ const agentMsgIdx = agent.messages.findIndex((m) => m.role === "tool" && m.toolCallId === toolCall.id);
2144
+ if (agentMsgIdx !== -1) agent.messages.splice(agentMsgIdx, 1);
2145
+ }
2146
+ if (existingResultIndex === -1) if (tool) {
2147
+ if (await this.executeSpecificTool(tool, toolCall, message, agent, agentId)) needsFollowUp = true;
2148
+ } else {
2149
+ const fallbackTool = getWildcardTool();
2150
+ if (fallbackTool) {
2151
+ if (await this.executeWildcardTool(fallbackTool, toolCall, message, agent, agentId)) needsFollowUp = true;
2139
2152
  }
2140
2153
  }
2141
2154
  }
@@ -2148,6 +2161,23 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2148
2161
  this._internal.suggestionEngine.reloadSuggestions(agentId);
2149
2162
  return runAgentResult;
2150
2163
  }
2164
+ isFrontendPlaceholderResult(message) {
2165
+ if (message.role !== "tool") return false;
2166
+ return this.normalizeToolResultContent(message.content) === "Forwarded to client";
2167
+ }
2168
+ normalizeToolResultContent(content) {
2169
+ if (typeof content === "string") return content.trim();
2170
+ if (Array.isArray(content)) {
2171
+ const text = content.flatMap((part) => {
2172
+ if (typeof part === "string") return [part];
2173
+ if (part && typeof part === "object" && "text" in part && typeof part.text === "string") return [part.text];
2174
+ return [];
2175
+ }).join("").trim();
2176
+ return text.length > 0 ? text : null;
2177
+ }
2178
+ if (content && typeof content === "object" && "text" in content && typeof content.text === "string") return content.text.trim();
2179
+ return null;
2180
+ }
2151
2181
  /**
2152
2182
  * Shared handler execution logic used by executeSpecificTool, executeWildcardTool, and runTool.
2153
2183
  * Handles arg parsing, subscriber notifications, handler invocation, result stringification,
@@ -2251,15 +2281,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2251
2281
  messageId: message.id
2252
2282
  });
2253
2283
  {
2284
+ var _agent$messages$inser;
2254
2285
  const messageIndex = agent.messages.findIndex((m) => m.id === message.id);
2255
2286
  if (messageIndex === -1) return false;
2287
+ let insertAt = messageIndex + 1;
2288
+ while (insertAt < agent.messages.length && ((_agent$messages$inser = agent.messages[insertAt]) === null || _agent$messages$inser === void 0 ? void 0 : _agent$messages$inser.role) === "tool") insertAt++;
2256
2289
  const toolMessage = {
2257
2290
  id: (0, _copilotkit_shared.randomUUID)(),
2258
2291
  role: "tool",
2259
2292
  toolCallId: toolCall.id,
2260
2293
  content: handlerResult.result
2261
2294
  };
2262
- agent.messages.splice(messageIndex + 1, 0, toolMessage);
2295
+ agent.messages.splice(insertAt, 0, toolMessage);
2263
2296
  if (!handlerResult.error && (tool === null || tool === void 0 ? void 0 : tool.followUp) !== false) return true;
2264
2297
  }
2265
2298
  return false;
@@ -2346,15 +2379,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2346
2379
  }, "Subscriber onToolExecutionEnd error:");
2347
2380
  }
2348
2381
  {
2382
+ var _agent$messages$inser2;
2349
2383
  const messageIndex = agent.messages.findIndex((m) => m.id === message.id);
2350
2384
  if (messageIndex === -1) return false;
2385
+ let insertAt = messageIndex + 1;
2386
+ while (insertAt < agent.messages.length && ((_agent$messages$inser2 = agent.messages[insertAt]) === null || _agent$messages$inser2 === void 0 ? void 0 : _agent$messages$inser2.role) === "tool") insertAt++;
2351
2387
  const toolMessage = {
2352
2388
  id: (0, _copilotkit_shared.randomUUID)(),
2353
2389
  role: "tool",
2354
2390
  toolCallId: toolCall.id,
2355
2391
  content: toolCallResult
2356
2392
  };
2357
- agent.messages.splice(messageIndex + 1, 0, toolMessage);
2393
+ agent.messages.splice(insertAt, 0, toolMessage);
2358
2394
  if (!errorMessage && (wildcardTool === null || wildcardTool === void 0 ? void 0 : wildcardTool.followUp) !== false) return true;
2359
2395
  }
2360
2396
  return false;