Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

3.3 KB
Raw Blame
import { isRecord } from "@linger/service-kit";
export const officePreviewProtocol = "linger-office-preview-v1";

export type OfficePreviewErrorCode =
  | "encrypted-file"
  | "invalid-container"
  | "invalid-file"
  | "limit-exceeded"
  | "render-failed"
  | "timeout"
  | "unsupported-file";

/**
 * The preview limit an Office file went over. The preflight worker has no
 * language, so the adapter words it.
 */
export type OfficePreviewLimit =
  | { kind: "cells"; max: number }
  | { kind: "compression" }
  | { kind: "entries" }
  | { kind: "entrySize" }
  | { kind: "expandedSize" }
  | { kind: "fileSize"; mebibytes: number }
  | { kind: "sheetSize"; sheet: string }
  | { kind: "sheets"; max: number }
  | { kind: "slides"; max: number };

export interface OfficePreviewRenderMessage {
  buffer: ArrayBuffer;
  filename: string;
  kind: "render";
  scrollPosition?: OfficeScrollPosition;
  mediaType: string;
  protocol: typeof officePreviewProtocol;
  requestId: string;
}

export interface OfficePreviewProbeMessage {
  kind: "probe";
  protocol: typeof officePreviewProtocol;
}

export interface OfficeScrollPosition {
  top: number;
  left: number;
}

export type OfficePreviewFrameMessage =
  | {
      kind: "scroll";
      position: OfficeScrollPosition;
      protocol: typeof officePreviewProtocol;
      requestId: string;
    }
  | {
      kind: "ready";
      protocol: typeof officePreviewProtocol;
    }
  | {
      kind: "loaded";
      protocol: typeof officePreviewProtocol;
      requestId: string;
    }
  | {
      code: OfficePreviewErrorCode;
      detail?: string;
      kind: "error";
      protocol: typeof officePreviewProtocol;
      requestId: string;
    };

export interface OfficePreviewPreflightRequest {
  buffer: ArrayBuffer;
  filename: string;
  mediaType: string;
  requestId: string;
}

export type OfficePreviewPreflightResponse =
  | {
      buffer: ArrayBuffer;
      kind: "passed";
      requestId: string;
    }
  | {
      code: OfficePreviewErrorCode;
      /** The failure in English, for developers. */
      detail?: string;
      kind: "failed";
      limit?: OfficePreviewLimit;
      requestId: string;
    };

const officePreviewErrorCodes = new Set<OfficePreviewErrorCode>([
  "encrypted-file",
  "invalid-container",
  "invalid-file",
  "limit-exceeded",
  "render-failed",
  "timeout",
  "unsupported-file",
]);

export function isOfficePreviewFrameMessage(
  value: unknown,
): value is OfficePreviewFrameMessage {
  if (!isRecord(value) || value.protocol !== officePreviewProtocol) {
    return false;
  }

  if (value.kind === "ready") {
    return true;
  }

  if (value.kind === "scroll") {
    return (
      typeof value.requestId === "string" &&
      isOfficeScrollPosition(value.position)
    );
  }
  if (value.kind === "loaded") {
    return typeof value.requestId === "string";
  }

  return (
    value.kind === "error" &&
    typeof value.requestId === "string" &&
    typeof value.code === "string" &&
    officePreviewErrorCodes.has(value.code as OfficePreviewErrorCode)
  );
}

export function isOfficeScrollPosition(
  value: unknown,
): value is OfficeScrollPosition {
  return (
    isRecord(value) &&
    typeof value.top === "number" &&
    Number.isFinite(value.top) &&
    value.top >= 0 &&
    typeof value.left === "number" &&
    Number.isFinite(value.left) &&
    value.left >= 0
  );
}