Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat: highlight variables in prompt content (#8632)

Leo Weigande45c24838c91
diff --git a/web/src/components/ui/CodeJsonViewer.tsx b/web/src/components/ui/CodeJsonViewer.tsx
index c734cf76ef..245b4f6251 100644
--- a/web/src/components/ui/CodeJsonViewer.tsx
+++ b/web/src/components/ui/CodeJsonViewer.tsx
@@ -19,7 +19,7 @@ import { useMarkdownContext } from "@/src/features/theming/useMarkdownContext";
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 { renderRichPromptContent } from "@/src/features/prompts/components/prompt-content-utils";
import { copyTextToClipboard } from "@/src/utils/clipboard";
export const IO_TABLE_CHAR_LIMIT = 10000;
@@ -101,7 +101,7 @@ export function JSONView(props: {
<Skeleton className="h-3 w-3/4" />
) : props.projectIdForPromptButtons ? (
<code className="whitespace-pre-wrap break-words">
- {renderContentWithPromptButtons(
+ {renderRichPromptContent(
props.projectIdForPromptButtons,
String(parsedJson),
)}
diff --git a/web/src/features/prompts/components/prompt-content-utils.tsx b/web/src/features/prompts/components/prompt-content-utils.tsx
new file mode 100644
index 0000000000..ebe59da580
--- /dev/null
+++ b/web/src/features/prompts/components/prompt-content-utils.tsx
@@ -0,0 +1,152 @@
+import React from "react";
+import { cn } from "@/src/utils/tailwind";
+import {
+ MUSTACHE_REGEX,
+ isValidVariableName,
+ PromptDependencyRegex,
+ type ParsedPromptDependencyTag,
+} from "@langfuse/shared";
+import { Badge } from "@/src/components/ui/badge";
+import { Button } from "@/src/components/ui/button";
+import { FileCode } from "lucide-react";
+
+const PromptVar = ({ name, isValid }: { name: string; isValid: boolean }) => (
+ <span className={cn(isValid ? "text-primary-accent" : "text-destructive")}>
+ {`{{${name}}}`}
+ </span>
+);
+
+type PromptReference = ParsedPromptDependencyTag & {
+ position: number;
+};
+
+const PromptReference = ({
+ promptRef,
+ projectId,
+}: {
+ promptRef: PromptReference;
+ projectId: string;
+}) => {
+ const getPromptUrl = (projectId: string, tag: PromptReference) => {
+ const baseUrl = `/project/${projectId}/prompts/`;
+ if (tag.type === "version") {
+ return `${baseUrl}${encodeURIComponent(tag.name)}?version=${tag.version}`;
+ } else {
+ return `${baseUrl}${encodeURIComponent(tag.name)}?label=${encodeURIComponent(tag.label)}`;
+ }
+ };
+
+ return (
+ <Button
+ variant="outline"
+ size="sm"
+ className="inline-flex items-center gap-1.5 rounded-sm border-dashed bg-muted/50 px-2 py-0.5 align-[-3px] text-xs font-medium transition-colors hover:bg-muted"
+ onClick={() => window.open(getPromptUrl(projectId, promptRef), "_blank")}
+ title={`Open prompt: ${promptRef.name}${promptRef.type === "version" ? ` (v${promptRef.version})` : promptRef.label ? ` (${promptRef.label})` : ""}`}
+ >
+ <FileCode className="h-3 w-3 flex-shrink-0 text-muted-foreground" />
+ <span className="truncate font-medium">
+ {promptRef.name}
+ {promptRef.type === "version" ? (
+ <Badge variant="outline" className="ml-1 px-1 py-0 text-[10px]">
+ v{promptRef.version}
+ </Badge>
+ ) : promptRef.label ? (
+ <Badge variant="outline" className="ml-1 px-1 py-0 text-[10px]">
+ {promptRef.label}
+ </Badge>
+ ) : (
+ ""
+ )}
+ </span>
+ </Button>
+ );
+};
+
+// Higher-level function that renders prompt content with all the rich formatting
+export const renderRichPromptContent = (
+ projectId: string,
+ content: string,
+): React.ReactNode[] => {
+ if (!content) return [];
+
+ const parts: React.ReactNode[] = [];
+ let lastIndex = 0;
+
+ // Create combined regex that captures both patterns in a single pass
+ // Group 1: prompt dependency inner content, Group 2: variable name
+ const combinedRegex = new RegExp(
+ `${PromptDependencyRegex.source}|${MUSTACHE_REGEX.source}`,
+ "g",
+ );
+
+ let match;
+ while ((match = combinedRegex.exec(content)) !== null) {
+ const index = match.index ?? 0;
+ const fullMatch = match[0];
+
+ // Add any text before this match
+ if (index > lastIndex) {
+ const textBefore = content.substring(lastIndex, index);
+ if (textBefore) parts.push(textBefore);
+ }
+
+ // Determine type based on which capture group matched and process directly
+ if (match[1] !== undefined) {
+ // First capture group = prompt dependency
+ const innerContent = match[1];
+ const tagParts = innerContent.split("|");
+ const params: Record<string, string> = {};
+
+ tagParts.forEach((part) => {
+ const [key, value] = part.split("=");
+ if (key && value) {
+ params[key] = value;
+ }
+ });
+
+ if (params.name) {
+ const tag: PromptReference = params.version
+ ? {
+ name: params.name,
+ type: "version",
+ version: Number(params.version),
+ position: index,
+ }
+ : {
+ name: params.name,
+ type: "label",
+ label: params.label || "",
+ position: index,
+ };
+
+ parts.push(
+ <React.Fragment key={`prompt-${index}`}>
+ <PromptReference promptRef={tag} projectId={projectId} />
+ </React.Fragment>,
+ );
+ } else {
+ parts.push(fullMatch);
+ }
+ } else if (match[2] !== undefined) {
+ // Second capture group = variable
+ const variable = match[2];
+ const isValid = isValidVariableName(variable);
+ parts.push(
+ <React.Fragment key={`var-${index}-${variable}`}>
+ <PromptVar name={variable} isValid={isValid} />
+ </React.Fragment>,
+ );
+ }
+
+ lastIndex = index + fullMatch.length;
+ }
+
+ // Add any remaining text
+ if (lastIndex < content.length) {
+ const remainingText = content.substring(lastIndex);
+ if (remainingText) parts.push(remainingText);
+ }
+
+ return parts;
+};
diff --git a/web/src/features/prompts/components/prompt-detail.tsx b/web/src/features/prompts/components/prompt-detail.tsx
index 4fe5ac7d81..7b1deccfeb 100644
--- a/web/src/features/prompts/components/prompt-detail.tsx
+++ b/web/src/features/prompts/components/prompt-detail.tsx
@@ -58,7 +58,7 @@ import { TagPromptDetailsPopover } from "@/src/features/tag/components/TagPrompt
import { SetPromptVersionLabels } from "@/src/features/prompts/components/SetPromptVersionLabels";
import { CommentDrawerButton } from "@/src/features/comments/CommentDrawerButton";
import { Command, CommandInput } from "@/src/components/ui/command";
-import { renderContentWithPromptButtons } from "@/src/features/prompts/components/renderContentWithPromptButtons";
+import { renderRichPromptContent } from "@/src/features/prompts/components/prompt-content-utils";
import { PromptVariableListPreview } from "@/src/features/prompts/components/PromptVariableListPreview";
const getPythonCode = (
@@ -532,7 +532,7 @@ export const PromptDetail = ({
/>
) : (
<CodeView
- content={renderContentWithPromptButtons(
+ content={renderRichPromptContent(
projectId as string,
prompt.prompt,
)}
diff --git a/web/src/features/prompts/components/renderContentWithPromptButtons.tsx b/web/src/features/prompts/components/renderContentWithPromptButtons.tsx
deleted file mode 100644
index bf0a83c223..0000000000
--- a/web/src/features/prompts/components/renderContentWithPromptButtons.tsx
+++ /dev/null
@@ -1,96 +0,0 @@
-import { Badge } from "@/src/components/ui/badge";
-
-import {
- PromptDependencyRegex,
- type ParsedPromptDependencyTag,
-} from "@langfuse/shared";
-
-import { FileCode } from "lucide-react";
-import { Button } from "@/src/components/ui/button";
-
-const getPromptUrl = (projectId: string, tag: ParsedPromptDependencyTag) => {
- const baseUrl = `/project/${projectId}/prompts/`;
- if (tag.type === "version") {
- return `${baseUrl}${encodeURIComponent(tag.name)}?version=${tag.version}`;
- } else {
- return `${baseUrl}${encodeURIComponent(tag.name)}?label=${encodeURIComponent(tag.label)}`;
- }
-};
-
-export const renderContentWithPromptButtons = (
- projectId: string,
- content: string,
-) => {
- if (!content) return null;
-
- const regex = PromptDependencyRegex;
- const parts: React.ReactNode[] = [];
-
- let lastIndex = 0;
- let match;
-
- while ((match = regex.exec(content)) !== null) {
- if (match.index > lastIndex) {
- parts.push(content.substring(lastIndex, match.index));
- }
-
- const tagContent = match[0];
- const innerContent = match[1];
- const tagParts = innerContent.split("|");
- const params: Record<string, string> = {};
-
- tagParts.forEach((part) => {
- const [key, value] = part.split("=");
- if (key && value) {
- params[key] = value;
- }
- });
-
- if (params.name) {
- const tag: ParsedPromptDependencyTag = params.version
- ? {
- name: params.name,
- type: "version",
- version: Number(params.version),
- }
- : { name: params.name, type: "label", label: params.label || "" };
-
- parts.push(
- <Button
- key={`${tag.name}-${match.index}`}
- variant="outline"
- size="sm"
- className="inline-flex items-center gap-1.5 rounded-sm border-dashed bg-muted/50 px-2 py-0.5 text-xs font-medium transition-colors hover:bg-muted"
- onClick={() => window.open(getPromptUrl(projectId, tag), "_blank")}
- title={`Open prompt: ${tag.name}${tag.type === "version" ? ` (v${tag.version})` : tag.label ? ` (${tag.label})` : ""}`}
- >
- <FileCode className="h-3 w-3 flex-shrink-0 text-muted-foreground" />
- <span className="truncate font-medium">
- {tag.name}
- {tag.type === "version" ? (
- <Badge variant="outline" className="ml-1 px-1 py-0 text-[10px]">
- v{tag.version}
- </Badge>
- ) : tag.label ? (
- <Badge variant="outline" className="ml-1 px-1 py-0 text-[10px]">
- {tag.label}
- </Badge>
- ) : (
- ""
- )}
- </span>
- </Button>,
- );
- } else {
- parts.push(tagContent);
- }
-
- lastIndex = match.index + match[0].length;
- }
-
- if (lastIndex < content.length) {
- parts.push(content.substring(lastIndex));
- }
-
- return parts;
-};