fix(moonshot): resolve 401 Unauthorized errors by trimming API keys and improving request compatibility
This commit is contained in:
@@ -203,5 +203,5 @@ func (c *Config) GetAPIKey(provider string) (string, error) {
|
|||||||
if val == "" {
|
if val == "" {
|
||||||
return "", fmt.Errorf("environment variable %s not set for %s", envVar, provider)
|
return "", fmt.Errorf("environment variable %s not set for %s", envVar, provider)
|
||||||
}
|
}
|
||||||
return val, nil
|
return strings.TrimSpace(val), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,9 +58,20 @@ func MessagesToOpenAIJSON(messages []models.UnifiedMessage) ([]interface{}, erro
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var finalContent interface{}
|
||||||
|
if len(parts) == 1 {
|
||||||
|
if p, ok := parts[0].(map[string]interface{}); ok && p["type"] == "text" {
|
||||||
|
finalContent = p["text"]
|
||||||
|
} else {
|
||||||
|
finalContent = parts
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
finalContent = parts
|
||||||
|
}
|
||||||
|
|
||||||
msg := map[string]interface{}{
|
msg := map[string]interface{}{
|
||||||
"role": m.Role,
|
"role": m.Role,
|
||||||
"content": parts,
|
"content": finalContent,
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.ReasoningContent != nil {
|
if m.ReasoningContent != nil {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ func NewMoonshotProvider(cfg config.MoonshotConfig, apiKey string) *MoonshotProv
|
|||||||
return &MoonshotProvider{
|
return &MoonshotProvider{
|
||||||
client: resty.New(),
|
client: resty.New(),
|
||||||
config: cfg,
|
config: cfg,
|
||||||
apiKey: apiKey,
|
apiKey: strings.TrimSpace(apiKey),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,11 +43,15 @@ func (p *MoonshotProvider) ChatCompletion(ctx context.Context, req *models.Unifi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
baseURL := strings.TrimRight(p.config.BaseURL, "/")
|
||||||
|
|
||||||
resp, err := p.client.R().
|
resp, err := p.client.R().
|
||||||
SetContext(ctx).
|
SetContext(ctx).
|
||||||
SetHeader("Authorization", "Bearer "+p.apiKey).
|
SetHeader("Authorization", "Bearer "+p.apiKey).
|
||||||
|
SetHeader("Content-Type", "application/json").
|
||||||
|
SetHeader("Accept", "application/json").
|
||||||
SetBody(body).
|
SetBody(body).
|
||||||
Post(fmt.Sprintf("%s/chat/completions", p.config.BaseURL))
|
Post(fmt.Sprintf("%s/chat/completions", baseURL))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("request failed: %w", err)
|
return nil, fmt.Errorf("request failed: %w", err)
|
||||||
@@ -79,12 +83,16 @@ func (p *MoonshotProvider) ChatCompletionStream(ctx context.Context, req *models
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
baseURL := strings.TrimRight(p.config.BaseURL, "/")
|
||||||
|
|
||||||
resp, err := p.client.R().
|
resp, err := p.client.R().
|
||||||
SetContext(ctx).
|
SetContext(ctx).
|
||||||
SetHeader("Authorization", "Bearer "+p.apiKey).
|
SetHeader("Authorization", "Bearer "+p.apiKey).
|
||||||
|
SetHeader("Content-Type", "application/json").
|
||||||
|
SetHeader("Accept", "text/event-stream").
|
||||||
SetBody(body).
|
SetBody(body).
|
||||||
SetDoNotParseResponse(true).
|
SetDoNotParseResponse(true).
|
||||||
Post(fmt.Sprintf("%s/chat/completions", p.config.BaseURL))
|
Post(fmt.Sprintf("%s/chat/completions", baseURL))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("request failed: %w", err)
|
return nil, fmt.Errorf("request failed: %w", err)
|
||||||
|
|||||||
Reference in New Issue
Block a user