2026-02-28 18:03:12 +00:00
package cmd
2026-02-28 18:28:27 +00:00
2026-02-28 18:41:20 +00:00
import (
2026-02-28 23:03:53 +00:00
"bufio"
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
"encoding/json"
2026-02-28 20:17:12 +00:00
"fmt"
2026-02-28 19:56:23 +00:00
"os"
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
"path/filepath"
2026-02-28 19:56:23 +00:00
"strings"
"github.com/fatih/color"
2026-02-28 18:41:20 +00:00
"github.com/spf13/cobra"
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
"github.com/spf13/viper"
2026-02-28 20:52:03 +00:00
"gmgauthier.com/grokkit/config"
2026-02-28 18:41:20 +00:00
"gmgauthier.com/grokkit/internal/grok"
)
2026-02-28 18:28:27 +00:00
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
type ChatHistory struct {
Messages [ ] map [ string ] string ` json:"messages" `
}
func loadChatHistory ( ) [ ] map [ string ] string {
histFile := getChatHistoryFile ( )
chore(lint): enhance golangci configuration and add security annotations
- Expand .golangci.yml with more linters (bodyclose, errcheck, etc.), settings for govet, revive, gocritic, gosec
- Add // nolint:gosec comments for intentional file operations and subprocesses
- Change file write permissions from 0644 to 0600 for better security
- Refactor loops, error handling, and test parallelism with t.Parallel()
- Minor fixes: ignore unused args, use errors.Is, adjust mkdir permissions to 0750
2026-03-04 20:00:32 +00:00
// nolint:gosec // intentional file read from config/home
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
data , err := os . ReadFile ( histFile )
if err != nil {
return nil
}
var hist ChatHistory
if err := json . Unmarshal ( data , & hist ) ; err != nil {
return nil
}
return hist . Messages
}
func saveChatHistory ( messages [ ] map [ string ] string ) error {
histFile := getChatHistoryFile ( )
hist := ChatHistory { Messages : messages }
data , err := json . MarshalIndent ( hist , "" , " " )
if err != nil {
return err
}
chore(lint): enhance golangci configuration and add security annotations
- Expand .golangci.yml with more linters (bodyclose, errcheck, etc.), settings for govet, revive, gocritic, gosec
- Add // nolint:gosec comments for intentional file operations and subprocesses
- Change file write permissions from 0644 to 0600 for better security
- Refactor loops, error handling, and test parallelism with t.Parallel()
- Minor fixes: ignore unused args, use errors.Is, adjust mkdir permissions to 0750
2026-03-04 20:00:32 +00:00
return os . WriteFile ( histFile , data , 0600 )
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
}
func getChatHistoryFile ( ) string {
configFile := viper . GetString ( "chat.history_file" )
if configFile != "" {
return configFile
}
home , _ := os . UserHomeDir ( )
if home == "" {
home = "."
}
histDir := filepath . Join ( home , ".config" , "grokkit" )
chore(lint): enhance golangci configuration and add security annotations
- Expand .golangci.yml with more linters (bodyclose, errcheck, etc.), settings for govet, revive, gocritic, gosec
- Add // nolint:gosec comments for intentional file operations and subprocesses
- Change file write permissions from 0644 to 0600 for better security
- Refactor loops, error handling, and test parallelism with t.Parallel()
- Minor fixes: ignore unused args, use errors.Is, adjust mkdir permissions to 0750
2026-03-04 20:00:32 +00:00
_ = os . MkdirAll ( histDir , 0750 ) // Ignore error, WriteFile will catch it
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
return filepath . Join ( histDir , "chat_history.json" )
}
2026-02-28 20:17:12 +00:00
var chatCmd = & cobra . Command {
Use : "chat" ,
2026-02-28 23:03:53 +00:00
Short : "Simple interactive CLI chat with Grok (full history + streaming)" ,
2026-02-28 20:17:12 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2026-02-28 20:52:03 +00:00
modelFlag , _ := cmd . Flags ( ) . GetString ( "model" )
2026-03-02 16:56:56 +00:00
model := config . GetModel ( "chat" , modelFlag )
2026-02-28 20:52:03 +00:00
2026-02-28 23:03:53 +00:00
client := grok . NewClient ( )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
// Strong system prompt to lock in correct model identity
systemPrompt := map [ string ] string {
"role" : "system" ,
"content" : fmt . Sprintf ( "You are Grok 4, the latest and most powerful model from xAI (2026). You are currently running as `%s`. Be helpful, truthful, and a little irreverent. Never claim to be an older model." , model ) ,
}
2026-02-28 21:53:35 +00:00
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
// Load history or start fresh
history := loadChatHistory ( )
if history == nil {
history = [ ] map [ string ] string { systemPrompt }
} else {
// Update system prompt in loaded history
if len ( history ) > 0 && history [ 0 ] [ "role" ] == "system" {
history [ 0 ] = systemPrompt
} else {
history = append ( [ ] map [ string ] string { systemPrompt } , history ... )
}
}
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
color . Cyan ( "┌──────────────────────────────────────────────────────────────┐" )
color . Cyan ( "│ Grokkit Chat — Model: %s │" , model )
color . Cyan ( "│ Type /quit or Ctrl+C to exit │" )
color . Cyan ( "└──────────────────────────────────────────────────────────────┘\n" )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
scanner := bufio . NewScanner ( os . Stdin )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
for {
color . Yellow ( "You > " )
if ! scanner . Scan ( ) {
break
}
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
input := strings . TrimSpace ( scanner . Text ( ) )
2026-02-28 20:17:12 +00:00
if input == "" {
2026-02-28 23:03:53 +00:00
continue
2026-02-28 19:56:23 +00:00
}
2026-02-28 23:03:53 +00:00
if input == "/quit" || input == "/q" || input == "exit" {
color . Cyan ( "\nGoodbye 👋\n" )
break
2026-02-28 19:56:23 +00:00
}
2026-02-28 23:03:53 +00:00
history = append ( history , map [ string ] string { "role" : "user" , "content" : input } )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
color . Green ( "Grok > " )
reply := client . Stream ( history , model )
2026-02-28 21:53:35 +00:00
2026-02-28 23:03:53 +00:00
history = append ( history , map [ string ] string { "role" : "assistant" , "content" : reply } )
2026-02-28 21:53:35 +00:00
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
// Save history after each exchange
_ = saveChatHistory ( history )
2026-02-28 23:03:53 +00:00
fmt . Println ( )
2026-02-28 21:53:35 +00:00
}
2026-02-28 23:03:53 +00:00
} ,
2026-02-28 22:59:16 +00:00
}