LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
import PropTypes from "prop-types";
import { useEffect, useRef, useState } from "react";
import { useSearchParams } from "react-router-dom";
import "./DocumentParser.css";
import { promptType } 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 { usePromptOutputStore } from "../../../store/prompt-output-store";
import { useSessionStore } from "../../../store/session-store";
import { EmptyState } from "../../widgets/empty-state/EmptyState";
import { PromptCardWrapper } from "../prompt-card/PromptCardWrapper";
let promptCardService;
let promptPatchApiSps;
let SpsPromptsEmptyState;
try {
const pcMod = await import(
"../../../plugins/prompt-card/prompt-card-service"
);
promptCardService = pcMod.promptCardService;
const helperMod = await import(
"../../../plugins/simple-prompt-studio/helper"
);
promptPatchApiSps = helperMod.promptPatchApiSps;
const spsMod = await import(
"../../../plugins/simple-prompt-studio/SpsPromptsEmptyState"
);
SpsPromptsEmptyState = spsMod.SpsPromptsEmptyState;
} catch {
// The component will remain null of it is not available
}
// Module-scoped to avoid per-render recompilation.
const UUID_RE =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const getFieldErrors = (err) => {
const data = err?.response?.data;
if (data?.type !== "validation_error" || !Array.isArray(data?.errors)) {
return {};
}
return data.errors.reduce((acc, e) => {
if (e?.attr) {
acc[e.attr] = e.detail || "Invalid value";
}
return acc;
}, {});
};
function DocumentParser({
addPromptInstance,
scrollToBottom,
setScrollToBottom,
}) {
const [enforceTypeList, setEnforceTypeList] = useState([]);
const [updatedPromptsCopy, setUpdatedPromptsCopy] = useState({});
const [isChallenge, setIsChallenge] = useState(false);
const [allTableSettings, setAllTableSettings] = useState([]);
const bottomRef = useRef(null);
const [searchParams, setSearchParams] = useSearchParams();
const {
details,
isSimplePromptStudio,
updateCustomTool,
getDropdownItems,
isChallengeEnabled,
isPublicSource,
} = useCustomToolStore();
const { sessionDetails } = useSessionStore();
const { setAlertDetails } = useAlertStore();
const axiosPrivate = useAxiosPrivate();
const handleException = useExceptionHandler();
const { promptOutputs } = usePromptOutputStore();
let promptCardApiService;
if (promptCardService && !isPublicSource) {
promptCardApiService = promptCardService();
}
useEffect(() => {
const outputTypeData = getDropdownItems("output_type") || {};
const dropdownList1 = Object.keys(outputTypeData)?.map((item) => {
return { value: outputTypeData[item] };
});
setEnforceTypeList(dropdownList1);
setIsChallenge(isChallengeEnabled);
if (promptCardApiService) {
promptCardApiService
.getAllTableSettings()
.then((res) => {
const data = res?.data;
setAllTableSettings(data || []);
})
.catch((err) => {
setAlertDetails(handleException(err));
});
}
return () => {
// Set the prompts with updated changes when the component is unmounted
const modifiedDetails = { ...useCustomToolStore.getState()?.details };
const modifiedPrompts = [...(modifiedDetails?.prompts || [])]?.map(
(item) => {
const itemPromptId = item?.prompt_id;
if (itemPromptId && updatedPromptsCopy[itemPromptId]) {
return updatedPromptsCopy[itemPromptId];
}
return item;
},
);
modifiedDetails["prompts"] = modifiedPrompts;
updateCustomTool({ details: modifiedDetails });
};
}, []);
useEffect(() => {
setIsChallenge(details.enable_challenge);
}, [details.enable_challenge]);
useEffect(() => {
if (scrollToBottom) {
// Scroll down to the lastest chat.
bottomRef.current?.scrollIntoView({ behavior: "smooth" });
setScrollToBottom(false);
}
}, [scrollToBottom]);
// Cross-link from Lookup Studio: scroll to a specific prompt.
useEffect(() => {
const scrollToPromptId = searchParams.get("scrollTo");
if (
!scrollToPromptId ||
!UUID_RE.test(scrollToPromptId) ||
!details?.prompts?.length
) {
return;
}
const el = document.querySelector(`[data-prompt-id="${scrollToPromptId}"]`);
if (el) {
el.scrollIntoView({ behavior: "smooth", block: "center" });
el.classList.add("highlighted-prompt");
setTimeout(() => el.classList.remove("highlighted-prompt"), 2000);
}
searchParams.delete("scrollTo");
setSearchParams(searchParams, { replace: true });
}, [details?.prompts, searchParams]);
const promptUrl = (urlPath) => {
return `/api/v1/unstract/${sessionDetails?.orgId}/prompt-studio/prompt/${urlPath}`;
};
const handleChangePromptCard = async (
name,
value,
promptId,
onFieldError,
) => {
const promptsAndNotes = details?.prompts || [];
if (name === "prompt_key") {
// Return if the prompt or the prompt key is empty
if (!value) {
return;
}
}
// 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,
);
if (index === -1) {
setAlertDetails({
type: "error",
content: "Prompt not found",
});
return;
}
const promptDetails = promptsAndNotes[index];
const body = {
[`${name}`]: value,
};
let url = promptUrl(promptDetails?.prompt_id + "/");
if (isSimplePromptStudio) {
url = promptPatchApiSps(promptDetails?.prompt_id);
}
const requestOptions = {
method: "PATCH",
url,
headers: {
"X-CSRFToken": sessionDetails?.csrfToken,
"Content-Type": "application/json",
},
data: body,
};
return axiosPrivate(requestOptions)
.then((res) => {
// Update the store with the modified prompt
const updatedPrompt = res?.data;
if (updatedPrompt) {
const modifiedDetails = { ...details };
const modifiedPrompts = [...(modifiedDetails?.prompts || [])];
const promptIndex = modifiedPrompts.findIndex(
(item) => item?.prompt_id === promptId,
);
if (promptIndex !== -1) {
modifiedPrompts[promptIndex] = updatedPrompt;
modifiedDetails["prompts"] = modifiedPrompts;
updateCustomTool({ details: modifiedDetails });
}
}
return res;
})
.catch((err) => {
// Reporting the rejection is opt-in: a caller that will not report it
// itself returns false and gets the alert, so a rejected value can
// never fail silently.
const handledInline = onFieldError?.(getFieldErrors(err));
if (!handledInline) {
setAlertDetails(handleException(err, "Failed to update"));
}
throw err;
});
};
const handleDelete = (promptId) => {
let url = promptUrl(promptId + "/");
if (isSimplePromptStudio) {
url = promptPatchApiSps(promptId);
}
const requestOptions = {
method: "DELETE",
url,
headers: {
"X-CSRFToken": sessionDetails?.csrfToken,
},
};
axiosPrivate(requestOptions)
.then(() => {
const modifiedDetails = { ...details };
const modifiedPrompts = [...(modifiedDetails?.prompts || [])].filter(
(item) => item?.prompt_id !== promptId,
);
modifiedDetails["prompts"] = modifiedPrompts;
updateCustomTool({ details: modifiedDetails });
})
.catch((err) => {
setAlertDetails(handleException(err, "Failed to delete"));
});
};
const getPromptOutputs = (promptId) => {
const keys = Object.keys(promptOutputs || {});
if (!keys?.length) {
return {};
}
const outputs = {};
keys.forEach((key) => {
if (key.startsWith(promptId)) {
outputs[key] = promptOutputs[key];
}
});
return outputs;
};
if (!details?.prompts?.length) {
if (isSimplePromptStudio && SpsPromptsEmptyState) {
return <SpsPromptsEmptyState />;
}
return (
<EmptyState
text="Add prompt or a note and choose the LLM profile"
btnText="Add Prompt"
handleClick={() => addPromptInstance(promptType.prompt)}
/>
);
}
return (
<div className="doc-parser-layout">
{details?.prompts?.map((item) => {
return (
<div key={item.prompt_id} data-prompt-id={item.prompt_id}>
<div className="doc-parser-pad-top" />
<PromptCardWrapper
item={item}
handleChangePromptCard={handleChangePromptCard}
handleDelete={handleDelete}
outputs={getPromptOutputs(item?.prompt_id)}
enforceTypeList={enforceTypeList}
allTableSettings={allTableSettings}
setAllTableSettings={setAllTableSettings}
setUpdatedPromptsCopy={setUpdatedPromptsCopy}
coverageCountData={item?.coverage}
isChallenge={isChallenge}
/>
<div ref={bottomRef} className="doc-parser-pad-bottom" />
</div>
);
})}
</div>
);
}
DocumentParser.propTypes = {
addPromptInstance: PropTypes.func.isRequired,
scrollToBottom: PropTypes.bool.isRequired,
setScrollToBottom: PropTypes.func.isRequired,
};
export { DocumentParser };