mirror of
https://gitcode.com/JianFeeeee/homeagent-sdk.git
synced 2026-10-31 22:37:25 +00:00
feat(examples): 内置插件外置化 —— cmd/localuse/mcp 迁入 example + files 新增按行号精确编辑
## 背景 主仓按「只有必须使用内部 SDK 能力的插件才内置」的判定标准(见主仓 ARCHITECTURE「内置插件的判定标准」)把纯公开 SDK 插件移出编译期内置清单。 本仓为它们提供外部插件工程(example/ 下,hmapdev 可直接构建)。 ★ 终审翻案:kbtree 与 multimodal **不**在本次外置之列 —— 逐方法对照 公开 SDK 后发现它们依赖内部专有能力(kbtree 要 KnowledgeAPI 的 Subtree/CategoryCounts/SearchIn/Categories 与树视图类型;multimodal 要 LLMAPI.Chat 与多模态消息类型),公开面没有,已加回主仓内置清单。 ## 新增工程(全部对齐当前模板 + 随用随起契约) | 工程 | 工具 | auto_start | 说明 | |---|---|---|---| | cmd | cmd_run | false(按需) | 沙箱执行 shell 命令 | | localuse | screensee/screensue/camerasue/speakeruse/clipboardsee/clipboardsue/computeruse | false(按需) | 本机外设 | | mcp | mcp_add/list/remove/restart_server | false(按需) | MCP 服务器接入(stdio/SSE) | 每工程:plg.json(含 sdk 字段声明)+ skill/<name>-tools/SKILL.md + README。按 auto_start 契约(sdk/autostart.go 的 OnDemandNeedsSkill), **auto_start=false 必须配 skill**,否则工具不出现在初始清单 ⇒ 模型不知道 它存在 ⇒ 永远不会被拉起,等于没装还占地方。 ## 移植要点 - 注册:内置版 init()+RegisterFactory → 外部版导出 NewPluginFactory; RegisterPluginMeta 的显示名进 plg.json/plugin.json。 - mcp:本地 rpcRequest/rpcResponse/rpcError 与 hmapdev 生成的 z_proc_gen.go 同名冲突(构建期生成物与本目录源码同包编译), 本地类型改名 mcpRPC*;internal/safeclose 内联为 safeCloseChan (外部插件不能引 internal 包,close-of-closed-channel 纪律随之带过来)。 - module 名:cmd 的 go.mod 用 homeagent-cmd(避免与标准库 cmd 混淆); name_en 对齐插件名,hmapdev 以 toSnake(name_en) 命名产物 (此前误写 Command/Local,产物成了 command_/local_)。 ## files:新增 files_edit_line(按行号精确编辑) files_edit 要求 old 文本全文件唯一,改「第三个 case 的 break」这类 非唯一文本时只能退化为整文件重写(有损)。files_edit_line 按行号 定位,op=replace/insert/delete,支持: - 多条 edit 从后往前应用(前面行号不漂移) - expect 安全闸:行内容与预期不符则整体拒绝(防行号过期改错位置) - CRLF/末尾换行保真 skill 同步补 edit_line 行。测试 5 条(含 expect 拒绝路径)。 ## 验证 hmapdev build 四工程全过:cmd/localuse/mcp/files 产物齐; go test files 全绿。
This commit is contained in:
100
example/files/editline_test.go
Normal file
100
example/files/editline_test.go
Normal file
@ -0,0 +1,100 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func newTestPlugin(t *testing.T) *Plugin {
|
||||
t.Helper()
|
||||
inst, err := NewPluginFactory("files", nil)
|
||||
if err != nil { t.Fatal(err) }
|
||||
return inst.(*Plugin)
|
||||
}
|
||||
|
||||
// 精确替换:old 在文件里出现多次时 files_edit 拒绝,edit_line 按行号可改。
|
||||
func TestEditLineReplacesNonUniqueLine(t *testing.T) {
|
||||
p := newTestPlugin(t)
|
||||
dir := t.TempDir()
|
||||
p.filesDir = dir
|
||||
file := filepath.Join(dir, "a.go")
|
||||
content := "return nil\nx := 1\nreturn nil\n"
|
||||
os.WriteFile(file, []byte(content), 0644)
|
||||
|
||||
res, err := p.handleEditLine(map[string]interface{}{
|
||||
"path": file,
|
||||
"edits": []interface{}{map[string]interface{}{
|
||||
"line": float64(3), "op": "replace", "text": "return err",
|
||||
}},
|
||||
})
|
||||
if err != nil { t.Fatal(err) }
|
||||
_ = res
|
||||
got, _ := os.ReadFile(file)
|
||||
want := "return nil\nx := 1\nreturn err\n"
|
||||
if string(got) != want {
|
||||
t.Fatalf("第3行应被替换。\nwant: %q\ngot: %q", want, string(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEditLineExpectGuard(t *testing.T) {
|
||||
p := newTestPlugin(t)
|
||||
dir := t.TempDir()
|
||||
p.filesDir = dir
|
||||
file := filepath.Join(dir, "a.txt")
|
||||
os.WriteFile(file, []byte("alpha\nbeta\ngamma\n"), 0644)
|
||||
|
||||
// 本插件的约定:工具级错误经**返回值**(isError:true)回传而非 Go error,
|
||||
// 与 handleEdit 一致 —— 断言返回体而不是 err。
|
||||
res, _ := p.handleEditLine(map[string]interface{}{
|
||||
"path": file,
|
||||
"edits": []interface{}{map[string]interface{}{
|
||||
"line": float64(2), "op": "replace", "text": "BETA", "expect": "delta",
|
||||
}},
|
||||
})
|
||||
m, _ := res.(map[string]interface{})
|
||||
if m == nil || m["isError"] != true {
|
||||
t.Fatalf("expect 不匹配应返回 isError:true,实际: %v", res)
|
||||
}
|
||||
content, _ := m["content"].(string)
|
||||
if !strings.Contains(content, "expected") {
|
||||
t.Fatalf("错误内容应指出 expect 不匹配: %s", content)
|
||||
}
|
||||
got, _ := os.ReadFile(file)
|
||||
if string(got) != "alpha\nbeta\ngamma\n" { t.Fatal("失败时文件不得被改") }
|
||||
}
|
||||
|
||||
func TestEditLineInsertAndDelete(t *testing.T) {
|
||||
p := newTestPlugin(t)
|
||||
dir := t.TempDir()
|
||||
p.filesDir = dir
|
||||
file := filepath.Join(dir, "a.txt")
|
||||
os.WriteFile(file, []byte("one\ntwo\n"), 0644)
|
||||
|
||||
if _, err := p.handleEditLine(map[string]interface{}{
|
||||
"path": file,
|
||||
"edits": []interface{}{
|
||||
map[string]interface{}{"line": float64(2), "op": "insert", "text": "one-five\none-b"}, // 插在 two 前
|
||||
map[string]interface{}{"line": float64(1), "op": "delete"},
|
||||
},
|
||||
}); err != nil { t.Fatal(err) }
|
||||
got, _ := os.ReadFile(file)
|
||||
// 从后往前应用:先删 line1(one),再在 line2(原 two)前插两行
|
||||
want := "one-five\none-b\ntwo\n"
|
||||
if string(got) != want { t.Fatalf("want %q got %q", want, string(got)) }
|
||||
}
|
||||
|
||||
func TestEditLineAppendAtEnd(t *testing.T) {
|
||||
p := newTestPlugin(t)
|
||||
dir := t.TempDir()
|
||||
p.filesDir = dir
|
||||
file := filepath.Join(dir, "a.txt")
|
||||
os.WriteFile(file, []byte("a\nb\n"), 0644)
|
||||
if _, err := p.handleEditLine(map[string]interface{}{
|
||||
"path": file,
|
||||
"edits": []interface{}{map[string]interface{}{"line": float64(3), "op": "insert", "text": "c"}},
|
||||
}); err != nil { t.Fatal(err) }
|
||||
got, _ := os.ReadFile(file)
|
||||
if string(got) != "a\nb\nc\n" { t.Fatalf("want a\\nb\\nc\\n got %q", string(got)) }
|
||||
}
|
||||
Reference in New Issue
Block a user