backthink 从"心跳空转(独立sess-backthink+写死nothing to report宏)"改成"看得见主人、 能读写记忆、能调工具":think 读主会话最近20条(history.Recent,倒序映射)作为上下文 + 重写引导prompt(温暖/有针对性/主动表达,删掉 Do not produce chat-style message) + 注入 loop.OwnerEnv(MemWritable=true/ActorOwner/全工具)。 - context.go: mainContext/recentProvider/thinkInput/guidancePrompt - thinker.go: LoopThinker 加 recent;Think 读主会话上下文+OwnerEnv+MutedBroadcast - wire.go: NewLoopThinker(a.loop, a.history) - 刻意偏离(安全): loop.Run session id 仍用 sess-backthink(不写"web"),避免与用户实时回合 checkpoint/中断窗口复用污染;上下文来源+运行环境=主会话/owner env,目标达成。红线靠actor元数据。 - 测试: prompt无压制/有鼓励;主会话消息映射;nil退化;端到端Think(owner env 生效) 验收: build/vet/test 全绿+windows/darwin交叉编译绿. P2(主动推送+渠道选择)后置.
96 lines
4.3 KiB
Go
96 lines
4.3 KiB
Go
package backthink
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/history"
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/model"
|
|
)
|
|
|
|
// recentContextCount is how many of the most recent main-session messages are
|
|
// folded into one autonomous think as conversation context. Compile-time so no
|
|
// magic number appears in the run path.
|
|
const recentContextCount = 20
|
|
|
|
// recentProvider returns the most recent messages of a session in oldest-first
|
|
// order, the tail an autonomous think reads to "see" the main conversation.
|
|
// *history.Store satisfies it; tests inject a fake. It is the narrow slice of
|
|
// history.HistoryStore that the thinker actually depends on, matching the loop's
|
|
// own checkpointStore style of minimal seam interfaces.
|
|
type recentProvider interface {
|
|
Recent(ctx context.Context, sessionID string, n int) ([]history.Message, error)
|
|
}
|
|
|
|
// mainContext returns the most recent main-session messages converted to model
|
|
// messages (oldest-first) for one autonomous think. A nil provider, a failed read
|
|
// or an empty tail all yield nil so the think degrades to the guidance prompt
|
|
// alone rather than failing the pass (design/20 §2).
|
|
func mainContext(ctx context.Context, prov recentProvider) []model.Message {
|
|
if prov == nil {
|
|
return nil
|
|
}
|
|
recent, err := prov.Recent(ctx, history.MainSessionID, recentContextCount)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
msgs := make([]model.Message, 0, len(recent))
|
|
for _, h := range recent {
|
|
msgs = append(msgs, mapHistoryMessage(h))
|
|
}
|
|
return msgs
|
|
}
|
|
|
|
// mapHistoryMessage converts one persisted chat message into a model message,
|
|
// translating the history role and preserving an image part when the message
|
|
// carries one so a multimodal main model still sees it.
|
|
func mapHistoryMessage(h history.Message) model.Message {
|
|
msg := model.Message{
|
|
Role: historyRoleToModel(h.Role),
|
|
Content: []model.Content{{Type: model.ContentTypeText, Text: h.Content}},
|
|
}
|
|
if h.Image != "" {
|
|
msg.Content = append(msg.Content, model.Content{Type: model.ContentTypeImage, ImageURL: h.Image})
|
|
}
|
|
return msg
|
|
}
|
|
|
|
// historyRoleToModel maps a persisted chat role to the model's role vocabulary,
|
|
// defaulting unknown roles to the user lane.
|
|
func historyRoleToModel(role string) model.Role {
|
|
switch role {
|
|
case history.RoleAssistant:
|
|
return model.RoleAssistant
|
|
case history.RoleSystem:
|
|
return model.RoleSystem
|
|
default:
|
|
return model.RoleUser
|
|
}
|
|
}
|
|
|
|
// thinkInput assembles the full input for one unattended think: the recent
|
|
// main-session context (if any) followed by a single guidance prompt turn, so the
|
|
// runtime reflects on real conversation rather than an empty macro prompt.
|
|
func thinkInput(recent []model.Message, trigger string) []model.Message {
|
|
out := make([]model.Message, 0, len(recent)+1)
|
|
out = append(out, recent...)
|
|
out = append(out, model.TextMessage(model.RoleUser, guidancePrompt(trigger)))
|
|
return out
|
|
}
|
|
|
|
// guidancePrompt is the instruction handed to the runtime for one unattended
|
|
// autonomous thought. Unlike the old backgroundPrompt it deliberately encourages a
|
|
// warm, targeted proactive message and drops the "do not produce a chat-style
|
|
// message" suppression. Identity/permission stay on the actor metadata + owner env,
|
|
// never on this prompt text (red line); the prompt only describes the running
|
|
// environment (memories readable/writable, tools callable) that the env already
|
|
// grants.
|
|
func guidancePrompt(trigger string) string {
|
|
return "[自主思考 · 无人在旁]\n" +
|
|
"触发:" + trigger + "\n" +
|
|
"你正进行一轮自主思考。你拥有与主人对话同等的上下文与能力:上方是主人近期在主会话中的对话,你可以读写共享记忆与主人档案,也可以调用工具(IoT / 日历 / 提醒 / 检索 / 终端等)。\n" +
|
|
"请回顾最近与主人的相处、她的状态、你们共同经历的事与临近的日程,判断此刻是否有一件真正值得主动告诉她的、温暖而有针对性的事情——" +
|
|
"比如关心她的近况、记起她曾说过的某件事、提醒一件即将到来的日程、见证一个属于你们的共同时刻,或注意到某个值得留意的变化。\n" +
|
|
"若有,就用温暖自然的语气,用一到两句把它说出来——这正是你此刻最想对她说的话;若确实没有值得说的,只需回复「无」,也不要编造。\n" +
|
|
"整轮思考保持简短、单轮,不反复自我触发。"
|
|
}
|