forked from Rockachopa/Timmy-time-dashboard
feat: persistent chat history with clear button
- Add dashboard/store.py: MessageLog dataclass singleton tracking user/agent/error messages for the lifetime of the server process - agents.py: write each chat turn to MessageLog; add GET and DELETE /agents/timmy/history routes returning the history.html partial - partials/history.html: render stored messages by role (YOU / TIMMY / SYSTEM); falls back to the Mission Control init message when empty - index.html: chat-log loads history via hx-get on page start; new CLEAR button in panel header sends hx-delete to reset the log - style.css: add .mc-btn-clear (muted, red-on-hover for the header) - tests: autouse reset_message_log fixture in conftest; 5 new history tests covering empty state, recording, offline errors, clear, and post-clear state → 32 tests total, all passing https://claude.ai/code/session_01KZMfwBpLuiv6x9GbzTqbys
This commit is contained in:
@@ -6,6 +6,7 @@ from fastapi.responses import HTMLResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
from timmy.agent import create_timmy
|
||||
from dashboard.store import message_log
|
||||
|
||||
router = APIRouter(prefix="/agents", tags=["agents"])
|
||||
templates = Jinja2Templates(directory=str(Path(__file__).parent.parent / "templates"))
|
||||
@@ -27,6 +28,25 @@ async def list_agents():
|
||||
return {"agents": list(AGENT_REGISTRY.values())}
|
||||
|
||||
|
||||
@router.get("/timmy/history", response_class=HTMLResponse)
|
||||
async def get_history(request: Request):
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"partials/history.html",
|
||||
{"messages": message_log.all()},
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/timmy/history", response_class=HTMLResponse)
|
||||
async def clear_history(request: Request):
|
||||
message_log.clear()
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"partials/history.html",
|
||||
{"messages": []},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/timmy/chat", response_class=HTMLResponse)
|
||||
async def chat_timmy(request: Request, message: str = Form(...)):
|
||||
timestamp = datetime.now().strftime("%H:%M:%S")
|
||||
@@ -40,6 +60,12 @@ async def chat_timmy(request: Request, message: str = Form(...)):
|
||||
except Exception as exc:
|
||||
error_text = f"Timmy is offline: {exc}"
|
||||
|
||||
message_log.append(role="user", content=message, timestamp=timestamp)
|
||||
if response_text is not None:
|
||||
message_log.append(role="agent", content=response_text, timestamp=timestamp)
|
||||
else:
|
||||
message_log.append(role="error", content=error_text, timestamp=timestamp)
|
||||
|
||||
return templates.TemplateResponse(
|
||||
request,
|
||||
"partials/chat_message.html",
|
||||
|
||||
Reference in New Issue
Block a user