mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-03 20:21:51 +08:00
Merge pull request #44 from RuoJi6/feat/notes-and-worker-count
feat: 任务/模板备注展示与 Worker 运行数量配置
This commit is contained in:
22 files changed
+569
-56
No files matched your search
@@ -414,6 +414,11 @@ function TaskDetailInner() {
|
||||
</Button>
|
||||
</div>
|
||||
<p className="truncate text-xs text-muted-foreground">{task.goal}</p>
|
||||
{task.remark ? (
|
||||
<p className="truncate text-xs text-muted-foreground" title={task.remark}>
|
||||
备注:{task.remark}
|
||||
</p>
|
||||
) : null}
|
||||
{/* Tabs */}
|
||||
<div className="no-scrollbar min-w-0 overflow-x-auto">
|
||||
<TabsList variant="default" className="min-w-max">
|
||||
|
||||
@@ -137,6 +137,7 @@ import type {
|
||||
TaskCategory,
|
||||
TaskStatus,
|
||||
} from "@/lib/types";
|
||||
import { MAX_TASK_REMARK_RUNES, MAX_TASK_WORKER_COUNT, parseWorkerCountInput } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
// fmtBytes renders a human file size for the upload manifest.
|
||||
@@ -3104,6 +3105,7 @@ function CreateTaskSheet({
|
||||
}) {
|
||||
const [open, setOpen] = React.useState(false);
|
||||
const [name, setName] = React.useState("");
|
||||
const [remark, setRemark] = React.useState("");
|
||||
const [categoryID, setCategoryID] = React.useState<number | undefined>(undefined);
|
||||
const [description, setDescription] = React.useState("");
|
||||
const [goal, setGoal] = React.useState("");
|
||||
@@ -3118,6 +3120,7 @@ function CreateTaskSheet({
|
||||
const [heartbeatMin, setHeartbeatMin] = React.useState("10"); // planner 心跳(分钟);默认10,下限10(与后端一致)
|
||||
const [seedFirstIntent, setSeedFirstIntent] = React.useState(false); // 创建时下发种子意图,worker 免等首轮 planner 直接开跑;默认关闭,走标准先规划再执行
|
||||
const [coverageEnabled, setCoverageEnabled] = React.useState(true); // 资产覆盖度功能;默认开。关闭=不计算/展示覆盖度、不累积范围、隐藏范围类工具(company 关联不受影响)
|
||||
const [workerCount, setWorkerCount] = React.useState(""); // 本任务 Worker 数;空/0=用全局默认
|
||||
const [interceptRules, setInterceptRules] = React.useState<AssetInterceptRuleInput[]>([]); // 任务级资产拦截规则(仅本任务生效,不进全局表)
|
||||
const [scheduleEnabled, setScheduleEnabled] = React.useState(false);
|
||||
const [schedule, setSchedule] = React.useState<ScheduleInput>({
|
||||
@@ -3181,12 +3184,18 @@ function CreateTaskSheet({
|
||||
toast.error(`最多关联 ${MAX_SOURCE_TASKS} 个来源任务`);
|
||||
return;
|
||||
}
|
||||
const workerCountNum = parseWorkerCountInput(workerCount);
|
||||
if (workerCountNum == null) {
|
||||
toast.error(`Worker 运行数量必须是 0-${MAX_TASK_WORKER_COUNT} 的整数`);
|
||||
return;
|
||||
}
|
||||
setCreating(true);
|
||||
try {
|
||||
const timeoutSec = Math.max(0, Math.floor(Number(timeoutMin) || 0)) * 60;
|
||||
const heartbeatSec = Math.max(10, Math.floor(Number(heartbeatMin) || 10)) * 60; // 下限 10min,与后端归一一致
|
||||
await api.createTask({
|
||||
name: name.trim(),
|
||||
remark: remark.trim(),
|
||||
categoryId: categoryID,
|
||||
description: description.trim(),
|
||||
goal: goal.trim(),
|
||||
@@ -3197,6 +3206,7 @@ function CreateTaskSheet({
|
||||
seedFirstIntent,
|
||||
planHeartbeatSeconds: heartbeatSec,
|
||||
coverageEnabled,
|
||||
workerCount: workerCountNum,
|
||||
schedule: scheduleEnabled ? { ...schedule, task_ids: [] } : undefined,
|
||||
interceptRules: interceptRules
|
||||
.map((r) => ({ ...r, pattern: r.pattern.trim() }))
|
||||
@@ -3204,6 +3214,7 @@ function CreateTaskSheet({
|
||||
});
|
||||
toast.success("任务已创建");
|
||||
setName("");
|
||||
setRemark("");
|
||||
setCategoryID(undefined);
|
||||
setDescription("");
|
||||
setGoal("");
|
||||
@@ -3215,6 +3226,7 @@ function CreateTaskSheet({
|
||||
setHeartbeatMin("10");
|
||||
setSeedFirstIntent(false);
|
||||
setCoverageEnabled(true);
|
||||
setWorkerCount("");
|
||||
setInterceptRules([]);
|
||||
setScheduleEnabled(false);
|
||||
setSchedule({
|
||||
@@ -3263,15 +3275,19 @@ function CreateTaskSheet({
|
||||
<TaskTemplateControls
|
||||
description={description}
|
||||
goal={goal}
|
||||
remark={remark}
|
||||
categoryID={categoryID}
|
||||
interceptRules={interceptRules}
|
||||
workerCount={workerCount}
|
||||
selectedTemplateID={selectedTemplateID}
|
||||
onSelectedTemplateIDChange={setSelectedTemplateID}
|
||||
onApply={(template) => {
|
||||
setDescription(template.description);
|
||||
setGoal(template.goal);
|
||||
setRemark(template.remark ?? "");
|
||||
setCategoryID(template.category_id ?? undefined);
|
||||
setInterceptRules(template.intercept_rules ?? []);
|
||||
setWorkerCount(template.worker_count ? String(template.worker_count) : "");
|
||||
setUploadCount(0);
|
||||
}}
|
||||
portalContainer={sheetContentRef}
|
||||
@@ -3285,6 +3301,17 @@ function CreateTaskSheet({
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="remark">备注(可选)</Label>
|
||||
<Textarea
|
||||
id="remark"
|
||||
className="min-h-20"
|
||||
maxLength={MAX_TASK_REMARK_RUNES}
|
||||
placeholder="补充说明、环境信息等,便于在任务列表识别"
|
||||
value={remark}
|
||||
onChange={(e) => setRemark(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="task-category">任务分类</FieldLabel>
|
||||
<CategoryPicker
|
||||
@@ -3507,7 +3534,7 @@ function CreateTaskSheet({
|
||||
<CollapsibleTrigger className="group flex w-full items-center gap-2 border-t pt-4 text-sm font-medium">
|
||||
<ChevronRightIcon className="text-muted-foreground size-4 transition-transform group-data-[state=open]:rotate-90" />
|
||||
高级设置
|
||||
<span className="text-muted-foreground ml-auto text-xs font-normal">超时 · 心跳 · 首个意图</span>
|
||||
<span className="text-muted-foreground ml-auto text-xs font-normal">超时 · 心跳 · Worker · 首个意图</span>
|
||||
</CollapsibleTrigger>
|
||||
<CollapsibleContent className="grid gap-5 pt-5">
|
||||
<div className="grid gap-2">
|
||||
@@ -3541,6 +3568,23 @@ function CreateTaskSheet({
|
||||
worker)。下限 10 分钟。
|
||||
</p>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="worker-count">Worker 运行数量(可选)</Label>
|
||||
<Input
|
||||
id="worker-count"
|
||||
type="number"
|
||||
min={0}
|
||||
max={MAX_TASK_WORKER_COUNT}
|
||||
className="w-40"
|
||||
placeholder="留空 = 全局默认"
|
||||
value={workerCount}
|
||||
onChange={(e) => setWorkerCount(e.target.value)}
|
||||
/>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
单独设置本任务并发运行的工作 Agent 数量;留空或 0 = 用全局默认(系统设置里的工作并发,默认
|
||||
3)。上限 50。
|
||||
</p>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<label htmlFor="seed-first-intent" className="flex items-center gap-2 text-sm">
|
||||
<Checkbox
|
||||
|
||||
@@ -8,6 +8,7 @@ import { toast } from "sonner";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { MAX_TASK_REMARK_RUNES } from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
type EditorProps = {
|
||||
@@ -17,7 +18,7 @@ type EditorProps = {
|
||||
};
|
||||
|
||||
export function RemarkEditor(props: EditorProps) {
|
||||
return <InlineTextEditor {...props} multiline fieldName="备注" maxCharacters={2000} emptyLabel="添加备注" />;
|
||||
return <InlineTextEditor {...props} multiline fieldName="备注" maxCharacters={MAX_TASK_REMARK_RUNES} emptyLabel="添加备注" />;
|
||||
}
|
||||
|
||||
export function ScheduleNameEditor(props: EditorProps) {
|
||||
|
||||
@@ -33,19 +33,28 @@ import { Sheet, SheetContent, SheetDescription, SheetFooter, SheetHeader, SheetT
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { api } from "@/lib/api";
|
||||
import type { AssetInterceptRuleInput, TaskCategory, TaskTemplate } from "@/lib/types";
|
||||
import {
|
||||
MAX_TASK_REMARK_RUNES,
|
||||
MAX_TASK_WORKER_COUNT,
|
||||
parseWorkerCountInput,
|
||||
type AssetInterceptRuleInput,
|
||||
type TaskCategory,
|
||||
type TaskTemplate,
|
||||
} from "@/lib/types";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
interface TemplateDraft {
|
||||
name: string;
|
||||
description: string;
|
||||
goal: string;
|
||||
remark: string;
|
||||
categoryID: number | null;
|
||||
interceptRules: AssetInterceptRuleInput[];
|
||||
workerCount: string; // 输入框用字符串;空/0=用全局默认
|
||||
}
|
||||
|
||||
// TemplateSeed 是「另存为模板」时从创建表单带入的初值。
|
||||
type TemplateSeed = Pick<TemplateDraft, "description" | "goal" | "categoryID" | "interceptRules">;
|
||||
type TemplateSeed = Pick<TemplateDraft, "description" | "goal" | "remark" | "categoryID" | "interceptRules" | "workerCount">;
|
||||
|
||||
interface TaskTemplateManagerProps {
|
||||
open: boolean;
|
||||
@@ -61,8 +70,10 @@ const emptyDraft = (): TemplateDraft => ({
|
||||
name: "",
|
||||
description: "",
|
||||
goal: "",
|
||||
remark: "",
|
||||
categoryID: null,
|
||||
interceptRules: [],
|
||||
workerCount: "",
|
||||
});
|
||||
|
||||
function templateDraft(template: TaskTemplate): TemplateDraft {
|
||||
@@ -70,8 +81,10 @@ function templateDraft(template: TaskTemplate): TemplateDraft {
|
||||
name: template.name,
|
||||
description: template.description,
|
||||
goal: template.goal,
|
||||
remark: template.remark ?? "",
|
||||
categoryID: template.category_id ?? null,
|
||||
interceptRules: template.intercept_rules ?? [],
|
||||
workerCount: template.worker_count ? String(template.worker_count) : "",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -108,8 +121,10 @@ function TaskTemplateManager({
|
||||
name: "",
|
||||
description: seed.description,
|
||||
goal: seed.goal,
|
||||
remark: seed.remark,
|
||||
categoryID: seed.categoryID,
|
||||
interceptRules: seed.interceptRules,
|
||||
workerCount: seed.workerCount,
|
||||
});
|
||||
} else if (templates[0]) {
|
||||
setSelectedID(templates[0].id);
|
||||
@@ -132,7 +147,7 @@ function TaskTemplateManager({
|
||||
setDraft(emptyDraft());
|
||||
};
|
||||
|
||||
const updateDraft = (field: "name" | "description" | "goal", value: string) => {
|
||||
const updateDraft = (field: "name" | "description" | "goal" | "remark" | "workerCount", value: string) => {
|
||||
setDraft((current) => ({ ...current, [field]: value }));
|
||||
};
|
||||
|
||||
@@ -141,11 +156,18 @@ function TaskTemplateManager({
|
||||
};
|
||||
|
||||
async function save() {
|
||||
const workerCount = parseWorkerCountInput(draft.workerCount);
|
||||
if (workerCount == null) {
|
||||
toast.error(`Worker 运行数量必须是 0-${MAX_TASK_WORKER_COUNT} 的整数`);
|
||||
return;
|
||||
}
|
||||
const input = {
|
||||
name: draft.name.trim(),
|
||||
description: draft.description.trim(),
|
||||
goal: draft.goal.trim(),
|
||||
remark: draft.remark.trim(),
|
||||
category_id: draft.categoryID,
|
||||
worker_count: workerCount,
|
||||
intercept_rules: draft.interceptRules
|
||||
.map((r) => ({ ...r, pattern: r.pattern.trim() }))
|
||||
.filter((r) => r.pattern !== ""),
|
||||
@@ -207,7 +229,7 @@ function TaskTemplateManager({
|
||||
<SheetContent className="grid h-full w-full! max-w-none! grid-rows-[auto_minmax(0,1fr)_auto] gap-0 overflow-hidden p-0 sm:w-[48rem]! sm:max-w-[48rem]!">
|
||||
<SheetHeader className="border-b px-6 py-5">
|
||||
<SheetTitle>任务模板管理</SheetTitle>
|
||||
<SheetDescription>模板保存描述、目标、分类与任务级拦截/允许规则;修改不会影响已经创建的任务。</SheetDescription>
|
||||
<SheetDescription>模板保存描述、目标、备注、Worker 数量、分类与任务级拦截/允许规则;修改不会影响已经创建的任务。</SheetDescription>
|
||||
</SheetHeader>
|
||||
<div className="grid min-h-0 overflow-y-auto lg:grid-cols-[15rem_minmax(0,1fr)] lg:overflow-hidden">
|
||||
<div className="flex min-h-0 flex-col border-b p-3 lg:border-r lg:border-b-0">
|
||||
@@ -232,6 +254,9 @@ function TaskTemplateManager({
|
||||
>
|
||||
<span className="block truncate font-medium text-sm">{template.name}</span>
|
||||
<span className="block truncate text-muted-foreground text-xs">{template.description}</span>
|
||||
{template.remark ? (
|
||||
<span className="block truncate text-muted-foreground/80 text-xs">备注:{template.remark}</span>
|
||||
) : null}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
@@ -269,6 +294,31 @@ function TaskTemplateManager({
|
||||
onChange={(event) => updateDraft("goal", event.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="task-template-remark">备注(可选)</FieldLabel>
|
||||
<Textarea
|
||||
id="task-template-remark"
|
||||
className="min-h-20"
|
||||
maxLength={MAX_TASK_REMARK_RUNES}
|
||||
value={draft.remark}
|
||||
placeholder="预设备注,应用模板时写入新任务"
|
||||
onChange={(event) => updateDraft("remark", event.target.value)}
|
||||
/>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="task-template-worker-count">Worker 运行数量(可选)</FieldLabel>
|
||||
<Input
|
||||
id="task-template-worker-count"
|
||||
type="number"
|
||||
min={0}
|
||||
max={MAX_TASK_WORKER_COUNT}
|
||||
className="w-40"
|
||||
placeholder="留空 = 全局默认"
|
||||
value={draft.workerCount}
|
||||
onChange={(event) => updateDraft("workerCount", event.target.value)}
|
||||
/>
|
||||
<FieldDescription>应用模板时预填本任务 Worker 数;留空或 0 = 用全局默认。</FieldDescription>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="task-template-category">任务分类</FieldLabel>
|
||||
<NativeSelect
|
||||
@@ -347,8 +397,10 @@ function TaskTemplateManager({
|
||||
interface TaskTemplateControlsProps {
|
||||
description: string;
|
||||
goal: string;
|
||||
remark?: string;
|
||||
categoryID?: number;
|
||||
interceptRules?: AssetInterceptRuleInput[];
|
||||
workerCount?: string;
|
||||
selectedTemplateID: number | null;
|
||||
onSelectedTemplateIDChange: (id: number | null) => void;
|
||||
onApply: (template: TaskTemplate) => void;
|
||||
@@ -358,8 +410,10 @@ interface TaskTemplateControlsProps {
|
||||
export function TaskTemplateControls({
|
||||
description,
|
||||
goal,
|
||||
remark = "",
|
||||
categoryID,
|
||||
interceptRules,
|
||||
workerCount = "",
|
||||
selectedTemplateID,
|
||||
onSelectedTemplateIDChange,
|
||||
onApply,
|
||||
@@ -410,8 +464,18 @@ export function TaskTemplateControls({
|
||||
return;
|
||||
}
|
||||
setTemplateInputValue(template.name);
|
||||
const hasContent = description.trim() !== "" || goal.trim() !== "";
|
||||
const changesContent = description !== template.description || goal !== template.goal;
|
||||
const currentWorkerCount = parseWorkerCountInput(workerCount);
|
||||
const templateWorkerCount = template.worker_count ?? 0;
|
||||
const hasContent =
|
||||
description.trim() !== "" ||
|
||||
goal.trim() !== "" ||
|
||||
remark.trim() !== "" ||
|
||||
(currentWorkerCount == null ? workerCount.trim() !== "" : currentWorkerCount !== 0);
|
||||
const changesContent =
|
||||
description !== template.description ||
|
||||
goal !== template.goal ||
|
||||
remark !== (template.remark ?? "") ||
|
||||
(currentWorkerCount == null ? workerCount.trim() !== "" : currentWorkerCount !== templateWorkerCount);
|
||||
if (hasContent && changesContent) {
|
||||
setPendingTemplate(template);
|
||||
return;
|
||||
@@ -455,8 +519,10 @@ export function TaskTemplateControls({
|
||||
openManager({
|
||||
description,
|
||||
goal,
|
||||
remark,
|
||||
categoryID: categoryID ?? null,
|
||||
interceptRules: interceptRules ?? [],
|
||||
workerCount,
|
||||
})
|
||||
}
|
||||
>
|
||||
@@ -498,7 +564,9 @@ export function TaskTemplateControls({
|
||||
</ComboboxList>
|
||||
</ComboboxContent>
|
||||
</Combobox>
|
||||
<FieldDescription>选择后会复制模板的描述、目标、分类与任务级规则,不与模板保持关联。</FieldDescription>
|
||||
<FieldDescription>
|
||||
选择后会复制模板的描述、目标、备注、Worker 数量、分类与任务级规则,不与模板保持关联。
|
||||
</FieldDescription>
|
||||
</Field>
|
||||
|
||||
<AlertDialog
|
||||
@@ -512,7 +580,7 @@ export function TaskTemplateControls({
|
||||
<AlertDialogContent>
|
||||
<AlertDialogHeader>
|
||||
<AlertDialogTitle>使用模板「{pendingTemplate?.name}」?</AlertDialogTitle>
|
||||
<AlertDialogDescription>当前已填写的描述和目标将被模板内容覆盖。</AlertDialogDescription>
|
||||
<AlertDialogDescription>当前已填写的描述、目标、备注和 Worker 数量将被模板内容覆盖。</AlertDialogDescription>
|
||||
</AlertDialogHeader>
|
||||
<AlertDialogFooter>
|
||||
<AlertDialogCancel>取消</AlertDialogCancel>
|
||||
|
||||
+6
-2
@@ -302,6 +302,7 @@ export const api = {
|
||||
task: (id: string) => get<Task>(`/tasks/${encodeURIComponent(id)}`),
|
||||
createTask: (input: {
|
||||
name?: string;
|
||||
remark?: string;
|
||||
categoryId?: number;
|
||||
description: string;
|
||||
goal: string;
|
||||
@@ -312,11 +313,13 @@ export const api = {
|
||||
seedFirstIntent?: boolean;
|
||||
planHeartbeatSeconds?: number;
|
||||
coverageEnabled?: boolean;
|
||||
workerCount?: number;
|
||||
interceptRules?: AssetInterceptRuleInput[];
|
||||
schedule?: ScheduleInput;
|
||||
}) =>
|
||||
post<Task>("/tasks", {
|
||||
name: input.name ?? "",
|
||||
remark: input.remark ?? "",
|
||||
category_id: input.categoryId ?? null,
|
||||
description: input.description,
|
||||
goal: input.goal,
|
||||
@@ -327,6 +330,7 @@ export const api = {
|
||||
seed_first_intent: input.seedFirstIntent ?? false,
|
||||
plan_heartbeat_seconds: input.planHeartbeatSeconds ?? 0, // 0 = 后端归一到默认 600(10min)
|
||||
coverage_enabled: input.coverageEnabled ?? true, // 默认开;false=关闭资产覆盖度功能
|
||||
worker_count: input.workerCount ?? 0, // 0 = 用全局默认 workers
|
||||
intercept_rules: input.interceptRules ?? [], // 任务级资产拦截规则
|
||||
schedule: input.schedule ? schedulePayload(input.schedule) : undefined,
|
||||
}),
|
||||
@@ -359,11 +363,11 @@ export const api = {
|
||||
}),
|
||||
taskTemplates: () => get<{ templates: TaskTemplate[] }>("/task-templates").then((r) => arr(r.templates)),
|
||||
createTaskTemplate: (
|
||||
input: Pick<TaskTemplate, "name" | "description" | "goal" | "category_id" | "intercept_rules">,
|
||||
input: Pick<TaskTemplate, "name" | "description" | "goal" | "remark" | "category_id" | "intercept_rules" | "worker_count">,
|
||||
) => post<TaskTemplate>("/task-templates", input),
|
||||
updateTaskTemplate: (
|
||||
id: number,
|
||||
input: Partial<Pick<TaskTemplate, "name" | "description" | "goal" | "category_id" | "intercept_rules">>,
|
||||
input: Partial<Pick<TaskTemplate, "name" | "description" | "goal" | "remark" | "category_id" | "intercept_rules" | "worker_count">>,
|
||||
) => patch<TaskTemplate>(`/task-templates/${id}`, input),
|
||||
deleteTaskTemplate: (id: number) => del<{ deleted: number }>(`/task-templates/${id}`),
|
||||
updateTaskLLMProfiles: (id: string, llmProfileIds: number[], activeLLMProfileId?: number) =>
|
||||
|
||||
+109
-15
@@ -33,6 +33,7 @@ import type {
|
||||
TrafficEvidenceRole,
|
||||
TrafficEvidenceSnapshot,
|
||||
} from "../types";
|
||||
import { MAX_TASK_REMARK_RUNES, MAX_TASK_WORKER_COUNT } from "../types";
|
||||
import * as D from "./data";
|
||||
|
||||
const delay = (ms = 120) => new Promise((r) => setTimeout(r, ms));
|
||||
@@ -998,6 +999,40 @@ function parseBody(body?: BodyInit | null): Record<string, unknown> {
|
||||
}
|
||||
}
|
||||
|
||||
function mockRemark(value: unknown): string {
|
||||
if (value === undefined || value === null) return "";
|
||||
if (typeof value !== "string") throw new Error("备注必须是字符串");
|
||||
const remark = value.trim();
|
||||
if (Array.from(remark).length > MAX_TASK_REMARK_RUNES) {
|
||||
throw new Error(`备注最多 ${MAX_TASK_REMARK_RUNES} 个字符`);
|
||||
}
|
||||
return remark;
|
||||
}
|
||||
|
||||
function mockWorkerCount(value: unknown): number {
|
||||
if (value === undefined || value === null) return 0;
|
||||
if (typeof value !== "number" || !Number.isInteger(value)) {
|
||||
throw new Error("Worker 运行数量必须是整数");
|
||||
}
|
||||
if (value < 0) throw new Error("Worker 运行数量不能为负数");
|
||||
if (value > MAX_TASK_WORKER_COUNT) throw new Error(`Worker 运行数量最多 ${MAX_TASK_WORKER_COUNT}`);
|
||||
return value;
|
||||
}
|
||||
|
||||
function mockStringField(value: unknown, label: string): string {
|
||||
if (value === undefined || value === null) return "";
|
||||
if (typeof value !== "string") throw new Error(`${label} 必须是字符串`);
|
||||
return value;
|
||||
}
|
||||
|
||||
function mockIntegerField(value: unknown, label: string): number | null {
|
||||
if (value === undefined || value === null) return null;
|
||||
if (typeof value !== "number" || !Number.isInteger(value)) {
|
||||
throw new Error(`${label} 必须是整数`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export async function mockHandle<T>(method: string, rawPath: string, body?: BodyInit | null): Promise<T> {
|
||||
advanceMockRetests();
|
||||
await delay();
|
||||
@@ -1191,19 +1226,24 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
|
||||
const profileIDs = [...((b.llm_profile_ids as number[] | undefined) ?? [])];
|
||||
const sourceTaskIDs = [...((b.source_task_ids as string[] | undefined) ?? [])];
|
||||
const companyIDs = [...new Set((b.company_ids as number[] | undefined) ?? [])];
|
||||
const name = mockStringField(b.name, "任务名称");
|
||||
const description = mockStringField(b.description, "任务描述");
|
||||
const goal = mockStringField(b.goal, "任务目标");
|
||||
if (companyIDs.some((companyID) => !mockCompanies.some((company) => company.id === companyID))) {
|
||||
throw new Error("关联企业不存在或无效");
|
||||
}
|
||||
const categoryID = typeof b.category_id === "number" ? b.category_id : undefined;
|
||||
const categoryID = mockIntegerField(b.category_id, "category_id") ?? undefined;
|
||||
const category = categoryID === undefined ? undefined : mockTaskCategories.find((item) => item.id === categoryID);
|
||||
if (categoryID !== undefined && !category) throw new Error("任务分类不存在");
|
||||
const created: Task = {
|
||||
id,
|
||||
name: String(b.name ?? ""),
|
||||
name,
|
||||
remark: mockRemark(b.remark),
|
||||
category_id: category?.id,
|
||||
category_name: category?.name,
|
||||
description: String(b.description ?? "新任务"),
|
||||
goal: String(b.goal ?? ""),
|
||||
description: description || "新任务",
|
||||
goal,
|
||||
worker_count: mockWorkerCount(b.worker_count),
|
||||
status: "created",
|
||||
created_at: now.toISOString(),
|
||||
created_unix: Math.floor(now.getTime() / 1000),
|
||||
@@ -1318,9 +1358,9 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
|
||||
if (path === "/task-templates" && m === "GET") return { templates: mockTaskTemplates };
|
||||
if (path === "/task-templates" && m === "POST") {
|
||||
const now = new Date().toISOString();
|
||||
const name = normalizedTemplateName(b.name);
|
||||
const description = String(b.description ?? "").trim();
|
||||
const goal = String(b.goal ?? "").trim();
|
||||
const name = normalizedTemplateName(mockStringField(b.name, "模板名称"));
|
||||
const description = mockStringField(b.description, "模板描述").trim();
|
||||
const goal = mockStringField(b.goal, "模板目标").trim();
|
||||
if (!name || !description || !goal) throw new Error("请填写模板名称、描述和目标");
|
||||
if (
|
||||
mockTaskTemplates.some((template) => normalizedTemplateName(template.name).toLowerCase() === name.toLowerCase())
|
||||
@@ -1328,11 +1368,19 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
|
||||
throw new Error("模板名称已存在");
|
||||
}
|
||||
const nextID = mockTaskTemplates.reduce((max, template) => Math.max(max, template.id), 0) + 1;
|
||||
const categoryID = mockIntegerField(b.category_id, "category_id");
|
||||
if (b.intercept_rules !== undefined && b.intercept_rules !== null && !Array.isArray(b.intercept_rules)) {
|
||||
throw new Error("intercept_rules 必须是数组");
|
||||
}
|
||||
const created: TaskTemplate = {
|
||||
id: nextID,
|
||||
name,
|
||||
description,
|
||||
goal,
|
||||
remark: mockRemark(b.remark),
|
||||
category_id: categoryID,
|
||||
intercept_rules: Array.isArray(b.intercept_rules) ? b.intercept_rules : [],
|
||||
worker_count: mockWorkerCount(b.worker_count),
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
};
|
||||
@@ -1342,9 +1390,17 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
|
||||
if (seg[0] === "task-templates" && seg.length === 2 && m === "PATCH") {
|
||||
const template = mockTaskTemplates.find((item) => item.id === Number(seg[1]));
|
||||
if (!template) return {};
|
||||
const name = typeof b.name === "string" ? normalizedTemplateName(b.name) : template.name;
|
||||
const description = typeof b.description === "string" ? b.description.trim() : template.description;
|
||||
const goal = typeof b.goal === "string" ? b.goal.trim() : template.goal;
|
||||
const has = (key: string) => Object.prototype.hasOwnProperty.call(b, key);
|
||||
const nameValue = has("name") ? mockStringField(b.name, "模板名称") : undefined;
|
||||
const descriptionValue = has("description") ? mockStringField(b.description, "模板描述") : undefined;
|
||||
const goalValue = has("goal") ? mockStringField(b.goal, "模板目标") : undefined;
|
||||
const categoryID = has("category_id") ? mockIntegerField(b.category_id, "category_id") : template.category_id;
|
||||
if (has("intercept_rules") && b.intercept_rules !== null && !Array.isArray(b.intercept_rules)) {
|
||||
throw new Error("intercept_rules 必须是数组");
|
||||
}
|
||||
const name = nameValue === undefined || b.name === null ? template.name : normalizedTemplateName(nameValue);
|
||||
const description = descriptionValue === undefined || b.description === null ? template.description : descriptionValue.trim();
|
||||
const goal = goalValue === undefined || b.goal === null ? template.goal : goalValue.trim();
|
||||
if (!name || !description || !goal) throw new Error("请填写模板名称、描述和目标");
|
||||
if (
|
||||
mockTaskTemplates.some(
|
||||
@@ -1353,9 +1409,25 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
|
||||
) {
|
||||
throw new Error("模板名称已存在");
|
||||
}
|
||||
const remark = has("remark") ? mockRemark(b.remark) : template.remark;
|
||||
const workerCount = has("worker_count") ? mockWorkerCount(b.worker_count) : template.worker_count;
|
||||
const hasName = has("name") && b.name !== null;
|
||||
const hasDescription = has("description") && b.description !== null;
|
||||
const hasGoal = has("goal") && b.goal !== null;
|
||||
const hasCategory = has("category_id");
|
||||
const hasRules = has("intercept_rules");
|
||||
const hasRemark = has("remark");
|
||||
const hasWorkerCount = has("worker_count");
|
||||
if (!hasName && !hasDescription && !hasGoal && !hasCategory && !hasRules && !hasRemark && !hasWorkerCount) {
|
||||
throw new Error("至少需要提供 name、description、goal、remark、category_id、intercept_rules 或 worker_count");
|
||||
}
|
||||
template.name = name;
|
||||
template.description = description;
|
||||
template.goal = goal;
|
||||
template.remark = remark;
|
||||
if (hasCategory) template.category_id = categoryID;
|
||||
if (hasRules) template.intercept_rules = Array.isArray(b.intercept_rules) ? b.intercept_rules : [];
|
||||
template.worker_count = workerCount;
|
||||
template.updated_at = new Date().toISOString();
|
||||
mockTaskTemplates.sort((a, b) => b.updated_at.localeCompare(a.updated_at) || b.id - a.id);
|
||||
return template;
|
||||
@@ -1374,12 +1446,34 @@ function route(m: string, path: string, seg: string[], q: URLSearchParams, b: Re
|
||||
if (seg[0] === "tasks" && seg.length === 2 && m === "PATCH") {
|
||||
const task = mockTasks.find((item) => item.id === seg[1]);
|
||||
if (!task) throw new Error("任务不存在");
|
||||
if (typeof b.name === "string") task.name = b.name.trim();
|
||||
if (typeof b.remark === "string") task.remark = b.remark.trim();
|
||||
if (typeof b.pinned === "boolean") {
|
||||
task.pinned = b.pinned;
|
||||
task.pinned_at = b.pinned ? (task.pinned_at ?? new Date().toISOString()) : null;
|
||||
const has = (key: string) => Object.prototype.hasOwnProperty.call(b, key);
|
||||
if (has("name") && b.name !== null && typeof b.name !== "string") throw new Error("任务名称必须是字符串");
|
||||
if (has("remark") && b.remark !== null && typeof b.remark !== "string") throw new Error("备注必须是字符串");
|
||||
if (has("pinned") && b.pinned !== null && typeof b.pinned !== "boolean") throw new Error("pinned 必须是布尔值");
|
||||
const namePresent = has("name") && b.name !== null;
|
||||
const remarkPresent = has("remark") && b.remark !== null;
|
||||
const pinnedPresent = has("pinned") && b.pinned !== null;
|
||||
if (!namePresent && !remarkPresent && !pinnedPresent) throw new Error("至少需要提供 name、pinned 或 remark");
|
||||
let nextName = task.name;
|
||||
let nextRemark = task.remark;
|
||||
let nextPinned = task.pinned;
|
||||
let nextPinnedAt = task.pinned_at;
|
||||
if (namePresent) {
|
||||
const name = (b.name as string).trim();
|
||||
if (!name) throw new Error("任务名称不能为空");
|
||||
if (Array.from(name).length > 200) throw new Error("任务名称最多 200 个字符");
|
||||
nextName = name;
|
||||
}
|
||||
if (remarkPresent) nextRemark = mockRemark(b.remark);
|
||||
if (pinnedPresent) {
|
||||
const pinned = b.pinned as boolean;
|
||||
nextPinned = pinned;
|
||||
nextPinnedAt = pinned ? (task.pinned_at ?? new Date().toISOString()) : null;
|
||||
}
|
||||
task.name = nextName;
|
||||
task.remark = nextRemark;
|
||||
task.pinned = nextPinned;
|
||||
task.pinned_at = nextPinnedAt;
|
||||
sortMockTasks();
|
||||
return structuredClone(task);
|
||||
}
|
||||
|
||||
@@ -4,10 +4,27 @@
|
||||
export type TaskStatus = "created" | "queued" | "running" | "paused" | "done" | "failed" | "timeout";
|
||||
export type EngineMode = "exploring" | "paused" | "stalled" | "idle";
|
||||
|
||||
// Keep these limits aligned with db.MaxTaskRemarkRunes and db.MaxTaskWorkerCount.
|
||||
export const MAX_TASK_REMARK_RUNES = 2000;
|
||||
export const MAX_TASK_WORKER_COUNT = 50;
|
||||
|
||||
// Parse the optional worker-count input without silently changing invalid values.
|
||||
// An empty field means 0 (use the global default); every other value must be an
|
||||
// integer in the backend's accepted range.
|
||||
export function parseWorkerCountInput(value: string): number | null {
|
||||
const trimmed = value.trim();
|
||||
if (trimmed === "") return 0;
|
||||
const parsed = Number(trimmed);
|
||||
if (!Number.isFinite(parsed) || !Number.isInteger(parsed)) return null;
|
||||
if (parsed < 0 || parsed > MAX_TASK_WORKER_COUNT) return null;
|
||||
return parsed;
|
||||
}
|
||||
|
||||
export interface Task {
|
||||
distinct_findings?: FindingDistinctStats;
|
||||
id: string;
|
||||
remark?: string;
|
||||
worker_count?: number; // 本任务 Worker 数;0/缺省=用全局默认
|
||||
name?: string; // 可选任务名称;空/缺省=未命名,展示时回退到描述
|
||||
category_id?: number;
|
||||
category_name?: string;
|
||||
@@ -56,8 +73,10 @@ export interface TaskTemplate {
|
||||
name: string;
|
||||
description: string;
|
||||
goal: string;
|
||||
remark?: string; // 预设备注
|
||||
category_id?: number | null; // 预设分类;null/缺省=无
|
||||
intercept_rules?: AssetInterceptRuleInput[]; // 预设的任务级拦截/允许规则
|
||||
worker_count?: number; // 预设 Worker 数;0/缺省=用全局默认
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user