2424import org .apache .shenyu .common .dto .SelectorData ;
2525import org .apache .shenyu .common .dto .convert .rule .AiProxyHandle ;
2626import org .apache .shenyu .common .enums .PluginEnum ;
27+ import org .apache .shenyu .common .exception .ShenyuException ;
2728import org .apache .shenyu .common .utils .GsonUtils ;
2829import org .apache .shenyu .plugin .ai .common .config .AiCommonConfig ;
2930import org .apache .shenyu .plugin .ai .proxy .enhanced .cache .AiProxyApiKeyCache ;
31+ import org .apache .shenyu .plugin .ai .proxy .enhanced .protocol .AiProxyProtocolFactory ;
32+ import org .apache .shenyu .plugin .ai .proxy .enhanced .provider .AiProxyProviderFactory ;
33+ import org .apache .shenyu .plugin .ai .proxy .enhanced .service .AiProxyConfigValidator ;
3034import org .apache .shenyu .plugin .base .cache .CommonHandleCache ;
3135import org .apache .shenyu .plugin .base .handler .PluginDataHandler ;
3236import org .apache .shenyu .plugin .base .utils .CacheKeyUtils ;
3337import org .slf4j .Logger ;
3438import org .slf4j .LoggerFactory ;
3539
3640import java .util .Objects ;
41+ import java .util .Set ;
3742
3843/**
3944 * this is ai proxy plugin handler.
@@ -42,30 +47,109 @@ public class AiProxyPluginHandler implements PluginDataHandler {
4247
4348 private static final Logger LOG = LoggerFactory .getLogger (AiProxyPluginHandler .class );
4449
50+ private static final Set <String > VALIDATION_REASONS = Set .of ("maxTokens.nonPositive" , "temperature.notFinite" , "temperature.negative" ,
51+ "baseUrl.invalidSyntax" , "baseUrl.invalidEndpoint" , "provider.unregistered" , "protocol.unregistered" );
52+
4553 private final CommonHandleCache <String , AiProxyHandle > selectorCachedHandle = new CommonHandleCache <>();
4654
55+ private final AiProxyProviderFactory providerFactory ;
56+
57+ private final AiProxyProtocolFactory protocolFactory ;
58+
4759 private volatile AiCommonConfig globalConfig ;
4860
61+ /**
62+ * Create a handler without registries for programmatic compatibility.
63+ * Provider and protocol names are checked when requests are prepared.
64+ * Logs one warning per instance about the missing update-time name checks.
65+ * Use the registry constructor to also check names before publishing defaults.
66+ */
4967 public AiProxyPluginHandler () {
68+ providerFactory = null ;
69+ protocolFactory = null ;
70+ LOG .warn ("AI proxy defaults validationMode=withoutRegistries; provider and protocol names are checked only when preparing requests" );
71+ }
72+
73+ /**
74+ * Create a handler that validates names against the runtime registries.
75+ *
76+ * @param providerFactory registered providers
77+ * @param protocolFactory registered protocols
78+ */
79+ public AiProxyPluginHandler (final AiProxyProviderFactory providerFactory , final AiProxyProtocolFactory protocolFactory ) {
80+ this .providerFactory = Objects .requireNonNull (providerFactory , "providerFactory" );
81+ this .protocolFactory = Objects .requireNonNull (protocolFactory , "protocolFactory" );
5082 }
5183
5284 @ Override
5385 public void handlerPlugin (final PluginData pluginData ) {
5486 if (Objects .isNull (pluginData )) {
5587 return ;
5688 }
89+ if (Boolean .FALSE .equals (pluginData .getEnabled ())) {
90+ globalConfig = null ;
91+ LOG .info ("AI proxy defaults action=disabled pluginId={} namespaceId={}" , pluginData .getId (), pluginData .getNamespaceId ());
92+ return ;
93+ }
94+ if (StringUtils .isBlank (pluginData .getConfig ())) {
95+ globalConfig = null ;
96+ LOG .info ("AI proxy defaults action=cleared reason=blankConfig pluginId={} namespaceId={}" , pluginData .getId (), pluginData .getNamespaceId ());
97+ return ;
98+ }
99+ final AiCommonConfig candidate ;
57100 try {
58- globalConfig = Boolean .FALSE .equals (pluginData .getEnabled ()) || StringUtils .isBlank (pluginData .getConfig ())
59- ? null : GsonUtils .getInstance ().fromJson (pluginData .getConfig (), AiCommonConfig .class );
101+ candidate = GsonUtils .getInstance ().fromJson (pluginData .getConfig (), AiCommonConfig .class );
60102 } catch (JsonParseException e ) {
103+ LOG .warn ("AI proxy defaults action=rejected reason=malformedConfig pluginId={} namespaceId={}" , pluginData .getId (), pluginData .getNamespaceId ());
104+ return ;
105+ }
106+ if (Objects .isNull (candidate )) {
61107 globalConfig = null ;
62- LOG .warn ("Invalid configuration for plugin {}; cleared global defaults" , pluginData .getName ());
108+ LOG .info ("AI proxy defaults action=cleared reason=jsonNull pluginId={} namespaceId={}" , pluginData .getId (), pluginData .getNamespaceId ());
109+ return ;
110+ }
111+ try {
112+ validateConfig (candidate );
113+ } catch (RuntimeException e ) {
114+ logRejectedConfig (e , pluginData );
115+ return ;
116+ }
117+ globalConfig = candidate ;
118+ }
119+
120+ private void logRejectedConfig (final RuntimeException error , final PluginData pluginData ) {
121+ String reason = error .getMessage ();
122+ if (error instanceof IllegalArgumentException && Objects .nonNull (reason ) && VALIDATION_REASONS .contains (reason )) {
123+ LOG .warn ("AI proxy defaults action=rejected reason={} pluginId={} namespaceId={}" , reason , pluginData .getId (), pluginData .getNamespaceId ());
124+ return ;
125+ }
126+ LOG .error ("AI proxy defaults action=rejected reason=unexpectedValidationFailure exceptionType={} pluginId={} namespaceId={}" ,
127+ error .getClass ().getSimpleName (), pluginData .getId (), pluginData .getNamespaceId ());
128+ }
129+
130+ private void validateConfig (final AiCommonConfig candidate ) {
131+ AiProxyConfigValidator .validate (candidate );
132+ if (Objects .nonNull (providerFactory ) && StringUtils .isNotBlank (candidate .getProvider ())) {
133+ try {
134+ providerFactory .getProvider (candidate .getProvider ());
135+ } catch (ShenyuException e ) {
136+ throw new IllegalArgumentException ("provider.unregistered" );
137+ }
138+ }
139+ if (Objects .nonNull (protocolFactory ) && StringUtils .isNotBlank (candidate .getProtocol ())) {
140+ try {
141+ protocolFactory .getProtocol (candidate .getProtocol ());
142+ } catch (ShenyuException e ) {
143+ throw new IllegalArgumentException ("protocol.unregistered" );
144+ }
63145 }
64146 }
65147
66148 @ Override
67149 public void removePlugin (final PluginData pluginData ) {
68150 globalConfig = null ;
151+ LOG .info ("AI proxy defaults action=removed pluginId={} namespaceId={}" ,
152+ Objects .isNull (pluginData ) ? null : pluginData .getId (), Objects .isNull (pluginData ) ? null : pluginData .getNamespaceId ());
69153 }
70154
71155 /**
0 commit comments