-
Notifications
You must be signed in to change notification settings - Fork 9.9k
agents sdk changelog (email, .queue and automatic context) #23828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f876f65
agents sdk changelog
whoiskatrin 7d10817
simplify examples
whoiskatrin ca349c2
simplify examples
whoiskatrin 68fc25f
simplify examples
whoiskatrin 22c6436
simplify examples
whoiskatrin a6563a0
Update src/content/changelog/agents/2025-07-21-agents-queue-email-con…
whoiskatrin 8f805fa
fixed trailing
whoiskatrin 179ecd5
small fixes and typos
whoiskatrin daf9d82
fix hyperlink
whoiskatrin c6a6781
Update 2025-07-21-agents-queue-email-context-update.mdx
threepointone 3f5e6b6
Update 2025-07-21-agents-queue-email-context-update.mdx
threepointone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
src/content/changelog/agents/2025-07-21-agents-queue-email-context-update.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| --- | ||
| 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 | ||
| - workflows | ||
| 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. | ||
whoiskatrin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Here's a full example using .queue() inside a tool, with batching and concurrency control: | ||
|
|
||
| ```ts | ||
| const queueMessage = tool({ | ||
| description: "Queue a message to be processed by the agent", | ||
| parameters: z.object({ | ||
| message: z.string().describe("The message to process"), | ||
| priority: z | ||
| .enum(["low", "normal", "high"]) | ||
| .default("normal") | ||
| .describe("Priority level for the message"), | ||
| }), | ||
| execute: async ({ message, priority = "normal" }) => { | ||
| const { agent } = getCurrentAgent<Chat>(); | ||
|
|
||
| if (!agent) { | ||
| return "Error: Agent not available"; | ||
| } | ||
|
|
||
| try { | ||
| // Create a batch of 100 messages | ||
| const batchNumbers = Array.from({ length: 100 }, (_, i) => i + 1); | ||
|
|
||
| const taskIds = await pMap( | ||
| batchNumbers, | ||
| async (i) => { | ||
| return agent.queue("processMessage", { | ||
| message: `${message} (batch ${i}/100)`, | ||
| priority, | ||
| }); | ||
| }, | ||
| { concurrency: 6 }, // Run up to 6 queues in parallel | ||
| ); | ||
|
|
||
| return `Queued 100 messages. Example Task IDs: ${taskIds.slice(0, 5).join(", ")}...`; | ||
| } catch (error) { | ||
| console.error("Error queueing message", error); | ||
| return `Error queueing message: ${error}`; | ||
| } | ||
| }, | ||
| }); | ||
| ``` | ||
whoiskatrin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Notes: | ||
|
|
||
| - this uses pMap to handle concurrent queuing with a limit (concurrency: 6). | ||
| - each .queue() call returns a task ID you can track. | ||
| - works seamlessly thanks to automatic context wrapping | ||
whoiskatrin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Want to try it yourself? Just define a method like processMessage in your agent, and you’re ready to scale. | ||
|
|
||
| ### New email adaptor | ||
|
|
||
| Want to build an AI agent that can receive and respond to emails automatically? With the new email adaptor and onEmail lifecycle method, now you can. | ||
whoiskatrin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```ts | ||
| export class EmailAgent extends Agent<Env, EmailAgentState> { | ||
| initialState = { | ||
| emailCount: 0, | ||
| emails: [], | ||
| autoReplyEnabled: true, | ||
| lastUpdated: new Date(), | ||
| }; | ||
|
|
||
| async onEmail(email: AgentEmail) { | ||
| const raw = await email.getRaw(); | ||
| const parsed = await PostalMime.parse(raw); | ||
|
|
||
| this.setState({ | ||
| ...this.state, | ||
| emailCount: this.state.emailCount + 1, | ||
| emails: [ | ||
| ...this.state.emails.slice(-9), | ||
| { | ||
| from: parsed.from?.address ?? email.from, | ||
| subject: parsed.subject ?? "No Subject", | ||
| text: parsed.text, | ||
| html: parsed.html, | ||
| to: email.to, | ||
| timestamp: new Date(), | ||
| messageId: parsed.messageId, | ||
| }, | ||
| ], | ||
| lastUpdated: new Date(), | ||
| }); | ||
|
|
||
| if (this.state.autoReplyEnabled && !this.isAutoReply(parsed)) { | ||
| await this.replyToEmail(email, { | ||
| fromName: "Email Agent", | ||
| body: `Thanks for your email! You've sent us "${parsed.subject}". We'll process it shortly.`, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
whoiskatrin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| 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] (cloudflare/agents/examples/email-agent) | ||
whoiskatrin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### Automatic context wrapping for custom methods | ||
|
|
||
| Defining custom methods on your agent is now even safer and cleaner. This means getCurrentAgent(), getCurrentRequest(), and getCurrentConnection() are now reliably accessible from anywhere in your agent — not just inside onRequest. | ||
|
|
||
| ```ts | ||
| export class MyAgent extends Agent<Env, State> { | ||
| async suggestReply(ctx, { message }) { | ||
whoiskatrin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // getCurrentAgent() now works! | ||
| return generateText({ | ||
| prompt: `Suggest a reply to: "${message}"`, | ||
| tools: [replyWithEmoji], | ||
| }); | ||
| } | ||
| } | ||
whoiskatrin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
whoiskatrin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Try it out and tell us what you build! | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.