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