refactoring
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package sse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
package main
|
package state
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mudler/local-agent-framework/core/agent"
|
||||||
|
)
|
||||||
|
|
||||||
type ConnectorConfig struct {
|
type ConnectorConfig struct {
|
||||||
Type string `json:"type"` // e.g. Slack
|
Type string `json:"type"` // e.g. Slack
|
||||||
@@ -31,3 +35,9 @@ type AgentConfig struct {
|
|||||||
LongTermMemory bool `json:"long_term_memory" form:"long_term_memory"`
|
LongTermMemory bool `json:"long_term_memory" form:"long_term_memory"`
|
||||||
SummaryLongTermMemory bool `json:"summary_long_term_memory" form:"summary_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)
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -11,9 +11,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mudler/local-agent-framework/core/agent"
|
"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/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 {
|
type AgentPool struct {
|
||||||
@@ -22,28 +24,31 @@ type AgentPool struct {
|
|||||||
pooldir string
|
pooldir string
|
||||||
pool AgentPoolData
|
pool AgentPoolData
|
||||||
agents map[string]*Agent
|
agents map[string]*Agent
|
||||||
managers map[string]Manager
|
managers map[string]sse.Manager
|
||||||
agentStatus map[string]*Status
|
agentStatus map[string]*Status
|
||||||
agentMemory map[string]*InMemoryDatabase
|
agentMemory map[string]*InMemoryDatabase
|
||||||
apiURL, model string
|
apiURL, model string
|
||||||
ragDB RAGDB
|
ragDB RAGDB
|
||||||
|
availableActions func(*AgentConfig) func(ctx context.Context) []Action
|
||||||
|
connectors func(*AgentConfig) []Connector
|
||||||
|
timeout string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Status struct {
|
type Status struct {
|
||||||
results []ActionState
|
ActionResults []ActionState
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Status) addResult(result ActionState) {
|
func (s *Status) addResult(result ActionState) {
|
||||||
// If we have more than 10 results, remove the oldest one
|
// If we have more than 10 results, remove the oldest one
|
||||||
if len(s.results) > 10 {
|
if len(s.ActionResults) > 10 {
|
||||||
s.results = s.results[1:]
|
s.ActionResults = s.ActionResults[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
s.results = append(s.results, result)
|
s.ActionResults = append(s.ActionResults, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Status) Results() []ActionState {
|
func (s *Status) Results() []ActionState {
|
||||||
return s.results
|
return s.ActionResults
|
||||||
}
|
}
|
||||||
|
|
||||||
type AgentPoolData map[string]AgentConfig
|
type AgentPoolData map[string]AgentConfig
|
||||||
@@ -59,7 +64,13 @@ func loadPoolFromFile(path string) (*AgentPoolData, error) {
|
|||||||
return poolData, err
|
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 exists, try to load an existing pool.
|
||||||
// if file does not exist, create a new pool.
|
// if file does not exist, create a new pool.
|
||||||
|
|
||||||
@@ -76,8 +87,11 @@ func NewAgentPool(model, apiURL, directory string, RagDB RAGDB) (*AgentPool, err
|
|||||||
agents: make(map[string]*Agent),
|
agents: make(map[string]*Agent),
|
||||||
pool: make(map[string]AgentConfig),
|
pool: make(map[string]AgentConfig),
|
||||||
agentStatus: make(map[string]*Status),
|
agentStatus: make(map[string]*Status),
|
||||||
managers: make(map[string]Manager),
|
managers: make(map[string]sse.Manager),
|
||||||
agentMemory: make(map[string]*InMemoryDatabase),
|
agentMemory: make(map[string]*InMemoryDatabase),
|
||||||
|
connectors: connectors,
|
||||||
|
availableActions: availableActions,
|
||||||
|
timeout: timeout,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,10 +106,13 @@ func NewAgentPool(model, apiURL, directory string, RagDB RAGDB) (*AgentPool, err
|
|||||||
ragDB: RagDB,
|
ragDB: RagDB,
|
||||||
model: model,
|
model: model,
|
||||||
agents: make(map[string]*Agent),
|
agents: make(map[string]*Agent),
|
||||||
managers: make(map[string]Manager),
|
managers: make(map[string]sse.Manager),
|
||||||
agentStatus: map[string]*Status{},
|
agentStatus: map[string]*Status{},
|
||||||
agentMemory: map[string]*InMemoryDatabase{},
|
agentMemory: map[string]*InMemoryDatabase{},
|
||||||
pool: *poolData,
|
pool: *poolData,
|
||||||
|
connectors: connectors,
|
||||||
|
availableActions: availableActions,
|
||||||
|
timeout: timeout,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +150,7 @@ func (a *AgentPool) GetStatusHistory(name string) *Status {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error {
|
func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error {
|
||||||
manager := NewManager(5)
|
manager := sse.NewManager(5)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
model := a.model
|
model := a.model
|
||||||
if config.Model != "" {
|
if config.Model != "" {
|
||||||
@@ -143,9 +160,9 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
config.PeriodicRuns = "10m"
|
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)
|
stateFile, characterFile, knowledgeBase := a.stateFiles(name)
|
||||||
|
|
||||||
@@ -180,7 +197,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
WithContext(ctx),
|
WithContext(ctx),
|
||||||
WithPeriodicRuns(config.PeriodicRuns),
|
WithPeriodicRuns(config.PeriodicRuns),
|
||||||
WithPermanentGoal(config.PermanentGoal),
|
WithPermanentGoal(config.PermanentGoal),
|
||||||
WithCharacter(agent.Character{
|
WithCharacter(Character{
|
||||||
Name: name,
|
Name: name,
|
||||||
}),
|
}),
|
||||||
WithActions(
|
WithActions(
|
||||||
@@ -188,7 +205,7 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
),
|
),
|
||||||
WithStateFile(stateFile),
|
WithStateFile(stateFile),
|
||||||
WithCharacterFile(characterFile),
|
WithCharacterFile(characterFile),
|
||||||
WithTimeout(timeout),
|
WithTimeout(a.timeout),
|
||||||
WithRAGDB(agentDB),
|
WithRAGDB(agentDB),
|
||||||
WithAgentReasoningCallback(func(state ActionCurrentState) bool {
|
WithAgentReasoningCallback(func(state ActionCurrentState) bool {
|
||||||
xlog.Info(
|
xlog.Info(
|
||||||
@@ -200,8 +217,8 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
)
|
)
|
||||||
|
|
||||||
manager.Send(
|
manager.Send(
|
||||||
NewMessage(
|
sse.NewMessage(
|
||||||
fmt.Sprintf(`Thinking: %s`, htmlIfy(state.Reasoning)),
|
fmt.Sprintf(`Thinking: %s`, utils.HTMLify(state.Reasoning)),
|
||||||
).WithEvent("status"),
|
).WithEvent("status"),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -239,8 +256,8 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
state.ActionCurrentState.Params,
|
state.ActionCurrentState.Params,
|
||||||
state.Result)
|
state.Result)
|
||||||
manager.Send(
|
manager.Send(
|
||||||
NewMessage(
|
sse.NewMessage(
|
||||||
htmlIfy(
|
utils.HTMLify(
|
||||||
text,
|
text,
|
||||||
),
|
),
|
||||||
).WithEvent("status"),
|
).WithEvent("status"),
|
||||||
@@ -315,8 +332,8 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
|
|||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
time.Sleep(1 * time.Second) // Send a message every seconds
|
time.Sleep(1 * time.Second) // Send a message every seconds
|
||||||
manager.Send(NewMessage(
|
manager.Send(sse.NewMessage(
|
||||||
htmlIfy(agent.State().String()),
|
utils.HTMLify(agent.State().String()),
|
||||||
).WithEvent("hud"))
|
).WithEvent("hud"))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -415,6 +432,6 @@ func (a *AgentPool) GetConfig(name string) *AgentConfig {
|
|||||||
return &agent
|
return &agent
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AgentPool) GetManager(name string) Manager {
|
func (a *AgentPool) GetManager(name string) sse.Manager {
|
||||||
return a.managers[name]
|
return a.managers[name]
|
||||||
}
|
}
|
||||||
@@ -5,9 +5,10 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"github.com/mudler/local-agent-framework/core/action"
|
"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/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"
|
"github.com/mudler/local-agent-framework/services/actions"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -38,8 +39,9 @@ var AvailableActions = []string{
|
|||||||
ActionSendMail,
|
ActionSendMail,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AgentConfig) availableActions(ctx context.Context) []Action {
|
func Actions(a *state.AgentConfig) func(ctx context.Context) []agent.Action {
|
||||||
allActions := []Action{}
|
return func(ctx context.Context) []agent.Action {
|
||||||
|
allActions := []agent.Action{}
|
||||||
|
|
||||||
for _, a := range a.Actions {
|
for _, a := range a.Actions {
|
||||||
var config map[string]string
|
var config map[string]string
|
||||||
@@ -78,4 +80,5 @@ func (a *AgentConfig) availableActions(ctx context.Context) []Action {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return allActions
|
return allActions
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import (
|
|||||||
|
|
||||||
"github.com/mudler/local-agent-framework/pkg/xlog"
|
"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/donseba/go-htmx"
|
||||||
"github.com/dslipak/pdf"
|
"github.com/dslipak/pdf"
|
||||||
@@ -20,11 +22,11 @@ import (
|
|||||||
type (
|
type (
|
||||||
App struct {
|
App struct {
|
||||||
htmx *htmx.HTMX
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
db := pool.GetAgentMemory(c.Params("name"))
|
db := pool.GetAgentMemory(c.Params("name"))
|
||||||
db.Reset()
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
db := pool.GetAgentMemory(c.Params("name"))
|
db := pool.GetAgentMemory(c.Params("name"))
|
||||||
knowledgeBase := db.Data()
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
file, err := c.FormFile("file")
|
file, err := c.FormFile("file")
|
||||||
if err != nil {
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
agent := pool.GetAgent(c.Params("name"))
|
agent := pool.GetAgent(c.Params("name"))
|
||||||
db := agent.Memory()
|
db := agent.Memory()
|
||||||
@@ -127,7 +129,7 @@ func (a *App) KnowledgeBaseFile(pool *AgentPool) func(c *fiber.Ctx) error {
|
|||||||
chunkSize = payload.ChunkSize
|
chunkSize = payload.ChunkSize
|
||||||
}
|
}
|
||||||
|
|
||||||
go StringsToKB(db, chunkSize, content)
|
go state.StringsToKB(db, chunkSize, content)
|
||||||
|
|
||||||
_, err = c.WriteString(chatDiv("File uploaded", "gray"))
|
_, 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 {
|
return func(c *fiber.Ctx) error {
|
||||||
agent := pool.GetAgent(c.Params("name"))
|
agent := pool.GetAgent(c.Params("name"))
|
||||||
db := agent.Memory()
|
db := agent.Memory()
|
||||||
@@ -158,13 +160,13 @@ func (a *App) KnowledgeBase(pool *AgentPool) func(c *fiber.Ctx) error {
|
|||||||
chunkSize = payload.ChunkSize
|
chunkSize = payload.ChunkSize
|
||||||
}
|
}
|
||||||
|
|
||||||
go WebsiteToKB(website, chunkSize, db)
|
go state.WebsiteToKB(website, chunkSize, db)
|
||||||
|
|
||||||
return c.Redirect("/knowledgebase/" + c.Params("name"))
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
payload := struct {
|
payload := struct {
|
||||||
Message string `form:"message"`
|
Message string `form:"message"`
|
||||||
@@ -180,9 +182,9 @@ func (a *App) Notify(pool *AgentPool) func(c *fiber.Ctx) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
agent := pool.GetAgent(c.Params("name"))
|
a := pool.GetAgent(c.Params("name"))
|
||||||
agent.Ask(
|
a.Ask(
|
||||||
WithText(query),
|
agent.WithText(query),
|
||||||
)
|
)
|
||||||
_, _ = c.Write([]byte("Message sent"))
|
_, _ = 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 {
|
return func(c *fiber.Ctx) error {
|
||||||
if err := pool.Remove(c.Params("name")); err != nil {
|
if err := pool.Remove(c.Params("name")); err != nil {
|
||||||
xlog.Info("Error removing agent", err)
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
xlog.Info("Pausing agent", c.Params("name"))
|
xlog.Info("Pausing agent", c.Params("name"))
|
||||||
agent := pool.GetAgent(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 {
|
return func(c *fiber.Ctx) error {
|
||||||
agent := pool.GetAgent(c.Params("name"))
|
agent := pool.GetAgent(c.Params("name"))
|
||||||
if agent != nil {
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
config := AgentConfig{}
|
config := state.AgentConfig{}
|
||||||
if err := c.BodyParser(&config); err != nil {
|
if err := c.BodyParser(&config); err != nil {
|
||||||
return err
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
agent := pool.GetConfig(c.Params("name"))
|
agent := pool.GetConfig(c.Params("name"))
|
||||||
if agent == nil {
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
file, err := c.FormFile("file")
|
file, err := c.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -275,7 +277,7 @@ func (a *App) ImportAgent(pool *AgentPool) func(c *fiber.Ctx) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
config := AgentConfig{}
|
config := state.AgentConfig{}
|
||||||
if err := json.Unmarshal(data, &config); err != nil {
|
if err := json.Unmarshal(data, &config); err != nil {
|
||||||
return err
|
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 {
|
return func(c *fiber.Ctx) error {
|
||||||
payload := struct {
|
payload := struct {
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
@@ -313,18 +315,18 @@ func (a *App) Chat(pool *AgentPool) func(c *fiber.Ctx) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
manager.Send(
|
manager.Send(
|
||||||
NewMessage(
|
sse.NewMessage(
|
||||||
chatDiv(query, "gray"),
|
chatDiv(query, "gray"),
|
||||||
).WithEvent("messages"))
|
).WithEvent("messages"))
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
agent := pool.GetAgent(agentName)
|
a := pool.GetAgent(agentName)
|
||||||
if agent == nil {
|
if a == nil {
|
||||||
xlog.Info("Agent not found in pool", c.Params("name"))
|
xlog.Info("Agent not found in pool", c.Params("name"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res := agent.Ask(
|
res := a.Ask(
|
||||||
WithText(query),
|
agent.WithText(query),
|
||||||
)
|
)
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
xlog.Error("Error asking agent", "agent", agentName, "error", res.Error)
|
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)
|
xlog.Info("we got a response from the agent", "agent", agentName, "response", res.Response)
|
||||||
}
|
}
|
||||||
manager.Send(
|
manager.Send(
|
||||||
NewMessage(
|
sse.NewMessage(
|
||||||
chatDiv(res.Response, "blue"),
|
chatDiv(res.Response, "blue"),
|
||||||
).WithEvent("messages"))
|
).WithEvent("messages"))
|
||||||
manager.Send(
|
manager.Send(
|
||||||
NewMessage(
|
sse.NewMessage(
|
||||||
disabledElement("inputMessage", false), // show again the input
|
disabledElement("inputMessage", false), // show again the input
|
||||||
).WithEvent("message_status"))
|
).WithEvent("message_status"))
|
||||||
|
|
||||||
@@ -345,7 +347,7 @@ func (a *App) Chat(pool *AgentPool) func(c *fiber.Ctx) error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
manager.Send(
|
manager.Send(
|
||||||
NewMessage(
|
sse.NewMessage(
|
||||||
loader() + disabledElement("inputMessage", true),
|
loader() + disabledElement("inputMessage", true),
|
||||||
).WithEvent("message_status"))
|
).WithEvent("message_status"))
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/mudler/local-agent-framework/pkg/xlog"
|
"github.com/mudler/local-agent-framework/pkg/xlog"
|
||||||
"github.com/mudler/local-agent-framework/services/connectors"
|
"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 (
|
const (
|
||||||
@@ -18,12 +18,6 @@ const (
|
|||||||
ConnectorGithubPRs = "github-prs"
|
ConnectorGithubPRs = "github-prs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Connector interface {
|
|
||||||
AgentResultCallback() func(state ActionState)
|
|
||||||
AgentReasoningCallback() func(state ActionCurrentState) bool
|
|
||||||
Start(a *Agent)
|
|
||||||
}
|
|
||||||
|
|
||||||
var AvailableConnectors = []string{
|
var AvailableConnectors = []string{
|
||||||
ConnectorTelegram,
|
ConnectorTelegram,
|
||||||
ConnectorSlack,
|
ConnectorSlack,
|
||||||
@@ -32,8 +26,8 @@ var AvailableConnectors = []string{
|
|||||||
ConnectorGithubPRs,
|
ConnectorGithubPRs,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AgentConfig) availableConnectors() []Connector {
|
func Connectors(a *state.AgentConfig) []state.Connector {
|
||||||
conns := []Connector{}
|
conns := []state.Connector{}
|
||||||
|
|
||||||
for _, c := range a.Connector {
|
for _, c := range a.Connector {
|
||||||
var config map[string]string
|
var config map[string]string
|
||||||
|
|||||||
@@ -10,9 +10,10 @@ import (
|
|||||||
fiber "github.com/gofiber/fiber/v2"
|
fiber "github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/template/html/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"
|
||||||
"github.com/mudler/local-agent-framework/pkg/llm/rag"
|
rag "github.com/mudler/local-agent-framework/pkg/vectorstore"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testModel = os.Getenv("TEST_MODEL")
|
var testModel = os.Getenv("TEST_MODEL")
|
||||||
@@ -52,7 +53,7 @@ func main() {
|
|||||||
stateDir := cwd + "/pool"
|
stateDir := cwd + "/pool"
|
||||||
os.MkdirAll(stateDir, 0755)
|
os.MkdirAll(stateDir, 0755)
|
||||||
|
|
||||||
var ragDB RAGDB
|
var ragDB agent.RAGDB
|
||||||
lai := llm.NewClient(apiKey, apiURL+"/v1", timeout)
|
lai := llm.NewClient(apiKey, apiURL+"/v1", timeout)
|
||||||
|
|
||||||
switch vectorStore {
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,9 +7,11 @@ import (
|
|||||||
fiber "github.com/gofiber/fiber/v2"
|
fiber "github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/filesystem"
|
"github.com/gofiber/fiber/v2/middleware/filesystem"
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
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{
|
webapp.Use("/public", filesystem.New(filesystem.Config{
|
||||||
Root: http.FS(embeddedFiles),
|
Root: http.FS(embeddedFiles),
|
||||||
@@ -59,14 +61,14 @@ func RegisterRoutes(webapp *fiber.App, pool *AgentPool, app *App) {
|
|||||||
return c.SendStatus(404)
|
return c.SendStatus(404)
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Handle(c, NewClient(randStringRunes(10)))
|
m.Handle(c, sse.NewClient(randStringRunes(10)))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
webapp.Get("/status/:name", func(c *fiber.Ctx) error {
|
webapp.Get("/status/:name", func(c *fiber.Ctx) error {
|
||||||
history := pool.GetStatusHistory(c.Params("name"))
|
history := pool.GetStatusHistory(c.Params("name"))
|
||||||
if history == nil {
|
if history == nil {
|
||||||
history = &Status{results: []agent.ActionState{}}
|
history = &state.Status{ActionResults: []agent.ActionState{}}
|
||||||
}
|
}
|
||||||
// reverse history
|
// reverse history
|
||||||
|
|
||||||
|
|||||||
9
pkg/utils/html.go
Normal file
9
pkg/utils/html.go
Normal 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
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package rag
|
package vectorstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package rag
|
package vectorstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package rag
|
package vectorstore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
Reference in New Issue
Block a user