added memmory
This commit is contained in:
@@ -11,6 +11,7 @@ from app.chat.notices import (
|
||||
format_pomodoro_context,
|
||||
format_tool_notice,
|
||||
)
|
||||
from app.memory.context import format_memory_context, get_memory_snapshot
|
||||
from app.projects.context import format_projects_context, get_projects_snapshot
|
||||
from app.db.models import ChatSession, Message
|
||||
from app.llm.client import LLMClient
|
||||
@@ -18,6 +19,7 @@ from app.pomodoro.service import PomodoroService
|
||||
from app.tools.registry import TOOL_DEFINITIONS, execute_tool
|
||||
|
||||
MAX_TOOL_ROUNDS = 5
|
||||
MAX_HISTORY_MESSAGES = 40
|
||||
|
||||
|
||||
class ChatService:
|
||||
@@ -48,23 +50,31 @@ class ChatService:
|
||||
self.db.commit()
|
||||
return True
|
||||
|
||||
def _build_system_prompt(self) -> str:
|
||||
def _build_system_prompt(self, session_id: int | None = None) -> str:
|
||||
status = PomodoroService(self.db).get_status()
|
||||
memory_snapshot = get_memory_snapshot(self.db, session_id)
|
||||
projects_snapshot = get_projects_snapshot(self.db)
|
||||
return (
|
||||
f"{self.character.get_system_prompt()}\n\n"
|
||||
f"{format_memory_context(memory_snapshot)}\n\n"
|
||||
f"{format_pomodoro_context(status)}\n\n"
|
||||
f"{format_projects_context(projects_snapshot)}"
|
||||
)
|
||||
|
||||
def _build_messages(self, session: ChatSession) -> list[dict[str, Any]]:
|
||||
system_prompt = self._build_system_prompt(session.id)
|
||||
all_chat = [m for m in session.messages if m.role != "notice"]
|
||||
if len(all_chat) > MAX_HISTORY_MESSAGES:
|
||||
system_prompt += (
|
||||
f"\n\n[История чата: в контексте последние {MAX_HISTORY_MESSAGES} "
|
||||
f"из {len(all_chat)} сообщений. Раннее — в сводке сессии, если сохранена.]"
|
||||
)
|
||||
messages: list[dict[str, Any]] = [
|
||||
{"role": "system", "content": self._build_system_prompt()}
|
||||
{"role": "system", "content": system_prompt}
|
||||
]
|
||||
for msg in session.messages:
|
||||
if msg.role == "notice":
|
||||
continue
|
||||
chat_messages = all_chat[-MAX_HISTORY_MESSAGES:] if len(all_chat) > MAX_HISTORY_MESSAGES else all_chat
|
||||
|
||||
for msg in chat_messages:
|
||||
content = msg.content or None
|
||||
entry: dict[str, Any] = {"role": msg.role, "content": content}
|
||||
if msg.tool_calls_json:
|
||||
@@ -136,7 +146,9 @@ class ChatService:
|
||||
for tool_call in tool_calls:
|
||||
fn = tool_call["function"]
|
||||
args = LLMClient.parse_tool_arguments(fn.get("arguments", ""))
|
||||
result = await execute_tool(self.db, fn["name"], args)
|
||||
result = await execute_tool(
|
||||
self.db, fn["name"], args, session_id=session_id
|
||||
)
|
||||
tool_message = {
|
||||
"role": "tool",
|
||||
"tool_call_id": tool_call["id"],
|
||||
|
||||
Reference in New Issue
Block a user