@reititin/client 0.0.5 → 0.0.7

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.
Files changed (2) hide show
  1. package/index.js +19 -13
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,45 +1,51 @@
1
1
  import http2 from 'http2';
2
2
  import axios from 'axios';
3
3
 
4
- const FINALIZE_URL = "https://reititin.com/api/finalizeMessage";
5
-
6
4
  export const ReititinClient = ({ agentId, onMessage }) => {
7
5
  const MESSAGE_ENDPOINT = `/api/agents/messages?agentId=${agentId}`;
6
+ const FINALIZE_URL = "https://test.reititin.com/api/finalizeMessage";
8
7
  let buffer = "";
9
8
 
10
9
  const connectSSE = () => {
11
10
  if (buffer) return;
11
+
12
12
  console.log("Waiting for messages.");
13
13
 
14
- const client = http2.connect('https://reititin.com');
14
+ const client = http2.connect('https://test.reititin.com');
15
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
- console.log("Received a message.")
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: ', '');
26
- if (onMessage) {
27
- const processedMessage = await onMessage(JSON.parse(rawData));
28
- await axios.post(FINALIZE_URL, { chat_id: chat_id, agent_id: agent_id, message: processedMessage });
29
- console.log(`Response sent.`);
21
+ try {
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: ', '');
26
+ if (onMessage && rawData && agent_id && chat_id) {
27
+ console.log("Message received. Processing.");
28
+ const processedMessage = await onMessage(JSON.parse(rawData));
29
+ await axios.post(FINALIZE_URL, { chat_id: chat_id, agent_id: agent_id, message: processedMessage });
30
+ console.log(`Response sent.`);
31
+ }
32
+ buffer = "";
33
+ } catch(err) {
34
+ console.log("Reconnecting.");
30
35
  }
31
- buffer = "";
32
36
  scheduleReconnect();
33
37
  });
34
38
 
35
39
  req.on('error', err => {
36
40
  console.error('Connection closed. Reconnecting.');
41
+ req.close();
37
42
  client.close();
38
43
  scheduleReconnect();
39
44
  });
40
45
 
41
46
  req.on('timeout', err => {
42
47
  console.error('Connection timedout. Reconnecting.');
48
+ req.close();
43
49
  client.close();
44
50
  scheduleReconnect();
45
51
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reititin/client",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Reititin client allows you to connect your LLM system to Reititin platform.",
5
5
  "keywords": [
6
6
  "AI",