Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

2.1 KB
Raw Blame
import { getClipboardTextByteLength, isClipboardTextByteLengthOverLimit } from './clipboard-text'
import { clampUtf8TextPrefix } from './utf8-byte-limits'

export const MOBILE_MARKDOWN_EDIT_MAX_BYTES = 256 * 1024
/** Over-budget mobile reads return a UTF-8-boundary prefix marked truncated. Matches the 2 MiB terminal snapshot; relay splice cap is 8 MiB. */
export const MOBILE_MARKDOWN_READ_MAX_BYTES = 2 * 1024 * 1024

export type RuntimeMarkdownReadOnlyReason =
  | 'unsupported_preview'
  | 'unsupported_tab'
  | 'unsupported_untitled'
  | 'file_too_large'

export type RuntimeMarkdownReadTabResult = {
  tabId: string
  filePath: string
  relativePath: string
  content: string
  isDirty: boolean
  version: string
  source: 'draft' | 'file'
  editable: boolean
  readOnlyReason?: RuntimeMarkdownReadOnlyReason
  /** Present only when `content` is a prefix; older phones ignore both fields. */
  truncated?: boolean
  /** The full document's UTF-8 size, sent with `truncated`. */
  byteLength?: number
}

export type RuntimeMarkdownSaveTabResult = {
  tabId: string
  version: string
  isDirty: false
  content: string
}

export function hashMarkdownContent(content: string): string {
  let hash = 0xcbf29ce484222325n
  for (let i = 0; i < content.length; i += 1) {
    hash ^= BigInt(content.charCodeAt(i))
    hash = BigInt.asUintN(64, hash * 0x100000001b3n)
  }
  return `content:${utf8ByteLength(content)}:${hash.toString(16).padStart(16, '0')}`
}

export function isMarkdownContentByteLengthOverLimit(content: string, maxBytes: number): boolean {
  return isClipboardTextByteLengthOverLimit(content, maxBytes)
}

export function utf8ByteLength(content: string): number {
  return getClipboardTextByteLength(content)
}

export function truncateMobileMarkdownRead(
  content: string
):
  | { content: string; truncated: false }
  | { content: string; truncated: true; byteLength: number } {
  const byteLength = utf8ByteLength(content)
  if (byteLength <= MOBILE_MARKDOWN_READ_MAX_BYTES) {
    return { content, truncated: false }
  }
  return {
    content: clampUtf8TextPrefix(content, MOBILE_MARKDOWN_READ_MAX_BYTES),
    truncated: true,
    byteLength
  }
}