Files
magpie/internal/provider/socket.go
T
yetone 1e2287758c copilot: a Responses turn goes as its delta over Copilot's WebSocket
yuushabu_buyinn on X asked whether Copilot's Responses route can send
the delta too: their Usage page has Copilot at 70.4K requests, up 129 GB
for 31 GB down, since each turn went whole over HTTP. Copilot's HTTP
/responses answers previous_response_id with 400, but its Responses
WebSocket at <api>/responses takes previous_response_id (store false)
on the same connection, as Copilot Chat sends it for a model whose
supported_endpoints list "ws:/responses".

- provider: the model list remembers which models list ws:/responses
  (copilotSee); SocketTurn gives a Copilot turn its socket at the API's
  /responses with Copilot's own headers (CopilotSocketTurn).
- gateway: overSocket takes such a Copilot turn; the message is
  {type:"response.create", initiator, ...body} without stream; the
  handshake has no OpenAI-Beta or X-Initiator; the keep key adds the
  Copilot session token. A status-less error frame and a refused
  handshake have the turn over HTTP, as before.
- keep: Copilot's reasoning ids are Base64 (+ and /), and plainItemIDs
  leaves such reasoning out of the next turn, so the real account's
  second turn never matched what it kept and went whole. A Copilot turn
  without its kept reasoning still carries on: Copilot keeps that
  reasoning with the response.

Tests (fake Copilot, no network): TestCopilotSocketSendsTheDelta,
OnlyForItsModels, RefusedGoesOverHTTP, ErrorFrameGoesOverHTTP,
DeltaWithoutItsReasoning. Each of the first four is red on origin/main;
DeltaWithoutItsReasoning is red with the sealed skip turned off (whole
input, no previous_response_id).

Real account (owner's Copilot, sandbox HOME, wsl-run magpie-desktop):
gpt-5.3-codex over the socket, turn 2 on the kept socket with
previous_response_id and only its new input, answer right ("apple
banana"). gpt-5.5 and gpt-5.4 answer 400 "not available for integrator
vscode-chat" on that account, so weren't tried.

Run at this tree (wsl-run, magpie-desktop): gofmt -l internal/ clean; go vet
of gateway and provider for GOOS linux, windows and darwin; go test
-run "Socket|Copilot" ./internal/gateway ./internal/provider ok. The full
`go test -count=1 -timeout 45m ./...` is queued on mote behind another
session's suite and was not finished at push.
2026-11-18 23:50:16 +08:00

169 lines
5.7 KiB
Go

package provider
import (
"context"
"net/http"
"net/url"
"strings"
)
// The ways a provider's Socket is set: SocketAuto tries the Responses
// WebSocket and leaves a host that refuses its handshake to HTTP for a
// while; SocketOn tries it every turn; SocketOff is HTTP alone, where
// the default would be the socket. "" is the default (SocketMode).
const (
SocketAuto = "auto"
SocketOn = "on"
SocketOff = "off"
)
// ParseSocket is the Socket v sets: auto, on, off (off, no or false), or
// "" for the default (default or empty); ok is false for anything else.
func ParseSocket(v string) (string, bool) {
switch strings.ToLower(strings.TrimSpace(v)) {
case SocketAuto:
return SocketAuto, true
case SocketOn, "yes", "true":
return SocketOn, true
case SocketOff, "no", "false":
return SocketOff, true
case "", "default":
return "", true
}
return "", false
}
// SocketMode is how p's Responses turns go: SocketAuto, SocketOn, or ""
// for HTTP alone. A Socket the user set is kept; unset, OpenAI's own API
// (an https Responses URL at api.openai.com) is auto, as Codex's built-in
// OpenAI provider speaks its WebSocket mode to it (supports_websockets in
// codex-rs's create_openai_provider). There a conversation's next turn
// goes as its new input after previous_response_id, even with store
// false, where HTTP sends the whole conversation every turn (KawaiiZapic
// on X). Any other address is HTTP alone unless set.
func (p Provider) SocketMode() string {
switch p.Socket {
case SocketOff:
return ""
case SocketAuto, SocketOn:
return p.Socket
}
if p.SocketByDefault() {
return SocketAuto
}
return ""
}
// SocketByDefault says p's turns go over the Responses WebSocket with
// Socket unset (SocketMode).
func (p Provider) SocketByDefault() bool {
if !p.TakesSocket() || p.PluginProvider() != "" {
return false
}
u, err := url.Parse(strings.TrimSpace(p.Responses))
return err == nil && u.Scheme == "https" && strings.EqualFold(u.Hostname(), "api.openai.com")
}
// TakesSocket says p can have its turns go over the Responses WebSocket
// (Socket): a provider of a key with a Responses URL. A signed-in account
// or a plugin's provider has its own way there.
func (p Provider) TakesSocket() bool {
return p.Account == nil && p.Responses != "" && !p.IsVertex()
}
type socketKey struct{}
// socketAt is where a provider's Responses turns are, and whether a host
// that refuses the socket is left to HTTP for a while; zero for a provider
// whose turns go over HTTP alone.
type socketAt struct {
host, path string
shun bool
// copilot: a Copilot account's turns, at whatever API host its
// session names (CopilotSocketTurn)
copilot bool
}
// withSocket is ctx carrying where p's turns may go over the Responses
// WebSocket; one that has none hides any it carried before, so a
// provider never takes another's.
func withSocket(ctx context.Context, p Provider) context.Context {
at, ok := p.socketAt()
if !ok {
if _, had := ctx.Value(socketKey{}).(socketAt); had {
return context.WithValue(ctx, socketKey{}, socketAt{})
}
return ctx
}
return context.WithValue(ctx, socketKey{}, at)
}
func (p Provider) socketAt() (socketAt, bool) {
if p.copilotSocket() {
return socketAt{copilot: true, shun: true}, true
}
mode := p.SocketMode()
if mode == "" || !p.TakesSocket() {
return socketAt{}, false
}
u, err := url.Parse(strings.TrimRight(strings.TrimSpace(p.Responses), "/"))
if err != nil || u.Host == "" || (u.Scheme != "https" && u.Scheme != "http") {
return socketAt{}, false
}
return socketAt{host: u.Host, path: u.Path + "/responses", shun: mode != SocketOn}, true
}
// SocketTurn says req is a Responses turn that may go over the Responses
// WebSocket: one to chatgpt.com's Codex backend, or to the Responses URL
// of a provider whose Socket is set (its ctx made by Via). shun says a
// refused handshake leaves the host to HTTP for a while.
func SocketTurn(req *http.Request) (ok, shun bool) {
if req == nil || req.URL == nil {
return false, false
}
if codexTurnURL(req.URL) {
return true, true
}
at, _ := req.Context().Value(socketKey{}).(socketAt)
if at.copilot {
return req.URL.Path == "/responses", true
}
if at.host == "" || !strings.EqualFold(req.URL.Host, at.host) || req.URL.Path != at.path {
return false, false
}
return true, at.shun
}
// A Copilot account's Responses turn goes over Copilot's Responses
// WebSocket, at the same /responses address, for a model whose
// supported_endpoints list "ws:/responses", as Copilot Chat sends it
// (microsoft/vscode extensions/copilot chatEndpoint.ts: useWebSocket,
// ModelSupportedEndpoint.WebSocketResponses). There a conversation's next
// turn goes on the same socket as its new input after
// previous_response_id with store false, where over HTTP Copilot answers
// previous_response_id 400 "previous_response_id is not supported" and
// every turn goes whole (yuushabu_buyinn on X: 129 GB up for 31 GB down).
// Socket "off" keeps an account's turns on HTTP.
func (p Provider) copilotSocket() bool {
return p.Account != nil && p.Account.Agent == "copilot" && p.Socket != SocketOff
}
// CopilotSocketTurn says req is a Copilot account's Responses turn that
// may go over Copilot's WebSocket (SocketTurn), its model aside
// (CopilotSocketModel).
func CopilotSocketTurn(req *http.Request) bool {
if req == nil || req.URL == nil {
return false
}
at, _ := req.Context().Value(socketKey{}).(socketAt)
return at.copilot && req.URL.Path == "/responses"
}
// CopilotSocketModel says Copilot's list named model served over its
// Responses WebSocket ("ws:/responses" in its supported_endpoints).
func CopilotSocketModel(model string) bool {
copilotSeenMu.Lock()
defer copilotSeenMu.Unlock()
return copilotSeenWS[model]
}