mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 09:28:14 +00:00
feat: converge dynamic plugins onto canonical homeagent-sdk
- Separate built-in plugin interface from external plugin interface - Route dynamic plugin loading through homeagent-sdk/sdk using reflection - Turn internal/sdk into an enhanced wrapper over canonical SDK types - Vendor SDK repo snapshot under third_party/homeagent-sdk for stable builds - Keep internal constructors/adapters for memory, knowledge, llm, settings - Align dynamic QQ loading with canonical SDK chain
This commit is contained in:
@ -1,38 +1,33 @@
|
||||
package sdk
|
||||
|
||||
import "gitcode.com/JianFeeeee/HomeAgent/internal/knowledge"
|
||||
import (
|
||||
sdkext "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||
"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 KnowledgeAPI = sdkext.KnowledgeAPI
|
||||
type Knowledge = sdkext.Knowledge
|
||||
|
||||
type knowledgeImpl struct {
|
||||
ks *knowledge.Store
|
||||
}
|
||||
type knowledgeImpl struct{ ks *knowledge.Store }
|
||||
|
||||
func NewKnowledge(ks *knowledge.Store) KnowledgeAPI {
|
||||
return &knowledgeImpl{ks: ks}
|
||||
}
|
||||
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
|
||||
func (k *knowledgeImpl) Search(query string, topK int) ([]*Knowledge, error) {
|
||||
if k.ks == nil { return nil, nil }
|
||||
got := k.ks.Search(query, topK)
|
||||
out := make([]*Knowledge, len(got))
|
||||
for i, item := range got {
|
||||
out[i] = &Knowledge{Name: item.Name, Content: item.Content}
|
||||
}
|
||||
return k.ks.Search(query, topK), nil
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (k *knowledgeImpl) Add(name, content string) error {
|
||||
if k.ks == nil {
|
||||
return nil
|
||||
}
|
||||
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
|
||||
}
|
||||
if k.ks == nil { return nil, nil }
|
||||
return k.ks.List(), nil
|
||||
}
|
||||
|
||||
@ -1,42 +1,29 @@
|
||||
package sdk
|
||||
|
||||
import agentAPI "gitcode.com/JianFeeeee/HomeAgent/internal/agent/api"
|
||||
import (
|
||||
sdkext "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||
agentAPI "gitcode.com/JianFeeeee/HomeAgent/internal/agent/api"
|
||||
)
|
||||
|
||||
type LLMAPI interface {
|
||||
ListSources() []string
|
||||
SetSource(name string) error
|
||||
CurrentSource() string
|
||||
}
|
||||
type LLMAPI = sdkext.LLMAPI
|
||||
|
||||
type llmImpl struct {
|
||||
mgr *agentAPI.ProviderManager
|
||||
}
|
||||
type llmImpl struct{ mgr *agentAPI.ProviderManager }
|
||||
|
||||
func NewLLM(mgr *agentAPI.ProviderManager) LLMAPI {
|
||||
return &llmImpl{mgr: mgr}
|
||||
}
|
||||
func NewLLM(mgr *agentAPI.ProviderManager) LLMAPI { return &llmImpl{mgr: mgr} }
|
||||
|
||||
func (l *llmImpl) ListSources() []string {
|
||||
if l.mgr == nil {
|
||||
return nil
|
||||
}
|
||||
if l.mgr == nil { return nil }
|
||||
return l.mgr.List()
|
||||
}
|
||||
|
||||
func (l *llmImpl) SetSource(name string) error {
|
||||
if l.mgr == nil {
|
||||
return nil
|
||||
}
|
||||
if l.mgr == nil { return nil }
|
||||
return l.mgr.SetDefault(name)
|
||||
}
|
||||
|
||||
func (l *llmImpl) CurrentSource() string {
|
||||
if l.mgr == nil {
|
||||
return ""
|
||||
}
|
||||
if l.mgr == nil { return "" }
|
||||
p := l.mgr.Default()
|
||||
if p == nil {
|
||||
return ""
|
||||
}
|
||||
if p == nil { return "" }
|
||||
return p.Name()
|
||||
}
|
||||
|
||||
@ -1,55 +1,26 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
sdkext "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||
"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 MemoryAPI = sdkext.MemoryAPI
|
||||
type Entity = sdkext.Entity
|
||||
type Relation = sdkext.Relation
|
||||
type Triple = sdkext.Triple
|
||||
|
||||
type Entity struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
MentionCount int `json:"mention_count"`
|
||||
}
|
||||
type TextMemoryAPI = sdkext.TextMemoryAPI
|
||||
type TextEvent = sdkext.TextEvent
|
||||
|
||||
type Relation struct {
|
||||
SourceName string `json:"source_name"`
|
||||
TargetName string `json:"target_name"`
|
||||
RelationType string `json:"relation_type"`
|
||||
}
|
||||
type DocMemoryAPI = sdkext.DocMemoryAPI
|
||||
type Doc = sdkext.Doc
|
||||
|
||||
type Triple struct {
|
||||
Subject string `json:"subject"`
|
||||
Relation string `json:"relation"`
|
||||
Object string `json:"object"`
|
||||
}
|
||||
type graphMemory struct{ db *memory.GraphDB }
|
||||
|
||||
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 NewGraphMemory(db *memory.GraphDB) MemoryAPI { return &graphMemory{db: db} }
|
||||
|
||||
func (m *graphMemory) Recall(query []string, depth int) ([]Entity, []Relation, error) {
|
||||
if m.db == nil {
|
||||
@ -71,9 +42,7 @@ func (m *graphMemory) Recall(query []string, depth int) ([]Entity, []Relation, e
|
||||
}
|
||||
|
||||
func (m *graphMemory) Commit(triples []Triple) error {
|
||||
if m.db == nil {
|
||||
return nil
|
||||
}
|
||||
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}
|
||||
@ -83,72 +52,50 @@ func (m *graphMemory) Commit(triples []Triple) error {
|
||||
}
|
||||
|
||||
func (m *graphMemory) Introspect() (map[string]interface{}, error) {
|
||||
if m.db == nil {
|
||||
return map[string]interface{}{}, nil
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
if m.db == nil { return 0, nil }
|
||||
return m.db.Purge(criteria, mode)
|
||||
}
|
||||
|
||||
type textMemoryImpl struct {
|
||||
tm *text.Memory
|
||||
type textMemoryImpl struct{ tm *text.Memory }
|
||||
|
||||
func NewTextMemory(tm *text.Memory) TextMemoryAPI { return &textMemoryImpl{tm: tm} }
|
||||
|
||||
func (m *textMemoryImpl) Append(evt TextEvent) error {
|
||||
if m.tm == nil { return nil }
|
||||
return m.tm.Append(text.Event{Timestamp: evt.Timestamp, Source: evt.Role, Input: evt.Content, AgentID: evt.Channel})
|
||||
}
|
||||
|
||||
func NewTextMemory(tm *text.Memory) TextMemoryAPI {
|
||||
return &textMemoryImpl{tm: tm}
|
||||
}
|
||||
type docMemoryImpl struct{ ds *doc.Store }
|
||||
|
||||
func (m *textMemoryImpl) Append(evt text.Event) error {
|
||||
if m.tm == nil {
|
||||
return nil
|
||||
func NewDocMemory(ds *doc.Store) DocMemoryAPI { return &docMemoryImpl{ds: ds} }
|
||||
|
||||
func (m *docMemoryImpl) Query(text string, topK int) []*Doc {
|
||||
if m.ds == nil { return nil }
|
||||
got := m.ds.Query(text, topK)
|
||||
out := make([]*Doc, len(got))
|
||||
for i, d := range got {
|
||||
out[i] = &Doc{ID: d.ID, Title: d.Summary, Content: d.Content}
|
||||
}
|
||||
return m.tm.Append(evt)
|
||||
return out
|
||||
}
|
||||
|
||||
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) Insert(d *Doc) error {
|
||||
if m.ds == nil { return nil }
|
||||
return m.ds.Insert(&doc.Doc{ID: d.ID, Summary: d.Title, Content: d.Content})
|
||||
}
|
||||
|
||||
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{}{}
|
||||
}
|
||||
if m.ds == nil { return map[string]interface{}{} }
|
||||
return m.ds.Stats()
|
||||
}
|
||||
|
||||
@ -2,8 +2,8 @@ package sdk
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
sdkext "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||
agentIO "gitcode.com/JianFeeeee/HomeAgent/internal/agent/io"
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/events"
|
||||
)
|
||||
@ -14,125 +14,80 @@ type Plugin interface {
|
||||
Stop() error
|
||||
}
|
||||
|
||||
type ToolHandler func(args map[string]interface{}) (interface{}, error)
|
||||
type StageHandler func(ctx *StageContext) error
|
||||
type ToolHandler = sdkext.ToolHandler
|
||||
type StageHandler = sdkext.StageHandler
|
||||
|
||||
type Stage string
|
||||
type Stage = sdkext.Stage
|
||||
|
||||
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"
|
||||
StageOnInput = sdkext.StageOnInput
|
||||
StagePreAction = sdkext.StagePreAction
|
||||
StagePostAction = sdkext.StagePostAction
|
||||
StageBeforeToolcall = sdkext.StageBeforeToolcall
|
||||
StageAfterToolcall = sdkext.StageAfterToolcall
|
||||
StageBeforeOutput = sdkext.StageBeforeOutput
|
||||
StageAfterOutput = sdkext.StageAfterOutput
|
||||
)
|
||||
|
||||
type StageContext struct {
|
||||
mu sync.RWMutex
|
||||
RawMessage string
|
||||
UserID string
|
||||
GroupID string
|
||||
ContextMsgs []map[string]interface{}
|
||||
LLMText string
|
||||
ReasoningContent string
|
||||
TokenUsage map[string]int
|
||||
ToolCalls []ToolCall
|
||||
ToolResults []ToolResult
|
||||
FinalText string
|
||||
Response *string
|
||||
Phase Stage
|
||||
Memory []MemItem
|
||||
NoMemory bool
|
||||
Extra map[string]interface{}
|
||||
}
|
||||
type StageContext = sdkext.StageContext
|
||||
type MemItem = sdkext.MemItem
|
||||
type ToolCall = sdkext.ToolCall
|
||||
type ToolResult = sdkext.ToolResult
|
||||
type ToolDef = sdkext.ToolDef
|
||||
|
||||
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 ToolRegistrar = func(name string, def ToolDef, handler ToolHandler) error
|
||||
type StageRegistrar = func(stage Stage, handler StageHandler)
|
||||
type APIRegistrar = func(name string) error
|
||||
|
||||
type MemItem struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
Score float64 `json:"score"`
|
||||
}
|
||||
type ioAdapter struct{ iom *agentIO.IOManager }
|
||||
|
||||
type ToolCall struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Plugin string `json:"plugin,omitempty"`
|
||||
Arguments map[string]interface{} `json:"arguments"`
|
||||
}
|
||||
|
||||
type ToolResult struct {
|
||||
CallID string `json:"call_id"`
|
||||
Name string `json:"name"`
|
||||
Plugin string `json:"plugin,omitempty"`
|
||||
Success bool `json:"success"`
|
||||
Result interface{} `json:"result"`
|
||||
}
|
||||
|
||||
type ToolDef struct {
|
||||
Name string `json:"name"`
|
||||
Plugin string `json:"plugin,omitempty"`
|
||||
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(),
|
||||
func (i ioAdapter) InjectInterruptText(source, channel, text string) {
|
||||
if i.iom != nil {
|
||||
i.iom.InjectInterrupt(source, channel, map[string]interface{}{"type": "text", "content": text})
|
||||
}
|
||||
}
|
||||
|
||||
// === IO 双通道 ===
|
||||
func (i ioAdapter) InjectText(source, channel, text string) {
|
||||
if i.iom != nil {
|
||||
i.iom.InjectInputTo(source, channel, "text", map[string]interface{}{"content": text})
|
||||
}
|
||||
}
|
||||
|
||||
func (i ioAdapter) InjectTextNoMemory(source, channel, text string) {
|
||||
if i.iom != nil {
|
||||
i.iom.InjectInputTo(source, channel, "text", map[string]interface{}{"content": text, "no_memory": true})
|
||||
}
|
||||
}
|
||||
|
||||
type PluginSDK struct {
|
||||
*sdkext.PluginSDK
|
||||
iom *agentIO.IOManager
|
||||
eventBus *events.Bus
|
||||
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 {
|
||||
base := sdkext.New(name, sett, regTool, regStage, regAPI)
|
||||
base.SetIOInjector(ioAdapter{iom: iom})
|
||||
base.SetMemoryAPI(mem)
|
||||
base.SetTextMemoryAPI(textMem)
|
||||
base.SetDocMemoryAPI(docMem)
|
||||
base.SetKnowledgeAPI(know)
|
||||
base.SetLLMAPI(llm)
|
||||
return &PluginSDK{
|
||||
PluginSDK: base,
|
||||
iom: iom,
|
||||
eventBus: eventBus,
|
||||
logger: log.Default(),
|
||||
}
|
||||
}
|
||||
|
||||
// InjectInput 注入任意类型的输入事件。
|
||||
// eventType 可选值: "text", "event", "command", 或插件自定义类型。
|
||||
// payload 可包含 "content" (文本), "image" (图片), "file" (文件), "audio" (音频) 等字段。
|
||||
func (s *PluginSDK) InjectInput(source, channel, eventType string, payload map[string]interface{}) {
|
||||
if s.iom != nil {
|
||||
s.iom.InjectInputTo(source, channel, eventType, payload)
|
||||
}
|
||||
}
|
||||
|
||||
// InjectInputSync 注入任意类型输入并同步等待响应。
|
||||
func (s *PluginSDK) InjectInputSync(source, channel, eventType string, payload map[string]interface{}) *agentIO.OutputEvent {
|
||||
if s.iom != nil {
|
||||
return s.iom.InjectInputSyncTo(source, channel, eventType, payload)
|
||||
@ -140,8 +95,6 @@ func (s *PluginSDK) InjectInputSync(source, channel, eventType string, payload m
|
||||
return nil
|
||||
}
|
||||
|
||||
// InjectInterrupt 向中断通道注入任意类型的输入事件,可打断当前 LLM 处理。
|
||||
// eventType 会被写入 payload["type"]。
|
||||
func (s *PluginSDK) InjectInterrupt(source, channel, eventType string, payload map[string]interface{}) {
|
||||
if s.iom != nil {
|
||||
if payload == nil {
|
||||
@ -152,30 +105,14 @@ func (s *PluginSDK) InjectInterrupt(source, channel, eventType string, payload m
|
||||
}
|
||||
}
|
||||
|
||||
// 以下 InjectText* / InjectInterruptText 为快捷方式,等价于调用对应的泛型方法并传入 "text" 类型。
|
||||
|
||||
func (s *PluginSDK) InjectText(source, channel, text string) {
|
||||
s.InjectInput(source, channel, "text", map[string]interface{}{"content": text})
|
||||
}
|
||||
|
||||
// InjectTextNoMemory 注入文本输入(不产生记忆)。适用于健康检查等无需记忆碎片的场景。
|
||||
func (s *PluginSDK) InjectTextNoMemory(source, channel, text string) {
|
||||
s.InjectInput(source, channel, "text", map[string]interface{}{"content": text, "no_memory": true})
|
||||
}
|
||||
|
||||
func (s *PluginSDK) InjectTextSync(source, channel, text string) *agentIO.OutputEvent {
|
||||
return s.InjectInputSync(source, channel, "text", map[string]interface{}{"content": text})
|
||||
}
|
||||
|
||||
// InjectTextSyncNoMemory 注入文本输入(同步等待,不产生记忆)。
|
||||
func (s *PluginSDK) InjectTextSyncNoMemory(source, channel, text string) *agentIO.OutputEvent {
|
||||
return s.InjectInputSync(source, channel, "text", map[string]interface{}{"content": text, "no_memory": true})
|
||||
}
|
||||
|
||||
func (s *PluginSDK) InjectInterruptText(source, channel, text string) {
|
||||
s.InjectInterrupt(source, channel, "text", map[string]interface{}{"content": text})
|
||||
}
|
||||
|
||||
func (s *PluginSDK) OutputChan() <-chan *agentIO.OutputEvent {
|
||||
if s.iom != nil {
|
||||
return s.iom.OutputChan()
|
||||
@ -203,8 +140,6 @@ func (s *PluginSDK) ListChannels() []agentIO.ChannelInfo {
|
||||
return nil
|
||||
}
|
||||
|
||||
// === 三通道 ===
|
||||
|
||||
func (s *PluginSDK) Publish(evt *events.Event) {
|
||||
if s.eventBus != nil {
|
||||
s.eventBus.Publish(evt)
|
||||
@ -218,64 +153,4 @@ func (s *PluginSDK) Subscribe(eventType events.EventType, handler events.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 def.Plugin == "" {
|
||||
def.Plugin = s.name
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterStageOwnTools 仅在 before_toolcall / after_toolcall 阶段监听当前插件自己的工具调用。
|
||||
// 其他阶段会退化为普通 RegisterStage。
|
||||
func (s *PluginSDK) RegisterStageOwnTools(stage Stage, handler StageHandler) {
|
||||
if s.regStage == nil {
|
||||
return
|
||||
}
|
||||
if stage != StageBeforeToolcall && stage != StageAfterToolcall {
|
||||
s.regStage(stage, handler)
|
||||
return
|
||||
}
|
||||
s.regStage(stage, func(ctx *StageContext) error {
|
||||
ctx.RLock()
|
||||
match := false
|
||||
switch stage {
|
||||
case StageBeforeToolcall:
|
||||
match = len(ctx.ToolCalls) > 0 && ctx.ToolCalls[0].Plugin == s.name
|
||||
case StageAfterToolcall:
|
||||
match = len(ctx.ToolResults) > 0 && ctx.ToolResults[0].Plugin == s.name
|
||||
}
|
||||
ctx.RUnlock()
|
||||
if !match {
|
||||
return nil
|
||||
}
|
||||
return handler(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
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 }
|
||||
func (s *PluginSDK) Logger() *log.Logger { return s.logger }
|
||||
|
||||
@ -1,35 +1,13 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
sdkext "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||||
internalConfig "gitcode.com/JianFeeeee/HomeAgent/internal/config"
|
||||
)
|
||||
|
||||
type ConfigDef = internalConfig.ConfigDef
|
||||
|
||||
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)
|
||||
|
||||
// 配置定义元信息
|
||||
RegisterDef(def internalConfig.ConfigDef)
|
||||
Defs(prefix string) []*internalConfig.ConfigDef
|
||||
|
||||
// 全局
|
||||
Dump() map[string]interface{}
|
||||
Plugins() []string
|
||||
}
|
||||
type ConfigDef = sdkext.ConfigDef
|
||||
type SettingsAPI = sdkext.SettingsAPI
|
||||
|
||||
type settingsImpl struct {
|
||||
pluginName string
|
||||
@ -41,101 +19,88 @@ func NewSettings(name string, reg *internalConfig.ConfigRegistry) SettingsAPI {
|
||||
}
|
||||
|
||||
func (s *settingsImpl) Get(key string) (interface{}, error) {
|
||||
if s.reg == nil {
|
||||
return nil, nil
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
if s.reg == nil { return nil, nil }
|
||||
return s.reg.List(prefix), nil
|
||||
}
|
||||
|
||||
func (s *settingsImpl) RegisterDef(def internalConfig.ConfigDef) {
|
||||
if s.reg == nil {
|
||||
return
|
||||
}
|
||||
s.reg.PluginConfig(s.pluginName).RegisterDef(def)
|
||||
}
|
||||
|
||||
func (s *settingsImpl) Defs(prefix string) []*internalConfig.ConfigDef {
|
||||
if s.reg == nil {
|
||||
return nil
|
||||
}
|
||||
return s.reg.PluginConfig(s.pluginName).ListDefs(prefix)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
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
|
||||
func (s *settingsImpl) RegisterDef(def sdkext.ConfigDef) {
|
||||
if s.reg == nil { return }
|
||||
s.reg.PluginConfig(s.pluginName).RegisterDef(internalConfig.ConfigDef{
|
||||
Key: def.Key, Type: def.Type, DisplayName: def.DisplayName, Description: def.Description,
|
||||
Category: def.Category, Options: def.Options,
|
||||
Default: stringifyDefault(def.Default),
|
||||
})
|
||||
}
|
||||
func (s *settingsImpl) Defs(prefix string) []*sdkext.ConfigDef {
|
||||
if s.reg == nil { return nil }
|
||||
defs := s.reg.PluginConfig(s.pluginName).ListDefs(prefix)
|
||||
out := make([]*sdkext.ConfigDef, len(defs))
|
||||
for i, d := range defs {
|
||||
cpy := sdkext.ConfigDef{
|
||||
Key: d.Key, Default: d.Default, Type: d.Type, DisplayName: d.DisplayName,
|
||||
Description: d.Description, Category: d.Category, Options: d.Options,
|
||||
}
|
||||
out[i] = &cpy
|
||||
}
|
||||
return out
|
||||
}
|
||||
func (s *settingsImpl) Dump() map[string]interface{} {
|
||||
if s.reg == nil { return nil }
|
||||
return s.reg.Dump()
|
||||
}
|
||||
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:])
|
||||
}
|
||||
if len(k) > 7 { names = append(names, k[7:]) }
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func stringifyDefault(v interface{}) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
return x
|
||||
case bool:
|
||||
if x { return "true" }
|
||||
return "false"
|
||||
default:
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user