🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(ui): enable clipboard copying for self-hosted instances (#7227)
marliessophieb7362258c65f
diff --git a/web/src/components/publish-object-switch.tsx b/web/src/components/publish-object-switch.tsxindex dbcdb80de6..243ebe6d07 100644--- a/web/src/components/publish-object-switch.tsx+++ b/web/src/components/publish-object-switch.tsx@@ -8,6 +8,7 @@ import {import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";import { api } from "@/src/utils/api";+import { copyTextToClipboard } from "@/src/utils/clipboard";import { CheckIcon, Globe, Link, Share2 } from "lucide-react";import { useState } from "react";@@ -96,7 +97,7 @@ const Base = (props: {const copyUrl = () => {setIsCopied(true);- void navigator.clipboard.writeText(window.location.href);+ void copyTextToClipboard(window.location.href);setTimeout(() => setIsCopied(false), 2500);};diff --git a/web/src/components/table/table-view-presets/hooks/useViewMutations.ts b/web/src/components/table/table-view-presets/hooks/useViewMutations.tsindex 572207e6a6..17d2afcf6c 100644--- a/web/src/components/table/table-view-presets/hooks/useViewMutations.ts+++ b/web/src/components/table/table-view-presets/hooks/useViewMutations.ts@@ -1,5 +1,6 @@import { showSuccessToast } from "@/src/features/notifications/showSuccessToast";import { api } from "@/src/utils/api";+import { copyTextToClipboard } from "@/src/utils/clipboard";type UseViewMutationsProps = {handleSetViewId: (viewId: string | null) => void;@@ -42,7 +43,7 @@ export const useViewMutations = ({const generatePermalinkMutation =api.TableViewPresets.generatePermalink.useMutation({onSuccess: (data) => {- navigator.clipboard.writeText(data);+ copyTextToClipboard(data);showSuccessToast({title: "Permalink copied to clipboard",description: "You can now share the permalink with others",diff --git a/web/src/components/trace/CopyIdsPopover.tsx b/web/src/components/trace/CopyIdsPopover.tsxindex caef7d7bfb..7e3b9ffcd8 100644--- a/web/src/components/trace/CopyIdsPopover.tsx+++ b/web/src/components/trace/CopyIdsPopover.tsx@@ -8,6 +8,7 @@ import {} from "@/src/components/ui/popover";import { cn } from "@/src/utils/tailwind";import { Label } from "@/src/components/ui/label";+import { copyTextToClipboard } from "@/src/utils/clipboard";interface IdItem {name: string;@@ -24,7 +25,7 @@ export const CopyIdsPopover = ({const [copiedId, setCopiedId] = useState<string | null>(null);const handleCopy = (textToCopy: string) => {- navigator.clipboard.writeText(textToCopy);+ copyTextToClipboard(textToCopy);setCopiedId(textToCopy);setTimeout(() => setCopiedId(null), 1500);};diff --git a/web/src/components/ui/CodeJsonViewer.tsx b/web/src/components/ui/CodeJsonViewer.tsxindex ca6de1c43b..122aa14a73 100644--- a/web/src/components/ui/CodeJsonViewer.tsx+++ b/web/src/components/ui/CodeJsonViewer.tsx@@ -13,6 +13,7 @@ import { type MediaReturnType } from "@/src/features/media/validation";import { LangfuseMediaView } from "@/src/components/ui/LangfuseMediaView";import { MarkdownJsonViewHeader } from "@/src/components/ui/MarkdownJsonView";import { renderContentWithPromptButtons } from "@/src/features/prompts/components/renderContentWithPromptButtons";+import { copyTextToClipboard } from "@/src/utils/clipboard";const IO_TABLE_CHAR_LIMIT = 10000;@@ -41,7 +42,8 @@ export function JSONView(props: {: (props.collapseStringsAfterLength ?? 500);const handleOnCopy = () => {- void navigator.clipboard.writeText(stringifyJsonNode(parsedJson));+ const textToCopy = stringifyJsonNode(parsedJson);+ void copyTextToClipboard(textToCopy);};const handleOnValueChange = () => {@@ -158,11 +160,11 @@ export function CodeView(props: {const handleCopy = () => {setIsCopied(true);- void navigator.clipboard.writeText(+ const content =typeof props.content === "string"? props.content- : (props.content?.join("\n") ?? ""),- );+ : (props.content?.join("\n") ?? "");+ void copyTextToClipboard(content);setTimeout(() => setIsCopied(false), 1000);};diff --git a/web/src/components/ui/Codeblock.tsx b/web/src/components/ui/Codeblock.tsxindex 74f29fc84e..77e101a5cb 100644--- a/web/src/components/ui/Codeblock.tsx+++ b/web/src/components/ui/Codeblock.tsx@@ -1,4 +1,5 @@import { Button } from "@/src/components/ui/button";+import { copyTextToClipboard } from "@/src/utils/clipboard";import { cn } from "@/src/utils/tailwind";import { Check, Copy } from "lucide-react";import { type FC, memo, useState } from "react";@@ -50,7 +51,7 @@ const CodeBlock: FC<Props> = memo(({ language, value, theme, className }) => {const [isCopied, setIsCopied] = useState(false);const handleCopy = () => {setIsCopied(true);- void navigator.clipboard.writeText(value ?? "");+ void copyTextToClipboard(value ?? "");setTimeout(() => setIsCopied(false), 1000);};diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsxindex b3fc0548a6..3731221f5b 100644--- a/web/src/components/ui/MarkdownViewer.tsx+++ b/web/src/components/ui/MarkdownViewer.tsx@@ -33,6 +33,7 @@ import { LangfuseMediaView } from "@/src/components/ui/LangfuseMediaView";import { type MediaReturnType } from "@/src/features/media/validation";import { JSONView } from "@/src/components/ui/CodeJsonViewer";import { MarkdownJsonViewHeader } from "@/src/components/ui/MarkdownJsonView";+import { copyTextToClipboard } from "@/src/utils/clipboard";type ReactMarkdownNode = ReactMarkdownExtraProps["node"];type ReactMarkdownNodeChildren = Exclude<@@ -271,7 +272,7 @@ export function MarkdownView({typeof markdown === "string"? markdown: parseOpenAIContentParts(markdown);- void navigator.clipboard.writeText(rawText);+ void copyTextToClipboard(rawText);};const handleOnValueChange = () => {diff --git a/web/src/features/playground/page/components/GenerationOutput.tsx b/web/src/features/playground/page/components/GenerationOutput.tsxindex 27d23647a9..3c11522e25 100644--- a/web/src/features/playground/page/components/GenerationOutput.tsx+++ b/web/src/features/playground/page/components/GenerationOutput.tsx@@ -5,6 +5,7 @@ import { CheckIcon, CopyIcon, PlusIcon } from "@radix-ui/react-icons";import { ChatMessageRole, ChatMessageType } from "@langfuse/shared";import { BracesIcon } from "lucide-react";import { ToolCallCard } from "@/src/components/ChatMessages/ToolCallCard";+import { copyTextToClipboard } from "@/src/utils/clipboard";export const GenerationOutput = () => {const [isCopied, setIsCopied] = useState(false);@@ -16,7 +17,8 @@ export const GenerationOutput = () => {const handleCopy = () => {setIsCopied(true);- void navigator.clipboard.writeText(isJson ? outputJson : output);+ const textToCopy = isJson ? outputJson : output;+ void copyTextToClipboard(textToCopy);setTimeout(() => setIsCopied(false), 1000);};diff --git a/web/src/features/prompts/components/PromptSelectionDialog.tsx b/web/src/features/prompts/components/PromptSelectionDialog.tsxindex e4c7640cd8..ceabf510d3 100644--- a/web/src/features/prompts/components/PromptSelectionDialog.tsx+++ b/web/src/features/prompts/components/PromptSelectionDialog.tsx@@ -19,6 +19,7 @@ import {import { Label } from "@/src/components/ui/label";import { api } from "@/src/utils/api";import { CopyIcon, ExternalLinkIcon } from "lucide-react";+import { copyTextToClipboard } from "@/src/utils/clipboard";type PromptSelectionDialogProps = {isOpen: boolean;@@ -42,7 +43,7 @@ export function PromptSelectionDialog({useState<string>("");const copySelectedTag = useCallback(() => {- navigator.clipboard.writeText(selectedTag);+ copyTextToClipboard(selectedTag);}, [selectedTag]);useEffect(() => {diff --git a/web/src/utils/clipboard.ts b/web/src/utils/clipboard.tsnew file mode 100644index 0000000000..6a96c7f77e--- /dev/null+++ b/web/src/utils/clipboard.ts@@ -0,0 +1,45 @@+/**+ * Clipboard Copy Fallback+ *+ * This fallback implementation enables copying text to clipboard when the secure Clipboard API+ * isn't available. This typically occurs when the page isn't served over HTTPS (TLS).+ *+ * Examples of affected scenarios:+ * - Self-hosted deployments without configured HTTPS certificates.+ * - Websites served over plain HTTP (except localhost and local IP addresses).+ *+ * Note:+ * - Local resources like http://localhost, http://127.0.0.1, and http://*.localhost are NOT affected.+ *+ * Important:+ * - This fallback method uses `execCommand`, which is deprecated.+ * - It will stop working once browsers fully remove support for `execCommand`.+ */+const _unsafeNonSecureCopyToClipboard = (text: string) => {+ try {+ const textArea = document.createElement("textarea");+ textArea.value = text;+ textArea.style.position = "fixed";+ textArea.style.left = "-9999px";+ document.body.appendChild(textArea);+ textArea.focus();+ textArea.select();+ document.execCommand("copy");+ document.body.removeChild(textArea);+ } catch (error) {+ console.error("Unable to copy to clipboard", error);+ }+};++/**+ * Copy text to clipboard using clipboard api or fallback to _unsafeNonSecureCopyToClipboard for non secure contexts+ *+ * @param text - Text to copy to clipboard+ * @returns Promise<void>+ */+export const copyTextToClipboard = async (text: string) => {+ if (typeof navigator.clipboard?.writeText === "function") {+ return navigator.clipboard.writeText(text);+ }+ return _unsafeNonSecureCopyToClipboard(text);+};