BotGroupDeliveryAction.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { DeliveryContext } from "@/v4/botGroupDeliveryContext.js";
  2. import { useContext, useMemo, type ReactNode } from "react";
  3. import { CircleAlertIcon } from "lucide-react";
  4. import type { BotGroupDelivery } from "@zcode/shared";
  5. import { Button } from "@/components/ui/button.js";
  6. import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover.js";
  7. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  8. import { useBotGroupTask } from "@/hooks/useBotGroupTask.js";
  9. import { useBotGroupMemberNames } from "@/hooks/useBotGroupMemberNames.js";
  10. import { BotGroupMemberNamesContext } from "@/v4/BotGroupMemberNamesContext.js";
  11. export function BotGroupDeliveryProvider({
  12. taskId,
  13. workspacePath,
  14. workspaceIdentity,
  15. children,
  16. }: {
  17. taskId: string | null;
  18. workspacePath: string;
  19. workspaceIdentity?: string;
  20. children: ReactNode;
  21. }) {
  22. // 同一任务只订阅一次群状态,历史回复不各自请求全量 Bot 状态。
  23. const { context, provider, busyId, retry, reconcile, retryPreparation } = useBotGroupTask(
  24. taskId,
  25. workspacePath,
  26. workspaceIdentity,
  27. );
  28. const memberNames = useBotGroupMemberNames(context);
  29. const value = useMemo(
  30. () => ({
  31. context,
  32. provider,
  33. busyId,
  34. retry,
  35. reconcile,
  36. retryPreparation,
  37. taskId,
  38. }),
  39. [context, provider, busyId, retry, reconcile, retryPreparation, taskId],
  40. );
  41. return (
  42. <DeliveryContext.Provider value={value}>
  43. <BotGroupMemberNamesContext.Provider value={memberNames}>
  44. {children}
  45. </BotGroupMemberNamesContext.Provider>
  46. </DeliveryContext.Provider>
  47. );
  48. }
  49. export function BotGroupDeliveryAction({
  50. turnId,
  51. sourceCommandIds,
  52. syncing,
  53. results,
  54. busyId,
  55. onRetry,
  56. onReconcile,
  57. }: {
  58. turnId: string;
  59. sourceCommandIds: readonly string[];
  60. syncing: boolean;
  61. results: BotGroupDelivery[];
  62. busyId?: string | null;
  63. onRetry: (id: string) => void;
  64. onReconcile: (id: string, received: boolean) => void;
  65. }) {
  66. const { intl } = useZCodeIntl();
  67. // Bug 原因:任务级恢复面板与队列浮层重叠,还重复展示正文;只在已关联轮次的操作栏展示。
  68. const unresolved = results.filter(
  69. (result) =>
  70. result.sourceCommandId &&
  71. sourceCommandIds.includes(result.sourceCommandId) &&
  72. (result.status === "failed" || result.status === "unknown"),
  73. );
  74. if (!unresolved.length) return null;
  75. const label = intl.formatMessage({
  76. id: unresolved.some((result) => result.status === "unknown")
  77. ? "bots.group.deliveryUnknown"
  78. : "bots.group.deliveryFailed",
  79. });
  80. return (
  81. <Popover>
  82. <PopoverTrigger asChild>
  83. <Button
  84. variant="ghost"
  85. size="icon-xs"
  86. className="text-warning"
  87. aria-label={label}
  88. title={label}
  89. data-bot-group-delivery={turnId}
  90. >
  91. <CircleAlertIcon className="size-3.5" />
  92. </Button>
  93. </PopoverTrigger>
  94. <PopoverContent
  95. align="end"
  96. className="max-w-[calc(100vw-2rem)] gap-2"
  97. data-bot-group-delivery-actions={turnId}
  98. >
  99. {unresolved.map((result, index) => (
  100. <div key={result.id} className="flex flex-col gap-1">
  101. <span className="text-ui-sm text-foreground-subtle">
  102. {intl.formatMessage({
  103. id:
  104. result.status === "unknown"
  105. ? "bots.group.deliveryUnknown"
  106. : "bots.group.deliveryFailed",
  107. })}
  108. {unresolved.length > 1 ? ` (${index + 1}/${unresolved.length})` : null}
  109. </span>
  110. {syncing ? (
  111. <div className="flex flex-wrap gap-1">
  112. {result.status === "unknown" ? (
  113. <>
  114. <Button
  115. variant="ghost"
  116. size="xs"
  117. disabled={busyId === result.id}
  118. onClick={() => onReconcile(result.id, true)}
  119. >
  120. {intl.formatMessage({ id: "bots.group.confirmReceived" })}
  121. </Button>
  122. <Button
  123. variant="ghost"
  124. size="xs"
  125. disabled={busyId === result.id}
  126. onClick={() => onReconcile(result.id, false)}
  127. >
  128. {intl.formatMessage({ id: "bots.group.confirmNotReceived" })}
  129. </Button>
  130. </>
  131. ) : (
  132. <Button
  133. variant="ghost"
  134. size="xs"
  135. disabled={busyId === result.id}
  136. onClick={() => onRetry(result.id)}
  137. >
  138. {intl.formatMessage({ id: "bots.group.resend" })}
  139. </Button>
  140. )}
  141. </div>
  142. ) : null}
  143. </div>
  144. ))}
  145. </PopoverContent>
  146. </Popover>
  147. );
  148. }
  149. export function ConnectedBotGroupDeliveryAction({
  150. turnId,
  151. sourceCommandIds,
  152. }: {
  153. turnId: string;
  154. sourceCommandIds: readonly string[];
  155. }) {
  156. const value = useContext(DeliveryContext);
  157. const context = value?.context;
  158. if (!value || !context?.group) return null;
  159. return (
  160. <BotGroupDeliveryAction
  161. turnId={turnId}
  162. sourceCommandIds={sourceCommandIds}
  163. syncing={context.group.enabled && context.activeTaskId === value.taskId}
  164. results={Object.values(context.group.deliveries ?? {}).filter(
  165. (result) => result.taskId === value.taskId,
  166. )}
  167. busyId={value.busyId}
  168. onRetry={(id) => {
  169. void value.retry(id);
  170. }}
  171. onReconcile={(id, received) => {
  172. void value.reconcile(id, received);
  173. }}
  174. />
  175. );
  176. }