Skip to content

Commit

Permalink
fix: Cleared the staticcheck with XTestTimeoutKey
Browse files Browse the repository at this point in the history
  • Loading branch information
teilomillet committed Dec 22, 2024
1 parent 623f314 commit 8b00b62
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion server/handlers/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (h *CompletionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Create context with timeout header if present
ctx := r.Context()
if timeoutHeader := r.Header.Get("X-Test-Timeout"); timeoutHeader != "" {
ctx = context.WithValue(ctx, "X-Test-Timeout", timeoutHeader)
ctx = context.WithValue(ctx, middleware.XTestTimeoutKey, timeoutHeader)
r = r.WithContext(ctx)

Check failure on line 347 in server/handlers/completion.go

View workflow job for this annotation

GitHub Actions / Test & Lint

SA4006: this value of `r` is never used (staticcheck)

Check failure on line 347 in server/handlers/completion.go

View workflow job for this annotation

GitHub Actions / Test & Lint

SA4006: this value of `r` is never used (staticcheck)
}

Expand Down
2 changes: 1 addition & 1 deletion server/handlers/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestCompletionHandlerIntegration(t *testing.T) {
// Create mock LLM
mockLLM := mocks.NewMockLLM(func(ctx context.Context, prompt *gollm.Prompt) (string, error) {
// If context has timeout header, simulate timeout
if ctx.Value("X-Test-Timeout") != nil {
if ctx.Value(middleware.XTestTimeoutKey) != nil {
// Sleep longer than the timeout
time.Sleep(5 * time.Second)
}
Expand Down
17 changes: 9 additions & 8 deletions server/mocks/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/teilomillet/gollm"
"github.com/teilomillet/gollm/llm"
"github.com/teilomillet/gollm/utils"
"github.com/teilomillet/hapax/server/middleware"
)

// MockLLM implements a mock LLM for testing purposes.
Expand All @@ -24,27 +25,27 @@ import (
// })
type MockLLM struct {
GenerateFunc func(context.Context, *gollm.Prompt) (string, error)
DebugFunc func(string, ...interface{})
Provider string // Provider name for testing
Model string // Model name for testing
DebugFunc func(string, ...interface{})
Provider string // Provider name for testing
Model string // Model name for testing
}

// NewMockLLM creates a new MockLLM with optional generate function.
// If generateFunc is nil, Generate will return empty string with no error.
func NewMockLLM(generateFunc func(context.Context, *gollm.Prompt) (string, error)) *MockLLM {
return &MockLLM{
GenerateFunc: generateFunc,
Provider: "mock",
Model: "mock-model",
Provider: "mock",
Model: "mock-model",
}
}

// NewMockLLMWithConfig creates a new MockLLM with specific provider and model names
func NewMockLLMWithConfig(provider, model string, generateFunc func(context.Context, *gollm.Prompt) (string, error)) *MockLLM {
return &MockLLM{
GenerateFunc: generateFunc,
Provider: provider,
Model: model,
Provider: provider,
Model: model,
}
}

Expand All @@ -53,7 +54,7 @@ func NewMockLLMWithConfig(provider, model string, generateFunc func(context.Cont
// The opts parameter is ignored in the mock to simplify testing.
func (m *MockLLM) Generate(ctx context.Context, prompt *gollm.Prompt, opts ...llm.GenerateOption) (string, error) {
// Check for timeout header
if ctx.Value("X-Test-Timeout") != nil {
if ctx.Value(middleware.XTestTimeoutKey) != nil {
// Sleep longer than the timeout
time.Sleep(10 * time.Second)
return "", context.DeadlineExceeded
Expand Down
5 changes: 3 additions & 2 deletions server/processing/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/teilomillet/gollm"
"github.com/teilomillet/hapax/config"
"github.com/teilomillet/hapax/server/middleware"
)

// Processor handles request processing and response formatting for LLM interactions.
Expand All @@ -25,7 +26,7 @@ import (
// The Processor is designed to be reusable across different request types
// while maintaining consistent formatting and error handling.
type Processor struct {
llm gollm.LLM // The LLM instance to use for generation
llm gollm.LLM // The LLM instance to use for generation
templates map[string]*template.Template // Compiled templates for request formatting
config *config.ProcessingConfig // Configuration for processing behavior
defaultPrompt string // Default system prompt for all requests
Expand Down Expand Up @@ -122,7 +123,7 @@ func (p *Processor) ProcessRequest(ctx context.Context, req *Request) (*Response

// Pass timeout header to LLM context if present
if timeoutHeader := ctx.Value("X-Test-Timeout"); timeoutHeader != nil {
ctx = context.WithValue(ctx, "X-Test-Timeout", timeoutHeader)
ctx = context.WithValue(ctx, middleware.XTestTimeoutKey, timeoutHeader)
}

// Send request to LLM
Expand Down

0 comments on commit 8b00b62

Please sign in to comment.