Files
HomeAgent/internal/agent/core/persona_test.go
JianFeeeee 46e014a8ca feat(persona): 首启人格门禁跨通道化 + 内核 persona_set 工具
WebUI 首启向导只覆盖 WebUI 这一条通道,而「人格该问一次」是所有通道的事:
走 QQ / CLI / ACP / 邮件来的人永远见不到那个向导,人格就永远是没确认过。

- 门禁移到 buildSystemPrompt(每轮重建 → WebUI/QQ/CLI/ACP/邮件全覆盖),
  以 core.internal.persona_initialized 为准:未确认时要求模型主动询问用户
  (默认 / 自定义 / 以后再说),确认后该段消失;personaStore 为 nil 时静默关闭。
- 新增内核内置工具 persona_set(mode=default|custom|later[, content]),
  落库逻辑与 WebUI 向导**共用 internal/config**(一个实现 + 两个薄入口:
  ConfigRegistry 直连 / 插件侧 SettingsAPI),避免两套语义各自漂移。
- AgentConfig 增加 PersonaStore 接口,cmd/homed 用 RegistryPersonaStore 实现。
- 非法输入(未知 mode / custom 空内容)在打标记**之前**拒绝:否则标记置位、
  向导被跳过,用户再没机会设。

E2E(隔离实例 + 真实 LLM 往返走 /v1/chat/completions,/var/tmp/persona/e2e.sh)9/9 PASS:
未确认时模型主动询问 → 用户答「用默认的」→ 模型调用 persona_set 落库并置位标记
→ 之后不再追问;反向对照(清标记 + 清会话上下文 + 重启)重新开始询问,
排除了「同一段对话里已问过」这一混淆。
2026-09-12 13:57:06 +08:00

113 lines
3.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package core
import (
"strings"
"testing"
agentAPI "gitcode.com/JianFeeeee/HomeAgent/internal/agent/api"
agentIO "gitcode.com/JianFeeeee/HomeAgent/internal/agent/io"
)
// newTestAgent 造一个最小可用的 AgentbuildToolDefs 要求 io 非 nil
func newTestAgent(st PersonaStore) *Agent {
return &Agent{io: agentIO.NewIOManager(), personaStore: st}
}
// fakePersonaStore 记录调用并可控地报告「是否已确认」。
type fakePersonaStore struct {
initialized bool
mode string
content string
calls int
}
func (f *fakePersonaStore) PersonaInitialized() bool { return f.initialized }
func (f *fakePersonaStore) SetPersona(mode, content string) (bool, error) {
f.calls++
f.mode, f.content = mode, content
f.initialized = true
return mode == "custom", nil
}
// 首启门禁:人格未确认时,**任何通道**的系统提示词都必须带上「去问用户」的指令;
// 确认后必须消失(否则会每轮反复追问)。
func TestPersonaOnboardingGateInSystemPrompt(t *testing.T) {
st := &fakePersonaStore{}
a := newTestAgent(st)
p := a.buildSystemPrompt("", "你好")
if !strings.Contains(p, "首启人格设定") || !strings.Contains(p, "persona_set") {
t.Fatalf("未确认人格时提示词应要求模型询问并调用 persona_set实际缺少该段")
}
// 模型落地后(标记置位)不再出现
if out := a.executePersonaTool(agentAPI.ToolCall{Name: "persona_set",
Arguments: map[string]interface{}{"mode": "default"}}); !strings.Contains(out, "默认人格") {
t.Fatalf("persona_set(default) 回执不对: %s", out)
}
if !st.initialized {
t.Fatal("落库后应置位标记")
}
if p2 := a.buildSystemPrompt("", "你好"); strings.Contains(p2, "首启人格设定") {
t.Fatal("人格已确认后不应再要求询问")
}
// 未接入配置personaStore 为 nil门禁与工具都必须静默关闭
b := newTestAgent(nil)
if pb := b.buildSystemPrompt("", "你好"); strings.Contains(pb, "首启人格设定") {
t.Fatal("未接入配置时不应出现首启门禁")
}
if out := b.executePersonaTool(agentAPI.ToolCall{Name: "persona_set"}); !strings.Contains(out, "不可用") {
t.Fatalf("未接入配置时工具应回明确错误,实际: %s", out)
}
}
// persona_set 的三选一语义与回执。
func TestPersonaSetToolModes(t *testing.T) {
cases := []struct {
mode, content, want string
}{
{"custom", "你是测试人格", "重启"},
{"default", "", "默认人格"},
{"later", "", "以后再说"},
}
for _, c := range cases {
st := &fakePersonaStore{}
a := newTestAgent(st)
out := a.executePersonaTool(agentAPI.ToolCall{Name: "persona_set",
Arguments: map[string]interface{}{"mode": c.mode, "content": c.content}})
if !strings.Contains(out, c.want) {
t.Errorf("mode=%s 回执应含 %q实际: %s", c.mode, c.want, out)
}
if st.calls != 1 || st.mode != c.mode || st.content != c.content {
t.Errorf("mode=%s 落库参数不对: calls=%d mode=%s content=%q", c.mode, st.calls, st.mode, st.content)
}
}
}
// 工具 schema 必须在 catalog 里出现(模型才可能调用)。
func TestPersonaSetToolDefPresent(t *testing.T) {
a := newTestAgent(&fakePersonaStore{})
found := false
for _, td := range a.buildToolDefs() {
if m, ok := td.(map[string]interface{}); ok {
if fn, ok := m["function"].(map[string]interface{}); ok && fn["name"] == "persona_set" {
found = true
}
}
}
if !found {
t.Fatal("buildToolDefs 未包含 persona_set")
}
// 未接入配置时不应暴露该工具
b := newTestAgent(nil)
for _, td := range b.buildToolDefs() {
if m, ok := td.(map[string]interface{}); ok {
if fn, ok := m["function"].(map[string]interface{}); ok && fn["name"] == "persona_set" {
t.Fatal("未接入配置时不应暴露 persona_set")
}
}
}
}