Hook to the webui
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/mudler/local-agent-framework/action"
|
||||
"github.com/mudler/local-agent-framework/xlog"
|
||||
|
||||
. "github.com/mudler/local-agent-framework/agent"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
const (
|
||||
// Actions
|
||||
ActionSearch = "search"
|
||||
ActionCustom = "custom"
|
||||
ActionGithubIssueLabeler = "github-issue-labeler"
|
||||
ActionGithubIssueOpener = "github-issue-opener"
|
||||
ActionGithubIssueCloser = "github-issue-closer"
|
||||
@@ -25,6 +27,7 @@ const (
|
||||
|
||||
var AvailableActions = []string{
|
||||
ActionSearch,
|
||||
ActionCustom,
|
||||
ActionGithubIssueLabeler,
|
||||
ActionGithubIssueOpener,
|
||||
ActionGithubIssueCloser,
|
||||
@@ -38,14 +41,21 @@ var AvailableActions = []string{
|
||||
func (a *AgentConfig) availableActions(ctx context.Context) []Action {
|
||||
actions := []Action{}
|
||||
|
||||
for _, action := range a.Actions {
|
||||
for _, a := range a.Actions {
|
||||
var config map[string]string
|
||||
if err := json.Unmarshal([]byte(action.Config), &config); err != nil {
|
||||
xlog.Info("Error unmarshalling action config", "error", err)
|
||||
if err := json.Unmarshal([]byte(a.Config), &config); err != nil {
|
||||
xlog.Error("Error unmarshalling action config", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
switch action.Name {
|
||||
switch a.Name {
|
||||
case ActionCustom:
|
||||
customAction, err := action.NewCustom(config, "")
|
||||
if err != nil {
|
||||
xlog.Error("Error creating custom action", "error", err)
|
||||
continue
|
||||
}
|
||||
actions = append(actions, customAction)
|
||||
case ActionSearch:
|
||||
actions = append(actions, external.NewSearch(config))
|
||||
case ActionGithubIssueLabeler:
|
||||
|
||||
@@ -1,97 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"github.com/mudler/local-agent-framework/action"
|
||||
"github.com/sashabaranov/go-openai/jsonschema"
|
||||
"github.com/traefik/yaegi/interp"
|
||||
"github.com/traefik/yaegi/stdlib"
|
||||
"jaytaylor.com/html2text"
|
||||
)
|
||||
|
||||
func NewCustom(config map[string]string) *CustomAction {
|
||||
|
||||
return &CustomAction{
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
type CustomAction struct {
|
||||
config map[string]string
|
||||
i *interp.Interpreter
|
||||
code *reflect.Value
|
||||
}
|
||||
|
||||
func (a *CustomAction) initializeInterpreter() error {
|
||||
if _, exists := a.config["code"]; exists && a.i == nil {
|
||||
i := interp.New(interp.Options{GoPath: "./_pkg"})
|
||||
if err := i.Use(stdlib.Symbols); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := i.Eval(a.config["code"])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
a.i = i
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *CustomAction) Run(ctx context.Context, params action.ActionParams) (string, error) {
|
||||
|
||||
result := struct {
|
||||
URL string `json:"url"`
|
||||
}{}
|
||||
err := params.Unmarshal(&result)
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
|
||||
return "", err
|
||||
}
|
||||
// download page with http.Client
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", result.URL, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
pagebyte, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
rendered, err := html2text.FromString(string(pagebyte), html2text.Options{PrettyTables: true})
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return fmt.Sprintf("The webpage '%s' content is:\n%s", result.URL, rendered), nil
|
||||
}
|
||||
|
||||
func (a *CustomAction) Definition() action.ActionDefinition {
|
||||
return action.ActionDefinition{
|
||||
Name: action.ActionDefinitionName(a.config["name"]),
|
||||
Description: a.config["description"],
|
||||
Properties: map[string]jsonschema.Definition{
|
||||
"url": {
|
||||
Type: jsonschema.String,
|
||||
Description: "The website URL.",
|
||||
},
|
||||
},
|
||||
Required: []string{"url"},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user