|
@@ -10,8 +10,10 @@ import (
|
|
|
"log"
|
|
|
"net/http"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
|
|
|
"git.riomhaire.com/gremlin/elizaservice/eliza"
|
|
|
+ "git.riomhaire.com/gremlin/elizaservice/facades/cache"
|
|
|
"github.com/gofrs/uuid"
|
|
|
"github.com/jaffee/commandeer"
|
|
|
|
|
@@ -28,12 +30,34 @@ var templates embed.FS
|
|
|
//go:embed bots/*
|
|
|
var bots embed.FS
|
|
|
|
|
|
+var cacheManager cache.BotCache
|
|
|
+var cacheTTL int
|
|
|
+
|
|
|
+type BotInteraction struct {
|
|
|
+ Time string `json:"time,omitempty" yaml:"time,omitempty"`
|
|
|
+ Question string `json:"question,omitempty" yaml:"question,omitempty"`
|
|
|
+ Answer string `json:"answer,omitempty" yaml:"answer,omitempty"`
|
|
|
+}
|
|
|
+
|
|
|
+/***********************************************************************************************
|
|
|
+* This is stored as JSON in the cache (redis)
|
|
|
+ ***********************************************************************************************/
|
|
|
+type SessionData struct {
|
|
|
+ SessionID string `json:"sessionID" yaml:"sessionID"`
|
|
|
+ StartTime string `json:"startTime,omitempty" yaml:"startTime,omitempty"`
|
|
|
+ User string `json:"user,omitempty" yaml:"user,omitempty"`
|
|
|
+ Bot string `json:"bot,omitempty" yaml:"bot,omitempty"`
|
|
|
+ Conversation []BotInteraction `json:"conversation,omitempty" yaml:"conversation,omitempty"`
|
|
|
+}
|
|
|
+
|
|
|
/***********************************************************************************************
|
|
|
*
|
|
|
***********************************************************************************************/
|
|
|
type Main struct {
|
|
|
- BotName string `help:"What This Bot is Called."`
|
|
|
- Port int `help:"What port bot is listening too"`
|
|
|
+ BotName string `help:"What This Bot is Called."`
|
|
|
+ Port int `help:"What port bot is listening too"`
|
|
|
+ Cache string `help:"What Cache configuration to use - redis format <host>:<port>:<db>"`
|
|
|
+ CacheTTL int `help:"How long cache is allowed to store data in seconds"`
|
|
|
}
|
|
|
|
|
|
/***********************************************************************************************
|
|
@@ -41,11 +65,37 @@ type Main struct {
|
|
|
***********************************************************************************************/
|
|
|
func NewMain() *Main {
|
|
|
return &Main{
|
|
|
- BotName: "Eliza",
|
|
|
- Port: 8070,
|
|
|
+ BotName: "Eliza",
|
|
|
+ Port: 8070,
|
|
|
+ Cache: "empire:6379:1",
|
|
|
+ CacheTTL: 3600,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/********************************
|
|
|
+This appends to session in cache latest info
|
|
|
+*********************************/
|
|
|
+func addToConversation(sessionID, bot, user, question, answer string) {
|
|
|
+ sessionData := SessionData{}
|
|
|
+ // Lookup session
|
|
|
+ jsonValue, found, _ := cacheManager.Retrieve(sessionID, false)
|
|
|
+
|
|
|
+ if !found {
|
|
|
+ sessionData = SessionData{SessionID: sessionID, Bot: bot, User: user, StartTime: time.Now().UTC().String(), Conversation: make([]BotInteraction, 0)}
|
|
|
+ }
|
|
|
+ if found {
|
|
|
+ if err := json.Unmarshal([]byte(jsonValue), &sessionData); err != nil {
|
|
|
+ sessionData = SessionData{SessionID: sessionID, Bot: bot, User: user, StartTime: time.Now().UTC().String(), Conversation: make([]BotInteraction, 0)}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // OK addToConversation
|
|
|
+ botInteraction := BotInteraction{time.Now().UTC().String(), question, answer}
|
|
|
+ sessionData.Conversation = append(sessionData.Conversation, botInteraction)
|
|
|
+ // Save it as json
|
|
|
+ j, _ := json.Marshal(sessionData)
|
|
|
+ cacheManager.Store(sessionID, string(j), cacheTTL)
|
|
|
+}
|
|
|
+
|
|
|
/***********************************************************************************************
|
|
|
* This at the moment ... but it could be one passed in via LTI if available
|
|
|
***********************************************************************************************/
|
|
@@ -86,6 +136,7 @@ func findPersonality(name string) (personality eliza.Personality, err error) {
|
|
|
***********************************************************************************************/
|
|
|
func chantboInteractiontEndpoint(c echo.Context) error {
|
|
|
// Extract question from GET request
|
|
|
+ user := c.QueryParam("user")
|
|
|
question := c.QueryParam("value")
|
|
|
botname := c.QueryParam("bot")
|
|
|
sessionID := c.QueryParam("session")
|
|
@@ -104,7 +155,11 @@ func chantboInteractiontEndpoint(c echo.Context) error {
|
|
|
character := eliza.NewBotPersonality(&personality)
|
|
|
answer := character.ReplyTo(question)
|
|
|
|
|
|
- fmt.Println("Session ID [", sessionID, "] Question [", question, "] Answer [", answer, "]")
|
|
|
+ msg := fmt.Sprintf("Bot [%s %s] Session ID [%s] Question [%s] Answer [%s]", botname, personality.Version, sessionID, question, answer)
|
|
|
+ fmt.Println(msg)
|
|
|
+
|
|
|
+ // update cache
|
|
|
+ addToConversation(sessionID, botname, user, question, answer)
|
|
|
|
|
|
// Return Eliza's answer
|
|
|
return c.String(http.StatusOK, answer)
|
|
@@ -157,15 +212,16 @@ func chantbotEndpoint(c echo.Context) error {
|
|
|
func (m *Main) Run() error {
|
|
|
|
|
|
fmt.Println(`
|
|
|
-####### # ### ####### #
|
|
|
-# # # # # #
|
|
|
-# # # # # #
|
|
|
-##### # # # # #
|
|
|
-# # # # #######
|
|
|
-# # # # # #
|
|
|
-####### ####### ### ####### # #
|
|
|
- `)
|
|
|
- fmt.Println()
|
|
|
+███████╗██╗ ██╗███████╗ █████╗
|
|
|
+██╔════╝██║ ██║╚══███╔╝██╔══██╗
|
|
|
+█████╗ ██║ ██║ ███╔╝ ███████║
|
|
|
+██╔══╝ ██║ ██║ ███╔╝ ██╔══██║
|
|
|
+███████╗███████╗██║███████╗██║ ██║
|
|
|
+╚══════╝╚══════╝╚═╝╚══════╝╚═╝ ╚═╝`)
|
|
|
+
|
|
|
+ // Create Cache Manager (redis)
|
|
|
+ cacheManager = cache.NewRedisCache(m.Cache)
|
|
|
+ cacheTTL = m.CacheTTL
|
|
|
|
|
|
e := echo.New()
|
|
|
e.Use(middleware.CORS())
|