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