fix: dedupe preset messages (#4961)

This commit is contained in:
鸦羽
2026-02-08 22:18:13 +08:00
committed by GitHub
parent a7e580407c
commit eca3ede7b0
4 changed files with 15 additions and 6 deletions
+9 -1
View File
@@ -3,7 +3,13 @@
from typing import Any, ClassVar, Literal, cast
from pydantic import BaseModel, GetCoreSchemaHandler, model_serializer, model_validator
from pydantic import (
BaseModel,
GetCoreSchemaHandler,
PrivateAttr,
model_serializer,
model_validator,
)
from pydantic_core import core_schema
@@ -178,6 +184,8 @@ class Message(BaseModel):
tool_call_id: str | None = None
"""The ID of the tool call."""
_no_save: bool = PrivateAttr(default=False)
@model_validator(mode="after")
def check_content_required(self):
# assistant + tool_calls is not None: allow content to be None
@@ -149,7 +149,10 @@ class ToolLoopAgentRunner(BaseAgentRunner[TContext]):
messages = []
# append existing messages in the run context
for msg in request.contexts:
messages.append(Message.model_validate(msg))
m = Message.model_validate(msg)
if isinstance(msg, dict) and msg.get("_no_save"):
m._no_save = True
messages.append(m)
if request.prompt is not None:
m = await request.assemble_context()
messages.append(Message.model_validate(m))
+1 -1
View File
@@ -313,7 +313,7 @@ class PersonaManager:
{
"role": "user" if user_turn else "assistant",
"content": dialog,
"_no_save": None, # 不持久化到 db
"_no_save": True, # 不持久化到 db
},
)
user_turn = not user_turn
@@ -355,9 +355,7 @@ class InternalAgentSubStage(Stage):
if message.role == "system" and not skipped_initial_system:
skipped_initial_system = True
continue
if message.role in ["assistant", "user"] and getattr(
message, "_no_save", None
):
if message.role in ["assistant", "user"] and message._no_save:
continue
message_to_save.append(message.model_dump())