-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_handler.go
148 lines (121 loc) · 3.13 KB
/
plugin_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"github.com/tmc/langchaingo/llms"
)
type Param struct {
Name string `json:"name"`
Description string `json:"description"`
}
type Action struct {
llms.FunctionDefinition
plugin *Plugin
execute func(map[string]any) (string, error)
}
func externalPluginExecutor(a Action) func(map[string]any) (string, error) {
return func(props map[string]any) (string, error) {
// send HTTP post request to plugin
toolParams, err := json.Marshal(props)
if err != nil {
return "", err
}
client := http.Client{
Timeout: time.Second * 10, // Timeout after 2 seconds
}
// url builders
url := url.URL{
Scheme: "http",
Host: a.plugin.Address,
Path: "/" + a.Name}
req, err := http.NewRequest("POST", url.String(), bytes.NewBuffer(toolParams))
req.Header.Set("Content-Type", "application/json")
if err != nil {
slog.Error("Error creating request", "error", err)
return "", err
}
res, err := client.Do(req)
if err != nil {
slog.Error("Error executing action", "actionName", a.Name, "error", err)
return "", err
}
if res.Body != nil {
defer res.Body.Close()
}
body, readErr := io.ReadAll(res.Body)
if readErr != nil {
slog.Error("Error reading response body", "error", readErr)
return "", readErr
}
if res.StatusCode != http.StatusOK {
slog.Error("Error executing action:", "actionName", a.Name, "status code", res.StatusCode)
return "", errors.New(string(body))
}
slog.Debug("Executing action", "actionName", a.Name)
return string(body), nil
}
}
type Plugin struct {
Name string `json:"name"`
Address string `json:"address"`
Actions []Action `json:"actions"`
RootPath string
}
func fetchPlugins(searchPath string) ([]Plugin, error) {
plugins := []Plugin{}
entries, err := os.ReadDir(searchPath)
if err != nil {
slog.Error("Error reading directory", "error", err)
return nil, err
}
for _, entry := range entries {
fullPath := filepath.Join(searchPath, entry.Name())
if entry.IsDir() {
// Recursively fetch plugins in subdirectories
subPlugins, err := fetchPlugins(fullPath)
if err != nil {
return nil, err
}
plugins = append(plugins, subPlugins...)
} else {
if entry.Name() != "parker.json" {
continue
}
plugin, err := parsePluginConfig(fullPath)
if err != nil {
return nil, err
}
plugins = append(plugins, plugin)
}
}
return plugins, nil
}
func parsePluginConfig(path string) (Plugin, error) {
// Read and parse the parker.json file
pluginData, err := os.ReadFile(path)
var plugin Plugin
if err != nil {
slog.Error("Error reading plugin file", "error", err)
return plugin, err
}
err = json.Unmarshal(pluginData, &plugin)
if err != nil {
slog.Error("Error parsing plugin JSON", "error", err)
return plugin, err
}
plugin.RootPath = filepath.Dir(path)
// set plugin on each action to be plugin
for i := 0; i < len(plugin.Actions); i++ {
plugin.Actions[i].plugin = &plugin
plugin.Actions[i].execute = externalPluginExecutor(plugin.Actions[i])
}
return plugin, nil
}