@memberjunction/ng-chat 4.0.0 → 4.2.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 +88 -264
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,29 +1,38 @@
|
|
|
1
1
|
# @memberjunction/ng-chat
|
|
2
2
|
|
|
3
|
-
A reusable Angular component
|
|
3
|
+
A reusable Angular chat component for building AI-assisted conversations, chatbots, or peer-to-peer chat interfaces in MemberJunction applications.
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
The `@memberjunction/ng-chat` package provides a feature-rich chat component with
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
7
|
+
The `@memberjunction/ng-chat` package provides a feature-rich chat component with markdown rendering, customizable welcome screens with suggested prompts, real-time message handling, auto-scrolling, loading indicators, and keyboard shortcuts. It can be used for AI conversations, customer support interfaces, or any messaging scenario.
|
|
8
|
+
|
|
9
|
+
```mermaid
|
|
10
|
+
graph TD
|
|
11
|
+
A[ChatModule] --> B[ChatComponent]
|
|
12
|
+
|
|
13
|
+
B --> C[Welcome Screen]
|
|
14
|
+
B --> D[Message List]
|
|
15
|
+
B --> E[Input Area]
|
|
16
|
+
|
|
17
|
+
C --> C1["AI Avatar
|
|
18
|
+
(large)"]
|
|
19
|
+
C --> C2["Suggested Questions
|
|
20
|
+
(up to 4)"]
|
|
21
|
+
|
|
22
|
+
D --> D1["User Messages"]
|
|
23
|
+
D --> D2["AI Messages
|
|
24
|
+
(with markdown)"]
|
|
25
|
+
D --> D3["Loading Indicator"]
|
|
26
|
+
|
|
27
|
+
E --> E1["Auto-Resize TextArea"]
|
|
28
|
+
E --> E2["Send Button"]
|
|
29
|
+
E --> E3["Clear Chat"]
|
|
30
|
+
|
|
31
|
+
style A fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
32
|
+
style B fill:#7c5295,stroke:#563a6b,color:#fff
|
|
33
|
+
style C fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
34
|
+
style D fill:#b8762f,stroke:#8a5722,color:#fff
|
|
35
|
+
```
|
|
27
36
|
|
|
28
37
|
## Installation
|
|
29
38
|
|
|
@@ -31,13 +40,6 @@ The `@memberjunction/ng-chat` package provides a feature-rich chat component wit
|
|
|
31
40
|
npm install @memberjunction/ng-chat
|
|
32
41
|
```
|
|
33
42
|
|
|
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
|
-
|
|
41
43
|
## Usage
|
|
42
44
|
|
|
43
45
|
### Import the Module
|
|
@@ -46,16 +48,12 @@ This package requires the following peer dependencies:
|
|
|
46
48
|
import { ChatModule } from '@memberjunction/ng-chat';
|
|
47
49
|
|
|
48
50
|
@NgModule({
|
|
49
|
-
imports: [
|
|
50
|
-
ChatModule,
|
|
51
|
-
// other imports
|
|
52
|
-
],
|
|
53
|
-
// ...
|
|
51
|
+
imports: [ChatModule]
|
|
54
52
|
})
|
|
55
53
|
export class YourModule { }
|
|
56
54
|
```
|
|
57
55
|
|
|
58
|
-
### Basic
|
|
56
|
+
### Basic Usage
|
|
59
57
|
|
|
60
58
|
```html
|
|
61
59
|
<mj-chat
|
|
@@ -79,10 +77,9 @@ import { ChatComponent, ChatMessage, ChatWelcomeQuestion } from '@memberjunction
|
|
|
79
77
|
#chatComponent
|
|
80
78
|
[AIImageURL]="'assets/bot-avatar.png'"
|
|
81
79
|
[AILargeImageURL]="'assets/bot-large.png'"
|
|
82
|
-
[InitialMessage]="'Hi!
|
|
80
|
+
[InitialMessage]="'Hi! How can I help you today?'"
|
|
83
81
|
[WelcomeQuestions]="welcomeQuestions"
|
|
84
82
|
[Placeholder]="'Ask me anything...'"
|
|
85
|
-
[ClearAllMessagesPrompt]="'Are you sure you want to clear this conversation?'"
|
|
86
83
|
(MessageAdded)="handleNewMessage($event)"
|
|
87
84
|
(ClearChatRequested)="handleClearChat()">
|
|
88
85
|
</mj-chat>
|
|
@@ -90,107 +87,36 @@ import { ChatComponent, ChatMessage, ChatWelcomeQuestion } from '@memberjunction
|
|
|
90
87
|
})
|
|
91
88
|
export class AIAssistantComponent {
|
|
92
89
|
@ViewChild('chatComponent') chatComponent!: ChatComponent;
|
|
93
|
-
|
|
90
|
+
|
|
94
91
|
welcomeQuestions: ChatWelcomeQuestion[] = [
|
|
95
|
-
{
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
topLine: 'Find information',
|
|
102
|
-
bottomLine: 'Search for customer details',
|
|
103
|
-
prompt: 'Find information about customer XYZ'
|
|
104
|
-
},
|
|
105
|
-
{
|
|
106
|
-
topLine: 'Summarize data',
|
|
107
|
-
bottomLine: 'Give me the key points from the data',
|
|
108
|
-
prompt: 'Summarize the key metrics from my dashboard'
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
topLine: 'Help with a task',
|
|
112
|
-
bottomLine: 'Walk me through creating a new entity',
|
|
113
|
-
prompt: 'Help me create a new entity in MemberJunction'
|
|
114
|
-
}
|
|
92
|
+
{ topLine: 'Generate a report', bottomLine: 'Create a sales summary', prompt: 'Generate a sales report for Q2' },
|
|
93
|
+
{ topLine: 'Find information', bottomLine: 'Search customer details', prompt: 'Find customer XYZ' },
|
|
94
|
+
{ topLine: 'Summarize data', bottomLine: 'Key metrics overview', prompt: 'Summarize dashboard metrics' },
|
|
95
|
+
{ topLine: 'Help with a task', bottomLine: 'Step-by-step guidance', prompt: 'Help me create a new entity' }
|
|
115
96
|
];
|
|
116
97
|
|
|
117
|
-
handleNewMessage(message: ChatMessage) {
|
|
118
|
-
// Process the new message
|
|
119
|
-
console.log('New message:', message);
|
|
120
|
-
|
|
98
|
+
async handleNewMessage(message: ChatMessage) {
|
|
121
99
|
if (message.senderType === 'user') {
|
|
122
|
-
|
|
123
|
-
|
|
100
|
+
this.chatComponent.ShowWaitingIndicator = true;
|
|
101
|
+
try {
|
|
102
|
+
const response = await this.aiService.getResponse(message.message);
|
|
103
|
+
this.chatComponent.SendMessage(response, 'Assistant', 'ai', null);
|
|
104
|
+
} finally {
|
|
105
|
+
this.chatComponent.ShowWaitingIndicator = false;
|
|
106
|
+
}
|
|
124
107
|
}
|
|
125
108
|
}
|
|
126
109
|
|
|
127
110
|
handleClearChat() {
|
|
128
|
-
// Clear any conversation state in your service
|
|
129
|
-
this.conversationHistory = [];
|
|
130
111
|
this.chatComponent.ClearAllMessages();
|
|
131
112
|
}
|
|
132
|
-
|
|
133
|
-
async processUserMessage(message: string) {
|
|
134
|
-
// Set loading state
|
|
135
|
-
this.chatComponent.ShowWaitingIndicator = true;
|
|
136
|
-
|
|
137
|
-
try {
|
|
138
|
-
// Call your AI or chat service
|
|
139
|
-
const response = await this.aiService.getResponse(message);
|
|
140
|
-
|
|
141
|
-
// Add AI response to chat
|
|
142
|
-
this.chatComponent.SendMessage(
|
|
143
|
-
response,
|
|
144
|
-
'Assistant',
|
|
145
|
-
'ai',
|
|
146
|
-
null
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
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
|
-
);
|
|
158
|
-
}
|
|
159
|
-
finally {
|
|
160
|
-
// End loading state
|
|
161
|
-
this.chatComponent.ShowWaitingIndicator = false;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
113
|
}
|
|
165
114
|
```
|
|
166
115
|
|
|
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
|
-
|
|
191
116
|
## API Reference
|
|
192
117
|
|
|
193
118
|
### Component Selector
|
|
119
|
+
|
|
194
120
|
`<mj-chat></mj-chat>`
|
|
195
121
|
|
|
196
122
|
### Inputs
|
|
@@ -211,173 +137,71 @@ this.chatComponent.SendMessage(
|
|
|
211
137
|
|
|
212
138
|
| Event | Type | Description |
|
|
213
139
|
|-------|------|-------------|
|
|
214
|
-
| `MessageAdded` | `EventEmitter<ChatMessage>` | Emitted when a new message is added
|
|
215
|
-
| `ClearChatRequested` | `EventEmitter<void>` | Emitted when user confirms clearing
|
|
140
|
+
| `MessageAdded` | `EventEmitter<ChatMessage>` | Emitted when a new message is added |
|
|
141
|
+
| `ClearChatRequested` | `EventEmitter<void>` | Emitted when user confirms clearing |
|
|
216
142
|
|
|
217
143
|
### Public Methods
|
|
218
144
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
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.
|
|
145
|
+
| Method | Parameters | Description |
|
|
146
|
+
|--------|------------|-------------|
|
|
147
|
+
| `SendCurrentMessage()` | none | Sends the current input field content |
|
|
148
|
+
| `SendMessage()` | `message, senderName, senderType, id, fireEvent?` | Adds a message programmatically |
|
|
149
|
+
| `SendUserMessage()` | `message: string` | Convenience method for user messages |
|
|
150
|
+
| `ClearAllMessages()` | none | Clears all messages and resets to initial state |
|
|
151
|
+
| `HandleClearChat()` | none | Shows clear confirmation dialog |
|
|
240
152
|
|
|
241
153
|
### Classes
|
|
242
154
|
|
|
243
|
-
#### ChatMessage
|
|
244
|
-
|
|
245
155
|
```typescript
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
constructor(
|
|
253
|
-
message: string,
|
|
254
|
-
senderName: string,
|
|
255
|
-
senderType: 'user' | 'ai',
|
|
256
|
-
id: any = null
|
|
257
|
-
);
|
|
156
|
+
class ChatMessage {
|
|
157
|
+
message: string;
|
|
158
|
+
senderName: string;
|
|
159
|
+
senderType: 'user' | 'ai';
|
|
160
|
+
id?: unknown;
|
|
258
161
|
}
|
|
259
|
-
```
|
|
260
162
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
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
|
|
163
|
+
class ChatWelcomeQuestion {
|
|
164
|
+
topLine: string;
|
|
165
|
+
bottomLine: string;
|
|
166
|
+
prompt: string;
|
|
268
167
|
}
|
|
269
168
|
```
|
|
270
169
|
|
|
271
170
|
## Styling
|
|
272
171
|
|
|
273
|
-
|
|
274
|
-
|
|
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
|
|
281
|
-
|
|
282
|
-
You can override these styles in your global styles or component styles:
|
|
172
|
+
Key CSS classes for customization:
|
|
283
173
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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
|
-
```
|
|
174
|
+
- `.chat-message-wrap` -- Container for each message
|
|
175
|
+
- `.chat-message` / `.chat-message-ai` -- Message content styling
|
|
176
|
+
- `.chat-message-image` -- Avatar container
|
|
177
|
+
- `.chat-input-container` -- Input area container
|
|
178
|
+
- `.chat-welcome-container` -- Welcome screen container
|
|
296
179
|
|
|
297
180
|
## Dependencies
|
|
298
181
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
-
|
|
307
|
-
|
|
308
|
-
### Integration with MemberJunction
|
|
182
|
+
| Package | Description |
|
|
183
|
+
|---------|-------------|
|
|
184
|
+
| `@memberjunction/core` | Core utilities |
|
|
185
|
+
| `@memberjunction/ng-container-directives` | Container directives |
|
|
186
|
+
| `@memberjunction/ng-shared-generic` | Shared generic components |
|
|
187
|
+
| `@memberjunction/ng-markdown` | Markdown rendering |
|
|
188
|
+
| `@progress/kendo-angular-indicators` | Loading spinner |
|
|
189
|
+
| `@progress/kendo-angular-buttons` | Button components |
|
|
190
|
+
| `@progress/kendo-angular-dialog` | Dialog for confirmations |
|
|
309
191
|
|
|
310
|
-
|
|
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
|
|
192
|
+
### Peer Dependencies
|
|
315
193
|
|
|
316
|
-
|
|
194
|
+
- `@angular/common` ^21.x
|
|
195
|
+
- `@angular/core` ^21.x
|
|
196
|
+
- `@angular/forms` ^21.x
|
|
317
197
|
|
|
318
|
-
|
|
198
|
+
## Build
|
|
319
199
|
|
|
320
200
|
```bash
|
|
321
|
-
|
|
201
|
+
cd packages/Angular/Generic/chat
|
|
322
202
|
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
203
|
```
|
|
376
204
|
|
|
377
|
-
##
|
|
205
|
+
## License
|
|
378
206
|
|
|
379
|
-
|
|
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
|
|
207
|
+
ISC
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ng-chat",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.2.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,14 +24,14 @@
|
|
|
24
24
|
"@angular/forms": "21.1.3"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@memberjunction/core": "4.
|
|
28
|
-
"@memberjunction/ng-container-directives": "4.
|
|
29
|
-
"@memberjunction/ng-shared-generic": "4.
|
|
27
|
+
"@memberjunction/core": "4.2.0",
|
|
28
|
+
"@memberjunction/ng-container-directives": "4.2.0",
|
|
29
|
+
"@memberjunction/ng-shared-generic": "4.2.0",
|
|
30
30
|
"@progress/kendo-angular-indicators": "22.0.1",
|
|
31
31
|
"@progress/kendo-angular-buttons": "22.0.1",
|
|
32
32
|
"@progress/kendo-angular-dialog": "22.0.1",
|
|
33
33
|
"tslib": "^2.8.1",
|
|
34
|
-
"@memberjunction/ng-markdown": "4.
|
|
34
|
+
"@memberjunction/ng-markdown": "4.2.0"
|
|
35
35
|
},
|
|
36
36
|
"sideEffects": false,
|
|
37
37
|
"repository": {
|