commune-ai 0.1.5 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +99 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,6 +16,26 @@ Build agents that receive emails, reply in threads, access conversation history,
|
|
|
16
16
|
|
|
17
17
|
---
|
|
18
18
|
|
|
19
|
+
## Table of Contents
|
|
20
|
+
|
|
21
|
+
- [Install](#install)
|
|
22
|
+
- [How to Setup (Dashboard Steps)](#how-to-setup-dashboard-steps)
|
|
23
|
+
- [1. Create and Verify Domain](#1-create-and-verify-domain)
|
|
24
|
+
- [2. Create Inbox](#2-create-inbox)
|
|
25
|
+
- [3. Create API Key](#3-create-api-key)
|
|
26
|
+
- [4. Configure Webhook](#4-configure-webhook)
|
|
27
|
+
- [5. Environment Variables](#5-environment-variables)
|
|
28
|
+
- [Receive emails instantly](#receive-emails-instantly)
|
|
29
|
+
- [Reply in email threads](#reply-in-email-threads)
|
|
30
|
+
- [Access conversation history](#access-conversation-history)
|
|
31
|
+
- [Manage Inboxes Programmatically](#manage-inboxes-programmatically)
|
|
32
|
+
- [How Webhooks Work](#how-webhooks-work)
|
|
33
|
+
- [Listen for Specific Inbox Events](#listen-for-specific-inbox-events)
|
|
34
|
+
- [Semantic Search (Coming Soon)](#semantic-search-coming-soon)
|
|
35
|
+
- [Complete agent example](#complete-agent-example)
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
19
39
|
## Install
|
|
20
40
|
```bash
|
|
21
41
|
npm install commune-ai
|
|
@@ -71,9 +91,24 @@ COMMUNE_BASE_URL=https://your-self-hosted-api.com
|
|
|
71
91
|
|
|
72
92
|
---
|
|
73
93
|
|
|
74
|
-
## Receive emails
|
|
94
|
+
## Receive emails at your webhook endpoint
|
|
95
|
+
|
|
96
|
+
Configure your inbox to send emails to your server, then mount the webhook handler at that endpoint.
|
|
97
|
+
|
|
98
|
+
### Step 1: Configure webhook URL for your inbox
|
|
99
|
+
|
|
100
|
+
First, set the webhook URL in your inbox settings (via dashboard or SDK):
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
await client.inboxes.setWebhook(domainId, inboxId, {
|
|
104
|
+
endpoint: "https://your-server.com/api/emails", // Your endpoint URL
|
|
105
|
+
events: ["email.received"],
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Step 2: Mount webhook handler at that URL
|
|
75
110
|
|
|
76
|
-
|
|
111
|
+
Your server must listen at the **exact same URL** you configured:
|
|
77
112
|
|
|
78
113
|
```ts
|
|
79
114
|
import "dotenv/config";
|
|
@@ -85,8 +120,8 @@ const client = new CommuneClient({
|
|
|
85
120
|
apiKey: process.env.COMMUNE_API_KEY, // ← From Step 3 above
|
|
86
121
|
});
|
|
87
122
|
|
|
88
|
-
//
|
|
89
|
-
const
|
|
123
|
+
// Create webhook handler to process incoming emails
|
|
124
|
+
const emailHandler = createWebhookHandler({
|
|
90
125
|
onEvent: async (message, context) => {
|
|
91
126
|
console.log(`📧 New email: ${message.content}`);
|
|
92
127
|
|
|
@@ -110,9 +145,11 @@ const handler = createWebhookHandler({
|
|
|
110
145
|
},
|
|
111
146
|
});
|
|
112
147
|
|
|
113
|
-
// Set up
|
|
148
|
+
// Set up your server
|
|
114
149
|
const app = express();
|
|
115
|
-
|
|
150
|
+
|
|
151
|
+
// This URL must match what you configured in Step 1
|
|
152
|
+
app.post("/api/emails", express.raw({ type: "*/*" }), emailHandler);
|
|
116
153
|
|
|
117
154
|
// Health check
|
|
118
155
|
app.get("/health", (req, res) => res.json({ status: "ok" }));
|
|
@@ -123,6 +160,12 @@ app.listen(3000, () => {
|
|
|
123
160
|
});
|
|
124
161
|
```
|
|
125
162
|
|
|
163
|
+
**Complete flow:**
|
|
164
|
+
1. Email arrives at your inbox
|
|
165
|
+
2. Resend sends webhook to Commune backend
|
|
166
|
+
3. Commune backend sends email to your configured endpoint (`/api/emails`)
|
|
167
|
+
4. Your `emailHandler` processes the email and replies
|
|
168
|
+
|
|
126
169
|
**Email structure:**
|
|
127
170
|
```ts
|
|
128
171
|
interface UnifiedMessage {
|
|
@@ -157,7 +200,8 @@ const client = new CommuneClient({
|
|
|
157
200
|
apiKey: process.env.COMMUNE_API_KEY,
|
|
158
201
|
});
|
|
159
202
|
|
|
160
|
-
|
|
203
|
+
// Create webhook handler
|
|
204
|
+
const emailHandler = createWebhookHandler({
|
|
161
205
|
onEvent: async (message, context) => {
|
|
162
206
|
const sender = message.participants.find(p => p.role === "sender")?.identity;
|
|
163
207
|
if (!sender) return;
|
|
@@ -174,9 +218,15 @@ const handler = createWebhookHandler({
|
|
|
174
218
|
},
|
|
175
219
|
});
|
|
176
220
|
|
|
221
|
+
// Set up your server
|
|
177
222
|
const app = express();
|
|
178
|
-
|
|
179
|
-
|
|
223
|
+
|
|
224
|
+
// Mount handler at your webhook endpoint
|
|
225
|
+
app.post("/api/emails", express.raw({ type: "*/*" }), emailHandler);
|
|
226
|
+
|
|
227
|
+
app.listen(3000, () => {
|
|
228
|
+
console.log("Agent running on port 3000");
|
|
229
|
+
});
|
|
180
230
|
```
|
|
181
231
|
|
|
182
232
|
---
|
|
@@ -304,14 +354,33 @@ await client.inboxes.remove(domainId, inboxId);
|
|
|
304
354
|
|
|
305
355
|
---
|
|
306
356
|
|
|
307
|
-
##
|
|
357
|
+
## How Webhooks Work
|
|
358
|
+
|
|
359
|
+
**Understanding the webhook flow:**
|
|
308
360
|
|
|
309
|
-
|
|
361
|
+
1. **Resend Webhooks** → When you verify a domain, the backend creates a webhook at Resend (email service) that points to the Commune backend
|
|
362
|
+
2. **Commune Backend** → Processes the webhook from Resend and looks up inbox configurations
|
|
363
|
+
3. **Your Webhook Endpoint** → If an inbox has a webhook URL set, Commune sends an HTTP POST to your endpoint
|
|
364
|
+
|
|
365
|
+
```typescript
|
|
366
|
+
// Webhook flow: Resend → Commune Backend → Your Server
|
|
367
|
+
// Your server uses createWebhookHandler to process webhooks from Commune
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
## Listen for specific inbox events
|
|
371
|
+
|
|
372
|
+
Route incoming emails based on which inbox received them. Set up different webhook endpoints for different inboxes.
|
|
310
373
|
|
|
311
374
|
```ts
|
|
312
|
-
import
|
|
375
|
+
import express from "express";
|
|
376
|
+
import { CommuneClient, createWebhookHandler } from "commune-ai";
|
|
313
377
|
|
|
314
|
-
const
|
|
378
|
+
const client = new CommuneClient({
|
|
379
|
+
apiKey: process.env.COMMUNE_API_KEY,
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
// Create webhook handler for routing
|
|
383
|
+
const emailRouter = createWebhookHandler({
|
|
315
384
|
onEvent: async (message, context) => {
|
|
316
385
|
const { inboxId, inboxAddress } = context.payload;
|
|
317
386
|
|
|
@@ -329,6 +398,18 @@ const handler = createWebhookHandler({
|
|
|
329
398
|
},
|
|
330
399
|
});
|
|
331
400
|
|
|
401
|
+
// Set up your server
|
|
402
|
+
const app = express();
|
|
403
|
+
|
|
404
|
+
// Mount the same handler at different endpoints for different inboxes
|
|
405
|
+
app.post("/api/support", express.raw({ type: "*/*" }), emailRouter); // For support inbox
|
|
406
|
+
app.post("/api/sales", express.raw({ type: "*/*" }), emailRouter); // For sales inbox
|
|
407
|
+
app.post("/api/general", express.raw({ type: "*/*" }), emailRouter); // For general inbox
|
|
408
|
+
|
|
409
|
+
app.listen(3000, () => {
|
|
410
|
+
console.log("Agent running with multiple inbox endpoints");
|
|
411
|
+
});
|
|
412
|
+
|
|
332
413
|
// Example handlers
|
|
333
414
|
async function handleSupportEmail(message: any, context: any) {
|
|
334
415
|
const sender = message.participants.find(p => p.role === "sender")?.identity;
|
|
@@ -408,7 +489,7 @@ const client = new CommuneClient({
|
|
|
408
489
|
apiKey: process.env.COMMUNE_API_KEY,
|
|
409
490
|
});
|
|
410
491
|
|
|
411
|
-
const
|
|
492
|
+
const emailProcessor = createWebhookHandler({
|
|
412
493
|
onEvent: async (message, context) => {
|
|
413
494
|
const sender = message.participants.find(p => p.role === "sender")?.identity;
|
|
414
495
|
if (!sender) return;
|
|
@@ -452,8 +533,11 @@ const handler = createWebhookHandler({
|
|
|
452
533
|
},
|
|
453
534
|
});
|
|
454
535
|
|
|
536
|
+
// Set up your server
|
|
455
537
|
const app = express();
|
|
456
|
-
|
|
538
|
+
|
|
539
|
+
// Mount webhook handler at your endpoint
|
|
540
|
+
app.post("/api/emails", express.raw({ type: "*/*" }), emailProcessor);
|
|
457
541
|
|
|
458
542
|
// Health check endpoint
|
|
459
543
|
app.get("/health", (req, res) => res.json({ status: "ok" }));
|