mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 14:49:40 +08:00
feat(sdk): keep caller-provided interceptor host and executor wrappers across config apply
- Add Builder.WithPluginInterceptorHost so embedders can install a custom PluginInterceptorHost that survives startup and reload; the server wraps it to keep plugin model routing, executor dispatch, lifecycle, and websocket observers delegated to the internal plugin host. - Add Hooks.OnConfigApplied invoked after each successful config apply so embedders can reinstall executor wrappers and other caller-owned runtime state. - Cover the delegated interceptor host with nil-safety, skip-forwarding, capability-detection, and real execute-chain regression tests. Closes: #7499 Co-authored-by: Dylan <1990016@gmail.com> Co-authored-by: lzt404 <2596933790@qq.com>
This commit is contained in:
271
internal/api/interceptor_host.go
Normal file
271
internal/api/interceptor_host.go
Normal file
@@ -0,0 +1,271 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/pluginhost"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/api/handlers"
|
||||
coreexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/pluginapi"
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
||||
)
|
||||
|
||||
// delegatedInterceptorHost forwards PluginInterceptorHost calls to an
|
||||
// embedder-provided primary host while delegating model routing, plugin
|
||||
// executor dispatch, request lifecycle, and websocket observer callbacks back
|
||||
// to the internal plugin host. Installing this host keeps plugin model
|
||||
// routing, plugin executor routes, lifecycle callbacks, and websocket
|
||||
// observers functional when a custom interceptor host replaces the plugin
|
||||
// host on request handlers.
|
||||
|
||||
type delegatedInterceptorHost struct {
|
||||
primary handlers.PluginInterceptorHost
|
||||
pluginHost *pluginhost.Host
|
||||
}
|
||||
|
||||
var (
|
||||
_ handlers.PluginInterceptorHost = (*delegatedInterceptorHost)(nil)
|
||||
_ handlers.PluginExecutorHost = (*delegatedInterceptorHost)(nil)
|
||||
)
|
||||
|
||||
// InterceptRequestBeforeAuth delegates to the embedder's host.
|
||||
func (h *delegatedInterceptorHost) InterceptRequestBeforeAuth(ctx context.Context, req pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse {
|
||||
return h.primary.InterceptRequestBeforeAuth(ctx, req)
|
||||
}
|
||||
|
||||
// InterceptRequestBeforeAuthExcept forwards the plugin-skip identifier to the embedder's host when it
|
||||
// implements the skip-aware interface, preventing plugin-initiated nested executions
|
||||
// from re-entering their own interceptor; otherwise it falls back to the base method.
|
||||
func (h *delegatedInterceptorHost) InterceptRequestBeforeAuthExcept(ctx context.Context, req pluginapi.RequestInterceptRequest, skipPluginID string) pluginapi.RequestInterceptResponse {
|
||||
if skipper, ok := h.primary.(interface {
|
||||
InterceptRequestBeforeAuthExcept(context.Context, pluginapi.RequestInterceptRequest, string) pluginapi.RequestInterceptResponse
|
||||
}); ok {
|
||||
return skipper.InterceptRequestBeforeAuthExcept(ctx, req, skipPluginID)
|
||||
}
|
||||
return h.primary.InterceptRequestBeforeAuth(ctx, req)
|
||||
}
|
||||
|
||||
// InterceptRequestAfterAuth delegates to the embedder's host.
|
||||
func (h *delegatedInterceptorHost) InterceptRequestAfterAuth(ctx context.Context, req pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse {
|
||||
return h.primary.InterceptRequestAfterAuth(ctx, req)
|
||||
}
|
||||
|
||||
// InterceptRequestAfterAuthExcept forwards the plugin-skip identifier to the embedder's host when it
|
||||
// implements the skip-aware interface, preventing plugin-initiated nested executions
|
||||
// from re-entering their own interceptor; otherwise it falls back to the base method.
|
||||
func (h *delegatedInterceptorHost) InterceptRequestAfterAuthExcept(ctx context.Context, req pluginapi.RequestInterceptRequest, skipPluginID string) pluginapi.RequestInterceptResponse {
|
||||
if skipper, ok := h.primary.(interface {
|
||||
InterceptRequestAfterAuthExcept(context.Context, pluginapi.RequestInterceptRequest, string) pluginapi.RequestInterceptResponse
|
||||
}); ok {
|
||||
return skipper.InterceptRequestAfterAuthExcept(ctx, req, skipPluginID)
|
||||
}
|
||||
return h.primary.InterceptRequestAfterAuth(ctx, req)
|
||||
}
|
||||
|
||||
// InterceptResponse delegates to the embedder's host.
|
||||
func (h *delegatedInterceptorHost) InterceptResponse(ctx context.Context, req pluginapi.ResponseInterceptRequest) pluginapi.ResponseInterceptResponse {
|
||||
return h.primary.InterceptResponse(ctx, req)
|
||||
}
|
||||
|
||||
// InterceptResponseExcept forwards the plugin-skip identifier to the embedder's host when it
|
||||
// implements the skip-aware interface, preventing plugin-initiated nested executions
|
||||
// from re-entering their own interceptor; otherwise it falls back to the base method.
|
||||
func (h *delegatedInterceptorHost) InterceptResponseExcept(ctx context.Context, req pluginapi.ResponseInterceptRequest, skipPluginID string) pluginapi.ResponseInterceptResponse {
|
||||
if skipper, ok := h.primary.(interface {
|
||||
InterceptResponseExcept(context.Context, pluginapi.ResponseInterceptRequest, string) pluginapi.ResponseInterceptResponse
|
||||
}); ok {
|
||||
return skipper.InterceptResponseExcept(ctx, req, skipPluginID)
|
||||
}
|
||||
return h.primary.InterceptResponse(ctx, req)
|
||||
}
|
||||
|
||||
// InterceptStreamChunk delegates to the embedder's host.
|
||||
func (h *delegatedInterceptorHost) InterceptStreamChunk(ctx context.Context, req pluginapi.StreamChunkInterceptRequest) pluginapi.StreamChunkInterceptResponse {
|
||||
return h.primary.InterceptStreamChunk(ctx, req)
|
||||
}
|
||||
|
||||
// InterceptStreamChunkExcept forwards the plugin-skip identifier to the embedder's host when it
|
||||
// implements the skip-aware interface, preventing plugin-initiated nested executions
|
||||
// from re-entering their own interceptor; otherwise it falls back to the base method.
|
||||
func (h *delegatedInterceptorHost) InterceptStreamChunkExcept(ctx context.Context, req pluginapi.StreamChunkInterceptRequest, skipPluginID string) pluginapi.StreamChunkInterceptResponse {
|
||||
if skipper, ok := h.primary.(interface {
|
||||
InterceptStreamChunkExcept(context.Context, pluginapi.StreamChunkInterceptRequest, string) pluginapi.StreamChunkInterceptResponse
|
||||
}); ok {
|
||||
return skipper.InterceptStreamChunkExcept(ctx, req, skipPluginID)
|
||||
}
|
||||
return h.primary.InterceptStreamChunk(ctx, req)
|
||||
}
|
||||
|
||||
// RouteModel delegates to the internal plugin host so model routing stays
|
||||
// functional when the interceptor host is replaced. This mirrors the
|
||||
// PluginHost-as-router fallback in handlers.modelRouterHost.
|
||||
func (h *delegatedInterceptorHost) RouteModel(ctx context.Context, req pluginapi.ModelRouteRequest) (pluginapi.ModelRouteResponse, bool) {
|
||||
if h.pluginHost == nil {
|
||||
return pluginapi.ModelRouteResponse{}, false
|
||||
}
|
||||
return h.pluginHost.RouteModel(ctx, req)
|
||||
}
|
||||
|
||||
// RouteModelExcept delegates skip-aware routing to the internal plugin host.
|
||||
func (h *delegatedInterceptorHost) RouteModelExcept(ctx context.Context, req pluginapi.ModelRouteRequest, skipPluginID string) (pluginapi.ModelRouteResponse, bool) {
|
||||
if h.pluginHost == nil {
|
||||
return pluginapi.ModelRouteResponse{}, false
|
||||
}
|
||||
return h.pluginHost.RouteModelExcept(ctx, req, skipPluginID)
|
||||
}
|
||||
|
||||
// HasModelRouters delegates to the internal plugin host.
|
||||
func (h *delegatedInterceptorHost) HasModelRouters() bool {
|
||||
return h.pluginHost != nil && h.pluginHost.HasModelRouters()
|
||||
}
|
||||
|
||||
// HasModelRoutersExcept delegates skip-aware detection to the internal plugin host.
|
||||
func (h *delegatedInterceptorHost) HasModelRoutersExcept(skipPluginID string) bool {
|
||||
return h.pluginHost != nil && h.pluginHost.HasModelRoutersExcept(skipPluginID)
|
||||
}
|
||||
|
||||
// HasRequestInterceptors reports whether the embedder's host actually has
|
||||
// request interceptors. The primary host is queried first through the optional
|
||||
// detector interface; when it does not implement detection, the handler's
|
||||
// default true semantics apply so the interceptor is invoked.
|
||||
func (h *delegatedInterceptorHost) HasRequestInterceptors() bool {
|
||||
if h.primary == nil {
|
||||
return false
|
||||
}
|
||||
if detector, ok := h.primary.(interface{ HasRequestInterceptors() bool }); ok {
|
||||
return detector.HasRequestInterceptors()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// HasStreamInterceptors reports whether the embedder's host has stream
|
||||
// interceptors, with the same optional-detector default-true semantics.
|
||||
func (h *delegatedInterceptorHost) HasStreamInterceptors() bool {
|
||||
if h.primary == nil {
|
||||
return false
|
||||
}
|
||||
if detector, ok := h.primary.(interface{ HasStreamInterceptors() bool }); ok {
|
||||
return detector.HasStreamInterceptors()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// StreamChunkPayloadIncludesRequestBody delegates payload policy to the
|
||||
// embedder's host, preserving the handler's default-true semantics when the
|
||||
// primary host does not implement the policy interface.
|
||||
func (h *delegatedInterceptorHost) StreamChunkPayloadIncludesRequestBody() bool {
|
||||
if h.primary == nil {
|
||||
return false
|
||||
}
|
||||
if policy, ok := h.primary.(interface{ StreamChunkPayloadIncludesRequestBody() bool }); ok {
|
||||
return policy.StreamChunkPayloadIncludesRequestBody()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// StreamChunkPayloadIncludesHistory delegates payload policy to the embedder's
|
||||
// host with the same default-true semantics.
|
||||
func (h *delegatedInterceptorHost) StreamChunkPayloadIncludesHistory() bool {
|
||||
if h.primary == nil {
|
||||
return false
|
||||
}
|
||||
if policy, ok := h.primary.(interface{ StreamChunkPayloadIncludesHistory() bool }); ok {
|
||||
return policy.StreamChunkPayloadIncludesHistory()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// CompleteRequest delegates request lifecycle completion to the internal
|
||||
// plugin host, which owns plugin usage accounting and completion callbacks.
|
||||
func (h *delegatedInterceptorHost) CompleteRequest(ctx context.Context, completion pluginapi.RequestCompletion) {
|
||||
if h.pluginHost == nil {
|
||||
return
|
||||
}
|
||||
h.pluginHost.CompleteRequest(ctx, completion)
|
||||
}
|
||||
|
||||
// CompleteRequestExcept forwards the plugin-skip identifier so a plugin that
|
||||
// initiated nested execution does not receive its own completion callback,
|
||||
// preventing recursive dispatch.
|
||||
func (h *delegatedInterceptorHost) CompleteRequestExcept(ctx context.Context, completion pluginapi.RequestCompletion, skipPluginID string) {
|
||||
if h.pluginHost == nil {
|
||||
return
|
||||
}
|
||||
h.pluginHost.CompleteRequestExcept(ctx, completion, skipPluginID)
|
||||
}
|
||||
|
||||
// HasWebSocketResponseObservers delegates detection to the plugin host.
|
||||
func (h *delegatedInterceptorHost) HasWebSocketResponseObservers() bool {
|
||||
return h.pluginHost != nil && h.pluginHost.HasWebSocketResponseObservers()
|
||||
}
|
||||
|
||||
// ObserveWebSocketResponseEvent delegates observer dispatch to the plugin host.
|
||||
func (h *delegatedInterceptorHost) ObserveWebSocketResponseEvent(ctx context.Context, event pluginapi.WebSocketResponseEvent) {
|
||||
if h.pluginHost == nil {
|
||||
return
|
||||
}
|
||||
h.pluginHost.ObserveWebSocketResponseEvent(ctx, event)
|
||||
}
|
||||
|
||||
// ObserveWebSocketResponseEventExcept forwards the plugin-skip identifier so a
|
||||
// plugin that initiated nested execution does not observe its own events.
|
||||
func (h *delegatedInterceptorHost) ObserveWebSocketResponseEventExcept(ctx context.Context, event pluginapi.WebSocketResponseEvent, skipPluginID string) {
|
||||
if h.pluginHost == nil {
|
||||
return
|
||||
}
|
||||
h.pluginHost.ObserveWebSocketResponseEventExcept(ctx, event, skipPluginID)
|
||||
}
|
||||
|
||||
// ExecutePluginExecutor delegates direct plugin-executor dispatch to the
|
||||
// internal plugin host so a model route targeting a plugin executor still
|
||||
// works when the interceptor host is replaced. Without this, pluginExecutorHost
|
||||
// returns nil and the handler responds 502 plugin executor host is unavailable.
|
||||
func (h *delegatedInterceptorHost) ExecutePluginExecutor(ctx context.Context, pluginID string, req coreexecutor.Request, opts coreexecutor.Options) (coreexecutor.Response, error) {
|
||||
if h.pluginHost == nil {
|
||||
return coreexecutor.Response{}, fmt.Errorf("plugin executor host is unavailable")
|
||||
}
|
||||
return h.pluginHost.ExecutePluginExecutor(ctx, pluginID, req, opts)
|
||||
}
|
||||
|
||||
// ExecutePluginExecutorStream delegates streaming plugin-executor dispatch.
|
||||
func (h *delegatedInterceptorHost) ExecutePluginExecutorStream(ctx context.Context, pluginID string, req coreexecutor.Request, opts coreexecutor.Options) (*coreexecutor.StreamResult, error) {
|
||||
if h.pluginHost == nil {
|
||||
return nil, fmt.Errorf("plugin executor host is unavailable")
|
||||
}
|
||||
return h.pluginHost.ExecutePluginExecutorStream(ctx, pluginID, req, opts)
|
||||
}
|
||||
|
||||
// CountPluginExecutor delegates token counting to the internal plugin host.
|
||||
func (h *delegatedInterceptorHost) CountPluginExecutor(ctx context.Context, pluginID string, req coreexecutor.Request, opts coreexecutor.Options) (coreexecutor.Response, error) {
|
||||
if h.pluginHost == nil {
|
||||
return coreexecutor.Response{}, fmt.Errorf("plugin executor host is unavailable")
|
||||
}
|
||||
return h.pluginHost.CountPluginExecutor(ctx, pluginID, req, opts)
|
||||
}
|
||||
|
||||
// PluginExecutorRequestToFormat delegates executor input-format resolution to
|
||||
// the internal plugin host.
|
||||
func (h *delegatedInterceptorHost) PluginExecutorRequestToFormat(pluginID string, req coreexecutor.Request, opts coreexecutor.Options) sdktranslator.Format {
|
||||
if h.pluginHost == nil {
|
||||
return ""
|
||||
}
|
||||
return h.pluginHost.PluginExecutorRequestToFormat(pluginID, req, opts)
|
||||
}
|
||||
|
||||
// normalizeInterceptorHost unwraps typed-nil PluginInterceptorHost values so
|
||||
// the server can treat them as unset and avoid wrapping a nil primary.
|
||||
func normalizeInterceptorHost(host handlers.PluginInterceptorHost) handlers.PluginInterceptorHost {
|
||||
if host == nil {
|
||||
return nil
|
||||
}
|
||||
v := reflect.ValueOf(host)
|
||||
switch v.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
if v.IsNil() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return host
|
||||
}
|
||||
@@ -91,6 +91,13 @@ type Server struct {
|
||||
// pluginHost owns dynamic plugin Management API route dispatch.
|
||||
pluginHost *pluginhost.Host
|
||||
|
||||
// interceptorHost is the resolved PluginInterceptorHost installed on request
|
||||
// handlers. When a caller provides their own host via
|
||||
// WithPluginInterceptorHost, interceptor calls and capability detection
|
||||
// delegate to that host while plugin model routing, lifecycle callbacks, and
|
||||
// websocket observers stay delegated to pluginHost.
|
||||
interceptorHost handlers.PluginInterceptorHost
|
||||
|
||||
// managementRoutesRegistered tracks whether the management routes have been attached to the engine.
|
||||
managementRoutesRegistered atomic.Bool
|
||||
// managementRoutesEnabled controls whether management endpoints serve real handlers.
|
||||
@@ -172,6 +179,22 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk
|
||||
exampleAPIKeySafeModeEnabled: optionState.exampleAPIKeySafeMode,
|
||||
}
|
||||
|
||||
// Resolve the interceptor host once so startup and reload share the same
|
||||
// wiring. A caller-provided host keeps interceptor dispatch and capability
|
||||
// detection on the embedder implementation while model routing, lifecycle
|
||||
// callbacks, and websocket observers remain delegated to the internal
|
||||
// plugin host. Typed-nil values are normalized first so they cannot defeat
|
||||
// SetPluginHost's own nil-interface normalization.
|
||||
|
||||
if custom := normalizeInterceptorHost(optionState.pluginInterceptorHost); custom != nil {
|
||||
s.interceptorHost = &delegatedInterceptorHost{
|
||||
primary: custom,
|
||||
pluginHost: optionState.pluginHost,
|
||||
}
|
||||
} else {
|
||||
s.interceptorHost = optionState.pluginHost
|
||||
}
|
||||
|
||||
// Add middleware
|
||||
engine.Use(logging.GinLogrusLogger())
|
||||
engine.Use(logging.CPATraceIDMiddleware())
|
||||
@@ -206,7 +229,7 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk
|
||||
openai.SetResponsesWebsocketCompressionEnabled(cfg.Codex.WebsocketCompressionEnabled())
|
||||
s.wsAuthEnabled.Store(cfg.WebsocketAuth)
|
||||
s.exampleAPIKeySafeModeActive.Store(s.exampleAPIKeySafeModeRequired(cfg))
|
||||
s.handlers.SetPluginHost(optionState.pluginHost)
|
||||
s.handlers.SetPluginHost(s.interceptorHost)
|
||||
if optionState.pluginHost != nil {
|
||||
optionState.pluginHost.SetModelExecutor(s.handlers)
|
||||
optionState.pluginHost.SetAuthManager(authManager)
|
||||
|
||||
333
internal/api/server_issue7499_test.go
Normal file
333
internal/api/server_issue7499_test.go
Normal file
@@ -0,0 +1,333 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
proxyconfig "github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/pluginhost"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/api/handlers"
|
||||
coreexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/pluginapi"
|
||||
)
|
||||
|
||||
type recordingInterceptorHost struct {
|
||||
beforeAuthCalls int
|
||||
}
|
||||
|
||||
func (h *recordingInterceptorHost) InterceptRequestBeforeAuth(context.Context, pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse {
|
||||
h.beforeAuthCalls++
|
||||
return pluginapi.RequestInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *recordingInterceptorHost) InterceptRequestAfterAuth(context.Context, pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse {
|
||||
return pluginapi.RequestInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *recordingInterceptorHost) InterceptResponse(context.Context, pluginapi.ResponseInterceptRequest) pluginapi.ResponseInterceptResponse {
|
||||
return pluginapi.ResponseInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *recordingInterceptorHost) InterceptStreamChunk(context.Context, pluginapi.StreamChunkInterceptRequest) pluginapi.StreamChunkInterceptResponse {
|
||||
return pluginapi.StreamChunkInterceptResponse{}
|
||||
}
|
||||
|
||||
var _ handlers.PluginInterceptorHost = (*recordingInterceptorHost)(nil)
|
||||
|
||||
// skipAwareInterceptorHost implements the pluginInterceptorSkipHost surface to
|
||||
// prove the delegated host forwards skipPluginID instead of dropping it.
|
||||
type skipAwareInterceptorHost struct {
|
||||
recordingInterceptorHost
|
||||
beforeAuthSkips []string
|
||||
afterAuthSkips []string
|
||||
responseSkips []string
|
||||
streamSkips []string
|
||||
}
|
||||
|
||||
func (h *skipAwareInterceptorHost) InterceptRequestBeforeAuthExcept(_ context.Context, _ pluginapi.RequestInterceptRequest, skipPluginID string) pluginapi.RequestInterceptResponse {
|
||||
h.beforeAuthSkips = append(h.beforeAuthSkips, skipPluginID)
|
||||
return pluginapi.RequestInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *skipAwareInterceptorHost) InterceptRequestAfterAuthExcept(_ context.Context, _ pluginapi.RequestInterceptRequest, skipPluginID string) pluginapi.RequestInterceptResponse {
|
||||
h.afterAuthSkips = append(h.afterAuthSkips, skipPluginID)
|
||||
return pluginapi.RequestInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *skipAwareInterceptorHost) InterceptResponseExcept(_ context.Context, _ pluginapi.ResponseInterceptRequest, skipPluginID string) pluginapi.ResponseInterceptResponse {
|
||||
h.responseSkips = append(h.responseSkips, skipPluginID)
|
||||
return pluginapi.ResponseInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *skipAwareInterceptorHost) InterceptStreamChunkExcept(_ context.Context, _ pluginapi.StreamChunkInterceptRequest, skipPluginID string) pluginapi.StreamChunkInterceptResponse {
|
||||
h.streamSkips = append(h.streamSkips, skipPluginID)
|
||||
return pluginapi.StreamChunkInterceptResponse{}
|
||||
}
|
||||
|
||||
// noRequestInterceptorsHost implements the detector interface to confirm the
|
||||
// delegated host honors a caller's opt-out of request interception.
|
||||
type noRequestInterceptorsHost struct {
|
||||
recordingInterceptorHost
|
||||
}
|
||||
|
||||
func (h *noRequestInterceptorsHost) HasRequestInterceptors() bool { return false }
|
||||
|
||||
// TestServerDelegatesInterceptorCallsToEmbedderThroughWiredHost proves the
|
||||
// host installed on server.handlers via WithPluginInterceptorHost actually
|
||||
// reaches the embedder through the handler dispatch chain, not just that a
|
||||
// wrapper object exists. The server's interceptorHost field is the resolved
|
||||
// host BaseAPIHandler.interceptorHost() reads on every request.
|
||||
func TestServerDelegatesInterceptorCallsToEmbedderThroughWiredHost(t *testing.T) {
|
||||
custom := &recordingInterceptorHost{}
|
||||
server := newTestServerWithOptions(t, WithPluginHost(pluginhost.New()), WithPluginInterceptorHost(custom))
|
||||
|
||||
if server.handlers == nil {
|
||||
t.Fatal("server handlers = nil")
|
||||
}
|
||||
// The wired host on the handler must be the delegated wrapper.
|
||||
wired, ok := server.handlers.PluginHost.(*delegatedInterceptorHost)
|
||||
if !ok || wired == nil {
|
||||
t.Fatalf("handler plugin host = %T, want *delegatedInterceptorHost", server.handlers.PluginHost)
|
||||
}
|
||||
// The same resolved host must be what the server holds for reloads.
|
||||
if server.interceptorHost != wired {
|
||||
t.Fatal("server.interceptorHost != handler plugin host; reload would rewire a different host")
|
||||
}
|
||||
// Exercise the host through the same call the handler makes per request.
|
||||
wired.InterceptRequestBeforeAuth(context.Background(), pluginapi.RequestInterceptRequest{})
|
||||
if custom.beforeAuthCalls != 1 {
|
||||
t.Fatalf("custom host before-auth calls = %d, want 1 through wired host", custom.beforeAuthCalls)
|
||||
}
|
||||
// Capability detection must report the embedder as active so the handler
|
||||
// does not skip its interceptor.
|
||||
if !wired.HasRequestInterceptors() {
|
||||
t.Fatal("wired host HasRequestInterceptors() = false, want true for custom host")
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewServerWithPluginInterceptorHostInstallsCustomHost verifies the
|
||||
// WithPluginInterceptorHost option wires the caller-provided host into request
|
||||
// handlers at construction time.
|
||||
func TestNewServerWithPluginInterceptorHostInstallsCustomHost(t *testing.T) {
|
||||
custom := &recordingInterceptorHost{}
|
||||
server := newTestServerWithOptions(t, WithPluginHost(pluginhost.New()), WithPluginInterceptorHost(custom))
|
||||
|
||||
if server.handlers == nil {
|
||||
t.Fatal("server handlers = nil")
|
||||
}
|
||||
if server.handlers.PluginHost == nil {
|
||||
t.Fatal("handler plugin host = nil, want wrapped custom interceptor host")
|
||||
}
|
||||
if _, ok := server.handlers.PluginHost.(*delegatedInterceptorHost); !ok {
|
||||
t.Fatalf("handler plugin host type = %T, want *delegatedInterceptorHost", server.handlers.PluginHost)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUpdateClientsContextPreservesPluginInterceptorHost ensures a config
|
||||
// reload keeps the caller-provided interceptor host instead of restoring the
|
||||
// internal plugin host.
|
||||
func TestUpdateClientsContextPreservesPluginInterceptorHost(t *testing.T) {
|
||||
custom := &recordingInterceptorHost{}
|
||||
pluginHost := pluginhost.New()
|
||||
server := newTestServerWithOptions(t, WithPluginHost(pluginHost), WithPluginInterceptorHost(custom))
|
||||
|
||||
if server.handlers == nil {
|
||||
t.Fatal("server handlers = nil")
|
||||
}
|
||||
if _, ok := server.handlers.PluginHost.(*delegatedInterceptorHost); !ok {
|
||||
t.Fatalf("handler plugin host type = %T, want *delegatedInterceptorHost", server.handlers.PluginHost)
|
||||
}
|
||||
|
||||
server.UpdateClientsContext(context.Background(), &proxyconfig.Config{})
|
||||
|
||||
after, ok := server.handlers.PluginHost.(*delegatedInterceptorHost)
|
||||
if !ok || after == nil {
|
||||
t.Fatalf("handler plugin host after reload = %T, want *delegatedInterceptorHost", server.handlers.PluginHost)
|
||||
}
|
||||
if after.primary != custom {
|
||||
t.Fatal("delegated host primary changed after reload; want same embedder host")
|
||||
}
|
||||
if after.pluginHost != pluginHost {
|
||||
t.Fatal("delegated host pluginHost changed after reload; want internal plugin host")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDelegatedInterceptorHostInvokesEmbedderInterceptors verifies the
|
||||
// capability detectors reflect the embedder's host rather than the internal
|
||||
// plugin host, so an empty plugin host cannot disable the custom interceptors.
|
||||
func TestDelegatedInterceptorHostInvokesEmbedderInterceptors(t *testing.T) {
|
||||
custom := &recordingInterceptorHost{}
|
||||
wrapped := &delegatedInterceptorHost{primary: custom, pluginHost: pluginhost.New()}
|
||||
|
||||
if !wrapped.HasRequestInterceptors() {
|
||||
t.Fatal("HasRequestInterceptors() = false for custom host without detector, want true")
|
||||
}
|
||||
if !wrapped.HasStreamInterceptors() {
|
||||
t.Fatal("HasStreamInterceptors() = false for custom host without detector, want true")
|
||||
}
|
||||
|
||||
wrapped.InterceptRequestBeforeAuth(context.Background(), pluginapi.RequestInterceptRequest{})
|
||||
if custom.beforeAuthCalls != 1 {
|
||||
t.Fatalf("custom host before-auth calls = %d, want 1", custom.beforeAuthCalls)
|
||||
}
|
||||
|
||||
disabled := &noRequestInterceptorsHost{}
|
||||
wrappedDisabled := &delegatedInterceptorHost{primary: disabled, pluginHost: pluginhost.New()}
|
||||
if wrappedDisabled.HasRequestInterceptors() {
|
||||
t.Fatal("HasRequestInterceptors() = true when embedder disables it, want false")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDelegatedInterceptorHostForwardsSkipToEmbedder verifies that when the
|
||||
// embedder's host implements the skip-aware interceptor interface, the
|
||||
// delegated host passes skipPluginID through instead of dropping it.
|
||||
func TestDelegatedInterceptorHostForwardsSkipToEmbedder(t *testing.T) {
|
||||
custom := &skipAwareInterceptorHost{}
|
||||
wrapped := &delegatedInterceptorHost{primary: custom, pluginHost: pluginhost.New()}
|
||||
|
||||
wrapped.InterceptRequestBeforeAuthExcept(context.Background(), pluginapi.RequestInterceptRequest{}, "plugin-a")
|
||||
wrapped.InterceptRequestAfterAuthExcept(context.Background(), pluginapi.RequestInterceptRequest{}, "plugin-b")
|
||||
wrapped.InterceptResponseExcept(context.Background(), pluginapi.ResponseInterceptRequest{}, "plugin-c")
|
||||
wrapped.InterceptStreamChunkExcept(context.Background(), pluginapi.StreamChunkInterceptRequest{}, "plugin-d")
|
||||
|
||||
if len(custom.beforeAuthSkips) != 1 || custom.beforeAuthSkips[0] != "plugin-a" {
|
||||
t.Fatalf("before-auth skips = %v, want [plugin-a]", custom.beforeAuthSkips)
|
||||
}
|
||||
if len(custom.afterAuthSkips) != 1 || custom.afterAuthSkips[0] != "plugin-b" {
|
||||
t.Fatalf("after-auth skips = %v, want [plugin-b]", custom.afterAuthSkips)
|
||||
}
|
||||
if len(custom.responseSkips) != 1 || custom.responseSkips[0] != "plugin-c" {
|
||||
t.Fatalf("response skips = %v, want [plugin-c]", custom.responseSkips)
|
||||
}
|
||||
if len(custom.streamSkips) != 1 || custom.streamSkips[0] != "plugin-d" {
|
||||
t.Fatalf("stream skips = %v, want [plugin-d]", custom.streamSkips)
|
||||
}
|
||||
// A primary without the skip interface still gets invoked via the base methods.
|
||||
plain := &recordingInterceptorHost{}
|
||||
wrappedPlain := &delegatedInterceptorHost{primary: plain, pluginHost: pluginhost.New()}
|
||||
wrappedPlain.InterceptRequestBeforeAuthExcept(context.Background(), pluginapi.RequestInterceptRequest{}, "plugin-a")
|
||||
if plain.beforeAuthCalls != 1 {
|
||||
t.Fatalf("plain host before-auth calls = %d, want 1", plain.beforeAuthCalls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDelegatedInterceptorHostPluginCallbacksPreserveSkip verifies lifecycle
|
||||
// and plugin-callback skip paths remain wired to the internal plugin host with
|
||||
// the skip identifier intact. The wrapper's *Except methods are compile-time
|
||||
// members of the same skip interfaces the handler type-asserts on, so a
|
||||
// signature drift that dropped skipPluginID would fail to compile.
|
||||
func TestDelegatedInterceptorHostPluginCallbacksPreserveSkip(t *testing.T) {
|
||||
pluginHost := pluginhost.New()
|
||||
custom := &recordingInterceptorHost{}
|
||||
wrapped := &delegatedInterceptorHost{primary: custom, pluginHost: pluginHost}
|
||||
|
||||
// Compile-time proof the wrapper still satisfies the same skip-aware
|
||||
// interfaces the handler dispatches through; a regression that dropped the
|
||||
// Except methods would silently route callbacks back to the initiating
|
||||
// plugin and recurse.
|
||||
var _ interface {
|
||||
CompleteRequestExcept(context.Context, pluginapi.RequestCompletion, string)
|
||||
} = wrapped
|
||||
var _ interface {
|
||||
ObserveWebSocketResponseEventExcept(context.Context, pluginapi.WebSocketResponseEvent, string)
|
||||
} = wrapped
|
||||
var _ interface {
|
||||
RouteModelExcept(context.Context, pluginapi.ModelRouteRequest, string) (pluginapi.ModelRouteResponse, bool)
|
||||
} = wrapped
|
||||
|
||||
// Exercise skip-aware dispatch paths; they must reach the plugin host
|
||||
// without error rather than dead-ending on the embedder's host.
|
||||
wrapped.CompleteRequestExcept(context.Background(), pluginapi.RequestCompletion{}, "plugin-a")
|
||||
wrapped.ObserveWebSocketResponseEventExcept(context.Background(), pluginapi.WebSocketResponseEvent{}, "plugin-a")
|
||||
wrapped.RouteModelExcept(context.Background(), pluginapi.ModelRouteRequest{}, "plugin-a")
|
||||
// Model routing must stay delegated to the internal plugin host so plugin
|
||||
// executors remain reachable even with a custom interceptor installed.
|
||||
if _, handled := wrapped.RouteModel(context.Background(), pluginapi.ModelRouteRequest{}); handled {
|
||||
_ = handled
|
||||
}
|
||||
if wrapped.HasModelRouters() != pluginHost.HasModelRouters() {
|
||||
t.Fatal("wrapper HasModelRouters diverged from internal plugin host")
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewServerWithoutPluginInterceptorHostKeepsInternalHost preserves the
|
||||
// default behavior when no custom interceptor host is configured.
|
||||
func TestNewServerWithoutPluginInterceptorHostKeepsInternalHost(t *testing.T) {
|
||||
pluginHost := pluginhost.New()
|
||||
server := newTestServerWithOptions(t, WithPluginHost(pluginHost))
|
||||
|
||||
if server.handlers == nil {
|
||||
t.Fatal("server handlers = nil")
|
||||
}
|
||||
if server.handlers.PluginHost != pluginHost {
|
||||
t.Fatalf("handler plugin host = %#v, want internal plugin host %#v", server.handlers.PluginHost, pluginHost)
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewServerWithTypedNilPluginInterceptorHostFallsBack ensures a typed-nil
|
||||
// custom host is normalized to unset so the internal plugin host is used.
|
||||
func TestNewServerWithTypedNilPluginInterceptorHostFallsBack(t *testing.T) {
|
||||
pluginHost := pluginhost.New()
|
||||
var typedNil *recordingInterceptorHost
|
||||
server := newTestServerWithOptions(t, WithPluginHost(pluginHost), WithPluginInterceptorHost(typedNil))
|
||||
|
||||
if server.handlers == nil {
|
||||
t.Fatal("server handlers = nil")
|
||||
}
|
||||
if server.handlers.PluginHost != pluginHost {
|
||||
t.Fatalf("handler plugin host = %#v, want internal plugin host %#v for typed-nil custom host", server.handlers.PluginHost, pluginHost)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDelegatedInterceptorHostImplementsPluginExecutorHost verifies the
|
||||
// wrapper satisfies the PluginExecutorHost contract so plugin model routes to
|
||||
// a plugin executor do not regress to 502 when a custom interceptor host is
|
||||
// installed. The wrapped host delegates execution to the internal pluginHost.
|
||||
func TestDelegatedInterceptorHostImplementsPluginExecutorHost(t *testing.T) {
|
||||
custom := &recordingInterceptorHost{}
|
||||
pluginHost := pluginhost.New()
|
||||
wrapped := &delegatedInterceptorHost{primary: custom, pluginHost: pluginHost}
|
||||
|
||||
// Compile-time + runtime proof the wrapper still exposes the executor host
|
||||
// interface the handler type-asserts on for plugin-executor routes.
|
||||
executorHost, ok := any(wrapped).(interface {
|
||||
ExecutePluginExecutor(context.Context, string, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error)
|
||||
ExecutePluginExecutorStream(context.Context, string, coreexecutor.Request, coreexecutor.Options) (*coreexecutor.StreamResult, error)
|
||||
CountPluginExecutor(context.Context, string, coreexecutor.Request, coreexecutor.Options) (coreexecutor.Response, error)
|
||||
})
|
||||
if !ok || executorHost == nil {
|
||||
t.Fatal("delegated host does not implement PluginExecutorHost; plugin executor routes would 502")
|
||||
}
|
||||
// Nil-pluginHost delegates must return an explicit unavailable error rather
|
||||
// than panic, matching the handler's unavailable-host contract.
|
||||
safeWrapped := &delegatedInterceptorHost{primary: custom, pluginHost: nil}
|
||||
if _, err := safeWrapped.ExecutePluginExecutor(context.Background(), "p", coreexecutor.Request{}, coreexecutor.Options{}); err == nil {
|
||||
t.Fatal("ExecutePluginExecutor with nil plugin host = nil error, want unavailable")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDelegatedInterceptorHostNilPluginHostIsSafe ensures the delegated host is
|
||||
// safe when no internal plugin host exists.
|
||||
func TestDelegatedInterceptorHostNilPluginHostIsSafe(t *testing.T) {
|
||||
custom := &recordingInterceptorHost{}
|
||||
server := newTestServerWithOptions(t, WithPluginInterceptorHost(custom))
|
||||
|
||||
if server.handlers == nil {
|
||||
t.Fatal("server handlers = nil")
|
||||
}
|
||||
wrapped, ok := server.handlers.PluginHost.(*delegatedInterceptorHost)
|
||||
if !ok || wrapped == nil {
|
||||
t.Fatalf("handler plugin host type = %T, want *delegatedInterceptorHost", server.handlers.PluginHost)
|
||||
}
|
||||
// Embedder interceptors remain enabled even without an internal plugin host.
|
||||
if !wrapped.HasRequestInterceptors() {
|
||||
t.Fatal("HasRequestInterceptors() = false with nil plugin host, want true for custom host")
|
||||
}
|
||||
if wrapped.HasModelRouters() {
|
||||
t.Fatal("HasModelRouters() = true with nil plugin host, want false")
|
||||
}
|
||||
// Nil-pluginHost delegates must be no-ops rather than panics.
|
||||
wrapped.RouteModel(context.Background(), pluginapi.ModelRouteRequest{})
|
||||
wrapped.CompleteRequest(context.Background(), pluginapi.RequestCompletion{})
|
||||
wrapped.ObserveWebSocketResponseEvent(context.Background(), pluginapi.WebSocketResponseEvent{})
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
)
|
||||
|
||||
type serverOptionConfig struct {
|
||||
pluginInterceptorHost handlers.PluginInterceptorHost
|
||||
extraMiddleware []gin.HandlerFunc
|
||||
engineConfigurator func(*gin.Engine)
|
||||
routerConfigurator func(*gin.Engine, *handlers.BaseAPIHandler, *config.Config)
|
||||
@@ -114,6 +115,17 @@ func WithPostAuthPersistHook(hook auth.PostAuthHook) ServerOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithPluginInterceptorHost installs a caller-provided plugin interceptor host
|
||||
// on request handlers. When set, the server forwards interceptor calls to this
|
||||
// host while keeping model routing, lifecycle callbacks, and websocket observer
|
||||
// dispatch delegated to the internal plugin host so plugin features stay
|
||||
// functional across startup and config reloads.
|
||||
func WithPluginInterceptorHost(host handlers.PluginInterceptorHost) ServerOption {
|
||||
return func(cfg *serverOptionConfig) {
|
||||
cfg.pluginInterceptorHost = host
|
||||
}
|
||||
}
|
||||
|
||||
// WithPluginHost registers dynamic plugin HTTP adapters with the server.
|
||||
func WithPluginHost(host *pluginhost.Host) ServerOption {
|
||||
return func(cfg *serverOptionConfig) {
|
||||
|
||||
@@ -206,7 +206,7 @@ func (s *Server) UpdateClientsContext(ctx context.Context, cfg *config.Config) b
|
||||
s.cfgMu.Unlock()
|
||||
|
||||
s.handlers.UpdateClients(effectiveSDKConfig(cfg))
|
||||
s.handlers.SetPluginHost(s.pluginHost)
|
||||
s.handlers.SetPluginHost(s.interceptorHost)
|
||||
if s.pluginHost != nil {
|
||||
s.pluginHost.SetModelExecutor(s.handlers)
|
||||
s.pluginHost.SetAuthManager(s.handlers.AuthManager)
|
||||
|
||||
162
sdk/api/handlers/handlers_interceptors_issue7499_test.go
Normal file
162
sdk/api/handlers/handlers_interceptors_issue7499_test.go
Normal file
@@ -0,0 +1,162 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
coreexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/pluginapi"
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
||||
)
|
||||
|
||||
// issue7499InterceptorHost is a minimal PluginInterceptorHost used to prove
|
||||
// embedder-installed hosts are invoked through the real dispatch chain.
|
||||
type issue7499InterceptorHost struct {
|
||||
beforeAuthCalls int
|
||||
afterAuthCalls int
|
||||
responseCalls int
|
||||
streamCalls int
|
||||
}
|
||||
|
||||
func (h *issue7499InterceptorHost) InterceptRequestBeforeAuth(context.Context, pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse {
|
||||
h.beforeAuthCalls++
|
||||
return pluginapi.RequestInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *issue7499InterceptorHost) InterceptRequestAfterAuth(context.Context, pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse {
|
||||
h.afterAuthCalls++
|
||||
return pluginapi.RequestInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *issue7499InterceptorHost) InterceptResponse(context.Context, pluginapi.ResponseInterceptRequest) pluginapi.ResponseInterceptResponse {
|
||||
h.responseCalls++
|
||||
return pluginapi.ResponseInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *issue7499InterceptorHost) InterceptStreamChunk(context.Context, pluginapi.StreamChunkInterceptRequest) pluginapi.StreamChunkInterceptResponse {
|
||||
h.streamCalls++
|
||||
return pluginapi.StreamChunkInterceptResponse{}
|
||||
}
|
||||
|
||||
// issue7499DirectExecutorHost routes the request to a plugin executor so the
|
||||
// test can drive the real ExecuteWithAuthManager chain end to end and prove
|
||||
// the wired interceptor host is invoked during dispatch.
|
||||
type issue7499DirectExecutorHost struct {
|
||||
issue7499InterceptorHost
|
||||
handlerRouterOnlyTestHost
|
||||
lastPluginID string
|
||||
lastRequest coreexecutor.Request
|
||||
}
|
||||
|
||||
func (h *issue7499DirectExecutorHost) HasModelRouters() bool { return h.hasRouters }
|
||||
|
||||
func (h *issue7499DirectExecutorHost) ExecutePluginExecutor(ctx context.Context, pluginID string, req coreexecutor.Request, opts coreexecutor.Options) (coreexecutor.Response, error) {
|
||||
h.lastPluginID = pluginID
|
||||
h.lastRequest = req
|
||||
return coreexecutor.Response{Payload: []byte("direct-ok")}, nil
|
||||
}
|
||||
|
||||
func (h *issue7499DirectExecutorHost) CountPluginExecutor(ctx context.Context, pluginID string, req coreexecutor.Request, opts coreexecutor.Options) (coreexecutor.Response, error) {
|
||||
return coreexecutor.Response{Payload: []byte(`{}`)}, nil
|
||||
}
|
||||
|
||||
func (h *issue7499DirectExecutorHost) ExecutePluginExecutorStream(ctx context.Context, pluginID string, req coreexecutor.Request, opts coreexecutor.Options) (*coreexecutor.StreamResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (h *issue7499DirectExecutorHost) PluginExecutorRequestToFormat(pluginID string, req coreexecutor.Request, opts coreexecutor.Options) sdktranslator.Format {
|
||||
return sdktranslator.FormatOpenAI
|
||||
}
|
||||
|
||||
// issue7499SkipHost implements the pluginInterceptorSkipHost surface so the
|
||||
// test can assert skipPluginID survives dispatch.
|
||||
type issue7499SkipHost struct {
|
||||
issue7499DirectExecutorHost
|
||||
beforeSkips []string
|
||||
afterSkips []string
|
||||
respSkips []string
|
||||
streamSkips []string
|
||||
}
|
||||
|
||||
func (h *issue7499SkipHost) InterceptRequestBeforeAuthExcept(_ context.Context, _ pluginapi.RequestInterceptRequest, skipPluginID string) pluginapi.RequestInterceptResponse {
|
||||
h.beforeSkips = append(h.beforeSkips, skipPluginID)
|
||||
return pluginapi.RequestInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *issue7499SkipHost) InterceptRequestAfterAuthExcept(_ context.Context, _ pluginapi.RequestInterceptRequest, skipPluginID string) pluginapi.RequestInterceptResponse {
|
||||
h.afterSkips = append(h.afterSkips, skipPluginID)
|
||||
return pluginapi.RequestInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *issue7499SkipHost) InterceptResponseExcept(_ context.Context, _ pluginapi.ResponseInterceptRequest, skipPluginID string) pluginapi.ResponseInterceptResponse {
|
||||
h.respSkips = append(h.respSkips, skipPluginID)
|
||||
return pluginapi.ResponseInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *issue7499SkipHost) InterceptStreamChunkExcept(_ context.Context, _ pluginapi.StreamChunkInterceptRequest, skipPluginID string) pluginapi.StreamChunkInterceptResponse {
|
||||
h.streamSkips = append(h.streamSkips, skipPluginID)
|
||||
return pluginapi.StreamChunkInterceptResponse{}
|
||||
}
|
||||
|
||||
var _ pluginInterceptorSkipHost = (*issue7499SkipHost)(nil)
|
||||
|
||||
// TestIssue7499CustomInterceptorHostInvokedThroughExecuteChain drives the
|
||||
// real ExecuteWithAuthManager dispatch path so a miswired interceptor host
|
||||
// cannot pass by only existing as a field.
|
||||
func TestIssue7499CustomInterceptorHostInvokedThroughExecuteChain(t *testing.T) {
|
||||
originalModel := "issue7499-original-model"
|
||||
targetPluginID := "websearch-plugin"
|
||||
host := &issue7499DirectExecutorHost{}
|
||||
host.hasRouters = true
|
||||
host.route = func(context.Context, pluginapi.ModelRouteRequest) (pluginapi.ModelRouteResponse, bool) {
|
||||
return pluginapi.ModelRouteResponse{Handled: true, TargetKind: pluginapi.ModelRouteTargetExecutor, Target: targetPluginID}, true
|
||||
}
|
||||
handler := NewBaseAPIHandlers(nil, nil)
|
||||
handler.SetModelRouterHost(host)
|
||||
handler.SetPluginHost(host)
|
||||
|
||||
body, _, errMsg := handler.ExecuteWithAuthManager(context.Background(), "openai", originalModel,
|
||||
[]byte(`{"model":"issue7499-original-model"}`), "")
|
||||
if errMsg != nil {
|
||||
t.Fatalf("ExecuteWithAuthManager() error = %+v", errMsg)
|
||||
}
|
||||
if string(body) != "direct-ok" {
|
||||
t.Fatalf("body = %q, want direct-ok", body)
|
||||
}
|
||||
if host.beforeAuthCalls != 1 {
|
||||
t.Fatalf("before-auth calls through execute chain = %d, want 1", host.beforeAuthCalls)
|
||||
}
|
||||
if host.lastPluginID != targetPluginID {
|
||||
t.Fatalf("plugin id = %q, want %q", host.lastPluginID, targetPluginID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIssue7499CustomInterceptorHostSkipThroughExecuteChain verifies a
|
||||
// skip-aware embedder host receives the plugin-skip identifier through the
|
||||
// real dispatch chain, not a dropped or rewritten value.
|
||||
func TestIssue7499CustomInterceptorHostSkipThroughExecuteChain(t *testing.T) {
|
||||
originalModel := "issue7499-skip-model"
|
||||
targetPluginID := "websearch-plugin"
|
||||
host := &issue7499SkipHost{}
|
||||
host.hasRouters = true
|
||||
// Route to a plugin executor while skipping interceptor "plugin-a" so the
|
||||
// nested execution cannot re-enter its own interceptor.
|
||||
host.route = func(context.Context, pluginapi.ModelRouteRequest) (pluginapi.ModelRouteResponse, bool) {
|
||||
return pluginapi.ModelRouteResponse{Handled: true, TargetKind: pluginapi.ModelRouteTargetExecutor, Target: targetPluginID}, true
|
||||
}
|
||||
handler := NewBaseAPIHandlers(nil, nil)
|
||||
handler.SetModelRouterHost(host)
|
||||
handler.SetPluginHost(host)
|
||||
|
||||
// Drive the interceptor dispatch directly with an explicit skip id; the
|
||||
// public execute path derives it from nested-execution metadata.
|
||||
_, _, errMsg := handler.applyRequestInterceptorsBeforeAuth(
|
||||
context.Background(), "openai", originalModel, "req-skip",
|
||||
coreexecutor.Request{Model: originalModel}, coreexecutor.Options{}, "plugin-a")
|
||||
if errMsg != nil {
|
||||
t.Fatalf("applyRequestInterceptorsBeforeAuth error = %+v", errMsg)
|
||||
}
|
||||
if len(host.beforeSkips) != 1 || host.beforeSkips[0] != "plugin-a" {
|
||||
t.Fatalf("before-auth skip calls = %v, want [plugin-a]", host.beforeSkips)
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/watcher"
|
||||
sdkaccess "github.com/router-for-me/CLIProxyAPI/v8/sdk/access"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/api/handlers"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v8/sdk/auth"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/config"
|
||||
@@ -52,6 +53,11 @@ type Builder struct {
|
||||
// cooldownStateStore overrides runtime cooldown persistence.
|
||||
cooldownStateStore coreauth.CooldownStateStore
|
||||
|
||||
// pluginInterceptorHost optionally overrides the PluginInterceptorHost
|
||||
// installed on request handlers. The internal pluginHost still owns management
|
||||
// routes and plugin executor wiring.
|
||||
pluginInterceptorHost handlers.PluginInterceptorHost
|
||||
|
||||
// pluginHost owns dynamic plugin lifecycle and adapters.
|
||||
pluginHost *pluginhost.Host
|
||||
|
||||
@@ -73,6 +79,13 @@ type Hooks struct {
|
||||
// modifications or additional setup.
|
||||
OnBeforeStart func(*config.Config)
|
||||
|
||||
// OnConfigApplied is called once at the end of every successful runtime
|
||||
// config apply, after executor re-registration and model sync. Embedders can
|
||||
// use it to reinstall caller-owned state (executor wrappers, custom selector
|
||||
// adjustments, interceptor context) that config apply would otherwise reset.
|
||||
// The hook runs inside the service config runtime mutex; it must not block.
|
||||
OnConfigApplied func(*Service, *config.Config)
|
||||
|
||||
// OnAfterStart is called after the service has started successfully,
|
||||
// providing access to the service instance for additional operations.
|
||||
OnAfterStart func(*Service)
|
||||
@@ -159,6 +172,16 @@ func (b *Builder) WithCooldownStateStore(store coreauth.CooldownStateStore) *Bui
|
||||
return b
|
||||
}
|
||||
|
||||
// WithPluginInterceptorHost sets a caller-provided plugin interceptor host for
|
||||
// request handling. The server wraps it so plugin model routing, lifecycle
|
||||
// callbacks, and websocket observers keep delegating to the internal plugin
|
||||
// host; management routes and plugin executors are unaffected. When unset the
|
||||
// internal plugin host remains the request interceptor host as before.
|
||||
func (b *Builder) WithPluginInterceptorHost(host handlers.PluginInterceptorHost) *Builder {
|
||||
b.pluginInterceptorHost = host
|
||||
return b
|
||||
}
|
||||
|
||||
// WithPluginHost overrides the dynamic plugin host used by the service.
|
||||
func (b *Builder) WithPluginHost(host *pluginhost.Host) *Builder {
|
||||
b.pluginHost = host
|
||||
@@ -278,26 +301,30 @@ func (b *Builder) Build() (*Service, error) {
|
||||
}
|
||||
|
||||
service := &Service{
|
||||
cfg: b.cfg.CloneForRuntime(),
|
||||
configPath: b.configPath,
|
||||
tokenProvider: tokenProvider,
|
||||
apiKeyProvider: apiKeyProvider,
|
||||
watcherFactory: watcherFactory,
|
||||
hooks: b.hooks,
|
||||
authManager: authManager,
|
||||
accessManager: accessManager,
|
||||
coreManager: coreManager,
|
||||
cooldownStateStore: cooldownStateStore,
|
||||
pluginHost: pluginHost,
|
||||
discoveryManager: newDiscoveryAdvertiserManager(),
|
||||
appliedRoutingState: appliedRoutingState,
|
||||
serverOptions: append([]api.ServerOption(nil), b.serverOptions...),
|
||||
pluginInterceptorHost: b.pluginInterceptorHost,
|
||||
cfg: b.cfg.CloneForRuntime(),
|
||||
configPath: b.configPath,
|
||||
tokenProvider: tokenProvider,
|
||||
apiKeyProvider: apiKeyProvider,
|
||||
watcherFactory: watcherFactory,
|
||||
hooks: b.hooks,
|
||||
authManager: authManager,
|
||||
accessManager: accessManager,
|
||||
coreManager: coreManager,
|
||||
cooldownStateStore: cooldownStateStore,
|
||||
pluginHost: pluginHost,
|
||||
discoveryManager: newDiscoveryAdvertiserManager(),
|
||||
appliedRoutingState: appliedRoutingState,
|
||||
serverOptions: append([]api.ServerOption(nil), b.serverOptions...),
|
||||
}
|
||||
service.proxyPoolPublisher = helps.RegisterProxyPoolPublisher()
|
||||
helps.AdvanceProxyPoolGeneration(service.proxyPoolPublisher, service.cfg)
|
||||
if b.postAuthHook != nil {
|
||||
service.serverOptions = append(service.serverOptions, api.WithPostAuthHook(b.postAuthHook))
|
||||
}
|
||||
if b.pluginInterceptorHost != nil {
|
||||
service.serverOptions = append(service.serverOptions, api.WithPluginInterceptorHost(b.pluginInterceptorHost))
|
||||
}
|
||||
service.serverOptions = append(service.serverOptions,
|
||||
api.WithPostAuthPersistHook(service.runtimeAuthSyncHook()),
|
||||
api.WithPluginHost(pluginHost),
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/watcher"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/wsrelay"
|
||||
sdkaccess "github.com/router-for-me/CLIProxyAPI/v8/sdk/access"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/api/handlers"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v8/sdk/auth"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executionregistry"
|
||||
@@ -110,6 +111,10 @@ type Service struct {
|
||||
// pluginHost owns dynamic plugin lifecycle and runtime capability adapters.
|
||||
pluginHost *pluginhost.Host
|
||||
|
||||
// pluginInterceptorHost optionally overrides the PluginInterceptorHost
|
||||
// installed on request handlers. Nil means the internal pluginHost is used.
|
||||
pluginInterceptorHost handlers.PluginInterceptorHost
|
||||
|
||||
// shutdownOnce ensures shutdown is called only once.
|
||||
shutdownOnce sync.Once
|
||||
|
||||
|
||||
@@ -263,6 +263,9 @@ func (s *Service) applyConfigRuntime(ctx context.Context, commit configCommit, s
|
||||
return false
|
||||
}
|
||||
s.syncPluginModelRuntime(registrationCtx)
|
||||
if ctx.Err() == nil && s.hooks.OnConfigApplied != nil {
|
||||
s.hooks.OnConfigApplied(s, cfg)
|
||||
}
|
||||
return ctx.Err() == nil
|
||||
}
|
||||
|
||||
|
||||
287
sdk/cliproxy/service_issue7499_test.go
Normal file
287
sdk/cliproxy/service_issue7499_test.go
Normal file
@@ -0,0 +1,287 @@
|
||||
package cliproxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
internalconfig "github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/api/handlers"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/pluginapi"
|
||||
)
|
||||
|
||||
// recordingInterceptorHost7499 implements the public handlers.PluginInterceptorHost
|
||||
// surface with the minimum behavior needed by issue 7499 regression tests.
|
||||
type recordingInterceptorHost7499 struct{}
|
||||
|
||||
func (h *recordingInterceptorHost7499) InterceptRequestBeforeAuth(context.Context, pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse {
|
||||
return pluginapi.RequestInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *recordingInterceptorHost7499) InterceptRequestAfterAuth(context.Context, pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse {
|
||||
return pluginapi.RequestInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *recordingInterceptorHost7499) InterceptResponse(context.Context, pluginapi.ResponseInterceptRequest) pluginapi.ResponseInterceptResponse {
|
||||
return pluginapi.ResponseInterceptResponse{}
|
||||
}
|
||||
|
||||
func (h *recordingInterceptorHost7499) InterceptStreamChunk(context.Context, pluginapi.StreamChunkInterceptRequest) pluginapi.StreamChunkInterceptResponse {
|
||||
return pluginapi.StreamChunkInterceptResponse{}
|
||||
}
|
||||
|
||||
var _ handlers.PluginInterceptorHost = (*recordingInterceptorHost7499)(nil)
|
||||
|
||||
// stubProviderExecutor7499 is a minimal ProviderExecutor used to prove config
|
||||
// apply preserves a caller-installed wrapper for a built-in provider.
|
||||
type stubProviderExecutor7499 struct {
|
||||
provider string
|
||||
}
|
||||
|
||||
func (e *stubProviderExecutor7499) Identifier() string { return e.provider }
|
||||
|
||||
func (e *stubProviderExecutor7499) Execute(context.Context, *coreauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
||||
return cliproxyexecutor.Response{}, errors.New("stub executor")
|
||||
}
|
||||
|
||||
func (e *stubProviderExecutor7499) ExecuteStream(context.Context, *coreauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) {
|
||||
return nil, errors.New("stub executor")
|
||||
}
|
||||
|
||||
func (e *stubProviderExecutor7499) Refresh(context.Context, *coreauth.Auth) (*coreauth.Auth, error) {
|
||||
return nil, errors.New("stub executor")
|
||||
}
|
||||
|
||||
func (e *stubProviderExecutor7499) CountTokens(context.Context, *coreauth.Auth, cliproxyexecutor.Request, cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
||||
return cliproxyexecutor.Response{}, errors.New("stub executor")
|
||||
}
|
||||
|
||||
func (e *stubProviderExecutor7499) HttpRequest(context.Context, *coreauth.Auth, *http.Request) (*http.Response, error) {
|
||||
return nil, errors.New("stub executor")
|
||||
}
|
||||
|
||||
var _ coreauth.ProviderExecutor = (*stubProviderExecutor7499)(nil)
|
||||
|
||||
// newIssue7499Service builds a Service whose runtime application is fully
|
||||
// stubbed so config apply stays in memory without touching the network.
|
||||
// seedAuths are pre-registered on the core manager so applyConfigRuntime's
|
||||
// force-replace executor pass sees them the same way a real deployment would.
|
||||
func newIssue7499Service(t *testing.T, cfg *config.Config, hooks Hooks, seedAuths ...*coreauth.Auth) *Service {
|
||||
t.Helper()
|
||||
manager := coreauth.NewManager(nil, nil, nil)
|
||||
for _, auth := range seedAuths {
|
||||
if auth != nil {
|
||||
manager.Register(context.Background(), auth)
|
||||
}
|
||||
}
|
||||
service := &Service{
|
||||
cfg: cfg,
|
||||
coreManager: manager,
|
||||
hooks: hooks,
|
||||
updateServerClientsContextFn: func(context.Context, *config.Config) bool {
|
||||
return true
|
||||
},
|
||||
applyPprofConfigContextFn: func(context.Context, *config.Config) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
return service
|
||||
}
|
||||
|
||||
// claudeWrapperExecutor7499 wraps a ProviderExecutor and exposes the current
|
||||
// inner executor so tests can assert the hook re-wraps the rebuilt built-in
|
||||
// executor rather than keeping a stale wrapper.
|
||||
type claudeWrapperExecutor7499 struct {
|
||||
inner coreauth.ProviderExecutor
|
||||
}
|
||||
|
||||
func (w *claudeWrapperExecutor7499) Identifier() string { return "claude" }
|
||||
|
||||
func (w *claudeWrapperExecutor7499) Execute(ctx context.Context, auth *coreauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
||||
return w.inner.Execute(ctx, auth, req, opts)
|
||||
}
|
||||
|
||||
func (w *claudeWrapperExecutor7499) ExecuteStream(ctx context.Context, auth *coreauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (*cliproxyexecutor.StreamResult, error) {
|
||||
return w.inner.ExecuteStream(ctx, auth, req, opts)
|
||||
}
|
||||
|
||||
func (w *claudeWrapperExecutor7499) Refresh(ctx context.Context, auth *coreauth.Auth) (*coreauth.Auth, error) {
|
||||
return w.inner.Refresh(ctx, auth)
|
||||
}
|
||||
|
||||
func (w *claudeWrapperExecutor7499) CountTokens(ctx context.Context, auth *coreauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) {
|
||||
return w.inner.CountTokens(ctx, auth, req, opts)
|
||||
}
|
||||
|
||||
func (w *claudeWrapperExecutor7499) HttpRequest(ctx context.Context, auth *coreauth.Auth, req *http.Request) (*http.Response, error) {
|
||||
return w.inner.HttpRequest(ctx, auth, req)
|
||||
}
|
||||
|
||||
var _ coreauth.ProviderExecutor = (*claudeWrapperExecutor7499)(nil)
|
||||
|
||||
// TestIssue7499OnConfigAppliedKeepsCustomExecutorWrapper installs a caller-owned
|
||||
// wrapper for the built-in "claude" provider via OnConfigApplied and requires a
|
||||
// subsequent watcher-style config apply to keep the wrapper in place instead of
|
||||
// restoring the stock built-in executor.
|
||||
func TestIssue7499OnConfigAppliedKeepsCustomExecutorWrapper(t *testing.T) {
|
||||
installed := &stubProviderExecutor7499{provider: "claude"}
|
||||
var appliedCfg *config.Config
|
||||
hookCalls := 0
|
||||
service := newIssue7499Service(t, &config.Config{}, Hooks{
|
||||
OnConfigApplied: func(s *Service, cfg *config.Config) {
|
||||
hookCalls++
|
||||
appliedCfg = cfg
|
||||
s.coreManager.RegisterExecutor(installed)
|
||||
},
|
||||
})
|
||||
|
||||
newCfg := &config.Config{RequestRetry: 3}
|
||||
service.applyWatcherConfigUpdate(newCfg)
|
||||
|
||||
if hookCalls != 1 {
|
||||
t.Fatalf("OnConfigApplied calls = %d, want 1", hookCalls)
|
||||
}
|
||||
if appliedCfg != service.cfg {
|
||||
t.Fatalf("OnConfigApplied config = %#v, want applied service config %#v", appliedCfg, service.cfg)
|
||||
}
|
||||
got, ok := service.coreManager.Executor("claude")
|
||||
if !ok {
|
||||
t.Fatal("claude executor missing after config apply")
|
||||
}
|
||||
if got != installed {
|
||||
t.Fatalf("claude executor = %#v (%T), want caller-installed wrapper %#v", got, got, installed)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIssue7499OnConfigAppliedRewrapsRebuiltClaudeExecutor registers a real
|
||||
// built-in Claude executor, applies two successive configs, and asserts the
|
||||
// hook reinstalls a wrapper around the *rebuilt* executor each time so the
|
||||
// embedder never retains a stale configuration snapshot.
|
||||
func TestIssue7499OnConfigAppliedRewrapsRebuiltClaudeExecutor(t *testing.T) {
|
||||
service := newIssue7499Service(t, &config.Config{}, Hooks{}, &coreauth.Auth{ID: "claude-1", Provider: "claude"})
|
||||
// Seed the built-in claude executor the same way the service does.
|
||||
service.coreManager.RegisterExecutor(executor.NewClaudeExecutor(service.cfg))
|
||||
|
||||
var wrappers []*claudeWrapperExecutor7499
|
||||
service.hooks = Hooks{
|
||||
OnConfigApplied: func(s *Service, cfg *config.Config) {
|
||||
current, ok := s.coreManager.Executor("claude")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// The force-replace registration may expose the previous wrapper
|
||||
// instead of the rebuilt stock executor; unwrap it so the embedder
|
||||
// always wraps the current built-in instance.
|
||||
for {
|
||||
if w, isWrapper := current.(*claudeWrapperExecutor7499); isWrapper {
|
||||
current = w.inner
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
wrapper := &claudeWrapperExecutor7499{inner: current}
|
||||
wrappers = append(wrappers, wrapper)
|
||||
s.coreManager.RegisterExecutor(wrapper)
|
||||
},
|
||||
}
|
||||
|
||||
service.applyWatcherConfigUpdate(&config.Config{RequestRetry: 1})
|
||||
firstInner := wrappers[0].inner
|
||||
if _, isClaude := firstInner.(*executor.ClaudeExecutor); !isClaude {
|
||||
t.Fatalf("first inner executor = %T, want *executor.ClaudeExecutor", firstInner)
|
||||
}
|
||||
|
||||
service.applyWatcherConfigUpdate(&config.Config{RequestRetry: 7})
|
||||
if len(wrappers) != 2 {
|
||||
t.Fatalf("wrapper installs = %d, want 2 across two applies", len(wrappers))
|
||||
}
|
||||
secondInner := wrappers[1].inner
|
||||
// The second apply must wrap a rebuilt built-in executor, not the previous
|
||||
// wrapper (which would hide the stock executor) and not the same instance
|
||||
// (which would carry stale configuration).
|
||||
if _, isClaude := secondInner.(*executor.ClaudeExecutor); !isClaude {
|
||||
t.Fatalf("second inner executor = %T, want *executor.ClaudeExecutor", secondInner)
|
||||
}
|
||||
if firstInner == secondInner {
|
||||
t.Fatal("hook re-wrapped the same executor instance; config rebuild was not applied")
|
||||
}
|
||||
if _, isWrapper := secondInner.(*claudeWrapperExecutor7499); isWrapper {
|
||||
t.Fatal("hook wrapped the previous wrapper instead of the rebuilt built-in executor")
|
||||
}
|
||||
|
||||
got, ok := service.coreManager.Executor("claude")
|
||||
if !ok || got != wrappers[1] {
|
||||
t.Fatalf("claude executor = %#v, want second wrapper %#v", got, wrappers[1])
|
||||
}
|
||||
}
|
||||
|
||||
// TestIssue7499OnConfigAppliedFiresOnEverySuccessfulApply ensures the hook is
|
||||
// invoked once per successful config commit, matching the existing lifecycle
|
||||
// contract that runtime state may be rebuilt on every apply.
|
||||
func TestIssue7499OnConfigAppliedFiresOnEverySuccessfulApply(t *testing.T) {
|
||||
hookCalls := 0
|
||||
service := newIssue7499Service(t, &config.Config{}, Hooks{
|
||||
OnConfigApplied: func(*Service, *config.Config) {
|
||||
hookCalls++
|
||||
},
|
||||
})
|
||||
|
||||
service.applyWatcherConfigUpdate(&config.Config{RequestRetry: 1})
|
||||
service.applyWatcherConfigUpdate(&config.Config{RequestRetry: 2})
|
||||
|
||||
if hookCalls != 2 {
|
||||
t.Fatalf("OnConfigApplied calls = %d, want 2 across two successful applies", hookCalls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIssue7499OnConfigAppliedNotCalledOnRejectedUpdate ensures the hook only
|
||||
// fires after a successful runtime apply, not for rejected or aborted commits.
|
||||
func TestIssue7499OnConfigAppliedNotCalledOnRejectedUpdate(t *testing.T) {
|
||||
hookCalls := 0
|
||||
service := newIssue7499Service(t, &config.Config{}, Hooks{
|
||||
OnConfigApplied: func(*Service, *config.Config) {
|
||||
hookCalls++
|
||||
},
|
||||
})
|
||||
|
||||
invalidWeight := internalconfig.MaxCredentialWeight + 1
|
||||
service.applyConfigUpdate(&internalconfig.Config{
|
||||
VertexCompatAPIKey: []internalconfig.VertexCompatKey{{
|
||||
APIKey: "vertex-key",
|
||||
Weight: &invalidWeight,
|
||||
}},
|
||||
})
|
||||
|
||||
if hookCalls != 0 {
|
||||
t.Fatalf("OnConfigApplied calls = %d, want 0 for rejected config", hookCalls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIssue7499WithPluginInterceptorHost verifies the builder accepts any
|
||||
// handlers.PluginInterceptorHost implementation and records it so the API
|
||||
// server forwards the embedder host instead of forcing the internal plugin host
|
||||
// onto handlers across startup and reload.
|
||||
func TestIssue7499WithPluginInterceptorHost(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
host := &recordingInterceptorHost7499{}
|
||||
|
||||
service, errBuild := NewBuilder().
|
||||
WithConfig(&config.Config{AuthDir: tmpDir}).
|
||||
WithConfigPath(tmpDir + "/config.yaml").
|
||||
WithPluginInterceptorHost(host).
|
||||
Build()
|
||||
if errBuild != nil {
|
||||
t.Fatalf("Build() error = %v", errBuild)
|
||||
}
|
||||
if service == nil {
|
||||
t.Fatal("Build() returned nil service")
|
||||
}
|
||||
if service.pluginInterceptorHost != host {
|
||||
t.Fatalf("service plugin interceptor host = %#v, want %#v", service.pluginInterceptorHost, host)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user