@copilotkit/react-ui 1.68.0 → 1.68.2

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
@@ -1427,6 +1427,36 @@ const Markdown = ({ content, components, remarkPlugins, rehypePlugins, ...rest }
1427
1427
  });
1428
1428
  };
1429
1429
 
1430
+ //#endregion
1431
+ //#region src/components/chat/feedback.ts
1432
+ /**
1433
+ * The feedback state a click on `kind` transitions to.
1434
+ *
1435
+ * Clicking the button that is already active retracts the feedback, so the
1436
+ * click deactivates it; every other click applies feedback.
1437
+ */
1438
+ function isActivatingClick(current, kind) {
1439
+ return current !== kind;
1440
+ }
1441
+ /**
1442
+ * Applies a feedback click to the message-id keyed map.
1443
+ *
1444
+ * A deactivating click removes the entry entirely rather than storing a falsy
1445
+ * value, so `messageFeedback[id]` stays absent for "no feedback given".
1446
+ */
1447
+ function applyFeedbackClick(previous, messageId, kind, isActive) {
1448
+ if (!isActive) {
1449
+ if (!(messageId in previous)) return previous;
1450
+ const { [messageId]: _retracted, ...rest } = previous;
1451
+ return rest;
1452
+ }
1453
+ if (previous[messageId] === kind) return previous;
1454
+ return {
1455
+ ...previous,
1456
+ [messageId]: kind
1457
+ };
1458
+ }
1459
+
1430
1460
  //#endregion
1431
1461
  //#region src/components/chat/messages/AssistantMessage.tsx
1432
1462
  const AssistantMessage = (props) => {
@@ -1446,10 +1476,10 @@ const AssistantMessage = (props) => {
1446
1476
  if (onRegenerate) onRegenerate();
1447
1477
  };
1448
1478
  const handleThumbsUp = () => {
1449
- if (onThumbsUp && message) onThumbsUp(message);
1479
+ if (onThumbsUp && message) onThumbsUp(message, isActivatingClick(feedback, "thumbsUp"));
1450
1480
  };
1451
1481
  const handleThumbsDown = () => {
1452
- if (onThumbsDown && message) onThumbsDown(message);
1482
+ if (onThumbsDown && message) onThumbsDown(message, isActivatingClick(feedback, "thumbsDown"));
1453
1483
  };
1454
1484
  const LoadingIcon = () => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
1455
1485
  "data-testid": "copilot-loading-cursor",
@@ -2506,21 +2536,18 @@ function CopilotChat({ instructions, suggestions = "auto", onSubmitMessage, make
2506
2536
  triggerChatError(error, "dropUpload", error);
2507
2537
  }
2508
2538
  };
2509
- const handleThumbsUp = (message) => {
2510
- if (onThumbsUp) onThumbsUp(message);
2511
- setMessageFeedback((prev) => ({
2512
- ...prev,
2513
- [message.id]: "thumbsUp"
2514
- }));
2515
- triggerObservabilityHook("onFeedbackGiven", message.id, "thumbsUp");
2539
+ const recordFeedback = (message, kind, isActive) => {
2540
+ const nextActive = isActive ?? true;
2541
+ setMessageFeedback((prev) => applyFeedbackClick(prev, message.id, kind, nextActive));
2542
+ if (nextActive) triggerObservabilityHook("onFeedbackGiven", message.id, kind);
2543
+ };
2544
+ const handleThumbsUp = (message, isActive) => {
2545
+ onThumbsUp?.(message, isActive ?? true);
2546
+ recordFeedback(message, "thumbsUp", isActive);
2516
2547
  };
2517
- const handleThumbsDown = (message) => {
2518
- if (onThumbsDown) onThumbsDown(message);
2519
- setMessageFeedback((prev) => ({
2520
- ...prev,
2521
- [message.id]: "thumbsDown"
2522
- }));
2523
- triggerObservabilityHook("onFeedbackGiven", message.id, "thumbsDown");
2548
+ const handleThumbsDown = (message, isActive) => {
2549
+ onThumbsDown?.(message, isActive ?? true);
2550
+ recordFeedback(message, "thumbsDown", isActive);
2524
2551
  };
2525
2552
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(WrappedCopilotChat, {
2526
2553
  icons,