refactoring

This commit is contained in:
Ettore Di Giacinto
2025-02-25 23:17:28 +01:00
parent 296734ba3b
commit 0139b79835
13 changed files with 177 additions and 139 deletions

View File

@@ -1,4 +1,4 @@
package main
package sse
import (
"bufio"

View File

@@ -1,4 +1,8 @@
package main
package state
import (
"github.com/mudler/local-agent-framework/core/agent"
)
type ConnectorConfig struct {
Type string `json:"type"` // e.g. Slack
@@ -31,3 +35,9 @@ type AgentConfig struct {
LongTermMemory bool `json:"long_term_memory" form:"long_term_memory"`
SummaryLongTermMemory bool `json:"summary_long_term_memory" form:"summary_long_term_memory"`
}
type Connector interface {
AgentResultCallback() func(state agent.ActionState)
AgentReasoningCallback() func(state agent.ActionCurrentState) bool
Start(a *agent.Agent)
}

View File

@@ -1,4 +1,4 @@
package main
package state
import (
"encoding/json"

View File

@@ -1,4 +1,4 @@
package main
package state
import (
"context"
@@ -11,39 +11,44 @@ import (
"time"
"github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/pkg/xlog"
. "github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/core/sse"
"github.com/mudler/local-agent-framework/pkg/utils"
"github.com/mudler/local-agent-framework/pkg/xlog"
)
type AgentPool struct {
sync.Mutex
file string
pooldir string
pool AgentPoolData
agents map[string]*Agent
managers map[string]Manager
agentStatus map[string]*Status
agentMemory map[string]*InMemoryDatabase
apiURL, model string
ragDB RAGDB
file string
pooldir string
pool AgentPoolData
agents map[string]*Agent
managers map[string]sse.Manager
agentStatus map[string]*Status
agentMemory map[string]*InMemoryDatabase
apiURL, model string
ragDB RAGDB
availableActions func(*AgentConfig) func(ctx context.Context) []Action
connectors func(*AgentConfig) []Connector
timeout string
}
type Status struct {
results []ActionState
ActionResults []ActionState
}
func (s *Status) addResult(result ActionState) {
// If we have more than 10 results, remove the oldest one
if len(s.results) > 10 {
s.results = s.results[1:]
if len(s.ActionResults) > 10 {
s.ActionResults = s.ActionResults[1:]
}
s.results = append(s.results, result)
s.ActionResults = append(s.ActionResults, result)
}
func (s *Status) Results() []ActionState {
return s.results
return s.ActionResults
}
type AgentPoolData map[string]AgentConfig
@@ -59,7 +64,13 @@ func loadPoolFromFile(path string) (*AgentPoolData, error) {
return poolData, err
}
func NewAgentPool(model, apiURL, directory string, RagDB RAGDB) (*AgentPool, error) {
func NewAgentPool(
model, apiURL, directory string,
RagDB RAGDB,
availableActions func(*AgentConfig) func(ctx context.Context) []agent.Action,
connectors func(*AgentConfig) []Connector,
timeout string,
) (*AgentPool, error) {
// if file exists, try to load an existing pool.
// if file does not exist, create a new pool.
@@ -68,16 +79,19 @@ func NewAgentPool(model, apiURL, directory string, RagDB RAGDB) (*AgentPool, err
if _, err := os.Stat(poolfile); err != nil {
// file does not exist, create a new pool
return &AgentPool{
file: poolfile,
pooldir: directory,
apiURL: apiURL,
model: model,
ragDB: RagDB,
agents: make(map[string]*Agent),
pool: make(map[string]AgentConfig),
agentStatus: make(map[string]*Status),
managers: make(map[string]Manager),
agentMemory: make(map[string]*InMemoryDatabase),
file: poolfile,
pooldir: directory,
apiURL: apiURL,
model: model,
ragDB: RagDB,
agents: make(map[string]*Agent),
pool: make(map[string]AgentConfig),
agentStatus: make(map[string]*Status),
managers: make(map[string]sse.Manager),
agentMemory: make(map[string]*InMemoryDatabase),
connectors: connectors,
availableActions: availableActions,
timeout: timeout,
}, nil
}
@@ -86,16 +100,19 @@ func NewAgentPool(model, apiURL, directory string, RagDB RAGDB) (*AgentPool, err
return nil, err
}
return &AgentPool{
file: poolfile,
apiURL: apiURL,
pooldir: directory,
ragDB: RagDB,
model: model,
agents: make(map[string]*Agent),
managers: make(map[string]Manager),
agentStatus: map[string]*Status{},
agentMemory: map[string]*InMemoryDatabase{},
pool: *poolData,
file: poolfile,
apiURL: apiURL,
pooldir: directory,
ragDB: RagDB,
model: model,
agents: make(map[string]*Agent),
managers: make(map[string]sse.Manager),
agentStatus: map[string]*Status{},
agentMemory: map[string]*InMemoryDatabase{},
pool: *poolData,
connectors: connectors,
availableActions: availableActions,
timeout: timeout,
}, nil
}
@@ -133,7 +150,7 @@ func (a *AgentPool) GetStatusHistory(name string) *Status {
}
func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error {
manager := NewManager(5)
manager := sse.NewManager(5)
ctx := context.Background()
model := a.model
if config.Model != "" {
@@ -143,9 +160,9 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
config.PeriodicRuns = "10m"
}
connectors := config.availableConnectors()
connectors := a.connectors(config)
actions := config.availableActions(ctx)
actions := a.availableActions(config)(ctx)
stateFile, characterFile, knowledgeBase := a.stateFiles(name)
@@ -180,7 +197,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
WithContext(ctx),
WithPeriodicRuns(config.PeriodicRuns),
WithPermanentGoal(config.PermanentGoal),
WithCharacter(agent.Character{
WithCharacter(Character{
Name: name,
}),
WithActions(
@@ -188,7 +205,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
),
WithStateFile(stateFile),
WithCharacterFile(characterFile),
WithTimeout(timeout),
WithTimeout(a.timeout),
WithRAGDB(agentDB),
WithAgentReasoningCallback(func(state ActionCurrentState) bool {
xlog.Info(
@@ -200,8 +217,8 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
)
manager.Send(
NewMessage(
fmt.Sprintf(`Thinking: %s`, htmlIfy(state.Reasoning)),
sse.NewMessage(
fmt.Sprintf(`Thinking: %s`, utils.HTMLify(state.Reasoning)),
).WithEvent("status"),
)
@@ -239,8 +256,8 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
state.ActionCurrentState.Params,
state.Result)
manager.Send(
NewMessage(
htmlIfy(
sse.NewMessage(
utils.HTMLify(
text,
),
).WithEvent("status"),
@@ -315,8 +332,8 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
go func() {
for {
time.Sleep(1 * time.Second) // Send a message every seconds
manager.Send(NewMessage(
htmlIfy(agent.State().String()),
manager.Send(sse.NewMessage(
utils.HTMLify(agent.State().String()),
).WithEvent("hud"))
}
}()
@@ -415,6 +432,6 @@ func (a *AgentPool) GetConfig(name string) *AgentConfig {
return &agent
}
func (a *AgentPool) GetManager(name string) Manager {
func (a *AgentPool) GetManager(name string) sse.Manager {
return a.managers[name]
}

View File

@@ -5,9 +5,10 @@ import (
"encoding/json"
"github.com/mudler/local-agent-framework/core/action"
"github.com/mudler/local-agent-framework/core/state"
"github.com/mudler/local-agent-framework/pkg/xlog"
. "github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/services/actions"
)
@@ -38,44 +39,46 @@ var AvailableActions = []string{
ActionSendMail,
}
func (a *AgentConfig) availableActions(ctx context.Context) []Action {
allActions := []Action{}
func Actions(a *state.AgentConfig) func(ctx context.Context) []agent.Action {
return func(ctx context.Context) []agent.Action {
allActions := []agent.Action{}
for _, a := range a.Actions {
var config map[string]string
if err := json.Unmarshal([]byte(a.Config), &config); err != nil {
xlog.Error("Error unmarshalling action config", "error", err)
continue
}
switch a.Name {
case ActionCustom:
customAction, err := action.NewCustom(config, "")
if err != nil {
xlog.Error("Error creating custom action", "error", err)
for _, a := range a.Actions {
var config map[string]string
if err := json.Unmarshal([]byte(a.Config), &config); err != nil {
xlog.Error("Error unmarshalling action config", "error", err)
continue
}
allActions = append(allActions, customAction)
case ActionSearch:
allActions = append(allActions, actions.NewSearch(config))
case ActionGithubIssueLabeler:
allActions = append(allActions, actions.NewGithubIssueLabeler(ctx, config))
case ActionGithubIssueOpener:
allActions = append(allActions, actions.NewGithubIssueOpener(ctx, config))
case ActionGithubIssueCloser:
allActions = append(allActions, actions.NewGithubIssueCloser(ctx, config))
case ActionGithubIssueSearcher:
allActions = append(allActions, actions.NewGithubIssueSearch(ctx, config))
case ActionScraper:
allActions = append(allActions, actions.NewScraper(config))
case ActionWikipedia:
allActions = append(allActions, actions.NewWikipedia(config))
case ActionBrowse:
allActions = append(allActions, actions.NewBrowse(config))
case ActionSendMail:
allActions = append(allActions, actions.NewSendMail(config))
}
}
return allActions
switch a.Name {
case ActionCustom:
customAction, err := action.NewCustom(config, "")
if err != nil {
xlog.Error("Error creating custom action", "error", err)
continue
}
allActions = append(allActions, customAction)
case ActionSearch:
allActions = append(allActions, actions.NewSearch(config))
case ActionGithubIssueLabeler:
allActions = append(allActions, actions.NewGithubIssueLabeler(ctx, config))
case ActionGithubIssueOpener:
allActions = append(allActions, actions.NewGithubIssueOpener(ctx, config))
case ActionGithubIssueCloser:
allActions = append(allActions, actions.NewGithubIssueCloser(ctx, config))
case ActionGithubIssueSearcher:
allActions = append(allActions, actions.NewGithubIssueSearch(ctx, config))
case ActionScraper:
allActions = append(allActions, actions.NewScraper(config))
case ActionWikipedia:
allActions = append(allActions, actions.NewWikipedia(config))
case ActionBrowse:
allActions = append(allActions, actions.NewBrowse(config))
case ActionSendMail:
allActions = append(allActions, actions.NewSendMail(config))
}
}
return allActions
}
}

View File

@@ -10,7 +10,9 @@ import (
"github.com/mudler/local-agent-framework/pkg/xlog"
. "github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/core/sse"
"github.com/mudler/local-agent-framework/core/state"
"github.com/donseba/go-htmx"
"github.com/dslipak/pdf"
@@ -20,11 +22,11 @@ import (
type (
App struct {
htmx *htmx.HTMX
pool *AgentPool
pool *state.AgentPool
}
)
func (a *App) KnowledgeBaseReset(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) KnowledgeBaseReset(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
db := pool.GetAgentMemory(c.Params("name"))
db.Reset()
@@ -32,7 +34,7 @@ func (a *App) KnowledgeBaseReset(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) KnowledgeBaseExport(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) KnowledgeBaseExport(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
db := pool.GetAgentMemory(c.Params("name"))
knowledgeBase := db.Data()
@@ -42,7 +44,7 @@ func (a *App) KnowledgeBaseExport(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) KnowledgeBaseImport(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) KnowledgeBaseImport(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
file, err := c.FormFile("file")
if err != nil {
@@ -85,7 +87,7 @@ func (a *App) KnowledgeBaseImport(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) KnowledgeBaseFile(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) KnowledgeBaseFile(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
agent := pool.GetAgent(c.Params("name"))
db := agent.Memory()
@@ -127,7 +129,7 @@ func (a *App) KnowledgeBaseFile(pool *AgentPool) func(c *fiber.Ctx) error {
chunkSize = payload.ChunkSize
}
go StringsToKB(db, chunkSize, content)
go state.StringsToKB(db, chunkSize, content)
_, err = c.WriteString(chatDiv("File uploaded", "gray"))
@@ -135,7 +137,7 @@ func (a *App) KnowledgeBaseFile(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) KnowledgeBase(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) KnowledgeBase(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
agent := pool.GetAgent(c.Params("name"))
db := agent.Memory()
@@ -158,13 +160,13 @@ func (a *App) KnowledgeBase(pool *AgentPool) func(c *fiber.Ctx) error {
chunkSize = payload.ChunkSize
}
go WebsiteToKB(website, chunkSize, db)
go state.WebsiteToKB(website, chunkSize, db)
return c.Redirect("/knowledgebase/" + c.Params("name"))
}
}
func (a *App) Notify(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) Notify(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
payload := struct {
Message string `form:"message"`
@@ -180,9 +182,9 @@ func (a *App) Notify(pool *AgentPool) func(c *fiber.Ctx) error {
return nil
}
agent := pool.GetAgent(c.Params("name"))
agent.Ask(
WithText(query),
a := pool.GetAgent(c.Params("name"))
a.Ask(
agent.WithText(query),
)
_, _ = c.Write([]byte("Message sent"))
@@ -190,7 +192,7 @@ func (a *App) Notify(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) Delete(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) Delete(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
if err := pool.Remove(c.Params("name")); err != nil {
xlog.Info("Error removing agent", err)
@@ -200,7 +202,7 @@ func (a *App) Delete(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) Pause(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) Pause(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
xlog.Info("Pausing agent", c.Params("name"))
agent := pool.GetAgent(c.Params("name"))
@@ -211,7 +213,7 @@ func (a *App) Pause(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) Start(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) Start(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
agent := pool.GetAgent(c.Params("name"))
if agent != nil {
@@ -221,9 +223,9 @@ func (a *App) Start(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) Create(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) Create(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
config := AgentConfig{}
config := state.AgentConfig{}
if err := c.BodyParser(&config); err != nil {
return err
}
@@ -242,7 +244,7 @@ func (a *App) Create(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) ExportAgent(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) ExportAgent(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
agent := pool.GetConfig(c.Params("name"))
if agent == nil {
@@ -254,7 +256,7 @@ func (a *App) ExportAgent(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) ImportAgent(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) ImportAgent(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
file, err := c.FormFile("file")
if err != nil {
@@ -275,7 +277,7 @@ func (a *App) ImportAgent(pool *AgentPool) func(c *fiber.Ctx) error {
return err
}
config := AgentConfig{}
config := state.AgentConfig{}
if err := json.Unmarshal(data, &config); err != nil {
return err
}
@@ -295,7 +297,7 @@ func (a *App) ImportAgent(pool *AgentPool) func(c *fiber.Ctx) error {
}
}
func (a *App) Chat(pool *AgentPool) func(c *fiber.Ctx) error {
func (a *App) Chat(pool *state.AgentPool) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
payload := struct {
Message string `json:"message"`
@@ -313,18 +315,18 @@ func (a *App) Chat(pool *AgentPool) func(c *fiber.Ctx) error {
return nil
}
manager.Send(
NewMessage(
sse.NewMessage(
chatDiv(query, "gray"),
).WithEvent("messages"))
go func() {
agent := pool.GetAgent(agentName)
if agent == nil {
a := pool.GetAgent(agentName)
if a == nil {
xlog.Info("Agent not found in pool", c.Params("name"))
return
}
res := agent.Ask(
WithText(query),
res := a.Ask(
agent.WithText(query),
)
if res.Error != nil {
xlog.Error("Error asking agent", "agent", agentName, "error", res.Error)
@@ -332,11 +334,11 @@ func (a *App) Chat(pool *AgentPool) func(c *fiber.Ctx) error {
xlog.Info("we got a response from the agent", "agent", agentName, "response", res.Response)
}
manager.Send(
NewMessage(
sse.NewMessage(
chatDiv(res.Response, "blue"),
).WithEvent("messages"))
manager.Send(
NewMessage(
sse.NewMessage(
disabledElement("inputMessage", false), // show again the input
).WithEvent("message_status"))
@@ -345,7 +347,7 @@ func (a *App) Chat(pool *AgentPool) func(c *fiber.Ctx) error {
}()
manager.Send(
NewMessage(
sse.NewMessage(
loader() + disabledElement("inputMessage", true),
).WithEvent("message_status"))

View File

@@ -6,7 +6,7 @@ import (
"github.com/mudler/local-agent-framework/pkg/xlog"
"github.com/mudler/local-agent-framework/services/connectors"
. "github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/core/state"
)
const (
@@ -18,12 +18,6 @@ const (
ConnectorGithubPRs = "github-prs"
)
type Connector interface {
AgentResultCallback() func(state ActionState)
AgentReasoningCallback() func(state ActionCurrentState) bool
Start(a *Agent)
}
var AvailableConnectors = []string{
ConnectorTelegram,
ConnectorSlack,
@@ -32,8 +26,8 @@ var AvailableConnectors = []string{
ConnectorGithubPRs,
}
func (a *AgentConfig) availableConnectors() []Connector {
conns := []Connector{}
func Connectors(a *state.AgentConfig) []state.Connector {
conns := []state.Connector{}
for _, c := range a.Connector {
var config map[string]string

View File

@@ -10,9 +10,10 @@ import (
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
. "github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/core/state"
"github.com/mudler/local-agent-framework/pkg/llm"
"github.com/mudler/local-agent-framework/pkg/llm/rag"
rag "github.com/mudler/local-agent-framework/pkg/vectorstore"
)
var testModel = os.Getenv("TEST_MODEL")
@@ -52,7 +53,7 @@ func main() {
stateDir := cwd + "/pool"
os.MkdirAll(stateDir, 0755)
var ragDB RAGDB
var ragDB agent.RAGDB
lai := llm.NewClient(apiKey, apiURL+"/v1", timeout)
switch vectorStore {
@@ -67,7 +68,7 @@ func main() {
}
}
pool, err := NewAgentPool(testModel, apiURL, stateDir, ragDB)
pool, err := state.NewAgentPool(testModel, apiURL, stateDir, ragDB, Actions, Connectors, timeout)
if err != nil {
panic(err)
}

View File

@@ -7,9 +7,11 @@ import (
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/mudler/local-agent-framework/core/agent"
"github.com/mudler/local-agent-framework/core/sse"
"github.com/mudler/local-agent-framework/core/state"
)
func RegisterRoutes(webapp *fiber.App, pool *AgentPool, app *App) {
func RegisterRoutes(webapp *fiber.App, pool *state.AgentPool, app *App) {
webapp.Use("/public", filesystem.New(filesystem.Config{
Root: http.FS(embeddedFiles),
@@ -59,14 +61,14 @@ func RegisterRoutes(webapp *fiber.App, pool *AgentPool, app *App) {
return c.SendStatus(404)
}
m.Handle(c, NewClient(randStringRunes(10)))
m.Handle(c, sse.NewClient(randStringRunes(10)))
return nil
})
webapp.Get("/status/:name", func(c *fiber.Ctx) error {
history := pool.GetStatusHistory(c.Params("name"))
if history == nil {
history = &Status{results: []agent.ActionState{}}
history = &state.Status{ActionResults: []agent.ActionState{}}
}
// reverse history

9
pkg/utils/html.go Normal file
View File

@@ -0,0 +1,9 @@
package utils
import "strings"
func HTMLify(s string) string {
s = strings.TrimSpace(s)
s = strings.ReplaceAll(s, "\n", "<br>")
return s
}

View File

@@ -1,4 +1,4 @@
package rag
package vectorstore
import (
"context"

View File

@@ -1,4 +1,4 @@
package rag
package vectorstore
import (
"context"

View File

@@ -1,4 +1,4 @@
package rag
package vectorstore
import (
"bytes"