@copilotkitnext/core 1.54.0-next.4 → 1.54.0-next.6

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
@@ -823,6 +823,13 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
823
823
  _defineProperty(this, "_tools", []);
824
824
  }
825
825
  /**
826
+ * Typed access to CopilotKitCore's internal ("friend") methods.
827
+ * Centralises the single unavoidable cast so call-sites stay clean.
828
+ */
829
+ get _internal() {
830
+ return this.core;
831
+ }
832
+ /**
826
833
  * Get all tools as a readonly array
827
834
  */
828
835
  get tools() {
@@ -880,9 +887,9 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
880
887
  await agent.detachActiveRun();
881
888
  agent.setMessages([]);
882
889
  agent.setState({});
883
- if (agent instanceof _ag_ui_client.HttpAgent) agent.headers = { ...this.core.headers };
890
+ if (agent instanceof _ag_ui_client.HttpAgent) agent.headers = { ...this._internal.headers };
884
891
  const runAgentResult = await agent.connectAgent({
885
- forwardedProps: this.core.properties,
892
+ forwardedProps: this._internal.properties,
886
893
  tools: this.buildFrontendTools(agent.agentId)
887
894
  }, this.createAgentErrorSubscriber(agent));
888
895
  return this.processAgentResult({
@@ -893,7 +900,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
893
900
  const connectError = error instanceof Error ? error : new Error(String(error));
894
901
  const context = {};
895
902
  if (agent.agentId) context.agentId = agent.agentId;
896
- await this.core.emitError({
903
+ await this._internal.emitError({
897
904
  error: connectError,
898
905
  code: CopilotKitCoreErrorCode.AGENT_CONNECT_FAILED,
899
906
  context
@@ -905,16 +912,16 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
905
912
  * Run an agent
906
913
  */
907
914
  async runAgent({ agent, forwardedProps }) {
908
- if (agent.agentId) this.core.suggestionEngine.clearSuggestions(agent.agentId);
909
- if (agent instanceof _ag_ui_client.HttpAgent) agent.headers = { ...this.core.headers };
915
+ if (agent.agentId) this._internal.suggestionEngine.clearSuggestions(agent.agentId);
916
+ if (agent instanceof _ag_ui_client.HttpAgent) agent.headers = { ...this._internal.headers };
910
917
  try {
911
918
  const runAgentResult = await agent.runAgent({
912
919
  forwardedProps: {
913
- ...this.core.properties,
920
+ ...this._internal.properties,
914
921
  ...forwardedProps
915
922
  },
916
923
  tools: this.buildFrontendTools(agent.agentId),
917
- context: Object.values(this.core.context)
924
+ context: Object.values(this._internal.context)
918
925
  }, this.createAgentErrorSubscriber(agent));
919
926
  return this.processAgentResult({
920
927
  runAgentResult,
@@ -924,7 +931,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
924
931
  const runError = error instanceof Error ? error : new Error(String(error));
925
932
  const context = {};
926
933
  if (agent.agentId) context.agentId = agent.agentId;
927
- await this.core.emitError({
934
+ await this._internal.emitError({
928
935
  error: runError,
929
936
  code: CopilotKitCoreErrorCode.AGENT_RUN_FAILED,
930
937
  context
@@ -959,83 +966,109 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
959
966
  }
960
967
  }
961
968
  if (needsFollowUp) return await this.runAgent({ agent });
962
- this.core.suggestionEngine.reloadSuggestions(agentId);
969
+ this._internal.suggestionEngine.reloadSuggestions(agentId);
963
970
  return runAgentResult;
964
971
  }
965
972
  /**
966
- * Execute a specific tool
973
+ * Shared handler execution logic used by executeSpecificTool, executeWildcardTool, and runTool.
974
+ * Handles arg parsing, subscriber notifications, handler invocation, result stringification,
975
+ * and error handling.
967
976
  */
968
- async executeSpecificTool(tool, toolCall, message, agent, agentId) {
969
- if ((tool === null || tool === void 0 ? void 0 : tool.agentId) && tool.agentId !== agent.agentId) return false;
977
+ async executeToolHandler({ tool, toolCall, agent, agentId, handlerArgs, toolType, messageId }) {
970
978
  let toolCallResult = "";
971
979
  let errorMessage;
972
- if (tool === null || tool === void 0 ? void 0 : tool.handler) {
973
- let parsedArgs;
974
- try {
975
- parsedArgs = ensureObjectArgs(JSON.parse(toolCall.function.arguments), toolCall.function.name);
976
- } catch (error) {
977
- const parseError = error instanceof Error ? error : new Error(String(error));
978
- errorMessage = parseError.message;
979
- await this.core.emitError({
980
- error: parseError,
981
- code: CopilotKitCoreErrorCode.TOOL_ARGUMENT_PARSE_FAILED,
982
- context: {
983
- agentId,
984
- toolCallId: toolCall.id,
985
- toolName: toolCall.function.name,
986
- rawArguments: toolCall.function.arguments,
987
- toolType: "specific",
988
- messageId: message.id
989
- }
990
- });
991
- }
992
- await this.core.notifySubscribers((subscriber) => {
993
- var _subscriber$onToolExe;
994
- return (_subscriber$onToolExe = subscriber.onToolExecutionStart) === null || _subscriber$onToolExe === void 0 ? void 0 : _subscriber$onToolExe.call(subscriber, {
995
- copilotkit: this.core,
996
- toolCallId: toolCall.id,
980
+ let isArgumentError = false;
981
+ let parsedArgs;
982
+ try {
983
+ parsedArgs = ensureObjectArgs(typeof handlerArgs === "string" ? JSON.parse(handlerArgs) : handlerArgs, toolCall.function.name);
984
+ } catch (error) {
985
+ const parseError = error instanceof Error ? error : new Error(String(error));
986
+ errorMessage = parseError.message;
987
+ isArgumentError = true;
988
+ await this._internal.emitError({
989
+ error: parseError,
990
+ code: CopilotKitCoreErrorCode.TOOL_ARGUMENT_PARSE_FAILED,
991
+ context: {
997
992
  agentId,
998
- toolName: toolCall.function.name,
999
- args: parsedArgs
1000
- });
1001
- }, "Subscriber onToolExecutionStart error:");
1002
- if (!errorMessage) try {
1003
- const result = await tool.handler(parsedArgs, {
1004
- toolCall,
1005
- agent
1006
- });
1007
- if (result === void 0 || result === null) toolCallResult = "";
1008
- else if (typeof result === "string") toolCallResult = result;
1009
- else toolCallResult = JSON.stringify(result);
1010
- } catch (error) {
1011
- const handlerError = error instanceof Error ? error : new Error(String(error));
1012
- errorMessage = handlerError.message;
1013
- await this.core.emitError({
1014
- error: handlerError,
1015
- code: CopilotKitCoreErrorCode.TOOL_HANDLER_FAILED,
1016
- context: {
1017
- agentId,
1018
- toolCallId: toolCall.id,
1019
- toolName: toolCall.function.name,
1020
- parsedArgs,
1021
- toolType: "specific",
1022
- messageId: message.id
1023
- }
1024
- });
1025
- }
1026
- if (errorMessage) toolCallResult = `Error: ${errorMessage}`;
1027
- await this.core.notifySubscribers((subscriber) => {
1028
- var _subscriber$onToolExe2;
1029
- return (_subscriber$onToolExe2 = subscriber.onToolExecutionEnd) === null || _subscriber$onToolExe2 === void 0 ? void 0 : _subscriber$onToolExe2.call(subscriber, {
1030
- copilotkit: this.core,
1031
993
  toolCallId: toolCall.id,
994
+ toolName: toolCall.function.name,
995
+ rawArguments: handlerArgs,
996
+ toolType,
997
+ ...messageId ? { messageId } : {}
998
+ }
999
+ });
1000
+ }
1001
+ await this._internal.notifySubscribers((subscriber) => {
1002
+ var _subscriber$onToolExe;
1003
+ return (_subscriber$onToolExe = subscriber.onToolExecutionStart) === null || _subscriber$onToolExe === void 0 ? void 0 : _subscriber$onToolExe.call(subscriber, {
1004
+ copilotkit: this.core,
1005
+ toolCallId: toolCall.id,
1006
+ agentId,
1007
+ toolName: toolCall.function.name,
1008
+ args: parsedArgs
1009
+ });
1010
+ }, "Subscriber onToolExecutionStart error:");
1011
+ if (!errorMessage) try {
1012
+ const result = await tool.handler(parsedArgs, {
1013
+ toolCall,
1014
+ agent
1015
+ });
1016
+ if (result === void 0 || result === null) toolCallResult = "";
1017
+ else if (typeof result === "string") toolCallResult = result;
1018
+ else toolCallResult = JSON.stringify(result);
1019
+ } catch (error) {
1020
+ const handlerError = error instanceof Error ? error : new Error(String(error));
1021
+ errorMessage = handlerError.message;
1022
+ await this._internal.emitError({
1023
+ error: handlerError,
1024
+ code: CopilotKitCoreErrorCode.TOOL_HANDLER_FAILED,
1025
+ context: {
1032
1026
  agentId,
1027
+ toolCallId: toolCall.id,
1033
1028
  toolName: toolCall.function.name,
1034
- result: errorMessage ? "" : toolCallResult,
1035
- error: errorMessage
1036
- });
1037
- }, "Subscriber onToolExecutionEnd error:");
1029
+ parsedArgs,
1030
+ toolType,
1031
+ ...messageId ? { messageId } : {}
1032
+ }
1033
+ });
1038
1034
  }
1035
+ if (errorMessage) toolCallResult = `Error: ${errorMessage}`;
1036
+ await this._internal.notifySubscribers((subscriber) => {
1037
+ var _subscriber$onToolExe2;
1038
+ return (_subscriber$onToolExe2 = subscriber.onToolExecutionEnd) === null || _subscriber$onToolExe2 === void 0 ? void 0 : _subscriber$onToolExe2.call(subscriber, {
1039
+ copilotkit: this.core,
1040
+ toolCallId: toolCall.id,
1041
+ agentId,
1042
+ toolName: toolCall.function.name,
1043
+ result: errorMessage ? "" : toolCallResult,
1044
+ error: errorMessage
1045
+ });
1046
+ }, "Subscriber onToolExecutionEnd error:");
1047
+ return {
1048
+ result: toolCallResult,
1049
+ error: errorMessage,
1050
+ isArgumentError
1051
+ };
1052
+ }
1053
+ /**
1054
+ * Execute a specific tool
1055
+ */
1056
+ async executeSpecificTool(tool, toolCall, message, agent, agentId) {
1057
+ if ((tool === null || tool === void 0 ? void 0 : tool.agentId) && tool.agentId !== agent.agentId) return false;
1058
+ let handlerResult = {
1059
+ result: "",
1060
+ error: void 0,
1061
+ isArgumentError: false
1062
+ };
1063
+ if (tool === null || tool === void 0 ? void 0 : tool.handler) handlerResult = await this.executeToolHandler({
1064
+ tool,
1065
+ toolCall,
1066
+ agent,
1067
+ agentId,
1068
+ handlerArgs: toolCall.function.arguments,
1069
+ toolType: "specific",
1070
+ messageId: message.id
1071
+ });
1039
1072
  {
1040
1073
  const messageIndex = agent.messages.findIndex((m) => m.id === message.id);
1041
1074
  if (messageIndex === -1) return false;
@@ -1043,15 +1076,18 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1043
1076
  id: (0, _copilotkitnext_shared.randomUUID)(),
1044
1077
  role: "tool",
1045
1078
  toolCallId: toolCall.id,
1046
- content: toolCallResult
1079
+ content: handlerResult.result
1047
1080
  };
1048
1081
  agent.messages.splice(messageIndex + 1, 0, toolMessage);
1049
- if (!errorMessage && (tool === null || tool === void 0 ? void 0 : tool.followUp) !== false) return true;
1082
+ if (!handlerResult.error && (tool === null || tool === void 0 ? void 0 : tool.followUp) !== false) return true;
1050
1083
  }
1051
1084
  return false;
1052
1085
  }
1053
1086
  /**
1054
- * Execute a wildcard tool
1087
+ * Execute a wildcard tool.
1088
+ * Wildcard tools receive args wrapped as `{toolName, args}`, which differs from
1089
+ * specific tools, so this method keeps its own arg-wrapping logic rather than
1090
+ * delegating to `executeToolHandler`.
1055
1091
  */
1056
1092
  async executeWildcardTool(wildcardTool, toolCall, message, agent, agentId) {
1057
1093
  if ((wildcardTool === null || wildcardTool === void 0 ? void 0 : wildcardTool.agentId) && wildcardTool.agentId !== agent.agentId) return false;
@@ -1064,7 +1100,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1064
1100
  } catch (error) {
1065
1101
  const parseError = error instanceof Error ? error : new Error(String(error));
1066
1102
  errorMessage = parseError.message;
1067
- await this.core.emitError({
1103
+ await this._internal.emitError({
1068
1104
  error: parseError,
1069
1105
  code: CopilotKitCoreErrorCode.TOOL_ARGUMENT_PARSE_FAILED,
1070
1106
  context: {
@@ -1081,7 +1117,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1081
1117
  toolName: toolCall.function.name,
1082
1118
  args: parsedArgs
1083
1119
  };
1084
- await this.core.notifySubscribers((subscriber) => {
1120
+ await this._internal.notifySubscribers((subscriber) => {
1085
1121
  var _subscriber$onToolExe3;
1086
1122
  return (_subscriber$onToolExe3 = subscriber.onToolExecutionStart) === null || _subscriber$onToolExe3 === void 0 ? void 0 : _subscriber$onToolExe3.call(subscriber, {
1087
1123
  copilotkit: this.core,
@@ -1102,7 +1138,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1102
1138
  } catch (error) {
1103
1139
  const handlerError = error instanceof Error ? error : new Error(String(error));
1104
1140
  errorMessage = handlerError.message;
1105
- await this.core.emitError({
1141
+ await this._internal.emitError({
1106
1142
  error: handlerError,
1107
1143
  code: CopilotKitCoreErrorCode.TOOL_HANDLER_FAILED,
1108
1144
  context: {
@@ -1116,7 +1152,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1116
1152
  });
1117
1153
  }
1118
1154
  if (errorMessage) toolCallResult = `Error: ${errorMessage}`;
1119
- await this.core.notifySubscribers((subscriber) => {
1155
+ await this._internal.notifySubscribers((subscriber) => {
1120
1156
  var _subscriber$onToolExe4;
1121
1157
  return (_subscriber$onToolExe4 = subscriber.onToolExecutionEnd) === null || _subscriber$onToolExe4 === void 0 ? void 0 : _subscriber$onToolExe4.call(subscriber, {
1122
1158
  copilotkit: this.core,
@@ -1143,6 +1179,94 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1143
1179
  return false;
1144
1180
  }
1145
1181
  /**
1182
+ * Programmatically execute a registered frontend tool without going through an LLM turn.
1183
+ * The handler runs, render components show up in the UI, and both the tool call and
1184
+ * result messages are added to `agent.messages`.
1185
+ */
1186
+ async runTool(params) {
1187
+ const { name, agentId, parameters = {}, followUp = false } = params;
1188
+ const tool = this.getTool({
1189
+ toolName: name,
1190
+ agentId
1191
+ });
1192
+ if (!tool) {
1193
+ const error = /* @__PURE__ */ new Error(`Tool not found: ${name}`);
1194
+ await this._internal.emitError({
1195
+ error,
1196
+ code: CopilotKitCoreErrorCode.TOOL_NOT_FOUND,
1197
+ context: {
1198
+ toolName: name,
1199
+ agentId
1200
+ }
1201
+ });
1202
+ throw error;
1203
+ }
1204
+ const resolvedAgentId = agentId !== null && agentId !== void 0 ? agentId : "default";
1205
+ const agent = this._internal.getAgent(resolvedAgentId);
1206
+ if (!agent) {
1207
+ const error = /* @__PURE__ */ new Error(`Agent not found: ${resolvedAgentId}`);
1208
+ await this._internal.emitError({
1209
+ error,
1210
+ code: CopilotKitCoreErrorCode.AGENT_NOT_FOUND,
1211
+ context: { agentId: resolvedAgentId }
1212
+ });
1213
+ throw error;
1214
+ }
1215
+ const toolCallId = (0, _copilotkitnext_shared.randomUUID)();
1216
+ const assistantMessage = {
1217
+ id: (0, _copilotkitnext_shared.randomUUID)(),
1218
+ role: "assistant",
1219
+ content: "",
1220
+ toolCalls: [{
1221
+ id: toolCallId,
1222
+ type: "function",
1223
+ function: {
1224
+ name,
1225
+ arguments: JSON.stringify(parameters)
1226
+ }
1227
+ }]
1228
+ };
1229
+ agent.messages.push(assistantMessage);
1230
+ let handlerResult = {
1231
+ result: "",
1232
+ error: void 0,
1233
+ isArgumentError: false
1234
+ };
1235
+ if (tool.handler) handlerResult = await this.executeToolHandler({
1236
+ tool,
1237
+ toolCall: assistantMessage.toolCalls[0],
1238
+ agent,
1239
+ agentId: resolvedAgentId,
1240
+ handlerArgs: parameters,
1241
+ toolType: "runTool"
1242
+ });
1243
+ const toolResultMessage = {
1244
+ id: (0, _copilotkitnext_shared.randomUUID)(),
1245
+ role: "tool",
1246
+ toolCallId,
1247
+ content: handlerResult.result
1248
+ };
1249
+ const assistantIndex = agent.messages.findIndex((m) => m.id === assistantMessage.id);
1250
+ if (assistantIndex !== -1) agent.messages.splice(assistantIndex + 1, 0, toolResultMessage);
1251
+ else agent.messages.push(toolResultMessage);
1252
+ if (!handlerResult.error && followUp !== false) {
1253
+ if (typeof followUp === "string" && followUp !== "generate") {
1254
+ const userMessage = {
1255
+ id: (0, _copilotkitnext_shared.randomUUID)(),
1256
+ role: "user",
1257
+ content: followUp
1258
+ };
1259
+ agent.messages.push(userMessage);
1260
+ }
1261
+ await this.runAgent({ agent });
1262
+ }
1263
+ return {
1264
+ toolCallId,
1265
+ result: handlerResult.result,
1266
+ error: handlerResult.error
1267
+ };
1268
+ }
1269
+ /**
1146
1270
  * Build frontend tools for an agent
1147
1271
  */
1148
1272
  buildFrontendTools(agentId) {
@@ -1162,7 +1286,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1162
1286
  const emitAgentError = async (error, code, extraContext = {}) => {
1163
1287
  const context = { ...extraContext };
1164
1288
  if (agent.agentId) context.agentId = agent.agentId;
1165
- await this.core.emitError({
1289
+ await this._internal.emitError({
1166
1290
  error,
1167
1291
  code,
1168
1292
  context
@@ -1414,6 +1538,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1414
1538
  CopilotKitCoreErrorCode["AGENT_RUN_ERROR_EVENT"] = "agent_run_error_event";
1415
1539
  CopilotKitCoreErrorCode["TOOL_ARGUMENT_PARSE_FAILED"] = "tool_argument_parse_failed";
1416
1540
  CopilotKitCoreErrorCode["TOOL_HANDLER_FAILED"] = "tool_handler_failed";
1541
+ CopilotKitCoreErrorCode["TOOL_NOT_FOUND"] = "tool_not_found";
1542
+ CopilotKitCoreErrorCode["AGENT_NOT_FOUND"] = "agent_not_found";
1417
1543
  CopilotKitCoreErrorCode["TRANSCRIPTION_FAILED"] = "transcription_failed";
1418
1544
  CopilotKitCoreErrorCode["TRANSCRIPTION_SERVICE_NOT_CONFIGURED"] = "transcription_service_not_configured";
1419
1545
  CopilotKitCoreErrorCode["TRANSCRIPTION_INVALID_AUDIO"] = "transcription_invalid_audio";
@@ -1638,6 +1764,14 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
1638
1764
  return this.runHandler.runAgent(params);
1639
1765
  }
1640
1766
  /**
1767
+ * Programmatically execute a registered frontend tool without going through an LLM turn.
1768
+ * The handler runs, render components show up in the UI, and both the tool call and
1769
+ * result messages are added to `agent.messages`.
1770
+ */
1771
+ async runTool(params) {
1772
+ return this.runHandler.runTool(params);
1773
+ }
1774
+ /**
1641
1775
  * State management (delegated to StateManager)
1642
1776
  */
1643
1777
  getStateByRun(agentId, threadId, runId) {