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 | 2x 2x 2x 1x 2x 1x 1x 1x 1x 2x 7x 7x 1x 2x 29x 29x 3x 6x | import RichMarkdown from "@/components/chat/RichMarkdown/RichMarkdown";
import { formatUsd } from "@/components/cost";
import { SWARM_RUN_STATUS, SectionCard, StatusBadge } from "@/components/detail";
import { DocumentDownloadProvider } from "@/components/document";
import {
hasAnswered,
type SwarmRun,
type SwarmThreadStep,
} from "@/objects/Swarm";
import { swarmRunHref } from "@/routes/resourceHref";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { parseServerDate, timeAgo } from "@/util/time";
import { useQuery } from "@tanstack/react-query";
import { ChevronDown, Loader2, MessagesSquare } from "lucide-react";
import React, { useState } from "react";
import { Link } from "react-router-dom";
import { taskExcerpt, threadPlace } from "../swarmDisplay";
/**
* A run's thread โ the request it follows up, and any after it โ on the
* run's page: every request in order, this one marked, each other one
* opening in place to what it asked and what it answered, or as its own
* page. How a person moves back and forth through sequential work.
*/
/** An earlier (or later) request, read in place: what it asked and what it
* answered โ the run read as its own page reads it, so the two share a
* cache. */
const RequestRead: React.FC<{ step: SwarmThreadStep }> = ({ step }) => {
const { getApiHandler } = useApiHandler();
const { data: run, isLoading } = useQuery<SwarmRun | null>({
queryKey: [QUERY_KEYS.GET.SWARM_RUN, step.id],
queryFn: () => getApiHandler().swarm_router.getRun(step.id),
});
if (isLoading) {
return (
<div className="flex justify-center py-4">
<Loader2 className="size-4 animate-spin text-gray-400" />
</div>
);
}
Iif (!run) {
return <p className="px-4 pb-3 text-xs italic text-gray-400">It could not be read.</p>;
}
const answered = hasAnswered(run);
return (
<div className="space-y-3 px-4 pb-4 pl-12">
<div>
<div className="mb-1 text-[10px] font-medium uppercase tracking-wide text-gray-400">
Asked
</div>
<p className="whitespace-pre-wrap text-sm text-gray-800 dark:text-gray-200">{run.task}</p>
</div>
<div>
<div className="mb-1 text-[10px] font-medium uppercase tracking-wide text-gray-400">
Answered
</div>
{run.output ? (
<div className="max-h-96 overflow-y-auto rounded-lg border border-gray-100 dark:border-gray-700 p-3">
<DocumentDownloadProvider
download={
answered
? (index, document) =>
getApiHandler().swarm_router.downloadRunDocument(
run.id,
index,
`${document.title}.pdf`,
)
: null
}
imageUrl={
answered
? (asset) => getApiHandler().swarm_router.runImageUrl(run.id, asset)
: null
}
>
<RichMarkdown content={run.output} className="text-sm" />
</DocumentDownloadProvider>
</div>
) : (
<p className="text-xs italic text-gray-500 dark:text-gray-400">
{run.error ?? "No answer yet."}
</p>
)}
</div>
<Link
to={swarmRunHref(run.id)}
className="inline-block text-xs text-gray-600 underline hover:text-gray-900 dark:text-gray-300 dark:hover:text-gray-100"
>
Open request {step.seq}
</Link>
</div>
);
};
const ThreadEntry: React.FC<{ step: SwarmThreadStep; current: boolean }> = ({
step,
current,
}) => {
const [open, setOpen] = useState(false);
return (
<li
aria-current={current ? "page" : undefined}
className={current ? "bg-gray-50 dark:bg-gray-700/30" : undefined}
>
<div className="flex items-center gap-3 px-4 py-2.5">
<span
className={
current
? "flex size-6 shrink-0 items-center justify-center rounded-full bg-gray-900 text-[11px] font-medium text-white dark:bg-white dark:text-gray-900"
: "flex size-6 shrink-0 items-center justify-center rounded-full bg-gray-100 text-[11px] font-medium text-gray-600 dark:bg-gray-700 dark:text-gray-300"
}
>
{step.seq}
</span>
<span className="min-w-0 flex-1 truncate text-sm text-gray-800 dark:text-gray-100">
{taskExcerpt(step.task, 110)}
</span>
<span className="hidden shrink-0 text-xs text-gray-400 sm:inline">
{timeAgo(parseServerDate(step.created_at))} ยท {formatUsd(step.spent_usd)}
</span>
<StatusBadge status={step.status} scale={SWARM_RUN_STATUS} size="xs" />
{current ? (
<span className="w-16 shrink-0 text-right text-[10px] font-medium text-gray-500">
This request
</span>
) : (
<button
type="button"
onClick={() => setOpen((v) => !v)}
aria-expanded={open}
aria-label={`${open ? "Hide" : "Read"} request ${step.seq}`}
className="flex w-16 shrink-0 items-center justify-end gap-0.5 text-xs text-gray-500 hover:text-gray-800 dark:hover:text-gray-200"
>
{open ? "Hide" : "Read"}
<ChevronDown className={open ? "size-3.5 rotate-180" : "size-3.5"} />
</button>
)}
</div>
{open && !current && <RequestRead step={step} />}
</li>
);
};
const ThreadPanel: React.FC<{ run: SwarmRun }> = ({ run }) => {
const place = threadPlace(run);
if (place.count < 2) return null;
return (
<SectionCard
icon={MessagesSquare}
title="This thread"
subtitle={`request ${place.seq} of ${place.count}, each building on the last`}
className="mb-4"
bodyClassName="p-0"
>
<ol
aria-label="Requests in this thread"
className="divide-y divide-gray-100 dark:divide-gray-700"
>
{place.steps.map((step) => (
<ThreadEntry key={step.id} step={step} current={step.id === run.id} />
))}
</ol>
</SectionCard>
);
};
export default ThreadPanel;
|