feat: real SSE streaming for agent chat + data cleanup on startup

SSE Streaming:
- Add POST /api/agent/chat/stream endpoint with real SSE streaming
- Agent.HandleMessageStream() streams final LLM response via onEvent callback
- Tool-calling rounds use non-streaming; tool status sent as 'tool' events
- Frontend uses ReadableStream API instead of fake word-by-word setTimeout
- SSE events: tool (tool execution), delta (text chunk), done (complete), error

Data Maintenance:
- Add cleanupOldData() on server startup (decisions >90d, equity >180d)
- Add CleanOldRecords to DecisionStore and EquityStore
- Add store-level logger for cleanup operations

Exchange Traders:
- Add GetAccountInfo() to Bitget, Bybit, Indodax, KuCoin, Lighter, OKX traders
- Remove unused imports in Aster/Lighter traders
This commit is contained in:
shinchan-zhai
2026-03-23 13:31:09 +08:00
parent 5fc826b4e0
commit bd3d532239
18 changed files with 422 additions and 43 deletions

View File

@@ -11,6 +11,7 @@ import (
func (s *Server) RegisterAgentHandler(h *agent.WebHandler) {
// Chat requires auth — can trigger trades and access account data
s.router.POST("/api/agent/chat", s.authMiddleware(), gin.WrapF(h.HandleChat))
s.router.POST("/api/agent/chat/stream", s.authMiddleware(), gin.WrapF(h.HandleChatStream))
// Public endpoints — read-only market data
s.router.GET("/api/agent/health", gin.WrapF(h.HandleHealth))
s.router.GET("/api/agent/klines", gin.WrapF(h.HandleKlines))

View File

@@ -640,7 +640,38 @@ func (s *Server) authMiddleware() gin.HandlerFunc {
}
// Start Start server
// cleanupOldData removes old decision records and equity snapshots on startup
// to prevent unbounded database growth.
func (s *Server) cleanupOldData() {
if s.store == nil {
return
}
// Clean decision records older than 90 days
if decisionStore := s.store.Decision(); decisionStore != nil {
deleted, err := decisionStore.CleanOldRecords("", 90)
if err != nil {
logger.Warnf("⚠️ Failed to clean old decision records: %v", err)
} else if deleted > 0 {
logger.Infof("🧹 Cleaned %d old decision records (>90 days)", deleted)
}
}
// Clean equity snapshots older than 180 days
if equityStore := s.store.Equity(); equityStore != nil {
deleted, err := equityStore.CleanOldRecords("", 180)
if err != nil {
logger.Warnf("⚠️ Failed to clean old equity snapshots: %v", err)
} else if deleted > 0 {
logger.Infof("🧹 Cleaned %d old equity snapshots (>180 days)", deleted)
}
}
}
func (s *Server) Start() error {
// Clean up old data on startup
s.cleanupOldData()
addr := fmt.Sprintf(":%d", s.port)
logger.Infof("🌐 API server starting at http://localhost%s", addr)
logger.Infof("📊 API Documentation:")