From b34ae1dbd64730a14e80b062200f6803d0b85e68 Mon Sep 17 00:00:00 2001 From: Jack Bridger Date: Tue, 24 Nov 2026 13:50:13 +0000 Subject: [PATCH] refactor(resend-slack-email-agent): use the Resend SDK for inbox threads and replies --- resend-slack-email-agent/README.md | 2 +- resend-slack-email-agent/package.json | 1 + resend-slack-email-agent/pnpm-lock.yaml | 40 +++++++++++++++++ .../src/trigger/resend.ts | 43 ++++++++++--------- 4 files changed, 64 insertions(+), 22 deletions(-) diff --git a/resend-slack-email-agent/README.md b/resend-slack-email-agent/README.md index eaf16386..66066631 100644 --- a/resend-slack-email-agent/README.md +++ b/resend-slack-email-agent/README.md @@ -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: diff --git a/resend-slack-email-agent/package.json b/resend-slack-email-agent/package.json index 727013dc..4a660e28 100644 --- a/resend-slack-email-agent/package.json +++ b/resend-slack-email-agent/package.json @@ -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": { diff --git a/resend-slack-email-agent/pnpm-lock.yaml b/resend-slack-email-agent/pnpm-lock.yaml index 8e313dad..8de40146 100644 --- a/resend-slack-email-agent/pnpm-lock.yaml +++ b/resend-slack-email-agent/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: ai: specifier: ^7.0.143 version: 7.0.154(zod@4.6.7) + resend: + specifier: ^6.39.0 + version: 6.39.0 zod: specifier: ^4.6.7 version: 4.6.7 @@ -447,6 +450,9 @@ packages: '@sodaru/yup-to-json-schema@2.0.1': resolution: {integrity: sha512-lWb0Wiz8KZ9ip/dY1eUqt7fhTPmL24p6Hmv5Fd9pzlzAdw/YNcWZr+tiCT4oZ4Zyxzi9+1X4zv82o7jYvcFxYA==} + '@stablelib/base64@1.0.1': + resolution: {integrity: sha512-1bnPQqSxSuc3Ii6MhBysoWCg58j97aUjuCSZrGSmDxNqtytIi0k8utUenAwTZN4V5mXXYGsVUI9zeBqy+jBOSQ==} + '@standard-schema/spec@1.1.0': resolution: {integrity: sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w==} @@ -861,6 +867,9 @@ packages: fast-npm-meta@0.2.2: resolution: {integrity: sha512-E+fdxeaOQGo/CMWc9f4uHFfgUPJRAu7N3uB8GBvB3SDPAIWJK4GKyYhkAGFq+GYrcbKNfQIz5VVQyJnDuPPCrg==} + fast-sha256@1.3.0: + resolution: {integrity: sha512-n11RGP/lrWEFI/bWdygLxhI+pVeo1ZYIVwvvPkW7azl/rOy+F3HYRZ2K5zeE9mmkhQppyv9sQFx0JM9UabnpPQ==} + fast-uri@3.1.9: resolution: {integrity: sha512-hS/0Wt3Ya043oKXs0sIxrMpKQITXhMLIrVAKKXi4gozGC9v7YRWVnXIZzjNPTO02M9BdxLCWqK1yRXgNziVyJQ==} @@ -1240,6 +1249,9 @@ packages: pkg-types@2.4.0: resolution: {integrity: sha512-07Fys7iVx0X6TcviMOgogM1Lk33ZyqOezMnU79S+kffWqwolG/5W9V8EgQSeYCIfFTYVgssRbnH630Oa2+soBA==} + postal-mime@2.7.6: + resolution: {integrity: sha512-UUlyE2KlxmvwMGq060onF/VWU3zD6dVW31FwokQ5v26jWd35fbs2MoTFzh+jXnPwAyV2MULMKXcBInGOl5TO6Q==} + posthog-node@5.35.6: resolution: {integrity: sha512-LwoXnR89A0l75jvFrXjtsAs0BbpyCjnwY8YIUkZT91rt1YnIUfBYiLj7qoUYApdNgesgWQQHqLXxLYX59a6ZYw==} engines: {node: ^20.20.0 || >=22.22.0} @@ -1292,6 +1304,15 @@ packages: resolution: {integrity: sha512-QT7FVMXfWOYFbeRBF6nu+I6tr2Tf3u0q8RIEjNob/heKY/nh7drD/k7eeMFmSQgnTtCzLDcCu/XEnpW2wk4xCQ==} engines: {node: '>=9.3.0 || >=8.10.0 <9.0.0'} + resend@6.39.0: + resolution: {integrity: sha512-szNf/OR2I5rreOt5dQr7+aZLGFhkNJ0pSjzQIkU1oIrfh7kMZFDJQ3/Cc9s1yVeapafYFg0/aNyj41lsbHPeUw==} + engines: {node: '>=20'} + peerDependencies: + '@react-email/render': '*' + peerDependenciesMeta: + '@react-email/render': + optional: true + resolve@1.22.13: resolution: {integrity: sha512-Dj/cW9zBV2aRj6XzCxuGIs3ESZKv02jixne3q6jFZYZ3/Tg09gHHviSMDL4kObh9pqSahdRHEnDoaP2gn8YLhA==} engines: {node: '>= 0.4'} @@ -1385,6 +1406,9 @@ packages: resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==} engines: {node: '>=0.10.0'} + standardwebhooks@1.1.1: + resolution: {integrity: sha512-bCbX9ZEyFkWPsRz7Bl3NuQUJohmwGSev/yhr7vhaGPlc4AfIrspIRa6cPTBuI1ItmrTDJ4d/S2hCsfe4+vQGnQ==} + statuses@2.0.2: resolution: {integrity: sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==} engines: {node: '>= 0.8'} @@ -1914,6 +1938,8 @@ snapshots: '@sodaru/yup-to-json-schema@2.0.1': {} + '@stablelib/base64@1.0.1': {} + '@standard-schema/spec@1.1.0': {} '@trigger.dev/build@4.8.0-webhooks.20261020092920(magicast@0.3.5)(supports-color@10.2.2)(typescript@5.9.3)(zod@4.5.4)': @@ -2449,6 +2475,8 @@ snapshots: fast-npm-meta@0.2.2: {} + fast-sha256@1.3.0: {} + fast-uri@3.1.9: {} fdir@6.5.0(picomatch@4.0.7): @@ -2787,6 +2815,8 @@ snapshots: exsolve: 1.1.3 pathe: 2.0.3 + postal-mime@2.7.6: {} + posthog-node@5.35.6: dependencies: '@posthog/core': 1.29.13 @@ -2837,6 +2867,11 @@ snapshots: transitivePeerDependencies: - supports-color + resend@6.39.0: + dependencies: + postal-mime: 2.7.6 + standardwebhooks: 1.1.1 + resolve@1.22.13: dependencies: es-errors: 1.3.0 @@ -2967,6 +3002,11 @@ snapshots: source-map@0.6.1: {} + standardwebhooks@1.1.1: + dependencies: + '@stablelib/base64': 1.0.1 + fast-sha256: 1.3.0 + statuses@2.0.2: {} std-env@3.10.0: {} diff --git a/resend-slack-email-agent/src/trigger/resend.ts b/resend-slack-email-agent/src/trigger/resend.ts index 5870052b..11b224cd 100644 --- a/resend-slack-email-agent/src/trigger/resend.ts +++ b/resend-slack-email-agent/src/trigger/resend.ts @@ -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; @@ -23,30 +24,29 @@ export type ThreadEmail = { received_at: string; }; -async function resend(path: string, init: RequestInit = {}): Promise { +// 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 { 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; } } @@ -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.