fix(goal): make cancellation a user-only control (#6667)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-14 23:48:13 +08:00
1 parent c927808aff
commit 12a86eaa86
33 files changed
+641 -63

No files matched your search

+37 -5
View File
@@ -3,15 +3,22 @@ import { useLayoutEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { MarkdownText, preloadMarkdownText } from "@/components/MarkdownText";
import { Button } from "@/components/ui/button";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { useOverlayEscapeFocus } from "@/hooks/useOverlayEscapeFocus";
import type { GoalStateWsPayload } from "@/lib/types";
import "./goal-island.css";
export function GoalIsland({ goal }: { goal: GoalStateWsPayload }) {
export function GoalIsland({ goal, cancelDisabled, onCancel }: {
goal: GoalStateWsPayload;
cancelDisabled: boolean;
onCancel: () => Promise<void>;
}) {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const [cancelling, setCancelling] = useState(false);
const [cancelFailed, setCancelFailed] = useState(false);
const [scrollBody, setScrollBody] = useState<HTMLDivElement | null>(null);
const panelRef = useRef<HTMLDivElement>(null);
const escapeFocus = useOverlayEscapeFocus();
@@ -36,10 +43,30 @@ export function GoalIsland({ goal }: { goal: GoalStateWsPayload }) {
const status = goal.status === "blocked" ? "blocked" : "active";
const recap = goal.recap?.trim() ?? "";
const collapsedLabel = status === "blocked" ? recap : label;
const cancelButton = <Button
type="button"
variant="ghost"
size="sm"
className="ml-auto h-7 shrink-0 px-2 text-caption text-muted-foreground"
disabled={cancelDisabled || cancelling}
aria-busy={cancelling}
title={cancelDisabled ? t("thread.composer.cancelGoalOffline") : undefined}
onClick={async () => {
setCancelling(true);
setCancelFailed(false);
try {
await onCancel();
} catch {
setCancelFailed(true);
} finally {
setCancelling(false);
}
}}
>{t("thread.composer.cancelGoal")}</Button>;
useLayoutEffect(() => {
if (!scrollBody) return;
if (status === "blocked") scrollBody.scrollTop = 0;
if (status === "blocked" || cancelFailed) scrollBody.scrollTop = 0;
const measure = () => {
scrollBody.toggleAttribute("data-overflow-top", scrollBody.scrollTop > 1);
scrollBody.toggleAttribute("data-overflow-bottom", scrollBody.scrollHeight - scrollBody.clientHeight - scrollBody.scrollTop > 1);
@@ -53,7 +80,7 @@ export function GoalIsland({ goal }: { goal: GoalStateWsPayload }) {
observer.disconnect();
scrollBody.removeEventListener("scroll", measure);
};
}, [scrollBody, status]);
}, [scrollBody, status, cancelFailed]);
return (
<div className="goal-island-position">
@@ -87,13 +114,18 @@ export function GoalIsland({ goal }: { goal: GoalStateWsPayload }) {
>
<div className="goal-island-surface" aria-hidden />
<div className="goal-island-panel-content">
{status !== "blocked" && summary ? <div className="goal-island-summary markdown-content"><h2>{summary}</h2></div> : null}
{status !== "blocked" ? <div className="goal-island-summary flex items-start gap-3">
{summary ? <div className="markdown-content min-w-0 flex-1"><h2>{summary}</h2></div> : null}
{cancelButton}
</div> : null}
<div ref={setScrollBody} className="goal-island-body">
<div className="goal-island-document">
{cancelFailed ? <p role="alert" className="mb-2 text-caption text-destructive">{t("thread.composer.cancelGoalFailed")}</p> : null}
{status === "blocked" ? <div className="goal-island-blocked">
<CircleAlert className="goal-island-status-icon" strokeWidth={2.5} aria-hidden="true" />
<span className="sr-only">{t("thread.composer.goalBlocked")}</span>
{recap ? <MarkdownText className="goal-island-reason">{recap}</MarkdownText> : null}
{recap ? <MarkdownText className="goal-island-reason flex-1">{recap}</MarkdownText> : null}
{cancelButton}
</div> : null}
{objective ? <MarkdownText className={status === "blocked" ? "mt-4" : undefined}>{objective}</MarkdownText> : null}
{status !== "blocked" && recap ? <MarkdownText className="mt-4 text-muted-foreground">{recap}</MarkdownText> : null}
+17 -2
View File
@@ -426,6 +426,18 @@ export function ThreadShell({
[historyKey, sessions, titleOverrides],
);
const { client, getToken, ingressLimits, modelName, token, webuiCapabilities } = useClient();
const [online, setOnline] = useState(client.status === "open");
useEffect(() => client.onStatus((status) => setOnline(status === "open")), [client]);
const cancelGoal = useCallback(async () => {
if (!chatId || client.status !== "open") throw new Error(t("thread.composer.cancelGoalOffline"));
await client.sendSystemCommand(chatId, "/cancel_goal").catch(() => {
throw new Error(t("thread.composer.cancelGoalFailed"));
});
const remainingGoal = client.getGoalState(chatId);
if (remainingGoal?.active || remainingGoal?.status === "blocked") {
throw new Error(t("thread.composer.cancelGoalFailed"));
}
}, [chatId, client, t]);
const favoriteWorkspaceProject = useCallback(
async (path: string, pinned: boolean) => {
const response = await client.requestMutation<{ favorite_projects: ProjectDirectory[] }>("workspace.favorite", { path, pinned });
@@ -1060,6 +1072,9 @@ export function ThreadShell({
const handleThreadSend = useCallback(
(content: string, images?: SendAttachment[], options?: SendOptions) => {
if (content.trim() === "/cancel_goal" && options?.sideChannel) {
return cancelGoal().then(() => true);
}
setRestoredFirstDraft((current) => current?.chatId === chatId ? { ...current, error: "" } : current);
const submitted = send(content, images, withWorkspaceScope(options));
if (
@@ -1073,7 +1088,7 @@ export function ThreadShell({
}
return submitted?.delivery ? submitted.delivery.then(() => true) : submitted !== null;
},
[chatId, send, withWorkspaceScope],
[cancelGoal, chatId, send, withWorkspaceScope],
);
const loadTemporaryFilePreview = useCallback((path: string) =>
@@ -1429,7 +1444,7 @@ export function ThreadShell({
className={previewOpen ? "h-10" : undefined}
title={title}
goal={currentGoalState && (currentGoalState.active || currentGoalState.status === "blocked")
? <GoalIsland key={historyKey} goal={currentGoalState} /> : undefined}
? <GoalIsland key={historyKey} goal={currentGoalState} cancelDisabled={!online} onCancel={cancelGoal} /> : undefined}
onToggleSidebar={onToggleSidebar}
theme={theme}
onToggleTheme={onToggleTheme}