mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-23 10:28:06 +00:00
feat: C ABI plugin loader with full CoreAPI dispatch
- internal/plugin/cabi/: dlopen + dlsym for plugin_init - create_core_api(): C struct with dispatch_bridge→go_core_dispatch - go_core_dispatch routes all 25 SDK methods (RegisterTool, MemoryAPI, etc.) - dynamic.go: cabiPlugin uses CreateCoreAPI(sdk)→Start(corePtr) - Each plugin gets a unique ID stored in CoreAPI.ctx - C functions moved to loader.c to avoid cgo duplicate symbol issues
This commit is contained in:
@ -9,7 +9,6 @@ import (
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
"reflect"
|
||||
"unsafe"
|
||||
|
||||
sdk "gitcode.com/JianFeeeee/HomeAgent/internal/sdk"
|
||||
pubsdk "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||
@ -61,42 +60,16 @@ type cabiPlugin struct {
|
||||
|
||||
func (p *cabiPlugin) Name() string { return p.name }
|
||||
func (p *cabiPlugin) Start(s *sdk.PluginSDK) error {
|
||||
// Build CoreAPI from the provided PluginSDK and pass to plugin
|
||||
corePtr := buildCoreAPI(s, p.name)
|
||||
// Create CoreAPI backed by the real PluginSDK and pass to plugin
|
||||
corePtr := p.handle.CreateCoreAPI(s)
|
||||
if corePtr == nil {
|
||||
return fmt.Errorf("cabi: failed to create CoreAPI for %s", p.name)
|
||||
}
|
||||
defer p.handle.FreeCoreAPI()
|
||||
|
||||
if err := p.handle.Start(corePtr); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("cabi: start %s: %w", p.name, err)
|
||||
}
|
||||
|
||||
// Discover tools/stages/channels registered by the plugin during Start
|
||||
defs, _ := p.handle.GetToolDefs()
|
||||
for _, d := range defs {
|
||||
var td pubsdk.ToolDef
|
||||
if err := json.Unmarshal(d, &td); err != nil {
|
||||
continue
|
||||
}
|
||||
toolName := td.Name
|
||||
td.Plugin = p.name
|
||||
s.RegisterTool(toolName, sdk.ToolDef{
|
||||
Name: toolName,
|
||||
Description: td.Description,
|
||||
Parameters: td.Parameters,
|
||||
Plugin: p.name,
|
||||
}, makeCABIHandler(p.handle, toolName))
|
||||
}
|
||||
|
||||
stages, _ := p.handle.GetStages()
|
||||
for _, stage := range stages {
|
||||
st := sdk.Stage(stage)
|
||||
s.RegisterStage(st, func(sc *sdk.StageContext) error {
|
||||
ctxJSON, _ := json.Marshal(map[string]interface{}{
|
||||
"raw_message": sc.RawMessage,
|
||||
"user_id": sc.UserID,
|
||||
"phase": string(sc.Phase),
|
||||
})
|
||||
return p.handle.InvokeStage(stage, string(ctxJSON))
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -105,19 +78,6 @@ func (p *cabiPlugin) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func makeCABIHandler(handle *cabi.Handle, toolName string) sdk.ToolHandler {
|
||||
return func(args map[string]interface{}) (interface{}, error) {
|
||||
return handle.InvokeTool(toolName, args)
|
||||
}
|
||||
}
|
||||
|
||||
// buildCoreAPI creates a C-compatible CoreAPI function table from a PluginSDK.
|
||||
// Returns an unsafe.Pointer to a C-allocated struct.
|
||||
// TODO: implement CoreAPI dispatch that calls back into the Go PluginSDK
|
||||
func buildCoreAPI(s *sdk.PluginSDK, pluginName string) unsafe.Pointer {
|
||||
return unsafe.Pointer(nil) // placeholder - will be implemented in core dispatch
|
||||
}
|
||||
|
||||
// tryLoadSO 尝试从插件目录加载 plugin.so。
|
||||
// 优先尝试 C ABI 加载(-buildmode=c-shared),失败时回退到 Go plugin.Open。
|
||||
func tryLoadSO(dir, name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||||
|
||||
Reference in New Issue
Block a user