mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
609ca49cf5
* feat(channel): add Matrix adapter support * fix(channel): prevent reasoning leaks in Matrix replies * fix(channel): persist Matrix sync cursors * fix(channel): improve Matrix markdown rendering * fix(channel): support Matrix attachments and multimodal history * fix(channel): expand Matrix reply media context * fix(handlers): allow media downloads for chat-access bots * fix(channel): classify Matrix DMs as direct chats * fix(channel): auto-join Matrix room invites * fix(channel): resolve Matrix room aliases for outbound send * fix(web): use Matrix brand icon in channel badges Replace the generic Matrix hashtag badge with the official brand asset so channel badges feel recognizable and fit the circular mask cleanly. * fix(channel): add Matrix room whitelist controls Let Matrix bots decide whether to auto-join invites and restrict inbound activity to allowed rooms or aliases. Expose the new controls in the web settings UI with line-based whitelist input so access rules stay explicit. * fix(channel): stabilize Matrix multimodal follow-ups and settings * fix(flow): avoid gosec panic on byte decoding * fix: fix golangci-lint * fix(channel): remove Matrix built-in ACL * fix(channel): preserve Matrix image captions * fix(channel): validate Matrix homeserver and sync access Fail Matrix connections early when the homeserver, access token, or /sync capability is misconfigured so bot health checks surface actionable errors. * fix(channel): preserve optional toggles and relax Matrix startup validation * fix(channel): tighten Matrix mention fallback parsing * fix(flow): skip structured assistant tool-call outputs * fix(flow): resolve merged resolver duplication Keep the internal agent resolver implementation after merging main so split helper files do not redeclare flow symbols. Restore user message normalization in sanitize and persistence paths to keep flow tests and command packages building. * fix(flow): remove unused merged resolver helper Drop the leftover truncate helper and import from the resolver merge fix so golangci-lint passes again without affecting flow behavior. --------- Co-authored-by: Acbox Liu <acbox0328@gmail.com>
100 lines
2.2 KiB
Go
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 ""
|
|
}
|