conversationShareSelectionStore.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. import { createConversationSharePopoverActions } from "@/conversation-share-interaction/conversationSharePopoverActions.js";
  2. import { create } from "zustand";
  3. import type { ConversationShareAccessMode, ConversationShareRejection } from "@zcode/shared";
  4. import type { ConversationShareFailureIssue } from "@zcode/services";
  5. export const DEFAULT_CONVERSATION_SHARE_ACCESS_MODE: ConversationShareAccessMode =
  6. "public_importable";
  7. export interface ConversationShareAttempt {
  8. key: string;
  9. clientRequestId: string;
  10. disclosureAcceptedAt: number;
  11. }
  12. export interface ConversationShareDisplayError {
  13. issues: readonly ConversationShareFailureIssue[];
  14. issueCount: number;
  15. omittedIssueCount?: number;
  16. requestId?: string;
  17. clientRequestId?: string;
  18. operationId?: string;
  19. status?: number;
  20. code?: number;
  21. rejection?: ConversationShareRejection;
  22. /** 没有服务端 issues 时展示的、按错误类型解析出的可行动文案。 */
  23. messageId?: string;
  24. /** 限流时服务端建议的等待秒数,用于“请 N 秒后重试”。 */
  25. retryAfterSeconds?: number;
  26. }
  27. /** 发布成功但有结果物被跳过时的非阻断提示。 */
  28. export interface ConversationShareDisplayWarnings {
  29. issues: readonly ConversationShareFailureIssue[];
  30. issueCount: number;
  31. omittedIssueCount?: number;
  32. }
  33. export type ConversationShareProgressPhase = "collecting" | "uploading" | "checking";
  34. export interface ConversationShareSelectionDraft {
  35. open?: boolean;
  36. pendingExcludedRowIds?: readonly number[] | null;
  37. availableRowIds: readonly number[];
  38. excludedRowIds: readonly number[];
  39. productTurnIdByRowId: Readonly<Record<number, string>>;
  40. accessMode: ConversationShareAccessMode;
  41. /** 用户选择的分享有效时长;null 表示使用服务端默认值。与权限一样跨轮保留。 */
  42. ttlMs: number | null;
  43. }
  44. export interface ConversationShareDockState {
  45. title?: string;
  46. disclosureAccepted: boolean;
  47. publishing: boolean;
  48. progress: ConversationShareProgressPhase;
  49. completedArtifacts: number;
  50. totalArtifacts: number;
  51. publishedShareUrl: string | null;
  52. error: ConversationShareDisplayError | null;
  53. warnings: ConversationShareDisplayWarnings | null;
  54. attempt: ConversationShareAttempt | null;
  55. }
  56. export interface ConversationShareSelectionState {
  57. openShare: (taskId: string) => void;
  58. closeShare: (taskId: string) => void;
  59. beginSelectionEdit: (taskId: string) => void;
  60. togglePendingRow: (taskId: string, rowId: number) => void;
  61. setAllPendingRowsSelected: (taskId: string, selected: boolean) => void;
  62. confirmSelectionEdit: (taskId: string) => void;
  63. cancelSelectionEdit: (taskId: string) => void;
  64. drafts: Record<string, ConversationShareSelectionDraft>;
  65. dockStates: Record<string, ConversationShareDockState>;
  66. setAccessMode: (taskId: string, accessMode: ConversationShareAccessMode) => void;
  67. setTtlMs: (taskId: string, ttlMs: number | null) => void;
  68. syncAvailableTurns: (
  69. taskId: string,
  70. turns: readonly { rowId: number; productTurnId: string }[],
  71. ) => void;
  72. /** 按 product turn 身份整轮取消选择(阻断项的「取消选择该轮」入口)。 */
  73. deselectProductTurn: (taskId: string, productTurnId: string) => void;
  74. updateDockState: (taskId: string, patch: Partial<ConversationShareDockState>) => void;
  75. resetForTests: () => void;
  76. }
  77. const EMPTY_DRAFT: ConversationShareSelectionDraft = Object.freeze({
  78. availableRowIds: Object.freeze([]),
  79. excludedRowIds: Object.freeze([]),
  80. productTurnIdByRowId: Object.freeze({}),
  81. accessMode: DEFAULT_CONVERSATION_SHARE_ACCESS_MODE,
  82. ttlMs: null,
  83. });
  84. export const DEFAULT_CONVERSATION_SHARE_DOCK_STATE: ConversationShareDockState = Object.freeze({
  85. disclosureAccepted: false,
  86. publishing: false,
  87. progress: "collecting",
  88. completedArtifacts: 0,
  89. totalArtifacts: 0,
  90. publishedShareUrl: null,
  91. error: null,
  92. warnings: null,
  93. attempt: null,
  94. });
  95. function normalizeRowIds(rowIds: readonly number[]): number[] {
  96. return [...new Set(rowIds.filter(Number.isSafeInteger))].sort((left, right) => left - right);
  97. }
  98. export function getConversationShareDraft(
  99. state: Pick<ConversationShareSelectionState, "drafts">,
  100. taskId: string,
  101. ): ConversationShareSelectionDraft {
  102. return state.drafts[taskId] ?? EMPTY_DRAFT;
  103. }
  104. export function getConversationShareDockState(
  105. state: Pick<ConversationShareSelectionState, "dockStates">,
  106. taskId: string,
  107. ): ConversationShareDockState {
  108. return state.dockStates[taskId] ?? DEFAULT_CONVERSATION_SHARE_DOCK_STATE;
  109. }
  110. export function getConversationShareSelectedRowIds(
  111. state: Pick<ConversationShareSelectionState, "drafts">,
  112. taskId: string,
  113. ): number[] {
  114. const draft = getConversationShareDraft(state, taskId);
  115. const excluded = new Set(draft.excludedRowIds);
  116. return draft.availableRowIds.filter((rowId) => !excluded.has(rowId));
  117. }
  118. export function getConversationShareSelectedProductTurnIds(
  119. state: Pick<ConversationShareSelectionState, "drafts">,
  120. taskId: string,
  121. source: "confirmed" | "pending" = "confirmed",
  122. ): string[] {
  123. const draft = getConversationShareDraft(state, taskId);
  124. const seen = new Set<string>();
  125. const excluded = new Set(
  126. source === "pending"
  127. ? (draft.pendingExcludedRowIds ?? draft.excludedRowIds)
  128. : draft.excludedRowIds,
  129. );
  130. return draft.availableRowIds
  131. .filter((rowId) => !excluded.has(rowId))
  132. .flatMap((rowId) => {
  133. const productTurnId = draft.productTurnIdByRowId[rowId];
  134. if (!productTurnId || seen.has(productTurnId)) return [];
  135. seen.add(productTurnId);
  136. return [productTurnId];
  137. });
  138. }
  139. export const useConversationShareSelectionStore = create<ConversationShareSelectionState>()(
  140. (set) => ({
  141. ...createConversationSharePopoverActions(
  142. set,
  143. getConversationShareDraft,
  144. getConversationShareDockState,
  145. DEFAULT_CONVERSATION_SHARE_DOCK_STATE,
  146. ),
  147. drafts: {},
  148. dockStates: {},
  149. setAccessMode: (taskId, accessMode) =>
  150. set((state) => ({
  151. drafts: {
  152. ...state.drafts,
  153. [taskId]: {
  154. ...(state.drafts[taskId] ?? EMPTY_DRAFT),
  155. accessMode,
  156. },
  157. },
  158. })),
  159. setTtlMs: (taskId, ttlMs) =>
  160. set((state) => ({
  161. drafts: {
  162. ...state.drafts,
  163. [taskId]: {
  164. ...(state.drafts[taskId] ?? EMPTY_DRAFT),
  165. ttlMs,
  166. },
  167. },
  168. })),
  169. syncAvailableTurns: (taskId, turns) =>
  170. set((state) => {
  171. const current = getConversationShareDraft(state, taskId);
  172. if (!current.open) return state;
  173. const productTurnIdByRowId = Object.fromEntries(
  174. turns.map((turn) => [turn.rowId, turn.productTurnId]),
  175. );
  176. const availableRowIds = normalizeRowIds(turns.map((turn) => turn.rowId));
  177. const available = new Set(availableRowIds);
  178. const nextDraft = {
  179. ...current,
  180. availableRowIds,
  181. excludedRowIds: current.excludedRowIds.filter((rowId) => available.has(rowId)),
  182. productTurnIdByRowId,
  183. };
  184. // 披露确认绑定有效分享范围;重开或运行轮次完成后新增内容,不能沿用旧确认。
  185. const before = new Set(getConversationShareSelectedProductTurnIds(state, taskId));
  186. const after = getConversationShareSelectedProductTurnIds(
  187. { drafts: { [taskId]: nextDraft } },
  188. taskId,
  189. );
  190. const changed = before.size !== after.length || after.some((id) => !before.has(id));
  191. return {
  192. drafts: { ...state.drafts, [taskId]: nextDraft },
  193. ...(changed
  194. ? {
  195. dockStates: {
  196. ...state.dockStates,
  197. [taskId]: {
  198. ...getConversationShareDockState(state, taskId),
  199. disclosureAccepted: false,
  200. },
  201. },
  202. }
  203. : {}),
  204. };
  205. }),
  206. updateDockState: (taskId, patch) =>
  207. set((state) => ({
  208. dockStates: {
  209. ...state.dockStates,
  210. [taskId]: {
  211. ...getConversationShareDockState(state, taskId),
  212. ...patch,
  213. },
  214. },
  215. })),
  216. resetForTests: () =>
  217. set({
  218. drafts: {},
  219. dockStates: {},
  220. }),
  221. }),
  222. );