Files
magpie/internal/gateway/codex_suggestions.go
T
yetone 8707ea7757 gateway: Codex's prompt suggestions go on a model set for them (@0xcb198609 on X)
The Codex app's home-page prompt suggestions (thread_source
ambient_suggestions, and their safety check, ambient_suggestion_safety;
the Routing page's 提示词建议) are a background agentic turn that reads
the project and connected apps. The app asks for the model its server
config names (codex-app-model-routing's
codex_ambient_suggestions_model_slug, gpt-6-luna) with provider model
fallback on, so with magpie as Codex's provider they ran on the
provider's default model, often auto: @0xcb198609 saw 76.9k tokens in
113s on deepseek-v4-pro. Codex has no config.toml key for that model,
only an on/off switch in the app (store key ambient-suggestions-enabled,
Settings › Configuration › Homepage suggestions; read from the 26.1106
bundle).

magpie now has a setting for it, "Codex prompt suggestions" in Codex's
row on the Agents page (Settings, beside Codex composer predictions):
- Empty, the default, is Codex's own pick: nothing moves. A value the
  user set is never overridden.
- A model or group of magpie's puts both calls there: in serve as the
  codex-suggestions move (the Routing page says why), and on the ChatGPT
  backend's route before the split, carrying the asked model to serve so
  the trace and ledger keep it. codexOwnOffMagpie leaves a bare
  gpt-6-luna to magpie while the setting is on (codexSideSet).
- A target that is disabled, unserved or not allowed to the gateway key
  leaves the call as asked, with a log line (compactRefused).
- To fit app.js's script budget without raising it, the suggestions and
  auto-review pickers share codexPick with codexSidePick, and every
  Codex row's button comes from condButton. The new route is named for
  the CLI (settings_parity_test.go). agent-prefs and settings-general
  list the new row.
- The row's tooltip and the Routing page's Suggestions story say where
  Codex turns them off. magpie's old story said "Suggested prompts",
  which the current app no longer shows; corrected in all five languages
  from the app's own locale files.

Run (mote, at this head rebased on e25210b6e):
- go test -timeout 45m ./...: ok, every package (101). A first run
  at e97271f02 had cli and gui red on the CLI-parity guard and
  TestCoreScriptBudget; both fixed here, without raising the budget.
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./...: clean. gofmt -l: clean.
- TestCodexSuggestionsMoved/Left/MovedOffChatGPT/ModelRefusesBareName,
  TestCodexSuggestionsSetting: ok.
- GUI: codex-suggestions (new), codex-predictions, codex-auto-review,
  routing-kind, i18n-keys: 58/58 ok before the rebase; at this head
  codex-suggestions, i18n-keys, agent-prefs, routing-kind 84/84 ok.
  Every GUI file that opens Codex's row or the Routing kind stories
  (26) ran too: red only in codex-async-compact (zh, zh-TW) and
  codex-memories-settings (chromium scroll), both red the same on
  origin/main e25210b6e and filed as #6658 and #6372.
- Rebased last onto 90d54a267 (internal/sessions and internal/usage
  only): internal/sessions, internal/usage, TestCoreScriptBudget, the
  route guards and the CodexSuggestions tests ok again there.

Broken by hand: codexSuggestionsTo returning "" and codexSideSet's new
case off: TestCodexSuggestionsMoved red (m1 sent, and a 404 "magpie
knows no model gpt-6-luna"), TestCodexSuggestionsMovedOffChatGPT red.

Not tried: a real Codex app's prompt suggestions against magpie (no
Codex.app here or on mote); the whole GUI suite (only the files that
open Codex's row, its settings or the Routing kind stories).
2026-11-16 18:59:40 +08:00

58 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"context"
"log"
"net/http"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// movedCodexSuggestions is the reason Codex's prompt suggestions went on
// the model set for them (provider.CodexSuggestionsModel) rather than the
// one they asked for.
const movedCodexSuggestions = "codex-suggestions"
// codexSuggestionsTo is the model one of the Codex app's prompt-suggestion
// calls (thread_source ambient_suggestions, and the safety check of what
// they found, ambient_suggestion_safety) goes on: the one set for them,
// "" to leave it on asked. The app asks for a model of OpenAI's that its
// server names (gpt-6-luna) and, on magpie as its provider, falls back to
// the provider's default model, so the suggestions, a long agentic turn
// that reads the project, ran on whatever "auto" picked (@0xcb198609 on
// X: 76.9k tokens on deepseek-v4-pro). Codex has no setting for their
// model; its own switch, Settings › Configuration › Homepage suggestions,
// turns them off. The model set must be enabled, served and allowed to
// the gateway key; when it isn't, the call goes on as asked and the log
// says why. The cheap checks come first, so an unset model costs a held
// settings read.
func codexSuggestionsTo(r *http.Request, agent, kind, asked string) string {
if agent != "codex" && !fromCodex(r.Header) || usage.PurposeOf(kind) != "kind:ambient_suggestions" {
return ""
}
to := provider.CodexSuggestionsModel()
if to == "" || to == asked {
return ""
}
if why := compactRefused(r, to); why != "" {
log.Printf("gateway: Codex's prompt suggestions stay on %s, not %s: %s", asked, to, why)
return ""
}
return to
}
type suggestionsAskedKey struct{}
// withSuggestionsAsked carries the model a prompt-suggestion call asked
// for, put on the model set for them on the ChatGPT backend's route, to
// serve, so the trace and the ledger keep what it asked.
func withSuggestionsAsked(r *http.Request, asked string) *http.Request {
return r.WithContext(context.WithValue(r.Context(), suggestionsAskedKey{}, asked))
}
func suggestionsAskedOf(r *http.Request) string {
s, _ := r.Context().Value(suggestionsAskedKey{}).(string)
return s
}