@peopl-health/nexus 1.1.3 → 1.1.5
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/examples/basic-usage.js
CHANGED
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
const { Nexus, setupDefaultRoutes } = require('@peopl/nexus');
|
|
2
1
|
const express = require('express');
|
|
3
|
-
require('
|
|
2
|
+
const { NexusMessaging, setupDefaultRoutes } = require('@peopl-health/nexus');
|
|
4
3
|
|
|
5
4
|
const app = express();
|
|
6
5
|
app.use(express.json());
|
|
7
6
|
|
|
8
|
-
async function
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
async function startServer() {
|
|
8
|
+
// Initialize Nexus with all services
|
|
9
|
+
const nexus = new NexusMessaging();
|
|
10
|
+
|
|
11
|
+
await nexus.initialize({
|
|
12
|
+
// MongoDB connection
|
|
13
|
+
mongoUri: process.env.MONGODB_URI,
|
|
14
|
+
|
|
15
|
+
// Messaging provider
|
|
16
|
+
provider: 'twilio',
|
|
17
|
+
providerConfig: {
|
|
18
|
+
accountSid: process.env.TWILIO_ACCOUNT_SID,
|
|
19
|
+
authToken: process.env.TWILIO_AUTH_TOKEN,
|
|
20
|
+
phoneNumber: process.env.TWILIO_PHONE_NUMBER
|
|
21
|
+
}
|
|
22
|
+
});
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
// Set up all default API routes with built-in controllers
|
|
25
|
+
setupDefaultRoutes(app);
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
// Add webhook endpoint for incoming messages
|
|
28
|
+
app.post('/webhook', async (req, res) => {
|
|
29
|
+
try {
|
|
30
|
+
await nexus.processIncomingMessage(req.body);
|
|
31
|
+
res.status(200).send('OK');
|
|
32
|
+
} catch (error) {
|
|
33
|
+
console.error('Webhook error:', error);
|
|
34
|
+
res.status(500).send('Error');
|
|
35
|
+
}
|
|
36
|
+
});
|
|
32
37
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
await nexus.sendMessage({
|
|
41
|
-
to: messageData.from,
|
|
42
|
-
message: responses[command] || `Unknown command: ${command}`
|
|
43
|
-
});
|
|
44
|
-
}
|
|
38
|
+
// Custom endpoint example
|
|
39
|
+
app.get('/status', (req, res) => {
|
|
40
|
+
res.json({
|
|
41
|
+
connected: nexus.isConnected(),
|
|
42
|
+
provider: 'twilio',
|
|
43
|
+
mongodb: nexus.mongodb?.readyState === 1,
|
|
44
|
+
airtable: !!nexus.airtable
|
|
45
45
|
});
|
|
46
|
+
});
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
// Example using Airtable
|
|
49
|
+
app.get('/airtable-test', async (req, res) => {
|
|
50
|
+
try {
|
|
51
|
+
const calendarBase = nexus.getAirtableBase('calendar');
|
|
52
|
+
// Use calendarBase to interact with Airtable
|
|
53
|
+
res.json({ success: true, message: 'Airtable connection ready' });
|
|
54
|
+
} catch (error) {
|
|
55
|
+
res.status(500).json({ error: error.message });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
52
58
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
console.
|
|
60
|
-
|
|
61
|
-
}
|
|
59
|
+
const PORT = process.env.PORT || 3000;
|
|
60
|
+
app.listen(PORT, () => {
|
|
61
|
+
console.log(`Server running on port ${PORT}`);
|
|
62
|
+
console.log('Available endpoints:');
|
|
63
|
+
console.log('- POST /webhook - Webhook for incoming messages');
|
|
64
|
+
console.log('- GET /status - Connection status');
|
|
65
|
+
console.log('- GET /airtable-test - Test Airtable connection');
|
|
66
|
+
console.log('- All Nexus API routes under /api/*');
|
|
67
|
+
});
|
|
62
68
|
}
|
|
63
69
|
|
|
64
|
-
|
|
65
|
-
process.on('SIGINT', async () => {
|
|
66
|
-
console.log('\nShutting down gracefully...');
|
|
67
|
-
process.exit(0);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
main().catch(console.error);
|
|
70
|
+
startServer().catch(console.error);
|
|
@@ -13,16 +13,19 @@ let assistants = {};
|
|
|
13
13
|
|
|
14
14
|
async function initializeNexus() {
|
|
15
15
|
await nexus.initialize({
|
|
16
|
-
|
|
16
|
+
// MongoDB connection
|
|
17
|
+
mongoUri: process.env.MONGODB_URI,
|
|
18
|
+
|
|
19
|
+
// Messaging provider
|
|
20
|
+
provider: process.env.MESSAGING_PROVIDER || 'twilio',
|
|
17
21
|
providerConfig: {
|
|
18
22
|
accountSid: process.env.TWILIO_ACCOUNT_SID,
|
|
19
23
|
authToken: process.env.TWILIO_AUTH_TOKEN,
|
|
20
24
|
phoneNumber: process.env.TWILIO_PHONE_NUMBER
|
|
21
25
|
},
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
},
|
|
26
|
+
|
|
27
|
+
// LLM provider
|
|
28
|
+
llm: 'openai',
|
|
26
29
|
llmConfig: {
|
|
27
30
|
apiKey: process.env.OPENAI_API_KEY
|
|
28
31
|
}
|
|
@@ -31,14 +34,12 @@ async function initializeNexus() {
|
|
|
31
34
|
assistants = {
|
|
32
35
|
sales: new ExampleAssistant({
|
|
33
36
|
assistantId: process.env.SALES_ASSISTANT_ID,
|
|
34
|
-
llmClient: nexus.getLLMProvider()
|
|
35
|
-
storage: nexus.getStorage(),
|
|
37
|
+
llmClient: nexus.getLLMProvider(),
|
|
36
38
|
nexus: nexus
|
|
37
39
|
}),
|
|
38
40
|
support: new ExampleAssistant({
|
|
39
41
|
assistantId: process.env.SUPPORT_ASSISTANT_ID,
|
|
40
|
-
llmClient: nexus.getLLMProvider()
|
|
41
|
-
storage: nexus.getStorage(),
|
|
42
|
+
llmClient: nexus.getLLMProvider(),
|
|
42
43
|
nexus: nexus
|
|
43
44
|
})
|
|
44
45
|
};
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
const { TwilioProvider } = require('../adapters/TwilioProvider');
|
|
2
2
|
const { BaileysProvider } = require('../adapters/BaileysProvider');
|
|
3
|
+
const mongoose = require('mongoose');
|
|
4
|
+
const { airtable, getBase } = require('../config/airtableConfig');
|
|
5
|
+
const OpenAI = require('openai');
|
|
3
6
|
|
|
4
7
|
/**
|
|
5
8
|
* Core messaging class that manages providers and message handling
|
|
@@ -9,6 +12,9 @@ class NexusMessaging {
|
|
|
9
12
|
this.config = config;
|
|
10
13
|
this.provider = null;
|
|
11
14
|
this.messageStorage = null;
|
|
15
|
+
this.mongodb = null;
|
|
16
|
+
this.airtable = airtable;
|
|
17
|
+
this.llmProvider = null;
|
|
12
18
|
this.handlers = {
|
|
13
19
|
onMessage: null,
|
|
14
20
|
onInteractive: null,
|
|
@@ -19,6 +25,97 @@ class NexusMessaging {
|
|
|
19
25
|
};
|
|
20
26
|
}
|
|
21
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Initialize MongoDB connection
|
|
30
|
+
* @param {string} mongoUri - MongoDB connection string
|
|
31
|
+
*/
|
|
32
|
+
async initializeMongoDB(mongoUri = process.env.MONGODB_URI) {
|
|
33
|
+
if (!mongoUri) {
|
|
34
|
+
throw new Error('MongoDB URI not provided. Please set MONGODB_URI environment variable or pass mongoUri parameter.');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
await mongoose.connect(mongoUri);
|
|
39
|
+
this.mongodb = mongoose.connection;
|
|
40
|
+
console.log('MongoDB connected successfully');
|
|
41
|
+
} catch (error) {
|
|
42
|
+
console.error('MongoDB connection failed:', error);
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Get Airtable base instance
|
|
49
|
+
* @param {string} baseId - Airtable base ID
|
|
50
|
+
*/
|
|
51
|
+
getAirtableBase(baseId) {
|
|
52
|
+
return getBase(baseId);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Initialize LLM provider
|
|
57
|
+
* @param {string} llmType - LLM provider type ('openai')
|
|
58
|
+
* @param {Object} llmConfig - LLM configuration
|
|
59
|
+
*/
|
|
60
|
+
initializeLLM(llmType, llmConfig) {
|
|
61
|
+
switch (llmType.toLowerCase()) {
|
|
62
|
+
case 'openai':
|
|
63
|
+
if (!llmConfig.apiKey) {
|
|
64
|
+
throw new Error('OpenAI API key is required');
|
|
65
|
+
}
|
|
66
|
+
this.llmProvider = new OpenAI({
|
|
67
|
+
apiKey: llmConfig.apiKey
|
|
68
|
+
});
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
throw new Error(`Unsupported LLM provider: ${llmType}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Get LLM provider instance
|
|
77
|
+
*/
|
|
78
|
+
getLLMProvider() {
|
|
79
|
+
return this.llmProvider;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Initialize Nexus with all services
|
|
84
|
+
* @param {Object} options - Configuration options
|
|
85
|
+
*/
|
|
86
|
+
async initialize(options = {}) {
|
|
87
|
+
const {
|
|
88
|
+
provider,
|
|
89
|
+
providerConfig,
|
|
90
|
+
mongoUri,
|
|
91
|
+
messageStorage,
|
|
92
|
+
llm,
|
|
93
|
+
llmConfig
|
|
94
|
+
} = options;
|
|
95
|
+
|
|
96
|
+
// Initialize MongoDB if URI provided
|
|
97
|
+
if (mongoUri || process.env.MONGODB_URI) {
|
|
98
|
+
await this.initializeMongoDB(mongoUri);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Initialize messaging provider
|
|
102
|
+
if (provider && providerConfig) {
|
|
103
|
+
await this.initializeProvider(provider, providerConfig);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Initialize LLM provider
|
|
107
|
+
if (llm && llmConfig) {
|
|
108
|
+
this.initializeLLM(llm, llmConfig);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Set message storage
|
|
112
|
+
if (messageStorage) {
|
|
113
|
+
this.setMessageStorage(messageStorage);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
|
|
22
119
|
/**
|
|
23
120
|
* Initialize messaging with specified provider
|
|
24
121
|
* @param {string} providerType - 'twilio' or 'baileys'
|
|
@@ -54,9 +54,9 @@ class MongoStorage {
|
|
|
54
54
|
});
|
|
55
55
|
|
|
56
56
|
return {
|
|
57
|
-
Message: mongoose.model('Message', messageSchema),
|
|
58
|
-
Interaction: mongoose.model('Interaction', interactionSchema),
|
|
59
|
-
Thread: mongoose.model('Thread', threadSchema)
|
|
57
|
+
Message: mongoose.models.Message || mongoose.model('Message', messageSchema),
|
|
58
|
+
Interaction: mongoose.models.Interaction || mongoose.model('Interaction', interactionSchema),
|
|
59
|
+
Thread: mongoose.models.Thread || mongoose.model('Thread', threadSchema)
|
|
60
60
|
};
|
|
61
61
|
}
|
|
62
62
|
|