This commit is contained in:
2026-02-25 08:24:05 +01:00
parent 914d608d35
commit 3997f3feee
7 changed files with 504 additions and 105 deletions

View File

@@ -273,7 +273,7 @@ final class EmailHandlerService {
---
Please provide a complete, well-formatted response to this email. Your response will be sent as an HTML email.
Please provide a complete, well-formatted response to this email. Write the reply directly — do not wrap it in code fences or markdown blocks.
"""
return prompt
@@ -299,7 +299,8 @@ final class EmailHandlerService {
- Be professional and courteous
- Keep responses concise and relevant
- Use proper email etiquette
- Format your response using Markdown (it will be converted to HTML)
- Format your response using Markdown (bold, lists, headings are welcome)
- Do NOT wrap your response in code fences or ```html blocks — write the email body directly
- If you need information from files, you have read-only access via MCP tools
- Never claim to write, modify, or delete files (read-only access)
- Sign emails appropriately
@@ -412,27 +413,46 @@ final class EmailHandlerService {
}
private func markdownToHTML(_ markdown: String) -> String {
// Basic markdown to HTML conversion
// TODO: Use proper markdown parser for production
var html = markdown
var text = markdown.trimmingCharacters(in: .whitespacesAndNewlines)
// Paragraphs
html = html.replacingOccurrences(of: "\n\n", with: "</p><p>")
html = "<p>\(html)</p>"
// Strip outer code fence wrapping the entire response
// Models sometimes wrap their reply in ```html ... ``` or ``` ... ```
let lines = text.components(separatedBy: "\n")
if lines.count >= 2 {
let first = lines[0].trimmingCharacters(in: .whitespaces)
let last = lines[lines.count - 1].trimmingCharacters(in: .whitespaces)
if (first == "```" || first.hasPrefix("```")) && last == "```" {
text = lines.dropFirst().dropLast().joined(separator: "\n")
.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
// Strip any remaining fenced code blocks (preserve content, remove fences)
text = text.replacingOccurrences(
of: #"```[a-z]*\n([\s\S]*?)```"#,
with: "$1",
options: .regularExpression
)
// Bold
html = html.replacingOccurrences(of: #"\*\*(.+?)\*\*"#, with: "<strong>$1</strong>", options: .regularExpression)
text = text.replacingOccurrences(of: #"\*\*(.+?)\*\*"#, with: "<strong>$1</strong>", options: .regularExpression)
// Italic
html = html.replacingOccurrences(of: #"\*(.+?)\*"#, with: "<em>$1</em>", options: .regularExpression)
// Italic (avoid matching bold's leftover *)
text = text.replacingOccurrences(of: #"(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)"#, with: "<em>$1</em>", options: .regularExpression)
// Inline code
html = html.replacingOccurrences(of: #"`(.+?)`"#, with: "<code>$1</code>", options: .regularExpression)
text = text.replacingOccurrences(of: #"`([^`\n]+)`"#, with: "<code>$1</code>", options: .regularExpression)
// Line breaks
html = html.replacingOccurrences(of: "\n", with: "<br>")
// Split into paragraphs on double newlines, wrap each in <p>
let paragraphs = text.components(separatedBy: "\n\n")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
.map { para in
let withBreaks = para.replacingOccurrences(of: "\n", with: "<br>")
return "<p>\(withBreaks)</p>"
}
return html
return paragraphs.joined(separator: "\n")
}
// MARK: - Error Handling