diff --git a/src/content/changelog/agents/2025-07-21-agents-queue-email-context-update.mdx b/src/content/changelog/agents/2025-07-21-agents-queue-email-context-update.mdx new file mode 100644 index 00000000000000..5ccd67e97f943f --- /dev/null +++ b/src/content/changelog/agents/2025-07-21-agents-queue-email-context-update.mdx @@ -0,0 +1,84 @@ +--- +title: Agents SDK now supports task queues, email handling, and context-wrapped methods +description: The latest release adds .queue(), email support, and automatic context for custom methods +products: + - agents + - workers +date: 2025-07-21 +--- + +The latest releases of [@cloudflare/agents](https://github.com/cloudflare/agents) brings three powerful improvements that make building and scaling agents even easier. You now get: + +### Lightweight .queue for fast task deferral + +You can use `.queue()` to enqueue background work — ideal for tasks like processing user messages, sending notifications etc. + +```ts +class MyAgent extends Agent { + doSomethingExpensive(payload) { + // a long running process that you want to run in the background + } + + queueSomething() { + await this.queue("doSomethingExpensive", somePayload); // this will NOT block further execution, and runs in the background + await this.queue("doSomethingExpensive", someOtherPayload); // the callback will NOT run until the previous callback is complete + // ... call as many times as you want + } +} +``` + +Want to try it yourself? Just define a method like processMessage in your agent, and you’re ready to scale. + +### New email adapter + +Want to build an AI agent that can receive and respond to emails automatically? With the new email adapter and onEmail lifecycle method, now you can. + +```ts +export class EmailAgent extends Agent { + async onEmail(email: AgentEmail) { + const raw = await email.getRaw(); + const parsed = await PostalMime.parse(raw); + + // create a response based on the email contents + // and then send a reply + + await this.replyToEmail(email, { + fromName: "Email Agent", + body: `Thanks for your email! You've sent us "${parsed.subject}". We'll process it shortly.`, + }); + } +} +``` + +You route incoming mail like this: + +```ts +export default { + async email(email, env) { + await routeAgentEmail(email, env, { + resolver: createAddressBasedEmailResolver("EmailAgent"), + }); + }, +}; +``` + +You can find a full example [here](https://github.com/cloudflare/agents/tree/main/examples/email-agent). + +### Automatic context wrapping for custom methods + +Custom methods are now automatically wrapped with the agent's context, so calling `getCurrentAgent()` should work regardless of where in an agent's lifecycle it's called. Previously this would not work on RPC calls, but now just works out of the box. + +```ts +export class MyAgent extends Agent { + async suggestReply(message) { + // getCurrentAgent() now correctly works, even when called inside an RPC method + const { agent } = getCurrentAgent()!; + return generateText({ + prompt: `Suggest a reply to: "${message}" from "${agent.name}"`, + tools: [replyWithEmoji], + }); + } +} +``` + +Try it out and tell us what you build!