← history for src/service.go
b5017ed0src/service.go75 lines⬡ raw↓ download
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
}