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"
2026-02-28 20:17:12 +00:00
"fmt"
2026-02-28 19:56:23 +00:00
"os"
"strings"
"github.com/fatih/color"
2026-02-28 18:41:20 +00:00
"github.com/spf13/cobra"
2026-02-28 20:52:03 +00:00
"gmgauthier.com/grokkit/config"
2026-03-04 11:04:08 +00:00
"gmgauthier.com/grokkit/internal/grok"
2026-02-28 18:41:20 +00:00
)
2026-02-28 18:28:27 +00:00
2026-03-04 10:49:53 +00:00
var chatCmd = & cobra . Command {
Use : "chat" ,
Short : "Interactive chat with Grok (use --agent for tool-enabled mode)" ,
Long : ` Start a persistent conversation with Grok .
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
2026-03-04 11:04:08 +00:00
Normal mode : coherent , reasoning - focused chat ( uses full model ) .
Agent mode ( -- agent ) : Grok can call tools with safety previews ( uses fast model ) . ` ,
2026-03-04 10:49:53 +00:00
Run : runChat ,
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
}
2026-03-04 10:49:53 +00:00
func init ( ) {
2026-03-04 11:04:08 +00:00
chatCmd . Flags ( ) . Bool ( "agent" , false , "Enable agent mode with tool calling (uses fast non-reasoning model)" )
chatCmd . Flags ( ) . String ( "model" , "" , "Override model" )
2026-03-04 10:49:53 +00:00
rootCmd . AddCommand ( chatCmd )
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
}
2026-03-04 10:49:53 +00:00
func runChat ( cmd * cobra . Command , args [ ] string ) {
agentMode , _ := cmd . Flags ( ) . GetBool ( "agent" )
2026-03-04 11:04:08 +00:00
modelFlag , _ := cmd . Flags ( ) . GetString ( "model" )
2026-03-04 10:49:53 +00:00
2026-03-04 11:04:08 +00:00
// Model switching logic
2026-03-04 10:49:53 +00:00
var model string
2026-03-04 11:04:08 +00:00
if modelFlag != "" {
model = modelFlag
2026-03-04 10:49:53 +00:00
} else if agentMode {
2026-03-04 11:04:08 +00:00
model = config . GetModel ( "chat-agent" , "" ) // we'll add this default next
2026-03-04 10:49:53 +00:00
} else {
model = config . GetModel ( "chat" , "" )
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
}
2026-03-04 11:04:08 +00:00
client := grok . NewClient ( )
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
2026-03-04 11:04:08 +00:00
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." , model ) ,
}
2026-02-28 21:53:35 +00:00
2026-03-04 10:49:53 +00:00
if agentMode {
2026-03-04 11:04:08 +00:00
systemPrompt [ "content" ] = ` You are Grok in Agent Mode .
You have access to tools : edit , scaffold , testgen , lint , commit , etc .
Always use tools when the user wants you to change code , generate tests , lint or commit .
Be concise and action - oriented . After every tool call , wait for the result . `
2026-03-04 10:49:53 +00:00
}
2026-02-28 21:53:35 +00:00
2026-03-04 11:04:08 +00:00
history := loadChatHistory ( )
if history == nil || len ( history ) == 0 {
history = [ ] map [ string ] string { systemPrompt }
} else if history [ 0 ] [ "role" ] != "system" {
history = append ( [ ] map [ string ] string { systemPrompt } , history ... )
} else {
history [ 0 ] = systemPrompt // refresh system prompt
}
color . Cyan ( "┌──────────────────────────────────────────────────────────────┐" )
color . Cyan ( "│ Grokkit Chat %s — Model: %s │" , map [ bool ] string { true : "(Agent Mode)" , false : "" } [ agentMode ] , model )
color . Cyan ( "│ Type /quit to exit │" )
color . Cyan ( "└──────────────────────────────────────────────────────────────┘\n" )
2026-02-28 21:53:35 +00:00
2026-03-04 10:49:53 +00:00
scanner := bufio . NewScanner ( os . Stdin )
for {
2026-03-04 11:04:08 +00:00
color . Yellow ( "You > " )
2026-03-04 10:49:53 +00:00
if ! scanner . Scan ( ) {
break
}
2026-02-28 21:53:35 +00:00
2026-03-04 11:04:08 +00:00
input := strings . TrimSpace ( scanner . Text ( ) )
2026-03-04 10:49:53 +00:00
if input == "" {
continue
}
2026-03-04 11:04:08 +00:00
if input == "/quit" || input == "/q" || input == "exit" {
color . Cyan ( "\nGoodbye 👋\n" )
break
}
2026-02-28 21:53:35 +00:00
2026-03-04 10:49:53 +00:00
history = append ( history , map [ string ] string { "role" : "user" , "content" : input } )
2026-02-28 19:56:23 +00:00
2026-03-04 11:04:08 +00:00
color . Green ( "Grok > " )
2026-03-04 10:49:53 +00:00
reply := client . Stream ( history , model )
2026-02-28 21:53:35 +00:00
2026-03-04 10:49:53 +00:00
history = append ( history , map [ string ] string { "role" : "assistant" , "content" : reply } )
2026-03-04 11:04:08 +00:00
_ = saveChatHistory ( history )
2026-02-28 21:53:35 +00:00
2026-03-04 11:04:08 +00:00
fmt . Println ( )
}
2026-02-28 22:59:16 +00:00
}