mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 09:28:14 +00:00
重构: 插件自注册 + .so 动态加载 + 中断打断机制
- 所有内置插件 init() 自注册 (plugin.RegisterFactory), 移除 main.go 硬编码 - 新增 .so 动态加载器 (internal/plugin/dynamic.go), 插件可编译为 plugin.so - 新增 plugin.json 元数据 (internal/plugin/manifest.go) - 新增 interceptLoop 独立 goroutine: (a) cancelLLM() 取消进行中的 HTTP 请求 (b) interceptCh → drainInterrupt() 注入 [打断消息] 到 LLM 上下文 (c) InjectInput 空闲时触发新处理循环 - 新增 internal/plugins/all.go 空白导入触发所有内置插件 init() - internal/sdk/ 作为 PluginSDK 正式 Go API - internal/api/ → internal/plugins/webui/ 迁移 - 删除旧 cmd/cli/, 使用 cmd/waiter/ 替代 - 更新 PLAN.md / ARCHITECTURE.md / README.md 文档
This commit is contained in:
38
internal/sdk/knowledge.go
Normal file
38
internal/sdk/knowledge.go
Normal file
@ -0,0 +1,38 @@
|
||||
package sdk
|
||||
|
||||
import "gitcode.com/JianFeeeee/HomeAgent/internal/knowledge"
|
||||
|
||||
type KnowledgeAPI interface {
|
||||
Search(query string, topK int) ([]*knowledge.Knowledge, error)
|
||||
Add(name, content string) error
|
||||
List() ([]string, error)
|
||||
}
|
||||
|
||||
type knowledgeImpl struct {
|
||||
ks *knowledge.Store
|
||||
}
|
||||
|
||||
func NewKnowledge(ks *knowledge.Store) KnowledgeAPI {
|
||||
return &knowledgeImpl{ks: ks}
|
||||
}
|
||||
|
||||
func (k *knowledgeImpl) Search(query string, topK int) ([]*knowledge.Knowledge, error) {
|
||||
if k.ks == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return k.ks.Search(query, topK), nil
|
||||
}
|
||||
|
||||
func (k *knowledgeImpl) Add(name, content string) error {
|
||||
if k.ks == nil {
|
||||
return nil
|
||||
}
|
||||
return k.ks.Add(name, content)
|
||||
}
|
||||
|
||||
func (k *knowledgeImpl) List() ([]string, error) {
|
||||
if k.ks == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return k.ks.List(), nil
|
||||
}
|
||||
42
internal/sdk/llm.go
Normal file
42
internal/sdk/llm.go
Normal file
@ -0,0 +1,42 @@
|
||||
package sdk
|
||||
|
||||
import agentAPI "gitcode.com/JianFeeeee/HomeAgent/internal/agent/api"
|
||||
|
||||
type LLMAPI interface {
|
||||
ListSources() []string
|
||||
SetSource(name string) error
|
||||
CurrentSource() string
|
||||
}
|
||||
|
||||
type llmImpl struct {
|
||||
mgr *agentAPI.ProviderManager
|
||||
}
|
||||
|
||||
func NewLLM(mgr *agentAPI.ProviderManager) LLMAPI {
|
||||
return &llmImpl{mgr: mgr}
|
||||
}
|
||||
|
||||
func (l *llmImpl) ListSources() []string {
|
||||
if l.mgr == nil {
|
||||
return nil
|
||||
}
|
||||
return l.mgr.List()
|
||||
}
|
||||
|
||||
func (l *llmImpl) SetSource(name string) error {
|
||||
if l.mgr == nil {
|
||||
return nil
|
||||
}
|
||||
return l.mgr.SetDefault(name)
|
||||
}
|
||||
|
||||
func (l *llmImpl) CurrentSource() string {
|
||||
if l.mgr == nil {
|
||||
return ""
|
||||
}
|
||||
p := l.mgr.Default()
|
||||
if p == nil {
|
||||
return ""
|
||||
}
|
||||
return p.Name()
|
||||
}
|
||||
154
internal/sdk/memory.go
Normal file
154
internal/sdk/memory.go
Normal file
@ -0,0 +1,154 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/memory"
|
||||
doc "gitcode.com/JianFeeeee/HomeAgent/internal/memory/document"
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/memory/text"
|
||||
)
|
||||
|
||||
type MemoryAPI interface {
|
||||
Recall(query []string, depth int) ([]Entity, []Relation, error)
|
||||
Commit(triples []Triple) error
|
||||
Introspect() (map[string]interface{}, error)
|
||||
MergeEntities(source, target string) (int, error)
|
||||
Purge(criteria map[string]string, mode string) (int, error)
|
||||
}
|
||||
|
||||
type Entity struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
MentionCount int `json:"mention_count"`
|
||||
}
|
||||
|
||||
type Relation struct {
|
||||
SourceName string `json:"source_name"`
|
||||
TargetName string `json:"target_name"`
|
||||
RelationType string `json:"relation_type"`
|
||||
}
|
||||
|
||||
type Triple struct {
|
||||
Subject string `json:"subject"`
|
||||
Relation string `json:"relation"`
|
||||
Object string `json:"object"`
|
||||
}
|
||||
|
||||
type TextMemoryAPI interface {
|
||||
Append(evt text.Event) error
|
||||
}
|
||||
|
||||
type DocMemoryAPI interface {
|
||||
Query(text string, topK int) []*doc.Doc
|
||||
Insert(doc *doc.Doc) error
|
||||
Remove(id string)
|
||||
Stats() map[string]interface{}
|
||||
}
|
||||
|
||||
type graphMemory struct {
|
||||
db *memory.GraphDB
|
||||
}
|
||||
|
||||
func NewGraphMemory(db *memory.GraphDB) MemoryAPI {
|
||||
return &graphMemory{db: db}
|
||||
}
|
||||
|
||||
func (m *graphMemory) Recall(query []string, depth int) ([]Entity, []Relation, error) {
|
||||
if m.db == nil {
|
||||
return nil, nil, nil
|
||||
}
|
||||
result, err := m.db.Recall(query, nil, depth, "")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
entities := make([]Entity, len(result.Entities))
|
||||
for i, e := range result.Entities {
|
||||
entities[i] = Entity{Name: e.Name, Type: e.Type, MentionCount: e.MentionCount}
|
||||
}
|
||||
relations := make([]Relation, len(result.Relations))
|
||||
for i, r := range result.Relations {
|
||||
relations[i] = Relation{SourceName: r.SourceName, TargetName: r.TargetName, RelationType: r.RelationType}
|
||||
}
|
||||
return entities, relations, nil
|
||||
}
|
||||
|
||||
func (m *graphMemory) Commit(triples []Triple) error {
|
||||
if m.db == nil {
|
||||
return nil
|
||||
}
|
||||
ts := make([]memory.Triple, len(triples))
|
||||
for i, t := range triples {
|
||||
ts[i] = memory.Triple{Subject: t.Subject, Relation: t.Relation, Object: t.Object}
|
||||
}
|
||||
_, _, err := m.db.Commit(ts, "plugin", 0)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *graphMemory) Introspect() (map[string]interface{}, error) {
|
||||
if m.db == nil {
|
||||
return map[string]interface{}{}, nil
|
||||
}
|
||||
return m.db.Introspect()
|
||||
}
|
||||
|
||||
func (m *graphMemory) MergeEntities(source, target string) (int, error) {
|
||||
if m.db == nil {
|
||||
return 0, nil
|
||||
}
|
||||
return m.db.MergeEntities(source, target)
|
||||
}
|
||||
|
||||
func (m *graphMemory) Purge(criteria map[string]string, mode string) (int, error) {
|
||||
if m.db == nil {
|
||||
return 0, nil
|
||||
}
|
||||
return m.db.Purge(criteria, mode)
|
||||
}
|
||||
|
||||
type textMemoryImpl struct {
|
||||
tm *text.Memory
|
||||
}
|
||||
|
||||
func NewTextMemory(tm *text.Memory) TextMemoryAPI {
|
||||
return &textMemoryImpl{tm: tm}
|
||||
}
|
||||
|
||||
func (m *textMemoryImpl) Append(evt text.Event) error {
|
||||
if m.tm == nil {
|
||||
return nil
|
||||
}
|
||||
return m.tm.Append(evt)
|
||||
}
|
||||
|
||||
type docMemoryImpl struct {
|
||||
ds *doc.Store
|
||||
}
|
||||
|
||||
func NewDocMemory(ds *doc.Store) DocMemoryAPI {
|
||||
return &docMemoryImpl{ds: ds}
|
||||
}
|
||||
|
||||
func (m *docMemoryImpl) Query(text string, topK int) []*doc.Doc {
|
||||
if m.ds == nil {
|
||||
return nil
|
||||
}
|
||||
return m.ds.Query(text, topK)
|
||||
}
|
||||
|
||||
func (m *docMemoryImpl) Insert(d *doc.Doc) error {
|
||||
if m.ds == nil {
|
||||
return nil
|
||||
}
|
||||
return m.ds.Insert(d)
|
||||
}
|
||||
|
||||
func (m *docMemoryImpl) Remove(id string) {
|
||||
if m.ds != nil {
|
||||
m.ds.Remove(id)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *docMemoryImpl) Stats() map[string]interface{} {
|
||||
if m.ds == nil {
|
||||
return map[string]interface{}{}
|
||||
}
|
||||
return m.ds.Stats()
|
||||
}
|
||||
230
internal/sdk/plugin.go
Normal file
230
internal/sdk/plugin.go
Normal file
@ -0,0 +1,230 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
agentIO "gitcode.com/JianFeeeee/HomeAgent/internal/agent/io"
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/events"
|
||||
)
|
||||
|
||||
type Plugin interface {
|
||||
Name() string
|
||||
Start(sdk *PluginSDK) error
|
||||
Stop() error
|
||||
}
|
||||
|
||||
type ToolHandler func(args map[string]interface{}) (interface{}, error)
|
||||
type StageHandler func(ctx *StageContext) error
|
||||
|
||||
type Stage string
|
||||
|
||||
const (
|
||||
StageOnInput Stage = "on_input"
|
||||
StagePreAction Stage = "pre_action"
|
||||
StagePostAction Stage = "post_action"
|
||||
StageBeforeToolcall Stage = "before_toolcall"
|
||||
StageAfterToolcall Stage = "after_toolcall"
|
||||
StageBeforeOutput Stage = "before_output"
|
||||
StageAfterOutput Stage = "after_output"
|
||||
)
|
||||
|
||||
type StageContext struct {
|
||||
mu sync.RWMutex
|
||||
RawMessage string
|
||||
UserID string
|
||||
GroupID string
|
||||
ContextMsgs []map[string]interface{}
|
||||
LLMText string
|
||||
ToolCalls []ToolCall
|
||||
ToolResults []ToolResult
|
||||
FinalText string
|
||||
Response *string
|
||||
Phase Stage
|
||||
Memory []MemItem
|
||||
Extra map[string]interface{}
|
||||
}
|
||||
|
||||
func (c *StageContext) RLock() { c.mu.RLock() }
|
||||
func (c *StageContext) RUnlock() { c.mu.RUnlock() }
|
||||
func (c *StageContext) Lock() { c.mu.Lock() }
|
||||
func (c *StageContext) Unlock() { c.mu.Unlock() }
|
||||
func (c *StageContext) IsResponded() bool { c.mu.RLock(); defer c.mu.RUnlock(); return c.Response != nil }
|
||||
|
||||
type MemItem struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
Score float64 `json:"score"`
|
||||
}
|
||||
|
||||
type ToolCall struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Arguments map[string]interface{} `json:"arguments"`
|
||||
}
|
||||
|
||||
type ToolResult struct {
|
||||
CallID string `json:"call_id"`
|
||||
Name string `json:"name"`
|
||||
Success bool `json:"success"`
|
||||
Result interface{} `json:"result"`
|
||||
}
|
||||
|
||||
type ToolDef struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Parameters map[string]interface{} `json:"parameters"`
|
||||
}
|
||||
|
||||
type ToolRegistrar func(name string, def ToolDef, handler ToolHandler) error
|
||||
type StageRegistrar func(stage Stage, handler StageHandler)
|
||||
type APIRegistrar func(name string) error
|
||||
|
||||
type PluginSDK struct {
|
||||
name string
|
||||
|
||||
iom *agentIO.IOManager
|
||||
eventBus *events.Bus
|
||||
mem MemoryAPI
|
||||
textMem TextMemoryAPI
|
||||
docMem DocMemoryAPI
|
||||
know KnowledgeAPI
|
||||
llm LLMAPI
|
||||
sett SettingsAPI
|
||||
regTool ToolRegistrar
|
||||
regStage StageRegistrar
|
||||
regAPI APIRegistrar
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func New(name string, iom *agentIO.IOManager, eventBus *events.Bus, mem MemoryAPI, textMem TextMemoryAPI, docMem DocMemoryAPI, know KnowledgeAPI, llm LLMAPI, sett SettingsAPI, regTool ToolRegistrar, regStage StageRegistrar, regAPI APIRegistrar) *PluginSDK {
|
||||
return &PluginSDK{
|
||||
name: name,
|
||||
iom: iom,
|
||||
eventBus: eventBus,
|
||||
mem: mem,
|
||||
textMem: textMem,
|
||||
docMem: docMem,
|
||||
know: know,
|
||||
llm: llm,
|
||||
sett: sett,
|
||||
regTool: regTool,
|
||||
regStage: regStage,
|
||||
regAPI: regAPI,
|
||||
logger: log.Default(),
|
||||
}
|
||||
}
|
||||
|
||||
// === IO 双通道 ===
|
||||
|
||||
func (s *PluginSDK) InjectInput(source, channel string, payload map[string]interface{}) {
|
||||
if s.iom != nil {
|
||||
s.iom.InjectInputTo(source, channel, "text", payload)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PluginSDK) InjectInterrupt(source, channel string, payload map[string]interface{}) {
|
||||
if s.iom != nil {
|
||||
p := payload
|
||||
if p == nil {
|
||||
p = map[string]interface{}{}
|
||||
}
|
||||
if _, ok := p["type"]; !ok {
|
||||
p["type"] = "text"
|
||||
}
|
||||
s.iom.InjectInterrupt(source, channel, p)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PluginSDK) InjectText(source, channel, text string) {
|
||||
if s.iom != nil {
|
||||
s.iom.InjectTextTo(source, channel, text)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PluginSDK) InjectTextSync(source, channel, text string) *agentIO.OutputEvent {
|
||||
if s.iom != nil {
|
||||
return s.iom.InjectTextSyncTo(source, channel, text)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PluginSDK) InjectInterruptText(source, channel, text string) {
|
||||
if s.iom != nil {
|
||||
s.iom.InjectInterruptText(source, channel, text)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PluginSDK) OutputChan() <-chan *agentIO.OutputEvent {
|
||||
if s.iom != nil {
|
||||
return s.iom.OutputChan()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PluginSDK) RegisterChannel(name string, dev agentIO.Device) error {
|
||||
if s.iom != nil {
|
||||
return s.iom.RegisterDevice(dev)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PluginSDK) UnregisterChannel(name string) {
|
||||
if s.iom != nil {
|
||||
s.iom.UnregisterDevice(name)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PluginSDK) ListChannels() []agentIO.ChannelInfo {
|
||||
if s.iom != nil {
|
||||
return s.iom.ListChannels()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// === 三通道 ===
|
||||
|
||||
func (s *PluginSDK) Publish(evt *events.Event) {
|
||||
if s.eventBus != nil {
|
||||
s.eventBus.Publish(evt)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PluginSDK) Subscribe(eventType events.EventType, handler events.Handler) func() {
|
||||
if s.eventBus != nil {
|
||||
return s.eventBus.Subscribe(eventType, handler)
|
||||
}
|
||||
return func() {}
|
||||
}
|
||||
|
||||
// === 能力 ===
|
||||
|
||||
func (s *PluginSDK) Memory() MemoryAPI { return s.mem }
|
||||
func (s *PluginSDK) TextMemory() TextMemoryAPI { return s.textMem }
|
||||
func (s *PluginSDK) DocMemory() DocMemoryAPI { return s.docMem }
|
||||
func (s *PluginSDK) Knowledge() KnowledgeAPI { return s.know }
|
||||
func (s *PluginSDK) LLM() LLMAPI { return s.llm }
|
||||
func (s *PluginSDK) Settings() SettingsAPI { return s.sett }
|
||||
|
||||
func (s *PluginSDK) RegisterTool(name string, def ToolDef, handler ToolHandler) error {
|
||||
if s.regTool != nil {
|
||||
return s.regTool(name, def, handler)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PluginSDK) RegisterStage(stage Stage, handler StageHandler) {
|
||||
if s.regStage != nil {
|
||||
s.regStage(stage, handler)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PluginSDK) RegisterPluginAPI(name string) error {
|
||||
if s.regAPI != nil {
|
||||
return s.regAPI(name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PluginSDK) PluginName() string { return s.name }
|
||||
func (s *PluginSDK) Logger() *log.Logger { return s.logger }
|
||||
121
internal/sdk/settings.go
Normal file
121
internal/sdk/settings.go
Normal file
@ -0,0 +1,121 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
internalConfig "gitcode.com/JianFeeeee/HomeAgent/internal/config"
|
||||
)
|
||||
|
||||
type SettingsAPI interface {
|
||||
// 插件自身配置表 config_<name>
|
||||
Get(key string) (interface{}, error)
|
||||
Set(key string, value interface{}) error
|
||||
List(prefix string) ([]string, error)
|
||||
|
||||
// 核心配置表 config
|
||||
GetCore(key string) (interface{}, error)
|
||||
SetCore(key string, value interface{}) error
|
||||
ListCore(prefix string) ([]string, error)
|
||||
|
||||
// 任意插件配置表 config_<plugin>
|
||||
GetPlugin(plugin, key string) (interface{}, error)
|
||||
SetPlugin(plugin, key string, value interface{}) error
|
||||
ListPlugin(plugin, prefix string) ([]string, error)
|
||||
|
||||
// 全局
|
||||
Dump() map[string]interface{}
|
||||
Plugins() []string
|
||||
}
|
||||
|
||||
type settingsImpl struct {
|
||||
pluginName string
|
||||
reg *internalConfig.ConfigRegistry
|
||||
}
|
||||
|
||||
func NewSettings(name string, reg *internalConfig.ConfigRegistry) SettingsAPI {
|
||||
return &settingsImpl{pluginName: name, reg: reg}
|
||||
}
|
||||
|
||||
func (s *settingsImpl) Get(key string) (interface{}, error) {
|
||||
if s.reg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.reg.PluginConfig(s.pluginName).Get(key)
|
||||
}
|
||||
|
||||
func (s *settingsImpl) Set(key string, value interface{}) error {
|
||||
if s.reg == nil {
|
||||
return nil
|
||||
}
|
||||
return s.reg.PluginConfig(s.pluginName).Set(key, value)
|
||||
}
|
||||
|
||||
func (s *settingsImpl) List(prefix string) ([]string, error) {
|
||||
if s.reg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.reg.PluginConfig(s.pluginName).List(prefix)
|
||||
}
|
||||
|
||||
func (s *settingsImpl) GetCore(key string) (interface{}, error) {
|
||||
if s.reg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.reg.Get(key)
|
||||
}
|
||||
|
||||
func (s *settingsImpl) SetCore(key string, value interface{}) error {
|
||||
if s.reg == nil {
|
||||
return nil
|
||||
}
|
||||
return s.reg.Set(key, value)
|
||||
}
|
||||
|
||||
func (s *settingsImpl) ListCore(prefix string) ([]string, error) {
|
||||
if s.reg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.reg.List(prefix), nil
|
||||
}
|
||||
|
||||
func (s *settingsImpl) Dump() map[string]interface{} {
|
||||
if s.reg == nil {
|
||||
return nil
|
||||
}
|
||||
return s.reg.Dump()
|
||||
}
|
||||
|
||||
func (s *settingsImpl) GetPlugin(plugin, key string) (interface{}, error) {
|
||||
if s.reg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.reg.PluginConfig(plugin).Get(key)
|
||||
}
|
||||
|
||||
func (s *settingsImpl) SetPlugin(plugin, key string, value interface{}) error {
|
||||
if s.reg == nil {
|
||||
return nil
|
||||
}
|
||||
return s.reg.PluginConfig(plugin).Set(key, value)
|
||||
}
|
||||
|
||||
func (s *settingsImpl) ListPlugin(plugin, prefix string) ([]string, error) {
|
||||
if s.reg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.reg.PluginConfig(plugin).List(prefix)
|
||||
}
|
||||
|
||||
func (s *settingsImpl) Plugins() []string {
|
||||
if s.reg == nil {
|
||||
return nil
|
||||
}
|
||||
keys := s.reg.List("config_")
|
||||
names := make([]string, 0, len(keys)+1)
|
||||
names = append(names, "core")
|
||||
for _, k := range keys {
|
||||
// config_xxx → xxx
|
||||
if len(k) > 7 {
|
||||
names = append(names, k[7:])
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
Reference in New Issue
Block a user