|
| 1 | +package executor |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" |
| 12 | + "github.com/router-for-me/CLIProxyAPI/v6/internal/util" |
| 13 | + "github.com/router-for-me/CLIProxyAPI/v6/internal/wsrelay" |
| 14 | + cliproxyauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth" |
| 15 | + cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor" |
| 16 | + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" |
| 17 | + "github.com/tidwall/sjson" |
| 18 | +) |
| 19 | + |
| 20 | +// AistudioExecutor routes AI Studio requests through a websocket-backed transport. |
| 21 | +type AistudioExecutor struct { |
| 22 | + provider string |
| 23 | + relay *wsrelay.Manager |
| 24 | + cfg *config.Config |
| 25 | +} |
| 26 | + |
| 27 | +// NewAistudioExecutor constructs a websocket executor for the provider name. |
| 28 | +func NewAistudioExecutor(cfg *config.Config, provider string, relay *wsrelay.Manager) *AistudioExecutor { |
| 29 | + return &AistudioExecutor{provider: strings.ToLower(provider), relay: relay, cfg: cfg} |
| 30 | +} |
| 31 | + |
| 32 | +// Identifier returns the provider key served by this executor. |
| 33 | +func (e *AistudioExecutor) Identifier() string { return e.provider } |
| 34 | + |
| 35 | +// PrepareRequest is a no-op because websocket transport already injects headers. |
| 36 | +func (e *AistudioExecutor) PrepareRequest(_ *http.Request, _ *cliproxyauth.Auth) error { |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +func (e *AistudioExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { |
| 41 | + translatedReq, body, err := e.translateRequest(req, opts, false) |
| 42 | + if err != nil { |
| 43 | + return cliproxyexecutor.Response{}, err |
| 44 | + } |
| 45 | + endpoint := e.buildEndpoint(req.Model, body.action, opts.Alt) |
| 46 | + wsReq := &wsrelay.HTTPRequest{ |
| 47 | + Method: http.MethodPost, |
| 48 | + URL: endpoint, |
| 49 | + Headers: http.Header{"Content-Type": []string{"application/json"}}, |
| 50 | + Body: body.payload, |
| 51 | + } |
| 52 | + |
| 53 | + var authID, authLabel, authType, authValue string |
| 54 | + if auth != nil { |
| 55 | + authID = auth.ID |
| 56 | + authLabel = auth.Label |
| 57 | + authType, authValue = auth.AccountInfo() |
| 58 | + } |
| 59 | + recordAPIRequest(ctx, e.cfg, upstreamRequestLog{ |
| 60 | + URL: endpoint, |
| 61 | + Method: http.MethodPost, |
| 62 | + Headers: wsReq.Headers.Clone(), |
| 63 | + Body: bytes.Clone(body.payload), |
| 64 | + Provider: e.provider, |
| 65 | + AuthID: authID, |
| 66 | + AuthLabel: authLabel, |
| 67 | + AuthType: authType, |
| 68 | + AuthValue: authValue, |
| 69 | + }) |
| 70 | + |
| 71 | + resp, err := e.relay.RoundTrip(ctx, e.provider, wsReq) |
| 72 | + if err != nil { |
| 73 | + recordAPIResponseError(ctx, e.cfg, err) |
| 74 | + return cliproxyexecutor.Response{}, err |
| 75 | + } |
| 76 | + recordAPIResponseMetadata(ctx, e.cfg, resp.Status, resp.Headers.Clone()) |
| 77 | + if len(resp.Body) > 0 { |
| 78 | + appendAPIResponseChunk(ctx, e.cfg, bytes.Clone(resp.Body)) |
| 79 | + } |
| 80 | + if resp.Status < 200 || resp.Status >= 300 { |
| 81 | + return cliproxyexecutor.Response{}, statusErr{code: resp.Status, msg: string(resp.Body)} |
| 82 | + } |
| 83 | + var param any |
| 84 | + out := sdktranslator.TranslateNonStream(ctx, body.toFormat, opts.SourceFormat, req.Model, bytes.Clone(opts.OriginalRequest), translatedReq, bytes.Clone(resp.Body), ¶m) |
| 85 | + return cliproxyexecutor.Response{Payload: []byte(out)}, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (e *AistudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (<-chan cliproxyexecutor.StreamChunk, error) { |
| 89 | + translatedReq, body, err := e.translateRequest(req, opts, true) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + endpoint := e.buildEndpoint(req.Model, body.action, opts.Alt) |
| 94 | + wsReq := &wsrelay.HTTPRequest{ |
| 95 | + Method: http.MethodPost, |
| 96 | + URL: endpoint, |
| 97 | + Headers: http.Header{"Content-Type": []string{"application/json"}}, |
| 98 | + Body: body.payload, |
| 99 | + } |
| 100 | + var authID, authLabel, authType, authValue string |
| 101 | + if auth != nil { |
| 102 | + authID = auth.ID |
| 103 | + authLabel = auth.Label |
| 104 | + authType, authValue = auth.AccountInfo() |
| 105 | + } |
| 106 | + recordAPIRequest(ctx, e.cfg, upstreamRequestLog{ |
| 107 | + URL: endpoint, |
| 108 | + Method: http.MethodPost, |
| 109 | + Headers: wsReq.Headers.Clone(), |
| 110 | + Body: bytes.Clone(body.payload), |
| 111 | + Provider: e.provider, |
| 112 | + AuthID: authID, |
| 113 | + AuthLabel: authLabel, |
| 114 | + AuthType: authType, |
| 115 | + AuthValue: authValue, |
| 116 | + }) |
| 117 | + stream, err := e.relay.Stream(ctx, e.provider, wsReq) |
| 118 | + if err != nil { |
| 119 | + recordAPIResponseError(ctx, e.cfg, err) |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + out := make(chan cliproxyexecutor.StreamChunk) |
| 123 | + go func() { |
| 124 | + defer close(out) |
| 125 | + var param any |
| 126 | + metadataLogged := false |
| 127 | + for event := range stream { |
| 128 | + if event.Err != nil { |
| 129 | + recordAPIResponseError(ctx, e.cfg, event.Err) |
| 130 | + out <- cliproxyexecutor.StreamChunk{Err: event.Err} |
| 131 | + return |
| 132 | + } |
| 133 | + switch event.Type { |
| 134 | + case wsrelay.MessageTypeStreamStart: |
| 135 | + if !metadataLogged && event.Status > 0 { |
| 136 | + recordAPIResponseMetadata(ctx, e.cfg, event.Status, event.Headers.Clone()) |
| 137 | + metadataLogged = true |
| 138 | + } |
| 139 | + case wsrelay.MessageTypeStreamChunk: |
| 140 | + if len(event.Payload) > 0 { |
| 141 | + appendAPIResponseChunk(ctx, e.cfg, bytes.Clone(event.Payload)) |
| 142 | + } |
| 143 | + lines := sdktranslator.TranslateStream(ctx, body.toFormat, opts.SourceFormat, req.Model, bytes.Clone(opts.OriginalRequest), translatedReq, bytes.Clone(event.Payload), ¶m) |
| 144 | + for i := range lines { |
| 145 | + out <- cliproxyexecutor.StreamChunk{Payload: []byte(lines[i])} |
| 146 | + } |
| 147 | + case wsrelay.MessageTypeStreamEnd: |
| 148 | + return |
| 149 | + case wsrelay.MessageTypeHTTPResp: |
| 150 | + if !metadataLogged && event.Status > 0 { |
| 151 | + recordAPIResponseMetadata(ctx, e.cfg, event.Status, event.Headers.Clone()) |
| 152 | + metadataLogged = true |
| 153 | + } |
| 154 | + if len(event.Payload) > 0 { |
| 155 | + appendAPIResponseChunk(ctx, e.cfg, bytes.Clone(event.Payload)) |
| 156 | + } |
| 157 | + lines := sdktranslator.TranslateStream(ctx, body.toFormat, opts.SourceFormat, req.Model, bytes.Clone(opts.OriginalRequest), translatedReq, bytes.Clone(event.Payload), ¶m) |
| 158 | + for i := range lines { |
| 159 | + out <- cliproxyexecutor.StreamChunk{Payload: []byte(lines[i])} |
| 160 | + } |
| 161 | + return |
| 162 | + case wsrelay.MessageTypeError: |
| 163 | + recordAPIResponseError(ctx, e.cfg, event.Err) |
| 164 | + out <- cliproxyexecutor.StreamChunk{Err: fmt.Errorf("wsrelay: %v", event.Err)} |
| 165 | + return |
| 166 | + } |
| 167 | + } |
| 168 | + }() |
| 169 | + return out, nil |
| 170 | +} |
| 171 | + |
| 172 | +func (e *AistudioExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Auth, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Response, error) { |
| 173 | + translatedReq, body, err := e.translateRequest(req, opts, false) |
| 174 | + if err != nil { |
| 175 | + return cliproxyexecutor.Response{}, err |
| 176 | + } |
| 177 | + endpoint := e.buildEndpoint(req.Model, "countTokens", "") |
| 178 | + wsReq := &wsrelay.HTTPRequest{ |
| 179 | + Method: http.MethodPost, |
| 180 | + URL: endpoint, |
| 181 | + Headers: http.Header{"Content-Type": []string{"application/json"}}, |
| 182 | + Body: body.payload, |
| 183 | + } |
| 184 | + var authID, authLabel, authType, authValue string |
| 185 | + if auth != nil { |
| 186 | + authID = auth.ID |
| 187 | + authLabel = auth.Label |
| 188 | + authType, authValue = auth.AccountInfo() |
| 189 | + } |
| 190 | + recordAPIRequest(ctx, e.cfg, upstreamRequestLog{ |
| 191 | + URL: endpoint, |
| 192 | + Method: http.MethodPost, |
| 193 | + Headers: wsReq.Headers.Clone(), |
| 194 | + Body: bytes.Clone(body.payload), |
| 195 | + Provider: e.provider, |
| 196 | + AuthID: authID, |
| 197 | + AuthLabel: authLabel, |
| 198 | + AuthType: authType, |
| 199 | + AuthValue: authValue, |
| 200 | + }) |
| 201 | + resp, err := e.relay.RoundTrip(ctx, e.provider, wsReq) |
| 202 | + if err != nil { |
| 203 | + recordAPIResponseError(ctx, e.cfg, err) |
| 204 | + return cliproxyexecutor.Response{}, err |
| 205 | + } |
| 206 | + recordAPIResponseMetadata(ctx, e.cfg, resp.Status, resp.Headers.Clone()) |
| 207 | + if len(resp.Body) > 0 { |
| 208 | + appendAPIResponseChunk(ctx, e.cfg, bytes.Clone(resp.Body)) |
| 209 | + } |
| 210 | + if resp.Status < 200 || resp.Status >= 300 { |
| 211 | + return cliproxyexecutor.Response{}, statusErr{code: resp.Status, msg: string(resp.Body)} |
| 212 | + } |
| 213 | + var param any |
| 214 | + out := sdktranslator.TranslateNonStream(ctx, body.toFormat, opts.SourceFormat, req.Model, bytes.Clone(opts.OriginalRequest), translatedReq, bytes.Clone(resp.Body), ¶m) |
| 215 | + return cliproxyexecutor.Response{Payload: []byte(out)}, nil |
| 216 | +} |
| 217 | + |
| 218 | +func (e *AistudioExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) (*cliproxyauth.Auth, error) { |
| 219 | + _ = ctx |
| 220 | + return auth, nil |
| 221 | +} |
| 222 | + |
| 223 | +type translatedPayload struct { |
| 224 | + payload []byte |
| 225 | + action string |
| 226 | + toFormat sdktranslator.Format |
| 227 | +} |
| 228 | + |
| 229 | +func (e *AistudioExecutor) translateRequest(req cliproxyexecutor.Request, opts cliproxyexecutor.Options, stream bool) ([]byte, translatedPayload, error) { |
| 230 | + from := opts.SourceFormat |
| 231 | + to := sdktranslator.FromString("gemini") |
| 232 | + payload := sdktranslator.TranslateRequest(from, to, req.Model, bytes.Clone(req.Payload), stream) |
| 233 | + if budgetOverride, includeOverride, ok := util.GeminiThinkingFromMetadata(req.Metadata); ok { |
| 234 | + payload = util.ApplyGeminiThinkingConfig(payload, budgetOverride, includeOverride) |
| 235 | + } |
| 236 | + payload = disableGeminiThinkingConfig(payload, req.Model) |
| 237 | + payload = fixGeminiImageAspectRatio(req.Model, payload) |
| 238 | + metadataAction := "generateContent" |
| 239 | + if req.Metadata != nil { |
| 240 | + if action, _ := req.Metadata["action"].(string); action == "countTokens" { |
| 241 | + metadataAction = action |
| 242 | + } |
| 243 | + } |
| 244 | + action := metadataAction |
| 245 | + if stream && action != "countTokens" { |
| 246 | + action = "streamGenerateContent" |
| 247 | + } |
| 248 | + payload, _ = sjson.DeleteBytes(payload, "session_id") |
| 249 | + return payload, translatedPayload{payload: payload, action: action, toFormat: to}, nil |
| 250 | +} |
| 251 | + |
| 252 | +func (e *AistudioExecutor) buildEndpoint(model, action, alt string) string { |
| 253 | + base := fmt.Sprintf("%s/%s/models/%s:%s", glEndpoint, glAPIVersion, model, action) |
| 254 | + if action == "streamGenerateContent" { |
| 255 | + if alt == "" { |
| 256 | + return base + "?alt=sse" |
| 257 | + } |
| 258 | + return base + "?$alt=" + url.QueryEscape(alt) |
| 259 | + } |
| 260 | + if alt != "" && action != "countTokens" { |
| 261 | + return base + "?$alt=" + url.QueryEscape(alt) |
| 262 | + } |
| 263 | + return base |
| 264 | +} |
0 commit comments