@copilotkitnext/react 0.0.7 → 0.0.9-alpha.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.js CHANGED
@@ -41,6 +41,7 @@ __export(index_exports, {
41
41
  CopilotChatUserMessage: () => CopilotChatUserMessage_default,
42
42
  CopilotChatView: () => CopilotChatView_default,
43
43
  CopilotKitProvider: () => CopilotKitProvider,
44
+ defineToolCallRender: () => defineToolCallRender,
44
45
  useAgent: () => useAgent,
45
46
  useAgentContext: () => useAgentContext,
46
47
  useCopilotChatConfiguration: () => useCopilotChatConfiguration,
@@ -58,6 +59,7 @@ var import_lucide_react2 = require("lucide-react");
58
59
 
59
60
  // src/providers/CopilotChatConfigurationProvider.tsx
60
61
  var import_react = require("react");
62
+ var import_shared = require("@copilotkitnext/shared");
61
63
  var import_jsx_runtime = require("react/jsx-runtime");
62
64
  var CopilotChatDefaultLabels = {
63
65
  chatInputPlaceholder: "Type a message...",
@@ -78,16 +80,20 @@ var CopilotChatDefaultLabels = {
78
80
  chatDisclaimerText: "AI can make mistakes. Please verify important information."
79
81
  };
80
82
  var CopilotChatConfiguration = (0, import_react.createContext)(null);
81
- var CopilotChatConfigurationProvider = ({ children, labels = {}, inputValue, onSubmitInput, onChangeInput }) => {
83
+ var CopilotChatConfigurationProvider = ({
84
+ children,
85
+ labels = {},
86
+ agentId,
87
+ threadId
88
+ }) => {
82
89
  const mergedLabels = {
83
90
  ...CopilotChatDefaultLabels,
84
91
  ...labels
85
92
  };
86
93
  const configurationValue = {
87
94
  labels: mergedLabels,
88
- inputValue,
89
- onSubmitInput,
90
- onChangeInput
95
+ agentId: agentId ?? import_shared.DEFAULT_AGENT_ID,
96
+ threadId
91
97
  };
92
98
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CopilotChatConfiguration.Provider, { value: configurationValue, children });
93
99
  };
@@ -537,10 +543,14 @@ function CopilotChatInput({
537
543
  className,
538
544
  ...props
539
545
  }) {
540
- const { inputValue, onSubmitInput, onChangeInput } = useCopilotChatConfiguration();
541
- value ??= inputValue;
542
- onSubmitMessage ??= onSubmitInput;
543
- onChange ??= onChangeInput;
546
+ const isControlled = value !== void 0;
547
+ const [internalValue, setInternalValue] = (0, import_react4.useState)(() => value ?? "");
548
+ (0, import_react4.useEffect)(() => {
549
+ if (!isControlled && value !== void 0) {
550
+ setInternalValue(value);
551
+ }
552
+ }, [isControlled, value]);
553
+ const resolvedValue = isControlled ? value ?? "" : internalValue;
544
554
  const inputRef = (0, import_react4.useRef)(null);
545
555
  const audioRecorderRef = (0, import_react4.useRef)(null);
546
556
  (0, import_react4.useEffect)(() => {
@@ -557,7 +567,11 @@ function CopilotChatInput({
557
567
  }
558
568
  }, [mode]);
559
569
  const handleChange = (e) => {
560
- onChange?.(e.target.value);
570
+ const nextValue = e.target.value;
571
+ if (!isControlled) {
572
+ setInternalValue(nextValue);
573
+ }
574
+ onChange?.(nextValue);
561
575
  };
562
576
  const handleKeyDown = (e) => {
563
577
  if (e.key === "Enter" && !e.shiftKey) {
@@ -566,17 +580,25 @@ function CopilotChatInput({
566
580
  }
567
581
  };
568
582
  const send = () => {
569
- const trimmed = value?.trim();
570
- if (trimmed) {
571
- onSubmitMessage?.(trimmed);
572
- if (inputRef.current) {
573
- inputRef.current.focus();
574
- }
583
+ if (!onSubmitMessage) {
584
+ return;
585
+ }
586
+ const trimmed = resolvedValue.trim();
587
+ if (!trimmed) {
588
+ return;
589
+ }
590
+ onSubmitMessage(trimmed);
591
+ if (!isControlled) {
592
+ setInternalValue("");
593
+ onChange?.("");
594
+ }
595
+ if (inputRef.current) {
596
+ inputRef.current.focus();
575
597
  }
576
598
  };
577
599
  const BoundTextArea = renderSlot(textArea, CopilotChatInput.TextArea, {
578
600
  ref: inputRef,
579
- value,
601
+ value: resolvedValue,
580
602
  onChange: handleChange,
581
603
  onKeyDown: handleKeyDown,
582
604
  autoFocus
@@ -590,7 +612,7 @@ function CopilotChatInput({
590
612
  );
591
613
  const BoundSendButton = renderSlot(sendButton, CopilotChatInput.SendButton, {
592
614
  onClick: send,
593
- disabled: !value?.trim() || !onSubmitMessage
615
+ disabled: !resolvedValue.trim() || !onSubmitMessage
594
616
  });
595
617
  const BoundStartTranscribeButton = renderSlot(
596
618
  startTranscribeButton,
@@ -1009,13 +1031,9 @@ var CopilotKitProvider = ({
1009
1031
  return { tools: processedTools, renderToolCalls: processedRenderToolCalls };
1010
1032
  }, [humanInTheLoopList]);
1011
1033
  const allTools = (0, import_react5.useMemo)(() => {
1012
- const tools = {};
1013
- frontendToolsList.forEach((tool) => {
1014
- tools[tool.name] = tool;
1015
- });
1016
- processedHumanInTheLoopTools.tools.forEach((tool) => {
1017
- tools[tool.name] = tool;
1018
- });
1034
+ const tools = [];
1035
+ tools.push(...frontendToolsList);
1036
+ tools.push(...processedHumanInTheLoopTools.tools);
1019
1037
  return tools;
1020
1038
  }, [frontendToolsList, processedHumanInTheLoopTools]);
1021
1039
  const allRenderToolCalls = (0, import_react5.useMemo)(() => {
@@ -1045,9 +1063,30 @@ var CopilotKitProvider = ({
1045
1063
  return copilotkit2;
1046
1064
  }, [allTools]);
1047
1065
  (0, import_react5.useEffect)(() => {
1048
- setCurrentRenderToolCalls(
1049
- (prev) => prev === allRenderToolCalls ? prev : allRenderToolCalls
1050
- );
1066
+ setCurrentRenderToolCalls((prev) => {
1067
+ const keyOf = (rc) => `${rc?.agentId ?? ""}:${rc?.name ?? ""}`;
1068
+ const computedMap = /* @__PURE__ */ new Map();
1069
+ for (const rc of allRenderToolCalls) {
1070
+ computedMap.set(keyOf(rc), rc);
1071
+ }
1072
+ const merged = [...computedMap.values()];
1073
+ for (const rc of prev) {
1074
+ const k = keyOf(rc);
1075
+ if (!computedMap.has(k)) merged.push(rc);
1076
+ }
1077
+ const sameLength = merged.length === prev.length;
1078
+ if (sameLength) {
1079
+ let same = true;
1080
+ for (let i = 0; i < merged.length; i++) {
1081
+ if (merged[i] !== prev[i]) {
1082
+ same = false;
1083
+ break;
1084
+ }
1085
+ }
1086
+ if (same) return prev;
1087
+ }
1088
+ return merged;
1089
+ });
1051
1090
  }, [allRenderToolCalls]);
1052
1091
  (0, import_react5.useEffect)(() => {
1053
1092
  copilotkit.setRuntimeUrl(runtimeUrl);
@@ -1076,10 +1115,7 @@ var useCopilotKit = () => {
1076
1115
  }
1077
1116
  (0, import_react5.useEffect)(() => {
1078
1117
  const unsubscribe = context.copilotkit.subscribe({
1079
- onRuntimeLoaded: () => {
1080
- forceUpdate();
1081
- },
1082
- onRuntimeLoadError: () => {
1118
+ onRuntimeConnectionStatusChanged: () => {
1083
1119
  forceUpdate();
1084
1120
  }
1085
1121
  });
@@ -1091,38 +1127,76 @@ var useCopilotKit = () => {
1091
1127
  };
1092
1128
 
1093
1129
  // src/hooks/use-render-tool-call.tsx
1094
- var import_shared = require("@copilotkitnext/shared");
1130
+ var import_shared2 = require("@copilotkitnext/shared");
1095
1131
  var import_jsx_runtime8 = require("react/jsx-runtime");
1096
1132
  function useRenderToolCall() {
1097
- const { currentRenderToolCalls } = useCopilotKit();
1133
+ const { currentRenderToolCalls, copilotkit } = useCopilotKit();
1134
+ const { agentId } = useCopilotChatConfiguration();
1135
+ const [executingToolCallIds, setExecutingToolCallIds] = (0, import_react6.useState)(() => /* @__PURE__ */ new Set());
1136
+ (0, import_react6.useEffect)(() => {
1137
+ const unsubscribe = copilotkit.subscribe({
1138
+ onToolExecutionStart: ({ toolCallId }) => {
1139
+ setExecutingToolCallIds((prev) => {
1140
+ if (prev.has(toolCallId)) return prev;
1141
+ const next = new Set(prev);
1142
+ next.add(toolCallId);
1143
+ return next;
1144
+ });
1145
+ },
1146
+ onToolExecutionEnd: ({ toolCallId }) => {
1147
+ setExecutingToolCallIds((prev) => {
1148
+ if (!prev.has(toolCallId)) return prev;
1149
+ const next = new Set(prev);
1150
+ next.delete(toolCallId);
1151
+ return next;
1152
+ });
1153
+ }
1154
+ });
1155
+ return () => unsubscribe();
1156
+ }, [copilotkit]);
1098
1157
  const renderToolCall = (0, import_react6.useCallback)(
1099
1158
  ({
1100
1159
  toolCall,
1101
1160
  toolMessage,
1102
- isLoading
1161
+ isRunning
1103
1162
  }) => {
1104
- const renderConfig = currentRenderToolCalls.find(
1163
+ const exactMatches = currentRenderToolCalls.filter(
1105
1164
  (rc) => rc.name === toolCall.function.name
1106
- ) || currentRenderToolCalls.find((rc) => rc.name === "*");
1165
+ );
1166
+ const renderConfig = exactMatches.find((rc) => rc.agentId === agentId) || exactMatches.find((rc) => !rc.agentId) || exactMatches[0] || currentRenderToolCalls.find((rc) => rc.name === "*");
1107
1167
  if (!renderConfig) {
1108
1168
  return null;
1109
1169
  }
1110
1170
  const RenderComponent = renderConfig.render;
1111
- const args = (0, import_shared.partialJSONParse)(toolCall.function.arguments);
1171
+ const args = (0, import_shared2.partialJSONParse)(toolCall.function.arguments);
1172
+ const toolName = toolCall.function.name;
1112
1173
  if (toolMessage) {
1113
1174
  return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
1114
1175
  RenderComponent,
1115
1176
  {
1177
+ name: toolName,
1116
1178
  args,
1117
1179
  status: import_core2.ToolCallStatus.Complete,
1118
1180
  result: toolMessage.content
1119
1181
  },
1120
1182
  toolCall.id
1121
1183
  );
1122
- } else if (isLoading) {
1184
+ } else if (executingToolCallIds.has(toolCall.id)) {
1185
+ return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
1186
+ RenderComponent,
1187
+ {
1188
+ name: toolName,
1189
+ args,
1190
+ status: import_core2.ToolCallStatus.Executing,
1191
+ result: void 0
1192
+ },
1193
+ toolCall.id
1194
+ );
1195
+ } else if (isRunning) {
1123
1196
  return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
1124
1197
  RenderComponent,
1125
1198
  {
1199
+ name: toolName,
1126
1200
  args,
1127
1201
  status: import_core2.ToolCallStatus.InProgress,
1128
1202
  result: void 0
@@ -1133,6 +1207,7 @@ function useRenderToolCall() {
1133
1207
  return /* @__PURE__ */ (0, import_jsx_runtime8.jsx)(
1134
1208
  RenderComponent,
1135
1209
  {
1210
+ name: toolName,
1136
1211
  args,
1137
1212
  status: import_core2.ToolCallStatus.Complete,
1138
1213
  result: ""
@@ -1141,7 +1216,7 @@ function useRenderToolCall() {
1141
1216
  );
1142
1217
  }
1143
1218
  },
1144
- [currentRenderToolCalls]
1219
+ [currentRenderToolCalls, executingToolCallIds, agentId]
1145
1220
  );
1146
1221
  return renderToolCall;
1147
1222
  }
@@ -1149,36 +1224,36 @@ function useRenderToolCall() {
1149
1224
  // src/hooks/use-frontend-tool.tsx
1150
1225
  var import_react7 = require("react");
1151
1226
  function useFrontendTool(tool) {
1152
- const { renderToolCalls, copilotkit, setCurrentRenderToolCalls } = useCopilotKit();
1227
+ const { copilotkit, setCurrentRenderToolCalls } = useCopilotKit();
1153
1228
  (0, import_react7.useEffect)(() => {
1154
- if (tool.name in copilotkit.tools) {
1229
+ const name = tool.name;
1230
+ if (copilotkit.getTool({ toolName: name, agentId: tool.agentId })) {
1155
1231
  console.warn(
1156
- `Tool '${tool.name}' already exists. It will be overridden.`
1232
+ `Tool '${name}' already exists for agent '${tool.agentId || "global"}'. Overriding with latest registration.`
1157
1233
  );
1234
+ copilotkit.removeTool(name, tool.agentId);
1158
1235
  }
1159
1236
  copilotkit.addTool(tool);
1160
- if (tool.render && tool.name in renderToolCalls) {
1161
- console.warn(
1162
- `Render component for tool '${tool.name}' already exists. It will be overridden.`
1163
- );
1164
- }
1165
- if (tool.render && tool.parameters) {
1166
- setCurrentRenderToolCalls((prev) => [
1167
- ...prev,
1168
- {
1169
- name: tool.name,
1170
- args: tool.parameters,
1171
- render: tool.render
1172
- }
1173
- ]);
1237
+ if (tool.render) {
1238
+ setCurrentRenderToolCalls((prev) => {
1239
+ const replaced = prev.filter(
1240
+ (rc) => !(rc.name === name && rc.agentId === tool.agentId)
1241
+ );
1242
+ return [
1243
+ ...replaced,
1244
+ {
1245
+ name,
1246
+ args: tool.parameters,
1247
+ agentId: tool.agentId,
1248
+ render: tool.render
1249
+ }
1250
+ ];
1251
+ });
1174
1252
  }
1175
1253
  return () => {
1176
- copilotkit.removeTool(tool.name);
1177
- setCurrentRenderToolCalls(
1178
- (prev) => prev.filter((rc) => rc.name !== tool.name)
1179
- );
1254
+ copilotkit.removeTool(name, tool.agentId);
1180
1255
  };
1181
- }, [tool, copilotkit, renderToolCalls, setCurrentRenderToolCalls]);
1256
+ }, [tool.name, copilotkit, setCurrentRenderToolCalls]);
1182
1257
  }
1183
1258
 
1184
1259
  // src/hooks/use-human-in-the-loop.tsx
@@ -1188,7 +1263,10 @@ function useHumanInTheLoop(tool) {
1188
1263
  const [status, setStatus] = (0, import_react8.useState)(
1189
1264
  "inProgress"
1190
1265
  );
1266
+ const statusRef = (0, import_react8.useRef)(status);
1191
1267
  const resolvePromiseRef = (0, import_react8.useRef)(null);
1268
+ const { setCurrentRenderToolCalls } = useCopilotKit();
1269
+ statusRef.current = status;
1192
1270
  const respond = (0, import_react8.useCallback)(async (result) => {
1193
1271
  if (resolvePromiseRef.current) {
1194
1272
  resolvePromiseRef.current(result);
@@ -1205,7 +1283,8 @@ function useHumanInTheLoop(tool) {
1205
1283
  const RenderComponent = (0, import_react8.useCallback)(
1206
1284
  (props) => {
1207
1285
  const ToolComponent = tool.render;
1208
- if (status === "inProgress" && props.status === "inProgress") {
1286
+ const currentStatus = statusRef.current;
1287
+ if (currentStatus === "inProgress" && props.status === "inProgress") {
1209
1288
  const enhancedProps = {
1210
1289
  ...props,
1211
1290
  name: tool.name,
@@ -1213,7 +1292,7 @@ function useHumanInTheLoop(tool) {
1213
1292
  respond: void 0
1214
1293
  };
1215
1294
  return import_react9.default.createElement(ToolComponent, enhancedProps);
1216
- } else if (status === "executing" && props.status === "executing") {
1295
+ } else if (currentStatus === "executing" && props.status === "executing") {
1217
1296
  const enhancedProps = {
1218
1297
  ...props,
1219
1298
  name: tool.name,
@@ -1221,7 +1300,7 @@ function useHumanInTheLoop(tool) {
1221
1300
  respond
1222
1301
  };
1223
1302
  return import_react9.default.createElement(ToolComponent, enhancedProps);
1224
- } else if (status === "complete" && props.status === "complete") {
1303
+ } else if (currentStatus === "complete" && props.status === "complete") {
1225
1304
  const enhancedProps = {
1226
1305
  ...props,
1227
1306
  name: tool.name,
@@ -1232,7 +1311,7 @@ function useHumanInTheLoop(tool) {
1232
1311
  }
1233
1312
  return import_react9.default.createElement(ToolComponent, props);
1234
1313
  },
1235
- [tool.render, tool.name, tool.description, status, respond]
1314
+ [tool.render, tool.name, tool.description, respond]
1236
1315
  );
1237
1316
  const frontendTool = {
1238
1317
  ...tool,
@@ -1240,42 +1319,75 @@ function useHumanInTheLoop(tool) {
1240
1319
  render: RenderComponent
1241
1320
  };
1242
1321
  useFrontendTool(frontendTool);
1322
+ (0, import_react8.useEffect)(() => {
1323
+ return () => {
1324
+ setCurrentRenderToolCalls(
1325
+ (prev) => prev.filter(
1326
+ (rc) => rc.name !== tool.name || rc.agentId !== tool.agentId
1327
+ )
1328
+ );
1329
+ };
1330
+ }, [setCurrentRenderToolCalls, tool.name, tool.agentId]);
1243
1331
  }
1244
1332
 
1245
1333
  // src/hooks/use-agent.tsx
1246
1334
  var import_react10 = require("react");
1247
- var import_shared2 = require("@copilotkitnext/shared");
1248
- function useAgent({ agentId } = {}) {
1249
- agentId ??= import_shared2.DEFAULT_AGENT_ID;
1335
+ var import_shared3 = require("@copilotkitnext/shared");
1336
+ var ALL_UPDATES = [
1337
+ "OnMessagesChanged" /* OnMessagesChanged */,
1338
+ "OnStateChanged" /* OnStateChanged */,
1339
+ "OnRunStatusChanged" /* OnRunStatusChanged */
1340
+ ];
1341
+ function useAgent({ agentId, updates } = {}) {
1342
+ agentId ??= import_shared3.DEFAULT_AGENT_ID;
1250
1343
  const { copilotkit } = useCopilotKit();
1251
1344
  const [, forceUpdate] = (0, import_react10.useReducer)((x) => x + 1, 0);
1252
- const [isRunning, setIsRunning] = (0, import_react10.useState)(false);
1345
+ const updateFlags = (0, import_react10.useMemo)(
1346
+ () => updates ?? ALL_UPDATES,
1347
+ [JSON.stringify(updates)]
1348
+ );
1253
1349
  const agent = (0, import_react10.useMemo)(() => {
1254
1350
  return copilotkit.getAgent(agentId);
1255
- }, [agentId, copilotkit.agents, copilotkit.didLoadRuntime, copilotkit]);
1351
+ }, [
1352
+ agentId,
1353
+ copilotkit.agents,
1354
+ copilotkit.runtimeConnectionStatus,
1355
+ copilotkit
1356
+ ]);
1256
1357
  (0, import_react10.useEffect)(() => {
1257
- const subscription = agent?.subscribe({
1258
- onMessagesChanged() {
1358
+ if (!agent) {
1359
+ return;
1360
+ }
1361
+ if (updateFlags.length === 0) {
1362
+ return;
1363
+ }
1364
+ const handlers = {};
1365
+ if (updateFlags.includes("OnMessagesChanged" /* OnMessagesChanged */)) {
1366
+ handlers.onMessagesChanged = () => {
1259
1367
  forceUpdate();
1260
- },
1261
- onStateChanged() {
1368
+ };
1369
+ }
1370
+ if (updateFlags.includes("OnStateChanged" /* OnStateChanged */)) {
1371
+ handlers.onStateChanged = () => {
1262
1372
  forceUpdate();
1263
- },
1264
- onRunInitialized() {
1265
- setIsRunning(true);
1266
- },
1267
- onRunFinalized() {
1268
- setIsRunning(false);
1269
- },
1270
- onRunFailed() {
1271
- setIsRunning(false);
1272
- }
1273
- });
1274
- return () => subscription?.unsubscribe();
1275
- }, [agent]);
1373
+ };
1374
+ }
1375
+ if (updateFlags.includes("OnRunStatusChanged" /* OnRunStatusChanged */)) {
1376
+ handlers.onRunInitialized = () => {
1377
+ forceUpdate();
1378
+ };
1379
+ handlers.onRunFinalized = () => {
1380
+ forceUpdate();
1381
+ };
1382
+ handlers.onRunFailed = () => {
1383
+ forceUpdate();
1384
+ };
1385
+ }
1386
+ const subscription = agent.subscribe(handlers);
1387
+ return () => subscription.unsubscribe();
1388
+ }, [agent, forceUpdate, JSON.stringify(updateFlags)]);
1276
1389
  return {
1277
- agent,
1278
- isRunning
1390
+ agent
1279
1391
  };
1280
1392
  }
1281
1393
 
@@ -1299,7 +1411,7 @@ var import_jsx_runtime9 = require("react/jsx-runtime");
1299
1411
  function CopilotChatToolCallsView({
1300
1412
  message,
1301
1413
  messages = [],
1302
- isLoading = false
1414
+ isRunning = false
1303
1415
  }) {
1304
1416
  const renderToolCall = useRenderToolCall();
1305
1417
  if (!message.toolCalls || message.toolCalls.length === 0) {
@@ -1312,7 +1424,7 @@ function CopilotChatToolCallsView({
1312
1424
  return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(import_react12.default.Fragment, { children: renderToolCall({
1313
1425
  toolCall,
1314
1426
  toolMessage,
1315
- isLoading
1427
+ isRunning
1316
1428
  }) }, toolCall.id);
1317
1429
  }) });
1318
1430
  }
@@ -1323,7 +1435,7 @@ var import_jsx_runtime10 = require("react/jsx-runtime");
1323
1435
  function CopilotChatAssistantMessage({
1324
1436
  message,
1325
1437
  messages,
1326
- isLoading,
1438
+ isRunning,
1327
1439
  onThumbsUp,
1328
1440
  onThumbsDown,
1329
1441
  onReadAloud,
@@ -1412,7 +1524,7 @@ function CopilotChatAssistantMessage({
1412
1524
  {
1413
1525
  message,
1414
1526
  messages,
1415
- isLoading
1527
+ isRunning
1416
1528
  }
1417
1529
  );
1418
1530
  if (children) {
@@ -1427,7 +1539,7 @@ function CopilotChatAssistantMessage({
1427
1539
  regenerateButton: boundRegenerateButton,
1428
1540
  message,
1429
1541
  messages,
1430
- isLoading,
1542
+ isRunning,
1431
1543
  onThumbsUp,
1432
1544
  onThumbsDown,
1433
1545
  onReadAloud,
@@ -1900,7 +2012,7 @@ function CopilotChatMessageView({
1900
2012
  assistantMessage,
1901
2013
  userMessage,
1902
2014
  cursor,
1903
- isLoading = false,
2015
+ isRunning = false,
1904
2016
  children,
1905
2017
  className,
1906
2018
  ...props
@@ -1911,7 +2023,7 @@ function CopilotChatMessageView({
1911
2023
  key: message.id,
1912
2024
  message,
1913
2025
  messages,
1914
- isLoading
2026
+ isRunning
1915
2027
  });
1916
2028
  } else if (message.role === "user") {
1917
2029
  return renderSlot(userMessage, CopilotChatUserMessage_default, {
@@ -1922,11 +2034,11 @@ function CopilotChatMessageView({
1922
2034
  return;
1923
2035
  }).filter(Boolean);
1924
2036
  if (children) {
1925
- return children({ messageElements, messages, isLoading });
2037
+ return children({ messageElements, messages, isRunning });
1926
2038
  }
1927
2039
  return /* @__PURE__ */ (0, import_jsx_runtime12.jsxs)("div", { className: (0, import_tailwind_merge6.twMerge)("flex flex-col", className), ...props, children: [
1928
2040
  messageElements,
1929
- isLoading && renderSlot(cursor, CopilotChatMessageView.Cursor, {})
2041
+ isRunning && renderSlot(cursor, CopilotChatMessageView.Cursor, {})
1930
2042
  ] });
1931
2043
  }
1932
2044
  CopilotChatMessageView.Cursor = function Cursor({
@@ -1962,6 +2074,8 @@ function CopilotChatView({
1962
2074
  disclaimer,
1963
2075
  messages = [],
1964
2076
  autoScroll = true,
2077
+ inputProps,
2078
+ isRunning = false,
1965
2079
  children,
1966
2080
  className,
1967
2081
  ...props
@@ -2001,9 +2115,14 @@ function CopilotChatView({
2001
2115
  };
2002
2116
  }, []);
2003
2117
  const BoundMessageView = renderSlot(messageView, CopilotChatMessageView_default, {
2004
- messages
2118
+ messages,
2119
+ isRunning
2005
2120
  });
2006
- const BoundInput = renderSlot(input, CopilotChatInput_default, {});
2121
+ const BoundInput = renderSlot(
2122
+ input,
2123
+ CopilotChatInput_default,
2124
+ inputProps ?? {}
2125
+ );
2007
2126
  const BoundFeather = renderSlot(feather, CopilotChatView.Feather, {});
2008
2127
  const BoundScrollView = renderSlot(scrollView, CopilotChatView.ScrollView, {
2009
2128
  autoScroll,
@@ -2221,83 +2340,100 @@ function CopilotChatView({
2221
2340
  var CopilotChatView_default = CopilotChatView;
2222
2341
 
2223
2342
  // src/components/chat/CopilotChat.tsx
2224
- var import_shared3 = require("@copilotkitnext/shared");
2343
+ var import_shared4 = require("@copilotkitnext/shared");
2225
2344
  var import_react16 = require("react");
2226
2345
  var import_ts_deepmerge = require("ts-deepmerge");
2346
+ var import_client = require("@ag-ui/client");
2227
2347
  var import_jsx_runtime14 = require("react/jsx-runtime");
2228
2348
  function CopilotChat({
2229
- agentId = import_shared3.DEFAULT_AGENT_ID,
2349
+ agentId = import_shared4.DEFAULT_AGENT_ID,
2230
2350
  threadId,
2231
2351
  ...props
2232
2352
  }) {
2233
2353
  const { agent } = useAgent({ agentId });
2234
- const [isLoading, setIsLoading] = (0, import_react16.useState)(false);
2235
- threadId = threadId ?? (0, import_react16.useMemo)(() => (0, import_shared3.randomUUID)(), []);
2236
- const subscriber = {
2237
- onTextMessageStartEvent: () => setIsLoading(false),
2238
- onToolCallStartEvent: () => setIsLoading(false)
2239
- };
2354
+ const { copilotkit } = useCopilotKit();
2355
+ const resolvedThreadId = (0, import_react16.useMemo)(() => threadId ?? (0, import_shared4.randomUUID)(), [threadId]);
2240
2356
  (0, import_react16.useEffect)(() => {
2241
- const connect = async () => {
2242
- setIsLoading(true);
2243
- await agent?.runAgent(
2244
- {
2245
- forwardedProps: { __copilotkitConnect: true }
2246
- },
2247
- subscriber
2248
- );
2249
- setIsLoading(false);
2357
+ const connect = async (agent2) => {
2358
+ try {
2359
+ await copilotkit.connectAgent({ agent: agent2, agentId });
2360
+ } catch (error) {
2361
+ if (error instanceof import_client.AGUIConnectNotImplementedError) {
2362
+ } else {
2363
+ throw error;
2364
+ }
2365
+ }
2250
2366
  };
2251
2367
  if (agent) {
2252
- agent.threadId = threadId;
2253
- if ("isCopilotKitAgent" in agent) {
2254
- connect();
2255
- } else {
2256
- setIsLoading(false);
2257
- }
2368
+ agent.threadId = resolvedThreadId;
2369
+ connect(agent);
2258
2370
  }
2259
2371
  return () => {
2260
2372
  };
2261
- }, [threadId, agent]);
2262
- const [inputValue, setInputValue] = (0, import_react16.useState)("");
2373
+ }, [resolvedThreadId, agent, copilotkit, agentId]);
2263
2374
  const onSubmitInput = (0, import_react16.useCallback)(
2264
2375
  async (value) => {
2265
- setInputValue("");
2266
2376
  agent?.addMessage({
2267
- id: (0, import_shared3.randomUUID)(),
2377
+ id: (0, import_shared4.randomUUID)(),
2268
2378
  role: "user",
2269
2379
  content: value
2270
2380
  });
2271
- setIsLoading(true);
2272
- await agent?.runAgent({}, subscriber);
2273
- setIsLoading(false);
2381
+ if (agent) {
2382
+ try {
2383
+ await copilotkit.runAgent({ agent, agentId });
2384
+ } catch (error) {
2385
+ console.error("CopilotChat: runAgent failed", error);
2386
+ }
2387
+ }
2274
2388
  },
2275
- [agent]
2389
+ [agent, copilotkit, agentId]
2276
2390
  );
2391
+ const {
2392
+ inputProps: providedInputProps,
2393
+ messageView: providedMessageView,
2394
+ ...restProps
2395
+ } = props;
2277
2396
  const mergedProps = (0, import_ts_deepmerge.merge)(
2278
2397
  {
2279
- messageView: { isLoading }
2398
+ isRunning: agent?.isRunning ?? false
2280
2399
  },
2281
2400
  {
2282
- ...props,
2283
- ...typeof props.messageView === "string" ? { messageView: { className: props.messageView } } : props.messageView !== void 0 ? { messageView: props.messageView } : {}
2401
+ ...restProps,
2402
+ ...typeof providedMessageView === "string" ? { messageView: { className: providedMessageView } } : providedMessageView !== void 0 ? { messageView: providedMessageView } : {}
2284
2403
  }
2285
2404
  );
2286
2405
  return /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(
2287
2406
  CopilotChatConfigurationProvider,
2288
2407
  {
2289
- inputValue,
2290
- onSubmitInput,
2291
- onChangeInput: setInputValue,
2408
+ agentId,
2409
+ threadId: resolvedThreadId,
2292
2410
  children: /* @__PURE__ */ (0, import_jsx_runtime14.jsx)(
2293
2411
  CopilotChatView,
2294
2412
  {
2295
- ...{ messages: agent?.messages ?? [], ...mergedProps }
2413
+ ...{
2414
+ messages: agent?.messages ?? [],
2415
+ inputProps: {
2416
+ onSubmitMessage: onSubmitInput,
2417
+ ...providedInputProps
2418
+ },
2419
+ ...mergedProps
2420
+ }
2296
2421
  }
2297
2422
  )
2298
2423
  }
2299
2424
  );
2300
2425
  }
2426
+
2427
+ // src/types/defineToolCallRender.ts
2428
+ function defineToolCallRender(def) {
2429
+ return {
2430
+ name: def.name,
2431
+ args: def.args,
2432
+ // Coerce to ComponentType to align with ReactToolCallRender
2433
+ render: def.render,
2434
+ ...def.agentId ? { agentId: def.agentId } : {}
2435
+ };
2436
+ }
2301
2437
  // Annotate the CommonJS export names for ESM import in node:
2302
2438
  0 && (module.exports = {
2303
2439
  AudioRecorderError,
@@ -2311,6 +2447,7 @@ function CopilotChat({
2311
2447
  CopilotChatUserMessage,
2312
2448
  CopilotChatView,
2313
2449
  CopilotKitProvider,
2450
+ defineToolCallRender,
2314
2451
  useAgent,
2315
2452
  useAgentContext,
2316
2453
  useCopilotChatConfiguration,