Codex asks its own model, through its ChatGPT sign-in, for each thread's description (thread_description), even while the conversation runs on magpie's models. Settings › General gets "Codex thread descriptions" beside "Codex thread titles". It defaults to Codex's own, which sends the request as Codex sent it, or it can name one of magpie's models or a routing group. The answer is wrapped as description-only JSON. The titles setting is untouched. Usage and Routing show these requests under their own Description purpose. The setting is kept across unrelated Settings saves. There is no Off yet, because Codex's handling of an empty description hasn't been checked. This is a first slice of #742. Squashed from the PR and merged onto current main. Conflicts resolved: - codex_backend.go: the description branch comes after the title branch, ahead of main's subagent pick. - gateway.go: main's CC Switch #1301 comment above the compaction block is kept; the PR had shortened it. - app.js: the gateway-mode list of hidden rows takes the new row. - i18n.js and settings_keep_test.go: both sides kept. - purposes.js: regenerated with go generate ./internal/usage. Run on the merged tree: - gofmt -l is clean. - go vet passes on darwin, linux -tags nogui and windows; the builds on all three succeed. - go test -tags nogui passes for ./internal/gateway, ./internal/gui, ./internal/settings and ./internal/usage. - GUI, chromium and webkit: codex-titles, gateway-mode, purpose-filter, routing-purpose-state, gui-de, gui-ja, gui-zh-tw, i18n-keys, settings-general/-groups/-sections/-search and click-scroll: 118/118. Broken by hand: with the description branch removed from codexBackend and handle, TestCodexDescriptionsSetting, TestCodexDescriptionFallbackOnMagpie and TestNoDescription fail. Not tried: a real Codex thread_description request, and whether Codex's sidebar takes the answer.
99 lines
3.0 KiB
Go
99 lines
3.0 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"
|
|
}
|
|
|
|
func isDescriptionKind(kind string) bool {
|
|
return kind == "thread_description"
|
|
}
|
|
|
|
// 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 ""
|
|
}
|