Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions commands/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func commandQuery(c *cli.Context) error {
}

// Get system context
systemContext, err := getSystemContext(query)
systemContext, err := getSystemContext(query, cfg.AI)
if err != nil {
slog.Warn("Failed to get system context", slog.Any("err", err))
}
Expand Down Expand Up @@ -225,7 +225,7 @@ func sanitizeSuggestedCommand(raw string) string {
return s
}

func getSystemContext(query string) (model.CommandSuggestVariables, error) {
func getSystemContext(query string, ai *model.AIConfig) (model.CommandSuggestVariables, error) {
// Get shell information
shell := os.Getenv("SHELL")
if shell == "" {
Expand All @@ -240,9 +240,25 @@ func getSystemContext(query string) (model.CommandSuggestVariables, error) {
// Get OS information
osInfo := runtime.GOOS

return model.CommandSuggestVariables{
vars := model.CommandSuggestVariables{
Shell: shell,
Os: osInfo,
Query: query,
}, nil
}

// Skip context fields when the user has opted out via config:
// [ai]
// shareContext = false
if ai != nil && ai.ShareContext != nil && !*ai.ShareContext {
return vars, nil
}

if pwd, err := os.Getwd(); err == nil {
vars.Pwd = pwd
}
if host, err := os.Hostname(); err == nil {
vars.Hostname = host
}

return vars, nil
}
13 changes: 10 additions & 3 deletions commands/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,25 +326,32 @@ func (s *queryTestSuite) TestGetSystemContext() {
os.Setenv("SHELL", "/bin/bash")
defer os.Setenv("SHELL", originalShell)

context, err := getSystemContext(query)
context, err := getSystemContext(query, nil)
assert.Nil(s.T(), err)
assert.Equal(s.T(), "bash", context.Shell)
assert.Equal(s.T(), runtime.GOOS, context.Os)
assert.Equal(s.T(), query, context.Query)
Comment on lines +329 to 333
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test for the default case (where ai is nil) should also verify that Pwd and Hostname are populated when ShareContext is not explicitly disabled. This ensures that the "default to true" behavior is correctly implemented and maintained.

Suggested change
context, err := getSystemContext(query, nil)
assert.Nil(s.T(), err)
assert.Equal(s.T(), "bash", context.Shell)
assert.Equal(s.T(), runtime.GOOS, context.Os)
assert.Equal(s.T(), query, context.Query)
context, err := getSystemContext(query, nil)
assert.Nil(s.T(), err)
assert.Equal(s.T(), "bash", context.Shell)
assert.Equal(s.T(), runtime.GOOS, context.Os)
assert.Equal(s.T(), query, context.Query)
// Verify that context fields are populated by default
assert.NotEmpty(s.T(), context.Pwd)
assert.NotEmpty(s.T(), context.Hostname)


// Test with no SHELL environment variable
os.Unsetenv("SHELL")
context, err = getSystemContext(query)
context, err = getSystemContext(query, nil)
assert.Nil(s.T(), err)
assert.Equal(s.T(), "unknown", context.Shell)
assert.Equal(s.T(), runtime.GOOS, context.Os)
assert.Equal(s.T(), query, context.Query)

// Test with full path shell
os.Setenv("SHELL", "/usr/local/bin/zsh")
context, err = getSystemContext(query)
context, err = getSystemContext(query, nil)
assert.Nil(s.T(), err)
assert.Equal(s.T(), "zsh", context.Shell)

// Opt-out: when ShareContext is explicitly false, pwd/hostname are omitted.
disabled := false
context, err = getSystemContext(query, &model.AIConfig{ShareContext: &disabled})
assert.Nil(s.T(), err)
assert.Empty(s.T(), context.Pwd)
assert.Empty(s.T(), context.Hostname)
}

func (s *queryTestSuite) TestQueryCommandWithAlias() {
Expand Down
8 changes: 5 additions & 3 deletions model/ai_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ type AIService interface {
}

type CommandSuggestVariables struct {
Shell string `json:"shell"`
Os string `json:"os"`
Query string `json:"query"`
Shell string `json:"shell"`
Os string `json:"os"`
Query string `json:"query"`
Pwd string `json:"pwd,omitempty"`
Hostname string `json:"hostname,omitempty"`
}

type sseAIService struct{}
Expand Down
3 changes: 3 additions & 0 deletions model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type AIAgentConfig struct {
type AIConfig struct {
Agent AIAgentConfig `toml:"agent,omitempty" yaml:"agent,omitempty" json:"agent,omitempty"`
ShowTips *bool `toml:"showTips" yaml:"showTips" json:"showTips"`
// ShareContext controls whether `shelltime q` sends the working directory
// and hostname alongside the prompt. Defaults to true if unset.
ShareContext *bool `toml:"shareContext,omitempty" yaml:"shareContext,omitempty" json:"shareContext,omitempty"`
}

type CCUsage struct {
Expand Down
Loading