TodoPanel.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { useState } from "react";
  2. import type { ZCodePlanStep } from "@zcode/shared";
  3. import { Button } from "@/components/ui/button.js";
  4. import {
  5. Plan,
  6. PlanAction,
  7. PlanContent,
  8. PlanDescription,
  9. PlanHeader,
  10. PlanTitle,
  11. } from "@/components/ai-elements/plan.js";
  12. import {
  13. QueueItem,
  14. QueueItemContent,
  15. QueueItemIndicator,
  16. QueueList,
  17. } from "@/components/ai-elements/queue.js";
  18. import {
  19. ChevronsUpDownIcon,
  20. ListTodo,
  21. LoaderIcon,
  22. } from "lucide-react";
  23. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  24. import { ControlHintTooltip } from "@/ControlHintTooltip.js";
  25. import { cn } from "./components/lib/utils.js";
  26. const todoStatusIndicatorClasses: Record<ZCodePlanStep["status"], string> = {
  27. pending: "text-foreground-subtlest",
  28. in_progress: "text-foreground",
  29. completed: "text-success",
  30. };
  31. const todoStatusContentClassesStreaming: Record<ZCodePlanStep["status"], string> = {
  32. pending: "text-foreground-subtle",
  33. in_progress: "animated-gradient-text",
  34. completed: "text-foreground-subtlest line-through",
  35. };
  36. // Bugfix: 任务停止/结束后 plan 可能还留着 in_progress 项,如果继续套 animated-gradient-text,
  37. // 面板就会一直显示 loading 态。非运行期降级为普通文本,仅保留状态色。
  38. const todoStatusContentClassesIdle: Record<ZCodePlanStep["status"], string> = {
  39. pending: "text-foreground-subtle",
  40. in_progress: "text-foreground",
  41. completed: "text-foreground-subtlest line-through",
  42. };
  43. export function TodoPanel({
  44. plan,
  45. isStreaming,
  46. }: {
  47. plan: ZCodePlanStep[] | null;
  48. isStreaming: boolean;
  49. }) {
  50. const { intl } = useZCodeIntl();
  51. const [isOpen, setIsOpen] = useState(false);
  52. if (!plan || plan.length === 0) {
  53. return null;
  54. }
  55. const completedCount = plan.filter(
  56. (step) => step.status === "completed",
  57. ).length;
  58. const isAllCompleted = completedCount === plan.length;
  59. // Bugfix: provider 可能先把 todo 全部标成 completed,再稍后才推 task_complete 清空 store。
  60. // 这段间隙继续展示“已完成 todo”会让用户误以为还有待处理事项,所以全完成后直接隐藏面板。
  61. if (isAllCompleted) {
  62. return null;
  63. }
  64. // Bugfix: 展开态之前会把 completed 项继续画出来,只是加删除线;完成项越多面板越长。
  65. // 这里仅保留未完成项,让 todo 面板专注显示还需要关注的后续工作。
  66. const visiblePlan = plan.filter((step) => step.status !== "completed");
  67. const activeStep =
  68. plan.find((step) => step.status === "in_progress") ??
  69. plan.find((step) => step.status !== "completed") ??
  70. plan[plan.length - 1];
  71. // Bugfix: loading 视觉只在任务真正运行期出现。task 已完成/失败/被停止时,
  72. // 即便 plan 仍残留 in_progress 项,也不再转菊花或做渐变动画。
  73. const showLoadingState = isStreaming && !isAllCompleted;
  74. const todoStatusContentClasses = isStreaming
  75. ? todoStatusContentClassesStreaming
  76. : todoStatusContentClassesIdle;
  77. const toggleLabel = intl.formatMessage({ id: "todo.panel.toggle" });
  78. const toggleOpen = () => setIsOpen((current) => !current);
  79. return (
  80. <Plan
  81. className="gap-0 overflow-hidden rounded-none border-0 bg-popover py-0"
  82. open={isOpen}
  83. onOpenChange={setIsOpen}
  84. isStreaming={isStreaming}
  85. >
  86. {/* 修复原因:PlanHeader 负责撑满布局,不能再承担点击展开,否则空白区域也会误触。
  87. 展开逻辑收敛到内容宽度按钮和右侧图标按钮,保留键盘与读屏状态。 */}
  88. <PlanHeader
  89. className={cn(
  90. "group flex items-center gap-3 px-4 !py-3",
  91. isOpen && "!pb-1",
  92. )}
  93. >
  94. <div className="flex min-w-0 flex-1 items-center">
  95. <button
  96. type="button"
  97. aria-expanded={isOpen}
  98. className="inline-flex min-w-0 max-w-full cursor-pointer items-center gap-3 rounded-md p-1 text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-input-border-focused"
  99. onClick={toggleOpen}
  100. >
  101. {isOpen ? (
  102. <>
  103. <ListTodo className="size-4 shrink-0 text-foreground" />
  104. <PlanTitle className="min-w-0 truncate text-ui-base font-medium text-foreground">
  105. {intl.formatMessage({ id: "todo.panel.title" })}
  106. </PlanTitle>
  107. <PlanDescription className="shrink-0 text-ui-base font-mono text-foreground-subtlest">
  108. {`${completedCount}/${plan.length}`}
  109. </PlanDescription>
  110. </>
  111. ) : showLoadingState ? (
  112. <>
  113. <LoaderIcon className="size-4 shrink-0 animate-spin text-foreground" />
  114. <PlanTitle className="animated-gradient-text min-w-0 truncate text-ui-base font-medium">
  115. {activeStep?.title ??
  116. intl.formatMessage({ id: "todo.panel.currentTask" })}
  117. </PlanTitle>
  118. </>
  119. ) : (
  120. <>
  121. <ListTodo className="size-4 shrink-0 text-foreground" />
  122. <PlanTitle className="min-w-0 truncate text-ui-base font-medium text-foreground">
  123. {activeStep?.title ??
  124. intl.formatMessage({ id: "todo.panel.currentTask" })}
  125. </PlanTitle>
  126. </>
  127. )}
  128. {!isOpen ? (
  129. <PlanDescription className="shrink-0 text-ui-base font-mono text-foreground-subtle">
  130. {`${completedCount}/${plan.length}`}
  131. </PlanDescription>
  132. ) : null}
  133. </button>
  134. </div>
  135. <PlanAction className="shrink-0">
  136. <ControlHintTooltip
  137. title={intl.formatMessage({
  138. id: isOpen ? "chat.longRunning.collapse" : "chat.longRunning.expand",
  139. })}
  140. >
  141. <Button
  142. type="button"
  143. size="icon"
  144. variant="ghost"
  145. aria-expanded={isOpen}
  146. aria-label={toggleLabel}
  147. onClick={(event) => {
  148. event.stopPropagation();
  149. toggleOpen();
  150. }}
  151. >
  152. <ChevronsUpDownIcon className="size-4" />
  153. <span className="sr-only">{toggleLabel}</span>
  154. </Button>
  155. </ControlHintTooltip>
  156. </PlanAction>
  157. </PlanHeader>
  158. <PlanContent className="p-1">
  159. <QueueList className="mt-0">
  160. {visiblePlan.map((step) => (
  161. <QueueItem
  162. key={step.id}
  163. className="gap-0 w-full rounded-xl px-3 py-2 hover:bg-hover/30"
  164. >
  165. <div className="flex min-w-0 items-center gap-3">
  166. <QueueItemIndicator
  167. className={todoStatusIndicatorClasses[step.status]}
  168. status={step.status}
  169. />
  170. <QueueItemContent
  171. className={`min-w-0 line-clamp-none ${todoStatusContentClasses[step.status]}`}
  172. completed={false}
  173. >
  174. {step.title}
  175. </QueueItemContent>
  176. </div>
  177. </QueueItem>
  178. ))}
  179. </QueueList>
  180. </PlanContent>
  181. </Plan>
  182. );
  183. }