- Bump plugin schema to version 3 and introduce `SchemaVersionStreamChunkOmitRequestBody`. - Treat missing plugin schema versions as legacy during RPC registration (`0 -> 1`) and expose schema on plugin descriptors. - In stream interception, keep request headers/bodies on header-init chunk and stop re-sending them on payload chunks for schema-v3+ plugins, with per-chunk cloning for legacy plugins.
Unified · Split
| @@ -10,6 +10,7 @@ import ( · 9 unmodified lines | ||
| 10 | 10 | "strings" |
| 11 | 11 | |
| 12 | 12 | coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" |
| 13 | + "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginabi" | |
| 13 | 14 | "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi" |
| 14 | 15 | log "github.com/sirupsen/logrus" |
| 15 | 16 | ) |
| @@ -211,8 +212,15 @@ func (h *Host) InterceptStreamChunkExcept(ctx context.Context, req pluginapi.Str · 195 unmodified lines | ||
| 211 | 212 | nextReq := req |
| 212 | 213 | nextReq.RequestHeaders = cloneHeader(req.RequestHeaders) |
| 213 | 214 | nextReq.ResponseHeaders = cloneHeader(current.Headers) |
| 214 | − nextReq.OriginalRequest = bytes.Clone(req.OriginalRequest) | |
| 215 | − nextReq.RequestBody = bytes.Clone(req.RequestBody) | |
| 215 | + // Schema v3+ omits request bodies on payload chunks to avoid re-sending multi-MB | |
| 216 | + // prompts across cgo/JSON for every frame. Legacy plugins still receive them. | |
| 217 | + if req.ChunkIndex != pluginapi.StreamChunkHeaderInitIndex && streamChunkOmitsRequestBodies(record.plugin.SchemaVersion) { | |
| 218 | + nextReq.OriginalRequest = nil | |
| 219 | + nextReq.RequestBody = nil | |
| 220 | + } else { | |
| 221 | + nextReq.OriginalRequest = bytes.Clone(req.OriginalRequest) | |
| 222 | + nextReq.RequestBody = bytes.Clone(req.RequestBody) | |
| 223 | + } | |
| 216 | 224 | nextReq.Body = bytes.Clone(current.Body) |
| 217 | 225 | nextReq.HistoryChunks = cloneByteSlices(req.HistoryChunks) |
| 218 | 226 | nextReq.Metadata = cloneInterceptorMetadata(req.Metadata) |
| @@ -244,6 +252,28 @@ func (h *Host) HasStreamInterceptors() bool { · 25 unmodified lines | ||
| 244 | 252 | return false |
| 245 | 253 | } |
| 246 | 254 | |
| 255 | +// StreamChunkPayloadIncludesRequestBody reports whether any active stream chunk | |
| 256 | +// interceptor still requires OriginalRequest/RequestBody on payload chunks | |
| 257 | +// (schema_version < SchemaVersionStreamChunkOmitRequestBody). | |
| 258 | +func (h *Host) StreamChunkPayloadIncludesRequestBody() bool { | |
| 259 | + if h == nil { | |
| 260 | + return false | |
| 261 | + } | |
| 262 | + for _, record := range h.activeRecords() { | |
| 263 | + if h.isPluginFused(record.id) || record.plugin.Capabilities.StreamChunkInterceptor == nil { | |
| 264 | + continue | |
| 265 | + } | |
| 266 | + if !streamChunkOmitsRequestBodies(record.plugin.SchemaVersion) { | |
| 267 | + return true | |
| 268 | + } | |
| 269 | + } | |
| 270 | + return false | |
| 271 | +} | |
| 272 | + | |
| 273 | +func streamChunkOmitsRequestBodies(schemaVersion uint32) bool { | |
| 274 | + return schemaVersion >= pluginabi.SchemaVersionStreamChunkOmitRequestBody | |
| 275 | +} | |
| 276 | + | |
| 247 | 277 | func (h *Host) HasRequestInterceptors() bool { |
| 248 | 278 | if h == nil { |
| 249 | 279 | return false |
| @@ -19,6 +19,7 @@ import ( · 18 unmodified lines | ||
| 19 | 19 | coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth" |
| 20 | 20 | coreexecutor "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/executor" |
| 21 | 21 | coreusage "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/usage" |
| 22 | + "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginabi" | |
| 22 | 23 | "github.com/router-for-me/CLIProxyAPI/v7/sdk/pluginapi" |
| 23 | 24 | sdktranslator "github.com/router-for-me/CLIProxyAPI/v7/sdk/translator" |
| 24 | 25 | ) |
| @@ -1691,6 +1692,84 @@ func TestHasStreamInterceptorsReflectsActiveStreamInterceptors(t *testing.T) { · 1666 unmodified lines | ||
| 1691 | 1692 | } |
| 1692 | 1693 | } |
| 1693 | 1694 | |
| 1695 | +func TestStreamChunkRequestBodyPolicyBySchemaVersion(t *testing.T) { | |
| 1696 | + var legacyGot, modernGot pluginapi.StreamChunkInterceptRequest | |
| 1697 | + host := newHostWithRecords( | |
| 1698 | + capabilityRecord{ | |
| 1699 | + id: "legacy", | |
| 1700 | + plugin: pluginapi.Plugin{ | |
| 1701 | + SchemaVersion: 2, | |
| 1702 | + Capabilities: pluginapi.Capabilities{ | |
| 1703 | + StreamChunkInterceptor: responseInterceptorFunc{ | |
| 1704 | + interceptStreamChunk: func(ctx context.Context, req pluginapi.StreamChunkInterceptRequest) (pluginapi.StreamChunkInterceptResponse, error) { | |
| 1705 | + legacyGot = req | |
| 1706 | + return pluginapi.StreamChunkInterceptResponse{Body: req.Body}, nil | |
| 1707 | + }, | |
| 1708 | + }, | |
| 1709 | + }, | |
| 1710 | + }, | |
| 1711 | + }, | |
| 1712 | + capabilityRecord{ | |
| 1713 | + id: "modern", | |
| 1714 | + plugin: pluginapi.Plugin{ | |
| 1715 | + SchemaVersion: pluginabi.SchemaVersionStreamChunkOmitRequestBody, | |
| 1716 | + Capabilities: pluginapi.Capabilities{ | |
| 1717 | + StreamChunkInterceptor: responseInterceptorFunc{ | |
| 1718 | + interceptStreamChunk: func(ctx context.Context, req pluginapi.StreamChunkInterceptRequest) (pluginapi.StreamChunkInterceptResponse, error) { | |
| 1719 | + modernGot = req | |
| 1720 | + return pluginapi.StreamChunkInterceptResponse{Body: req.Body}, nil | |
| 1721 | + }, | |
| 1722 | + }, | |
| 1723 | + }, | |
| 1724 | + }, | |
| 1725 | + }, | |
| 1726 | + ) | |
| 1727 | + if !host.StreamChunkPayloadIncludesRequestBody() { | |
| 1728 | + t.Fatal("StreamChunkPayloadIncludesRequestBody() = false, want true when legacy stream interceptor is active") | |
| 1729 | + } | |
| 1730 | + | |
| 1731 | + _ = host.InterceptStreamChunk(context.Background(), pluginapi.StreamChunkInterceptRequest{ | |
| 1732 | + OriginalRequest: []byte("original"), | |
| 1733 | + RequestBody: []byte("request"), | |
| 1734 | + Body: []byte("chunk"), | |
| 1735 | + ChunkIndex: 0, | |
| 1736 | + }) | |
| 1737 | + if string(legacyGot.OriginalRequest) != "original" || string(legacyGot.RequestBody) != "request" { | |
| 1738 | + t.Fatalf("legacy payload bodies = original:%q body:%q, want preserved", legacyGot.OriginalRequest, legacyGot.RequestBody) | |
| 1739 | + } | |
| 1740 | + if len(modernGot.OriginalRequest) != 0 || len(modernGot.RequestBody) != 0 { | |
| 1741 | + t.Fatalf("modern payload bodies = original:%q body:%q, want omitted", modernGot.OriginalRequest, modernGot.RequestBody) | |
| 1742 | + } | |
| 1743 | + | |
| 1744 | + legacyGot = pluginapi.StreamChunkInterceptRequest{} | |
| 1745 | + modernGot = pluginapi.StreamChunkInterceptRequest{} | |
| 1746 | + _ = host.InterceptStreamChunk(context.Background(), pluginapi.StreamChunkInterceptRequest{ | |
| 1747 | + OriginalRequest: []byte("original"), | |
| 1748 | + RequestBody: []byte("request"), | |
| 1749 | + ChunkIndex: pluginapi.StreamChunkHeaderInitIndex, | |
| 1750 | + }) | |
| 1751 | + if string(legacyGot.OriginalRequest) != "original" || string(modernGot.OriginalRequest) != "original" { | |
| 1752 | + t.Fatalf("header-init bodies not preserved: legacy=%q modern=%q", legacyGot.OriginalRequest, modernGot.OriginalRequest) | |
| 1753 | + } | |
| 1754 | + | |
| 1755 | + modernOnly := newHostWithRecords(capabilityRecord{ | |
| 1756 | + id: "modern-only", | |
| 1757 | + plugin: pluginapi.Plugin{ | |
| 1758 | + SchemaVersion: pluginabi.SchemaVersionStreamChunkOmitRequestBody, | |
| 1759 | + Capabilities: pluginapi.Capabilities{ | |
| 1760 | + StreamChunkInterceptor: responseInterceptorFunc{ | |
| 1761 | + interceptStreamChunk: func(ctx context.Context, req pluginapi.StreamChunkInterceptRequest) (pluginapi.StreamChunkInterceptResponse, error) { | |
| 1762 | + return pluginapi.StreamChunkInterceptResponse{}, nil | |
| 1763 | + }, | |
| 1764 | + }, | |
| 1765 | + }, | |
| 1766 | + }, | |
| 1767 | + }) | |
| 1768 | + if modernOnly.StreamChunkPayloadIncludesRequestBody() { | |
| 1769 | + t.Fatal("StreamChunkPayloadIncludesRequestBody() = true, want false for schema v3+ only") | |
| 1770 | + } | |
| 1771 | +} | |
| 1772 | + | |
| 1694 | 1773 | func TestHasRequestInterceptorsReflectsActiveRequestInterceptors(t *testing.T) { |
| 1695 | 1774 | responseOnly := newHostWithRecords(capabilityRecord{ |
| 1696 | 1775 | id: "response", |
| @@ -68,8 +68,14 @@ func registerRPCPlugin(ctx context.Context, host *Host, id string, client plugin · 67 unmodified lines | ||
| 68 | 68 | return pluginapi.Plugin{}, fmt.Errorf("plugin schema version %d is not supported", resp.SchemaVersion) |
| 69 | 69 | } |
| 70 | 70 | adapter := &rpcPluginAdapter{id: id, host: host, client: client} |
| 71 | + schemaVersion := resp.SchemaVersion | |
| 72 | + if schemaVersion == 0 { | |
| 73 | + // Missing schema_version is treated as the original contract. | |
| 74 | + schemaVersion = 1 | |
| 75 | + } | |
| 71 | 76 | plugin := pluginapi.Plugin{ |
| 72 | − Metadata: resp.Metadata, | |
| 77 | + Metadata: resp.Metadata, | |
| 78 | + SchemaVersion: schemaVersion, | |
| 73 | 79 | Capabilities: pluginapi.Capabilities{ |
| 74 | 80 | FrontendAuthProviderExclusive: resp.Capabilities.FrontendAuthProvider && resp.Capabilities.FrontendAuthProviderExclusive, |
| 75 | 81 | ExecutorModelScope: resp.Capabilities.ExecutorModelScope, |
| @@ -108,12 +108,16 @@ func TestRegisterRPCPluginSendsHostSchemaVersion(t *testing.T) { · 107 unmodified lines | ||
| 108 | 108 | registerResult: validTestPlugin("schema"), |
| 109 | 109 | }) |
| 110 | 110 | |
| 111 | − if _, errRegister := registerRPCPlugin(context.Background(), nil, "schema", lookup, pluginabi.MethodPluginRegister, []byte("mode: test")); errRegister != nil { | |
| 111 | + registered, errRegister := registerRPCPlugin(context.Background(), nil, "schema", lookup, pluginabi.MethodPluginRegister, []byte("mode: test")) | |
| 112 | + if errRegister != nil { | |
| 112 | 113 | t.Fatalf("registerRPCPlugin() error = %v", errRegister) |
| 113 | 114 | } |
| 114 | 115 | if lookup.lastLifecycle.SchemaVersion != pluginabi.SchemaVersion { |
| 115 | 116 | t.Fatalf("lifecycle schema_version = %d, want %d", lookup.lastLifecycle.SchemaVersion, pluginabi.SchemaVersion) |
| 116 | 117 | } |
| 118 | + if registered.SchemaVersion != pluginabi.SchemaVersion { | |
| 119 | + t.Fatalf("registered SchemaVersion = %d, want %d", registered.SchemaVersion, pluginabi.SchemaVersion) | |
| 120 | + } | |
| 117 | 121 | if string(lookup.lastLifecycle.ConfigYAML) != "mode: test" { |
| 118 | 122 | t.Fatalf("lifecycle config = %q, want input config", lookup.lastLifecycle.ConfigYAML) |
| 119 | 123 | } |
| @@ -146,6 +150,9 @@ func TestRegisterRPCPluginAcceptsModelRouterOnSchema1(t *testing.T) { · 26 unmodified lines | ||
| 146 | 150 | if registered.Capabilities.ModelRouter == nil { |
| 147 | 151 | t.Fatal("ModelRouter = nil, want adapter") |
| 148 | 152 | } |
| 153 | + if registered.SchemaVersion != 1 { | |
| 154 | + t.Fatalf("registered SchemaVersion = %d, want 1", registered.SchemaVersion) | |
| 155 | + } | |
| 149 | 156 | } |
| 150 | 157 | |
| 151 | 158 | func TestRPCModelRouteUsesAdapter(t *testing.T) { |
| @@ -32,6 +32,25 @@ type streamInterceptorDetector interface { · 31 unmodified lines | ||
| 32 | 32 | HasStreamInterceptors() bool |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | +// streamChunkRequestBodyPolicy reports whether payload stream-chunk interceptors | |
| 36 | +// still require OriginalRequest/RequestBody (legacy schema_version < 3). | |
| 37 | +type streamChunkRequestBodyPolicy interface { | |
| 38 | + StreamChunkPayloadIncludesRequestBody() bool | |
| 39 | +} | |
| 40 | + | |
| 41 | +// streamChunkPayloadIncludesRequestBody returns true when at least one active | |
| 42 | +// stream interceptor needs per-chunk request bodies. Evaluated per call so | |
| 43 | +// mid-stream plugin reloads stay correct. Unknown hosts default to true. | |
| 44 | +func streamChunkPayloadIncludesRequestBody(host PluginInterceptorHost) bool { | |
| 45 | + if host == nil { | |
| 46 | + return false | |
| 47 | + } | |
| 48 | + if policy, ok := host.(streamChunkRequestBodyPolicy); ok { | |
| 49 | + return policy.StreamChunkPayloadIncludesRequestBody() | |
| 50 | + } | |
| 51 | + return true | |
| 52 | +} | |
| 53 | + | |
| 35 | 54 | type requestInterceptorDetector interface { |
| 36 | 55 | HasRequestInterceptors() bool |
| 37 | 56 | } |
| @@ -26,6 +26,8 @@ type handlerInterceptorTestHost struct { · 25 unmodified lines | ||
| 26 | 26 | interceptResponse func(context.Context, pluginapi.ResponseInterceptRequest) pluginapi.ResponseInterceptResponse |
| 27 | 27 | interceptStreamChunk func(context.Context, pluginapi.StreamChunkInterceptRequest) pluginapi.StreamChunkInterceptResponse |
| 28 | 28 | completeRequest func(context.Context, pluginapi.RequestCompletion) |
| 29 | + // includeStreamChunkRequestBodies simulates legacy schema_version < 3 plugins. | |
| 30 | + includeStreamChunkRequestBodies bool | |
| 29 | 31 | } |
| 30 | 32 | |
| 31 | 33 | type handlerInterceptorNoStreamTestHost struct { |
| @@ -82,6 +84,15 @@ func (h *handlerInterceptorTestHost) CompleteRequest(ctx context.Context, comple · 50 unmodified lines | ||
| 82 | 84 | } |
| 83 | 85 | } |
| 84 | 86 | |
| 87 | +// StreamChunkPayloadIncludesRequestBody implements streamChunkRequestBodyPolicy. | |
| 88 | +// Default false simulates schema_version >= 3 (omit request bodies on payload chunks). | |
| 89 | +func (h *handlerInterceptorTestHost) StreamChunkPayloadIncludesRequestBody() bool { | |
| 90 | + if h == nil { | |
| 91 | + return false | |
| 92 | + } | |
| 93 | + return h.includeStreamChunkRequestBodies | |
| 94 | +} | |
| 95 | + | |
| 85 | 96 | type interceptorCaptureExecutor struct { |
| 86 | 97 | provider string |
| 87 | 98 | |
| @@ -905,17 +916,23 @@ func TestHandlerStreamInterceptorRewritesAndDropsChunks(t *testing.T) { · 817 unmodified lines | ||
| 905 | 916 | if req.RequestHeaders.Get("X-Stage") != "after" { |
| 906 | 917 | t.Fatalf("stream request headers = %#v, want after-auth header", req.RequestHeaders) |
| 907 | 918 | } |
| 908 | − if string(req.OriginalRequest) != `{"stage":"after-stream"}` { | |
| 909 | − t.Fatalf("stream original request = %q, want after-auth body", req.OriginalRequest) | |
| 910 | − } | |
| 911 | − if string(req.RequestBody) != `{"stage":"after-stream"}` { | |
| 912 | − t.Fatalf("stream request body = %q, want after-auth body", req.RequestBody) | |
| 913 | − } | |
| 914 | 919 | if req.ChunkIndex == pluginapi.StreamChunkHeaderInitIndex { |
| 920 | + if string(req.OriginalRequest) != `{"stage":"after-stream"}` { | |
| 921 | + t.Fatalf("stream original request = %q, want after-auth body", req.OriginalRequest) | |
| 922 | + } | |
| 923 | + if string(req.RequestBody) != `{"stage":"after-stream"}` { | |
| 924 | + t.Fatalf("stream request body = %q, want after-auth body", req.RequestBody) | |
| 925 | + } | |
| 915 | 926 | headers := cloneHeader(req.ResponseHeaders) |
| 916 | 927 | headers.Set("X-Stream", "plugin") |
| 917 | 928 | return pluginapi.StreamChunkInterceptResponse{Headers: headers} |
| 918 | 929 | } |
| 930 | + if len(req.OriginalRequest) != 0 { | |
| 931 | + t.Fatalf("payload chunk OriginalRequest = %q, want omitted for schema v3+", req.OriginalRequest) | |
| 932 | + } | |
| 933 | + if len(req.RequestBody) != 0 { | |
| 934 | + t.Fatalf("payload chunk RequestBody = %q, want omitted for schema v3+", req.RequestBody) | |
| 935 | + } | |
| 919 | 936 | if req.ResponseHeaders.Get("X-Upstream") != "stream" { |
| 920 | 937 | t.Fatalf("stream response headers = %#v, want upstream header", req.ResponseHeaders) |
| 921 | 938 | } |
| @@ -957,6 +974,65 @@ func TestHandlerStreamInterceptorRewritesAndDropsChunks(t *testing.T) { · 35 unmodified lines | ||
| 957 | 974 | } |
| 958 | 975 | } |
| 959 | 976 | |
| 977 | +func TestHandlerStreamInterceptorLegacySchemaClonesRequestBodiesOnPayloadChunks(t *testing.T) { | |
| 978 | + model := "handler-interceptor-stream-legacy-clone-model" | |
| 979 | + executor := &interceptorCaptureExecutor{ | |
| 980 | + stream: func(ctx context.Context, auth *coreauth.Auth, req coreexecutor.Request, opts coreexecutor.Options) (*coreexecutor.StreamResult, error) { | |
| 981 | + chunks := make(chan coreexecutor.StreamChunk, 2) | |
| 982 | + chunks <- coreexecutor.StreamChunk{Payload: []byte("first")} | |
| 983 | + chunks <- coreexecutor.StreamChunk{Payload: []byte("second")} | |
| 984 | + close(chunks) | |
| 985 | + return &coreexecutor.StreamResult{ | |
| 986 | + Headers: http.Header{"X-Upstream": []string{"stream"}}, | |
| 987 | + Chunks: chunks, | |
| 988 | + }, nil | |
| 989 | + }, | |
| 990 | + } | |
| 991 | + handler := newInterceptorHandler(t, model, executor, &sdkconfig.SDKConfig{PassthroughHeaders: true}) | |
| 992 | + var payloadBodies [][]byte | |
| 993 | + handler.SetPluginHost(&handlerInterceptorTestHost{ | |
| 994 | + includeStreamChunkRequestBodies: true, | |
| 995 | + interceptRequestAfterAuth: func(ctx context.Context, req pluginapi.RequestInterceptRequest) pluginapi.RequestInterceptResponse { | |
| 996 | + return pluginapi.RequestInterceptResponse{Body: []byte(`{"stage":"legacy-stream"}`)} | |
| 997 | + }, | |
| 998 | + interceptStreamChunk: func(ctx context.Context, req pluginapi.StreamChunkInterceptRequest) pluginapi.StreamChunkInterceptResponse { | |
| 999 | + if req.ChunkIndex == pluginapi.StreamChunkHeaderInitIndex { | |
| 1000 | + if string(req.OriginalRequest) != `{"stage":"legacy-stream"}` || string(req.RequestBody) != `{"stage":"legacy-stream"}` { | |
| 1001 | + t.Fatalf("header-init bodies = original:%q body:%q", req.OriginalRequest, req.RequestBody) | |
| 1002 | + } | |
| 1003 | + // Mutate delivered slices; later chunks must not observe this mutation. | |
| 1004 | + req.OriginalRequest[0] = 'X' | |
| 1005 | + req.RequestBody[0] = 'Y' | |
| 1006 | + return pluginapi.StreamChunkInterceptResponse{} | |
| 1007 | + } | |
| 1008 | + if string(req.OriginalRequest) != `{"stage":"legacy-stream"}` { | |
| 1009 | + t.Fatalf("payload OriginalRequest = %q, want isolated clone of after-auth body", req.OriginalRequest) | |
| 1010 | + } | |
| 1011 | + if string(req.RequestBody) != `{"stage":"legacy-stream"}` { | |
| 1012 | + t.Fatalf("payload RequestBody = %q, want isolated clone of after-auth body", req.RequestBody) | |
| 1013 | + } | |
| 1014 | + payloadBodies = append(payloadBodies, req.OriginalRequest) | |
| 1015 | + req.OriginalRequest[0] = 'Z' | |
| 1016 | + return pluginapi.StreamChunkInterceptResponse{Body: req.Body} | |
| 1017 | + }, | |
| 1018 | + }) | |
| 1019 | + | |
| 1020 | + dataChan, _, errChan := handler.ExecuteStreamWithAuthManager(context.Background(), "openai", model, []byte(fmt.Sprintf(`{"model":%q}`, model)), "") | |
| 1021 | + for range dataChan { | |
| 1022 | + } | |
| 1023 | + for msg := range errChan { | |
| 1024 | + if msg != nil { | |
| 1025 | + t.Fatalf("unexpected stream error: %+v", msg) | |
| 1026 | + } | |
| 1027 | + } | |
| 1028 | + if len(payloadBodies) != 2 { | |
| 1029 | + t.Fatalf("payload body deliveries = %d, want 2", len(payloadBodies)) | |
| 1030 | + } | |
| 1031 | + if &payloadBodies[0][0] == &payloadBodies[1][0] { | |
| 1032 | + t.Fatal("payload OriginalRequest slices alias across chunks; want fresh clones") | |
| 1033 | + } | |
| 1034 | +} | |
| 1035 | + | |
| 960 | 1036 | func TestHandlerStreamInterceptorInitializesHeadersBeforeReturn(t *testing.T) { |
| 961 | 1037 | model := "handler-interceptor-stream-header-before-return-model" |
| 962 | 1038 | initStarted := make(chan struct{}) |
| @@ -81,19 +81,28 @@ func (h *BaseAPIHandler) streamWithPluginExecutor(ctx context.Context, entryProt · 80 unmodified lines | ||
| 81 | 81 | streamInterceptorsActive := streamInterceptorsEnabled(interceptorHost) |
| 82 | 82 | rawStreamHeaders := cloneHeader(streamResult.Headers) |
| 83 | 83 | baseStreamHeaders := cloneHeader(streamResult.Headers) |
| 84 | + // Request headers and request bodies are stream-invariant. Keep a private snapshot | |
| 85 | + // and clone into each interceptor call so plugins cannot mutate shared storage. | |
| 86 | + // Schema v3+ payload chunks omit these bodies (host also strips per plugin). | |
| 87 | + var streamRequestHeaders http.Header | |
| 88 | + var streamOriginalRequest []byte | |
| 89 | + var streamRequestBody []byte | |
| 84 | 90 | applyStreamHeaders := func(headers http.Header) { |
| 85 | 91 | rawStreamHeaders = finalInterceptorHeaders(rawStreamHeaders, headers) |
| 86 | 92 | } |
| 87 | 93 | if streamInterceptorsActive { |
| 94 | + streamRequestHeaders = cloneHeader(opts.Headers) | |
| 95 | + streamOriginalRequest = cloneBytes(opts.OriginalRequest) | |
| 96 | + streamRequestBody = cloneBytes(req.Payload) | |
| 88 | 97 | intercepted := interceptStreamChunk(ctx, interceptorHost, pluginapi.StreamChunkInterceptRequest{ |
| 89 | 98 | RequestID: lifecycle.requestID(), |
| 90 | 99 | SourceFormat: responseProtocol, |
| 91 | 100 | Model: modelName, |
| 92 | 101 | RequestedModel: originalRequestedModel, |
| 93 | − RequestHeaders: cloneHeader(opts.Headers), | |
| 102 | + RequestHeaders: cloneHeader(streamRequestHeaders), | |
| 94 | 103 | ResponseHeaders: cloneHeader(rawStreamHeaders), |
| 95 | − OriginalRequest: cloneBytes(opts.OriginalRequest), | |
| 96 | − RequestBody: cloneBytes(req.Payload), | |
| 104 | + OriginalRequest: cloneBytes(streamOriginalRequest), | |
| 105 | + RequestBody: cloneBytes(streamRequestBody), | |
| 97 | 106 | ChunkIndex: pluginapi.StreamChunkHeaderInitIndex, |
| 98 | 107 | Metadata: opts.Metadata, |
| 99 | 108 | }, execOptions.SkipInterceptorPluginID) |
| @@ -161,20 +170,25 @@ func (h *BaseAPIHandler) streamWithPluginExecutor(ctx context.Context, entryProt · 61 unmodified lines | ||
| 161 | 170 | } |
| 162 | 171 | payload := cloneBytes(chunk.Payload) |
| 163 | 172 | if streamInterceptorsActive { |
| 164 | − intercepted := interceptStreamChunk(ctx, interceptorHost, pluginapi.StreamChunkInterceptRequest{ | |
| 173 | + chunkReq := pluginapi.StreamChunkInterceptRequest{ | |
| 165 | 174 | RequestID: lifecycle.requestID(), |
| 166 | 175 | SourceFormat: responseProtocol, |
| 167 | 176 | Model: modelName, |
| 168 | 177 | RequestedModel: originalRequestedModel, |
| 169 | − RequestHeaders: cloneHeader(opts.Headers), | |
| 178 | + RequestHeaders: cloneHeader(streamRequestHeaders), | |
| 170 | 179 | ResponseHeaders: cloneHeader(rawStreamHeaders), |
| 171 | − OriginalRequest: cloneBytes(opts.OriginalRequest), | |
| 172 | − RequestBody: cloneBytes(req.Payload), | |
| 173 | 180 | Body: payload, |
| 174 | 181 | HistoryChunks: cloneByteSlices(historyChunks), |
| 175 | 182 | ChunkIndex: chunkIndex, |
| 176 | 183 | Metadata: opts.Metadata, |
| 177 | − }, execOptions.SkipInterceptorPluginID) | |
| 184 | + } | |
| 185 | + // Re-evaluate each chunk so mid-stream plugin reloads stay correct. | |
| 186 | + // Schema v3+ omits bodies here (one header-init clone only). | |
| 187 | + if streamChunkPayloadIncludesRequestBody(interceptorHost) { | |
| 188 | + chunkReq.OriginalRequest = cloneBytes(streamOriginalRequest) | |
| 189 | + chunkReq.RequestBody = cloneBytes(streamRequestBody) | |
| 190 | + } | |
| 191 | + intercepted := interceptStreamChunk(ctx, interceptorHost, chunkReq, execOptions.SkipInterceptorPluginID) | |
| 178 | 192 | applyStreamHeaders(intercepted.Headers) |
| 179 | 193 | if len(intercepted.Body) > 0 { |
| 180 | 194 | payload = cloneBytes(intercepted.Body) |
| @@ -323,6 +337,12 @@ func (h *BaseAPIHandler) executeStreamWithAuthManagerFormats(ctx context.Context · 142 unmodified lines | ||
| 323 | 337 | streamClosedBeforeRead := false |
| 324 | 338 | streamCanceledBeforeRead := false |
| 325 | 339 | streamHeaderInitialized := false |
| 340 | + // Request headers/bodies are stream-invariant after after-auth capture. Keep a private | |
| 341 | + // snapshot and clone into each interceptor call so plugins cannot mutate shared storage. | |
| 342 | + // Schema v3+ payload chunks omit these bodies (host also strips per plugin). | |
| 343 | + var streamRequestHeaders http.Header | |
| 344 | + var streamOriginalRequest []byte | |
| 345 | + var streamRequestBody []byte | |
| 326 | 346 | |
| 327 | 347 | applyStreamHeaders := func(headers http.Header) { |
| 328 | 348 | rawStreamHeaders = finalInterceptorHeaders(rawStreamHeaders, headers) |
| @@ -333,15 +353,18 @@ func (h *BaseAPIHandler) executeStreamWithAuthManagerFormats(ctx context.Context · 4 unmodified lines | ||
| 333 | 353 | return |
| 334 | 354 | } |
| 335 | 355 | executedReq, executedOpts := executedRequest() |
| 356 | + streamRequestHeaders = cloneHeader(executedOpts.Headers) | |
| 357 | + streamOriginalRequest = cloneBytes(executedOpts.OriginalRequest) | |
| 358 | + streamRequestBody = cloneBytes(executedReq.Payload) | |
| 336 | 359 | intercepted := interceptStreamChunk(ctx, interceptorHost, pluginapi.StreamChunkInterceptRequest{ |
| 337 | 360 | RequestID: lifecycle.requestID(), |
| 338 | 361 | SourceFormat: responseProtocol, |
| 339 | 362 | Model: normalizedModel, |
| 340 | 363 | RequestedModel: originalRequestedModel, |
| 341 | − RequestHeaders: cloneHeader(executedOpts.Headers), | |
| 364 | + RequestHeaders: cloneHeader(streamRequestHeaders), | |
| 342 | 365 | ResponseHeaders: cloneHeader(rawStreamHeaders), |
| 343 | − OriginalRequest: cloneBytes(executedOpts.OriginalRequest), | |
| 344 | − RequestBody: cloneBytes(executedReq.Payload), | |
| 366 | + OriginalRequest: cloneBytes(streamOriginalRequest), | |
| 367 | + RequestBody: cloneBytes(streamRequestBody), | |
| 345 | 368 | ChunkIndex: pluginapi.StreamChunkHeaderInitIndex, |
| 346 | 369 | Metadata: executedOpts.Metadata, |
| 347 | 370 | }, execOptions.SkipInterceptorPluginID) |
| @@ -353,21 +376,25 @@ func (h *BaseAPIHandler) executeStreamWithAuthManagerFormats(ctx context.Context · 5 unmodified lines | ||
| 353 | 376 | applyStreamHeaderInit() |
| 354 | 377 | payload = cloneBytes(payload) |
| 355 | 378 | if streamInterceptorsActive { |
| 356 | − executedReq, executedOpts := executedRequest() | |
| 357 | − intercepted := interceptStreamChunk(ctx, interceptorHost, pluginapi.StreamChunkInterceptRequest{ | |
| 379 | + chunkReq := pluginapi.StreamChunkInterceptRequest{ | |
| 358 | 380 | RequestID: lifecycle.requestID(), |
| 359 | 381 | SourceFormat: responseProtocol, |
| 360 | 382 | Model: normalizedModel, |
| 361 | 383 | RequestedModel: originalRequestedModel, |
| 362 | − RequestHeaders: cloneHeader(executedOpts.Headers), | |
| 384 | + RequestHeaders: cloneHeader(streamRequestHeaders), | |
| 363 | 385 | ResponseHeaders: cloneHeader(rawStreamHeaders), |
| 364 | − OriginalRequest: cloneBytes(executedOpts.OriginalRequest), | |
| 365 | − RequestBody: cloneBytes(executedReq.Payload), | |
| 366 | 386 | Body: payload, |
| 367 | 387 | HistoryChunks: cloneByteSlices(historyChunks), |
| 368 | 388 | ChunkIndex: *chunkIndex, |
| 369 | − Metadata: executedOpts.Metadata, | |
| 370 | − }, execOptions.SkipInterceptorPluginID) | |
| 389 | + Metadata: opts.Metadata, | |
| 390 | + } | |
| 391 | + // Re-evaluate each chunk so mid-stream plugin reloads stay correct. | |
| 392 | + // Schema v3+ omits bodies here (one header-init clone only). | |
| 393 | + if streamChunkPayloadIncludesRequestBody(interceptorHost) { | |
| 394 | + chunkReq.OriginalRequest = cloneBytes(streamOriginalRequest) | |
| 395 | + chunkReq.RequestBody = cloneBytes(streamRequestBody) | |
| 396 | + } | |
| 397 | + intercepted := interceptStreamChunk(ctx, interceptorHost, chunkReq, execOptions.SkipInterceptorPluginID) | |
| 371 | 398 | applyStreamHeaders(intercepted.Headers) |
| 372 | 399 | if len(intercepted.Body) > 0 { |
| 373 | 400 | payload = cloneBytes(intercepted.Body) |
| @@ -7,7 +7,13 @@ const ( · 6 unmodified lines | ||
| 7 | 7 | ABIVersion uint32 = 1 |
| 8 | 8 | // SchemaVersion tracks the RPC JSON contract exchanged at plugin.register. |
| 9 | 9 | // Version 2 adds request lifecycle completion and active request termination. |
| 10 | − SchemaVersion uint32 = 2 | |
| 10 | + // Version 3 omits OriginalRequest/RequestBody on payload stream chunks | |
| 11 | + // (ChunkIndex >= 0); those fields remain on StreamChunkHeaderInitIndex only. | |
| 12 | + // Plugins that still need per-chunk request bodies should keep schema_version < 3. | |
| 13 | + SchemaVersion uint32 = 3 | |
| 14 | + // SchemaVersionStreamChunkOmitRequestBody is the first schema version that omits | |
| 15 | + // request bodies on payload stream-chunk interceptor calls. | |
| 16 | + SchemaVersionStreamChunkOmitRequestBody uint32 = 3 | |
| 11 | 17 | ) |
| 12 | 18 | |
| 13 | 19 | const ( |
| @@ -27,8 +27,11 @@ func TestEnvelopeRoundTrip(t *testing.T) { · 26 unmodified lines | ||
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | func TestMethodNamesAreStable(t *testing.T) { |
| 30 | − if SchemaVersion != 2 { | |
| 31 | − t.Fatalf("SchemaVersion = %d, want 2", SchemaVersion) | |
| 30 | + if SchemaVersion != 3 { | |
| 31 | + t.Fatalf("SchemaVersion = %d, want 3", SchemaVersion) | |
| 32 | + } | |
| 33 | + if SchemaVersionStreamChunkOmitRequestBody != 3 { | |
| 34 | + t.Fatalf("SchemaVersionStreamChunkOmitRequestBody = %d, want 3", SchemaVersionStreamChunkOmitRequestBody) | |
| 32 | 35 | } |
| 33 | 36 | if MethodPluginRegister != "plugin.register" { |
| 34 | 37 | t.Fatalf("MethodPluginRegister = %q", MethodPluginRegister) |
| @@ -15,6 +15,9 @@ type Plugin struct { · 14 unmodified lines | ||
| 15 | 15 | Metadata Metadata |
| 16 | 16 | // Capabilities declares the optional integration points implemented by the plugin. |
| 17 | 17 | Capabilities Capabilities |
| 18 | + // SchemaVersion is the plugin contract version negotiated at registration. | |
| 19 | + // Zero means unset (treated as legacy by the host). | |
| 20 | + SchemaVersion uint32 | |
| 18 | 21 | } |
| 19 | 22 | |
| 20 | 23 | // Metadata describes a plugin for registry, logging, and diagnostics. |
| @@ -1087,9 +1090,17 @@ type StreamChunkInterceptRequest struct { · 1066 unmodified lines | ||
| 1087 | 1090 | RequestedModel string |
| 1088 | 1091 | RequestHeaders http.Header |
| 1089 | 1092 | ResponseHeaders http.Header |
| 1093 | + // OriginalRequest contains the raw client request body. | |
| 1094 | + // Always populated on header-init (ChunkIndex == StreamChunkHeaderInitIndex), as a fresh clone. | |
| 1095 | + // On payload chunks (ChunkIndex >= 0): | |
| 1096 | + // - schema_version >= 3: omitted (nil); cache from header-init or request intercept hooks | |
| 1097 | + // - schema_version < 3: populated as a fresh clone each call (legacy compatibility) | |
| 1098 | + // Callers must treat this slice as read-only; hosts clone before delivery to keep snapshots isolated. | |
| 1090 | 1099 | OriginalRequest []byte |
| 1091 | − RequestBody []byte | |
| 1092 | − Body []byte | |
| 1100 | + // RequestBody contains the provider/executed request payload. | |
| 1101 | + // Same population / cloning / schema-version rules as OriginalRequest. | |
| 1102 | + RequestBody []byte | |
| 1103 | + Body []byte | |
| 1093 | 1104 | // HistoryChunks contains a bounded recent history of chunks already delivered downstream. |
| 1094 | 1105 | // The host currently retains at most 64 chunks and 1 MiB total history bytes. |
| 1095 | 1106 | HistoryChunks [][]byte |