Skip to content

Automate browser based workflows with AI

Add script version selector to Code tab (#5016)

pedrohsdbc30e43bc84fe
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useBlockScriptsQuery.ts b/skyvern-frontend/src/routes/workflows/hooks/useBlockScriptsQuery.ts
index b3095b8f9..ed53cc7e3 100644
--- a/skyvern-frontend/src/routes/workflows/hooks/useBlockScriptsQuery.ts
+++ b/skyvern-frontend/src/routes/workflows/hooks/useBlockScriptsQuery.ts
@@ -22,10 +22,7 @@ function useBlockScriptsQuery({
}: Props) {
const credentialGetter = useCredentialGetter();
- return useQuery<{
- blocks: { [blockName: string]: string };
- main_script: string | null;
- }>({
+ return useQuery<ScriptBlocksResponse>({
queryKey: [
"block-scripts",
workflowPermanentId,
@@ -37,7 +34,7 @@ function useBlockScriptsQuery({
queryFn: async () => {
const client = await getClient(credentialGetter, "sans-api-v1");
- const result = await client
+ return client
.post<ScriptBlocksResponse>(`/scripts/${workflowPermanentId}/blocks`, {
cache_key: cacheKey ?? "",
cache_key_value: cacheKeyValue ?? "",
@@ -45,8 +42,6 @@ function useBlockScriptsQuery({
workflow_run_id: workflowRunId ?? null,
})
.then((response) => response.data);
-
- return { blocks: result.blocks, main_script: result.main_script };
},
refetchInterval: () => {
if (!pollIntervalMs || pollIntervalMs === 0) {
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useScriptVersionCodeQuery.ts b/skyvern-frontend/src/routes/workflows/hooks/useScriptVersionCodeQuery.ts
new file mode 100644
index 000000000..2a771cf36
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/hooks/useScriptVersionCodeQuery.ts
@@ -0,0 +1,26 @@
+import { getClient } from "@/api/AxiosClient";
+import { useCredentialGetter } from "@/hooks/useCredentialGetter";
+import { useQuery } from "@tanstack/react-query";
+import { ScriptBlocksResponse } from "../types/scriptTypes";
+
+type Props = {
+ scriptId?: string | null;
+ version?: number | null;
+};
+
+function useScriptVersionCodeQuery({ scriptId, version }: Props) {
+ const credentialGetter = useCredentialGetter();
+
+ return useQuery<ScriptBlocksResponse>({
+ queryKey: ["script-version-code", scriptId, version],
+ queryFn: async () => {
+ const client = await getClient(credentialGetter, "sans-api-v1");
+ return client
+ .get(`/scripts/${scriptId}/versions/${version}`)
+ .then((response) => response.data);
+ },
+ enabled: !!scriptId && version != null,
+ });
+}
+
+export { useScriptVersionCodeQuery };
diff --git a/skyvern-frontend/src/routes/workflows/hooks/useScriptVersionsQuery.ts b/skyvern-frontend/src/routes/workflows/hooks/useScriptVersionsQuery.ts
new file mode 100644
index 000000000..39fd7b281
--- /dev/null
+++ b/skyvern-frontend/src/routes/workflows/hooks/useScriptVersionsQuery.ts
@@ -0,0 +1,25 @@
+import { getClient } from "@/api/AxiosClient";
+import { useCredentialGetter } from "@/hooks/useCredentialGetter";
+import { useQuery } from "@tanstack/react-query";
+import { ScriptVersionListResponse } from "../types/scriptTypes";
+
+type Props = {
+ scriptId?: string | null;
+};
+
+function useScriptVersionsQuery({ scriptId }: Props) {
+ const credentialGetter = useCredentialGetter();
+
+ return useQuery<ScriptVersionListResponse>({
+ queryKey: ["script-versions", scriptId],
+ queryFn: async () => {
+ const client = await getClient(credentialGetter, "sans-api-v1");
+ return client
+ .get(`/scripts/${scriptId}/versions`)
+ .then((response) => response.data);
+ },
+ enabled: !!scriptId,
+ });
+}
+
+export { useScriptVersionsQuery };
diff --git a/skyvern-frontend/src/routes/workflows/types/scriptTypes.ts b/skyvern-frontend/src/routes/workflows/types/scriptTypes.ts
index 2bc71b235..b266dd447 100644
--- a/skyvern-frontend/src/routes/workflows/types/scriptTypes.ts
+++ b/skyvern-frontend/src/routes/workflows/types/scriptTypes.ts
@@ -1,6 +1,8 @@
export type ScriptBlocksResponse = {
blocks: { [blockName: string]: string };
main_script: string | null;
+ script_id: string | null;
+ version: number | null;
};
export type CacheKeyValuesResponse = {
@@ -10,3 +12,14 @@ export type CacheKeyValuesResponse = {
total_count: number;
values: string[];
};
+
+export type ScriptVersionSummary = {
+ version: number;
+ script_revision_id: string;
+ created_at: string;
+ run_id: string | null;
+};
+
+export type ScriptVersionListResponse = {
+ versions: ScriptVersionSummary[];
+};
diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunCode.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunCode.tsx
index acfa0d462..d0709f29c 100644
--- a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunCode.tsx
+++ b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunCode.tsx
@@ -14,6 +14,8 @@ import { statusIsFinalized } from "@/routes/tasks/types";
import { CodeEditor } from "@/routes/workflows/components/CodeEditor";
import { useBlockScriptsQuery } from "@/routes/workflows/hooks/useBlockScriptsQuery";
import { useCacheKeyValuesQuery } from "@/routes/workflows/hooks/useCacheKeyValuesQuery";
+import { useScriptVersionsQuery } from "@/routes/workflows/hooks/useScriptVersionsQuery";
+import { useScriptVersionCodeQuery } from "@/routes/workflows/hooks/useScriptVersionCodeQuery";
import { useWorkflowRunWithWorkflowQuery } from "@/routes/workflows/hooks/useWorkflowRunWithWorkflowQuery";
import { constructCacheKeyValue } from "@/routes/workflows/editor/utils";
import { getCode, getOrderedBlockLabels } from "@/routes/workflows/utils";
@@ -81,11 +83,49 @@ function WorkflowRunCode(props?: Props) {
? blockScriptsPublished
: blockScriptsPending;
+ // Script version state
+ const scriptId = activeScripts?.script_id ?? null;
+ const currentVersion = activeScripts?.version ?? null;
+ const [selectedVersion, setSelectedVersion] = useState<number | null>(null);
+
+ // Reset selected version when the active script changes
+ useEffect(() => {
+ setSelectedVersion(null);
+ }, [scriptId, currentVersion]);
+
+ // Fetch available versions for this script
+ const { data: versionsData, isFetched: versionsFetched } =
+ useScriptVersionsQuery({ scriptId });
+ // Sort descending so versions[0] is always the latest
+ const versions = [...(versionsData?.versions ?? [])].sort(
+ (a, b) => b.version - a.version,
+ );
+
+ // If user selected a different version, fetch that version's code
+ const isViewingOtherVersion =
+ selectedVersion !== null && selectedVersion !== currentVersion;
+ const { data: selectedVersionCode, isFetching: isLoadingVersion } =
+ useScriptVersionCodeQuery({
+ scriptId,
+ version: isViewingOtherVersion ? selectedVersion : null,
+ });
+
+ // Determine which code to display
+ const displayScripts = isViewingOtherVersion
+ ? selectedVersionCode
+ : activeScripts;
+
+ // For non-adaptive-caching, use block labels from the displayed version
+ // (older versions may have different blocks than the current run)
+ const displayBlockLabels = isViewingOtherVersion
+ ? Object.keys(displayScripts?.blocks ?? {})
+ : orderedBlockLabels;
+
// For adaptive caching, prefer the full main.py script over stitched blocks
const code = (
- isAdaptiveCaching && activeScripts?.main_script
- ? activeScripts.main_script
- : getCode(orderedBlockLabels, activeScripts?.blocks).join("")
+ isAdaptiveCaching && displayScripts?.main_script
+ ? displayScripts.main_script
+ : getCode(displayBlockLabels, displayScripts?.blocks).join("")
).trim();
const isGeneratingCode = !isFinalized && !hasPublishedCode;
@@ -133,16 +173,80 @@ function WorkflowRunCode(props?: Props) {
);
}
+ const hasVersions = versions.length > 1;
+
+ // Version selector component (shared between both render paths)
+ // Wait for the versions query to settle before rendering to avoid a flash
+ // from static label to dropdown when the query resolves.
+ const versionSelector = !versionsFetched ? null : hasVersions ? (
+ <div className="flex items-center gap-2">
+ <Label className="whitespace-nowrap text-xs text-slate-400">
+ Version
+ </Label>
+ <Select
+ value={String(selectedVersion ?? currentVersion ?? "")}
+ onValueChange={(v: string) => setSelectedVersion(Number(v))}
+ >
+ <SelectTrigger className="h-7 w-auto min-w-[7rem] gap-1.5 px-2 text-xs">
+ <SelectValue placeholder="Version" />
+ </SelectTrigger>
+ <SelectContent>
+ {versions.map((v) => {
+ const isRunVersion = v.version === currentVersion;
+ const isLatest = v.version === versions[0]?.version;
+ return (
+ <SelectItem key={v.version} value={String(v.version)}>
+ <span className="flex items-center gap-1.5">
+ <span
+ className={cn({
+ "font-semibold text-emerald-400": isRunVersion,
+ })}
+ >
+ v{v.version}
+ </span>
+ {isRunVersion && (
+ <span className="rounded-sm bg-emerald-900/50 px-1 py-0.5 text-[10px] leading-none text-emerald-300">
+ this run
+ </span>
+ )}
+ {isLatest && !isRunVersion && (
+ <span className="rounded-sm bg-blue-900/50 px-1 py-0.5 text-[10px] leading-none text-blue-300">
+ latest
+ </span>
+ )}
+ </span>
+ </SelectItem>
+ );
+ })}
+ </SelectContent>
+ </Select>
+ </div>
+ ) : currentVersion != null ? (
+ <span className="text-xs text-slate-500">v{currentVersion}</span>
+ ) : null;
+
if (!showCacheKeyValueSelector || !cacheKey || cacheKey === "") {
return (
- <CodeEditor
- className="h-full overflow-y-scroll"
- language="python"
- value={code}
- lineWrap={false}
- readOnly
- fontSize={10}
- />
+ <div className="relative flex h-full w-full flex-col gap-2">
+ {versionSelector && (
+ <div className="flex justify-end">{versionSelector}</div>
+ )}
+ <CodeEditor
+ className={cn("h-full overflow-y-scroll", {
+ "animate-pulse": isGeneratingCode || isLoadingVersion,
+ })}
+ language="python"
+ value={code}
+ lineWrap={false}
+ readOnly
+ fontSize={10}
+ />
+ {code.length > 0 && (
+ <div className="absolute bottom-2 right-3 flex items-center justify-end">
+ <CopyAndExplainCode code={code} />
+ </div>
+ )}
+ </div>
);
}
@@ -160,9 +264,10 @@ function WorkflowRunCode(props?: Props) {
return (
<div className="relative flex h-full w-full flex-col items-end justify-center gap-2">
- {cacheKeyValueSet.size > 0 ? (
- <div className="flex w-full justify-end gap-4">
- <div className="flex items-center justify-around gap-2">
+ <div className="flex w-full items-center justify-end gap-4">
+ {versionSelector}
+ {cacheKeyValueSet.size > 0 ? (
+ <div className="flex items-center gap-2">
<Label className="w-[7rem]">Code Key Value</Label>
<HelpTooltip
content={
@@ -171,44 +276,43 @@ function WorkflowRunCode(props?: Props) {
: "Which generated (& cached) code to view."
}
/>
- </div>
+ <Select
+ disabled={!isFinalized}
+ value={cacheKeyValue}
+ onValueChange={(v: string) => setCacheKeyValue(v)}
+ >
+ <SelectTrigger className="max-w-[15rem] [&>span]:text-ellipsis">
+ <SelectValue placeholder="Code Key Value" />
+ </SelectTrigger>
+ <SelectContent>
+ {Array.from(cacheKeyValueSet)
+ .sort()
+ .map((value, i) => {
+ const v = value
+ ? value.length === 0
+ ? "default"
+ : value
+ : "default";
- <Select
- disabled={!isFinalized}
- value={cacheKeyValue}
- onValueChange={(v: string) => setCacheKeyValue(v)}
- >
- <SelectTrigger className="max-w-[15rem] [&>span]:text-ellipsis">
- <SelectValue placeholder="Code Key Value" />
- </SelectTrigger>
- <SelectContent>
- {Array.from(cacheKeyValueSet)
- .sort()
- .map((value, i) => {
- const v = value
- ? value.length === 0
- ? "default"
- : value
- : "default";
-
- return (
- <SelectItem key={`${v}-${i}`} value={v}>
- {value === cacheKeyValueForWorkflowRun &&
- isFinalized === true ? (
- <span className="underline">{v}</span>
- ) : (
- v
- )}
- </SelectItem>
- );
- })}
- </SelectContent>
- </Select>
- </div>
- ) : null}
+ return (
+ <SelectItem key={`${v}-${i}`} value={v}>
+ {value === cacheKeyValueForWorkflowRun &&
+ isFinalized === true ? (
+ <span className="underline">{v}</span>
+ ) : (
+ v
+ )}
+ </SelectItem>
+ );
+ })}
+ </SelectContent>
+ </Select>
+ </div>
+ ) : null}
+ </div>
<CodeEditor
className={cn("h-full w-full overflow-y-scroll", {
- "animate-pulse": isGeneratingCode,
+ "animate-pulse": isGeneratingCode || isLoadingVersion,
})}
language="python"
value={code}
@@ -216,7 +320,7 @@ function WorkflowRunCode(props?: Props) {
readOnly
fontSize={10}
/>
- <div className="absolute right-[0.75rem] top-[3.5rem] flex items-center justify-end">
+ <div className="absolute bottom-2 right-3 flex items-center justify-end">
<CopyAndExplainCode code={code} />
</div>
</div>
diff --git a/skyvern/forge/sdk/routes/scripts.py b/skyvern/forge/sdk/routes/scripts.py
index f3c43771d..a3884af37 100644
--- a/skyvern/forge/sdk/routes/scripts.py
+++ b/skyvern/forge/sdk/routes/scripts.py
@@ -60,6 +60,8 @@ async def get_script_blocks_response(
workflow_permanent_id: str,
script_revision_id: str,
include_main_script: bool = False,
+ script_id: str | None = None,
+ version: int | None = None,
) -> ScriptBlocksResponse:
script_blocks = await app.DATABASE.get_script_blocks_by_script_revision_id(
script_revision_id=script_revision_id,
@@ -79,7 +81,7 @@ async def get_script_blocks_response(
organization_id=organization_id,
script_revision_id=script_revision_id,
)
- return ScriptBlocksResponse(blocks={}, main_script=main_script)
+ return ScriptBlocksResponse(blocks={}, main_script=main_script, script_id=script_id, version=version)
result: dict[str, str] = {}
@@ -180,7 +182,7 @@ async def get_script_blocks_response(
script_revision_id=script_revision_id,
)
- return ScriptBlocksResponse(blocks=result, main_script=main_script)
+ return ScriptBlocksResponse(blocks=result, main_script=main_script, script_id=script_id, version=version)
@base_router.post(
@@ -312,6 +314,8 @@ async def get_script_version_code(
organization_id=current_org.organization_id,
workflow_permanent_id=script_id,
include_main_script=True,
+ script_id=script.script_id,
+ version=script.version,
)
@@ -548,6 +552,8 @@ async def get_workflow_script_blocks(
organization_id=current_org.organization_id,
workflow_permanent_id=workflow_permanent_id,
include_main_script=include_main_script,
+ script_id=published_script.script_id,
+ version=published_script.version,
)
cache_key = block_script_request.cache_key or workflow.cache_key or ""
@@ -577,6 +583,8 @@ async def get_workflow_script_blocks(
organization_id=current_org.organization_id,
workflow_permanent_id=workflow_permanent_id,
include_main_script=include_main_script,
+ script_id=script.script_id,
+ version=script.version,
)
diff --git a/skyvern/schemas/scripts.py b/skyvern/schemas/scripts.py
index 49530c412..4097d04e7 100644
--- a/skyvern/schemas/scripts.py
+++ b/skyvern/schemas/scripts.py
@@ -169,6 +169,8 @@ class ScriptCacheKeyValuesResponse(BaseModel):
class ScriptBlocksResponse(BaseModel):
blocks: dict[str, str]
main_script: str | None = None
+ script_id: str | None = None
+ version: int | None = None
class ScriptBlocksRequest(BaseModel):