modified: bot.db

modified:   handlers/dashboard.go
	modified:   main.go
	modified:   services/openrouter.go
	modified:   templates/dashboard.html
This commit is contained in:
2026-03-01 07:10:01 -03:00
parent d4d395356b
commit 6f7987c3fe
5 changed files with 328 additions and 151 deletions

View File

@@ -60,29 +60,6 @@ func ShowDashboard(c *gin.Context) {
})
}
// Test OpenRouter via the Dashboard
func TestAIHandler(c *gin.Context) {
var body struct {
Prompt string `json:"prompt"`
}
if err := c.BindJSON(&body); err != nil {
c.JSON(400, gin.H{"response": "Invalid request, dummy."})
return
}
// Calling the service we wrote earlier
response, err := services.GetAIResponse([]services.Message{
{Role: "user", Content: body.Prompt},
})
if err != nil {
c.JSON(500, gin.H{"response": "AI Error: " + err.Error()})
return
}
c.JSON(200, gin.H{"response": response})
}
// Add this to handlers/dashboard.go
func CreateAppointmentHandler(c *gin.Context) {
var body struct {
@@ -172,25 +149,38 @@ func PostMessageHandler(c *gin.Context) {
var body struct {
Content string `json:"content"`
}
c.BindJSON(&body)
if err := c.BindJSON(&body); err != nil {
c.Status(400)
return
}
// 1. Save User Message
// 1. Save User Message to DB first
db.Conn.Exec("INSERT INTO messages (chat_id, role, content) VALUES (?, 'user', ?)", chatId, body.Content)
// 2. Fetch history for AI
// 2. Fetch history
rows, _ := db.Conn.Query("SELECT role, content FROM messages WHERE chat_id = ? ORDER BY created_at ASC", chatId)
var history []services.Message
defer rows.Close()
for rows.Next() {
var m services.Message
rows.Scan(&m.Role, &m.Content)
history = append(history, m)
}
// 3. Get AI Response
aiResp, _ := services.GetAIResponse(history)
// 3. Set Headers for Streaming
c.Writer.Header().Set("Content-Type", "text/event-stream")
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Connection", "keep-alive")
c.Writer.Header().Set("Transfer-Encoding", "chunked")
// 4. Save Assistant Message
db.Conn.Exec("INSERT INTO messages (chat_id, role, content) VALUES (?, 'assistant', ?)", chatId, aiResp)
// 4. Call the Stream Service
// We pass a function that writes chunks directly to the HTTP response
fullResponse, _ := services.StreamAIResponse(history, func(chunk string) {
c.Writer.Write([]byte(chunk))
c.Writer.Flush() // Important: Send it NOW, don't buffer
})
c.JSON(200, gin.H{"response": aiResp})
// 5. Save the FULL Assistant Message to DB for history
// We do this AFTER the stream finishes so the next load has the full text
db.Conn.Exec("INSERT INTO messages (chat_id, role, content) VALUES (?, 'assistant', ?)", chatId, fullResponse)
}