|
@@ -0,0 +1,276 @@
|
|
|
|
|
+// ============================================================
|
|
|
|
|
+// TaskDispatchDialog —— 项目级任务分发(纯 UI 拆分,不走 agent 整理)
|
|
|
|
|
+// ============================================================
|
|
|
|
|
+// 设计:docs/specs/workspace-task-dispatch.md(2026-10-18 二次对齐后的 UI 形态)。
|
|
|
|
|
+// 交互合同:
|
|
|
|
|
+// - 贴入 bug 清单 → 启发式拆分为任务卡片(编号/项目符号/空行分段);
|
|
|
|
|
+// - 卡片可编辑标题与说明、可删除、可勾选;
|
|
|
|
|
+// - 确认后在目标 workspace 批量创建【草稿任务】(initialPrompt 预填,不自动发送);
|
|
|
|
|
+// 执行由用户在 TaskList 点开任务、发送即跑——agent 只负责执行,不参与整理。
|
|
|
|
|
+// 解析是确定性启发式:不做任何模型调用,结果可预期、可撤销(重新解析覆盖)。
|
|
|
|
|
+
|
|
|
|
|
+import { useMemo, useState } from "react";
|
|
|
|
|
+import { Trash2Icon } from "lucide-react";
|
|
|
|
|
+import { Button } from "@/components/ui/button.js";
|
|
|
|
|
+import { Checkbox } from "@/components/ui/checkbox.js";
|
|
|
|
|
+import {
|
|
|
|
|
+ Dialog,
|
|
|
|
|
+ DialogContent,
|
|
|
|
|
+ DialogFooter,
|
|
|
|
|
+ DialogHeader,
|
|
|
|
|
+ DialogTitle,
|
|
|
|
|
+} from "@/components/ui/dialog.js";
|
|
|
|
|
+import { Input } from "@/components/ui/input.js";
|
|
|
|
|
+import { Textarea } from "@/components/ui/textarea.js";
|
|
|
|
|
+import { useZCodeIntl } from "@/i18n/IntlProvider.js";
|
|
|
|
|
+import { logger } from "@/logger.js";
|
|
|
|
|
+
|
|
|
|
|
+const MAX_DISPATCH_TASKS = 50;
|
|
|
|
|
+
|
|
|
|
|
+export interface TaskDispatchDraft {
|
|
|
|
|
+ key: string;
|
|
|
|
|
+ title: string;
|
|
|
|
|
+ detail: string;
|
|
|
|
|
+ selected: boolean;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+interface ParsedItem {
|
|
|
|
|
+ title: string;
|
|
|
|
|
+ detail: string;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const LIST_MARKER_PATTERN = /^\s*(?:\d+[.、)]|[-*•])\s*/;
|
|
|
|
|
+
|
|
|
|
|
+function isListMarkerLine(line: string): boolean {
|
|
|
|
|
+ return LIST_MARKER_PATTERN.test(line);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 确定性拆分启发式:
|
|
|
|
|
+ * 1. 按空行分段;
|
|
|
|
|
+ * 2. 段内出现 ≥2 个列表标记行(1. / 1) / 1、 / - / * / •)时按标记逐项拆,
|
|
|
|
|
+ * 标记行为标题、随后的非标记行归入该项说明;
|
|
|
|
|
+ * 3. 否则整段一项:首行为标题,其余为说明。
|
|
|
|
|
+ * 标题超长时截断到 60 字符(TaskList 展示友好),完整内容保留在说明里。
|
|
|
|
|
+ */
|
|
|
|
|
+export function parseTaskDraftsText(text: string): ParsedItem[] {
|
|
|
|
|
+ const blocks = text
|
|
|
|
|
+ .replace(/\r\n/g, "\n")
|
|
|
|
|
+ .split(/\n\s*\n/)
|
|
|
|
|
+ .map((block) => block.trim())
|
|
|
|
|
+ .filter(Boolean);
|
|
|
|
|
+
|
|
|
|
|
+ const items: ParsedItem[] = [];
|
|
|
|
|
+ for (const block of blocks) {
|
|
|
|
|
+ const lines = block.split("\n");
|
|
|
|
|
+ const markerCount = lines.filter(isListMarkerLine).length;
|
|
|
|
|
+ if (markerCount >= 2) {
|
|
|
|
|
+ let current: ParsedItem | null = null;
|
|
|
|
|
+ for (const line of lines) {
|
|
|
|
|
+ if (isListMarkerLine(line)) {
|
|
|
|
|
+ if (current) items.push(current);
|
|
|
|
|
+ const rawTitle = line.replace(LIST_MARKER_PATTERN, "").trim();
|
|
|
|
|
+ current = { title: rawTitle.slice(0, 60), detail: rawTitle };
|
|
|
|
|
+ } else if (current) {
|
|
|
|
|
+ const trimmed = line.trim();
|
|
|
|
|
+ current.detail = current.detail ? `${current.detail}\n${trimmed}` : trimmed;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (current) items.push(current);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const [firstLine, ...rest] = lines;
|
|
|
|
|
+ const rawTitle = (firstLine ?? block).replace(LIST_MARKER_PATTERN, "").trim();
|
|
|
|
|
+ const detail = rest.map((line) => line.trim()).filter(Boolean).join("\n");
|
|
|
|
|
+ items.push({ title: rawTitle.slice(0, 60), detail: [rawTitle, detail].filter(Boolean).join("\n") });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return items.slice(0, MAX_DISPATCH_TASKS);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function TaskDispatchDialog({
|
|
|
|
|
+ open,
|
|
|
|
|
+ target,
|
|
|
|
|
+ onOpenChange,
|
|
|
|
|
+ onCreateTask,
|
|
|
|
|
+}: {
|
|
|
|
|
+ open: boolean;
|
|
|
|
|
+ target: { workspacePath: string; workspaceIdentity?: string } | null;
|
|
|
|
|
+ onOpenChange: (open: boolean) => void;
|
|
|
|
|
+ onCreateTask: (request: {
|
|
|
|
|
+ initialPrompt: string;
|
|
|
|
|
+ targetWorkspace: { workspacePath: string; workspaceIdentity?: string };
|
|
|
|
|
+ }) => void;
|
|
|
|
|
+}) {
|
|
|
|
|
+ const { intl } = useZCodeIntl();
|
|
|
|
|
+ const [rawText, setRawText] = useState("");
|
|
|
|
|
+ const [drafts, setDrafts] = useState<TaskDispatchDraft[]>([]);
|
|
|
|
|
+
|
|
|
|
|
+ const selectedCount = useMemo(
|
|
|
|
|
+ () => drafts.filter((draft) => draft.selected).length,
|
|
|
|
|
+ [drafts],
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ const parse = () => {
|
|
|
|
|
+ const parsed = parseTaskDraftsText(rawText);
|
|
|
|
|
+ setDrafts(
|
|
|
|
|
+ parsed.map((item, index) => ({
|
|
|
|
|
+ key: `draft-${index}-${item.title.slice(0, 12)}`,
|
|
|
|
|
+ title: item.title,
|
|
|
|
|
+ detail: item.detail,
|
|
|
|
|
+ selected: true,
|
|
|
|
|
+ })),
|
|
|
|
|
+ );
|
|
|
|
|
+ if (parsed.length === 0) {
|
|
|
|
|
+ logger.info("[TaskDispatchDialog] 解析结果为空", { rawLength: rawText.length });
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const reset = () => {
|
|
|
|
|
+ setRawText("");
|
|
|
|
|
+ setDrafts([]);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ const confirm = () => {
|
|
|
|
|
+ if (!target) return;
|
|
|
|
|
+ const chosen = drafts.filter((draft) => draft.selected && draft.title.trim());
|
|
|
|
|
+ for (const draft of chosen) {
|
|
|
|
|
+ onCreateTask({
|
|
|
|
|
+ // 预填正文 = 标题 + 完整说明;用户点开任务发送后由 agent 执行。
|
|
|
|
|
+ initialPrompt: draft.detail.trim() || draft.title.trim(),
|
|
|
|
|
+ targetWorkspace: {
|
|
|
|
|
+ workspacePath: target.workspacePath,
|
|
|
|
|
+ ...(target.workspaceIdentity ? { workspaceIdentity: target.workspaceIdentity } : {}),
|
|
|
|
|
+ },
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ logger.info("[TaskDispatchDialog] 批量创建草稿任务完成", {
|
|
|
|
|
+ requested: chosen.length,
|
|
|
|
|
+ workspacePath: target.workspacePath,
|
|
|
|
|
+ });
|
|
|
|
|
+ reset();
|
|
|
|
|
+ onOpenChange(false);
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ return (
|
|
|
|
|
+ <Dialog
|
|
|
|
|
+ open={open}
|
|
|
|
|
+ onOpenChange={(next) => {
|
|
|
|
|
+ if (!next) reset();
|
|
|
|
|
+ onOpenChange(next);
|
|
|
|
|
+ }}
|
|
|
|
|
+ >
|
|
|
|
|
+ <DialogContent className="flex max-h-[85vh] w-[min(680px,92vw)] max-w-none flex-col overflow-hidden rounded-2xl p-0">
|
|
|
|
|
+ <div className="flex min-h-0 flex-1 flex-col gap-4 p-6">
|
|
|
|
|
+ <DialogHeader>
|
|
|
|
|
+ <DialogTitle>
|
|
|
|
|
+ {intl.formatMessage({ id: "taskDispatch.title" })}
|
|
|
|
|
+ </DialogTitle>
|
|
|
|
|
+ </DialogHeader>
|
|
|
|
|
+ {drafts.length === 0 ? (
|
|
|
|
|
+ <div className="flex min-h-0 flex-1 flex-col gap-3">
|
|
|
|
|
+ <Textarea
|
|
|
|
|
+ className="min-h-[220px] flex-1 resize-none font-mono text-ui-sm"
|
|
|
|
|
+ placeholder={intl.formatMessage({ id: "taskDispatch.pastePlaceholder" })}
|
|
|
|
|
+ value={rawText}
|
|
|
|
|
+ onChange={(event) => setRawText(event.target.value)}
|
|
|
|
|
+ />
|
|
|
|
|
+ <div className="flex items-center justify-between gap-3">
|
|
|
|
|
+ <p className="text-ui-caption text-foreground-subtle">
|
|
|
|
|
+ {intl.formatMessage({ id: "taskDispatch.parseHint" })}
|
|
|
|
|
+ </p>
|
|
|
|
|
+ <Button
|
|
|
|
|
+ size="sm"
|
|
|
|
|
+ disabled={!rawText.trim()}
|
|
|
|
|
+ onClick={parse}
|
|
|
|
|
+ >
|
|
|
|
|
+ {intl.formatMessage({ id: "taskDispatch.parse" })}
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ ) : (
|
|
|
|
|
+ <div className="flex min-h-0 flex-1 flex-col gap-3">
|
|
|
|
|
+ <div className="flex items-center justify-between">
|
|
|
|
|
+ <p className="text-ui-caption text-foreground-subtle">
|
|
|
|
|
+ {intl.formatMessage(
|
|
|
|
|
+ { id: "taskDispatch.selectedCount" },
|
|
|
|
|
+ { selected: selectedCount, total: drafts.length },
|
|
|
|
|
+ )}
|
|
|
|
|
+ </p>
|
|
|
|
|
+ <div className="flex gap-2">
|
|
|
|
|
+ <Button variant="ghost" size="sm" onClick={reset}>
|
|
|
|
|
+ {intl.formatMessage({ id: "taskDispatch.reparse" })}
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div className="flex min-h-0 flex-1 flex-col gap-2 overflow-y-auto pr-1">
|
|
|
|
|
+ {drafts.map((draft, index) => (
|
|
|
|
|
+ <div
|
|
|
|
|
+ key={draft.key}
|
|
|
|
|
+ className="flex flex-col gap-2 rounded-xl border border-border p-3"
|
|
|
|
|
+ >
|
|
|
|
|
+ <div className="flex items-center gap-2">
|
|
|
|
|
+ <Checkbox
|
|
|
|
|
+ checked={draft.selected}
|
|
|
|
|
+ onCheckedChange={(checked) => {
|
|
|
|
|
+ setDrafts((prev) =>
|
|
|
|
|
+ prev.map((item, i) =>
|
|
|
|
|
+ i === index ? { ...item, selected: checked === true } : item,
|
|
|
|
|
+ ),
|
|
|
|
|
+ );
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ <Input
|
|
|
|
|
+ className="h-8 flex-1"
|
|
|
|
|
+ value={draft.title}
|
|
|
|
|
+ onChange={(event) => {
|
|
|
|
|
+ const value = event.target.value;
|
|
|
|
|
+ setDrafts((prev) =>
|
|
|
|
|
+ prev.map((item, i) => (i === index ? { ...item, title: value } : item)),
|
|
|
|
|
+ );
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ <Button
|
|
|
|
|
+ variant="ghost"
|
|
|
|
|
+ size="icon-sm"
|
|
|
|
|
+ aria-label={intl.formatMessage({ id: "taskDispatch.remove" })}
|
|
|
|
|
+ onClick={() => {
|
|
|
|
|
+ setDrafts((prev) => prev.filter((_, i) => i !== index));
|
|
|
|
|
+ }}
|
|
|
|
|
+ >
|
|
|
|
|
+ <Trash2Icon className="h-3.5 w-3.5" />
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <Textarea
|
|
|
|
|
+ className="min-h-[52px] resize-none text-ui-sm"
|
|
|
|
|
+ value={draft.detail}
|
|
|
|
|
+ onChange={(event) => {
|
|
|
|
|
+ const value = event.target.value;
|
|
|
|
|
+ setDrafts((prev) =>
|
|
|
|
|
+ prev.map((item, i) => (i === index ? { ...item, detail: value } : item)),
|
|
|
|
|
+ );
|
|
|
|
|
+ }}
|
|
|
|
|
+ />
|
|
|
|
|
+ </div>
|
|
|
|
|
+ ))}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )}
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <DialogFooter className="border-t border-border p-4">
|
|
|
|
|
+ <Button variant="ghost" onClick={() => onOpenChange(false)}>
|
|
|
|
|
+ {intl.formatMessage({ id: "common.cancel" })}
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ <Button
|
|
|
|
|
+ disabled={drafts.length === 0 || selectedCount === 0}
|
|
|
|
|
+ onClick={confirm}
|
|
|
|
|
+ >
|
|
|
|
|
+ {intl.formatMessage(
|
|
|
|
|
+ { id: "taskDispatch.createDrafts" },
|
|
|
|
|
+ { count: selectedCount },
|
|
|
|
|
+ )}
|
|
|
|
|
+ </Button>
|
|
|
|
|
+ </DialogFooter>
|
|
|
|
|
+ </DialogContent>
|
|
|
|
|
+ </Dialog>
|
|
|
|
|
+ );
|
|
|
|
|
+}
|