This commit is contained in:
lpf
2026-03-05 12:55:30 +08:00
parent 2fbb98bccd
commit 94e8f2c9d9
18 changed files with 378 additions and 1126 deletions

View File

@@ -77,6 +77,7 @@ func (hs *HeartbeatService) checkHeartbeat() {
func (hs *HeartbeatService) buildPrompt() string {
notesFile := filepath.Join(hs.workspace, "HEARTBEAT.md")
agentsFile := filepath.Join(hs.workspace, "AGENTS.md")
var notes string
if data, err := os.ReadFile(notesFile); err == nil {
@@ -85,18 +86,50 @@ func (hs *HeartbeatService) buildPrompt() string {
notes = candidate
}
}
agents := ""
if data, err := os.ReadFile(agentsFile); err == nil {
agents = strings.TrimSpace(string(data))
}
ackToken := heartbeatAckTokenFromText(agents)
if ackToken == "" {
ackToken = heartbeatAckTokenFromText(notes)
}
now := time.Now().Format("2006-01-02 15:04")
tpl := hs.promptTemplate
if strings.TrimSpace(tpl) == "" {
tpl = "Read HEARTBEAT.md if it exists (workspace context). Follow it strictly. Do not infer or repeat old tasks from prior chats. If nothing needs attention, reply HEARTBEAT_OK."
if strings.TrimSpace(ackToken) != "" {
tpl = fmt.Sprintf("Follow workspace policy in AGENTS.md first, then HEARTBEAT.md. If no action is needed, return %s.", ackToken)
} else {
tpl = "Follow workspace policy in AGENTS.md first, then HEARTBEAT.md."
}
}
prompt := fmt.Sprintf("%s\n\nCurrent time: %s\n\n%s\n", tpl, now, notes)
prompt := fmt.Sprintf("%s\n\nCurrent time: %s\n\n## AGENTS.md\n%s\n\n## HEARTBEAT.md\n%s\n", tpl, now, agents, notes)
return prompt
}
func heartbeatAckTokenFromText(text string) string {
for _, line := range strings.Split(text, "\n") {
t := strings.TrimSpace(line)
if t == "" {
continue
}
raw := strings.TrimLeft(t, "-*# ")
lower := strings.ToLower(raw)
if !strings.HasPrefix(lower, "heartbeat_ack_token:") {
continue
}
v := strings.TrimSpace(raw[len("heartbeat_ack_token:"):])
v = strings.Trim(v, "`\"' ")
if v != "" {
return v
}
}
return ""
}
func (hs *HeartbeatService) log(message string) {
logFile := filepath.Join(hs.workspace, "memory", "heartbeat.log")
f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)