@terryavg/neptune-ai-chatbot 1.0.1 → 1.0.2

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 (59) hide show
  1. package/README.md +137 -2
  2. package/dist/chat-input-42V4ESQB.mjs +478 -0
  3. package/dist/chat-input-4T42R7FR.mjs +481 -0
  4. package/dist/chat-input-BJVIIYOP.mjs +484 -0
  5. package/dist/chat-input-HT47ZLQN.mjs +456 -0
  6. package/dist/chat-input-JFGPZBBQ.mjs +495 -0
  7. package/dist/chat-input-JQ7PSHNW.mjs +515 -0
  8. package/dist/chat-input-USWVSAYY.mjs +461 -0
  9. package/dist/chat-input-VTCTV5ED.mjs +495 -0
  10. package/dist/chat-input-WJMGLQJI.mjs +495 -0
  11. package/dist/chat-message-2VQW74CO.mjs +2230 -0
  12. package/dist/chat-message-35ZWSSQV.mjs +1926 -0
  13. package/dist/chat-message-3CUUWTZG.mjs +2429 -0
  14. package/dist/chat-message-3NHSJNKL.mjs +2241 -0
  15. package/dist/chat-message-3Z7B2WYC.mjs +2228 -0
  16. package/dist/chat-message-564AHZHH.mjs +2186 -0
  17. package/dist/chat-message-5QLTW4K7.mjs +2122 -0
  18. package/dist/chat-message-7EH2TDZG.mjs +1910 -0
  19. package/dist/chat-message-7QHJRHQG.mjs +1909 -0
  20. package/dist/chat-message-AAXNH5TF.mjs +2088 -0
  21. package/dist/chat-message-D36YF274.mjs +1910 -0
  22. package/dist/chat-message-DVCXEL4Z.mjs +2111 -0
  23. package/dist/chat-message-E6KB3AST.mjs +1906 -0
  24. package/dist/chat-message-G2NWPAXK.mjs +1904 -0
  25. package/dist/chat-message-H62Z3DW5.mjs +2368 -0
  26. package/dist/chat-message-HIEZ7B5R.mjs +2190 -0
  27. package/dist/chat-message-I6AKFPK6.mjs +2156 -0
  28. package/dist/chat-message-IKYSAVAB.mjs +1918 -0
  29. package/dist/chat-message-IZL6JHV2.mjs +2429 -0
  30. package/dist/chat-message-J7PVUQO6.mjs +1878 -0
  31. package/dist/chat-message-K6QILTW5.mjs +1897 -0
  32. package/dist/chat-message-MDORLI2R.mjs +2228 -0
  33. package/dist/chat-message-MFUY6KOE.mjs +1910 -0
  34. package/dist/chat-message-MYKOR5OF.mjs +1890 -0
  35. package/dist/chat-message-NKMTAMGG.mjs +1906 -0
  36. package/dist/chat-message-NNGD3FUH.mjs +2168 -0
  37. package/dist/chat-message-QAPRO542.mjs +1915 -0
  38. package/dist/chat-message-R4IJ3MXU.mjs +2146 -0
  39. package/dist/chat-message-U7UCBLHI.mjs +2145 -0
  40. package/dist/chat-message-UKXGFKUR.mjs +1903 -0
  41. package/dist/chat-message-UNIBCF3T.mjs +1900 -0
  42. package/dist/chat-message-UXLPL76T.mjs +1890 -0
  43. package/dist/chat-message-VB54UOHB.mjs +2306 -0
  44. package/dist/chat-message-WUNUZLKI.mjs +2228 -0
  45. package/dist/chat-message-WYN5UZRD.mjs +1927 -0
  46. package/dist/chat-message-Y5OJSR2O.mjs +2228 -0
  47. package/dist/chat-message-YPDHL6WW.mjs +2114 -0
  48. package/dist/chunk-26656QB3.mjs +406 -0
  49. package/dist/chunk-2RXQ2EZ2.mjs +295 -0
  50. package/dist/chunk-DVWFDUN4.mjs +407 -0
  51. package/dist/chunk-JLRHY3SB.mjs +405 -0
  52. package/dist/chunk-XFSEOBLD.mjs +432 -0
  53. package/dist/index.css +174 -43
  54. package/dist/index.d.mts +218 -1
  55. package/dist/index.d.ts +218 -1
  56. package/dist/index.js +2088 -1400
  57. package/dist/index.mjs +606 -413
  58. package/dist/styles.css +1 -1
  59. package/package.json +2 -1
@@ -0,0 +1,456 @@
1
+ import {
2
+ chatClient
3
+ } from "./chunk-5VL3YPMQ.mjs";
4
+ import "./chunk-FWCSY2DS.mjs";
5
+
6
+ // app/components/chat-input.tsx
7
+ import {
8
+ useState,
9
+ useRef,
10
+ useEffect
11
+ } from "react";
12
+ import { Send, Paperclip, X, FileText } from "lucide-react";
13
+ import { jsx, jsxs } from "react/jsx-runtime";
14
+ function ChatInput({
15
+ conversationId,
16
+ agentId,
17
+ debug = false,
18
+ onAddUserMessage,
19
+ onStreamStart,
20
+ onStreamUpdate,
21
+ onStreamEnd,
22
+ onError,
23
+ messages,
24
+ isStreaming,
25
+ disabled = false,
26
+ onThreadCreated,
27
+ onToolExecutionStart,
28
+ onToolExecutionEnd,
29
+ accentColor = "#8B7FD9"
30
+ }) {
31
+ const [input, setInput] = useState("");
32
+ const [isSubmitting, setIsSubmitting] = useState(false);
33
+ const [attachment, setAttachment] = useState(null);
34
+ const textareaRef = useRef(null);
35
+ const fileInputRef = useRef(null);
36
+ const abortControllerRef = useRef(null);
37
+ const isImageAttachment = (attachment == null ? void 0 : attachment.type) === "image";
38
+ useEffect(() => {
39
+ if (textareaRef.current) {
40
+ textareaRef.current.style.height = "inherit";
41
+ textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
42
+ }
43
+ }, [input]);
44
+ const handleFileChange = async (e) => {
45
+ var _a;
46
+ const file = (_a = e.target.files) == null ? void 0 : _a[0];
47
+ if (!file) return;
48
+ const isImage = file.type.startsWith("image/");
49
+ const isPdf = file.type === "application/pdf";
50
+ if (!isImage && !isPdf) {
51
+ onError({
52
+ title: "Unsupported File Type",
53
+ message: "Only images (PNG, JPG, GIF, etc.) and PDF files are supported at this time."
54
+ });
55
+ return;
56
+ }
57
+ if (file.size > 10 * 1024 * 1024) {
58
+ onError({
59
+ title: "File Too Large",
60
+ message: "The selected file exceeds the 10MB size limit. Please choose a smaller file."
61
+ });
62
+ return;
63
+ }
64
+ try {
65
+ const base64data = await fileToBase64(file);
66
+ if (isImage) {
67
+ setAttachment({
68
+ name: file.name,
69
+ type: "image",
70
+ mediaType: file.type,
71
+ data: base64data,
72
+ image: base64data
73
+ });
74
+ } else {
75
+ setAttachment({
76
+ name: file.name,
77
+ type: "file",
78
+ mediaType: file.type,
79
+ data: base64data,
80
+ filename: file.name
81
+ });
82
+ }
83
+ } catch (error) {
84
+ console.error("File processing error:", error);
85
+ onError({
86
+ title: "File Processing Error",
87
+ message: "There was an unexpected error processing your file. Please try again."
88
+ });
89
+ }
90
+ };
91
+ const handlePaste = async (e) => {
92
+ var _a;
93
+ const clipboardItems = (_a = e.clipboardData) == null ? void 0 : _a.items;
94
+ if (!clipboardItems) return;
95
+ for (let i = 0; i < clipboardItems.length; i++) {
96
+ const item = clipboardItems[i];
97
+ const trimmedItemType = item.type.trim();
98
+ if (trimmedItemType.includes("image")) {
99
+ e.preventDefault();
100
+ const file = item.getAsFile();
101
+ if (!file) continue;
102
+ if (file.size > 5 * 1024 * 1024) {
103
+ onError({
104
+ title: "Image Too Large",
105
+ message: "The pasted image exceeds the 5MB size limit. Please paste a smaller image."
106
+ });
107
+ return;
108
+ }
109
+ try {
110
+ const base64data = await fileToBase64(file);
111
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
112
+ const filename = `pasted-image-${timestamp}.${trimmedItemType.split("/")[1] || "png"}`;
113
+ setAttachment({
114
+ name: filename,
115
+ type: "image",
116
+ mediaType: trimmedItemType,
117
+ data: base64data,
118
+ image: base64data
119
+ });
120
+ return;
121
+ } catch (error) {
122
+ console.error("Error processing pasted image:", error);
123
+ onError({
124
+ title: "Image Processing Error",
125
+ message: "There was an unexpected error processing the pasted image. Please try again."
126
+ });
127
+ }
128
+ }
129
+ }
130
+ };
131
+ const fileToBase64 = (file) => {
132
+ return new Promise((resolve, reject) => {
133
+ const reader = new FileReader();
134
+ reader.readAsDataURL(file);
135
+ reader.onload = () => {
136
+ if (typeof reader.result === "string") {
137
+ resolve(reader.result);
138
+ } else {
139
+ reject(new Error("Failed to convert file to base64"));
140
+ }
141
+ };
142
+ reader.onerror = (error) => reject(error);
143
+ });
144
+ };
145
+ const clearAttachment = () => {
146
+ setAttachment(null);
147
+ if (fileInputRef.current) {
148
+ fileInputRef.current.value = "";
149
+ }
150
+ };
151
+ const handleAttachmentClick = () => {
152
+ var _a;
153
+ (_a = fileInputRef.current) == null ? void 0 : _a.click();
154
+ };
155
+ const handleSubmit = async (e) => {
156
+ e.preventDefault();
157
+ if (!input.trim() && !attachment || isSubmitting || disabled) return;
158
+ if (!agentId) {
159
+ onError({
160
+ title: "Missing Configuration",
161
+ message: "Assistant ID or Agent ID is not set. Please ensure it is provided in the URL (e.g., ?assistantId=your-id or ?agentId=your-id)."
162
+ });
163
+ setIsSubmitting(false);
164
+ return;
165
+ }
166
+ let accumulatedResponse = "";
167
+ let errorOccurred = false;
168
+ let finalErrorMessage = "";
169
+ let metadata = null;
170
+ try {
171
+ setIsSubmitting(true);
172
+ const abortController = new AbortController();
173
+ abortControllerRef.current = abortController;
174
+ const userMessageText = input.trim();
175
+ let messageContentParts = [];
176
+ if (userMessageText) {
177
+ messageContentParts.push({
178
+ type: "text",
179
+ text: userMessageText
180
+ });
181
+ }
182
+ if (attachment) {
183
+ if (attachment.type === "image") {
184
+ messageContentParts.push({
185
+ type: "image",
186
+ image: attachment.image || attachment.data,
187
+ // Fallback to data if image not set
188
+ mediaType: attachment.mediaType
189
+ });
190
+ } else {
191
+ messageContentParts.push({
192
+ type: "file",
193
+ data: attachment.data,
194
+ mediaType: attachment.mediaType,
195
+ filename: attachment.filename || attachment.name
196
+ });
197
+ }
198
+ }
199
+ const finalMessageContent = messageContentParts.length === 1 && messageContentParts[0].type === "text" && !attachment ? messageContentParts[0].text : messageContentParts;
200
+ setInput("");
201
+ clearAttachment();
202
+ onAddUserMessage({
203
+ id: `local-user-${Date.now()}`,
204
+ content: finalMessageContent,
205
+ role: "user",
206
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
207
+ });
208
+ onStreamStart();
209
+ const { stream, threadId } = await chatClient.messages.sendMessage(
210
+ agentId,
211
+ conversationId,
212
+ finalMessageContent,
213
+ messages,
214
+ abortController.signal,
215
+ debug,
216
+ void 0,
217
+ // stepData
218
+ conversationId.startsWith("temp-")
219
+ // isTemporary - detect by ID prefix
220
+ );
221
+ if (threadId && threadId !== conversationId && onThreadCreated) {
222
+ console.log(`Conversation created with ID: ${threadId}`);
223
+ onThreadCreated(conversationId, threadId);
224
+ }
225
+ const reader = stream.getReader();
226
+ const decoder = new TextDecoder();
227
+ let done = false;
228
+ let buffer = "";
229
+ let isToolExecuting = false;
230
+ let currentToolName = "";
231
+ while (!done) {
232
+ try {
233
+ if (abortController.signal.aborted) {
234
+ console.log("Request was aborted by user");
235
+ break;
236
+ }
237
+ const { value, done: readerDone } = await reader.read();
238
+ done = readerDone;
239
+ if (value) {
240
+ buffer += decoder.decode(value, { stream: true });
241
+ const lines = buffer.split("\n");
242
+ buffer = lines.pop() || "";
243
+ for (const line of lines) {
244
+ if (!line.trim()) continue;
245
+ if (line.startsWith("data: ")) {
246
+ const jsonString = line.substring(6);
247
+ if (jsonString === "[DONE]") {
248
+ done = true;
249
+ break;
250
+ }
251
+ try {
252
+ const event = JSON.parse(jsonString);
253
+ switch (event.type) {
254
+ case "start-step":
255
+ break;
256
+ case "tool-input-start":
257
+ currentToolName = event.toolName || "Unknown Tool";
258
+ isToolExecuting = true;
259
+ if (onToolExecutionStart) {
260
+ onToolExecutionStart(
261
+ currentToolName
262
+ );
263
+ }
264
+ break;
265
+ case "tool-input-delta":
266
+ break;
267
+ case "tool-input-available":
268
+ break;
269
+ case "tool-output-available":
270
+ if (isToolExecuting && onToolExecutionEnd) {
271
+ onToolExecutionEnd();
272
+ }
273
+ isToolExecuting = false;
274
+ currentToolName = "";
275
+ break;
276
+ case "finish-step":
277
+ if (isToolExecuting && onToolExecutionEnd) {
278
+ onToolExecutionEnd();
279
+ }
280
+ isToolExecuting = false;
281
+ currentToolName = "";
282
+ break;
283
+ case "text-delta":
284
+ accumulatedResponse += event.delta;
285
+ onStreamUpdate(event.delta);
286
+ break;
287
+ case "data-finish-result":
288
+ metadata = event.data;
289
+ break;
290
+ case "error":
291
+ throw new Error(
292
+ event.errorText || "Stream error occurred"
293
+ );
294
+ // Ignore other event types
295
+ default:
296
+ break;
297
+ }
298
+ } catch (parseError) {
299
+ console.error(
300
+ "Error parsing SSE event:",
301
+ parseError
302
+ );
303
+ }
304
+ }
305
+ }
306
+ }
307
+ } catch (streamReadError) {
308
+ console.error(
309
+ "Error reading stream chunk:",
310
+ streamReadError
311
+ );
312
+ errorOccurred = true;
313
+ finalErrorMessage = "An error occurred while reading the response.";
314
+ onError({
315
+ title: "Stream Error",
316
+ message: finalErrorMessage
317
+ });
318
+ done = true;
319
+ }
320
+ }
321
+ if ((metadata == null ? void 0 : metadata.threadID) && metadata.threadID !== conversationId && onThreadCreated) {
322
+ onThreadCreated(conversationId, metadata.threadID);
323
+ }
324
+ } catch (error) {
325
+ console.error("Error during chat submission:", error);
326
+ if ((error == null ? void 0 : error.name) === "AbortError") {
327
+ return;
328
+ }
329
+ errorOccurred = true;
330
+ let title = "Request Error";
331
+ let localMessage = "An unexpected error occurred. Please try again.";
332
+ if (error == null ? void 0 : error.isConfigurationError) {
333
+ title = "Configuration Error";
334
+ localMessage = error.message || "Assistant ID is missing or invalid.";
335
+ } else if (error == null ? void 0 : error.isApiError) {
336
+ title = `API Error (${error.status})`;
337
+ localMessage = error.message || error.statusText || "An error occurred processing your request.";
338
+ } else if (error == null ? void 0 : error.isNetworkError) {
339
+ title = "Network Error";
340
+ localMessage = error.message || "Failed to connect to the server.";
341
+ } else if (error instanceof Error) {
342
+ localMessage = error.message;
343
+ }
344
+ finalErrorMessage = localMessage;
345
+ onError({ title, message: finalErrorMessage });
346
+ } finally {
347
+ onStreamEnd(
348
+ errorOccurred ? finalErrorMessage : accumulatedResponse,
349
+ metadata
350
+ );
351
+ setIsSubmitting(false);
352
+ abortControllerRef.current = null;
353
+ }
354
+ };
355
+ useEffect(() => {
356
+ if (!isStreaming && abortControllerRef.current) {
357
+ console.log("Aborting request due to streaming stopped");
358
+ abortControllerRef.current.abort();
359
+ abortControllerRef.current = null;
360
+ }
361
+ }, [isStreaming]);
362
+ const handleKeyDown = (e) => {
363
+ if (e.key === "Enter" && !e.shiftKey) {
364
+ e.preventDefault();
365
+ handleSubmit(e);
366
+ }
367
+ };
368
+ return /* @__PURE__ */ jsxs(
369
+ "form",
370
+ {
371
+ onSubmit: handleSubmit,
372
+ className: "chat-input relative px-2 pt-3 pb-3 flex w-full items-center border bg-white dark:bg-gray-100 rounded-2xl shadow-sm overflow-hidden",
373
+ style: { borderColor: accentColor },
374
+ children: [
375
+ attachment && /* @__PURE__ */ jsxs("div", { className: "absolute top-0 left-0 right-0 bg-gray-100 dark:bg-gray-700 p-2 flex items-center justify-between", children: [
376
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center space-x-2 truncate max-w-[90%]", children: [
377
+ isImageAttachment ? /* @__PURE__ */ jsx("div", { className: "w-6 h-6 rounded overflow-hidden bg-white", children: /* @__PURE__ */ jsx(
378
+ "img",
379
+ {
380
+ src: attachment.image || attachment.data,
381
+ alt: "Preview",
382
+ className: "w-full h-full object-cover"
383
+ }
384
+ ) }) : /* @__PURE__ */ jsx(
385
+ FileText,
386
+ {
387
+ size: 16,
388
+ className: "text-gray-600 dark:text-gray-300"
389
+ }
390
+ ),
391
+ /* @__PURE__ */ jsx("span", { className: "text-xs truncate", children: attachment.name })
392
+ ] }),
393
+ /* @__PURE__ */ jsx(
394
+ "button",
395
+ {
396
+ type: "button",
397
+ onClick: clearAttachment,
398
+ className: "text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200",
399
+ children: /* @__PURE__ */ jsx(X, { size: 16 })
400
+ }
401
+ )
402
+ ] }),
403
+ /* @__PURE__ */ jsx(
404
+ "textarea",
405
+ {
406
+ ref: textareaRef,
407
+ value: input,
408
+ onChange: (e) => setInput(e.target.value),
409
+ onKeyDown: handleKeyDown,
410
+ onPaste: handlePaste,
411
+ placeholder: disabled ? "Please complete the form above..." : "Type a message...",
412
+ className: `max-h-32 min-h-8 w-full resize-none bg-transparent py-2 pl-6 pr-16 focus:outline-none text-base ${attachment ? "mt-8" : ""} ${disabled ? "opacity-50 cursor-not-allowed" : ""}`,
413
+ rows: 1,
414
+ disabled: isSubmitting || disabled
415
+ }
416
+ ),
417
+ /* @__PURE__ */ jsx(
418
+ "input",
419
+ {
420
+ type: "file",
421
+ ref: fileInputRef,
422
+ onChange: handleFileChange,
423
+ accept: "application/pdf,image/*",
424
+ className: "hidden"
425
+ }
426
+ ),
427
+ /* @__PURE__ */ jsx(
428
+ "button",
429
+ {
430
+ type: "button",
431
+ onClick: handleAttachmentClick,
432
+ disabled: isSubmitting || isStreaming || disabled,
433
+ className: "p-2 absolute bottom-3 right-12 rounded-full enabled:hover:bg-gray-300 enabled:dark:hover:bg-gray-700 disabled:opacity-40",
434
+ style: { color: accentColor },
435
+ "aria-label": "Attach file or image",
436
+ children: /* @__PURE__ */ jsx(Paperclip, { size: 20 })
437
+ }
438
+ ),
439
+ /* @__PURE__ */ jsx(
440
+ "button",
441
+ {
442
+ type: "submit",
443
+ disabled: !input.trim() && !attachment || isSubmitting || disabled,
444
+ className: "ml-2 p-2 absolute bottom-3 right-3 rounded-full enabled:hover:bg-gray-300 enabled:dark:hover:bg-gray-700 disabled:opacity-40",
445
+ style: { color: accentColor },
446
+ "aria-label": "Send message",
447
+ children: /* @__PURE__ */ jsx(Send, { size: 20 })
448
+ }
449
+ )
450
+ ]
451
+ }
452
+ );
453
+ }
454
+ export {
455
+ ChatInput as default
456
+ };