|
|
@@ -1,12 +1,13 @@
|
|
|
// ============================================================
|
|
|
-// TaskDispatchModule —— 任务分发独立模块(M2 骨架)
|
|
|
+// TaskDispatchModule —— 任务分发独立模块(M3)
|
|
|
// ============================================================
|
|
|
// 设计合同:docs/specs/workspace-task-dispatch.md §8(三次/四次对齐形态)。
|
|
|
// 四区布局:项目选择器 → 分析动作 → 按执行计划分组的任务清单 → 模块会话历史。
|
|
|
-// M2 骨架仅承载布局与状态框架;分析编排(M3)、确认执行(M4)、历史数据(M3)
|
|
|
-// 逐步填充。样式遵守 DESIGN.md(text-ui-* 字号体系)。
|
|
|
+// M3:分析会话编排(createSession moduleScope=task-dispatch → sessions-index
|
|
|
+// 终态监听 → rowsRange 解析计划);M4 接确认分发。样式遵守 DESIGN.md。
|
|
|
|
|
|
-import { useMemo, useState } from "react";
|
|
|
+import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
|
+import { AlertCircle, Loader2 } from "lucide-react";
|
|
|
import { Button } from "@/components/ui/button.js";
|
|
|
import {
|
|
|
Select,
|
|
|
@@ -20,7 +21,21 @@ import { cn } from "@/components/lib/utils.js";
|
|
|
import { useTabStore } from "@/store/TabStoreProvider.js";
|
|
|
import { isWorkspaceTab } from "@/store/tabStore.js";
|
|
|
import { useLocalWorkspaceScopes } from "@/hooks/useLocalWorkspaceScopes.js";
|
|
|
+import { useSessionsIndex } from "@/v4/useSessionsIndex.js";
|
|
|
import { getWorkspaceKey } from "@/lib/workspaceKey.js";
|
|
|
+import {
|
|
|
+ isTerminalSessionPhase,
|
|
|
+ useTaskDispatchAnalysis,
|
|
|
+ type TaskDispatchTarget,
|
|
|
+} from "@/task-dispatch/useTaskDispatchAnalysis.js";
|
|
|
+import {
|
|
|
+ taskDispatchHistoryStore,
|
|
|
+ type TaskDispatchAnalysisRecord,
|
|
|
+} from "@/task-dispatch/taskDispatchHistoryStore.js";
|
|
|
+import {
|
|
|
+ TaskDispatchPlanList,
|
|
|
+ type TaskDispatchDraft,
|
|
|
+} from "@/task-dispatch/TaskDispatchPlanList.js";
|
|
|
|
|
|
/** 模块内可选的项目目标(本地 + 远程 workspace)。 */
|
|
|
export interface TaskDispatchWorkspaceOption {
|
|
|
@@ -55,6 +70,61 @@ function workspaceBasename(path: string): string {
|
|
|
return path.replace(/\\/g, "/").split("/").filter(Boolean).pop() ?? path;
|
|
|
}
|
|
|
|
|
|
+function planToDrafts(
|
|
|
+ tasks: { title: string; prompt: string; dependsOn?: string[]; parallelGroup?: number }[],
|
|
|
+): TaskDispatchDraft[] {
|
|
|
+ return tasks.map((task, index) => ({
|
|
|
+ key: `task-${index}-${task.title.slice(0, 24)}`,
|
|
|
+ title: task.title,
|
|
|
+ prompt: task.prompt,
|
|
|
+ dependsOn: task.dependsOn ?? [],
|
|
|
+ parallelGroup: task.parallelGroup,
|
|
|
+ checked: true,
|
|
|
+ }));
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 分析会话终态监听器:仅在活跃分析存在时挂载(hooks 不能条件调用,用子组件承载订阅)。
|
|
|
+ * sessions-index 里该会话 phase 到终态 → 一次性回调 onTerminal。
|
|
|
+ */
|
|
|
+function TaskDispatchAnalysisWatcher({
|
|
|
+ target,
|
|
|
+ sessionId,
|
|
|
+ onTerminal,
|
|
|
+}: {
|
|
|
+ target: TaskDispatchTarget;
|
|
|
+ sessionId: string;
|
|
|
+ onTerminal: (phase: string) => void;
|
|
|
+}) {
|
|
|
+ const { sessions } = useSessionsIndex(target.workspacePath, target.workspaceIdentity ?? undefined);
|
|
|
+ const firedRef = useRef(false);
|
|
|
+ useEffect(() => {
|
|
|
+ if (firedRef.current) return;
|
|
|
+ const summary = sessions.find((session) => session.sessionId === sessionId);
|
|
|
+ if (summary && isTerminalSessionPhase(summary.phase)) {
|
|
|
+ firedRef.current = true;
|
|
|
+ onTerminal(summary.phase);
|
|
|
+ }
|
|
|
+ }, [sessions, sessionId, onTerminal]);
|
|
|
+ return null;
|
|
|
+}
|
|
|
+
|
|
|
+function formatHistoryStatus(
|
|
|
+ intl: ReturnType<typeof useZCodeIntl>["intl"],
|
|
|
+ record: TaskDispatchAnalysisRecord,
|
|
|
+): string {
|
|
|
+ if (record.status === "analyzing") {
|
|
|
+ return intl.formatMessage({ id: "taskDispatch.module.historyStatusAnalyzing" });
|
|
|
+ }
|
|
|
+ if (record.status === "failed") {
|
|
|
+ return intl.formatMessage({ id: "taskDispatch.module.historyStatusFailed" });
|
|
|
+ }
|
|
|
+ return intl.formatMessage(
|
|
|
+ { id: "taskDispatch.module.historyStatusDone" },
|
|
|
+ { count: record.plan?.tasks.length ?? 0 },
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
export function TaskDispatchModule({
|
|
|
workspaceOptions,
|
|
|
}: {
|
|
|
@@ -64,33 +134,113 @@ export function TaskDispatchModule({
|
|
|
const { intl } = useZCodeIntl();
|
|
|
const derivedOptions = useTaskDispatchWorkspaceOptions();
|
|
|
const options = workspaceOptions ?? derivedOptions;
|
|
|
- const [selectedKey, setSelectedKey] = useState<string | null>(
|
|
|
- options[0]?.key ?? null,
|
|
|
- );
|
|
|
+ const [selectedKey, setSelectedKey] = useState<string | null>(options[0]?.key ?? null);
|
|
|
const selected = useMemo(
|
|
|
() => options.find((option) => option.key === selectedKey) ?? null,
|
|
|
[options, selectedKey],
|
|
|
);
|
|
|
|
|
|
+ const { pending, startAnalysis, collectPlan } = useTaskDispatchAnalysis();
|
|
|
+ const [activeAnalysis, setActiveAnalysis] = useState<{
|
|
|
+ record: TaskDispatchAnalysisRecord;
|
|
|
+ target: TaskDispatchTarget;
|
|
|
+ } | null>(null);
|
|
|
+ // handleTerminal 的稳定读取源:回调经 useCallback 固定引用,避免闭包读到过期分析。
|
|
|
+ const activeAnalysisRef = useRef(activeAnalysis);
|
|
|
+ useEffect(() => {
|
|
|
+ activeAnalysisRef.current = activeAnalysis;
|
|
|
+ }, [activeAnalysis]);
|
|
|
+ const [drafts, setDrafts] = useState<TaskDispatchDraft[]>([]);
|
|
|
+ const [error, setError] = useState<string | null>(null);
|
|
|
+ const history = useSyncExternalStore(
|
|
|
+ taskDispatchHistoryStore.subscribe,
|
|
|
+ taskDispatchHistoryStore.getSnapshot,
|
|
|
+ );
|
|
|
+ const historyForSelected = useMemo(
|
|
|
+ () => (selected ? history.filter((record) => record.workspaceKey === selected.key) : []),
|
|
|
+ [history, selected],
|
|
|
+ );
|
|
|
+
|
|
|
+ const targetFromOption = useCallback(
|
|
|
+ (option: NonNullable<typeof selected>): TaskDispatchTarget => ({
|
|
|
+ workspacePath: option.workspacePath,
|
|
|
+ workspaceIdentity: option.workspaceIdentity,
|
|
|
+ label: option.label,
|
|
|
+ }),
|
|
|
+ [],
|
|
|
+ );
|
|
|
+
|
|
|
+ const handleAnalyze = useCallback(() => {
|
|
|
+ if (!selected) return;
|
|
|
+ setError(null);
|
|
|
+ setDrafts([]);
|
|
|
+ void startAnalysis(targetFromOption(selected)).then((result) => {
|
|
|
+ if (!result.ok || !result.record) {
|
|
|
+ setError(result.error ?? "createSession failed");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ setActiveAnalysis({ record: result.record, target: targetFromOption(selected) });
|
|
|
+ });
|
|
|
+ }, [selected, startAnalysis, targetFromOption]);
|
|
|
+
|
|
|
+ const handleTerminal = useCallback(
|
|
|
+ (phase: string) => {
|
|
|
+ const current = activeAnalysisRef.current;
|
|
|
+ if (!current) return;
|
|
|
+ // error/completedInterrupted 也尝试解析已有输出(可能有完整计划块);解析失败再报错。
|
|
|
+ void collectPlan(current.record, current.target).then((result) => {
|
|
|
+ if (result.ok) {
|
|
|
+ setDrafts(planToDrafts(result.plan.tasks));
|
|
|
+ setError(null);
|
|
|
+ } else {
|
|
|
+ setError(phase === "error" ? `${phase}: ${result.error}` : result.error);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ [collectPlan],
|
|
|
+ );
|
|
|
+
|
|
|
+ const handleLoadHistory = useCallback((record: TaskDispatchAnalysisRecord) => {
|
|
|
+ if (record.status === "done" && record.plan) {
|
|
|
+ setDrafts(planToDrafts(record.plan.tasks));
|
|
|
+ setError(null);
|
|
|
+ setActiveAnalysis(null);
|
|
|
+ }
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ const updateDraft = useCallback(
|
|
|
+ (key: string, patch: Partial<TaskDispatchDraft>) => {
|
|
|
+ setDrafts((current) =>
|
|
|
+ current.map((draft) => (draft.key === key ? { ...draft, ...patch } : draft)),
|
|
|
+ );
|
|
|
+ },
|
|
|
+ [],
|
|
|
+ );
|
|
|
+
|
|
|
+ const analyzing = activeAnalysis !== null && drafts.length === 0 && error === null;
|
|
|
+
|
|
|
return (
|
|
|
<div className="flex h-full min-h-0 flex-col">
|
|
|
<header className="flex shrink-0 items-center justify-between gap-3 border-b border-border px-4 py-3">
|
|
|
<h1 className="text-ui-lg font-medium">
|
|
|
{intl.formatMessage({ id: "taskDispatch.module.title" })}
|
|
|
</h1>
|
|
|
- {/* M3:选定项目后触发分析会话(moduleScope=task-dispatch)。 */}
|
|
|
- <Button size="sm" disabled={!selected}>
|
|
|
- {intl.formatMessage({ id: "taskDispatch.module.analyze" })}
|
|
|
+ <Button size="sm" disabled={!selected || pending || analyzing} onClick={handleAnalyze}>
|
|
|
+ {analyzing ? (
|
|
|
+ <>
|
|
|
+ <Loader2 className="size-4 animate-spin" />
|
|
|
+ {intl.formatMessage({ id: "taskDispatch.module.analyzing" })}
|
|
|
+ </>
|
|
|
+ ) : (
|
|
|
+ intl.formatMessage({ id: "taskDispatch.module.analyze" })
|
|
|
+ )}
|
|
|
</Button>
|
|
|
</header>
|
|
|
<div className="flex shrink-0 items-center gap-2 border-b border-border px-4 py-2">
|
|
|
<span className="text-ui-sm text-foreground-subtle">
|
|
|
{intl.formatMessage({ id: "taskDispatch.module.targetProject" })}
|
|
|
</span>
|
|
|
- <Select
|
|
|
- value={selectedKey ?? undefined}
|
|
|
- onValueChange={(value) => setSelectedKey(value)}
|
|
|
- >
|
|
|
+ <Select value={selectedKey ?? undefined} onValueChange={(value) => setSelectedKey(value)}>
|
|
|
<SelectTrigger className="h-8 w-[min(360px,60%)]" size="sm">
|
|
|
<SelectValue
|
|
|
placeholder={intl.formatMessage({
|
|
|
@@ -107,29 +257,93 @@ export function TaskDispatchModule({
|
|
|
</SelectContent>
|
|
|
</Select>
|
|
|
</div>
|
|
|
+ {error !== null ? (
|
|
|
+ <div
|
|
|
+ role="alert"
|
|
|
+ className="flex items-start gap-2 border-b border-border bg-surface px-4 py-2 text-ui-sm text-foreground"
|
|
|
+ >
|
|
|
+ <AlertCircle className="mt-0.5 size-4 shrink-0 text-foreground-subtle" />
|
|
|
+ <div className="min-w-0 flex-col">
|
|
|
+ <p>{intl.formatMessage({ id: "taskDispatch.module.analyzeFailed" })}</p>
|
|
|
+ <p className="truncate text-ui-caption text-foreground-subtle">{error}</p>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ ) : null}
|
|
|
<div className="flex min-h-0 flex-1">
|
|
|
- {/* 主区:按执行计划分组的任务清单(M2 占位;M3 填充模型分析结果)。 */}
|
|
|
- <section
|
|
|
- className={cn(
|
|
|
- "flex min-w-0 flex-1 flex-col items-center justify-center gap-2 p-6",
|
|
|
- "text-ui-base text-foreground-subtle",
|
|
|
+ {/* 主区:按执行计划分组的任务清单(M4 在此基础上接统一权限确认与分发)。 */}
|
|
|
+ <section className="min-w-0 flex-1 overflow-y-auto p-4">
|
|
|
+ {drafts.length > 0 ? (
|
|
|
+ <TaskDispatchPlanList
|
|
|
+ drafts={drafts}
|
|
|
+ onToggle={(key, checked) => updateDraft(key, { checked })}
|
|
|
+ onTitleChange={(key, title) => updateDraft(key, { title })}
|
|
|
+ onPromptChange={(key, prompt) => updateDraft(key, { prompt })}
|
|
|
+ onRemove={(key) =>
|
|
|
+ setDrafts((current) => current.filter((draft) => draft.key !== key))
|
|
|
+ }
|
|
|
+ />
|
|
|
+ ) : analyzing ? (
|
|
|
+ <div className="flex h-full flex-col items-center justify-center gap-2 text-ui-base text-foreground-subtle">
|
|
|
+ <Loader2 className="size-5 animate-spin" />
|
|
|
+ <p>{intl.formatMessage({ id: "taskDispatch.module.analyzingHint" })}</p>
|
|
|
+ </div>
|
|
|
+ ) : (
|
|
|
+ <div className="flex h-full flex-col items-center justify-center gap-2 text-ui-base text-foreground-subtle">
|
|
|
+ <p>{intl.formatMessage({ id: "taskDispatch.module.emptyAnalysis" })}</p>
|
|
|
+ <p className="text-ui-caption">
|
|
|
+ {intl.formatMessage({ id: "taskDispatch.module.emptyAnalysisHint" })}
|
|
|
+ </p>
|
|
|
+ </div>
|
|
|
)}
|
|
|
- >
|
|
|
- <p>{intl.formatMessage({ id: "taskDispatch.module.emptyAnalysis" })}</p>
|
|
|
- <p className="text-ui-caption">
|
|
|
- {intl.formatMessage({ id: "taskDispatch.module.emptyAnalysisHint" })}
|
|
|
- </p>
|
|
|
</section>
|
|
|
- {/* 侧区:模块会话历史(M2 占位;M3 按 moduleScope 过滤呈现)。 */}
|
|
|
+ {/* 侧区:模块会话历史(UI 注册表 + sessions-index 活性;spec §8 M3 口径)。 */}
|
|
|
<aside className="flex w-72 shrink-0 flex-col border-l border-border">
|
|
|
<div className="border-b border-border px-3 py-2 text-ui-sm font-medium text-foreground-subtle">
|
|
|
{intl.formatMessage({ id: "taskDispatch.module.history" })}
|
|
|
</div>
|
|
|
- <div className="flex flex-1 items-center justify-center p-3 text-ui-caption text-foreground-subtlest">
|
|
|
- {intl.formatMessage({ id: "taskDispatch.module.historyEmpty" })}
|
|
|
+ <div className="min-h-0 flex-1 overflow-y-auto p-2">
|
|
|
+ {historyForSelected.length === 0 ? (
|
|
|
+ <div className="flex h-full items-center justify-center p-3 text-ui-caption text-foreground-subtlest">
|
|
|
+ {intl.formatMessage({ id: "taskDispatch.module.historyEmpty" })}
|
|
|
+ </div>
|
|
|
+ ) : (
|
|
|
+ <ul className="flex flex-col gap-1">
|
|
|
+ {historyForSelected.map((record) => (
|
|
|
+ <li key={record.id}>
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ onClick={() => handleLoadHistory(record)}
|
|
|
+ disabled={record.status !== "done"}
|
|
|
+ className={cn(
|
|
|
+ "flex w-full flex-col gap-0.5 rounded-[var(--ui-radius,8px)] px-2 py-1.5 text-left",
|
|
|
+ "hover:bg-surface-hover disabled:cursor-default disabled:opacity-70",
|
|
|
+ )}
|
|
|
+ >
|
|
|
+ <span className="flex items-center gap-1 truncate text-ui-sm text-foreground">
|
|
|
+ {record.status === "analyzing" ? (
|
|
|
+ <Loader2 className="size-3 shrink-0 animate-spin" />
|
|
|
+ ) : null}
|
|
|
+ {record.workspaceLabel}
|
|
|
+ </span>
|
|
|
+ <span className="text-ui-caption text-foreground-subtle">
|
|
|
+ {formatHistoryStatus(intl, record)} ·{" "}
|
|
|
+ {new Date(record.createdAt).toLocaleString()}
|
|
|
+ </span>
|
|
|
+ </button>
|
|
|
+ </li>
|
|
|
+ ))}
|
|
|
+ </ul>
|
|
|
+ )}
|
|
|
</div>
|
|
|
</aside>
|
|
|
</div>
|
|
|
+ {activeAnalysis ? (
|
|
|
+ <TaskDispatchAnalysisWatcher
|
|
|
+ target={activeAnalysis.target}
|
|
|
+ sessionId={activeAnalysis.record.sessionId}
|
|
|
+ onTerminal={handleTerminal}
|
|
|
+ />
|
|
|
+ ) : null}
|
|
|
</div>
|
|
|
);
|
|
|
}
|