Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

3.0 KB
Raw Blame
import {
  CLIPBOARD_CHUNK_CHARS,
  CLIPBOARD_MAX_CHARS,
  parseClipboardContent,
} from "./remoteClipboard.js";
import type { RemoteDesktopRequest } from "./remoteDesktop.js";

/** Explicit transfer only. Sequential bounded invokes keep screen traffic responsive. */
export async function transferClipboardContent(
  action: "copy" | "paste",
  lease: string,
  request: <T>(message: RemoteDesktopRequest) => Promise<T>,
  check: () => void,
  clipboard: {
    read(): Promise<string>;
    write(json: string): Promise<void>;
    progress?(done: number, total: number): void;
  },
): Promise<void> {
  let id: string | undefined;
  try {
    check();
    if (action === "paste") {
      const json = await clipboard.read();
      check();
      parseClipboardContent(json);
      const result = await request<{ id: string }>({
        op: "clipboardContent",
        lease,
        action: "begin",
        length: json.length,
      });
      id = result.id;
      if (typeof id !== "string" || !id)
        throw new Error("CLIPBOARD_UNSUPPORTED");
      for (
        let offset = 0;
        offset < json.length;
        offset += CLIPBOARD_CHUNK_CHARS
      ) {
        check();
        await request({
          op: "clipboardContent",
          lease,
          action: "write",
          id,
          offset,
          data: json.slice(offset, offset + CLIPBOARD_CHUNK_CHARS),
        });
        clipboard.progress?.(
          Math.min(json.length, offset + CLIPBOARD_CHUNK_CHARS),
          json.length,
        );
      }
      check();
      await request({ op: "clipboardContent", lease, action: "commit", id });
      check();
    } else {
      const result = await request<{ id: string; length: number }>({
        op: "clipboardContent",
        lease,
        action: "copy",
      });
      id = result.id;
      if (typeof id !== "string" || !id)
        throw new Error("CLIPBOARD_UNSUPPORTED");
      if (
        !Number.isSafeInteger(result.length) ||
        result.length <= 0 ||
        result.length > CLIPBOARD_MAX_CHARS
      )
        throw new Error("CLIPBOARD_TOO_LONG");
      let json = "";
      while (json.length < result.length) {
        check();
        const { data } = await request<{ data: string }>({
          op: "clipboardContent",
          lease,
          action: "read",
          id,
          offset: json.length,
        });
        if (
          typeof data !== "string" ||
          !data.length ||
          data.length > CLIPBOARD_CHUNK_CHARS ||
          json.length + data.length > result.length
        )
          throw new Error("CLIPBOARD_UNSUPPORTED");
        json += data;
        clipboard.progress?.(json.length, result.length);
      }
      check();
      parseClipboardContent(json);
      await clipboard.write(json);
      check();
    }
  } finally {
    // Best-effort disposal only; never retry a paste after an uncertain response.
    if (id)
      void request({
        op: "clipboardContent",
        lease,
        action: "cancel",
        id,
      }).catch(() => {});
  }
}