184 lines
7.9 KiB
JavaScript
184 lines
7.9 KiB
JavaScript
import { CopilotChatDefaultLabels, useCopilotChatConfiguration } from "../../providers/CopilotChatConfigurationProvider.mjs";
|
|
import { Button } from "../ui/button.mjs";
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "../ui/tooltip.mjs";
|
|
import { renderSlot } from "../../lib/slots.mjs";
|
|
import { useMemo, useState } from "react";
|
|
import { twMerge } from "tailwind-merge";
|
|
import { Check, ChevronLeft, ChevronRight, Copy, Edit } from "lucide-react";
|
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
|
|
//#region src/components/chat/CopilotChatUserMessage.tsx
|
|
function flattenUserMessageContent(content) {
|
|
if (!content) return "";
|
|
if (typeof content === "string") return content;
|
|
return content.map((part) => {
|
|
if (part && typeof part === "object" && "type" in part && part.type === "text" && typeof part.text === "string") return part.text;
|
|
return "";
|
|
}).filter((text) => text.length > 0).join("\n");
|
|
}
|
|
function CopilotChatUserMessage({ message, onEditMessage, branchIndex, numberOfBranches, onSwitchToBranch, additionalToolbarItems, messageRenderer, toolbar, copyButton, editButton, branchNavigation, children, className, ...props }) {
|
|
const flattenedContent = useMemo(() => flattenUserMessageContent(message.content), [message.content]);
|
|
const BoundMessageRenderer = renderSlot(messageRenderer, CopilotChatUserMessage.MessageRenderer, { content: flattenedContent });
|
|
const BoundCopyButton = renderSlot(copyButton, CopilotChatUserMessage.CopyButton, { onClick: async () => {
|
|
if (flattenedContent) try {
|
|
await navigator.clipboard.writeText(flattenedContent);
|
|
} catch (err) {
|
|
console.error("Failed to copy message:", err);
|
|
}
|
|
} });
|
|
const BoundEditButton = renderSlot(editButton, CopilotChatUserMessage.EditButton, { onClick: () => onEditMessage?.({ message }) });
|
|
const BoundBranchNavigation = renderSlot(branchNavigation, CopilotChatUserMessage.BranchNavigation, {
|
|
currentBranch: branchIndex,
|
|
numberOfBranches,
|
|
onSwitchToBranch,
|
|
message
|
|
});
|
|
const showBranchNavigation = numberOfBranches && numberOfBranches > 1 && onSwitchToBranch;
|
|
const BoundToolbar = renderSlot(toolbar, CopilotChatUserMessage.Toolbar, { children: /* @__PURE__ */ jsxs("div", {
|
|
className: "cpk:flex cpk:items-center cpk:gap-1 cpk:justify-end",
|
|
children: [
|
|
additionalToolbarItems,
|
|
BoundCopyButton,
|
|
onEditMessage && BoundEditButton,
|
|
showBranchNavigation && BoundBranchNavigation
|
|
]
|
|
}) });
|
|
if (children) return /* @__PURE__ */ jsx("div", {
|
|
"data-copilotkit": true,
|
|
style: { display: "contents" },
|
|
children: children({
|
|
messageRenderer: BoundMessageRenderer,
|
|
toolbar: BoundToolbar,
|
|
copyButton: BoundCopyButton,
|
|
editButton: BoundEditButton,
|
|
branchNavigation: BoundBranchNavigation,
|
|
message,
|
|
branchIndex,
|
|
numberOfBranches,
|
|
additionalToolbarItems
|
|
})
|
|
});
|
|
return /* @__PURE__ */ jsxs("div", {
|
|
"data-copilotkit": true,
|
|
"data-testid": "copilot-user-message",
|
|
className: twMerge("copilotKitMessage copilotKitUserMessage cpk:flex cpk:flex-col cpk:items-end cpk:group cpk:pt-10", className),
|
|
"data-message-id": message.id,
|
|
...props,
|
|
children: [BoundMessageRenderer, BoundToolbar]
|
|
});
|
|
}
|
|
(function(_CopilotChatUserMessage) {
|
|
_CopilotChatUserMessage.Container = ({ children, className, ...props }) => /* @__PURE__ */ jsx("div", {
|
|
className: twMerge("cpk:flex cpk:flex-col cpk:items-end cpk:group", className),
|
|
...props,
|
|
children
|
|
});
|
|
_CopilotChatUserMessage.MessageRenderer = ({ content, className }) => /* @__PURE__ */ jsx("div", {
|
|
className: twMerge("cpk:prose cpk:dark:prose-invert cpk:bg-muted cpk:relative cpk:max-w-[80%] cpk:rounded-[18px] cpk:px-4 cpk:py-1.5 cpk:data-[multiline]:py-3 cpk:inline-block cpk:whitespace-pre-wrap", className),
|
|
children: content
|
|
});
|
|
_CopilotChatUserMessage.Toolbar = ({ className, ...props }) => /* @__PURE__ */ jsx("div", {
|
|
"data-testid": "copilot-user-toolbar",
|
|
className: twMerge("cpk:w-full cpk:bg-transparent cpk:flex cpk:items-center cpk:justify-end cpk:-mr-[5px] cpk:mt-[4px] cpk:invisible cpk:group-hover:visible", className),
|
|
...props
|
|
});
|
|
const ToolbarButton = _CopilotChatUserMessage.ToolbarButton = ({ title, children, className, ...props }) => {
|
|
return /* @__PURE__ */ jsxs(Tooltip, { children: [/* @__PURE__ */ jsx(TooltipTrigger, {
|
|
asChild: true,
|
|
children: /* @__PURE__ */ jsx(Button, {
|
|
type: "button",
|
|
variant: "assistantMessageToolbarButton",
|
|
"aria-label": title,
|
|
className: twMerge(className),
|
|
...props,
|
|
children
|
|
})
|
|
}), /* @__PURE__ */ jsx(TooltipContent, {
|
|
side: "bottom",
|
|
children: /* @__PURE__ */ jsx("p", { children: title })
|
|
})] });
|
|
};
|
|
_CopilotChatUserMessage.CopyButton = ({ className, title, onClick, ...props }) => {
|
|
const labels = useCopilotChatConfiguration()?.labels ?? CopilotChatDefaultLabels;
|
|
const [copied, setCopied] = useState(false);
|
|
const handleClick = (event) => {
|
|
setCopied(true);
|
|
setTimeout(() => setCopied(false), 2e3);
|
|
if (onClick) onClick(event);
|
|
};
|
|
return /* @__PURE__ */ jsx(ToolbarButton, {
|
|
"data-testid": "copilot-user-copy-button",
|
|
title: title || labels.userMessageToolbarCopyMessageLabel,
|
|
onClick: handleClick,
|
|
className,
|
|
...props,
|
|
children: copied ? /* @__PURE__ */ jsx(Check, { className: "cpk:size-[18px]" }) : /* @__PURE__ */ jsx(Copy, { className: "cpk:size-[18px]" })
|
|
});
|
|
};
|
|
_CopilotChatUserMessage.EditButton = ({ className, title, ...props }) => {
|
|
const labels = useCopilotChatConfiguration()?.labels ?? CopilotChatDefaultLabels;
|
|
return /* @__PURE__ */ jsx(ToolbarButton, {
|
|
"data-testid": "copilot-edit-button",
|
|
title: title || labels.userMessageToolbarEditMessageLabel,
|
|
className,
|
|
...props,
|
|
children: /* @__PURE__ */ jsx(Edit, { className: "cpk:size-[18px]" })
|
|
});
|
|
};
|
|
_CopilotChatUserMessage.BranchNavigation = ({ className, currentBranch = 0, numberOfBranches = 1, onSwitchToBranch, message, ...props }) => {
|
|
if (!numberOfBranches || numberOfBranches <= 1 || !onSwitchToBranch) return null;
|
|
const canGoPrev = currentBranch > 0;
|
|
const canGoNext = currentBranch < numberOfBranches - 1;
|
|
return /* @__PURE__ */ jsxs("div", {
|
|
"data-testid": "copilot-branch-navigation",
|
|
className: twMerge("cpk:flex cpk:items-center cpk:gap-1", className),
|
|
...props,
|
|
children: [
|
|
/* @__PURE__ */ jsx(Button, {
|
|
type: "button",
|
|
variant: "assistantMessageToolbarButton",
|
|
onClick: () => onSwitchToBranch?.({
|
|
branchIndex: currentBranch - 1,
|
|
numberOfBranches,
|
|
message
|
|
}),
|
|
disabled: !canGoPrev,
|
|
className: "cpk:h-6 cpk:w-6 cpk:p-0",
|
|
children: /* @__PURE__ */ jsx(ChevronLeft, { className: "cpk:size-[20px]" })
|
|
}),
|
|
/* @__PURE__ */ jsxs("span", {
|
|
className: "cpk:text-sm cpk:text-muted-foreground cpk:px-0 cpk:font-medium",
|
|
children: [
|
|
currentBranch + 1,
|
|
"/",
|
|
numberOfBranches
|
|
]
|
|
}),
|
|
/* @__PURE__ */ jsx(Button, {
|
|
type: "button",
|
|
variant: "assistantMessageToolbarButton",
|
|
onClick: () => onSwitchToBranch?.({
|
|
branchIndex: currentBranch + 1,
|
|
numberOfBranches,
|
|
message
|
|
}),
|
|
disabled: !canGoNext,
|
|
className: "cpk:h-6 cpk:w-6 cpk:p-0",
|
|
children: /* @__PURE__ */ jsx(ChevronRight, { className: "cpk:size-[20px]" })
|
|
})
|
|
]
|
|
});
|
|
};
|
|
})(CopilotChatUserMessage || (CopilotChatUserMessage = {}));
|
|
CopilotChatUserMessage.Container.displayName = "CopilotChatUserMessage.Container";
|
|
CopilotChatUserMessage.MessageRenderer.displayName = "CopilotChatUserMessage.MessageRenderer";
|
|
CopilotChatUserMessage.Toolbar.displayName = "CopilotChatUserMessage.Toolbar";
|
|
CopilotChatUserMessage.ToolbarButton.displayName = "CopilotChatUserMessage.ToolbarButton";
|
|
CopilotChatUserMessage.CopyButton.displayName = "CopilotChatUserMessage.CopyButton";
|
|
CopilotChatUserMessage.EditButton.displayName = "CopilotChatUserMessage.EditButton";
|
|
CopilotChatUserMessage.BranchNavigation.displayName = "CopilotChatUserMessage.BranchNavigation";
|
|
var CopilotChatUserMessage_default = CopilotChatUserMessage;
|
|
|
|
//#endregion
|
|
export { CopilotChatUserMessage_default as default };
|
|
//# sourceMappingURL=CopilotChatUserMessage.mjs.map
|