Files
magpie/internal/gateway/session_parent.go
T
maYandyetone 6fe8169502 gateway, agent: Codex draws with its native image tool through magpie (#870)
* fix(agent): give magpie's provider table the actor-authorization header

Codex offers its built-in image tool (image_gen.imagegen) — and the other
OpenAI-provider tools — only to a provider it reads as the OpenAI actor:
ModelProviderInfo::uses_openai_actor_authorization(), i.e. a provider that
does not require OpenAI auth but carries x-openai-actor-authorization.
magpie writes [model_providers.magpie] but carried neither signal, so a
Codex on magpie was never given the tool and could not draw through a
Codex backend such as sub2api's.

Write http_headers = { "x-openai-actor-authorization" = "magpie" } into
that table. magpie does not forward the header upstream: codexHeader
rejects anything naming "authorization".

Test: TestCodexMagpieProviderCarriesActorAuthorization.

* gateway/provider: let a Responses-only relay (Codex backend, sub2api) draw

A provider added as 'OpenAI Responses' only (what a Codex backend such as
sub2api speaks) has no chat base, so Drawers() returned nil before ever
looking at the vendor's list, draw() refused with 'can't draw', and
drawImages() posted to an empty base. The vendor's own /v1/models does list
its image models (sub2api serves gpt-image-2.5), and they are meant to be
picked in Settings → Images.

- Drawers: a Responses-only provider draws when it has any OpenAI-shaped
  base, not only a chat one.
- draw: refuse only when neither Chat nor Responses is set.
- drawImages: fall back to the Responses base when there is no chat base.
- List: the add form's 'Fetch models' keeps the vendor's image models so
  one can be picked while the provider is added.

Tests: TestResponsesOnlyProviderDraws, TestListOffersImageModels.

* fix: route Codex native images through its turn provider

---------

Co-authored-by: yetone <yetoneful@gmail.com>
2026-10-05 17:19:42 +08:00

95 lines
2.9 KiB
Go

package gateway
import (
"bytes"
"encoding/json"
"net/http"
"strings"
)
func isTitleKind(kind string) bool {
return kind == "thread_title" || kind == "thread_title_reconsideration" || kind == "title_generation"
}
// Codex projects turn metadata into headers, but its canonical transport is
// client_metadata in the Responses body. Read only identity fields; never keep
// prompts or the rest of the metadata in the trace.
type sessionMetadata struct {
Turn string `json:"turn_id"`
Source string `json:"thread_source"`
Parent string `json:"parent_thread_id"`
Forked string `json:"forked_from_thread_id"`
Installation string `json:"installation_id"`
}
func requestSessionMetadata(h http.Header, body []byte) sessionMetadata {
var envelope struct {
Metadata map[string]json.RawMessage `json:"client_metadata"`
}
var m sessionMetadata
if bytes.Contains(body, []byte("client_metadata")) && json.Unmarshal(body, &envelope) == nil {
if raw := envelope.Metadata["x-codex-turn-metadata"]; len(raw) > 0 {
var text string
if json.Unmarshal(raw, &text) == nil {
raw = []byte(text)
}
if json.Unmarshal(raw, &m) == nil {
return m
}
} else if len(envelope.Metadata) > 0 {
_ = json.Unmarshal(envelope.Metadata["turn_id"], &m.Turn)
// Compatibility projections used by clients without the full blob.
_ = json.Unmarshal(envelope.Metadata["thread_source"], &m.Source)
_ = json.Unmarshal(envelope.Metadata["parent_thread_id"], &m.Parent)
if m.Parent == "" {
_ = json.Unmarshal(envelope.Metadata["x-codex-parent-thread-id"], &m.Parent)
}
_ = json.Unmarshal(envelope.Metadata["forked_from_thread_id"], &m.Forked)
_ = json.Unmarshal(envelope.Metadata["installation_id"], &m.Installation)
if m.Source != "" || m.Parent != "" || m.Forked != "" {
return m
}
}
}
projectedTurn := m.Turn
m = sessionMetadata{}
if json.Unmarshal([]byte(h.Get("x-codex-turn-metadata")), &m) != nil {
return sessionMetadata{Turn: projectedTurn}
}
if projectedTurn != "" {
m.Turn = projectedTurn
}
return m
}
func requestCallKind(h http.Header, m sessionMetadata) string {
if h.Get("x-openai-subagent") != "" || h.Get("x-openai-memgen-request") != "" {
return callKind(h)
}
switch source := strings.TrimSpace(m.Source); source {
case "", "user", "subagent":
return callKind(h)
default:
if len(source) > 40 {
source = source[:40]
}
return source
}
}
// Only title helpers inherit their originating chat. A user-created fork is a
// separate conversation, even though it carries the same fork ancestry.
func titleParentSession(h http.Header, m sessionMetadata, kind string) string {
if !isTitleKind(kind) {
return ""
}
for _, id := range []string{m.Parent, m.Forked, h.Get("x-codex-parent-thread-id")} {
id = strings.TrimSpace(id)
if id == "" || len(id) > 128 || strings.ContainsAny(id, "\r\n\t") || id == sessionOf(h) {
continue
}
return id
}
return ""
}