Skip to content

Automate browser based workflows with AI

fix(tests): make streaming registries monkeypatch import-order independent (#6460)

Trevor Cheungcb1630173266
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx
index c87225c82..7230f6673 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx
@@ -150,7 +150,7 @@ async function renderChat() {
const view = render(<WorkflowCopilotChat />);
// Let the mount-time chat-history fetch settle.
await waitFor(() =>
- expect(screen.getByPlaceholderText(/Type your message/)).toBeTruthy(),
+ expect(screen.getByPlaceholderText(/Message Skyvern Copilot/)).toBeTruthy(),
);
return view;
}
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.scroll.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.scroll.test.tsx
index c9bf44d06..1cd7703b0 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.scroll.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.scroll.test.tsx
@@ -64,7 +64,7 @@ import { WorkflowCopilotChat } from "./WorkflowCopilotChat";
async function renderChat() {
render(<WorkflowCopilotChat />);
await waitFor(() =>
- expect(screen.getByPlaceholderText(/Type your message/)).toBeTruthy(),
+ expect(screen.getByPlaceholderText(/Message Skyvern Copilot/)).toBeTruthy(),
);
}
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
index ad2a4ac3d..e863e9c9f 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
@@ -10,7 +10,12 @@ import {
import { getClient } from "@/api/AxiosClient";
import { useCredentialGetter } from "@/hooks/useCredentialGetter";
import { useParams } from "react-router-dom";
-import { ReloadIcon, Cross2Icon, ChevronDownIcon } from "@radix-ui/react-icons";
+import {
+ ReloadIcon,
+ Cross2Icon,
+ ChevronDownIcon,
+ CheckIcon,
+} from "@radix-ui/react-icons";
import { stringify as convertToYAML } from "yaml";
import { useWorkflowHasChangesStore } from "@/store/WorkflowHasChangesStore";
import { WorkflowCreateYAMLRequest } from "@/routes/workflows/types/workflowYamlTypes";
@@ -56,6 +61,20 @@ import {
import { computeFollowSignature, useStickToBottom } from "./useStickToBottom";
import { useSpeechToTextField } from "@/hooks/useSpeechToTextField";
import { SpeechInputButton } from "@/components/SpeechInputButton";
+import { useFeatureFlag } from "@/hooks/useFeatureFlag";
+import {
+ DropdownMenu,
+ DropdownMenuTrigger,
+ DropdownMenuContent,
+ DropdownMenuItem,
+} from "@/components/ui/dropdown-menu";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipProvider,
+ TooltipTrigger,
+} from "@/components/ui/tooltip";
+import { cn } from "@/util/utils";
// Cap on retained per-turn snap-back snapshots. A typical session has a
// handful of turns; this ceiling guards a runaway long-running chat.
@@ -68,6 +87,44 @@ function formatElapsedSeconds(ms: number): string {
return `${m}:${s.toString().padStart(2, "0")}`;
}
+// Ask's mark is a text dingbat; Build's is a color emoji that ModeGlyph flattens
+// to a tone-adaptive monochrome silhouette so both read flat on the dark UI.
+const ASK_GLYPH = "\u275D\uFE0E";
+const BUILD_GLYPH = "\uD83D\uDC09";
+
+function isPictographic(glyph: string): boolean {
+ try {
+ return /\p{Extended_Pictographic}/u.test(glyph);
+ } catch {
+ return false;
+ }
+}
+
+function ModeGlyph({
+ mode,
+ tone = "light",
+}: {
+ mode: "ask" | "build";
+ tone?: "light" | "dark";
+}) {
+ const glyph = mode === "build" ? BUILD_GLYPH : ASK_GLYPH;
+ const filter = isPictographic(glyph)
+ ? tone === "dark"
+ ? "grayscale(1) brightness(0)"
+ : "grayscale(1) brightness(0) invert(1)"
+ : undefined;
+ return (
+ <span className="inline-flex h-[18px] w-[18px] items-center justify-center leading-none">
+ <span
+ className={cn(mode === "build" ? "text-[16px]" : "text-[15px]")}
+ style={{ lineHeight: 1, filter }}
+ >
+ {glyph}
+ </span>
+ </span>
+ );
+}
+
function ConvoAggregatePill({
messages,
isInFlight,
@@ -293,6 +350,15 @@ export function WorkflowCopilotChat({
initialMessage,
onInitialMessageConsumed,
}: WorkflowCopilotChatProps = {}) {
+ const copilotV2Enabled =
+ useFeatureFlag("ENABLE_WORKFLOW_COPILOT_V2") === true;
+ const codeBlockModeEnabled =
+ useFeatureFlag("WORKFLOW_COPILOT_CODE_BLOCK_MODE") === true;
+ const [composerMode, setComposerMode] = useState<"ask" | "build">("build");
+ const [codeWorkflow, setCodeWorkflow] = useState(true);
+ // Build can never be active unless the V2 flag is on.
+ const isBuild = copilotV2Enabled && composerMode === "build";
+ const showCodeToggle = isBuild && codeBlockModeEnabled;
const [messages, setMessages] = useState<ChatMessage[]>([]);
const [proposedWorkflow, setProposedWorkflow] =
useState<WorkflowApiResponse | null>(null);
@@ -433,8 +499,16 @@ export function WorkflowCopilotChat({
const textarea = textareaRef.current;
if (!textarea) return;
+ if (!textarea.value) {
+ textarea.style.height = "40px";
+ textarea.style.overflowY = "hidden";
+ return;
+ }
+
textarea.style.height = "auto";
- textarea.style.height = `${Math.min(textarea.scrollHeight, 150)}px`;
+ const newHeight = Math.min(textarea.scrollHeight, 150);
+ textarea.style.height = `${newHeight}px`;
+ textarea.style.overflowY = newHeight >= 150 ? "auto" : "hidden";
}, []);
useEffect(() => {
@@ -1135,6 +1209,8 @@ export function WorkflowCopilotChat({
browser_session_id: liveBrowserSessionId ?? null,
message: messageContent,
workflow_yaml: workflowYaml,
+ mode: copilotV2Enabled ? composerMode : null,
+ code_block: showCodeToggle ? codeWorkflow : null,
cancel_token: cancelToken,
} as WorkflowCopilotChatRequest,
(payload) => {
@@ -1242,12 +1318,16 @@ export function WorkflowCopilotChat({
applyStoredNarrativeEvent,
applyWorkflowUpdate,
autoAccept,
+ codeWorkflow,
+ composerMode,
+ copilotV2Enabled,
credentialGetter,
getSaveData,
inputValue,
isLiveBrowserReady,
liveBrowserSessionId,
requiresLiveBrowser,
+ showCodeToggle,
updateQueuedPrompt,
workflowCopilotChatId,
workflowPermanentId,
@@ -1757,7 +1837,7 @@ export function WorkflowCopilotChat({
isHearingSpeech={isSpeechHearing}
disabled={inputDisabled}
onToggle={toggleSpeech}
- className="h-9 w-9"
+ className="h-10 w-10 rounded-lg"
/>
<textarea
ref={textareaRef}
@@ -1768,18 +1848,18 @@ export function WorkflowCopilotChat({
? "Type a message to queue for the next turn…"
: isWaitingForLiveBrowser
? "Type a prompt to queue..."
- : "Type your message... (Shift+Enter for new line)"
+ : "Message Skyvern Copilot…"
}
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={handleKeyPress}
disabled={inputDisabled}
rows={1}
- className="flex-1 resize-none rounded-md border border-input bg-slate-elevation2 px-3 py-2 text-sm text-foreground placeholder:text-muted-foreground focus:border-ring focus:outline-none disabled:cursor-not-allowed disabled:opacity-50"
+ className="min-h-10 flex-1 resize-none rounded-lg border border-input bg-slate-elevation2 px-3 py-2 text-sm leading-6 text-foreground placeholder:text-muted-foreground focus:border-ring focus:outline-none disabled:cursor-not-allowed disabled:opacity-50"
style={{
- minHeight: "38px",
+ minHeight: "40px",
maxHeight: "150px",
- overflow: "auto",
+ overflowY: "hidden",
}}
/>
{isLoading && queuedPrompt ? (
@@ -1787,14 +1867,14 @@ export function WorkflowCopilotChat({
<button
onClick={cancelQueuedPrompt}
title="Edit queued message"
- className="rounded-md border border-border px-3 py-2 text-sm font-medium text-muted-foreground hover:bg-accent hover:text-accent-foreground"
+ className="flex h-10 items-center justify-center rounded-lg border border-border px-3 text-sm font-medium text-muted-foreground hover:bg-accent hover:text-accent-foreground"
>
Edit queued
</button>
<button
onClick={cancelSend}
title="Cancel run"
- className="rounded-md bg-destructive px-3 py-2 text-sm font-medium text-destructive-foreground hover:bg-destructive/90"
+ className="flex h-10 items-center justify-center rounded-lg bg-destructive px-3 text-sm font-medium text-destructive-foreground hover:bg-destructive/90"
>
Cancel run
</button>
@@ -1804,14 +1884,14 @@ export function WorkflowCopilotChat({
<button
onClick={() => handleSend()}
title="Queue for the next turn"
- className="rounded-md border border-border px-3 py-2 text-sm font-medium text-muted-foreground hover:bg-accent hover:text-accent-foreground"
+ className="flex h-10 items-center justify-center rounded-lg border border-border px-3 text-sm font-medium text-muted-foreground hover:bg-accent hover:text-accent-foreground"
>
Queue
</button>
<button
onClick={cancelSend}
title="Cancel run"
- className="rounded-md bg-destructive px-3 py-2 text-sm font-medium text-destructive-foreground hover:bg-destructive/90"
+ className="flex h-10 items-center justify-center rounded-lg bg-destructive px-3 text-sm font-medium text-destructive-foreground hover:bg-destructive/90"
>
Cancel run
</button>
@@ -1820,17 +1900,108 @@ export function WorkflowCopilotChat({
<button
onClick={cancelQueuedPrompt}
title="Edit queued prompt"
- className="rounded-md bg-destructive px-4 py-2 text-sm font-medium text-destructive-foreground hover:bg-destructive/90"
+ className="flex h-10 items-center justify-center rounded-lg bg-destructive px-4 text-sm font-medium text-destructive-foreground hover:bg-destructive/90"
>
Cancel
</button>
- ) : (
+ ) : !copilotV2Enabled ? (
<button
onClick={() => handleSend()}
- className="rounded-md bg-cta px-4 py-2 text-sm font-medium text-cta-foreground hover:bg-cta-hover"
+ className="flex h-10 items-center justify-center rounded-lg bg-cta px-4 text-sm font-medium text-cta-foreground hover:bg-cta-hover"
>
Send
</button>
+ ) : (
+ <div className="flex items-end gap-2">
+ {showCodeToggle ? (
+ <TooltipProvider>
+ <Tooltip delayDuration={300}>
+ <TooltipTrigger asChild>
+ <button
+ type="button"
+ aria-pressed={codeWorkflow}
+ onClick={() => setCodeWorkflow((v) => !v)}
+ className={cn(
+ "flex h-10 w-10 items-center justify-center rounded-lg border transition-colors hover:brightness-[1.18] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
+ codeWorkflow
+ ? "border-sky-300 bg-sky-300/15 text-sky-300"
+ : "border-slate-600 bg-transparent text-slate-400",
+ )}
+ >
+ <span
+ className="text-[13px] font-semibold"
+ style={{ lineHeight: 1 }}
+ >
+ {"</>"}
+ </span>
+ </button>
+ </TooltipTrigger>
+ <TooltipContent className="max-w-[260px]">
+ Build the workflow as code. Faster and more flexible, but
+ may need extra detail to handle every edge case.
+ </TooltipContent>
+ </Tooltip>
+ </TooltipProvider>
+ ) : null}
+ <div className="flex items-stretch">
+ <button
+ onClick={() => handleSend()}
+ className="flex h-10 items-center gap-1.5 rounded-l-lg bg-cta px-3 py-2 text-sm font-medium text-cta-foreground hover:bg-cta-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
+ >
+ <ModeGlyph mode={isBuild ? "build" : "ask"} tone="dark" />
+ {isBuild ? "Build" : "Ask"}
+ </button>
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <button
+ type="button"
+ title="Switch mode"
+ aria-label="Switch mode"
+ className="flex h-10 w-8 items-center justify-center rounded-r-lg border-l border-black/20 bg-cta text-cta-foreground hover:bg-cta-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
+ >
+ <ChevronDownIcon className="h-3.5 w-3.5" />
+ </button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent
+ side="top"
+ align="end"
+ className="w-[272px] p-1.5"
+ >
+ <DropdownMenuItem
+ onSelect={() => setComposerMode("ask")}
+ className="flex items-start gap-2.5"
+ >
+ <ModeGlyph mode="ask" />
+ <span className="flex flex-1 flex-col">
+ <span className="text-sm font-medium">Ask</span>
+ <span className="text-xs leading-snug text-muted-foreground">
+ Answer questions and make quick workflow edits.
+ </span>
+ </span>
+ {!isBuild ? (
+ <CheckIcon className="h-4 w-4 text-sky-400" />
+ ) : null}
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ onSelect={() => setComposerMode("build")}
+ className="flex items-start gap-2.5"
+ >
+ <ModeGlyph mode="build" />
+ <span className="flex flex-1 flex-col">
+ <span className="text-sm font-medium">Build</span>
+ <span className="text-xs leading-snug text-muted-foreground">
+ Navigates the site to design your workflow, then tests
+ that it works.
+ </span>
+ </span>
+ {isBuild ? (
+ <CheckIcon className="h-4 w-4 text-sky-400" />
+ ) : null}
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </div>
+ </div>
)}
</div>
</div>
diff --git a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
index 0e4383d6b..4d37ca8c7 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
@@ -37,6 +37,8 @@ export interface WorkflowCopilotChatRequest {
browser_session_id?: string | null;
message: string;
workflow_yaml: string;
+ mode?: "ask" | "build" | null;
+ code_block?: boolean | null;
cancel_token?: string;
}
diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.py
index 8407d98ee..f0e1d1b2c 100644
--- a/skyvern/forge/agent_functions.py
+++ b/skyvern/forge/agent_functions.py
@@ -1197,16 +1197,17 @@ class AgentFunction:
"""
return ""
- def get_copilot_config(self) -> CopilotConfig | None:
+ def get_copilot_config(self, code_block_mode: bool | None = None) -> CopilotConfig | None:
"""Return an optional workflow copilot config override."""
- return CopilotConfig(
- block_authoring_policy=block_authoring_policy_from_code_only_mode(settings.WORKFLOW_COPILOT_CODE_BLOCK_MODE)
- )
+ resolved = settings.WORKFLOW_COPILOT_CODE_BLOCK_MODE if code_block_mode is None else code_block_mode
+ return CopilotConfig(block_authoring_policy=block_authoring_policy_from_code_only_mode(resolved))
- async def get_copilot_config_for_request(self, organization_id: str | None = None) -> CopilotConfig | None:
+ async def get_copilot_config_for_request(
+ self, organization_id: str | None = None, code_block_mode: bool | None = None
+ ) -> CopilotConfig | None:
"""Return a request-scoped workflow copilot config override."""
del organization_id
- return self.get_copilot_config()
+ return self.get_copilot_config(code_block_mode)
def detect_ats_platform(self, url_or_domain: str | None) -> str | None:
"""Detect if a URL belongs to a known ATS platform.
diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.py
index 822164b92..e788ba4de 100644
--- a/skyvern/forge/sdk/routes/workflow_copilot.py
+++ b/skyvern/forge/sdk/routes/workflow_copilot.py
@@ -1630,7 +1630,9 @@ async def _new_copilot_chat_post(
return
copilot_config = (
- await app.AGENT_FUNCTION.get_copilot_config_for_request(organization.organization_id)
+ await app.AGENT_FUNCTION.get_copilot_config_for_request(
+ organization.organization_id, code_block_mode=chat_request.code_block
+ )
) or CopilotConfig()
# Spawn the cancel watcher only after the chat row exists; cancels
@@ -1905,7 +1907,11 @@ async def _new_copilot_chat_post(
COPILOT_V2_FLAG_KEY = "ENABLE_WORKFLOW_COPILOT_V2"
-async def _should_use_copilot_v2(organization: Organization, workflow_permanent_id: str) -> bool:
+async def _should_use_copilot_v2(
+ organization: Organization, workflow_permanent_id: str, mode: str | None = None
+) -> bool:
+ if mode is not None:
+ return mode == "build"
if settings.ENABLE_WORKFLOW_COPILOT_V2:
return True
try:
@@ -1933,7 +1939,7 @@ async def workflow_copilot_chat_post(
chat_request: WorkflowCopilotChatRequest,
organization: Organization = Depends(org_auth_service.get_current_org),
) -> EventSourceResponse:
- if await _should_use_copilot_v2(organization, chat_request.workflow_permanent_id):
+ if await _should_use_copilot_v2(organization, chat_request.workflow_permanent_id, mode=chat_request.mode):
return await _new_copilot_chat_post(request, chat_request, organization)
async def stream_handler(stream: EventSourceStream) -> None:
diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.py
index bcb8e6906..47ef8356c 100644
--- a/skyvern/forge/sdk/schemas/workflow_copilot.py
+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py
@@ -1,6 +1,6 @@
from datetime import datetime
from enum import StrEnum
-from typing import Any
+from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, Field
@@ -68,6 +68,12 @@ class WorkflowCopilotChatRequest(BaseModel):
)
message: str = Field(..., description="The message that user sends")
workflow_yaml: str = Field(..., description="Current workflow YAML including unsaved changes")
+ mode: Literal["ask", "build"] | None = Field(
+ None, description="Per-request copilot path selector; None falls back to feature flags."
+ )
+ code_block: bool | None = Field(
+ None, description="Per-request code-block authoring; honored only on the build/v2 path."
+ )
cancel_token: str | None = Field(
None,
description=(
diff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.py
index 199554ba0..042d59c87 100644
--- a/tests/unit/test_workflow_copilot_route.py
+++ b/tests/unit/test_workflow_copilot_route.py
@@ -34,7 +34,7 @@ from skyvern.forge.sdk.schemas.workflow_copilot import (
)
-def _make_chat_request() -> WorkflowCopilotChatRequest:
+def _make_chat_request(mode: str | None = None, code_block: bool | None = None) -> WorkflowCopilotChatRequest:
return WorkflowCopilotChatRequest(
workflow_permanent_id="wpid-1",
workflow_id="wf-request",
@@ -42,6 +42,8 @@ def _make_chat_request() -> WorkflowCopilotChatRequest:
workflow_run_id=None,
message="Please update it",
workflow_yaml="title: Example",
+ mode=mode,
+ code_block=code_block,
)
@@ -162,6 +164,78 @@ async def test_flag_on_dispatches_to_new_copilot(monkeypatch: pytest.MonkeyPatch
new_copilot_mock.assert_awaited_once()
+async def test_request_mode_ask_forces_v1_over_flag_on(monkeypatch: pytest.MonkeyPatch) -> None:
+ """mode='ask' must take the v1 path even when the settings flag is on."""
+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)
+
+ new_copilot_mock = AsyncMock(side_effect=AssertionError("mode='ask' must not reach the v2 path"))
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.routes.workflow_copilot._new_copilot_chat_post",
+ new_copilot_mock,
+ )
+
+ captured = _install_fake_create(monkeypatch)
+
+ request = MagicMock()
+ request.headers = {}
+ organization = SimpleNamespace(organization_id="org-1")
+
+ response = await workflow_copilot_chat_post(request, _make_chat_request(mode="ask"), organization)
+
+ assert response is captured["sentinel"]
+ new_copilot_mock.assert_not_awaited()
+
+
+async def test_request_mode_build_forces_v2_over_flag_off(monkeypatch: pytest.MonkeyPatch) -> None:
+ """mode='build' must take the v2 path even when the settings flag is off."""
+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", False)
+ _install_mock_provider(monkeypatch, return_value=False)
+
+ sentinel = object()
+ new_copilot_mock = AsyncMock(return_value=sentinel)
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.routes.workflow_copilot._new_copilot_chat_post",
+ new_copilot_mock,
+ )
+
+ request = MagicMock()
+ request.headers = {}
+ organization = SimpleNamespace(organization_id="org-1")
+
+ response = await workflow_copilot_chat_post(request, _make_chat_request(mode="build"), organization)
+
+ assert response is sentinel
+ new_copilot_mock.assert_awaited_once()
+
+
+async def test_request_mode_absent_follows_flag(monkeypatch: pytest.MonkeyPatch) -> None:
+ """mode absent (None) keeps following the settings flag in both directions."""
+ new_copilot_mock = AsyncMock(return_value=object())
+ monkeypatch.setattr(
+ "skyvern.forge.sdk.routes.workflow_copilot._new_copilot_chat_post",
+ new_copilot_mock,
+ )
+ captured = _install_fake_create(monkeypatch)
+
+ request = MagicMock()
+ request.headers = {}
+ organization = SimpleNamespace(organization_id="org-1")
+
+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", False)
+ _install_mock_provider(monkeypatch, return_value=False)
+ response = await workflow_copilot_chat_post(request, _make_chat_request(mode=None), organization)
+ assert response is captured["sentinel"]
+ new_copilot_mock.assert_not_awaited()
+
+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)
+ response = await workflow_copilot_chat_post(request, _make_chat_request(mode=None), organization)
+ assert response is new_copilot_mock.return_value
+ new_copilot_mock.assert_awaited_once()
+
+
def _setup_new_copilot_mocks(
monkeypatch: pytest.MonkeyPatch,
chat: SimpleNamespace,