@nanahoshi/mona-ui 0.43.1 → 0.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/chat/package.json +4 -0
- package/docs/api/chat.json +1 -0
- package/docs/api/chat.md +212 -0
- package/docs/api/i18n.json +1 -1
- package/docs/api/i18n.md +34 -0
- package/docs/api/index.json +1 -1
- package/docs/api/locales.json +1 -1
- package/docs/api/locales.md +14 -14
- package/docs/api/timeline.json +1 -0
- package/docs/api/timeline.md +188 -0
- package/docs/chat.md +480 -0
- package/docs/manifest.json +1 -1
- package/docs/timeline.md +482 -0
- package/fesm2022/nanahoshi-mona-ui-chat.mjs +1230 -0
- package/fesm2022/nanahoshi-mona-ui-expansion-panel.mjs +1 -6
- package/fesm2022/nanahoshi-mona-ui-i18n-icu.mjs +37 -0
- package/fesm2022/nanahoshi-mona-ui-internal.mjs +6 -1
- package/fesm2022/nanahoshi-mona-ui-locales-ar-sa.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-de-de.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-es-es.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-fr-fr.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-id-id.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-it-it.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ja-jp.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ko-kr.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-pl-pl.mjs +29 -0
- package/fesm2022/nanahoshi-mona-ui-locales-pt-br.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-ru-ru.mjs +29 -0
- package/fesm2022/nanahoshi-mona-ui-locales-tr-tr.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-zh-cn.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-locales-zh-tw.mjs +19 -0
- package/fesm2022/nanahoshi-mona-ui-timeline.mjs +1028 -0
- package/i18n/catalogs/en-US.messages.json +19 -0
- package/llms.txt +8 -1
- package/locales/ar-sa/ar-sa.messages.json +19 -0
- package/locales/de-de/de-de.messages.json +19 -0
- package/locales/es-es/es-es.messages.json +19 -0
- package/locales/fr-fr/fr-fr.messages.json +19 -0
- package/locales/id-id/id-id.messages.json +19 -0
- package/locales/it-it/it-it.messages.json +19 -0
- package/locales/ja-jp/ja-jp.messages.json +19 -0
- package/locales/ko-kr/ko-kr.messages.json +19 -0
- package/locales/pl-pl/pl-pl.messages.json +19 -0
- package/locales/pt-br/pt-br.messages.json +19 -0
- package/locales/ru-ru/ru-ru.messages.json +19 -0
- package/locales/tr-tr/tr-tr.messages.json +19 -0
- package/locales/zh-cn/zh-cn.messages.json +19 -0
- package/locales/zh-tw/zh-tw.messages.json +19 -0
- package/package.json +9 -1
- package/timeline/package.json +4 -0
- package/types/nanahoshi-mona-ui-chat.d.ts +424 -0
- package/types/nanahoshi-mona-ui-i18n.d.ts +43 -0
- package/types/nanahoshi-mona-ui-internal.d.ts +4 -0
- package/types/nanahoshi-mona-ui-locales-ar-sa.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-de-de.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-es-es.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-fr-fr.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-id-id.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-it-it.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-ja-jp.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-ko-kr.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-pl-pl.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-pt-br.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-ru-ru.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-tr-tr.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-zh-cn.d.ts +19 -0
- package/types/nanahoshi-mona-ui-locales-zh-tw.d.ts +19 -0
- package/types/nanahoshi-mona-ui-timeline.d.ts +338 -0
package/docs/chat.md
ADDED
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
# Chat
|
|
2
|
+
|
|
3
|
+
> A conversation with people or an assistant: message bubbles by day and author, a message box that sends files, and replies that stream in.
|
|
4
|
+
|
|
5
|
+
Entry point: `@nanahoshi/mona-ui/chat`
|
|
6
|
+
|
|
7
|
+
**Selector:** `mona-chat`
|
|
8
|
+
|
|
9
|
+
`ChatComponent` shows a conversation, with people or with an assistant: its messages in bubbles, grouped by day and by author, the user's own at the end and others' at the start beside their avatar. Below them, a message box sends with Enter, takes files, and stops a reply while it streams in. Suggested replies and who's typing sit between the two.
|
|
10
|
+
|
|
11
|
+
You keep the messages. The chat emits what the user sends, and shows what you give it, so a message appears once you add it.
|
|
12
|
+
|
|
13
|
+
## Overview
|
|
14
|
+
|
|
15
|
+
Give the chat the user and the messages, and add a message to them whenever the user sends one or a reply comes in. The chat lays them out, keeps the newest in view, and tells screen readers about each new one.
|
|
16
|
+
|
|
17
|
+
**Use Chat when:**
|
|
18
|
+
|
|
19
|
+
- People write to each other in turns: support conversations, comments on a task, direct messages.
|
|
20
|
+
- Users ask an assistant questions and read its answers as they stream in.
|
|
21
|
+
|
|
22
|
+
For a list of comments that aren't a back-and-forth, use a [list view](list-view.md). For short status messages that come and go, use a [notification](notification.md).
|
|
23
|
+
|
|
24
|
+
## Import & Quick Start
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { ChatComponent } from "@nanahoshi/mona-ui/chat";
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
```html
|
|
31
|
+
<mona-chat aria-label="Chat with Ana Silva" [messages]="messages()" [user]="me" (send)="onSend($event)" />
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
protected readonly ana: ChatUser = { id: "ana", name: "Ana Silva" };
|
|
36
|
+
protected readonly me: ChatUser = { id: "me", name: "Sam Lee" };
|
|
37
|
+
protected readonly messages = signal<readonly ChatMessage[]>([
|
|
38
|
+
{ author: this.ana, id: 1, text: "Lunch on Friday?", timestamp: new Date(2026, 10, 20, 12, 0) }
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
protected onSend(event: ChatSendEvent): void {
|
|
42
|
+
this.messages.update(messages => [
|
|
43
|
+
...messages,
|
|
44
|
+
{ author: this.me, id: crypto.randomUUID(), text: event.text, timestamp: new Date() }
|
|
45
|
+
]);
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
A message whose author has the user's `id` is the user's own. Give each message an `id` too, so a copy that replaces it, as when its status changes or a reply streams in, stays the same message.
|
|
50
|
+
|
|
51
|
+
## Anatomy
|
|
52
|
+
|
|
53
|
+
| Directive | Selector | Replaces |
|
|
54
|
+
| --- | --- | --- |
|
|
55
|
+
| `ChatMessageTemplateDirective` | `ng-template[monaChatMessageTemplate]` | A message's text, inside its bubble |
|
|
56
|
+
| `ChatAttachmentTemplateDirective` | `ng-template[monaChatAttachmentTemplate]` | A file's picture, name and size |
|
|
57
|
+
| `ChatEmptyTemplateDirective` | `ng-template[monaChatEmptyTemplate]` | "No messages yet" |
|
|
58
|
+
|
|
59
|
+
The message template's context is the message (`let-message`) and whether it's the user's own (`let-mine="mine"`). The attachment template's is the file (`let-attachment`), its message (`let-message="message"`) and `mine`. A message's files, its status and the caret of a reply that's streaming stay where they are around a message template.
|
|
60
|
+
|
|
61
|
+
Each message is a `ChatMessage`:
|
|
62
|
+
|
|
63
|
+
| Field | Shows |
|
|
64
|
+
| --- | --- |
|
|
65
|
+
| `author` | Who sent it: a `ChatUser`, with an `id`, a `name` and an optional `avatarUrl` |
|
|
66
|
+
| `text` | What it says, with its line breaks |
|
|
67
|
+
| `timestamp` | When it was sent: the day above the day's first message, and the time under each run |
|
|
68
|
+
| `attachments` | Files sent with it, each with a `name`, and an optional `size`, `type` and `url` |
|
|
69
|
+
| `status` | For the user's own: `"sending"`, `"sent"` or `"failed"` |
|
|
70
|
+
| `streaming` | Whether its text is still coming in |
|
|
71
|
+
|
|
72
|
+
## Examples
|
|
73
|
+
|
|
74
|
+
### A conversation
|
|
75
|
+
|
|
76
|
+
Ana answers each message after a moment, and the chat shows that she's typing while she does. Messages from the same person within five minutes of each other share a run: one name and avatar beside them, and one time below.
|
|
77
|
+
|
|
78
|
+
`basic-example.component.ts`
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { Component, DestroyRef, inject, signal } from "@angular/core";
|
|
82
|
+
import { type ChatMessage, type ChatSendEvent, type ChatUser, ChatComponent } from "@nanahoshi/mona-ui/chat";
|
|
83
|
+
|
|
84
|
+
function minutesAgo(minutes: number): Date {
|
|
85
|
+
return new Date(Date.now() - minutes * 60_000);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const REPLIES = ["Nice, thanks!", "Sounds good to me.", "On it.", "Let's talk at the stand-up."];
|
|
89
|
+
|
|
90
|
+
@Component({
|
|
91
|
+
selector: "app-chat-basic-example",
|
|
92
|
+
imports: [ChatComponent],
|
|
93
|
+
templateUrl: "./basic-example.component.html"
|
|
94
|
+
})
|
|
95
|
+
export class ChatBasicExampleComponent {
|
|
96
|
+
readonly #timers: ReturnType<typeof setTimeout>[] = [];
|
|
97
|
+
#nextId = 5;
|
|
98
|
+
#nextReply = 0;
|
|
99
|
+
protected readonly ana: ChatUser = { id: "ana", name: "Ana Silva" };
|
|
100
|
+
protected readonly me: ChatUser = { id: "me", name: "Sam Lee" };
|
|
101
|
+
protected readonly messages = signal<readonly ChatMessage[]>([
|
|
102
|
+
{ author: this.ana, id: 1, text: "Did the release go out?", timestamp: minutesAgo(25 * 60) },
|
|
103
|
+
{ author: this.me, id: 2, text: "Not yet, the last test is flaky.", timestamp: minutesAgo(25 * 60 - 3) },
|
|
104
|
+
{ author: this.ana, id: 3, text: "Morning! Any luck with that test?", timestamp: minutesAgo(40) },
|
|
105
|
+
{ author: this.ana, id: 4, text: "I can pair on it after lunch.", timestamp: minutesAgo(39) }
|
|
106
|
+
]);
|
|
107
|
+
protected readonly typing = signal<readonly ChatUser[]>([]);
|
|
108
|
+
|
|
109
|
+
public constructor() {
|
|
110
|
+
inject(DestroyRef).onDestroy(() => this.#timers.forEach(timer => clearTimeout(timer)));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
protected onSend(event: ChatSendEvent): void {
|
|
114
|
+
this.messages.update(messages => [
|
|
115
|
+
...messages,
|
|
116
|
+
{ author: this.me, id: this.#nextId++, text: event.text, timestamp: new Date() }
|
|
117
|
+
]);
|
|
118
|
+
// Ana types for a moment, then answers.
|
|
119
|
+
this.#timers.push(
|
|
120
|
+
setTimeout(() => this.typing.set([this.ana]), 500),
|
|
121
|
+
setTimeout(() => {
|
|
122
|
+
this.typing.set([]);
|
|
123
|
+
const text = REPLIES[this.#nextReply++ % REPLIES.length];
|
|
124
|
+
this.messages.update(messages => [
|
|
125
|
+
...messages,
|
|
126
|
+
{ author: this.ana, id: this.#nextId++, text, timestamp: new Date() }
|
|
127
|
+
]);
|
|
128
|
+
}, 2000)
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`basic-example.component.html`
|
|
135
|
+
|
|
136
|
+
```html
|
|
137
|
+
<mona-chat
|
|
138
|
+
aria-label="Chat with Ana Silva"
|
|
139
|
+
class="h-112 max-w-xl"
|
|
140
|
+
[messages]="messages()"
|
|
141
|
+
[typing]="typing()"
|
|
142
|
+
[user]="me"
|
|
143
|
+
(send)="onSend($event)" />
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Files and failed messages
|
|
147
|
+
|
|
148
|
+
With `allowFiles`, the paperclip button attaches files, and `accept` limits which ones the picker offers. The files go out with the message in `send`. This example shows the message as sending, then as sent, or as not sent when a file is over 1 MB. Retry emits `retry` with the message, to send it again.
|
|
149
|
+
|
|
150
|
+
`files-example.component.ts`
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
import { Component, DestroyRef, inject, signal } from "@angular/core";
|
|
154
|
+
import { type ChatMessage, type ChatSendEvent, type ChatUser, ChatComponent } from "@nanahoshi/mona-ui/chat";
|
|
155
|
+
|
|
156
|
+
/** Files over this size "fail" to send the first time, to show the Retry button. */
|
|
157
|
+
const SIZE_LIMIT = 1024 * 1024;
|
|
158
|
+
|
|
159
|
+
const MOCKUP = `data:image/svg+xml,${encodeURIComponent(
|
|
160
|
+
'<svg xmlns="http://www.w3.org/2000/svg" width="320" height="180"><rect width="320" height="180" fill="#dbe4f0"/>' +
|
|
161
|
+
'<rect x="24" y="24" width="120" height="16" rx="4" fill="#7b93b5"/><rect x="24" y="56" width="272" height="100" ' +
|
|
162
|
+
'rx="8" fill="#fff"/></svg>'
|
|
163
|
+
)}`;
|
|
164
|
+
|
|
165
|
+
@Component({
|
|
166
|
+
selector: "app-chat-files-example",
|
|
167
|
+
imports: [ChatComponent],
|
|
168
|
+
templateUrl: "./files-example.component.html"
|
|
169
|
+
})
|
|
170
|
+
export class ChatFilesExampleComponent {
|
|
171
|
+
readonly #timers: ReturnType<typeof setTimeout>[] = [];
|
|
172
|
+
readonly #urls: string[] = [];
|
|
173
|
+
#nextId = 3;
|
|
174
|
+
protected readonly kenji: ChatUser = { id: "kenji", name: "Kenji Mori" };
|
|
175
|
+
protected readonly me: ChatUser = { id: "me", name: "Sam Lee" };
|
|
176
|
+
protected readonly messages = signal<readonly ChatMessage[]>([
|
|
177
|
+
{
|
|
178
|
+
attachments: [{ name: "settings-mockup.svg", size: 1840, type: "image/svg+xml", url: MOCKUP }],
|
|
179
|
+
author: this.kenji,
|
|
180
|
+
id: 1,
|
|
181
|
+
text: "Here's the new settings page.",
|
|
182
|
+
timestamp: new Date(Date.now() - 10 * 60_000)
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
author: this.kenji,
|
|
186
|
+
id: 2,
|
|
187
|
+
text: "Send me the spec when you have it. Anything over 1 MB fails the first time here.",
|
|
188
|
+
timestamp: new Date(Date.now() - 9 * 60_000)
|
|
189
|
+
}
|
|
190
|
+
]);
|
|
191
|
+
|
|
192
|
+
public constructor() {
|
|
193
|
+
inject(DestroyRef).onDestroy(() => {
|
|
194
|
+
this.#timers.forEach(timer => clearTimeout(timer));
|
|
195
|
+
this.#urls.forEach(url => URL.revokeObjectURL(url));
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
protected onRetry(message: ChatMessage): void {
|
|
200
|
+
this.#deliver(message.id, "sent");
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
protected onSend(event: ChatSendEvent): void {
|
|
204
|
+
const id = this.#nextId++;
|
|
205
|
+
const attachments = event.files.map(file => {
|
|
206
|
+
const url = URL.createObjectURL(file);
|
|
207
|
+
this.#urls.push(url);
|
|
208
|
+
return { name: file.name, size: file.size, type: file.type, url };
|
|
209
|
+
});
|
|
210
|
+
this.messages.update(messages => [
|
|
211
|
+
...messages,
|
|
212
|
+
{ attachments, author: this.me, id, status: "sending", text: event.text, timestamp: new Date() }
|
|
213
|
+
]);
|
|
214
|
+
const tooLarge = event.files.some(file => file.size > SIZE_LIMIT);
|
|
215
|
+
this.#deliver(id, tooLarge ? "failed" : "sent");
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Shows the message as sending for a moment, then as sent or failed. */
|
|
219
|
+
#deliver(id: unknown, status: "failed" | "sent"): void {
|
|
220
|
+
this.#setStatus(id, "sending");
|
|
221
|
+
this.#timers.push(setTimeout(() => this.#setStatus(id, status), 900));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
#setStatus(id: unknown, status: ChatMessage["status"]): void {
|
|
225
|
+
this.messages.update(messages =>
|
|
226
|
+
messages.map(message => (message.id === id ? { ...message, status } : message))
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
`files-example.component.html`
|
|
233
|
+
|
|
234
|
+
```html
|
|
235
|
+
<mona-chat
|
|
236
|
+
accept="image/*,.pdf,.txt"
|
|
237
|
+
aria-label="Chat with Kenji Mori"
|
|
238
|
+
class="h-112 max-w-xl"
|
|
239
|
+
[allowFiles]="true"
|
|
240
|
+
[messages]="messages()"
|
|
241
|
+
[user]="me"
|
|
242
|
+
(retry)="onRetry($event)"
|
|
243
|
+
(send)="onSend($event)" />
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
### An assistant
|
|
247
|
+
|
|
248
|
+
The empty template greets the user, and the suggested replies give them questions to start with. Each answer streams in a word at a time, with a caret at its end and Stop in place of Send. The message template shows lines that start with "- " as a list.
|
|
249
|
+
|
|
250
|
+
`assistant-example.component.ts`
|
|
251
|
+
|
|
252
|
+
```typescript
|
|
253
|
+
import { Component, computed, DestroyRef, inject, signal } from "@angular/core";
|
|
254
|
+
import {
|
|
255
|
+
type ChatMessage,
|
|
256
|
+
type ChatSendEvent,
|
|
257
|
+
type ChatUser,
|
|
258
|
+
ChatComponent,
|
|
259
|
+
ChatEmptyTemplateDirective,
|
|
260
|
+
ChatMessageTemplateDirective
|
|
261
|
+
} from "@nanahoshi/mona-ui/chat";
|
|
262
|
+
|
|
263
|
+
/** A paragraph, or the lines of a list, of a reply's text. */
|
|
264
|
+
type Block =
|
|
265
|
+
{ readonly kind: "list"; readonly items: readonly string[] } | { readonly kind: "text"; readonly text: string };
|
|
266
|
+
|
|
267
|
+
const ANSWERS: Readonly<Record<string, string>> = {
|
|
268
|
+
"Which components work in forms?":
|
|
269
|
+
"Every input works with reactive forms and template-driven forms:\n- Text box, text area and masked text box\n- Drop-downs, combo box and multi-select\n- Date, time and color pickers",
|
|
270
|
+
"How do I show a chart?":
|
|
271
|
+
"Add a chart with one series:\n- Import ChartComponent and a series, such as the line series\n- Give the series your data and a field\n- Size the chart with a class"
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
const FALLBACK =
|
|
275
|
+
"I'm a pretend assistant, so I only know two answers. Try one of the suggestions:\n- Which components work in forms?\n- How do I show a chart?";
|
|
276
|
+
|
|
277
|
+
@Component({
|
|
278
|
+
selector: "app-chat-assistant-example",
|
|
279
|
+
imports: [ChatComponent, ChatEmptyTemplateDirective, ChatMessageTemplateDirective],
|
|
280
|
+
templateUrl: "./assistant-example.component.html"
|
|
281
|
+
})
|
|
282
|
+
export class ChatAssistantExampleComponent {
|
|
283
|
+
#nextId = 1;
|
|
284
|
+
#stream: ReturnType<typeof setInterval> | null = null;
|
|
285
|
+
protected readonly assistant: ChatUser = { id: "assistant", name: "Docs Assistant" };
|
|
286
|
+
protected readonly me: ChatUser = { id: "me", name: "Sam Lee" };
|
|
287
|
+
protected readonly messages = signal<readonly ChatMessage[]>([]);
|
|
288
|
+
/** Starter questions in an empty chat, and one follow-up after that. */
|
|
289
|
+
protected readonly suggestions = computed(() =>
|
|
290
|
+
this.messages().length === 0 ? Object.keys(ANSWERS) : ["Tell me something else"]
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
public constructor() {
|
|
294
|
+
inject(DestroyRef).onDestroy(() => this.onStop());
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Splits a reply into paragraphs and lists, where each line starting with "- " is an item. */
|
|
298
|
+
protected blocks(text: string): Block[] {
|
|
299
|
+
const blocks: Block[] = [];
|
|
300
|
+
for (const line of text.split("\n")) {
|
|
301
|
+
const last = blocks.at(-1);
|
|
302
|
+
if (line.startsWith("- ")) {
|
|
303
|
+
if (last?.kind === "list") {
|
|
304
|
+
blocks[blocks.length - 1] = { items: [...last.items, line.slice(2)], kind: "list" };
|
|
305
|
+
} else {
|
|
306
|
+
blocks.push({ items: [line.slice(2)], kind: "list" });
|
|
307
|
+
}
|
|
308
|
+
} else if (line.trim()) {
|
|
309
|
+
blocks.push({ kind: "text", text: line });
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return blocks;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
protected onSend(event: ChatSendEvent): void {
|
|
316
|
+
this.messages.update(messages => [
|
|
317
|
+
...messages,
|
|
318
|
+
{ author: this.me, id: this.#nextId++, text: event.text, timestamp: new Date() }
|
|
319
|
+
]);
|
|
320
|
+
this.#streamReply(ANSWERS[event.text] ?? FALLBACK);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Stops the reply where it got to. */
|
|
324
|
+
protected onStop(): void {
|
|
325
|
+
if (this.#stream != null) {
|
|
326
|
+
clearInterval(this.#stream);
|
|
327
|
+
this.#stream = null;
|
|
328
|
+
}
|
|
329
|
+
this.messages.update(messages =>
|
|
330
|
+
messages.map(message => (message.streaming ? { ...message, streaming: false } : message))
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Streams the reply in a word at a time, as a model's tokens arrive. */
|
|
335
|
+
#streamReply(reply: string): void {
|
|
336
|
+
const id = this.#nextId++;
|
|
337
|
+
const words = reply.split(/(?<=\s)/);
|
|
338
|
+
let shown = 0;
|
|
339
|
+
this.messages.update(messages => [
|
|
340
|
+
...messages,
|
|
341
|
+
{ author: this.assistant, id, streaming: true, text: "", timestamp: new Date() }
|
|
342
|
+
]);
|
|
343
|
+
this.#stream = setInterval(() => {
|
|
344
|
+
shown++;
|
|
345
|
+
const done = shown >= words.length;
|
|
346
|
+
const text = words.slice(0, shown).join("");
|
|
347
|
+
this.messages.update(messages =>
|
|
348
|
+
messages.map(message => (message.id === id ? { ...message, streaming: !done, text } : message))
|
|
349
|
+
);
|
|
350
|
+
if (done) {
|
|
351
|
+
this.onStop();
|
|
352
|
+
}
|
|
353
|
+
}, 60);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
`assistant-example.component.html`
|
|
359
|
+
|
|
360
|
+
```html
|
|
361
|
+
<mona-chat
|
|
362
|
+
aria-label="Docs assistant"
|
|
363
|
+
placeholder="Ask about a component"
|
|
364
|
+
class="h-112 max-w-xl"
|
|
365
|
+
[messages]="messages()"
|
|
366
|
+
[suggestions]="suggestions()"
|
|
367
|
+
[user]="me"
|
|
368
|
+
(send)="onSend($event)"
|
|
369
|
+
(stop)="onStop()">
|
|
370
|
+
<ng-template monaChatEmptyTemplate>
|
|
371
|
+
<p class="font-medium text-foreground">Hi, I'm the docs assistant.</p>
|
|
372
|
+
<p>Ask about a component, or pick a question below.</p>
|
|
373
|
+
</ng-template>
|
|
374
|
+
<ng-template monaChatMessageTemplate let-message>
|
|
375
|
+
@for (block of blocks(message.text ?? ""); track $index) {
|
|
376
|
+
@if (block.kind === "list") {
|
|
377
|
+
<ul class="my-1 list-disc ps-5">
|
|
378
|
+
@for (item of block.items; track $index) {
|
|
379
|
+
<li>{{ item }}</li>
|
|
380
|
+
}
|
|
381
|
+
</ul>
|
|
382
|
+
} @else {
|
|
383
|
+
<p class="whitespace-pre-wrap">{{ block.text }}</p>
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
</ng-template>
|
|
387
|
+
</mona-chat>
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
## Appearance & Styling
|
|
391
|
+
|
|
392
|
+
The chat has the surface and border of a card, and is 32rem tall by default. Set its height with a class, such as `class="h-96"`. The messages scroll inside it, and the message box stays at the bottom.
|
|
393
|
+
|
|
394
|
+
The user's bubbles are in the primary color, and others' in the muted surface color. Each run is at most 36rem wide, or 85% of the chat's width when that's narrower. A message that's sending is faded, and one that failed has "Not sent" and Retry under it in the error color.
|
|
395
|
+
|
|
396
|
+
In forced colors, each bubble has a border in the system's text color, as its fill isn't painted, and the caret and the typing dots are drawn in the text color too. For users who prefer reduced motion, the caret doesn't blink and the typing dots don't bounce.
|
|
397
|
+
|
|
398
|
+
## Behavior
|
|
399
|
+
|
|
400
|
+
### Sending
|
|
401
|
+
|
|
402
|
+
Enter sends the message, and Shift+Enter starts a new line. An Enter that picks a word in an input method's composition, as in Japanese or Chinese, doesn't send. The message box grows with its text, up to 15rem, then scrolls.
|
|
403
|
+
|
|
404
|
+
`send` emits the text, trimmed, and the files. A message needs text or a file: the Send button is disabled until it has one, and while the chat is `disabled`. After sending, the box is cleared and keeps focus. Pressing a suggested reply sends its text the same way.
|
|
405
|
+
|
|
406
|
+
`draft` is the text in the box. Bind it two-way, `[(draft)]`, to keep a draft for each conversation, or to tell others that the user is typing.
|
|
407
|
+
|
|
408
|
+
### Days and runs
|
|
409
|
+
|
|
410
|
+
The chat puts the day above its first message: "Today", "Yesterday", or the date, in the locale, with the year when it isn't this year. A run of one author's messages gets one time below it, formatted with `timeFormat`. A message without a `timestamp` joins the day and run before it.
|
|
411
|
+
|
|
412
|
+
### Scrolling
|
|
413
|
+
|
|
414
|
+
The chat opens at the newest message, and keeps to it as messages arrive and the message box grows, while the user is reading there. Once they've scrolled up, new messages from others don't move what they're reading: a New messages button appears, and takes them to the bottom. A message the user sends always takes them there.
|
|
415
|
+
|
|
416
|
+
### Streaming replies
|
|
417
|
+
|
|
418
|
+
While a message's `streaming` is `true`, it shows a caret at the end of its text, the Send button becomes Stop, and the suggested replies are hidden. Replace the message with a copy that has more text as each part comes in, and set `streaming` to `false` when it's done. Stop emits `stop`; end the reply there.
|
|
419
|
+
|
|
420
|
+
### Typing
|
|
421
|
+
|
|
422
|
+
`typing` holds the people typing. The chat names up to three, joined in the locale's way, as in "Ana and Ben are typing", and counts more, as in "4 people are typing". Leave the user out of it: the row is for others.
|
|
423
|
+
|
|
424
|
+
## Accessibility
|
|
425
|
+
|
|
426
|
+
The messages are a log, named by the chat's `aria-label`, or "Messages" in the locale. The log is a Tab stop, so keyboard users can scroll it with the arrow keys.
|
|
427
|
+
|
|
428
|
+
Screen readers hear each new message from someone else once, as "Ana Silva: Lunch on Friday?", politely, after what they're reading. A reply that streams in is announced once it's done, not word by word, which is why the log itself isn't live. The user's own messages and the ones shown at first aren't announced. Nor is the typing row, which changes often; it's there to read.
|
|
429
|
+
|
|
430
|
+
Each run starts with its author's name, which for the user's own is "You", read but not shown. Avatars are hidden from screen readers, as the name is beside them. A day and a time are each in a `<time>` element, with the date in ISO 8601 in its `datetime`.
|
|
431
|
+
|
|
432
|
+
The message box, the attach button, each file's remove button, Send and Stop are named in the locale. A picture sent as a file has an empty `alt`, as its name is below it; to describe pictures, give the chat an attachment template.
|
|
433
|
+
|
|
434
|
+
### Keyboard
|
|
435
|
+
|
|
436
|
+
| Key | Action |
|
|
437
|
+
| --- | --- |
|
|
438
|
+
| `Tab` / `Shift+Tab` | Moves focus through the messages, the Retry and New messages buttons, the suggested replies, and the message box and its buttons |
|
|
439
|
+
| `ArrowUp` / `ArrowDown` | Scrolls the focused messages |
|
|
440
|
+
| `Enter` | In the message box, sends the message; on a button, presses it |
|
|
441
|
+
| `Shift+Enter` | In the message box, starts a new line |
|
|
442
|
+
|
|
443
|
+
### ARIA
|
|
444
|
+
|
|
445
|
+
| Attribute | When present | Value |
|
|
446
|
+
| --- | --- | --- |
|
|
447
|
+
| `role` | On the messages | `"log"` |
|
|
448
|
+
| `aria-label` | On the messages | The chat's `aria-label`, or "Messages" in the locale |
|
|
449
|
+
| `aria-live` | On the messages | `"off"`: the chat announces new messages itself |
|
|
450
|
+
| `tabindex` | On the messages | `"0"` |
|
|
451
|
+
| `aria-busy` | On a message's bubble, while it streams | `"true"` |
|
|
452
|
+
| `role` / `aria-label` | On the suggested replies | `"group"` and "Suggested replies" in the locale |
|
|
453
|
+
| `aria-label` | On the message box, and the attach, remove, Send and Stop buttons | Their names in the locale |
|
|
454
|
+
| `aria-hidden` | On avatars, the caret and the typing dots | `"true"` |
|
|
455
|
+
|
|
456
|
+
## API
|
|
457
|
+
|
|
458
|
+
Everything below is imported from `@nanahoshi/mona-ui/chat`. Each name links to its reference page, with every input, output, property and method.
|
|
459
|
+
|
|
460
|
+
| Name | Kind | Description |
|
|
461
|
+
| --- | --- | --- |
|
|
462
|
+
| [`ChatComponent`](api/chat.md#chatcomponent) | Component | A conversation: its messages, by day, with the user's at the end and others' at the start beside their avatar, and a message box below them. |
|
|
463
|
+
| [`ChatAttachmentTemplateDirective`](api/chat.md#chatattachmenttemplatedirective) | Directive | Template for each file sent with a message, in place of its picture, name and size, such as a preview of a document. |
|
|
464
|
+
| [`ChatEmptyTemplateDirective`](api/chat.md#chatemptytemplatedirective) | Directive | Template for a chat with no messages, in place of "No messages yet", such as a greeting from an assistant. |
|
|
465
|
+
| [`ChatMessageTemplateDirective`](api/chat.md#chatmessagetemplatedirective) | Directive | Template for each message's content, inside its bubble, in place of its text, such as text rendered from Markdown. |
|
|
466
|
+
|
|
467
|
+
### Types
|
|
468
|
+
|
|
469
|
+
| Type | Kind | Description |
|
|
470
|
+
| --- | --- | --- |
|
|
471
|
+
| [`CHAT_DEFAULT_MESSAGES`](api/chat.md#chat-default-messages) | Constant | |
|
|
472
|
+
| [`ChatAttachment`](api/chat.md#chatattachment) | Interface | A file sent with a message. |
|
|
473
|
+
| [`ChatAttachmentTemplateContext`](api/chat.md#chatattachmenttemplatecontext) | Interface | The context of a `monaChatAttachmentTemplate`: the file, as `let-attachment`. |
|
|
474
|
+
| [`ChatMessage`](api/chat.md#chatmessage) | Interface | A message in a chat. |
|
|
475
|
+
| [`ChatMessageTemplateContext`](api/chat.md#chatmessagetemplatecontext) | Interface | The context of a `monaChatMessageTemplate`: the message, as `let-message`. |
|
|
476
|
+
| [`ChatSendEvent`](api/chat.md#chatsendevent) | Interface | What the user sends: their message's text, trimmed, and the files they attached to it. |
|
|
477
|
+
| [`ChatUser`](api/chat.md#chatuser) | Interface | Someone taking part in a chat: the user, or someone they're talking to, a person or an assistant. |
|
|
478
|
+
| [`ChatMessageStatus`](api/chat.md#chatmessagestatus) | Type alias | Where a message of the user's stands: `"sending"`, shown faded with "Sending" under it, `"sent"`, or `"failed"`, with "Not sent" and a Retry button under it. |
|
|
479
|
+
|
|
480
|
+
**Related types:** [`MonaChatMessages`](api/i18n.md#monachatmessages)
|