conversation-session-store.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. editorStateJson: string | null;
  35. exists: boolean;
  36. mode: string | null;
  37. modelSelection: {
  38. providerId: string;
  39. modelId: string;
  40. reasoningLevel: string | null;
  41. } | null;
  42. scopeId: string;
  43. text: string;
  44. }
  45. export interface ConfigOptionSnapshot {
  46. category: string | null;
  47. currentValue: string | null;
  48. id: string | null;
  49. optionValues: string[];
  50. status?: string | null;
  51. type: string | null;
  52. }
  53. export interface TimelineSummarySnapshot {
  54. goalIteration: number | null;
  55. id: string | null;
  56. inputId: string | null;
  57. operationId: string | null;
  58. status: string | null;
  59. targetId: string | null;
  60. type: string | null;
  61. verificationPassed: boolean | null;
  62. }
  63. /** 读取当前 v4 Composer 的唯一持久 Draft;不再探测已删除的 Zustand 草稿镜像。 */
  64. export function getV4ComposerDraftScopeSnapshot(
  65. workspacePath: string,
  66. taskId: string | null,
  67. workspaceIdentity?: string,
  68. ): Promise<V4ComposerDraftScopeSnapshot> {
  69. return browser.execute(
  70. (workspacePathArg, taskIdArg, workspaceIdentityArg) => {
  71. const scopeId = taskIdArg ?? "__draft__";
  72. const workspaceKey = workspaceIdentityArg?.trim() || workspacePathArg;
  73. const intentRaw = window.localStorage.getItem(
  74. `zcode-composer-workspace-scope:v1:${JSON.stringify(["desktop", workspaceKey])}`,
  75. );
  76. const intent = intentRaw
  77. ? (JSON.parse(intentRaw) as {
  78. clientNamespace: string;
  79. hostId: string;
  80. projectId: string | null;
  81. })
  82. : null;
  83. const storageKey =
  84. taskIdArg === null && intent
  85. ? `zcode-v4-composer-project-drafts:v1:${JSON.stringify([intent.clientNamespace, intent.hostId, intent.projectId])}`
  86. : `zcode-v4-composer-drafts:v1:${encodeURIComponent(workspaceKey)}`;
  87. let draft: Record<string, unknown> | null = null;
  88. try {
  89. const raw = window.localStorage.getItem(storageKey);
  90. if (raw) {
  91. const parsed = JSON.parse(raw) as {
  92. scopes?: Record<string, Record<string, unknown>>;
  93. };
  94. draft = parsed.scopes?.[scopeId] ?? null;
  95. }
  96. } catch {
  97. draft = null;
  98. }
  99. const selection =
  100. draft?.modelSelection &&
  101. typeof draft.modelSelection === "object" &&
  102. !Array.isArray(draft.modelSelection)
  103. ? (draft.modelSelection as Record<string, unknown>)
  104. : null;
  105. const options =
  106. selection?.options &&
  107. typeof selection.options === "object" &&
  108. !Array.isArray(selection.options)
  109. ? (selection.options as Record<string, unknown>)
  110. : null;
  111. const providerId = typeof selection?.providerId === "string" ? selection.providerId : null;
  112. const modelId = typeof selection?.modelId === "string" ? selection.modelId : null;
  113. return {
  114. editorStateJson: typeof draft?.editorStateJson === "string" ? draft.editorStateJson : null,
  115. exists: draft !== null,
  116. mode: typeof draft?.mode === "string" ? draft.mode : null,
  117. modelSelection:
  118. providerId && modelId
  119. ? {
  120. providerId,
  121. modelId,
  122. reasoningLevel:
  123. typeof options?.reasoningLevel === "string" ? options.reasoningLevel : null,
  124. }
  125. : null,
  126. scopeId,
  127. text: typeof draft?.text === "string" ? draft.text : "",
  128. };
  129. },
  130. workspacePath,
  131. taskId,
  132. workspaceIdentity,
  133. );
  134. }
  135. export async function getTaskStoreSnapshot(
  136. taskId: string,
  137. workspaceKey?: string,
  138. ): Promise<TaskStoreSnapshot> {
  139. const result = await browser.execute(readTaskStoreSnapshotInBrowser, taskId, workspaceKey);
  140. // 修复原因:WebDriver execute 的返回对象顶层字段名 `error` 会被 chromedriver
  141. // 当成协议错误处理,导致原本用于诊断的 store snapshot 被吞成 WebDriverError。
  142. return result.snapshot;
  143. }
  144. export async function waitForTaskStoreSnapshot(
  145. taskId: string,
  146. predicate: (snapshot: TaskStoreSnapshot) => boolean = (snapshot) =>
  147. snapshot.workspaceKey !== null,
  148. timeoutMsg = `没有在会话 store 中观察到 task: ${taskId}`,
  149. timeout = 30000,
  150. ): Promise<TaskStoreSnapshot> {
  151. let latest: TaskStoreSnapshot | null = null;
  152. try {
  153. await browser.waitUntil(
  154. async () => {
  155. latest = await getTaskStoreSnapshot(taskId);
  156. return predicate(latest);
  157. },
  158. {
  159. timeout,
  160. timeoutMsg,
  161. },
  162. );
  163. } catch (error) {
  164. const suffix = latest ? `; latest=${JSON.stringify(latest)}` : "";
  165. const causeMessage = error instanceof Error ? error.message : String(error);
  166. throw new Error(`${timeoutMsg}${suffix}; cause=${causeMessage}`);
  167. }
  168. if (!latest) {
  169. throw new Error(`${timeoutMsg}; latest=null`);
  170. }
  171. // 修复原因:远端 CI 偶发先看到 DOM streaming,再看到 Zustand task map hydrate。
  172. // 用等待型读取让断言对真实运行状态收敛,而不是把一次性读不到 store 当业务失败。
  173. return latest;
  174. }