LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
UN-1722 [FEAT] Add export reminder for Prompt Studio projects in use (#1547)
Athul79c8d07dc741
diff --git a/backend/prompt_studio/prompt_studio_core_v2/constants.py b/backend/prompt_studio/prompt_studio_core_v2/constants.pyindex 0c846bdbb..645b67431 100644--- a/backend/prompt_studio/prompt_studio_core_v2/constants.py+++ b/backend/prompt_studio/prompt_studio_core_v2/constants.py@@ -180,6 +180,15 @@ class IndexingConstants:RUN_ID = "run_id"+class DeploymentType:+ """Deployment types where Prompt Studio tools can be used."""++ API_DEPLOYMENT = "API Deployment"+ ETL_PIPELINE = "ETL Pipeline"+ TASK_PIPELINE = "Task Pipeline"+ HUMAN_QUALITY_REVIEW = "Human in the Loop"++class DefaultValues:"""Default values used throughout the prompt studio helper."""diff --git a/backend/prompt_studio/prompt_studio_core_v2/exceptions.py b/backend/prompt_studio/prompt_studio_core_v2/exceptions.pyindex ebf4911e3..c4a7ffd20 100644--- a/backend/prompt_studio/prompt_studio_core_v2/exceptions.py+++ b/backend/prompt_studio/prompt_studio_core_v2/exceptions.py@@ -111,3 +111,8 @@ class PromptNotRun(APIException):if code is not None:self.code = codesuper().__init__(detail, code)+++class DeploymentUsageCheckError(APIException):+ status_code = 500+ default_detail = "Failed to check deployment usage"diff --git a/backend/prompt_studio/prompt_studio_core_v2/urls.py b/backend/prompt_studio/prompt_studio_core_v2/urls.pyindex 8989b355e..228368544 100644--- a/backend/prompt_studio/prompt_studio_core_v2/urls.py+++ b/backend/prompt_studio/prompt_studio_core_v2/urls.py@@ -55,6 +55,10 @@ prompt_studio_project_transfer = PromptStudioCoreView.as_view({"get": "export_project", "post": "import_project"})+prompt_studio_deployment_usage = PromptStudioCoreView.as_view(+ {"get": "check_deployment_usage"}+)+urlpatterns = format_suffix_patterns([@@ -134,5 +138,10 @@ urlpatterns = format_suffix_patterns(prompt_studio_project_transfer,name="prompt_studio_project_transfer_import",),+ path(+ "prompt-studio/<uuid:pk>/check_deployment_usage/",+ prompt_studio_deployment_usage,+ name="prompt_studio_deployment_usage",+ ),])diff --git a/backend/prompt_studio/prompt_studio_core_v2/views.py b/backend/prompt_studio/prompt_studio_core_v2/views.pyindex d7cec3c6f..5e1f0d2a3 100644--- a/backend/prompt_studio/prompt_studio_core_v2/views.py+++ b/backend/prompt_studio/prompt_studio_core_v2/views.py@@ -5,12 +5,14 @@ from datetime import datetimefrom typing import Anyfrom account_v2.custom_exceptions import DuplicateData+from api_v2.models import APIDeploymentfrom django.db import IntegrityErrorfrom django.db.models import QuerySetfrom django.http import HttpRequest, HttpResponsefrom file_management.constants import FileInformationKey as FileKeyfrom file_management.exceptions import FileNotFoundfrom permissions.permission import IsOwner, IsOwnerOrSharedUserOrSharedToOrg+from pipeline_v2.models import Pipelinefrom plugins import get_pluginfrom rest_framework import status, viewsetsfrom rest_framework.decorators import action@@ -21,6 +23,7 @@ from tool_instance_v2.models import ToolInstancefrom utils.file_storage.helpers.prompt_studio_file_helper import PromptStudioFileHelperfrom utils.user_context import UserContextfrom utils.user_session import UserSessionUtils+from workflow_manager.endpoint_v2.models import WorkflowEndpointfrom prompt_studio.prompt_profile_manager_v2.constants import (ProfileManagerErrors,@@ -29,6 +32,7 @@ from prompt_studio.prompt_profile_manager_v2.constants import (from prompt_studio.prompt_profile_manager_v2.models import ProfileManagerfrom prompt_studio.prompt_profile_manager_v2.serializers import ProfileManagerSerializerfrom prompt_studio.prompt_studio_core_v2.constants import (+ DeploymentType,FileViewTypes,ToolStudioErrors,ToolStudioPromptKeys,@@ -37,6 +41,7 @@ from prompt_studio.prompt_studio_core_v2.document_indexing_service import (DocumentIndexingService,)from prompt_studio.prompt_studio_core_v2.exceptions import (+ DeploymentUsageCheckError,IndexingAPIError,MaxProfilesReachedError,ToolDeleteError,@@ -119,27 +124,105 @@ class PromptStudioCoreView(viewsets.ModelViewSet):organization_id = UserSessionUtils.get_organization_id(self.request)instance.delete(organization_id)+ def _check_tool_usage_in_workflows(self, instance: CustomTool) -> tuple[bool, set]:+ """Check if a tool is being used in any workflows.++ Args:+ instance: The CustomTool instance to check++ Returns:+ Tuple of (is_used: bool, dependent_workflows: set)+ """+ registry = getattr(instance, "prompt_studio_registries", None)+ if not registry:+ return False, set()++ dependent_wfs = set(+ ToolInstance.objects.filter(tool_id=registry.pk)+ .values_list("workflow_id", flat=True)+ .distinct()+ )+ return bool(dependent_wfs), dependent_wfs++ def _get_deployment_types(self, workflow_ids: set) -> set:+ """Get all deployment types where the tool is used.++ Args:+ workflow_ids: Set of workflow IDs to check++ Returns:+ Set of deployment type strings+ """+ deployment_types: set = set()++ # Check API Deployments (include inactive to prevent drift)+ if APIDeployment.objects.filter(workflow_id__in=workflow_ids).exists():+ deployment_types.add(DeploymentType.API_DEPLOYMENT)++ # Check Pipelines using mapping instead of if/elif+ pipeline_type_mapping = {+ Pipeline.PipelineType.ETL: DeploymentType.ETL_PIPELINE,+ Pipeline.PipelineType.TASK: DeploymentType.TASK_PIPELINE,+ }+ pipelines = (+ Pipeline.objects.filter(workflow_id__in=workflow_ids)+ .values_list("pipeline_type", flat=True)+ .distinct()+ )+ for pipeline_type in pipelines:+ if pipeline_type in pipeline_type_mapping:+ deployment_types.add(pipeline_type_mapping[pipeline_type])++ # Check for Manual Review+ if WorkflowEndpoint.objects.filter(+ workflow_id__in=workflow_ids,+ connection_type=WorkflowEndpoint.ConnectionType.MANUALREVIEW,+ ).exists():+ deployment_types.add(DeploymentType.HUMAN_QUALITY_REVIEW)++ return deployment_types++ def _format_deployment_types_message(self, deployment_types: set) -> str:+ """Format deployment types into human-readable message.++ Args:+ deployment_types: Set of deployment type strings++ Returns:+ Formatted message string or empty string if no types+ """+ if not deployment_types:+ return ""++ types_list = sorted(deployment_types)+ if len(types_list) == 1:+ types_text = types_list[0]+ elif len(types_list) == 2:+ types_text = f"{types_list[0]} or {types_list[1]}"+ else:+ types_text = ", ".join(types_list[:-1]) + f", or {types_list[-1]}"++ return (+ f"You have made changes to this Prompt Studio project. "+ f"This project is used in {types_text}. "+ f"Please export it for the changes to take effect in the deployment(s)."+ )+def destroy(self, request: Request, *args: tuple[Any], **kwargs: dict[str, Any]) -> Response:instance: CustomTool = self.get_object()# Checks if tool is exported- if hasattr(instance, "prompt_studio_registry"):- exported_tool_instances_in_use = ToolInstance.objects.filter(- tool_id__exact=instance.prompt_studio_registry.pk+ is_used, dependent_wfs = self._check_tool_usage_in_workflows(instance)+ if is_used:+ logger.info(+ f"Cannot destroy custom tool {instance.tool_id},"+ f" depended by workflows {dependent_wfs}"+ )+ raise ToolDeleteError(+ "Failed to delete Prompt Studio project; it's used in other workflows."+ "Delete its usages first.")- dependent_wfs = set()- for tool_instance in exported_tool_instances_in_use:- dependent_wfs.add(tool_instance.workflow_id)- if len(dependent_wfs) > 0:- logger.info(- f"Cannot destroy custom tool {instance.tool_id},"- f" depended by workflows {dependent_wfs}"- )- raise ToolDeleteError(- "Failed to delete tool, its used in other workflows. "- "Delete its usages first"- )return super().destroy(request, *args, **kwargs)def partial_update(@@ -569,9 +652,9 @@ class PromptStudioCoreView(viewsets.ModelViewSet):def export_tool_info(self, request: Request, pk: Any = None) -> Response:custom_tool = self.get_object()serialized_instances = None- if hasattr(custom_tool, "prompt_studio_registry"):+ if hasattr(custom_tool, "prompt_studio_registries"):serialized_instances = PromptStudioRegistryInfoSerializer(- custom_tool.prompt_studio_registry+ custom_tool.prompt_studio_registries).datareturn Response(serialized_instances)@@ -672,3 +755,41 @@ class PromptStudioCoreView(viewsets.ModelViewSet):{"error": "Failed to import project"},status=status.HTTP_500_INTERNAL_SERVER_ERROR,)++ @action(detail=True, methods=["get"])+ def check_deployment_usage(self, request: Request, pk: Any = None) -> Response:+ """Check if the Prompt Studio project is used in any deployments.++ This endpoint checks if the exported tool from this project is being used in:+ - API Deployments+ - ETL Pipelines+ - Task Pipelines+ - Manual Review (Human Quality Review)++ Returns:+ Response: Contains is_used flag and deployment types where it's used+ """+ try:+ instance: CustomTool = self.get_object()+ is_used, workflow_ids = self._check_tool_usage_in_workflows(instance)++ deployment_info: dict = {+ "is_used": is_used,+ "deployment_types": [],+ "message": "",+ }++ if is_used and workflow_ids:+ deployment_types = self._get_deployment_types(workflow_ids)+ deployment_info["deployment_types"] = list(deployment_types)+ deployment_info["message"] = self._format_deployment_types_message(+ deployment_types+ )++ return Response(deployment_info, status=status.HTTP_200_OK)++ except Exception as e:+ logger.error(f"Error checking deployment usage for tool {pk}: {e}")+ raise DeploymentUsageCheckError(+ detail=f"Failed to check deployment usage: {str(e)}"+ )diff --git a/frontend/src/components/custom-tools/document-parser/DocumentParser.jsx b/frontend/src/components/custom-tools/document-parser/DocumentParser.jsxindex 2fd73b97d..cc16a13f1 100644--- a/frontend/src/components/custom-tools/document-parser/DocumentParser.jsx+++ b/frontend/src/components/custom-tools/document-parser/DocumentParser.jsx@@ -116,6 +116,10 @@ function DocumentParser({}}+ // Mark that changes have been made when any prompt field is modified+ const { setHasUnsavedChanges } = useCustomToolStore.getState();+ setHasUnsavedChanges(true);+const index = promptsAndNotes.findIndex((item) => item?.prompt_id === promptId);diff --git a/frontend/src/components/custom-tools/export-reminder-bar/ExportReminderBar.css b/frontend/src/components/custom-tools/export-reminder-bar/ExportReminderBar.cssnew file mode 100644index 000000000..0c9f6bc7e--- /dev/null+++ b/frontend/src/components/custom-tools/export-reminder-bar/ExportReminderBar.css@@ -0,0 +1,31 @@+.export-reminder-bar {+ position: sticky;+ top: 0;+ z-index: 100;+ width: 100%;+}++.export-reminder-bar .ant-alert {+ border-radius: 0;+ border: none;+ background-color: #fffbe6;+ border-bottom: 1px solid #ffe58f;+}++.export-reminder-content {+ width: 100%;+ display: flex;+ align-items: center;+ justify-content: center;+ gap: 12px;+}++.export-reminder-text {+ flex: 1;+ max-width: 800px;+ text-align: left;+}++.export-reminder-button {+ margin-left: auto;+}diff --git a/frontend/src/components/custom-tools/export-reminder-bar/ExportReminderBar.jsx b/frontend/src/components/custom-tools/export-reminder-bar/ExportReminderBar.jsxnew file mode 100644index 000000000..13967e5ad--- /dev/null+++ b/frontend/src/components/custom-tools/export-reminder-bar/ExportReminderBar.jsx@@ -0,0 +1,43 @@+import { ExclamationCircleOutlined } from "@ant-design/icons";+import { Alert, Button, Space } from "antd";+import PropTypes from "prop-types";+import "./ExportReminderBar.css";++function ExportReminderBar({ message, onExport, isExporting }) {+ return (+ <div className="export-reminder-bar">+ <Alert+ message={+ <Space className="export-reminder-content">+ <ExclamationCircleOutlined />+ <span className="export-reminder-text">{message}</span>+ <Button+ type="primary"+ size="small"+ onClick={onExport}+ loading={isExporting}+ className="export-reminder-button"+ >+ Export Now+ </Button>+ </Space>+ }+ type="warning"+ banner+ closable={false}+ />+ </div>+ );+}++ExportReminderBar.propTypes = {+ message: PropTypes.string.isRequired,+ onExport: PropTypes.func.isRequired,+ isExporting: PropTypes.bool,+};++ExportReminderBar.defaultProps = {+ isExporting: false,+};++export { ExportReminderBar };diff --git a/frontend/src/components/custom-tools/header/Header.jsx b/frontend/src/components/custom-tools/header/Header.jsxindex 345a8e6f7..ed05f463e 100644--- a/frontend/src/components/custom-tools/header/Header.jsx+++ b/frontend/src/components/custom-tools/header/Header.jsx@@ -44,7 +44,8 @@ function Header({setOpenCloneModal,}) {const [isExportLoading, setIsExportLoading] = useState(false);- const { details, isPublicSource } = useCustomToolStore();+ const { details, isPublicSource, markChangesAsExported } =+ useCustomToolStore();const { sessionDetails } = useSessionStore();const { setAlertDetails } = useAlertStore();const axiosPrivate = useAxiosPrivate();@@ -92,6 +93,8 @@ function Header({type: "success",content: "Custom tool exported successfully",});+ // Clear the export reminder after successful export+ markChangesAsExported();}).catch((err) => {if (err?.response?.data?.errors[0]?.code === "warning") {@@ -117,7 +120,7 @@ function Header({handleExport(selectedUsers, toolDetail, isSharedWithEveryone, true);setConfirmModalVisible(false);- }, [lastExportParams]);+ }, [lastExportParams, handleExport]);const handleShare = (isEdit) => {try {diff --git a/frontend/src/components/custom-tools/tool-ide/ToolIde.jsx b/frontend/src/components/custom-tools/tool-ide/ToolIde.jsxindex 63bb241b6..7de5d7078 100644--- a/frontend/src/components/custom-tools/tool-ide/ToolIde.jsx+++ b/frontend/src/components/custom-tools/tool-ide/ToolIde.jsx@@ -1,5 +1,5 @@import { Col, Row } from "antd";-import { useState, useEffect } from "react";+import { useState, useEffect, useCallback, useRef } from "react";import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate";import { useExceptionHandler } from "../../../hooks/useExceptionHandler";@@ -10,6 +10,7 @@ import { DocumentManager } from "../document-manager/DocumentManager";import { Header } from "../header/Header";import { SettingsModal } from "../settings-modal/SettingsModal";import { ToolsMain } from "../tools-main/ToolsMain";+import { ExportReminderBar } from "../export-reminder-bar/ExportReminderBar";import "./ToolIde.css";import usePostHogEvents from "../../../hooks/usePostHogEvents.js";import { PageTitle } from "../../widgets/page-title/PageTitle.jsx";@@ -37,6 +38,7 @@ try {}function ToolIde() {const [openSettings, setOpenSettings] = useState(false);+ const customToolStore = useCustomToolStore();const {details,updateCustomTool,@@ -47,7 +49,11 @@ function ToolIde() {deleteIndexDoc,shareId,isPublicSource,- } = useCustomToolStore();+ hasUnsavedChanges,+ deploymentUsageInfo,+ setDeploymentUsageInfo,+ markChangesAsExported,+ } = customToolStore;const { sessionDetails } = useSessionStore();const { setAlertDetails } = useAlertStore();const axiosPrivate = useAxiosPrivate();@@ -57,6 +63,11 @@ function ToolIde() {const [openShareConfirmation, setOpenShareConfirmation] = useState(false);const [openShareModal, setOpenShareModal] = useState(false);const [openCloneModal, setOpenCloneModal] = useState(false);+ const [showExportReminder, setShowExportReminder] = useState(false);+ const [isExporting, setIsExporting] = useState(false);+ const isCheckingUsageRef = useRef(false);+ const hasCheckedForCurrentSessionRef = useRef(false);+ const abortControllerRef = useRef(null);useEffect(() => {if (openShareModal) {@@ -77,6 +88,136 @@ function ToolIde() {}}, [openShareModal]);+ // Check deployment usage when there are unsaved changes (only once per edit session)+ const checkDeploymentUsage = useCallback(async () => {+ const currentHasUnsavedChanges =+ useCustomToolStore.getState().hasUnsavedChanges;++ // Skip if: no tool, no unsaved changes, already checking, or already checked this session+ if (+ !details?.tool_id ||+ !currentHasUnsavedChanges ||+ isCheckingUsageRef.current ||+ hasCheckedForCurrentSessionRef.current+ ) {+ return;+ }++ // Cancel any pending request+ if (abortControllerRef.current) {+ abortControllerRef.current.abort();+ }+ abortControllerRef.current = new AbortController();++ isCheckingUsageRef.current = true;+ hasCheckedForCurrentSessionRef.current = true;++ try {+ const response = await axiosPrivate.get(+ `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/${details?.tool_id}/check_deployment_usage/`,+ { signal: abortControllerRef.current.signal }+ );++ const usageInfo = response.data;+ setDeploymentUsageInfo(usageInfo);++ // Only show reminder if there are still unsaved changes+ const stillHasUnsavedChanges =+ useCustomToolStore.getState().hasUnsavedChanges;+ if (stillHasUnsavedChanges && usageInfo?.is_used && usageInfo?.message) {+ setShowExportReminder(true);+ } else {+ setShowExportReminder(false);+ }+ } catch (error) {+ // Ignore abort errors+ if (error.name === "AbortError" || error.name === "CanceledError") {+ return;+ }+ console.error("Error checking deployment usage:", error);+ setShowExportReminder(false);+ } finally {+ isCheckingUsageRef.current = false;+ }+ }, [+ details?.tool_id,+ axiosPrivate,+ sessionDetails?.orgId,+ setDeploymentUsageInfo,+ ]);++ // Trigger deployment check when unsaved changes are detected+ useEffect(() => {+ if (hasUnsavedChanges) {+ checkDeploymentUsage();+ } else {+ // Reset the check flag when changes are cleared (after export)+ hasCheckedForCurrentSessionRef.current = false;+ setShowExportReminder(false);+ }+ }, [hasUnsavedChanges, checkDeploymentUsage]);++ // Cleanup abort controller on unmount+ useEffect(() => {+ return () => {+ if (abortControllerRef.current) {+ abortControllerRef.current.abort();+ }+ };+ }, []);++ // Handle export from reminder bar+ const handleExportFromReminder = useCallback(async () => {+ setIsExporting(true);+ try {+ const requestOptions = {+ method: "POST",+ url: `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/export/${details?.tool_id}`,+ headers: {+ "X-CSRFToken": sessionDetails?.csrfToken,+ "Content-Type": "application/json",+ },+ data: {+ is_shared_with_org: false,+ user_id: [details?.created_by || sessionDetails?.userId],+ force_export: false,+ },+ };++ await axiosPrivate(requestOptions);++ setAlertDetails({+ type: "success",+ content: "Custom tool exported successfully",+ });++ // Mark changes as exported+ markChangesAsExported();+ setShowExportReminder(false);++ try {+ setPostHogCustomEvent("ps_exported_from_reminder", {+ info: "Exported from reminder bar",+ tool_name: details?.tool_name,+ });+ } catch (err) {+ // Ignore posthog errors+ }+ } catch (err) {+ setAlertDetails(handleException(err, "Failed to export"));+ } finally {+ setIsExporting(false);+ }+ }, [+ axiosPrivate,+ details,+ sessionDetails,+ setAlertDetails,+ handleException,+ markChangesAsExported,+ setPostHogCustomEvent,+ ]);+const generateIndex = async (doc) => {const docId = doc?.document_id;@@ -186,6 +327,13 @@ function ToolIde() {<div className="tool-ide-layout"><PageTitle title={details?.tool_name} />{isPublicSource && HeaderPublic && <HeaderPublic />}+ {showExportReminder && deploymentUsageInfo?.message && (+ <ExportReminderBar+ message={deploymentUsageInfo.message}+ onExport={handleExportFromReminder}+ isExporting={isExporting}+ />+ )}<div><HeaderhandleUpdateTool={handleUpdateTool}diff --git a/frontend/src/components/helpers/custom-tools/CustomToolsHelper.js b/frontend/src/components/helpers/custom-tools/CustomToolsHelper.jsindex fe3a5f5e6..54f56abdb 100644--- a/frontend/src/components/helpers/custom-tools/CustomToolsHelper.js+++ b/frontend/src/components/helpers/custom-tools/CustomToolsHelper.js@@ -37,6 +37,9 @@ function CustomToolsHelper() {llmProfiles: [],selectedDoc: null,adapters: [],+ hasUnsavedChanges: false,+ deploymentUsageInfo: null,+ lastExportedAt: null,};const reqOpsPromptStudio = {diff --git a/frontend/src/store/custom-tool-store.js b/frontend/src/store/custom-tool-store.jsindex 288686828..04d8b2360 100644--- a/frontend/src/store/custom-tool-store.js+++ b/frontend/src/store/custom-tool-store.js@@ -23,6 +23,9 @@ const defaultState = {isChallengeEnabled: false,adapters: [],selectedHighlight: null,+ hasUnsavedChanges: false,+ lastExportedAt: null,+ deploymentUsageInfo: null,};const defaultPromptInstance = {@@ -49,7 +52,13 @@ const useCustomToolStore = create((setState, getState) => ({setState({ ...defaultState });},setCustomTool: (entireState) => {- setState(entireState);+ // Reset unsaved changes when loading a new tool+ setState({+ ...entireState,+ hasUnsavedChanges: false,+ deploymentUsageInfo: entireState?.deploymentUsageInfo ?? null,+ lastExportedAt: entireState?.lastExportedAt ?? null,+ });},updateCustomTool: (entireState) => {setState((state) => ({ state, ...entireState }));@@ -68,6 +77,8 @@ const useCustomToolStore = create((setState, getState) => ({promptsAndNotes.push(newNote);}newState["details"]["prompts"] = [...promptsAndNotes];+ // Mark as having unsaved changes when a new prompt/note is added+ newState["hasUnsavedChanges"] = true;setState({ ...newState });},deleteInstance: (promptId) => {@@ -77,6 +88,8 @@ const useCustomToolStore = create((setState, getState) => ({(item) => item?.prompt_id !== promptId);newState["details"]["prompts"] = filteredData;+ // Mark as having unsaved changes when a prompt/note is deleted+ newState["hasUnsavedChanges"] = true;setState({ ...newState });},getDropdownItems: (propertyName) => {@@ -100,6 +113,21 @@ const useCustomToolStore = create((setState, getState) => ({existingState.indexDocs = docs;setState(existingState);},+ setHasUnsavedChanges: (hasChanges) => {+ setState({ hasUnsavedChanges: hasChanges });+ },+ setLastExportedAt: (timestamp) => {+ setState({ lastExportedAt: timestamp });+ },+ setDeploymentUsageInfo: (info) => {+ setState({ deploymentUsageInfo: info });+ },+ markChangesAsExported: () => {+ setState({+ hasUnsavedChanges: false,+ lastExportedAt: new Date().toISOString(),+ });+ },}));export { useCustomToolStore };