LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
FEAT: Added "Run All Prompts" with Concurrent API Request Queuing Mechanism (#749)
Tahier Hussain146130b936f8
diff --git a/frontend/src/components/custom-tools/document-manager/DocumentManager.jsx b/frontend/src/components/custom-tools/document-manager/DocumentManager.jsxindex b34cbd3ff..0435a0101 100644--- a/frontend/src/components/custom-tools/document-manager/DocumentManager.jsx+++ b/frontend/src/components/custom-tools/document-manager/DocumentManager.jsx@@ -88,7 +88,7 @@ function DocumentManager({ generateIndex, handleUpdateTool, handleDocChange }) {const {selectedDoc,listOfDocs,- disableLlmOrDocChange,+ isMultiPassExtractLoading,details,indexDocs,isSinglePassExtractLoading,@@ -353,7 +353,7 @@ function DocumentManager({ generateIndex, handleUpdateTool, handleDocChange }) {size="small"disabled={!selectedDoc ||- disableLlmOrDocChange?.length > 0 ||+ isMultiPassExtractLoading ||isSinglePassExtractLoading ||page <= 1}@@ -366,7 +366,7 @@ function DocumentManager({ generateIndex, handleUpdateTool, handleDocChange }) {size="small"disabled={!selectedDoc ||- disableLlmOrDocChange?.length > 0 ||+ isMultiPassExtractLoading ||isSinglePassExtractLoading ||page >= listOfDocs?.length}diff --git a/frontend/src/components/custom-tools/editable-text/EditableText.jsx b/frontend/src/components/custom-tools/editable-text/EditableText.jsxindex b2f90bd4c..a212f0ede 100644--- a/frontend/src/components/custom-tools/editable-text/EditableText.jsx+++ b/frontend/src/components/custom-tools/editable-text/EditableText.jsx@@ -15,18 +15,13 @@ function EditableText({handleChange,isTextarea,placeHolder,+ isCoverageLoading,}) {const name = isTextarea ? "prompt" : "prompt_key";const [triggerHandleChange, setTriggerHandleChange] = useState(false);const [isHovered, setIsHovered] = useState(false);const divRef = useRef(null);- const {- disableLlmOrDocChange,- indexDocs,- selectedDoc,- isSinglePassExtractLoading,- isPublicSource,- } = useCustomToolStore();+ const { isSinglePassExtractLoading, isPublicSource } = useCustomToolStore();useEffect(() => {setText(defaultText);@@ -91,9 +86,7 @@ function EditableText({onBlur={handleBlur}onClick={() => setIsEditing(true)}disabled={- disableLlmOrDocChange.includes(promptId) ||- isSinglePassExtractLoading ||- isPublicSource+ isCoverageLoading || isSinglePassExtractLoading || isPublicSource}/>);@@ -115,10 +108,7 @@ function EditableText({onBlur={handleBlur}onClick={() => setIsEditing(true)}disabled={- disableLlmOrDocChange.includes(promptId) ||- indexDocs.includes(selectedDoc?.document_id) ||- isSinglePassExtractLoading ||- isPublicSource+ isCoverageLoading || isSinglePassExtractLoading || isPublicSource}/>);@@ -129,11 +119,12 @@ EditableText.propTypes = {setIsEditing: PropTypes.func.isRequired,text: PropTypes.string,setText: PropTypes.func.isRequired,- promptId: PropTypes.string.isRequired,- defaultText: PropTypes.string.isRequired,+ promptId: PropTypes.string,+ defaultText: PropTypes.string,handleChange: PropTypes.func.isRequired,isTextarea: PropTypes.bool,placeHolder: PropTypes.string,+ isCoverageLoading: PropTypes.bool.isRequired,};export { EditableText };diff --git a/frontend/src/components/custom-tools/manage-docs-modal/ManageDocsModal.jsx b/frontend/src/components/custom-tools/manage-docs-modal/ManageDocsModal.jsxindex b7569282b..3d9d53e7d 100644--- a/frontend/src/components/custom-tools/manage-docs-modal/ManageDocsModal.jsx+++ b/frontend/src/components/custom-tools/manage-docs-modal/ManageDocsModal.jsx@@ -79,7 +79,7 @@ function ManageDocsModal({updateCustomTool,details,defaultLlmProfile,- disableLlmOrDocChange,+ isMultiPassExtractLoading,indexDocs,rawIndexStatus,summarizeIndexStatus,@@ -392,7 +392,7 @@ function ManageDocsModal({icon={<ReloadOutlined />}onClick={() => handleReIndexBtnClick(item)}disabled={- disableLlmOrDocChange?.length > 0 ||+ isMultiPassExtractLoading ||isSinglePassExtractLoading ||indexDocs.includes(item?.document_id) ||isUploading ||@@ -418,7 +418,7 @@ function ManageDocsModal({size="small"className="display-flex-align-center"disabled={- disableLlmOrDocChange?.length > 0 ||+ isMultiPassExtractLoading ||isSinglePassExtractLoading ||indexDocs.includes(item?.document_id) ||isUploading ||@@ -435,7 +435,7 @@ function ManageDocsModal({checked={selectedDoc?.document_id === item?.document_id}onClick={() => handleDocChange(item)}disabled={- disableLlmOrDocChange?.length > 0 ||+ isMultiPassExtractLoading ||isSinglePassExtractLoading ||indexDocs.includes(item?.document_id)}@@ -447,7 +447,7 @@ function ManageDocsModal({}, [listOfDocs,selectedDoc,- disableLlmOrDocChange,+ isMultiPassExtractLoading,rawIndexStatus,summarizeIndexStatus,indexDocs,@@ -602,7 +602,7 @@ function ManageDocsModal({loading={isUploading}disabled={!defaultLlmProfile ||- disableLlmOrDocChange?.length > 0 ||+ isMultiPassExtractLoading ||isSinglePassExtractLoading ||isPublicSource}diff --git a/frontend/src/components/custom-tools/output-for-doc-modal/OutputForDocModal.jsx b/frontend/src/components/custom-tools/output-for-doc-modal/OutputForDocModal.jsxindex ef69983c9..b1743a5d6 100644--- a/frontend/src/components/custom-tools/output-for-doc-modal/OutputForDocModal.jsx+++ b/frontend/src/components/custom-tools/output-for-doc-modal/OutputForDocModal.jsx@@ -50,7 +50,7 @@ function OutputForDocModal({promptId,promptKey,profileManagerId,- docOutputs,+ docOutputs = [],}) {const [promptOutputs, setPromptOutputs] = useState([]);const [rows, setRows] = useState([]);@@ -60,7 +60,7 @@ function OutputForDocModal({details,listOfDocs,selectedDoc,- disableLlmOrDocChange,+ isMultiPassExtractLoading,singlePassExtractMode,isSinglePassExtractLoading,isPublicSource,@@ -89,10 +89,6 @@ function OutputForDocModal({isSinglePassExtractLoading,]);- useEffect(() => {- updatePromptOutput(docOutputs);- }, [docOutputs]);-useEffect(() => {handleRowsGeneration(promptOutputs);}, [promptOutputs, tokenUsage]);@@ -360,9 +356,7 @@ function OutputForDocModal({<Buttonsize="small"onClick={() => navigate("outputAnalyzer")}- disabled={- disableLlmOrDocChange?.length > 0 || isSinglePassExtractLoading- }+ disabled={isMultiPassExtractLoading || isSinglePassExtractLoading}>View in Output Analyzer</Button>@@ -391,8 +385,8 @@ function OutputForDocModal({OutputForDocModal.propTypes = {open: PropTypes.bool.isRequired,setOpen: PropTypes.func.isRequired,- promptId: PropTypes.string.isRequired,- promptKey: PropTypes.string.isRequired,+ promptId: PropTypes.string,+ promptKey: PropTypes.string,profileManagerId: PropTypes.string,docOutputs: PropTypes.object,};diff --git a/frontend/src/components/custom-tools/prompt-card/DisplayPromptResult.jsx b/frontend/src/components/custom-tools/prompt-card/DisplayPromptResult.jsxindex a73bc34fc..f5bba6460 100644--- a/frontend/src/components/custom-tools/prompt-card/DisplayPromptResult.jsx+++ b/frontend/src/components/custom-tools/prompt-card/DisplayPromptResult.jsx@@ -1,11 +1,41 @@-import { Typography } from "antd";+import { Spin, Typography } from "antd";import PropTypes from "prop-types";import { InfoCircleFilled } from "@ant-design/icons";-import { displayPromptResult } from "../../../helpers/GetStaticData";+import {+ displayPromptResult,+ generateApiRunStatusId,+ PROMPT_RUN_API_STATUSES,+} from "../../../helpers/GetStaticData";import "./PromptCard.css";+import { useCustomToolStore } from "../../../store/custom-tool-store";+import { useEffect, useState } from "react";+import { SpinnerLoader } from "../../widgets/spinner-loader/SpinnerLoader";++function DisplayPromptResult({ output, profileId, docId, promptRunStatus }) {+ const [isLoading, setIsLoading] = useState(false);+ const { singlePassExtractMode, isSinglePassExtractLoading } =+ useCustomToolStore();++ useEffect(() => {+ if (singlePassExtractMode && isSinglePassExtractLoading) {+ setIsLoading(true);+ return;+ }++ const key = generateApiRunStatusId(docId, profileId);+ if (promptRunStatus?.[key] === PROMPT_RUN_API_STATUSES.RUNNING) {+ setIsLoading(true);+ return;+ }++ setIsLoading(false);+ }, [promptRunStatus, isSinglePassExtractLoading]);++ if (isLoading) {+ return <Spin indicator={<SpinnerLoader size="small" />} />;+ }-function DisplayPromptResult({ output }) {if (output === undefined) {return (<Typography.Text className="prompt-not-ran">@@ -25,7 +55,10 @@ function DisplayPromptResult({ output }) {}DisplayPromptResult.propTypes = {- output: PropTypes.any.isRequired,+ output: PropTypes.any,+ profileId: PropTypes.string,+ docId: PropTypes.string,+ promptRunStatus: PropTypes.object,};export { DisplayPromptResult };diff --git a/frontend/src/components/custom-tools/prompt-card/Header.jsx b/frontend/src/components/custom-tools/prompt-card/Header.jsxindex 906434ce1..f72a36083 100644--- a/frontend/src/components/custom-tools/prompt-card/Header.jsx+++ b/frontend/src/components/custom-tools/prompt-card/Header.jsx@@ -11,7 +11,10 @@ import { useEffect, useState } from "react";import { Button, Checkbox, Col, Dropdown, Row, Tag, Tooltip } from "antd";import PropTypes from "prop-types";-import { promptStudioUpdateStatus } from "../../../helpers/GetStaticData";+import {+ PROMPT_RUN_TYPES,+ promptStudioUpdateStatus,+} from "../../../helpers/GetStaticData";import { ConfirmModal } from "../../widgets/confirm-modal/ConfirmModal";import { EditableText } from "../editable-text/EditableText";import { useCustomToolStore } from "../../../store/custom-tool-store";@@ -40,13 +43,11 @@ function Header({setIsEditingTitle,expandCard,setExpandCard,- enabledProfiles,spsLoading,handleSpsLoading,}) {const {selectedDoc,- disableLlmOrDocChange,singlePassExtractMode,isSinglePassExtractLoading,indexDocs,@@ -58,9 +59,9 @@ function Header({const [isDisablePrompt, setIsDisablePrompt] = useState(null);- const handleRunBtnClick = (profileManager = null, coverAllDoc = true) => {+ const handleRunBtnClick = (promptRunType, docId = null) => {setExpandCard(true);- handleRun(profileManager, coverAllDoc, enabledProfiles, true);+ handleRun(promptRunType, promptDetails?.prompt_id, null, docId);};const handleDisablePrompt = (event) => {@@ -97,7 +98,7 @@ function Header({),key: "delete",disabled:- disableLlmOrDocChange?.includes(promptDetails?.prompt_id) ||+ isCoverageLoading ||isSinglePassExtractLoading ||indexDocs?.includes(selectedDoc?.document_id) ||isPublicSource,@@ -122,6 +123,7 @@ function Header({defaultText={promptDetails?.prompt_key}handleChange={handleChange}placeHolder={updatePlaceHolder}+ isCoverageLoading={isCoverageLoading}/></Col><Col span={12} className="display-flex-right">@@ -186,13 +188,16 @@ function Header({type="text"className="prompt-card-action-button"onClick={() =>- handleRunBtnClick(promptDetails?.profile_manager, false)+ handleRunBtnClick(+ PROMPT_RUN_TYPES.RUN_ONE_PROMPT_ALL_LLMS_ONE_DOC,+ selectedDoc?.document_id+ )}disabled={(updateStatus?.promptId === promptDetails?.prompt_id &&updateStatus?.status ===promptStudioUpdateStatus?.isUpdating) ||- disableLlmOrDocChange?.includes(promptDetails?.prompt_id) ||+ isCoverageLoading ||indexDocs?.includes(selectedDoc?.document_id) ||isPublicSource ||spsLoading[selectedDoc?.document_id]@@ -206,12 +211,16 @@ function Header({size="small"type="text"className="prompt-card-action-button"- onClick={handleRunBtnClick}+ onClick={() =>+ handleRunBtnClick(+ PROMPT_RUN_TYPES.RUN_ONE_PROMPT_ALL_LLMS_ALL_DOCS+ )+ }disabled={(updateStatus?.promptId === promptDetails?.prompt_id &&updateStatus?.status ===promptStudioUpdateStatus?.isUpdating) ||- disableLlmOrDocChange?.includes(promptDetails?.prompt_id) ||+ isCoverageLoading ||indexDocs?.includes(selectedDoc?.document_id) ||isPublicSource}@@ -259,7 +268,6 @@ Header.propTypes = {setIsEditingTitle: PropTypes.func.isRequired,expandCard: PropTypes.bool.isRequired,setExpandCard: PropTypes.func.isRequired,- enabledProfiles: PropTypes.array.isRequired,spsLoading: PropTypes.object,handleSpsLoading: PropTypes.func.isRequired,};diff --git a/frontend/src/components/custom-tools/prompt-card/PrompDnd.jsx b/frontend/src/components/custom-tools/prompt-card/PrompDnd.jsxindex 9fbd3ad65..3f1bd2d29 100644--- a/frontend/src/components/custom-tools/prompt-card/PrompDnd.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PrompDnd.jsx@@ -1,13 +1,15 @@import PropTypes from "prop-types";import { useDrag, useDrop } from "react-dnd";-import { useRef } from "react";+import { memo, useRef } from "react";import { NotesCard } from "../notes-card/NotesCard";import { PromptCard } from "./PromptCard";import { promptType } from "../../../helpers/GetStaticData";import { useCustomToolStore } from "../../../store/custom-tool-store";+import usePromptRun from "../../../hooks/usePromptRun";+import { usePromptRunStatusStore } from "../../../store/prompt-run-status-store";-function PromptDnd({+const PromptDnd = memo(function PromptDnd({item,index,handleChangePromptCard,@@ -19,6 +21,10 @@ function PromptDnd({}) {const ref = useRef(null);const { isSimplePromptStudio } = useCustomToolStore();+ const { handlePromptRunRequest } = usePromptRun();+ const promptRunStatus = usePromptRunStatusStore(+ (state) => state?.promptRunStatus?.[item?.prompt_id] || {}+ );const [, drop] = useDrop({accept: "PROMPT_CARD",@@ -34,6 +40,7 @@ function PromptDnd({isDragging: monitor.isDragging(),}),});+drag(drop(ref));return (@@ -47,6 +54,8 @@ function PromptDnd({promptOutputs={outputs}enforceTypeList={enforceTypeList}setUpdatedPromptsCopy={setUpdatedPromptsCopy}+ handlePromptRunRequest={handlePromptRunRequest}+ promptRunStatus={promptRunStatus}/>)}{item.prompt_type === promptType.notes && (@@ -60,7 +69,9 @@ function PromptDnd({)}</div>);-}+});++PromptDnd.displayName = "PromptDnd";PromptDnd.propTypes = {item: PropTypes.object.isRequired,diff --git a/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx b/frontend/src/components/custom-tools/prompt-card/PromptCard.jsxindex a8063e7e8..62b53147f 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptCard.jsx@@ -1,525 +1,221 @@import PropTypes from "prop-types";-import { useEffect, useState } from "react";+import { memo, useEffect, useState } from "react";import {- generateUUID,- pollForCompletion,+ PROMPT_RUN_API_STATUSES,promptStudioUpdateStatus,} from "../../../helpers/GetStaticData";-import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate";-import { useExceptionHandler } from "../../../hooks/useExceptionHandler";import { useAlertStore } from "../../../store/alert-store";import { useCustomToolStore } from "../../../store/custom-tool-store";-import { useSessionStore } from "../../../store/session-store";import { useSocketCustomToolStore } from "../../../store/socket-custom-tool";import { OutputForDocModal } from "../output-for-doc-modal/OutputForDocModal";import usePostHogEvents from "../../../hooks/usePostHogEvents";import { PromptCardItems } from "./PromptCardItems";import "./PromptCard.css";-import usePromptOutput from "../../../hooks/usePromptOutput";import { handleUpdateStatus } from "./constants";--let promptRunApiSps;-try {- promptRunApiSps =- require("../../../plugins/simple-prompt-studio/helper").promptRunApiSps;-} catch {- // The component will remain null of it is not available-}--function PromptCard({- promptDetails,- handleChangePromptCard,- handleDelete,- updatePlaceHolder,- promptOutputs,- enforceTypeList,- setUpdatedPromptsCopy,-}) {- const [promptDetailsState, setPromptDetailsState] = useState({});- const [isPromptDetailsStateUpdated, setIsPromptDetailsStateUpdated] =- useState(false);- const [updateStatus, setUpdateStatus] = useState({- promptId: null,- status: null,- });- const [isRunLoading, setIsRunLoading] = useState({});- const [promptKey, setPromptKey] = useState("");- const [promptText, setPromptText] = useState("");- const [selectedLlmProfileId, setSelectedLlmProfileId] = useState(null);- const [coverage, setCoverage] = useState({});- const [coverageTotal, setCoverageTotal] = useState(0);- const [isCoverageLoading, setIsCoverageLoading] = useState(false);- const [openOutputForDoc, setOpenOutputForDoc] = useState(false);- const [progressMsg, setProgressMsg] = useState({});- const [docOutputs, setDocOutputs] = useState([]);- const [timers, setTimers] = useState({}); // Prompt run timer- const [spsLoading, setSpsLoading] = useState({});- const {- llmProfiles,- selectedDoc,- listOfDocs,- updateCustomTool,- details,- defaultLlmProfile,- disableLlmOrDocChange,- summarizeIndexStatus,- singlePassExtractMode,- isSinglePassExtractLoading,- isSimplePromptStudio,- } = useCustomToolStore();- const { messages } = useSocketCustomToolStore();- const { sessionDetails } = useSessionStore();- const { setAlertDetails } = useAlertStore();- const axiosPrivate = useAxiosPrivate();- const handleException = useExceptionHandler();- const { setPostHogCustomEvent } = usePostHogEvents();- const { updatePromptOutputState } = usePromptOutput();-- useEffect(() => {- if (- isPromptDetailsStateUpdated ||- !Object.keys(promptDetails || {})?.length- )- return;- setPromptDetailsState(promptDetails);- setIsPromptDetailsStateUpdated(true);- }, [promptDetails]);-- useEffect(() => {- // Find the latest message that matches the criteria- const msg = [...messages]- .reverse()- .find(- (item) =>- (item?.component?.prompt_id === promptDetailsState?.prompt_id ||- item?.component?.prompt_key === promptKey) &&- (item?.level === "INFO" || item?.level === "ERROR")- );-- // If no matching message is found, return early- if (!msg) {- return;- }-- // Set the progress message state with the found message- setProgressMsg({- message: msg?.message || "",- level: msg?.level || "INFO",+// import { usePromptRunStatusStore } from "../../../store/prompt-run-status-store";++const PromptCard = memo(+ ({+ promptDetails,+ handleChangePromptCard,+ handleDelete,+ updatePlaceHolder,+ promptOutputs,+ enforceTypeList,+ setUpdatedPromptsCopy,+ handlePromptRunRequest,+ promptRunStatus,+ }) => {+ const [promptDetailsState, setPromptDetailsState] = useState({});+ const [isPromptDetailsStateUpdated, setIsPromptDetailsStateUpdated] =+ useState(false);+ const [updateStatus, setUpdateStatus] = useState({+ promptId: null,+ status: null,});- }, [messages]);-- useEffect(() => {- setSelectedLlmProfileId(- promptDetailsState?.profile_manager || llmProfiles[0]?.profile_id- );- }, [promptDetailsState]);-- useEffect(() => {- resetInfoMsgs();- }, [- selectedLlmProfileId,- selectedDoc,- listOfDocs,- singlePassExtractMode,- isSinglePassExtractLoading,- defaultLlmProfile,- ]);-- useEffect(() => {- let listOfIds = [...disableLlmOrDocChange];- const promptId = promptDetailsState?.prompt_id;- const isIncluded = listOfIds.includes(promptId);-- const isDocLoading = docOutputs?.some(- (doc) => doc?.key?.startsWith(`${promptId}__`) && doc?.isLoading- );-- if ((isIncluded && isDocLoading) || (!isIncluded && !isDocLoading)) {- return;- }-- if (isIncluded && !isDocLoading) {- listOfIds = listOfIds.filter((item) => item !== promptId);- }-- if (!isIncluded && isDocLoading) {- listOfIds.push(promptId);- }-- updateCustomTool({ disableLlmOrDocChange: listOfIds });- }, [docOutputs]);-- useEffect(() => {- if (isCoverageLoading && coverageTotal === listOfDocs?.length) {- setIsCoverageLoading(false);- setCoverageTotal(0);- }- }, [coverageTotal]);-- const resetInfoMsgs = () => {- setProgressMsg({}); // Reset Progress Message- };-- useEffect(() => {- const isProfilePresent = llmProfiles?.some(- (profile) => profile?.profile_id === defaultLlmProfile- );-- // If selectedLlmProfileId is not present, set it to null- if (!isProfilePresent) {- setSelectedLlmProfileId(null);- }- }, [llmProfiles]);-- const handleChange = async (- event,- promptId,- dropdownItem,- isUpdateStatus = false- ) => {- let name = "";- let value = "";- if (dropdownItem?.length) {- name = dropdownItem;- value = event;- } else {- name = event.target.name;- value = event.target.value;- }-- const prevPromptDetailsState = { ...promptDetailsState };-- const updatedPromptDetailsState = { ...promptDetailsState };- updatedPromptDetailsState[name] = value;-- handleUpdateStatus(- isUpdateStatus,- promptId,- promptStudioUpdateStatus.isUpdating,- setUpdateStatus- );- setPromptDetailsState(updatedPromptDetailsState);- return handleChangePromptCard(name, value, promptId)- .then((res) => {- const data = res?.data;- setUpdatedPromptsCopy((prev) => {- prev[promptId] = data;- return prev;- });- handleUpdateStatus(- isUpdateStatus,- promptId,- promptStudioUpdateStatus.done,- setUpdateStatus+ const [promptKey, setPromptKey] = useState("");+ const [promptText, setPromptText] = useState("");+ const [selectedLlmProfileId, setSelectedLlmProfileId] = useState(null);++ const [isCoverageLoading, setIsCoverageLoading] = useState(false);+ const [openOutputForDoc, setOpenOutputForDoc] = useState(false);+ const [progressMsg, setProgressMsg] = useState({});+ const [spsLoading, setSpsLoading] = useState({});+ const { llmProfiles, selectedDoc, details, summarizeIndexStatus } =+ useCustomToolStore();+ const { messages } = useSocketCustomToolStore();+ const { setAlertDetails } = useAlertStore();+ const { setPostHogCustomEvent } = usePostHogEvents();++ useEffect(() => {+ if (+ isPromptDetailsStateUpdated ||+ !Object.keys(promptDetails || {})?.length+ )+ return;+ setPromptDetailsState(promptDetails);+ setIsPromptDetailsStateUpdated(true);+ }, [promptDetails]);++ useEffect(() => {+ // Find the latest message that matches the criteria+ const msg = [...messages]+ .reverse()+ .find(+ (item) =>+ (item?.component?.prompt_id === promptDetailsState?.prompt_id ||+ item?.component?.prompt_key === promptKey) &&+ (item?.level === "INFO" || item?.level === "ERROR"));- })- .catch(() => {- handleUpdateStatus(isUpdateStatus, promptId, null, setUpdateStatus);- setPromptDetailsState(prevPromptDetailsState);- })- .finally(() => {- if (isUpdateStatus) {- setTimeout(() => {- handleUpdateStatus(true, promptId, null, setUpdateStatus);- }, 3000);- }- });- };-- // Function to update loading state for a specific document and profile- const handleIsRunLoading = (docId, profileId, isLoading) => {- setIsRunLoading((prevLoadingProfiles) => ({- ...prevLoadingProfiles,- [`${docId}_${profileId}`]: isLoading,- }));- };-- const handleSelectDefaultLLM = (llmProfileId) => {- setSelectedLlmProfileId(llmProfileId);- handleChange(- llmProfileId,- promptDetailsState?.prompt_id,- "profile_manager"- );- };- const handleTypeChange = (value) => {- handleChange(value, promptDetailsState?.prompt_id, "enforce_type", true);- };+ // If no matching message is found, return early+ if (!msg) {+ return;+ }- const handleDocOutputs = (docId, promptId, profileId, isLoading, output) => {- if (isSimplePromptStudio) {- return;- }- setDocOutputs((prev) => {- const updatedDocOutputs = [...prev];- const key = `${promptId}__${docId}__${profileId}`;- // Update the entry for the provided docId with isLoading and output- const newData = {- key,- isLoading,- output,- };- const index = updatedDocOutputs.findIndex((item) => item.key === key);+ // Set the progress message state with the found message+ setProgressMsg({+ message: msg?.message || "",+ level: msg?.level || "INFO",+ });+ }, [messages]);- if (index !== -1) {- // Update the existing object- updatedDocOutputs[index] = newData;- } else {- // Append the new object- updatedDocOutputs.push(newData);- }- return updatedDocOutputs;- });- };+ useEffect(() => {+ setSelectedLlmProfileId(+ promptDetailsState?.profile_manager || llmProfiles[0]?.profile_id+ );+ }, [promptDetailsState]);- const handleSpsLoading = (docId, isLoadingStatus) => {- setSpsLoading((prev) => ({- ...prev,- [docId]: isLoadingStatus,- }));- };+ useEffect(() => {+ const promptRunStatusKeys = Object.keys(promptRunStatus);- // Generate the result for the currently selected document- const handleRun = (- profileManagerId,- coverAllDoc = true,- selectedLlmProfiles = [],- runAllLLM = false- ) => {- try {- setPostHogCustomEvent("ps_prompt_run", {- info: "Click on 'Run Prompt' button (Multi Pass)",- });- } catch (err) {- // If an error occurs while setting custom posthog event, ignore it and continue- }+ const coverageLoading = promptRunStatusKeys.some(+ (key) => promptRunStatus[key] === PROMPT_RUN_API_STATUSES.RUNNING+ );+ setIsCoverageLoading(coverageLoading);+ }, [promptRunStatus]);- const validateInputs = (- profileManagerId,- selectedLlmProfiles,- coverAllDoc+ const handleChange = async (+ event,+ promptId,+ dropdownItem,+ isUpdateStatus = false) => {- if (- !profileManagerId &&- !promptDetailsState?.profile_manager?.length &&- !(!coverAllDoc && selectedLlmProfiles?.length > 0) &&- !isSimplePromptStudio- ) {- setAlertDetails({- type: "error",- content: "LLM Profile is not selected",- });- return true;+ let name = "";+ let value = "";+ if (dropdownItem?.length) {+ name = dropdownItem;+ value = event;+ } else {+ name = event.target.name;+ value = event.target.value;}- if (!selectedDoc) {- setAlertDetails({- type: "error",- content: "Document not selected",- });- return true;- }+ const prevPromptDetailsState = { ...promptDetailsState };- if (!promptKey) {- setAlertDetails({- type: "error",- content: "Prompt key cannot be empty",- });- return true;- }+ const updatedPromptDetailsState = { ...promptDetailsState };+ updatedPromptDetailsState[name] = value;- if (!promptText) {- setAlertDetails({- type: "error",- content: "Prompt cannot be empty",+ handleUpdateStatus(+ isUpdateStatus,+ promptId,+ promptStudioUpdateStatus.isUpdating,+ setUpdateStatus+ );+ setPromptDetailsState(updatedPromptDetailsState);+ return handleChangePromptCard(name, value, promptId)+ .then((res) => {+ const data = res?.data;+ setUpdatedPromptsCopy((prev) => {+ prev[promptId] = data;+ return prev;+ });+ handleUpdateStatus(+ isUpdateStatus,+ promptId,+ promptStudioUpdateStatus.done,+ setUpdateStatus+ );+ })+ .catch(() => {+ handleUpdateStatus(isUpdateStatus, promptId, null, setUpdateStatus);+ setPromptDetailsState(prevPromptDetailsState);+ })+ .finally(() => {+ if (isUpdateStatus) {+ setTimeout(() => {+ handleUpdateStatus(true, promptId, null, setUpdateStatus);+ }, 3000);+ }});- return true;- }-- return false;};- if (validateInputs(profileManagerId, selectedLlmProfiles, coverAllDoc)) {- return;- }- setIsCoverageLoading(true);- setCoverageTotal(0);- resetInfoMsgs();+ const handleSelectDefaultLLM = (llmProfileId) => {+ setSelectedLlmProfileId(llmProfileId);+ handleChange(+ llmProfileId,+ promptDetailsState?.prompt_id,+ "profile_manager"+ );+ };- const docId = selectedDoc?.document_id;- const isSummaryIndexed = [...summarizeIndexStatus].find(- (item) => item?.docId === docId && item?.isIndexed === true- );+ const handleTypeChange = (value) => {+ handleChange(value, promptDetailsState?.prompt_id, "enforce_type", true);+ };- if (- !isSummaryIndexed &&- details?.summarize_as_source &&- details?.summarize_llm_profile- ) {- // Summary needs to be indexed before running the prompt- handleIsRunLoading(selectedDoc?.document_id, selectedLlmProfileId, false);- setCoverageTotal(1);- handleCoverage(selectedLlmProfileId);- setAlertDetails({- type: "error",- content: `Summary needs to be indexed before running the prompt - ${selectedDoc?.document_name}.`,- });- return;- }+ const handleSpsLoading = (docId, isLoadingStatus) => {+ setSpsLoading((prev) => ({+ ...prev,+ [docId]: isLoadingStatus,+ }));+ };- if (runAllLLM) {- let selectedProfiles = llmProfiles;- if (selectedLlmProfiles?.length) {- selectedProfiles = llmProfiles.filter((profile) =>- selectedLlmProfiles.includes(profile?.profile_id)- );+ // Generate the result for the currently selected document+ const handleRun = (promptRunType, promptId, profileId, documentId) => {+ try {+ setPostHogCustomEvent("ps_prompt_run", {+ info: "Click on 'Run Prompt' button (Multi Pass)",+ });+ } catch (err) {+ // If an error occurs while setting custom posthog event, ignore it and continue}- for (const profile of selectedProfiles) {- handleDocOutputs(- docId,- promptDetailsState?.prompt_id,- profile?.profile_id,- true,- null- );- setIsCoverageLoading(true);-- handleIsRunLoading(selectedDoc?.document_id, profile?.profile_id, true);-- const startTime = Date.now();- handleRunApiRequest(docId, profile?.profile_id)- .then((res) => {- const data = res?.data || [];- const value = data[0]?.output;-- updatePromptOutputState(data, false);- handleDocOutputs(- docId,- promptDetailsState?.prompt_id,- profile?.profile_id,- false,- value- );+ const validateInputs = () => {+ if (!selectedDoc) {+ setAlertDetails({+ type: "error",+ content: "Document not selected",+ });+ return true;+ }- updateDocCoverage(- promptDetailsState?.prompt_id,- profile?.profile_id,- docId- );- })- .catch((err) => {- handleDocOutputs(- docId,- promptDetailsState?.prompt_id,- profile?.profile_id,- false,- null- );- setAlertDetails(- handleException(err, `Failed to generate output for ${docId}`)- );- })- .finally(() => {- handleIsRunLoading(docId, profile?.profile_id, false);- setIsCoverageLoading(false);- const endTime = Date.now();- updateTimers(startTime, endTime, profile?.profile_id);+ if (!promptKey) {+ setAlertDetails({+ type: "error",+ content: "Prompt key cannot be empty",});- runCoverageForAllDoc(coverAllDoc, profile.profile_id);- }- } else {- handleIsRunLoading(selectedDoc?.document_id, profileManagerId, true);- handleDocOutputs(- docId,- promptDetailsState?.prompt_id,- profileManagerId,- true,- null- );- const startTime = Date.now();- handleRunApiRequest(docId, profileManagerId)- .then((res) => {- const data = res?.data || [];- const value = data[0]?.output;+ return true;+ }- updatePromptOutputState(data, false);- updateDocCoverage(- promptDetailsState?.prompt_id,- profileManagerId,- docId- );- handleDocOutputs(- docId,- promptDetailsState?.prompt_id,- profileManagerId,- false,- value- );- setCoverageTotal(1);- })- .catch((err) => {- handleIsRunLoading(- selectedDoc?.document_id,- selectedLlmProfileId,- false- );- handleDocOutputs(- docId,- promptDetailsState?.prompt_id,- profileManagerId,- false,- null- );- setAlertDetails(- handleException(err, `Failed to generate output for ${docId}`)- );- })- .finally(() => {- setIsCoverageLoading(false);- handleIsRunLoading(selectedDoc?.document_id, profileManagerId, false);- const endTime = Date.now();- updateTimers(startTime, endTime, profileManagerId);- });- runCoverageForAllDoc(coverAllDoc, profileManagerId);- }- };+ if (!promptText) {+ setAlertDetails({+ type: "error",+ content: "Prompt cannot be empty",+ });+ return true;+ }- const updateTimers = (startTime, endTime, profileManagerId) => {- setTimers((prev) => {- prev[profileManagerId] = {- startTime,- endTime,+ return false;};- return prev;- });- };-- const runCoverageForAllDoc = (coverAllDoc, profileManagerId) => {- if (coverAllDoc) {- handleCoverage(profileManagerId);- }- };-- // Get the coverage for all the documents except the one that's currently selected- const handleCoverage = (profileManagerId) => {- const listOfDocsToProcess = [...listOfDocs].filter(- (item) => item?.document_id !== selectedDoc?.document_id- );- if (listOfDocsToProcess?.length === 0) {- setIsCoverageLoading(false);- return;- }+ if (validateInputs()) {+ return;+ }- let totalCoverageValue = 1;- listOfDocsToProcess.forEach((item) => {- const docId = item?.document_id;+ const docId = selectedDoc?.document_id;const isSummaryIndexed = [...summarizeIndexStatus].find(- (indexStatus) =>- indexStatus?.docId === docId && indexStatus?.isIndexed === true+ (item) => item?.docId === docId && item?.isIndexed === true);if (@@ -528,181 +224,54 @@ function PromptCard({details?.summarize_llm_profile) {// Summary needs to be indexed before running the prompt- totalCoverageValue++;- setCoverageTotal(totalCoverageValue);setAlertDetails({type: "error",- content: `Summary needs to be indexed before running the prompt - ${item?.document_name}.`,+ content: `Summary needs to be indexed before running the prompt - ${selectedDoc?.document_name}.`,});return;}- setIsCoverageLoading(true);- handleDocOutputs(- docId,- promptDetailsState?.prompt_id,- profileManagerId,- true,- null- );- handleRunApiRequest(docId, profileManagerId)- .then((res) => {- const data = res?.data || [];- const outputValue = data[0]?.output;- updateDocCoverage(- promptDetailsState?.prompt_id,- profileManagerId,- docId- );- handleDocOutputs(- docId,- promptDetailsState?.prompt_id,- profileManagerId,- false,- outputValue- );- })- .catch((err) => {- handleDocOutputs(- docId,- promptDetailsState?.prompt_id,- profileManagerId,- false,- null- );- setAlertDetails(- handleException(err, `Failed to generate output for ${docId}`)- );- })- .finally(() => {- totalCoverageValue++;- if (listOfDocsToProcess?.length >= totalCoverageValue) {- setIsCoverageLoading(false);- return;- }- setCoverageTotal(totalCoverageValue);- });- });- };-- const updateDocCoverage = (promptId, profileManagerId, docId) => {- setCoverage((prevCoverage) => {- const keySuffix = `${promptId}_${profileManagerId}`;- const key = singlePassExtractMode ? `singlepass_${keySuffix}` : keySuffix;-- // Create a shallow copy of the previous coverage state- const updatedCoverage = { ...prevCoverage };-- // If the key exists in the updated coverage object, update the docs_covered array- if (updatedCoverage[key]) {- if (!updatedCoverage[key].docs_covered.includes(docId)) {- updatedCoverage[key].docs_covered = [- ...updatedCoverage[key].docs_covered,- docId,- ];- }- } else {- // Otherwise, add the key to the updated coverage object with the new entry- updatedCoverage[key] = {- prompt_id: promptId,- profile_manager: profileManagerId,- docs_covered: [docId],- };- }-- return updatedCoverage;- });- };-- const handleRunApiRequest = async (docId, profileManagerId) => {- const promptId = promptDetailsState?.prompt_id;- const runId = generateUUID();- const maxWaitTime = 30 * 1000; // 30 seconds- const pollingInterval = 5000; // 5 seconds-- const body = {- document_id: docId,- id: promptId,+ handlePromptRunRequest(promptRunType, promptId, profileId, documentId);};- if (profileManagerId) {- body.profile_manager = profileManagerId;- let url = `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/fetch_response/${details?.tool_id}`;- if (!isSimplePromptStudio) {- body["run_id"] = runId;- } else {- body["sps_id"] = details?.tool_id;- url = promptRunApiSps;- }-- const requestOptions = {- method: "POST",- url,- headers: {- "X-CSRFToken": sessionDetails?.csrfToken,- "Content-Type": "application/json",- },- data: body,- };-- const makeApiRequest = (requestOptions) => {- return axiosPrivate(requestOptions);- };- const startTime = Date.now();- return pollForCompletion(- startTime,- requestOptions,- maxWaitTime,- pollingInterval,- makeApiRequest- )- .then((response) => {- return response;- })- .catch((err) => {- throw err;- });- }- };+ return (+ <>+ <PromptCardItems+ promptDetails={promptDetailsState}+ enforceTypeList={enforceTypeList}+ promptKey={promptKey}+ setPromptKey={setPromptKey}+ promptText={promptText}+ setPromptText={setPromptText}+ progressMsg={progressMsg}+ handleRun={handleRun}+ handleChange={handleChange}+ handleTypeChange={handleTypeChange}+ handleDelete={handleDelete}+ updateStatus={updateStatus}+ updatePlaceHolder={updatePlaceHolder}+ isCoverageLoading={isCoverageLoading}+ setOpenOutputForDoc={setOpenOutputForDoc}+ selectedLlmProfileId={selectedLlmProfileId}+ handleSelectDefaultLLM={handleSelectDefaultLLM}+ spsLoading={spsLoading}+ handleSpsLoading={handleSpsLoading}+ promptOutputs={promptOutputs}+ promptRunStatus={promptRunStatus}+ />+ <OutputForDocModal+ open={openOutputForDoc}+ setOpen={setOpenOutputForDoc}+ promptId={promptDetailsState?.prompt_id}+ promptKey={promptDetailsState?.prompt_key}+ profileManagerId={selectedLlmProfileId}+ />+ </>+ );+ }+);- return (- <>- <PromptCardItems- promptDetails={promptDetailsState}- enforceTypeList={enforceTypeList}- isRunLoading={isRunLoading}- promptKey={promptKey}- setPromptKey={setPromptKey}- promptText={promptText}- setPromptText={setPromptText}- coverage={coverage}- progressMsg={progressMsg}- handleRun={handleRun}- handleChange={handleChange}- handleTypeChange={handleTypeChange}- handleDelete={handleDelete}- updateStatus={updateStatus}- updatePlaceHolder={updatePlaceHolder}- isCoverageLoading={isCoverageLoading}- setOpenOutputForDoc={setOpenOutputForDoc}- selectedLlmProfileId={selectedLlmProfileId}- handleSelectDefaultLLM={handleSelectDefaultLLM}- timers={timers}- spsLoading={spsLoading}- handleSpsLoading={handleSpsLoading}- promptOutputs={promptOutputs}- />- <OutputForDocModal- open={openOutputForDoc}- setOpen={setOpenOutputForDoc}- promptId={promptDetailsState?.prompt_id}- promptKey={promptDetailsState?.prompt_key}- profileManagerId={selectedLlmProfileId}- docOutputs={docOutputs}- />- </>- );-}+PromptCard.displayName = "PromptCard";PromptCard.propTypes = {promptDetails: PropTypes.object.isRequired,@@ -712,6 +281,8 @@ PromptCard.propTypes = {promptOutputs: PropTypes.object.isRequired,enforceTypeList: PropTypes.array.isRequired,setUpdatedPromptsCopy: PropTypes.func.isRequired,+ handlePromptRunRequest: PropTypes.func.isRequired,+ promptRunStatus: PropTypes.object.isRequired,};export { PromptCard };diff --git a/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx b/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsxindex 1f8463a9c..e279f76bd 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptCardItems.jsx@@ -31,12 +31,10 @@ try {function PromptCardItems({promptDetails,enforceTypeList,- isRunLoading,promptKey,setPromptKey,promptText,setPromptText,- coverage,progressMsg,handleRun,handleChange,@@ -48,30 +46,27 @@ function PromptCardItems({setOpenOutputForDoc,selectedLlmProfileId,handleSelectDefaultLLM,- timers,spsLoading,handleSpsLoading,promptOutputs,+ promptRunStatus,}) {const {llmProfiles,selectedDoc,listOfDocs,- disableLlmOrDocChange,- singlePassExtractMode,isSinglePassExtractLoading,indexDocs,isSimplePromptStudio,isPublicSource,adapters,- defaultLlmProfile,} = useCustomToolStore();const [isEditingPrompt, setIsEditingPrompt] = useState(false);const [isEditingTitle, setIsEditingTitle] = useState(false);const [expandCard, setExpandCard] = useState(true);const [llmProfileDetails, setLlmProfileDetails] = useState([]);const [openIndexProfile, setOpenIndexProfile] = useState(null);- const [coverageCount, setCoverageCount] = useState(0);+ const [coverageCount] = useState(0);const [enabledProfiles, setEnabledProfiles] = useState(llmProfiles.map((profile) => profile.profile_id));@@ -137,23 +132,10 @@ function PromptCardItems({);};- const getCoverageData = () => {- const profileId = singlePassExtractMode- ? defaultLlmProfile- : selectedLlmProfileId;- const keySuffix = `${promptDetails?.prompt_id}_${profileId}`;- const key = singlePassExtractMode ? `singlepass_${keySuffix}` : keySuffix;- return coverage[key]?.docs_covered?.length || 0;- };-useEffect(() => {setExpandCard(true);}, [isSinglePassExtractLoading]);- useEffect(() => {- setCoverageCount(getCoverageData());- }, [singlePassExtractMode, coverage]);-useEffect(() => {getAdapterInfo(adapters);}, [llmProfiles, selectedLlmProfileId, enabledProfiles]);@@ -200,18 +182,14 @@ function PromptCardItems({handleChange={handleChange}isTextarea={true}placeHolder={updatePlaceHolder}+ isCoverageLoading={isCoverageLoading}/></div><>{!isSimplePromptStudio && (<><Divider className="prompt-card-divider" />- <Space- direction="vertical"- className={`prompt-card-comp-layout ${- !isRunLoading && "prompt-card-comp-layout-border"- }`}- >+ <Space direction="vertical" className="prompt-card-comp-layout"><div className="prompt-card-llm-profiles"><Space direction="horizontal"><Button@@ -250,9 +228,7 @@ function PromptCardItems({options={enforceTypeList}value={promptDetails?.enforce_type || null}disabled={- disableLlmOrDocChange.includes(- promptDetails?.prompt_id- ) ||+ isCoverageLoading ||isSinglePassExtractLoading ||indexDocs.includes(selectedDoc?.document_id) ||isPublicSource@@ -268,11 +244,9 @@ function PromptCardItems({<Row><PromptOutputpromptDetails={promptDetails}- isRunLoading={isRunLoading}handleRun={handleRun}selectedLlmProfileId={selectedLlmProfileId}handleSelectDefaultLLM={handleSelectDefaultLLM}- timers={timers}spsLoading={spsLoading}llmProfileDetails={llmProfileDetails}setOpenIndexProfile={setOpenIndexProfile}@@ -282,6 +256,7 @@ function PromptCardItems({setIsIndexOpen={setIsIndexOpen}enforceType={enforceType}promptOutputs={promptOutputs}+ promptRunStatus={promptRunStatus}/></Row></Collapse.Panel>@@ -298,12 +273,10 @@ function PromptCardItems({PromptCardItems.propTypes = {promptDetails: PropTypes.object.isRequired,enforceTypeList: PropTypes.array,- isRunLoading: PropTypes.object,promptKey: PropTypes.text,setPromptKey: PropTypes.func.isRequired,promptText: PropTypes.text,setPromptText: PropTypes.func.isRequired,- coverage: PropTypes.number.isRequired,progressMsg: PropTypes.object.isRequired,handleRun: PropTypes.func.isRequired,handleChange: PropTypes.func.isRequired,@@ -315,10 +288,10 @@ PromptCardItems.propTypes = {isCoverageLoading: PropTypes.bool.isRequired,setOpenOutputForDoc: PropTypes.func.isRequired,selectedLlmProfileId: PropTypes.string,- timers: PropTypes.object.isRequired,spsLoading: PropTypes.object,handleSpsLoading: PropTypes.func.isRequired,promptOutputs: PropTypes.object.isRequired,+ promptRunStatus: PropTypes.object.isRequired,};export { PromptCardItems };diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsxindex a897390aa..962c95132 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutput.jsx@@ -14,15 +14,18 @@ import {Image,Radio,Space,- Spin,Tooltip,Typography,} from "antd";import { motion, AnimatePresence } from "framer-motion";import CheckableTag from "antd/es/tag/CheckableTag";-import { displayPromptResult } from "../../../helpers/GetStaticData";-import { SpinnerLoader } from "../../widgets/spinner-loader/SpinnerLoader";+import {+ displayPromptResult,+ generateApiRunStatusId,+ PROMPT_RUN_API_STATUSES,+ PROMPT_RUN_TYPES,+} from "../../../helpers/GetStaticData";import { TokenUsage } from "../token-usage/TokenUsage";import { useWindowDimensions } from "../../../hooks/useWindowDimensions";import { useCustomToolStore } from "../../../store/custom-tool-store";@@ -51,12 +54,9 @@ try {function PromptOutput({promptDetails,- isRunLoading,handleRun,selectedLlmProfileId,handleSelectDefaultLLM,- timers,- spsLoading,llmProfileDetails,setOpenIndexProfile,enabledProfiles,@@ -65,13 +65,13 @@ function PromptOutput({setIsIndexOpen,enforceType,promptOutputs,+ promptRunStatus,}) {const { width: windowWidth } = useWindowDimensions();const componentWidth = windowWidth * 0.4;const {selectedDoc,singlePassExtractMode,- isSinglePassExtractLoading,isSimplePromptStudio,isPublicSource,defaultLlmProfile,@@ -142,16 +142,7 @@ function PromptOutput({<><Divider className="prompt-card-divider" /><div className="prompt-card-result prompt-card-div">- {isSinglePassExtractLoading ||- spsLoading[selectedDoc?.document_id] ? (- <Spin indicator={<SpinnerLoader size="small" />} />- ) : (- <Typography.Paragraph className="prompt-card-res font-size-12">- <div className="expanded-output">- <DisplayPromptResult output={promptOutput} />- </div>- </Typography.Paragraph>- )}+ <DisplayPromptResult output={promptOutput} /><div className="prompt-profile-run"><CopyPromptOutputBtnisDisabled={isTableExtraction}@@ -165,6 +156,7 @@ function PromptOutput({enforceType={enforceType}displayLlmProfile={false}promptOutputs={promptOutputs}+ promptRunStatus={promptRunStatus}/></div></div>@@ -195,6 +187,10 @@ function PromptOutput({promptOutputData = promptOutputs[promptOutputKey];}}++ const isPromptLoading =+ promptRunStatus?.[generateApiRunStatusId(docId, profileId)] ===+ PROMPT_RUN_API_STATUSES.RUNNING;return (<motion.divkey={profileId}@@ -230,32 +226,20 @@ function PromptOutput({{!singlePassExtractMode && (<TokenUsagetokenUsageId={tokenUsageId}- isLoading={- isRunLoading[- `${selectedDoc?.document_id}_${profileId}`- ]- }+ isLoading={isPromptLoading}/>)}</Typography.Text><Typography.Text className="prompt-cost-item"><PromptRunTimer- timer={timers[profileId]}- isLoading={- isRunLoading[- `${selectedDoc?.document_id}_${profileId}`- ]- }+ timer={promptOutputData?.timer}+ isLoading={isPromptLoading}/></Typography.Text><Typography.Text className="prompt-cost-item"><PromptRunCosttokenUsage={promptOutputData?.tokenUsage}- isLoading={- isRunLoading[- `${selectedDoc?.document_id}_${profileId}`- ]- }+ isLoading={isPromptLoading}/></Typography.Text></div>@@ -328,21 +312,12 @@ function PromptOutput({{isTableExtraction ? (<div />) : (- <>- {isRunLoading[- `${selectedDoc?.document_id}_${profileId}`- ] ? (- <Spin indicator={<SpinnerLoader size="small" />} />- ) : (- <Typography.Paragraph className="prompt-card-res font-size-12">- <div className="expanded-output">- <DisplayPromptResult- output={promptOutputData?.output}- />- </div>- </Typography.Paragraph>- )}- </>+ <DisplayPromptResult+ output={promptOutputData?.output}+ profileId={profileId}+ docId={selectedDoc?.document_id}+ promptRunStatus={promptRunStatus}+ />)}<div className="prompt-profile-run"><Tooltip title="Run LLM for current document">@@ -350,12 +325,15 @@ function PromptOutput({size="small"type="text"className="prompt-card-action-button"- onClick={() => handleRun(profileId, false)}- disabled={- isRunLoading[- `${selectedDoc?.document_id}_${profileId}`- ] || isPublicSource+ onClick={() =>+ handleRun(+ PROMPT_RUN_TYPES.RUN_ONE_PROMPT_ONE_LLM_ONE_DOC,+ promptDetails?.prompt_id,+ profileId,+ selectedDoc?.document_id+ )}+ disabled={isPromptLoading || isPublicSource}><PlayCircleOutlined className="prompt-card-actions-head" /></Button>@@ -365,12 +343,15 @@ function PromptOutput({size="small"type="text"className="prompt-card-action-button"- onClick={() => handleRun(profileId, true)}- disabled={- isRunLoading[- `${selectedDoc?.document_id}_${profileId}`- ] || isPublicSource+ onClick={() =>+ handleRun(+ PROMPT_RUN_TYPES.RUN_ONE_PROMPT_ONE_LLM_ALL_DOCS,+ promptDetails?.prompt_id,+ profileId,+ null+ )}+ disabled={isPromptLoading || isPublicSource}><PlayCircleFilled className="prompt-card-actions-head" /></Button>@@ -389,6 +370,7 @@ function PromptOutput({enforceType={enforceType}displayLlmProfile={true}promptOutputs={promptOutputs}+ promptRunStatus={promptRunStatus}/></div></div>@@ -406,20 +388,18 @@ function PromptOutput({PromptOutput.propTypes = {promptDetails: PropTypes.object.isRequired,- isRunLoading: PropTypes.object,handleRun: PropTypes.func.isRequired,handleSelectDefaultLLM: PropTypes.func.isRequired,selectedLlmProfileId: PropTypes.string,- timers: PropTypes.object.isRequired,- spsLoading: PropTypes.object,llmProfileDetails: PropTypes.array.isRequired,setOpenIndexProfile: PropTypes.func.isRequired,enabledProfiles: PropTypes.array.isRequired,setEnabledProfiles: PropTypes.func.isRequired,isNotSingleLlmProfile: PropTypes.bool.isRequired,setIsIndexOpen: PropTypes.func.isRequired,- enforceType: PropTypes.string.isRequired,+ enforceType: PropTypes.string,promptOutputs: PropTypes.object.isRequired,+ promptRunStatus: PropTypes.object.isRequired,};export { PromptOutput };diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutputExpandBtn.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutputExpandBtn.jsxindex 97f80fc93..4063c601a 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptOutputExpandBtn.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutputExpandBtn.jsx@@ -11,6 +11,7 @@ function PromptOutputExpandBtn({enforceType,displayLlmProfile,promptOutputs,+ promptRunStatus,}) {const [openModal, setOpenModal] = useState(false);@@ -34,6 +35,7 @@ function PromptOutputExpandBtn({enforceType={enforceType}displayLlmProfile={displayLlmProfile}promptOutputs={promptOutputs}+ promptRunStatus={promptRunStatus}/></>);@@ -42,9 +44,10 @@ function PromptOutputExpandBtn({PromptOutputExpandBtn.propTypes = {promptId: PropTypes.string.isRequired,llmProfiles: PropTypes.array.isRequired,- enforceType: PropTypes.string.isRequired,+ enforceType: PropTypes.string,displayLlmProfile: PropTypes.bool.isRequired,promptOutputs: PropTypes.object.isRequired,+ promptRunStatus: PropTypes.object.isRequired,};export { PromptOutputExpandBtn };diff --git a/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsx b/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsxindex 2e80ebc03..939277735 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptOutputsModal.jsx@@ -22,6 +22,7 @@ function PromptOutputsModal({enforceType,displayLlmProfile,promptOutputs,+ promptRunStatus,}) {const { singlePassExtractMode, selectedDoc } = useCustomToolStore();const { generatePromptOutputKey } = usePromptOutput();@@ -45,9 +46,10 @@ function PromptOutputsModal({<Row style={{ height: "85vh" }}>{llmProfiles.map((profile, index) => {const profileId = profile?.profile_id;+ const docId = selectedDoc?.document_id;const promptOutputKey = generatePromptOutputKey(promptId,- selectedDoc?.document_id,+ docId,profileId,singlePassExtractMode,true@@ -87,7 +89,12 @@ function PromptOutputsModal({pagination={10}/>) : (- <DisplayPromptResult output={promptOutputData?.output} />+ <DisplayPromptResult+ output={promptOutputData?.output}+ profileId={profileId}+ docId={docId}+ promptRunStatus={promptRunStatus}+ />)}</div></div>@@ -105,9 +112,10 @@ PromptOutputsModal.propTypes = {setOpen: PropTypes.func.isRequired,promptId: PropTypes.string.isRequired,llmProfiles: PropTypes.array.isRequired,- enforceType: PropTypes.string.isRequired,+ enforceType: PropTypes.string,displayLlmProfile: PropTypes.bool.isRequired,promptOutputs: PropTypes.object.isRequired,+ promptRunStatus: PropTypes.object.isRequired,};export { PromptOutputsModal };diff --git a/frontend/src/components/custom-tools/prompt-card/PromptRun.jsx b/frontend/src/components/custom-tools/prompt-card/PromptRun.jsxnew file mode 100644index 000000000..b73b5ad1e--- /dev/null+++ b/frontend/src/components/custom-tools/prompt-card/PromptRun.jsx@@ -0,0 +1,77 @@+import { useEffect } from "react";+import Cookies from "js-cookie";+import { usePromptRunQueueStore } from "../../../store/prompt-run-queue-store";+import usePromptRun from "../../../hooks/usePromptRun";+import { useCustomToolStore } from "../../../store/custom-tool-store";+import { usePromptRunStatusStore } from "../../../store/prompt-run-status-store";++const MAX_ACTIVE_APIS = 5;+/*+ Change this to 'true' to allow persistence of the prompt run state+ Right now, this feature cannot be support as the prompt studio details+ are not persisted accoss the entire application.+*/+const PROMPT_RUN_STATE_PERSISTENCE = false;++function PromptRun() {+ const activeApis = usePromptRunQueueStore((state) => state.activeApis);+ const queue = usePromptRunQueueStore((state) => state.queue);+ const setPromptRunQueue = usePromptRunQueueStore(+ (state) => state.setPromptRunQueue+ );+ const { runPrompt, syncPromptRunApisAndStatus } = usePromptRun();+ const promptRunStatus = usePromptRunStatusStore(+ (state) => state.promptRunStatus+ );+ const updateCustomTool = useCustomToolStore(+ (state) => state.updateCustomTool+ );++ useEffect(() => {+ // Retrieve queue from cookies on component load+ const queueData = Cookies.get("promptRunQueue");+ if (queueData && JSON.parse(queueData)?.length) {+ const promptApis = JSON.parse(queueData);+ syncPromptRunApisAndStatus(promptApis);+ }++ // Setup the beforeunload event handler to store queue in cookies+ const handleBeforeUnload = () => {+ if (!PROMPT_RUN_STATE_PERSISTENCE) return;+ const { queue } = usePromptRunQueueStore.getState(); // Get the latest state dynamically+ if (queue?.length) {+ Cookies.set("promptRunQueue", JSON.stringify(queue), {+ expires: 5 / 1440, // Expire in 5 minutes+ });+ }+ };++ window.addEventListener("beforeunload", handleBeforeUnload);++ return () => {+ window.removeEventListener("beforeunload", handleBeforeUnload); // Clean up event listener+ };+ }, [syncPromptRunApisAndStatus]);++ useEffect(() => {+ if (!queue?.length || activeApis >= MAX_ACTIVE_APIS) return;++ const canRunApis = MAX_ACTIVE_APIS - activeApis;+ const apisToRun = queue.slice(0, canRunApis);++ setPromptRunQueue({+ activeApis: activeApis + apisToRun.length,+ queue: queue.slice(apisToRun.length),+ });+ runPrompt(apisToRun);+ }, [activeApis, queue, setPromptRunQueue, runPrompt]);++ useEffect(() => {+ const isMultiPassExtractLoading = !!Object.keys(promptRunStatus).length;+ updateCustomTool({ isMultiPassExtractLoading });+ }, [promptRunStatus, updateCustomTool]);++ return null;+}++export { PromptRun };diff --git a/frontend/src/components/custom-tools/prompt-card/PromptRunCost.jsx b/frontend/src/components/custom-tools/prompt-card/PromptRunCost.jsxindex 01438442f..e91a9837a 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptRunCost.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptRunCost.jsx@@ -16,8 +16,8 @@ const PromptRunCost = memo(({ tokenUsage, isLoading }) => {PromptRunCost.displayName = "PromptRunCost";PromptRunCost.propTypes = {- tokenUsage: PropTypes.number,- isLoading: PropTypes.bool.isRequired,+ tokenUsage: PropTypes.object,+ isLoading: PropTypes.bool,};export { PromptRunCost };diff --git a/frontend/src/components/custom-tools/prompt-card/PromptRunTimer.jsx b/frontend/src/components/custom-tools/prompt-card/PromptRunTimer.jsxindex 0e2e2ef63..c0da7b152 100644--- a/frontend/src/components/custom-tools/prompt-card/PromptRunTimer.jsx+++ b/frontend/src/components/custom-tools/prompt-card/PromptRunTimer.jsx@@ -2,18 +2,18 @@ import { memo } from "react";import PropTypes from "prop-types";const PromptRunTimer = memo(({ timer, isLoading }) => {- if (!timer?.startTime || !timer?.endTime || isLoading) {+ if (!timer || isLoading) {return "Time: 0s";}- return `Time: ${Math.floor((timer?.endTime - timer?.startTime) / 1000)}s`;+ return `Time: ${timer}s`;});PromptRunTimer.displayName = "PromptRunTimer";PromptRunTimer.propTypes = {- timer: PropTypes.object,- isLoading: PropTypes.bool.isRequired,+ timer: PropTypes.number,+ isLoading: PropTypes.bool,};export { PromptRunTimer };diff --git a/frontend/src/components/custom-tools/prompt-card/RunAllPrompts.jsx b/frontend/src/components/custom-tools/prompt-card/RunAllPrompts.jsxnew file mode 100644index 000000000..cf1cc5b27--- /dev/null+++ b/frontend/src/components/custom-tools/prompt-card/RunAllPrompts.jsx@@ -0,0 +1,46 @@+import { PlayCircleFilled, PlayCircleOutlined } from "@ant-design/icons";+import { Button, Space, Tooltip } from "antd";+import { useCustomToolStore } from "../../../store/custom-tool-store";+import usePromptRun from "../../../hooks/usePromptRun";+import { PROMPT_RUN_TYPES } from "../../../helpers/GetStaticData";++function RunAllPrompts() {+ const { selectedDoc, isMultiPassExtractLoading, isPublicSource } =+ useCustomToolStore();+ const { handlePromptRunRequest } = usePromptRun();++ return (+ <Space>+ <Tooltip title="Run all prompts for all LLMs and current document">+ <Button+ icon={<PlayCircleOutlined className="prompt-card-actions-head" />}+ onClick={() =>+ handlePromptRunRequest(+ PROMPT_RUN_TYPES.RUN_ALL_PROMPTS_ALL_LLMS_ONE_DOC,+ null,+ null,+ selectedDoc?.document_id+ )+ }+ disabled={isMultiPassExtractLoading || isPublicSource}+ />+ </Tooltip>+ <Tooltip title="Run all prompts for all LLMs and documents">+ <Button+ icon={<PlayCircleFilled className="prompt-card-actions-head" />}+ onClick={() =>+ handlePromptRunRequest(+ PROMPT_RUN_TYPES.RUN_ALL_PROMPTS_ALL_LLMS_ALL_DOCS,+ null,+ null,+ null+ )+ }+ disabled={isMultiPassExtractLoading || isPublicSource}+ />+ </Tooltip>+ </Space>+ );+}++export { RunAllPrompts };diff --git a/frontend/src/components/custom-tools/token-usage/TokenUsage.jsx b/frontend/src/components/custom-tools/token-usage/TokenUsage.jsxindex 513f1633b..54b39fb2b 100644--- a/frontend/src/components/custom-tools/token-usage/TokenUsage.jsx+++ b/frontend/src/components/custom-tools/token-usage/TokenUsage.jsx@@ -32,8 +32,8 @@ function TokenUsage({ tokenUsageId, isLoading }) {}TokenUsage.propTypes = {- tokenUsageId: PropTypes.string.isRequired,- isLoading: PropTypes.bool.isRequired,+ tokenUsageId: PropTypes.string,+ isLoading: PropTypes.bool,};export { TokenUsage };diff --git a/frontend/src/components/custom-tools/tool-ide/ToolIde.jsx b/frontend/src/components/custom-tools/tool-ide/ToolIde.jsxindex fd2e1c2d8..019d4a954 100644--- a/frontend/src/components/custom-tools/tool-ide/ToolIde.jsx+++ b/frontend/src/components/custom-tools/tool-ide/ToolIde.jsx@@ -53,7 +53,7 @@ function ToolIde() {const {details,updateCustomTool,- disableLlmOrDocChange,+ isMultiPassExtractLoading,selectedDoc,indexDocs,pushIndexDoc,@@ -197,7 +197,7 @@ function ToolIde() {};const handleDocChange = (doc) => {- if (disableLlmOrDocChange?.length > 0) {+ if (isMultiPassExtractLoading) {setAlertDetails({type: "error",content: "Please wait for the run to complete",diff --git a/frontend/src/components/custom-tools/tools-main/ToolsMain.jsx b/frontend/src/components/custom-tools/tools-main/ToolsMain.jsxindex 718ca53e8..1006c7cdb 100644--- a/frontend/src/components/custom-tools/tools-main/ToolsMain.jsx+++ b/frontend/src/components/custom-tools/tools-main/ToolsMain.jsx@@ -26,7 +26,7 @@ function ToolsMain() {defaultLlmProfile,selectedDoc,updateCustomTool,- disableLlmOrDocChange,+ isMultiPassExtractLoading,isSimplePromptStudio,singlePassExtractMode,} = useCustomToolStore();@@ -56,7 +56,7 @@ function ToolsMain() {) : ("Combined Output"),- disabled: prompts?.length === 0 || disableLlmOrDocChange?.length > 0,+ disabled: prompts?.length === 0 || isMultiPassExtractLoading,},];diff --git a/frontend/src/components/custom-tools/tools-main/ToolsMainActionBtns.jsx b/frontend/src/components/custom-tools/tools-main/ToolsMainActionBtns.jsxindex 3868e1c0c..ee31930e7 100644--- a/frontend/src/components/custom-tools/tools-main/ToolsMainActionBtns.jsx+++ b/frontend/src/components/custom-tools/tools-main/ToolsMainActionBtns.jsx@@ -5,6 +5,7 @@ import { useNavigate } from "react-router-dom";import { useCustomToolStore } from "../../../store/custom-tool-store";import usePostHogEvents from "../../../hooks/usePostHogEvents";import { useTokenUsageStore } from "../../../store/token-usage-store";+import { RunAllPrompts } from "../prompt-card/RunAllPrompts";// Import single pass related componentslet RunSinglePassBtn;@@ -34,7 +35,7 @@ try {function ToolsMainActionBtns() {const {- disableLlmOrDocChange,+ isMultiPassExtractLoading,singlePassExtractMode,isSinglePassExtractLoading,isSimplePromptStudio,@@ -72,13 +73,12 @@ function ToolsMainActionBtns() {tokenUsage={tokenUsage?.[tokenUsageId]}/>)}+ {!singlePassExtractMode && <RunAllPrompts />}<Tooltip title="Output Analyzer"><Buttonicon={<BarChartOutlined />}onClick={handleOutputAnalyzerBtnClick}- disabled={- disableLlmOrDocChange?.length > 0 || isSinglePassExtractLoading- }+ disabled={isMultiPassExtractLoading || isSinglePassExtractLoading}/></Tooltip></Space>diff --git a/frontend/src/components/helpers/auth/PersistentLogin.js b/frontend/src/components/helpers/auth/PersistentLogin.jsindex b1268db1a..1ed1051d8 100644--- a/frontend/src/components/helpers/auth/PersistentLogin.js+++ b/frontend/src/components/helpers/auth/PersistentLogin.js@@ -5,6 +5,7 @@ import useSessionValid from "../../../hooks/useSessionValid";import { useSessionStore } from "../../../store/session-store";import { SocketMessages } from "../socket-messages/SocketMessages";import { GenericLoader } from "../../generic-loader/GenericLoader";+import { PromptRun } from "../../custom-tools/prompt-card/PromptRun";function PersistentLogin() {const [isLoading, setIsLoading] = useState(true);@@ -34,6 +35,7 @@ function PersistentLogin() {<><Outlet /><SocketMessages />+ <PromptRun /></>);}diff --git a/frontend/src/helpers/GetStaticData.js b/frontend/src/helpers/GetStaticData.jsindex f44d0284c..ef76a50a0 100644--- a/frontend/src/helpers/GetStaticData.js+++ b/frontend/src/helpers/GetStaticData.js@@ -523,6 +523,24 @@ const isValidJsonKey = (key) => {return regex.test(key);};+const PROMPT_RUN_TYPES = {+ RUN_ONE_PROMPT_ONE_LLM_ONE_DOC: "RUN_ONE_PROMPT_ONE_LLM_ONE_DOC",+ RUN_ONE_PROMPT_ONE_LLM_ALL_DOCS: "RUN_ONE_PROMPT_ONE_LLM_ALL_DOCS",+ RUN_ONE_PROMPT_ALL_LLMS_ONE_DOC: "RUN_ONE_PROMPT_ALL_LLMS_ONE_DOC",+ RUN_ONE_PROMPT_ALL_LLMS_ALL_DOCS: "RUN_ONE_PROMPT_ALL_LLMS_ALL_DOCS",+ RUN_ALL_PROMPTS_ALL_LLMS_ONE_DOC: "RUN_ALL_PROMPTS_ALL_LLMS_ONE_DOC",+ RUN_ALL_PROMPTS_ALL_LLMS_ALL_DOCS: "RUN_ALL_PROMPTS_ALL_LLMS_ALL_DOCS",+};++const PROMPT_RUN_API_STATUSES = {+ RUNNING: "RUNNING",+ COMPLETED: "COMPLETED",+};++const generateApiRunStatusId = (docId, profileId) => {+ return `${docId}__${profileId}`;+};+export {CONNECTOR_TYPE_MAP,O_AUTH_PROVIDERS,@@ -569,4 +587,7 @@ export {displayURL,formatNumberWithCommas,isValidJsonKey,+ PROMPT_RUN_TYPES,+ PROMPT_RUN_API_STATUSES,+ generateApiRunStatusId,};diff --git a/frontend/src/hooks/usePromptOutput.js b/frontend/src/hooks/usePromptOutput.jsindex ea241d554..28c3a6fba 100644--- a/frontend/src/hooks/usePromptOutput.js+++ b/frontend/src/hooks/usePromptOutput.js@@ -85,7 +85,7 @@ const usePromptOutput = () => {return url;};- const updatePromptOutputState = (data, isReset) => {+ const updatePromptOutputState = (data, isReset, timer = 0) => {const outputs = {};let isTokenUsageForSinglePassAdded = false;@@ -117,6 +117,7 @@ const usePromptOutput = () => {challengeData: item?.challenge_data,tokenUsage: item?.token_usage,output: item?.output,+ timer,};if (item?.is_single_pass_extract && isTokenUsageForSinglePassAdded)diff --git a/frontend/src/hooks/usePromptRun.js b/frontend/src/hooks/usePromptRun.jsnew file mode 100644index 000000000..421ab957f--- /dev/null+++ b/frontend/src/hooks/usePromptRun.js@@ -0,0 +1,219 @@+import {+ generateApiRunStatusId,+ generateUUID,+ pollForCompletion,+ PROMPT_RUN_API_STATUSES,+ PROMPT_RUN_TYPES,+} from "../helpers/GetStaticData";+import { useAlertStore } from "../store/alert-store";+import { useCustomToolStore } from "../store/custom-tool-store";+import { usePromptRunQueueStore } from "../store/prompt-run-queue-store";+import { usePromptRunStatusStore } from "../store/prompt-run-status-store";+import { useSessionStore } from "../store/session-store";+import { useAxiosPrivate } from "./useAxiosPrivate";+import { useExceptionHandler } from "./useExceptionHandler";+import usePromptOutput from "./usePromptOutput";++const usePromptRun = () => {+ const { pushPromptRunApi, freeActiveApi } = usePromptRunQueueStore();+ const { generatePromptOutputKey, updatePromptOutputState } =+ usePromptOutput();+ const { addPromptStatus, removePromptStatus } = usePromptRunStatusStore();+ const { details, llmProfiles, listOfDocs, selectedDoc } =+ useCustomToolStore();+ const { sessionDetails } = useSessionStore();+ const axiosPrivate = useAxiosPrivate();+ const { setAlertDetails } = useAlertStore();+ const handleException = useExceptionHandler();++ const makeApiRequest = (requestOptions) => axiosPrivate(requestOptions);++ const runPromptApi = (api) => {+ const [promptId, docId, profileId] = api.split("__");+ const runId = generateUUID();++ const body = {+ id: promptId,+ document_id: docId,+ profile_manager: profileId,+ run_id: runId,+ };++ const requestOptions = {+ method: "POST",+ url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/fetch_response/${details?.tool_id}`,+ headers: {+ "X-CSRFToken": sessionDetails?.csrfToken,+ "Content-Type": "application/json",+ },+ data: body,+ };++ const startTime = Date.now();+ const maxWaitTime = 30 * 1000; // 30 seconds+ const pollingInterval = 5000; // 5 seconds++ pollForCompletion(+ startTime,+ requestOptions,+ maxWaitTime,+ pollingInterval,+ makeApiRequest+ )+ .then((res) => {+ if (docId !== selectedDoc?.document_id) return;+ const data = res?.data || [];+ const timeTakenInSeconds = Math.floor((Date.now() - startTime) / 1000);+ updatePromptOutputState(data, false, timeTakenInSeconds);+ })+ .catch((err) => {+ setAlertDetails(+ handleException(err, "Failed to generate prompt output")+ );+ })+ .finally(() => {+ freeActiveApi();+ const statusKey = generateApiRunStatusId(docId, profileId);+ removePromptStatus(promptId, statusKey);+ });+ };++ const runPrompt = (listOfApis) => {+ if (!listOfApis?.length) return;+ listOfApis.forEach(runPromptApi);+ };++ const prepareApiRequests = (promptIds, profileIds, docIds) => {+ const apiRequestsToQueue = [];+ const promptRunApiStatus = [];+ const combinations = [];++ for (const promptId of promptIds) {+ for (const profileId of profileIds) {+ for (const docId of docIds) {+ combinations.push({ promptId, profileId, docId });+ }+ }+ }++ combinations.forEach(({ promptId, profileId, docId }) => {+ if (!promptRunApiStatus[promptId]) {+ promptRunApiStatus[promptId] = {};+ }+ const key = generatePromptOutputKey(+ promptId,+ docId,+ profileId,+ null,+ false+ );+ const statusKey = generateApiRunStatusId(docId, profileId);+ promptRunApiStatus[promptId][statusKey] = PROMPT_RUN_API_STATUSES.RUNNING;+ apiRequestsToQueue.push(key);+ });++ return { apiRequestsToQueue, promptRunApiStatus };+ };++ const syncPromptRunApisAndStatus = (promptApis) => {+ const promptRunApiStatus = {};++ promptApis.forEach((apiDetails) => {+ const [promptId, docId, profileId] = apiDetails.split("__");+ const statusKey = generateApiRunStatusId(docId, profileId);++ if (!promptRunApiStatus[promptId]) {+ promptRunApiStatus[promptId] = {};+ }++ promptRunApiStatus[promptId][statusKey] = PROMPT_RUN_API_STATUSES.RUNNING;+ });++ addPromptStatus(promptRunApiStatus);+ pushPromptRunApi(promptApis);+ };++ const handlePromptRunRequest = (+ promptRunType,+ promptId = null,+ profileId = null,+ docId = null+ ) => {+ const promptIds = promptId+ ? [promptId]+ : details?.prompts.map((p) => p.prompt_id) || [];+ const profileIds = profileId+ ? [profileId]+ : llmProfiles.map((p) => p.profile_id) || [];+ const docIds = docId ? [docId] : listOfDocs.map((d) => d.document_id) || [];++ let apiRequestsToQueue = [];+ let promptRunApiStatus = {};++ const paramsMap = {+ [PROMPT_RUN_TYPES.RUN_ONE_PROMPT_ONE_LLM_ONE_DOC]: {+ requiredParams: ["promptId", "profileId", "docId"],+ prompts: [promptId],+ profiles: [profileId],+ docs: [docId],+ },+ [PROMPT_RUN_TYPES.RUN_ONE_PROMPT_ONE_LLM_ALL_DOCS]: {+ requiredParams: ["promptId", "profileId"],+ prompts: [promptId],+ profiles: [profileId],+ docs: docIds,+ },+ [PROMPT_RUN_TYPES.RUN_ONE_PROMPT_ALL_LLMS_ONE_DOC]: {+ requiredParams: ["promptId", "docId"],+ prompts: [promptId],+ profiles: profileIds,+ docs: [docId],+ },+ [PROMPT_RUN_TYPES.RUN_ONE_PROMPT_ALL_LLMS_ALL_DOCS]: {+ requiredParams: ["promptId"],+ prompts: [promptId],+ profiles: profileIds,+ docs: docIds,+ },+ [PROMPT_RUN_TYPES.RUN_ALL_PROMPTS_ALL_LLMS_ONE_DOC]: {+ requiredParams: ["docId"],+ prompts: promptIds,+ profiles: profileIds,+ docs: [docId],+ },+ [PROMPT_RUN_TYPES.RUN_ALL_PROMPTS_ALL_LLMS_ALL_DOCS]: {+ requiredParams: [],+ prompts: promptIds,+ profiles: profileIds,+ docs: docIds,+ },+ };++ const params = paramsMap[promptRunType];+ if (!params) return;++ const paramValues = { promptId, profileId, docId };+ const missingParams = params.requiredParams.filter(+ (param) => !paramValues[param]+ );++ if (missingParams.length > 0) return;++ ({ apiRequestsToQueue, promptRunApiStatus } = prepareApiRequests(+ params.prompts,+ params.profiles,+ params.docs+ ));++ addPromptStatus(promptRunApiStatus);+ pushPromptRunApi(apiRequestsToQueue);+ };++ return {+ runPrompt,+ handlePromptRunRequest,+ syncPromptRunApisAndStatus,+ };+};++export default usePromptRun;diff --git a/frontend/src/store/custom-tool-store.js b/frontend/src/store/custom-tool-store.jsindex b7b8980b9..b61ff918e 100644--- a/frontend/src/store/custom-tool-store.js+++ b/frontend/src/store/custom-tool-store.js@@ -15,6 +15,7 @@ const defaultState = {rawIndexStatus: [],summarizeIndexStatus: [],singlePassExtractMode: false,+ isMultiPassExtractLoading: false,isSinglePassExtractLoading: false,isSimplePromptStudio: false,shareId: null,@@ -50,8 +51,7 @@ const useCustomToolStore = create((setState, getState) => ({setState(entireState);},updateCustomTool: (entireState) => {- const existingState = { ...getState() };- setState({ existingState, ...entireState });+ setState((state) => ({ state, ...entireState }));},addNewInstance: (type) => {const newState = { ...getState() };diff --git a/frontend/src/store/prompt-run-queue-store.js b/frontend/src/store/prompt-run-queue-store.jsnew file mode 100644index 000000000..35e888e1d--- /dev/null+++ b/frontend/src/store/prompt-run-queue-store.js@@ -0,0 +1,43 @@+import { create } from "zustand";++const defaultState = {+ activeApis: 0,+ queue: [],+};++const STORE_VARIABLES = { ...defaultState };++const usePromptRunQueueStore = create((setState, getState) => ({+ ...STORE_VARIABLES,+ setDefaultPromptRunQueue: () => {+ setState({ ...defaultState });+ },+ setPromptRunQueue: (promptRunQueueState) => {+ setState(promptRunQueueState);+ },+ pushPromptRunApi: (promptRunApiDetails) => {+ const existingState = { ...getState() };+ const newQueue = [...(existingState?.queue || []), ...promptRunApiDetails];+ setState({ existingState, ...{ queue: newQueue } });+ },+ freeActiveApi: (numOfApis = 1) => {+ const existingState = { ...getState() };+ const newActiveApis = existingState?.activeApis - numOfApis;++ if (newActiveApis < 0) return;++ setState({ ...existingState, ...{ activeApis: newActiveApis } });+ },+ removePromptRunApi: () => {+ const existingState = { ...getState() };+ const newActiveApis = existingState?.activeApis;+ const newQueue = [...(existingState?.queue || [])];+ if (!newQueue?.length) {+ return;+ }+ newQueue.shift();+ setState({ activeApis: newActiveApis - 1, queue: newQueue });+ },+}));++export { usePromptRunQueueStore };diff --git a/frontend/src/store/prompt-run-status-store.js b/frontend/src/store/prompt-run-status-store.jsnew file mode 100644index 000000000..dcc852a50--- /dev/null+++ b/frontend/src/store/prompt-run-status-store.js@@ -0,0 +1,50 @@+import { create } from "zustand";++const STORE_VARIABLES = {+ promptRunStatus: {},+};++const usePromptRunStatusStore = create((setState, getState) => ({+ ...STORE_VARIABLES,+ clearPromptStatus: () => {+ setState({ promptRunStatus: {} });+ },+ addPromptStatus: (promptStatus) => {+ setState((state) => {+ const currentStatus = state.promptRunStatus || {};+ const newStatus = { ...currentStatus };++ for (const promptId in promptStatus) {+ if (Object.hasOwn(promptStatus, promptId)) {+ newStatus[promptId] = {+ ...currentStatus[promptId],+ ...promptStatus[promptId],+ };+ }+ }++ return { promptRunStatus: newStatus };+ });+ },+ removePromptStatus: (promptId, key) => {+ setState((state) => {+ const currentStatus = state.promptRunStatus || {};+ const newStatus = { ...currentStatus };++ if (Object.hasOwn(newStatus, promptId)) {+ const promptStatus = { ...newStatus[promptId] };+ delete promptStatus[key];++ if (Object.keys(promptStatus).length === 0) {+ delete newStatus[promptId];+ } else {+ newStatus[promptId] = promptStatus;+ }+ }++ return { promptRunStatus: newStatus };+ });+ },+}));++export { usePromptRunStatusStore };