Repository navigation
feat: add getDrivingAgent agent-detection helper #8492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
98d1e39
feat: add getDrivingAgent agent-detection helper
seancdavis 84c9b48
test: cover getDrivingAgent signals, nesting, and ignored markers
seancdavis 38c708f
docs: document NETLIFY_AGENT for agent detection
seancdavis ba67e09
fix: guard agent-name lookup against prototype keys
seancdavis 608b21e
test: consolidate prototype-key regression cases
seancdavis 5154f78
feat: detect Warp runs and let NETLIFY_AGENT override unconditionally
seancdavis 5b2eae4
fix: address CodeRabbit review on agent detection
seancdavis 4ec9772
fix: tighten agent-detection precedence and input hardening
seancdavis 000b3b8
fix: honor name@version announcements and let the first match win
seancdavis 6a205b5
feat: recognize documented AI_AGENT aliases
seancdavis 4d55c4d
Merge remote-tracking branch 'origin/main' into ex-3040-agent-detection
seancdavis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| // Only markers an agent product sets on its own count as a signal; never infer from process names, terminals, or the process tree. | ||
|
|
||
| export type DrivingAgent = { | ||
| name: string | ||
| source: string | ||
| version?: string | ||
| markers?: string[] | ||
| otherValue?: string | ||
| } | ||
|
|
||
| export const CANONICAL_AGENT_NAMES = [ | ||
| 'claude', | ||
| 'codex', | ||
| 'copilot', | ||
| 'gemini', | ||
| 'cursor', | ||
| 'opencode', | ||
| 'kiro', | ||
| 'cline', | ||
| 'amp', | ||
| 'claudeai', | ||
| 'chatgpt', | ||
| 'other', | ||
| ] as const | ||
|
|
||
| export type CanonicalAgentName = (typeof CANONICAL_AGENT_NAMES)[number] | ||
|
|
||
| const ANNOUNCED_NAME_TABLE = new Map<string, CanonicalAgentName>([ | ||
| ...CANONICAL_AGENT_NAMES.map((name) => [name, name] as const), | ||
| ['claude-code', 'claude'], | ||
| ['claude-ai', 'claudeai'], | ||
| ['github_copilot_vscode_agent', 'copilot'], | ||
| ]) | ||
|
|
||
| type ParsedAnnouncedName = { | ||
| name: CanonicalAgentName | ||
| version?: string | ||
| otherValue?: string | ||
| } | ||
|
|
||
| const sanitizeAnnouncedValue = (raw: string): string => raw.replace(/[^A-Za-z0-9_.-]/g, '').slice(0, 64) | ||
|
|
||
| const parseAnnouncedName = (raw: string): ParsedAnnouncedName => { | ||
| const sanitized = sanitizeAnnouncedValue(raw) | ||
|
|
||
| const exact = ANNOUNCED_NAME_TABLE.get(sanitized) | ||
| if (exact) { | ||
| return { name: exact } | ||
| } | ||
|
|
||
| const withoutAgentSuffix = sanitized.replace(/_agent$/, '') | ||
| const suffixMatch = ANNOUNCED_NAME_TABLE.get(withoutAgentSuffix) | ||
| if (suffixMatch) { | ||
| return { name: suffixMatch } | ||
| } | ||
|
|
||
| const lastUnderscore = withoutAgentSuffix.lastIndexOf('_') | ||
| if (lastUnderscore !== -1) { | ||
| const head = withoutAgentSuffix.slice(0, lastUnderscore) | ||
| const headMatch = ANNOUNCED_NAME_TABLE.get(head) | ||
| if (headMatch) { | ||
| const tail = withoutAgentSuffix.slice(lastUnderscore + 1) | ||
| return { name: headMatch, version: tail.replace(/-/g, '.') } | ||
| } | ||
| } | ||
|
|
||
| return { name: 'other', otherValue: sanitized } | ||
| } | ||
|
|
||
| const nonEmpty = (value: string | undefined): string | undefined => (value ? value : undefined) | ||
|
|
||
| type Signal = { | ||
| source: string | ||
| detect: (env: NodeJS.ProcessEnv) => ParsedAnnouncedName | undefined | ||
| } | ||
|
|
||
| const SIGNALS: Signal[] = [ | ||
| { | ||
| source: 'NETLIFY_AGENT', | ||
| detect: (env) => { | ||
| const value = nonEmpty(env.NETLIFY_AGENT) | ||
| return value === undefined ? undefined : parseAnnouncedName(value) | ||
| }, | ||
| }, | ||
| { | ||
| source: 'CODEX_CI', | ||
| detect: (env) => (env.CODEX_CI === '1' ? { name: 'codex' } : undefined), | ||
| }, | ||
| { | ||
| source: 'GEMINI_CLI', | ||
| detect: (env) => (env.GEMINI_CLI === '1' ? { name: 'gemini' } : undefined), | ||
| }, | ||
| { | ||
| source: 'COPILOT_CLI', | ||
| detect: (env) => (env.COPILOT_CLI === '1' ? { name: 'copilot' } : undefined), | ||
| }, | ||
| { | ||
| source: 'COPILOT_AGENT_SESSION_ID', | ||
| detect: (env) => (nonEmpty(env.COPILOT_AGENT_SESSION_ID) === undefined ? undefined : { name: 'copilot' }), | ||
| }, | ||
| { | ||
| source: 'OPENCODE', | ||
| detect: (env) => | ||
| env.OPENCODE === '1' && nonEmpty(env.OPENCODE_TERMINAL) === undefined ? { name: 'opencode' } : undefined, | ||
| }, | ||
| { | ||
| source: 'AGENT_DISPLAY_OUT', | ||
| detect: (env) => (nonEmpty(env.AGENT_DISPLAY_OUT) === undefined ? undefined : { name: 'kiro' }), | ||
| }, | ||
| { | ||
| source: 'AGENT_CONTEXT_OUT', | ||
| detect: (env) => (nonEmpty(env.AGENT_CONTEXT_OUT) === undefined ? undefined : { name: 'kiro' }), | ||
| }, | ||
| { | ||
| source: 'AI_AGENT', | ||
| detect: (env) => { | ||
| const value = nonEmpty(env.AI_AGENT) | ||
| return value === undefined ? undefined : parseAnnouncedName(value) | ||
| }, | ||
| }, | ||
| { | ||
| source: 'COPILOT_AGENT', | ||
| detect: (env) => (env.COPILOT_AGENT === '1' ? { name: 'copilot' } : undefined), | ||
| }, | ||
| { | ||
| source: 'CURSOR_AGENT', | ||
| detect: (env) => (env.CURSOR_AGENT === '1' ? { name: 'cursor' } : undefined), | ||
| }, | ||
| { | ||
| source: 'CLINE_ACTIVE', | ||
| detect: (env) => (env.CLINE_ACTIVE === 'true' ? { name: 'cline' } : undefined), | ||
| }, | ||
| { | ||
| source: 'AGENT', | ||
| detect: (env) => (env.AGENT === 'amp' ? { name: 'amp' } : undefined), | ||
| }, | ||
| { | ||
| source: 'CLAUDE_CODE_CHILD_SESSION', | ||
| detect: (env) => (env.CLAUDE_CODE_CHILD_SESSION === '1' ? { name: 'claude' } : undefined), | ||
| }, | ||
| ] | ||
|
|
||
| type SignalMatch = { source: string } & ParsedAnnouncedName | ||
|
|
||
| export const getDrivingAgent = (env: NodeJS.ProcessEnv = process.env): DrivingAgent | undefined => { | ||
| const matches: SignalMatch[] = [] | ||
|
|
||
| for (const signal of SIGNALS) { | ||
| const result = signal.detect(env) | ||
| if (result) { | ||
| matches.push({ source: signal.source, ...result }) | ||
| } | ||
| } | ||
|
|
||
| if (matches.length === 0) { | ||
| return undefined | ||
| } | ||
|
|
||
| const winner = matches.find((match) => match.name !== 'other') ?? matches[0] | ||
|
seancdavis marked this conversation as resolved.
Outdated
|
||
|
|
||
| const version = | ||
| winner.source === 'AI_AGENT' | ||
| ? winner.version | ||
| : winner.source === 'CODEX_CI' | ||
| ? nonEmpty(env.CODEX_VERSION) | ||
| : undefined | ||
|
seancdavis marked this conversation as resolved.
Outdated
|
||
|
|
||
| const distinctNames = [...new Set(matches.map((match) => match.name))] | ||
|
|
||
| return { | ||
| name: winner.name, | ||
| source: winner.source, | ||
| ...(version ? { version } : {}), | ||
| ...(distinctNames.length >= 2 ? { markers: distinctNames } : {}), | ||
| ...(winner.name === 'other' ? { otherValue: winner.otherValue ?? '' } : {}), | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.