Files
CLIProxyAPI/internal/runtime/executor/helps/websocket_observer_helpers.go
Luis Pater 6e6afab99a refactor: migrate gjson, sjson, and base64 to MoeForge/moego
- Replace tidwall/gjson and tidwall/sjson with github.com/MoeForge/moego/ojson
  and encoding/base64 with github.com/MoeForge/moego/base64 across the main
  module and examples
- Introduce cross-method *ojson.Doc request rewriting pipelines across
  thinking, signature, client/codex, runtime/executor/helps, pluginhost, and
  all provider executors with FinalizePayloadDoc as the final semantic barrier
- Convert all consecutive JSON mutations (>= 2 Set/SetRaw/Delete operations)
  to batch edits on ojson.Doc
- Upgrade read-only byte paths to ojson.GetBytesNoCopy,
  ojson.GetManyBytesNoCopy, and util.ParseGJSONBytesNoCopy with explicit
  strings.Clone ownership protection across streaming and persistent state
- Use base64.StdEncoding.ValidateString for zero-allocation signature checks
  and base64.EncodeDataURL for single-allocation data URL construction
2026-11-03 03:05:04 +08:00

50 lines
1.5 KiB
Go

package helps
import (
"context"
"strings"
"github.com/MoeForge/moego/ojson"
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
)
// EmitWebSocketResponseEvent delivers an upstream WebSocket response frame to any configured observer.
func EmitWebSocketResponseEvent(ctx context.Context, opts cliproxyexecutor.Options, auth *cliproxyauth.Auth, provider, model string, payload []byte) {
if opts.WebSocketResponseObserver == nil || len(payload) == 0 {
return
}
var authID, authLabel, authType string
if auth != nil {
authID = auth.ID
authLabel = auth.Label
authType, _ = auth.AccountInfo()
}
eventType := strings.Clone(ojson.GetBytesNoCopy(payload, "type").String())
var requestID string
var traceID string
if opts.Metadata != nil {
if reqID, ok := opts.Metadata["request_id"].(string); ok {
requestID = reqID
}
if trID, ok := opts.Metadata["trace_id"].(string); ok {
traceID = trID
}
}
requestedModel := PayloadRequestedModel(opts, model)
opts.WebSocketResponseObserver(ctx, cliproxyexecutor.WebSocketResponseEvent{
RequestID: requestID,
TraceID: traceID,
SourceFormat: opts.SourceFormat.String(),
Model: model,
RequestedModel: requestedModel,
Provider: provider,
AuthID: authID,
AuthLabel: authLabel,
AuthType: authType,
EventType: eventType,
Payload: payload,
Metadata: opts.Metadata,
})
}