Files
PaperClipAI/ui/src/components/routine-page/RoutinePage.tsx
T
DottaandClaude Fable 5.2 33fac97cad feat(routines): ship the redesigned routine page and let live webhooks be tested again (#22205)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Routines start agent work on a schedule, from a webhook, or on
demand
> - The routine detail page split one routine across eight sections
behind a section sidebar (overview, triggers, variables, secrets,
delivery, runs, activity, history)
> - People had to hunt through sections to see what a routine does and
to change it, and a finished webhook could not be tested again without
starting the routine
> - This pull request makes the approved two-column routine page, one
Edit dialog and one Webhook dialog the real routed page, and lets a live
webhook go back into test mode
> - The benefit is one page that answers what the routine does, when it
runs, who runs it and what it made, with every change in one dialog

## Linked Issues or Issue Description

**Problem**
The routine detail page spreads one routine over eight sections and a
section sidebar. Changing a routine means visiting several sections,
each with its own save bar. A webhook that is already live cannot be
tested again: the API only accepts `setupPending: false`, so testing a
live webhook starts the routine.

**Proposed solution**
Ship the reviewed design (stories `routine-page-design`,
`routine-edit-dialog-design`, `routine-webhook-dialog-design`) as the
routed page in both UI shells, wired to the real API. Let `PATCH
/api/routine-triggers/:id` accept `setupPending: true` for a live
webhook so the next events are checked but never start the routine.

**Alternatives considered**
Keep the sections and restyle them. The owner rejected this: the section
sidebar should go.

## What Changed

- `/routines/:id` renders `RoutinePage` in both shells
(`RoutineDetail.production.tsx` is removed). Status switch, Repeats
rows, default agent, responsible user, project, Asks for, Secrets, Run
rules, Managed by plugin, Instructions with Show all, copy and the
comment count, and History.
- History is paged: 20 tasks per page from `GET
/companies/:id/issues?originKind=routine_execution&originId=…&sortField=created&sortDir=desc&limit=21&offset=…`,
with a real Load more. Coalesced runs show as "folded into a task".
- A failed refetch keeps the loaded page on screen (`useQueryView`); a
History failure shows `QueryErrorState` in place.
- Header: Edit, "…" (Version history, Audit log, Archive / Restore) and
Run now. In the streamlined desktop shell they sit in the breadcrumb
toolbar; elsewhere above the page.
- Version history (`/routines/:id/history`, restore and restored-webhook
keys kept) and Audit log (`/routines/:id/activity`,
`RoutineAuditActivity`) open as sub-views with a way back. Old section
addresses and `?tab=` redirect to `/routines/:id`.
- Edit dialog saves title, instructions, variables, secrets, agent,
project and responsible user in one request against the revision the
dialog opened with (save conflict shows "changed while you were
editing"). The first schedule is created, changed or removed after that.
Priority and delivery settings are never sent, so saved values stay.
- Webhooks are created, changed, tested and removed (with Undo) right
away from the Edit dialog, with the Webhook dialog nested inside it.
Closing the Webhook dialog ends setup or test mode. A webhook left in
test mode shows "Testing · events don’t run" on the page and offers
Finish testing.
- Responsibility (only with `enableResponsibilities`): the page shows
direct and through-project responsibilities from the link lookup; the
dialog links or unlinks the direct one. With the flag off nothing is
fetched or shown.
- The routines list creates routines with the same dialog in create
mode, filed in the open folder, and opens the new routine page.
- Server: `setupPending: true` on a live webhook enters test mode
(planned by a pure `planTriggerUpdate`, which also gives the activity
action) (422 for other trigger kinds; 409 for a disabled or removed
webhook or an archived routine). Test deliveries keep the last real
delivery as `lastWebhookDelivery.previous`, and finishing testing
restores it. Activity `routine.trigger_test_started`.
- Additions to the reviewed design, from review, so no capability is
lost: the schedule popover has Pause/Resume; standing-check and API rows
open a small popover with Pause/Resume and Remove; removing a trigger
from the page leaves a "… removed. Undo" line under Repeats. Stories:
`StandingCheckOpened`, `ScheduleTriggerPaused(Opened)`,
`RemovedWithUndo`.
- Save is several requests (routine, schedule, responsibility). It
changes only the schedule the form opened with, records each finished
step, and a partial failure says what was saved and that saving again
finishes it. The dialog cannot be dismissed while saving. Failed reads
show a Retry state, never empty choices.
- Removed: the routine section sidebar and contextual sidebar, the
trigger wizard, trigger cards, save bar, section components and their
tests.
- Storybook: the three routine stories moved to `stories/routines` as
current production stories, a new story mounts the routed page against a
fixture API, and stories that used the old page were updated or removed.

## Verification

- `pnpm --filter @paperclipai/server exec vitest run
src/__tests__/routines-e2e.test.ts` (17/17) and
`src/__tests__/routines-service.test.ts` (105/105).
- `pnpm exec vitest run` on the routine page, routines list, routine
form, webhook instructions, Layout, shell navigation, audit and editor
tests: 275 tests pass.
- `cd ui && npx tsc -p tsconfig.json --noEmit`, `node
scripts/check-token-gates.mjs`, `node scripts/check-maps.mjs`, `node
scripts/storybook-catalog.mjs --check`: pass.
- `pnpm verify` exits 2 (global CI input changed); required CI supplies
the broad coverage.
- Manual: an isolated worktree instance (`pnpm paperclipai worktree init
--empty`, `pnpm dev`) with a seeded routine. Headless Playwright: an old
`/triggers` address redirects, no section sidebar, History shows 20 then
23 after Load more, the schedule popover opens, pause and resume, Edit →
Add webhook → created key → Finish, title save, Test webhook → Finish
testing, Version history, Audit log shows "Started testing a webhook",
phone width has no horizontal overflow, and Create routine from the list
opens the new page. No console errors or failed API calls.

## Risks

- Behaviour change: the section pages are gone. Old links redirect to
the single page.
- The delivery-setting editors (concurrency, catch-up, activity gate)
and priority are removed from the UI. Saved values stay, and the API
still accepts them.
- The create dialog no longer offers a folder picker or delivery
settings. A new routine goes into the folder the list is showing and
takes the default settings.
- Replace key in the Webhook dialog acts at once, as in the reviewed
design. The old confirmation is gone.
- No migration. `lastWebhookDelivery.previous` is an optional field
inside the existing jsonb column.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, in Claude Code with
tool use (shell, file edits, headless browser) and subagents for the
server change and the Storybook move.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 13:13:15 -06:00

796 lines
29 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useId, useState, type ReactNode } from "react";
import type { Issue, RoutineDetail, RoutineTrigger } from "@paperclipai/shared";
import {
ArrowLeft,
CalendarClock,
Check,
ChevronDown,
Copy,
GitBranch,
History,
KeyRound,
MessageSquare,
MoreHorizontal,
Pencil,
Radar,
ScrollText,
Sparkles,
Terminal,
Trash2,
Webhook,
} from "lucide-react";
import { AgentAvatar, type AvatarAgent } from "@/components/AgentAvatar";
import { RunButton } from "@/components/AgentActionButtons";
import { MarkdownBody } from "@/components/MarkdownBody";
import { ScheduleEditor } from "@/components/ScheduleEditor";
import { TaskSummaryList } from "@/components/TaskSummaryList";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { ToggleSwitch } from "@/components/ui/toggle-switch";
import { deriveInitials } from "@/components/Identity";
import { copyTextToClipboard } from "@/lib/clipboard";
import { Link } from "@/lib/router";
import { cn } from "@/lib/utils";
import {
ROUTINE_STATE_LABELS,
WEBHOOK_HEALTH_LABELS,
customRunRules,
describeScheduleTrigger,
formatNextRun,
instructionsPreview,
nextScheduledRun,
routineAutomationState,
routinePausedNotice,
routineStatusDetail,
webhookHealth,
webhookTitle,
} from "./routine-summary";
export type RoutinePerson = { id: string; name: string; avatarUrl?: string | null };
export type RoutinePageAgent = AvatarAgent & { id: string; name: string; urlKey?: string | null };
export type RoutinePageProject = { id: string; name: string; color?: string | null; urlKey?: string | null };
/** A responsibility this routine is under: linked itself, or through its project. */
export type RoutineResponsibility = {
id: string;
title: string;
areaTitle?: string | null;
via: "direct" | "project";
};
export type RoutineScheduleChange = { cronExpression: string; timezone: string };
export type RoutinePageProps = {
routine: RoutineDetail;
agent: RoutinePageAgent | null;
project: RoutinePageProject | null;
responsibleUser: RoutinePerson | null;
/** Leave undefined while areas of responsibility are switched off; the field is then hidden. */
responsibilities?: readonly RoutineResponsibility[];
/** Tasks this routine created, newest first. Pass the pages loaded so far. */
history: readonly Issue[];
historyLoading?: boolean;
/** Shown in place of the task list when History could not be loaded. */
historyError?: ReactNode;
/** More tasks exist beyond the loaded pages. */
hasMoreHistory?: boolean;
loadingMoreHistory?: boolean;
onLoadMoreHistory?: () => void;
/** Recent runs that made no task because an earlier run was still working. */
skippedRunCount?: number;
/** Open comments on the instructions. */
instructionCommentCount?: number;
statusPending?: boolean;
onToggleActive: (active: boolean) => void;
onEdit: () => void;
onSaveSchedule: (triggerId: string, change: RoutineScheduleChange) => void;
onRemoveSchedule: (triggerId: string) => void;
onOpenWebhook: (trigger: RoutineTrigger) => void;
/** Pauses or resumes one schedule, standing check or API trigger. Omit to hide Pause/Resume. */
onSetTriggerPaused?: (triggerId: string, paused: boolean) => void;
/** Removes a standing check or API trigger. Omit to keep those rows read-only. */
onRemoveTrigger?: (trigger: RoutineTrigger) => void;
/** Triggers removed from this page, offered back with Undo under Repeats. */
removedTriggers?: readonly RoutineTrigger[];
onRestoreTrigger?: (trigger: RoutineTrigger) => void;
onOpenInstructionComments?: () => void;
/** Show the whole instructions, e.g. while their comments are open. */
instructionsExpanded?: boolean;
/** Wraps the rendered instructions, e.g. with the comment layer that anchors to them. */
renderInstructionsBody?: (body: ReactNode) => ReactNode;
/** Router state for task links in History, so a task can lead back here. */
historyLinkState?: unknown;
};
function Field({ label, children, action }: { label: string; children: ReactNode; action?: ReactNode }) {
const headingId = useId();
return (
<section className="flex min-w-0 flex-col gap-2" aria-labelledby={headingId}>
<div className="flex items-center justify-between gap-2">
<h2 id={headingId} className="text-sm text-muted-foreground">{label}</h2>
{action}
</div>
{children}
</section>
);
}
function PersonAvatar({ person }: { person: RoutinePerson }) {
return (
<Avatar size="xs" aria-hidden="true">
{person.avatarUrl ? <AvatarImage src={person.avatarUrl} alt="" /> : null}
<AvatarFallback>{deriveInitials(person.name)}</AvatarFallback>
</Avatar>
);
}
function localTimezone(): string {
try {
return Intl.DateTimeFormat().resolvedOptions().timeZone;
} catch {
return "UTC";
}
}
const rowButtonClass =
"group -mx-2 flex min-w-0 items-center gap-2 rounded-md px-2 py-1.5 text-left text-sm hover:bg-accent/50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring";
function ScheduleRow({
trigger,
routineActive,
onSave,
onRemove,
onSetPaused,
}: {
trigger: RoutineTrigger;
routineActive: boolean;
onSave: (change: RoutineScheduleChange) => void;
onRemove: () => void;
onSetPaused?: (paused: boolean) => void;
}) {
const [open, setOpen] = useState(false);
const [cron, setCron] = useState(trigger.cronExpression ?? "0 9 * * 1-5");
const [valid, setValid] = useState(true);
const timezone = trigger.timezone ?? localTimezone();
const dimmed = !trigger.enabled || !routineActive;
return (
<Popover
open={open}
onOpenChange={(next) => {
setOpen(next);
if (next) setCron(trigger.cronExpression ?? "0 9 * * 1-5");
}}
>
<PopoverTrigger asChild>
<button type="button" className={rowButtonClass} aria-label={`Change schedule: ${describeScheduleTrigger(trigger)}`}>
<CalendarClock className="h-4 w-4 shrink-0 text-muted-foreground" aria-hidden="true" />
<span className={cn("min-w-0 flex-1 truncate font-medium", dimmed && "text-muted-foreground")}>
{describeScheduleTrigger(trigger)}
</span>
<span className="shrink-0 text-xs text-muted-foreground">{trigger.enabled ? timezone : "Paused"}</span>
</button>
</PopoverTrigger>
{/* Wider when Pause/Resume joins Remove and Save, so the footer stays on one row. */}
<PopoverContent align="start" className={cn("flex flex-col gap-4", onSetPaused ? "w-96" : "w-80")}>
<ScheduleEditor value={cron} onChange={setCron} onValidityChange={setValid} />
<p className="text-xs text-muted-foreground">Times are in {timezone}.</p>
<div className="flex items-center justify-between gap-2">
<div className="flex items-center gap-1">
<Button
variant="ghost"
size="sm"
className="text-muted-foreground"
onClick={() => {
setOpen(false);
onRemove();
}}
>
Remove schedule
</Button>
{onSetPaused ? (
<Button
variant="ghost"
size="sm"
className="text-muted-foreground"
onClick={() => {
setOpen(false);
onSetPaused(trigger.enabled);
}}
>
{trigger.enabled ? "Pause" : "Resume"}
</Button>
) : null}
</div>
<Button
size="sm"
disabled={!valid}
onClick={() => {
setOpen(false);
onSave({ cronExpression: cron, timezone });
}}
>
Save schedule
</Button>
</div>
</PopoverContent>
</Popover>
);
}
function WebhookRow({
trigger,
routineActive,
onOpen,
}: {
trigger: RoutineTrigger;
routineActive: boolean;
onOpen: () => void;
}) {
const health = webhookHealth(trigger);
const ignored = !routineActive && health !== "setup" && health !== "testing";
const Icon = trigger.signingMode === "github_hmac" ? GitBranch : Webhook;
return (
<button type="button" className={rowButtonClass} onClick={onOpen} aria-label={`Open ${webhookTitle(trigger)}`}>
<Icon className="h-4 w-4 shrink-0 text-muted-foreground" aria-hidden="true" />
<span className={cn("min-w-0 flex-1 truncate font-medium", (ignored || health === "paused") && "text-muted-foreground")}>
{webhookTitle(trigger)}
</span>
<span className={cn("shrink-0 text-xs", health === "rejected" && !ignored ? "text-destructive" : "text-muted-foreground")}>
{ignored ? "Ignoring events" : WEBHOOK_HEALTH_LABELS[health]}
</span>
</button>
);
}
/** A standing check or API trigger: nothing to configure, but it can be paused or removed. */
function StaticTriggerRow({
icon: Icon,
title,
detail,
description,
trigger,
routineActive,
onSetPaused,
onRemove,
}: {
icon: typeof Radar;
title: string;
detail: string;
description: string;
trigger: RoutineTrigger;
routineActive: boolean;
onSetPaused?: (paused: boolean) => void;
onRemove?: () => void;
}) {
const [open, setOpen] = useState(false);
const dimmed = !trigger.enabled || !routineActive;
const content = (
<>
<Icon className="h-4 w-4 shrink-0 text-muted-foreground" aria-hidden="true" />
<span className={cn("min-w-0 flex-1 truncate font-medium", dimmed && "text-muted-foreground")}>{title}</span>
<span className="shrink-0 text-xs text-muted-foreground">{trigger.enabled ? detail : "Paused"}</span>
</>
);
if (!onSetPaused && !onRemove) {
return <div className="flex min-w-0 items-center gap-2 py-1.5 text-sm">{content}</div>;
}
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<button type="button" className={rowButtonClass} aria-label={`Change ${title}`}>
{content}
</button>
</PopoverTrigger>
<PopoverContent align="start" className="flex w-80 flex-col gap-4">
<p className="text-sm text-muted-foreground">{description}</p>
<div className="flex items-center justify-between gap-2">
{onRemove ? (
<Button
variant="ghost"
size="sm"
className="text-muted-foreground"
onClick={() => {
setOpen(false);
onRemove();
}}
>
Remove
</Button>
) : <span />}
{onSetPaused ? (
<Button
variant="outline"
size="sm"
onClick={() => {
setOpen(false);
onSetPaused(trigger.enabled);
}}
>
{trigger.enabled ? "Pause" : "Resume"}
</Button>
) : null}
</div>
</PopoverContent>
</Popover>
);
}
/** How a removed trigger is named in its "removed. Undo" line. */
export function triggerTitle(trigger: RoutineTrigger): string {
if (trigger.kind === "schedule") return describeScheduleTrigger(trigger);
if (trigger.kind === "webhook") return webhookTitle(trigger);
if (trigger.kind === "heartbeat") return "Standing check";
return "API trigger";
}
function Instructions({
markdown,
commentCount = 0,
onOpenComments,
onEdit,
forceExpanded = false,
renderBody = (body) => body,
}: {
markdown: string;
commentCount?: number;
onOpenComments?: () => void;
onEdit: () => void;
forceExpanded?: boolean;
renderBody?: (body: ReactNode) => ReactNode;
}) {
const [expandedState, setExpanded] = useState(false);
const expanded = expandedState || forceExpanded;
const [copied, setCopied] = useState(false);
const { preview, truncated } = instructionsPreview(markdown);
const empty = markdown.trim().length === 0;
return (
<Field
label="Instructions"
action={empty ? null : (
<div className="flex items-center gap-1">
{commentCount > 0 ? (
<Button variant="ghost" size="xs" className="text-muted-foreground" onClick={onOpenComments}>
<MessageSquare />
{commentCount} {commentCount === 1 ? "comment" : "comments"}
</Button>
) : null}
<Button
variant="ghost"
size="icon-xs"
className="text-muted-foreground"
aria-label={copied ? "Instructions copied" : "Copy instructions"}
onClick={async () => {
await copyTextToClipboard(markdown);
setCopied(true);
window.setTimeout(() => setCopied(false), 1500);
}}
>
{copied ? <Check /> : <Copy />}
</Button>
</div>
)}
>
{empty ? (
<div className="flex flex-col items-start gap-2">
<p className="text-sm text-muted-foreground">No instructions yet. The agent only gets the title.</p>
<Button variant="outline" size="sm" onClick={onEdit}>Add instructions</Button>
</div>
) : (
<>
{/* Comments anchor to the full text, so the comment layer only wraps it when the whole text shows. */}
{expanded || !truncated
? renderBody(
<MarkdownBody className="text-sm text-foreground" linkIssueReferences>
{markdown}
</MarkdownBody>,
)
: (
<MarkdownBody className="text-sm text-foreground" linkIssueReferences>
{preview}
</MarkdownBody>
)}
{truncated && !forceExpanded ? (
<Button
variant="ghost"
size="sm"
className="-mx-2 w-fit"
aria-expanded={expanded}
onClick={() => setExpanded((value) => !value)}
>
{expanded ? "Show less" : "Show all"}
<ChevronDown className={cn("h-3.5 w-3.5 transition-transform", expanded && "rotate-180")} />
</Button>
) : null}
</>
)}
</Field>
);
}
export function RoutinePage({
routine,
agent,
project,
responsibleUser,
responsibilities,
history,
historyLoading = false,
historyError,
hasMoreHistory = false,
loadingMoreHistory = false,
onLoadMoreHistory,
skippedRunCount = 0,
instructionCommentCount,
statusPending = false,
onToggleActive,
onEdit,
onSaveSchedule,
onRemoveSchedule,
onSetTriggerPaused,
onRemoveTrigger,
removedTriggers = [],
onRestoreTrigger,
onOpenWebhook,
onOpenInstructionComments,
instructionsExpanded = false,
renderInstructionsBody,
historyLinkState,
}: RoutinePageProps) {
const state = routineAutomationState(routine);
const schedules = routine.triggers.filter((trigger) => trigger.kind === "schedule");
const webhooks = routine.triggers.filter((trigger) => trigger.kind === "webhook");
const standingCheck = routine.triggers.find((trigger) => trigger.kind === "heartbeat");
const apiTrigger = routine.triggers.find((trigger) => trigger.kind === "api");
const nextRun = state === "active" ? nextScheduledRun(routine.triggers) : null;
const hasEnabledWebhook = webhooks.some((trigger) => trigger.enabled && !trigger.setupPending);
const runRules = customRunRules(routine);
const secretNames = Object.keys(routine.env ?? {});
const archived = state === "archived";
const active = state === "active";
const hasSchedule = schedules.some((trigger) => trigger.enabled);
const pausedNotice = routinePausedNotice({ state, hasWebhooks: webhooks.length > 0, hasSchedule });
const statusDetail = routineStatusDetail({ state, nextRun, hasEnabledWebhook, hasStandingCheck: Boolean(standingCheck) });
return (
<div className="mx-auto grid w-full max-w-6xl grid-cols-1 gap-x-12 gap-y-10 px-4 py-8 md:px-8 lg:grid-cols-5">
<div className="flex min-w-0 flex-col gap-6 lg:col-span-2">
<div className="flex min-w-0 flex-col gap-1.5">
<h1 className="text-xl font-semibold text-balance">{routine.title}</h1>
{routine.managedByPlugin ? (
<p className="flex items-center gap-1.5 text-xs text-muted-foreground">
<Sparkles className="h-3.5 w-3.5" aria-hidden="true" />
Managed by {routine.managedByPlugin.pluginDisplayName}
</p>
) : null}
</div>
<Field label="Status">
<div className="flex min-w-0 flex-wrap items-center gap-x-3 gap-y-1">
<ToggleSwitch
checked={state === "active"}
onCheckedChange={onToggleActive}
disabled={statusPending || archived || state === "draft"}
aria-label={state === "active" ? "Pause this routine" : "Turn this routine on"}
/>
<span className={cn("text-sm font-medium", state !== "active" && "text-muted-foreground")}>
{ROUTINE_STATE_LABELS[state]}
</span>
{statusDetail ? <span className="text-sm text-muted-foreground">{statusDetail}</span> : null}
</div>
{pausedNotice ? <p className="text-sm text-muted-foreground" role="note">{pausedNotice}</p> : null}
{state === "draft" ? (
<p className="text-sm text-muted-foreground">
<button type="button" className="font-medium text-foreground underline-offset-4 hover:underline" onClick={onEdit}>
Choose a default agent
</button>{" "}
to turn this routine on.
</p>
) : null}
</Field>
<Field label="Repeats">
<div className="flex min-w-0 flex-col">
{schedules.map((trigger) => (
<ScheduleRow
key={trigger.id}
trigger={trigger}
routineActive={active}
onSave={(change) => onSaveSchedule(trigger.id, change)}
onRemove={() => onRemoveSchedule(trigger.id)}
onSetPaused={onSetTriggerPaused ? (paused) => onSetTriggerPaused(trigger.id, paused) : undefined}
/>
))}
{webhooks.map((trigger) => (
<WebhookRow key={trigger.id} trigger={trigger} routineActive={active} onOpen={() => onOpenWebhook(trigger)} />
))}
{standingCheck ? (
<StaticTriggerRow
icon={Radar}
title="Standing check"
detail="Each time the agent wakes up"
description="The agent carries out these instructions each time it wakes up and reports what it found. It never creates a task by itself."
trigger={standingCheck}
routineActive={active}
onSetPaused={onSetTriggerPaused ? (paused) => onSetTriggerPaused(standingCheck.id, paused) : undefined}
onRemove={onRemoveTrigger ? () => onRemoveTrigger(standingCheck) : undefined}
/>
) : null}
{apiTrigger ? (
<StaticTriggerRow
icon={Terminal}
title="API"
detail="Started by an API call"
description="Starts this routine when an app or agent calls the routine API."
trigger={apiTrigger}
routineActive={active}
onSetPaused={onSetTriggerPaused ? (paused) => onSetTriggerPaused(apiTrigger.id, paused) : undefined}
onRemove={onRemoveTrigger ? () => onRemoveTrigger(apiTrigger) : undefined}
/>
) : null}
{removedTriggers.map((trigger) => (
<p key={trigger.id} role="status" className="flex min-w-0 items-center gap-2 py-1.5 text-sm text-muted-foreground">
<span className="min-w-0 flex-1 truncate">{triggerTitle(trigger)} removed.</span>
{onRestoreTrigger ? (
<Button type="button" variant="ghost" size="xs" onClick={() => onRestoreTrigger(trigger)}>Undo</Button>
) : null}
</p>
))}
{routine.triggers.length === 0 && removedTriggers.length === 0 ? (
<p className="py-1.5 text-sm text-muted-foreground">Doesn’t repeat. Runs only when you start it.</p>
) : null}
</div>
</Field>
<Field label="Default agent">
{agent ? (
<Link
to={`/agents/${agent.urlKey ?? agent.id}`}
className="flex w-fit max-w-full items-center gap-2 py-1.5 text-sm font-medium hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
>
<AgentAvatar agent={agent} size={20} className="shrink-0" />
<span className="truncate">{agent.name}</span>
</Link>
) : (
<Button variant="outline" size="sm" className="w-fit" onClick={onEdit}>Choose an agent</Button>
)}
</Field>
<Field label="Default responsible user">
{responsibleUser ? (
<p className="flex min-w-0 items-center gap-2 py-1.5 text-sm font-medium">
<PersonAvatar person={responsibleUser} />
<span className="truncate">{responsibleUser.name}</span>
</p>
) : (
<p className="py-1.5 text-sm text-muted-foreground">No one yet</p>
)}
</Field>
<Field label="Project">
{project ? (
<Link
to={`/projects/${project.urlKey ?? project.id}`}
className="flex w-fit max-w-full items-center gap-2 py-1.5 text-sm font-medium hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
>
<span className="h-3.5 w-3.5 shrink-0 rounded-sm" style={{ backgroundColor: project.color ?? "var(--project-none)" }} aria-hidden="true" />
<span className="truncate">{project.name}</span>
</Link>
) : (
<p className="py-1.5 text-sm text-muted-foreground">No project</p>
)}
</Field>
{responsibilities ? (
<Field label="Responsibility">
{responsibilities.length === 0 ? (
<p className="py-1.5 text-sm text-muted-foreground">None</p>
) : (
<ul className="flex min-w-0 flex-col">
{responsibilities.map((responsibility) => (
<li key={responsibility.id} className="flex min-w-0 items-baseline gap-2 py-1.5 text-sm">
<Link
to={`/responsibilities/${responsibility.id}`}
className="min-w-0 truncate font-medium hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
>
{responsibility.title}
</Link>
<span className="shrink-0 text-xs text-muted-foreground">
{responsibility.via === "project" && project
? `Through ${project.name}`
: responsibility.areaTitle ?? ""}
</span>
</li>
))}
</ul>
)}
</Field>
) : null}
{routine.variables.length > 0 ? (
<Field label="Asks for">
<ul className="flex min-w-0 flex-col gap-1.5 py-1.5">
{routine.variables.map((variable) => (
<li key={variable.name} className="flex min-w-0 items-baseline gap-2 text-sm">
<span className="truncate font-medium">{variable.label || variable.name}</span>
<span className="shrink-0 text-xs text-muted-foreground">
{variable.defaultValue != null && variable.defaultValue !== ""
? `Usually ${String(variable.defaultValue)}`
: variable.required
? "Required"
: "Optional"}
</span>
</li>
))}
</ul>
</Field>
) : null}
{secretNames.length > 0 ? (
<Field label="Secrets">
<ul className="flex min-w-0 flex-wrap gap-x-4 gap-y-1.5 py-1.5">
{secretNames.map((name) => (
<li key={name} className="flex min-w-0 items-center gap-1.5 font-mono text-xs">
<KeyRound className="h-3.5 w-3.5 shrink-0 text-muted-foreground" aria-hidden="true" />
<span className="truncate">{name}</span>
</li>
))}
</ul>
</Field>
) : null}
{runRules.length > 0 ? (
<Field label="Run rules">
<ul className="flex flex-col gap-1 py-1.5 text-sm">
{runRules.map((rule) => <li key={rule}>{rule}</li>)}
</ul>
</Field>
) : null}
</div>
<div className="flex min-w-0 flex-col gap-8 lg:col-span-3">
<Instructions
markdown={routine.description ?? ""}
commentCount={instructionCommentCount}
onOpenComments={onOpenInstructionComments}
onEdit={onEdit}
forceExpanded={instructionsExpanded}
renderBody={renderInstructionsBody}
/>
<Field label="History">
{historyError && history.length === 0 ? (
historyError
) : historyLoading ? (
<p className="py-1.5 text-sm text-muted-foreground" role="status">Loading tasks…</p>
) : history.length === 0 ? (
<p className="py-1.5 text-sm text-muted-foreground">
{state === "active" && nextRun
? `No tasks yet. The first one starts ${formatNextRun(nextRun)}.`
: "No tasks yet. Each run creates a task here."}
</p>
) : (
<TaskSummaryList issues={history} label="Tasks created by this routine" issueLinkState={historyLinkState} />
)}
{historyError && history.length > 0 ? historyError : null}
{hasMoreHistory && history.length > 0 ? (
<Button
variant="ghost"
size="sm"
className="-mx-2 w-fit text-muted-foreground"
disabled={loadingMoreHistory}
onClick={onLoadMoreHistory}
>
{loadingMoreHistory ? "Loading…" : "Load more"}
</Button>
) : null}
{skippedRunCount > 0 ? (
<p className="text-xs text-muted-foreground">
{skippedRunCount === 1 ? "1 run was" : `${skippedRunCount} runs were`} folded into a task that was still in
progress.
</p>
) : null}
</Field>
</div>
</div>
);
}
/** Version history and the audit log open as their own view, with a way back to the routine. */
export function RoutineSubpage({
routineTitle,
title,
onBack,
children,
}: {
routineTitle: string;
title: string;
onBack: () => void;
children: ReactNode;
}) {
return (
<div className="mx-auto flex w-full max-w-5xl flex-col gap-6 px-4 py-8 md:px-8">
<div className="flex flex-col items-start gap-2">
<Button variant="ghost" size="sm" className="-mx-2 text-muted-foreground" onClick={onBack}>
<ArrowLeft className="h-3.5 w-3.5" />
{routineTitle}
</Button>
<h1 className="text-xl font-semibold">{title}</h1>
</div>
{children}
</div>
);
}
export type RoutinePageActionsProps = {
archived?: boolean;
runPending?: boolean;
/** Start with the menu open, for review. */
defaultMenuOpen?: boolean;
onEdit: () => void;
onRunNow: () => void;
onOpenVersionHistory: () => void;
onOpenAuditLog: () => void;
onArchive: () => void;
onRestore?: () => void;
};
/** Header controls for the routine page: edit, a short overflow menu, and Run now. */
export function RoutinePageActions({
archived = false,
runPending = false,
defaultMenuOpen = false,
onEdit,
onRunNow,
onOpenVersionHistory,
onOpenAuditLog,
onArchive,
onRestore,
}: RoutinePageActionsProps) {
return (
<div className="flex items-center gap-1">
{archived ? null : (
<Button variant="ghost" size="icon-sm" aria-label="Edit routine" onClick={onEdit}>
<Pencil />
</Button>
)}
<DropdownMenu defaultOpen={defaultMenuOpen}>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon-sm" aria-label="More routine actions">
<MoreHorizontal />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<DropdownMenuItem onSelect={onOpenVersionHistory}>
<History />
Version history
</DropdownMenuItem>
<DropdownMenuItem onSelect={onOpenAuditLog}>
<ScrollText />
Audit log
</DropdownMenuItem>
<DropdownMenuSeparator />
{archived && onRestore ? (
<DropdownMenuItem onSelect={onRestore}>Restore routine</DropdownMenuItem>
) : (
<DropdownMenuItem variant="destructive" onSelect={onArchive}>
<Trash2 />
Archive routine
</DropdownMenuItem>
)}
</DropdownMenuContent>
</DropdownMenu>
{archived ? null : <RunButton onClick={onRunNow} disabled={runPending} />}
</div>
);
}