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
2 changes: 1 addition & 1 deletion resend-slack-email-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Each email chain gets one long-running [Trigger.dev](https://trigger.dev) run th
- `src/trigger/email-thread.ts`: one run per email chain. It posts the email to Slack, drafts a reply, creates a wait token and waits on it. A click sends or drops the draft, a thread reply redrafts it, and a new email posts and redrafts. After 30 quiet days the run ends; the next email starts a new run in the same Slack thread.
- `src/trigger/inbound.ts`: the webhook tasks. A new email wakes the chain's run, or starts one. Slack thread replies and button clicks complete the waiting token.
- `src/trigger/draft.ts`: the drafting prompt, using the [AI SDK](https://ai-sdk.dev) through the Vercel AI Gateway. Tune `SYSTEM_PROMPT` for your inbox.
- `src/trigger/resend.ts` and `src/trigger/slack.ts`: thin API clients.
- `src/trigger/resend.ts`: reads threads and sends replies with the [Resend SDK](https://resend.com/docs/sdks). `src/trigger/slack.ts`: a thin Slack Web API client.

What keeps it safe:

Expand Down
1 change: 1 addition & 0 deletions resend-slack-email-agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@trigger.dev/sdk": "4.8.0-webhooks.20261020092920",
"@trigger.dev/slack": "4.8.0-webhooks.20261020092920",
"ai": "^7.0.143",
"resend": "^6.39.0",
"zod": "^4.6.7"
},
"devDependencies": {
Expand Down
40 changes: 40 additions & 0 deletions resend-slack-email-agent/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 22 additions & 21 deletions resend-slack-email-agent/src/trigger/resend.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Thin client for the Resend Inboxes API: https://resend.com/docs/api-reference/inboxes
// Resend Inboxes, through the Resend SDK: https://resend.com/docs/api-reference/inboxes
import { Resend } from "resend";

export type ResendWebhookEvent = {
type: string;
Expand All @@ -23,30 +24,29 @@ export type ThreadEmail = {
received_at: string;
};

async function resend<T>(path: string, init: RequestInit = {}): Promise<T> {
// Created on first use, so the task files load without RESEND_API_KEY set.
let client: Resend | undefined;
function resend(): Resend {
const key = process.env.RESEND_API_KEY;
if (!key) throw new Error("RESEND_API_KEY is not set");
const res = await fetch(`https://api.resend.com${path}`, {
...init,
headers: { authorization: `Bearer ${key}`, "content-type": "application/json", ...init.headers },
});
if (!res.ok) throw new Error(`Resend ${init.method ?? "GET"} ${path} failed: ${res.status} ${await res.text()}`);
return (await res.json()) as T;
return (client ??= new Resend(key));
}

const threadPath = (inboxId: string, threadId: string) =>
`/inboxes/${encodeURIComponent(inboxId)}/threads/${encodeURIComponent(threadId)}`;

// Every message in the thread, oldest first, with bodies.
export async function listThreadEmails(inboxId: string, threadId: string): Promise<ThreadEmail[]> {
const emails: ThreadEmail[] = [];
let after: string | undefined;
for (;;) {
const query = new URLSearchParams({ limit: "100", ...(after ? { after } : {}) });
const page = await resend<{ data: ThreadEmail[]; has_more: boolean }>(`${threadPath(inboxId, threadId)}/emails?${query}`);
emails.push(...page.data);
if (!page.has_more || page.data.length === 0) return emails;
after = page.data.at(-1)!.id;
const { data, error } = await resend().inboxes.threads.emails.list({
inboxId,
threadId,
limit: 100,
...(after ? { after } : {}),
});
if (error) throw new Error(`Resend list thread emails failed: ${error.message}`);
emails.push(...data.data);
if (!data.has_more || data.data.length === 0) return emails;
after = data.data.at(-1)!.id;
}
}

Expand All @@ -59,11 +59,12 @@ export async function replyToEmail(
text: string,
idempotencyKey: string
): Promise<{ id: string }> {
return resend(`${threadPath(inboxId, threadId)}/emails/${encodeURIComponent(emailId)}/reply`, {
method: "POST",
headers: { "idempotency-key": idempotencyKey },
body: JSON.stringify({ text }),
});
const { data, error } = await resend().inboxes.threads.emails.reply(
{ inboxId, threadId, emailId, text },
{ idempotencyKey }
);
if (error) throw new Error(`Resend reply failed: ${error.message}`);
return data;
}

// The readable body of an email: its text part, or its HTML with the tags stripped.
Expand Down