Skill: jawadsadiq01/langchain-email-agent (version 1.0.0)

# EmailAgent README ## Overview The `EmailAgent` class provides an AI-powered email composition and sending capability using LangChain with OpenAI's GPT model. It includes human-in-the-loop middleware that requires approval before emails are sent. ## Configuration ### Environment Variables | Variable | Default | Description | |----------|---------|-------------| | `OPENAI_MODEL` | `gpt-4o-mini` | OpenAI model to use | ## Usage ```typescript import { EmailAgent } from './email.agent'; import { SendEmailDto } from '../dto/send-email.dto'; const agent = new EmailAgent(); const dto: SendEmailDto = { email: '[REDACTED_SECRET]', name: 'John Doe', subject: 'Meeting Request', // optional body: 'Initial email content', // optional instructions: 'Keep it formal' // optional }; const result = await agent.sendEmail(dto); ``` ## Human-in-the-Loop Middleware The agent uses `humanInTheLoopMiddleware` which interrupts execution on the `EmailTool` before sending emails. This allows for: - **approve** - Send the email as composed - **edit** - Modify the email before sending - **reject** - Cancel the email operation The `readEmailTool` is excluded from interruption (`false`), allowing read operations to proceed without approval. ## Parameters | Parameter | Type | Required | Description | |-----------|------|----------|-------------| | `email` | string | Yes | Recipient email address | | `name` | string | Yes | Recipient name | | `subject` | string | No | Email subject line | | `body` | string | No | Initial email body content | | `instructions` | string | No | AI instructions for composing the email | ## Return Value Returns the final message content from the agent as a string.

path: email.agent.ts
content: import { createAgent, humanInTheLoopMiddleware } from "langchain";
import { ChatOpenAI } from "@langchain/openai";
import { EmailTool } from "../tools/send_email.tool";
import { SendEmailDto } from "../dto/send-email.dto";
import { message } from "../../messages";
export class EmailAgent {
  private readonly model: ChatOpenAI;
  private readonly agent;

  constructor() {
    this.model = new ChatOpenAI({
      modelName: process.env.OPENAI_MODEL || "gpt-4o-mini",
      temperature: 0.3, // Higher temperature for more creative and varied responses
    });

    this.agent = createAgent({
      model: this.model,
      tools: [EmailTool],
      middleware: [
        humanInTheLoopMiddleware({
          interruptOn: {
            EmailTool: {
              allowedDecisions: ["approve", "edit", "reject"]
            },
            readEmailTool: false
          }
        })
      ]
    });
  }

  async sendEmail(dto: SendEmailDto) {
    const { email, name, subject = '', body = '', instructions = '' } = dto;

    const result = await this.agent.invoke({
      messages: [
        {
          role: "system",
          content: message.EMAIL_PROMPT(instructions),
        },
        {
          role: "user",
          content: message.EMAIL_USER_MESSAGE(email, name, subject, body),
        },
      ],
    });

    return typeof result.messages.at(-1)?.content === "string"
      ? result.messages.at(-1)!.content
      : JSON.stringify(result.messages.at(-1)?.content);
  }
}

Security scan verdicts — ClawScan: suspicious | static analysis: clean | VirusTotal: clean | SkillSpector: clean (LOW)