conversation-session-composer-prefill.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { mkdir, truncate, writeFile } from "node:fs/promises";
  2. import { dirname } from "node:path";
  3. export interface ComposerPrefillExpectation {
  4. attachments?: ComposerPrefillAttachment[];
  5. attachmentLocalPaths?: Record<string, string>;
  6. attachmentFilenames: string[];
  7. text: string;
  8. }
  9. export type ComposerPrefillAttachment =
  10. | {
  11. dataBase64?: string;
  12. filename: string;
  13. kind: "image";
  14. localPath?: string;
  15. mimeType: string;
  16. sizeBytes: number;
  17. }
  18. | {
  19. filename: string;
  20. kind: "file";
  21. localPath?: string;
  22. mimeType: string;
  23. sizeBytes: number;
  24. textContent?: string;
  25. };
  26. const PNG_FIXTURE_BASE64 =
  27. "iVBORw0KGgoAAAANSUhEUgAAAEAAAAAgCAYAAACinX6EAAAATUlEQVR4AeXBMQEAIAzAsK43KlCBf2NMSJM5933CJE7iJE7iJE7iJE7iJE7iJE7iJE7iJE7iJE7iJE7iJE7iJE7iJE7iJE7iJE7iJG4B7pwBpTup40wAAAAASUVORK5CYII=";
  28. const PNG_FIXTURE_SIZE_BYTES = 134;
  29. export async function writeComposerPrefillPngFixture(localPath: string) {
  30. await mkdir(dirname(localPath), { recursive: true });
  31. await writeFile(localPath, Buffer.from(PNG_FIXTURE_BASE64, "base64"));
  32. }
  33. export async function writeOversizedComposerPrefillPngFixture(
  34. localPath: string,
  35. sizeBytes: number,
  36. ) {
  37. await writeComposerPrefillPngFixture(localPath);
  38. // 修复原因:agent 侧会重新 stat 本地图片大小;只改 composer metadata 不能覆盖真实大图路径引用分支。
  39. await truncate(localPath, sizeBytes);
  40. }
  41. export async function setComposerPrefill(
  42. taskId: string | null,
  43. expectation: ComposerPrefillExpectation,
  44. ) {
  45. const result = (await browser.execute(
  46. (taskIdArg, nextExpectation, pngBase64, pngSizeBytes) => {
  47. const bridge = (
  48. window as typeof window & {
  49. __zcodeSessionStoreE2E?: {
  50. getState?: () => {
  51. setPendingComposerPrefill?: (
  52. workspacePath: string,
  53. prefill: {
  54. attachments: Array<
  55. | {
  56. dataBase64?: string;
  57. filename: string;
  58. kind: "image";
  59. localPath?: string;
  60. mimeType: string;
  61. sizeBytes: number;
  62. }
  63. | {
  64. filename: string;
  65. kind: "file";
  66. localPath?: string;
  67. mimeType: string;
  68. sizeBytes: number;
  69. textContent?: string;
  70. }
  71. >;
  72. taskId: string;
  73. text: string;
  74. },
  75. ) => void;
  76. workspaces?: Record<string, { activeTaskId?: string | null }>;
  77. };
  78. };
  79. }
  80. ).__zcodeSessionStoreE2E;
  81. const state = bridge?.getState?.();
  82. if (!state?.setPendingComposerPrefill) {
  83. return { ok: false, reason: "store-missing" };
  84. }
  85. const workspaces = state.workspaces ?? {};
  86. const workspaceEntry =
  87. Object.entries(workspaces).find(([, workspace]) =>
  88. taskIdArg === null
  89. ? workspace.activeTaskId === null
  90. : workspace.activeTaskId === taskIdArg,
  91. ) ?? Object.entries(workspaces)[0];
  92. if (!workspaceEntry) {
  93. return { ok: false, reason: "workspace-missing" };
  94. }
  95. const [workspacePath] = workspaceEntry;
  96. const attachments =
  97. nextExpectation.attachments ??
  98. nextExpectation.attachmentFilenames.map((filename) => {
  99. const localPath = nextExpectation.attachmentLocalPaths?.[filename];
  100. if (filename.endsWith(".png")) {
  101. return {
  102. kind: "image" as const,
  103. filename,
  104. mimeType: "image/png",
  105. sizeBytes: pngSizeBytes,
  106. ...(localPath ? { localPath } : { dataBase64: pngBase64 }),
  107. };
  108. }
  109. return {
  110. kind: "file" as const,
  111. filename,
  112. mimeType: filename.endsWith(".md") ? "text/markdown" : "text/plain",
  113. ...(localPath
  114. ? { localPath }
  115. : { textContent: `E2E composer draft attachment ${filename}` }),
  116. sizeBytes: filename.length,
  117. };
  118. });
  119. state.setPendingComposerPrefill(workspacePath, {
  120. taskId: taskIdArg ?? "__draft__",
  121. text: nextExpectation.text,
  122. attachments,
  123. });
  124. return { ok: true, count: attachments.length };
  125. },
  126. taskId,
  127. expectation,
  128. PNG_FIXTURE_BASE64,
  129. PNG_FIXTURE_SIZE_BYTES,
  130. )) as { count?: number; ok: boolean; reason?: string };
  131. const expectedAttachmentCount = expectation.attachments?.length ?? expectation.attachmentFilenames.length;
  132. if (!result.ok || result.count !== expectedAttachmentCount) {
  133. throw new Error(`设置 composer prefill 附件失败: ${JSON.stringify(result)}`);
  134. }
  135. }
  136. export async function waitForVisibleAttachmentNames(filenames: string[]) {
  137. await browser.waitUntil(
  138. async () =>
  139. browser.execute((expectedFilenames) => {
  140. const bodyText = document.body.innerText;
  141. return expectedFilenames.every((filename) => bodyText.includes(filename));
  142. }, filenames),
  143. {
  144. timeout: 10000,
  145. timeoutMsg: `composer 附件没有显示: ${filenames.join(", ")}`,
  146. },
  147. );
  148. }