commune-ai 0.1.4 → 0.1.6
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 +154 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,6 +16,25 @@ 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
|
+
- [Listen for Specific Inbox Events](#listen-for-specific-inbox-events)
|
|
33
|
+
- [Semantic Search (Coming Soon)](#semantic-search-coming-soon)
|
|
34
|
+
- [Complete agent example](#complete-agent-example)
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
19
38
|
## Install
|
|
20
39
|
```bash
|
|
21
40
|
npm install commune-ai
|
|
@@ -224,6 +243,141 @@ console.log(`Found ${searchResults.length} relevant emails`);
|
|
|
224
243
|
|
|
225
244
|
---
|
|
226
245
|
|
|
246
|
+
## Manage Inboxes Programmatically
|
|
247
|
+
|
|
248
|
+
Create and manage email inboxes for your agents programmatically.
|
|
249
|
+
|
|
250
|
+
### Create Multiple Inboxes
|
|
251
|
+
|
|
252
|
+
Set up different inboxes for different purposes:
|
|
253
|
+
|
|
254
|
+
```ts
|
|
255
|
+
import { CommuneClient } from "commune-ai";
|
|
256
|
+
|
|
257
|
+
const client = new CommuneClient({
|
|
258
|
+
apiKey: process.env.COMMUNE_API_KEY,
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
// Create inboxes for different departments
|
|
262
|
+
async function setupInboxes(domainId: string) {
|
|
263
|
+
const supportInbox = await client.inboxes.create(domainId, {
|
|
264
|
+
localPart: "support", // Creates support@yourdomain.com
|
|
265
|
+
agent: {
|
|
266
|
+
id: "support-agent",
|
|
267
|
+
name: "Support Bot",
|
|
268
|
+
metadata: { department: "customer-success" }
|
|
269
|
+
},
|
|
270
|
+
status: "active",
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const salesInbox = await client.inboxes.create(domainId, {
|
|
274
|
+
localPart: "sales", // Creates sales@yourdomain.com
|
|
275
|
+
agent: {
|
|
276
|
+
id: "sales-agent",
|
|
277
|
+
name: "Sales Bot",
|
|
278
|
+
metadata: { department: "sales" }
|
|
279
|
+
},
|
|
280
|
+
status: "active",
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
return { supportInbox, salesInbox };
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### Set Webhooks for Specific Inboxes
|
|
288
|
+
|
|
289
|
+
Configure webhooks per inbox to route emails to different handlers:
|
|
290
|
+
|
|
291
|
+
```ts
|
|
292
|
+
async function configureWebhooks(domainId: string, inboxes: any) {
|
|
293
|
+
// Support inbox webhook
|
|
294
|
+
await client.inboxes.setWebhook(domainId, inboxes.supportInbox.id, {
|
|
295
|
+
endpoint: "https://your-api.com/webhooks/support",
|
|
296
|
+
events: ["email.received"],
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// Sales inbox webhook
|
|
300
|
+
await client.inboxes.setWebhook(domainId, inboxes.salesInbox.id, {
|
|
301
|
+
endpoint: "https://your-api.com/webhooks/sales",
|
|
302
|
+
events: ["email.received"],
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
### List and Manage Inboxes
|
|
308
|
+
|
|
309
|
+
```ts
|
|
310
|
+
// List all inboxes for a domain
|
|
311
|
+
const inboxes = await client.inboxes.list(domainId);
|
|
312
|
+
console.log(`Found ${inboxes.length} inboxes`);
|
|
313
|
+
|
|
314
|
+
// Update an inbox
|
|
315
|
+
await client.inboxes.update(domainId, inboxId, {
|
|
316
|
+
localPart: "help", // Change from "support" to "help"
|
|
317
|
+
status: "active",
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
// Delete an inbox
|
|
321
|
+
await client.inboxes.remove(domainId, inboxId);
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
---
|
|
325
|
+
|
|
326
|
+
## Listen for Specific Inbox Events
|
|
327
|
+
|
|
328
|
+
Route incoming emails based on which inbox received them:
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
import { createWebhookHandler } from "commune-ai";
|
|
332
|
+
|
|
333
|
+
const handler = createWebhookHandler({
|
|
334
|
+
onEvent: async (message, context) => {
|
|
335
|
+
const { inboxId, inboxAddress } = context.payload;
|
|
336
|
+
|
|
337
|
+
console.log(`📧 Email received at: ${inboxAddress}`);
|
|
338
|
+
|
|
339
|
+
// Route to different handlers based on inbox
|
|
340
|
+
if (inboxAddress?.includes("support")) {
|
|
341
|
+
await handleSupportEmail(message, context);
|
|
342
|
+
} else if (inboxAddress?.includes("sales")) {
|
|
343
|
+
await handleSalesEmail(message, context);
|
|
344
|
+
} else {
|
|
345
|
+
// Default handler for other inboxes
|
|
346
|
+
await handleGeneralEmail(message, context);
|
|
347
|
+
}
|
|
348
|
+
},
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
// Example handlers
|
|
352
|
+
async function handleSupportEmail(message: any, context: any) {
|
|
353
|
+
const sender = message.participants.find(p => p.role === "sender")?.identity;
|
|
354
|
+
if (sender) {
|
|
355
|
+
await client.messages.send({
|
|
356
|
+
to: sender,
|
|
357
|
+
text: "Thanks for contacting support! We'll get back to you soon.",
|
|
358
|
+
conversation_id: message.conversation_id,
|
|
359
|
+
domainId: context.payload.domainId,
|
|
360
|
+
inboxId: context.payload.inboxId,
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async function handleSalesEmail(message: any, context: any) {
|
|
366
|
+
const sender = message.participants.find(p => p.role === "sender")?.identity;
|
|
367
|
+
if (sender) {
|
|
368
|
+
await client.messages.send({
|
|
369
|
+
to: sender,
|
|
370
|
+
text: "Thanks for your interest! A sales representative will contact you.",
|
|
371
|
+
conversation_id: message.conversation_id,
|
|
372
|
+
domainId: context.payload.domainId,
|
|
373
|
+
inboxId: context.payload.inboxId,
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
---
|
|
380
|
+
|
|
227
381
|
## Semantic Search (Coming Soon)
|
|
228
382
|
|
|
229
383
|
Search across all emails in your organization using natural language queries.
|