browserRecordingArtifactMaterializer.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { copyFile, mkdir, rename, rm } from "node:fs/promises";
  2. import { dirname, posix, relative, resolve } from "node:path";
  3. import { randomUUID } from "node:crypto";
  4. import type { IRemoteBackend } from "@zcode/server/remote";
  5. import type { BrowserRecordingArtifact } from "@zcode/shared";
  6. function resolveWorkspaceRecordingPath(workspacePath: string, outputPath: string): string {
  7. const root = resolve(workspacePath);
  8. const target = resolve(root, outputPath);
  9. const relation = relative(root, target);
  10. if (!relation || relation.startsWith("..") || resolve(root, relation) !== target) {
  11. throw new Error("recording outputPath must stay inside the workspace");
  12. }
  13. if (!target.toLowerCase().endsWith(".webm")) {
  14. throw new Error("recording outputPath must end with .webm");
  15. }
  16. return target;
  17. }
  18. /**
  19. * main 只返回短期 WebM;Host 按当前 workspace authority 落盘。本地使用同目录 rename,远端
  20. * 复用已有 backend.upload,避免让远端 Agent 依赖 Desktop 临时路径。
  21. */
  22. export async function materializeBrowserRecordingArtifact(input: {
  23. artifact: BrowserRecordingArtifact;
  24. localPath: string;
  25. outputPath: string;
  26. workspacePath: string;
  27. remoteSessionId?: string;
  28. remoteBackend?: Pick<IRemoteBackend, "upload">;
  29. }): Promise<BrowserRecordingArtifact> {
  30. if (input.remoteSessionId) {
  31. if (!input.remoteBackend) {
  32. throw new Error("remote Browser recording materialization is unavailable for this session");
  33. }
  34. const normalizedWorkspace = posix.normalize(input.workspacePath.replace(/\\/gu, "/"));
  35. const rawOutputSegments = input.outputPath.split(/[\\/]+/u);
  36. const normalizedOutput = posix.normalize(input.outputPath.replace(/\\/gu, "/"));
  37. if (
  38. rawOutputSegments.some((segment) => segment === ".." || segment === "." || !segment) ||
  39. normalizedOutput === ".." ||
  40. normalizedOutput.startsWith("../") ||
  41. posix.isAbsolute(normalizedOutput)
  42. ) {
  43. throw new Error("recording outputPath must stay inside the remote workspace");
  44. }
  45. if (!normalizedOutput.toLowerCase().endsWith(".webm")) {
  46. throw new Error("recording outputPath must end with .webm");
  47. }
  48. const remoteTargetPath = posix.join(normalizedWorkspace, normalizedOutput);
  49. await input.remoteBackend.upload(input.localPath, remoteTargetPath);
  50. return { ...input.artifact, path: remoteTargetPath };
  51. }
  52. const targetPath = resolveWorkspaceRecordingPath(input.workspacePath, input.outputPath);
  53. await mkdir(dirname(targetPath), { recursive: true });
  54. const stagingPath = `${targetPath}.zcode-recording-${randomUUID()}.tmp`;
  55. try {
  56. await copyFile(input.localPath, stagingPath);
  57. // Windows 不能用 rename 原子覆盖已有文件;先移除明确的目标 WebM,再提交 staging 文件。
  58. await rm(targetPath, { force: true });
  59. await rename(stagingPath, targetPath);
  60. } finally {
  61. await rm(stagingPath, { force: true }).catch(() => undefined);
  62. }
  63. return { ...input.artifact, path: targetPath };
  64. }