Files
Memoh/internal/pipeline/turn_response.go
T
Acbox d05ba8956a fix: stop escaped history context blowup
Avoid feeding structured tool payloads back into pipeline context as doubly encoded JSON, and return readable history summaries instead of raw message blobs.
2026-04-23 17:56:09 +08:00

37 lines
1018 B
Go

package pipeline
import (
"encoding/json"
"strings"
"github.com/memohai/memoh/internal/conversation"
messagepkg "github.com/memohai/memoh/internal/message"
)
// DecodeTurnResponseEntry converts a persisted bot message into a TR entry for
// pipeline context composition. Only visible text is preserved; structured
// tool-call / tool-result payloads are skipped to avoid re-injecting raw JSON
// into later prompts.
func DecodeTurnResponseEntry(msg messagepkg.Message) (TurnResponseEntry, bool) {
role := strings.TrimSpace(msg.Role)
if role != "assistant" && role != "tool" {
return TurnResponseEntry{}, false
}
var modelMsg conversation.ModelMessage
if err := json.Unmarshal(msg.Content, &modelMsg); err != nil {
return TurnResponseEntry{}, false
}
content := strings.TrimSpace(modelMsg.TextContent())
if content == "" {
return TurnResponseEntry{}, false
}
return TurnResponseEntry{
RequestedAtMs: msg.CreatedAt.UnixMilli(),
Role: msg.Role,
Content: content,
}, true
}