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 | 1x | import ClarifyForm, {
ClarifyAnswers,
} from "@/components/chat/ClarifyForm/ClarifyForm";
import RichMarkdown from "@/components/chat/RichMarkdown/RichMarkdown";
import { SkillRun, SkillRunRefusal } from "@/objects/Skill";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { parseServerDate, timeAgo } from "@/util/time";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { Check, Hand, Loader2, MessageCircleQuestion, X } from "lucide-react";
import React, { useState } from "react";
/**
* A paused run, waiting for the person who started it.
*
* Two kinds of pause, one panel. A node that asked questions shows them as
* the same form the chat uses; an approval gate shows the work so far and
* the author's note, and takes an approve / reject with optional feedback —
* feedback on an approval reaches the next node, feedback on a rejection is
* kept as the reason. Either way the run carries on in the background.
*/
const RunPausePanel: React.FC<{ skillId: string; run: SkillRun }> = ({
skillId,
run,
}) => {
const { getApiHandler } = useApiHandler();
const queryClient = useQueryClient();
const [feedback, setFeedback] = useState("");
const [refusal, setRefusal] = useState<SkillRunRefusal | null>(null);
const pause = run.interrupt;
const refresh = () => {
void queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.SKILL_RUN, skillId, run.id],
});
void queryClient.invalidateQueries({
queryKey: [QUERY_KEYS.GET.SKILL_RUNS, skillId],
});
};
const respond = useMutation({
mutationFn: (body: {
approved?: boolean;
feedback?: string;
answers?: Record<string, string[]>;
}) => getApiHandler().skill_router.respond(skillId, run.id, body),
onMutate: () => setRefusal(null),
onSuccess: (result) => {
if ("refused" in result) setRefusal(result.refused);
else refresh();
},
});
const cancel = useMutation({
mutationFn: () => getApiHandler().skill_router.cancelRun(skillId, run.id),
onSuccess: refresh,
});
const busy = respond.isPending || cancel.isPending;
if (!pause) return null;
return (
<div 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 justify-between gap-3">
<div className="flex items-start gap-2">
{pause.kind === "clarification" ? (
<MessageCircleQuestion className="size-4 mt-0.5 text-amber-600 dark:text-amber-400 shrink-0" />
) : (
<Hand 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">
{pause.kind === "clarification"
? "The skill has questions before it continues"
: `Approve before “${pause.node_id ?? "the next step"}” runs`}
</p>
<p className="text-[11px] text-amber-800/80 dark:text-amber-200/70">
Paused {timeAgo(parseServerDate(pause.paused_at))}. It waits up
to 24 hours, then is cancelled.
</p>
</div>
</div>
<button
type="button"
disabled={busy}
onClick={() => cancel.mutate()}
className="shrink-0 text-xs px-2.5 py-1 rounded-lg border border-amber-300 dark:border-amber-700 text-amber-800 dark:text-amber-200 hover:bg-amber-100 dark:hover:bg-amber-900/40 disabled:opacity-50"
>
Cancel run
</button>
</div>
{pause.kind === "clarification" ? (
<ClarifyForm
request={{
thread_id: pause.thread_id ?? "",
intro: pause.intro,
questions: pause.questions,
}}
disabled={busy}
onSubmit={(answers: ClarifyAnswers) => respond.mutate({ answers })}
onSkip={() => respond.mutate({ answers: {} })}
/>
) : (
<div className="space-y-3">
{pause.message && (
<p className="text-sm text-gray-800 dark:text-gray-200">
{pause.message}
</p>
)}
{Object.entries(pause.completed ?? {}).map(([node, text]) => (
<details
key={node}
open
className="rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800"
>
<summary className="px-3 py-2 text-xs font-mono font-semibold text-gray-700 dark:text-gray-200 cursor-pointer">
{node}
</summary>
<div className="px-3 pb-3 max-h-72 overflow-y-auto">
<RichMarkdown content={text || "_No output._"} className="text-sm" />
</div>
</details>
))}
<label className="block">
<span className="text-xs font-medium text-gray-700 dark:text-gray-300">
Feedback{" "}
<span className="font-normal text-gray-400">
optional — passed to the next step if you approve
</span>
</span>
<textarea
value={feedback}
onChange={(e) => setFeedback(e.target.value)}
rows={2}
className="mt-1 w-full text-sm px-3 py-2 rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-800 dark:text-gray-200 resize-y focus:outline-none focus:ring-1 focus:ring-violet-300"
/>
</label>
<div className="flex flex-wrap justify-end gap-2">
<button
type="button"
disabled={busy}
onClick={() => respond.mutate({ approved: false, feedback })}
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" />
Reject and stop
</button>
<button
type="button"
disabled={busy}
onClick={() => respond.mutate({ approved: true, feedback })}
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"
>
{respond.isPending ? (
<Loader2 className="size-3.5 animate-spin" />
) : (
<Check className="size-3.5" />
)}
Approve and continue
</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>
)}
</div>
);
};
export default RunPausePanel;
|