|
| 1 | +// Copyright 2022-2026 Salesforce, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package useragent |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "runtime" |
| 21 | + "strings" |
| 22 | +) |
| 23 | + |
| 24 | +// AIAgent represents a detected AI coding agent that invoked the CLI. |
| 25 | +type AIAgent struct { |
| 26 | + Name string |
| 27 | + Entry string |
| 28 | +} |
| 29 | + |
| 30 | +// GetAIAgent checks environment variables to determine if the CLI is being run |
| 31 | +// by an AI coding agent. Returns nil if no agent is detected. Detection |
| 32 | +// priority: CLAUDECODE > CODEX_CI > GEMINI_CLI > CLINE_ACTIVE > CURSOR_AGENT > |
| 33 | +// AGENT. |
| 34 | +func GetAIAgent() *AIAgent { |
| 35 | + switch { |
| 36 | + case os.Getenv("CLAUDECODE") == "1": |
| 37 | + return &AIAgent{ |
| 38 | + Name: "claude-code", |
| 39 | + Entry: os.Getenv("CLAUDE_CODE_ENTRYPOINT"), |
| 40 | + } |
| 41 | + case os.Getenv("CODEX_CI") == "1": |
| 42 | + return &AIAgent{Name: "codex"} |
| 43 | + case os.Getenv("GEMINI_CLI") == "1": |
| 44 | + return &AIAgent{Name: "gemini-cli"} |
| 45 | + case os.Getenv("CLINE_ACTIVE") == "true": |
| 46 | + return &AIAgent{Name: "cline"} |
| 47 | + case os.Getenv("CURSOR_AGENT") == "1": |
| 48 | + return &AIAgent{Name: "cursor"} |
| 49 | + case os.Getenv("AGENT") != "": |
| 50 | + return &AIAgent{Name: os.Getenv("AGENT")} |
| 51 | + default: |
| 52 | + return nil |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// GetAIAgentName returns the normalized name of the detected AI agent, or an |
| 57 | +// empty string if no agent is detected. |
| 58 | +func GetAIAgentName() string { |
| 59 | + if aiAgent := GetAIAgent(); aiAgent != nil { |
| 60 | + return aiAgent.Name |
| 61 | + } |
| 62 | + return "" |
| 63 | +} |
| 64 | + |
| 65 | +// BuildUserAgent constructs the HTTP User-Agent header value for the CLI. |
| 66 | +// The base format is "slack-cli/<version> (os: <goos>)". If an AI agent is |
| 67 | +// detected, an " AI-Agent (name: ..., entry: ...)" suffix is appended. |
| 68 | +// |
| 69 | +// Examples: |
| 70 | +// |
| 71 | +// slack-cli/2.38.1 (os: darwin) |
| 72 | +// slack-cli/2.38.1 (os: darwin) AI-Agent (name: claude-code, entry: cli) |
| 73 | +// slack-cli/2.38.1 (os: linux) AI-Agent (name: cursor) |
| 74 | +func BuildUserAgent(cliVersion string) string { |
| 75 | + ua := fmt.Sprintf("slack-cli/%s (os: %s)", cliVersion, runtime.GOOS) |
| 76 | + if aiAgent := GetAIAgent(); aiAgent != nil { |
| 77 | + var parts []string |
| 78 | + parts = append(parts, "name: "+aiAgent.Name) |
| 79 | + if aiAgent.Entry != "" { |
| 80 | + parts = append(parts, "entry: "+aiAgent.Entry) |
| 81 | + } |
| 82 | + ua += " AI-Agent (" + strings.Join(parts, ", ") + ")" |
| 83 | + } |
| 84 | + return ua |
| 85 | +} |
0 commit comments