🪢 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): handle placeholder messages in diff view (#7726)
Nimarb0aeb8c45472
diff --git a/web/src/features/prompts/components/NewPromptForm/ReviewPromptDialog.tsx b/web/src/features/prompts/components/NewPromptForm/ReviewPromptDialog.tsxindex 9ba33c5ebc..118aae3512 100644--- a/web/src/features/prompts/components/NewPromptForm/ReviewPromptDialog.tsx+++ b/web/src/features/prompts/components/NewPromptForm/ReviewPromptDialog.tsx@@ -22,6 +22,20 @@ type ReviewPromptDialogProps = {getNewPromptValues: () => NewPromptFormSchemaType;};+const formatMessages = (messages: any[], excludeKeys: string[] = []) => {+ return JSON.stringify(+ messages.map(m =>+ Object.fromEntries(+ Object.entries(m)+ .filter(([k]) => !excludeKeys.includes(k) && (k !== "type" || m.type === "placeholder"))+ .sort(([a], [b]) => a.localeCompare(b))+ )+ ),+ null,+ 2+ );+};+export const ReviewPromptDialog: React.FC<ReviewPromptDialogProps> = (props,) => {@@ -37,23 +51,13 @@ export const ReviewPromptDialog: React.FC<ReviewPromptDialogProps> = (}}, [open, setNewPromptValues, getNewPromptValues]);- const initialPromptContent: string =- initialPrompt.type === "text"- ? (initialPrompt.prompt as string)- : JSON.stringify(initialPrompt.prompt, null, 2);-- const newPromptContent: string =- initialPrompt.type === "text"- ? (newPromptValue?.textPrompt ?? "")- : JSON.stringify(- newPromptValue?.chatPrompt.map((m) =>- Object.fromEntries(- Object.entries(m).filter(([k, _]) => k !== "id" && k !== "type"),- ),- ) ?? "{}",- null,- 2,- );+ const initialPromptContent = initialPrompt.type === "text"+ ? (initialPrompt.prompt as string)+ : formatMessages(initialPrompt.prompt as any[]);++ const newPromptContent = initialPrompt.type === "text"+ ? (newPromptValue?.textPrompt ?? "")+ : formatMessages(newPromptValue?.chatPrompt ?? [], ["id"]);const newConfig = JSON.stringify(JSON.parse(newPromptValue?.config ?? "{}"),diff --git a/web/src/features/prompts/components/PromptVersionDiffDialog.tsx b/web/src/features/prompts/components/PromptVersionDiffDialog.tsxindex 697ade9bc6..a49353b2d7 100644--- a/web/src/features/prompts/components/PromptVersionDiffDialog.tsx+++ b/web/src/features/prompts/components/PromptVersionDiffDialog.tsx@@ -21,6 +21,32 @@ type PromptVersionDiffDialogProps = {rightPrompt: Prompt;};+// Create a word-based diff that preserves JSON structure+const createSmartDiff = (oldPrompt: Prompt, newPrompt: Prompt): { oldString: string; newString: string } => {+ if (oldPrompt.type === "text" || newPrompt.type === "text") {+ return {+ oldString: oldPrompt.type === "text" ? (oldPrompt.prompt as string) : JSON.stringify(oldPrompt.prompt, null, 2),+ newString: newPrompt.type === "text" ? (newPrompt.prompt as string) : JSON.stringify(newPrompt.prompt, null, 2)+ };+ }++ const formatMessages = (messages: any[]) =>+ JSON.stringify(+ messages.map(m =>+ Object.fromEntries(+ Object.entries(m).sort(([a], [b]) => a.localeCompare(b))+ )+ ),+ null,+ 2+ );++ return {+ oldString: formatMessages(oldPrompt.prompt as any[]),+ newString: formatMessages(newPrompt.prompt as any[])+ };+};+export const PromptVersionDiffDialog: React.FC<PromptVersionDiffDialogProps> = (props,) => {@@ -72,16 +98,7 @@ export const PromptVersionDiffDialog: React.FC<PromptVersionDiffDialogProps> = (<div><h3 className="mb-2 text-base font-medium">Content</h3><DiffViewer- oldString={- leftPrompt.type === "chat"- ? JSON.stringify(leftPrompt.prompt, null, 2)- : (leftPrompt.prompt as string)- }- newString={- rightPrompt.type === "chat"- ? JSON.stringify(rightPrompt.prompt, null, 2)- : (rightPrompt.prompt as string)- }+ {...createSmartDiff(leftPrompt, rightPrompt)}oldLabel={`v${leftPrompt.version}`}newLabel={`v${rightPrompt.version}`}oldSubLabel={leftPrompt.commitMessage ?? undefined}