feat: add context.Context + unexport client fields #14
@@ -234,6 +234,7 @@ func (c *Client) GetAllFilesInPath(ctx context.Context, owner, repo, path string
|
||||
case "dir":
|
||||
subResults, err := c.GetAllFilesInPath(ctx, owner, repo, entry.Path)
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not recurse into %s: %v", entry.Path, err)
|
||||
continue
|
||||
}
|
||||
for k, v := range subResults {
|
||||
|
||||
+1
-1
@@ -31,13 +31,13 @@ func NewClient(baseURL, apiKey, model string) *Client {
|
||||
}
|
||||
}
|
||||
|
||||
// WithTemperature sets the temperature for LLM requests (0 = omit, uses server default).
|
||||
// WithTimeout sets the HTTP request timeout for LLM calls (default 5 minutes).
|
||||
func (c *Client) WithTimeout(d time.Duration) *Client {
|
||||
c.http.Timeout = d
|
||||
return c
|
||||
}
|
||||
|
||||
// WithTemperature sets the temperature for LLM requests (0 = omit, uses server default).
|
||||
func (c *Client) WithTemperature(t float64) *Client {
|
||||
c.temperature = t
|
||||
return c
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestComplete_Success(t *testing.T) {
|
||||
@@ -186,3 +187,24 @@ func TestComplete_TemperatureIncludedWhenSet(t *testing.T) {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithTimeout(t *testing.T) {
|
||||
client := NewClient("http://example.com", "key", "model")
|
||||
result := client.WithTimeout(10 * time.Second)
|
||||
if result != client {
|
||||
t.Error("WithTimeout should return the same client for chaining")
|
||||
}
|
||||
// Verify timeout causes failure on slow server
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"choices":[{"message":{"content":"ok"}}]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
shortClient := NewClient(server.URL, "key", "model").WithTimeout(50 * time.Millisecond)
|
||||
_, err := shortClient.Complete(context.Background(), []Message{{Role: "user", Content: "hi"}})
|
||||
if err == nil {
|
||||
t.Error("expected timeout error with 50ms timeout and 200ms server delay")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user