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.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
import type { SearchFileResult, SearchMatch } from './code-search-types'
import type { SearchAccumulator } from './text-search'
import { sliceAtCodeUnitLimit } from './surrogate-safe-text-slice'
import { ownRetainedString } from './own-retained-string'
// Bound repeated context so dense search results fit within the SSH message limit.
export const MAX_LINE_CONTENT_LENGTH = 500
const TRUNCATION_MARKER = '…'
type ClampedLineContext = {
lineContent: string
column: number
matchLength: number
displayColumn?: number
displayMatchLength?: number
}
function clampLineContext(
text: string,
matchStart: number,
matchLength: number
): ClampedLineContext {
if (text.length <= MAX_LINE_CONTENT_LENGTH) {
return { lineContent: text, column: matchStart + 1, matchLength }
}
const clampedMatchLength = Math.min(matchLength, MAX_LINE_CONTENT_LENGTH)
const remaining = MAX_LINE_CONTENT_LENGTH - clampedMatchLength
const leftBudget = Math.floor(remaining / 2)
let windowStart = Math.max(0, matchStart - leftBudget)
let windowEnd = Math.min(text.length, windowStart + MAX_LINE_CONTENT_LENGTH)
windowStart = Math.max(0, windowEnd - MAX_LINE_CONTENT_LENGTH)
if (windowStart > 0 && (text.codePointAt(windowStart - 1) ?? 0) > 0xffff) {
windowStart++
}
let snippet = sliceAtCodeUnitLimit(text.slice(windowStart), windowEnd - windowStart)
windowEnd = windowStart + snippet.length
const displayStart = Math.max(windowStart, Math.min(matchStart, windowEnd))
const displayMatchLength = Math.max(
0,
Math.min(matchStart + matchLength, windowEnd) - displayStart
)
let column = displayStart - windowStart + 1
if (windowStart > 0) {
snippet = TRUNCATION_MARKER + snippet
column += TRUNCATION_MARKER.length
}
if (windowEnd < text.length) {
snippet += TRUNCATION_MARKER
}
return {
// A tiny slice can otherwise retain the entire source line until results are released.
lineContent: ownRetainedString(snippet),
column: matchStart + 1,
matchLength,
displayColumn: column,
displayMatchLength
}
}
export function pushSearchMatch(args: {
fileResult: SearchFileResult
accumulator: SearchAccumulator
lineContent: string
matchStart: number
matchLength: number
lineNumber: number
maxResults: number
}): 'continue' | 'stop' {
const clamped = clampLineContext(args.lineContent, args.matchStart, args.matchLength)
const match: SearchMatch = {
line: args.lineNumber,
column: clamped.column,
matchLength: clamped.matchLength,
lineContent: clamped.lineContent
}
if (clamped.displayColumn !== undefined) {
match.displayColumn = clamped.displayColumn
}
if (clamped.displayMatchLength !== undefined) {
match.displayMatchLength = clamped.displayMatchLength
}
args.fileResult.matches.push(match)
args.fileResult.matchCount = (args.fileResult.matchCount ?? 0) + 1
args.accumulator.totalMatches++
if (args.accumulator.totalMatches >= args.maxResults) {
args.accumulator.truncated = true
return 'stop'
}
return 'continue'
}