feat(web): create webhook automations and inspect their deliveries (#15088)
This commit is contained in:
1 parent
aee889b0e0
commit
ea4b44343d
5 files changed
+520
-47
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import { useAtomValue } from "@effect/atom-react";
|
||||
import {
|
||||
Clock3Icon,
|
||||
CopyIcon,
|
||||
InboxIcon,
|
||||
MoreHorizontalIcon,
|
||||
PencilIcon,
|
||||
PlayIcon,
|
||||
@@ -17,6 +19,8 @@ import type {
|
||||
ScheduledTaskId,
|
||||
ScheduledTaskSchedule,
|
||||
ScheduledTaskUpsertInput,
|
||||
ScheduledTaskWebhookDeliveryOutcome,
|
||||
ScheduledTaskWebhookDeliverySummary,
|
||||
ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
import {
|
||||
@@ -37,7 +41,14 @@ import {
|
||||
deriveProviderInstanceEntries,
|
||||
sortProviderInstanceEntries,
|
||||
} from "../../providerInstances";
|
||||
import { useEnvironment, type EnvironmentPresentation } from "../../state/environments";
|
||||
import { requestConfirmDialog } from "../../confirmDialog";
|
||||
import { webhookAddress } from "@t3tools/client-runtime/webhook-address";
|
||||
import { useCopyToClipboard } from "../../hooks/useCopyToClipboard";
|
||||
import {
|
||||
useEnvironment,
|
||||
useEnvironmentHttpBaseUrl,
|
||||
type EnvironmentPresentation,
|
||||
} from "../../state/environments";
|
||||
import { useProjects } from "../../state/entities";
|
||||
import { useEnvironmentQuery } from "../../state/query";
|
||||
import { EMPTY_SERVER_PROVIDERS, serverEnvironment } from "../../state/server";
|
||||
@@ -46,10 +57,14 @@ import { WorktreeBaseBranchPicker } from "../WorktreeBaseBranchPicker";
|
||||
import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
|
||||
import { useSettingsScope } from "./SettingsScopeContext";
|
||||
import {
|
||||
DEFAULT_WEBHOOK_PROMPT,
|
||||
WEBHOOK_SIGNATURE_DEFAULTS,
|
||||
matchesScheduledTaskScope,
|
||||
scheduleFromDraft,
|
||||
scheduledTaskDefaultModel,
|
||||
taskToDraft,
|
||||
type DraftState,
|
||||
type ScheduleMode,
|
||||
type WorkspaceMode,
|
||||
} from "./scheduledTasksSettings.logic";
|
||||
import { Label } from "../ui/label";
|
||||
@@ -112,6 +127,9 @@ const EMPTY_DRAFT: DraftState = {
|
||||
runtimeMode: "full-access",
|
||||
interactionMode: "default",
|
||||
baseModelSelection: null,
|
||||
signatureEnabled: false,
|
||||
...WEBHOOK_SIGNATURE_DEFAULTS,
|
||||
signatureSecret: "",
|
||||
};
|
||||
|
||||
/** Labelled field: a caption sitting above its control. */
|
||||
@@ -148,19 +166,6 @@ function splitModelKey(value: string): ModelSelection | null {
|
||||
};
|
||||
}
|
||||
|
||||
function scheduleFromDraft(draft: DraftState): ScheduledTaskSchedule {
|
||||
if (draft.scheduleMode === "interval") {
|
||||
const everyMs = Math.round(Number(draft.intervalMinutes) * 60_000);
|
||||
return { type: "interval", everyMs };
|
||||
}
|
||||
const selectedEveryDay = draft.weekdays.size === 0 || draft.weekdays.size === 7;
|
||||
return {
|
||||
type: "fixed_time",
|
||||
timeOfDay: draft.timeOfDay || "09:00",
|
||||
...(selectedEveryDay ? {} : { weekdays: [...draft.weekdays].toSorted() }),
|
||||
};
|
||||
}
|
||||
|
||||
export function scheduleLabel(schedule: ScheduledTaskSchedule): string {
|
||||
if (schedule.type === "webhook") return "On webhook";
|
||||
if (schedule.type === "interval") {
|
||||
@@ -200,6 +205,20 @@ export function relativeLabel(value: string | null): string {
|
||||
return `in ${Math.round(hours / 24)}d`;
|
||||
}
|
||||
|
||||
const DELIVERY_OUTCOME_LABELS: Record<ScheduledTaskWebhookDeliveryOutcome, string> = {
|
||||
accepted: "Ran",
|
||||
dispatch_failed: "Run failed",
|
||||
rejected_signature: "Bad signature",
|
||||
disabled: "Task paused",
|
||||
rate_limited: "Rate limited",
|
||||
};
|
||||
|
||||
function deliveryOutcomeVariant(outcome: ScheduledTaskWebhookDeliveryOutcome) {
|
||||
if (outcome === "accepted") return "success";
|
||||
if (outcome === "disabled" || outcome === "rate_limited") return "warning";
|
||||
return "error";
|
||||
}
|
||||
|
||||
function statusVariant(status: ScheduledTask["lastRunStatus"]) {
|
||||
if (status === "failed") return "error";
|
||||
if (status === "succeeded") return "success";
|
||||
@@ -313,7 +332,7 @@ function ScheduledTaskEnvironmentSection({
|
||||
const linkedTask = tasks?.find((task) => task.id === taskId);
|
||||
const openedLink = useRef(false);
|
||||
useEffect(() => {
|
||||
if (!openedLink.current && linkedTask && linkedTask.schedule.type !== "webhook") {
|
||||
if (!openedLink.current && linkedTask) {
|
||||
openedLink.current = true;
|
||||
onEdit(environment.environmentId, linkedTask);
|
||||
}
|
||||
@@ -347,12 +366,6 @@ function ScheduledTaskEnvironmentSection({
|
||||
description="This task no longer exists or is outside the selected project scope."
|
||||
role="status"
|
||||
/>
|
||||
) : linkedTask?.schedule.type === "webhook" ? (
|
||||
<SettingsRow
|
||||
title="Webhook tasks can't be edited here yet"
|
||||
description="Update T3 Code to edit this task."
|
||||
role="status"
|
||||
/>
|
||||
) : null}
|
||||
{tasks.length === 0 ? (
|
||||
<SettingsRow
|
||||
@@ -385,6 +398,8 @@ function ScheduledTaskRow({
|
||||
readonly onEdit: () => void;
|
||||
}) {
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [deliveriesOpen, setDeliveriesOpen] = useState(false);
|
||||
const isWebhook = task.schedule.type === "webhook";
|
||||
const toggle = useAtomCommand(serverEnvironment.setScheduledTaskEnabled, {
|
||||
label: "scheduled task enabled",
|
||||
});
|
||||
@@ -422,11 +437,13 @@ function ScheduledTaskRow({
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<span>
|
||||
{scheduleLabel(task.schedule)} ·{" "}
|
||||
{task.enabled
|
||||
? task.nextRunAt
|
||||
? `Next run ${relativeLabel(task.nextRunAt)}`
|
||||
: "Not scheduled"
|
||||
: "Paused"}
|
||||
{!task.enabled
|
||||
? "Paused"
|
||||
: isWebhook
|
||||
? "Listening"
|
||||
: task.nextRunAt
|
||||
? `Next run ${relativeLabel(task.nextRunAt)}`
|
||||
: "Not scheduled"}
|
||||
</span>
|
||||
{task.lastRunStatus !== "never" ? (
|
||||
<Badge variant={statusVariant(task.lastRunStatus)}>{task.lastRunStatus}</Badge>
|
||||
@@ -456,15 +473,21 @@ function ScheduledTaskRow({
|
||||
<MoreHorizontalIcon className="size-4" />
|
||||
</MenuTrigger>
|
||||
<MenuPopup align="end">
|
||||
{/* Webhook tasks are not editable here yet; saving would drop their URL. */}
|
||||
<MenuItem onClick={onEdit} disabled={task.schedule.type === "webhook"}>
|
||||
<MenuItem onClick={onEdit}>
|
||||
<PencilIcon />
|
||||
Edit
|
||||
</MenuItem>
|
||||
<MenuItem onClick={() => void act("run")} disabled={task.schedule.type === "webhook"}>
|
||||
<PlayIcon />
|
||||
Run now
|
||||
</MenuItem>
|
||||
{isWebhook ? (
|
||||
<MenuItem onClick={() => setDeliveriesOpen(true)}>
|
||||
<InboxIcon />
|
||||
Deliveries
|
||||
</MenuItem>
|
||||
) : (
|
||||
<MenuItem onClick={() => void act("run")}>
|
||||
<PlayIcon />
|
||||
Run now
|
||||
</MenuItem>
|
||||
)}
|
||||
<MenuSeparator />
|
||||
<MenuItem onClick={() => void act("delete")}>
|
||||
<Trash2Icon />
|
||||
@@ -472,12 +495,240 @@ function ScheduledTaskRow({
|
||||
</MenuItem>
|
||||
</MenuPopup>
|
||||
</Menu>
|
||||
{deliveriesOpen ? (
|
||||
<WebhookDeliveriesDialog
|
||||
environmentId={environmentId}
|
||||
task={task}
|
||||
onClose={() => setDeliveriesOpen(false)}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function WebhookDeliveriesDialog({
|
||||
environmentId,
|
||||
task,
|
||||
onClose,
|
||||
}: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly task: ScheduledTask;
|
||||
readonly onClose: () => void;
|
||||
}) {
|
||||
const deliveriesQuery = useEnvironmentQuery(
|
||||
serverEnvironment.scheduledTaskWebhookDeliveries({ environmentId, input: { id: task.id } }),
|
||||
);
|
||||
const [selectedId, setSelectedId] = useState<ScheduledTaskWebhookDeliverySummary["id"] | null>(
|
||||
null,
|
||||
);
|
||||
const selectedQuery = useEnvironmentQuery(
|
||||
selectedId === null
|
||||
? null
|
||||
: serverEnvironment.scheduledTaskWebhookDelivery({
|
||||
environmentId,
|
||||
input: { id: task.id, deliveryId: selectedId },
|
||||
}),
|
||||
);
|
||||
const deliveries = deliveriesQuery.data?.deliveries ?? null;
|
||||
const selected = selectedQuery.data?.delivery ?? null;
|
||||
const error = selectedId === null ? deliveriesQuery.error : selectedQuery.error;
|
||||
|
||||
return (
|
||||
<Dialog
|
||||
open
|
||||
onOpenChange={(next) => {
|
||||
if (!next) onClose();
|
||||
}}
|
||||
>
|
||||
<DialogPopup className="max-w-2xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Deliveries · {task.title}</DialogTitle>
|
||||
<DialogDescription>Recent requests to this task's webhook URL.</DialogDescription>
|
||||
</DialogHeader>
|
||||
<DialogPanel>
|
||||
{error ? <p className="text-sm text-destructive">{error}</p> : null}
|
||||
{selectedId !== null && selected === null && selectedQuery.error === null ? (
|
||||
<p className="text-sm text-muted-foreground" role="status">
|
||||
Loading delivery…
|
||||
</p>
|
||||
) : selected ? (
|
||||
<div className="space-y-4">
|
||||
<div className="flex flex-wrap items-center gap-2 text-sm">
|
||||
<Badge variant={deliveryOutcomeVariant(selected.outcome)}>
|
||||
{DELIVERY_OUTCOME_LABELS[selected.outcome]}
|
||||
</Badge>
|
||||
<span>
|
||||
{selected.method} · {relativeLabel(selected.receivedAt)}
|
||||
</span>
|
||||
{selected.signatureVerified ? (
|
||||
<span className="text-muted-foreground">Signature verified</span>
|
||||
) : null}
|
||||
</div>
|
||||
{selected.error ? <p className="text-sm text-destructive">{selected.error}</p> : null}
|
||||
<DeliveryBlock title="Prompt sent to the agent">
|
||||
{selected.renderedPrompt ?? "No run was started for this request."}
|
||||
</DeliveryBlock>
|
||||
{selected.missingFields.length > 0 ? (
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Empty placeholders: {selected.missingFields.join(", ")}
|
||||
</p>
|
||||
) : null}
|
||||
<DeliveryBlock title="Headers">
|
||||
{Object.entries(selected.headers)
|
||||
.map(([name, value]) => `${name}: ${value}`)
|
||||
.join("\n")}
|
||||
</DeliveryBlock>
|
||||
{selected.query ? (
|
||||
<DeliveryBlock title="Query">{selected.query}</DeliveryBlock>
|
||||
) : null}
|
||||
<DeliveryBlock title={selected.bodyTruncated ? "Body (truncated)" : "Body"}>
|
||||
{selected.body || "(empty)"}
|
||||
</DeliveryBlock>
|
||||
</div>
|
||||
) : selectedId !== null ? null : deliveries === null ? (
|
||||
<p className="text-sm text-muted-foreground" role="status">
|
||||
Loading deliveries…
|
||||
</p>
|
||||
) : deliveries.length === 0 ? (
|
||||
<p className="text-sm text-muted-foreground">No requests yet.</p>
|
||||
) : (
|
||||
<ul className="divide-y divide-border">
|
||||
{deliveries.map((delivery) => (
|
||||
<li key={delivery.id}>
|
||||
<button
|
||||
type="button"
|
||||
className="flex w-full flex-wrap items-center gap-2 py-2 text-left text-sm hover:bg-accent/40"
|
||||
onClick={() => setSelectedId(delivery.id)}
|
||||
>
|
||||
<Badge variant={deliveryOutcomeVariant(delivery.outcome)}>
|
||||
{DELIVERY_OUTCOME_LABELS[delivery.outcome]}
|
||||
</Badge>
|
||||
<span>{delivery.method}</span>
|
||||
<span className="text-muted-foreground">
|
||||
{relativeLabel(delivery.receivedAt)}
|
||||
</span>
|
||||
{delivery.missingFields.length > 0 ? (
|
||||
<span className="text-muted-foreground">
|
||||
{delivery.missingFields.length} empty placeholder
|
||||
{delivery.missingFields.length === 1 ? "" : "s"}
|
||||
</span>
|
||||
) : null}
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
</DialogPanel>
|
||||
<DialogFooter>
|
||||
{selectedId !== null ? (
|
||||
<Button variant="outline" size="sm" onClick={() => setSelectedId(null)}>
|
||||
Back
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
disabled={deliveriesQuery.isPending}
|
||||
onClick={deliveriesQuery.refresh}
|
||||
>
|
||||
Refresh
|
||||
</Button>
|
||||
)}
|
||||
<DialogClose render={<Button size="sm" />}>Done</DialogClose>
|
||||
</DialogFooter>
|
||||
</DialogPopup>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
function DeliveryBlock({ title, children }: { title: string; children: ReactNode }) {
|
||||
return (
|
||||
<div className="space-y-1">
|
||||
<Label>{title}</Label>
|
||||
<pre className="max-h-64 overflow-auto rounded-md bg-muted p-2 text-xs whitespace-pre-wrap break-all">
|
||||
{children}
|
||||
</pre>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function WebhookEndpointField({
|
||||
environmentId,
|
||||
task,
|
||||
}: {
|
||||
readonly environmentId: EnvironmentId;
|
||||
readonly task: ScheduledTask | null;
|
||||
}) {
|
||||
const httpBaseUrl = useEnvironmentHttpBaseUrl(environmentId);
|
||||
const { copyToClipboard, isCopied } = useCopyToClipboard({ target: "webhook URL" });
|
||||
const rotate = useAtomCommand(serverEnvironment.rotateScheduledTaskWebhookToken, {
|
||||
label: "scheduled task rotate webhook token",
|
||||
});
|
||||
const [rotating, setRotating] = useState(false);
|
||||
const endpoint = task?.webhook;
|
||||
if (!task || !endpoint) {
|
||||
return <p className="text-sm text-muted-foreground">The URL appears after you save.</p>;
|
||||
}
|
||||
const { address: url, copyable, note } = webhookAddress(endpoint, httpBaseUrl);
|
||||
const rotateUrl = async () => {
|
||||
const confirmed =
|
||||
(await requestConfirmDialog("Rotate this webhook URL?\nThe current URL stops working.", {
|
||||
variant: "destructive",
|
||||
})) ??
|
||||
// No themed dialog host is mounted; fall back to the native prompt
|
||||
// rather than rotating unasked.
|
||||
window.confirm("Rotate this webhook URL? The current URL stops working.");
|
||||
if (!confirmed) return;
|
||||
setRotating(true);
|
||||
const result = await rotate({ environmentId, input: { id: task.id } });
|
||||
setRotating(false);
|
||||
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
|
||||
toastManager.add(
|
||||
stackedThreadToast({
|
||||
type: "error",
|
||||
title: "Could not rotate webhook URL",
|
||||
description: String(squashAtomCommandFailure(result)),
|
||||
}),
|
||||
);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<div className="space-y-1.5">
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
readOnly
|
||||
aria-label="Webhook URL"
|
||||
value={url}
|
||||
onFocus={(event) => event.currentTarget.select()}
|
||||
/>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
type="button"
|
||||
// A bare path is not a URL a sender can call.
|
||||
disabled={!copyable}
|
||||
onClick={() => copyToClipboard(url, undefined)}
|
||||
>
|
||||
<CopyIcon />
|
||||
{isCopied ? "Copied" : "Copy"}
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
type="button"
|
||||
disabled={rotating}
|
||||
onClick={() => void rotateUrl()}
|
||||
>
|
||||
Rotate
|
||||
</Button>
|
||||
</div>
|
||||
{note !== null ? <p className="text-xs text-muted-foreground">{note}</p> : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ScheduledTaskEditorDialog({
|
||||
initialEnvironmentId,
|
||||
task,
|
||||
@@ -527,6 +778,11 @@ function ScheduledTaskEditorDialog({
|
||||
draft.editingId !== null &&
|
||||
tasksQuery.data !== null &&
|
||||
!tasksQuery.data.tasks.some((entry) => entry.id === draft.editingId);
|
||||
// The live row, so a rotated URL shows up without reopening the dialog.
|
||||
// Once the list has loaded, a missing task is gone; don't keep showing its URL.
|
||||
const liveTask = tasksQuery.data
|
||||
? (tasksQuery.data.tasks.find((entry) => entry.id === draft.editingId) ?? null)
|
||||
: task;
|
||||
const selectedProjectId = draft.projectId || projects[0]?.id || "";
|
||||
const selectedProject = projects.find((project) => project.id === selectedProjectId);
|
||||
|
||||
@@ -574,6 +830,16 @@ function ScheduledTaskEditorDialog({
|
||||
return;
|
||||
}
|
||||
const schedule = scheduleFromDraft(draft);
|
||||
if (
|
||||
schedule.type === "webhook" &&
|
||||
schedule.signature &&
|
||||
(!schedule.signature.header ||
|
||||
(!schedule.signature.secret &&
|
||||
!(liveTask?.schedule.type === "webhook" && liveTask.webhook?.hasSecret)))
|
||||
) {
|
||||
reportFailure("Signing secret is required", "Enter the signature header and secret.");
|
||||
return;
|
||||
}
|
||||
if (
|
||||
schedule.type === "interval" &&
|
||||
(!Number.isSafeInteger(schedule.everyMs) || schedule.everyMs < MIN_SCHEDULED_TASK_INTERVAL_MS)
|
||||
@@ -643,7 +909,8 @@ function ScheduledTaskEditorDialog({
|
||||
<DialogHeader>
|
||||
<DialogTitle>{draft.editingId ? "Edit task" : "New task"}</DialogTitle>
|
||||
<DialogDescription>
|
||||
Run a prompt automatically — on an interval or at a fixed time.
|
||||
Run a prompt automatically — on an interval, at a fixed time, or when a webhook is
|
||||
called.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
@@ -833,17 +1100,116 @@ function ScheduledTaskEditorDialog({
|
||||
aria-label="Schedule type"
|
||||
value={[draft.scheduleMode]}
|
||||
onValueChange={(values) => {
|
||||
const mode = values[0];
|
||||
if (mode === "fixed" || mode === "interval")
|
||||
setDraft((current) => ({ ...current, scheduleMode: mode }));
|
||||
const mode = values[0] as ScheduleMode | undefined;
|
||||
if (mode === "fixed" || mode === "interval" || mode === "webhook")
|
||||
setDraft((current) => ({
|
||||
...current,
|
||||
scheduleMode: mode,
|
||||
prompt:
|
||||
mode === "webhook" && !current.prompt.trim()
|
||||
? DEFAULT_WEBHOOK_PROMPT
|
||||
: current.prompt,
|
||||
}));
|
||||
}}
|
||||
>
|
||||
<Toggle value="fixed">At a time</Toggle>
|
||||
<Toggle value="interval">Every interval</Toggle>
|
||||
<Toggle value="webhook">On webhook</Toggle>
|
||||
</ToggleGroup>
|
||||
</div>
|
||||
|
||||
{draft.scheduleMode === "fixed" ? (
|
||||
{draft.scheduleMode === "webhook" ? (
|
||||
<div className="space-y-4">
|
||||
<WebhookEndpointField environmentId={environmentId} task={liveTask} />
|
||||
<p className="text-xs text-muted-foreground">
|
||||
{
|
||||
"Each request runs the prompt. Use {{body.path}}, {{headers.name}}, {{query.name}}, {{body}} or {{request}} in the prompt; only what it names reaches the agent."
|
||||
}
|
||||
</p>
|
||||
<div className="flex items-center justify-between gap-4">
|
||||
<div className="min-w-0 space-y-1">
|
||||
<Label htmlFor="scheduled-task-signature">Require signature</Label>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Reject requests without a valid HMAC-SHA256 signature of the body.
|
||||
</p>
|
||||
</div>
|
||||
<Switch
|
||||
id="scheduled-task-signature"
|
||||
checked={draft.signatureEnabled}
|
||||
onCheckedChange={(signatureEnabled) =>
|
||||
setDraft((current) => ({ ...current, signatureEnabled }))
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
{draft.signatureEnabled ? (
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
<Field label="Header" htmlFor="scheduled-task-signature-header">
|
||||
<Input
|
||||
id="scheduled-task-signature-header"
|
||||
value={draft.signatureHeader}
|
||||
onChange={(event) =>
|
||||
setDraft((current) => ({
|
||||
...current,
|
||||
signatureHeader: event.target.value,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Prefix" htmlFor="scheduled-task-signature-prefix">
|
||||
<Input
|
||||
id="scheduled-task-signature-prefix"
|
||||
value={draft.signaturePrefix}
|
||||
placeholder="None"
|
||||
onChange={(event) =>
|
||||
setDraft((current) => ({
|
||||
...current,
|
||||
signaturePrefix: event.target.value,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
<Field label="Encoding" htmlFor="scheduled-task-signature-encoding">
|
||||
<Select
|
||||
value={draft.signatureEncoding}
|
||||
onValueChange={(value) =>
|
||||
setDraft((current) => ({
|
||||
...current,
|
||||
signatureEncoding: value === "base64" ? "base64" : "hex",
|
||||
}))
|
||||
}
|
||||
>
|
||||
<SelectTrigger size="sm" id="scheduled-task-signature-encoding">
|
||||
<SelectValue>{draft.signatureEncoding}</SelectValue>
|
||||
</SelectTrigger>
|
||||
<SelectPopup>
|
||||
<SelectItem value="hex">hex</SelectItem>
|
||||
<SelectItem value="base64">base64</SelectItem>
|
||||
</SelectPopup>
|
||||
</Select>
|
||||
</Field>
|
||||
<Field label="Secret" htmlFor="scheduled-task-signature-secret">
|
||||
<Input
|
||||
id="scheduled-task-signature-secret"
|
||||
type="password"
|
||||
autoComplete="off"
|
||||
value={draft.signatureSecret}
|
||||
placeholder={
|
||||
liveTask?.schedule.type === "webhook" && liveTask.webhook?.hasSecret
|
||||
? "Unchanged"
|
||||
: "Shared secret"
|
||||
}
|
||||
onChange={(event) =>
|
||||
setDraft((current) => ({
|
||||
...current,
|
||||
signatureSecret: event.target.value,
|
||||
}))
|
||||
}
|
||||
/>
|
||||
</Field>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : draft.scheduleMode === "fixed" ? (
|
||||
<div className="flex flex-wrap items-center gap-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<Label htmlFor="scheduled-task-time">Run at</Label>
|
||||
|
||||
@@ -19,6 +19,7 @@ import { deriveProviderInstanceEntries } from "../../providerInstances";
|
||||
import {
|
||||
scheduledTaskDefaultModel,
|
||||
matchesScheduledTaskScope,
|
||||
scheduleFromDraft,
|
||||
taskToDraft,
|
||||
} from "./scheduledTasksSettings.logic";
|
||||
|
||||
@@ -168,6 +169,33 @@ describe("editing scheduled task branch settings", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("webhook scheduled tasks", () => {
|
||||
const signature = { header: "x-signature", encoding: "base64", prefix: "" } as const;
|
||||
const webhookTask: ScheduledTask = {
|
||||
...legacyTask,
|
||||
schedule: { type: "webhook", signature },
|
||||
webhook: { path: "/api/hooks/legacy-task/token", url: null, hasSecret: true },
|
||||
};
|
||||
|
||||
it("keeps a stored secret when the secret field is left blank", () => {
|
||||
const draft = taskToDraft(webhookTask);
|
||||
expect(draft.scheduleMode).toBe("webhook");
|
||||
expect(draft.signatureSecret).toBe("");
|
||||
expect(scheduleFromDraft(draft)).toEqual({ type: "webhook", signature });
|
||||
expect(scheduleFromDraft({ ...draft, signatureSecret: " new " })).toEqual({
|
||||
type: "webhook",
|
||||
signature: { ...signature, secret: "new" },
|
||||
});
|
||||
});
|
||||
|
||||
it("drops the signature when it is switched off and offers GitHub's settings", () => {
|
||||
const draft = taskToDraft({ ...webhookTask, schedule: { type: "webhook", signature: null } });
|
||||
expect(draft.signatureEnabled).toBe(false);
|
||||
expect(draft.signatureHeader).toBe("x-hub-signature-256");
|
||||
expect(scheduleFromDraft(draft)).toEqual({ type: "webhook", signature: null });
|
||||
});
|
||||
});
|
||||
|
||||
describe("scheduled task model defaults", () => {
|
||||
const instanceId = ProviderInstanceId.make("codex");
|
||||
const projectId = ProjectId.make("project");
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
type ProjectId,
|
||||
ScheduledTaskId,
|
||||
type ScheduledTask,
|
||||
type ScheduledTaskUpsertSchedule,
|
||||
type ModelSelection,
|
||||
type RuntimeMode,
|
||||
type ProviderInteractionMode,
|
||||
@@ -43,7 +44,7 @@ export function validateScheduledTasksSearch(raw: Record<string, unknown>) {
|
||||
};
|
||||
}
|
||||
|
||||
type ScheduleMode = "fixed" | "interval";
|
||||
export type ScheduleMode = "fixed" | "interval" | "webhook";
|
||||
export type WorkspaceMode = "root" | "worktree" | "existing_worktree";
|
||||
|
||||
export interface DraftState {
|
||||
@@ -71,6 +72,49 @@ export interface DraftState {
|
||||
* (reasoning, temperature, …) when the model itself is left unchanged.
|
||||
*/
|
||||
readonly baseModelSelection: ModelSelection | null;
|
||||
readonly signatureEnabled: boolean;
|
||||
readonly signatureHeader: string;
|
||||
readonly signatureEncoding: "hex" | "base64";
|
||||
readonly signaturePrefix: string;
|
||||
/** Write-only: empty keeps the secret already stored on the server. */
|
||||
readonly signatureSecret: string;
|
||||
}
|
||||
|
||||
/** GitHub's signature settings, the most common sender. */
|
||||
export const WEBHOOK_SIGNATURE_DEFAULTS = {
|
||||
signatureHeader: "x-hub-signature-256",
|
||||
signatureEncoding: "hex",
|
||||
signaturePrefix: "sha256=",
|
||||
} as const;
|
||||
|
||||
/** Prompt a new webhook task starts with: the whole request, which the user can narrow down. */
|
||||
export const DEFAULT_WEBHOOK_PROMPT = "Handle this webhook:\n{{request}}";
|
||||
|
||||
export function scheduleFromDraft(draft: DraftState): ScheduledTaskUpsertSchedule {
|
||||
if (draft.scheduleMode === "webhook") {
|
||||
const secret = draft.signatureSecret.trim();
|
||||
return {
|
||||
type: "webhook",
|
||||
signature: draft.signatureEnabled
|
||||
? {
|
||||
header: draft.signatureHeader.trim(),
|
||||
encoding: draft.signatureEncoding,
|
||||
prefix: draft.signaturePrefix,
|
||||
...(secret ? { secret } : {}),
|
||||
}
|
||||
: null,
|
||||
};
|
||||
}
|
||||
if (draft.scheduleMode === "interval") {
|
||||
const everyMs = Math.round(Number(draft.intervalMinutes) * 60_000);
|
||||
return { type: "interval", everyMs };
|
||||
}
|
||||
const selectedEveryDay = draft.weekdays.size === 0 || draft.weekdays.size === 7;
|
||||
return {
|
||||
type: "fixed_time",
|
||||
timeOfDay: draft.timeOfDay || "09:00",
|
||||
...(selectedEveryDay ? {} : { weekdays: [...draft.weekdays].toSorted() }),
|
||||
};
|
||||
}
|
||||
|
||||
export function taskToDraft(task: ScheduledTask): DraftState {
|
||||
@@ -84,7 +128,8 @@ export function taskToDraft(task: ScheduledTask): DraftState {
|
||||
title: task.title,
|
||||
prompt: task.prompt,
|
||||
enabled: task.enabled,
|
||||
scheduleMode: schedule.type === "interval" ? "interval" : "fixed",
|
||||
scheduleMode:
|
||||
schedule.type === "interval" ? "interval" : schedule.type === "webhook" ? "webhook" : "fixed",
|
||||
intervalMinutes:
|
||||
schedule.type === "interval" ? String(Math.max(1, schedule.everyMs / 60_000)) : "15",
|
||||
timeOfDay: schedule.type === "fixed_time" ? schedule.timeOfDay : "09:00",
|
||||
@@ -105,6 +150,15 @@ export function taskToDraft(task: ScheduledTask): DraftState {
|
||||
runtimeMode: task.runtimeMode,
|
||||
interactionMode: task.interactionMode,
|
||||
baseModelSelection: task.modelSelection,
|
||||
...(schedule.type === "webhook" && schedule.signature !== null
|
||||
? {
|
||||
signatureEnabled: true,
|
||||
signatureHeader: schedule.signature.header,
|
||||
signatureEncoding: schedule.signature.encoding,
|
||||
signaturePrefix: schedule.signature.prefix,
|
||||
}
|
||||
: { signatureEnabled: false, ...WEBHOOK_SIGNATURE_DEFAULTS }),
|
||||
signatureSecret: "",
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,31 @@ environment's time zone, which may differ from your phone's.
|
||||
You can edit, pause, resume, run immediately, or delete a task from the list.
|
||||
Leaving an edited form asks before discarding unsaved changes.
|
||||
|
||||
## Webhook automations
|
||||
|
||||
In **Settings → Scheduled tasks**, choose **On webhook** as a task's schedule to run it whenever another service
|
||||
calls its URL, such as GitHub on a new pull request or a CI job that failed.
|
||||
After you save the task, copy its URL from the editor. If the environment uses a
|
||||
[T3 Connect](remote-access.md) managed tunnel, the URL is public; otherwise it
|
||||
works anywhere the environment itself is reachable. **Rotate** replaces the URL and
|
||||
the old one stops working.
|
||||
|
||||
The prompt decides what the agent sees. Placeholders pull values out of the
|
||||
request: `{{body.path}}` for a JSON or form field, `{{headers.name}}`,
|
||||
`{{query.name}}`, `{{body}}` for the raw body, and `{{request}}` for everything.
|
||||
For example, `Review this PR: {{body.pull_request.html_url}}` sends only the
|
||||
pull request link. A placeholder with no value is left empty.
|
||||
|
||||
For GitHub, turn on **Require signature**, keep the header
|
||||
`x-hub-signature-256`, hex encoding and the `sha256=` prefix, and enter the
|
||||
same secret in the repository's webhook settings with content type
|
||||
`application/json`. Requests without a valid signature are rejected.
|
||||
|
||||
On desktop and web, pick **Deliveries** from a task's menu to see recent
|
||||
requests and the prompt each one produced. If the environment is offline, the sender gets an error and
|
||||
nothing runs; redeliver from the sender, such as GitHub's **Recent Deliveries**,
|
||||
once it is back.
|
||||
|
||||
## Defaults and inheritance
|
||||
|
||||
General contains the model and workspace for new threads. Integrations controls agent browser
|
||||
|
||||
@@ -1076,6 +1076,14 @@ export function createServerEnvironmentAtoms<R, E>(
|
||||
label: "environment-data:server:process-resource-history",
|
||||
tag: WS_METHODS.serverGetProcessResourceHistory,
|
||||
}),
|
||||
scheduledTaskWebhookDeliveries: createEnvironmentRpcQueryAtomFamily(runtime, {
|
||||
label: "environment-data:server:scheduled-task:webhook-deliveries",
|
||||
tag: WS_METHODS.scheduledTasksListWebhookDeliveries,
|
||||
}),
|
||||
scheduledTaskWebhookDelivery: createEnvironmentRpcQueryAtomFamily(runtime, {
|
||||
label: "environment-data:server:scheduled-task:webhook-delivery",
|
||||
tag: WS_METHODS.scheduledTasksGetWebhookDelivery,
|
||||
}),
|
||||
/** Live scheduled-task list: snapshot on subscribe, fresh list after every server-side change. */
|
||||
scheduledTasksLive: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
|
||||
label: "environment-data:server:scheduled-tasks:live",
|
||||
@@ -1291,14 +1299,6 @@ export function createServerEnvironmentAtoms<R, E>(
|
||||
scheduler: configScheduler,
|
||||
concurrency: configConcurrency,
|
||||
}),
|
||||
listScheduledTaskWebhookDeliveries: createEnvironmentRpcCommand(runtime, {
|
||||
label: "environment-data:server:scheduled-task:list-webhook-deliveries",
|
||||
tag: WS_METHODS.scheduledTasksListWebhookDeliveries,
|
||||
}),
|
||||
getScheduledTaskWebhookDelivery: createEnvironmentRpcCommand(runtime, {
|
||||
label: "environment-data:server:scheduled-task:get-webhook-delivery",
|
||||
tag: WS_METHODS.scheduledTasksGetWebhookDelivery,
|
||||
}),
|
||||
refreshUsageRates: createEnvironmentRpcCommand(runtime, {
|
||||
label: "environment-data:server:refresh-usage-rates",
|
||||
tag: WS_METHODS.serverRefreshUsageRates,
|
||||
|
||||
Reference in new issue
Block a user