Files
LocalAGI/services/actions/sendmail.go
Ettore Di Giacinto c23e655f44 feat(agent): shared state, allow to track conversations globally (#148)
* feat(agent): shared state, allow to track conversations globally

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* Cleanup

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

* track conversations initiated by the bot

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>

---------

Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
2025-05-11 22:23:01 +02:00

126 lines
2.9 KiB
Go

package actions
import (
"context"
"fmt"
"net/smtp"
"github.com/mudler/LocalAGI/core/types"
"github.com/mudler/LocalAGI/pkg/config"
"github.com/sashabaranov/go-openai/jsonschema"
)
func NewSendMail(config map[string]string) *SendMailAction {
return &SendMailAction{
username: config["username"],
password: config["password"],
email: config["email"],
smtpHost: config["smtpHost"],
smtpPort: config["smtpPort"],
}
}
type SendMailAction struct {
username string
password string
email string
smtpHost string
smtpPort string
}
func (a *SendMailAction) Run(ctx context.Context, sharedState *types.AgentSharedState, params types.ActionParams) (types.ActionResult, error) {
result := struct {
Message string `json:"message"`
To string `json:"to"`
Subject string `json:"subject"`
}{}
err := params.Unmarshal(&result)
if err != nil {
fmt.Printf("error: %v", err)
return types.ActionResult{}, err
}
// Authentication.
auth := smtp.PlainAuth("", a.email, a.password, a.smtpHost)
// Sending email.
err = smtp.SendMail(
fmt.Sprintf("%s:%s", a.smtpHost, a.smtpPort),
auth, a.email, []string{
result.To,
}, []byte(result.Message))
if err != nil {
return types.ActionResult{}, err
}
return types.ActionResult{Result: fmt.Sprintf("Email sent to %s", result.To)}, nil
}
func (a *SendMailAction) Definition() types.ActionDefinition {
return types.ActionDefinition{
Name: "send_email",
Description: "Send an email.",
Properties: map[string]jsonschema.Definition{
"to": {
Type: jsonschema.String,
Description: "The email address to send the email to.",
},
"subject": {
Type: jsonschema.String,
Description: "The subject of the email.",
},
"message": {
Type: jsonschema.String,
Description: "The message to send.",
},
},
Required: []string{"to", "subject", "message"},
}
}
func (a *SendMailAction) Plannable() bool {
return true
}
// SendMailConfigMeta returns the metadata for SendMail action configuration fields
func SendMailConfigMeta() []config.Field {
return []config.Field{
{
Name: "smtpHost",
Label: "SMTP Host",
Type: config.FieldTypeText,
Required: true,
HelpText: "SMTP server host (e.g., smtp.gmail.com)",
},
{
Name: "smtpPort",
Label: "SMTP Port",
Type: config.FieldTypeText,
Required: true,
DefaultValue: "587",
HelpText: "SMTP server port (e.g., 587)",
},
{
Name: "username",
Label: "SMTP Username",
Type: config.FieldTypeText,
Required: true,
HelpText: "SMTP username/email address",
},
{
Name: "password",
Label: "SMTP Password",
Type: config.FieldTypeText,
Required: true,
HelpText: "SMTP password or app password",
},
{
Name: "email",
Label: "From Email",
Type: config.FieldTypeText,
Required: true,
HelpText: "Sender email address",
},
}
}