Initial commit: Cyrene Plugins SDK + Plugin Manager

Extracted from Cyrene main repo (backend/pkg/plugins + backend/plugin-manager).
Contains SDK interfaces (Plugin/Tool/HostAPI), 13 built-in plugins,
ToolRegistry with call log ring buffer, and Plugin Manager REST API service.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-06 09:49:12 +08:00
commit 5c807d76a0
27 changed files with 3609 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
package main
import (
"context"
"fmt"
"log"
"net/http"
"git.yeij.top/AskaEth/Cyrene-Plugins/manager"
"git.yeij.top/AskaEth/Cyrene-Plugins/sdk"
)
type hostAPI struct {
registry *manager.ToolRegistry
}
func newHostAPI(registry *manager.ToolRegistry) *hostAPI {
return &hostAPI{registry: registry}
}
func (h *hostAPI) CallLLM(_ context.Context, _ []sdk.LLMMessage) (*sdk.LLMResponse, error) {
return nil, fmt.Errorf("LLM call not available in plugin host")
}
func (h *hostAPI) SearchMemory(_ context.Context, _, _ string, _ int) ([]sdk.MemoryEntry, error) {
return nil, fmt.Errorf("memory search not available in plugin host")
}
func (h *hostAPI) StoreMemory(_ context.Context, _ sdk.MemoryEntry) error {
return fmt.Errorf("memory store not available in plugin host")
}
func (h *hostAPI) Logger() sdk.Logger {
return log.Default()
}
func (h *hostAPI) GetConfig(key string) (string, error) {
return "", fmt.Errorf("config key not found: %s", key)
}
func (h *hostAPI) SetConfig(_, _ string) error { return nil }
func (h *hostAPI) PublishEvent(_ context.Context, _ map[string]interface{}) error { return nil }
func (h *hostAPI) HTTPClient() *http.Client {
return http.DefaultClient
}
@@ -0,0 +1,32 @@
package config
import "os"
type Config struct {
Port string
Env string
DataDir string
IoTSvcURL string
}
func Load() *Config {
cfg := &Config{
Port: "8094",
Env: "development",
DataDir: "./data",
IoTSvcURL: "http://localhost:8093",
}
if v := os.Getenv("PORT"); v != "" {
cfg.Port = v
}
if v := os.Getenv("ENV"); v != "" {
cfg.Env = v
}
if v := os.Getenv("DATA_DIR"); v != "" {
cfg.DataDir = v
}
if v := os.Getenv("IOT_SERVICE_URL"); v != "" {
cfg.IoTSvcURL = v
}
return cfg
}
@@ -0,0 +1,210 @@
package handler
import (
"encoding/json"
"net/http"
"strings"
"git.yeij.top/AskaEth/Cyrene-Plugins/manager"
)
// PluginHandler exposes the Plugin Manager REST API via net/http.
type PluginHandler struct {
mgr *manager.PluginManager
}
func NewPluginHandler(mgr *manager.PluginManager) *PluginHandler {
return &PluginHandler{mgr: mgr}
}
func (h *PluginHandler) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("/api/v1/plugins", h.listPlugins)
mux.HandleFunc("/api/v1/plugins/", h.pluginRoute)
mux.HandleFunc("/api/v1/tools", h.listTools)
mux.HandleFunc("/api/v1/tools/", h.toolRoute)
mux.HandleFunc("/api/v1/health", h.health)
}
func (h *PluginHandler) health(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]interface{}{"status": "ok", "service": "plugin-manager"})
}
func (h *PluginHandler) listPlugins(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
writeJSON(w, http.StatusMethodNotAllowed, errResp("method not allowed"))
return
}
plugins := h.mgr.List()
writeJSON(w, http.StatusOK, map[string]interface{}{"plugins": plugins, "total": len(plugins)})
}
func (h *PluginHandler) pluginRoute(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/api/v1/plugins/")
parts := strings.SplitN(path, "/", 2)
pluginID := parts[0]
if pluginID == "" {
h.listPlugins(w, r)
return
}
if len(parts) == 1 {
switch r.Method {
case "GET":
h.getPlugin(w, pluginID)
case "DELETE":
h.uninstallPlugin(w, r, pluginID)
default:
writeJSON(w, http.StatusMethodNotAllowed, errResp("method not allowed"))
}
return
}
action := parts[1]
switch action {
case "enable":
if r.Method != "POST" {
writeJSON(w, http.StatusMethodNotAllowed, errResp("method not allowed"))
return
}
h.enablePlugin(w, r, pluginID)
case "disable":
if r.Method != "POST" {
writeJSON(w, http.StatusMethodNotAllowed, errResp("method not allowed"))
return
}
h.disablePlugin(w, r, pluginID)
case "reload":
if r.Method != "POST" {
writeJSON(w, http.StatusMethodNotAllowed, errResp("method not allowed"))
return
}
h.reloadPlugin(w, r, pluginID)
case "tools":
if r.Method != "GET" {
writeJSON(w, http.StatusMethodNotAllowed, errResp("method not allowed"))
return
}
h.pluginTools(w, pluginID)
default:
writeJSON(w, http.StatusNotFound, errResp("not found"))
}
}
func (h *PluginHandler) getPlugin(w http.ResponseWriter, id string) {
info, ok := h.mgr.Get(id)
if !ok {
writeJSON(w, http.StatusNotFound, errResp("plugin not found"))
return
}
writeJSON(w, http.StatusOK, info)
}
func (h *PluginHandler) enablePlugin(w http.ResponseWriter, r *http.Request, id string) {
if err := h.mgr.Enable(r.Context(), id); err != nil {
writeJSON(w, http.StatusInternalServerError, errResp(err.Error()))
return
}
writeJSON(w, http.StatusOK, map[string]string{"status": "enabled"})
}
func (h *PluginHandler) disablePlugin(w http.ResponseWriter, r *http.Request, id string) {
if err := h.mgr.Disable(r.Context(), id); err != nil {
writeJSON(w, http.StatusInternalServerError, errResp(err.Error()))
return
}
writeJSON(w, http.StatusOK, map[string]string{"status": "disabled"})
}
func (h *PluginHandler) reloadPlugin(w http.ResponseWriter, r *http.Request, id string) {
if err := h.mgr.Reload(r.Context(), id); err != nil {
writeJSON(w, http.StatusInternalServerError, errResp(err.Error()))
return
}
writeJSON(w, http.StatusOK, map[string]string{"status": "reloaded"})
}
func (h *PluginHandler) uninstallPlugin(w http.ResponseWriter, r *http.Request, id string) {
if err := h.mgr.Uninstall(r.Context(), id); err != nil {
writeJSON(w, http.StatusInternalServerError, errResp(err.Error()))
return
}
writeJSON(w, http.StatusOK, map[string]string{"status": "uninstalled"})
}
func (h *PluginHandler) pluginTools(w http.ResponseWriter, id string) {
info, ok := h.mgr.Get(id)
if !ok {
writeJSON(w, http.StatusNotFound, errResp("plugin not found"))
return
}
registry := h.mgr.Registry()
tools := make([]interface{}, 0)
for _, toolID := range info.Tools {
if t, ok := registry.Get(toolID); ok {
tools = append(tools, t.Definition())
}
}
writeJSON(w, http.StatusOK, map[string]interface{}{"tools": tools, "total": len(tools)})
}
func (h *PluginHandler) listTools(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
writeJSON(w, http.StatusMethodNotAllowed, errResp("method not allowed"))
return
}
defs := h.mgr.Registry().Definitions()
writeJSON(w, http.StatusOK, map[string]interface{}{"tools": defs, "total": len(defs)})
}
func (h *PluginHandler) toolRoute(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/api/v1/tools/")
toolID := path
if strings.HasSuffix(path, "/execute") {
toolID = strings.TrimSuffix(path, "/execute")
if r.Method != "POST" {
writeJSON(w, http.StatusMethodNotAllowed, errResp("method not allowed"))
return
}
h.executeTool(w, r, toolID)
return
}
if r.Method != "GET" {
writeJSON(w, http.StatusMethodNotAllowed, errResp("method not allowed"))
return
}
tool, ok := h.mgr.Registry().Get(toolID)
if !ok {
writeJSON(w, http.StatusNotFound, errResp("tool not found"))
return
}
writeJSON(w, http.StatusOK, tool.Definition())
}
func (h *PluginHandler) executeTool(w http.ResponseWriter, r *http.Request, toolID string) {
var body struct {
Arguments map[string]interface{} `json:"arguments"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeJSON(w, http.StatusBadRequest, errResp("invalid request body"))
return
}
result, err := h.mgr.Registry().Execute(r.Context(), toolID, body.Arguments)
if err != nil {
writeJSON(w, http.StatusInternalServerError, errResp(err.Error()))
return
}
writeJSON(w, http.StatusOK, result)
}
func errResp(msg string) map[string]string {
return map[string]string{"error": msg}
}
func writeJSON(w http.ResponseWriter, status int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
json.NewEncoder(w).Encode(data)
}
+112
View File
@@ -0,0 +1,112 @@
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"git.yeij.top/AskaEth/Cyrene-Plugins/sdk"
iotquery "git.yeij.top/AskaEth/Cyrene-Plugins/iot_query"
)
type iotClient struct {
baseURL string
httpClient *http.Client
}
func newIoTClient(baseURL string) *iotClient {
return &iotClient{
baseURL: baseURL,
httpClient: &http.Client{Timeout: 5 * time.Second},
}
}
func (c *iotClient) GetAllDevices(ctx context.Context) ([]sdk.IoTDeviceState, error) {
url := c.baseURL + "/api/v1/devices"
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result struct {
Devices []sdk.IoTDeviceState `json:"devices"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return result.Devices, nil
}
func (c *iotClient) GetDevice(ctx context.Context, deviceID string) (*sdk.IoTDeviceState, error) {
url := fmt.Sprintf("%s/api/v1/devices/%s", c.baseURL, deviceID)
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var dev sdk.IoTDeviceState
if err := json.NewDecoder(resp.Body).Decode(&dev); err != nil {
return nil, err
}
return &dev, nil
}
// iotControllerAdapter adapts IoTClient to iotcontrol.IoTController.
type iotControllerAdapter struct {
query iotquery.IoTClient
client *http.Client
baseURL string
}
func newIoTControllerAdapter(query iotquery.IoTClient, baseURL string) *iotControllerAdapter {
return &iotControllerAdapter{
query: query,
client: &http.Client{Timeout: 5 * time.Second},
baseURL: baseURL,
}
}
func (a *iotControllerAdapter) GetDevice(ctx context.Context, deviceID string) (*sdk.IoTDeviceState, error) {
return a.query.GetDevice(ctx, deviceID)
}
func (a *iotControllerAdapter) SetDeviceProperty(ctx context.Context, deviceID, property string, value interface{}) error {
url := fmt.Sprintf("%s/api/v1/devices/%s/property", a.baseURL, deviceID)
body, _ := json.Marshal(map[string]interface{}{"property": property, "value": value})
req, _ := http.NewRequestWithContext(ctx, "POST", url, strings.NewReader(string(body)))
req.Header.Set("Content-Type", "application/json")
resp, err := a.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
msg, _ := io.ReadAll(resp.Body)
return fmt.Errorf("set property failed: HTTP %d - %s", resp.StatusCode, string(msg))
}
return nil
}
func (a *iotControllerAdapter) ToggleDevice(ctx context.Context, deviceID string) (*sdk.IoTDeviceState, error) {
url := fmt.Sprintf("%s/api/v1/devices/%s/toggle", a.baseURL, deviceID)
req, _ := http.NewRequestWithContext(ctx, "POST", url, nil)
resp, err := a.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var dev sdk.IoTDeviceState
if err := json.NewDecoder(resp.Body).Decode(&dev); err != nil {
return nil, err
}
return &dev, nil
}
+100
View File
@@ -0,0 +1,100 @@
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"git.yeij.top/AskaEth/Cyrene-Plugins/calculator"
"git.yeij.top/AskaEth/Cyrene-Plugins/crypto"
"git.yeij.top/AskaEth/Cyrene-Plugins/datetime"
fileplugin "git.yeij.top/AskaEth/Cyrene-Plugins/file"
httpplugin "git.yeij.top/AskaEth/Cyrene-Plugins/http"
iotcontrol "git.yeij.top/AskaEth/Cyrene-Plugins/iot_control"
iotquery "git.yeij.top/AskaEth/Cyrene-Plugins/iot_query"
jsonplugin "git.yeij.top/AskaEth/Cyrene-Plugins/json"
"git.yeij.top/AskaEth/Cyrene-Plugins/manager"
"git.yeij.top/AskaEth/Cyrene-Plugins/markdown"
"git.yeij.top/AskaEth/Cyrene-Plugins/random"
"git.yeij.top/AskaEth/Cyrene-Plugins/sdk"
"git.yeij.top/AskaEth/Cyrene-Plugins/text"
webfetch "git.yeij.top/AskaEth/Cyrene-Plugins/web_fetch"
websearch "git.yeij.top/AskaEth/Cyrene-Plugins/web_search"
"git.yeij.top/AskaEth/Cyrene-Plugins/cmd/plugin-manager/internal/config"
"git.yeij.top/AskaEth/Cyrene-Plugins/cmd/plugin-manager/internal/handler"
)
func main() {
cfg := config.Load()
var iotAPI iotquery.IoTClient
if cfg.IoTSvcURL != "" {
iotAPI = newIoTClient(cfg.IoTSvcURL)
}
registry := manager.NewToolRegistry()
host := newHostAPI(registry)
mgr := manager.NewPluginManager(registry, host)
builtins := []sdk.Plugin{
&calculator.CalculatorPlugin{},
&datetime.DatetimePlugin{},
&text.TextPlugin{},
&crypto.CryptoPlugin{},
&random.RandomPlugin{},
&markdown.MarkdownPlugin{},
&jsonplugin.JSONPlugin{},
fileplugin.NewFilePlugin(cfg.DataDir),
httpplugin.NewHTTPPlugin(),
websearch.NewWebSearchPlugin(),
webfetch.NewWebFetchPlugin(),
iotquery.NewIoTQueryPlugin(iotAPI),
}
for _, p := range builtins {
if err := mgr.Install(p); err != nil {
println("WARN: install plugin failed:", err.Error())
}
}
if iotAPI != nil {
ctrlPlugin := iotcontrol.NewIoTControlPlugin(newIoTControllerAdapter(iotAPI, cfg.IoTSvcURL))
if err := mgr.Install(ctrlPlugin); err != nil {
println("WARN: install plugin failed:", err.Error())
}
}
ctx := context.Background()
errs := mgr.EnableAll(ctx)
for _, e := range errs {
println("WARN: enable plugin failed:", e.Error())
}
println("Plugin Manager: all built-in plugins enabled")
mux := http.NewServeMux()
ph := handler.NewPluginHandler(mgr)
ph.RegisterRoutes(mux)
println("Plugin Manager listening on port", cfg.Port)
srv := &http.Server{Addr: ":" + cfg.Port, Handler: mux}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
println("FATAL:", err.Error())
os.Exit(1)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
println("Shutting down Plugin Manager...")
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
mgr.Shutdown(shutdownCtx)
srv.Shutdown(shutdownCtx)
println("Plugin Manager stopped")
}