-
Notifications
You must be signed in to change notification settings - Fork 94
Add AIDescription field and AI-mode help resolver #1563
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "os" | ||
| "strconv" | ||
|
|
||
| "github.com/jfrog/jfrog-cli-core/v2/common/commands" | ||
| ) | ||
|
|
||
| // EnvAIHelp opts a process in or out of AI-oriented help text rendering. | ||
| // Values parseable by strconv.ParseBool (1/t/true/0/f/false, case-insensitive) | ||
| // force the mode on or off. Unset or unparseable falls back to | ||
| // ExecutionContext.IsAgent auto-detection. | ||
| const EnvAIHelp = "JFROG_CLI_AI_HELP" | ||
|
|
||
| // AIAgentDetector reports whether the running process is an AI agent. | ||
| // The default consults the memoized ExecutionContext in | ||
| // common/commands. Exposed as a variable so tests can inject a deterministic | ||
| // answer — DetectExecutionContext caches via sync.Once and cannot be reset. | ||
| var AIAgentDetector = func() bool { | ||
| return commands.DetectExecutionContext().IsAgent | ||
| } | ||
|
|
||
| // AIHelpEnabled reports whether help rendering should prefer AIDescription | ||
| // over Description. The env var, when parseable as a bool, wins over | ||
| // auto-detection so users can opt out of agent-flavored help. | ||
| func AIHelpEnabled() bool { | ||
| if v, ok := os.LookupEnv(EnvAIHelp); ok { | ||
| if b, err := strconv.ParseBool(v); err == nil { | ||
| return b | ||
| } | ||
| } | ||
| return AIAgentDetector() | ||
| } | ||
|
|
||
| // ResolveDescription returns the AI variant when it is non-empty and AI help | ||
| // is enabled; otherwise it returns the human variant. An empty ai always | ||
| // falls back to human, so partial backfill across commands is safe. | ||
| func ResolveDescription(human, ai string) string { | ||
| if ai != "" && AIHelpEnabled() { | ||
| return ai | ||
| } | ||
| return human | ||
| } | ||
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,113 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // withAgentDetector installs an AIAgentDetector for the duration of a test. | ||
| // commands.DetectExecutionContext is sync.Once-memoized, so we can't reach | ||
| // the underlying detection by toggling env vars — instead we replace the | ||
| // hook AIHelpEnabled uses. | ||
| func withAgentDetector(t *testing.T, isAgent bool) { | ||
| t.Helper() | ||
| prev := AIAgentDetector | ||
| AIAgentDetector = func() bool { return isAgent } | ||
| t.Cleanup(func() { AIAgentDetector = prev }) | ||
| } | ||
|
|
||
| func TestResolveDescription(t *testing.T) { | ||
| const human = "human help" | ||
| const ai = "ai help" | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| envAIHelp string // pass "" to leave env unset | ||
| setEnv bool // if false, don't touch the env var at all | ||
| isAgent bool | ||
| ai string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "env force-on, no agent -> AI text", | ||
| envAIHelp: "true", | ||
| setEnv: true, | ||
| isAgent: false, | ||
| ai: ai, | ||
| expected: ai, | ||
| }, | ||
| { | ||
| name: "env force-off beats detected agent -> human text", | ||
| envAIHelp: "false", | ||
| setEnv: true, | ||
| isAgent: true, | ||
| ai: ai, | ||
| expected: human, | ||
| }, | ||
| { | ||
| name: "no env + agent detected -> AI text", | ||
| setEnv: false, | ||
| isAgent: true, | ||
| ai: ai, | ||
| expected: ai, | ||
| }, | ||
| { | ||
| name: "no env + no agent -> human text", | ||
| setEnv: false, | ||
| isAgent: false, | ||
| ai: ai, | ||
| expected: human, | ||
| }, | ||
| { | ||
| name: "agent detected + empty AI -> human fallback", | ||
| setEnv: false, | ||
| isAgent: true, | ||
| ai: "", | ||
| expected: human, | ||
| }, | ||
| { | ||
| name: "invalid env value falls back to detection (no agent here)", | ||
| envAIHelp: "maybe", | ||
| setEnv: true, | ||
| isAgent: false, | ||
| ai: ai, | ||
| expected: human, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| withAgentDetector(t, tc.isAgent) | ||
| if tc.setEnv { | ||
| t.Setenv(EnvAIHelp, tc.envAIHelp) | ||
| } | ||
| assert.Equal(t, tc.expected, ResolveDescription(human, tc.ai)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAIHelpEnabledEnvParsing(t *testing.T) { | ||
| // Detection deliberately returns true so we can prove env-parsing | ||
| // short-circuits: only truthy/falsy env values should affect the result; | ||
| // invalid values must fall through to the (here forced-true) detector. | ||
| tests := []struct { | ||
| value string | ||
| expected bool | ||
| }{ | ||
| {"true", true}, | ||
| {"1", true}, | ||
| {"TRUE", true}, | ||
| {"false", false}, | ||
| {"0", false}, | ||
| {"maybe", true}, // unparseable -> falls back to AIAgentDetector (true) | ||
| {"", true}, // empty -> ParseBool error -> falls back to detector | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run("value="+tc.value, func(t *testing.T) { | ||
| withAgentDetector(t, true) | ||
| t.Setenv(EnvAIHelp, tc.value) | ||
| assert.Equal(t, tc.expected, AIHelpEnabled()) | ||
| }) | ||
| } | ||
| } |
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
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.
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.
nit: IsAIAgentRequest or similar would match better with a boolean return