Merge restoration policy into system message

This commit is contained in:
Mikei386
2026-09-08 08:37:53 +02:00
parent 118e32005e
commit 636e48ce93
2 changed files with 27 additions and 1 deletions
+12 -1
View File
@@ -1396,7 +1396,18 @@ def _inject_restoration_system_policy(data: dict, path: str) -> dict:
if path == "/v1/chat/completions":
messages = data.get("messages")
if isinstance(messages, list):
messages.insert(0, {"role": "system", "content": policy})
# Qwen's chat template permits exactly one system message and it
# must be the first message. The global router policy may already
# have created that message, so extend it instead of inserting a
# second system message in front of it.
if (messages and isinstance(messages[0], dict)
and messages[0].get("role") == "system"
and isinstance(messages[0].get("content"), str)):
existing = messages[0]["content"]
if policy not in existing:
messages[0]["content"] = f"{policy}\n\n{existing}"
else:
messages.insert(0, {"role": "system", "content": policy})
elif path == "/v1/responses":
instructions = data.get("instructions")
data["instructions"] = (