-
Notifications
You must be signed in to change notification settings - Fork 129
Add agent detection library #4287
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pietern
wants to merge
1
commit into
main
Choose a base branch
from
agent-ua
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+258
−0
Open
Changes from all commits
Commits
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
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,27 @@ | ||
| // This file integrates agent detection with the user agent string. | ||
| // | ||
| // The actual detection logic is in libs/agent. This file simply retrieves | ||
| // the detected agent name from the context and adds it to the user agent. | ||
| // | ||
| // Example user agent strings: | ||
| // - With Claude Code: "cli/X.Y.Z ... agent/claude-code ..." | ||
| // - No agent: "cli/X.Y.Z ..." (no agent tag) | ||
| package root | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/databricks/cli/libs/agent" | ||
| "github.com/databricks/databricks-sdk-go/useragent" | ||
| ) | ||
|
|
||
| // Key in the user agent | ||
| const agentKey = "agent" | ||
|
|
||
| func withAgentInUserAgent(ctx context.Context) context.Context { | ||
| product := agent.Product(ctx) | ||
| if product == "" { | ||
| return ctx | ||
| } | ||
| return useragent.InContext(ctx, agentKey, product) | ||
| } |
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,42 @@ | ||
| package root | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/libs/agent" | ||
| "github.com/databricks/databricks-sdk-go/useragent" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestAgentClaudeCode(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx = agent.Mock(ctx, agent.ClaudeCode) | ||
|
|
||
| ctx = withAgentInUserAgent(ctx) | ||
| assert.Contains(t, useragent.FromContext(ctx), "agent/claude-code") | ||
| } | ||
|
|
||
| func TestAgentGeminiCLI(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx = agent.Mock(ctx, agent.GeminiCLI) | ||
|
|
||
| ctx = withAgentInUserAgent(ctx) | ||
| assert.Contains(t, useragent.FromContext(ctx), "agent/gemini-cli") | ||
| } | ||
|
|
||
| func TestAgentCursor(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx = agent.Mock(ctx, agent.Cursor) | ||
|
|
||
| ctx = withAgentInUserAgent(ctx) | ||
| assert.Contains(t, useragent.FromContext(ctx), "agent/cursor") | ||
| } | ||
|
|
||
| func TestAgentNotSet(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx = agent.Mock(ctx, "") | ||
|
|
||
| ctx = withAgentInUserAgent(ctx) | ||
| assert.NotContains(t, useragent.FromContext(ctx), "agent/") | ||
| } |
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,76 @@ | ||
| package agent | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/databricks/cli/libs/env" | ||
| ) | ||
|
|
||
| // Product name constants | ||
| const ( | ||
| ClaudeCode = "claude-code" | ||
| GeminiCLI = "gemini-cli" | ||
| Cursor = "cursor" | ||
| ) | ||
|
|
||
| // Environment variable constants | ||
| const ( | ||
| claudeCodeEnvVar = "CLAUDECODE" | ||
| geminiCliEnvVar = "GEMINI_CLI" | ||
| cursorAgentEnvVar = "CURSOR_AGENT" | ||
| ) | ||
|
|
||
| // key is a package-local type for context keys | ||
| type key int | ||
|
|
||
| const ( | ||
| productKey = key(1) | ||
| ) | ||
|
|
||
| // detect performs the actual detection logic. | ||
| // Returns product name string or empty string if detection is ambiguous. | ||
| // Only returns a product if exactly one agent is detected. | ||
| func detect(ctx context.Context) string { | ||
| var detected []string | ||
|
|
||
| if env.Get(ctx, claudeCodeEnvVar) != "" { | ||
| detected = append(detected, ClaudeCode) | ||
| } | ||
|
|
||
| if env.Get(ctx, geminiCliEnvVar) != "" { | ||
| detected = append(detected, GeminiCLI) | ||
| } | ||
|
|
||
| if env.Get(ctx, cursorAgentEnvVar) != "" { | ||
| detected = append(detected, Cursor) | ||
| } | ||
|
|
||
| // Only return a product if exactly one agent is detected | ||
| if len(detected) == 1 { | ||
| return detected[0] | ||
| } | ||
|
|
||
| return "" | ||
| } | ||
|
|
||
| // Detect detects the agent and stores it in context. | ||
| // It returns a new context with the detection result set. | ||
| func Detect(ctx context.Context) context.Context { | ||
| return context.WithValue(ctx, productKey, detect(ctx)) | ||
| } | ||
|
|
||
| // Mock is a helper for tests to mock the detection result. | ||
| func Mock(ctx context.Context, product string) context.Context { | ||
| return context.WithValue(ctx, productKey, product) | ||
| } | ||
|
|
||
| // Product returns the detected agent product name from context. | ||
| // Returns empty string if no agent was detected. | ||
| // Panics if called before Detect() or Mock(). | ||
| func Product(ctx context.Context) string { | ||
| v := ctx.Value(productKey) | ||
| if v == nil { | ||
| panic("agent.Product called without calling agent.Detect first") | ||
| } | ||
| return v.(string) | ||
| } | ||
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,104 @@ | ||
| package agent | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/databricks/cli/libs/env" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDetect(t *testing.T) { | ||
| ctx := context.Background() | ||
| // Clear other agent env vars to ensure clean test environment | ||
| ctx = env.Set(ctx, geminiCliEnvVar, "") | ||
| ctx = env.Set(ctx, cursorAgentEnvVar, "") | ||
| ctx = env.Set(ctx, claudeCodeEnvVar, "1") | ||
|
|
||
| ctx = Detect(ctx) | ||
|
|
||
| assert.Equal(t, ClaudeCode, Product(ctx)) | ||
| } | ||
|
|
||
| func TestProductCalledBeforeDetect(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| require.Panics(t, func() { | ||
| Product(ctx) | ||
| }) | ||
| } | ||
|
|
||
| func TestMock(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx = Mock(ctx, "test-agent") | ||
|
|
||
| assert.Equal(t, "test-agent", Product(ctx)) | ||
| } | ||
|
|
||
| func TestDetectNoAgent(t *testing.T) { | ||
| ctx := context.Background() | ||
| ctx = env.Set(ctx, claudeCodeEnvVar, "") | ||
| ctx = env.Set(ctx, geminiCliEnvVar, "") | ||
| ctx = env.Set(ctx, cursorAgentEnvVar, "") | ||
|
|
||
| ctx = Detect(ctx) | ||
|
|
||
| assert.Equal(t, "", Product(ctx)) | ||
| } | ||
|
|
||
| func TestDetectClaudeCode(t *testing.T) { | ||
| ctx := context.Background() | ||
| // Clear other agent env vars to ensure clean test environment | ||
| ctx = env.Set(ctx, geminiCliEnvVar, "") | ||
| ctx = env.Set(ctx, cursorAgentEnvVar, "") | ||
| ctx = env.Set(ctx, claudeCodeEnvVar, "1") | ||
|
|
||
| result := detect(ctx) | ||
| assert.Equal(t, ClaudeCode, result) | ||
| } | ||
|
|
||
| func TestDetectGeminiCLI(t *testing.T) { | ||
| ctx := context.Background() | ||
| // Clear other agent env vars to ensure clean test environment | ||
| ctx = env.Set(ctx, claudeCodeEnvVar, "") | ||
| ctx = env.Set(ctx, cursorAgentEnvVar, "") | ||
| ctx = env.Set(ctx, geminiCliEnvVar, "1") | ||
|
|
||
| result := detect(ctx) | ||
| assert.Equal(t, GeminiCLI, result) | ||
| } | ||
|
|
||
| func TestDetectCursor(t *testing.T) { | ||
| ctx := context.Background() | ||
| // Clear other agent env vars to ensure clean test environment | ||
| ctx = env.Set(ctx, claudeCodeEnvVar, "") | ||
| ctx = env.Set(ctx, geminiCliEnvVar, "") | ||
| ctx = env.Set(ctx, cursorAgentEnvVar, "1") | ||
|
|
||
| result := detect(ctx) | ||
| assert.Equal(t, Cursor, result) | ||
| } | ||
|
|
||
| func TestDetectMultipleAgents(t *testing.T) { | ||
| ctx := context.Background() | ||
| // Clear all agent env vars first | ||
| ctx = env.Set(ctx, cursorAgentEnvVar, "") | ||
| // If multiple agents are detected, return empty string | ||
| ctx = env.Set(ctx, claudeCodeEnvVar, "1") | ||
| ctx = env.Set(ctx, geminiCliEnvVar, "1") | ||
|
|
||
| result := detect(ctx) | ||
| assert.Equal(t, "", result) | ||
| } | ||
|
|
||
| func TestDetectMultipleAgentsAllThree(t *testing.T) { | ||
| ctx := context.Background() | ||
| // If all three agents are detected, return empty string | ||
| ctx = env.Set(ctx, claudeCodeEnvVar, "1") | ||
| ctx = env.Set(ctx, geminiCliEnvVar, "1") | ||
| ctx = env.Set(ctx, cursorAgentEnvVar, "1") | ||
|
|
||
| result := detect(ctx) | ||
| assert.Equal(t, "", result) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed offline, Codex seems to set
CODEX_SANDBOX.