Commit Graph
3 Commits
Author SHA1 Message Date
yetone ab084fdf25 gateway: a model's own system prompt, added after the agent's on every route (liucl on X)
liucl asked for a system prompt per model rather than per agent. A
provider's editor now has a System prompt box in each model's Model
settings row (Providers > a provider > Model settings). The gateway adds
the text after the agent's own system prompt on every request for that
model, whatever agent sends it. Empty, the default, sends the body byte
for byte as before.

It is only ever appended. Codex's base_instructions, Claude Code's
identity block and any agent's prompt stay first and unchanged
(withModelSystem in internal/gateway/model_system.go), in the agent's
protocol, in serve's candidate loop after triedOn, so before translation
and before a plugin's fetch:
- Anthropic: string system + "\n\n" + text; a block array gets one more
  text block; none gets system.
- Chat: the last leading system/developer message gets it after a blank
  line (or one more text part); none gets a system message first.
- Responses: a developer message after the leading system/developer
  items, instructions untouched; a string input becomes that message plus
  the user's; none when previous_response_id is set (the turn it follows
  had it).
- Gemini: systemInstruction / system_instruction gets one more part.
count_tokens counts it (countOn). magpie's own asks (search and vision
side calls, Codex titles, the router's classifier) get none. A fallback
to another model gets that model's own, or none.

Stored as settings.modelSystemPrompts["pid/model"] (SetModelSystemPrompt:
trimmed, at most 64KB, no NUL, a model the provider serves), saved through
modelPrefs[id].system; renames carry it (perModelFields). The Restore
default title now says it takes the system prompt away too; the four
tests that pin that title follow it. app.js stays in its budget with one
comment cut, no raise.

Run, on mote at this head:
- TestModelSystem* (every wire format with exact bytes, empty unchanged,
  own asks skipped, messages/chat/responses/gemini/count_tokens routes,
  another model sent byte for byte as before, a plugin's model),
  TestProviderSaveModelPrefs, settings keep test.
- Broken by hand, each red then restored: no call in serve, no call in
  countOn, prepend in Anthropic, empty not guarded, own asks included, any
  model of the provider; SetModelPrefs ignoring system; the GUI not
  staging it.
- GUI: model-system-prompt.test.cjs (Chromium and WebKit, en zh zh-TW ja
  de, 1200 and 420px), model-limits, model-headers, provider-same-as,
  provider-detect, provider-listed-as, i18n-keys.
- Real run: Claude Code 2.1.321 -p in a sandbox HOME and
  CLAUDE_CONFIG_DIR, through a sandbox `magpie serve` built from this
  tree, to a fake Anthropic upstream recording bodies. Its /v1/messages
  system had 3 blocks, Claude Code's own first, the prompt once, at the
  end of the last block; count_tokens sent "Be terse.\n\nAlways answer in
  Chinese."
- gofmt; go vet ./...; GOOS=linux go vet -tags nogui ./...;
  GOOS=windows go vet ./...: clean.
- The full go test -timeout 45m ./... and the whole GUI suite were
  queued on mote at this head and run after the tag (the owner's ship-now
  rule); what they find goes in follow-up commits.

Not tried: Codex or any agent other than Claude Code for real; a real
vendor upstream; the Responses, chat and Gemini shapes only through the
gateway tests' fake upstreams.
2026-11-10 07:25:54 +08:00
yetone e5b5397c96 providers: a model's context window and max output are set in its Model settings row
sayagoodbyeplz on Discord: WorkBuddy's deepseek-v4.1-flash replies 128000
tokens, not the 384000 models.dev lists, and there was no visible place
to set that for one model. The editor's Advanced Context window and Max
output already took "model=size", but a user looking at the model never
saw them.

Each model's row in Model settings now has a Context window box and a
Max output box beside "Listed as". The placeholder is the listed value
(the vendor's list, else models.dev's, within the window), or the "*"
value from Advanced when one is set. A value typed there is staged as
model=size in Advanced's field, so both say the same thing, and the Save
sends it in contexts/outputs as before. An empty box drops the model's
own value. A bad value is refused with Advanced's message. Restore
default clears both, and its title says so in every language. A
classifier-only provider has neither box, as in Advanced.
/api/providers gives each model a listedOutput (its reply limit without
the user's Max output) so the placeholder doesn't show the user's own
value back.

The boxes are drawn by modellimits.js, imported when the editor draws a
model's row (like keyspend.js), and nine comments in app.js are cut, so
the core stays in its budget with no raise (1,533,541 of 1,533,802 on a5c97cd83).

Run (mote before the modellimits.js move; WSL magpie-desktop, Go 1.26.8,
at this tree, after it, since mote went down):
- go vet ./... (mote, darwin; again on internal/gui, agent and provider
  at the rebased head on a5c97cd83); GOOS=windows go vet ./... and
  GOOS=linux go vet -tags nogui ./... (WSL): ok
- go test ./internal/gui/ ./internal/agent/ ./internal/provider/ (mote): ok,
  except the real-window AppKit tests TestCoveredWindowStaysUp and
  TestClosingWindowMarksPageOutOfSight killed at their deadlines (#4924,
  #2645); WSL, -tags nogui: TestProviderModelOutput*|Budget ok
- TestProviderModelOutputSaid gains a check that listedOutput is
  models.dev's 131072 while the user's 100000 is set
- node --test internal/gui/tests/model-limits.test.cjs (new): 20/20,
  Chromium and WebKit, en/zh/zh-TW/ja/de, 1200 and 420px: placeholder
  384K, 128k typed -> Advanced "deepseek-v4.1-flash=128k" and unsaved,
  a bad value refused, Advanced's "*" becomes the placeholder, the Save
  body's outputs/contexts, Restore default clears the model's own
- WSL, at this tree: model-limits, model-headers, model-price-tiers,
  model-api-preset, provider-detect, provider-listed-as,
  provider-same-as, provider-max-output, provider-output-picks,
  old-webkit, i18n-keys: 105/106, the one red i18n-keys' "a plugin's
  words are its own", red at a46190e1f too (#5332, #5426);
  TestCoreScriptBudget ok. Before that, every model-*, provider-*,
  editor-*, context-picks/tune*, i18n-*, menu-scroll, click-scroll and
  old-webkit file (2325 tests): the reds in model-chip-steady,
  model-copy-id, provider-models-rest, provider-picks-stay,
  provider-fetch-again, provider-typed, model-suffix-own, model-load,
  editor-advanced-align and click-scroll are red the same at a46190e1f on
  WSL (#4388, #4621, #4396, #4397, #5232, #4673, #4962, #4615, #4385).
  That run found two of this change's own: the rows' hooks kept on the
  draft made an untouched editor read as unsaved (model-price-tiers,
  model-api-preset), and a row filled after an await moved the page
  (provider-detect); the hooks are left out of the draft's value and a
  row is filled at once once modellimits.js is loaded.
Broken by hand: model-limits.test.cjs on origin/main's assets fails at
once (no .mlimit row).
Not tried: the real app window; the full GUI suite.
Full suite not run at push.

Reported-by: sayagoodbyeplz (Discord)
2026-11-08 19:05:36 +08:00
yetone 7a6211d047 providers: a model's own headers, in Model settings
axelburks on Discord: a vendor routes one model by a header of its own.
A key's provider (built-in or custom) can give each model headers
(settings.ModelHeaders["pid/model"]). They go on that provider's
requests for that model alone, after the provider's headers and after
auth: a name both have is sent once, as the model's, so a user's value
is never overridden by the provider's or magpie's key header. The
gateway names the model (provider.WithModel) on every route that asks
one: chat on each API, translated and streamed, count_tokens, System
One's decision, embeddings, images, videos and the model test. A
fallback provider serving the same model id gets its own headers only.
Accounts and plugins carry their sign-in's headers and take none.

The editor's "Name, price and reasoning level" panel is now "Model
settings" (every language), with + Add header per model, staged and
made with the Save; Restore default takes them away.
The rows being typed are kept out of the draft's
compare, so an untouched editor still closes without asking.

Broken by hand: dropping WithModel from gateway.go turns
TestModelHeadersReachTheirModelOnEveryRoute red; dropping the provider
id check in modelOf turns TestModelHeadersStayWithTheirProvider red.

Run, on this stack over origin/main 0fc3455e2 (mote's suite lock had
hours of queue, so the suites ran on WSL magpie-desktop under flock):
- go test -timeout 45m ./internal/{provider,gateway,gui,cli,settings,fx}
  on WSL, ok; ./internal/codexcat on mote, ok; gofmt -l clean;
  go vet on mac, GOOS=linux -tags nogui and GOOS=windows on mote, ok;
  TestCoreScriptBudget ok (paid by cutting comments, no raise).
- GUI, both engines, every language, on WSL at the pushed head and at
  0fc3455e2: every touched test file plus the provider-editor files a
  first run found red. No red at head that isn't red at 0fc3455e2. The
  shared reds have issues: i18n-keys #5332, model-chip-steady #4388,
  model-copy-id #4621, models-works-only #4391, plugin-move-unserved
  #4392, provider-fetch-again #5232, provider-levels-save #4394,
  provider-levels-scroll #4395, provider-models-rest #4396,
  provider-picks-stay #4397, provider-typed #4673, remote-magpie #4398,
  zero-priced-free #5262, model-reasonless (webkit de) #4100,
  name-suffix-no-names #1937.
- Rebased on origin/main 4ab1191bc (gateway.go moved under it) and run
  again at the pushed head: gateway and provider ok on WSL, gofmt,
  vet (mac, linux nogui, windows) and the budget ok, i18n-keys,
  model-headers and routing-groups-back with only #5332's red.
Not run at push: the whole GUI suite (1277 files).

Not tried: a real vendor that routes by header (Ark); fake vendors only.

Reported-by: axelburks (Discord)
2026-11-08 11:28:54 +08:00