mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-11-01 06:47:29 +00:00
记忆层: - MemoryBlock 承载 FrameTS/PageSeq/PageKind/BlockRole/CallID - coldcontext 归档与回填按完整页传递,工具调用参数和配对键不丢失 - 召回先估算总 token,超预算时按全局页相似度标记 inactive - inactive 仅是 context 渲染期状态,不落盘、不进入 coldcontext,Prune 后重置 - Peek/确认/Remove 两阶段召回,失败保留冷上下文副本 - stablemem 记录 last_recalled_at,删块连带清理边 命名: - internal/memory/document → internal/memory/coldcontext - Doc/DocEvent/DocHit → ColdDoc/ColdEvent/ColdHit - ContextToColdContext/RecentColdContexts/FindColdContexts - 保留既有 JSON 标签、doc_ 文件前缀与 doc_count 状态键,兼容存量数据 安全治理: - 工具策略门禁、安全配置、事件总线发布者身份与入站脱敏 - 声明式 SecurityAPI 与 QQ owner/guest profile - 日志保留策略与相关 e2e/回归判据 验证:go build ./...、go vet ./...、go test ./...(62 包); 生产副本 e2e:7013 块迁移后 ID/向量/指纹 0 变化、0 缺失。
160 lines
4.6 KiB
Go
160 lines
4.6 KiB
Go
package core
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
agentAPI "github.com/JianFeeeee/HologramAgent/internal/agent/api"
|
|
agentIO "github.com/JianFeeeee/HologramAgent/internal/agent/io"
|
|
)
|
|
|
|
type mockOutputDevice struct {
|
|
name string
|
|
caps agentIO.OutputCapability
|
|
tools []agentIO.ToolDef
|
|
toolFn func(string, map[string]interface{}) (interface{}, error)
|
|
}
|
|
|
|
func (d *mockOutputDevice) Name() string { return d.name }
|
|
func (d *mockOutputDevice) Type() agentIO.DeviceType { return agentIO.DeviceOutput }
|
|
func (d *mockOutputDevice) Description() string { return "mock " + d.name }
|
|
func (d *mockOutputDevice) Tools() []agentIO.ToolDef { return d.tools }
|
|
func (d *mockOutputDevice) Start() error { return nil }
|
|
func (d *mockOutputDevice) Stop() error { return nil }
|
|
func (d *mockOutputDevice) OutputCapabilities() agentIO.OutputCapability { return d.caps }
|
|
func (d *mockOutputDevice) ChannelDef() agentIO.ChannelDef { return agentIO.ChannelDef{} }
|
|
func (d *mockOutputDevice) Execute(tool string, args map[string]interface{}) (interface{}, error) {
|
|
if d.toolFn != nil {
|
|
return d.toolFn(tool, args)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func TestExecuteOutputListChannels(t *testing.T) {
|
|
io := agentIO.NewIOManager()
|
|
dev := &mockOutputDevice{
|
|
name: "speaker",
|
|
caps: agentIO.CapText | agentIO.CapAudio,
|
|
}
|
|
io.RegisterDevice(dev)
|
|
|
|
a := &Agent{io: io}
|
|
result := a.executeOutputListChannels()
|
|
if result == "" || result == "没有可用通道" {
|
|
t.Errorf("expected channel list, got: %s", result)
|
|
}
|
|
}
|
|
|
|
func TestExecuteOutputListChannelsEmpty(t *testing.T) {
|
|
io := agentIO.NewIOManager()
|
|
a := &Agent{io: io}
|
|
result := a.executeOutputListChannels()
|
|
if result != "没有可用通道" {
|
|
t.Errorf("expected '没有可用通道', got: %s", result)
|
|
}
|
|
}
|
|
|
|
func TestExecuteOutputSendTool(t *testing.T) {
|
|
io := agentIO.NewIOManager()
|
|
io.RegisterDevice(&mockOutputDevice{
|
|
name: "screen",
|
|
caps: agentIO.CapText,
|
|
})
|
|
|
|
a := &Agent{io: io}
|
|
tc := agentAPI.ToolCall{Name: "output_send__screen", Arguments: map[string]interface{}{
|
|
"payload": "hello world",
|
|
"type": "text",
|
|
}}
|
|
result := a.executeOutputSendTool(tc)
|
|
if result != "ok" {
|
|
t.Errorf("expected ok, got: %s", result)
|
|
}
|
|
}
|
|
|
|
func TestExecuteOutputSendToolEmptyContent(t *testing.T) {
|
|
io := agentIO.NewIOManager()
|
|
a := &Agent{io: io}
|
|
tc := agentAPI.ToolCall{Name: "output_send__screen", Arguments: map[string]interface{}{}}
|
|
result := a.executeOutputSendTool(tc)
|
|
if result == "" || strings.Contains(result, "已通过") {
|
|
t.Errorf("expected error for empty content, got: %s", result)
|
|
}
|
|
}
|
|
|
|
func TestExecuteOutputSendToolChannelNotExist(t *testing.T) {
|
|
io := agentIO.NewIOManager()
|
|
a := &Agent{io: io}
|
|
tc := agentAPI.ToolCall{Name: "output_send__nonexistent", Arguments: map[string]interface{}{
|
|
"payload": "hello",
|
|
"type": "text",
|
|
}}
|
|
result := a.executeOutputSendTool(tc)
|
|
if !strings.Contains(result, "不存在") && !strings.Contains(result, "不可用") {
|
|
t.Errorf("expected error for nonexistent channel, got: %s", result)
|
|
}
|
|
}
|
|
|
|
func TestExecuteOutputSendToolNoTextCap(t *testing.T) {
|
|
io := agentIO.NewIOManager()
|
|
io.RegisterDevice(&mockOutputDevice{
|
|
name: "camera",
|
|
caps: agentIO.CapImage,
|
|
})
|
|
|
|
a := &Agent{io: io}
|
|
tc := agentAPI.ToolCall{Name: "output_send__camera", Arguments: map[string]interface{}{
|
|
"payload": "hello",
|
|
"type": "text",
|
|
}}
|
|
result := a.executeOutputSendTool(tc)
|
|
if strings.Contains(result, "已通过") {
|
|
t.Errorf("should reject channel without text capability")
|
|
}
|
|
}
|
|
|
|
func TestBuildToolDefsOutputToolsWithDevice(t *testing.T) {
|
|
io := agentIO.NewIOManager()
|
|
io.RegisterDevice(&mockOutputDevice{
|
|
name: "screen",
|
|
caps: agentIO.CapText,
|
|
})
|
|
a := &Agent{io: io, knowledge: nil, coldcontextStore: nil, pluginReg: nil}
|
|
tools := a.buildToolDefs()
|
|
|
|
foundSend := false
|
|
foundHelp := false
|
|
for _, td := range tools {
|
|
m, ok := td.(map[string]interface{})
|
|
if !ok {
|
|
continue
|
|
}
|
|
fn, ok := m["function"].(map[string]interface{})
|
|
if !ok {
|
|
continue
|
|
}
|
|
name, _ := fn["name"].(string)
|
|
switch name {
|
|
case "output_send__screen":
|
|
foundSend = true
|
|
case "output_send__screen_help":
|
|
foundHelp = true
|
|
}
|
|
}
|
|
if !foundSend {
|
|
t.Error("output_send__screen should be in tools when device registered")
|
|
}
|
|
if !foundHelp {
|
|
t.Error("output_send__screen_help should be in tools when device registered")
|
|
}
|
|
}
|
|
|
|
func TestGetAllToolsEmpty(t *testing.T) {
|
|
io := agentIO.NewIOManager()
|
|
a := &Agent{io: io}
|
|
tools := a.buildToolDefs()
|
|
if len(tools) < 1 {
|
|
t.Errorf("expected at least 1 tool, got %d", len(tools))
|
|
}
|
|
}
|