package provider import ( "errors" "net/http" "github.com/alibaba/higress/plugins/wasm-go/extensions/ai-proxy/util" "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper" "github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types" ) const ( yiDomain = "api.lingyiwanwu.com" yiChatCompletionPath = "/v1/chat/completions" ) type yiProviderInitializer struct { } func (m *yiProviderInitializer) ValidateConfig(config ProviderConfig) error { if config.apiTokens == nil || len(config.apiTokens) == 0 { return errors.New("no apiToken found in provider config") } return nil } func (m *yiProviderInitializer) CreateProvider(config ProviderConfig) (Provider, error) { return &yiProvider{ config: config, contextCache: createContextCache(&config), }, nil } type yiProvider struct { config ProviderConfig contextCache *contextCache } func (m *yiProvider) GetProviderType() string { return providerTypeYi } func (m *yiProvider) OnRequestHeaders(ctx wrapper.HttpContext, apiName ApiName, log wrapper.Log) (types.Action, error) { if apiName != ApiNameChatCompletion { return types.ActionContinue, errUnsupportedApiName } m.config.handleRequestHeaders(m, ctx, apiName, log) return types.ActionContinue, nil } func (m *yiProvider) OnRequestBody(ctx wrapper.HttpContext, apiName ApiName, body []byte, log wrapper.Log) (types.Action, error) { if apiName != ApiNameChatCompletion { return types.ActionContinue, errUnsupportedApiName } return m.config.handleRequestBody(m, m.contextCache, ctx, apiName, body, log) } func (m *yiProvider) TransformRequestHeaders(ctx wrapper.HttpContext, apiName ApiName, headers http.Header, log wrapper.Log) { util.OverwriteRequestPathHeader(headers, yiChatCompletionPath) util.OverwriteRequestHostHeader(headers, yiDomain) util.OverwriteRequestAuthorizationHeader(headers, "Bearer "+m.config.GetApiTokenInUse(ctx)) headers.Del("Content-Length") }