Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
import { artifactApiClient } from "@/api/AxiosClient";
import { getWithMintRetry } from "@/api/artifactUrls";
import { ArtifactApiResponse } from "@/api/types";
import { Skeleton } from "@/components/ui/skeleton";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { CodeEditor } from "@/routes/workflows/components/CodeEditor";
import { useQueries } from "@tanstack/react-query";
// https://stackoverflow.com/a/60338028
function format(html: string) {
const tab = "\t";
let result = "";
let indent = "";
html.split(/>\s*</).forEach(function (element) {
if (element.match(/^\/\w/)) {
indent = indent.substring(tab.length);
}
result += indent + "<" + element + ">\r\n";
if (element.match(/^<?\w[^>]*[^/]$/) && !element.startsWith("input")) {
indent += tab;
}
});
return result.substring(1, result.length - 3);
}
function getFormattedResult(type: "json" | "html" | "text", result: unknown) {
switch (type) {
case "json":
return JSON.stringify(result, null, 2);
case "html":
return format(result as string);
case "text":
return result;
}
}
function getEndpoint(type: "json" | "html" | "text") {
switch (type) {
case "json":
return "/artifact/json";
case "html":
case "text":
return "/artifact/text";
}
}
type Props = {
type: "json" | "html" | "text";
artifacts: Array<ArtifactApiResponse>;
};
function Artifact({ type, artifacts }: Props) {
const credentialGetter = useCredentialGetter();
const total = artifacts.length;
const archivedCount = artifacts.filter((a) => a.archived).length;
const allArchived = total > 0 && archivedCount === total;
const partiallyArchived = archivedCount > 0 && archivedCount < total;
function fetchArtifact(artifact: ArtifactApiResponse) {
if (artifact.signed_url) {
return getWithMintRetry(
artifact.signed_url,
artifact.artifact_id,
credentialGetter,
);
}
if (artifact.uri.startsWith("file://")) {
const endpoint = getEndpoint(type);
return artifactApiClient
.get(endpoint, {
params: {
path: artifact.uri.slice(7),
},
})
.then((response) => response.data);
}
}
const results = useQueries({
queries:
artifacts?.map((artifact) => {
return {
queryKey: ["artifact", artifact.artifact_id],
queryFn: () => fetchArtifact(artifact),
enabled: !artifact.archived,
};
}) ?? [],
});
if (allArchived) {
return (
<div className="p-4 text-muted-foreground">
This data has been archived. To request restoration, please contact
[email protected]
</div>
);
}
if (results.some((result) => result.isLoading)) {
return <Skeleton className="h-48 w-full" />;
}
return (
<div className="flex flex-col gap-2">
{partiallyArchived && (
<div className="text-sm text-muted-foreground">
{archivedCount} of {total} items archived and not shown. Contact
[email protected] to request restoration.
</div>
)}
<CodeEditor
language={type === "text" ? undefined : type}
className="w-full"
value={
results.some((result) => result.isError)
? JSON.stringify(results.find((result) => result.isError)?.error)
: results
.filter((_, i) => !artifacts[i]?.archived)
.map((result) => getFormattedResult(type, result.data))
.join(",\n")
}
minHeight="96px"
maxHeight="500px"
readOnly
/>
</div>
);
}
export { Artifact };