Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions apps/web/app/agent-teams/runs/[runId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { LoadFailure } from "@/components/load-failure";
import { useCallback, useMemo, useState } from "react";
import Link from "next/link";
import { useParams } from "next/navigation";
import { ExternalLink } from "lucide-react";
import { ExternalLink, Square } from "lucide-react";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { toast } from "sonner";
import { errorMessage } from "@orbit/api-client";

import { StepNumber, agentLookup, teamMode } from "@/components/teams/team-parts";
import { WorkflowView } from "@/components/teams/workflow/workflow-view";
Expand All @@ -18,7 +21,10 @@ import { DescriptionList } from "@/components/ui/description-list";
import { PageHeader } from "@/components/ui/page-header";
import { Skeleton } from "@/components/ui/skeleton";
import { ExecutionStatusBadge } from "@/components/ui/status-badge";
import { useAgentTeam, useAllAgents, useTeamRun } from "@/lib/queries";
import { useConfirm } from "@/components/ui/confirm";
import { queryKeys, useAgentTeam, useAllAgents, useMe, useTeamRun } from "@/lib/queries";
import { canStopTeamRun } from "@/lib/run-controls";
import { cancelTeamRun } from "@/lib/teams";
import { cn, formatDateTime, relativeTime } from "@/lib/utils";

const DONE_STEP = new Set(["succeeded", "completed"]);
Expand All @@ -40,6 +46,21 @@ export default function TeamRunPage() {
const run = useTeamRun(params.runId);
const team = useAgentTeam(run.data?.run.teamId ?? "");
const agents = useAllAgents();
const me = useMe();
const confirm = useConfirm();
const queryClient = useQueryClient();
const stop = useMutation({
mutationFn: () => cancelTeamRun(params.runId),
onSuccess: (detail) => {
queryClient.setQueryData(queryKeys.teamRun(params.runId), detail);
toast.success("Run stopped");
},
onError: (error) => toast.error(`The run could not be stopped: ${errorMessage(error)}`),
onSettled: () => {
void queryClient.invalidateQueries({ queryKey: queryKeys.teamRuns });
void queryClient.invalidateQueries({ queryKey: queryKeys.executions() });
},
});
const agentName = useMemo(() => agentLookup(agents.data?.items ?? []), [agents.data]);
const workflowAgentName = useMemo(() => (id: string) => agentName(id).name, [agentName]);
// Selecting a step on the graph points at its latest row in the list.
Expand Down Expand Up @@ -84,11 +105,31 @@ export default function TeamRunPage() {
const totalSteps = steps.length + upcoming.length;
const teamName = team.data?.name ?? "Agent team";
const total = duration(detail.createdAt, detail.finishedAt);
const live = steps.filter((step) => !step.finishedAt && step.executionId).length;

async function confirmStop() {
const confirmed = await confirm({
title: "Stop this run?",
description: `The run ends as cancelled${
live ? ` and ${live === 1 ? "its running step execution is" : `its ${live} running step executions are`} cancelled` : ""
}. Steps that have not started will not run. This cannot be undone.`,
confirmLabel: "Stop run",
destructive: true,
});
if (confirmed) stop.mutate();
}

return (
<div className="space-y-6">
<PageHeader
back={{ href: "/agent-teams/runs", label: "Team runs" }}
actions={
canStopTeamRun(detail.status, me.data?.permissions) ? (
<Button variant="destructive" loading={stop.isPending} onClick={() => void confirmStop()}>
<Square /> Stop run
</Button>
) : undefined
}
title={<span className="line-clamp-3">{task}</span>}
meta={
<>
Expand Down
67 changes: 55 additions & 12 deletions apps/web/components/workers/launches-section.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Rocket, ScrollText, Square } from "lucide-react";
import { Archive, Rocket, ScrollText, Square, Trash2 } from "lucide-react";

import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
Expand All @@ -10,6 +10,7 @@ import { Segmented } from "@/components/ui/segmented";
import { Skeleton } from "@/components/ui/skeleton";
import { Status } from "@/components/ui/status";
import type { useWorkerLaunches } from "@/lib/queries";
import { launchEndActions, launchesForView, type LaunchView } from "@/lib/run-controls";
import type { WorkerLaunch } from "@/lib/types";
import { relativeTime } from "@/lib/utils";
import { errorMessage } from "@orbit/api-client";
Expand All @@ -18,20 +19,29 @@ import { isActive, launchErrorText, launchStatusMeta, targetLabel } from "./shar

function LaunchRow({
item,
permissions,
relaunching,
stopping,
ending,
onLogs,
onStop,
onRelaunch,
onArchive,
onDelete,
}: {
item: WorkerLaunch;
permissions: readonly string[] | undefined;
relaunching: boolean;
stopping: boolean;
ending: boolean;
onLogs: () => void;
onStop: () => void;
onRelaunch: () => void;
onArchive: () => void;
onDelete: () => void;
}) {
const active = isActive(item);
const end = launchEndActions(item, permissions);
const relaunchRequired = item.relaunchRequired;
return (
<li className="border-b px-4 py-3 last:border-b-0 sm:px-5">
Expand All @@ -41,6 +51,7 @@ function LaunchRow({
<Status meta={launchStatusMeta(item.status)} />
<p className="min-w-0 truncate text-sm font-semibold">{item.name}</p>
{relaunchRequired ? <Badge variant="warning">Relaunch required</Badge> : null}
{item.archivedAt ? <Badge variant="secondary">Archived</Badge> : null}
</div>
<p className="text-[13px] text-muted-foreground">
{targetLabel(item.target)} ·{" "}
Expand All @@ -62,7 +73,7 @@ function LaunchRow({
</p>
) : null}
</div>
<div className="flex items-center gap-2 md:shrink-0">
<div className="flex flex-wrap items-center gap-2 md:shrink-0">
{active ? (
<Button size="sm" variant="outline" className="max-md:flex-1" loading={stopping} onClick={onStop}>
<Square /> Stop
Expand All @@ -75,6 +86,22 @@ function LaunchRow({
<Button size="sm" variant="ghost" className="max-md:flex-1" onClick={onLogs}>
<ScrollText /> Logs
</Button>
{end.archive ? (
<Button size="sm" variant="ghost" className="max-md:flex-1" disabled={ending} onClick={onArchive}>
<Archive /> Archive
</Button>
) : null}
{end.remove ? (
<Button
size="sm"
variant="ghost"
className="text-destructive hover:text-destructive max-md:flex-1"
disabled={ending}
onClick={onDelete}
>
<Trash2 /> Delete
</Button>
) : null}
</div>
</div>
</li>
Expand All @@ -84,24 +111,33 @@ function LaunchRow({
/** Worker instances started from a profile by the launcher. */
export function LaunchesSection({
launches,
showInactive,
onShowInactiveChange,
view,
onViewChange,
permissions,
isRelaunching,
isStopping,
isEnding,
onLogs,
onStop,
onRelaunch,
onArchive,
onDelete,
}: {
launches: ReturnType<typeof useWorkerLaunches>;
showInactive: boolean;
onShowInactiveChange: (value: boolean) => void;
view: LaunchView;
onViewChange: (value: LaunchView) => void;
permissions: readonly string[] | undefined;
isRelaunching: (item: WorkerLaunch) => boolean;
isStopping: (item: WorkerLaunch) => boolean;
isEnding: (item: WorkerLaunch) => boolean;
onLogs: (item: WorkerLaunch) => void;
onStop: (item: WorkerLaunch) => void;
onRelaunch: (item: WorkerLaunch) => void;
onArchive: (item: WorkerLaunch) => void;
onDelete: (item: WorkerLaunch) => void;
}) {
const launchList = launches.data ?? [];
const launchList = launchesForView(launches.data ?? [], view);
const showInactive = view !== "active";
return (
<Section
title="Launches"
Expand All @@ -110,11 +146,12 @@ export function LaunchesSection({
actions={
<Segmented
label="Launches to show"
value={showInactive ? "history" : "active"}
onChange={(value) => onShowInactiveChange(value === "history")}
value={view}
onChange={(value) => onViewChange(value as LaunchView)}
options={[
{ value: "active", label: "Active" },
{ value: "history", label: "History" },
{ value: "archived", label: "Archived" },
]}
/>
}
Expand All @@ -132,15 +169,17 @@ export function LaunchesSection({
) : !launchList.length ? (
<EmptyState
icon={<Rocket />}
title={showInactive ? "No launches yet" : "No active launches"}
title={view === "archived" ? "No archived launches" : showInactive ? "No launches yet" : "No active launches"}
description={
showInactive
view === "archived"
? "Archive an ended launch in History to move it here."
: showInactive
? "Launch an enabled profile to start a worker."
: "Launch an enabled profile to start a worker, or switch to History to see past launches."
}
action={
showInactive ? undefined : (
<Button variant="outline" onClick={() => onShowInactiveChange(true)}>
<Button variant="outline" onClick={() => onViewChange("history")}>
Show history
</Button>
)
Expand All @@ -153,11 +192,15 @@ export function LaunchesSection({
<LaunchRow
key={item.id}
item={item}
permissions={permissions}
relaunching={isRelaunching(item)}
stopping={isStopping(item)}
ending={isEnding(item)}
onLogs={() => onLogs(item)}
onStop={() => onStop(item)}
onRelaunch={() => onRelaunch(item)}
onArchive={() => onArchive(item)}
onDelete={() => onDelete(item)}
/>
))}
</ul>
Expand Down
42 changes: 40 additions & 2 deletions apps/web/components/workers/use-worker-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import { toast } from "sonner";

import { useConfirm } from "@/components/ui/confirm";
import { api } from "@/lib/api";
import { launchWorkerProfile, stopWorkerLaunch, workerLaunchLogs } from "@/lib/launcher";
import {
archiveWorkerLaunch,
deleteWorkerLaunch,
launchWorkerProfile,
stopWorkerLaunch,
workerLaunchLogs,
} from "@/lib/launcher";
import { queryKeys, unwrap } from "@/lib/queries";
import type { WorkerLaunch, WorkerProfile } from "@/lib/types";
import { errorMessage } from "@orbit/api-client";

import { isActive } from "./shared";

/** Launch, relaunch, stop, enable/disable, delete and logs for worker profiles and launches. */
/** Launch, relaunch, stop, archive, enable/disable, delete and logs for worker profiles and launches. */
export function useWorkerActions(launchList: WorkerLaunch[]) {
const queryClient = useQueryClient();
const confirm = useConfirm();
Expand Down Expand Up @@ -61,6 +67,24 @@ export function useWorkerActions(launchList: WorkerLaunch[]) {
onError: (error) => toast.error(errorMessage(error)),
});

const archive = useMutation({
mutationFn: archiveWorkerLaunch,
onSuccess: () => {
toast.success("Launch archived");
void queryClient.invalidateQueries({ queryKey: queryKeys.workerLaunches });
},
onError: (error) => toast.error(errorMessage(error)),
});

const removeLaunch = useMutation({
mutationFn: deleteWorkerLaunch,
onSuccess: () => {
toast.success("Launch deleted");
void queryClient.invalidateQueries({ queryKey: queryKeys.workerLaunches });
},
onError: (error) => toast.error(errorMessage(error)),
});

const toggle = useMutation({
mutationFn: async ({ id, enabled }: { id: string; enabled: boolean }) =>
unwrap(
Expand Down Expand Up @@ -133,8 +157,22 @@ export function useWorkerActions(launchList: WorkerLaunch[]) {
if (confirmed) remove.mutate(profile.id);
}

async function confirmDeleteLaunch(item: WorkerLaunch) {
const confirmed = await confirm({
title: `Delete launch ${item.name}?`,
description:
"The launch record is removed from ORBIT. The worker has already ended; its profile and executions are kept. This cannot be undone.",
confirmLabel: "Delete launch",
destructive: true,
});
if (confirmed) removeLaunch.mutate(item.id);
}

return {
launch,
archive,
removeLaunch,
confirmDeleteLaunch,
relaunch,
stop,
toggle,
Expand Down
20 changes: 15 additions & 5 deletions apps/web/components/workers/workers-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { Button } from "@/components/ui/button";
import { ImportPortableButton } from "@/components/import-portable-dialog";
import { Callout } from "@/components/ui/callout";
import { PageHeader } from "@/components/ui/page-header";
import { useMeta, useWorkerLaunches, useWorkerProfiles, useWorkers } from "@/lib/queries";
import { useMe, useMeta, useWorkerLaunches, useWorkerProfiles, useWorkers } from "@/lib/queries";
import type { LaunchView } from "@/lib/run-controls";
import type { WorkerProfile } from "@/lib/types";

import { BootstrapDialog } from "./bootstrap-dialog";
Expand All @@ -25,8 +26,10 @@ import { freshFailedLaunches, workersSummary } from "./workers-summary";
/** The Workers page: live workers, profiles and launches, plus the profile editor. */
export function WorkersView() {
const profiles = useWorkerProfiles();
const [showInactive, setShowInactive] = useState(false);
const [launchView, setLaunchView] = useState<LaunchView>("active");
const showInactive = launchView !== "active";
const launches = useWorkerLaunches(showInactive);
const me = useMe();
const liveWorkers = useWorkers();
const meta = useMeta();
const platformVersion = meta.data?.version;
Expand Down Expand Up @@ -138,7 +141,7 @@ export function WorkersView() {
title={`${failedLaunches.length} ${failedLaunches.length === 1 ? "launch has" : "launches have"} failed`}
action={
showInactive ? undefined : (
<Button size="sm" variant="outline" onClick={() => setShowInactive(true)}>
<Button size="sm" variant="outline" onClick={() => setLaunchView("history")}>
Show history
</Button>
)
Expand Down Expand Up @@ -168,8 +171,15 @@ export function WorkersView() {

<LaunchesSection
launches={launches}
showInactive={showInactive}
onShowInactiveChange={setShowInactive}
view={launchView}
onViewChange={setLaunchView}
permissions={me.data?.permissions}
isEnding={(item) =>
(actions.archive.isPending && actions.archive.variables === item.id) ||
(actions.removeLaunch.isPending && actions.removeLaunch.variables === item.id)
}
onArchive={(item) => actions.archive.mutate(item.id)}
onDelete={(item) => void actions.confirmDeleteLaunch(item)}
isRelaunching={(item) => actions.launch.isPending && actions.launch.variables === item.profileId}
isStopping={(item) => actions.stop.isPending && actions.stop.variables === item.id}
onLogs={(item) => void actions.openLogs(item.id, item.name)}
Expand Down
13 changes: 13 additions & 0 deletions apps/web/lib/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export function stopWorkerLaunch(launchId: string): Promise<WorkerLaunch> {
});
}

/** Hide an ended launch from the launch history; it stays readable under Archived. */
export function archiveWorkerLaunch(launchId: string): Promise<WorkerLaunch> {
return request<WorkerLaunch>(`/api/v1/worker-launches/${launchId}/archive`, {
method: "POST",
body: "{}",
});
}

/** Remove an ended launch record for good. */
export function deleteWorkerLaunch(launchId: string): Promise<void> {
return request<void>(`/api/v1/worker-launches/${launchId}`, { method: "DELETE" });
}

export function workerLaunchLogs(launchId: string): Promise<{ logs: string }> {
return request<{ logs: string }>(`/api/v1/worker-launches/${launchId}/logs`);
}
Expand Down
Loading