Files
magpie/internal/provider/factory_client.go
T
yetone e413dd8445 factory: another agent's request to Factory's GPT, Grok and open models opens as Droid's does (#506 aohun: Codex on glm-5.3-flash through a Factory account, 403 Forbidden; the user: 只要不是 claude code,都可以尝试伪装一下) — in #242 the same account's GPT, Grok and GLM answered Droid through magpie every time and refused Grok Build and Claude Code every time, on the same models, efforts, endpoint and headers, the body the only difference, and glm-5.3-flash is a model Factory serves (droid 0.231.0's registry: provider factory, generic chat completions, fireworks/baseten/databricks), sent where and as droid sends it. droid 0.231.0 (its binary, read, not run) opens every system prompt with YOU_ARE_DROID_SYSTEM_PROMPT, "You are Droid, an AI software engineering agent built by Factory.": its system blocks [that line, the agent prompt, …] go as Responses' instructions joined with "\n", and on chat completions as one system message put first, joined the same way; its small calls (the latency ping) send the line alone. A request on /api/llm/o now does too: Responses' instructions, or the first system message of chat completions (text parts joined as droid joins them), open with the line and "\n" before the agent's own; with none, the line alone goes first. A request that already opens with it (droid's own) goes on byte for byte, as does everything on /api/llm/a (Claude, MiniMax M2.7), which is left as the agent sent it. The 403 text now says magpie opens other agents' GPT, Grok and open-model requests as Droid's but Factory may still tell them apart, that Claude models from other agents are refused, to use the model from Droid, and that the org's policy or plan is left when Droid is refused too. GLM-5.3-Flash takes images (droid 0.231.0 says images: true; the rest of the table matches), and the requests say droid 0.231.0. Not tried with a real Factory account: whether the line is all Factory checks is not known. TestFactoryOpensAsDroid (Codex-shaped chat and Responses, Claude Code's system parts, no system, droid's own chat and Responses unchanged byte for byte, Anthropic's Messages unchanged) and TestFactoryGLMFlashImages fail without the change. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./internal/gateway/ ./internal/provider/ pass.
2026-10-02 13:03:41 +08:00

120 lines
3.9 KiB
Go

package provider
// Factory takes a subscription's model requests only from Droid (#242,
// #506): the same account's GPT, Grok and GLM answered Droid through magpie
// every time and refused Codex, Grok Build and Claude Code every time, on
// the same models, efforts, endpoint and headers, the body the only
// difference. Every request droid sends opens its system prompt with one
// line, droid's YOU_ARE_DROID_SYSTEM_PROMPT (droid 0.231.0: its system
// blocks are [that line, the agent's prompt, reminders…], sent as
// Responses' instructions joined with "\n", and on chat completions as one
// system message first, joined the same way; its smaller calls, the
// latency ping among them, send the line alone). So another agent's request
// to Factory's OpenAI-shaped API (/api/llm/o: GPT and Grok on Responses,
// the open models on chat completions) opens with that line too, the
// agent's own system prompt after it, as droid's own does. Anthropic's
// Messages (/api/llm/a: Claude, MiniMax M2.7) is left as the agent sent
// it, and so is a request that already opens with the line: droid's own
// goes on byte for byte.
import (
"encoding/json"
"strings"
)
// factoryDroidLine is the line every droid system prompt opens with.
const factoryDroidLine = "You are Droid, an AI software engineering agent built by Factory."
// factoryDroidBody is body, a request to Factory's /api/llm/o at path, as
// droid would open it: Responses' instructions, or chat completions' first
// system message, starting with droid's line. Anything else, a body that
// can't be read, or one that already starts so is returned as it is.
func factoryDroidBody(path string, body []byte) []byte {
if !strings.Contains(path, "/llm/o/") || len(body) == 0 {
return body
}
var m map[string]json.RawMessage
if zcodeDecode(body, &m) != nil {
return body
}
switch {
case strings.HasSuffix(path, "/responses"):
var in string
if raw, ok := m["instructions"]; ok && string(raw) != "null" && json.Unmarshal(raw, &in) != nil {
return body // not a string: not something droid sends
}
if strings.HasPrefix(in, factoryDroidLine) {
return body
}
if strings.TrimSpace(in) == "" {
in = factoryDroidLine
} else {
in = factoryDroidLine + "\n" + in
}
m["instructions"], _ = zcodeEncode(in)
case strings.HasSuffix(path, "/chat/completions"):
var msgs []map[string]any
if m["messages"] == nil || zcodeDecode(m["messages"], &msgs) != nil {
return body
}
if !factoryDroidChat(&msgs) {
return body
}
var err error
if m["messages"], err = zcodeEncode(msgs); err != nil {
return body
}
default:
return body
}
out, err := zcodeEncode(m)
if err != nil {
return body
}
return out
}
// factoryDroidChat opens msgs' first system message with droid's line, or
// puts one before them with the line alone: false when it opens so already.
func factoryDroidChat(msgs *[]map[string]any) bool {
ms := *msgs
if len(ms) > 0 && ms[0]["role"] == "system" {
switch c := ms[0]["content"].(type) {
case string:
if strings.HasPrefix(c, factoryDroidLine) {
return false
}
if strings.TrimSpace(c) == "" {
ms[0]["content"] = factoryDroidLine
} else {
ms[0]["content"] = factoryDroidLine + "\n" + c
}
return true
case []any:
// droid sends a string, its blocks joined with "\n": text
// parts alone are joined so
texts := []string{factoryDroidLine}
for i, p := range c {
p, _ := p.(map[string]any)
t, ok := p["text"].(string)
if p["type"] != "text" || !ok {
texts = nil
break
}
if i == 0 && strings.HasPrefix(t, factoryDroidLine) {
return false
}
texts = append(texts, t)
}
if texts != nil {
ms[0]["content"] = strings.Join(texts, "\n")
} else {
ms[0]["content"] = append([]any{map[string]any{"type": "text", "text": factoryDroidLine}}, c...)
}
return true
}
}
*msgs = append([]map[string]any{{"role": "system", "content": factoryDroidLine}}, ms...)
return true
}