File size: 7,013 Bytes
8baf122 | 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | package plugins
import (
"context"
"fmt"
"plugin"
"strings"
"github.com/maximhq/bifrost/core/schemas"
)
// SharedObjectPluginLoader is the loader for shared object plugins
type SharedObjectPluginLoader struct{}
func openPlugin(dp *DynamicPlugin) (*plugin.Plugin, error) {
// Checking if path is URL or file path
if strings.HasPrefix(dp.Path, "http") {
// Download the file
tempPath, err := DownloadPlugin(dp.Path, ".so")
if err != nil {
return nil, err
}
dp.Path = tempPath
}
pluginObj, err := plugin.Open(dp.Path)
if err != nil {
return nil, err
}
dp.plugin = pluginObj
return pluginObj, nil
}
// LoadPlugin loads a generic plugin from a shared object file
// It uses optional symbol lookup - only GetName and Cleanup are required
// All other hook methods are optional and stored as nil if not implemented
func (l *SharedObjectPluginLoader) LoadPlugin(path string, config any) (schemas.BasePlugin, error) {
dp := &DynamicPlugin{
Path: path,
}
pluginObj, err := openPlugin(dp)
if err != nil {
return nil, err
}
// Optional Init method
if initSym, err := pluginObj.Lookup("Init"); err == nil {
if initFunc, ok := initSym.(func(config any) error); ok {
if err := initFunc(config); err != nil {
return nil, fmt.Errorf("plugin Init failed: %w", err)
}
} else {
return nil, fmt.Errorf("failed to cast Init to func(config any) error")
}
}
// Required: GetName
getNameSym, err := pluginObj.Lookup("GetName")
if err != nil {
return nil, fmt.Errorf("required symbol GetName not found: %w", err)
}
var ok bool
if dp.getName, ok = getNameSym.(func() string); !ok {
return nil, fmt.Errorf("failed to cast GetName to func() string\nSee docs for more information: https://docs.getbifrost.ai/plugins/writing-go-plugin")
}
// Required: Cleanup
cleanupSym, err := pluginObj.Lookup("Cleanup")
if err != nil {
return nil, fmt.Errorf("required symbol Cleanup not found: %w", err)
}
if dp.cleanup, ok = cleanupSym.(func() error); !ok {
return nil, fmt.Errorf("failed to cast Cleanup to func() error\nSee docs for more information: https://docs.getbifrost.ai/plugins/writing-go-plugin")
}
// Optional: HTTPTransportPreHook
if sym, err := pluginObj.Lookup("HTTPTransportPreHook"); err == nil {
if dp.httpTransportPreHook, ok = sym.(func(ctx *schemas.BifrostContext, req *schemas.HTTPRequest) (*schemas.HTTPResponse, error)); !ok {
return nil, fmt.Errorf("failed to cast HTTPTransportPreHook to expected signature")
}
}
// Optional: HTTPTransportPostHook
if sym, err := pluginObj.Lookup("HTTPTransportPostHook"); err == nil {
if dp.httpTransportPostHook, ok = sym.(func(ctx *schemas.BifrostContext, req *schemas.HTTPRequest, resp *schemas.HTTPResponse) error); !ok {
return nil, fmt.Errorf("failed to cast HTTPTransportPostHook to expected signature")
}
}
// Optional: HTTPTransportStreamChunkHook
if sym, err := pluginObj.Lookup("HTTPTransportStreamChunkHook"); err == nil {
if dp.httpTransportStreamChunkHook, ok = sym.(func(ctx *schemas.BifrostContext, req *schemas.HTTPRequest, chunk *schemas.BifrostStreamChunk) (*schemas.BifrostStreamChunk, error)); !ok {
return nil, fmt.Errorf("failed to cast HTTPTransportStreamChunkHook to expected signature")
}
}
// Optional: PreLLMHook (with backward compatibility for legacy PreHook)
if sym, err := pluginObj.Lookup("PreLLMHook"); err == nil {
if dp.preLLMHook, ok = sym.(func(ctx *schemas.BifrostContext, req *schemas.BifrostRequest) (*schemas.BifrostRequest, *schemas.LLMPluginShortCircuit, error)); !ok {
return nil, fmt.Errorf("failed to cast PreLLMHook to expected signature")
}
} else if sym, err := pluginObj.Lookup("PreHook"); err == nil {
// Legacy backward compatibility (v1.3.x): treat PreHook as PreLLMHook
if dp.preLLMHook, ok = sym.(func(ctx *schemas.BifrostContext, req *schemas.BifrostRequest) (*schemas.BifrostRequest, *schemas.LLMPluginShortCircuit, error)); !ok {
return nil, fmt.Errorf("failed to cast PreHook to expected signature (legacy backward compatibility)")
}
}
// Optional: PostLLMHook (with backward compatibility for legacy PostHook)
if sym, err := pluginObj.Lookup("PostLLMHook"); err == nil {
if dp.postLLMHook, ok = sym.(func(ctx *schemas.BifrostContext, resp *schemas.BifrostResponse, bifrostErr *schemas.BifrostError) (*schemas.BifrostResponse, *schemas.BifrostError, error)); !ok {
return nil, fmt.Errorf("failed to cast PostLLMHook to expected signature")
}
} else if sym, err := pluginObj.Lookup("PostHook"); err == nil {
// Legacy backward compatibility (v1.3.x): treat PostHook as PostLLMHook
if dp.postLLMHook, ok = sym.(func(ctx *schemas.BifrostContext, resp *schemas.BifrostResponse, bifrostErr *schemas.BifrostError) (*schemas.BifrostResponse, *schemas.BifrostError, error)); !ok {
return nil, fmt.Errorf("failed to cast PostHook to expected signature (legacy backward compatibility)")
}
}
// Optional: PreMCPHook
if sym, err := pluginObj.Lookup("PreMCPHook"); err == nil {
if dp.preMCPHook, ok = sym.(func(ctx *schemas.BifrostContext, req *schemas.BifrostMCPRequest) (*schemas.BifrostMCPRequest, *schemas.MCPPluginShortCircuit, error)); !ok {
return nil, fmt.Errorf("failed to cast PreMCPHook to expected signature")
}
}
// Optional: PostMCPHook
if sym, err := pluginObj.Lookup("PostMCPHook"); err == nil {
if dp.postMCPHook, ok = sym.(func(ctx *schemas.BifrostContext, resp *schemas.BifrostMCPResponse, bifrostErr *schemas.BifrostError) (*schemas.BifrostMCPResponse, *schemas.BifrostError, error)); !ok {
return nil, fmt.Errorf("failed to cast PostMCPHook to expected signature")
}
}
// Optional: Inject (ObservabilityPlugin)
if sym, err := pluginObj.Lookup("Inject"); err == nil {
if dp.inject, ok = sym.(func(ctx context.Context, trace *schemas.Trace) error); !ok {
return nil, fmt.Errorf("failed to cast Inject to expected signature")
}
}
return dp, nil
}
// VerifyBasePlugin verifies a plugin at the given path
// Returns the name of the plugin or an empty string if the plugin is invalid
// Returns an error if the plugin is invalid
// This method is used to verify that the plugin is a valid base plugin and has the required symbols
func (l *SharedObjectPluginLoader) VerifyBasePlugin(path string) (string, error) {
dp := &DynamicPlugin{
Path: path,
}
pluginObj, err := openPlugin(dp)
if err != nil {
return "", err
}
// Required: GetName
getNameSym, err := pluginObj.Lookup("GetName")
if err != nil {
return "", fmt.Errorf("required symbol GetName not found: %w", err)
}
var ok bool
if dp.getName, ok = getNameSym.(func() string); !ok {
return "", fmt.Errorf("failed to cast GetName to func() string")
}
// Required: Cleanup
cleanupSym, err := pluginObj.Lookup("Cleanup")
if err != nil {
return "", fmt.Errorf("required symbol Cleanup not found: %w", err)
}
if dp.cleanup, ok = cleanupSym.(func() error); !ok {
return "", fmt.Errorf("failed to cast Cleanup to func() error")
}
return dp.getName(), nil
}
|