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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 1x 2x 3x 2x 2x 9x 37x 9x 9x 9x 9x 9x 9x 9x 63x 63x 9x 9x 15x 1x | import ClarifyForm, {
ClarifyAnswers,
} from "@/components/chat/ClarifyForm/ClarifyForm";
import RichMarkdown from "@/components/chat/RichMarkdown/RichMarkdown";
import { describeCost, formatUsd } from "@/components/cost";
import {
MetaGrid,
MetaItem,
SWARM_TASK_STATUS,
SectionCard,
StatusBadge,
} from "@/components/detail";
import type { SwarmRefusal, SwarmRun, SwarmTask } from "@/objects/Swarm";
import { decisionHref, swarmRunHref } from "@/routes/resourceHref";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { AlertTriangle, MessageCircleQuestion, ScanSearch } from "lucide-react";
import React, { useState } from "react";
import { Link } from "react-router-dom";
import {
askedBy,
briefUsd,
buildsOn,
memberOf,
roundOf,
taskKindLabel,
taskTook,
threadPlace,
} from "../swarmDisplay";
/**
* One task of a run: what it was asked, who did it and how, where it came
* from (its round, the review that asked for it, the piece it redoes), what
* it wrote, what it cost and where its money came from and went — and, for a
* piece whose agent asked something, the questions to answer. Its record is
* a link away ("Why did it do this?").
*/
const BRIEF_FIELDS: [keyof SwarmTask["brief"], string][] = [
["specialism", "As"],
["objective", "What it is for"],
["output_format", "Hands back"],
["sources", "Where to look"],
["boundaries", "Not its business"],
["done_when", "Done when"],
["review_note", "Sent back because"],
];
const QuestionsForm: React.FC<{ run: SwarmRun; task: SwarmTask }> = ({ run, task }) => {
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const [refusal, setRefusal] = useState<SwarmRefusal | null>(null);
const respond = useMutation({
mutationFn: (answers: Record<string, string[]>) =>
getApiHandler().swarm_router.respond(run.id, task.id, { answers }),
onMutate: () => setRefusal(null),
onSuccess: (result) => {
Iif ("refused" in result) setRefusal(result.refused);
else
void queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.SWARM_RUN, run.id],
});
},
});
const pause = task.questions;
Iif (!pause) return null;
return (
<div className="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">
<MessageCircleQuestion 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">
{task.agent_name} has questions before it does this piece
</p>
<p className="text-[11px] text-amber-800/80 dark:text-amber-200/70">
The rest of the team carries on. Answer, and the piece runs again
with your answers; skip, and it goes on its own judgement.
</p>
</div>
</div>
<ClarifyForm
request={{ thread_id: task.id, questions: pause.questions }}
disabled={respond.isPending}
onSubmit={(answers: ClarifyAnswers) => respond.mutate(answers)}
onSkip={() => respond.mutate({})}
/>
{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>
)}
</div>
);
};
/** A task this one came from, opened in place when the page can. */
const Related: React.FC<{
label: string;
task: SwarmTask;
onSelect?: (id: string) => void;
}> = ({ label, task, onSelect }) => (
<span className="flex items-center gap-1.5">
<span className="text-gray-400 dark:text-gray-500">{label}</span>
{onSelect ? (
<button
type="button"
onClick={() => onSelect(task.id)}
className="rounded-md border border-gray-200 dark:border-gray-600 px-2 py-0.5 text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-700"
>
<span className="font-mono">{task.handle}</span> {task.title}
</button>
) : (
<span className="text-gray-700 dark:text-gray-200">
<span className="font-mono">{task.handle}</span> {task.title}
</span>
)}
</span>
);
export interface TaskPanelProps {
run: SwarmRun;
task: SwarmTask;
/** Opens another task of the run (the review that asked for this one, the
* piece it redoes) in place. */
onSelect?: (id: string) => void;
}
const TaskPanel: React.FC<TaskPanelProps> = ({ run, task, onSelect }) => {
const member = memberOf(run, task);
const byId = new Map(run.tasks.map((t) => [t.id, t]));
const round = roundOf(task, byId);
const asker = askedBy(task, run.tasks);
const original = task.brief?.redoes ? byId.get(task.brief.redoes) : undefined;
const given = briefUsd(task, "topped_up_usd");
const passed = briefUsd(task, "passed_on_usd");
const record = decisionHref(task.invocation_id);
const brief = BRIEF_FIELDS.filter(([key]) => {
const value = task.brief?.[key];
return typeof value === "string" && value.trim();
});
// The earlier pieces of its thread this piece carries further.
const earlier = buildsOn(task, threadPlace(run).steps);
return (
<SectionCard
title={`${task.handle} ${task.title}`}
subtitle={`${taskKindLabel(task)} · ${task.agent_name}${
member?.spawn ? " (briefed as a specialist)" : ""
}`}
action={
record ? (
<Link
to={record}
className="flex items-center gap-1.5 text-xs px-2.5 py-1.5 rounded-lg border border-gray-200 dark:border-gray-600 text-gray-600 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700"
>
<ScanSearch className="size-3" />
Why did it do this?
</Link>
) : undefined
}
className="mb-4"
>
<div className="space-y-4">
<MetaGrid columns={4}>
<MetaItem
label="Status"
value={<StatusBadge status={task.status} scale={SWARM_TASK_STATUS} />}
/>
<MetaItem
label="Spent"
value={
<span className="text-sm tabular-nums text-gray-800 dark:text-gray-200">
{task.llm_calls === 0
? "—"
: describeCost({ cost_usd: task.cost_usd, unpriced: task.unpriced_calls }).text}
{task.budget_usd != null && (
<span className="text-gray-400"> of {formatUsd(task.budget_usd)}</span>
)}
{given ? (
<span className="block text-xs text-gray-400">
{formatUsd(given)} of it from finished work
</span>
) : null}
{passed ? (
<span className="block text-xs text-gray-400">
{formatUsd(passed)} passed on to waiting work
</span>
) : null}
</span>
}
/>
<MetaItem
label="Model"
value={
<span className="text-sm text-gray-800 dark:text-gray-200">
{task.tier === "lighter" ? "A lighter model" : "Its own"}
{task.cost_profile ? ` · ${task.cost_profile}` : ""}
</span>
}
/>
<MetaItem
label="Took"
value={
<span className="text-sm text-gray-800 dark:text-gray-200">
{taskTook(task) ?? "—"}
{(task.attempts ?? 0) > 1 && (
<span className="text-gray-400"> · tried {task.attempts} times</span>
)}
</span>
}
/>
</MetaGrid>
{(round > 1 || asker || original) && (
<div className="flex flex-wrap items-center gap-x-4 gap-y-1.5 text-xs">
{round > 1 && (
<span className="text-gray-500 dark:text-gray-400">Round {round}</span>
)}
{asker && <Related label="Asked for by" task={asker} onSelect={onSelect} />}
{original && <Related label="Redo of" task={original} onSelect={onSelect} />}
</div>
)}
{task.status === "awaiting_input" && <QuestionsForm run={run} task={task} />}
{task.error && (
<div className="flex items-start gap-2 px-3 py-2 rounded-lg border border-amber-200 dark:border-amber-800 bg-amber-50 dark:bg-amber-950/30 text-xs text-amber-800 dark:text-amber-200">
<AlertTriangle className="size-3.5 shrink-0 mt-0.5" />
<span>{task.error}</span>
</div>
)}
{brief.length > 0 && (
<dl className="grid grid-cols-1 sm:grid-cols-[9rem_1fr] gap-x-3 gap-y-1.5 text-sm">
{brief.map(([key, label]) => (
<React.Fragment key={key}>
<dt className="text-xs text-gray-400 dark:text-gray-500">{label}</dt>
<dd className="text-gray-800 dark:text-gray-200">
{String(task.brief[key])}
</dd>
</React.Fragment>
))}
</dl>
)}
{earlier.length > 0 && (
<div className="flex flex-wrap items-center gap-1.5 text-xs">
<span className="text-gray-400 dark:text-gray-500">Builds on</span>
{earlier.map((piece) =>
piece.runId ? (
<Link
key={piece.ref}
to={swarmRunHref(piece.runId, piece.handle)}
className="rounded-md border border-gray-200 dark:border-gray-600 px-2 py-0.5 font-mono text-gray-700 dark:text-gray-200 hover:bg-gray-50 dark:hover:bg-gray-700"
title={`Request ${piece.seq}'s piece ${piece.handle}, given to this piece in full`}
>
{piece.handle} of request {piece.seq}
</Link>
) : (
<span key={piece.ref} className="font-mono text-gray-600 dark:text-gray-300">
{piece.ref}
</span>
),
)}
</div>
)}
{task.output ? (
<div className="max-h-96 overflow-y-auto rounded-lg border border-gray-100 dark:border-gray-700 p-3">
<RichMarkdown content={task.output} className="text-sm" />
</div>
) : task.result ? (
<pre className="max-h-72 overflow-y-auto text-[11px] font-mono whitespace-pre-wrap rounded-lg bg-gray-50 dark:bg-gray-900/50 p-3 text-gray-600 dark:text-gray-400">
{JSON.stringify(task.result, null, 2)}
</pre>
) : (
task.status !== "awaiting_input" && (
<p className="text-xs italic text-gray-400 dark:text-gray-500">
Nothing written yet.
</p>
)
)}
</div>
</SectionCard>
);
};
export default TaskPanel;
|