DEV Community

Cover image for AI E-Mail Event Booking Agent
Dmitry Rechkin
Dmitry Rechkin

Posted on

AI E-Mail Event Booking Agent

This is a submission for the Nylas Challenge: AI Expedition.

What I Built and Why

As a member of the strata council, I regularly handle reservations for the amenity room. While the current volume of bookings isn’t overwhelming, it does come with its own set of challenges—negotiating time slots, enforcing reservation rules, ensuring the completion of PDF forms, verifying them, and then manually booking the room.

With more residents moving into our building, I began to think about automating this process. However, communicating new methods, such as using specific URLs for reservations, might not work for everyone. That's when the idea of building an AI agent to manage reservations started taking shape in my mind. The Nylas challenge was the final push I needed to put aside other projects and dive into this one.

Nylas offers the exact tools I was looking for, so I decided to create something practical that we can actually use in our building.

Demo

Driven by the motto “go PRO or go home,” I might have gone a bit overboard with the video. The real action starts at the 2:30 mark and lasts about 2 minutes.

Code

The source code for the AI email event booking agent can be found on

AI Email Event Booking Agent

This project implements an AI-powered Email Event Booking Agent designed to automate the process of handling incoming event booking requests via email. The agent intelligently processes emails, validates the information provided, requests additional details if necessary, checks calendar availability, fills out PDF forms, and books events.

This solution is optimized to run on serverless Cloudflare Workers, aiming to minimize external dependencies for enhanced speed and efficiency. Many packages have been custom-written to support this goal, ensuring that the solution remains lightweight and fast.

Key Features

  • Serverless Architecture: Runs on Cloudflare Workers, leveraging Cloudflare KV for storage.
  • Nylas API Integration: Handles incoming emails via the Nylas API, transforming them into a common format for AI processing.
  • Dynamic AI Agent: Utilizes AI to decide which tools to use based on natural language instructions and a customizable JSON schema.
  • Minimal Dependencies: Custom code replaces…

Your Journey

I'm a huge fan of serverless technology, particularly Cloudflare Workers.

Initially, I planned to take advantage of the Nylas SDK. However, I quickly ran into issues because it wasn’t particularly compatible with Cloudflare Workers.

No big deal, I figured. Nylas has well-documented APIs, which are explained in detail here.

The challenge was understanding which combination of APIs would best suit my needs.

At first, I decided to use the Create Events API and the Send Message API.

Then, I discovered the Nylas Scheduler API, which seemed perfect for my AI Event Booking Agent. You can learn more about it here.

However, I quickly realized that the requirements to create a configuration object, followed by a session, might be too complex for an AI to handle. So, I reverted to my original plan of using the Email, Calendar, and Event APIs separately.

Although the webhook provides a snippet of the email, I realized that the body of the email is often in HTML and can contain previous messages embedded within it. This could potentially confuse the AI agent, so I embarked on a journey to find a library that could extract just the latest email body as text, making it easier to feed to the LLM.

After much searching, I found that nothing worked as expected or required Node.js. So, I implemented my own solution, which turned out to be pretty decent and will likely be used in future projects: text-email-body-parser.

One of the requirements was to be able to read and fill PDF forms. While this is relatively easy with existing libraries, I needed a set of shortcut classes capable of reading and filling PDF files represented as byte arrays since working with files was out of the question in this case.

During the process of implementing my abstraction layer on top of the Nylas API, which could be used in a Node.js-less environment, I created two helper packages: calendar-nylas and message-nylas. These packages enable the AI Event Booking Agent to perform the necessary tasks for booking events and sending emails.

As I researched the best way to implement the agent, I chose Vercel’s AI SDK (AI SDK Core) because it provides a streamlined method for automating tool calls. This allows me to focus on creating tools and refining prompts instead of worrying about the mechanics.

To make my code easily adaptable for tool use, I created a tiny library, foundation-core, which lets me create actions in a predictable way and use them as tools with automatic input and output validation using Zod.

Next, I wanted to define a schema for the data to collect, so I could efficiently extract it with AI and then pass it to my tool. While Zod is amazing for this, I couldn't find a pre-existing solution that allowed dynamic schema definition without generating static code.

This led to the creation of json-schema-to-zod, which I can use to store data structures as JSON schemas in KV storage and dynamically convert them to Zod schemas for use with AI tools.

Finally, it was just a matter of putting everything together. I’ve described the architecture in the repository, so I won’t go into it again here.

Conclusion

This project was an exciting journey into the world of AI-driven automation. The combination of Nylas, Cloudflare Workers, and Vercel’s AI SDK allowed me to build a practical solution that I can use in my own building. It’s fascinating to see how AI can manage something as complex as event bookings, complete with all the quirks and challenges of real-world scenarios.

While there were a few hurdles along the way, like adapting the Nylas API for Cloudflare Workers and dealing with the intricacies of parsing email bodies, these challenges ultimately led to the creation of several reusable libraries that I’m sure will be beneficial in future projects.

In the end, the AI Event Booking Agent not only simplifies the reservation process but also opens the door to more sophisticated AI-driven workflows. I’m excited to see how this project evolves and how AI can further enhance everyday tasks in our work and life.

Top comments (0)