forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot-chat-assistant-message.ts
More file actions
498 lines (444 loc) · 15.6 KB
/
Copy pathcopilot-chat-assistant-message.ts
File metadata and controls
498 lines (444 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import {
Component,
TemplateRef,
ContentChild,
signal,
computed,
Type,
ChangeDetectionStrategy,
ViewEncapsulation,
Optional,
Inject,
input,
output,
} from "@angular/core";
import { CommonModule } from "@angular/common";
import { CopilotSlot } from "../../slots/copilot-slot";
import { CopilotChatToolCallsView } from "./copilot-chat-tool-calls-view";
import type { Message } from "@ag-ui/core";
import {
type AssistantMessage,
type CopilotChatAssistantMessageOnThumbsUpProps,
type CopilotChatAssistantMessageOnThumbsDownProps,
type CopilotChatAssistantMessageOnReadAloudProps,
type CopilotChatAssistantMessageOnRegenerateProps,
type AssistantMessageMarkdownRendererContext,
type AssistantMessageCopyButtonContext,
type ThumbsUpButtonContext,
type ThumbsDownButtonContext,
type ReadAloudButtonContext,
type RegenerateButtonContext,
type AssistantMessageToolbarContext,
} from "./copilot-chat-assistant-message.types";
import { CopilotChatAssistantMessageRenderer } from "./copilot-chat-assistant-message-renderer";
import {
CopilotChatAssistantMessageCopyButton,
CopilotChatAssistantMessageThumbsUpButton,
CopilotChatAssistantMessageThumbsDownButton,
} from "./copilot-chat-assistant-message-buttons";
import { CopilotChatAssistantMessageToolbar } from "./copilot-chat-assistant-message-toolbar";
import { cn } from "../../utils";
import { CopilotChatViewHandlers } from "./copilot-chat-view-handlers";
@Component({
standalone: true,
selector: "copilot-chat-assistant-message",
host: { "data-copilotkit": "" },
imports: [
CommonModule,
CopilotSlot,
CopilotChatAssistantMessageRenderer,
CopilotChatAssistantMessageCopyButton,
CopilotChatAssistantMessageToolbar,
CopilotChatToolCallsView,
],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
template: `
<div [class]="computedClass()" [attr.data-message-id]="message().id">
<!-- Markdown Renderer -->
@if (markdownRendererTemplate || markdownRendererComponent()) {
<copilot-slot
[slot]="markdownRendererTemplate || markdownRendererComponent()"
[context]="markdownRendererContext()"
[defaultComponent]="CopilotChatAssistantMessageRenderer"
>
</copilot-slot>
} @else {
<copilot-chat-assistant-message-renderer
[content]="message().content || ''"
[inputClass]="markdownRendererClass()"
>
</copilot-chat-assistant-message-renderer>
}
<!-- Tool Calls View -->
@if (toolCallsViewTemplate || toolCallsViewComponent()) {
<copilot-slot
[slot]="toolCallsViewTemplate || toolCallsViewComponent()"
[context]="toolCallsViewContext()"
[defaultComponent]="CopilotChatToolCallsView"
>
</copilot-slot>
} @else if (message().toolCalls && message()!.toolCalls!.length > 0) {
<copilot-chat-tool-calls-view
[message]="message()!"
[messages]="messages()"
[isLoading]="isLoading()"
>
</copilot-chat-tool-calls-view>
}
<!-- Toolbar: show only when there is assistant text content -->
@if (toolbarVisible() && hasMessageContent()) {
@if (toolbarTemplate || toolbarComponent()) {
<copilot-slot
[slot]="toolbarTemplate || toolbarComponent()"
[context]="toolbarContext()"
[defaultComponent]="CopilotChatAssistantMessageToolbar"
>
</copilot-slot>
} @else {
<div copilotChatAssistantMessageToolbar [inputClass]="toolbarClass()">
<div class="flex items-center gap-1">
<!-- Copy button -->
@if (copyButtonTemplate || copyButtonComponent()) {
<copilot-slot
[slot]="copyButtonTemplate || copyButtonComponent()"
[context]="{ content: message().content || '' }"
[defaultComponent]="CopilotChatAssistantMessageCopyButton"
[outputs]="copyButtonOutputs"
>
</copilot-slot>
} @else {
<copilot-chat-assistant-message-copy-button
[content]="message().content"
[inputClass]="copyButtonClass()"
(clicked)="handleCopy()"
>
</copilot-chat-assistant-message-copy-button>
}
<!-- Thumbs up button - show if custom slot provided OR if handler available at top level -->
@if (
thumbsUpButtonComponent() ||
thumbsUpButtonTemplate ||
handlers.hasAssistantThumbsUpHandler()
) {
<copilot-slot
[slot]="thumbsUpButtonTemplate || thumbsUpButtonComponent()"
[context]="{}"
[defaultComponent]="defaultThumbsUpButtonComponent"
[outputs]="thumbsUpButtonOutputs"
>
</copilot-slot>
}
<!-- Thumbs down button - show if custom slot provided OR if handler available at top level -->
@if (
thumbsDownButtonComponent() ||
thumbsDownButtonTemplate ||
handlers.hasAssistantThumbsDownHandler()
) {
<copilot-slot
[slot]="thumbsDownButtonTemplate || thumbsDownButtonComponent()"
[context]="{}"
[defaultComponent]="defaultThumbsDownButtonComponent"
[outputs]="thumbsDownButtonOutputs"
>
</copilot-slot>
}
<!-- Read aloud button - only show if custom slot provided -->
@if (readAloudButtonComponent() || readAloudButtonTemplate) {
<copilot-slot
[slot]="readAloudButtonTemplate || readAloudButtonComponent()"
[context]="{}"
[outputs]="readAloudButtonOutputs"
>
</copilot-slot>
}
<!-- Regenerate button - only show if custom slot provided -->
@if (regenerateButtonComponent() || regenerateButtonTemplate) {
<copilot-slot
[slot]="regenerateButtonTemplate || regenerateButtonComponent()"
[context]="{}"
[outputs]="regenerateButtonOutputs"
>
</copilot-slot>
}
<!-- Additional toolbar items -->
@if (additionalToolbarItems()) {
<ng-container
[ngTemplateOutlet]="additionalToolbarItems() || null"
></ng-container>
}
</div>
</div>
}
}
</div>
`,
styles: [
`
/* Import KaTeX styles */
@import "katex/dist/katex.min.css";
:host {
display: block;
width: 100%;
}
/* Atom One Light theme for highlight.js */
.hljs {
color: rgb(56, 58, 66);
background: transparent;
}
.hljs-comment,
.hljs-quote {
color: #a0a1a7;
font-style: italic;
}
.hljs-doctag,
.hljs-formula,
.hljs-keyword {
color: #a626a4;
}
.hljs-deletion,
.hljs-name,
.hljs-section,
.hljs-selector-tag,
.hljs-subst {
color: #e45649;
}
.hljs-literal {
color: #0184bb;
}
.hljs-addition,
.hljs-attribute,
.hljs-meta .hljs-string,
.hljs-regexp,
.hljs-string {
color: #50a14f;
}
.hljs-attr,
.hljs-number,
.hljs-selector-attr,
.hljs-selector-class,
.hljs-selector-pseudo,
.hljs-template-variable,
.hljs-type,
.hljs-variable {
color: #986801;
}
.hljs-params {
color: rgb(56, 58, 66);
}
.hljs-bullet,
.hljs-link,
.hljs-meta,
.hljs-selector-id,
.hljs-symbol,
.hljs-title {
color: #4078f2;
}
.hljs-built_in,
.hljs-class .hljs-title,
.hljs-title.class_ {
color: #c18401;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: 700;
}
.hljs-link {
text-decoration: underline;
}
/* Atom One Dark theme for highlight.js */
.dark .hljs {
color: #abb2bf;
background: transparent;
}
.dark .hljs-comment,
.dark .hljs-quote {
color: #5c6370;
font-style: italic;
}
.dark .hljs-doctag,
.dark .hljs-formula,
.dark .hljs-keyword {
color: #c678dd;
}
.dark .hljs-deletion,
.dark .hljs-name,
.dark .hljs-section,
.dark .hljs-selector-tag,
.dark .hljs-subst {
color: #e06c75;
}
.dark .hljs-literal {
color: #56b6c2;
}
.dark .hljs-addition,
.dark .hljs-attribute,
.dark .hljs-meta .hljs-string,
.dark .hljs-regexp,
.dark .hljs-string {
color: #98c379;
}
.dark .hljs-attr,
.dark .hljs-number,
.dark .hljs-selector-attr,
.dark .hljs-selector-class,
.dark .hljs-selector-pseudo,
.dark .hljs-template-variable,
.dark .hljs-type,
.dark .hljs-variable {
color: #d19a66;
}
.dark .hljs-bullet,
.dark .hljs-link,
.dark .hljs-meta,
.dark .hljs-selector-id,
.dark .hljs-symbol,
.dark .hljs-title {
color: #61aeee;
}
.dark .hljs-built_in,
.dark .hljs-class .hljs-title,
.dark .hljs-title.class_ {
color: #e6c07b;
}
.dark .hljs-params {
color: #abb2bf; /* same as regular text */
}
.dark .hljs-emphasis {
font-style: italic;
}
.dark .hljs-strong {
font-weight: 700;
}
.dark .hljs-link {
text-decoration: underline;
}
`,
],
})
export class CopilotChatAssistantMessage {
// Capture templates from content projection
@ContentChild("markdownRenderer", { read: TemplateRef })
markdownRendererTemplate?: TemplateRef<AssistantMessageMarkdownRendererContext>;
@ContentChild("toolbar", { read: TemplateRef })
toolbarTemplate?: TemplateRef<AssistantMessageToolbarContext>;
@ContentChild("copyButton", { read: TemplateRef })
copyButtonTemplate?: TemplateRef<AssistantMessageCopyButtonContext>;
@ContentChild("thumbsUpButton", { read: TemplateRef })
thumbsUpButtonTemplate?: TemplateRef<ThumbsUpButtonContext>;
@ContentChild("thumbsDownButton", { read: TemplateRef })
thumbsDownButtonTemplate?: TemplateRef<ThumbsDownButtonContext>;
@ContentChild("readAloudButton", { read: TemplateRef })
readAloudButtonTemplate?: TemplateRef<ReadAloudButtonContext>;
@ContentChild("regenerateButton", { read: TemplateRef })
regenerateButtonTemplate?: TemplateRef<RegenerateButtonContext>;
@ContentChild("toolCallsView", { read: TemplateRef })
toolCallsViewTemplate?: TemplateRef<any>;
// Class inputs for styling default components
readonly markdownRendererClass = input<string | undefined>(undefined);
readonly toolbarClass = input<string | undefined>(undefined);
readonly copyButtonClass = input<string | undefined>(undefined);
readonly thumbsUpButtonClass = input<string | undefined>(undefined);
readonly thumbsDownButtonClass = input<string | undefined>(undefined);
readonly readAloudButtonClass = input<string | undefined>(undefined);
readonly regenerateButtonClass = input<string | undefined>(undefined);
readonly toolCallsViewClass = input<string | undefined>(undefined);
// Component inputs for overrides
readonly markdownRendererComponent = input<Type<any> | undefined>(undefined);
readonly toolbarComponent = input<Type<any> | undefined>(undefined);
readonly copyButtonComponent = input<Type<any> | undefined>(undefined);
readonly thumbsUpButtonComponent = input<Type<any> | undefined>(undefined);
readonly thumbsDownButtonComponent = input<Type<any> | undefined>(undefined);
readonly readAloudButtonComponent = input<Type<any> | undefined>(undefined);
readonly regenerateButtonComponent = input<Type<any> | undefined>(undefined);
readonly toolCallsViewComponent = input<Type<any> | undefined>(undefined);
// Regular inputs
readonly message = input.required<AssistantMessage>();
readonly messages = input<Message[]>([]);
readonly isLoading = input<boolean>(false);
readonly additionalToolbarItems = input<TemplateRef<any> | undefined>(
undefined,
);
readonly toolbarVisible = input<boolean>(true);
readonly inputClass = input<string | undefined>(undefined);
// DI service exposes handler availability scoped to CopilotChatView
// Make it optional with a default fallback for testing
handlers: CopilotChatViewHandlers;
constructor(
@Optional()
@Inject(CopilotChatViewHandlers)
handlers?: CopilotChatViewHandlers | null,
) {
this.handlers = handlers || new CopilotChatViewHandlers();
}
// Output events
thumbsUp = output<CopilotChatAssistantMessageOnThumbsUpProps>();
thumbsDown = output<CopilotChatAssistantMessageOnThumbsDownProps>();
readAloud = output<CopilotChatAssistantMessageOnReadAloudProps>();
regenerate = output<CopilotChatAssistantMessageOnRegenerateProps>();
// Signals
customClass = signal<string | undefined>(undefined);
// Computed values
computedClass = computed(() => {
return cn(
"prose max-w-full break-words dark:prose-invert",
this.customClass(),
);
});
// Default components
protected readonly defaultThumbsUpButtonComponent =
CopilotChatAssistantMessageThumbsUpButton;
protected readonly defaultThumbsDownButtonComponent =
CopilotChatAssistantMessageThumbsDownButton;
protected readonly CopilotChatAssistantMessageRenderer =
CopilotChatAssistantMessageRenderer;
protected readonly CopilotChatAssistantMessageToolbar =
CopilotChatAssistantMessageToolbar;
protected readonly CopilotChatAssistantMessageCopyButton =
CopilotChatAssistantMessageCopyButton;
protected readonly CopilotChatToolCallsView = CopilotChatToolCallsView;
// Context for slots (reactive via signals)
markdownRendererContext = computed<AssistantMessageMarkdownRendererContext>(
() => ({
content: this.message()?.content || "",
}),
);
// Output maps for slots
copyButtonOutputs = { clicked: () => this.handleCopy() };
thumbsUpButtonOutputs = { clicked: () => this.handleThumbsUp() };
thumbsDownButtonOutputs = { clicked: () => this.handleThumbsDown() };
readAloudButtonOutputs = { clicked: () => this.handleReadAloud() };
regenerateButtonOutputs = { clicked: () => this.handleRegenerate() };
toolbarContext = computed<AssistantMessageToolbarContext>(() => ({
children: null, // Will be populated by the toolbar content
}));
// Return true if assistant message has non-empty text content
hasMessageContent(): boolean {
const raw = (this.message()?.content ?? "") as any;
const content = typeof raw === "string" ? raw : String(raw ?? "");
return content.trim().length > 0;
}
toolCallsViewContext = computed(() => ({
message: this.message(),
messages: this.messages(),
isLoading: this.isLoading(),
}));
handleCopy(): void {
// Copy is handled by the button component itself
// This is just for any additional logic if needed
}
handleThumbsUp(): void {
this.thumbsUp.emit({ message: this.message() });
}
handleThumbsDown(): void {
this.thumbsDown.emit({ message: this.message() });
}
handleReadAloud(): void {
this.readAloud.emit({ message: this.message() });
}
handleRegenerate(): void {
this.regenerate.emit({ message: this.message() });
}
}