conversation-session-chat-root-snapshot-script.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. export interface V4ChatRootBrowserSnapshot {
  2. activeInputId: string | null;
  3. bridgeError: string | null;
  4. canStop: boolean;
  5. modelSwitchPending: boolean;
  6. paneSessionId: string | null;
  7. queueCount: number;
  8. runtimeStatus: string;
  9. taskId: string | null;
  10. workspaceKey: string | null;
  11. }
  12. type BrowserWorkspaceState = {
  13. draftRuntime?: { status?: string | null };
  14. modelSwitchPending?: boolean;
  15. taskRuntimeByTaskId?: Record<
  16. string,
  17. { activeInputId?: string | null; status?: string | null }
  18. >;
  19. };
  20. export function readV4ChatRootSnapshotInBrowser(
  21. v4PaneTestId: string,
  22. v4QueueItemPrefix: string,
  23. v4StopTestId: string,
  24. ): V4ChatRootBrowserSnapshot {
  25. const pane = document.querySelector<HTMLElement>(`[data-testid="${v4PaneTestId}"]`);
  26. const canStop = Boolean(
  27. pane?.querySelector(`[data-testid="${v4StopTestId}"]`),
  28. );
  29. const paneSessionId = pane?.getAttribute("data-session-id") ?? null;
  30. const taskId = paneSessionId && paneSessionId !== "draft" ? paneSessionId : null;
  31. const e2eWindow = window as Window & {
  32. __zcodeSessionStoreE2E?: {
  33. getState?: () => { workspaces?: Record<string, BrowserWorkspaceState> };
  34. };
  35. __zcodeTabStoreE2E?: {
  36. getState?: () => {
  37. activeWorkspaceIdentity?: string | null;
  38. activeWorkspacePath?: string | null;
  39. };
  40. };
  41. };
  42. const tabState = e2eWindow.__zcodeTabStoreE2E?.getState?.();
  43. const workspaceKey =
  44. tabState?.activeWorkspaceIdentity?.trim() || tabState?.activeWorkspacePath || null;
  45. const sessionState = e2eWindow.__zcodeSessionStoreE2E?.getState?.();
  46. const workspace = workspaceKey ? sessionState?.workspaces?.[workspaceKey] : undefined;
  47. const taskRuntime = taskId ? workspace?.taskRuntimeByTaskId?.[taskId] : undefined;
  48. const runtimeStatus = taskId
  49. ? taskRuntime?.status
  50. : workspace?.draftRuntime?.status;
  51. const bridgeError = !pane
  52. ? "v4-pane-missing"
  53. : !e2eWindow.__zcodeTabStoreE2E?.getState
  54. ? "tab-store-bridge-missing"
  55. : !e2eWindow.__zcodeSessionStoreE2E?.getState
  56. ? "session-store-bridge-missing"
  57. : !workspaceKey
  58. ? "active-workspace-key-missing"
  59. : !workspace
  60. ? "active-workspace-not-found"
  61. : null;
  62. return {
  63. activeInputId: taskRuntime?.activeInputId ?? null,
  64. bridgeError,
  65. canStop,
  66. modelSwitchPending: workspace?.modelSwitchPending === true,
  67. paneSessionId,
  68. queueCount: document.querySelectorAll(
  69. `li[data-testid^="${v4QueueItemPrefix}-"][data-queue-item-id]`,
  70. ).length,
  71. // Bug 根因:V4 session 刚从 completed 切回 running 时,pane 的 control projection
  72. // 已经显示 Stop,但 E2E store bridge 仍可能保留上一帧 completed。运行态断言应以
  73. // 用户可操作的 Stop 合同为准,避免把真实 running turn 误判成 idle。
  74. runtimeStatus: canStop ? "streaming" : (runtimeStatus ?? "idle"),
  75. taskId,
  76. workspaceKey,
  77. };
  78. }