diff --git a/src/features/agents/AgentsView.tsx b/src/features/agents/AgentsView.tsx index f1b0eec8d5..d648577b72 100644 --- a/src/features/agents/AgentsView.tsx +++ b/src/features/agents/AgentsView.tsx @@ -40,11 +40,14 @@ import { customSection, groupMembers, pinnedMembers, + projectGroups, + projectSection, sectionOfTask, statusGroups, statusSection, type OrganizeGroup, } from "./organize"; +import { setTaskListOptions, useTaskListOptions } from "./taskListOptions"; import { agentIcon, externalKey, @@ -78,9 +81,26 @@ import { basename, useProject } from "@/platform/project"; import { useTx } from "@/platform/i18n"; import { executeAction } from "@/platform/actions"; +/** The window's projects when it holds several (a workspace), else none. */ +function useWorkspaceProjects(): readonly string[] { + const projects = useProject((s) => s.project?.workspace?.projects); + return projects && projects.length > 1 ? projects : NO_PROJECTS; +} +const NO_PROJECTS: readonly string[] = []; + export function AgentsHeaderActions() { + const several = useWorkspaceProjects().length > 1; + const byProject = useTaskListOptions((s) => s.groupByProject); return ( <> + {several && ( + setTaskListOptions({ groupByProject: !byProject })} + /> + )} @@ -91,6 +111,7 @@ type Node = | { kind: "pinned"; tasks: AgentTask[] } | { kind: "custom"; group: OrganizeGroup; tasks: AgentTask[] } | { kind: "group"; group: TaskGroup } + | { kind: "project"; root: string; tasks: AgentTask[] } | { kind: "task"; task: AgentTask }; function nodeKey(n: Node): string { @@ -101,14 +122,18 @@ function nodeKey(n: Node): string { return customSection(n.group.id); case "group": return statusSection(n.group.status); + case "project": + return projectSection(n.root); case "task": return n.task.id; } } -/** Where a task shows: the pinned section first, else its group, else its status group. */ -function keysOf(task: AgentTask): string[] { - return [sectionOfTask(task, useAgents.getState().organize)]; +/** Where a task shows: the pinned section first, else its group, else its status group (or its project's). */ +function keysOf(task: AgentTask, rootOf: ((task: AgentTask) => string) | null): string[] { + const section = sectionOfTask(task, useAgents.getState().organize); + const kept = section === statusSection("archived") || section === statusSection("automated"); + return [rootOf && section.startsWith("g:") && !kept ? projectSection(rootOf(task)) : section]; } type DropIndicator = { kind: "section"; key: string } | { kind: "row"; key: string; position: "before" | "after" }; @@ -218,6 +243,20 @@ export function AgentsView() { }, [selected]); const organize = useAgents((s) => s.organize); const pinned = useMemo(() => new Set(organize.pinned), [organize.pinned]); + const workspaceProjects = useWorkspaceProjects(); + const groupByProject = useTaskListOptions((s) => s.groupByProject); + // Tasks list under their project only while the window holds several. + const rootOf = useMemo( + () => + groupByProject && workspaceProjects.length > 1 && project + ? (task: AgentTask) => task.projectRoot ?? project.root + : null, + [groupByProject, workspaceProjects, project], + ); + const rootOfRef = useRef(rootOf); + useEffect(() => { + rootOfRef.current = rootOf; + }, [rootOf]); const [query, setQuery] = useState(""); const treeRef = useRef(null); // Follow the active session tab. @@ -228,7 +267,7 @@ export function AgentsView() { if (!task) return; setSelected((current) => (current.has(task) ? current : new Set([task]))); const known = useAgents.getState().tasks.get(task); - const keys = known ? keysOf(known) : []; + const keys = known ? keysOf(known, rootOfRef.current) : []; if (keys.length) setCollapsed((c) => (keys.some((k) => c.has(k)) ? new Set([...c].filter((k) => !keys.includes(k))) : c)); }), @@ -256,9 +295,17 @@ export function AgentsView() { const members = groupMembers(all, organize, group.id); return matches && !members.length ? [] : [{ kind: "custom", group, tasks: members }]; }), - ...statusGroups(all, organize).map((group): Node => ({ kind: "group", group })), + ...(() => { + const groups = statusGroups(all, organize); + if (!rootOf) return groups.map((group): Node => ({ kind: "group", group })); + const split = projectGroups(groups, workspaceProjects, rootOf); + return [ + ...split.projects.map((p): Node => ({ kind: "project", ...p })), + ...split.rest.map((group): Node => ({ kind: "group", group })), + ]; + })(), ]; - }, [tasks, organize, matches, unread, automatedRunFilter, latestAutomated]); + }, [tasks, organize, matches, unread, automatedRunFilter, latestAutomated, rootOf, workspaceProjects]); const expanded = useMemo( () => new Set(roots.map(nodeKey).filter((k) => matches || !collapsed.has(k))), [roots, collapsed, matches], @@ -343,7 +390,8 @@ export function AgentsView() { apply: () => moveTasksToStatusGroup(movable.map((t) => t.id)), }; } - if (searchingRef.current) return null; + // Manual order is per status group; rows under a project keep its attention order. + if (searchingRef.current || rootOfRef.current) return null; const target = tasks.get(targetKey); if (!target || sourceIds.includes(target.id)) return null; const movable = sources.filter((t) => canReorderTask(t, target, organize)); @@ -601,7 +649,9 @@ export function AgentsView() { ? "Pinned" : n.kind === "custom" ? n.group.name - : n.group.label + : n.kind === "project" + ? basename(n.root) + : n.group.label } getChildren={(n) => n.kind === "task" @@ -661,6 +711,8 @@ export function AgentsView() { return Pinned} count={n.tasks.length} />; case "custom": return ; + case "project": + return ; case "group": return ( )} - {project?.workspace && ( + {project?.workspace && !rootOf && ( {basename(n.task.projectRoot ?? project.root)} diff --git a/src/features/agents/organize.test.ts b/src/features/agents/organize.test.ts index 70f4607ebc..34f7d349c0 100644 --- a/src/features/agents/organize.test.ts +++ b/src/features/agents/organize.test.ts @@ -9,6 +9,7 @@ import { loadOrganize, parseOrganize, pinnedMembers, + projectGroups, pruneOrganize, reorderGroupsInOrganize, reorderTaskInOrganize, @@ -269,3 +270,27 @@ describe("task organize", () => { expect(reorderTaskInOrganize(EMPTY_ORGANIZE, all, "m-ready", "a-newer", "before")).toBe(EMPTY_ORGANIZE); }); }); + +describe("grouping by project", () => { + it("lists each project's active tasks in attention order and keeps archived and automated groups", () => { + const root: Record = { a: "/w/api", b: "/w/web", c: "/w/api", d: "/w/api", e: "/other" }; + const tasks = [ + task("a", "ready", 3), + task("b", "running", 2), + task("c", "needsInput", 1), + task("d", "archived", 5), + task("e", "ready", 4), + ]; + const split = projectGroups( + statusGroups(tasks, EMPTY_ORGANIZE), + ["/w/web", "/w/api", "/w/empty"], + (t) => root[t.id]!, + ); + expect(split.projects.map((p) => [p.root, p.tasks.map((t) => t.id)])).toEqual([ + ["/w/web", ["b"]], + ["/w/api", ["c", "a"]], + ["/other", ["e"]], + ]); + expect(split.rest.map((g) => [g.status, g.tasks.map((t) => t.id)])).toEqual([["archived", ["d"]]]); + }); +}); diff --git a/src/features/agents/organize.ts b/src/features/agents/organize.ts index 804256fbd9..fef0bc37f3 100644 --- a/src/features/agents/organize.ts +++ b/src/features/agents/organize.ts @@ -375,3 +375,33 @@ export function reorderGroupsInOrganize( }); return { ...organize, groups }; } + +/** Section of a project's tasks when the list is grouped by project. */ +export const projectSection = (root: string) => `p:${root}`; + +/** + * Status groups regrouped by project: each project of `projects` (then any other root a task names) + * gets the tasks of the active status groups in attention order; the archived and automated groups + * stay as they are. + */ +export function projectGroups( + groups: readonly TaskGroup[], + projects: readonly string[], + rootOf: (task: AgentTask) => string, +): { projects: { root: string; tasks: AgentTask[] }[]; rest: TaskGroup[] } { + const kept = (g: TaskGroup) => g.status === "archived" || g.status === "automated"; + const byRoot = new Map(projects.map((root) => [root, []])); + for (const group of groups) { + if (kept(group)) continue; + for (const task of group.tasks) { + const root = rootOf(task); + const list = byRoot.get(root); + if (list) list.push(task); + else byRoot.set(root, [task]); + } + } + return { + projects: [...byRoot].filter(([, tasks]) => tasks.length).map(([root, tasks]) => ({ root, tasks })), + rest: groups.filter(kept), + }; +} diff --git a/src/features/agents/taskListOptions.ts b/src/features/agents/taskListOptions.ts new file mode 100644 index 0000000000..1ce1cee03e --- /dev/null +++ b/src/features/agents/taskListOptions.ts @@ -0,0 +1,31 @@ +/** Agents task list view options, persisted across sessions. */ +import { create } from "zustand"; + +export interface TaskListOptions { + /** With several projects in the window, tasks are listed under their project instead of their status. */ + groupByProject: boolean; +} + +const KEY = "openidea.agents.taskList.v1"; + +function load(): TaskListOptions { + try { + const raw = localStorage.getItem(KEY); + const o = raw ? (JSON.parse(raw) as Record) : {}; + return { groupByProject: o.groupByProject === true }; + } catch { + return { groupByProject: false }; + } +} + +export const useTaskListOptions = create(() => load()); + +/** Updates the options and persists them. */ +export function setTaskListOptions(patch: Partial) { + useTaskListOptions.setState(patch); + try { + localStorage.setItem(KEY, JSON.stringify(useTaskListOptions.getState())); + } catch { + // Storage unavailable: the options last for this session. + } +} diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 674f553b3f..232dc2f21b 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -1,4 +1,5 @@ { + "Group by Project": "Group by Project", "Copy string concatenation text to the clipboard": "Copy string concatenation text to the clipboard", "Copy string literal text to the clipboard": "Copy string literal text to the clipboard", "GitHub Issues": "GitHub Issues", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 87d00ad9fb..7decb5b59a 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -1,4 +1,5 @@ { + "Group by Project": "按项目分组", "Copy string concatenation text to the clipboard": "将字符串拼接文本复制到剪贴板", "Copy string literal text to the clipboard": "将字符串字面量文本复制到剪贴板", "GitHub Issues": "GitHub 议题",