Files
magpie/internal/gateway/relay_status.go
T
yetone 761556a0cc provider: Qoder's built-in is retired; its plugin serves it
qoder and qoder-cn join retiredBuiltins, as Zed did in c0605abc2.
@magpie-community/opencode-qoder-auth serves both, moved or not.

Removed (built-in only):
- gateway: the Qoder dispatch in gateway.go and gateway/qoder.go (the
  translator), with its tests (qoder_test, qoder_busy_test, qoder_cn_test).
- provider: qoder_usage.go; the Accounts() loop, builtinLogins case and
  loginQuota branch, the Logins/side-list/switch/forget cases, SetLoginOn
  and alsoOn, the sign-in case, the subscription_usage fetch, and qoder
  in asksOwnBackend; qoderLogins(); the built-in tests (qoder_test,
  qoder_cn_test, qoder_free_test).
- internal/qoder: codec, cosy, models, oauth, usage, userinfo, ids, store
  and their tests.

Kept, and why:
- migrate_qoder.go (the mover) and the legacy qoder.json migration it
  reaches through qoderLoginsOf. It now reads a pair with qoderSaved.
- qoder_checkin.go: the check-in walks plugin accounts (heldPlugins).
- internal/qoder/endpoints.go: the provider keys, Site/SiteOf, the
  Credential the mover reads and NewMachineID, with the notice.
- The PLUGIN-SERVED notice in provider/qoder.go and internal/qoder.
- gateway's relayStatus/retryAfterOn, which commandcode.go uses, moved to
  relay_status.go; the tests' qoderRoundTrip is now roundTripFunc
  (roundtrip_test.go), no change in meaning.

Tests changed in meaning:
- TestOwnStoreSignInAgainDropsTheReading lost its qoder case.
- New TestQoderLegacyFileMoves: a legacy qoder.json moves through the
  mover and is removed. Red when migrateQoder is skipped (tried by hand).

Run (machine load ~300, so narrow by request):
- gofmt -l internal: empty
- go vet -tags nogui ./...; GOOS=linux and GOOS=windows the same: clean
- go test -tags nogui -count=1 -timeout 45m -run 'Qoder|qoder|Migrat|migrat|Mover|Moved|Retired|SignIn|AsksOwnBackend|Plugin|Afresh|Checkin' ./internal/provider ./internal/gateway ./internal/cli ./internal/gui ./internal/qoder
  provider ok 114s, gateway ok 201s, cli ok 18s, gui ok 12s, qoder no tests
Not run: the whole packages, the full suite, the GUI node suite.
2026-10-17 23:11:00 +08:00

91 lines
2.3 KiB
Go

package gateway
import (
"context"
"math"
"net/http"
"strconv"
"time"
"github.com/yetone/magpie/internal/provider"
)
// relayStatus relays a backend's events and keeps its upstream failures
// before a response is committed, including failures after partial
// non-streaming text; once streaming output has begun a failure is a
// terminal error event. It is for the backends that report their own
// statuses; name is the backend's, for an answer that never came.
func relayStatus(w http.ResponseWriter, from provider.Protocol, name string, req *Request, events <-chan Event, usage *Usage, abort context.CancelFunc) (int, string) {
fail := func(ev Event) (int, string) {
abort()
code := ev.Status
if code < 400 || code > 599 {
code = 502
}
retryAfterOn(w, ev.Wait)
return writeError(w, from, code, ev.Text), ev.Text
}
if req.Stream {
var head []Event
for ev := range events {
if ev.Kind == KError {
return fail(ev)
}
head = append(head, ev)
if ev.Kind != KStart && ev.Kind != KUsage {
break
}
}
if len(head) == 0 || head[len(head)-1].Kind == KStart || head[len(head)-1].Kind == KUsage {
return fail(Event{Text: name + " ended without an answer"})
}
sw := newSSEWriter(w)
enc := encoder(from, sw, req, usage)
var failed string
see := func(ev Event) bool {
if ev.Kind == KStart || ev.Kind == KUsage {
usage.add(ev.Usage)
}
enc.event(ev)
if ev.Kind == KError {
failed = ev.Text
return false
}
return true
}
for _, ev := range head {
see(ev)
}
// kept from the client's idle timeout while none comes (#436)
relayEvents(events, sw, enc, see)
if failed != "" {
abort()
return 200, failed
}
enc.finish()
return 200, ""
}
var col collector
for ev := range events {
if ev.Kind == KError {
return fail(ev)
}
col.add(ev)
}
res := col.finish()
usage.add(res.Usage)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(renderUsage(from, res, req, usage))
return 200, ""
}
// retryAfterOn sends, with an error about to be written to w, how long
// the vendor said to wait before asking again, when it said: the agent
// reads it, and so does the gateway's busy handling (noteBusy).
func retryAfterOn(w http.ResponseWriter, wait time.Duration) {
if wait > 0 {
w.Header().Set("Retry-After", strconv.Itoa(int(math.Ceil(wait.Seconds()))))
}
}