docs(client): add documentation comments to Client struct and methods
Add detailed GoDoc-style comments for the Client struct, NewClient function, and various streaming methods to improve code readability and documentation. Also include a comment for CleanCodeResponse and minor formatting adjustments.
This commit is contained in:
parent
0aa806be70
commit
f763976a27
@ -16,11 +16,16 @@ import (
|
|||||||
"gmgauthier.com/grokkit/internal/logger"
|
"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 {
|
type Client struct {
|
||||||
APIKey string
|
APIKey string
|
||||||
BaseURL 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 {
|
func NewClient() *Client {
|
||||||
key := os.Getenv("XAI_API_KEY")
|
key := os.Getenv("XAI_API_KEY")
|
||||||
if 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 {
|
func (c *Client) Stream(messages []map[string]string, model string) string {
|
||||||
return c.StreamWithTemp(messages, model, 0.7)
|
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 {
|
func (c *Client) StreamWithTemp(messages []map[string]string, model string, temperature float64) string {
|
||||||
return c.streamInternal(messages, model, temperature, true)
|
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 {
|
func (c *Client) StreamSilent(messages []map[string]string, model string) string {
|
||||||
return c.streamInternal(messages, model, 0.7, false)
|
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()
|
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 {
|
func CleanCodeResponse(text string) string {
|
||||||
fence := "```"
|
fence := "```"
|
||||||
|
|
||||||
@ -160,4 +173,4 @@ func CleanCodeResponse(text string) string {
|
|||||||
text = strings.TrimSpace(text)
|
text = strings.TrimSpace(text)
|
||||||
|
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user