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.

5.8 KB
Raw Blame
// Share grab extraction contracts across main/preload/renderer, including redaction and budgets.

/** Page-level metadata captured at selection time. */
export type BrowserGrabPageContext = {
  sanitizedUrl: string
  title: string
  viewportWidth: number
  viewportHeight: number
  scrollX: number
  scrollY: number
  devicePixelRatio: number
  capturedAt: string
}

/** Accessibility metadata for the selected element. */
export type BrowserGrabAccessibility = {
  role: string | null
  accessibleName: string | null
  ariaLabel: string | null
  ariaLabelledBy: string | null
}

/** Curated subset of computed styles. */
export type BrowserGrabComputedStyles = {
  display: string
  position: string
  width: string
  height: string
  margin: string
  padding: string
  color: string
  backgroundColor: string
  border: string
  borderRadius: string
  fontFamily: string
  fontSize: string
  fontWeight: string
  lineHeight: string
  textAlign: string
  zIndex: string
}

/** Viewport-relative or page-relative rectangle in CSS pixels. */
export type BrowserGrabRect = {
  x: number
  y: number
  width: number
  height: number
}

/** The selected element's extracted data. */
export type BrowserGrabTarget = {
  tagName: string
  selector: string
  elementPath?: string
  fullPath?: string
  cssClasses?: string
  nearbyElements?: string[]
  selectedText?: string | null
  isFixed?: boolean
  reactComponents?: string | null
  sourceFile?: string | null
  textSnippet: string
  htmlSnippet: string
  attributes: Record<string, string>
  accessibility: BrowserGrabAccessibility
  rectViewport: BrowserGrabRect
  rectPage: BrowserGrabRect
  computedStyles: BrowserGrabComputedStyles
}

/** Screenshot attachment — always PNG, either a data URL or a temp file path. */
export type BrowserGrabScreenshot = {
  mimeType: 'image/png'
  dataUrl: string
  width: number
  height: number
}

/** The full payload extracted from a browser grab selection. */
export type BrowserGrabPayload = {
  page: BrowserGrabPageContext
  target: BrowserGrabTarget
  nearbyText: string[]
  ancestorPath: string[]
  screenshot: BrowserGrabScreenshot | null
}

/** Persisted annotation payloads keep DOM context but drop transient screenshots. */
export type BrowserAnnotationPayload = Omit<BrowserGrabPayload, 'screenshot'> & {
  screenshot: null
}

export type GrabIntent = 'copy' | 'annotate'

/** Why a grab operation was cancelled before the user selected an element. */
export type BrowserGrabCancelReason = 'user' | 'tab-inactive' | 'navigation' | 'evicted' | 'timeout'

/** Discriminated union for the result of a single grab operation. */
export type BrowserGrabResult =
  | { opId: string; kind: 'selected'; payload: BrowserGrabPayload }
  | { opId: string; kind: 'context-selected'; payload: BrowserGrabPayload }
  | { opId: string; kind: 'cancelled'; reason: BrowserGrabCancelReason }
  | { opId: string; kind: 'error'; reason: string }

export type BrowserSetGrabModeArgs = {
  browserPageId: string
  enabled: boolean
}

/** Why a grab IPC call was rejected before the operation could start. */
export type BrowserGrabRejectReason =
  | 'not-ready'
  | 'not-authorized'
  | 'already-active'
  | 'injection-failed'

export type BrowserSetGrabModeResult = { ok: true } | { ok: false; reason: BrowserGrabRejectReason }

export type BrowserAwaitGrabSelectionArgs = {
  browserPageId: string
  opId: string
}

export type BrowserCancelGrabArgs = {
  browserPageId: string
}

export type BrowserCaptureSelectionScreenshotArgs = {
  browserPageId: string
  rect: BrowserGrabRect
}

export type BrowserCaptureSelectionScreenshotResult =
  | { ok: true; screenshot: BrowserGrabScreenshot }
  | { ok: false; reason: string }

export type BrowserExtractHoverArgs = {
  browserPageId: string
}

export type BrowserExtractHoverResult =
  | { ok: true; payload: BrowserGrabPayload }
  | { ok: false; reason: string }

export type BrowserAnnotationIntent = 'fix' | 'change' | 'question' | 'approve'

export type BrowserAnnotationPriority = 'blocking' | 'important' | 'suggestion'

export type BrowserPageAnnotation = {
  id: string
  browserPageId: string
  comment: string
  intent: BrowserAnnotationIntent
  priority: BrowserAnnotationPriority
  createdAt: string
  payload: BrowserAnnotationPayload
  /** Pasted images, saved on the worktree's host like a terminal image paste. */
  imagePaths?: string[]
}

// Payload budgets are enforced in guest and main.

export const GRAB_BUDGET = {
  textSnippetMaxLength: 200,
  nearbyTextEntryMaxLength: 200,
  nearbyTextMaxEntries: 10,
  htmlSnippetMaxLength: 4096,
  ancestorPathMaxEntries: 10,
  nearbyElementsMaxEntries: 6,
  nearbyElementMaxLength: 160,
  selectorMaxLength: 700,
  pathMaxLength: 900,
  cssClassesMaxLength: 500,
  selectedTextMaxLength: 500,
  sourceFileMaxLength: 500,
  reactComponentsMaxLength: 500,
  annotationCommentMaxLength: 2000,
  annotationsMaxPerPage: 20,
  /** Hard byte budget for screenshot PNG data URL before we omit the screenshot. */
  screenshotMaxBytes: 2 * 1024 * 1024
} as const

export function isBrowserAnnotationPageFull(annotationCount: number): boolean {
  return annotationCount >= GRAB_BUDGET.annotationsMaxPerPage
}

/** Only these attribute names are included in the payload by default. */
export const GRAB_SAFE_ATTRIBUTE_NAMES = new Set([
  'id',
  'class',
  'name',
  'type',
  'role',
  'href',
  'src',
  'alt',
  'title',
  'placeholder',
  'for',
  'action',
  'method'
])

/** Attribute names matching aria-* are always included. */
export function isAriaAttribute(name: string): boolean {
  return name.startsWith('aria-')
}

/** Redact OAuth/credential-shaped values, not broad words like code/state that match ordinary CSS classes and degrade extraction. */
export const GRAB_SECRET_PATTERNS = [
  'access_token',
  'auth_token',
  'api_key',
  'apikey',
  'client_secret',
  'oauth_state',
  'x-amz-',
  'session_id',
  'sessionid',
  'csrf',
  'secret',
  'password',
  'passwd'
]