@reititin/client 0.0.1 → 0.0.3
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/index.js +9 -9
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
1
|
import http2 from 'http2';
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
|
|
4
|
-
const FINALIZE_URL = "https://
|
|
4
|
+
const FINALIZE_URL = "https://reititin.com/api/finalizeMessage";
|
|
5
5
|
|
|
6
|
-
export const
|
|
7
|
-
const
|
|
6
|
+
export const ReititinClient = ({ agentId, onMessage }) => {
|
|
7
|
+
const MESSAGE_ENDPOINT = `/api/agents/messages?agentId=${agentId}`;
|
|
8
8
|
let buffer = "";
|
|
9
9
|
|
|
10
10
|
const connectSSE = () => {
|
|
11
11
|
if (buffer) return;
|
|
12
12
|
console.log("Waiting for messages.");
|
|
13
13
|
|
|
14
|
-
const client = http2.connect('https://
|
|
15
|
-
const req = client.request({ ':method': 'GET', ':path':
|
|
14
|
+
const client = http2.connect('https://reititin.com');
|
|
15
|
+
const req = client.request({ ':method': 'GET', ':path': MESSAGE_ENDPOINT, 'accept': 'text/event-stream' })
|
|
16
16
|
.setEncoding('utf8');
|
|
17
17
|
|
|
18
18
|
req.on('data', chunk => buffer += chunk);
|
|
19
19
|
|
|
20
20
|
req.on('end', async () => {
|
|
21
21
|
console.log("Received a message.")
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
const lines = buffer.split('\n').map(line => line.trim());
|
|
23
|
+
const chat_id = lines.find(line => line.startsWith('chat_id:'))?.split(': ')[1];
|
|
24
|
+
const agent_id = lines.find(line => line.startsWith('agent_id:'))?.split(': ')[1];
|
|
25
|
+
const rawData = lines.find(line => line.startsWith('data:'))?.replace('data: ', '');
|
|
25
26
|
if (onMessage) {
|
|
26
27
|
const processedMessage = await onMessage(rawData);
|
|
27
28
|
await axios.post(FINALIZE_URL, { chat_id: chat_id, agent_id: agent_id, message: processedMessage });
|
|
28
29
|
console.log(`Response sent.`);
|
|
29
30
|
}
|
|
30
|
-
|
|
31
31
|
buffer = "";
|
|
32
32
|
scheduleReconnect();
|
|
33
33
|
});
|