feat: add User-Agent header and update LLM client context handling

This commit is contained in:
kite 2026-05-16 10:30:28 +08:00
parent 3586cf799b
commit 93e02db24a
3 changed files with 16 additions and 1 deletions

View file

@ -13,6 +13,7 @@ import (
)
func main() {
llm.AppVersion = Version
llm.InitEmbeddedLoader()
ctx := context.Background()

View file

@ -675,7 +675,7 @@ func (a *Agent) performLlmCodeReview(ctx context.Context, messages []llm.Message
rec := fs.AppendTaskRecord(session.MainTask, append([]llm.Message(nil), messages...))
startTime := time.Now()
resp, err := a.args.LLMClient.Completions(llm.ChatRequest{
resp, err := a.args.LLMClient.CompletionsWithCtx(ctx, llm.ChatRequest{
Model: a.args.Model,
Messages: messages,
Tools: a.args.MainToolDefs,

View file

@ -22,6 +22,16 @@ import (
const maxRetries = 10 // Maximum number of retry attempts with exponential backoff.
var AppVersion = "dev"
func userAgent(provider string) string {
ua := "open-code-review/" + AppVersion
if provider != "" {
ua += " | " + provider
}
return ua
}
// LLMClient is the unified interface for all LLM protocol implementations.
type LLMClient interface {
Completions(req ChatRequest) (*ChatResponse, error)
@ -361,6 +371,7 @@ func (c *OpenAIClient) StreamCompletion(req ChatRequest, cb func(chunk []byte) e
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+c.cfg.APIKey)
httpReq.Header.Set("Accept", "text/event-stream")
httpReq.Header.Set("User-Agent", userAgent(""))
resp, err := c.client.Do(httpReq)
if err != nil {
@ -413,6 +424,7 @@ func (c *OpenAIClient) doRequestCtx(ctx context.Context, model string, req ChatR
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Authorization", "Bearer "+c.cfg.APIKey)
httpReq.Header.Set("User-Agent", userAgent(""))
resp, err := c.client.Do(httpReq)
if err != nil {
@ -527,6 +539,7 @@ func (c *AnthropicClient) StreamCompletion(req ChatRequest, cb func(chunk []byte
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("x-api-key", c.cfg.APIKey)
httpReq.Header.Set("anthropic-version", anthropicVersion)
httpReq.Header.Set("User-Agent", userAgent("claude"))
resp, err := c.client.Do(httpReq)
if err != nil {
@ -627,6 +640,7 @@ func (c *AnthropicClient) doRequestCtx(ctx context.Context, model string, req Ch
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("x-api-key", c.cfg.APIKey)
httpReq.Header.Set("anthropic-version", anthropicVersion)
httpReq.Header.Set("User-Agent", userAgent("claude"))
resp, err := c.client.Do(httpReq)
if err != nil {