"use client"; import * as React from "react"; import { ListTodo } from "lucide-react"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { cn } from "@/lib/utils"; // TodoPopover shows the latest TodoWrite state of a session (chat conversation or // task worker/planner replay). The full todo list isn't in the activity list // (detail is lazy), so on open it fetches the detail of the most-recent TodoWrite // call — by seq — and parses its {todos:[…]} JSON. Purely client-side; the caller // supplies the seq + a detail fetcher (conversation or exploration endpoint). export function TodoPopover({ seq, fetchDetail, }: { seq: number | null; fetchDetail: (seq: number) => Promise; }) { const [open, setOpen] = React.useState(false); const [todos, setTodos] = React.useState<{ content: string; status: string }[] | null>(null); const [loading, setLoading] = React.useState(false); const [err, setErr] = React.useState(""); const load = React.useCallback(async () => { if (seq == null) return; setLoading(true); setErr(""); try { const detail = await fetchDetail(seq); const start = detail.indexOf("{"); // detail 可能带 "TodoWrite " 前缀 const parsed = JSON.parse(start >= 0 ? detail.slice(start) : detail); setTodos(Array.isArray(parsed?.todos) ? parsed.todos : []); } catch { setErr("解析 Todo 失败"); setTodos(null); } finally { setLoading(false); } }, [seq, fetchDetail]); // refetch on each open — todos change as the run progresses. React.useEffect(() => { if (open) load(); }, [open, load]); const disabled = seq == null; const MARK: Record = { pending: "☐", in_progress: "▶", completed: "✔" }; return (

最近 Todo{loading ? " · 加载中…" : ""}

{err &&

{err}

} {todos && todos.length === 0 && !loading && (

(空)

)}
    {(todos ?? []).map((t, i) => (
  • {MARK[t.status] ?? "☐"} {t.content}
  • ))}
); }