Hook to the webui
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/mudler/local-agent-framework/action"
|
||||||
"github.com/mudler/local-agent-framework/xlog"
|
"github.com/mudler/local-agent-framework/xlog"
|
||||||
|
|
||||||
. "github.com/mudler/local-agent-framework/agent"
|
. "github.com/mudler/local-agent-framework/agent"
|
||||||
@@ -13,6 +14,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
// Actions
|
// Actions
|
||||||
ActionSearch = "search"
|
ActionSearch = "search"
|
||||||
|
ActionCustom = "custom"
|
||||||
ActionGithubIssueLabeler = "github-issue-labeler"
|
ActionGithubIssueLabeler = "github-issue-labeler"
|
||||||
ActionGithubIssueOpener = "github-issue-opener"
|
ActionGithubIssueOpener = "github-issue-opener"
|
||||||
ActionGithubIssueCloser = "github-issue-closer"
|
ActionGithubIssueCloser = "github-issue-closer"
|
||||||
@@ -25,6 +27,7 @@ const (
|
|||||||
|
|
||||||
var AvailableActions = []string{
|
var AvailableActions = []string{
|
||||||
ActionSearch,
|
ActionSearch,
|
||||||
|
ActionCustom,
|
||||||
ActionGithubIssueLabeler,
|
ActionGithubIssueLabeler,
|
||||||
ActionGithubIssueOpener,
|
ActionGithubIssueOpener,
|
||||||
ActionGithubIssueCloser,
|
ActionGithubIssueCloser,
|
||||||
@@ -38,14 +41,21 @@ var AvailableActions = []string{
|
|||||||
func (a *AgentConfig) availableActions(ctx context.Context) []Action {
|
func (a *AgentConfig) availableActions(ctx context.Context) []Action {
|
||||||
actions := []Action{}
|
actions := []Action{}
|
||||||
|
|
||||||
for _, action := range a.Actions {
|
for _, a := range a.Actions {
|
||||||
var config map[string]string
|
var config map[string]string
|
||||||
if err := json.Unmarshal([]byte(action.Config), &config); err != nil {
|
if err := json.Unmarshal([]byte(a.Config), &config); err != nil {
|
||||||
xlog.Info("Error unmarshalling action config", "error", err)
|
xlog.Error("Error unmarshalling action config", "error", err)
|
||||||
continue
|
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:
|
case ActionSearch:
|
||||||
actions = append(actions, external.NewSearch(config))
|
actions = append(actions, external.NewSearch(config))
|
||||||
case ActionGithubIssueLabeler:
|
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