← history for src/main.go
b5017ed0src/main.go92 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main

import (
    "bufio"
    "errors"
    "fmt"
    "os"
    "strings"
)

type RemoteChatApp struct {
    config RemoteChatConfiguration
}

func main() {
    configPath := os.Getenv("HOME") + "/.config/remote-chat/config.json"

    if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) {
        fmt.Printf("Config file %s doesn't exist\n", configPath)
        os.Exit(1)
    }

    cfg := GetClientConfig(configPath)
    var c = RemoteChatApp{config: cfg}

    model := cfg.Model
    if len(os.Args) > 1 {
        model = os.Args[1]
    }

    db, err := openDB(cfg.DbFile)
    if err != nil {
        fmt.Fprintf(os.Stderr, "❌ Cannot open database: %v\n", err)
        os.Exit(1)
    }
    defer db.Close()

    if err := initSchema(db); err != nil {
        fmt.Fprintf(os.Stderr, "❌ Cannot initialise schema: %v\n", err)
        os.Exit(1)
    }

    fmt.Printf("\n  Ollama CLI Chat\n")
    fmt.Printf("    model    : %s\n", model)
    fmt.Printf("    database : %s\n", cfg.DbFile)
    fmt.Printf("    commands : :history   :model <name>   :quit\n")
    fmt.Printf("%s\n\n", strings.Repeat("─", 64))

    scanner := bufio.NewScanner(os.Stdin)
    ch := make(chan result, 1)

    for {
        fmt.Print("πŸ‘€  You : ")
        if !scanner.Scan() {
            break
        }

        input := strings.TrimSpace(scanner.Text())
        if input == "" {
            continue
        }

        switch {
        case input == ":quit" || input == ":exit" || input == ":q":
            fmt.Println("πŸ‘‹  Goodbye!")
            return

        case input == ":history":
            printHistory(db)
            continue

        case strings.HasPrefix(input, ":model "):
            model = strings.TrimPrefix(input, ":model ")
            fmt.Printf("βœ…  Model switched to: %s\n\n", model)
            continue
        }
        fmt.Print("πŸ€–  Bot : ⏳ thinking…")
        c.processMessage(db, input, model, ch)

        res := <-ch
        fmt.Print("\rπŸ€–  Bot : ")
        if res.err != nil {
            fmt.Printf("❌ %v\n\n", res.err)
        } else {
            fmt.Printf("%s\n\n", strings.TrimSpace(res.text))
        }
    }

    if err := scanner.Err(); err != nil {
        fmt.Fprintf(os.Stderr, "❌ Scanner: %v\n", err)
    }
}