Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

3.0 KB
Raw Blame
// The desktop app keeps a "Sign in with Boat" connection where it keeps the pasted Boat key: in the operating system's
// encrypted credential document, written only by this process. The embedded server reads it at start (its environment,
// like the key) and asks for every later read and write over the private utility-process port.
export const BOAT_SIGN_IN_CREDENTIAL = "boatSignIn";
export const BOAT_SIGN_IN_ENV = "OMB_BOAT_SIGN_IN";
const REQUEST = "openmausbot:boat-sign-in-store";
const RESULT = "openmausbot:boat-sign-in-store-result";
const MAX_BYTES = 16_384;

const stored = (credentials) => {
  const value = credentials?.[BOAT_SIGN_IN_CREDENTIAL];
  return typeof value === "string" && value ? value : null;
};

/** Set, even when empty, only by a packaged app: its presence tells the server the connection is kept here. */
export function boatSignInEnv(credentials, packaged) {
  return packaged ? { [BOAT_SIGN_IN_ENV]: stored(credentials) ?? "" } : {};
}

export function decodeBoatSignInStoreRequest(rawMessage) {
  const message = rawMessage?.data ?? rawMessage;
  if (!message || typeof message !== "object" || Array.isArray(message) || message.type !== REQUEST) return null;
  if (typeof message.requestId !== "string" || !/^[A-Za-z0-9_-]{1,80}$/.test(message.requestId)) return null;
  if (message.op === "read") return { requestId: message.requestId, op: "read" };
  if (message.op !== "write") return null;
  const value = message.value;
  if (value !== null && (typeof value !== "string" || !value || Buffer.byteLength(value) > MAX_BYTES)) return null;
  return { requestId: message.requestId, op: "write", value };
}

/** `read()` is the current credential document; `update(derive)` is the app's one serialized, encrypted writer. */
export function createBoatSignInStore({ packaged, read, update, log }) {
  const answer = async (request) => {
    if (!packaged) return { type: RESULT, requestId: request.requestId, ok: false };
    try {
      if (request.op === "read") return { type: RESULT, requestId: request.requestId, ok: true, value: stored(read()) };
      await update((credentials) => {
        if (request.value === null) delete credentials[BOAT_SIGN_IN_CREDENTIAL];
        else credentials[BOAT_SIGN_IN_CREDENTIAL] = request.value;
        return credentials;
      });
      return { type: RESULT, requestId: request.requestId, ok: true };
    } catch (error) {
      // The reason only: a credential never reaches the log.
      log(`boat sign-in ${request.op} failed: ${error?.message ?? "error"}`);
      return { type: RESULT, requestId: request.requestId, ok: false };
    }
  };
  return {
    /** True when the message was a store request (then answered on the same port). */
    receive(proc, rawMessage) {
      const request = decodeBoatSignInStoreRequest(rawMessage);
      if (!request) return false;
      void answer(request).then((result) => {
        try { proc.postMessage(result); } catch (error) { log(`boat sign-in result failed: ${error?.message ?? "error"}`); }
      });
      return true;
    },
  };
}