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"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
|
APIKey string
|
|
|
|
|
BaseURL string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// Stream prints live to terminal (used by non-TUI commands)
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StreamWithTemp allows specifying temperature
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// StreamSilent returns the full text without printing (used by TUI and agent)
|
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-02-28 18:41:20 +00:00
|
|
|
url := c.BaseURL + "/chat/completions"
|
|
|
|
|
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 {
|
|
|
|
|
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 {
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
|
if err != nil {
|
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
|
|
|
|
|
|
|
|
var fullReply strings.Builder
|
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
|
|
|
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-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-02-28 18:41:20 +00:00
|
|
|
return fullReply.String()
|
|
|
|
|
}
|
2026-02-28 20:31:02 +00:00
|
|
|
|
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
|
|
|
// CleanCodeResponse removes markdown fences and returns pure code content
|
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-02-28 22:59:16 +00:00
|
|
|
}
|