All files / src/apps/user/pages/swarms/run FollowUpForm.tsx

100% Statements 19/19
88.88% Branches 8/9
100% Functions 6/6
100% Lines 19/19

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                                      2x 18x 18x 18x 18x   18x 21x   18x   18x 2x 2x   2x 1x 1x   1x 1x 1x       18x                                       2x                
import { SectionCard } from "@/components/detail";
import type { SwarmFollowUp, SwarmRefusal, SwarmRun } from "@/objects/Swarm";
import { swarmRunHref } from "@/routes/resourceHref";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { CornerDownRight } from "lucide-react";
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import RequestForm from "../RequestForm";
import { threadPlace } from "../swarmDisplay";
 
/**
 * Follow up a finished run: the swarm, as it is now, sent on the next
 * request of the run's thread. Its team reads every earlier request, the
 * previous answer, and the earlier pieces its lead says a new piece builds
 * on — so sequential work builds on itself rather than starting over. It
 * starts from this run's budget and time.
 */
const FollowUpForm: React.FC<{ run: SwarmRun }> = ({ run }) => {
  const { getApiHandler } = useApiHandler();
  const navigate = useNavigate();
  const queryClient = useQueryClient();
  const [refusal, setRefusal] = useState<SwarmRefusal | null>(null);
  // A shape that plans (or routes) may wait for the plan's approval.
  const plans = run.tasks.some(
    (t) => (t.kind === "plan" || t.kind === "route") && (t.depth ?? 0) === 0,
  );
  const next = threadPlace(run).seq + 1;
 
  const send = useMutation({
    mutationFn: (body: SwarmFollowUp) => getApiHandler().swarm_router.followUp(run.id, body),
    onMutate: () => setRefusal(null),
    onSuccess: (result) => {
      if ("refused" in result) {
        setRefusal(result.refused);
        return;
      }
      void queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET.SWARM_THREADS] });
      void queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET.SWARM_RUN, run.id] });
      void navigate(swarmRunHref(result.run.id));
    },
  });
 
  return (
    <SectionCard
      icon={CornerDownRight}
      title="Follow up"
      subtitle={`request ${next} of this thread`}
      className="mb-4"
    >
      <RequestForm
        defaults={{
          budget_usd: run.budget_usd,
          time_cap_s: run.time_cap_s,
          require_approval: run.config.defaults?.require_approval ?? true,
        }}
        plans={plans}
        label="Your follow-up"
        placeholder="What next? Ask for more on part of the answer, a change to it, or the next step that builds on it."
        sendLabel="Follow up"
        carried={run.file_ids.length}
        pending={send.isPending}
        refusal={refusal}
        onSend={(body) => send.mutate(body)}
        note="The team reads every earlier request, the answer above, and the earlier pieces its lead says the new work builds on. It runs the swarm as it is now."
      />
    </SectionCard>
  );
};
 
export default FollowUpForm;