2026-02-28 18:41:20 +00:00
|
|
|
package grok
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"bytes"
|
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
|
|
|
"context"
|
2026-02-28 18:41:20 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
"regexp"
|
2026-02-28 18:41:20 +00:00
|
|
|
"strings"
|
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
|
|
|
"time"
|
2026-02-28 18:41:20 +00:00
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
2026-03-01 12:35:21 +00:00
|
|
|
"gmgauthier.com/grokkit/internal/logger"
|
2026-02-28 18:41:20 +00:00
|
|
|
)
|
|
|
|
|
|
2026-03-02 20:20:18 +00:00
|
|
|
// Client represents a client for interacting with the XAI API.
|
|
|
|
|
// It holds the API key and base URL for making requests.
|
2026-02-28 18:41:20 +00:00
|
|
|
type Client struct {
|
|
|
|
|
APIKey string
|
|
|
|
|
BaseURL string
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 20:20:18 +00:00
|
|
|
// NewClient creates and returns a new Client instance.
|
|
|
|
|
// It retrieves the API key from the XAI_API_KEY environment variable.
|
|
|
|
|
// If the variable is not set, it prints an error and exits the program.
|
2026-02-28 18:41:20 +00:00
|
|
|
func NewClient() *Client {
|
|
|
|
|
key := os.Getenv("XAI_API_KEY")
|
|
|
|
|
if key == "" {
|
|
|
|
|
color.Red("Error: XAI_API_KEY environment variable not set")
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
return &Client{
|
|
|
|
|
APIKey: key,
|
|
|
|
|
BaseURL: "https://api.x.ai/v1",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 20:20:18 +00:00
|
|
|
// Stream sends a streaming chat completion request to the API and prints the response live to the terminal.
|
|
|
|
|
// It uses a default temperature of 0.7. This is intended for non-TUI commands.
|
|
|
|
|
// Returns the full response text.
|
2026-02-28 18:41:20 +00:00
|
|
|
func (c *Client) Stream(messages []map[string]string, model string) string {
|
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 c.StreamWithTemp(messages, model, 0.7)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 20:20:18 +00:00
|
|
|
// StreamWithTemp sends a streaming chat completion request to the API with a specified temperature
|
|
|
|
|
// and prints the response live to the terminal.
|
|
|
|
|
// Returns the full response text.
|
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 (c *Client) StreamWithTemp(messages []map[string]string, model string, temperature float64) string {
|
|
|
|
|
return c.streamInternal(messages, model, temperature, true)
|
2026-02-28 21:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-02 20:20:18 +00:00
|
|
|
// StreamSilent sends a streaming chat completion request to the API and returns the full response text
|
|
|
|
|
// without printing it live. It uses a default temperature of 0.7.
|
|
|
|
|
// This is intended for TUI and agent use.
|
2026-02-28 21:53:35 +00:00
|
|
|
func (c *Client) StreamSilent(messages []map[string]string, model string) string {
|
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 c.streamInternal(messages, model, 0.7, false)
|
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
|
|
|
func (c *Client) streamInternal(messages []map[string]string, model string, temperature float64, printLive bool) string {
|
2026-03-01 12:35:21 +00:00
|
|
|
startTime := time.Now()
|
2026-02-28 18:41:20 +00:00
|
|
|
url := c.BaseURL + "/chat/completions"
|
2026-03-01 12:35:21 +00:00
|
|
|
|
|
|
|
|
logger.Debug("preparing API request",
|
|
|
|
|
"model", model,
|
|
|
|
|
"temperature", temperature,
|
|
|
|
|
"message_count", len(messages),
|
|
|
|
|
"stream", true)
|
|
|
|
|
|
2026-02-28 18:41:20 +00:00
|
|
|
payload := map[string]interface{}{
|
|
|
|
|
"model": model,
|
|
|
|
|
"messages": messages,
|
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
|
|
|
"temperature": temperature,
|
2026-02-28 18:41:20 +00:00
|
|
|
"stream": true,
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 12:08:49 +00:00
|
|
|
body, err := json.Marshal(payload)
|
|
|
|
|
if err != nil {
|
2026-03-01 12:35:21 +00:00
|
|
|
logger.Error("failed to marshal API request", "error", err)
|
2026-03-01 12:08:49 +00:00
|
|
|
color.Red("Failed to marshal request: %v", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(body))
|
2026-03-01 12:08:49 +00:00
|
|
|
if err != nil {
|
2026-03-01 12:35:21 +00:00
|
|
|
logger.Error("failed to create HTTP request", "error", err, "url", url)
|
2026-03-01 12:08:49 +00:00
|
|
|
color.Red("Failed to create request: %v", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2026-02-28 18:41:20 +00:00
|
|
|
req.Header.Set("Authorization", "Bearer "+c.APIKey)
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
2026-03-01 12:35:21 +00:00
|
|
|
logger.Info("sending API request", "model", model, "url", url)
|
|
|
|
|
|
2026-02-28 18:41:20 +00:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
2026-03-01 12:35:21 +00:00
|
|
|
logger.Error("API request failed",
|
|
|
|
|
"error", err,
|
|
|
|
|
"model", model,
|
|
|
|
|
"duration_ms", time.Since(startTime).Milliseconds())
|
2026-02-28 19:56:23 +00:00
|
|
|
color.Red("Request failed: %v", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2026-02-28 21:53:35 +00:00
|
|
|
defer resp.Body.Close()
|
2026-02-28 18:41:20 +00:00
|
|
|
|
2026-03-01 12:35:21 +00:00
|
|
|
logger.Debug("API response received",
|
|
|
|
|
"status", resp.Status,
|
|
|
|
|
"status_code", resp.StatusCode,
|
|
|
|
|
"duration_ms", time.Since(startTime).Milliseconds())
|
|
|
|
|
|
2026-02-28 18:41:20 +00:00
|
|
|
var fullReply strings.Builder
|
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
2026-03-01 12:35:21 +00:00
|
|
|
chunkCount := 0
|
2026-02-28 18:41:20 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
|
line := scanner.Text()
|
|
|
|
|
if strings.HasPrefix(line, "data: ") {
|
2026-02-28 20:17:12 +00:00
|
|
|
data := strings.TrimPrefix(line, "data: ")
|
2026-02-28 18:41:20 +00:00
|
|
|
if data == "[DONE]" {
|
|
|
|
|
break
|
|
|
|
|
}
|
2026-02-28 20:17:12 +00:00
|
|
|
var chunk map[string]any
|
2026-02-28 18:41:20 +00:00
|
|
|
if json.Unmarshal([]byte(data), &chunk) == nil {
|
2026-02-28 20:17:12 +00:00
|
|
|
if choices, ok := chunk["choices"].([]any); ok && len(choices) > 0 {
|
|
|
|
|
if delta, ok := choices[0].(map[string]any)["delta"].(map[string]any); ok {
|
|
|
|
|
if content, ok := delta["content"].(string); ok && content != "" {
|
2026-03-01 12:35:21 +00:00
|
|
|
chunkCount++
|
2026-02-28 18:41:20 +00:00
|
|
|
fullReply.WriteString(content)
|
2026-02-28 21:53:35 +00:00
|
|
|
if printLive {
|
|
|
|
|
fmt.Print(content)
|
|
|
|
|
}
|
2026-02-28 18:41:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-28 21:53:35 +00:00
|
|
|
if printLive {
|
|
|
|
|
fmt.Println()
|
|
|
|
|
}
|
2026-03-01 12:35:21 +00:00
|
|
|
|
|
|
|
|
responseLength := fullReply.Len()
|
|
|
|
|
duration := time.Since(startTime)
|
|
|
|
|
|
|
|
|
|
logger.Info("API request completed",
|
|
|
|
|
"model", model,
|
|
|
|
|
"response_length", responseLength,
|
|
|
|
|
"chunks_received", chunkCount,
|
|
|
|
|
"duration_ms", duration.Milliseconds(),
|
|
|
|
|
"duration", duration.String())
|
|
|
|
|
|
2026-02-28 18:41:20 +00:00
|
|
|
return fullReply.String()
|
|
|
|
|
}
|
2026-02-28 20:31:02 +00:00
|
|
|
|
2026-03-02 20:20:18 +00:00
|
|
|
// CleanCodeResponse removes Markdown code fences from the input text and returns the pure code content.
|
|
|
|
|
// It removes opening fences (with optional language tags) and closing fences, then trims leading and trailing whitespace.
|
|
|
|
|
// Internal blank lines are preserved as they may be intentional in code.
|
2026-02-28 20:31:02 +00:00
|
|
|
func CleanCodeResponse(text string) string {
|
chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
fence := "```"
|
|
|
|
|
|
|
|
|
|
// Remove any line that starts with the fence (opening fence, possibly with language tag)
|
|
|
|
|
text = regexp.MustCompile(`(?m)^`+regexp.QuoteMeta(fence)+`.*$`).ReplaceAllString(text, "")
|
|
|
|
|
|
|
|
|
|
// Remove any line that is just the fence (closing fence)
|
|
|
|
|
text = regexp.MustCompile(`(?m)^`+regexp.QuoteMeta(fence)+`\s*$`).ReplaceAllString(text, "")
|
|
|
|
|
|
|
|
|
|
// Trim only leading and trailing whitespace.
|
|
|
|
|
// Do NOT collapse internal blank lines — they are intentional in code.
|
2026-02-28 20:31:02 +00:00
|
|
|
text = strings.TrimSpace(text)
|
chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
|
2026-02-28 20:31:02 +00:00
|
|
|
return text
|
2026-03-02 20:20:18 +00:00
|
|
|
}
|