feat(agents): Select the active session in the Agents list (#140)

Switching between agent session tabs in the editor now moves the Agents
tool window's selection to that task, expanding its group (e.g. Archived)
when it is collapsed. The list also opens with the active session selected.
Tabs of other kinds leave the selection as it is.

The editor service gains activeInput() and onActiveInputChange() so
features can follow the active tab of any kind.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-10-28 20:36:55 +08:00
1 джерело f8b3ef6840
коміт 44b2d11b2d
4 files changed
+43 -3

No files matched your search

+23 -3
Переглянути файл
@@ -1,10 +1,11 @@
/** Agents tool window: tasks grouped by what they need from the user. */
import { useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import type { AgentStatus as Status, AgentTask } from "@/bindings";
import { executeAction } from "@/platform/actions";
import { maybeEditorService, type EditorInput } from "@/workbench/contributions";
import { confirmChecked } from "@/workbench/messages";
import { EmptyState, Icon, IconButton, Spinner, T, Tree, showContextMenu, type MenuItem } from "@/ui";
import { age, branchLabel, groupTasks, isBusy, type TaskGroup } from "./model";
import { SESSION_KIND, age, branchLabel, groupTasks, isBusy, type TaskGroup } from "./model";
import { archiveTask, deleteTask, openHome, openTask, useAgents } from "./store";
import styles from "./Agents.module.css";
import { tx } from "@/platform/i18n";
@@ -17,6 +18,9 @@ type Node = { kind: "group"; group: TaskGroup } | { kind: "task"; task: AgentTas
const nodeKey = (n: Node) => (n.kind === "group" ? `g:${n.group.status}` : n.task.id);
const sessionTask = (input: EditorInput | null | undefined) =>
input?.kind === SESSION_KIND ? (input.data as { task?: string } | undefined)?.task : undefined;
export function TaskStatusIcon({ status }: { status: Status }) {
switch (status) {
case "needsInput":
@@ -39,7 +43,23 @@ export function AgentsView() {
const tasks = useAgents((s) => s.tasks);
const providers = useAgents((s) => s.providers);
const [collapsed, setCollapsed] = useState<Set<string>>(() => new Set(["g:archived"]));
const [selected, setSelected] = useState<Set<string>>(() => new Set());
const [selected, setSelected] = useState<Set<string>>(() => {
const task = sessionTask(maybeEditorService()?.activeInput?.());
return new Set(task ? [task] : []);
});
// Follow the active session tab.
useEffect(
() =>
maybeEditorService()?.onActiveInputChange?.((input) => {
const task = sessionTask(input);
if (!task) return;
setSelected(new Set([task]));
const group = groupTasks(useAgents.getState().tasks.values()).find((g) => g.tasks.some((t) => t.id === task));
const key = group && `g:${group.status}`;
if (key) setCollapsed((c) => (c.has(key) ? new Set([...c].filter((k) => k !== key)) : c));
}),
[],
);
const roots = useMemo<Node[]>(() => groupTasks(tasks.values()).map((group) => ({ kind: "group", group })), [tasks]);
const expanded = useMemo(() => new Set(roots.map(nodeKey).filter((k) => !collapsed.has(k))), [roots, collapsed]);
if (!loaded)