fixed
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _format_time(seconds: int) -> str:
|
||||
minutes, secs = divmod(max(0, seconds), 60)
|
||||
return f"{minutes:02d}:{secs:02d}"
|
||||
|
||||
|
||||
def format_pomodoro_notice(tool_name: str, raw_result: str) -> str | None:
|
||||
try:
|
||||
data = json.loads(raw_result)
|
||||
except json.JSONDecodeError:
|
||||
return None
|
||||
|
||||
if isinstance(data, dict) and "error" in data:
|
||||
return f"⏱ Помидоро: {data['error']}"
|
||||
|
||||
if tool_name in ("get_pomodoro_status", "start_pomodoro", "stop_pomodoro"):
|
||||
return _format_status_notice(data)
|
||||
|
||||
if tool_name == "get_pomodoro_history":
|
||||
return _format_history_notice(data)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _format_status_notice(data: dict[str, Any]) -> str:
|
||||
status = data.get("status", "idle")
|
||||
task = data.get("task_note") or "без описания"
|
||||
remaining = data.get("remaining_seconds", 0)
|
||||
duration = data.get("duration_min", 25)
|
||||
|
||||
if status == "idle":
|
||||
return "⏱ **Помидоро:** таймер не запущен."
|
||||
|
||||
if status == "running":
|
||||
return (
|
||||
f"⏱ **Помидоро запущен** · осталось **{_format_time(remaining)}** "
|
||||
f"из {duration} мин · задача: _{task}_"
|
||||
)
|
||||
|
||||
if status == "paused":
|
||||
elapsed = data.get("elapsed_seconds", 0)
|
||||
return (
|
||||
f"⏱ **Помидоро на паузе** · прошло {_format_time(elapsed)} "
|
||||
f"из {duration} мин · задача: _{task}_"
|
||||
)
|
||||
|
||||
if status == "completed":
|
||||
return f"⏱ **Помидоро завершён** · {duration} мин · задача: _{task}_"
|
||||
|
||||
if status == "cancelled":
|
||||
return f"⏱ **Помидоро отменён** · задача: _{task}_"
|
||||
|
||||
return f"⏱ Помидоро: {status}"
|
||||
|
||||
|
||||
def _format_history_notice(data: Any) -> str:
|
||||
if not isinstance(data, list) or not data:
|
||||
return "⏱ **История помидоро** пуста."
|
||||
|
||||
lines = ["⏱ **История помидоро:**"]
|
||||
for item in data[:10]:
|
||||
task = item.get("task_note") or "без описания"
|
||||
status = item.get("status", "?")
|
||||
duration = item.get("duration_min", "?")
|
||||
lines.append(f"- {task} ({duration} мин, {status})")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def format_pomodoro_context(status: dict[str, Any]) -> str:
|
||||
notice = _format_status_notice(status)
|
||||
return f"[Актуальный статус помидоро]\n{notice}"
|
||||
@@ -5,9 +5,11 @@ from typing import Any
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.config import get_settings
|
||||
from app.character.service import CharacterService
|
||||
from app.chat.notices import format_pomodoro_context, format_pomodoro_notice
|
||||
from app.db.models import ChatSession, Message
|
||||
from app.llm.client import LLMClient
|
||||
from app.pomodoro.service import PomodoroService
|
||||
from app.tools.registry import TOOL_DEFINITIONS, execute_tool
|
||||
|
||||
MAX_TOOL_ROUNDS = 5
|
||||
@@ -17,7 +19,7 @@ class ChatService:
|
||||
def __init__(self, db: Session):
|
||||
self.db = db
|
||||
self.llm = LLMClient()
|
||||
self.system_prompt = get_settings().load_system_prompt()
|
||||
self.character = CharacterService()
|
||||
|
||||
def list_sessions(self) -> list[ChatSession]:
|
||||
stmt = select(ChatSession).order_by(ChatSession.updated_at.desc())
|
||||
@@ -41,9 +43,21 @@ class ChatService:
|
||||
self.db.commit()
|
||||
return True
|
||||
|
||||
def _build_system_prompt(self) -> str:
|
||||
status = PomodoroService(self.db).get_status()
|
||||
return (
|
||||
f"{self.character.get_system_prompt()}\n\n"
|
||||
f"{format_pomodoro_context(status)}"
|
||||
)
|
||||
|
||||
def _build_messages(self, session: ChatSession) -> list[dict[str, Any]]:
|
||||
messages: list[dict[str, Any]] = [{"role": "system", "content": self.system_prompt}]
|
||||
messages: list[dict[str, Any]] = [
|
||||
{"role": "system", "content": self._build_system_prompt()}
|
||||
]
|
||||
for msg in session.messages:
|
||||
if msg.role == "notice":
|
||||
continue
|
||||
|
||||
content = msg.content or None
|
||||
entry: dict[str, Any] = {"role": msg.role, "content": content}
|
||||
if msg.tool_calls_json:
|
||||
@@ -123,7 +137,16 @@ class ChatService:
|
||||
}
|
||||
messages.append(tool_message)
|
||||
self._save_message(session_id, "tool", result, tool_call_id=tool_call["id"])
|
||||
yield self._sse("tool", {"name": fn["name"], "result": json.loads(result)})
|
||||
|
||||
notice = format_pomodoro_notice(fn["name"], result)
|
||||
if notice:
|
||||
self._save_message(session_id, "notice", notice)
|
||||
yield self._sse("notice", {"content": notice})
|
||||
|
||||
yield self._sse(
|
||||
"pomodoro",
|
||||
{"name": fn["name"], "result": json.loads(result)},
|
||||
)
|
||||
|
||||
continue
|
||||
|
||||
|
||||
Reference in New Issue
Block a user