feat: bootstrap goaichat CLI and config system
This commit is contained in:
110
internal/openai/client.go
Normal file
110
internal/openai/client.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const defaultTimeout = 30 * time.Second
|
||||
|
||||
// Client wraps HTTP access to the OpenAI-compatible Chat Completions API.
|
||||
type Client struct {
|
||||
apiKey string
|
||||
baseURL string
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
// ClientOption customizes client construction.
|
||||
type ClientOption func(*Client)
|
||||
|
||||
// WithHTTPClient overrides the default HTTP client.
|
||||
func WithHTTPClient(hc *http.Client) ClientOption {
|
||||
return func(c *Client) {
|
||||
c.httpClient = hc
|
||||
}
|
||||
}
|
||||
|
||||
// WithBaseURL overrides the default base URL.
|
||||
func WithBaseURL(url string) ClientOption {
|
||||
return func(c *Client) {
|
||||
c.baseURL = url
|
||||
}
|
||||
}
|
||||
|
||||
// NewClient creates a Client with the provided API key and options.
|
||||
func NewClient(apiKey string, opts ...ClientOption) (*Client, error) {
|
||||
apiKey = strings.TrimSpace(apiKey)
|
||||
if apiKey == "" {
|
||||
return nil, errors.New("api key cannot be empty")
|
||||
}
|
||||
|
||||
client := &Client{
|
||||
apiKey: apiKey,
|
||||
baseURL: "https://api.openai.com/v1",
|
||||
httpClient: &http.Client{
|
||||
Timeout: defaultTimeout,
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(client)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// CreateChatCompletion issues a chat completion request.
|
||||
func (c *Client) CreateChatCompletion(ctx context.Context, req ChatCompletionRequest) (*ChatCompletionResponse, error) {
|
||||
if c == nil {
|
||||
return nil, errors.New("client is nil")
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode request: %w", err)
|
||||
}
|
||||
|
||||
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/chat/completions", bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+c.apiKey)
|
||||
|
||||
resp, err := c.httpClient.Do(httpReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("execute request: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||
return decodeSuccess(resp.Body)
|
||||
}
|
||||
|
||||
return nil, decodeError(resp.Body, resp.StatusCode)
|
||||
}
|
||||
|
||||
func decodeSuccess(r io.Reader) (*ChatCompletionResponse, error) {
|
||||
var response ChatCompletionResponse
|
||||
if err := json.NewDecoder(r).Decode(&response); err != nil {
|
||||
return nil, fmt.Errorf("decode response: %w", err)
|
||||
}
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
func decodeError(r io.Reader, status int) error {
|
||||
var apiErr ErrorResponse
|
||||
if err := json.NewDecoder(r).Decode(&apiErr); err != nil {
|
||||
return fmt.Errorf("api error (status %d): failed to decode body: %w", status, err)
|
||||
}
|
||||
|
||||
return fmt.Errorf("api error (status %d): %s", status, apiErr.Error.Message)
|
||||
}
|
93
internal/openai/client_test.go
Normal file
93
internal/openai/client_test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package openai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewClient_EmptyKey(t *testing.T) {
|
||||
if _, err := NewClient(" "); err == nil {
|
||||
t.Fatal("expected error for empty API key")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatCompletion_Success(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if got, want := r.Header.Get("Authorization"), "Bearer test-key"; got != want {
|
||||
t.Fatalf("expected auth header %q, got %q", want, got)
|
||||
}
|
||||
|
||||
if got, want := r.URL.Path, "/chat/completions"; got != want {
|
||||
t.Fatalf("expected path %q, got %q", want, got)
|
||||
}
|
||||
|
||||
response := ChatCompletionResponse{
|
||||
ID: "chatcmpl-1",
|
||||
Object: "chat.completion",
|
||||
Choices: []ChatCompletionChoice{
|
||||
{
|
||||
Index: 0,
|
||||
Message: ChatMessage{
|
||||
Role: "assistant",
|
||||
Content: "Hello!",
|
||||
},
|
||||
FinishReason: "stop",
|
||||
},
|
||||
},
|
||||
Usage: Usage{PromptTokens: 1, CompletionTokens: 1, TotalTokens: 2},
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
t.Fatalf("failed to encode response: %v", err)
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
client, err := NewClient("test-key", WithBaseURL(ts.URL), WithHTTPClient(ts.Client()))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient returned error: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
|
||||
resp, err := client.CreateChatCompletion(ctx, ChatCompletionRequest{
|
||||
Model: "gpt-test",
|
||||
Messages: []ChatMessage{
|
||||
{Role: "user", Content: "Hello?"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateChatCompletion returned error: %v", err)
|
||||
}
|
||||
|
||||
if resp.Choices[0].Message.Content != "Hello!" {
|
||||
t.Fatalf("unexpected response content: %q", resp.Choices[0].Message.Content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatCompletion_Error(t *testing.T) {
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
_ = json.NewEncoder(w).Encode(ErrorResponse{Error: APIError{Message: "invalid"}})
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
client, err := NewClient("test-key", WithBaseURL(ts.URL), WithHTTPClient(ts.Client()))
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient returned error: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = client.CreateChatCompletion(ctx, ChatCompletionRequest{Model: "gpt-test"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unauthorized response")
|
||||
}
|
||||
}
|
51
internal/openai/types.go
Normal file
51
internal/openai/types.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package openai
|
||||
|
||||
// ChatMessage represents a single message within a chat completion request or response.
|
||||
type ChatMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// ChatCompletionRequest encapsulates the payload for the OpenAI Chat Completions API.
|
||||
type ChatCompletionRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []ChatMessage `json:"messages"`
|
||||
MaxTokens *int `json:"max_tokens,omitempty"`
|
||||
Temperature *float64 `json:"temperature,omitempty"`
|
||||
Stream bool `json:"stream,omitempty"`
|
||||
}
|
||||
|
||||
// ChatCompletionChoice captures an individual response choice returned from the API.
|
||||
type ChatCompletionChoice struct {
|
||||
Index int `json:"index"`
|
||||
Message ChatMessage `json:"message"`
|
||||
FinishReason string `json:"finish_reason"`
|
||||
}
|
||||
|
||||
// Usage captures token accounting for a chat completion call.
|
||||
type Usage struct {
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
CompletionTokens int `json:"completion_tokens"`
|
||||
TotalTokens int `json:"total_tokens"`
|
||||
}
|
||||
|
||||
// ChatCompletionResponse represents the top-level response payload from the API.
|
||||
type ChatCompletionResponse struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
Choices []ChatCompletionChoice `json:"choices"`
|
||||
Usage Usage `json:"usage"`
|
||||
}
|
||||
|
||||
// APIError captures structured error responses returned by the API.
|
||||
type APIError struct {
|
||||
Message string `json:"message"`
|
||||
Type string `json:"type"`
|
||||
Param string `json:"param"`
|
||||
Code any `json:"code"`
|
||||
}
|
||||
|
||||
// ErrorResponse is returned on non-2xx responses.
|
||||
type ErrorResponse struct {
|
||||
Error APIError `json:"error"`
|
||||
}
|
Reference in New Issue
Block a user