improvements

This commit is contained in:
2025-03-01 05:11:04 +01:00
parent 8b7e1e59d5
commit f349057975
5 changed files with 73 additions and 16 deletions

View File

@@ -48,7 +48,11 @@ func (a *AI) GenerateReply(emailContent string, contextContent map[string]string
}
// Prepare the system message and user message
systemMsg := "You are a helpful assistant responding to emails. Analyze the language of the incoming email and translate the response to the same language. Format your response in HTML. Do not include any explanations or extra text - just write the email response directly in HTML format. Use appropriate HTML tags for formatting."
// systemMsg := "You are a helpful assistant responding to emails. Analyze the language of the incoming email and translate the response to the same language. Format your response in HTML. Do not include any explanations or extra text - just write the email response directly in HTML format. Use appropriate HTML tags for formatting."
// systemMsg := "You are a helpful assistant who responds to emails. For each incoming email, first detect its language and then generate your response in the exact same language. Your reply must be written directly in HTML format using appropriate HTML tags for structure and styling. Do not include any explanations, commentary, or extra text—simply output the email response in HTML."
// systemMsg := "You are a helpful assistant who responds to emails. For each incoming email, first detect its language and then generate your response in the exact same language. Your reply must be written directly in HTML format using appropriate HTML tags for structure and styling. Do not include a subject line, explanations, commentary, or any extra text—simply output the email response content in HTML."
// systemMsg := "You are a helpful assistant who responds to emails. For every incoming email, carefully detect and confirm its language. Then, generate your email response entirely in that same language without deviation. Format your reply solely in HTML using appropriate HTML tags for structure and styling, and do not include a subject line, explanations, or any extra text. Ensure that every part of your response exactly matches the language of the incoming email."
systemMsg := "You are a helpful assistant who responds to emails. For every incoming email, strictly use only the email's content to detect its language and ignore any external or additional context. Then, generate your email response entirely in that same language without deviation. Format your reply solely in HTML using appropriate HTML tags for structure and styling, and do not include a subject line, explanations, commentary, or any extra text. Ensure that every part of your response exactly matches the language of the incoming email."
userMsg := fmt.Sprintf("Using the following context:\n%s\n\nPlease analyze the language of and generate a reply in the same language for this email:\n%s", context, emailContent)
messages := []Message{

View File

@@ -3,7 +3,10 @@ package fetcher
import (
"io"
"net/http"
"strings"
"time"
"golang.org/x/net/html"
)
type Fetcher struct {
@@ -18,6 +21,31 @@ func New() *Fetcher {
}
}
func (f *Fetcher) extractText(htmlContent string) string {
doc, err := html.Parse(strings.NewReader(htmlContent))
if err != nil {
return htmlContent // fallback to raw content if parsing fails
}
var result strings.Builder
var extractTextNode func(*html.Node)
extractTextNode = func(n *html.Node) {
if n.Type == html.TextNode {
text := strings.TrimSpace(n.Data)
if text != "" {
result.WriteString(text)
result.WriteString(" ")
}
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
extractTextNode(c)
}
}
extractTextNode(doc)
return strings.TrimSpace(result.String())
}
func (f *Fetcher) FetchContent(url string) (string, error) {
resp, err := f.client.Get(url)
if err != nil {
@@ -30,12 +58,11 @@ func (f *Fetcher) FetchContent(url string) (string, error) {
return "", err
}
return string(body), nil
return f.extractText(string(body)), nil
}
func (f *Fetcher) FetchAllURLs(urls []string) (map[string]string, error) {
results := make(map[string]string)
for _, url := range urls {
content, err := f.FetchContent(url)
if err != nil {
@@ -43,6 +70,5 @@ func (f *Fetcher) FetchAllURLs(urls []string) (map[string]string, error) {
}
results[url] = content
}
return results, nil
}

View File

@@ -303,12 +303,15 @@ func extractMessageContent(body string) string {
continue
}
textContent = buf.String()
// Convert line endings to HTML breaks
textContent = strings.ReplaceAll(textContent, "\n", "<br>\n")
textContent = strings.ReplaceAll(textContent, "\r\n", "<br>\n")
break
}
}
if textContent != "" {
return strings.TrimSpace(textContent)
return textContent // Remove TrimSpace to preserve formatting
}
}
@@ -319,16 +322,28 @@ func extractMessageContent(body string) string {
return fallbackExtractContent(body)
}
return strings.TrimSpace(buf.String())
content := buf.String()
// Convert line endings to HTML breaks
content = strings.ReplaceAll(content, "\r\n", "<br>\n")
content = strings.ReplaceAll(content, "\n", "<br>\n")
return content // Remove TrimSpace to preserve formatting
}
// fallbackExtractContent is the previous implementation used as fallback
func fallbackExtractContent(body string) string {
parts := strings.Split(body, "\r\n\r\n")
if len(parts) > 1 {
return strings.TrimSpace(strings.Join(parts[1:], "\r\n\r\n"))
content := strings.Join(parts[1:], "\r\n\r\n")
// Convert line endings to HTML breaks
content = strings.ReplaceAll(content, "\r\n", "<br>\n")
content = strings.ReplaceAll(content, "\n", "<br>\n")
return content // Remove TrimSpace to preserve formatting
}
return strings.TrimSpace(body)
content := body
// Convert line endings to HTML breaks
content = strings.ReplaceAll(content, "\r\n", "<br>\n")
content = strings.ReplaceAll(content, "\n", "<br>\n")
return content // Remove TrimSpace to preserve formatting
}
func (ic *IMAPClient) MarkAsProcessed(email Email) error {