Allow to configure connectors

This commit is contained in:
mudler
2024-04-08 20:15:32 +02:00
parent 533caeee96
commit 66b1847644
6 changed files with 105 additions and 12 deletions

View File

@@ -7,19 +7,21 @@ import (
"path/filepath"
"time"
"github.com/mudler/local-agent-framework/example/webui/connector"
. "github.com/mudler/local-agent-framework/agent"
"github.com/mudler/local-agent-framework/external"
)
type ConnectorConfig struct {
Type string `json:"type"` // e.g. Slack
Config map[string]interface{} `json:"config"`
Type string `json:"type"` // e.g. Slack
Config string `json:"config"`
}
type ActionsConfig string
type AgentConfig struct {
Connector []ConnectorConfig `json:"connector" form:"connector" `
Connector []ConnectorConfig `json:"connectors" form:"connectors" `
Actions []ActionsConfig `json:"actions" form:"actions"`
// This is what needs to be part of ActionsConfig
Model string `json:"model" form:"model"`
@@ -111,7 +113,12 @@ func (a *AgentPool) List() []string {
return agents
}
var AvailableActions = []string{"search"}
const (
ConnectorTelegram = "telegram"
ActionSearch = "search"
)
var AvailableActions = []string{ActionSearch}
func (a *AgentConfig) availableActions() []Action {
actions := []Action{}
@@ -123,7 +130,7 @@ func (a *AgentConfig) availableActions() []Action {
for _, action := range a.Actions {
fmt.Println("Set Action", action)
switch action {
case "search":
case ActionSearch:
actions = append(actions, external.NewSearch(3))
}
}
@@ -131,6 +138,39 @@ func (a *AgentConfig) availableActions() []Action {
return actions
}
type Connector interface {
AgentResultCallback() func(state ActionState)
AgentReasoningCallback() func(state ActionCurrentState) bool
Start(a *Agent)
}
var AvailableConnectors = []string{ConnectorTelegram}
func (a *AgentConfig) availableConnectors() []Connector {
connectors := []Connector{}
for _, c := range a.Connector {
fmt.Println("Set Connector", c)
switch c.Type {
case ConnectorTelegram:
var config map[string]string
if err := json.Unmarshal([]byte(c.Config), &config); err != nil {
fmt.Println("Error unmarshalling connector config", err)
continue
}
fmt.Println("Config", config)
cc, err := connector.NewTelegramConnector(config)
if err != nil {
fmt.Println("Error creating telegram connector", err)
continue
}
connectors = append(connectors, cc)
}
}
return connectors
}
func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error {
manager := NewManager(5)
model := a.model
@@ -140,6 +180,9 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
if config.PeriodicRuns == "" {
config.PeriodicRuns = "10m"
}
connectors := config.availableConnectors()
fmt.Println("Creating agent", name)
fmt.Println("Model", model)
fmt.Println("API URL", a.apiURL)
@@ -165,6 +208,12 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
fmt.Sprintf(`Thinking: %s`, htmlIfy(state.Reasoning)),
).WithEvent("status"),
)
for _, c := range connectors {
if !c.AgentReasoningCallback()(state) {
return false
}
}
return true
}),
WithAgentResultCallback(func(state ActionState) {
@@ -185,6 +234,10 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
),
).WithEvent("status"),
)
for _, c := range connectors {
c.AgentResultCallback()(state)
}
}),
}
if config.HUD {
@@ -220,6 +273,10 @@ func (a *AgentPool) startAgentWithConfig(name string, config *AgentConfig) error
}
}()
for _, c := range connectors {
go c.Start(agent)
}
go func() {
for {
time.Sleep(1 * time.Second) // Send a message every seconds

View File

@@ -18,12 +18,40 @@
<input type="text" name="name" id="name" class="mt-1 focus:ring-blue-500 focus:border-blue-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md bg-gray-700 text-white" placeholder="Name">
</div>
<!--
<div class="mb-4">
<label for="connector" class="block text-sm font-medium text-gray-400">Connector Config (JSON)</label>
<textarea id="connector" name="connector" class="mt-1 focus:ring-blue-500 focus:border-blue-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md bg-gray-700 text-white" placeholder='[{"...":"..."}]'></textarea>
<div id="connectorsSection">
<div class="connector mb-4">
</div>
</div>
-->
<button type="button" id="addConnectorButton" class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-500 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
Add Connector
</button>
<script>
document.getElementById('addConnectorButton').addEventListener('click', function() {
const connectorsSection = document.getElementById('connectorsSection');
const newConnectorIndex = connectorsSection.getElementsByClassName('connector').length;
const newConnectorHTML = `
<div class="connector mb-4">
<div class="mb-4">
<label for="connectorType${newConnectorIndex}" class="block text-sm font-medium text-gray-400">Connector Type</label>
<select name="connectors[${newConnectorIndex}].type" id="connectorType${newConnectorIndex}" class="mt-1 focus:ring-blue-500 focus:border-blue-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md bg-gray-700 text-white">
{{ range .Connectors }}
<option value="{{.}}">{{.}}</option>
{{ end }}
</select>
</div>
<div class="mb-4">
<label for="connectorConfig${newConnectorIndex}" class="block text-sm font-medium text-gray-400">Connector Config (JSON)</label>
<textarea id="connectorConfig${newConnectorIndex}" name="connectors[${newConnectorIndex}].config" class="mt-1 focus:ring-blue-500 focus:border-blue-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md bg-gray-700 text-white" placeholder='{"token":"..."}'></textarea>
</div>
</div>
`;
connectorsSection.insertAdjacentHTML('beforeend', newConnectorHTML);
});
</script>
<div class="mb-4" id="action_box">
<label for="actions" class="block text-sm font-medium text-gray-400">Agent Actions</label>
<select style="display: none" name="actions" id="actions" class="mt-1 focus:ring-blue-500 focus:border-blue-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md bg-gray-700 text-white">

View File

@@ -88,8 +88,9 @@ func main() {
webapp.Get("/create", func(c *fiber.Ctx) error {
return c.Render("create.html", fiber.Map{
"Title": "Hello, World!",
"Actions": AvailableActions,
"Title": "Hello, World!",
"Actions": AvailableActions,
"Connectors": AvailableConnectors,
})
})