diff --git a/internal/grok/client.go b/internal/grok/client.go index 9439ba7..4bc63f6 100644 --- a/internal/grok/client.go +++ b/internal/grok/client.go @@ -16,11 +16,16 @@ import ( "gmgauthier.com/grokkit/internal/logger" ) +// Client represents a client for interacting with the XAI API. +// It holds the API key and base URL for making requests. type Client struct { APIKey string BaseURL string } +// 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. func NewClient() *Client { key := os.Getenv("XAI_API_KEY") if key == "" { @@ -33,17 +38,23 @@ func NewClient() *Client { } } -// Stream prints live to terminal (used by non-TUI commands) +// 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. func (c *Client) Stream(messages []map[string]string, model string) string { return c.StreamWithTemp(messages, model, 0.7) } -// StreamWithTemp allows specifying temperature +// 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. func (c *Client) StreamWithTemp(messages []map[string]string, model string, temperature float64) string { return c.streamInternal(messages, model, temperature, true) } -// StreamSilent returns the full text without printing (used by TUI and agent) +// 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. func (c *Client) StreamSilent(messages []map[string]string, model string) string { return c.streamInternal(messages, model, 0.7, false) } @@ -145,7 +156,9 @@ func (c *Client) streamInternal(messages []map[string]string, model string, temp return fullReply.String() } -// CleanCodeResponse removes markdown fences and returns pure code content +// 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. func CleanCodeResponse(text string) string { fence := "```" @@ -160,4 +173,4 @@ func CleanCodeResponse(text string) string { text = strings.TrimSpace(text) return text -} +} \ No newline at end of file