allow to set configs for actions too

This commit is contained in:
Ettore Di Giacinto
2024-04-08 22:58:53 +02:00
parent 66b1847644
commit cf3c4549da
3 changed files with 61 additions and 25 deletions

View File

@@ -18,7 +18,10 @@ type ConnectorConfig struct {
Config string `json:"config"` Config string `json:"config"`
} }
type ActionsConfig string type ActionsConfig struct {
Name string `json:"name"` // e.g. search
Config string `json:"config"`
}
type AgentConfig struct { type AgentConfig struct {
Connector []ConnectorConfig `json:"connectors" form:"connectors" ` Connector []ConnectorConfig `json:"connectors" form:"connectors" `
@@ -123,15 +126,19 @@ var AvailableActions = []string{ActionSearch}
func (a *AgentConfig) availableActions() []Action { func (a *AgentConfig) availableActions() []Action {
actions := []Action{} actions := []Action{}
if len(a.Actions) == 0 {
// Return search as default
return []Action{external.NewSearch(3)}
}
for _, action := range a.Actions { for _, action := range a.Actions {
fmt.Println("Set Action", action) fmt.Println("Set Action", action)
switch action {
var config map[string]string
if err := json.Unmarshal([]byte(action.Config), &config); err != nil {
fmt.Println("Error unmarshalling connector config", err)
continue
}
fmt.Println("Config", config)
switch action.Name {
case ActionSearch: case ActionSearch:
actions = append(actions, external.NewSearch(3)) actions = append(actions, external.NewSearch(config))
} }
} }
@@ -151,14 +158,16 @@ func (a *AgentConfig) availableConnectors() []Connector {
for _, c := range a.Connector { for _, c := range a.Connector {
fmt.Println("Set Connector", c) fmt.Println("Set Connector", c)
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)
switch c.Type { switch c.Type {
case ConnectorTelegram: 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) cc, err := connector.NewTelegramConnector(config)
if err != nil { if err != nil {
fmt.Println("Error creating telegram connector", err) fmt.Println("Error creating telegram connector", err)

View File

@@ -53,15 +53,33 @@
</script> </script>
<div class="mb-4" id="action_box"> <div class="mb-4" id="action_box">
<label for="actions" class="block text-sm font-medium text-gray-400">Agent Actions</label> <div class="action mb-4">
<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"> </div>
{{ range .Actions }}
<option value="{{.}}">{{.}}</option>
{{ end }}
</select>
</div> </div>
<button type="button" 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" hx-on:click="let actionbox=document.getElementById('actions'); let clone=actionbox.cloneNode(true); clone.style.display ='block'; actionbox.after(clone)">Add action</button> <button id="action_button" type="button" 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 action</button>
<script>
document.getElementById('action_button').addEventListener('click', function() {
const actionsSection = document.getElementById('action_box');
const ii = actionsSection.getElementsByClassName('connector').length;
const newConnectorHTML = `
<div class="action mb-4">
<label for="actionsName${ii}" class="block text-sm font-medium text-gray-400">Connector Type</label>
<select name="actions[${ii}].name" id="actionsName${ii}" 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 .Actions }}
<option value="{{.}}">{{.}}</option>
{{ end }}
</select>
<div class="mb-4">
<label for="actionsConfig${ii}" class="block text-sm font-medium text-gray-400">Connector Config (JSON)</label>
<textarea id="actionsConfig${ii}" name="actions[${ii}].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>
`;
actionsSection.insertAdjacentHTML('beforeend', newConnectorHTML);
});
</script>
<div class="mb-4"> <div class="mb-4">

17
external/search.go vendored
View File

@@ -8,11 +8,20 @@ import (
"github.com/sashabaranov/go-openai/jsonschema" "github.com/sashabaranov/go-openai/jsonschema"
) )
func NewSearch(results int) *SearchAction { func NewSearch(config map[string]string) *SearchAction {
if results == 0 { results := config["results"]
results = 3 intResult := 1
// decode int from string
if results != "" {
_, err := fmt.Sscanf(results, "%d", &intResult)
if err != nil {
fmt.Printf("error: %v", err)
}
} }
return &SearchAction{results: results}
fmt.Println("Search action with results: ", intResult)
return &SearchAction{results: intResult}
} }
type SearchAction struct{ results int } type SearchAction struct{ results int }