ragged-chat-sdk 1.0.10 → 1.0.12

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 +28 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -39,6 +39,11 @@ export function init(config = {}) {
39
39
  async function fetchConfig() {
40
40
  try {
41
41
  const response = await fetch(`${API_URL}/chat/${subdomain}/config`);
42
+ if (!response.ok) {
43
+ const errorData = await response.json().catch(() => ({}));
44
+ console.error('[Ragged SDK] Config Error:', response.status, errorData.error || response.statusText);
45
+ return;
46
+ }
42
47
  chatConfig = await response.json();
43
48
  } catch (error) {
44
49
  console.error('[Ragged SDK] Failed to fetch config:', error);
@@ -48,19 +53,38 @@ export function init(config = {}) {
48
53
  // Send message to chatbot
49
54
  async function sendMessage(message) {
50
55
  try {
56
+ // Only send the last 20 messages as history to avoid payload size and validation limits
57
+ const historyToSend = messages.slice(-20);
58
+
51
59
  const response = await fetch(`${API_URL}/chat/${subdomain}`, {
52
60
  method: 'POST',
53
61
  headers: { 'Content-Type': 'application/json' },
54
- body: JSON.stringify({ message, history: messages })
62
+ body: JSON.stringify({ message, history: historyToSend })
55
63
  });
56
- const data = await response.json();
64
+
65
+ const data = await response.json().catch(() => ({ error: 'Unknown Server Error' }));
66
+
67
+ if (!response.ok) {
68
+ console.error('[Ragged SDK] Chat Error:', response.status, data.error || data.message || response.statusText);
69
+
70
+ // Handle validation errors specifically to give better feedback
71
+ if (data.errors && Array.isArray(data.errors)) {
72
+ const validationMsg = data.errors.map(err => `${err.path}: ${err.message}`).join(', ');
73
+ return `Validation Error: ${validationMsg}`;
74
+ }
75
+
76
+ return data.details || data.error || data.message || "Sorry, I'm having trouble processing that right now. Please try again later.";
77
+ }
78
+
57
79
  return data.response;
58
80
  } catch (error) {
59
- console.error('[Ragged SDK] Failed to send message:', error);
60
- return "Sorry, I'm having trouble connecting right now.";
81
+ console.error('[Ragged SDK] Network Error:', error);
82
+ return "Sorry, I'm having trouble connecting to the server. Please check your internet connection.";
61
83
  }
62
84
  }
63
85
 
86
+
87
+
64
88
  // Switch between tabs
65
89
  function switchTab(tabId) {
66
90
  activeTab = tabId;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ragged-chat-sdk",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "Official SDK for integrating Ragged Chatbots into your website.",
5
5
  "type": "module",
6
6
  "main": "index.js",