DEV Community

Kyungsu Kang
Kyungsu Kang

Posted on

7 7 7 6 7

Gmail Agent Built with TypeScript

Quickly Build a Gmail Agent with Agentica CLI

Image description

https://github.com/wrtnlabs/agentica

This tutorial shows you how to effortlessly set up a Gmail Agent powered by OpenAI's GPT model using the Agentica CLI. In just a few minutes, you can automate your email tasks and focus on what truly matters.


Easy CLI Setup

With Agentica’s latest CLI wizard, you can start your project without any hassle. Open your terminal and run:

npx agentica start gmail-agent
Enter fullscreen mode Exit fullscreen mode

This command launches the Agentica Setup Wizard, which will guide you through:

  • Installing the required packages
  • Choosing your package manager and project type
  • Selecting the GMAIL controller
  • Entering your OPENAI_API_KEY

After you complete the wizard, Agentica automatically generates your code, creates a .env file, and installs all dependencies.


Overview of the Generated Code

Once setup is complete, you’ll get a code template like the one below:

import { Agentica } from "@agentica/core";
import typia from "typia";
import dotenv from "dotenv";
import { OpenAI } from "openai";

import { GmailService } from "@wrtnlabs/connector-gmail";

dotenv.config();

export const agent = new Agentica({
  model: "chatgpt",
  vendor: {
    api: new OpenAI({
      apiKey: process.env.OPENAI_API_KEY!,
    }),
    model: "gpt-4o-mini",
  },
  controllers: [
    {
      name: "Gmail Connector",
      protocol: "class",
      application: typia.llm.application<GmailService, "chatgpt">(),
      execute: new GmailService(),
    },
  ],
});

const main = async () => {
  console.log(await agent.conversate("What can you do?"));
};

main();
Enter fullscreen mode Exit fullscreen mode

This template sets up your Gmail Agent to interact with Gmail using OpenAI’s GPT model.


Setting Up Google API Credentials

Before running your agent, add your Google API credentials to the .env file in your project root:

OPENAI_API_KEY=your-openai-api-key
GMAIL_CLIENT_ID=your-gmail-client-id
GMAIL_CLIENT_SECRET=your-gmail-client-secret
GMAIL_REFRESH_TOKEN=your-gmail-refresh-token
Enter fullscreen mode Exit fullscreen mode

To get these credentials:

  1. Create a project in the Google Cloud Console and enable the Gmail API.
  2. Generate OAuth 2.0 credentials to obtain your Client ID, Client Secret, and Refresh Token.

What Your Agent Can Do

Your Gmail Agent will:

  • Process Gmail Data: Use the GmailService connector to read, search, and manage emails.
  • Handle Natural Language Commands: Leverage OpenAI's GPT model to understand and process your requests.
  • Ensure Type Safety: Utilize typia to maintain robust type safety.
  • Manage Credentials Securely: Use dotenv for safe and easy environment variable management.

Selective Function Exposure

For enhanced security and easier maintenance, you can choose to expose only specific functions using TypeScript’s Pick utility. For example, to include only functions for creating drafts, finding emails, sending emails, deleting email lists, and hard deleting, you can configure your agent as follows:

export const GmailAgent = new Agentica({
  model: "chatgpt",
  vendor: {
    api: openai,
    model: "gpt-4o-mini",
  },
  controllers: [
    {
      name: "Gmail Connector",
      protocol: "class",
      application: typia.llm.application<
        Pick<
          GmailService,
          | "createDraft"
          | "findEmails"
          | "deleteMailList"
          | "sendEmail"
          | "hardDelete"
        >,
        "chatgpt"
      >(),
      execute: new GmailService({
        clientId: process.env.GMAIL_CLIENT_ID!,
        clientSecret: process.env.GMAIL_CLIENT_SECRET!,
        secret: process.env.GMAIL_REFRESH_TOKEN!,
      }),
    },
  ],
});
Enter fullscreen mode Exit fullscreen mode

This selective exposure makes your integration more secure and easier to maintain.


Conclusion

By using the Agentica CLI, you can build an AI-powered Gmail Agent in just a few minutes—without the hassle of manual setup. Say goodbye to repetitive email tasks and free up your time for more important work.

Start building your Gmail Agent today and experience the benefits of smart email automation! 🚀

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay