@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,495 @@
1
+ import {
2
+ chatClient
3
+ } from "./chunk-XFSEOBLD.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
+ streaming = true,
31
+ theme = "light",
32
+ inputBackgroundColor = "#ffffff",
33
+ inputBackgroundColorDark = "#303030"
34
+ }) {
35
+ const [input, setInput] = useState("");
36
+ const [isSubmitting, setIsSubmitting] = useState(false);
37
+ const [attachments, setAttachments] = useState([]);
38
+ const textareaRef = useRef(null);
39
+ const fileInputRef = useRef(null);
40
+ const abortControllerRef = useRef(null);
41
+ useEffect(() => {
42
+ if (textareaRef.current) {
43
+ textareaRef.current.style.height = "inherit";
44
+ textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`;
45
+ }
46
+ }, [input]);
47
+ const handleFileChange = async (e) => {
48
+ const files = e.target.files;
49
+ if (!files || files.length === 0) return;
50
+ const newAttachments = [];
51
+ for (let i = 0; i < files.length; i++) {
52
+ const file = files[i];
53
+ const isImage = file.type.startsWith("image/");
54
+ const isPdf = file.type === "application/pdf";
55
+ if (!isImage && !isPdf) {
56
+ onError({
57
+ title: "Unsupported File Type",
58
+ message: `File "${file.name}" is not supported. Only images (PNG, JPG, GIF, etc.) and PDF files are supported at this time.`
59
+ });
60
+ continue;
61
+ }
62
+ if (file.size > 10 * 1024 * 1024) {
63
+ onError({
64
+ title: "File Too Large",
65
+ message: `File "${file.name}" exceeds the 10MB size limit. Please choose a smaller file.`
66
+ });
67
+ continue;
68
+ }
69
+ try {
70
+ const base64data = await fileToBase64(file);
71
+ if (isImage) {
72
+ newAttachments.push({
73
+ name: file.name,
74
+ type: "image",
75
+ mediaType: file.type,
76
+ data: base64data,
77
+ image: base64data
78
+ });
79
+ } else {
80
+ newAttachments.push({
81
+ name: file.name,
82
+ type: "file",
83
+ mediaType: file.type,
84
+ data: base64data,
85
+ filename: file.name
86
+ });
87
+ }
88
+ } catch (error) {
89
+ console.error("File processing error:", error);
90
+ onError({
91
+ title: "File Processing Error",
92
+ message: `There was an unexpected error processing "${file.name}". Please try again.`
93
+ });
94
+ }
95
+ }
96
+ setAttachments((prev) => [...prev, ...newAttachments]);
97
+ };
98
+ const handlePaste = async (e) => {
99
+ var _a;
100
+ const clipboardItems = (_a = e.clipboardData) == null ? void 0 : _a.items;
101
+ if (!clipboardItems) return;
102
+ for (let i = 0; i < clipboardItems.length; i++) {
103
+ const item = clipboardItems[i];
104
+ const trimmedItemType = item.type.trim();
105
+ if (trimmedItemType.includes("image")) {
106
+ e.preventDefault();
107
+ const file = item.getAsFile();
108
+ if (!file) continue;
109
+ if (file.size > 5 * 1024 * 1024) {
110
+ onError({
111
+ title: "Image Too Large",
112
+ message: "The pasted image exceeds the 5MB size limit. Please paste a smaller image."
113
+ });
114
+ return;
115
+ }
116
+ try {
117
+ const base64data = await fileToBase64(file);
118
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-");
119
+ const filename = `pasted-image-${timestamp}.${trimmedItemType.split("/")[1] || "png"}`;
120
+ setAttachments((prev) => [...prev, {
121
+ name: filename,
122
+ type: "image",
123
+ mediaType: trimmedItemType,
124
+ data: base64data,
125
+ image: base64data
126
+ }]);
127
+ return;
128
+ } catch (error) {
129
+ console.error("Error processing pasted image:", error);
130
+ onError({
131
+ title: "Image Processing Error",
132
+ message: "There was an unexpected error processing the pasted image. Please try again."
133
+ });
134
+ }
135
+ }
136
+ }
137
+ };
138
+ const fileToBase64 = (file) => {
139
+ return new Promise((resolve, reject) => {
140
+ const reader = new FileReader();
141
+ reader.readAsDataURL(file);
142
+ reader.onload = () => {
143
+ if (typeof reader.result === "string") {
144
+ resolve(reader.result);
145
+ } else {
146
+ reject(new Error("Failed to convert file to base64"));
147
+ }
148
+ };
149
+ reader.onerror = (error) => reject(error);
150
+ });
151
+ };
152
+ const clearAttachments = () => {
153
+ setAttachments([]);
154
+ if (fileInputRef.current) {
155
+ fileInputRef.current.value = "";
156
+ }
157
+ };
158
+ const removeAttachment = (index) => {
159
+ setAttachments((prev) => prev.filter((_, i) => i !== index));
160
+ };
161
+ const handleAttachmentClick = () => {
162
+ var _a;
163
+ (_a = fileInputRef.current) == null ? void 0 : _a.click();
164
+ };
165
+ const handleSubmit = async (e) => {
166
+ e.preventDefault();
167
+ if (!input.trim() && attachments.length === 0 || isSubmitting || disabled) return;
168
+ if (!agentId) {
169
+ onError({
170
+ title: "Missing Configuration",
171
+ 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)."
172
+ });
173
+ setIsSubmitting(false);
174
+ return;
175
+ }
176
+ let accumulatedResponse = "";
177
+ let errorOccurred = false;
178
+ let finalErrorMessage = "";
179
+ let metadata = null;
180
+ try {
181
+ setIsSubmitting(true);
182
+ const abortController = new AbortController();
183
+ abortControllerRef.current = abortController;
184
+ const userMessageText = input.trim();
185
+ let messageContentParts = [];
186
+ if (userMessageText) {
187
+ messageContentParts.push({
188
+ type: "text",
189
+ text: userMessageText
190
+ });
191
+ }
192
+ attachments.forEach((attachment) => {
193
+ if (attachment.type === "image") {
194
+ messageContentParts.push({
195
+ type: "image",
196
+ image: attachment.image || attachment.data,
197
+ mediaType: attachment.mediaType
198
+ });
199
+ } else {
200
+ messageContentParts.push({
201
+ type: "file",
202
+ data: attachment.data,
203
+ mediaType: attachment.mediaType,
204
+ filename: attachment.filename || attachment.name
205
+ });
206
+ }
207
+ });
208
+ const finalMessageContent = messageContentParts.length === 1 && messageContentParts[0].type === "text" && attachments.length === 0 ? messageContentParts[0].text : messageContentParts;
209
+ setInput("");
210
+ clearAttachments();
211
+ onAddUserMessage({
212
+ id: `local-user-${Date.now()}`,
213
+ content: finalMessageContent,
214
+ role: "user",
215
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
216
+ });
217
+ onStreamStart();
218
+ const { stream, threadId } = await chatClient.messages.sendMessage(
219
+ agentId,
220
+ conversationId,
221
+ finalMessageContent,
222
+ messages,
223
+ abortController.signal,
224
+ debug,
225
+ void 0,
226
+ // stepData
227
+ conversationId.startsWith("temp-"),
228
+ // isTemporary - detect by ID prefix
229
+ streaming
230
+ // streaming mode
231
+ );
232
+ if (threadId && threadId !== conversationId && onThreadCreated) {
233
+ onThreadCreated(conversationId, threadId);
234
+ }
235
+ if (!streaming) {
236
+ const reader = stream.getReader();
237
+ const decoder = new TextDecoder();
238
+ let responseText = "";
239
+ while (true) {
240
+ const { done, value } = await reader.read();
241
+ if (done) break;
242
+ responseText += decoder.decode(value, { stream: true });
243
+ }
244
+ const response = JSON.parse(responseText);
245
+ const outputText = response.output || "";
246
+ accumulatedResponse = outputText;
247
+ onStreamUpdate(outputText);
248
+ metadata = response;
249
+ if (response.threadID && response.threadID !== conversationId && onThreadCreated) {
250
+ onThreadCreated(conversationId, response.threadID);
251
+ }
252
+ } else {
253
+ const reader = stream.getReader();
254
+ const decoder = new TextDecoder();
255
+ let done = false;
256
+ let buffer = "";
257
+ let isToolExecuting = false;
258
+ let currentToolName = "";
259
+ while (!done) {
260
+ try {
261
+ if (abortController.signal.aborted) {
262
+ break;
263
+ }
264
+ const { value, done: readerDone } = await reader.read();
265
+ done = readerDone;
266
+ if (value) {
267
+ buffer += decoder.decode(value, { stream: true });
268
+ const lines = buffer.split("\n");
269
+ buffer = lines.pop() || "";
270
+ for (const line of lines) {
271
+ if (!line.trim()) continue;
272
+ if (line.startsWith("data: ")) {
273
+ const jsonString = line.substring(6);
274
+ if (jsonString === "[DONE]") {
275
+ done = true;
276
+ break;
277
+ }
278
+ try {
279
+ const event = JSON.parse(jsonString);
280
+ switch (event.type) {
281
+ case "start-step":
282
+ break;
283
+ case "tool-input-start":
284
+ currentToolName = event.toolName || "Unknown Tool";
285
+ isToolExecuting = true;
286
+ if (onToolExecutionStart) {
287
+ onToolExecutionStart(
288
+ currentToolName
289
+ );
290
+ }
291
+ break;
292
+ case "tool-input-delta":
293
+ break;
294
+ case "tool-input-available":
295
+ break;
296
+ case "tool-output-available":
297
+ if (isToolExecuting && onToolExecutionEnd) {
298
+ onToolExecutionEnd();
299
+ }
300
+ isToolExecuting = false;
301
+ currentToolName = "";
302
+ break;
303
+ case "finish-step":
304
+ if (isToolExecuting && onToolExecutionEnd) {
305
+ onToolExecutionEnd();
306
+ }
307
+ isToolExecuting = false;
308
+ currentToolName = "";
309
+ break;
310
+ case "text-delta":
311
+ accumulatedResponse += event.delta;
312
+ onStreamUpdate(event.delta);
313
+ break;
314
+ case "data-finish-result":
315
+ metadata = event.data;
316
+ break;
317
+ case "error":
318
+ throw new Error(
319
+ event.errorText || "Stream error occurred"
320
+ );
321
+ // Ignore other event types
322
+ default:
323
+ break;
324
+ }
325
+ } catch (parseError) {
326
+ console.error(
327
+ "Error parsing SSE event:",
328
+ parseError
329
+ );
330
+ }
331
+ }
332
+ }
333
+ }
334
+ } catch (streamReadError) {
335
+ console.error(
336
+ "Error reading stream chunk:",
337
+ streamReadError
338
+ );
339
+ errorOccurred = true;
340
+ finalErrorMessage = "An error occurred while reading the response.";
341
+ onError({
342
+ title: "Stream Error",
343
+ message: finalErrorMessage
344
+ });
345
+ done = true;
346
+ }
347
+ }
348
+ if ((metadata == null ? void 0 : metadata.threadID) && metadata.threadID !== conversationId && onThreadCreated) {
349
+ onThreadCreated(conversationId, metadata.threadID);
350
+ }
351
+ }
352
+ } catch (error) {
353
+ console.error("Error during chat submission:", error);
354
+ if ((error == null ? void 0 : error.name) === "AbortError") {
355
+ return;
356
+ }
357
+ errorOccurred = true;
358
+ let title = "Request Error";
359
+ let localMessage = "An unexpected error occurred. Please try again.";
360
+ if (error == null ? void 0 : error.isConfigurationError) {
361
+ title = "Configuration Error";
362
+ localMessage = error.message || "Assistant ID is missing or invalid.";
363
+ } else if (error == null ? void 0 : error.isApiError) {
364
+ title = `API Error (${error.status})`;
365
+ localMessage = error.message || error.statusText || "An error occurred processing your request.";
366
+ } else if (error == null ? void 0 : error.isNetworkError) {
367
+ title = "Network Error";
368
+ localMessage = error.message || "Failed to connect to the server.";
369
+ } else if (error instanceof Error) {
370
+ localMessage = error.message;
371
+ }
372
+ finalErrorMessage = localMessage;
373
+ onError({ title, message: finalErrorMessage });
374
+ } finally {
375
+ onStreamEnd(
376
+ errorOccurred ? finalErrorMessage : accumulatedResponse,
377
+ metadata
378
+ );
379
+ setIsSubmitting(false);
380
+ abortControllerRef.current = null;
381
+ }
382
+ };
383
+ useEffect(() => {
384
+ if (!isStreaming && abortControllerRef.current) {
385
+ abortControllerRef.current.abort();
386
+ abortControllerRef.current = null;
387
+ }
388
+ }, [isStreaming]);
389
+ const handleKeyDown = (e) => {
390
+ if (e.key === "Enter" && !e.shiftKey) {
391
+ e.preventDefault();
392
+ handleSubmit(e);
393
+ }
394
+ };
395
+ return /* @__PURE__ */ jsxs(
396
+ "form",
397
+ {
398
+ onSubmit: handleSubmit,
399
+ className: "chat-input relative px-2 pt-3 pb-3 flex w-full items-center border rounded-2xl shadow-sm overflow-hidden",
400
+ style: {
401
+ borderColor: accentColor,
402
+ backgroundColor: theme === "dark" ? inputBackgroundColorDark : inputBackgroundColor
403
+ },
404
+ children: [
405
+ attachments.length > 0 && /* @__PURE__ */ jsx("div", { className: "absolute top-0 left-0 right-0 bg-gray-100 dark:bg-gray-700 p-2 flex flex-wrap gap-2 max-h-24 overflow-y-auto", children: attachments.map((att, index) => {
406
+ const isImage = att.type === "image";
407
+ return /* @__PURE__ */ jsxs(
408
+ "div",
409
+ {
410
+ className: "flex items-center space-x-2 bg-white dark:bg-gray-800 rounded-lg px-2 py-1 border border-gray-200 dark:border-gray-600",
411
+ children: [
412
+ isImage ? /* @__PURE__ */ jsx("div", { className: "w-6 h-6 rounded overflow-hidden bg-white", children: /* @__PURE__ */ jsx(
413
+ "img",
414
+ {
415
+ src: att.image || att.data,
416
+ alt: "Preview",
417
+ className: "w-full h-full object-cover"
418
+ }
419
+ ) }) : /* @__PURE__ */ jsx(
420
+ FileText,
421
+ {
422
+ size: 16,
423
+ className: "text-gray-600 dark:text-gray-300"
424
+ }
425
+ ),
426
+ /* @__PURE__ */ jsx("span", { className: "text-xs truncate max-w-[120px]", children: att.name }),
427
+ /* @__PURE__ */ jsx(
428
+ "button",
429
+ {
430
+ type: "button",
431
+ onClick: () => removeAttachment(index),
432
+ className: "text-gray-500 hover:text-red-600 dark:text-gray-400 dark:hover:text-red-400",
433
+ children: /* @__PURE__ */ jsx(X, { size: 14 })
434
+ }
435
+ )
436
+ ]
437
+ },
438
+ index
439
+ );
440
+ }) }),
441
+ /* @__PURE__ */ jsx(
442
+ "textarea",
443
+ {
444
+ ref: textareaRef,
445
+ value: input,
446
+ onChange: (e) => setInput(e.target.value),
447
+ onKeyDown: handleKeyDown,
448
+ onPaste: handlePaste,
449
+ placeholder: disabled ? "Please complete the form above..." : "Type a message...",
450
+ className: `max-h-32 min-h-8 w-full resize-none bg-transparent py-2 pl-6 pr-16 focus:outline-none text-base ${attachments.length > 0 ? "mt-28" : ""} ${disabled ? "opacity-50 cursor-not-allowed" : ""}`,
451
+ rows: 1,
452
+ disabled: isSubmitting || disabled
453
+ }
454
+ ),
455
+ /* @__PURE__ */ jsx(
456
+ "input",
457
+ {
458
+ type: "file",
459
+ ref: fileInputRef,
460
+ onChange: handleFileChange,
461
+ accept: "application/pdf,image/*",
462
+ multiple: true,
463
+ className: "hidden"
464
+ }
465
+ ),
466
+ /* @__PURE__ */ jsx(
467
+ "button",
468
+ {
469
+ type: "button",
470
+ onClick: handleAttachmentClick,
471
+ disabled: isSubmitting || isStreaming || disabled,
472
+ className: "p-2 absolute bottom-3 right-12 rounded-full enabled:hover:bg-gray-300 enabled:dark:hover:bg-gray-700 disabled:opacity-40",
473
+ style: { color: accentColor },
474
+ "aria-label": "Attach file or image",
475
+ children: /* @__PURE__ */ jsx(Paperclip, { size: 20 })
476
+ }
477
+ ),
478
+ /* @__PURE__ */ jsx(
479
+ "button",
480
+ {
481
+ type: "submit",
482
+ disabled: !input.trim() && attachments.length === 0 || isSubmitting || disabled,
483
+ 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",
484
+ style: { color: accentColor },
485
+ "aria-label": "Send message",
486
+ children: /* @__PURE__ */ jsx(Send, { size: 20 })
487
+ }
488
+ )
489
+ ]
490
+ }
491
+ );
492
+ }
493
+ export {
494
+ ChatInput as default
495
+ };