@memberjunction/ng-chat 2.43.0 → 2.45.0
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 +235 -46
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,17 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @memberjunction/ng-chat
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A reusable Angular component library for building chat interfaces in MemberJunction applications. This package provides a flexible chat component that can be used for AI-assisted conversations, chatbots, or peer-to-peer chat applications.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The `@memberjunction/ng-chat` package provides a feature-rich chat component with built-in support for:
|
|
8
|
+
- User and AI message rendering with distinct styling
|
|
9
|
+
- Markdown formatting in messages
|
|
10
|
+
- Welcome screen with suggested prompts
|
|
11
|
+
- Real-time message handling
|
|
12
|
+
- Responsive design with auto-scrolling
|
|
13
|
+
- Loading indicators for async operations
|
|
4
14
|
|
|
5
15
|
## Features
|
|
6
16
|
|
|
7
|
-
- **Flexible Message System**: Display both user and AI messages with appropriate styling
|
|
8
|
-
- **Markdown Support**:
|
|
9
|
-
- **Welcome Screen**: Customizable welcome screen with suggested questions/prompts
|
|
10
|
-
- **
|
|
11
|
-
- **Loading
|
|
12
|
-
- **Responsive Design**:
|
|
13
|
-
- **Auto-Resizing Input**: Text
|
|
14
|
-
- **Clear Conversation**:
|
|
17
|
+
- **Flexible Message System**: Display both user and AI messages with appropriate styling and avatars
|
|
18
|
+
- **Markdown Support**: Full markdown rendering support for rich text messages
|
|
19
|
+
- **Welcome Screen**: Customizable welcome screen with up to 4 suggested questions/prompts
|
|
20
|
+
- **Smart Scrolling**: Automatic scroll-to-bottom with manual scroll detection
|
|
21
|
+
- **Loading States**: Built-in loading indicator for async operations (e.g., AI responses)
|
|
22
|
+
- **Responsive Design**: Mobile-friendly design that adapts to different screen sizes
|
|
23
|
+
- **Auto-Resizing Input**: Text area automatically expands as users type longer messages
|
|
24
|
+
- **Clear Conversation**: Confirmation dialog for clearing conversation history
|
|
25
|
+
- **Customizable Avatars**: Support for custom AI avatar images
|
|
26
|
+
- **Keyboard Support**: Send messages with Enter key (Shift+Enter for new lines)
|
|
15
27
|
|
|
16
28
|
## Installation
|
|
17
29
|
|
|
@@ -19,6 +31,13 @@ The Chat Component is a reusable Angular component designed for building chat in
|
|
|
19
31
|
npm install @memberjunction/ng-chat
|
|
20
32
|
```
|
|
21
33
|
|
|
34
|
+
### Peer Dependencies
|
|
35
|
+
|
|
36
|
+
This package requires the following peer dependencies:
|
|
37
|
+
- `@angular/common`: ^18.0.2
|
|
38
|
+
- `@angular/core`: ^18.0.2
|
|
39
|
+
- `@angular/forms`: ^18.0.2
|
|
40
|
+
|
|
22
41
|
## Usage
|
|
23
42
|
|
|
24
43
|
### Import the Module
|
|
@@ -47,25 +66,31 @@ export class YourModule { }
|
|
|
47
66
|
</mj-chat>
|
|
48
67
|
```
|
|
49
68
|
|
|
50
|
-
###
|
|
69
|
+
### Complete Example with AI Integration
|
|
51
70
|
|
|
52
71
|
```typescript
|
|
53
|
-
|
|
54
|
-
import { ChatWelcomeQuestion } from '@memberjunction/ng-chat';
|
|
72
|
+
import { Component, ViewChild } from '@angular/core';
|
|
73
|
+
import { ChatComponent, ChatMessage, ChatWelcomeQuestion } from '@memberjunction/ng-chat';
|
|
55
74
|
|
|
56
75
|
@Component({
|
|
57
76
|
selector: 'app-ai-assistant',
|
|
58
77
|
template: `
|
|
59
78
|
<mj-chat
|
|
79
|
+
#chatComponent
|
|
60
80
|
[AIImageURL]="'assets/bot-avatar.png'"
|
|
61
81
|
[AILargeImageURL]="'assets/bot-large.png'"
|
|
82
|
+
[InitialMessage]="'Hi! I'm your AI assistant. How can I help you today?'"
|
|
62
83
|
[WelcomeQuestions]="welcomeQuestions"
|
|
84
|
+
[Placeholder]="'Ask me anything...'"
|
|
85
|
+
[ClearAllMessagesPrompt]="'Are you sure you want to clear this conversation?'"
|
|
63
86
|
(MessageAdded)="handleNewMessage($event)"
|
|
64
87
|
(ClearChatRequested)="handleClearChat()">
|
|
65
88
|
</mj-chat>
|
|
66
89
|
`
|
|
67
90
|
})
|
|
68
91
|
export class AIAssistantComponent {
|
|
92
|
+
@ViewChild('chatComponent') chatComponent!: ChatComponent;
|
|
93
|
+
|
|
69
94
|
welcomeQuestions: ChatWelcomeQuestion[] = [
|
|
70
95
|
{
|
|
71
96
|
topLine: 'Generate a report',
|
|
@@ -100,8 +125,9 @@ export class AIAssistantComponent {
|
|
|
100
125
|
}
|
|
101
126
|
|
|
102
127
|
handleClearChat() {
|
|
103
|
-
//
|
|
104
|
-
|
|
128
|
+
// Clear any conversation state in your service
|
|
129
|
+
this.conversationHistory = [];
|
|
130
|
+
this.chatComponent.ClearAllMessages();
|
|
105
131
|
}
|
|
106
132
|
|
|
107
133
|
async processUserMessage(message: string) {
|
|
@@ -122,6 +148,13 @@ export class AIAssistantComponent {
|
|
|
122
148
|
}
|
|
123
149
|
catch (error) {
|
|
124
150
|
console.error('Error getting AI response:', error);
|
|
151
|
+
// Show error message to user
|
|
152
|
+
this.chatComponent.SendMessage(
|
|
153
|
+
'Sorry, I encountered an error processing your request.',
|
|
154
|
+
'Assistant',
|
|
155
|
+
'ai',
|
|
156
|
+
null
|
|
157
|
+
);
|
|
125
158
|
}
|
|
126
159
|
finally {
|
|
127
160
|
// End loading state
|
|
@@ -131,24 +164,79 @@ export class AIAssistantComponent {
|
|
|
131
164
|
}
|
|
132
165
|
```
|
|
133
166
|
|
|
167
|
+
### Programmatic Message Sending
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
// Send a user message programmatically
|
|
171
|
+
this.chatComponent.SendUserMessage('Hello, AI!');
|
|
172
|
+
|
|
173
|
+
// Send an AI message
|
|
174
|
+
this.chatComponent.SendMessage(
|
|
175
|
+
'Hello! How can I assist you today?',
|
|
176
|
+
'Assistant',
|
|
177
|
+
'ai',
|
|
178
|
+
messageId // optional ID for tracking
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
// Send a message without triggering the MessageAdded event
|
|
182
|
+
this.chatComponent.SendMessage(
|
|
183
|
+
'System message',
|
|
184
|
+
'System',
|
|
185
|
+
'ai',
|
|
186
|
+
null,
|
|
187
|
+
false // fireEvent = false
|
|
188
|
+
);
|
|
189
|
+
```
|
|
190
|
+
|
|
134
191
|
## API Reference
|
|
135
192
|
|
|
193
|
+
### Component Selector
|
|
194
|
+
`<mj-chat></mj-chat>`
|
|
195
|
+
|
|
136
196
|
### Inputs
|
|
137
197
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
198
|
+
| Property | Type | Default | Description |
|
|
199
|
+
|----------|------|---------|-------------|
|
|
200
|
+
| `InitialMessage` | `string` | `''` | Initial message shown when chat is empty |
|
|
201
|
+
| `Messages` | `ChatMessage[]` | `[]` | Array of messages to display |
|
|
202
|
+
| `AIImageURL` | `string` | `''` | URL for the AI's avatar image (24px max width) |
|
|
203
|
+
| `AILargeImageURL` | `string` | `''` | URL for the AI's large avatar on welcome screen |
|
|
204
|
+
| `WelcomeQuestions` | `ChatWelcomeQuestion[]` | `[]` | Array of up to 4 welcome prompts |
|
|
205
|
+
| `ClearAllMessagesPrompt` | `string` | `'Are you sure you want to clear all messages?'` | Confirmation dialog text |
|
|
206
|
+
| `AllowSend` | `boolean` | `true` | Enable/disable message sending |
|
|
207
|
+
| `Placeholder` | `string` | `'Type a message...'` | Input field placeholder text |
|
|
208
|
+
| `ShowWaitingIndicator` | `boolean` | `false` | Show/hide loading spinner |
|
|
147
209
|
|
|
148
210
|
### Outputs
|
|
149
211
|
|
|
150
|
-
|
|
151
|
-
|
|
212
|
+
| Event | Type | Description |
|
|
213
|
+
|-------|------|-------------|
|
|
214
|
+
| `MessageAdded` | `EventEmitter<ChatMessage>` | Emitted when a new message is added to the chat |
|
|
215
|
+
| `ClearChatRequested` | `EventEmitter<void>` | Emitted when user confirms clearing the chat |
|
|
216
|
+
|
|
217
|
+
### Public Methods
|
|
218
|
+
|
|
219
|
+
#### `SendCurrentMessage(): void`
|
|
220
|
+
Sends the current message in the input field. Automatically clears the input after sending.
|
|
221
|
+
|
|
222
|
+
#### `SendMessage(message: string, senderName: string, senderType: 'user' | 'ai', id: any, fireEvent: boolean = true): void`
|
|
223
|
+
Adds a message to the chat programmatically.
|
|
224
|
+
|
|
225
|
+
**Parameters:**
|
|
226
|
+
- `message`: The message text (supports markdown)
|
|
227
|
+
- `senderName`: Display name of the sender
|
|
228
|
+
- `senderType`: Either 'user' or 'ai' for styling
|
|
229
|
+
- `id`: Optional ID for message tracking
|
|
230
|
+
- `fireEvent`: Whether to emit the MessageAdded event
|
|
231
|
+
|
|
232
|
+
#### `SendUserMessage(message: string): void`
|
|
233
|
+
Convenience method to send a user message. Automatically sets senderName to 'User' and senderType to 'user'.
|
|
234
|
+
|
|
235
|
+
#### `ClearAllMessages(): void`
|
|
236
|
+
Clears all messages from the chat and resets to initial state.
|
|
237
|
+
|
|
238
|
+
#### `HandleClearChat(): void`
|
|
239
|
+
Shows the confirmation dialog for clearing chat. This emits the ClearChatRequested event when confirmed.
|
|
152
240
|
|
|
153
241
|
### Classes
|
|
154
242
|
|
|
@@ -156,12 +244,17 @@ export class AIAssistantComponent {
|
|
|
156
244
|
|
|
157
245
|
```typescript
|
|
158
246
|
export class ChatMessage {
|
|
159
|
-
public message: string;
|
|
160
|
-
public senderName: string;
|
|
161
|
-
public senderType: 'user' | 'ai';
|
|
162
|
-
public id?: any;
|
|
163
|
-
|
|
164
|
-
constructor(
|
|
247
|
+
public message: string; // The message content (supports markdown)
|
|
248
|
+
public senderName: string; // Display name of sender
|
|
249
|
+
public senderType: 'user' | 'ai'; // Type for styling purposes
|
|
250
|
+
public id?: any; // Optional ID for tracking
|
|
251
|
+
|
|
252
|
+
constructor(
|
|
253
|
+
message: string,
|
|
254
|
+
senderName: string,
|
|
255
|
+
senderType: 'user' | 'ai',
|
|
256
|
+
id: any = null
|
|
257
|
+
);
|
|
165
258
|
}
|
|
166
259
|
```
|
|
167
260
|
|
|
@@ -169,26 +262,122 @@ export class ChatMessage {
|
|
|
169
262
|
|
|
170
263
|
```typescript
|
|
171
264
|
export class ChatWelcomeQuestion {
|
|
172
|
-
public topLine: string = "";
|
|
173
|
-
public bottomLine: string = "";
|
|
174
|
-
public prompt: string = "";
|
|
265
|
+
public topLine: string = ""; // Main text of the suggestion
|
|
266
|
+
public bottomLine: string = ""; // Supporting text/description
|
|
267
|
+
public prompt: string = ""; // The actual prompt to send when clicked
|
|
175
268
|
}
|
|
176
269
|
```
|
|
177
270
|
|
|
178
|
-
|
|
271
|
+
## Styling
|
|
179
272
|
|
|
180
|
-
|
|
181
|
-
- `SendMessage(message, senderName, senderType, id, fireEvent)`: Sends a specified message
|
|
182
|
-
- `SendUserMessage(message)`: Convenience method to send a user message
|
|
183
|
-
- `ClearAllMessages()`: Clears all messages from the chat interface
|
|
273
|
+
The component provides several CSS classes for customization:
|
|
184
274
|
|
|
185
|
-
|
|
275
|
+
- `.chat-message-wrap`: Container for each message
|
|
276
|
+
- `.chat-message`: The message content container
|
|
277
|
+
- `.chat-message-ai`: Additional class for AI messages
|
|
278
|
+
- `.chat-message-image`: Avatar/icon container
|
|
279
|
+
- `.chat-input-container`: Input area container
|
|
280
|
+
- `.chat-welcome-container`: Welcome screen container
|
|
186
281
|
|
|
187
|
-
|
|
282
|
+
You can override these styles in your global styles or component styles:
|
|
283
|
+
|
|
284
|
+
```css
|
|
285
|
+
/* Example: Customize AI message background */
|
|
286
|
+
::ng-deep .chat-message-ai {
|
|
287
|
+
background-color: #f0f4f8;
|
|
288
|
+
border-left: 3px solid #3182ce;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/* Example: Customize user avatar */
|
|
292
|
+
::ng-deep .chat-message-wrap:has(.chat-message:not(.chat-message-ai)) .chat-message-image {
|
|
293
|
+
color: #48bb78;
|
|
294
|
+
}
|
|
295
|
+
```
|
|
188
296
|
|
|
189
297
|
## Dependencies
|
|
190
298
|
|
|
191
|
-
|
|
192
|
-
- `@
|
|
193
|
-
- `@
|
|
194
|
-
- `@progress/kendo-angular-
|
|
299
|
+
### Runtime Dependencies
|
|
300
|
+
- `@memberjunction/core`: Core MemberJunction utilities
|
|
301
|
+
- `@memberjunction/ng-container-directives`: Container directive utilities
|
|
302
|
+
- `@progress/kendo-angular-indicators`: Loading spinner component
|
|
303
|
+
- `@progress/kendo-angular-buttons`: Button components
|
|
304
|
+
- `@progress/kendo-angular-dialog`: Dialog component for confirmations
|
|
305
|
+
- `ngx-markdown`: Markdown rendering support
|
|
306
|
+
- `tslib`: TypeScript runtime helpers
|
|
307
|
+
|
|
308
|
+
### Integration with MemberJunction
|
|
309
|
+
|
|
310
|
+
This component integrates seamlessly with other MemberJunction packages:
|
|
311
|
+
|
|
312
|
+
- Use with `@memberjunction/ai` for AI-powered conversations
|
|
313
|
+
- Combine with `@memberjunction/core-entities` for entity-aware chat
|
|
314
|
+
- Integrate with MemberJunction's authentication for user context
|
|
315
|
+
|
|
316
|
+
## Build and Development
|
|
317
|
+
|
|
318
|
+
This package uses Angular's library build system. To build the package:
|
|
319
|
+
|
|
320
|
+
```bash
|
|
321
|
+
# From the package directory
|
|
322
|
+
npm run build
|
|
323
|
+
|
|
324
|
+
# From the repository root
|
|
325
|
+
turbo build --filter="@memberjunction/ng-chat"
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
The built files will be output to the `dist/` directory.
|
|
329
|
+
|
|
330
|
+
## Advanced Usage
|
|
331
|
+
|
|
332
|
+
### Custom Message Rendering
|
|
333
|
+
|
|
334
|
+
While the component handles markdown rendering by default, you can extend functionality by processing messages before sending:
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
// Pre-process messages with custom formatting
|
|
338
|
+
const formattedMessage = this.formatMessage(userInput);
|
|
339
|
+
this.chatComponent.SendUserMessage(formattedMessage);
|
|
340
|
+
|
|
341
|
+
// Add metadata to messages using the id field
|
|
342
|
+
this.chatComponent.SendMessage(
|
|
343
|
+
response,
|
|
344
|
+
'Assistant',
|
|
345
|
+
'ai',
|
|
346
|
+
{ timestamp: Date.now(), tokens: 150, model: 'gpt-4' }
|
|
347
|
+
);
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
### Maintaining Conversation History
|
|
351
|
+
|
|
352
|
+
```typescript
|
|
353
|
+
// Store conversation for persistence
|
|
354
|
+
private saveConversation() {
|
|
355
|
+
const messages = this.chatComponent.Messages;
|
|
356
|
+
localStorage.setItem('chat-history', JSON.stringify(messages));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Restore previous conversation
|
|
360
|
+
private loadConversation() {
|
|
361
|
+
const saved = localStorage.getItem('chat-history');
|
|
362
|
+
if (saved) {
|
|
363
|
+
const messages = JSON.parse(saved) as ChatMessage[];
|
|
364
|
+
messages.forEach(msg => {
|
|
365
|
+
this.chatComponent.SendMessage(
|
|
366
|
+
msg.message,
|
|
367
|
+
msg.senderName,
|
|
368
|
+
msg.senderType,
|
|
369
|
+
msg.id,
|
|
370
|
+
false // Don't fire events when restoring
|
|
371
|
+
);
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
## Notes
|
|
378
|
+
|
|
379
|
+
- The component automatically focuses the input field after operations
|
|
380
|
+
- Messages support full markdown syntax including code blocks, lists, and links
|
|
381
|
+
- The welcome screen is only shown when there are no messages
|
|
382
|
+
- Default icons use Font Awesome (fa-robot for AI, fa-user for users)
|
|
383
|
+
- The component uses Angular's ChangeDetectorRef for optimal performance
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ng-chat",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.45.0",
|
|
4
4
|
"description": "MemberJunction: Reusable Chat Component - can be used for AI or peer to peer chat applications.",
|
|
5
5
|
"main": "./dist/public-api.js",
|
|
6
6
|
"typings": "./dist/public-api.d.ts",
|
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
"@angular/forms": "18.0.2"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@memberjunction/core": "2.
|
|
28
|
-
"@memberjunction/ng-container-directives": "2.
|
|
27
|
+
"@memberjunction/core": "2.45.0",
|
|
28
|
+
"@memberjunction/ng-container-directives": "2.45.0",
|
|
29
29
|
"@progress/kendo-angular-indicators": "16.2.0",
|
|
30
30
|
"@progress/kendo-angular-buttons": "16.2.0",
|
|
31
31
|
"@progress/kendo-angular-dialog": "16.2.0",
|