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.
73 lines
2.2 KiB
Go
73 lines
2.2 KiB
Go
package provider
|
|
|
|
// A model's own system prompt (liucl on X): text the user gives one model
|
|
// of a provider, kept in settings' ModelSystemPrompts by "<provider
|
|
// id>/<model id>", which the gateway adds after the agent's own system
|
|
// prompt on every request for that model, whichever agent sends it. It
|
|
// only appends: the agent's prompt, and an identity it starts with, stay
|
|
// first and as they were. A signed-in account's model and a plugin's take
|
|
// it too, as the gateway adds it before a request is translated for them.
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// ModelSystemPrompt is the system prompt the user gave provider pid's
|
|
// model, "" for none. It reads the held settings, as it is asked on every
|
|
// try of a request.
|
|
func ModelSystemPrompt(pid, model string) string {
|
|
if pid == "" || model == "" {
|
|
return ""
|
|
}
|
|
return heldSettings().ModelSystemPrompts[pid+"/"+model]
|
|
}
|
|
|
|
// maxModelSystemPrompt bounds one model's system prompt.
|
|
const maxModelSystemPrompt = 64 << 10
|
|
|
|
// SetModelSystemPrompt gives a provider's model, spelt "provider/model",
|
|
// the system prompt text, in place of the one it had; "" takes it away.
|
|
func SetModelSystemPrompt(ref, text string) error {
|
|
_, err := setModelSystemPrompt(ref, text)
|
|
return err
|
|
}
|
|
|
|
func setModelSystemPrompt(ref, text string) (bool, error) {
|
|
p, model, err := splitRef(ref)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
text = strings.TrimSpace(text)
|
|
if text != "" {
|
|
if !p.serves(model) {
|
|
return false, fmt.Errorf("%s has no model %s (magpie provider %s lists them)", p.ID, model, p.ID)
|
|
}
|
|
if len(text) > maxModelSystemPrompt {
|
|
return false, fmt.Errorf("a model's system prompt is at most %d KB", maxModelSystemPrompt>>10)
|
|
}
|
|
if strings.ContainsRune(text, 0) {
|
|
return false, fmt.Errorf("a system prompt has no NUL")
|
|
}
|
|
}
|
|
s := settings.Load()
|
|
key := p.ID + "/" + model
|
|
if s.ModelSystemPrompts[key] == text {
|
|
return false, nil
|
|
}
|
|
if text == "" {
|
|
delete(s.ModelSystemPrompts, key)
|
|
} else {
|
|
if s.ModelSystemPrompts == nil {
|
|
s.ModelSystemPrompts = map[string]string{}
|
|
}
|
|
s.ModelSystemPrompts[key] = text
|
|
}
|
|
if err := settings.Save(s); err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|