@cmnd-ai/chatbot-react 1.7.0 → 1.7.1

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.
@@ -3,79 +3,86 @@ const processStream = async (reader, onData) => {
3
3
  if (!onData)
4
4
  return;
5
5
  let fullAssistantMessage = "";
6
- let fullMessageContext = "";
6
+ let buffer = ""; // buffer to store incomplete JSON string fragments
7
7
  try {
8
8
  while (true) {
9
9
  const { value, done } = await reader.read();
10
10
  if (done) {
11
11
  break;
12
12
  }
13
- // Process the incoming chunk of data
14
- const chunk = value; // Assuming `value` is a string
15
- // Split the chunk by lines
13
+ const chunk = typeof value === "string" ? value : new TextDecoder().decode(value);
16
14
  const lines = chunk.split("\n");
17
15
  for (const line of lines) {
18
- let dataString = line;
19
- if (line === "") {
20
- // skip empty line of content
16
+ if (line.trim() === "")
21
17
  continue;
22
- }
23
- if (line.startsWith("data: ")) {
24
- dataString = line.slice(6);
25
- }
18
+ const dataString = line.startsWith("data: ") ? line.slice(6) : line;
26
19
  if (dataString === "[DONE]") {
27
20
  onData({
28
21
  completionFinished: true,
29
22
  finalResponseWithUsageData: false,
30
23
  });
24
+ continue;
31
25
  }
32
- else {
33
- try {
34
- const dataObject = JSON.parse(fullMessageContext + dataString);
35
- if (dataObject.error) {
36
- throw new StreamError({
37
- error: dataObject.error,
38
- path: dataObject.path,
39
- message: dataObject.error,
40
- });
41
- }
42
- if (dataObject.content) {
43
- fullAssistantMessage += dataObject.content;
44
- onData({
45
- completionFinished: false,
46
- finalResponseWithUsageData: false,
47
- message: fullAssistantMessage,
48
- });
49
- }
50
- else {
51
- //at this point, the dataObject does not have a content propery
52
- //and it is completed
53
- //get the last message from the dataObject to check if it is a function call
54
- const { messages, completionFinished, finalResponseWithUsageData, chatbotConversationId, conversationId, totalTokens, totalCost, } = dataObject;
55
- onData({
56
- messages,
57
- completionFinished,
58
- finalResponseWithUsageData,
59
- conversationId,
60
- chatbotConversationId,
61
- totalTokens,
62
- totalCost,
63
- });
64
- }
26
+ // Accumulate and try parsing
27
+ buffer += dataString;
28
+ try {
29
+ const dataObject = JSON.parse(buffer);
30
+ buffer = ""; // Clear the buffer after successful parse
31
+ if (dataObject.error) {
32
+ throw new StreamError({
33
+ error: dataObject.error,
34
+ path: dataObject.path,
35
+ message: dataObject.error,
36
+ });
37
+ }
38
+ if (dataObject.content) {
39
+ fullAssistantMessage += dataObject.content;
40
+ onData({
41
+ completionFinished: false,
42
+ finalResponseWithUsageData: false,
43
+ message: fullAssistantMessage,
44
+ });
65
45
  }
66
- catch (error) {
67
- console.error("Error processing stream", error);
46
+ else if (dataObject.function_call) {
47
+ // console.log("no processing necessary");
48
+ }
49
+ else {
50
+ //at this point, the dataObject does not have a content propery
51
+ //and it is completed
52
+ //get the last message from the dataObject to check if it is a function call
53
+ const { messages, completionFinished, finalResponseWithUsageData, conversationId, chatbotConversationId, totalTokens, totalCost, } = dataObject;
68
54
  onData({
69
- completionFinished: true,
70
- message: error instanceof StreamError
71
- ? error.message
72
- : "Oops! I ran into a problem.",
73
- finalResponseWithUsageData: true,
55
+ messages,
56
+ completionFinished,
57
+ finalResponseWithUsageData,
58
+ conversationId,
59
+ chatbotConversationId,
60
+ totalTokens,
61
+ totalCost,
74
62
  });
75
63
  }
76
64
  }
65
+ catch (error) {
66
+ // Only handle as error if it's NOT a syntax error
67
+ if (error instanceof SyntaxError) {
68
+ // Wait for more data
69
+ continue;
70
+ }
71
+ console.error("StreamError caught", error);
72
+ onData({
73
+ completionFinished: true,
74
+ message: error instanceof StreamError
75
+ ? error.message
76
+ : "Oops! I ran into a problem.",
77
+ finalResponseWithUsageData: true,
78
+ });
79
+ }
77
80
  }
78
81
  }
82
+ // Final check for any leftover buffered content
83
+ if (buffer.trim()) {
84
+ console.warn("Leftover data in buffer at stream end:", buffer);
85
+ }
79
86
  }
80
87
  catch (error) {
81
88
  console.error("Error processing stream", error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmnd-ai/chatbot-react",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "main": "dist/index.js",
5
5
  "description": "",
6
6
  "type": "module",