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 落库并置位标记
→ 之后不再追问;反向对照(清标记 + 清会话上下文 + 重启)重新开始询问,
排除了「同一段对话里已问过」这一混淆。
This commit is contained in:
JianFeeeee
2026-09-12 13:55:04 +08:00
parent ea21803acb
commit 46e014a8ca
10 changed files with 476 additions and 67 deletions

View File

@ -57,9 +57,13 @@ type Agent struct {
// 由记忆系统本身决定。为 nil 时全部媒体接线静默跳过。
mediaStore *media.Store
// 人格设定
// 人格设定(内容来自启动时载入的人格文件/配置项)
personality *agentPkg.Personality
// 人格落库面:首启门禁与 persona_set 工具使用(见 persona.go
// 为 nil 时门禁与工具都静默关闭(例如单测里不接配置的场景)。
personaStore PersonaStore
// 插件注册表(用于 plgreload
pluginReg *plugin.Registry
pluginDir string
@ -183,6 +187,7 @@ type AgentConfig struct {
MultimodalSpace vector.MultimodalEmbedder
FusionCfg CrossModalFusionConfig // 跨模态融合权重;零值用默认
Personality *agentPkg.Personality
PersonaStore PersonaStore // 人格设定的读写面(首启门禁 + persona_set 工具)
PluginReg *plugin.Registry
PluginDir string
DistillInterval time.Duration
@ -269,6 +274,7 @@ func New(cfg AgentConfig) *Agent {
textMem: cfg.TextMemory,
mediaStore: cfg.MediaStore,
personality: cfg.Personality,
personaStore: cfg.PersonaStore,
pluginReg: cfg.PluginReg,
pluginDir: cfg.PluginDir,
distillInterval: cfg.DistillInterval,

View File

@ -0,0 +1,54 @@
package core
import (
"fmt"
"strings"
agentAPI "gitcode.com/JianFeeeee/HomeAgent/internal/agent/api"
)
// PersonaStore 是人格设定的读写面。
//
// 首启门禁buildSystemPrompt与 persona_set 工具都通过它工作,实现在 cmd/homed
// 读写配置项 core.agent.personal_prompt 与一次性标记 core.internal.persona_initialized
// (落库逻辑与 WebUI 向导共用 internal/config 的实现)。
//
// 为什么放在内核而不是某个通道插件:人格是**任何通道都要问一次**的事。
// 系统提示词每轮重建门禁放在这里WebUI / QQ / CLI / ACP / 邮件等全部通道自动覆盖。
type PersonaStore interface {
// PersonaInitialized 报告人格是否已确认(向导或工具已问过)。
PersonaInitialized() bool
// SetPersona 落库人格并打一次性标记,返回是否需要重启才生效。
SetPersona(mode, content string) (restartRequired bool, err error)
}
// executePersonaTool 落地首启人格设定。
//
// 成功即打一次性标记 → 之后 buildSystemPrompt 不再要求模型询问人格。
// custom 模式返回「需重启生效」:人格在 homed 启动时载入。
func (a *Agent) executePersonaTool(tc agentAPI.ToolCall) string {
if a.personaStore == nil {
return "人格设定不可用:内核未接入配置"
}
mode, _ := tc.Arguments["mode"].(string)
content, _ := tc.Arguments["content"].(string)
mode = strings.TrimSpace(mode)
restart, err := a.personaStore.SetPersona(mode, content)
if err != nil {
return fmt.Sprintf("人格设定失败:%v", err)
}
switch mode {
case "custom":
msg := "已保存自定义人格"
if restart {
msg += "**重启 homed 后生效**(人格在启动时载入)"
}
return msg
case "default":
return "已确认使用默认人格"
case "later":
return "已记为「以后再说」,继续使用默认人格"
default:
return "已保存人格设定"
}
}

View File

@ -0,0 +1,112 @@
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")
}
}
}
}

View File

@ -45,6 +45,8 @@ func (a *Agent) executeToolCall(tc agentAPI.ToolCall) (ret string) {
func (a *Agent) executeToolCallInner(tc agentAPI.ToolCall) string {
switch {
case tc.Name == "persona_set":
return a.executePersonaTool(tc)
case strings.HasPrefix(tc.Name, "memory_"):
return a.executeMemoryTool(tc)
case strings.HasPrefix(tc.Name, "social_"):

View File

@ -89,6 +89,16 @@ func (a *Agent) buildSystemPrompt(memContext string, userInput string) string {
}
}
// 首启人格门禁(跨通道唯一闸口):人格未确认时,要求模型主动询问用户。
// 系统提示词每轮重建,因此 WebUI / QQ / CLI / ACP / 邮件等所有通道都会带上它;
// 模型调用 persona_set或用户在 WebUI 向导里选)落地后,标记置位,本段消失。
if a.personaStore != nil && !a.personaStore.PersonaInitialized() {
prompt += "\n\n【首启人格设定】你的**人格设定尚未确认**。请在本轮回复里先问用户一句:" +
"要用默认人格,还是自定义一个?拿到明确答复后**必须调用 persona_set 工具**落库:" +
"用户选默认 → mode=default自定义 → mode=custom 且把内容写进 content" +
"用户说以后再说 → mode=later。用户答复前不要假设已设置也不要反复追问同一件事。"
}
prompt += a.buildToolCatalog()
return prompt
@ -197,6 +207,24 @@ func (a *Agent) buildToolDefs() []interface{} {
}
}
if a.personaStore != nil {
tools = append(tools, map[string]interface{}{
"type": "function",
"function": map[string]interface{}{
"name": "persona_set",
"description": "【首启人格】落地用户的人格选择并记录「已经问过」。仅在用户明确答复后调用:默认用 mode=default自定义用 mode=custom 并把人格内容放进 content用户说以后再说用 mode=later。",
"parameters": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"mode": map[string]interface{}{"type": "string", "description": "default | custom | later"},
"content": map[string]interface{}{"type": "string", "description": "自定义人格内容mode=custom 时必填)"},
},
"required": []string{"mode"},
},
},
})
}
if a.memory != nil {
tools = append(tools, map[string]interface{}{
"type": "function",