conversation-session-store.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { readTaskStoreSnapshotInBrowser } from "./conversation-session-store-snapshot-script.js";
  2. export interface TaskStoreSnapshot {
  3. activeInputId: string | null;
  4. activeTaskId: string | null;
  5. activeTaskConfigOptions: ConfigOptionSnapshot[];
  6. error: string | null;
  7. hasPendingPermission: boolean;
  8. knownWorkspaceKeys: string[];
  9. lastSelectedModelStorage: Record<string, string | null>;
  10. messageCount: number;
  11. selectedProvider: string | null;
  12. selectedSupplierKey: string | null;
  13. queueCount: number;
  14. queuedTexts: string[];
  15. runtimeStatus: string | null;
  16. stopRequested: boolean;
  17. streamingAssistantCount: number;
  18. taskConfigOptions: ConfigOptionSnapshot[];
  19. taskId: string;
  20. taskMeta: {
  21. model: string | null;
  22. provider: string | null;
  23. targetId: string | null;
  24. targetObjective: string | null;
  25. targetStatus: string | null;
  26. title: string | null;
  27. } | null;
  28. timelineSummaries: TimelineSummarySnapshot[];
  29. uiError: string | null;
  30. workspaceConfigOptions: ConfigOptionSnapshot[];
  31. workspaceKey: string | null;
  32. }
  33. export interface V4ComposerDraftScopeSnapshot {
  34. exists: boolean;
  35. mode: string | null;
  36. modelSelection: {
  37. providerId: string;
  38. modelId: string;
  39. reasoningLevel: string | null;
  40. } | null;
  41. scopeId: string;
  42. text: string;
  43. }
  44. export interface ConfigOptionSnapshot {
  45. category: string | null;
  46. currentValue: string | null;
  47. id: string | null;
  48. optionValues: string[];
  49. status?: string | null;
  50. type: string | null;
  51. }
  52. export interface TimelineSummarySnapshot {
  53. goalIteration: number | null;
  54. id: string | null;
  55. inputId: string | null;
  56. operationId: string | null;
  57. status: string | null;
  58. targetId: string | null;
  59. type: string | null;
  60. verificationPassed: boolean | null;
  61. }
  62. /** 读取当前 v4 Composer 的唯一持久 Draft;不再探测已删除的 Zustand 草稿镜像。 */
  63. export function getV4ComposerDraftScopeSnapshot(
  64. workspacePath: string,
  65. taskId: string | null,
  66. workspaceIdentity?: string,
  67. ): Promise<V4ComposerDraftScopeSnapshot> {
  68. return browser.execute(
  69. (workspacePathArg, taskIdArg, workspaceIdentityArg) => {
  70. const scopeId = taskIdArg ?? "__draft__";
  71. const workspaceKey = workspaceIdentityArg?.trim() || workspacePathArg;
  72. const storageKey = `zcode-v4-composer-drafts:v1:${encodeURIComponent(workspaceKey)}`;
  73. let draft: Record<string, unknown> | null = null;
  74. try {
  75. const raw = window.localStorage.getItem(storageKey);
  76. if (raw) {
  77. const parsed = JSON.parse(raw) as {
  78. scopes?: Record<string, Record<string, unknown>>;
  79. };
  80. draft = parsed.scopes?.[scopeId] ?? null;
  81. }
  82. } catch {
  83. draft = null;
  84. }
  85. const selection =
  86. draft?.modelSelection &&
  87. typeof draft.modelSelection === "object" &&
  88. !Array.isArray(draft.modelSelection)
  89. ? (draft.modelSelection as Record<string, unknown>)
  90. : null;
  91. const options =
  92. selection?.options &&
  93. typeof selection.options === "object" &&
  94. !Array.isArray(selection.options)
  95. ? (selection.options as Record<string, unknown>)
  96. : null;
  97. const providerId = typeof selection?.providerId === "string" ? selection.providerId : null;
  98. const modelId = typeof selection?.modelId === "string" ? selection.modelId : null;
  99. return {
  100. exists: draft !== null,
  101. mode: typeof draft?.mode === "string" ? draft.mode : null,
  102. modelSelection:
  103. providerId && modelId
  104. ? {
  105. providerId,
  106. modelId,
  107. reasoningLevel:
  108. typeof options?.reasoningLevel === "string" ? options.reasoningLevel : null,
  109. }
  110. : null,
  111. scopeId,
  112. text: typeof draft?.text === "string" ? draft.text : "",
  113. };
  114. },
  115. workspacePath,
  116. taskId,
  117. workspaceIdentity,
  118. );
  119. }
  120. export async function getTaskStoreSnapshot(
  121. taskId: string,
  122. workspaceKey?: string,
  123. ): Promise<TaskStoreSnapshot> {
  124. const result = await browser.execute(readTaskStoreSnapshotInBrowser, taskId, workspaceKey);
  125. // 修复原因:WebDriver execute 的返回对象顶层字段名 `error` 会被 chromedriver
  126. // 当成协议错误处理,导致原本用于诊断的 store snapshot 被吞成 WebDriverError。
  127. return result.snapshot;
  128. }
  129. export async function waitForTaskStoreSnapshot(
  130. taskId: string,
  131. predicate: (snapshot: TaskStoreSnapshot) => boolean = (snapshot) =>
  132. snapshot.workspaceKey !== null,
  133. timeoutMsg = `没有在会话 store 中观察到 task: ${taskId}`,
  134. timeout = 30000,
  135. ): Promise<TaskStoreSnapshot> {
  136. let latest: TaskStoreSnapshot | null = null;
  137. try {
  138. await browser.waitUntil(
  139. async () => {
  140. latest = await getTaskStoreSnapshot(taskId);
  141. return predicate(latest);
  142. },
  143. {
  144. timeout,
  145. timeoutMsg,
  146. },
  147. );
  148. } catch (error) {
  149. const suffix = latest ? `; latest=${JSON.stringify(latest)}` : "";
  150. const causeMessage = error instanceof Error ? error.message : String(error);
  151. throw new Error(`${timeoutMsg}${suffix}; cause=${causeMessage}`);
  152. }
  153. if (!latest) {
  154. throw new Error(`${timeoutMsg}; latest=null`);
  155. }
  156. // 修复原因:远端 CI 偶发先看到 DOM streaming,再看到 Zustand task map hydrate。
  157. // 用等待型读取让断言对真实运行状态收敛,而不是把一次性读不到 store 当业务失败。
  158. return latest;
  159. }