mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-22 18:08:04 +00:00
原 Handle 是一个 550 行的巨型 switch(C ABI 51 个 case 的整块平移),
按协议面拆进同包 5 个新文件、14 个小函数:
corehandler_register.go handleRegister 注册面(tool/stage/output/api/input)
corehandler_inject.go handleInject IO 注入 + SetToolBlocks
corehandler_memory.go handleGraphMemory 图记忆
handleDocMemory 文档记忆
handleKnowledge 知识库
handleTextMemory 文本记忆
corehandler_settings.go handleSettings 设置(14 个 method 共用一条实现)
handleLLM LLM 源
handleSocial 社交图只读
handleLifecycle 生命周期开关
corehandler_runtime.go handlePluginMgr 插件管理
handleStageLocks 段锁仲裁
handleEvents 事件订阅
handleArena 共享槽池
Handle 保留 capability 强制检查,只做「method → 分部函数」一跳。
零漂移保证:case 标签由脚本从原文提取(不手抄常量名),case 体逐字搬迁,
逐函数比对确认 59 个标签 / 510 行 case 体与原文件完全一致(仅行首缩进经 gofmt 重排)。
验证:go build ./... / go vet / go test ./internal/plugin/... / -race 全绿。
149 lines
4.7 KiB
Go
149 lines
4.7 KiB
Go
package proc
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
|
||
pubsdk "gitcode.com/JianFeeeee/homeagent-sdk/sdk"
|
||
)
|
||
|
||
// handlePluginMgr 处理插件管理:reloadOne / listLoaded / isDisabled。
|
||
//
|
||
// 本函数体是 corehandler.go 里 Handle 那一个大 switch 的**整块平移**:
|
||
// case 标签与 case 体逐字保留,只换了宿主函数(§3.2 的平移原则)。
|
||
func (h *coreHandler) handlePluginMgr(method string, params json.RawMessage) (interface{}, error) {
|
||
switch method {
|
||
// ---- 插件管理(原 case 48/49/50)----
|
||
case MethodPluginReloadOne:
|
||
pm := h.sdk.PluginMgr()
|
||
if pm == nil {
|
||
return nil, errUnavailable("plugin manager")
|
||
}
|
||
var p struct {
|
||
Name string `json:"name"`
|
||
}
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, pm.ReloadOne(p.Name)
|
||
case MethodPluginListLoaded:
|
||
pm := h.sdk.PluginMgr()
|
||
if pm == nil {
|
||
return nil, errUnavailable("plugin manager")
|
||
}
|
||
list := pm.ListLoadedPlugins()
|
||
if list == nil {
|
||
list = []string{}
|
||
}
|
||
return map[string]interface{}{"plugins": list}, nil
|
||
case MethodPluginIsDisabled:
|
||
pm := h.sdk.PluginMgr()
|
||
if pm == nil {
|
||
return nil, errUnavailable("plugin manager")
|
||
}
|
||
var p struct {
|
||
Name string `json:"name"`
|
||
}
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
return map[string]interface{}{"disabled": pm.IsPluginDisabled(p.Name)}, nil
|
||
|
||
}
|
||
|
||
// 组内不应到达:Handle 的分派表与本函数的 case 标签同源,
|
||
// 出现即说明两处不同步。
|
||
return nil, fmt.Errorf("未知 method: %s", method)
|
||
}
|
||
|
||
// handleStageLocks 处理共享段锁仲裁(§3.7)。
|
||
//
|
||
// 本函数体是 corehandler.go 里 Handle 那一个大 switch 的**整块平移**:
|
||
// case 标签与 case 体逐字保留,只换了宿主函数(§3.2 的平移原则)。
|
||
func (h *coreHandler) handleStageLocks(method string, params json.RawMessage) (interface{}, error) {
|
||
switch method {
|
||
// ---- 共享段锁仲裁(新增,§3.7)----
|
||
case MethodStageLock:
|
||
if h.locks == nil {
|
||
return nil, fmt.Errorf("stage.lock: 锁仲裁未就绪")
|
||
}
|
||
return nil, h.locks.acquire(h.name)
|
||
case MethodStageUnlock:
|
||
if h.locks == nil {
|
||
return nil, fmt.Errorf("stage.unlock: 锁仲裁未就绪")
|
||
}
|
||
return nil, h.locks.release(h.name)
|
||
|
||
}
|
||
|
||
// 组内不应到达:Handle 的分派表与本函数的 case 标签同源,
|
||
// 出现即说明两处不同步。
|
||
return nil, fmt.Errorf("未知 method: %s", method)
|
||
}
|
||
|
||
// handleEvents 处理事件订阅(§3.6,子进程下首次真正可用)。
|
||
//
|
||
// 本函数体是 corehandler.go 里 Handle 那一个大 switch 的**整块平移**:
|
||
// case 标签与 case 体逐字保留,只换了宿主函数(§3.2 的平移原则)。
|
||
func (h *coreHandler) handleEvents(method string, params json.RawMessage) (interface{}, error) {
|
||
switch method {
|
||
// ---- 事件订阅(原 case 23/24,子进程下首次真正可用,§3.6)----
|
||
case MethodEventsSubscribe:
|
||
var p struct {
|
||
Types []pubsdk.EventType `json:"types"`
|
||
}
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
if h.evtRing == nil {
|
||
return nil, fmt.Errorf("%s: 事件环未就绪", method)
|
||
}
|
||
// 订阅请求来自子进程——handler 直接注册到 Bus,
|
||
// 事件经 EventRing 写入环后由子进程消费。
|
||
h.evtRing.EvtRingSubscribe(p.Types)
|
||
return nil, nil
|
||
|
||
case MethodEventsUnsubscribe:
|
||
// 事件环的订阅没有持久化句柄(取消函数由 Subscribe 返回但子进程未保存)。
|
||
// 当前设计:子进程 Stop 时由内核统一清理其订阅。
|
||
return nil, nil
|
||
|
||
}
|
||
|
||
// 组内不应到达:Handle 的分派表与本函数的 case 标签同源,
|
||
// 出现即说明两处不同步。
|
||
return nil, fmt.Errorf("未知 method: %s", method)
|
||
}
|
||
|
||
// handleArena 处理共享槽池分配/释放(内部传输层,见 protocol.go)。
|
||
//
|
||
// 本函数体是 corehandler.go 里 Handle 那一个大 switch 的**整块平移**:
|
||
// case 标签与 case 体逐字保留,只换了宿主函数(§3.2 的平移原则)。
|
||
func (h *coreHandler) handleArena(method string, params json.RawMessage) (interface{}, error) {
|
||
switch method {
|
||
// ---- 共享槽池(内部传输层,见 protocol.go 注释)----
|
||
case MethodArenaAlloc:
|
||
var p ArenaAllocParams
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
ref, err := h.arenaAlloc(p.Size)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return ArenaAllocResult{Ref: ref}, nil
|
||
|
||
case MethodArenaFree:
|
||
var p ArenaFreeParams
|
||
if err := unmarshal(params, &p); err != nil {
|
||
return nil, err
|
||
}
|
||
return nil, h.arenaFree(p.Ref)
|
||
|
||
}
|
||
|
||
// 组内不应到达:Handle 的分派表与本函数的 case 标签同源,
|
||
// 出现即说明两处不同步。
|
||
return nil, fmt.Errorf("未知 method: %s", method)
|
||
}
|