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.4 KB
Raw Blame
import {
  imagePasteWritesFollowedByText,
  separateImagePasteFromFollowingText
} from './image-paste-following-text'
import { isWindowsAbsolutePathLike } from './cross-platform-path'
import { filesystemPathToFileUri } from './file-uri-path'

// Whether raw-path paste is verified to create an attachment; others use file references.
export type AgentImageHandling = 'attachment' | 'unsupported'

const IMAGE_ATTACHMENT_AGENTS: ReadonlySet<string> = new Set<string>([
  'claude',
  'openclaude',
  'codex',
  'gemini',
  'cursor',
  'copilot',
  'droid',
  // Grok supports bracketed path/image chips (xAI terminal docs and pager paste.rs).
  'grok'
])

export function getAgentImageHandling(agent: string | null | undefined): AgentImageHandling {
  return agent && IMAGE_ATTACHMENT_AGENTS.has(agent) ? 'attachment' : 'unsupported'
}

/** Quoted file references can resemble image markers when paths contain whitespace. */
export const NATIVE_CHAT_QUOTED_FILE_REFERENCE = /@"(?:\\"|[^"])*"|@'[^']*'/

export function formatNativeChatFileReference(filePath: string): string {
  if (!/[\s@]/.test(filePath)) {
    return `@${filePath}`
  }
  if (!filePath.includes('"')) {
    return `@"${filePath}"`
  }
  if (!filePath.includes("'")) {
    return `@'${filePath}'`
  }
  const escaped = filePath.replace(/"/g, '\\"')
  return `@"${escaped}"`
}

export function formatAgentImagePath(agent: string | null | undefined, filePath: string): string {
  if (
    agent === 'omp' &&
    /[\s@]/.test(filePath) &&
    filePath.includes('"') &&
    filePath.includes("'") &&
    (filePath.startsWith('/') || isWindowsAbsolutePathLike(filePath))
  ) {
    // OMP's mention lexer cannot escape quotes, but its path resolver decodes file URLs.
    return `@"${filesystemPathToFileUri(filePath)}"`
  }
  return getAgentImageHandling(agent) === 'attachment'
    ? filePath
    : formatNativeChatFileReference(filePath)
}

export function agentImagePasteWrites(
  agent: string | null | undefined,
  framedPastes: readonly string[],
  followedByText: boolean
): string[] {
  if (getAgentImageHandling(agent) === 'attachment') {
    return imagePasteWritesFollowedByText(framedPastes, followedByText)
  }
  // TUI paste frames disappear in the editor; file mentions still need word boundaries.
  return framedPastes.map((paste, index) =>
    separateImagePasteFromFollowingText(paste, index < framedPastes.length - 1 || followedByText)
  )
}