🪢 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
perf(playground): memoizate ChatMessages (#5937)
Hassieb Pakzad61c2ec268ab9
diff --git a/web/src/components/ChatMessages/ChatMessageComponent.tsx b/web/src/components/ChatMessages/ChatMessageComponent.tsxindex 7b6897271e..2b0942c6d5 100644--- a/web/src/components/ChatMessages/ChatMessageComponent.tsx+++ b/web/src/components/ChatMessages/ChatMessageComponent.tsx@@ -1,6 +1,6 @@import { capitalize } from "lodash";import { GripVertical, MinusCircleIcon } from "lucide-react";-import { useState } from "react";+import { memo, useState, useCallback } from "react";import {ChatMessageRole,SYSTEM_ROLES,@@ -86,7 +86,10 @@ export const ChatMessageComponent: React.FC<ChatMessageProps> = ({}};- const placeholder = `Enter ${getRoleNamePlaceholder(message.role)} message here.`;+ const onValueChange = useCallback(+ (value: string) => updateMessage(message.id, "content", value),+ [message.id, updateMessage],+ );return (<Card@@ -120,15 +123,10 @@ export const ChatMessageComponent: React.FC<ChatMessageProps> = ({{capitalize(message.role)}</Button></div>- <CodeMirrorEditor+ <MemoizedEditorvalue={message.content}- onChange={(value) => updateMessage(message.id, "content", value)}- mode="prompt"- minHeight={30}- className="w-full"- editable={true}- lineNumbers={false}- placeholder={placeholder}+ onChange={onValueChange}+ role={message.role}/><Buttonvariant="ghost"@@ -142,3 +140,25 @@ export const ChatMessageComponent: React.FC<ChatMessageProps> = ({</Card>);};++const MemoizedEditor = memo(function MemoizedEditor(props: {+ value: string;+ role: (typeof ROLES)[1];+ onChange: (value: string) => void;+}) {+ const { value, role, onChange } = props;+ const placeholder = `Enter ${getRoleNamePlaceholder(role)} message here.`;++ return (+ <CodeMirrorEditor+ value={value}+ onChange={onChange}+ mode="prompt"+ minHeight={30}+ className="w-full"+ editable={true}+ lineNumbers={false}+ placeholder={placeholder}+ />+ );+});diff --git a/web/src/components/ChatMessages/index.tsx b/web/src/components/ChatMessages/index.tsxindex d5ffa1872f..d93eaa2a54 100644--- a/web/src/components/ChatMessages/index.tsx+++ b/web/src/components/ChatMessages/index.tsx@@ -83,8 +83,12 @@ export const ChatMessages: React.FC<ChatMessagesProps> = (props) => {{props.messages.map((message, index) => {return (<ChatMessageComponent- {...{ message, ...props, index }}key={message.id}+ message={message}+ index={index}+ deleteMessage={props.deleteMessage}+ updateMessage={props.updateMessage}+ availableRoles={props.availableRoles}/>);})}diff --git a/web/src/ee/features/playground/page/context/index.tsx b/web/src/ee/features/playground/page/context/index.tsxindex 714dac2073..7fd2afa37b 100644--- a/web/src/ee/features/playground/page/context/index.tsx+++ b/web/src/ee/features/playground/page/context/index.tsx@@ -136,28 +136,33 @@ export const PlaygroundProvider: React.FC<PropsWithChildren> = ({useEffect(updatePromptVariables, [messages, updatePromptVariables]);- const addMessage: PlaygroundContextType["addMessage"] = (role, content) => {- const message = createEmptyMessage(role, content);- setMessages((prev) => [...prev, message]);-- return message;- };-- const updateMessage: PlaygroundContextType["updateMessage"] = (- id,- key,- value,- ) => {- setMessages((prev) =>- prev.map((message) =>- message.id === id ? { ...message, [key]: value } : message,- ),- );- };+ const addMessage: PlaygroundContextType["addMessage"] = useCallback(+ (role, content) => {+ const message = createEmptyMessage(role, content);+ setMessages((prev) => [...prev, message]);++ return message;+ },+ [],+ );++ const updateMessage: PlaygroundContextType["updateMessage"] = useCallback(+ (id, key, value) => {+ setMessages((prev) =>+ prev.map((message) =>+ message.id === id ? { ...message, [key]: value } : message,+ ),+ );+ },+ [],+ );- const deleteMessage: PlaygroundContextType["deleteMessage"] = (id) => {- setMessages((prev) => prev.filter((message) => message.id !== id));- };+ const deleteMessage: PlaygroundContextType["deleteMessage"] = useCallback(+ (id) => {+ setMessages((prev) => prev.filter((message) => message.id !== id));+ },+ [],+ );const handleSubmit: PlaygroundContextType["handleSubmit"] =useCallback(async () => {@@ -216,15 +221,18 @@ export const PlaygroundProvider: React.FC<PropsWithChildren> = ({useCommandEnter(!isStreaming, handleSubmit);- const updatePromptVariableValue = (variable: string, value: string) => {- setPromptVariables((prev) =>- prev.map((v) => (v.name === variable ? { ...v, value } : v)),- );- };+ const updatePromptVariableValue = useCallback(+ (variable: string, value: string) => {+ setPromptVariables((prev) =>+ prev.map((v) => (v.name === variable ? { ...v, value } : v)),+ );+ },+ [],+ );- const deletePromptVariable = (variable: string) => {+ const deletePromptVariable = useCallback((variable: string) => {setPromptVariables((prev) => prev.filter((v) => v.name !== variable));- };+ }, []);return (<PlaygroundContext.Provider