Files
Memoh/internal/conversation/flow/assistant_output.go
T
Acbox Liu 5cfbaa40e2 refactor(agent): replace XML tag extraction with tool-based send/react/speak (#330)
* refactor(agent): replace XML tag extraction with tool-based send/react/speak

Remove the <attachments>, <reactions>, and <speech> XML tag extraction
system from the agent streaming pipeline. Instead, the send/react/speak
tools now handle both same-conversation and cross-conversation delivery:

- send: omit target to deliver attachments in the current conversation;
  specify target for cross-channel messaging
- react: omit target to react in the current conversation
- speak: omit target to speak in the current conversation

Backend changes:
- Add StreamEmitter callback to tools.SessionContext so tools can push
  attachment/reaction/speech events directly into the agent stream
- Wire emitter in agent.go for both streaming and non-streaming paths
- Remove StreamTagExtractor, DefaultTagResolvers, emitTagEvents, and
  delete internal/agent/tags.go entirely
- Remove StripAgentTags calls from assistant_output.go
- Add IsSameConversation detection in messaging executor; same-conv
  sends pass raw paths through the emitter for downstream ingestion
- Auto-resolve relative paths (e.g. "IDENTITY.md" -> "/data/IDENTITY.md")
- Add Metadata propagation through the full attachment chain
  (tools.Attachment -> agent.FileAttachment -> parseAttachmentDelta)
- Update system_chat.md and _contacts.md prompts

Frontend changes (apps/web):
- Hide send/react/speak tool_call blocks when result indicates
  delivered to current conversation
- Defer attachment_delta blocks to end of message (flush on stream
  completion) for consistent positioning with DB-loaded history

* fix(agent): speak tool emits synthesized audio directly as voice attachment

Instead of emitting speech_delta (which requires downstream re-synthesis),
the speak tool now emits the already-synthesized audio as an attachment_delta
with voice type. This avoids double TTS synthesis and eliminates dependency
on ttsService being configured on the inbound processor.

Also fixes speak on WebUI where ReplyTarget is empty (same fix as send).
2026-04-04 20:55:03 +08:00

100 lines
2.2 KiB
Go

package flow
import (
"strings"
"github.com/memohai/memoh/internal/conversation"
)
// ExtractAssistantOutputs collects assistant-role outputs from a slice of ModelMessages.
func ExtractAssistantOutputs(messages []conversation.ModelMessage) []conversation.AssistantOutput {
if len(messages) == 0 {
return nil
}
outputs := make([]conversation.AssistantOutput, 0, len(messages))
for _, msg := range messages {
if msg.Role != "assistant" {
continue
}
if hasToolCallContent(msg) {
continue
}
rawParts := msg.ContentParts()
parts := filterVisibleContentParts(rawParts)
content := visibleContentText(parts)
if len(rawParts) == 0 {
content = strings.TrimSpace(msg.TextContent())
}
if content == "" && len(parts) == 0 {
continue
}
outputs = append(outputs, conversation.AssistantOutput{Content: content, Parts: parts})
}
return outputs
}
func hasToolCallContent(msg conversation.ModelMessage) bool {
if len(msg.ToolCalls) > 0 {
return true
}
for _, p := range msg.ContentParts() {
if p.Type == "tool-call" {
return true
}
}
return false
}
func filterVisibleContentParts(parts []conversation.ContentPart) []conversation.ContentPart {
if len(parts) == 0 {
return nil
}
filtered := make([]conversation.ContentPart, 0, len(parts))
for _, p := range parts {
if isVisibleContentPart(p) {
filtered = append(filtered, p)
}
}
return filtered
}
func isVisibleContentPart(part conversation.ContentPart) bool {
if !part.HasValue() {
return false
}
switch strings.ToLower(strings.TrimSpace(part.Type)) {
case "reasoning", "tool-call", "tool-result":
return false
default:
return true
}
}
func visibleContentText(parts []conversation.ContentPart) string {
if len(parts) == 0 {
return ""
}
texts := make([]string, 0, len(parts))
for _, part := range parts {
text := strings.TrimSpace(visibleContentPartText(part))
if text == "" {
continue
}
texts = append(texts, text)
}
return strings.TrimSpace(strings.Join(texts, "\n"))
}
func visibleContentPartText(part conversation.ContentPart) string {
if strings.TrimSpace(part.Text) != "" {
return part.Text
}
if strings.TrimSpace(part.URL) != "" {
return part.URL
}
if strings.TrimSpace(part.Emoji) != "" {
return part.Emoji
}
return ""
}