From 39f956e5c1e6a6708604b16583c22ab3b4430946 Mon Sep 17 00:00:00 2001 From: jagadeeswaran-zipstack Date: Tue, 24 Dec 2024 17:16:05 +0530 Subject: [PATCH] [Fix] Prompt Studio Output update issue (#912) * fixes for prompt sudio coverage * fixed prompt studio local variable issue * updated return type * added optional chaining * added missing type * added comment for error suppression * handled edge cases * added fix on run all prompt coverage issue * added optional chaining * fixed prompt output update issue * prettier fix --- .../prompt-card/PromptCardItems.jsx | 40 +++++++++------ .../custom-tools/tool-ide/ToolIde.jsx | 19 ++++--- frontend/src/hooks/usePromptOutput.js | 50 ++++++++++--------- 3 files changed, 64 insertions(+), 45 deletions(-) diff --git a/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx b/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx index 5fd33d053..5d7f2f9c0 100644 --- a/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx +++ b/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx @@ -19,7 +19,6 @@ import { Header } from "./Header"; import { OutputForIndex } from "./OutputForIndex"; import { PromptOutput } from "./PromptOutput"; import { TABLE_ENFORCE_TYPE, RECORD_ENFORCE_TYPE } from "./constants"; -import usePromptOutput from "../../../hooks/usePromptOutput"; let TableExtractionSettingsBtn; try { @@ -63,11 +62,9 @@ function PromptCardItems({ isSimplePromptStudio, isPublicSource, adapters, - defaultLlmProfile, singlePassExtractMode, } = useCustomToolStore(); - const { generatePromptOutputKey } = usePromptOutput(); const [isEditingPrompt, setIsEditingPrompt] = useState(false); const [isEditingTitle, setIsEditingTitle] = useState(false); const [expandCard, setExpandCard] = useState(true); @@ -81,17 +78,6 @@ function PromptCardItems({ const divRef = useRef(null); const [enforceType, setEnforceType] = useState(""); const promptId = promptDetails?.prompt_id; - const docId = selectedDoc?.document_id; - const promptProfile = promptDetails?.profile_manager || defaultLlmProfile; - const promptOutputKey = generatePromptOutputKey( - promptId, - docId, - promptProfile, - singlePassExtractMode, - true - ); - const promptCoverage = - promptOutputs[promptOutputKey]?.coverage || coverageCountData; useEffect(() => { if (enforceType !== promptDetails?.enforce_type) { @@ -123,6 +109,32 @@ function PromptCardItems({ return result; }; + const getUpdatedCoverage = (promptId, singlePass, promptOutputs) => { + let updatedCoverage = null; + Object.keys(promptOutputs).forEach((key) => { + const [keyPromptId, , , keyIsSinglePass] = key.split("__"); // Destructure the key parts + + // Check if the key matches the criteria + if (keyPromptId === promptId && keyIsSinglePass === String(singlePass)) { + const currentCoverage = promptOutputs[key]?.coverage || []; + + // Update the highestCoverage if the current one is longer + if ( + !updatedCoverage || + currentCoverage.length > updatedCoverage.length + ) { + updatedCoverage = currentCoverage; + } + } + }); + + return updatedCoverage; + }; + + const promptCoverage = + getUpdatedCoverage(promptId, singlePassExtractMode, promptOutputs) || + coverageCountData; + const getAdapterInfo = async (adapterData) => { // If simple prompt studio, return early if (isSimplePromptStudio) { diff --git a/frontend/src/components/custom-tools/tool-ide/ToolIde.jsx b/frontend/src/components/custom-tools/tool-ide/ToolIde.jsx index 019d4a954..866bf4126 100644 --- a/frontend/src/components/custom-tools/tool-ide/ToolIde.jsx +++ b/frontend/src/components/custom-tools/tool-ide/ToolIde.jsx @@ -216,13 +216,18 @@ function ToolIde() { const body = { output: doc?.document_id, }; - handleUpdateTool(body).catch((err) => { - const revertSelectedDoc = { - selectedDoc: prevSelectedDoc, - }; - updateCustomTool(revertSelectedDoc); - setAlertDetails(handleException(err, "Failed to select the document")); - }); + handleUpdateTool(body) + .then((res) => { + const updatedToolData = res?.data; + updateCustomTool({ details: updatedToolData }); + }) + .catch((err) => { + const revertSelectedDoc = { + selectedDoc: prevSelectedDoc, + }; + updateCustomTool(revertSelectedDoc); + setAlertDetails(handleException(err, "Failed to select the document")); + }); }; return ( diff --git a/frontend/src/hooks/usePromptOutput.js b/frontend/src/hooks/usePromptOutput.js index 4c6fba3cf..3d17152d7 100644 --- a/frontend/src/hooks/usePromptOutput.js +++ b/frontend/src/hooks/usePromptOutput.js @@ -24,8 +24,7 @@ try { const usePromptOutput = () => { const { sessionDetails } = useSessionStore(); const { setTokenUsage, updateTokenUsage } = useTokenUsageStore(); - const { setPromptOutput, updatePromptOutput, promptOutputs } = - usePromptOutputStore(); + const { setPromptOutput, updatePromptOutput } = usePromptOutputStore(); const { isSimplePromptStudio, isPublicSource, selectedDoc } = useCustomToolStore(); const axiosPrivate = useAxiosPrivate(); @@ -154,32 +153,35 @@ const usePromptOutput = () => { setPromptOutput(outputs); setTokenUsage(tokenUsageDetails); } else { - let updatedPromptOutputs = promptOutputs; - Object.keys(outputs).forEach((key) => { - const [keyPromptId, keyDoctId, keyLlmProfile, keyIsSinglePass] = - key.split("__"); - // only add output of selected document - if (keyDoctId === selectedDoc?.document_id) { - const currentOutput = { [key]: outputs[key] }; - updatedPromptOutputs = { ...promptOutputs, ...currentOutput }; - } - Object.keys(updatedPromptOutputs).forEach((innerKey) => { - const [existingPromptId, , existingLlmProfile, existingIsSinglePass] = - innerKey.split("__"); // Extract promptId from key - if ( - keyPromptId === existingPromptId && - keyLlmProfile === existingLlmProfile && - keyIsSinglePass === existingIsSinglePass - ) { - updatedPromptOutputs[innerKey].coverage = outputs[key]?.coverage; - } - }); - }); - updatePromptOutput(updatedPromptOutputs); + const prevOutputs = usePromptOutputStore.getState().promptOutputs; + updatePromptOutput(updateCoverage(prevOutputs, outputs)); updateTokenUsage(tokenUsageDetails); } }; + const updateCoverage = (promptOutputs, outputs) => { + let updatedPromptOutputs = promptOutputs; + Object.keys(outputs).forEach((key) => { + const [keyPromptId, keyDoctId, , keyIsSinglePass] = key.split("__"); + // only add output of selected document + if (keyDoctId === selectedDoc?.document_id) { + const currentOutput = { [key]: outputs[key] }; + updatedPromptOutputs = { ...promptOutputs, ...currentOutput }; + } + Object.keys(updatedPromptOutputs).forEach((innerKey) => { + const [existingPromptId, , , existingIsSinglePass] = + innerKey.split("__"); // Extract promptId from key + if ( + keyPromptId === existingPromptId && + keyIsSinglePass === existingIsSinglePass + ) { + updatedPromptOutputs[innerKey].coverage = outputs[key]?.coverage; + } + }); + }); + return updatedPromptOutputs; + }; + const promptOutputApi = async ( toolId, docId = null,