Implement language detection in AI response generation and enhance message content extraction
This commit is contained in:
@@ -268,7 +268,6 @@ func (ic *IMAPClient) SaveDraft(email Email, response string) error {
|
||||
// by removing email headers and MIME boundaries
|
||||
func extractMessageContent(body string) string {
|
||||
logger.WithField("bodyLength", len(body)).Debug("Starting message content extraction")
|
||||
|
||||
msg, err := mail.ReadMessage(strings.NewReader(body))
|
||||
if err != nil {
|
||||
logger.WithFields(logrus.Fields{
|
||||
@@ -295,89 +294,106 @@ func extractMessageContent(body string) string {
|
||||
"params": params,
|
||||
}).Debug("Parsed message Content-Type")
|
||||
|
||||
var content string
|
||||
if strings.HasPrefix(mediaType, "multipart/") {
|
||||
boundary := params["boundary"]
|
||||
if boundary == "" {
|
||||
logger.WithField("mediaType", mediaType).Debug("No boundary found in multipart message, falling back to simple extraction")
|
||||
return fallbackExtractContent(body)
|
||||
}
|
||||
|
||||
logger.WithFields(logrus.Fields{
|
||||
"mediaType": mediaType,
|
||||
"boundary": boundary,
|
||||
}).Debug("Processing multipart message")
|
||||
|
||||
reader := multipart.NewReader(msg.Body, boundary)
|
||||
var textContent string
|
||||
partIndex := 0
|
||||
|
||||
for {
|
||||
part, err := reader.NextPart()
|
||||
if err == io.EOF {
|
||||
logger.Debug("Finished processing all multipart parts")
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
logger.WithFields(logrus.Fields{
|
||||
"error": err,
|
||||
"partIndex": partIndex,
|
||||
}).Debug("Error reading multipart part, falling back to simple extraction")
|
||||
return fallbackExtractContent(body)
|
||||
}
|
||||
|
||||
contentType := part.Header.Get("Content-Type")
|
||||
logger.WithFields(logrus.Fields{
|
||||
"partIndex": partIndex,
|
||||
"contentType": contentType,
|
||||
}).Debug("Processing message part")
|
||||
|
||||
if strings.HasPrefix(contentType, "text/plain") {
|
||||
buf := new(bytes.Buffer)
|
||||
_, err := buf.ReadFrom(part)
|
||||
if err != nil {
|
||||
logger.WithFields(logrus.Fields{
|
||||
"error": err,
|
||||
"partIndex": partIndex,
|
||||
}).Debug("Failed to read text/plain part")
|
||||
continue
|
||||
}
|
||||
textContent = buf.String()
|
||||
logger.WithFields(logrus.Fields{
|
||||
"partIndex": partIndex,
|
||||
"contentLength": len(textContent),
|
||||
}).Debug("Successfully extracted text/plain content")
|
||||
textContent = strings.ReplaceAll(textContent, "\n", "<br>\n")
|
||||
textContent = strings.ReplaceAll(textContent, "\r\n", "<br>\n")
|
||||
break
|
||||
}
|
||||
partIndex++
|
||||
}
|
||||
|
||||
if textContent != "" {
|
||||
logger.WithField("contentLength", len(textContent)).Debug("Successfully extracted content from multipart message")
|
||||
return textContent
|
||||
}
|
||||
logger.Debug("No text/plain content found in multipart message, trying to read body directly")
|
||||
content = handleMultipartMessage(msg.Body, params["boundary"])
|
||||
} else {
|
||||
content = handleSinglePartMessage(msg.Body)
|
||||
}
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
_, err = buf.ReadFrom(msg.Body)
|
||||
if err != nil {
|
||||
logger.WithFields(logrus.Fields{
|
||||
"error": err,
|
||||
"mediaType": mediaType,
|
||||
}).Debug("Failed to read message body, falling back to simple extraction")
|
||||
if content == "" {
|
||||
logger.Debug("No content extracted, falling back to simple extraction")
|
||||
return fallbackExtractContent(body)
|
||||
}
|
||||
|
||||
content := buf.String()
|
||||
// Clean up the content
|
||||
content = cleanMessageContent(content)
|
||||
|
||||
logger.WithField("contentLength", len(content)).Debug("Successfully extracted and cleaned message content")
|
||||
return content
|
||||
}
|
||||
|
||||
func handleMultipartMessage(reader io.Reader, boundary string) string {
|
||||
if boundary == "" {
|
||||
logger.Debug("No boundary found in multipart message")
|
||||
return ""
|
||||
}
|
||||
|
||||
mReader := multipart.NewReader(reader, boundary)
|
||||
var textContent string
|
||||
partIndex := 0
|
||||
|
||||
for {
|
||||
part, err := mReader.NextPart()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
logger.WithError(err).Debug("Error reading multipart part")
|
||||
return ""
|
||||
}
|
||||
|
||||
contentType := part.Header.Get("Content-Type")
|
||||
if strings.HasPrefix(contentType, "text/plain") {
|
||||
buf := new(bytes.Buffer)
|
||||
if _, err := buf.ReadFrom(part); err != nil {
|
||||
continue
|
||||
}
|
||||
textContent = buf.String()
|
||||
break
|
||||
}
|
||||
partIndex++
|
||||
}
|
||||
|
||||
return textContent
|
||||
}
|
||||
|
||||
func handleSinglePartMessage(reader io.Reader) string {
|
||||
buf := new(bytes.Buffer)
|
||||
if _, err := buf.ReadFrom(reader); err != nil {
|
||||
logger.WithError(err).Debug("Failed to read message body")
|
||||
return ""
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func cleanMessageContent(content string) string {
|
||||
// Remove any remaining email headers that might be in the body
|
||||
lines := strings.Split(content, "\n")
|
||||
var cleanLines []string
|
||||
headerSection := true
|
||||
|
||||
for _, line := range lines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
|
||||
// Empty line marks the end of headers
|
||||
if headerSection && trimmed == "" {
|
||||
headerSection = false
|
||||
continue
|
||||
}
|
||||
|
||||
// Skip header lines
|
||||
if headerSection && (strings.Contains(trimmed, ":") || trimmed == "") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Add non-header lines
|
||||
if !headerSection {
|
||||
cleanLines = append(cleanLines, line)
|
||||
}
|
||||
}
|
||||
|
||||
content = strings.Join(cleanLines, "\n")
|
||||
|
||||
// Convert newlines to HTML breaks for display
|
||||
content = strings.ReplaceAll(content, "\r\n", "<br>\n")
|
||||
content = strings.ReplaceAll(content, "\n", "<br>\n")
|
||||
logger.WithFields(logrus.Fields{
|
||||
"contentLength": len(content),
|
||||
"mediaType": mediaType,
|
||||
}).Debug("Successfully extracted content from message body")
|
||||
return content
|
||||
|
||||
// Remove any remaining email signature markers
|
||||
content = strings.Split(content, "\n-- ")[0]
|
||||
content = strings.Split(content, "<br>-- ")[0]
|
||||
|
||||
return strings.TrimSpace(content)
|
||||
}
|
||||
|
||||
// fallbackExtractContent is the previous implementation used as fallback
|
||||
|
||||
Reference in New Issue
Block a user