@origonai/web-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.
package/src/http.js CHANGED
@@ -3,7 +3,7 @@
3
3
  * Handles all HTTP requests without depending on external state
4
4
  */
5
5
 
6
- import { getCredentials, getExternalId } from './chat.js'
6
+ import { getCredentials, getExternalId, getConfigData } from './chat.js'
7
7
  import { MESSAGE_ROLES } from './constants.js'
8
8
 
9
9
  const AUTHENTICATION_ERROR = 'Something went wrong initializing the chat'
@@ -86,6 +86,57 @@ export async function getSession(sessionId) {
86
86
  return { control, messages }
87
87
  }
88
88
 
89
+ /**
90
+ * Validate a file against attachment config rules
91
+ * @param {File} file
92
+ * @returns {string|null} Error message if validation fails, null if valid
93
+ */
94
+ function validateAttachment(file) {
95
+ const configData = getConfigData()
96
+ const attachmentsConfig = configData?.attachments
97
+
98
+ if (!attachmentsConfig) {
99
+ return 'Attachments are not enabled'
100
+ }
101
+
102
+ const fileName = file.name || ''
103
+ const fileExtension = fileName.split('.').pop()?.toLowerCase()
104
+
105
+ if (!fileExtension) {
106
+ return 'File type is not supported'
107
+ }
108
+
109
+ // Find the category that supports this file extension
110
+ const categories = ['images', 'documents', 'audio', 'videos']
111
+ let matchedCategory = null
112
+ let matchedConfig = null
113
+
114
+ for (const category of categories) {
115
+ const categoryConfig = attachmentsConfig[category]
116
+ if (categoryConfig?.supportedFileTypes?.includes(fileExtension)) {
117
+ matchedCategory = category
118
+ matchedConfig = categoryConfig
119
+ break
120
+ }
121
+ }
122
+
123
+ if (!matchedCategory) {
124
+ return `File type ".${fileExtension}" is not supported`
125
+ }
126
+
127
+ if (!matchedConfig.enabled) {
128
+ return `${matchedCategory.charAt(0).toUpperCase() + matchedCategory.slice(1)} attachments are not supported`
129
+ }
130
+
131
+ // maxSize is in MB
132
+ const fileSizeMB = file.size / (1024 * 1024)
133
+ if (matchedConfig.maxSize && fileSizeMB > matchedConfig.maxSize) {
134
+ return `${fileName} size exceeds the maximum allowed size of ${matchedConfig.maxSize}MB`
135
+ }
136
+
137
+ return null
138
+ }
139
+
89
140
  /**
90
141
  * Upload attachment with progress tracking
91
142
  * @param {File} file
@@ -105,6 +156,17 @@ export function uploadAttachment(file, onProgress, onComplete) {
105
156
  return null
106
157
  }
107
158
 
159
+ // Validate attachment against config (skip if authenticated via token)
160
+ if (!token) {
161
+ const validationError = validateAttachment(file)
162
+ if (validationError) {
163
+ if (onComplete) {
164
+ onComplete(new Error(validationError), null)
165
+ }
166
+ return null
167
+ }
168
+ }
169
+
108
170
  const xhr = new XMLHttpRequest()
109
171
  const formData = new FormData()
110
172
  formData.append('file', file)
@@ -226,7 +288,15 @@ async function fetchRequest(pathname, method = 'GET', body = null) {
226
288
  throw new Error(INITIALIZATION_ERROR)
227
289
  }
228
290
 
229
- const url = `${endpoint}${pathname}`
291
+ const baseUrl = new URL(endpoint)
292
+ const [path, queryString] = pathname.split('?')
293
+ baseUrl.pathname = baseUrl.pathname.replace(/\/$/, '') + path
294
+ if (queryString) {
295
+ new URLSearchParams(queryString).forEach((value, key) => {
296
+ baseUrl.searchParams.append(key, value)
297
+ })
298
+ }
299
+ const url = baseUrl.toString()
230
300
 
231
301
  const headers = {
232
302
  'Content-Type': 'application/json'
package/src/utils.js CHANGED
@@ -65,7 +65,7 @@ export function getSseEndpoint(baseUrl) {
65
65
  let sseEndpoint
66
66
  try {
67
67
  const url = new URL(baseUrl)
68
- sseEndpoint = `https://${url.hostname}${url.pathname}/sse`
68
+ sseEndpoint = `https://${url.hostname}${url.pathname}/sse${url.search}`
69
69
  } catch {
70
70
  console.error('SSE Invalid base URL: ', baseUrl)
71
71
  }