commune-ai 0.1.2 → 0.1.4
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 +104 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,51 +27,100 @@ pnpm add commune-ai
|
|
|
27
27
|
|
|
28
28
|
---
|
|
29
29
|
|
|
30
|
+
## How to Setup (Dashboard Steps)
|
|
31
|
+
|
|
32
|
+
**Before you can receive emails, you need to set up your domain, inbox, and API key in the Commune dashboard.**
|
|
33
|
+
|
|
34
|
+
### 1. Create and Verify Domain
|
|
35
|
+
1. Go to your [Commune dashboard](https://your-dashboard.com)
|
|
36
|
+
2. Sign up and create an organization
|
|
37
|
+
3. Go to **Domains** → Click **"Add Domain"**
|
|
38
|
+
4. Enter a subdomain (e.g., `agent.yourcompany.com`)
|
|
39
|
+
5. Click **"Create"** - The dashboard will show DNS records to add
|
|
40
|
+
6. Add the DNS records shown in the dashboard to your DNS provider
|
|
41
|
+
7. Click **"Verify"** in the dashboard (wait 5-10 minutes for DNS propagation)
|
|
42
|
+
|
|
43
|
+
### 2. Create Inbox
|
|
44
|
+
1. In the dashboard, go to **Inboxes**
|
|
45
|
+
2. Click **"Create Inbox"**
|
|
46
|
+
3. Enter a local part (e.g., `support`, `help`, `agent`)
|
|
47
|
+
4. This creates an email address like `support@agent.yourcompany.com`
|
|
48
|
+
5. **Copy the Domain ID and Inbox ID** - you'll need these for your webhook
|
|
49
|
+
|
|
50
|
+
### 3. Create API Key
|
|
51
|
+
1. Go to **API Keys** in the dashboard
|
|
52
|
+
2. Click **"Create Key"**
|
|
53
|
+
3. Enter a name (e.g., "production-agent")
|
|
54
|
+
4. **Copy the API key immediately** (it only shows once!)
|
|
55
|
+
5. Store it securely as an environment variable
|
|
56
|
+
|
|
57
|
+
### 4. Configure Webhook
|
|
58
|
+
1. In your domain settings, go to **Webhooks**
|
|
59
|
+
2. Set the webhook endpoint URL to your server's webhook handler
|
|
60
|
+
3. Configure webhook events (usually "email.received")
|
|
61
|
+
4. **Optional:** Set a webhook secret for verification
|
|
62
|
+
|
|
63
|
+
### 5. Environment Variables
|
|
64
|
+
```bash
|
|
65
|
+
# Required
|
|
66
|
+
COMMUNE_API_KEY=cmk_your_api_key_from_dashboard
|
|
67
|
+
|
|
68
|
+
# Optional (for self-hosting)
|
|
69
|
+
COMMUNE_BASE_URL=https://your-self-hosted-api.com
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
30
74
|
## Receive emails instantly
|
|
31
75
|
|
|
32
76
|
Get emails delivered to your agent as structured webhook data.
|
|
33
77
|
|
|
34
78
|
```ts
|
|
79
|
+
import "dotenv/config";
|
|
35
80
|
import express from "express";
|
|
36
81
|
import { CommuneClient, createWebhookHandler } from "commune-ai";
|
|
37
82
|
|
|
83
|
+
// Initialize with your API key from dashboard
|
|
38
84
|
const client = new CommuneClient({
|
|
39
|
-
apiKey: process.env.COMMUNE_API_KEY,
|
|
85
|
+
apiKey: process.env.COMMUNE_API_KEY, // ← From Step 3 above
|
|
40
86
|
});
|
|
41
87
|
|
|
88
|
+
// Handle incoming emails
|
|
42
89
|
const handler = createWebhookHandler({
|
|
43
90
|
onEvent: async (message, context) => {
|
|
44
|
-
|
|
45
|
-
// message = {
|
|
46
|
-
// channel: "email",
|
|
47
|
-
// conversation_id: "thread_id",
|
|
48
|
-
// participants: [{ role: "sender", identity: "user@example.com" }],
|
|
49
|
-
// content: "Can you help with pricing?",
|
|
50
|
-
// metadata: { subject: "Pricing Question", ... }
|
|
51
|
-
// }
|
|
91
|
+
console.log(`📧 New email: ${message.content}`);
|
|
52
92
|
|
|
53
|
-
|
|
93
|
+
// Find sender's email address
|
|
94
|
+
const sender = message.participants.find(p => p.role === "sender")?.identity;
|
|
95
|
+
if (!sender) return;
|
|
54
96
|
|
|
55
97
|
// Your agent logic here
|
|
56
98
|
const reply = `Thanks for your email! I'll help with that.`;
|
|
57
99
|
|
|
58
100
|
// Reply in the same email thread
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
101
|
+
await client.messages.send({
|
|
102
|
+
to: sender,
|
|
103
|
+
text: reply,
|
|
104
|
+
conversation_id: message.conversation_id,
|
|
105
|
+
domainId: context.payload.domainId, // ← From Step 2 above
|
|
106
|
+
inboxId: context.payload.inboxId, // ← From Step 2 above
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
console.log(`✅ Replied to: ${sender}`);
|
|
69
110
|
},
|
|
70
111
|
});
|
|
71
112
|
|
|
113
|
+
// Set up webhook endpoint
|
|
72
114
|
const app = express();
|
|
73
115
|
app.post("/webhook", express.raw({ type: "*/*" }), handler);
|
|
74
|
-
|
|
116
|
+
|
|
117
|
+
// Health check
|
|
118
|
+
app.get("/health", (req, res) => res.json({ status: "ok" }));
|
|
119
|
+
|
|
120
|
+
app.listen(3000, () => {
|
|
121
|
+
console.log("🤖 Agent listening on port 3000");
|
|
122
|
+
console.log("📧 Send emails to your inbox address to test!");
|
|
123
|
+
});
|
|
75
124
|
```
|
|
76
125
|
|
|
77
126
|
**Email structure:**
|
|
@@ -171,11 +220,44 @@ const searchResults = await client.search({
|
|
|
171
220
|
});
|
|
172
221
|
|
|
173
222
|
console.log(`Found ${searchResults.length} relevant emails`);
|
|
174
|
-
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## Semantic Search (Coming Soon)
|
|
228
|
+
|
|
229
|
+
Search across all emails in your organization using natural language queries.
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
import { CommuneClient } from "commune-ai";
|
|
233
|
+
|
|
234
|
+
const client = new CommuneClient({
|
|
235
|
+
apiKey: process.env.COMMUNE_API_KEY,
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
// Find emails similar to a query
|
|
239
|
+
const results = await client.search({
|
|
240
|
+
query: "What were the pricing questions from last week?",
|
|
241
|
+
limit: 10,
|
|
242
|
+
threshold: 0.7, // Similarity threshold 0-1
|
|
243
|
+
before: "2024-12-31", // Only search recent emails
|
|
244
|
+
sender: "customer@example.com", // Filter by sender (optional)
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
console.log(`Found ${results.length} relevant emails:`);
|
|
248
|
+
for (const result of results) {
|
|
175
249
|
console.log(`Match (${result.similarity.toFixed(2)}): ${result.message.content}`);
|
|
250
|
+
console.log(`Highlights: ${result.highlights.join(", ")}`);
|
|
176
251
|
}
|
|
177
252
|
```
|
|
178
253
|
|
|
254
|
+
**What you get:**
|
|
255
|
+
- **Natural language queries** - Search like "pricing issues from enterprise customers"
|
|
256
|
+
- **Similarity scores** - Ranked results by relevance (0-1)
|
|
257
|
+
- **Highlighted matches** - Text snippets showing why the email matched
|
|
258
|
+
- **Filtering options** - By date range, sender, or other criteria
|
|
259
|
+
- **Fast results** - AI-powered semantic matching
|
|
260
|
+
|
|
179
261
|
---
|
|
180
262
|
|
|
181
263
|
## Complete agent example
|