package main import ( "fmt" "strings" "time" ) type result struct { text string err error } // processMessage saves the request, calls Ollama, and saves the response — // all in a separate goroutine. It sends a single result on ch when done. func (c *RemoteChatApp) processMessage(db *DB, message, model string, ch chan<- result) { go func() { requestID, err := saveRequest(db, message) if err != nil { ch <- result{err: err} return } response, err := c.callOllama(message, model) if err != nil { ch <- result{err: err} return } if err := saveResponse(db, requestID, response); err != nil { ch <- result{err: err} return } ch <- result{text: response} }() } func printHistory(db *DB) { rows, err := getRequestsHistory(db) if err != nil { fmt.Printf("❌ History error: %v\n", err) return } fmt.Printf("\n%s\n", strings.Repeat("─", 64)) fmt.Println(" 📜 Last conversations (newest first)") fmt.Printf("%s\n", strings.Repeat("─", 64)) if len(rows) == 0 { fmt.Println(" (no conversations yet)") } for _, row := range rows { ts := row["created_at"] if t, err := time.Parse("2006-01-02 15:04:05", ts); err == nil { ts = t.Format("Jan 02 2006 15:04:05") } fmt.Printf("\n 🕐 %s\n", ts) fmt.Printf(" 👤 %s\n", decodeB64(row["message"])) fmt.Printf(" 🤖 %s\n", decodeB64(row["response"])) } fmt.Printf("%s\n", strings.Repeat("─", 64)) } func getRequestsHistory(db *DB) ([]Row, error) { rows, err := db.Query(` SELECT r.message, rs.response, r.created_at FROM requests r JOIN responses rs ON rs.id_request = r.id ORDER BY r.created_at DESC LIMIT 10 `) return rows, err }