conversation-session-store.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 intentRaw = window.localStorage.getItem(
  73. `zcode-composer-workspace-scope:v1:${JSON.stringify(["desktop", workspaceKey])}`,
  74. );
  75. const intent = intentRaw
  76. ? (JSON.parse(intentRaw) as {
  77. clientNamespace: string;
  78. hostId: string;
  79. projectId: string | null;
  80. })
  81. : null;
  82. const storageKey =
  83. taskIdArg === null && intent
  84. ? `zcode-v4-composer-project-drafts:v1:${JSON.stringify([intent.clientNamespace, intent.hostId, intent.projectId])}`
  85. : `zcode-v4-composer-drafts:v1:${encodeURIComponent(workspaceKey)}`;
  86. let draft: Record<string, unknown> | null = null;
  87. try {
  88. const raw = window.localStorage.getItem(storageKey);
  89. if (raw) {
  90. const parsed = JSON.parse(raw) as {
  91. scopes?: Record<string, Record<string, unknown>>;
  92. };
  93. draft = parsed.scopes?.[scopeId] ?? null;
  94. }
  95. } catch {
  96. draft = null;
  97. }
  98. const selection =
  99. draft?.modelSelection &&
  100. typeof draft.modelSelection === "object" &&
  101. !Array.isArray(draft.modelSelection)
  102. ? (draft.modelSelection as Record<string, unknown>)
  103. : null;
  104. const options =
  105. selection?.options &&
  106. typeof selection.options === "object" &&
  107. !Array.isArray(selection.options)
  108. ? (selection.options as Record<string, unknown>)
  109. : null;
  110. const providerId = typeof selection?.providerId === "string" ? selection.providerId : null;
  111. const modelId = typeof selection?.modelId === "string" ? selection.modelId : null;
  112. return {
  113. exists: draft !== null,
  114. mode: typeof draft?.mode === "string" ? draft.mode : null,
  115. modelSelection:
  116. providerId && modelId
  117. ? {
  118. providerId,
  119. modelId,
  120. reasoningLevel:
  121. typeof options?.reasoningLevel === "string" ? options.reasoningLevel : null,
  122. }
  123. : null,
  124. scopeId,
  125. text: typeof draft?.text === "string" ? draft.text : "",
  126. };
  127. },
  128. workspacePath,
  129. taskId,
  130. workspaceIdentity,
  131. );
  132. }
  133. export async function getTaskStoreSnapshot(
  134. taskId: string,
  135. workspaceKey?: string,
  136. ): Promise<TaskStoreSnapshot> {
  137. const result = await browser.execute(readTaskStoreSnapshotInBrowser, taskId, workspaceKey);
  138. // 修复原因:WebDriver execute 的返回对象顶层字段名 `error` 会被 chromedriver
  139. // 当成协议错误处理,导致原本用于诊断的 store snapshot 被吞成 WebDriverError。
  140. return result.snapshot;
  141. }
  142. export async function waitForTaskStoreSnapshot(
  143. taskId: string,
  144. predicate: (snapshot: TaskStoreSnapshot) => boolean = (snapshot) =>
  145. snapshot.workspaceKey !== null,
  146. timeoutMsg = `没有在会话 store 中观察到 task: ${taskId}`,
  147. timeout = 30000,
  148. ): Promise<TaskStoreSnapshot> {
  149. let latest: TaskStoreSnapshot | null = null;
  150. try {
  151. await browser.waitUntil(
  152. async () => {
  153. latest = await getTaskStoreSnapshot(taskId);
  154. return predicate(latest);
  155. },
  156. {
  157. timeout,
  158. timeoutMsg,
  159. },
  160. );
  161. } catch (error) {
  162. const suffix = latest ? `; latest=${JSON.stringify(latest)}` : "";
  163. const causeMessage = error instanceof Error ? error.message : String(error);
  164. throw new Error(`${timeoutMsg}${suffix}; cause=${causeMessage}`);
  165. }
  166. if (!latest) {
  167. throw new Error(`${timeoutMsg}; latest=null`);
  168. }
  169. // 修复原因:远端 CI 偶发先看到 DOM streaming,再看到 Zustand task map hydrate。
  170. // 用等待型读取让断言对真实运行状态收敛,而不是把一次性读不到 store 当业务失败。
  171. return latest;
  172. }