feat: background agents

This commit is contained in:
Tomasz Stefaniak 2025-10-09 12:42:43 -07:00
parent bcd6c272cc
commit 2edcd6ae02
6 changed files with 275 additions and 4 deletions

2
core/index.d.ts vendored
View file

@ -462,7 +462,7 @@ export interface PromptLog {
completion: string;
}
export type MessageModes = "chat" | "agent" | "plan";
export type MessageModes = "chat" | "agent" | "plan" | "background";
export type ToolStatus =
| "generating" // Tool call arguments are being streamed from the LLM

View file

@ -0,0 +1,166 @@
import { Loader2 } from "lucide-react";
import { useContext, useEffect, useState } from "react";
import { useAuth } from "../../context/Auth";
import { IdeMessengerContext } from "../../context/IdeMessenger";
interface Agent {
id: string;
name: string | null;
status: string;
repoUrl: string;
createdAt: string;
metadata?: {
github_repo?: string;
};
}
export function AgentsList() {
const { session } = useAuth();
const ideMessenger = useContext(IdeMessengerContext);
const [agents, setAgents] = useState<Agent[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
async function fetchAgents() {
if (!session) {
setAgents([]);
setIsLoading(false);
return;
}
try {
setIsLoading(true);
// Request agent list from IDE
const result = await ideMessenger.request("listBackgroundAgents", {});
if (result && Array.isArray(result)) {
setAgents(result);
setError(null);
} else {
setAgents([]);
}
} catch (err: any) {
console.error("Failed to fetch agents:", err);
setError(err.message || "Failed to load agents");
setAgents([]);
} finally {
setIsLoading(false);
}
}
void fetchAgents();
// Poll for updates every 10 seconds
const interval = setInterval(() => {
void fetchAgents();
}, 10000);
return () => clearInterval(interval);
}, [session, ideMessenger]);
if (isLoading) {
return (
<div className="flex items-center justify-center py-8">
<Loader2 className="h-6 w-6 animate-spin text-gray-400" />
</div>
);
}
if (error) {
return (
<div className="px-2 py-4 text-sm text-red-500">
Error loading agents: {error}
</div>
);
}
if (agents.length === 0) {
return (
<div className="px-2 py-4 text-center text-sm text-gray-500">
No background agents yet. Submit a message above to create one.
</div>
);
}
return (
<div className="flex flex-col gap-2">
<div className="px-2 text-xs font-semibold text-gray-500">
Recent Agents
</div>
<div className="flex flex-col gap-1">
{agents.map((agent) => (
<div
key={agent.id}
className="cursor-pointer rounded-lg border border-gray-200 bg-gray-50 p-3 transition-colors hover:bg-gray-100"
onClick={() => {
// Open agent detail in browser
const agentUrl = `https://app.continue.dev/agents/${agent.id}`;
ideMessenger.post("openUrl", agentUrl);
}}
>
<div className="flex items-start justify-between">
<div className="min-w-0 flex-1">
<div className="truncate text-sm font-medium text-gray-900">
{agent.name || "Unnamed Agent"}
</div>
<div className="mt-0.5 truncate text-xs text-gray-500">
{agent.metadata?.github_repo || agent.repoUrl}
</div>
</div>
<div className="ml-2">
<AgentStatusBadge status={agent.status} />
</div>
</div>
<div className="mt-1 text-xs text-gray-400">
{formatRelativeTime(agent.createdAt)}
</div>
</div>
))}
</div>
</div>
);
}
function AgentStatusBadge({ status }: { status: string }) {
const statusColors: Record<string, string> = {
running: "bg-green-100 text-green-800",
pending: "bg-yellow-100 text-yellow-800",
creating: "bg-blue-100 text-blue-800",
failure: "bg-red-100 text-red-800",
suspended: "bg-gray-100 text-gray-800",
};
const color = statusColors[status] || "bg-gray-100 text-gray-800";
return (
<span
className={`inline-flex items-center rounded px-2 py-0.5 text-xs font-medium ${color}`}
>
{status}
</span>
);
}
function formatRelativeTime(dateString: string): string {
try {
const date = new Date(dateString);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffMins = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMs / 3600000);
const diffDays = Math.floor(diffMs / 86400000);
if (diffMins < 1) {
return "just now";
} else if (diffMins < 60) {
return `${diffMins}m ago`;
} else if (diffHours < 24) {
return `${diffHours}h ago`;
} else {
return `${diffDays}d ago`;
}
} catch {
return dateString;
}
}

View file

@ -0,0 +1,55 @@
import { RocketLaunchIcon } from "@heroicons/react/24/outline";
import { useCallback, useState } from "react";
import { useAuth } from "../../context/Auth";
import { AgentsList } from "./AgentsList";
interface BackgroundModeViewProps {
onCreateAgent: (prompt: string) => void;
}
export function BackgroundModeView({ onCreateAgent }: BackgroundModeViewProps) {
const { session, login } = useAuth();
const [isLoggingIn, setIsLoggingIn] = useState(false);
const handleSignIn = useCallback(async () => {
setIsLoggingIn(true);
try {
await login(false);
} catch (error) {
console.error("Login failed:", error);
} finally {
setIsLoggingIn(false);
}
}, [login]);
if (!session) {
return (
<div className="flex flex-col items-center justify-center gap-4 px-4 py-8">
<RocketLaunchIcon className="h-16 w-16 text-gray-400" />
<div className="max-w-md text-center">
<h3 className="mb-2 text-lg font-semibold">Background Agents</h3>
<p className="mb-4 text-sm text-gray-600">
Trigger long-running background agents that work on your codebase
autonomously. Sign in to Continue to get started.
</p>
<button
onClick={handleSignIn}
disabled={isLoggingIn}
className="rounded-lg bg-blue-500 px-6 py-2 text-white hover:bg-blue-600 disabled:opacity-50"
>
{isLoggingIn ? "Signing in..." : "Sign In to Continue"}
</button>
</div>
</div>
);
}
return (
<div className="flex flex-col gap-4 py-4">
<div className="px-2 text-sm text-gray-600">
Agents you trigger will run in the background and appear below.
</div>
<AgentsList />
</div>
);
}

View file

@ -1,5 +1,6 @@
import {
ChatBubbleLeftIcon,
RocketLaunchIcon,
SparklesIcon,
SwatchIcon,
} from "@heroicons/react/24/outline";
@ -21,5 +22,7 @@ export function ModeIcon({
return <SwatchIcon className={className} />;
case "chat":
return <ChatBubbleLeftIcon className={className} />;
case "background":
return <RocketLaunchIcon className={className} />;
}
}

View file

@ -39,6 +39,8 @@ export function ModeSelect() {
dispatch(setMode("plan"));
} else if (mode === "plan") {
dispatch(setMode("agent"));
} else if (mode === "agent") {
dispatch(setMode("background"));
} else {
dispatch(setMode("chat"));
}
@ -96,7 +98,13 @@ export function ModeSelect() {
>
<ModeIcon mode={mode} />
<span className="hidden sm:block">
{mode === "chat" ? "Chat" : mode === "agent" ? "Agent" : "Plan"}
{mode === "chat"
? "Chat"
: mode === "agent"
? "Agent"
: mode === "background"
? "Background"
: "Plan"}
</span>
<ChevronDownIcon
className="h-2 w-2 flex-shrink-0"
@ -165,6 +173,24 @@ export function ModeSelect() {
/>
</ListboxOption>
<ListboxOption value="background" className={"gap-1"}>
<div className="flex flex-row items-center gap-1.5">
<ModeIcon mode="background" />
<span className="">Background</span>
<ToolTip
style={{
zIndex: 200001,
}}
content="Trigger background agents"
>
<InformationCircleIcon className="h-2.5 w-2.5 flex-shrink-0" />
</ToolTip>
</div>
<CheckIcon
className={`ml-auto h-3 w-3 ${mode === "background" ? "" : "opacity-0"}`}
/>
</ListboxOption>
<div className="text-description-muted px-2 py-1">
{`${metaKeyLabel} . for next mode`}
</div>

View file

@ -44,6 +44,7 @@ import { isJetBrains, isMetaEquivalentKeyPressed } from "../../util";
import { ToolCallDiv } from "./ToolCallDiv";
import { useStore } from "react-redux";
import { BackgroundModeView } from "../../components/BackgroundMode/BackgroundModeView";
import { CliInstallBanner } from "../../components/CliInstallBanner";
import { FatalErrorIndicator } from "../../components/config/FatalErrorNotice";
import InlineErrorMessage from "../../components/mainInput/InlineErrorMessage";
@ -125,6 +126,7 @@ export function Chat() {
const hasDismissedExploreDialog = useAppSelector(
(state) => state.ui.hasDismissedExploreDialog,
);
const mode = useAppSelector((state) => state.session.mode);
const jetbrains = useMemo(() => {
return isJetBrains();
}, []);
@ -169,6 +171,17 @@ export function Chat() {
const codeToEditSnapshot = stateSnapshot.editModeState.codeToEdit;
const selectedModelByRole =
stateSnapshot.config.config.selectedModelByRole;
const currentMode = stateSnapshot.session.mode;
// Handle background mode specially
if (currentMode === "background" && !isCurrentlyInEdit) {
// Background mode triggers agent creation instead of chat
ideMessenger.post("createBackgroundAgent", { editorState });
if (editorToClearOnSend) {
editorToClearOnSend.commands.clearContent();
}
return;
}
// Cancel all pending tool calls
latestPendingToolCalls.forEach((toolCallState) => {
@ -457,8 +470,16 @@ export function Chat() {
</div>
<FatalErrorIndicator />
{!hasDismissedExploreDialog && <ExploreDialogWatcher />}
{history.length === 0 && (
<EmptyChatBody showOnboardingCard={onboardingCard.show} />
{mode === "background" ? (
<BackgroundModeView
onCreateAgent={(prompt) => {
ideMessenger.post("createBackgroundAgent", { prompt });
}}
/>
) : (
history.length === 0 && (
<EmptyChatBody showOnboardingCard={onboardingCard.show} />
)
)}
</div>
</div>