Files
archived-orca/src/shared/agent-title-identity.ts
T

147 lines
4.5 KiB
TypeScript

import { isQoderTerminalTitle } from './qoder-terminal-title'
import { getPiStateTitleBrand } from './pi-state-title-marker'
import {
AGY_AGENT_NAME_RE,
CLAUDE_IDLE,
DROID_AGENT_NAME_RE,
HERMES_AGENT_NAME_RE,
containsAgentSpinnerGlyph,
isClaudeManagementTitle,
isCursorAgentTitle,
isGeminiTerminalTitle,
isPiAgentTitle,
titleHasAgentName
} from './agent-title-core'
import { isDeepSeekBuildTerminalTitle } from './dsb-terminal-title'
import { isOpenCodeNativeTitle } from './opencode-terminal-title'
import {
getPiCompatibleSyntheticAgentLabel,
getPiCompatibleTitleBrand
} from './pi-compatible-synthetic-title'
import { memoizeTitleClassification } from './terminal-title-classification-memo'
/** Scope prompt-cache timers to Claude title conventions. */
function computeIsClaudeAgent(title: string): boolean {
if (!title || isClaudeManagementTitle(title) || isOpenCodeNativeTitle(title)) {
return false
}
const lower = title.toLowerCase()
// Claude prefixes identify the owner more strongly than other agents named in task text.
if (title.startsWith(`${CLAUDE_IDLE} `) || title === CLAUDE_IDLE) {
return true
}
if (title.startsWith('. ') || title.startsWith('* ')) {
return true
}
// Why: a working DeepSeek Build title uses Claude's braille frame.
if (isDeepSeekBuildTerminalTitle(title)) {
return false
}
if (containsAgentSpinnerGlyph(title)) {
// Other agents use braille too; require controlled Cursor identity rather than a task-text token.
return !isCursorAgentTitle(title) && !lower.includes('openclaude')
}
const trimmedTitle = title.trimStart()
return (
trimmedTitle.toLowerCase().startsWith('claude') && titleHasAgentName(trimmedTitle, 'claude')
)
}
/** Pure in `title` — memoized so repeated selector reads skip the regex ladder. */
export const isClaudeAgent: (title: string) => boolean =
memoizeTitleClassification(computeIsClaudeAgent)
function computeAgentLabel(title: string): string | null {
if (isClaudeManagementTitle(title)) {
return null
}
// OpenCode's native envelope owns session text even when it names other agents or carries their glyphs.
if (isOpenCodeNativeTitle(title)) {
return 'OpenCode'
}
// Why: Claude task titles can mention another CLI; the prefix is the identity
// signal, not arbitrary task text.
if (
title.startsWith(`${CLAUDE_IDLE} `) ||
title === CLAUDE_IDLE ||
title.startsWith('. ') ||
title.startsWith('* ')
) {
return 'Claude Code'
}
const piBrand = getPiStateTitleBrand(title) ?? getPiCompatibleTitleBrand(title)
if (piBrand) {
return piBrand
}
if (isQoderTerminalTitle(title)) {
return title.includes('Qoder CLI CN') ? 'Qoder CLI CN' : 'Qoder CLI'
}
// Why: the DSB matcher distinguishes native prefixes from glyphs inside task text.
if (isDeepSeekBuildTerminalTitle(title)) {
return 'DeepSeek Build'
}
if (isGeminiTerminalTitle(title)) {
return 'Gemini CLI'
}
// Resolve Pi-compatible synthetic titles before generic braille identity heuristics.
const piCompatibleSyntheticAgentLabel = getPiCompatibleSyntheticAgentLabel(title)
if (piCompatibleSyntheticAgentLabel) {
return piCompatibleSyntheticAgentLabel
}
if (isPiAgentTitle(title)) {
return 'Pi'
}
if (titleHasAgentName(title, 'codex')) {
return 'Codex'
}
if (titleHasAgentName(title, 'openclaude')) {
return 'OpenClaude'
}
if (titleHasAgentName(title, 'copilot')) {
return 'GitHub Copilot'
}
if (titleHasAgentName(title, 'grok')) {
return 'Grok'
}
if (titleHasAgentName(title, 'devin')) {
return 'Devin'
}
if (titleHasAgentName(title, 'jcode')) {
return 'Jcode'
}
if (titleHasAgentName(title, 'antigravity') || AGY_AGENT_NAME_RE.test(title)) {
return 'Antigravity'
}
if (titleHasAgentName(title, 'opencode')) {
return 'OpenCode'
}
if (titleHasAgentName(title, 'mimo')) {
return 'MiMo Code'
}
if (titleHasAgentName(title, 'aider')) {
return 'Aider'
}
// Cursor is ordinary vocabulary: match its closed title set before Claude claims the braille frame (matching @cursor routing).
if (isCursorAgentTitle(title)) {
return 'Cursor'
}
if (DROID_AGENT_NAME_RE.test(title)) {
return 'Droid'
}
if (HERMES_AGENT_NAME_RE.test(title)) {
return 'Hermes'
}
if (isClaudeAgent(title)) {
return 'Claude Code'
}
return null
}
/** Pure in `title` — memoized so repeated selector reads skip the regex ladder. */
export const getAgentLabel: (title: string) => string | null =
memoizeTitleClassification(computeAgentLabel)