Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion workers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { handleReplyEmail, handleForwardEmail } from "./routes/reply-forward";
import { Folders } from "../shared/folders";
import type { Env } from "./types";
import { requireMailbox, type MailboxContext } from "./lib/mailbox";
import { logSendRateLimitHit } from "./lib/rate-limit";

type AppContext = Context<MailboxContext>;

Expand Down Expand Up @@ -181,7 +182,10 @@ app.post("/api/v1/mailboxes/:mailboxId/emails", async (c: AppContext) => {
const { messageId, outgoingMessageId } = generateMessageId(fromDomain);
const stub = c.var.mailboxStub;
const rateLimitError = await (stub as any).checkSendRateLimit();
if (rateLimitError) return c.json({ error: rateLimitError }, 429);
if (rateLimitError) {
logSendRateLimitHit(mailboxId, "api.sendEmail", rateLimitError);
return c.json({ error: rateLimitError }, 429);
}
const attachmentData = await storeAttachments(c.env.BUCKET, messageId, attachments);

await stub.createEmail(Folders.SENT, {
Expand Down
11 changes: 11 additions & 0 deletions workers/lib/rate-limit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) 2026 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

export function logSendRateLimitHit(
mailboxId: string,
source: string,
error: string,
) {
console.warn("Send rate limit hit", { mailboxId, source, error });
}
3 changes: 3 additions & 0 deletions workers/lib/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
buildThreadingHeaders,
} from "./email-helpers";
import { verifyDraft } from "./ai";
import { logSendRateLimitHit } from "./rate-limit";
import { sendEmail } from "../email-sender";
import { Folders } from "../../shared/folders";
import type { Env } from "../types";
Expand Down Expand Up @@ -408,6 +409,7 @@ export async function toolSendReply(
// Check send rate limit
const rateLimitError = await (stub as unknown as RateLimitStub).checkSendRateLimit();
if (rateLimitError) {
logSendRateLimitHit(mailboxId, "tool.sendReply", rateLimitError);
return { error: rateLimitError };
}

Expand Down Expand Up @@ -486,6 +488,7 @@ export async function toolSendEmail(
// Check send rate limit
const rateLimitError = await (stub as unknown as RateLimitStub).checkSendRateLimit();
if (rateLimitError) {
logSendRateLimitHit(mailboxId, "tool.sendEmail", rateLimitError);
return { error: rateLimitError };
}

Expand Down
3 changes: 3 additions & 0 deletions workers/routes/reply-forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { SendEmailRequestSchema } from "../lib/schemas";
import { Folders } from "../../shared/folders";
import type { MailboxContext } from "../lib/mailbox";
import { logSendRateLimitHit } from "../lib/rate-limit";

type AppContext = Context<MailboxContext>;
type RateLimitStub = { checkSendRateLimit: () => Promise<string | null> };
Expand Down Expand Up @@ -50,6 +51,7 @@ export async function handleReplyEmail(c: AppContext) {
const rateLimitError = await (stub as unknown as RateLimitStub)
.checkSendRateLimit();
if (rateLimitError) {
logSendRateLimitHit(mailboxId, "api.replyEmail", rateLimitError);
return c.json({ error: rateLimitError }, 429);
}

Expand Down Expand Up @@ -140,6 +142,7 @@ export async function handleForwardEmail(c: AppContext) {
const rateLimitError = await (stub as unknown as RateLimitStub)
.checkSendRateLimit();
if (rateLimitError) {
logSendRateLimitHit(mailboxId, "api.forwardEmail", rateLimitError);
return c.json({ error: rateLimitError }, 429);
}

Expand Down