@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.
- package/README.md +137 -2
- package/dist/chat-input-42V4ESQB.mjs +478 -0
- package/dist/chat-input-4T42R7FR.mjs +481 -0
- package/dist/chat-input-BJVIIYOP.mjs +484 -0
- package/dist/chat-input-HT47ZLQN.mjs +456 -0
- package/dist/chat-input-JFGPZBBQ.mjs +495 -0
- package/dist/chat-input-JQ7PSHNW.mjs +515 -0
- package/dist/chat-input-USWVSAYY.mjs +461 -0
- package/dist/chat-input-VTCTV5ED.mjs +495 -0
- package/dist/chat-input-WJMGLQJI.mjs +495 -0
- package/dist/chat-message-2VQW74CO.mjs +2230 -0
- package/dist/chat-message-35ZWSSQV.mjs +1926 -0
- package/dist/chat-message-3CUUWTZG.mjs +2429 -0
- package/dist/chat-message-3NHSJNKL.mjs +2241 -0
- package/dist/chat-message-3Z7B2WYC.mjs +2228 -0
- package/dist/chat-message-564AHZHH.mjs +2186 -0
- package/dist/chat-message-5QLTW4K7.mjs +2122 -0
- package/dist/chat-message-7EH2TDZG.mjs +1910 -0
- package/dist/chat-message-7QHJRHQG.mjs +1909 -0
- package/dist/chat-message-AAXNH5TF.mjs +2088 -0
- package/dist/chat-message-D36YF274.mjs +1910 -0
- package/dist/chat-message-DVCXEL4Z.mjs +2111 -0
- package/dist/chat-message-E6KB3AST.mjs +1906 -0
- package/dist/chat-message-G2NWPAXK.mjs +1904 -0
- package/dist/chat-message-H62Z3DW5.mjs +2368 -0
- package/dist/chat-message-HIEZ7B5R.mjs +2190 -0
- package/dist/chat-message-I6AKFPK6.mjs +2156 -0
- package/dist/chat-message-IKYSAVAB.mjs +1918 -0
- package/dist/chat-message-IZL6JHV2.mjs +2429 -0
- package/dist/chat-message-J7PVUQO6.mjs +1878 -0
- package/dist/chat-message-K6QILTW5.mjs +1897 -0
- package/dist/chat-message-MDORLI2R.mjs +2228 -0
- package/dist/chat-message-MFUY6KOE.mjs +1910 -0
- package/dist/chat-message-MYKOR5OF.mjs +1890 -0
- package/dist/chat-message-NKMTAMGG.mjs +1906 -0
- package/dist/chat-message-NNGD3FUH.mjs +2168 -0
- package/dist/chat-message-QAPRO542.mjs +1915 -0
- package/dist/chat-message-R4IJ3MXU.mjs +2146 -0
- package/dist/chat-message-U7UCBLHI.mjs +2145 -0
- package/dist/chat-message-UKXGFKUR.mjs +1903 -0
- package/dist/chat-message-UNIBCF3T.mjs +1900 -0
- package/dist/chat-message-UXLPL76T.mjs +1890 -0
- package/dist/chat-message-VB54UOHB.mjs +2306 -0
- package/dist/chat-message-WUNUZLKI.mjs +2228 -0
- package/dist/chat-message-WYN5UZRD.mjs +1927 -0
- package/dist/chat-message-Y5OJSR2O.mjs +2228 -0
- package/dist/chat-message-YPDHL6WW.mjs +2114 -0
- package/dist/chunk-26656QB3.mjs +406 -0
- package/dist/chunk-2RXQ2EZ2.mjs +295 -0
- package/dist/chunk-DVWFDUN4.mjs +407 -0
- package/dist/chunk-JLRHY3SB.mjs +405 -0
- package/dist/chunk-XFSEOBLD.mjs +432 -0
- package/dist/index.css +174 -43
- package/dist/index.d.mts +218 -1
- package/dist/index.d.ts +218 -1
- package/dist/index.js +2088 -1400
- package/dist/index.mjs +606 -413
- package/dist/styles.css +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -6,11 +6,146 @@ npm i @terryavg/neptune-ai-chatbot
|
|
|
6
6
|
```
|
|
7
7
|
|
|
8
8
|
## Usage
|
|
9
|
+
|
|
10
|
+
### Basic Usage
|
|
11
|
+
```tsx
|
|
12
|
+
import { NeptuneChatBot } from "@terryavg/neptune-ai-chatbot";
|
|
13
|
+
import '@terryavg/neptune-ai-chatbot/styles.css';
|
|
14
|
+
|
|
15
|
+
function App() {
|
|
16
|
+
return <NeptuneChatBot agentId="your-agent-id" />;
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### With Customization
|
|
9
21
|
```tsx
|
|
10
22
|
import { NeptuneChatBot } from "@terryavg/neptune-ai-chatbot";
|
|
11
23
|
import '@terryavg/neptune-ai-chatbot/styles.css';
|
|
12
24
|
|
|
13
25
|
function App() {
|
|
14
|
-
return
|
|
26
|
+
return (
|
|
27
|
+
<NeptuneChatBot
|
|
28
|
+
agentId="your-agent-id"
|
|
29
|
+
theme="dark"
|
|
30
|
+
title="My Assistant"
|
|
31
|
+
messageBubbleColor="#E5E3F8"
|
|
32
|
+
messageBubbleColorDark="rgba(168, 140, 250, 0.3)"
|
|
33
|
+
accentColor="#8B7FD9"
|
|
34
|
+
streamingText="AI is thinking..."
|
|
35
|
+
welcomeMessagePrimary="Welcome!"
|
|
36
|
+
welcomeMessageSecondary="How can I assist you today?"
|
|
37
|
+
/>
|
|
38
|
+
);
|
|
15
39
|
}
|
|
16
|
-
```
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Configuration
|
|
43
|
+
|
|
44
|
+
### Props
|
|
45
|
+
|
|
46
|
+
All customization options are available through props with full TypeScript support and JSDoc documentation:
|
|
47
|
+
|
|
48
|
+
| Prop | Type | Default | Description |
|
|
49
|
+
|------|------|---------|-------------|
|
|
50
|
+
| `agentId` | `string` | **required** | Unique identifier for the AI agent |
|
|
51
|
+
| `theme` | `"light" \| "dark"` | `"light"` | Color theme for the chat interface |
|
|
52
|
+
| `title` | `string` | `"Naia"` | Title displayed in the chat header |
|
|
53
|
+
| `messageBubbleColor` | `string` | `"#E5E3F8"` | Background color for message bubbles (light mode) |
|
|
54
|
+
| `messageBubbleColorDark` | `string` | `"rgba(168, 140, 250, 0.3)"` | Background color for message bubbles (dark mode) |
|
|
55
|
+
| `accentColor` | `string` | `"#8B7FD9"` | Accent color for buttons and interactive elements (light mode) |
|
|
56
|
+
| `accentColorDark` | `string` | `"#A88CFA"` | Accent color for buttons and interactive elements (dark mode) |
|
|
57
|
+
| `scrollButtonColor` | `string` | `"#6366F1"` | Color for the scroll-to-bottom button (light mode) |
|
|
58
|
+
| `scrollButtonColorDark` | `string` | `"#818CF8"` | Color for the scroll-to-bottom button (dark mode) |
|
|
59
|
+
| `streamingText` | `string` | `"NAIA is working on it..."` | Text displayed while AI is generating a response |
|
|
60
|
+
| `streamingTextColor` | `string` | `"#2563EB"` | Color of the streaming indicator text (light mode) |
|
|
61
|
+
| `streamingTextColorDark` | `string` | `"#818CF8"` | Color of the streaming indicator text (dark mode) |
|
|
62
|
+
| `welcomeMessagePrimary` | `string` | `"Hi there!"` | Primary welcome message on the initial screen |
|
|
63
|
+
| `welcomeMessageSecondary` | `string` | `"How can I help you today?"` | Secondary welcome message below the primary message |
|
|
64
|
+
| `welcomeIcon` | `string` | - | Path to a custom icon image for the welcome screen |
|
|
65
|
+
| `welcomeIconSize` | `string` | `"10rem"` | Size of the welcome icon with CSS units |
|
|
66
|
+
| `streaming` | `boolean` | `true` | Enable streaming responses from the AI |
|
|
67
|
+
| `sidebarBackgroundColor` | `string` | `"#FAFAFA"` | Background color for the sidebar (light mode) |
|
|
68
|
+
| `sidebarBackgroundColorDark` | `string` | `"#1F1F1F"` | Background color for the sidebar (dark mode) |
|
|
69
|
+
| `inputBackgroundColor` | `string` | `"#FFFFFF"` | Background color for the chat input field (light mode) |
|
|
70
|
+
| `inputBackgroundColorDark` | `string` | `"#303030"` | Background color for the chat input field (dark mode) |
|
|
71
|
+
| `headerBackgroundColor` | `string` | `"#FFFFFF"` | Background color for the header (light mode) |
|
|
72
|
+
| `headerBackgroundColorDark` | `string` | `"#1F1F1F"` | Background color for the header (dark mode) |
|
|
73
|
+
| `vectorColor` | `string` | `"#9333EA"` | Base color for vector search results (light mode) - automatically generates variations |
|
|
74
|
+
| `vectorColorDark` | `string` | `"#A855F7"` | Base color for vector search results (dark mode) - automatically generates variations |
|
|
75
|
+
| `debug` | `boolean` | `false` | Enable debug mode for console logs |
|
|
76
|
+
| `localDebug` | `LocalDebugConfig` | - | Configuration for local development (see below) |
|
|
77
|
+
| `onToolStart` | `(metadata: any) => void` | - | Callback fired when a tool starts executing during streaming |
|
|
78
|
+
| `onToolInput` | `(metadata: any) => void` | - | Callback fired when tool input is available during streaming |
|
|
79
|
+
| `onToolFinish` | `(metadata: any) => void` | - | Callback fired when a tool finishes executing during streaming |
|
|
80
|
+
| `onChunk` | `(chunk: string) => void` | - | Callback fired for each text chunk received during streaming |
|
|
81
|
+
| `onFinish` | `(metadata: any) => void` | - | Callback fired when streaming finishes |
|
|
82
|
+
|
|
83
|
+
### Streaming Event Callbacks
|
|
84
|
+
|
|
85
|
+
When `streaming` is enabled, you can hook into various streaming events to track the AI's response generation:
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
<NeptuneChatBot
|
|
89
|
+
agentId="your-agent-id"
|
|
90
|
+
streaming={true}
|
|
91
|
+
onToolStart={(metadata) => {
|
|
92
|
+
console.log('Tool started:', metadata.toolName);
|
|
93
|
+
}}
|
|
94
|
+
onToolInput={(metadata) => {
|
|
95
|
+
console.log('Tool input available:', metadata);
|
|
96
|
+
}}
|
|
97
|
+
onToolFinish={(metadata) => {
|
|
98
|
+
console.log('Tool finished:', metadata);
|
|
99
|
+
}}
|
|
100
|
+
onChunk={(chunk) => {
|
|
101
|
+
console.log('Received text chunk:', chunk);
|
|
102
|
+
}}
|
|
103
|
+
onFinish={(metadata) => {
|
|
104
|
+
console.log('Streaming completed:', metadata);
|
|
105
|
+
}}
|
|
106
|
+
/>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Available callbacks:
|
|
110
|
+
- **onToolStart**: Fires when a tool begins execution (e.g., web search, API call)
|
|
111
|
+
- **onToolInput**: Fires when the tool's input parameters are generated
|
|
112
|
+
- **onToolFinish**: Fires when the tool completes and returns output
|
|
113
|
+
- **onChunk**: Fires for each text chunk received from the AI
|
|
114
|
+
- **onFinish**: Fires when the entire response stream is complete
|
|
115
|
+
|
|
116
|
+
### Local Development Configuration
|
|
117
|
+
|
|
118
|
+
The `localDebug` prop is used when the component runs **outside of a Neptune environment**. When deployed within Neptune, authentication is handled automatically.
|
|
119
|
+
|
|
120
|
+
**Note:** This configuration is only required for local development and testing.
|
|
121
|
+
|
|
122
|
+
```tsx
|
|
123
|
+
<NeptuneChatBot
|
|
124
|
+
agentId="your-agent-id"
|
|
125
|
+
localDebug={{
|
|
126
|
+
username: "your-username",
|
|
127
|
+
password: "your-password",
|
|
128
|
+
baseUrl: "http://localhost:3000"
|
|
129
|
+
}}
|
|
130
|
+
/>
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
The `localDebug` object includes:
|
|
134
|
+
- `username` - Username for local authentication
|
|
135
|
+
- `password` - Password for local authentication
|
|
136
|
+
- `baseUrl` - Base URL for the local API server
|
|
137
|
+
|
|
138
|
+
## Features
|
|
139
|
+
|
|
140
|
+
- 🎨 **Fully Customizable** - Theme colors, text, and UI elements
|
|
141
|
+
- 🌓 **Dark Mode Support** - Built-in light and dark theme support
|
|
142
|
+
- 📎 **Multiple File Attachments** - Upload multiple images and PDFs
|
|
143
|
+
- ⚡ **Real-time Streaming** - See AI responses as they're generated
|
|
144
|
+
- 💬 **Conversation History** - Automatic conversation management and persistence
|
|
145
|
+
- 📱 **Responsive Design** - Works seamlessly on desktop and mobile
|
|
146
|
+
- ♿ **Accessible** - ARIA labels and keyboard navigation support
|
|
147
|
+
- 🔧 **TypeScript** - Full type safety with comprehensive JSDoc documentation
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
MIT
|
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
import {
|
|
2
|
+
chatClient
|
|
3
|
+
} from "./chunk-DVWFDUN4.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
|
+
}) {
|
|
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
|
+
streaming
|
|
222
|
+
// streaming mode
|
|
223
|
+
);
|
|
224
|
+
if (threadId && threadId !== conversationId && onThreadCreated) {
|
|
225
|
+
console.log(`Conversation created with ID: ${threadId}`);
|
|
226
|
+
onThreadCreated(conversationId, threadId);
|
|
227
|
+
}
|
|
228
|
+
if (!streaming) {
|
|
229
|
+
const reader = stream.getReader();
|
|
230
|
+
const decoder = new TextDecoder();
|
|
231
|
+
let responseText = "";
|
|
232
|
+
while (true) {
|
|
233
|
+
const { done, value } = await reader.read();
|
|
234
|
+
if (done) break;
|
|
235
|
+
responseText += decoder.decode(value, { stream: true });
|
|
236
|
+
}
|
|
237
|
+
const response = JSON.parse(responseText);
|
|
238
|
+
const outputText = response.output || "";
|
|
239
|
+
accumulatedResponse = outputText;
|
|
240
|
+
onStreamUpdate(outputText);
|
|
241
|
+
metadata = response;
|
|
242
|
+
if (response.threadID && response.threadID !== conversationId && onThreadCreated) {
|
|
243
|
+
onThreadCreated(conversationId, response.threadID);
|
|
244
|
+
}
|
|
245
|
+
} else {
|
|
246
|
+
const reader = stream.getReader();
|
|
247
|
+
const decoder = new TextDecoder();
|
|
248
|
+
let done = false;
|
|
249
|
+
let buffer = "";
|
|
250
|
+
let isToolExecuting = false;
|
|
251
|
+
let currentToolName = "";
|
|
252
|
+
while (!done) {
|
|
253
|
+
try {
|
|
254
|
+
if (abortController.signal.aborted) {
|
|
255
|
+
console.log("Request was aborted by user");
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
const { value, done: readerDone } = await reader.read();
|
|
259
|
+
done = readerDone;
|
|
260
|
+
if (value) {
|
|
261
|
+
buffer += decoder.decode(value, { stream: true });
|
|
262
|
+
const lines = buffer.split("\n");
|
|
263
|
+
buffer = lines.pop() || "";
|
|
264
|
+
for (const line of lines) {
|
|
265
|
+
if (!line.trim()) continue;
|
|
266
|
+
if (line.startsWith("data: ")) {
|
|
267
|
+
const jsonString = line.substring(6);
|
|
268
|
+
if (jsonString === "[DONE]") {
|
|
269
|
+
done = true;
|
|
270
|
+
break;
|
|
271
|
+
}
|
|
272
|
+
try {
|
|
273
|
+
const event = JSON.parse(jsonString);
|
|
274
|
+
switch (event.type) {
|
|
275
|
+
case "start-step":
|
|
276
|
+
break;
|
|
277
|
+
case "tool-input-start":
|
|
278
|
+
currentToolName = event.toolName || "Unknown Tool";
|
|
279
|
+
isToolExecuting = true;
|
|
280
|
+
if (onToolExecutionStart) {
|
|
281
|
+
onToolExecutionStart(
|
|
282
|
+
currentToolName
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
break;
|
|
286
|
+
case "tool-input-delta":
|
|
287
|
+
break;
|
|
288
|
+
case "tool-input-available":
|
|
289
|
+
break;
|
|
290
|
+
case "tool-output-available":
|
|
291
|
+
if (isToolExecuting && onToolExecutionEnd) {
|
|
292
|
+
onToolExecutionEnd();
|
|
293
|
+
}
|
|
294
|
+
isToolExecuting = false;
|
|
295
|
+
currentToolName = "";
|
|
296
|
+
break;
|
|
297
|
+
case "finish-step":
|
|
298
|
+
if (isToolExecuting && onToolExecutionEnd) {
|
|
299
|
+
onToolExecutionEnd();
|
|
300
|
+
}
|
|
301
|
+
isToolExecuting = false;
|
|
302
|
+
currentToolName = "";
|
|
303
|
+
break;
|
|
304
|
+
case "text-delta":
|
|
305
|
+
accumulatedResponse += event.delta;
|
|
306
|
+
onStreamUpdate(event.delta);
|
|
307
|
+
break;
|
|
308
|
+
case "data-finish-result":
|
|
309
|
+
metadata = event.data;
|
|
310
|
+
break;
|
|
311
|
+
case "error":
|
|
312
|
+
throw new Error(
|
|
313
|
+
event.errorText || "Stream error occurred"
|
|
314
|
+
);
|
|
315
|
+
// Ignore other event types
|
|
316
|
+
default:
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
} catch (parseError) {
|
|
320
|
+
console.error(
|
|
321
|
+
"Error parsing SSE event:",
|
|
322
|
+
parseError
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
} catch (streamReadError) {
|
|
329
|
+
console.error(
|
|
330
|
+
"Error reading stream chunk:",
|
|
331
|
+
streamReadError
|
|
332
|
+
);
|
|
333
|
+
errorOccurred = true;
|
|
334
|
+
finalErrorMessage = "An error occurred while reading the response.";
|
|
335
|
+
onError({
|
|
336
|
+
title: "Stream Error",
|
|
337
|
+
message: finalErrorMessage
|
|
338
|
+
});
|
|
339
|
+
done = true;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if ((metadata == null ? void 0 : metadata.threadID) && metadata.threadID !== conversationId && onThreadCreated) {
|
|
343
|
+
onThreadCreated(conversationId, metadata.threadID);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
} catch (error) {
|
|
347
|
+
console.error("Error during chat submission:", error);
|
|
348
|
+
if ((error == null ? void 0 : error.name) === "AbortError") {
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
errorOccurred = true;
|
|
352
|
+
let title = "Request Error";
|
|
353
|
+
let localMessage = "An unexpected error occurred. Please try again.";
|
|
354
|
+
if (error == null ? void 0 : error.isConfigurationError) {
|
|
355
|
+
title = "Configuration Error";
|
|
356
|
+
localMessage = error.message || "Assistant ID is missing or invalid.";
|
|
357
|
+
} else if (error == null ? void 0 : error.isApiError) {
|
|
358
|
+
title = `API Error (${error.status})`;
|
|
359
|
+
localMessage = error.message || error.statusText || "An error occurred processing your request.";
|
|
360
|
+
} else if (error == null ? void 0 : error.isNetworkError) {
|
|
361
|
+
title = "Network Error";
|
|
362
|
+
localMessage = error.message || "Failed to connect to the server.";
|
|
363
|
+
} else if (error instanceof Error) {
|
|
364
|
+
localMessage = error.message;
|
|
365
|
+
}
|
|
366
|
+
finalErrorMessage = localMessage;
|
|
367
|
+
onError({ title, message: finalErrorMessage });
|
|
368
|
+
} finally {
|
|
369
|
+
onStreamEnd(
|
|
370
|
+
errorOccurred ? finalErrorMessage : accumulatedResponse,
|
|
371
|
+
metadata
|
|
372
|
+
);
|
|
373
|
+
setIsSubmitting(false);
|
|
374
|
+
abortControllerRef.current = null;
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
useEffect(() => {
|
|
378
|
+
if (!isStreaming && abortControllerRef.current) {
|
|
379
|
+
console.log("Aborting request due to streaming stopped");
|
|
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 bg-white dark:bg-gray-100 rounded-2xl shadow-sm overflow-hidden",
|
|
395
|
+
style: { borderColor: accentColor },
|
|
396
|
+
children: [
|
|
397
|
+
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: [
|
|
398
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center space-x-2 truncate max-w-[90%]", children: [
|
|
399
|
+
isImageAttachment ? /* @__PURE__ */ jsx("div", { className: "w-6 h-6 rounded overflow-hidden bg-white", children: /* @__PURE__ */ jsx(
|
|
400
|
+
"img",
|
|
401
|
+
{
|
|
402
|
+
src: attachment.image || attachment.data,
|
|
403
|
+
alt: "Preview",
|
|
404
|
+
className: "w-full h-full object-cover"
|
|
405
|
+
}
|
|
406
|
+
) }) : /* @__PURE__ */ jsx(
|
|
407
|
+
FileText,
|
|
408
|
+
{
|
|
409
|
+
size: 16,
|
|
410
|
+
className: "text-gray-600 dark:text-gray-300"
|
|
411
|
+
}
|
|
412
|
+
),
|
|
413
|
+
/* @__PURE__ */ jsx("span", { className: "text-xs truncate", children: attachment.name })
|
|
414
|
+
] }),
|
|
415
|
+
/* @__PURE__ */ jsx(
|
|
416
|
+
"button",
|
|
417
|
+
{
|
|
418
|
+
type: "button",
|
|
419
|
+
onClick: clearAttachment,
|
|
420
|
+
className: "text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200",
|
|
421
|
+
children: /* @__PURE__ */ jsx(X, { size: 16 })
|
|
422
|
+
}
|
|
423
|
+
)
|
|
424
|
+
] }),
|
|
425
|
+
/* @__PURE__ */ jsx(
|
|
426
|
+
"textarea",
|
|
427
|
+
{
|
|
428
|
+
ref: textareaRef,
|
|
429
|
+
value: input,
|
|
430
|
+
onChange: (e) => setInput(e.target.value),
|
|
431
|
+
onKeyDown: handleKeyDown,
|
|
432
|
+
onPaste: handlePaste,
|
|
433
|
+
placeholder: disabled ? "Please complete the form above..." : "Type a message...",
|
|
434
|
+
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" : ""}`,
|
|
435
|
+
rows: 1,
|
|
436
|
+
disabled: isSubmitting || disabled
|
|
437
|
+
}
|
|
438
|
+
),
|
|
439
|
+
/* @__PURE__ */ jsx(
|
|
440
|
+
"input",
|
|
441
|
+
{
|
|
442
|
+
type: "file",
|
|
443
|
+
ref: fileInputRef,
|
|
444
|
+
onChange: handleFileChange,
|
|
445
|
+
accept: "application/pdf,image/*",
|
|
446
|
+
className: "hidden"
|
|
447
|
+
}
|
|
448
|
+
),
|
|
449
|
+
/* @__PURE__ */ jsx(
|
|
450
|
+
"button",
|
|
451
|
+
{
|
|
452
|
+
type: "button",
|
|
453
|
+
onClick: handleAttachmentClick,
|
|
454
|
+
disabled: isSubmitting || isStreaming || disabled,
|
|
455
|
+
className: "p-2 absolute bottom-3 right-12 rounded-full enabled:hover:bg-gray-300 enabled:dark:hover:bg-gray-700 disabled:opacity-40",
|
|
456
|
+
style: { color: accentColor },
|
|
457
|
+
"aria-label": "Attach file or image",
|
|
458
|
+
children: /* @__PURE__ */ jsx(Paperclip, { size: 20 })
|
|
459
|
+
}
|
|
460
|
+
),
|
|
461
|
+
/* @__PURE__ */ jsx(
|
|
462
|
+
"button",
|
|
463
|
+
{
|
|
464
|
+
type: "submit",
|
|
465
|
+
disabled: !input.trim() && !attachment || isSubmitting || disabled,
|
|
466
|
+
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",
|
|
467
|
+
style: { color: accentColor },
|
|
468
|
+
"aria-label": "Send message",
|
|
469
|
+
children: /* @__PURE__ */ jsx(Send, { size: 20 })
|
|
470
|
+
}
|
|
471
|
+
)
|
|
472
|
+
]
|
|
473
|
+
}
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
export {
|
|
477
|
+
ChatInput as default
|
|
478
|
+
};
|