Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | 2x 2x 9x 9x 9x 9x 9x 9x 9x 9x 9x 2x 9x 2x 2x 9x 1x 1x 1x 9x 1x 1x 9x 9x 9x 89x 9x 9x 1x 1x 1x 1x 1x 9x 12x 12x 1x 1x 1x 1x 1x 1x 37x 24x 1x 1x | import { formatUsd } from "@/components/cost";
import type { SwarmRefusal, SwarmRun } from "@/objects/Swarm";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { Check, ClipboardList, Loader2, X } from "lucide-react";
import React, { useState } from "react";
/**
* The lead's plan, waiting for the person who sent the swarm: each piece, who
* does it and what it is for, and what the plan is expected to cost against
* the budget. Approve as it stands, or with pieces dropped, a piece given to
* another member, or the budget changed; or decline it, and nothing more is
* spent.
*/
const inputCls =
"rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 px-2 py-1 text-xs text-gray-800 dark:text-gray-200 focus:outline-none focus:ring-1 focus:ring-gray-300";
const PlanApproval: React.FC<{ run: SwarmRun }> = ({ run }) => {
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const plan = run.plan;
const members = run.config.roster ?? [];
const [dropped, setDropped] = useState<Set<string>>(new Set());
const [assigned, setAssigned] = useState<Record<string, string>>({});
const [budget, setBudget] = useState<string>(String(run.budget_usd));
const [refusal, setRefusal] = useState<SwarmRefusal | null>(null);
const refresh = () =>
void queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET.SWARM_RUN, run.id] });
const onResult = (result: { run: SwarmRun } | { refused: SwarmRefusal }) => {
Iif ("refused" in result) setRefusal(result.refused);
else refresh();
};
const approve = useMutation({
mutationFn: () => {
const amount = Number(budget);
return getApiHandler().swarm_router.approve(run.id, {
drop: [...dropped],
assign: assigned,
...(Number.isFinite(amount) && amount !== run.budget_usd ? { budget_usd: amount } : {}),
});
},
onMutate: () => setRefusal(null),
onSuccess: onResult,
});
const reject = useMutation({
mutationFn: () => getApiHandler().swarm_router.reject(run.id),
onMutate: () => setRefusal(null),
onSuccess: onResult,
});
const busy = approve.isPending || reject.isPending;
Iif (!plan) return null;
const pieces = plan.tasks ?? [];
// A piece goes only to a member of the role it was planned for: a part
// to a manager, a piece to a worker (svc-core refuses any other).
const roleOf = (handle: string) => members.find((m) => m.handle === handle)?.role;
const kept = pieces.length - dropped.size;
const toggle = (handle: string) =>
setDropped((current) => {
const next = new Set(current);
Iif (next.has(handle)) next.delete(handle);
else next.add(handle);
return next;
});
return (
<section
aria-label="The lead's plan"
className="mb-4 rounded-xl border border-amber-200 dark:border-amber-800 bg-amber-50/60 dark:bg-amber-950/20 p-4 space-y-3"
>
<div className="flex items-start gap-2">
<ClipboardList className="size-4 mt-0.5 text-amber-600 dark:text-amber-400 shrink-0" />
<div>
<p className="text-sm font-semibold text-amber-900 dark:text-amber-100">
The lead's plan is ready for you
</p>
<p className="text-[11px] text-amber-800/80 dark:text-amber-200/70">
{plan.approach === "one_agent"
? "One member does it all, as one piece."
: `${pieces.length} piece${pieces.length === 1 ? "" : "s"}, done in parallel where they can be, then reviewed.`}
{plan.estimate_usd != null &&
` Expected to cost about ${formatUsd(plan.estimate_usd)} of ${formatUsd(run.budget_usd)}.`}
</p>
</div>
</div>
<ol className="space-y-2">
{pieces.map((piece) => {
const off = dropped.has(piece.handle);
return (
<li
key={piece.handle}
className={`rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 p-3 ${
off ? "opacity-50" : ""
}`}
>
<div className="flex flex-wrap items-center gap-2">
<label className="flex items-center gap-2 text-sm font-medium text-gray-800 dark:text-gray-100">
<input
type="checkbox"
checked={!off}
disabled={busy}
onChange={() => toggle(piece.handle)}
aria-label={`Keep ${piece.handle} ${piece.title}`}
/>
<span className="font-mono text-xs text-gray-400">{piece.handle}</span>
{piece.title}
</label>
<span className="ml-auto flex items-center gap-1.5 text-xs text-gray-500 dark:text-gray-400">
by
<select
className={inputCls}
value={assigned[piece.handle] ?? piece.member}
disabled={busy || off}
aria-label={`Who does ${piece.handle}`}
onChange={(e) =>
setAssigned((current) => {
const next = { ...current };
Iif (e.target.value === piece.member) delete next[piece.handle];
else next[piece.handle] = e.target.value;
return next;
})
}
>
{members
.filter((m) => m.role === roleOf(piece.member))
.map((m) => (
<option key={m.handle} value={m.handle}>
{m.agent_name}
</option>
))}
</select>
</span>
</div>
{piece.specialism && (
<p className="mt-1 text-xs text-gray-500 dark:text-gray-400">
As {piece.specialism}
</p>
)}
{piece.objective && (
<p className="mt-1 text-xs text-gray-700 dark:text-gray-300">{piece.objective}</p>
)}
{piece.done_when && (
<p className="mt-0.5 text-[11px] text-gray-400 dark:text-gray-500">
Done when: {piece.done_when}
</p>
)}
{(piece.builds_on?.length ?? 0) > 0 && (
<p className="mt-0.5 text-[11px] text-gray-500 dark:text-gray-400">
Builds on{" "}
{piece.builds_on
?.map((ref) => {
const at = ref.indexOf(".");
return `${ref.slice(at + 1)} of request ${ref.slice(0, at)}`;
})
.join(", ")}
</p>
)}
{piece.after.length > 0 && (
<p className="mt-0.5 text-[11px] text-gray-400 dark:text-gray-500">
Needs {piece.after.join(", ")} first
</p>
)}
</li>
);
})}
</ol>
{(plan.assumptions ?? []).length > 0 && (
<div className="text-xs text-gray-600 dark:text-gray-300">
<div className="font-medium mb-0.5">The plan assumes</div>
<ul className="list-disc pl-5 space-y-0.5">
{plan.assumptions!.map((a) => (
<li key={a}>{a}</li>
))}
</ul>
</div>
)}
<div className="flex flex-wrap items-center gap-2">
<label className="flex items-center gap-1.5 text-xs text-gray-600 dark:text-gray-300">
Budget $
<input
type="number"
min={0.05}
step={0.05}
className={`${inputCls} w-24`}
value={budget}
disabled={busy}
onChange={(e) => setBudget(e.target.value)}
aria-label="Budget in dollars"
/>
</label>
<div className="ml-auto flex gap-2">
<button
type="button"
disabled={busy}
onClick={() => reject.mutate()}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm rounded-lg border border-red-200 dark:border-red-800 text-red-600 dark:text-red-400 hover:bg-red-50 dark:hover:bg-red-950/30 disabled:opacity-50"
>
<X className="size-3.5" />
Decline
</button>
<button
type="button"
disabled={busy || kept === 0}
onClick={() => approve.mutate()}
className="inline-flex items-center gap-1.5 px-3 py-1.5 text-sm font-medium rounded-lg bg-gray-900 dark:bg-white text-white dark:text-gray-900 hover:opacity-80 disabled:opacity-50"
>
{approve.isPending ? (
<Loader2 className="size-3.5 animate-spin" />
) : (
<Check className="size-3.5" />
)}
Approve and start
</button>
</div>
</div>
{refusal && (
<div className="text-xs px-3 py-2 rounded-lg bg-red-50 dark:bg-red-950/40 border border-red-200 dark:border-red-800 text-red-700 dark:text-red-300">
{refusal.message}
{Object.entries(refusal.fields).map(([field, reason]) => (
<div key={field}>
<span className="font-mono">{field}</span>: {reason}
</div>
))}
</div>
)}
</section>
);
};
export default PlanApproval;
|