🪢 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(prompts): Align prompt variable handling in UI with SDK/compiler (#13680)
Ben Bachemb207a95c0a60
diff --git a/packages/shared/src/utils/stringChecks.ts b/packages/shared/src/utils/stringChecks.tsindex 60130711fe..d1fc8ebd80 100644--- a/packages/shared/src/utils/stringChecks.ts+++ b/packages/shared/src/utils/stringChecks.ts@@ -7,14 +7,15 @@ export function getIsCharOrUnderscore(value: string): boolean {// Regex for valid variable names (unicode letters, underscores, starting with letter)export const VARIABLE_REGEX = /^\p{L}[\p{L}\p{N}_]*$/u;-// Regex to find variables in mustache syntax-export const MUSTACHE_REGEX = /{{([^{}]*)}}+/g;+// Regex to find variables in mustache syntax. Extra surrounding braces are+// treated as literals by SDK/compiler behavior, e.g. {{{name}}} -> {value}.+export const MUSTACHE_REGEX = /{{([^{}]*)}}/g;// Regex to find multiline variables-export const MULTILINE_VARIABLE_REGEX = /{{[^}]*\n[^}]*}}/g;+export const MULTILINE_VARIABLE_REGEX = /{{[^{}]*\n[^{}]*}}/g;// Regex to find unclosed variables-export const UNCLOSED_VARIABLE_REGEX = /{{(?![^{]*}})/g;+export const UNCLOSED_VARIABLE_REGEX = /{{(?!{)(?![^{]*}})/g;export function isValidVariableName(variable: string): boolean {return VARIABLE_REGEX.test(variable);diff --git a/web/src/components/editor/CodeMirrorEditor.clienttest.tsx b/web/src/components/editor/CodeMirrorEditor.clienttest.tsxnew file mode 100644index 0000000000..fd2b2276c5--- /dev/null+++ b/web/src/components/editor/CodeMirrorEditor.clienttest.tsx@@ -0,0 +1,18 @@+import { getPromptVariableDiagnostics } from "@/src/components/editor/CodeMirrorEditor";++describe("getPromptVariableDiagnostics", () => {+ it("accepts triple-brace prompt variables as an inner variable with literal outer braces", () => {+ expect(getPromptVariableDiagnostics("Use {{{placeholder}}} here")).toEqual(+ [],+ );+ });++ it("reports unclosed inner variables inside extra literal braces", () => {+ expect(getPromptVariableDiagnostics("Use {{{placeholder} here")).toEqual([+ expect.objectContaining({+ from: 5,+ message: "Unclosed variable brackets",+ }),+ ]);+ });+});diff --git a/web/src/components/editor/CodeMirrorEditor.tsx b/web/src/components/editor/CodeMirrorEditor.tsxindex bf33a6c939..61daece212 100644--- a/web/src/components/editor/CodeMirrorEditor.tsx+++ b/web/src/components/editor/CodeMirrorEditor.tsx@@ -44,6 +44,12 @@ const promptLanguage = StreamLanguage.define({return "keyword";}+ // Extra surrounding braces are literals; the inner {{variable}} compiles.+ if (stream.match("{{{", false)) {+ stream.next();+ return null;+ }+// Highlight mustache variablesif (stream.match("{{")) {const start = stream.pos;@@ -57,10 +63,8 @@ const promptLanguage = StreamLanguage.define({},});-// Linter for prompt variables-const promptLinter = linter((view) => {+export const getPromptVariableDiagnostics = (content: string): Diagnostic[] => {const diagnostics: Diagnostic[] = [];- const content = view.state.doc.toString();// Check for multiline variablesfor (const match of content.matchAll(MULTILINE_VARIABLE_REGEX)) {@@ -128,7 +132,12 @@ const promptLinter = linter((view) => {}return diagnostics;-});+};++// Linter for prompt variables+const promptLinter = linter((view) =>+ getPromptVariableDiagnostics(view.state.doc.toString()),+);// Create a language support instance that combines the language and its configurationconst promptSupport = new LanguageSupport(promptLanguage);diff --git a/web/src/components/ui/PromptReferences.clienttest.tsx b/web/src/components/ui/PromptReferences.clienttest.tsxnew file mode 100644index 0000000000..ada42241e3--- /dev/null+++ b/web/src/components/ui/PromptReferences.clienttest.tsx@@ -0,0 +1,19 @@+import { render } from "@testing-library/react";+import { extractVariables } from "@langfuse/shared";+import { renderRichPromptContent } from "@/src/components/ui/PromptReferences";++describe("renderRichPromptContent", () => {+ it("preserves triple-brace prompt variable text in rich prompt rendering", () => {+ const { container } = render(+ <>{renderRichPromptContent("Use {{{placeholder}}} here")}</>,+ );++ expect(container.textContent).toBe("Use {{{placeholder}}} here");+ });++ it("extracts the inner variable from triple-brace prompt variables", () => {+ expect(extractVariables("Use {{{placeholder}}} here")).toEqual([+ "placeholder",+ ]);+ });+});diff --git a/web/src/components/ui/PromptReferences.tsx b/web/src/components/ui/PromptReferences.tsxindex fe194cf4a2..c9de5fac9a 100644--- a/web/src/components/ui/PromptReferences.tsx+++ b/web/src/components/ui/PromptReferences.tsx@@ -192,7 +192,7 @@ export const PromptReferenceButton = ({);};-const PromptVar = ({ name, isValid }: { name: string; isValid: boolean }) => (+const PromptVar = ({ text, isValid }: { text: string; isValid: boolean }) => (<spandir="ltr"style={{ unicodeBidi: "isolate" }}@@ -201,7 +201,7 @@ const PromptVar = ({ name, isValid }: { name: string; isValid: boolean }) => ("whitespace-nowrap",)}>- {`{{${name}}}`}+ {text}</span>);@@ -253,7 +253,7 @@ export const renderRichPromptContent = (content: string): React.ReactNode[] => {const variable = match[2];parts.push(<React.Fragment key={`var-${index}-${variable}`}>- <PromptVar name={variable} isValid={isValidVariableName(variable)} />+ <PromptVar text={fullMatch} isValid={isValidVariableName(variable)} /></React.Fragment>,);}