initialize paraclub-ai-mailer project with core components and configuration
This commit is contained in:
99
internal/ai/ai.go
Normal file
99
internal/ai/ai.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"paraclub-ai-mailer/config"
|
||||
)
|
||||
|
||||
type AI struct {
|
||||
config config.AIConfig
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
type OpenRouterRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []Message `json:"messages"`
|
||||
Temperature float32 `json:"temperature"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type OpenRouterResponse struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
|
||||
func New(cfg config.AIConfig) *AI {
|
||||
return &AI{
|
||||
config: cfg,
|
||||
client: &http.Client{},
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AI) GenerateReply(emailContent string, contextContent map[string]string) (string, error) {
|
||||
// Prepare context from all URLs
|
||||
var context string
|
||||
for url, content := range contextContent {
|
||||
context += fmt.Sprintf("\nContext from %s:\n%s\n", url, content)
|
||||
}
|
||||
|
||||
// Prepare the system message and user message
|
||||
systemMsg := "You are a helpful assistant. Use the provided context to help answer the email professionally and accurately."
|
||||
userMsg := fmt.Sprintf("Using the following context:\n%s\n\nPlease generate a reply for this email:\n%s", context, emailContent)
|
||||
|
||||
messages := []Message{
|
||||
{Role: "system", Content: systemMsg},
|
||||
{Role: "user", Content: userMsg},
|
||||
}
|
||||
|
||||
reqBody := OpenRouterRequest{
|
||||
Model: a.config.Model,
|
||||
Messages: messages,
|
||||
Temperature: a.config.Temperature,
|
||||
MaxTokens: a.config.MaxTokens,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", "https://openrouter.ai/api/v1/chat/completions", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.config.OpenRouterAPIKey))
|
||||
|
||||
resp, err := a.client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("OpenRouter API returned status code: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var result OpenRouterResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(result.Choices) == 0 {
|
||||
return "", fmt.Errorf("no response generated")
|
||||
}
|
||||
|
||||
return result.Choices[0].Message.Content, nil
|
||||
}
|
||||
48
internal/fetcher/fetcher.go
Normal file
48
internal/fetcher/fetcher.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package fetcher
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Fetcher struct {
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func New() *Fetcher {
|
||||
return &Fetcher{
|
||||
client: &http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Fetcher) FetchContent(url string) (string, error) {
|
||||
resp, err := f.client.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return 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 {
|
||||
return nil, err
|
||||
}
|
||||
results[url] = content
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
187
internal/imap/imap.go
Normal file
187
internal/imap/imap.go
Normal file
@@ -0,0 +1,187 @@
|
||||
package imap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"paraclub-ai-mailer/config"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/emersion/go-imap"
|
||||
"github.com/emersion/go-imap/client"
|
||||
)
|
||||
|
||||
type IMAPClient struct {
|
||||
client *client.Client
|
||||
config config.IMAPConfig
|
||||
processed map[string]bool
|
||||
}
|
||||
|
||||
// MessageLiteral implements imap.Literal for draft messages
|
||||
type MessageLiteral struct {
|
||||
content []byte
|
||||
}
|
||||
|
||||
func (m *MessageLiteral) Read(p []byte) (n int, err error) {
|
||||
return copy(p, m.content), io.EOF
|
||||
}
|
||||
|
||||
func (m *MessageLiteral) Len() int {
|
||||
return len(m.content)
|
||||
}
|
||||
|
||||
func New(cfg config.IMAPConfig) (*IMAPClient, error) {
|
||||
addr := fmt.Sprintf("%s:%d", cfg.Server, cfg.Port)
|
||||
var c *client.Client
|
||||
var err error
|
||||
|
||||
if cfg.UseTLS {
|
||||
c, err = client.DialTLS(addr, nil)
|
||||
} else {
|
||||
c, err = client.Dial(addr)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to IMAP server: %v", err)
|
||||
}
|
||||
|
||||
if err := c.Login(cfg.Username, cfg.Password); err != nil {
|
||||
return nil, fmt.Errorf("failed to login: %v", err)
|
||||
}
|
||||
|
||||
return &IMAPClient{
|
||||
client: c,
|
||||
config: cfg,
|
||||
processed: make(map[string]bool),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type Email struct {
|
||||
ID string
|
||||
Subject string
|
||||
From string
|
||||
Body string
|
||||
}
|
||||
|
||||
func (ic *IMAPClient) FetchUnprocessedEmails() ([]Email, error) {
|
||||
_, err := ic.client.Select(ic.config.MailboxIn, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
criteria := imap.NewSearchCriteria()
|
||||
criteria.WithoutFlags = []string{"\\Seen", "Processed"}
|
||||
|
||||
uids, err := ic.client.Search(criteria)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(uids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
seqSet := new(imap.SeqSet)
|
||||
seqSet.AddNum(uids...)
|
||||
|
||||
messages := make(chan *imap.Message, 10)
|
||||
section := &imap.BodySectionName{Peek: true}
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- ic.client.Fetch(seqSet, []imap.FetchItem{section.FetchItem()}, messages)
|
||||
}()
|
||||
|
||||
var emails []Email
|
||||
for msg := range messages {
|
||||
if ic.processed[msg.Envelope.MessageId] {
|
||||
continue
|
||||
}
|
||||
|
||||
r := msg.GetBody(section)
|
||||
if r == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var bodyBuilder strings.Builder
|
||||
_, err := io.Copy(&bodyBuilder, r)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
emails = append(emails, Email{
|
||||
ID: msg.Envelope.MessageId,
|
||||
Subject: msg.Envelope.Subject,
|
||||
From: msg.Envelope.From[0].Address(),
|
||||
Body: bodyBuilder.String(),
|
||||
})
|
||||
|
||||
ic.processed[msg.Envelope.MessageId] = true
|
||||
}
|
||||
|
||||
if err := <-done; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return emails, nil
|
||||
}
|
||||
|
||||
func (ic *IMAPClient) SaveDraft(email Email, response string) error {
|
||||
_, err := ic.client.Select(ic.config.DraftBox, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Format the draft message
|
||||
draft := fmt.Sprintf("From: %s\r\n"+
|
||||
"To: %s\r\n"+
|
||||
"Subject: Re: %s\r\n"+
|
||||
"Content-Type: text/plain; charset=UTF-8\r\n"+
|
||||
"\r\n%s",
|
||||
ic.config.Username,
|
||||
email.From,
|
||||
email.Subject,
|
||||
response)
|
||||
|
||||
// Create literal message
|
||||
literal := &MessageLiteral{content: []byte(draft)}
|
||||
|
||||
// Save the draft
|
||||
flags := []string{"\\Draft"}
|
||||
if err := ic.client.Append(ic.config.DraftBox, flags, time.Now(), literal); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ic *IMAPClient) MarkAsProcessed(email Email) error {
|
||||
_, err := ic.client.Select(ic.config.MailboxIn, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
criteria := imap.NewSearchCriteria()
|
||||
criteria.Header.Set("Message-Id", email.ID)
|
||||
|
||||
uids, err := ic.client.Search(criteria)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(uids) == 0 {
|
||||
return fmt.Errorf("email not found")
|
||||
}
|
||||
|
||||
seqSet := new(imap.SeqSet)
|
||||
seqSet.AddNum(uids...)
|
||||
|
||||
return ic.client.Store(seqSet, imap.FormatFlagsOp(imap.AddFlags, true), []interface{}{"Processed"}, nil)
|
||||
}
|
||||
|
||||
func (ic *IMAPClient) Close() error {
|
||||
if ic.client != nil {
|
||||
ic.client.Logout()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
58
internal/logger/logger.go
Normal file
58
internal/logger/logger.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var log = logrus.New()
|
||||
|
||||
func Init(level string, filePath string) error {
|
||||
// Set log level
|
||||
lvl, err := logrus.ParseLevel(level)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.SetLevel(lvl)
|
||||
|
||||
// Configure output
|
||||
if filePath != "" {
|
||||
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.SetOutput(io.MultiWriter(os.Stdout, file))
|
||||
}
|
||||
|
||||
log.SetFormatter(&logrus.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Info(args ...interface{}) {
|
||||
log.Info(args...)
|
||||
}
|
||||
|
||||
func Error(args ...interface{}) {
|
||||
log.Error(args...)
|
||||
}
|
||||
|
||||
func Debug(args ...interface{}) {
|
||||
log.Debug(args...)
|
||||
}
|
||||
|
||||
func Warn(args ...interface{}) {
|
||||
log.Warn(args...)
|
||||
}
|
||||
|
||||
func WithField(key string, value interface{}) *logrus.Entry {
|
||||
return log.WithField(key, value)
|
||||
}
|
||||
|
||||
func WithFields(fields logrus.Fields) *logrus.Entry {
|
||||
return log.WithFields(fields)
|
||||
}
|
||||
Reference in New Issue
Block a user