mirror of
https://github.com/mariocandela/beelzebub.git
synced 2025-07-01 18:47:26 -04:00
Feat: Add FallbackCommand for HTTP Strategy, refactor packages strategies (#175)
Add FallbackCommand for HTTP Strategy, refactor packages strategies, improve histories implementations.
This commit is contained in:
47
historystore/history_store.go
Normal file
47
historystore/history_store.go
Normal file
@ -0,0 +1,47 @@
|
||||
package historystore
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/mariocandela/beelzebub/v3/plugins"
|
||||
)
|
||||
|
||||
// HistoryStore is a thread-safe structure for storing Messages used to build LLM Context.
|
||||
type HistoryStore struct {
|
||||
sync.RWMutex
|
||||
sessions map[string][]plugins.Message
|
||||
}
|
||||
|
||||
// NewHistoryStore returns a prepared HistoryStore
|
||||
func NewHistoryStore() *HistoryStore {
|
||||
return &HistoryStore{
|
||||
sessions: make(map[string][]plugins.Message),
|
||||
}
|
||||
}
|
||||
|
||||
// HasKey returns true if the supplied key exists in the map.
|
||||
func (hs *HistoryStore) HasKey(key string) bool {
|
||||
hs.RLock()
|
||||
defer hs.RUnlock()
|
||||
_, ok := hs.sessions[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
// Query returns the value stored at the map
|
||||
func (hs *HistoryStore) Query(key string) []plugins.Message {
|
||||
hs.RLock()
|
||||
defer hs.RUnlock()
|
||||
return hs.sessions[key]
|
||||
}
|
||||
|
||||
// Append will add the slice of Mesages to the entry for the key.
|
||||
// If the map has not yet been initalised, then a new map is created.
|
||||
func (hs *HistoryStore) Append(key string, message ...plugins.Message) {
|
||||
hs.Lock()
|
||||
defer hs.Unlock()
|
||||
// In the unexpected case that the map has not yet been initalised, create it.
|
||||
if hs.sessions == nil {
|
||||
hs.sessions = make(map[string][]plugins.Message)
|
||||
}
|
||||
hs.sessions[key] = append(hs.sessions[key], message...)
|
||||
}
|
47
historystore/history_store_test.go
Normal file
47
historystore/history_store_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package historystore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mariocandela/beelzebub/v3/plugins"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewHistoryStore(t *testing.T) {
|
||||
hs := NewHistoryStore()
|
||||
assert.NotNil(t, hs)
|
||||
assert.NotNil(t, hs.sessions)
|
||||
}
|
||||
|
||||
func TestHasKey(t *testing.T) {
|
||||
hs := NewHistoryStore()
|
||||
hs.sessions["testKey"] = []plugins.Message{}
|
||||
assert.True(t, hs.HasKey("testKey"))
|
||||
assert.False(t, hs.HasKey("nonExistentKey"))
|
||||
}
|
||||
|
||||
func TestQuery(t *testing.T) {
|
||||
hs := NewHistoryStore()
|
||||
expectedMessages := []plugins.Message{{Role: "user", Content: "Hello"}}
|
||||
hs.sessions["testKey"] = expectedMessages
|
||||
actualMessages := hs.Query("testKey")
|
||||
assert.Equal(t, expectedMessages, actualMessages)
|
||||
}
|
||||
|
||||
func TestAppend(t *testing.T) {
|
||||
hs := NewHistoryStore()
|
||||
message1 := plugins.Message{Role: "user", Content: "Hello"}
|
||||
message2 := plugins.Message{Role: "assistant", Content: "Hi"}
|
||||
hs.Append("testKey", message1)
|
||||
assert.Equal(t, []plugins.Message{message1}, hs.sessions["testKey"])
|
||||
hs.Append("testKey", message2)
|
||||
assert.Equal(t, []plugins.Message{message1, message2}, hs.sessions["testKey"])
|
||||
}
|
||||
|
||||
func TestAppendNilSessions(t *testing.T) {
|
||||
hs := &HistoryStore{}
|
||||
message1 := plugins.Message{Role: "user", Content: "Hello"}
|
||||
hs.Append("testKey", message1)
|
||||
assert.NotNil(t, hs.sessions)
|
||||
assert.Equal(t, []plugins.Message{message1}, hs.sessions["testKey"])
|
||||
}
|
Reference in New Issue
Block a user