WeKnora/internal/container/plugin_runtime.go

160 lines
5.0 KiB
Go

package container
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/redis/go-redis/v9"
"go.uber.org/dig"
"gorm.io/gorm"
"github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/application/service"
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/handler"
"github.com/Tencent/WeKnora/internal/plugin/activate"
"github.com/Tencent/WeKnora/internal/plugin/host"
"github.com/Tencent/WeKnora/internal/plugin/hostapi"
"github.com/Tencent/WeKnora/internal/plugin/install"
"github.com/Tencent/WeKnora/internal/plugin/reconcile"
pluginregistry "github.com/Tencent/WeKnora/internal/plugin/registry"
"github.com/Tencent/WeKnora/internal/plugin/remote"
plugintenancy "github.com/Tencent/WeKnora/internal/plugin/tenancy"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/Tencent/WeKnora/pluginsdk/client"
)
// newPluginPackageStore keeps plugin packages in the deployment's object
// storage. Packages belong to the platform, not a tenant, so they skip the
// tenant resource catalog.
func newPluginPackageStore(cfg *config.Config) (reconcile.PackageStore, error) {
fs, err := initRawFileService(cfg)
if err != nil {
return nil, err
}
return reconcile.NewFileStore(fs), nil
}
// pluginActivators are the domains installed plugins contribute to.
type pluginActivators struct {
dig.In
Host *host.Manager
Remote *remote.Manager
WebSearch *activate.WebSearch
Connectors *activate.Connectors
Parsers *activate.Parsers
MCP *activate.MCPServers
Skills *activate.Skills
Vendors *activate.ModelVendors
Invoker *activate.Invoker
}
// list orders the activators: the runtimes first, so a code plugin is
// reachable before anything routes calls to it.
func (a pluginActivators) list() []reconcile.Activator {
return []reconcile.Activator{
a.Host, a.Remote, a.WebSearch, a.Connectors, a.Parsers, a.Vendors, a.MCP, a.Skills,
}
}
// newPluginHostAPI serves the Host API and gives calls a way back to it: the
// embedded host's plugins reach this node on loopback, remote plugins only
// through WEKNORA_PLUGIN_HOST_API_URL.
func newPluginHostAPI(
cfg *config.Config, iv *activate.Invoker, repo interfaces.PluginKVRepository, cleaner interfaces.ResourceCleaner,
) *hostapi.Handler {
issuer := hostapi.NewIssuerFromEnv()
url := strings.TrimSpace(os.Getenv("WEKNORA_PLUGIN_HOST_API_URL"))
iv.SetPublicHostAPI(url)
if url == "" {
port := 8080
if cfg != nil && cfg.Server != nil && cfg.Server.Port > 0 {
port = cfg.Server.Port
}
url = fmt.Sprintf("http://127.0.0.1:%d", port)
}
iv.SetHostAPI(issuer, url)
kv := hostapi.NewKV(repo)
ctx, cancel := context.WithCancel(context.Background())
kv.StartSweeper(ctx, 10*time.Minute)
cleaner.RegisterWithName("PluginKVSweeper", func() error { cancel(); return nil })
return hostapi.NewHandler(issuer, kv)
}
// bindPluginActivators hands the activators what they need once the plugin
// services exist.
func bindPluginActivators(
a pluginActivators, t *plugintenancy.Service, repo interfaces.PluginRepository,
skills *service.TenantSkillService,
) {
a.MCP.Bind(t, repo)
a.Invoker.Bind(t, repo)
a.Skills.Bind(t)
skills.SetPluginSkills(a.Skills)
}
// newPluginInvoker reaches code plugins through this node's plugin host or,
// for remote ones, at their registered URLs.
func newPluginInvoker(h *host.Manager, r *remote.Manager) *activate.Invoker {
return activate.NewInvoker(pluginClients{host: h, remote: r})
}
// pluginClients finds a code plugin in whichever runtime loaded it.
type pluginClients struct {
host *host.Manager
remote *remote.Manager
}
func (c pluginClients) Client(pluginID string) (*client.Client, error) {
if c.remote.Owns(pluginID) {
return c.remote.Client(pluginID)
}
return c.host.Client(pluginID)
}
func newMCPServiceRepository(db *gorm.DB, plugins *activate.MCPServers) interfaces.MCPServiceRepository {
return plugins.Repository(repository.NewMCPServiceRepository(db))
}
func newPluginReconciler(
repo interfaces.PluginRepository,
store reconcile.PackageStore,
reg *pluginregistry.Registry,
rdb *redis.Client,
activators pluginActivators,
) *reconcile.Reconciler {
return reconcile.New(reconcile.Options{
Repo: repo, Store: store, Registry: reg, Redis: rdb, Activators: activators.list(),
})
}
func newPluginInstaller(
repo interfaces.PluginRepository,
store reconcile.PackageStore,
r *reconcile.Reconciler,
) *install.Service {
return install.NewService(repo, store, r, handler.Version).WithChecks(activate.CheckModelVendors)
}
// startPluginReconciler loads installed plugins before the server takes
// traffic, then keeps this node in step with the others.
func startPluginReconciler(
r *reconcile.Reconciler, hostManager *host.Manager, remoteManager *remote.Manager,
cleaner interfaces.ResourceCleaner,
) {
hostManager.SetReporter(r)
remoteManager.SetReporter(r)
ctx, cancel := context.WithCancel(context.Background())
r.Start(ctx)
cleaner.RegisterWithName("PluginReconciler", func() error {
cancel()
hostManager.Close()
remoteManager.Close()
return nil
})
}