Refactor API URL building in OpenAI and Claude components

- Update `buildAPIURL` function in OpenAI provider to delegate URL construction to `connector.BuildAPIURL`, ensuring consistent URL formatting across the agent LLM and sandbox proxy paths.
- Modify backend URL construction in Claude's `BuildProxyConfig` to utilize the shared `connector.BuildAPIURL` helper, applying the necessary `/v1` prefix for compatibility.

This change enhances code maintainability and consistency in API URL handling across different components.
This commit is contained in:
Max 2026-02-08 12:31:28 +08:00
parent fea1ac0708
commit 31a75f0161
2 changed files with 8 additions and 15 deletions

View file

@ -129,17 +129,11 @@ type Provider struct {
adapters []adapters.CapabilityAdapter adapters []adapters.CapabilityAdapter
} }
// buildAPIURL builds the complete API URL from host and endpoint // buildAPIURL builds the complete API URL from host and endpoint.
// If host ends with /, it's used as-is (user has specified full path) // Delegates to the shared connector.BuildAPIURL for consistent URL building
// Otherwise, /v1 prefix is added automatically (standard for OpenAI-compatible APIs) // across the agent LLM path and the sandbox proxy path.
func buildAPIURL(host, endpoint string) string { func buildAPIURL(host, endpoint string) string {
// If host ends with /, use it as-is (user has specified full path like /v1/ or /api/) return connector.BuildAPIURL(host, endpoint)
// Otherwise, add /v1 prefix (standard for OpenAI-compatible APIs)
if !strings.HasSuffix(host, "/") {
endpoint = "/v1" + endpoint
}
host = strings.TrimSuffix(host, "/")
return host + endpoint
} }
// New create a new OpenAI provider with capability adapters // New create a new OpenAI provider with capability adapters

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/yaoapp/gou/connector"
agentContext "github.com/yaoapp/yao/agent/context" agentContext "github.com/yaoapp/yao/agent/context"
) )
@ -355,11 +356,9 @@ func BuildProxyConfig(opts *Options) ([]byte, error) {
return nil, fmt.Errorf("options is required") return nil, fmt.Errorf("options is required")
} }
// Build backend URL - ensure it ends with /chat/completions // Build backend URL using the shared connector.BuildAPIURL helper
backendURL := opts.ConnectorHost // so that the /v1 prefix is applied consistently with the agent LLM path.
if !strings.HasSuffix(backendURL, "/chat/completions") { backendURL := connector.BuildAPIURL(opts.ConnectorHost, "/chat/completions")
backendURL = strings.TrimSuffix(backendURL, "/") + "/chat/completions"
}
config := map[string]interface{}{ config := map[string]interface{}{
"backend": backendURL, "backend": backendURL,