Skip to content

Commit 7851cde

Browse files
authored
Add detection for AI CLI environment variables (#669)
* Add AiCliDetector for AI CLI environment detection Introduces AiCliDetector to detect if running in an AI-powered CLI environment (GitHub Copilot CLI, Aider, Claude Code) via environment variables. Updates documentation and DisabledChecker to include AiCliDetector. Adds related tests and documentation snippets. * Update Directory.Build.props
1 parent 21b9bf4 commit 7851cde

File tree

6 files changed

+94
-2
lines changed

6 files changed

+94
-2
lines changed

readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ DiffEngine manages launching and cleanup of diff tools. It is designed to be use
4646
* [Closing a tool](#closing-a-tool)
4747
* [File type detection](#file-type-detection)
4848
* [BuildServerDetector](#buildserverdetector)
49+
* [AiCliDetector](#aiclidetector)
4950
* [Disable for a machine/process](#disable-for-a-machineprocess)
5051
* [Disable in code](#disable-in-code)
5152
* [Icons](#icons)<!-- endToc -->
@@ -161,6 +162,29 @@ var isAppVeyor = BuildServerDetector.IsAppVeyor;
161162
<!-- endSnippet -->
162163

163164

165+
## AiCliDetector
166+
167+
`AiCliDetector.Detected` returns true if the current code is running in an AI-powered CLI environment.
168+
169+
Supports:
170+
171+
* [GitHub Copilot CLI](https://docs.github.com/en/copilot/using-github-copilot/using-github-copilot-in-the-command-line)
172+
* [Aider](https://aider.chat/docs/config/dotenv.html)
173+
* [Claude Code](https://docs.anthropic.com/en/docs/build-with-claude/claude-cli)
174+
175+
There are also individual properties to check for each specific AI CLI
176+
177+
<!-- snippet: AiCliDetectorProps -->
178+
<a id='snippet-AiCliDetectorProps'></a>
179+
```cs
180+
var isCopilotCli = AiCliDetector.IsCopilotCli;
181+
var isAider = AiCliDetector.IsAider;
182+
var isClaudeCode = AiCliDetector.IsClaudeCode;
183+
```
184+
<sup><a href='/src/DiffEngine.Tests/AiCliDetectorTest.cs#L9-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-AiCliDetectorProps' title='Start of snippet'>anchor</a></sup>
185+
<!-- endSnippet -->
186+
187+
164188
## Disable for a machine/process
165189

166190
Set an environment variable `DiffEngine_Disabled` with the value `true`.

readme.source.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ There are also individual properties to check for each specific build system
8484
snippet: BuildServerDetectorProps
8585

8686

87+
## AiCliDetector
88+
89+
`AiCliDetector.Detected` returns true if the current code is running in an AI-powered CLI environment.
90+
91+
Supports:
92+
93+
* [GitHub Copilot CLI](https://docs.github.com/en/copilot/using-github-copilot/using-github-copilot-in-the-command-line)
94+
* [Aider](https://aider.chat/docs/config/dotenv.html)
95+
* [Claude Code](https://docs.anthropic.com/en/docs/build-with-claude/claude-cli)
96+
97+
There are also individual properties to check for each specific AI CLI
98+
99+
snippet: AiCliDetectorProps
100+
101+
87102
## Disable for a machine/process
88103

89104
Set an environment variable `DiffEngine_Disabled` with the value `true`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class AiCliDetectorTest(ITestOutputHelper output) :
2+
XunitContextBase(output)
3+
{
4+
[Fact]
5+
public void Props()
6+
{
7+
// ReSharper disable UnusedVariable
8+
9+
#region AiCliDetectorProps
10+
11+
var isCopilotCli = AiCliDetector.IsCopilotCli;
12+
var isAider = AiCliDetector.IsAider;
13+
var isClaudeCode = AiCliDetector.IsClaudeCode;
14+
15+
#endregion
16+
17+
// ReSharper restore UnusedVariable
18+
}
19+
}

src/DiffEngine/AiCliDetector.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace DiffEngine;
2+
3+
public static class AiCliDetector
4+
{
5+
static AiCliDetector()
6+
{
7+
var variables = Environment.GetEnvironmentVariables();
8+
9+
// GitHub Copilot CLI
10+
// https://docs.github.com/en/copilot/using-github-copilot/using-github-copilot-in-the-command-line
11+
IsCopilotCli = variables.Contains("GITHUB_COPILOT_CLI");
12+
13+
// Aider
14+
// https://aider.chat/docs/config/dotenv.html
15+
IsAider = variables.Contains("AIDER_GIT_DNAME") || variables.Contains("AIDER");
16+
17+
// Claude Code
18+
// https://docs.anthropic.com/en/docs/build-with-claude/claude-cli
19+
IsClaudeCode = variables.Contains("CLAUDE_CODE") || variables.Contains("ANTHROPIC_CLI");
20+
21+
Detected = IsCopilotCli ||
22+
IsAider ||
23+
IsClaudeCode;
24+
}
25+
26+
public static bool IsCopilotCli { get; }
27+
28+
public static bool IsAider { get; }
29+
30+
public static bool IsClaudeCode { get; }
31+
32+
public static bool Detected { get; set; }
33+
}

src/DiffEngine/DisabledChecker.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public static bool IsDisable()
55
var variable = Environment.GetEnvironmentVariable("DiffEngine_Disabled");
66
return string.Equals(variable, "true", StringComparison.OrdinalIgnoreCase) ||
77
BuildServerDetector.Detected ||
8-
ContinuousTestingDetector.Detected;
8+
ContinuousTestingDetector.Detected ||
9+
AiCliDetector.Detected;
910
}
1011
}

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;CS0649;NU1608;NU1109</NoWarn>
5-
<Version>18.0.1</Version>
5+
<Version>18.1.0</Version>
66
<AssemblyVersion>1.0.0</AssemblyVersion>
77
<PackageTags>Testing, Snapshot, Diff, Compare</PackageTags>
88
<Description>Launches diff tools based on file extensions. Designed to be consumed by snapshot testing libraries.</Description>

0 commit comments

Comments
 (0)